Skip to content

Commit 9034771

Browse files
committed
selftests/bpf: test array presets in veristat
Modify existing veristat tests to verify that array presets are applied as expected. Introduce few negative tests as well to check that common error modes are handled. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
1 parent d4b62c8 commit 9034771

File tree

2 files changed

+114
-23
lines changed

2 files changed

+114
-23
lines changed

tools/testing/selftests/bpf/prog_tests/test_veristat.c

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ static void test_set_global_vars_succeeds(void)
6060
" -G \"var_s8 = -128\" "\
6161
" -G \"var_u8 = 255\" "\
6262
" -G \"var_ea = EA2\" "\
63-
" -G \"var_eb = EB2\" "\
64-
" -G \"var_ec = EC2\" "\
63+
" -G \"var_eb = EB2\" "\
64+
" -G \"var_ec=EC2\" "\
6565
" -G \"var_b = 1\" "\
66-
" -G \"struct1.struct2.u.var_u8 = 170\" "\
66+
" -G \"struct1[2].struct2[1].u.var_u8[2]=170\" "\
6767
" -G \"union1.struct3.var_u8_l = 0xaa\" "\
6868
" -G \"union1.struct3.var_u8_h = 0xaa\" "\
69+
" -G \"arr[3]= 171\" " \
70+
" -G \"arr[EA2] =172\" " \
71+
" -G \"enum_arr[EC2]=EA3\" " \
6972
"-vl2 > %s", fix->veristat, fix->tmpfile);
7073

7174
read(fix->fd, fix->output, fix->sz);
@@ -81,8 +84,11 @@ static void test_set_global_vars_succeeds(void)
8184
__CHECK_STR("_w=12 ", "var_eb = EB2");
8285
__CHECK_STR("_w=13 ", "var_ec = EC2");
8386
__CHECK_STR("_w=1 ", "var_b = 1");
84-
__CHECK_STR("_w=170 ", "struct1.struct2.u.var_u8 = 170");
87+
__CHECK_STR("_w=170 ", "struct1.struct2[1].u.var_u8[2] = 170");
8588
__CHECK_STR("_w=0xaaaa ", "union1.var_u16 = 0xaaaa");
89+
__CHECK_STR("_w=171 ", "arr[3]= 171");
90+
__CHECK_STR("_w=172 ", "arr[EA2] =172");
91+
__CHECK_STR("_w=10 ", "enum_arr[EC2]=EA3");
8692

8793
out:
8894
teardown_fixture(fix);
@@ -129,6 +135,66 @@ static void test_set_global_vars_out_of_range(void)
129135
teardown_fixture(fix);
130136
}
131137

138+
static void test_unsupported_2dim_array_type(void)
139+
{
140+
struct fixture *fix = init_fixture();
141+
142+
SYS_FAIL(out,
143+
"%s set_global_vars.bpf.o -G \"matrix[0][0] = 1\" -vl2 2> %s",
144+
fix->veristat, fix->tmpfile);
145+
146+
read(fix->fd, fix->output, fix->sz);
147+
__CHECK_STR("Could not parse 'matrix[0][0]'", "matrix");
148+
149+
out:
150+
teardown_fixture(fix);
151+
}
152+
153+
static void test_unsupported_ptr_array_type(void)
154+
{
155+
struct fixture *fix = init_fixture();
156+
157+
SYS_FAIL(out,
158+
"%s set_global_vars.bpf.o -G \"ptr_arr[0] = 0\" -vl2 2> %s",
159+
fix->veristat, fix->tmpfile);
160+
161+
read(fix->fd, fix->output, fix->sz);
162+
__CHECK_STR("Unsupported array type for variable ptr_arr", "ptr_arr");
163+
164+
out:
165+
teardown_fixture(fix);
166+
}
167+
168+
static void test_array_out_of_bounds(void)
169+
{
170+
struct fixture *fix = init_fixture();
171+
172+
SYS_FAIL(out,
173+
"%s set_global_vars.bpf.o -G \"arr[99] = 0\" -vl2 2> %s",
174+
fix->veristat, fix->tmpfile);
175+
176+
read(fix->fd, fix->output, fix->sz);
177+
__CHECK_STR("Preset index 99 is invalid or out of bounds", "arr[99]");
178+
179+
out:
180+
teardown_fixture(fix);
181+
}
182+
183+
static void test_array_index_not_found(void)
184+
{
185+
struct fixture *fix = init_fixture();
186+
187+
SYS_FAIL(out,
188+
"%s set_global_vars.bpf.o -G \"arr[EG2] = 0\" -vl2 2> %s",
189+
fix->veristat, fix->tmpfile);
190+
191+
read(fix->fd, fix->output, fix->sz);
192+
__CHECK_STR("Could not find array index as enum value EG2", "arr[EG2]");
193+
194+
out:
195+
teardown_fixture(fix);
196+
}
197+
132198
void test_veristat(void)
133199
{
134200
if (test__start_subtest("set_global_vars_succeeds"))
@@ -139,6 +205,18 @@ void test_veristat(void)
139205

140206
if (test__start_subtest("set_global_vars_from_file_succeeds"))
141207
test_set_global_vars_from_file_succeeds();
208+
209+
if (test__start_subtest("test_unsupported_2dim_array_type"))
210+
test_unsupported_2dim_array_type();
211+
212+
if (test__start_subtest("test_unsupported_ptr_array_type"))
213+
test_unsupported_ptr_array_type();
214+
215+
if (test__start_subtest("test_array_out_of_bounds"))
216+
test_array_out_of_bounds();
217+
218+
if (test__start_subtest("test_array_index_not_found"))
219+
test_array_index_not_found();
142220
}
143221

