Commit f0b7d119 authored by Shuyi Cheng's avatar Shuyi Cheng Committed by Andrii Nakryiko

selftests/bpf: Switch existing selftests to using open_opts for custom BTF

This patch mainly replaces the bpf_object_load_attr of
the core_autosize.c and core_reloc.c files with bpf_object_open_opts.
Signed-off-by: default avatarShuyi Cheng <chengshuyi@linux.alibaba.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1626180159-112996-4-git-send-email-chengshuyi@linux.alibaba.com
parent 18353c87
...@@ -53,8 +53,8 @@ void test_core_autosize(void) ...@@ -53,8 +53,8 @@ void test_core_autosize(void)
char btf_file[] = "/tmp/core_autosize.btf.XXXXXX"; char btf_file[] = "/tmp/core_autosize.btf.XXXXXX";
int err, fd = -1, zero = 0; int err, fd = -1, zero = 0;
int char_id, short_id, int_id, long_long_id, void_ptr_id, id; int char_id, short_id, int_id, long_long_id, void_ptr_id, id;
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
struct test_core_autosize* skel = NULL; struct test_core_autosize* skel = NULL;
struct bpf_object_load_attr load_attr = {};
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_map *bss_map; struct bpf_map *bss_map;
struct btf *btf = NULL; struct btf *btf = NULL;
...@@ -125,9 +125,10 @@ void test_core_autosize(void) ...@@ -125,9 +125,10 @@ void test_core_autosize(void)
fd = -1; fd = -1;
/* open and load BPF program with custom BTF as the kernel BTF */ /* open and load BPF program with custom BTF as the kernel BTF */
skel = test_core_autosize__open(); open_opts.btf_custom_path = btf_file;
skel = test_core_autosize__open_opts(&open_opts);
if (!ASSERT_OK_PTR(skel, "skel_open")) if (!ASSERT_OK_PTR(skel, "skel_open"))
return; goto cleanup;
/* disable handle_signed() for now */ /* disable handle_signed() for now */
prog = bpf_object__find_program_by_name(skel->obj, "handle_signed"); prog = bpf_object__find_program_by_name(skel->obj, "handle_signed");
...@@ -135,9 +136,7 @@ void test_core_autosize(void) ...@@ -135,9 +136,7 @@ void test_core_autosize(void)
goto cleanup; goto cleanup;
bpf_program__set_autoload(prog, false); bpf_program__set_autoload(prog, false);
load_attr.obj = skel->obj; err = bpf_object__load(skel->obj);
load_attr.target_btf_path = btf_file;
err = bpf_object__load_xattr(&load_attr);
if (!ASSERT_OK(err, "prog_load")) if (!ASSERT_OK(err, "prog_load"))
goto cleanup; goto cleanup;
...@@ -204,14 +203,13 @@ void test_core_autosize(void) ...@@ -204,14 +203,13 @@ void test_core_autosize(void)
skel = NULL; skel = NULL;
/* now re-load with handle_signed() enabled, it should fail loading */ /* now re-load with handle_signed() enabled, it should fail loading */
skel = test_core_autosize__open(); open_opts.btf_custom_path = btf_file;
skel = test_core_autosize__open_opts(&open_opts);
if (!ASSERT_OK_PTR(skel, "skel_open")) if (!ASSERT_OK_PTR(skel, "skel_open"))
return; goto cleanup;
load_attr.obj = skel->obj; err = test_core_autosize__load(skel);
load_attr.target_btf_path = btf_file; if (!ASSERT_ERR(err, "skel_load"))
err = bpf_object__load_xattr(&load_attr);
if (!ASSERT_ERR(err, "bad_prog_load"))
goto cleanup; goto cleanup;
cleanup: cleanup:
......
...@@ -816,7 +816,7 @@ static size_t roundup_page(size_t sz) ...@@ -816,7 +816,7 @@ static size_t roundup_page(size_t sz)
void test_core_reloc(void) void test_core_reloc(void)
{ {
const size_t mmap_sz = roundup_page(sizeof(struct data)); const size_t mmap_sz = roundup_page(sizeof(struct data));
struct bpf_object_load_attr load_attr = {}; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
struct core_reloc_test_case *test_case; struct core_reloc_test_case *test_case;
const char *tp_name, *probe_name; const char *tp_name, *probe_name;
int err, i, equal; int err, i, equal;
...@@ -846,9 +846,16 @@ void test_core_reloc(void) ...@@ -846,9 +846,16 @@ void test_core_reloc(void)
continue; continue;
} }
obj = bpf_object__open_file(test_case->bpf_obj_file, NULL); if (test_case->btf_src_file) {
err = access(test_case->btf_src_file, R_OK);
if (!ASSERT_OK(err, "btf_src_file"))
goto cleanup;
}
open_opts.btf_custom_path = test_case->btf_src_file;
obj = bpf_object__open_file(test_case->bpf_obj_file, &open_opts);
if (!ASSERT_OK_PTR(obj, "obj_open")) if (!ASSERT_OK_PTR(obj, "obj_open"))
continue; goto cleanup;
probe_name = "raw_tracepoint/sys_enter"; probe_name = "raw_tracepoint/sys_enter";
tp_name = "sys_enter"; tp_name = "sys_enter";
...@@ -862,17 +869,7 @@ void test_core_reloc(void) ...@@ -862,17 +869,7 @@ void test_core_reloc(void)
"prog '%s' not found\n", probe_name)) "prog '%s' not found\n", probe_name))
goto cleanup; goto cleanup;
err = bpf_object__load(obj);
if (test_case->btf_src_file) {
err = access(test_case->btf_src_file, R_OK);
if (!ASSERT_OK(err, "btf_src_file"))
goto cleanup;
}
load_attr.obj = obj;
load_attr.log_level = 0;
load_attr.target_btf_path = test_case->btf_src_file;
err = bpf_object__load_xattr(&load_attr);
if (err) { if (err) {
if (!test_case->fails) if (!test_case->fails)
ASSERT_OK(err, "obj_load"); ASSERT_OK(err, "obj_load");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment