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);
21 REGISTER_TEST(tests_pass_valgrind_noleaks);
23 /* Note: we already test safe_mode in run_tests.c */
24 static const char *can_run_vg(struct manifest *m)
26 unsigned int timeleft = default_timeout_ms;
29 if (!run_command(m, &timeleft, &output,
30 "valgrind -q --error-exitcode=0 true"))
31 return talloc_asprintf(m, "No valgrind support: %s", output);
36 ==2749== Conditional jump or move depends on uninitialised value(s)
37 ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
38 ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
39 ==2749== by 0x408EACF: printf (printf.c:35)
40 ==2749== by 0x8048465: main (in /tmp/foo)
42 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
43 ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
44 ==2749== by 0x8048444: main (in /tmp/foo)
48 static bool blank_line(const char *line)
50 return line[strspn(line, "=0123456789 ")] == '\0';
53 /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
54 static char **extract_matching(const char *prefix, char *lines[])
56 unsigned int i, num_ret = 0;
57 char **ret = talloc_array(lines, char *, talloc_array_length(lines));
59 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
60 if (strstarts(lines[i], prefix)) {
61 ret[num_ret++] = talloc_steal(ret, lines[i]);
62 lines[i] = (char *)"";
65 ret[num_ret++] = NULL;
67 /* Make sure length is correct! */
68 return talloc_realloc(NULL, ret, char *, num_ret);
71 static char *get_leaks(char *lines[], char **errs)
73 char *leaks = talloc_strdup(lines, "");
76 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
77 if (strstr(lines[i], " lost ")) {
79 if (strstr(lines[i], " definitely lost ")) {
80 /* Definite leak, report. */
81 while (lines[i] && !blank_line(lines[i])) {
82 leaks = talloc_append_string(leaks,
84 leaks = talloc_append_string(leaks,
89 /* Not definite, ignore. */
90 while (lines[i] && !blank_line(lines[i]))
94 while (lines[i] && !blank_line(lines[i])) {
95 *errs = talloc_append_string(*errs, lines[i]);
96 *errs = talloc_append_string(*errs, "\n");
104 /* Returns leaks, and sets errs[] */
105 static char *analyze_output(const char *output, char **errs)
107 char *leaks = talloc_strdup(output, "");
109 char **lines = strsplit(output, output, "\n");
111 *errs = talloc_strdup(output, "");
112 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
113 unsigned int preflen = strspn(lines[i], "=0123456789");
114 char *prefix, **sublines;
116 /* Ignore erased lines, or weird stuff. */
120 prefix = talloc_strndup(output, lines[i], preflen);
121 sublines = extract_matching(prefix, lines);
123 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
137 static const char *concat(struct score *score, char *bits[])
140 char *ret = talloc_strdup(score, "");
142 for (i = 0; bits[i]; i++) {
144 ret = talloc_append_string(ret, " ");
145 ret = talloc_append_string(ret, bits[i]);
150 /* FIXME: Run examples, too! */
151 static void do_run_tests_vg(struct manifest *m,
153 unsigned int *timeleft,
157 struct list_head *list;
160 /* This is slow, so we run once but grab leak info. */
163 foreach_ptr(list, &m->run_tests, &m->api_tests) {
164 list_for_each(list, i, list) {
165 char *output, *err, *log;
170 options = concat(score,
171 per_file_options(&tests_pass_valgrind,
173 if (streq(options, "FAIL"))
176 /* FIXME: Valgrind's output sucks. XML is unreadable by
177 * humans *and* doesn't support children reporting. */
178 log = talloc_asprintf(score,
179 "%s.valgrind-log", i->compiled);
181 talloc_set_destructor(log,
182 unlink_file_destructor);
184 pass = run_command(score, timeleft, &cmdout,
185 "valgrind -q --error-exitcode=101"
191 output = grab_file(i, log, NULL);
192 /* No valgrind errors? Expect it to pass... */
193 if (!output || output[0] == '\0') {
195 err = talloc_asprintf(score,
203 i->leak_info = analyze_output(output, &err);
206 score_file_error(score, i, 0, "%s", err);
214 static void do_leakcheck_vg(struct manifest *m,
216 unsigned int *timeleft,
220 struct list_head *list;
224 foreach_ptr(list, &m->run_tests, &m->api_tests) {
225 list_for_each(list, i, list) {
226 options = per_file_options(&tests_pass_valgrind_noleaks,
229 if (streq(options[0], "FAIL")) {
233 errx(1, "Unknown leakcheck options '%s'",
238 score_file_error(score, i, 0, "%s",
245 /* FIXME: We don't fail for this, since many tests leak. */
252 /* Gcc's warn_unused_result is fascist bullshit. */
253 #define doesnt_matter()
255 static void run_under_debugger_vg(struct manifest *m, struct score *score)
257 struct file_error *first;
260 /* Don't ask anything if they suppressed tests. */
264 if (!ask("Should I run the first failing test under the debugger?"))
267 first = list_top(&score->per_file_errors, struct file_error, list);
268 command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
270 per_file_options(&tests_pass_valgrind,
272 first->file->compiled);
277 struct ccanlint tests_pass_valgrind = {
278 .key = "tests_pass_valgrind",
279 .name = "Module's run and api tests succeed under valgrind",
280 .can_run = can_run_vg,
281 .check = do_run_tests_vg,
282 .handle = run_under_debugger_vg,
283 .takes_options = true,
284 .needs = "tests_pass"
287 struct ccanlint tests_pass_valgrind_noleaks = {
288 .key = "tests_pass_valgrind_noleaks",
289 .name = "Module's run and api tests have no memory leaks",
290 .check = do_leakcheck_vg,
291 .takes_options = true,
292 .needs = "tests_pass_valgrind"