Skip to content

Commit 672ea96

Browse files
mykyta5Kernel Patches Daemon
authored andcommitted
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 <[email protected]>
1 parent 77eda48 commit 672ea96

File tree

2 files changed

+159
-24
lines changed

2 files changed

+159
-24
lines changed

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

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ 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][2].u.var_u8[2]=170\" "\
6767
" -G \"union1.struct3.var_u8_l = 0xaa\" "\
6868
" -G \"union1.struct3.var_u8_h = 0xaa\" "\
69-
"-vl2 > %s", fix->veristat, fix->tmpfile);
69+
" -G \"arr[3]= 171\" " \
70+
" -G \"arr[EA2] =172\" " \
71+
" -G \"enum_arr[EC2]=EA3\" " \
72+
" -G \"three_d[31][7][EA2]=173\"" \
73+
" -G \"struct1[2].struct2[1][2].u.mat[5][3]=174\" " \
74+
" -G \"struct11[7][5].struct2[0][1].u.mat[3][0] = 175\" " \
75+
" -vl2 > %s", fix->veristat, fix->tmpfile);
7076

7177
read(fix->fd, fix->output, fix->sz);
7278
__CHECK_STR("_w=0xf000000000000001 ", "var_s64 = 0xf000000000000001");
@@ -81,8 +87,14 @@ static void test_set_global_vars_succeeds(void)
8187
__CHECK_STR("_w=12 ", "var_eb = EB2");
8288
__CHECK_STR("_w=13 ", "var_ec = EC2");
8389
__CHECK_STR("_w=1 ", "var_b = 1");
84-
__CHECK_STR("_w=170 ", "struct1.struct2.u.var_u8 = 170");
90+
__CHECK_STR("_w=170 ", "struct1[2].struct2[1][2].u.var_u8[2]=170");
8591
__CHECK_STR("_w=0xaaaa ", "union1.var_u16 = 0xaaaa");
92+
__CHECK_STR("_w=171 ", "arr[3]= 171");
93+
__CHECK_STR("_w=172 ", "arr[EA2] =172");
94+
__CHECK_STR("_w=10 ", "enum_arr[EC2]=EA3");
95+
__CHECK_STR("_w=173 ", "matrix[31][7][11]=173");
96+
__CHECK_STR("_w=174 ", "struct1[2].struct2[1][2].u.mat[5][3]=174");
97+
__CHECK_STR("_w=175 ", "struct11[7][5].struct2[0][1].u.mat[3][0]=175");
8698

8799
out:
88100
teardown_fixture(fix);
@@ -129,6 +141,95 @@ static void test_set_global_vars_out_of_range(void)
129141
teardown_fixture(fix);
130142
}
131143

