bpf: selftests: test_loader: Support __btf_path() annotation
authorDaniel Xu <dxu@dxuuu.xyz>
Mon, 11 Dec 2023 20:20:08 +0000 (13:20 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Wed, 13 Dec 2023 23:42:19 +0000 (15:42 -0800)
This commit adds support for per-prog btf_custom_path. This is necessary
for testing CO-RE relocations on non-vmlinux types using test_loader
infrastructure.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Link: https://lore.kernel.org/r/660ea7f2fdbdd5103bc1af87c9fc931f05327926.1702325874.git.dxu@dxuuu.xyz
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/progs/bpf_misc.h
tools/testing/selftests/bpf/test_loader.c

index 799fff4995d87083dbd73b2ca913c1bad7a9afe0..2fd59970c43a2d082d62b1e65449e98707600e35 100644 (file)
@@ -71,6 +71,7 @@
 #define __retval_unpriv(val)   __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
 #define __auxiliary            __attribute__((btf_decl_tag("comment:test_auxiliary")))
 #define __auxiliary_unpriv     __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
+#define __btf_path(path)       __attribute__((btf_decl_tag("comment:test_btf_path=" path)))
 
 /* Convenience macro for use with 'asm volatile' blocks */
 #define __naked __attribute__((naked))
index a350ecdfba4a8215d34d0363c06d6fd7804e0ad8..74ceb7877ae220cc9d05d88e9b5121455f0360f9 100644 (file)
@@ -27,6 +27,7 @@
 #define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv="
 #define TEST_TAG_AUXILIARY "comment:test_auxiliary"
 #define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv"
+#define TEST_BTF_PATH "comment:test_btf_path="
 
 /* Warning: duplicated in bpf_misc.h */
 #define POINTER_VALUE  0xcafe4all
@@ -58,6 +59,7 @@ struct test_spec {
        const char *prog_name;
        struct test_subspec priv;
        struct test_subspec unpriv;
+       const char *btf_custom_path;
        int log_level;
        int prog_flags;
        int mode_mask;
@@ -288,6 +290,8 @@ static int parse_test_spec(struct test_loader *tester,
                                        goto cleanup;
                                update_flags(&spec->prog_flags, flags, clear);
                        }
+               } else if (str_has_pfx(s, TEST_BTF_PATH)) {
+                       spec->btf_custom_path = s + sizeof(TEST_BTF_PATH) - 1;
                }
        }
 
@@ -578,6 +582,9 @@ void run_subtest(struct test_loader *tester,
                }
        }
 
+       /* Implicitly reset to NULL if next test case doesn't specify */
+       open_opts->btf_custom_path = spec->btf_custom_path;
+
        tobj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, open_opts);
        if (!ASSERT_OK_PTR(tobj, "obj_open_mem")) /* shouldn't happen */
                goto subtest_cleanup;