1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #define __CHECK_STR(str, name) \
8 do { \
9 if (!ASSERT_HAS_SUBSTR(fix->output, (str), (name))) \
10 goto out; \
11 } while (0)
12
13 struct fixture {
14 char tmpfile[80];
15 int fd;
16 char *output;
17 size_t sz;
18 char veristat[80];
19 };
20
init_fixture(void)21 static struct fixture *init_fixture(void)
22 {
23 struct fixture *fix = malloc(sizeof(struct fixture));
24
25 /* for no_alu32 and cpuv4 veristat is in parent folder */
26 if (access("./veristat", F_OK) == 0)
27 strscpy(fix->veristat, "./veristat");
28 else if (access("../veristat", F_OK) == 0)
29 strscpy(fix->veristat, "../veristat");
30 else
31 PRINT_FAIL("Can't find veristat binary");
32
33 snprintf(fix->tmpfile, sizeof(fix->tmpfile), "/tmp/test_veristat.XXXXXX");
34 fix->fd = mkstemp(fix->tmpfile);
35 fix->sz = 1000000;
36 fix->output = malloc(fix->sz);
37 return fix;
38 }
39
read_output(struct fixture * fix)40 static void read_output(struct fixture *fix)
41 {
42 ssize_t len = pread(fix->fd, fix->output, fix->sz - 1, 0);
43
44 fix->output[len < 0 ? 0 : len] = 0;
45 ASSERT_GE(len, 0, "pread");
46 }
47
teardown_fixture(struct fixture * fix)48 static void teardown_fixture(struct fixture *fix)
49 {
50 free(fix->output);
51 close(fix->fd);
52 remove(fix->tmpfile);
53 free(fix);
54 }
55
test_set_global_vars_succeeds(void)56 static void test_set_global_vars_succeeds(void)
57 {
58 struct fixture *fix = init_fixture();
59
60 SYS(out,
61 "%s set_global_vars.bpf.o"\
62 " -G \"var_s64 = 0xf000000000000001\" "\
63 " -G \"var_u64 = 0xfedcba9876543210\" "\
64 " -G \"var_s32 = -0x80000000\" "\
65 " -G \"var_u32 = 0x76543210\" "\
66 " -G \"var_s16 = -32768\" "\
67 " -G \"var_u16 = 60652\" "\
68 " -G \"var_s8 = -128\" "\
69 " -G \"var_u8 = 255\" "\
70 " -G \"var_ea = EA2\" "\
71 " -G \"var_eb = EB2\" "\
72 " -G \"var_ec=EC2\" "\
73 " -G \"var_b = 1\" "\
74 " -G \"struct1[2].struct2[1][2].u.var_u8[2]=170\" "\
75 " -G \"union1.struct3.var_u8_l = 0xaa\" "\
76 " -G \"union1.struct3.var_u8_h = 0xaa\" "\
77 " -G \"arr[3]= 171\" " \
78 " -G \"arr[EA2] =172\" " \
79 " -G \"enum_arr[EC2]=EA3\" " \
80 " -G \"three_d[31][7][EA2]=173\"" \
81 " -G \"struct1[2].struct2[1][2].u.mat[5][3]=174\" " \
82 " -G \"struct11 [ 7 ] [ 5 ] .struct2[0][1].u.mat[3][0] = 175\" " \
83 " -vl2 > %s", fix->veristat, fix->tmpfile);
84
85 read_output(fix);
86 __CHECK_STR("=0xf000000000000001 ", "var_s64 = 0xf000000000000001");
87 __CHECK_STR("=0xfedcba9876543210 ", "var_u64 = 0xfedcba9876543210");
88 __CHECK_STR("=0x80000000 ", "var_s32 = -0x80000000");
89 __CHECK_STR("=0x76543210 ", "var_u32 = 0x76543210");
90 __CHECK_STR("=0x8000 ", "var_s16 = -32768");
91 __CHECK_STR("=0xecec ", "var_u16 = 60652");
92 __CHECK_STR("=128 ", "var_s8 = -128");
93 __CHECK_STR("=255 ", "var_u8 = 255");
94 __CHECK_STR("=11 ", "var_ea = EA2");
95 __CHECK_STR("=12 ", "var_eb = EB2");
96 __CHECK_STR("=13 ", "var_ec = EC2");
97 __CHECK_STR("=1 ", "var_b = 1");
98 __CHECK_STR("=170 ", "struct1[2].struct2[1][2].u.var_u8[2]=170");
99 __CHECK_STR("=0xaaaa ", "union1.var_u16 = 0xaaaa");
100 __CHECK_STR("=171 ", "arr[3]= 171");
101 __CHECK_STR("=172 ", "arr[EA2] =172");
102 __CHECK_STR("=10 ", "enum_arr[EC2]=EA3");
103 __CHECK_STR("=173 ", "matrix[31][7][11]=173");
104 __CHECK_STR("=174 ", "struct1[2].struct2[1][2].u.mat[5][3]=174");
105 __CHECK_STR("=175 ", "struct11[7][5].struct2[0][1].u.mat[3][0]=175");
106
107 out:
108 teardown_fixture(fix);
109 }
110
test_set_global_vars_from_file_succeeds(void)111 static void test_set_global_vars_from_file_succeeds(void)
112 {
113 struct fixture *fix = init_fixture();
114 char input_file[80];
115 const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
116 int fd;
117
118 snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
119 fd = mkstemp(input_file);
120 if (!ASSERT_GE(fd, 0, "valid fd"))
121 goto out;
122
123 write(fd, vars, strlen(vars));
124 syncfs(fd);
125 SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
126 fix->veristat, input_file, fix->tmpfile);
127 read_output(fix);
128 __CHECK_STR("=0x8000 ", "var_s16 = -32768");
129 __CHECK_STR("=0xecec ", "var_u16 = 60652");
130
131 out:
132 close(fd);
133 remove(input_file);
134 teardown_fixture(fix);
135 }
136
test_set_global_vars_out_of_range(void)137 static void test_set_global_vars_out_of_range(void)
138 {
139 struct fixture *fix = init_fixture();
140
141 SYS_FAIL(out,
142 "%s set_global_vars.bpf.o -G \"var_s32 = 2147483648\" -vl2 2> %s",
143 fix->veristat, fix->tmpfile);
144
145 read_output(fix);
146 __CHECK_STR("is out of range [-2147483648; 2147483647]", "out of range");
147
148 out:
149 teardown_fixture(fix);
150 }
151
test_unsupported_ptr_array_type(void)152 static void test_unsupported_ptr_array_type(void)
153 {
154 struct fixture *fix = init_fixture();
155
156 SYS_FAIL(out,
157 "%s set_global_vars.bpf.o -G \"ptr_arr[0] = 0\" -vl2 2> %s",
158 fix->veristat, fix->tmpfile);
159
160 read_output(fix);
161 __CHECK_STR("Can't set ptr_arr[0]. Only ints and enums are supported", "ptr_arr");
162
163 out:
164 teardown_fixture(fix);
165 }
166
test_array_out_of_bounds(void)167 static void test_array_out_of_bounds(void)
168 {
169 struct fixture *fix = init_fixture();
170
171 SYS_FAIL(out,
172 "%s set_global_vars.bpf.o -G \"arr[99] = 0\" -vl2 2> %s",
173 fix->veristat, fix->tmpfile);
174
175 read_output(fix);
176 __CHECK_STR("Array index 99 is out of bounds", "arr[99]");
177
178 out:
179 teardown_fixture(fix);
180 }
181
test_array_index_not_found(void)182 static void test_array_index_not_found(void)
183 {
184 struct fixture *fix = init_fixture();
185
186 SYS_FAIL(out,
187 "%s set_global_vars.bpf.o -G \"arr[EG2] = 0\" -vl2 2> %s",
188 fix->veristat, fix->tmpfile);
189
190 read_output(fix);
191 __CHECK_STR("Can't resolve enum value EG2", "arr[EG2]");
192
193 out:
194 teardown_fixture(fix);
195 }
196
test_array_index_for_non_array(void)197 static void test_array_index_for_non_array(void)
198 {
199 struct fixture *fix = init_fixture();
200
201 SYS_FAIL(out,
202 "%s set_global_vars.bpf.o -G \"var_b[0] = 1\" -vl2 2> %s",
203 fix->veristat, fix->tmpfile);
204
205 pread(fix->fd, fix->output, fix->sz, 0);
206 __CHECK_STR("Array index is not expected for var_b", "var_b[0] = 1");
207
208 SYS_FAIL(out,
209 "%s set_global_vars.bpf.o -G \"union1.struct3[0].var_u8_l=1\" -vl2 2> %s",
210 fix->veristat, fix->tmpfile);
211
212 pread(fix->fd, fix->output, fix->sz, 0);
213 __CHECK_STR("Array index is not expected for struct3", "union1.struct3[0].var_u8_l=1");
214
215 out:
216 teardown_fixture(fix);
217 }
218
test_no_array_index_for_array(void)219 static void test_no_array_index_for_array(void)
220 {
221 struct fixture *fix = init_fixture();
222
223 SYS_FAIL(out,
224 "%s set_global_vars.bpf.o -G \"arr = 1\" -vl2 2> %s",
225 fix->veristat, fix->tmpfile);
226
227 pread(fix->fd, fix->output, fix->sz, 0);
228 __CHECK_STR("Can't set arr. Only ints and enums are supported", "arr = 1");
229
230 SYS_FAIL(out,
231 "%s set_global_vars.bpf.o -G \"struct1[0].struct2.u.var_u8[2]=1\" -vl2 2> %s",
232 fix->veristat, fix->tmpfile);
233
234 pread(fix->fd, fix->output, fix->sz, 0);
235 __CHECK_STR("Can't resolve field u for non-composite type", "struct1[0].struct2.u.var_u8[2]=1");
236
237 out:
238 teardown_fixture(fix);
239 }
240
241 /*
242 * Name filter tests below run veristat on veristat_foo.bpf.o and
243 * veristat_bar.bpf.o, both defining programs 'foo', 'bar' and 'buz'.
244 * Every entry describes a single (filters, file, prog) combination and
245 * tells whether that program is expected in the veristat output:
246 * 'true' if it is, 'false' if it is not and -1 if veristat is expected
247 * to reject the filter.
248 */
249 #define FILTER_OBJS "veristat_foo.bpf.o veristat_bar.bpf.o"
250
251 static const struct name_filter_case {
252 const char *filters;
253 const char *file;
254 const char *prog;
255 int included;
256 } name_filter_cases[] = {
257 /* no filters, every program is processed */
258 { "", "foo", "foo", true },
259 { "", "foo", "bar", true },
260 { "", "foo", "buz", true },
261 { "", "bar", "foo", true },
262 { "", "bar", "bar", true },
263 { "", "bar", "buz", true },
264 /* deny filters */
265 { "-f '!*foo*'", "foo", "bar", false },
266 { "-f '!*foo*'", "bar", "foo", false },
267 { "-f '!*foo*'", "bar", "bar", true },
268 { "-f '!*foo*/bar'", "foo", "bar", false },
269 { "-f '!*foo*/bar'", "foo", "buz", true },
270 { "-f '!*foo*/bar'", "bar", "bar", true },
271 { "-f '!*foo*/'", "foo", "bar", false },
272 { "-f '!*foo*/'", "bar", "bar", true },
273 { "-f '!/bar'", "foo", "bar", false },
274 { "-f '!/bar'", "foo", "foo", true },
275 { "-f '!/'", "foo", "bar", -1 },
276 { "-f '!'", "foo", "bar", -1 },
277 /* allow filters */
278 { "-f '*foo*'", "foo", "bar", true },
279 { "-f '*foo*'", "bar", "foo", true },
280 { "-f '*foo*'", "bar", "bar", false },
281 { "-f '*foo*/bar'", "foo", "bar", true },
282 { "-f '*foo*/bar'", "foo", "buz", false },
283 { "-f '*foo*/bar'", "bar", "bar", false },
284 { "-f '*foo*/'", "foo", "bar", true },
285 { "-f '*foo*/'", "bar", "bar", false },
286 { "-f '/bar'", "foo", "bar", true },
287 { "-f '/bar'", "foo", "foo", false },
288 { "-f '/'", "foo", "bar", -1 },
289 { "-f ''", "foo", "bar", -1 },
290 /* allow and deny filters combined */
291 { "-f '*foo*/' -f '!/bar'", "foo", "foo", true },
292 { "-f '*foo*/' -f '!/bar'", "foo", "bar", false },
293 { "-f '*foo*/' -f '!/bar'", "bar", "foo", false },
294 };
295
test_name_filters(void)296 static void test_name_filters(void)
297 {
298 struct fixture *fix = init_fixture();
299 const struct name_filter_case *t;
300 char cmd[512], row[64], name[128];
301 int i, err;
302
303 for (i = 0; i < ARRAY_SIZE(name_filter_cases); i++) {
304 t = &name_filter_cases[i];
305 /* stderr is merged with stdout in order to catch error messages */
306 snprintf(cmd, sizeof(cmd), "%s " FILTER_OBJS " -q -o csv -e file,prog %s > %s 2>&1",
307 fix->veristat, t->filters, fix->tmpfile);
308 err = system(cmd);
309 read_output(fix);
310
311 snprintf(row, sizeof(row), "veristat_%s.bpf.o,%s", t->file, t->prog);
312 snprintf(name, sizeof(name), "veristat %s: %s", t->filters, row);
313 switch (t->included) {
314 case true:
315 ASSERT_OK(err, name);
316 ASSERT_HAS_SUBSTR(fix->output, row, name);
317 break;
318 case false:
319 ASSERT_OK(err, name);
320 ASSERT_FALSE(!!strstr(fix->output, row), name);
321 break;
322 case -1:
323 ASSERT_NEQ(err, 0, name);
324 ASSERT_HAS_SUBSTR(fix->output, "Invalid filter", name);
325 break;
326 }
327 }
328
329 teardown_fixture(fix);
330 }
331
test_veristat(void)332 void test_veristat(void)
333 {
334 if (test__start_subtest("set_global_vars_succeeds"))
335 test_set_global_vars_succeeds();
336
337 if (test__start_subtest("set_global_vars_out_of_range"))
338 test_set_global_vars_out_of_range();
339
340 if (test__start_subtest("set_global_vars_from_file_succeeds"))
341 test_set_global_vars_from_file_succeeds();
342
343 if (test__start_subtest("test_unsupported_ptr_array_type"))
344 test_unsupported_ptr_array_type();
345
346 if (test__start_subtest("test_array_out_of_bounds"))
347 test_array_out_of_bounds();
348
349 if (test__start_subtest("test_array_index_not_found"))
350 test_array_index_not_found();
351
352 if (test__start_subtest("test_array_index_for_non_array"))
353 test_array_index_for_non_array();
354
355 if (test__start_subtest("test_no_array_index_for_array"))
356 test_no_array_index_for_array();
357
358 if (test__start_subtest("name_filters"))
359 test_name_filters();
360 }
361
362 #undef __CHECK_STR
363