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 /* Note: we already test safe_mode in run_tests.c */
22 static const char *can_run_vg(struct manifest *m)
25 return talloc_asprintf(m, "No valgrind support");
29 static void do_leakcheck_vg(struct manifest *m,
30 unsigned int *timeleft,
33 static struct ccanlint tests_pass_valgrind_noleaks = {
34 .key = "tests_pass_valgrind_noleaks",
35 .name = "Module's run and api tests have no memory leaks",
36 .check = do_leakcheck_vg,
37 .takes_options = true,
38 .needs = "tests_pass_valgrind"
40 REGISTER_TEST(tests_pass_valgrind_noleaks);
44 ==2749== Conditional jump or move depends on uninitialised value(s)
45 ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
46 ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
47 ==2749== by 0x408EACF: printf (printf.c:35)
48 ==2749== by 0x8048465: main (in /tmp/foo)
50 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
51 ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
52 ==2749== by 0x8048444: main (in /tmp/foo)
56 static bool blank_line(const char *line)
58 return line[strspn(line, "=0123456789 ")] == '\0';
61 /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
62 static char **extract_matching(const char *prefix, char *lines[])
64 unsigned int i, num_ret = 0;
65 char **ret = talloc_array(lines, char *, talloc_array_length(lines));
67 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
68 if (strstarts(lines[i], prefix)) {
69 ret[num_ret++] = talloc_steal(ret, lines[i]);
70 lines[i] = (char *)"";
73 ret[num_ret++] = NULL;
75 /* Make sure length is correct! */
76 return talloc_realloc(NULL, ret, char *, num_ret);
79 static char *get_leaks(char *lines[], char **errs)
81 char *leaks = talloc_strdup(lines, "");
84 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
85 if (strstr(lines[i], " lost ")) {
87 if (strstr(lines[i], " definitely lost ")) {
88 /* Definite leak, report. */
89 while (lines[i] && !blank_line(lines[i])) {
90 leaks = talloc_append_string(leaks,
92 leaks = talloc_append_string(leaks,
97 /* Not definite, ignore. */
98 while (lines[i] && !blank_line(lines[i]))
102 while (lines[i] && !blank_line(lines[i])) {
103 *errs = talloc_append_string(*errs, lines[i]);
104 *errs = talloc_append_string(*errs, "\n");
112 /* Returns leaks, and sets errs[] */
113 static char *analyze_output(const char *output, char **errs)
115 char *leaks = talloc_strdup(output, "");
117 char **lines = strsplit(output, output, "\n");
119 *errs = talloc_strdup(output, "");
120 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
121 unsigned int preflen = strspn(lines[i], "=0123456789");
122 char *prefix, **sublines;
124 /* Ignore erased lines, or weird stuff. */
128 prefix = talloc_strndup(output, lines[i], preflen);
129 sublines = extract_matching(prefix, lines);
131 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
145 static const char *concat(struct score *score, char *bits[])
148 char *ret = talloc_strdup(score, "");
150 for (i = 0; bits[i]; i++) {
152 ret = talloc_append_string(ret, " ");
153 ret = talloc_append_string(ret, bits[i]);
158 /* FIXME: Run examples, too! */
159 static void do_run_tests_vg(struct manifest *m,
160 unsigned int *timeleft,
164 struct list_head *list;
166 /* This is slow, so we run once but grab leak info. */
169 foreach_ptr(list, &m->run_tests, &m->api_tests) {
170 list_for_each(list, i, list) {
175 options = concat(score,
176 per_file_options(&tests_pass_valgrind,
178 if (streq(options, "FAIL")) {
183 output = grab_file(i, i->valgrind_log, NULL);
184 /* No valgrind errors? */
185 if (!output || output[0] == '\0') {
189 i->leak_info = analyze_output(output, &err);
192 score_file_error(score, i, 0, "%s", err);
200 static void do_leakcheck_vg(struct manifest *m,
201 unsigned int *timeleft,
205 struct list_head *list;
209 foreach_ptr(list, &m->run_tests, &m->api_tests) {
210 list_for_each(list, i, list) {
211 options = per_file_options(&tests_pass_valgrind_noleaks,
214 if (streq(options[0], "FAIL")) {
218 errx(1, "Unknown leakcheck options '%s'",
223 score_file_error(score, i, 0, "%s",
230 /* FIXME: We don't fail for this, since many tests leak. */
237 /* Gcc's warn_unused_result is fascist bullshit. */
238 #define doesnt_matter()
240 static void run_under_debugger_vg(struct manifest *m, struct score *score)
242 struct file_error *first;
245 /* Don't ask anything if they suppressed tests. */
249 if (!ask("Should I run the first failing test under the debugger?"))
252 first = list_top(&score->per_file_errors, struct file_error, list);
253 command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
255 per_file_options(&tests_pass_valgrind,
257 first->file->compiled[COMPILE_NORMAL]);
262 struct ccanlint tests_pass_valgrind = {
263 .key = "tests_pass_valgrind",
264 .name = "Module's run and api tests succeed under valgrind",
265 .can_run = can_run_vg,
266 .check = do_run_tests_vg,
267 .handle = run_under_debugger_vg,
268 .takes_options = true,
269 .needs = "tests_pass"
271 REGISTER_TEST(tests_pass_valgrind);