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] https://github.com/llvm/llvm-project/pull/131731 [2] https://patchew.org/linux/20250506-default-const-init-clang-v2-1-fcfb69703264@xxxxxxxxxx/ Signed-off-by: Yonghong Song <yonghong.song@xxxxxxxxx> --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_bpf_unreachable.c | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_bpf_unreachable.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index e66a57970d28..790fff264ec6 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -14,6 +14,7 @@ #include "verifier_bounds_deduction_non_const.skel.h" #include "verifier_bounds_mix_sign_unsign.skel.h" #include "verifier_bpf_get_stack.skel.h" +#include "verifier_bpf_unreachable.skel.h" #include "verifier_bswap.skel.h" #include "verifier_btf_ctx_access.skel.h" #include "verifier_btf_unreliable_prog.skel.h" @@ -148,6 +149,7 @@ void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction); void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); } void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); } void test_verifier_bpf_get_stack(void) { RUN(verifier_bpf_get_stack); } +void test_verifier_bpf_unreachable(void) { RUN(verifier_bpf_unreachable); } void test_verifier_bswap(void) { RUN(verifier_bswap); } void test_verifier_btf_ctx_access(void) { RUN(verifier_btf_ctx_access); } void test_verifier_btf_unreliable_prog(void) { RUN(verifier_btf_unreliable_prog); } diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_unreachable.c b/tools/testing/selftests/bpf/progs/verifier_bpf_unreachable.c new file mode 100644 index 000000000000..c8b14b6022a5 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_bpf_unreachable.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" + +SEC("socket") +__description("bpf_unreachable with simple c code") +__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?") +void bpf_unreachable_with_simple_c(void) +{ + bpf_unreachable(); +} + +SEC("socket") +__description("bpf_unreachable as the second-from-last insn") +__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?") +__naked void bpf_unreachable_at_func_end(void) +{ + asm volatile ( + "r0 = 0;" + "call %[bpf_unreachable];" + "exit;" + : + : __imm(bpf_unreachable) + : __clobber_all); +} + +SEC("socket") +__description("dead code bpf_unreachable() in the middle of code") +__success +__naked void dead_bpf_unreachable_in_middle(void) +{ + asm volatile ( + "r0 = 0;" + "if r0 == 0 goto +1;" + "call %[bpf_unreachable];" + "r0 = 2;" + "exit;" + : + : __imm(bpf_unreachable) + : __clobber_all); +} + +SEC("socket") +__description("reachable bpf_unreachable() in the middle of code") +__failure __msg("unexpected bpf_unreachable() due to uninitialized variable?") +__naked void live_bpf_unreachable_in_middle(void) +{ + asm volatile ( + "r0 = 0;" + "if r0 == 1 goto +1;" + "call %[bpf_unreachable];" + "r0 = 2;" + "exit;" + : + : __imm(bpf_unreachable) + : __clobber_all); +} + +char _license[] SEC("license") = "GPL"; -- 2.47.1