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>
8 #include "tests_pass.h"
21 REGISTER_TEST(tests_pass_valgrind);
22 REGISTER_TEST(tests_pass_valgrind_noleaks);
24 /* Note: we already test safe_mode in run_tests.c */
25 static const char *can_run_vg(struct manifest *m)
28 return talloc_asprintf(m, "No valgrind support");
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 /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
51 static char **extract_matching(const char *prefix, char *lines[])
53 unsigned int i, num_ret = 0;
54 char **ret = talloc_array(lines, char *, talloc_array_length(lines));
56 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
57 if (strstarts(lines[i], prefix)) {
58 ret[num_ret++] = talloc_steal(ret, lines[i]);
59 lines[i] = (char *)"";
62 ret[num_ret++] = NULL;
64 /* Make sure length is correct! */
65 return talloc_realloc(NULL, ret, char *, num_ret);
68 static char *get_leaks(char *lines[], char **errs)
70 char *leaks = talloc_strdup(lines, "");
73 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
74 if (strstr(lines[i], " lost ")) {
76 if (strstr(lines[i], " definitely lost ")) {
77 /* Definite leak, report. */
78 while (lines[i] && !blank_line(lines[i])) {
79 leaks = talloc_append_string(leaks,
81 leaks = talloc_append_string(leaks,
86 /* Not definite, ignore. */
87 while (lines[i] && !blank_line(lines[i]))
91 while (lines[i] && !blank_line(lines[i])) {
92 *errs = talloc_append_string(*errs, lines[i]);
93 *errs = talloc_append_string(*errs, "\n");
101 /* Returns leaks, and sets errs[] */
102 static char *analyze_output(const char *output, char **errs)
104 char *leaks = talloc_strdup(output, "");
106 char **lines = strsplit(output, output, "\n");
108 *errs = talloc_strdup(output, "");
109 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
110 unsigned int preflen = strspn(lines[i], "=0123456789");
111 char *prefix, **sublines;
113 /* Ignore erased lines, or weird stuff. */
117 prefix = talloc_strndup(output, lines[i], preflen);
118 sublines = extract_matching(prefix, lines);
120 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
134 static const char *concat(struct score *score, char *bits[])
137 char *ret = talloc_strdup(score, "");
139 for (i = 0; bits[i]; i++) {
141 ret = talloc_append_string(ret, " ");
142 ret = talloc_append_string(ret, bits[i]);
147 /* FIXME: Run examples, too! */
148 static void do_run_tests_vg(struct manifest *m,
150 unsigned int *timeleft,
154 struct list_head *list;
156 /* This is slow, so we run once but grab leak info. */
159 foreach_ptr(list, &m->run_tests, &m->api_tests) {
160 list_for_each(list, i, list) {
165 options = concat(score,
166 per_file_options(&tests_pass_valgrind,
168 if (streq(options, "FAIL"))
172 talloc_set_destructor(i->valgrind_log, NULL);
174 output = grab_file(i, i->valgrind_log, NULL);
175 /* No valgrind errors? */
176 if (!output || output[0] == '\0') {
180 i->leak_info = analyze_output(output, &err);
183 score_file_error(score, i, 0, "%s", err);
191 static void do_leakcheck_vg(struct manifest *m,
193 unsigned int *timeleft,
197 struct list_head *list;
201 foreach_ptr(list, &m->run_tests, &m->api_tests) {
202 list_for_each(list, i, list) {
203 options = per_file_options(&tests_pass_valgrind_noleaks,
206 if (streq(options[0], "FAIL")) {
210 errx(1, "Unknown leakcheck options '%s'",
215 score_file_error(score, i, 0, "%s",
222 /* FIXME: We don't fail for this, since many tests leak. */
229 /* Gcc's warn_unused_result is fascist bullshit. */
230 #define doesnt_matter()
232 static void run_under_debugger_vg(struct manifest *m, struct score *score)
234 struct file_error *first;
237 /* Don't ask anything if they suppressed tests. */
241 if (!ask("Should I run the first failing test under the debugger?"))
244 first = list_top(&score->per_file_errors, struct file_error, list);
245 command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
247 per_file_options(&tests_pass_valgrind,
249 first->file->compiled);
254 struct ccanlint tests_pass_valgrind = {
255 .key = "tests_pass_valgrind",
256 .name = "Module's run and api tests succeed under valgrind",
257 .can_run = can_run_vg,
258 .check = do_run_tests_vg,
259 .handle = run_under_debugger_vg,
260 .takes_options = true,
261 .needs = "tests_pass"
264 struct ccanlint tests_pass_valgrind_noleaks = {
265 .key = "tests_pass_valgrind_noleaks",
266 .name = "Module's run and api tests have no memory leaks",
267 .check = do_leakcheck_vg,
268 .takes_options = true,
269 .needs = "tests_pass_valgrind"