30a806eb934bd5cf9799a8b74ab5e88c348d1187
[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                         } else {
125                                 i->leak_info = get_leaks(output, &err);
126                         }
127                         if (err)
128                                 score_file_error(score, i, 0, err);
129                         else
130                                 score->score++;
131                 }
132         }
133
134         if (score->score == score->total)
135                 score->pass = true;
136 }
137
138 static void do_leakcheck_vg(struct manifest *m,
139                             bool keep,
140                             unsigned int *timeleft,
141                             struct score *score)
142 {
143         struct ccan_file *i;
144         struct list_head *list;
145         bool leaks = false;
146
147         foreach_ptr(list, &m->run_tests, &m->api_tests) {
148                 list_for_each(list, i, list) {
149                         if (i->leak_info) {
150                                 score_file_error(score, i, 0, i->leak_info);
151                                 leaks = true;
152                         }
153                 }
154         }
155
156         if (!leaks) {
157                 score->score = 1;
158                 score->pass = true;
159         }
160 }
161
162 /* Gcc's warn_unused_result is fascist bullshit. */
163 #define doesnt_matter()
164
165 static void run_under_debugger_vg(struct manifest *m, struct score *score)
166 {
167         struct file_error *first;
168         char *command;
169
170         if (!ask("Should I run the first failing test under the debugger?"))
171                 return;
172
173         first = list_top(&score->per_file_errors, struct file_error, list);
174         command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
175                                   run_tests_vg.options ?
176                                   run_tests_vg.options : "",
177                                   first->file->compiled);
178         if (system(command))
179                 doesnt_matter();
180 }
181
182 struct ccanlint tests_pass_valgrind = {
183         .key = "tests_pass_valgrind",
184         .name = "Module's run and api tests succeed under valgrind",
185         .can_run = can_run_vg,
186         .check = do_run_tests_vg,
187         .handle = run_under_debugger_vg,
188         .takes_options = true,
189         .needs = "tests_pass"
190 };
191
192 REGISTER_TEST(tests_pass_valgrind);
193
194 struct ccanlint tests_pass_valgrind_noleaks = {
195         .key = "tests_pass_valgrind_noleaks",
196         .name = "Module's run and api tests leak memory",
197         .check = do_leakcheck_vg,
198         .needs = "tests_pass_valgrind"
199 };
200
201 REGISTER_TEST(tests_pass_valgrind_noleaks);