Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3#include <test_progs.h>
4
5const char *err_str;
6bool found;
7
8static 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;
24out:
25 printf(format, log_buf);
26 return 0;
27}
28
29extern int extra_prog_load_log_flags;
30
31static 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
55err_out:
56 bpf_object__close(obj);
57 return err;
58}
59
60struct test_def {
61 const char *file;
62 const char *err_str;
63};
64
65void test_test_global_funcs(void)
66{
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);
104}
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3#include <test_progs.h>
4
5const char *err_str;
6bool found;
7
8static 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;
24out:
25 printf(format, log_buf);
26 return 0;
27}
28
29extern int extra_prog_load_log_flags;
30
31static int check_load(const char *file)
32{
33 struct bpf_prog_load_attr attr;
34 struct bpf_object *obj = NULL;
35 int err, prog_fd;
36
37 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
38 attr.file = file;
39 attr.prog_type = BPF_PROG_TYPE_UNSPEC;
40 attr.log_level = extra_prog_load_log_flags;
41 attr.prog_flags = BPF_F_TEST_RND_HI32;
42 found = false;
43 err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
44 bpf_object__close(obj);
45 return err;
46}
47
48struct test_def {
49 const char *file;
50 const char *err_str;
51};
52
53void test_test_global_funcs(void)
54{
55 struct test_def tests[] = {
56 { "test_global_func1.o", "combined stack size of 4 calls is 544" },
57 { "test_global_func2.o" },
58 { "test_global_func3.o" , "the call stack of 8 frames" },
59 { "test_global_func4.o" },
60 { "test_global_func5.o" , "expected pointer to ctx, but got PTR" },
61 { "test_global_func6.o" , "modified ctx ptr R2" },
62 { "test_global_func7.o" , "foo() doesn't return scalar" },
63 { "test_global_func8.o" },
64 { "test_global_func9.o" },
65 { "test_global_func10.o", "invalid indirect read from stack" },
66 { "test_global_func11.o", "Caller passes invalid args into func#1" },
67 { "test_global_func12.o", "invalid mem access 'mem_or_null'" },
68 { "test_global_func13.o", "Caller passes invalid args into func#1" },
69 { "test_global_func14.o", "reference type('FWD S') size cannot be determined" },
70 { "test_global_func15.o", "At program exit the register R0 has value" },
71 { "test_global_func16.o", "invalid indirect read from stack" },
72 };
73 libbpf_print_fn_t old_print_fn = NULL;
74 int err, i, duration = 0;
75
76 old_print_fn = libbpf_set_print(libbpf_debug_print);
77
78 for (i = 0; i < ARRAY_SIZE(tests); i++) {
79 const struct test_def *test = &tests[i];
80
81 if (!test__start_subtest(test->file))
82 continue;
83
84 err_str = test->err_str;
85 err = check_load(test->file);
86 CHECK_FAIL(!!err ^ !!err_str);
87 if (err_str)
88 CHECK(found, "", "expected string '%s'", err_str);
89 }
90 libbpf_set_print(old_print_fn);
91}