1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include <tools/ccanlint/ccanlint.h>
#include <tools/tools.h>
#include <ccan/str/str.h>
#include <ccan/take/take.h>
#include <ccan/foreach/foreach.h>
#include <ccan/tal/grab_file/grab_file.h>
#include "tests_pass.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <string.h>
#include <ctype.h>
/* Note: we already test safe_mode in run_tests.c */
static const char *can_run_vg(struct manifest *m)
{
if (!do_valgrind)
return tal_fmt(m, "No valgrind support");
return NULL;
}
static void do_leakcheck_vg(struct manifest *m,
unsigned int *timeleft,
struct score *score);
static struct ccanlint tests_pass_valgrind_noleaks = {
.key = "tests_pass_valgrind_noleaks",
.name = "Module's run and api tests have no memory leaks",
.check = do_leakcheck_vg,
.takes_options = true,
.needs = "tests_pass_valgrind"
};
REGISTER_TEST(tests_pass_valgrind_noleaks);
/* Example output:
==2749== Conditional jump or move depends on uninitialised value(s)
==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
==2749== by 0x408EACF: printf (printf.c:35)
==2749== by 0x8048465: main (in /tmp/foo)
==2749==
==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2749== by 0x8048444: main (in /tmp/foo)
==2749==
*/
static bool blank_line(const char *line)
{
return line[strspn(line, "=0123456789 ")] == '\0';
}
/* Removes matching lines from lines array, returns them. FIXME: Hacky. */
static char **extract_matching(const char *prefix, char *lines[])
{
unsigned int i, num_ret = 0;
char **ret = tal_arr(lines, char *, tal_count(lines));
for (i = 0; i < tal_count(lines) - 1; i++) {
if (strstarts(lines[i], prefix)) {
ret[num_ret++] = tal_strdup(ret, lines[i]);
lines[i] = (char *)"";
}
}
ret[num_ret++] = NULL;
/* Make sure tal_count is correct! */
tal_resize(&ret, num_ret);
return ret;
}
static char *get_leaks(char *lines[], char **errs)
{
char *leaks = tal_strdup(lines, "");
unsigned int i;
for (i = 0; i < tal_count(lines) - 1; i++) {
if (strstr(lines[i], " lost ")) {
/* A leak... */
if (strstr(lines[i], " definitely lost ")) {
/* Definite leak, report. */
while (lines[i] && !blank_line(lines[i])) {
tal_append_fmt(&leaks, "%s\n",
lines[i]);
i++;
}
} else
/* Not definite, ignore. */
while (lines[i] && !blank_line(lines[i]))
i++;
} else {
/* A real error. */
while (lines[i] && !blank_line(lines[i])) {
if (!*errs)
*errs = tal_fmt(NULL, "%s\n", lines[i]);
else
tal_append_fmt(errs, "%s\n", lines[i]);
i++;
}
}
}
return leaks;
}
/* Returns leaks, and sets errs[] */
static char *analyze_output(const char *output, char **errs)
{
char *leaks = tal_strdup(output, "");
unsigned int i;
char **lines = tal_strsplit(output, output, "\n", STR_EMPTY_OK);
*errs = tal_strdup(output, "");
for (i = 0; i < tal_count(lines) - 1; i++) {
unsigned int preflen = strspn(lines[i], "=0123456789");
char *prefix, **sublines;
/* Ignore erased lines, or weird stuff. */
if (preflen == 0)
continue;
prefix = tal_strndup(output, lines[i], preflen);
sublines = extract_matching(prefix, lines);
leaks = tal_strcat(output, take(leaks),
take(get_leaks(sublines, errs)));
}
if (!leaks[0]) {
tal_free(leaks);
leaks = NULL;
}
if (!(*errs)[0]) {
tal_free(*errs);
*errs = NULL;
}
return leaks;
}
static const char *concat(struct score *score, char *bits[])
{
unsigned int i;
char *ret = tal_strdup(score, "");
for (i = 0; bits[i]; i++) {
if (i)
ret = tal_strcat(score, take(ret), " ");
ret = tal_strcat(score, take(ret), bits[i]);
}
return ret;
}
/* FIXME: Run examples, too! */
static void do_run_tests_vg(struct manifest *m,
unsigned int *timeleft,
struct score *score)
{
struct ccan_file *i;
struct list_head *list;
/* This is slow, so we run once but grab leak info. */
score->total = 0;
score->pass = true;
foreach_ptr(list, &m->run_tests, &m->api_tests) {
list_for_each(list, i, list) {
char *err, *output;
const char *options;
score->total++;
options = concat(score,
per_file_options(&tests_pass_valgrind,
i));
if (streq(options, "FAIL")) {
i->leak_info = NULL;
continue;
}
output = grab_file(i, i->valgrind_log);
/* No valgrind errors? */
if (!output || output[0] == '\0') {
err = NULL;
i->leak_info = NULL;
} else {
i->leak_info = analyze_output(output, &err);
}
if (err) {
score_file_error(score, i, 0, "%s", err);
score->pass = false;
} else
score->score++;
}
}
}
static void do_leakcheck_vg(struct manifest *m,
unsigned int *timeleft,
struct score *score)
{
struct ccan_file *i;
struct list_head *list;
char **options;
bool leaks = false;
foreach_ptr(list, &m->run_tests, &m->api_tests) {
list_for_each(list, i, list) {
options = per_file_options(&tests_pass_valgrind_noleaks,
i);
if (options[0]) {
if (streq(options[0], "FAIL")) {
leaks = true;
continue;
}
errx(1, "Unknown leakcheck options '%s'",
options[0]);
}
if (i->leak_info) {
score_file_error(score, i, 0, "%s",
i->leak_info);
leaks = true;
}
}
}
/* FIXME: We don't fail for this, since many tests leak. */
score->pass = true;
if (!leaks) {
score->score = 1;
}
}
/* Gcc's warn_unused_result is fascist bullshit. */
#define doesnt_matter()
static void run_under_debugger_vg(struct manifest *m, struct score *score)
{
struct file_error *first;
char *command;
/* Don't ask anything if they suppressed tests. */
if (score->pass)
return;
if (!ask("Should I run the first failing test under the debugger?"))
return;
first = list_top(&score->per_file_errors, struct file_error, list);
command = tal_fmt(m, "valgrind --leak-check=full --db-attach=yes%s %s %s",
concat(score, per_file_options(&tests_pass_valgrind,
first->file)),
valgrind_suppress, first->file->compiled[COMPILE_NORMAL]);
if (system(command))
doesnt_matter();
}
struct ccanlint tests_pass_valgrind = {
.key = "tests_pass_valgrind",
.name = "Module's run and api tests succeed under valgrind",
.can_run = can_run_vg,
.check = do_run_tests_vg,
.handle = run_under_debugger_vg,
.takes_options = true,
.needs = "tests_pass"
};
REGISTER_TEST(tests_pass_valgrind);
|