144+
static void test_unsupported_ptr_array_type(void)
145+
{
146+
struct fixture *fix = init_fixture();
147+
148+
SYS_FAIL(out,
149+
"%s set_global_vars.bpf.o -G \"ptr_arr[0] = 0\" -vl2 2> %s",
150+
fix->veristat, fix->tmpfile);
151+
152+
read(fix->fd, fix->output, fix->sz);
153+
__CHECK_STR("Can't set ptr_arr[0]. Only ints and enums are supported", "ptr_arr");
154+
155+
out:
156+
teardown_fixture(fix);
157+
}
158+
159+
static void test_array_out_of_bounds(void)
160+
{
161+
struct fixture *fix = init_fixture();
162+
163+
SYS_FAIL(out,
164+
"%s set_global_vars.bpf.o -G \"arr[99] = 0\" -vl2 2> %s",
165+
fix->veristat, fix->tmpfile);
166+
167+
read(fix->fd, fix->output, fix->sz);
168+
__CHECK_STR("Array index 99 is out of bounds", "arr[99]");
169+
170+
out:
171+
teardown_fixture(fix);
172+
}
173+
174+
static void test_array_index_not_found(void)
175+
{
176+
struct fixture *fix = init_fixture();
177+
178+
SYS_FAIL(out,
179+
"%s set_global_vars.bpf.o -G \"arr[EG2] = 0\" -vl2 2> %s",
180+
fix->veristat, fix->tmpfile);
181+
182+
read(fix->fd, fix->output, fix->sz);
183+
__CHECK_STR("Can't resolve enum value EG2", "arr[EG2]");
184+
185+
out:
186+
teardown_fixture(fix);
187+
}
188+
189+
static void test_array_index_for_non_array(void)
190+
{
191+
struct fixture *fix = init_fixture();
192+
193+
SYS_FAIL(out,
194+
"%s set_global_vars.bpf.o -G \"var_b[0] = 1\" -vl2 2> %s",
195+
fix->veristat, fix->tmpfile);
196+
197+
pread(fix->fd, fix->output, fix->sz, 0);
198+
__CHECK_STR("Array index is not expected for var_b", "var_b[0] = 1");
199+
200+
SYS_FAIL(out,
201+
"%s set_global_vars.bpf.o -G \"union1.struct3[0].var_u8_l=1\" -vl2 2> %s",
202+
fix->veristat, fix->tmpfile);
203+
204+
pread(fix->fd, fix->output, fix->sz, 0);
205+
__CHECK_STR("Array index is not expected for struct3", "union1.struct3[0].var_u8_l=1");
206+
207+
out:
208+
teardown_fixture(fix);
209+
}
210+
211+
static void test_no_array_index_for_array(void)
212+
{
213+
struct fixture *fix = init_fixture();
214+
215+
SYS_FAIL(out,
216+
"%s set_global_vars.bpf.o -G \"arr = 1\" -vl2 2> %s",
217+
fix->veristat, fix->tmpfile);
218+
219+
pread(fix->fd, fix->output, fix->sz, 0);
220+
__CHECK_STR("Can't set arr. Only ints and enums are supported", "arr = 1");
221+
222+
SYS_FAIL(out,
223+
"%s set_global_vars.bpf.o -G \"struct1[0].struct2.u.var_u8[2]=1\" -vl2 2> %s",
224+
fix->veristat, fix->tmpfile);
225+
226+
pread(fix->fd, fix->output, fix->sz, 0);
227+
__CHECK_STR("Can't resolve field u for non-composite type", "struct1[0].struct2.u.var_u8[2]=1");
228+
229+
out:
230+
teardown_fixture(fix);
231+
}
232+
132233
void test_veristat(void)
133234
{
134235
if (test__start_subtest("set_global_vars_succeeds"))
@@ -139,6 +240,22 @@ void test_veristat(void)
139240

140241
if (test__start_subtest("set_global_vars_from_file_succeeds"))
141242
test_set_global_vars_from_file_succeeds();
243+
244+
if (test__start_subtest("test_unsupported_ptr_array_type"))
245+
test_unsupported_ptr_array_type();
246+
247+
if (test__start_subtest("test_array_out_of_bounds"))
248+
test_array_out_of_bounds();
249+
250+
if (test__start_subtest("test_array_index_not_found"))
251+
test_array_index_not_found();
252+
253+
if (test__start_subtest("test_array_index_for_non_array"))
254+
test_array_index_for_non_array();
255+
256+
if (test__start_subtest("test_no_array_index_for_array"))
257+
test_no_array_index_for_array();
258+
142259
}
143260

144261
#undef __CHECK_STR

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

Lines changed: 37 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 three_d[47][19][17];
33+
const volatile i32 *ptr_arr[32];
2634

2735
struct Struct {
2836
int:16;
@@ -35,34 +43,38 @@ 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;
49+
s32 mat[7][5];
4150
} u;
4251
};
43-
} struct2;
52+
} struct2[2][4];
4453
};
4554

4655
const volatile __u32 stru = 0; /* same prefix as below */
47-
const volatile struct Struct struct1 = {.struct2 = {.u = {.var_u8 = 1}}};
56+
const volatile struct Struct struct1[3];
57+
const volatile struct Struct struct11[11][7];
4858

49-
union Union {
50-
__u16 var_u16;
51-
struct Struct3 {
52-
struct {
53-
__u8 var_u8_l;
54-
};
59+
struct Struct3 {
60+
struct {
61+
u8 var_u8_l;
62+
};
63+
struct {
5564
struct {
56-
struct {
57-
__u8 var_u8_h;
58-
};
65+
u8 var_u8_h;
5966
};
60-
} struct3;
67+
};
6168
};
6269

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

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

6779
SEC("socket")
6880
int test_set_globals(void *ctx)
@@ -81,8 +93,14 @@ int test_set_globals(void *ctx)
8193
a = var_eb;
8294
a = var_ec;
8395
a = var_b;
84-
a = struct1.struct2.u.var_u8;
96+
a = struct1[2].struct2[1][2].u.var_u8[2];
8597
a = union1.var_u16;
98+
a = arr[3];
99+
a = arr[EA2];
100+
a = enum_arr[EC2];
101+
a = three_d[31][7][EA2];
102+
a = struct1[2].struct2[1][2].u.mat[5][3];
103+
a = struct11[7][5].struct2[0][1].u.mat[3][0];
86104

87105
return a;
88106
}

0 commit comments

Comments
 (0)