1 // SPDX-License-Identifier: GPL-2.0
4 #include <bpf/libbpf.h>
5 #include <util/llvm-utils.h>
6 #include <util/cache.h>
12 #ifdef HAVE_LIBBPF_SUPPORT
13 static int test__bpf_parsing(void *obj_buf
, size_t obj_buf_sz
)
15 struct bpf_object
*obj
;
17 obj
= bpf_object__open_buffer(obj_buf
, obj_buf_sz
, NULL
);
18 if (libbpf_get_error(obj
))
20 bpf_object__close(obj
);
24 static int test__bpf_parsing(void *obj_buf __maybe_unused
,
25 size_t obj_buf_sz __maybe_unused
)
27 pr_debug("Skip bpf parsing\n");
35 bool should_load_fail
;
36 } bpf_source_table
[__LLVM_TESTCASE_MAX
] = {
37 [LLVM_TESTCASE_BASE
] = {
38 .source
= test_llvm__bpf_base_prog
,
39 .desc
= "Basic BPF llvm compile",
41 [LLVM_TESTCASE_KBUILD
] = {
42 .source
= test_llvm__bpf_test_kbuild_prog
,
43 .desc
= "kbuild searching",
45 [LLVM_TESTCASE_BPF_PROLOGUE
] = {
46 .source
= test_llvm__bpf_test_prologue_prog
,
47 .desc
= "Compile source for BPF prologue generation",
49 [LLVM_TESTCASE_BPF_RELOCATION
] = {
50 .source
= test_llvm__bpf_test_relocation
,
51 .desc
= "Compile source for BPF relocation",
52 .should_load_fail
= true,
57 test_llvm__fetch_bpf_obj(void **p_obj_buf
,
59 enum test_llvm__testcase idx
,
61 bool *should_load_fail
)
65 const char *tmpl_old
, *clang_opt_old
;
66 char *tmpl_new
= NULL
, *clang_opt_new
= NULL
;
67 int err
, old_verbose
, ret
= TEST_FAIL
;
69 if (idx
>= __LLVM_TESTCASE_MAX
)
72 source
= bpf_source_table
[idx
].source
;
73 desc
= bpf_source_table
[idx
].desc
;
75 *should_load_fail
= bpf_source_table
[idx
].should_load_fail
;
78 * Skip this test if user's .perfconfig doesn't set [llvm] section
79 * and clang is not found in $PATH, and this is not perf test -v
81 if (!force
&& (verbose
<= 0 &&
82 !llvm_param
.user_set_param
&&
83 llvm__search_clang())) {
84 pr_debug("No clang and no verbosive, skip this test\n");
89 * llvm is verbosity when error. Suppress all error output if
92 old_verbose
= verbose
;
99 if (!llvm_param
.clang_bpf_cmd_template
)
102 if (!llvm_param
.clang_opt
)
103 llvm_param
.clang_opt
= strdup("");
105 err
= asprintf(&tmpl_new
, "echo '%s' | %s%s", source
,
106 llvm_param
.clang_bpf_cmd_template
,
107 old_verbose
? "" : " 2>/dev/null");
110 err
= asprintf(&clang_opt_new
, "-xc %s", llvm_param
.clang_opt
);
114 tmpl_old
= llvm_param
.clang_bpf_cmd_template
;
115 llvm_param
.clang_bpf_cmd_template
= tmpl_new
;
116 clang_opt_old
= llvm_param
.clang_opt
;
117 llvm_param
.clang_opt
= clang_opt_new
;
119 err
= llvm__compile_bpf("-", p_obj_buf
, p_obj_buf_sz
);
121 llvm_param
.clang_bpf_cmd_template
= tmpl_old
;
122 llvm_param
.clang_opt
= clang_opt_old
;
124 verbose
= old_verbose
;
133 pr_debug("Failed to compile test case: '%s'\n", desc
);
137 int test__llvm(struct test
*test __maybe_unused
, int subtest
)
140 void *obj_buf
= NULL
;
141 size_t obj_buf_sz
= 0;
142 bool should_load_fail
= false;
144 if ((subtest
< 0) || (subtest
>= __LLVM_TESTCASE_MAX
))
147 ret
= test_llvm__fetch_bpf_obj(&obj_buf
, &obj_buf_sz
,
148 subtest
, false, &should_load_fail
);
150 if (ret
== TEST_OK
&& !should_load_fail
) {
151 ret
= test__bpf_parsing(obj_buf
, obj_buf_sz
);
152 if (ret
!= TEST_OK
) {
153 pr_debug("Failed to parse test case '%s'\n",
154 bpf_source_table
[subtest
].desc
);
162 int test__llvm_subtest_get_nr(void)
164 return __LLVM_TESTCASE_MAX
;
167 const char *test__llvm_subtest_get_desc(int subtest
)
169 if ((subtest
< 0) || (subtest
>= __LLVM_TESTCASE_MAX
))
172 return bpf_source_table
[subtest
].desc
;