Skip to content

Commit 91a1f49

Browse files
etsalKernel Patches Daemon
authored andcommitted
selftests: Add selftests for void globals
Add additional testing for void global functions. The tests ensure that calls to void global functions properly keep R0 invalid. Also make sure that exception callbacks still require a return value. Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
1 parent 2f31ef6 commit 91a1f49

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "test_global_func15.skel.h"
1919
#include "test_global_func16.skel.h"
2020
#include "test_global_func17.skel.h"
21+
#include "test_global_func18.skel.h"
2122
#include "test_global_func_ctx_args.skel.h"
2223

2324
#include "bpf/libbpf_internal.h"
@@ -155,6 +156,7 @@ void test_test_global_funcs(void)
155156
RUN_TESTS(test_global_func15);
156157
RUN_TESTS(test_global_func16);
157158
RUN_TESTS(test_global_func17);
159+
RUN_TESTS(test_global_func18);
158160
RUN_TESTS(test_global_func_ctx_args);
159161

160162
if (test__start_subtest("ctx_arg_rewrite"))

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ struct {
2929
private(A) struct bpf_spin_lock lock;
3030
private(A) struct bpf_rb_root rbtree __contains(foo, node);
3131

32-
__noinline void *exception_cb_bad_ret_type(u64 cookie)
32+
__noinline void *exception_cb_bad_ret_type1(u64 cookie)
3333
{
3434
return NULL;
3535
}
3636

37+
__noinline void exception_cb_bad_ret_type2(u64 cookie)
38+
{
39+
}
40+
3741
__noinline int exception_cb_bad_arg_0(void)
3842
{
3943
return 0;
@@ -50,8 +54,8 @@ __noinline int exception_cb_ok_arg_small(int a)
5054
}
5155

5256
SEC("?tc")
53-
__exception_cb(exception_cb_bad_ret_type)
54-
__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.")
57+
__exception_cb(exception_cb_bad_ret_type1)
58+
__failure __msg("Global function exception_cb_bad_ret_type1() return value not void or scalar.")
5559
int reject_exception_cb_type_1(struct __sk_buff *ctx)
5660
{
5761
bpf_throw(0);
@@ -85,6 +89,15 @@ int reject_exception_cb_type_4(struct __sk_buff *ctx)
8589
return 0;
8690
}
8791

92+
SEC("?tc")
93+
__exception_cb(exception_cb_bad_ret_type2)
94+
__failure __msg("exception cb cannot return void")
95+
int reject_exception_cb_type_5(struct __sk_buff *ctx)
96+
{
97+
bpf_throw(0);
98+
return 0;
99+
}
100+
88101
__noinline
89102
static int timer_cb(void *map, int *key, struct bpf_timer *timer)
90103
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <vmlinux.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include "bpf_misc.h"
5+
6+
__weak
7+
void foo(void)
8+
{
9+
}
10+
11+
SEC("tc")
12+
__failure __msg("!read_ok")
13+
int global_func18(struct __sk_buff *skb)
14+
{
15+
foo();
16+
17+
asm volatile(
18+
"r1 = r0;"
19+
:::
20+
);
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)