]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_pass_valgrind.c
tools: use tal instead of talloc.
[ccan] / tools / ccanlint / tests / tests_pass_valgrind.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/take/take.h>
5 #include <ccan/foreach/foreach.h>
6 #include "tests_pass.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <err.h>
16 #include <string.h>
17 #include <ctype.h>
18
19 /* Note: we already test safe_mode in run_tests.c */
20 static const char *can_run_vg(struct manifest *m)
21 {
22         if (!do_valgrind)
23                 return tal_fmt(m, "No valgrind support");
24         return NULL;
25 }
26
27 static void do_leakcheck_vg(struct manifest *m,
28                             unsigned int *timeleft,
29                             struct score *score);
30
31 static struct ccanlint tests_pass_valgrind_noleaks = {
32         .key = "tests_pass_valgrind_noleaks",
33         .name = "Module's run and api tests have no memory leaks",
34         .check = do_leakcheck_vg,
35         .takes_options = true,
36         .needs = "tests_pass_valgrind"
37 };
38 REGISTER_TEST(tests_pass_valgrind_noleaks);
39
40
41 /* Example output:
42 ==2749== Conditional jump or move depends on uninitialised value(s)
43 ==2749==    at 0x4026C60: strnlen (mc_replace_strmem.c:263)
44 ==2749==    by 0x40850E3: vfprintf (vfprintf.c:1614)
45 ==2749==    by 0x408EACF: printf (printf.c:35)
46 ==2749==    by 0x8048465: main (in /tmp/foo)
47 ==2749== 
48 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
49 ==2749==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
50 ==2749==    by 0x8048444: main (in /tmp/foo)
51 ==2749== 
52 */
53
54 static bool blank_line(const char *line)
55 {
56         return line[strspn(line, "=0123456789 ")] == '\0';
57 }
58
59 /* Removes matching lines from lines array, returns them.  FIXME: Hacky. */
60 static char **extract_matching(const char *prefix, char *lines[])
61 {
62         unsigned int i, num_ret = 0;
63         char **ret = tal_arr(lines, char *, tal_count(lines));
64
65         for (i = 0; i < tal_count(lines) - 1; i++) {
66                 if (strstarts(lines[i], prefix)) {
67                         ret[num_ret++] = tal_strdup(ret, lines[i]);
68                         lines[i] = (char *)"";
69                 }
70         }
71         ret[num_ret++] = NULL;
72
73         /* Make sure tal_count is correct! */
74         tal_resize(&ret, num_ret);
75         return ret;
76 }
77
78 static char *get_leaks(char *lines[], char **errs)
79 {
80         char *leaks = tal_strdup(lines, "");
81         unsigned int i;
82
83         for (i = 0; i < tal_count(lines) - 1; i++) {
84                 if (strstr(lines[i], " lost ")) {
85                         /* A leak... */
86                         if (strstr(lines[i], " definitely lost ")) {
87                                 /* Definite leak, report. */
88                                 while (lines[i] && !blank_line(lines[i])) {
89                                         tal_append_fmt(&leaks, "%s\n",
90                                                        lines[i]);
91                                         i++;
92                                 }
93                         } else
94                                 /* Not definite, ignore. */
95                                 while (lines[i] && !blank_line(lines[i]))
96                                         i++;
97                 } else {
98                         /* A real error. */
99                         while (lines[i] && !blank_line(lines[i])) {
100                                 if (!*errs)
101                                         *errs = tal_fmt(NULL, "%s\n", lines[i]);
102                                 else
103                                         tal_append_fmt(errs, "%s\n", lines[i]);
104                                 i++;
105                         }
106                 }
107         }
108         return leaks;
109 }
110
111 /* Returns leaks, and sets errs[] */
112 static char *analyze_output(const char *output, char **errs)
113 {
114         char *leaks = tal_strdup(output, "");
115         unsigned int i;
116         char **lines = tal_strsplit(output, output, "\n", STR_EMPTY_OK);
117
118         *errs = tal_strdup(output, "");
119         for (i = 0; i < tal_count(lines) - 1; i++) {
120                 unsigned int preflen = strspn(lines[i], "=0123456789");
121                 char *prefix, **sublines;
122
123                 /* Ignore erased lines, or weird stuff. */
124                 if (preflen == 0)
125                         continue;
126
127                 prefix = tal_strndup(output, lines[i], preflen);
128                 sublines = extract_matching(prefix, lines);
129
130                 leaks = tal_strcat(output, take(leaks),
131                                    take(get_leaks(sublines, errs)));
132         }
133
134         if (!leaks[0]) {
135                 tal_free(leaks);
136                 leaks = NULL;
137         }
138         if (!(*errs)[0]) {
139                 tal_free(*errs);
140                 *errs = NULL;
141         }
142         return leaks;
143 }
144
145 static const char *concat(struct score *score, char *bits[])
146 {
147         unsigned int i;
148         char *ret = tal_strdup(score, "");
149
150         for (i = 0; bits[i]; i++) {
151                 if (i)
152                         ret = tal_strcat(score, take(ret), " ");
153                 ret = tal_strcat(score, take(ret), bits[i]);
154         }
155         return ret;
156 }
157
158 /* FIXME: Run examples, too! */
159 static void do_run_tests_vg(struct manifest *m,
160                             unsigned int *timeleft,
161                             struct score *score)
162 {
163         struct ccan_file *i;
164         struct list_head *list;
165
166         /* This is slow, so we run once but grab leak info. */
167         score->total = 0;
168         score->pass = true;
169         foreach_ptr(list, &m->run_tests, &m->api_tests) {
170                 list_for_each(list, i, list) {
171                         char *err, *output;
172                         const char *options;
173
174                         score->total++;
175                         options = concat(score,
176                                          per_file_options(&tests_pass_valgrind,
177                                                           i));
178                         if (streq(options, "FAIL")) {
179                                 i->leak_info = NULL;
180                                 continue;
181                         }
182
183                         output = tal_grab_file(i, i->valgrind_log, NULL);
184                         /* No valgrind errors? */
185                         if (!output || output[0] == '\0') {
186                                 err = NULL;
187                                 i->leak_info = NULL;
188                         } else {
189                                 i->leak_info = analyze_output(output, &err);
190                         }
191                         if (err) {
192                                 score_file_error(score, i, 0, "%s", err);
193                                 score->pass = false;
194                         } else
195                                 score->score++;
196                 }
197         }
198 }
199
200 static void do_leakcheck_vg(struct manifest *m,
201                             unsigned int *timeleft,
202                             struct score *score)
203 {
204         struct ccan_file *i;
205         struct list_head *list;
206         char **options;
207         bool leaks = false;
208
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,
212                                                    i);
213                         if (options[0]) {
214                                 if (streq(options[0], "FAIL")) {
215                                         leaks = true;
216                                         continue;
217                                 }
218                                 errx(1, "Unknown leakcheck options '%s'",
219                                      options[0]);
220                         }
221
222                         if (i->leak_info) {
223                                 score_file_error(score, i, 0, "%s",
224                                                  i->leak_info);
225                                 leaks = true;
226                         }
227                 }
228         }
229
230         /* FIXME: We don't fail for this, since many tests leak. */ 
231         score->pass = true;
232         if (!leaks) {
233                 score->score = 1;
234         }
235 }
236
237 /* Gcc's warn_unused_result is fascist bullshit. */
238 #define doesnt_matter()
239
240 static void run_under_debugger_vg(struct manifest *m, struct score *score)
241 {
242         struct file_error *first;
243         char *command;
244
245         /* Don't ask anything if they suppressed tests. */
246         if (score->pass)
247                 return;
248
249         if (!ask("Should I run the first failing test under the debugger?"))
250                 return;
251
252         first = list_top(&score->per_file_errors, struct file_error, list);
253         command = tal_fmt(m, "valgrind --leak-check=full --db-attach=yes%s %s",
254                           concat(score, per_file_options(&tests_pass_valgrind,
255                                                          first->file)),
256                           first->file->compiled[COMPILE_NORMAL]);
257         if (system(command))
258                 doesnt_matter();
259 }
260
261 struct ccanlint tests_pass_valgrind = {
262         .key = "tests_pass_valgrind",
263         .name = "Module's run and api tests succeed under valgrind",
264         .can_run = can_run_vg,
265         .check = do_run_tests_vg,
266         .handle = run_under_debugger_vg,
267         .takes_options = true,
268         .needs = "tests_pass"
269 };
270 REGISTER_TEST(tests_pass_valgrind);