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 /* FIXME: Run examples, too! */
137 static void do_run_tests_vg(struct manifest *m,
139 unsigned int *timeleft,
143 struct list_head *list;
146 /* This is slow, so we run once but grab leak info. */
148 foreach_ptr(list, &m->run_tests, &m->api_tests) {
149 list_for_each(list, i, list) {
150 char *output, *err, *log;
154 /* FIXME: Valgrind's output sucks. XML is unreadable by
155 * humans *and* doesn't support children reporting. */
156 log = talloc_asprintf(score,
157 "%s.valgrind-log", i->compiled);
159 talloc_set_destructor(log,
160 unlink_file_destructor);
162 pass = run_command(score, timeleft, &cmdout,
163 "valgrind -q --error-exitcode=101"
167 tests_pass_valgrind.options ?
168 tests_pass_valgrind.options : "",
170 output = grab_file(i, log, NULL);
171 /* No valgrind errors? Expect it to pass... */
172 if (!output || output[0] == '\0') {
174 err = talloc_asprintf(score,
182 i->leak_info = analyze_output(output, &err);
185 score_file_error(score, i, 0, "%s", err);
191 if (score->score == score->total)
195 static void do_leakcheck_vg(struct manifest *m,
197 unsigned int *timeleft,
201 struct list_head *list;
204 foreach_ptr(list, &m->run_tests, &m->api_tests) {
205 list_for_each(list, i, list) {
207 score_file_error(score, i, 0, "%s",
214 /* FIXME: We don't fail for this, since many tests leak. */
221 /* Gcc's warn_unused_result is fascist bullshit. */
222 #define doesnt_matter()
224 static void run_under_debugger_vg(struct manifest *m, struct score *score)
226 struct file_error *first;
229 if (!ask("Should I run the first failing test under the debugger?"))
232 first = list_top(&score->per_file_errors, struct file_error, list);
233 command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
234 tests_pass_valgrind.options ?
235 tests_pass_valgrind.options : "",
236 first->file->compiled);
241 struct ccanlint tests_pass_valgrind = {
242 .key = "tests_pass_valgrind",
243 .name = "Module's run and api tests succeed under valgrind",
244 .can_run = can_run_vg,
245 .check = do_run_tests_vg,
246 .handle = run_under_debugger_vg,
247 .takes_options = true,
248 .needs = "tests_pass"
251 struct ccanlint tests_pass_valgrind_noleaks = {
252 .key = "tests_pass_valgrind_noleaks",
253 .name = "Module's run and api tests have no memory leaks",
254 .check = do_leakcheck_vg,
255 .needs = "tests_pass_valgrind"
258 REGISTER_TEST(tests_pass_valgrind_noleaks);