Skip to content

Commit 92de53d

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
selftests/bpf: Add unit tests with __bpf_trap() kfunc
Add some inline-asm tests and C tests where __bpf_trap() or __builtin_trap() is used in the code. The __builtin_trap() test is guarded with llvm21 ([1]) since otherwise the compilation failure will happen. [1] llvm/llvm-project#131731 Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20250523205331.1291734-1-yonghong.song@linux.dev Tested-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent f95695f commit 92de53d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-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_trap.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_trap(void) { RUN(verifier_bpf_trap); }
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
#if __clang_major__ >= 21 && 0
8+
SEC("socket")
9+
__description("__builtin_trap with simple c code")
10+
__failure __msg("unexpected __bpf_trap() due to uninitialized variable?")
11+
void bpf_builtin_trap_with_simple_c(void)
12+
{
13+
__builtin_trap();
14+
}
15+
#endif
16+
17+
SEC("socket")
18+
__description("__bpf_trap with simple c code")
19+
__failure __msg("unexpected __bpf_trap() due to uninitialized variable?")
20+
void bpf_trap_with_simple_c(void)
21+
{
22+
__bpf_trap();
23+
}
24+
25+
SEC("socket")
26+
__description("__bpf_trap as the second-from-last insn")
27+
__failure __msg("unexpected __bpf_trap() due to uninitialized variable?")
28+
__naked void bpf_trap_at_func_end(void)
29+
{
30+
asm volatile (
31+
"r0 = 0;"
32+
"call %[__bpf_trap];"
33+
"exit;"
34+
:
35+
: __imm(__bpf_trap)
36+
: __clobber_all);
37+
}
38+
39+
SEC("socket")
40+
__description("dead code __bpf_trap in the middle of code")
41+
__success
42+
__naked void dead_bpf_trap_in_middle(void)
43+
{
44+
asm volatile (
45+
"r0 = 0;"
46+
"if r0 == 0 goto +1;"
47+
"call %[__bpf_trap];"
48+
"r0 = 2;"
49+
"exit;"
50+
:
51+
: __imm(__bpf_trap)
52+
: __clobber_all);
53+
}
54+
55+
SEC("socket")
56+
__description("reachable __bpf_trap in the middle of code")
57+
__failure __msg("unexpected __bpf_trap() due to uninitialized variable?")
58+
__naked void live_bpf_trap_in_middle(void)
59+
{
60+
asm volatile (
61+
"r0 = 0;"
62+
"if r0 == 1 goto +1;"
63+
"call %[__bpf_trap];"
64+
"r0 = 2;"
65+
"exit;"
66+
:
67+
: __imm(__bpf_trap)
68+
: __clobber_all);
69+
}
70+
71+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)