]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_pass_valgrind.c
ccanlint: use ccan/time
[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 static char *get_leaks(const char *output, char **errs)
53 {
54         char *leaks = talloc_strdup(output, "");
55         unsigned int i;
56         char **lines = strsplit(output, output, "\n");
57
58         *errs = talloc_strdup(output, "");
59         for (i = 0; i < talloc_array_length(lines) - 1; 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                                     " --child-silent-after-fork=yes"
116                                     " --leak-check=full"
117                                     " --log-fd=3 %s %s"
118                                     " 3> valgrind.log",
119                                     tests_pass_valgrind.options ?
120                                     tests_pass_valgrind.options : "",
121                                     i->compiled);
122                         output = grab_file(i, "valgrind.log", NULL);
123                         if (!output || output[0] == '\0') {
124                                 err = NULL;
125                                 i->leak_info = NULL;
126                         } else {
127                                 i->leak_info = get_leaks(output, &err);
128                         }
129                         if (err)
130                                 score_file_error(score, i, 0, "%s", err);
131                         else
132                                 score->score++;
133                 }
134         }
135
136         if (score->score == score->total)
137                 score->pass = true;
138 }
139
140 static void do_leakcheck_vg(struct manifest *m,
141                             bool keep,
142                             unsigned int *timeleft,
143                             struct score *score)
144 {
145         struct ccan_file *i;
146         struct list_head *list;
147         bool leaks = false;
148
149         foreach_ptr(list, &m->run_tests, &m->api_tests) {
150                 list_for_each(list, i, list) {
151                         if (i->leak_info) {
152                                 score_file_error(score, i, 0, "%s",
153                                                  i->leak_info);
154                                 leaks = true;
155                         }
156                 }
157         }
158
159         /* FIXME: We don't fail for this, since many tests leak. */ 
160         score->pass = true;
161         if (!leaks) {
162                 score->score = 1;
163         }
164 }
165
166 /* Gcc's warn_unused_result is fascist bullshit. */
167 #define doesnt_matter()
168
169 static void run_under_debugger_vg(struct manifest *m, struct score *score)
170 {
171         struct file_error *first;
172         char *command;
173
174         if (!ask("Should I run the first failing test under the debugger?"))
175                 return;
176
177         first = list_top(&score->per_file_errors, struct file_error, list);
178         command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
179                                   tests_pass_valgrind.options ?
180                                   tests_pass_valgrind.options : "",
181                                   first->file->compiled);
182         if (system(command))
183                 doesnt_matter();
184 }
185
186 struct ccanlint tests_pass_valgrind = {
187         .key = "tests_pass_valgrind",
188         .name = "Module's run and api tests succeed under valgrind",
189         .can_run = can_run_vg,
190         .check = do_run_tests_vg,
191         .handle = run_under_debugger_vg,
192         .takes_options = true,
193         .needs = "tests_pass"
194 };
195
196 struct ccanlint tests_pass_valgrind_noleaks = {
197         .key = "tests_pass_valgrind_noleaks",
198         .name = "Module's run and api tests have no memory leaks",
199         .check = do_leakcheck_vg,
200         .needs = "tests_pass_valgrind"
201 };
202
203 REGISTER_TEST(tests_pass_valgrind_noleaks);