Commit 0d7fefeb authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Use non-autoloaded programs in few tests

Take advantage of new libbpf feature for declarative non-autoloaded BPF
program SEC() definitions in few test that test single program at a time
out of many available programs within the single BPF object.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220419002452.632125-2-andrii@kernel.org
parent a3820c48
...@@ -6,11 +6,10 @@ ...@@ -6,11 +6,10 @@
void test_helper_restricted(void) void test_helper_restricted(void)
{ {
int prog_i = 0, prog_cnt; int prog_i = 0, prog_cnt;
int duration = 0;
do { do {
struct test_helper_restricted *test; struct test_helper_restricted *test;
int maybeOK; int err;
test = test_helper_restricted__open(); test = test_helper_restricted__open();
if (!ASSERT_OK_PTR(test, "open")) if (!ASSERT_OK_PTR(test, "open"))
...@@ -21,12 +20,11 @@ void test_helper_restricted(void) ...@@ -21,12 +20,11 @@ void test_helper_restricted(void)
for (int j = 0; j < prog_cnt; ++j) { for (int j = 0; j < prog_cnt; ++j) {
struct bpf_program *prog = *test->skeleton->progs[j].prog; struct bpf_program *prog = *test->skeleton->progs[j].prog;
maybeOK = bpf_program__set_autoload(prog, prog_i == j); bpf_program__set_autoload(prog, true);
ASSERT_OK(maybeOK, "set autoload");
} }
maybeOK = test_helper_restricted__load(test); err = test_helper_restricted__load(test);
CHECK(!maybeOK, test->skeleton->progs[prog_i].name, "helper isn't restricted"); ASSERT_ERR(err, "load_should_fail");
test_helper_restricted__destroy(test); test_helper_restricted__destroy(test);
} while (++prog_i < prog_cnt); } while (++prog_i < prog_cnt);
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include <test_progs.h> #include <test_progs.h>
static void toggle_object_autoload_progs(const struct bpf_object *obj,
const char *name_load)
{
struct bpf_program *prog;
bpf_object__for_each_program(prog, obj) {
const char *name = bpf_program__name(prog);
if (!strcmp(name_load, name))
bpf_program__set_autoload(prog, true);
else
bpf_program__set_autoload(prog, false);
}
}
void test_reference_tracking(void) void test_reference_tracking(void)
{ {
const char *file = "test_sk_lookup_kern.o"; const char *file = "test_sk_lookup_kern.o";
...@@ -39,6 +24,7 @@ void test_reference_tracking(void) ...@@ -39,6 +24,7 @@ void test_reference_tracking(void)
goto cleanup; goto cleanup;
bpf_object__for_each_program(prog, obj_iter) { bpf_object__for_each_program(prog, obj_iter) {
struct bpf_program *p;
const char *name; const char *name;
name = bpf_program__name(prog); name = bpf_program__name(prog);
...@@ -49,7 +35,12 @@ void test_reference_tracking(void) ...@@ -49,7 +35,12 @@ void test_reference_tracking(void)
if (!ASSERT_OK_PTR(obj, "obj_open_file")) if (!ASSERT_OK_PTR(obj, "obj_open_file"))
goto cleanup; goto cleanup;
toggle_object_autoload_progs(obj, name); /* all programs are not loaded by default, so just set
* autoload to true for the single prog under test
*/
p = bpf_object__find_program_by_name(obj, name);
bpf_program__set_autoload(p, true);
/* Expect verifier failure if test name has 'err' */ /* Expect verifier failure if test name has 'err' */
if (strncmp(name, "err_", sizeof("err_") - 1) == 0) { if (strncmp(name, "err_", sizeof("err_") - 1) == 0) {
libbpf_print_fn_t old_print_fn; libbpf_print_fn_t old_print_fn;
......
...@@ -44,16 +44,12 @@ static void strncmp_full_str_cmp(struct strncmp_test *skel, const char *name, ...@@ -44,16 +44,12 @@ static void strncmp_full_str_cmp(struct strncmp_test *skel, const char *name,
static void test_strncmp_ret(void) static void test_strncmp_ret(void)
{ {
struct strncmp_test *skel; struct strncmp_test *skel;
struct bpf_program *prog;
int err, got; int err, got;
skel = strncmp_test__open(); skel = strncmp_test__open();
if (!ASSERT_OK_PTR(skel, "strncmp_test open")) if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
return; return;
bpf_object__for_each_program(prog, skel->obj)
bpf_program__set_autoload(prog, false);
bpf_program__set_autoload(skel->progs.do_strncmp, true); bpf_program__set_autoload(skel->progs.do_strncmp, true);
err = strncmp_test__load(skel); err = strncmp_test__load(skel);
...@@ -91,18 +87,13 @@ static void test_strncmp_ret(void) ...@@ -91,18 +87,13 @@ static void test_strncmp_ret(void)
static void test_strncmp_bad_not_const_str_size(void) static void test_strncmp_bad_not_const_str_size(void)
{ {
struct strncmp_test *skel; struct strncmp_test *skel;
struct bpf_program *prog;
int err; int err;
skel = strncmp_test__open(); skel = strncmp_test__open();
if (!ASSERT_OK_PTR(skel, "strncmp_test open")) if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
return; return;
bpf_object__for_each_program(prog, skel->obj) bpf_program__set_autoload(skel->progs.strncmp_bad_not_const_str_size, true);
bpf_program__set_autoload(prog, false);
bpf_program__set_autoload(skel->progs.strncmp_bad_not_const_str_size,
true);
err = strncmp_test__load(skel); err = strncmp_test__load(skel);
ASSERT_ERR(err, "strncmp_test load bad_not_const_str_size"); ASSERT_ERR(err, "strncmp_test load bad_not_const_str_size");
...@@ -113,18 +104,13 @@ static void test_strncmp_bad_not_const_str_size(void) ...@@ -113,18 +104,13 @@ static void test_strncmp_bad_not_const_str_size(void)
static void test_strncmp_bad_writable_target(void) static void test_strncmp_bad_writable_target(void)
{ {
struct strncmp_test *skel; struct strncmp_test *skel;
struct bpf_program *prog;
int err; int err;
skel = strncmp_test__open(); skel = strncmp_test__open();
if (!ASSERT_OK_PTR(skel, "strncmp_test open")) if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
return; return;
bpf_object__for_each_program(prog, skel->obj) bpf_program__set_autoload(skel->progs.strncmp_bad_writable_target, true);
bpf_program__set_autoload(prog, false);
bpf_program__set_autoload(skel->progs.strncmp_bad_writable_target,
true);
err = strncmp_test__load(skel); err = strncmp_test__load(skel);
ASSERT_ERR(err, "strncmp_test load bad_writable_target"); ASSERT_ERR(err, "strncmp_test load bad_writable_target");
...@@ -135,18 +121,13 @@ static void test_strncmp_bad_writable_target(void) ...@@ -135,18 +121,13 @@ static void test_strncmp_bad_writable_target(void)
static void test_strncmp_bad_not_null_term_target(void) static void test_strncmp_bad_not_null_term_target(void)
{ {
struct strncmp_test *skel; struct strncmp_test *skel;
struct bpf_program *prog;
int err; int err;
skel = strncmp_test__open(); skel = strncmp_test__open();
if (!ASSERT_OK_PTR(skel, "strncmp_test open")) if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
return; return;
bpf_object__for_each_program(prog, skel->obj) bpf_program__set_autoload(skel->progs.strncmp_bad_not_null_term_target, true);
bpf_program__set_autoload(prog, false);
bpf_program__set_autoload(skel->progs.strncmp_bad_not_null_term_target,
true);
err = strncmp_test__load(skel); err = strncmp_test__load(skel);
ASSERT_ERR(err, "strncmp_test load bad_not_null_term_target"); ASSERT_ERR(err, "strncmp_test load bad_not_null_term_target");
......
...@@ -19,7 +19,7 @@ unsigned int no_const_str_size = STRNCMP_STR_SZ; ...@@ -19,7 +19,7 @@ unsigned int no_const_str_size = STRNCMP_STR_SZ;
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int do_strncmp(void *ctx) int do_strncmp(void *ctx)
{ {
if ((bpf_get_current_pid_tgid() >> 32) != target_pid) if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
...@@ -29,7 +29,7 @@ int do_strncmp(void *ctx) ...@@ -29,7 +29,7 @@ int do_strncmp(void *ctx)
return 0; return 0;
} }
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int strncmp_bad_not_const_str_size(void *ctx) int strncmp_bad_not_const_str_size(void *ctx)
{ {
/* The value of string size is not const, so will fail */ /* The value of string size is not const, so will fail */
...@@ -37,7 +37,7 @@ int strncmp_bad_not_const_str_size(void *ctx) ...@@ -37,7 +37,7 @@ int strncmp_bad_not_const_str_size(void *ctx)
return 0; return 0;
} }
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int strncmp_bad_writable_target(void *ctx) int strncmp_bad_writable_target(void *ctx)
{ {
/* Compared target is not read-only, so will fail */ /* Compared target is not read-only, so will fail */
...@@ -45,7 +45,7 @@ int strncmp_bad_writable_target(void *ctx) ...@@ -45,7 +45,7 @@ int strncmp_bad_writable_target(void *ctx)
return 0; return 0;
} }
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int strncmp_bad_not_null_term_target(void *ctx) int strncmp_bad_not_null_term_target(void *ctx)
{ {
/* Compared target is not null-terminated, so will fail */ /* Compared target is not null-terminated, so will fail */
......
...@@ -56,7 +56,7 @@ static void spin_lock_work(void) ...@@ -56,7 +56,7 @@ static void spin_lock_work(void)
} }
} }
SEC("raw_tp/sys_enter") SEC("?raw_tp/sys_enter")
int raw_tp_timer(void *ctx) int raw_tp_timer(void *ctx)
{ {
timer_work(); timer_work();
...@@ -64,7 +64,7 @@ int raw_tp_timer(void *ctx) ...@@ -64,7 +64,7 @@ int raw_tp_timer(void *ctx)
return 0; return 0;
} }
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int tp_timer(void *ctx) int tp_timer(void *ctx)
{ {
timer_work(); timer_work();
...@@ -72,7 +72,7 @@ int tp_timer(void *ctx) ...@@ -72,7 +72,7 @@ int tp_timer(void *ctx)
return 0; return 0;
} }
SEC("kprobe/sys_nanosleep") SEC("?kprobe/sys_nanosleep")
int kprobe_timer(void *ctx) int kprobe_timer(void *ctx)
{ {
timer_work(); timer_work();
...@@ -80,7 +80,7 @@ int kprobe_timer(void *ctx) ...@@ -80,7 +80,7 @@ int kprobe_timer(void *ctx)
return 0; return 0;
} }
SEC("perf_event") SEC("?perf_event")
int perf_event_timer(void *ctx) int perf_event_timer(void *ctx)
{ {
timer_work(); timer_work();
...@@ -88,7 +88,7 @@ int perf_event_timer(void *ctx) ...@@ -88,7 +88,7 @@ int perf_event_timer(void *ctx)
return 0; return 0;
} }
SEC("raw_tp/sys_enter") SEC("?raw_tp/sys_enter")
int raw_tp_spin_lock(void *ctx) int raw_tp_spin_lock(void *ctx)
{ {
spin_lock_work(); spin_lock_work();
...@@ -96,7 +96,7 @@ int raw_tp_spin_lock(void *ctx) ...@@ -96,7 +96,7 @@ int raw_tp_spin_lock(void *ctx)
return 0; return 0;
} }
SEC("tp/syscalls/sys_enter_nanosleep") SEC("?tp/syscalls/sys_enter_nanosleep")
int tp_spin_lock(void *ctx) int tp_spin_lock(void *ctx)
{ {
spin_lock_work(); spin_lock_work();
...@@ -104,7 +104,7 @@ int tp_spin_lock(void *ctx) ...@@ -104,7 +104,7 @@ int tp_spin_lock(void *ctx)
return 0; return 0;
} }
SEC("kprobe/sys_nanosleep") SEC("?kprobe/sys_nanosleep")
int kprobe_spin_lock(void *ctx) int kprobe_spin_lock(void *ctx)
{ {
spin_lock_work(); spin_lock_work();
...@@ -112,7 +112,7 @@ int kprobe_spin_lock(void *ctx) ...@@ -112,7 +112,7 @@ int kprobe_spin_lock(void *ctx)
return 0; return 0;
} }
SEC("perf_event") SEC("?perf_event")
int perf_event_spin_lock(void *ctx) int perf_event_spin_lock(void *ctx)
{ {
spin_lock_work(); spin_lock_work();
......
...@@ -52,7 +52,7 @@ static struct bpf_sock_tuple *get_tuple(void *data, __u64 nh_off, ...@@ -52,7 +52,7 @@ static struct bpf_sock_tuple *get_tuple(void *data, __u64 nh_off,
return result; return result;
} }
SEC("tc") SEC("?tc")
int sk_lookup_success(struct __sk_buff *skb) int sk_lookup_success(struct __sk_buff *skb)
{ {
void *data_end = (void *)(long)skb->data_end; void *data_end = (void *)(long)skb->data_end;
...@@ -78,7 +78,7 @@ int sk_lookup_success(struct __sk_buff *skb) ...@@ -78,7 +78,7 @@ int sk_lookup_success(struct __sk_buff *skb)
return sk ? TC_ACT_OK : TC_ACT_UNSPEC; return sk ? TC_ACT_OK : TC_ACT_UNSPEC;
} }
SEC("tc") SEC("?tc")
int sk_lookup_success_simple(struct __sk_buff *skb) int sk_lookup_success_simple(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -90,7 +90,7 @@ int sk_lookup_success_simple(struct __sk_buff *skb) ...@@ -90,7 +90,7 @@ int sk_lookup_success_simple(struct __sk_buff *skb)
return 0; return 0;
} }
SEC("tc") SEC("?tc")
int err_use_after_free(struct __sk_buff *skb) int err_use_after_free(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -105,7 +105,7 @@ int err_use_after_free(struct __sk_buff *skb) ...@@ -105,7 +105,7 @@ int err_use_after_free(struct __sk_buff *skb)
return family; return family;
} }
SEC("tc") SEC("?tc")
int err_modify_sk_pointer(struct __sk_buff *skb) int err_modify_sk_pointer(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -120,7 +120,7 @@ int err_modify_sk_pointer(struct __sk_buff *skb) ...@@ -120,7 +120,7 @@ int err_modify_sk_pointer(struct __sk_buff *skb)
return 0; return 0;
} }
SEC("tc") SEC("?tc")
int err_modify_sk_or_null_pointer(struct __sk_buff *skb) int err_modify_sk_or_null_pointer(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -134,7 +134,7 @@ int err_modify_sk_or_null_pointer(struct __sk_buff *skb) ...@@ -134,7 +134,7 @@ int err_modify_sk_or_null_pointer(struct __sk_buff *skb)
return 0; return 0;
} }
SEC("tc") SEC("?tc")
int err_no_release(struct __sk_buff *skb) int err_no_release(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -143,7 +143,7 @@ int err_no_release(struct __sk_buff *skb) ...@@ -143,7 +143,7 @@ int err_no_release(struct __sk_buff *skb)
return 0; return 0;
} }
SEC("tc") SEC("?tc")
int err_release_twice(struct __sk_buff *skb) int err_release_twice(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -155,7 +155,7 @@ int err_release_twice(struct __sk_buff *skb) ...@@ -155,7 +155,7 @@ int err_release_twice(struct __sk_buff *skb)
return 0; return 0;
} }
SEC("tc") SEC("?tc")
int err_release_unchecked(struct __sk_buff *skb) int err_release_unchecked(struct __sk_buff *skb)
{ {
struct bpf_sock_tuple tuple = {}; struct bpf_sock_tuple tuple = {};
...@@ -172,7 +172,7 @@ void lookup_no_release(struct __sk_buff *skb) ...@@ -172,7 +172,7 @@ void lookup_no_release(struct __sk_buff *skb)
bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0); bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
} }
SEC("tc") SEC("?tc")
int err_no_release_subcall(struct __sk_buff *skb) int err_no_release_subcall(struct __sk_buff *skb)
{ {
lookup_no_release(skb); lookup_no_release(skb);
......
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