1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/foreach/foreach.h>
6 #include <ccan/grab_file/grab_file.h>
7 #include <ccan/str_talloc/str_talloc.h>
20 REGISTER_TEST(tests_pass_valgrind);
22 /* Note: we already test safe_mode in run_tests.c */
23 static const char *can_run_vg(struct manifest *m)
25 unsigned int timeleft = default_timeout_ms;
28 if (!run_command(m, &timeleft, &output,
29 "valgrind -q --error-exitcode=0 true"))
30 return talloc_asprintf(m, "No valgrind support: %s", output);
35 ==2749== Conditional jump or move depends on uninitialised value(s)
36 ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
37 ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
38 ==2749== by 0x408EACF: printf (printf.c:35)
39 ==2749== by 0x8048465: main (in /tmp/foo)
41 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
42 ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
43 ==2749== by 0x8048444: main (in /tmp/foo)
47 static bool blank_line(const char *line)
49 return line[strspn(line, "=0123456789 ")] == '\0';
52 /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
53 static char **extract_matching(const char *prefix, char *lines[])
55 unsigned int i, num_ret = 0;
56 char **ret = talloc_array(lines, char *, talloc_array_length(lines));
58 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
59 if (strstarts(lines[i], prefix)) {
60 ret[num_ret++] = talloc_steal(ret, lines[i]);
61 lines[i] = (char *)"";
64 ret[num_ret++] = NULL;
66 /* Make sure length is correct! */
67 return talloc_realloc(NULL, ret, char *, num_ret);
70 static char *get_leaks(char *lines[], char **errs)
72 char *leaks = talloc_strdup(lines, "");
75 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
76 if (strstr(lines[i], " lost ")) {
78 if (strstr(lines[i], " definitely lost ")) {
79 /* Definite leak, report. */
80 while (lines[i] && !blank_line(lines[i])) {
81 leaks = talloc_append_string(leaks,
83 leaks = talloc_append_string(leaks,
88 /* Not definite, ignore. */
89 while (lines[i] && !blank_line(lines[i]))
93 while (lines[i] && !blank_line(lines[i])) {
94 *errs = talloc_append_string(*errs, lines[i]);
95 *errs = talloc_append_string(*errs, "\n");
103 /* Returns leaks, and sets errs[] */
104 static char *analyze_output(const char *output, char **errs)
106 char *leaks = talloc_strdup(output, "");
108 char **lines = strsplit(output, output, "\n");
110 *errs = talloc_strdup(output, "");
111 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
112 unsigned int preflen = strspn(lines[i], "=0123456789");
113 char *prefix, **sublines;
115 /* Ignore erased lines, or weird stuff. */
119 prefix = talloc_strndup(output, lines[i], preflen);
120 sublines = extract_matching(prefix, lines);
122 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
136 static const char *concat(struct score *score, char *bits[])
139 char *ret = talloc_strdup(score, "");
141 for (i = 0; bits[i]; i++) {
143 ret = talloc_append_string(ret, " ");
144 ret = talloc_append_string(ret, bits[i]);
149 /* FIXME: Run examples, too! */
150 static void do_run_tests_vg(struct manifest *m,
152 unsigned int *timeleft,
156 struct list_head *list;
159 /* This is slow, so we run once but grab leak info. */
162 foreach_ptr(list, &m->run_tests, &m->api_tests) {
163 list_for_each(list, i, list) {
164 char *output, *err, *log;
169 options = concat(score,
170 per_file_options(&tests_pass_valgrind,
172 if (streq(options, "FAIL"))
175 /* FIXME: Valgrind's output sucks. XML is unreadable by
176 * humans *and* doesn't support children reporting. */
177 log = talloc_asprintf(score,
178 "%s.valgrind-log", i->compiled);
180 talloc_set_destructor(log,
181 unlink_file_destructor);
183 pass = run_command(score, timeleft, &cmdout,
184 "valgrind -q --error-exitcode=101"
190 output = grab_file(i, log, NULL);
191 /* No valgrind errors? Expect it to pass... */
192 if (!output || output[0] == '\0') {
194 err = talloc_asprintf(score,
202 i->leak_info = analyze_output(output, &err);
205 score_file_error(score, i, 0, "%s", err);
213 static void do_leakcheck_vg(struct manifest *m,
215 unsigned int *timeleft,
219 struct list_head *list;
222 foreach_ptr(list, &m->run_tests, &m->api_tests) {
223 list_for_each(list, i, list) {
225 score_file_error(score, i, 0, "%s",
232 /* FIXME: We don't fail for this, since many tests leak. */
239 /* Gcc's warn_unused_result is fascist bullshit. */
240 #define doesnt_matter()
242 static void run_under_debugger_vg(struct manifest *m, struct score *score)
244 struct file_error *first;
247 /* Don't ask anything if they suppressed tests. */
251 if (!ask("Should I run the first failing test under the debugger?"))
254 first = list_top(&score->per_file_errors, struct file_error, list);
255 command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
257 per_file_options(&tests_pass_valgrind,
259 first->file->compiled);
264 struct ccanlint tests_pass_valgrind = {
265 .key = "tests_pass_valgrind",
266 .name = "Module's run and api tests succeed under valgrind",
267 .can_run = can_run_vg,
268 .check = do_run_tests_vg,
269 .handle = run_under_debugger_vg,
270 .takes_options = true,
271 .needs = "tests_pass"
274 struct ccanlint tests_pass_valgrind_noleaks = {
275 .key = "tests_pass_valgrind_noleaks",
276 .name = "Module's run and api tests have no memory leaks",
277 .check = do_leakcheck_vg,
278 .needs = "tests_pass_valgrind"
281 REGISTER_TEST(tests_pass_valgrind_noleaks);