ccanlint: run tests under valgrind initially.
[ccan] / tools / ccanlint / tests / tests_pass_valgrind.c
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"
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <err.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 REGISTER_TEST(tests_pass_valgrind);
22 REGISTER_TEST(tests_pass_valgrind_noleaks);
23
24 /* Note: we already test safe_mode in run_tests.c */
25 static const char *can_run_vg(struct manifest *m)
26 {
27         if (!do_valgrind)
28                 return talloc_asprintf(m, "No valgrind support");
29         return NULL;
30 }
31
32 /* Example 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)
38 ==2749== 
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)
42 ==2749== 
43 */
44
45 static bool blank_line(const char *line)
46 {
47         return line[strspn(line, "=0123456789 ")] == '\0';
48 }
49
50 /* Removes matching lines from lines array, returns them.  FIXME: Hacky. */
51 static char **extract_matching(const char *prefix, char *lines[])
52 {
53         unsigned int i, num_ret = 0;
54         char **ret = talloc_array(lines, char *, talloc_array_length(lines));
55
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 *)"";
60                 }
61         }
62         ret[num_ret++] = NULL;
63
64         /* Make sure length is correct! */
65         return talloc_realloc(NULL, ret, char *, num_ret);
66 }
67
68 static char *get_leaks(char *lines[], char **errs)
69 {
70         char *leaks = talloc_strdup(lines, "");
71         unsigned int i;
72
73         for (i = 0; i < talloc_array_length(lines) - 1; i++) {
74                 if (strstr(lines[i], " lost ")) {
75                         /* A leak... */
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,
80                                                                      lines[i]);
81                                         leaks = talloc_append_string(leaks,
82                                                                      "\n");
83                                         i++;
84                                 }
85                         } else
86                                 /* Not definite, ignore. */
87                                 while (lines[i] && !blank_line(lines[i]))
88                                         i++;
89                 } else {
90                         /* A real error. */
91                         while (lines[i] && !blank_line(lines[i])) {
92                                 *errs = talloc_append_string(*errs, lines[i]);
93                                 *errs = talloc_append_string(*errs, "\n");
94                                 i++;
95                         }
96                 }
97         }
98         return leaks;
99 }
100
101 /* Returns leaks, and sets errs[] */
102 static char *analyze_output(const char *output, char **errs)
103 {
104         char *leaks = talloc_strdup(output, "");
105         unsigned int i;
106         char **lines = strsplit(output, output, "\n");
107
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;
112
113                 /* Ignore erased lines, or weird stuff. */
114                 if (preflen == 0)
115                         continue;
116
117                 prefix = talloc_strndup(output, lines[i], preflen);
118                 sublines = extract_matching(prefix, lines);
119
120                 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
121         }
122
123         if (!leaks[0]) {
124                 talloc_free(leaks);
125                 leaks = NULL;
126         }
127         if (!(*errs)[0]) {
128                 talloc_free(*errs);
129                 *errs = NULL;
130         }
131         return leaks;
132 }
133
134 static const char *concat(struct score *score, char *bits[])
135 {
136         unsigned int i;
137         char *ret = talloc_strdup(score, "");
138
139         for (i = 0; bits[i]; i++) {
140                 if (i)
141                         ret = talloc_append_string(ret, " ");
142                 ret = talloc_append_string(ret, bits[i]);
143         }
144         return ret;
145 }
146
147 /* FIXME: Run examples, too! */
148 static void do_run_tests_vg(struct manifest *m,
149                             bool keep,
150                             unsigned int *timeleft,
151                             struct score *score)
152 {
153         struct ccan_file *i;
154         struct list_head *list;
155
156         /* This is slow, so we run once but grab leak info. */
157         score->total = 0;
158         score->pass = true;
159         foreach_ptr(list, &m->run_tests, &m->api_tests) {
160                 list_for_each(list, i, list) {
161                         char *err, *output;
162                         const char *options;
163
164                         score->total++;
165                         options = concat(score,
166                                          per_file_options(&tests_pass_valgrind,
167                                                           i));
168                         if (streq(options, "FAIL"))
169                                 continue;
170
171                         if (keep)
172                                 talloc_set_destructor(i->valgrind_log, NULL);
173
174                         output = grab_file(i, i->valgrind_log, NULL);
175                         /* No valgrind errors? */
176                         if (!output || output[0] == '\0') {
177                                 err = NULL;
178                                 i->leak_info = NULL;
179                         } else {
180                                 i->leak_info = analyze_output(output, &err);
181                         }
182                         if (err) {
183                                 score_file_error(score, i, 0, "%s", err);
184                                 score->pass = false;
185                         } else
186                                 score->score++;
187                 }
188         }
189 }
190
191 static void do_leakcheck_vg(struct manifest *m,
192                             bool keep,
193                             unsigned int *timeleft,
194                             struct score *score)
195 {
196         struct ccan_file *i;
197         struct list_head *list;
198         char **options;
199         bool leaks = false;
200
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,
204                                                    i);
205                         if (options[0]) {
206                                 if (streq(options[0], "FAIL")) {
207                                         leaks = true;
208                                         continue;
209                                 }
210                                 errx(1, "Unknown leakcheck options '%s'",
211                                      options[0]);
212                         }
213
214                         if (i->leak_info) {
215                                 score_file_error(score, i, 0, "%s",
216                                                  i->leak_info);
217                                 leaks = true;
218                         }
219                 }
220         }
221
222         /* FIXME: We don't fail for this, since many tests leak. */ 
223         score->pass = true;
224         if (!leaks) {
225                 score->score = 1;
226         }
227 }
228
229 /* Gcc's warn_unused_result is fascist bullshit. */
230 #define doesnt_matter()
231
232 static void run_under_debugger_vg(struct manifest *m, struct score *score)
233 {
234         struct file_error *first;
235         char *command;
236
237         /* Don't ask anything if they suppressed tests. */
238         if (score->pass)
239                 return;
240
241         if (!ask("Should I run the first failing test under the debugger?"))
242                 return;
243
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",
246                                   concat(score,
247                                          per_file_options(&tests_pass_valgrind,
248                                                           first->file)),
249                                   first->file->compiled);
250         if (system(command))
251                 doesnt_matter();
252 }
253
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"
262 };
263
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"
270 };
271