BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>
Subject: [PATCH bpf-next v3 2/2] selftests/bpf: Add unit tests with bpf_unreachable() kfunc
Date: Mon, 19 May 2025 13:33:49 -0700	[thread overview]
Message-ID: <20250519203349.2061406-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20250519203339.2060080-1-yonghong.song@linux.dev>

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@kernel.org/

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 .../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


      parent reply	other threads:[~2025-05-19 20:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-19 20:33 [PATCH bpf-next v3 0/2] bpf: Warn with bpf_unreachable() kfunc maybe due to uninitialized variable Yonghong Song
2025-05-19 20:33 ` [PATCH bpf-next v3 1/2] " Yonghong Song
2025-05-19 22:48   ` Alexei Starovoitov
2025-05-20 15:25     ` Yonghong Song
2025-05-20 16:29       ` Alexei Starovoitov
2025-05-20 18:01         ` Yonghong Song
2025-05-20 18:39           ` Alexei Starovoitov
2025-05-20 18:46             ` Alexei Starovoitov
2025-05-20 19:50               ` Yonghong Song
2025-05-20 19:40             ` Yonghong Song
2025-05-20 20:59             ` Yonghong Song
2025-05-20 21:14               ` Alexei Starovoitov
2025-05-19 20:33 ` Yonghong Song [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250519203349.2061406-1-yonghong.song@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox