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