Skip to content

Commit 86a17e4

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Add union argument tests using fexit programs
By referencing commit 1642a39 ("selftests/bpf: Add struct argument tests with fentry/fexit programs."), test the following cases for union argument support: * 8B union argument. * 16B union argument. cd tools/testing/selftests/bpf ./test_progs -t tracing_struct/union_args 472/3 tracing_struct/union_args:OK 472 tracing_struct:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Leon Hwang <[email protected]>
1 parent bd428b7 commit 86a17e4

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,39 @@ static void test_struct_many_args(void)
112112
tracing_struct_many_args__destroy(skel);
113113
}
114114

115+
static void test_union_args(void)
116+
{
117+
struct tracing_struct *skel;
118+
int err;
119+
120+
skel = tracing_struct__open_and_load();
121+
if (!ASSERT_OK_PTR(skel, "tracing_struct__open_and_load"))
122+
return;
123+
124+
err = tracing_struct__attach(skel);
125+
if (!ASSERT_OK(err, "tracing_struct__attach"))
126+
goto out;
127+
128+
ASSERT_OK(trigger_module_test_read(256), "trigger_read");
129+
130+
ASSERT_EQ(skel->bss->ut1_a_a, 1, "ut1:a.arg.a");
131+
ASSERT_EQ(skel->bss->ut1_b, 4, "ut1:b");
132+
ASSERT_EQ(skel->bss->ut1_c, 5, "ut1:c");
133+
134+
ASSERT_EQ(skel->bss->ut2_a, 6, "ut2:a");
135+
ASSERT_EQ(skel->bss->ut2_b_a, 2, "ut2:b.arg.a");
136+
ASSERT_EQ(skel->bss->ut2_b_b, 3, "ut2:b.arg.b");
137+
138+
out:
139+
tracing_struct__destroy(skel);
140+
}
141+
115142
void test_tracing_struct(void)
116143
{
117144
if (test__start_subtest("struct_args"))
118145
test_struct_args();
119146
if (test__start_subtest("struct_many_args"))
120147
test_struct_many_args();
148+
if (test__start_subtest("union_args"))
149+
test_union_args();
121150
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ struct bpf_testmod_struct_arg_3 {
1818
int b[];
1919
};
2020

21+
union bpf_testmod_union_arg_1 {
22+
char a;
23+
short b;
24+
struct bpf_testmod_struct_arg_1 arg;
25+
};
26+
27+
union bpf_testmod_union_arg_2 {
28+
int a;
29+
long b;
30+
struct bpf_testmod_struct_arg_2 arg;
31+
};
32+
2133
long t1_a_a, t1_a_b, t1_b, t1_c, t1_ret, t1_nregs;
2234
__u64 t1_reg0, t1_reg1, t1_reg2, t1_reg3;
2335
long t2_a, t2_b_a, t2_b_b, t2_c, t2_ret;
@@ -26,6 +38,9 @@ long t4_a_a, t4_b, t4_c, t4_d, t4_e_a, t4_e_b, t4_ret;
2638
long t5_ret;
2739
int t6;
2840

41+
long ut1_a_a, ut1_b, ut1_c;
42+
long ut2_a, ut2_b_a, ut2_b_b;
43+
2944
SEC("fentry/bpf_testmod_test_struct_arg_1")
3045
int BPF_PROG2(test_struct_arg_1, struct bpf_testmod_struct_arg_2, a, int, b, int, c)
3146
{
@@ -130,4 +145,22 @@ int BPF_PROG2(test_struct_arg_11, struct bpf_testmod_struct_arg_3 *, a)
130145
return 0;
131146
}
132147

148+
SEC("fexit/bpf_testmod_test_union_arg_1")
149+
int BPF_PROG2(test_union_arg_1, union bpf_testmod_union_arg_1, a, int, b, int, c)
150+
{
151+
ut1_a_a = a.arg.a;
152+
ut1_b = b;
153+
ut1_c = c;
154+
return 0;
155+
}
156+
157+
SEC("fexit/bpf_testmod_test_union_arg_2")
158+
int BPF_PROG2(test_union_arg_2, int, a, union bpf_testmod_union_arg_2, b)
159+
{
160+
ut2_a = a;
161+
ut2_b_a = b.arg.a;
162+
ut2_b_b = b.arg.b;
163+
return 0;
164+
}
165+
133166
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ struct bpf_testmod_struct_arg_5 {
6262
long d;
6363
};
6464

65+
union bpf_testmod_union_arg_1 {
66+
char a;
67+
short b;
68+
struct bpf_testmod_struct_arg_1 arg;
69+
};
70+
71+
union bpf_testmod_union_arg_2 {
72+
int a;
73+
long b;
74+
struct bpf_testmod_struct_arg_2 arg;
75+
};
76+
6577
__bpf_hook_start();
6678

6779
noinline int
@@ -128,6 +140,20 @@ bpf_testmod_test_struct_arg_9(u64 a, void *b, short c, int d, void *e, char f,
128140
return bpf_testmod_test_struct_arg_result;
129141
}
130142

143+
noinline int
144+
bpf_testmod_test_union_arg_1(union bpf_testmod_union_arg_1 a, int b, int c)
145+
{
146+
bpf_testmod_test_struct_arg_result = a.arg.a + b + c;
147+
return bpf_testmod_test_struct_arg_result;
148+
}
149+
150+
noinline int
151+
bpf_testmod_test_union_arg_2(int a, union bpf_testmod_union_arg_2 b)
152+
{
153+
bpf_testmod_test_struct_arg_result = a + b.arg.a + b.arg.b;
154+
return bpf_testmod_test_struct_arg_result;
155+
}
156+
131157
noinline int
132158
bpf_testmod_test_arg_ptr_to_struct(struct bpf_testmod_struct_arg_1 *a) {
133159
bpf_testmod_test_struct_arg_result = a->a;
@@ -408,6 +434,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
408434
struct bpf_testmod_struct_arg_3 *struct_arg3;
409435
struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
410436
struct bpf_testmod_struct_arg_5 struct_arg5 = {23, 24, 25, 26};
437+
union bpf_testmod_union_arg_1 union_arg1 = { .arg = {1} };
438+
union bpf_testmod_union_arg_2 union_arg2 = { .arg = {2, 3} };
411439
int i = 1;
412440

413441
while (bpf_testmod_return_ptr(i))
@@ -425,6 +453,9 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
425453
(void)bpf_testmod_test_struct_arg_9(16, (void *)17, 18, 19, (void *)20,
426454
21, 22, struct_arg5, 27);
427455

456+
(void)bpf_testmod_test_union_arg_1(union_arg1, 4, 5);
457+
(void)bpf_testmod_test_union_arg_2(6, union_arg2);
458+
428459
(void)bpf_testmod_test_arg_ptr_to_struct(&struct_arg1_2);
429460

430461
(void)trace_bpf_testmod_test_raw_tp_null_tp(NULL);

0 commit comments

Comments
 (0)