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 static char *get_leaks(const char *output, char **errs)
54 char *leaks = talloc_strdup(output, "");
56 char **lines = strsplit(output, output, "\n");
58 *errs = talloc_strdup(output, "");
59 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
60 if (strstr(lines[i], " lost ")) {
62 if (strstr(lines[i], " definitely lost ")) {
63 /* Definite leak, report. */
64 while (lines[i] && !blank_line(lines[i])) {
65 leaks = talloc_append_string(leaks,
67 leaks = talloc_append_string(leaks,
72 /* Not definite, ignore. */
73 while (lines[i] && !blank_line(lines[i]))
77 while (lines[i] && !blank_line(lines[i])) {
78 *errs = talloc_append_string(*errs, lines[i]);
79 *errs = talloc_append_string(*errs, "\n");
95 /* FIXME: Run examples, too! */
96 static void do_run_tests_vg(struct manifest *m,
98 unsigned int *timeleft,
102 struct list_head *list;
105 /* This is slow, so we run once but grab leak info. */
107 foreach_ptr(list, &m->run_tests, &m->api_tests) {
108 list_for_each(list, i, list) {
111 /* FIXME: Valgrind's output sucks. XML is unreadable by
112 * humans, and you can't have both. */
113 run_command(score, timeleft, &cmdout,
114 "valgrind -q --error-exitcode=101"
115 " --child-silent-after-fork=yes"
119 tests_pass_valgrind.options ?
120 tests_pass_valgrind.options : "",
122 output = grab_file(i, "valgrind.log", NULL);
123 if (!output || output[0] == '\0') {
127 i->leak_info = get_leaks(output, &err);
130 score_file_error(score, i, 0, err);
136 if (score->score == score->total)
140 static void do_leakcheck_vg(struct manifest *m,
142 unsigned int *timeleft,
146 struct list_head *list;
149 foreach_ptr(list, &m->run_tests, &m->api_tests) {
150 list_for_each(list, i, list) {
152 score_file_error(score, i, 0, i->leak_info);
164 /* Gcc's warn_unused_result is fascist bullshit. */
165 #define doesnt_matter()
167 static void run_under_debugger_vg(struct manifest *m, struct score *score)
169 struct file_error *first;
172 if (!ask("Should I run the first failing test under the debugger?"))
175 first = list_top(&score->per_file_errors, struct file_error, list);
176 command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
177 tests_pass_valgrind.options ?
178 tests_pass_valgrind.options : "",
179 first->file->compiled);
184 struct ccanlint tests_pass_valgrind = {
185 .key = "tests_pass_valgrind",
186 .name = "Module's run and api tests succeed under valgrind",
187 .can_run = can_run_vg,
188 .check = do_run_tests_vg,
189 .handle = run_under_debugger_vg,
190 .takes_options = true,
191 .needs = "tests_pass"
194 struct ccanlint tests_pass_valgrind_noleaks = {
195 .key = "tests_pass_valgrind_noleaks",
196 .name = "Module's run and api tests have no memory leaks",
197 .check = do_leakcheck_vg,
198 .needs = "tests_pass_valgrind"
201 REGISTER_TEST(tests_pass_valgrind_noleaks);