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 /* Note: we already test safe_mode in run_tests.c */
21 static const char *can_run_vg(struct manifest *m)
23 unsigned int timeleft = default_timeout_ms;
26 if (!run_command(m, &timeleft, &output,
27 "valgrind -q --error-exitcode=0 true"))
28 return talloc_asprintf(m, "No valgrind support: %s", output);
33 ==2749== Conditional jump or move depends on uninitialised value(s)
34 ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
35 ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
36 ==2749== by 0x408EACF: printf (printf.c:35)
37 ==2749== by 0x8048465: main (in /tmp/foo)
39 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
40 ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
41 ==2749== by 0x8048444: main (in /tmp/foo)
45 static bool blank_line(const char *line)
47 return line[strspn(line, "=0123456789 ")] == '\0';
50 static char *get_leaks(const char *output, char **errs)
52 char *leaks = talloc_strdup(output, "");
54 char **lines = strsplit(output, output, "\n", &num);
56 *errs = talloc_strdup(output, "");
57 for (i = 0; i < num; i++) {
58 if (strstr(lines[i], " lost ")) {
60 if (strstr(lines[i], " definitely lost ")) {
61 /* Definite leak, report. */
62 while (lines[i] && !blank_line(lines[i])) {
63 leaks = talloc_append_string(leaks,
65 leaks = talloc_append_string(leaks,
70 /* Not definite, ignore. */
71 while (lines[i] && !blank_line(lines[i]))
75 while (lines[i] && !blank_line(lines[i])) {
76 *errs = talloc_append_string(*errs, lines[i]);
77 *errs = talloc_append_string(*errs, "\n");
93 /* FIXME: Run examples, too! */
94 static void do_run_tests_vg(struct manifest *m,
96 unsigned int *timeleft,
100 struct list_head *list;
103 /* This is slow, so we run once but grab leak info. */
105 foreach_ptr(list, &m->run_tests, &m->api_tests) {
106 list_for_each(list, i, list) {
109 /* FIXME: Valgrind's output sucks. XML is unreadable by
110 * humans, and you can't have both. */
111 run_command(score, timeleft, &cmdout,
112 "valgrind -q --error-exitcode=101"
116 run_tests_vg.options ?
117 run_tests_vg.options : "",
119 output = grab_file(i, "valgrind.log", NULL);
120 if (!output || output[0] == '\0') {
123 i->leak_info = get_leaks(output, &err);
126 score_file_error(score, i, 0, err);
132 if (score->score == score->total)
136 static void do_leakcheck_vg(struct manifest *m,
138 unsigned int *timeleft,
142 struct list_head *list;
145 foreach_ptr(list, &m->run_tests, &m->api_tests) {
146 list_for_each(list, i, list) {
148 score_file_error(score, i, 0, i->leak_info);
160 /* Gcc's warn_unused_result is fascist bullshit. */
161 #define doesnt_matter()
163 static void run_under_debugger_vg(struct manifest *m, struct score *score)
165 struct file_error *first;
168 if (!ask("Should I run the first failing test under the debugger?"))
171 first = list_top(&score->per_file_errors, struct file_error, list);
172 command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
173 run_tests_vg.options ?
174 run_tests_vg.options : "",
175 first->file->compiled);
180 struct ccanlint run_tests_vg = {
181 .key = "tests_pass_valgrind",
182 .name = "Module's run and api tests succeed under valgrind",
183 .can_run = can_run_vg,
184 .check = do_run_tests_vg,
185 .handle = run_under_debugger_vg,
186 .takes_options = true
189 REGISTER_TEST(run_tests_vg, &run_tests, NULL);
191 struct ccanlint run_tests_vg_leak = {
192 .key = "tests_pass_valgrind_noleaks",
193 .name = "Module's run and api tests leak memory",
194 .check = do_leakcheck_vg,
197 REGISTER_TEST(run_tests_vg_leak, &run_tests_vg, NULL);