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