Skip to content

Commit 95ebb37

Browse files
anakryikoborkmann
authored andcommitted
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 <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Stanislav Fomichev <sdf@google.com> Link: https://lore.kernel.org/bpf/20230216045954.3002473-3-andrii@kernel.org
1 parent d384dce commit 95ebb37

18 files changed

+174
-123
lines changed
Lines changed: 34 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,41 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2020 Facebook */
33
#include <test_progs.h>
4-
5-
const char *err_str;
6-
bool found;
7-
8-
static int libbpf_debug_print(enum libbpf_print_level level,
9-
const char *format, va_list args)
10-
{
11-
char *log_buf;
12-
13-
if (level != LIBBPF_WARN ||
14-
strcmp(format, "libbpf: \n%s\n")) {
15-
vprintf(format, args);
16-
return 0;
17-
}
18-
19-
log_buf = va_arg(args, char *);
20-
if (!log_buf)
21-
goto out;
22-
if (err_str && strstr(log_buf, err_str) == 0)
23-
found = true;
24-
out:
25-
printf(format, log_buf);
26-
return 0;
27-
}
28-
29-
extern int extra_prog_load_log_flags;
30-
31-
static int check_load(const char *file)
32-
{
33-
struct bpf_object *obj = NULL;
34-
struct bpf_program *prog;
35-
int err;
36-
37-
found = false;
38-
39-
obj = bpf_object__open_file(file, NULL);
40-
err = libbpf_get_error(obj);
41-
if (err)
42-
return err;
43-
44-
prog = bpf_object__next_program(obj, NULL);
45-
if (!prog) {
46-
err = -ENOENT;
47-
goto err_out;
48-
}
49-
50-
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
51-
bpf_program__set_log_level(prog, extra_prog_load_log_flags);
52-
53-
err = bpf_object__load(obj);
54-
55-
err_out:
56-
bpf_object__close(obj);
57-
return err;
58-
}
59-
60-
struct test_def {
61-
const char *file;
62-
const char *err_str;
63-
};
4+
#include "test_global_func1.skel.h"
5+
#include "test_global_func2.skel.h"
6+
#include "test_global_func3.skel.h"
7+
#include "test_global_func4.skel.h"
8+
#include "test_global_func5.skel.h"
9+
#include "test_global_func6.skel.h"
10+
#include "test_global_func7.skel.h"
11+
#include "test_global_func8.skel.h"
12+
#include "test_global_func9.skel.h"
13+
#include "test_global_func10.skel.h"
14+
#include "test_global_func11.skel.h"
15+
#include "test_global_func12.skel.h"
16+
#include "test_global_func13.skel.h"
17+
#include "test_global_func14.skel.h"
18+
#include "test_global_func15.skel.h"
19+
#include "test_global_func16.skel.h"
20+
#include "test_global_func17.skel.h"
6421

