diff mbox series

[bpf-next,v2,2/3] selftests/bpf: Allow the program to select specific modes for testing

Message ID 20250213131214.164982-3-mrpre@163.com (mailing list archive)
State New
Headers show
Series bpf: Fix array bounds error with may_goto and add selftest | expand

Commit Message

Jiayuan Chen Feb. 13, 2025, 1:12 p.m. UTC
In some cases, the verification logic under the interpreter and JIT
differs, such as may_goto, and the test program behaves differently under
different runtime modes, requiring separate verification logic for each
result.

Signed-off-by: Jiayuan Chen <mrpre@163.com>
---
 tools/testing/selftests/bpf/progs/bpf_misc.h |  2 ++
 tools/testing/selftests/bpf/test_loader.c    | 27 ++++++++++++++++++++
 2 files changed, 29 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index f45f4352feeb..dd23822cfb5a 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -135,6 +135,8 @@ 
 #define __arch_arm64		__arch("ARM64")
 #define __arch_riscv64		__arch("RISCV64")
 #define __caps_unpriv(caps)	__attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
+#define __use_jit()		__attribute__((btf_decl_tag("comment:run_mode=jit")))
+#define __use_interp()		__attribute__((btf_decl_tag("comment:run_mode=interpreter")))
 
 /* Define common capabilities tested using __caps_unpriv */
 #define CAP_NET_ADMIN		12
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 53b06647cf57..2c23178d9a7a 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -37,6 +37,7 @@ 
 #define TEST_TAG_JITED_PFX "comment:test_jited="
 #define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
 #define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
+#define TEST_TAG_RUN_MODE_PFX "comment:run_mode="
 
 /* Warning: duplicated in bpf_misc.h */
 #define POINTER_VALUE	0xcafe4all
@@ -55,6 +56,11 @@  enum mode {
 	UNPRIV = 2
 };
 
+enum run_mode {
+	JIT = 1 << 0,
+	INTERP = 1 << 1
+};
+
 struct expect_msg {
 	const char *substr; /* substring match */
 	regex_t regex;
@@ -87,6 +93,7 @@  struct test_spec {
 	int prog_flags;
 	int mode_mask;
 	int arch_mask;
+	int run_mode;
 	bool auxiliary;
 	bool valid;
 };
@@ -406,6 +413,7 @@  static int parse_test_spec(struct test_loader *tester,
 	bool collect_jit = false;
 	int func_id, i, err = 0;
 	u32 arch_mask = 0;
+	u32 run_mode = 0;
 	struct btf *btf;
 	enum arch arch;
 
@@ -580,10 +588,22 @@  static int parse_test_spec(struct test_loader *tester,
 			if (err)
 				goto cleanup;
 			spec->mode_mask |= UNPRIV;
+		} else if (str_has_pfx(s, TEST_TAG_RUN_MODE_PFX)) {
+			val = s + sizeof(TEST_TAG_RUN_MODE_PFX) - 1;
+			if (strcmp(val, "jit") == 0) {
+				run_mode = JIT;
+			} else if (strcmp(val, "interpreter") == 0) {
+				run_mode = INTERP;
+			} else {
+				PRINT_FAIL("bad run mode spec: '%s'", val);
+				err = -EINVAL;
+				goto cleanup;
+			}
 		}
 	}
 
 	spec->arch_mask = arch_mask ?: -1;
+	spec->run_mode = run_mode ?: (JIT | INTERP);
 
 	if (spec->mode_mask == 0)
 		spec->mode_mask = PRIV;
@@ -930,6 +950,7 @@  void run_subtest(struct test_loader *tester,
 	struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
 	struct bpf_program *tprog = NULL, *tprog_iter;
 	struct bpf_link *link, *links[32] = {};
+	bool jit_enabled = is_jit_enabled();
 	struct test_spec *spec_iter;
 	struct cap_state caps = {};
 	struct bpf_object *tobj;
@@ -946,6 +967,12 @@  void run_subtest(struct test_loader *tester,
 		return;
 	}
 
+	if ((jit_enabled && spec->run_mode & INTERP) ||
+	    (!jit_enabled && spec->run_mode & JIT)) {
+		test__skip();
+		return;
+	}
+
 	if (unpriv) {
 		if (!can_execute_unpriv(tester, spec)) {
 			test__skip();