4365f2a60227bc1a259ad4671d23efa72f87f0e9
[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 struct ccanlint run_tests_vg;
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 static char *get_leaks(const char *output, char **errs)
53 {
54         char *leaks = talloc_strdup(output, "");
55         unsigned int i, num;
56         char **lines = strsplit(output, output, "\n", &num);
57
58         *errs = talloc_strdup(output, "");
59         for (i = 0; i < num; i++) {
60                 if (strstr(lines[i], " lost ")) {
61                         /* A leak... */
62                         if (strstr(lines[i], " definitely lost ")) {
63                                 /* Definite leak, report. */
64                                 while (lines[i] && !blank_line(lines[i])) {
65                                         leaks = talloc_append_string(leaks,
66                                                                      lines[i]);
67                                         leaks = talloc_append_string(leaks,
68                                                                      "\n");
69                                         i++;
70                                 }
71                         } else
72                                 /* Not definite, ignore. */
73                                 while (lines[i] && !blank_line(lines[i]))
74                                         i++;
75                 } else {
76                         /* A real error. */
77                         while (lines[i] && !blank_line(lines[i])) {
78                                 *errs = talloc_append_string(*errs, lines[i]);
79                                 *errs = talloc_append_string(*errs, "\n");
80                                 i++;
81                         }
82                 }
83         }
84         if (!leaks[0]) {
85                 talloc_free(leaks);
86                 leaks = NULL;
87         }
88         if (!(*errs)[0]) {
89                 talloc_free(*errs);
90                 *errs = NULL;
91         }
92         return leaks;
93 }
94
95 /* FIXME: Run examples, too! */
96 static void do_run_tests_vg(struct manifest *m,
97                              bool keep,
98                             unsigned int *timeleft,
99                             struct score *score)
100 {
101         struct ccan_file *i;
102         struct list_head *list;
103         char *cmdout;
104
105         /* This is slow, so we run once but grab leak info. */
106         score->total = 0;
107         foreach_ptr(list, &m->run_tests, &m->api_tests) {
108                 list_for_each(list, i, list) {
109                         char *output, *err;
110                         score->total++;
111                         /* FIXME: Valgrind's output sucks.  XML is unreadable by
112                          * humans, and you can't have both. */
113                         run_command(score, timeleft, &cmdout,
114                                     "valgrind -q --error-exitcode=101"
115                                     " --leak-check=full"
116                                     " --log-fd=3 %s %s"
117                                     " 3> valgrind.log",
118                                     run_tests_vg.options ?
119                                     run_tests_vg.options : "",
120                                     i->compiled);
121                         output = grab_file(i, "valgrind.log", NULL);
122                         if (!output || output[0] == '\0') {
123                                 err = NULL;
124                                 i->leak_info = NULL;
125                         } else {
126                                 i->leak_info = get_leaks(output, &err);
127                         }
128                         if (err)
129                                 score_file_error(score, i, 0, err);
130                         else
131                                 score->score++;
132                 }
133         }
134
135         if (score->score == score->total)
136                 score->pass = true;
137 }
138
139 static void do_leakcheck_vg(struct manifest *m,
140                             bool keep,
141                             unsigned int *timeleft,
142                             struct score *score)
143 {
144         struct ccan_file *i;
145         struct list_head *list;
146         bool leaks = false;
147
148         foreach_ptr(list, &m->run_tests, &m->api_tests) {
149                 list_for_each(list, i, list) {
150                         if (i->leak_info) {
151                                 score_file_error(score, i, 0, i->leak_info);
152                                 leaks = true;
153                         }
154                 }
155         }
156
157         if (!leaks) {
158                 score->score = 1;
159                 score->pass = true;
160         }
161 }
162
163 /* Gcc's warn_unused_result is fascist bullshit. */
164 #define doesnt_matter()
165
166 static void run_under_debugger_vg(struct manifest *m, struct score *score)
167 {
168         struct file_error *first;
169         char *command;
170
171         if (!ask("Should I run the first failing test under the debugger?"))
172                 return;
173
174         first = list_top(&score->per_file_errors, struct file_error, list);
175         command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
176                                   run_tests_vg.options ?
177                                   run_tests_vg.options : "",
178                                   first->file->compiled);
179         if (system(command))
180                 doesnt_matter();
181 }
182
183 struct ccanlint tests_pass_valgrind = {
184         .key = "tests_pass_valgrind",
185         .name = "Module's run and api tests succeed under valgrind",
186         .can_run = can_run_vg,
187         .check = do_run_tests_vg,
188         .handle = run_under_debugger_vg,
189         .takes_options = true,
190         .needs = "tests_pass"
191 };
192
193 REGISTER_TEST(tests_pass_valgrind);
194
195 struct ccanlint tests_pass_valgrind_noleaks = {
196         .key = "tests_pass_valgrind_noleaks",
197         .name = "Module's run and api tests have no memory leaks",
198         .check = do_leakcheck_vg,
199         .needs = "tests_pass_valgrind"
200 };
201
202 REGISTER_TEST(tests_pass_valgrind_noleaks);