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