6522
void test_test_global_funcs(void)
6623
{
67-
struct test_def tests[] = {
68-
{ "test_global_func1.bpf.o", "combined stack size of 4 calls is 544" },
69-
{ "test_global_func2.bpf.o" },
70-
{ "test_global_func3.bpf.o", "the call stack of 8 frames" },
71-
{ "test_global_func4.bpf.o" },
72-
{ "test_global_func5.bpf.o", "expected pointer to ctx, but got PTR" },
73-
{ "test_global_func6.bpf.o", "modified ctx ptr R2" },
74-
{ "test_global_func7.bpf.o", "foo() doesn't return scalar" },
75-
{ "test_global_func8.bpf.o" },
76-
{ "test_global_func9.bpf.o" },
77-
{ "test_global_func10.bpf.o", "invalid indirect read from stack" },
78-
{ "test_global_func11.bpf.o", "Caller passes invalid args into func#1" },
79-
{ "test_global_func12.bpf.o", "invalid mem access 'mem_or_null'" },
80-
{ "test_global_func13.bpf.o", "Caller passes invalid args into func#1" },
81-
{ "test_global_func14.bpf.o", "reference type('FWD S') size cannot be determined" },
82-
{ "test_global_func15.bpf.o", "At program exit the register R0 has value" },
83-
{ "test_global_func16.bpf.o", "invalid indirect read from stack" },
84-
{ "test_global_func17.bpf.o", "Caller passes invalid args into func#1" },
85-
};
86-
libbpf_print_fn_t old_print_fn = NULL;
87-
int err, i, duration = 0;
88-
89-
old_print_fn = libbpf_set_print(libbpf_debug_print);
90-
91-
for (i = 0; i < ARRAY_SIZE(tests); i++) {
92-
const struct test_def *test = &tests[i];
93-
94-
if (!test__start_subtest(test->file))
95-
continue;
96-
97-
err_str = test->err_str;
98-
err = check_load(test->file);
99-
CHECK_FAIL(!!err ^ !!err_str);
100-
if (err_str)
101-
CHECK(found, "", "expected string '%s'", err_str);
102-
}
103-
libbpf_set_print(old_print_fn);
24+
RUN_TESTS(test_global_func1);
25+
RUN_TESTS(test_global_func2);
26+
RUN_TESTS(test_global_func3);
27+
RUN_TESTS(test_global_func4);
28+
RUN_TESTS(test_global_func5);
29+
RUN_TESTS(test_global_func6);
30+
RUN_TESTS(test_global_func7);
31+
RUN_TESTS(test_global_func8);
32+
RUN_TESTS(test_global_func9);
33+
RUN_TESTS(test_global_func10);
34+
RUN_TESTS(test_global_func11);
35+
RUN_TESTS(test_global_func12);
36+
RUN_TESTS(test_global_func13);
37+
RUN_TESTS(test_global_func14);
38+
RUN_TESTS(test_global_func15);
39+
RUN_TESTS(test_global_func16);
40+
RUN_TESTS(test_global_func17);
10441
}

tools/testing/selftests/bpf/progs/test_global_func1.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#include <stddef.h>
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
67

7-
#ifndef MAX_STACK
88
#define MAX_STACK (512 - 3 * 32 + 8)
9-
#endif
109

1110
static __attribute__ ((noinline))
1211
int f0(int var, struct __sk_buff *skb)
@@ -39,7 +38,8 @@ int f3(int val, struct __sk_buff *skb, int var)
3938
}
4039

4140
SEC("tc")
42-
int test_cls(struct __sk_buff *skb)
41+
__failure __msg("combined stack size of 4 calls is 544")
42+
int global_func1(struct __sk_buff *skb)
4343
{
4444
return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
4545
}

tools/testing/selftests/bpf/progs/test_global_func10.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
struct Small {
78
int x;
@@ -21,7 +22,8 @@ __noinline int foo(const struct Big *big)
2122
}
2223

