]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_pass_valgrind.c
ccanlint: make valgrind understand child output.
[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 <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 REGISTER_TEST(tests_pass_valgrind);
21
22 /* Note: we already test safe_mode in run_tests.c */
23 static const char *can_run_vg(struct manifest *m)
24 {
25         unsigned int timeleft = default_timeout_ms;
26         char *output;
27
28         if (!run_command(m, &timeleft, &output,
29                          "valgrind -q --error-exitcode=0 true"))
30                 return talloc_asprintf(m, "No valgrind support: %s", output);
31         return NULL;
32 }
33
34 /* Example output:
35 ==2749== Conditional jump or move depends on uninitialised value(s)
36 ==2749==    at 0x4026C60: strnlen (mc_replace_strmem.c:263)
37 ==2749==    by 0x40850E3: vfprintf (vfprintf.c:1614)
38 ==2749==    by 0x408EACF: printf (printf.c:35)
39 ==2749==    by 0x8048465: main (in /tmp/foo)
40 ==2749== 
41 ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
42 ==2749==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
43 ==2749==    by 0x8048444: main (in /tmp/foo)
44 ==2749== 
45 */
46
47 static bool blank_line(const char *line)
48 {
49         return line[strspn(line, "=0123456789 ")] == '\0';
50 }
51
52 /* Removes matching lines from lines array, returns them.  FIXME: Hacky. */
53 static char **extract_matching(const char *prefix, char *lines[])
54 {
55         unsigned int i, num_ret = 0;
56         char **ret = talloc_array(lines, char *, talloc_array_length(lines));
57
58         for (i = 0; i < talloc_array_length(lines) - 1; i++) {
59                 if (strstarts(lines[i], prefix)) {
60                         ret[num_ret++] = talloc_steal(ret, lines[i]);
61                         lines[i] = (char *)"";
62                 }
63         }
64         ret[num_ret++] = NULL;
65
66         /* Make sure length is correct! */
67         return talloc_realloc(NULL, ret, char *, num_ret);
68 }
69
70 static char *get_leaks(char *lines[], char **errs)
71 {
72         char *leaks = talloc_strdup(lines, "");
73         unsigned int i;
74
75         for (i = 0; i < talloc_array_length(lines) - 1; i++) {
76                 if (strstr(lines[i], " lost ")) {
77                         /* A leak... */
78                         if (strstr(lines[i], " definitely lost ")) {
79                                 /* Definite leak, report. */
80                                 while (lines[i] && !blank_line(lines[i])) {
81                                         leaks = talloc_append_string(leaks,
82                                                                      lines[i]);
83                                         leaks = talloc_append_string(leaks,
84                                                                      "\n");
85                                         i++;
86                                 }
87                         } else
88                                 /* Not definite, ignore. */
89                                 while (lines[i] && !blank_line(lines[i]))
90                                         i++;
91                 } else {
92                         /* A real error. */
93                         while (lines[i] && !blank_line(lines[i])) {
94                                 *errs = talloc_append_string(*errs, lines[i]);
95                                 *errs = talloc_append_string(*errs, "\n");
96                                 i++;
97                         }
98                 }
99         }
100         return leaks;
101 }
102
103 /* Returns leaks, and sets errs[] */
104 static char *analyze_output(const char *output, char **errs)
105 {
106         char *leaks = talloc_strdup(output, "");
107         unsigned int i;
108         char **lines = strsplit(output, output, "\n");
109
110         *errs = talloc_strdup(output, "");
111         for (i = 0; i < talloc_array_length(lines) - 1; i++) {
112                 unsigned int preflen = strspn(lines[i], "=0123456789");
113                 char *prefix, **sublines;
114
115                 /* Ignore erased lines, or weird stuff. */
116                 if (preflen == 0)
117                         continue;
118
119                 prefix = talloc_strndup(output, lines[i], preflen);
120                 sublines = extract_matching(prefix, lines);
121
122                 leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
123         }
124
125         if (!leaks[0]) {
126                 talloc_free(leaks);
127                 leaks = NULL;
128         }
129         if (!(*errs)[0]) {
130                 talloc_free(*errs);
131                 *errs = NULL;
132         }
133         return leaks;
134 }
135
136 /* FIXME: Run examples, too! */
137 static void do_run_tests_vg(struct manifest *m,
138                              bool keep,
139                             unsigned int *timeleft,
140                             struct score *score)
141 {
142         struct ccan_file *i;
143         struct list_head *list;
144         char *cmdout;
145
146         /* This is slow, so we run once but grab leak info. */
147         score->total = 0;
148         foreach_ptr(list, &m->run_tests, &m->api_tests) {
149                 list_for_each(list, i, list) {
150                         char *output, *err, *log;
151                         bool pass;
152                         score->total++;
153
154                         /* FIXME: Valgrind's output sucks.  XML is unreadable by
155                          * humans *and* doesn't support children reporting. */
156                         log = talloc_asprintf(score,
157                                               "%s.valgrind-log", i->compiled);
158                         if (!keep)
159                                 talloc_set_destructor(log,
160                                                       unlink_file_destructor);
161
162                         pass = run_command(score, timeleft, &cmdout,
163                                          "valgrind -q --error-exitcode=101"
164                                            " --leak-check=full"
165                                            " --log-fd=3 %s %s"
166                                            " 3> %s",
167                                            tests_pass_valgrind.options ?
168                                            tests_pass_valgrind.options : "",
169                                            i->compiled, log);
170                         output = grab_file(i, log, NULL);
171                         /* No valgrind errors?  Expect it to pass... */
172                         if (!output || output[0] == '\0') {
173                                 if (!pass) {
174                                         err = talloc_asprintf(score,
175                                                               "Test failed:\n"
176                                                               "%s",
177                                                               cmdout);
178                                 } else
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                         else
187                                 score->score++;
188                 }
189         }
190
191         if (score->score == score->total)
192                 score->pass = true;
193 }
194
195 static void do_leakcheck_vg(struct manifest *m,
196                             bool keep,
197                             unsigned int *timeleft,
198                             struct score *score)
199 {
200         struct ccan_file *i;
201         struct list_head *list;
202         bool leaks = false;
203
204         foreach_ptr(list, &m->run_tests, &m->api_tests) {
205                 list_for_each(list, i, list) {
206                         if (i->leak_info) {
207                                 score_file_error(score, i, 0, "%s",
208                                                  i->leak_info);
209                                 leaks = true;
210                         }
211                 }
212         }
213
214         /* FIXME: We don't fail for this, since many tests leak. */ 
215         score->pass = true;
216         if (!leaks) {
217                 score->score = 1;
218         }
219 }
220
221 /* Gcc's warn_unused_result is fascist bullshit. */
222 #define doesnt_matter()
223
224 static void run_under_debugger_vg(struct manifest *m, struct score *score)
225 {
226         struct file_error *first;
227         char *command;
228
229         if (!ask("Should I run the first failing test under the debugger?"))
230                 return;
231
232         first = list_top(&score->per_file_errors, struct file_error, list);
233         command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
234                                   tests_pass_valgrind.options ?
235                                   tests_pass_valgrind.options : "",
236                                   first->file->compiled);
237         if (system(command))
238                 doesnt_matter();
239 }
240
241 struct ccanlint tests_pass_valgrind = {
242         .key = "tests_pass_valgrind",
243         .name = "Module's run and api tests succeed under valgrind",
244         .can_run = can_run_vg,
245         .check = do_run_tests_vg,
246         .handle = run_under_debugger_vg,
247         .takes_options = true,
248         .needs = "tests_pass"
249 };
250
251 struct ccanlint tests_pass_valgrind_noleaks = {
252         .key = "tests_pass_valgrind_noleaks",
253         .name = "Module's run and api tests have no memory leaks",
254         .check = do_leakcheck_vg,
255         .needs = "tests_pass_valgrind"
256 };
257
258 REGISTER_TEST(tests_pass_valgrind_noleaks);