forked from Rust-for-Linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Convert test_global_funcs test to test_loader framework
Convert 17 test_global_funcs subtests into test_loader framework for easier maintenance and more declarative way to define expected failures/successes. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
Showing
18 changed files
with
174 additions
and
123 deletions.
There are no files selected for viewing
131 changes: 34 additions & 97 deletions
131
tools/testing/selftests/bpf/prog_tests/test_global_funcs.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,41 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include <test_progs.h> | ||
|
||
const char *err_str; | ||
bool found; | ||
|
||
static int libbpf_debug_print(enum libbpf_print_level level, | ||
const char *format, va_list args) | ||
{ | ||
char *log_buf; | ||
|
||
if (level != LIBBPF_WARN || | ||
strcmp(format, "libbpf: \n%s\n")) { | ||
vprintf(format, args); | ||
return 0; | ||
} | ||
|
||
log_buf = va_arg(args, char *); | ||
if (!log_buf) | ||
goto out; | ||
if (err_str && strstr(log_buf, err_str) == 0) | ||
found = true; | ||
out: | ||
printf(format, log_buf); | ||
return 0; | ||
} | ||
|
||
extern int extra_prog_load_log_flags; | ||
|
||
static int check_load(const char *file) | ||
{ | ||
struct bpf_object *obj = NULL; | ||
struct bpf_program *prog; | ||
int err; | ||
|
||
found = false; | ||
|
||
obj = bpf_object__open_file(file, NULL); | ||
err = libbpf_get_error(obj); | ||
if (err) | ||
return err; | ||
|
||
prog = bpf_object__next_program(obj, NULL); | ||
if (!prog) { | ||
err = -ENOENT; | ||
goto err_out; | ||
} | ||
|
||
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32); | ||
bpf_program__set_log_level(prog, extra_prog_load_log_flags); | ||
|
||
err = bpf_object__load(obj); | ||
|
||
err_out: | ||
bpf_object__close(obj); | ||
return err; | ||
} | ||
|
||
struct test_def { | ||
const char *file; | ||
const char *err_str; | ||
}; | ||
#include "test_global_func1.skel.h" | ||
#include "test_global_func2.skel.h" | ||
#include "test_global_func3.skel.h" | ||
#include "test_global_func4.skel.h" | ||
#include "test_global_func5.skel.h" | ||
#include "test_global_func6.skel.h" | ||
#include "test_global_func7.skel.h" | ||
#include "test_global_func8.skel.h" | ||
#include "test_global_func9.skel.h" | ||
#include "test_global_func10.skel.h" | ||
#include "test_global_func11.skel.h" | ||
#include "test_global_func12.skel.h" | ||
#include "test_global_func13.skel.h" | ||
#include "test_global_func14.skel.h" | ||
#include "test_global_func15.skel.h" | ||
#include "test_global_func16.skel.h" | ||
#include "test_global_func17.skel.h" | ||
|
||
void test_test_global_funcs(void) | ||
{ | ||
struct test_def tests[] = { | ||
{ "test_global_func1.bpf.o", "combined stack size of 4 calls is 544" }, | ||
{ "test_global_func2.bpf.o" }, | ||
{ "test_global_func3.bpf.o", "the call stack of 8 frames" }, | ||
{ "test_global_func4.bpf.o" }, | ||
{ "test_global_func5.bpf.o", "expected pointer to ctx, but got PTR" }, | ||
{ "test_global_func6.bpf.o", "modified ctx ptr R2" }, | ||
{ "test_global_func7.bpf.o", "foo() doesn't return scalar" }, | ||
{ "test_global_func8.bpf.o" }, | ||
{ "test_global_func9.bpf.o" }, | ||
{ "test_global_func10.bpf.o", "invalid indirect read from stack" }, | ||
{ "test_global_func11.bpf.o", "Caller passes invalid args into func#1" }, | ||
{ "test_global_func12.bpf.o", "invalid mem access 'mem_or_null'" }, | ||
{ "test_global_func13.bpf.o", "Caller passes invalid args into func#1" }, | ||
{ "test_global_func14.bpf.o", "reference type('FWD S') size cannot be determined" }, | ||
{ "test_global_func15.bpf.o", "At program exit the register R0 has value" }, | ||
{ "test_global_func16.bpf.o", "invalid indirect read from stack" }, | ||
{ "test_global_func17.bpf.o", "Caller passes invalid args into func#1" }, | ||
}; | ||
libbpf_print_fn_t old_print_fn = NULL; | ||
int err, i, duration = 0; | ||
|
||
old_print_fn = libbpf_set_print(libbpf_debug_print); | ||
|
||
for (i = 0; i < ARRAY_SIZE(tests); i++) { | ||
const struct test_def *test = &tests[i]; | ||
|
||
if (!test__start_subtest(test->file)) | ||
continue; | ||
|
||
err_str = test->err_str; | ||
err = check_load(test->file); | ||
CHECK_FAIL(!!err ^ !!err_str); | ||
if (err_str) | ||
CHECK(found, "", "expected string '%s'", err_str); | ||
} | ||
libbpf_set_print(old_print_fn); | ||
RUN_TESTS(test_global_func1); | ||
RUN_TESTS(test_global_func2); | ||
RUN_TESTS(test_global_func3); | ||
RUN_TESTS(test_global_func4); | ||
RUN_TESTS(test_global_func5); | ||
RUN_TESTS(test_global_func6); | ||
RUN_TESTS(test_global_func7); | ||
RUN_TESTS(test_global_func8); | ||
RUN_TESTS(test_global_func9); | ||
RUN_TESTS(test_global_func10); | ||
RUN_TESTS(test_global_func11); | ||
RUN_TESTS(test_global_func12); | ||
RUN_TESTS(test_global_func13); | ||
RUN_TESTS(test_global_func14); | ||
RUN_TESTS(test_global_func15); | ||
RUN_TESTS(test_global_func16); | ||
RUN_TESTS(test_global_func17); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,45 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
/* Copyright (c) 2020 Facebook */ | ||
#include <stddef.h> | ||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_misc.h" | ||
|
||
#define MAX_STACK (512 - 3 * 32) | ||
#include "test_global_func1.c" | ||
|
||
static __attribute__ ((noinline)) | ||
int f0(int var, struct __sk_buff *skb) | ||
{ | ||
return skb->len; | ||
} | ||
|
||
__attribute__ ((noinline)) | ||
int f1(struct __sk_buff *skb) | ||
{ | ||
volatile char buf[MAX_STACK] = {}; | ||
|
||
return f0(0, skb) + skb->len; | ||
} | ||
|
||
int f3(int, struct __sk_buff *skb, int); | ||
|
||
__attribute__ ((noinline)) | ||
int f2(int val, struct __sk_buff *skb) | ||
{ | ||
return f1(skb) + f3(val, skb, 1); | ||
} | ||
|
||
__attribute__ ((noinline)) | ||
int f3(int val, struct __sk_buff *skb, int var) | ||
{ | ||
volatile char buf[MAX_STACK] = {}; | ||
|
||
return skb->ifindex * val * var; | ||
} | ||
|
||
SEC("tc") | ||
__success | ||
int global_func2(struct __sk_buff *skb) | ||
{ | ||
return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.