144222
#undef __CHECK_STR

tools/testing/selftests/bpf/progs/set_global_vars.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77

88
char _license[] SEC("license") = "GPL";
99

10-
enum Enum { EA1 = 0, EA2 = 11 };
10+
typedef __s32 s32;
11+
typedef s32 i32;
12+
typedef __u8 u8;
13+
14+
enum Enum { EA1 = 0, EA2 = 11, EA3 = 10 };
1115
enum Enumu64 {EB1 = 0llu, EB2 = 12llu };
1216
enum Enums64 { EC1 = 0ll, EC2 = 13ll };
1317

1418
const volatile __s64 var_s64 = -1;
1519
const volatile __u64 var_u64 = 0;
16-
const volatile __s32 var_s32 = -1;
20+
const volatile i32 var_s32 = -1;
1721
const volatile __u32 var_u32 = 0;
1822
const volatile __s16 var_s16 = -1;
1923
const volatile __u16 var_u16 = 0;
2024
const volatile __s8 var_s8 = -1;
21-
const volatile __u8 var_u8 = 0;
25+
const volatile u8 var_u8 = 0;
2226
const volatile enum Enum var_ea = EA1;
2327
const volatile enum Enumu64 var_eb = EB1;
2428
const volatile enum Enums64 var_ec = EC1;
2529
const volatile bool var_b = false;
30+
const volatile i32 arr[32];
31+
const volatile enum Enum enum_arr[32];
32+
const volatile i32 matrix[32][32];
33+
const volatile i32 *ptr_arr[32];
2634

2735
struct Struct {
2836
int:16;
@@ -35,34 +43,36 @@ struct Struct {
3543
volatile struct {
3644
const int:1;
3745
union {
38-
const volatile __u8 var_u8;
46+
const volatile u8 var_u8[3];
3947
const volatile __s16 filler3;
4048
const int:1;
4149
} u;
4250
};
43-
} struct2;
51+
} struct2[2];
4452
};
4553

4654
const volatile __u32 stru = 0; /* same prefix as below */
47-
const volatile struct Struct struct1 = {.struct2 = {.u = {.var_u8 = 1}}};
55+
const volatile struct Struct struct1[3] = {{.struct2 = {{}, {.u = {.var_u8 = {1}}}}}};
4856

49-
union Union {
50-
__u16 var_u16;
51-
struct Struct3 {
52-
struct {
53-
__u8 var_u8_l;
54-
};
57+
struct Struct3 {
58+
struct {
59+
u8 var_u8_l;
60+
};
61+
struct {
5562
struct {
56-
struct {
57-
__u8 var_u8_h;
58-
};
63+
u8 var_u8_h;
5964
};
60-
} struct3;
65+
};
6166
};
6267

63-
const volatile union Union union1 = {.var_u16 = -1};
68+
typedef struct Struct3 Struct3_t;
6469

65-
char arr[4] = {0};
70+
union Union {
71+
__u16 var_u16;
72+
Struct3_t struct3;
73+
};
74+
75+
const volatile union Union union1 = {.var_u16 = -1};
6676

6777
SEC("socket")
6878
int test_set_globals(void *ctx)
@@ -81,8 +91,11 @@ int test_set_globals(void *ctx)
8191
a = var_eb;
8292
a = var_ec;
8393
a = var_b;
84-
a = struct1.struct2.u.var_u8;
94+
a = struct1[2].struct2[1].u.var_u8[2];
8595
a = union1.var_u16;
96+
a = arr[3];
97+
a = arr[EA2];
98+
a = enum_arr[EC2];
8699

87100
return a;
88101
}

0 commit comments

Comments
 (0)