From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from 69-171-232-181.mail-mxout.facebook.com (69-171-232-181.mail-mxout.facebook.com [69.171.232.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E42801F3BA9 for ; Mon, 19 May 2025 20:34:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=69.171.232.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747686842; cv=none; b=UgBRtO9097DBtvRzj03aDxmMg/qbGd5JhvSLlcXc7/7si5xqEuJmhLE5vYSOcwT+EwwOBi9EW4Rn6JubIY+UoFcIA54etv3Qv1aqZoxLzWevt2QkKMepUmvFGXXe0adw6ZEYQALv0d16EVYLMpVEuJyed7M7OsKGzEoKtIKpPm8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747686842; c=relaxed/simple; bh=U0ljfMwpbkZ6eyRPk696G3ijBUjLirZar1B4xN4VR2E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vu5IyHu/VAF8K2nrMdTcQ/QevddjNAIieU916HxyxP/by4NMslPjBne1BxCuQK7MVCjRXNdfvQzA8fpO4hFTySnNUVdElXRIW0GdOe86RLHQ8UVZ1nNII4oqwz/1tqqnRjPbwAwPRZk1KdcvB+Hd+8eAiCSAW0bt+DTqm/nrzwo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.client-ip=69.171.232.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devvm16039.vll0.facebook.com (Postfix, from userid 128203) id AEB4C7CA8835; Mon, 19 May 2025 13:33:49 -0700 (PDT) From: Yonghong Song To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@fb.com, Martin KaFai Lau 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 Message-ID: <20250519203349.2061406-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250519203339.2060080-1-yonghong.song@linux.dev> References: <20250519203339.2060080-1-yonghong.song@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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-fc= fb69703264@kernel.org/ Signed-off-by: Yonghong Song --- .../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_unreac= hable.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/te= sting/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(v= erifier_bounds_deduction); void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_b= ounds_deduction_non_const); } void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mi= x_sign_unsign); } void test_verifier_bpf_get_stack(void) { RUN(verifier_bpf_get_sta= ck); } +void test_verifier_bpf_unreachable(void) { RUN(verifier_bpf_unreach= able); } void test_verifier_bswap(void) { RUN(verifier_bswap); } void test_verifier_btf_ctx_access(void) { RUN(verifier_btf_ctx_acc= ess); } void test_verifier_btf_unreliable_prog(void) { RUN(verifier_btf_unrelia= ble_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 +#include +#include "bpf_misc.h" + +SEC("socket") +__description("bpf_unreachable with simple c code") +__failure __msg("unexpected bpf_unreachable() due to uninitialized varia= ble?") +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 varia= ble?") +__naked void bpf_unreachable_at_func_end(void) +{ + asm volatile ( + "r0 =3D 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 =3D 0;" + "if r0 =3D=3D 0 goto +1;" + "call %[bpf_unreachable];" + "r0 =3D 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 varia= ble?") +__naked void live_bpf_unreachable_in_middle(void) +{ + asm volatile ( + "r0 =3D 0;" + "if r0 =3D=3D 1 goto +1;" + "call %[bpf_unreachable];" + "r0 =3D 2;" + "exit;" + : + : __imm(bpf_unreachable) + : __clobber_all); +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.47.1