Skip to content

Commit e74d80a

Browse files
Yonghong SongKernel Patches Daemon
Yonghong Song
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add unit tests with bpf_unreachable() kfunc
The compiler support for bpf_unreachable() ([1]) will be in llvm21, but the current kernel will have a build failure with llvm21 ([2]). So all unit tests have explicit references to bpf_unreachable(). The test where bpf_unreachable() is generated by compiler will be provided later. [1] llvm/llvm-project#131731 [2] https://patchew.org/linux/[email protected]/ Signed-off-by: Yonghong Song <[email protected]>
1 parent 2859591 commit e74d80a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "verifier_bounds_deduction_non_const.skel.h"
1515
#include "verifier_bounds_mix_sign_unsign.skel.h"
1616
#include "verifier_bpf_get_stack.skel.h"
17+
#include "verifier_bpf_unreachable.skel.h"
1718
#include "verifier_bswap.skel.h"
1819
#include "verifier_btf_ctx_access.skel.h"
1920
#include "verifier_btf_unreliable_prog.skel.h"
@@ -148,6 +149,7 @@ void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction);
148149
void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); }
149150
void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); }
150151
void test_verifier_bpf_get_stack(void) { RUN(verifier_bpf_get_stack); }
152+
void test_verifier_bpf_unreachable(void) { RUN(verifier_bpf_unreachable); }
151153
void test_verifier_bswap(void) { RUN(verifier_bswap); }
152154
void test_verifier_btf_ctx_access(void) { RUN(verifier_btf_ctx_access); }
153155
void test_verifier_btf_unreliable_prog(void) { RUN(verifier_btf_unreliable_prog); }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
7+
SEC("socket")
8+
__description("bpf_unreachable with simple c code")
9+
__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?")
10+
void bpf_unreachable_with_simple_c(void)
11+
{
12+
bpf_unreachable();
13+
}
14+
15+
SEC("socket")
16+
__description("bpf_unreachable as the second-from-last insn")
17+
__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?")
18+
__naked void bpf_unreachable_at_func_end(void)
19+
{
20+
asm volatile (
21+
"r0 = 0;"
22+
"call %[bpf_unreachable];"
23+
"exit;"
24+
:
25+
: __imm(bpf_unreachable)
26+
: __clobber_all);
27+
}
28+
29+
SEC("socket")
30+
__description("dead code bpf_unreachable() in the middle of code")
31+
__success
32+
__naked void dead_bpf_unreachable_in_middle(void)
33+
{
34+
asm volatile (
35+
"r0 = 0;"
36+
"if r0 == 0 goto +1;"
37+
"call %[bpf_unreachable];"
38+
"r0 = 2;"
39+
"exit;"
40+
:
41+
: __imm(bpf_unreachable)
42+
: __clobber_all);
43+
}
44+
45+
SEC("socket")
46+
__description("reachable bpf_unreachable() in the middle of code")
47+
__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?")
48+
__naked void live_bpf_unreachable_in_middle(void)
49+
{
50+
asm volatile (
51+
"r0 = 0;"
52+
"if r0 == 1 goto +1;"
53+
"call %[bpf_unreachable];"
54+
"r0 = 2;"
55+
"exit;"
56+
:
57+
: __imm(bpf_unreachable)
58+
: __clobber_all);
59+
}
60+
61+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)