#include <tools/ccanlint/ccanlint.h>
#include <tools/tools.h>
-#include <ccan/talloc/talloc.h>
#include <ccan/str/str.h>
+#include <ccan/take/take.h>
#include <ccan/foreach/foreach.h>
-#include <ccan/grab_file/grab_file.h>
-#include <ccan/str_talloc/str_talloc.h>
+#include <ccan/tal/grab_file/grab_file.h>
+#include "tests_pass.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
-REGISTER_TEST(tests_pass_valgrind);
-
/* Note: we already test safe_mode in run_tests.c */
static const char *can_run_vg(struct manifest *m)
{
- unsigned int timeleft = default_timeout_ms;
- char *output;
-
- if (!run_command(m, &timeleft, &output,
- "valgrind -q --error-exitcode=0 true"))
- return talloc_asprintf(m, "No valgrind support: %s", output);
+ if (!do_valgrind)
+ return tal_fmt(m, "No valgrind support");
return NULL;
}
+static void do_leakcheck_vg(struct manifest *m,
+ unsigned int *timeleft,
+ struct score *score);
+
+static struct ccanlint tests_pass_valgrind_noleaks = {
+ .key = "tests_pass_valgrind_noleaks",
+ .name = "Module's run and api tests have no memory leaks",
+ .check = do_leakcheck_vg,
+ .takes_options = true,
+ .needs = "tests_pass_valgrind"
+};
+REGISTER_TEST(tests_pass_valgrind_noleaks);
+
+
/* Example output:
==2749== Conditional jump or move depends on uninitialised value(s)
==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
return line[strspn(line, "=0123456789 ")] == '\0';
}
-static char *get_leaks(const char *output, char **errs)
+/* Removes matching lines from lines array, returns them. FIXME: Hacky. */
+static char **extract_matching(const char *prefix, char *lines[])
{
- char *leaks = talloc_strdup(output, "");
+ unsigned int i, num_ret = 0;
+ char **ret = tal_arr(lines, char *, tal_count(lines));
+
+ for (i = 0; i < tal_count(lines) - 1; i++) {
+ if (strstarts(lines[i], prefix)) {
+ ret[num_ret++] = tal_strdup(ret, lines[i]);
+ lines[i] = (char *)"";
+ }
+ }
+ ret[num_ret++] = NULL;
+
+ /* Make sure tal_count is correct! */
+ tal_resize(&ret, num_ret);
+ return ret;
+}
+
+static char *get_leaks(char *lines[], char **errs)
+{
+ char *leaks = tal_strdup(lines, "");
unsigned int i;
- char **lines = strsplit(output, output, "\n");
- *errs = talloc_strdup(output, "");
- for (i = 0; i < talloc_array_length(lines) - 1; i++) {
+ for (i = 0; i < tal_count(lines) - 1; i++) {
if (strstr(lines[i], " lost ")) {
/* A leak... */
if (strstr(lines[i], " definitely lost ")) {
/* Definite leak, report. */
while (lines[i] && !blank_line(lines[i])) {
- leaks = talloc_append_string(leaks,
- lines[i]);
- leaks = talloc_append_string(leaks,
- "\n");
+ tal_append_fmt(&leaks, "%s\n",
+ lines[i]);
i++;
}
} else
} else {
/* A real error. */
while (lines[i] && !blank_line(lines[i])) {
- *errs = talloc_append_string(*errs, lines[i]);
- *errs = talloc_append_string(*errs, "\n");
+ if (!*errs)
+ *errs = tal_fmt(NULL, "%s\n", lines[i]);
+ else
+ tal_append_fmt(errs, "%s\n", lines[i]);
i++;
}
}
}
+ return leaks;
+}
+
+/* Returns leaks, and sets errs[] */
+static char *analyze_output(const char *output, char **errs)
+{
+ char *leaks = tal_strdup(output, "");
+ unsigned int i;
+ char **lines = tal_strsplit(output, output, "\n", STR_EMPTY_OK);
+
+ *errs = tal_strdup(output, "");
+ for (i = 0; i < tal_count(lines) - 1; i++) {
+ unsigned int preflen = strspn(lines[i], "=0123456789");
+ char *prefix, **sublines;
+
+ /* Ignore erased lines, or weird stuff. */
+ if (preflen == 0)
+ continue;
+
+ prefix = tal_strndup(output, lines[i], preflen);
+ sublines = extract_matching(prefix, lines);
+
+ leaks = tal_strcat(output, take(leaks),
+ take(get_leaks(sublines, errs)));
+ }
+
if (!leaks[0]) {
- talloc_free(leaks);
+ tal_free(leaks);
leaks = NULL;
}
if (!(*errs)[0]) {
- talloc_free(*errs);
+ tal_free(*errs);
*errs = NULL;
}
return leaks;
}
+static const char *concat(struct score *score, char *bits[])
+{
+ unsigned int i;
+ char *ret = tal_strdup(score, "");
+
+ for (i = 0; bits[i]; i++) {
+ if (i)
+ ret = tal_strcat(score, take(ret), " ");
+ ret = tal_strcat(score, take(ret), bits[i]);
+ }
+ return ret;
+}
+
/* FIXME: Run examples, too! */
static void do_run_tests_vg(struct manifest *m,
- bool keep,
- unsigned int *timeleft,
+ unsigned int *timeleft UNNEEDED,
struct score *score)
{
struct ccan_file *i;
struct list_head *list;
- char *cmdout;
/* This is slow, so we run once but grab leak info. */
score->total = 0;
+ score->pass = true;
foreach_ptr(list, &m->run_tests, &m->api_tests) {
list_for_each(list, i, list) {
- char *output, *err;
+ char *err, *output;
+ const char *options;
+
score->total++;
- /* FIXME: Valgrind's output sucks. XML is unreadable by
- * humans, and you can't have both. */
- run_command(score, timeleft, &cmdout,
- "valgrind -q --error-exitcode=101"
- " --child-silent-after-fork=yes"
- " --leak-check=full"
- " --log-fd=3 %s %s"
- " 3> valgrind.log",
- tests_pass_valgrind.options ?
- tests_pass_valgrind.options : "",
- i->compiled);
- output = grab_file(i, "valgrind.log", NULL);
+ options = concat(score,
+ per_file_options(&tests_pass_valgrind,
+ i));
+ if (streq(options, "FAIL")) {
+ i->leak_info = NULL;
+ continue;
+ }
+
+ output = grab_file(i, i->valgrind_log);
+ /* No valgrind errors? */
if (!output || output[0] == '\0') {
err = NULL;
i->leak_info = NULL;
} else {
- i->leak_info = get_leaks(output, &err);
+ i->leak_info = analyze_output(output, &err);
}
- if (err)
+ if (err) {
score_file_error(score, i, 0, "%s", err);
- else
+ score->pass = false;
+ } else
score->score++;
}
}
-
- if (score->score == score->total)
- score->pass = true;
}
static void do_leakcheck_vg(struct manifest *m,
- bool keep,
- unsigned int *timeleft,
+ unsigned int *timeleft UNNEEDED,
struct score *score)
{
struct ccan_file *i;
struct list_head *list;
+ char **options;
bool leaks = false;
foreach_ptr(list, &m->run_tests, &m->api_tests) {
list_for_each(list, i, list) {
+ options = per_file_options(&tests_pass_valgrind_noleaks,
+ i);
+ if (options[0]) {
+ if (streq(options[0], "FAIL")) {
+ leaks = true;
+ continue;
+ }
+ errx(1, "Unknown leakcheck options '%s'",
+ options[0]);
+ }
+
if (i->leak_info) {
score_file_error(score, i, 0, "%s",
i->leak_info);
}
}
+ /* FIXME: We don't fail for this, since many tests leak. */
+ score->pass = true;
if (!leaks) {
score->score = 1;
- score->pass = true;
}
}
struct file_error *first;
char *command;
+ /* Don't ask anything if they suppressed tests. */
+ if (score->pass)
+ return;
+
if (!ask("Should I run the first failing test under the debugger?"))
return;
first = list_top(&score->per_file_errors, struct file_error, list);
- command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
- tests_pass_valgrind.options ?
- tests_pass_valgrind.options : "",
- first->file->compiled);
- if (system(command))
+ command = tal_fmt(m, "valgrind --leak-check=full --db-attach=yes%s %s %s",
+ concat(score, per_file_options(&tests_pass_valgrind,
+ first->file)),
+ valgrind_suppress, first->file->compiled[COMPILE_NORMAL]);
+ if (system(command)) {
doesnt_matter();
+ }
}
struct ccanlint tests_pass_valgrind = {
.takes_options = true,
.needs = "tests_pass"
};
-
-struct ccanlint tests_pass_valgrind_noleaks = {
- .key = "tests_pass_valgrind_noleaks",
- .name = "Module's run and api tests have no memory leaks",
- .check = do_leakcheck_vg,
- .needs = "tests_pass_valgrind"
-};
-
-REGISTER_TEST(tests_pass_valgrind_noleaks);
+REGISTER_TEST(tests_pass_valgrind);