2324
SEC("cgroup_skb/ingress")
24-
int test_cls(struct __sk_buff *skb)
25+
__failure __msg("invalid indirect read from stack")
26+
int global_func10(struct __sk_buff *skb)
2527
{
2628
const struct Small small = {.x = skb->len };
2729

tools/testing/selftests/bpf/progs/test_global_func11.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
struct S {
78
int x;
@@ -13,7 +14,8 @@ __noinline int foo(const struct S *s)
1314
}
1415

1516
SEC("cgroup_skb/ingress")
16-
int test_cls(struct __sk_buff *skb)
17+
__failure __msg("Caller passes invalid args into func#1")
18+
int global_func11(struct __sk_buff *skb)
1719
{
1820
return foo((const void *)skb);
1921
}

tools/testing/selftests/bpf/progs/test_global_func12.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
struct S {
78
int x;
@@ -13,7 +14,8 @@ __noinline int foo(const struct S *s)
1314
}
1415

1516
SEC("cgroup_skb/ingress")
16-
int test_cls(struct __sk_buff *skb)
17+
__failure __msg("invalid mem access 'mem_or_null'")
18+
int global_func12(struct __sk_buff *skb)
1719
{
1820
const struct S s = {.x = skb->len };
1921

tools/testing/selftests/bpf/progs/test_global_func13.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
struct S {
78
int x;
@@ -16,7 +17,8 @@ __noinline int foo(const struct S *s)
1617
}
1718

1819
SEC("cgroup_skb/ingress")
19-
int test_cls(struct __sk_buff *skb)
20+
__failure __msg("Caller passes invalid args into func#1")
21+
int global_func13(struct __sk_buff *skb)
2022
{
2123
const struct S *s = (const struct S *)(0xbedabeda);
2224

tools/testing/selftests/bpf/progs/test_global_func14.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
struct S;
78

@@ -14,7 +15,8 @@ __noinline int foo(const struct S *s)
1415
}
1516

1617
SEC("cgroup_skb/ingress")
17-
int test_cls(struct __sk_buff *skb)
18+
__failure __msg("reference type('FWD S') size cannot be determined")
19+
int global_func14(struct __sk_buff *skb)
1820
{
1921

2022
return foo(NULL);

tools/testing/selftests/bpf/progs/test_global_func15.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
__noinline int foo(unsigned int *v)
78
{
@@ -12,7 +13,8 @@ __noinline int foo(unsigned int *v)
1213
}
1314

1415
SEC("cgroup_skb/ingress")
15-
int test_cls(struct __sk_buff *skb)
16+
__failure __msg("At program exit the register R0 has value")
17+
int global_func15(struct __sk_buff *skb)
1618
{
1719
unsigned int v = 1;
1820

tools/testing/selftests/bpf/progs/test_global_func16.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h>
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
56

67
__noinline int foo(int (*arr)[10])
78
{
@@ -12,7 +13,8 @@ __noinline int foo(int (*arr)[10])
1213
}
1314

1415
SEC("cgroup_skb/ingress")
15-
int test_cls(struct __sk_buff *skb)
16+
__failure __msg("invalid indirect read from stack")
17+
int global_func16(struct __sk_buff *skb)
1618
{
1719
int array[10];
1820

tools/testing/selftests/bpf/progs/test_global_func17.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#include <vmlinux.h>
33
#include <bpf/bpf_helpers.h>
4+
#include "bpf_misc.h"
45

56
__noinline int foo(int *p)
67
{
@@ -10,7 +11,8 @@ __noinline int foo(int *p)
1011
const volatile int i;
1112

1213
SEC("tc")
13-
int test_cls(struct __sk_buff *skb)
14+
__failure __msg("Caller passes invalid args into func#1")
15+
int global_func17(struct __sk_buff *skb)
1416
{
1517
return foo((int *)&i);
1618
}
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
/* Copyright (c) 2020 Facebook */
3+
#include <stddef.h>
4+
#include <linux/bpf.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
7+
38
#define MAX_STACK (512 - 3 * 32)
4-
#include "test_global_func1.c"
9+
10+
static __attribute__ ((noinline))
11+
int f0(int var, struct __sk_buff *skb)
12+
{
13+
return skb->len;
14+
}
15+
16+
__attribute__ ((noinline))
17+
int f1(struct __sk_buff *skb)
18+
{
19+
volatile char buf[MAX_STACK] = {};
20+
21+
return f0(0, skb) + skb->len;
22+
}
23+
24+
int f3(int, struct __sk_buff *skb, int);
25+
26+
__attribute__ ((noinline))
27+
int f2(int val, struct __sk_buff *skb)
28+
{
29+
return f1(skb) + f3(val, skb, 1);
30+
}
31+
32+
__attribute__ ((noinline))
33+
int f3(int val, struct __sk_buff *skb, int var)
34+
{
35+
volatile char buf[MAX_STACK] = {};
36+
37+
return skb->ifindex * val * var;
38+
}
39+
40+
SEC("tc")
41+
__success
42+
int global_func2(struct __sk_buff *skb)
43+
{
44+
return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
45+
}

tools/testing/selftests/bpf/progs/test_global_func3.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stddef.h>
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
67

78
__attribute__ ((noinline))
89
int f1(struct __sk_buff *skb)
@@ -46,20 +47,15 @@ int f7(struct __sk_buff *skb)
4647
return f6(skb);
4748
}
4849

49-
#ifndef NO_FN8
5050
__attribute__ ((noinline))
5151
int f8(struct __sk_buff *skb)
5252
{
5353
return f7(skb);
5454
}
55-
#endif
5655

5756
SEC("tc")
58-
int test_cls(struct __sk_buff *skb)
57+
__failure __msg("the call stack of 8 frames")
58+
int global_func3(struct __sk_buff *skb)
5959
{
60-
#ifndef NO_FN8
6160
return f8(skb);
62-
#else
63-
return f7(skb);
64-
#endif
6561
}

0 commit comments

Comments
 (0)