Skip to content

Commit fc3ab17

Browse files
committed
Merge branch 'selftests-bpf-implement-setting-global-variables-in-veristat'
Mykyta Yatsenko says: ==================== selftests/bpf: implement setting global variables in veristat From: Mykyta Yatsenko <[email protected]> To better verify some complex BPF programs by veristat, it would be useful to preset global variables. This patch set implements this functionality and introduces tests for veristat. v4->v5 * Rework parsing to use sscanf for integers * Addressing nits v3->v4: * Fixing bug in set_global_var introduced by refactoring in previous patch set * Addressed nits from Eduard v2->v3: * Reworked parsing of the presets, using sscanf to split into variable and value, but still use strtoll/strtoull to support range checks when parsing integers * Fix test failures for no_alu32 & cpuv4 by checking if veristat binary is in parent folder * Introduce __CHECK_STR macro for simplifying checks in test * Modify tests into sub-tests ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 0ba0ef0 + 3d1033c commit fc3ab17

File tree

5 files changed

+495
-1
lines changed

5 files changed

+495
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
688688
$(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
689689
$(RESOLVE_BTFIDS) \
690690
$(TRUNNER_BPFTOOL) \
691+
$(OUTPUT)/veristat \
691692
| $(TRUNNER_BINARY)-extras
692693
$$(call msg,BINARY,,$$@)
693694
$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LDFLAGS) -o $$@
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
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+
strcpy(fix->veristat, "./veristat");
28+
else if (access("../veristat", F_OK) == 0)
29+
strcpy(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+
40+
static void teardown_fixture(struct fixture *fix)
41+
{
42+
free(fix->output);
43+
close(fix->fd);
44+
remove(fix->tmpfile);
45+
free(fix);
46+
}
47+
48+
static void test_set_global_vars_succeeds(void)
49+
{
50+
struct fixture *fix = init_fixture();
51+
52+
SYS(out,
53+
"%s set_global_vars.bpf.o"\
54+
" -G \"var_s64 = 0xf000000000000001\" "\
55+
" -G \"var_u64 = 0xfedcba9876543210\" "\
56+
" -G \"var_s32 = -0x80000000\" "\
57+
" -G \"var_u32 = 0x76543210\" "\
58+
" -G \"var_s16 = -32768\" "\
59+
" -G \"var_u16 = 60652\" "\
60+
" -G \"var_s8 = -128\" "\
61+
" -G \"var_u8 = 255\" "\
62+
" -G \"var_ea = EA2\" "\
63+
" -G \"var_eb = EB2\" "\
64+
" -G \"var_ec = EC2\" "\
65+
" -G \"var_b = 1\" "\
66+
"-vl2 > %s", fix->veristat, fix->tmpfile);
67+
68+
read(fix->fd, fix->output, fix->sz);
69+
__CHECK_STR("_w=0xf000000000000001 ", "var_s64 = 0xf000000000000001");
70+
__CHECK_STR("_w=0xfedcba9876543210 ", "var_u64 = 0xfedcba9876543210");
71+
__CHECK_STR("_w=0x80000000 ", "var_s32 = -0x80000000");
72+
__CHECK_STR("_w=0x76543210 ", "var_u32 = 0x76543210");
73+
__CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
74+
__CHECK_STR("_w=0xecec ", "var_u16 = 60652");
75+
__CHECK_STR("_w=128 ", "var_s8 = -128");
76+
__CHECK_STR("_w=255 ", "var_u8 = 255");
77+
__CHECK_STR("_w=11 ", "var_ea = EA2");
78+
__CHECK_STR("_w=12 ", "var_eb = EB2");
79+
__CHECK_STR("_w=13 ", "var_ec = EC2");
80+
__CHECK_STR("_w=1 ", "var_b = 1");
81+
82+
out:
83+
teardown_fixture(fix);
84+
}
85+
86+
static void test_set_global_vars_from_file_succeeds(void)
87+
{
88+
struct fixture *fix = init_fixture();
89+
char input_file[80];
90+
const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
91+
int fd;
92+
93+
snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
94+
fd = mkstemp(input_file);
95+
if (!ASSERT_GE(fd, 0, "valid fd"))
96+
goto out;
97+
98+
write(fd, vars, strlen(vars));
99+
syncfs(fd);
100+
SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
101+
fix->veristat, input_file, fix->tmpfile);
102+
read(fix->fd, fix->output, fix->sz);
103+
__CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
104+
__CHECK_STR("_w=0xecec ", "var_u16 = 60652");
105+
106+
out:
107+
close(fd);
108+
remove(input_file);
109+
teardown_fixture(fix);
110+
}
111+
112+
static void test_set_global_vars_out_of_range(void)
113+
{
114+
struct fixture *fix = init_fixture();
115+
116+
SYS_FAIL(out,
117+
"%s set_global_vars.bpf.o -G \"var_s32 = 2147483648\" -vl2 2> %s",
118+
fix->veristat, fix->tmpfile);
119+
120+
read(fix->fd, fix->output, fix->sz);
121+
__CHECK_STR("is out of range [-2147483648; 2147483647]", "out of range");
122+
123+
out:
124+
teardown_fixture(fix);
125+
}
126+
127+
void test_veristat(void)
128+
{
129+
if (test__start_subtest("set_global_vars_succeeds"))
130+
test_set_global_vars_succeeds();
131+
132+
if (test__start_subtest("set_global_vars_out_of_range"))
133+
test_set_global_vars_out_of_range();
134+
135+
if (test__start_subtest("set_global_vars_from_file_succeeds"))
136+
test_set_global_vars_from_file_succeeds();
137+
}
138+
139+
#undef __CHECK_STR
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#include "bpf_experimental.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
#include <stdbool.h>
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
enum Enum { EA1 = 0, EA2 = 11 };
11+
enum Enumu64 {EB1 = 0llu, EB2 = 12llu };
12+
enum Enums64 { EC1 = 0ll, EC2 = 13ll };
13+
14+
const volatile __s64 var_s64 = -1;
15+
const volatile __u64 var_u64 = 0;
16+
const volatile __s32 var_s32 = -1;
17+
const volatile __u32 var_u32 = 0;
18+
const volatile __s16 var_s16 = -1;
19+
const volatile __u16 var_u16 = 0;
20+
const volatile __s8 var_s8 = -1;
21+
const volatile __u8 var_u8 = 0;
22+
const volatile enum Enum var_ea = EA1;
23+
const volatile enum Enumu64 var_eb = EB1;
24+
const volatile enum Enums64 var_ec = EC1;
25+
const volatile bool var_b = false;
26+
27+
char arr[4] = {0};
28+
29+
SEC("socket")
30+
int test_set_globals(void *ctx)
31+
{
32+
volatile __s8 a;
33+
34+
a = var_s64;
35+
a = var_u64;
36+
a = var_s32;
37+
a = var_u32;
38+
a = var_s16;
39+
a = var_u16;
40+
a = var_s8;
41+
a = var_u8;
42+
a = var_ea;
43+
a = var_eb;
44+
a = var_ec;
45+
a = var_b;
46+
return a;
47+
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ void hexdump(const char *prefix, const void *buf, size_t len);
427427
goto goto_label; \
428428
})
429429

430+
#define SYS_FAIL(goto_label, fmt, ...) \
431+
({ \
432+
char cmd[1024]; \
433+
snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
434+
if (!ASSERT_NEQ(0, system(cmd), cmd)) \
435+
goto goto_label; \
436+
})
437+
430438
#define ALL_TO_DEV_NULL " >/dev/null 2>&1"
431439

432440
#define SYS_NOFAIL(fmt, ...) \

0 commit comments

Comments
 (0)