]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_pass_valgrind.c
ccanlint: remove argument to -k/--keep
[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                             unsigned int *timeleft,
150                             struct score *score)
151 {
152         struct ccan_file *i;
153         struct list_head *list;
154
155         /* This is slow, so we run once but grab leak info. */
156         score->total = 0;
157         score->pass = true;
158         foreach_ptr(list, &m->run_tests, &m->api_tests) {
159                 list_for_each(list, i, list) {
160                         char *err, *output;
161                         const char *options;
162
163                         score->total++;
164                         options = concat(score,
165                                          per_file_options(&tests_pass_valgrind,
166                                                           i));
167                         if (streq(options, "FAIL")) {
168                                 i->leak_info = NULL;
169                                 continue;
170                         }
171
172                         output = grab_file(i, i->valgrind_log, NULL);
173                         /* No valgrind errors? */
174                         if (!output || output[0] == '\0') {
175                                 err = NULL;
176                                 i->leak_info = NULL;
177                         } else {
178                                 i->leak_info = analyze_output(output, &err);
179                         }
180                         if (err) {
181                                 score_file_error(score, i, 0, "%s", err);
182                                 score->pass = false;
183                         } else
184                                 score->score++;
185                 }
186         }
187 }
188
189 static void do_leakcheck_vg(struct manifest *m,
190                             unsigned int *timeleft,
191                             struct score *score)
192 {
193         struct ccan_file *i;
194         struct list_head *list;
195         char **options;
196         bool leaks = false;
197
198         foreach_ptr(list, &m->run_tests, &m->api_tests) {
199                 list_for_each(list, i, list) {
200                         options = per_file_options(&tests_pass_valgrind_noleaks,
201                                                    i);
202                         if (options[0]) {
203                                 if (streq(options[0], "FAIL")) {
204                                         leaks = true;
205                                         continue;
206                                 }
207                                 errx(1, "Unknown leakcheck options '%s'",
208                                      options[0]);
209                         }
210
211                         if (i->leak_info) {
212                                 score_file_error(score, i, 0, "%s",
213                                                  i->leak_info);
214                                 leaks = true;
215                         }
216                 }
217         }
218
219         /* FIXME: We don't fail for this, since many tests leak. */ 
220         score->pass = true;
221         if (!leaks) {
222                 score->score = 1;
223         }
224 }
225
226 /* Gcc's warn_unused_result is fascist bullshit. */
227 #define doesnt_matter()
228
229 static void run_under_debugger_vg(struct manifest *m, struct score *score)
230 {
231         struct file_error *first;
232         char *command;
233
234         /* Don't ask anything if they suppressed tests. */
235         if (score->pass)
236                 return;
237
238         if (!ask("Should I run the first failing test under the debugger?"))
239                 return;
240
241         first = list_top(&score->per_file_errors, struct file_error, list);
242         command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
243                                   concat(score,
244                                          per_file_options(&tests_pass_valgrind,
245                                                           first->file)),
246                                   first->file->compiled[COMPILE_NORMAL]);
247         if (system(command))
248                 doesnt_matter();
249 }
250
251 struct ccanlint tests_pass_valgrind = {
252         .key = "tests_pass_valgrind",
253         .name = "Module's run and api tests succeed under valgrind",
254         .can_run = can_run_vg,
255         .check = do_run_tests_vg,
256         .handle = run_under_debugger_vg,
257         .takes_options = true,
258         .needs = "tests_pass"
259 };
260
261 struct ccanlint tests_pass_valgrind_noleaks = {
262         .key = "tests_pass_valgrind_noleaks",
263         .name = "Module's run and api tests have no memory leaks",
264         .check = do_leakcheck_vg,
265         .takes_options = true,
266         .needs = "tests_pass_valgrind"
267 };
268