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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <string.h>
#include <stdio.h>
#define __CHECK_STR(str, name) \
do { \
if (!ASSERT_HAS_SUBSTR(fix->output, (str), (name))) \
goto out; \
} while (0)
struct fixture {
char tmpfile[80];
int fd;
char *output;
size_t sz;
char veristat[80];
};
static struct fixture *init_fixture(void)
{
struct fixture *fix = malloc(sizeof(struct fixture));
/* for no_alu32 and cpuv4 veristat is in parent folder */
if (access("./veristat", F_OK) == 0)
strscpy(fix->veristat, "./veristat");
else if (access("../veristat", F_OK) == 0)
strscpy(fix->veristat, "../veristat");
else
PRINT_FAIL("Can't find veristat binary");
snprintf(fix->tmpfile, sizeof(fix->tmpfile), "/tmp/test_veristat.XXXXXX");
fix->fd = mkstemp(fix->tmpfile);
fix->sz = 1000000;
fix->output = malloc(fix->sz);
return fix;
}
static void read_output(struct fixture *fix)
{
ssize_t len = pread(fix->fd, fix->output, fix->sz - 1, 0);
fix->output[len < 0 ? 0 : len] = 0;
ASSERT_GE(len, 0, "pread");
}
static void teardown_fixture(struct fixture *fix)
{
free(fix->output);
close(fix->fd);
remove(fix->tmpfile);
free(fix);
}
static void test_set_global_vars_succeeds(void)
{
struct fixture *fix = init_fixture();
SYS(out,
"%s set_global_vars.bpf.o"\
" -G \"var_s64 = 0xf000000000000001\" "\
" -G \"var_u64 = 0xfedcba9876543210\" "\
" -G \"var_s32 = -0x80000000\" "\
" -G \"var_u32 = 0x76543210\" "\
" -G \"var_s16 = -32768\" "\
" -G \"var_u16 = 60652\" "\
" -G \"var_s8 = -128\" "\
" -G \"var_u8 = 255\" "\
" -G \"var_ea = EA2\" "\
" -G \"var_eb = EB2\" "\
" -G \"var_ec=EC2\" "\
" -G \"var_b = 1\" "\
" -G \"struct1[2].struct2[1][2].u.var_u8[2]=170\" "\
" -G \"union1.struct3.var_u8_l = 0xaa\" "\
" -G \"union1.struct3.var_u8_h = 0xaa\" "\
" -G \"arr[3]= 171\" " \
" -G \"arr[EA2] =172\" " \
" -G \"enum_arr[EC2]=EA3\" " \
" -G \"three_d[31][7][EA2]=173\"" \
" -G \"struct1[2].struct2[1][2].u.mat[5][3]=174\" " \
" -G \"struct11 [ 7 ] [ 5 ] .struct2[0][1].u.mat[3][0] = 175\" " \
" -vl2 > %s", fix->veristat, fix->tmpfile);
read_output(fix);
__CHECK_STR("=0xf000000000000001 ", "var_s64 = 0xf000000000000001");
__CHECK_STR("=0xfedcba9876543210 ", "var_u64 = 0xfedcba9876543210");
__CHECK_STR("=0x80000000 ", "var_s32 = -0x80000000");
__CHECK_STR("=0x76543210 ", "var_u32 = 0x76543210");
__CHECK_STR("=0x8000 ", "var_s16 = -32768");
__CHECK_STR("=0xecec ", "var_u16 = 60652");
__CHECK_STR("=128 ", "var_s8 = -128");
__CHECK_STR("=255 ", "var_u8 = 255");
__CHECK_STR("=11 ", "var_ea = EA2");
__CHECK_STR("=12 ", "var_eb = EB2");
__CHECK_STR("=13 ", "var_ec = EC2");
__CHECK_STR("=1 ", "var_b = 1");
__CHECK_STR("=170 ", "struct1[2].struct2[1][2].u.var_u8[2]=170");
__CHECK_STR("=0xaaaa ", "union1.var_u16 = 0xaaaa");
__CHECK_STR("=171 ", "arr[3]= 171");
__CHECK_STR("=172 ", "arr[EA2] =172");
__CHECK_STR("=10 ", "enum_arr[EC2]=EA3");
__CHECK_STR("=173 ", "matrix[31][7][11]=173");
__CHECK_STR("=174 ", "struct1[2].struct2[1][2].u.mat[5][3]=174");
__CHECK_STR("=175 ", "struct11[7][5].struct2[0][1].u.mat[3][0]=175");
out:
teardown_fixture(fix);
}
static void test_set_global_vars_from_file_succeeds(void)
{
struct fixture *fix = init_fixture();
char input_file[80];
const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
int fd;
snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
fd = mkstemp(input_file);
if (!ASSERT_GE(fd, 0, "valid fd"))
goto out;
write(fd, vars, strlen(vars));
syncfs(fd);
SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
fix->veristat, input_file, fix->tmpfile);
read_output(fix);
__CHECK_STR("=0x8000 ", "var_s16 = -32768");
__CHECK_STR("=0xecec ", "var_u16 = 60652");
out:
close(fd);
remove(input_file);
teardown_fixture(fix);
}
static void test_set_global_vars_out_of_range(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"var_s32 = 2147483648\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
read_output(fix);
__CHECK_STR("is out of range [-2147483648; 2147483647]", "out of range");
out:
teardown_fixture(fix);
}
static void test_unsupported_ptr_array_type(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"ptr_arr[0] = 0\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
read_output(fix);
__CHECK_STR("Can't set ptr_arr[0]. Only ints and enums are supported", "ptr_arr");
out:
teardown_fixture(fix);
}
static void test_array_out_of_bounds(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"arr[99] = 0\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
read_output(fix);
__CHECK_STR("Array index 99 is out of bounds", "arr[99]");
out:
teardown_fixture(fix);
}
static void test_array_index_not_found(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"arr[EG2] = 0\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
read_output(fix);
__CHECK_STR("Can't resolve enum value EG2", "arr[EG2]");
out:
teardown_fixture(fix);
}
static void test_array_index_for_non_array(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"var_b[0] = 1\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
pread(fix->fd, fix->output, fix->sz, 0);
__CHECK_STR("Array index is not expected for var_b", "var_b[0] = 1");
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"union1.struct3[0].var_u8_l=1\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
pread(fix->fd, fix->output, fix->sz, 0);
__CHECK_STR("Array index is not expected for struct3", "union1.struct3[0].var_u8_l=1");
out:
teardown_fixture(fix);
}
static void test_no_array_index_for_array(void)
{
struct fixture *fix = init_fixture();
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"arr = 1\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
pread(fix->fd, fix->output, fix->sz, 0);
__CHECK_STR("Can't set arr. Only ints and enums are supported", "arr = 1");
SYS_FAIL(out,
"%s set_global_vars.bpf.o -G \"struct1[0].struct2.u.var_u8[2]=1\" -vl2 2> %s",
fix->veristat, fix->tmpfile);
pread(fix->fd, fix->output, fix->sz, 0);
__CHECK_STR("Can't resolve field u for non-composite type", "struct1[0].struct2.u.var_u8[2]=1");
out:
teardown_fixture(fix);
}
/*
* Name filter tests below run veristat on veristat_foo.bpf.o and
* veristat_bar.bpf.o, both defining programs 'foo', 'bar' and 'buz'.
* Every entry describes a single (filters, file, prog) combination and
* tells whether that program is expected in the veristat output:
* 'true' if it is, 'false' if it is not and -1 if veristat is expected
* to reject the filter.
*/
#define FILTER_OBJS "veristat_foo.bpf.o veristat_bar.bpf.o"
static const struct name_filter_case {
const char *filters;
const char *file;
const char *prog;
int included;
} name_filter_cases[] = {
/* no filters, every program is processed */
{ "", "foo", "foo", true },
{ "", "foo", "bar", true },
{ "", "foo", "buz", true },
{ "", "bar", "foo", true },
{ "", "bar", "bar", true },
{ "", "bar", "buz", true },
/* deny filters */
{ "-f '!*foo*'", "foo", "bar", false },
{ "-f '!*foo*'", "bar", "foo", false },
{ "-f '!*foo*'", "bar", "bar", true },
{ "-f '!*foo*/bar'", "foo", "bar", false },
{ "-f '!*foo*/bar'", "foo", "buz", true },
{ "-f '!*foo*/bar'", "bar", "bar", true },
{ "-f '!*foo*/'", "foo", "bar", false },
{ "-f '!*foo*/'", "bar", "bar", true },
{ "-f '!/bar'", "foo", "bar", false },
{ "-f '!/bar'", "foo", "foo", true },
{ "-f '!/'", "foo", "bar", -1 },
{ "-f '!'", "foo", "bar", -1 },
/* allow filters */
{ "-f '*foo*'", "foo", "bar", true },
{ "-f '*foo*'", "bar", "foo", true },
{ "-f '*foo*'", "bar", "bar", false },
{ "-f '*foo*/bar'", "foo", "bar", true },
{ "-f '*foo*/bar'", "foo", "buz", false },
{ "-f '*foo*/bar'", "bar", "bar", false },
{ "-f '*foo*/'", "foo", "bar", true },
{ "-f '*foo*/'", "bar", "bar", false },
{ "-f '/bar'", "foo", "bar", true },
{ "-f '/bar'", "foo", "foo", false },
{ "-f '/'", "foo", "bar", -1 },
{ "-f ''", "foo", "bar", -1 },
/* allow and deny filters combined */
{ "-f '*foo*/' -f '!/bar'", "foo", "foo", true },
{ "-f '*foo*/' -f '!/bar'", "foo", "bar", false },
{ "-f '*foo*/' -f '!/bar'", "bar", "foo", false },
};
static void test_name_filters(void)
{
struct fixture *fix = init_fixture();
const struct name_filter_case *t;
char cmd[512], row[64], name[128];
int i, err;
for (i = 0; i < ARRAY_SIZE(name_filter_cases); i++) {
t = &name_filter_cases[i];
/* stderr is merged with stdout in order to catch error messages */
snprintf(cmd, sizeof(cmd), "%s " FILTER_OBJS " -q -o csv -e file,prog %s > %s 2>&1",
fix->veristat, t->filters, fix->tmpfile);
err = system(cmd);
read_output(fix);
snprintf(row, sizeof(row), "veristat_%s.bpf.o,%s", t->file, t->prog);
snprintf(name, sizeof(name), "veristat %s: %s", t->filters, row);
switch (t->included) {
case true:
ASSERT_OK(err, name);
ASSERT_HAS_SUBSTR(fix->output, row, name);
break;
case false:
ASSERT_OK(err, name);
ASSERT_FALSE(!!strstr(fix->output, row), name);
break;
case -1:
ASSERT_NEQ(err, 0, name);
ASSERT_HAS_SUBSTR(fix->output, "Invalid filter", name);
break;
}
}
teardown_fixture(fix);
}
void test_veristat(void)
{
if (test__start_subtest("set_global_vars_succeeds"))
test_set_global_vars_succeeds();
if (test__start_subtest("set_global_vars_out_of_range"))
test_set_global_vars_out_of_range();
if (test__start_subtest("set_global_vars_from_file_succeeds"))
test_set_global_vars_from_file_succeeds();
if (test__start_subtest("test_unsupported_ptr_array_type"))
test_unsupported_ptr_array_type();
if (test__start_subtest("test_array_out_of_bounds"))
test_array_out_of_bounds();
if (test__start_subtest("test_array_index_not_found"))
test_array_index_not_found();
if (test__start_subtest("test_array_index_for_non_array"))
test_array_index_for_non_array();
if (test__start_subtest("test_no_array_index_for_array"))
test_no_array_index_for_array();
if (test__start_subtest("name_filters"))
test_name_filters();
}
#undef __CHECK_STR
|