1 // SPDX-License-Identifier: GPL-2.0
8 #ifdef HAVE_LIBBPF_SUPPORT
9 #include <bpf/libbpf.h>
10 #include <util/llvm-utils.h>
12 static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
14 struct bpf_object *obj;
16 obj = bpf_object__open_mem(obj_buf, obj_buf_sz, NULL);
17 if (libbpf_get_error(obj))
19 bpf_object__close(obj);
26 bool should_load_fail;
27 } bpf_source_table[__LLVM_TESTCASE_MAX] = {
28 [LLVM_TESTCASE_BASE] = {
29 .source = test_llvm__bpf_base_prog,
30 .desc = "Basic BPF llvm compile",
32 [LLVM_TESTCASE_KBUILD] = {
33 .source = test_llvm__bpf_test_kbuild_prog,
34 .desc = "kbuild searching",
36 [LLVM_TESTCASE_BPF_PROLOGUE] = {
37 .source = test_llvm__bpf_test_prologue_prog,
38 .desc = "Compile source for BPF prologue generation",
40 [LLVM_TESTCASE_BPF_RELOCATION] = {
41 .source = test_llvm__bpf_test_relocation,
42 .desc = "Compile source for BPF relocation",
43 .should_load_fail = true,
48 test_llvm__fetch_bpf_obj(void **p_obj_buf,
50 enum test_llvm__testcase idx,
52 bool *should_load_fail)
56 const char *tmpl_old, *clang_opt_old;
57 char *tmpl_new = NULL, *clang_opt_new = NULL;
58 int err, old_verbose, ret = TEST_FAIL;
60 if (idx >= __LLVM_TESTCASE_MAX)
63 source = bpf_source_table[idx].source;
64 desc = bpf_source_table[idx].desc;
66 *should_load_fail = bpf_source_table[idx].should_load_fail;
69 * Skip this test if user's .perfconfig doesn't set [llvm] section
70 * and clang is not found in $PATH
72 if (!force && (!llvm_param.user_set_param &&
73 llvm__search_clang())) {
74 pr_debug("No clang, skip this test\n");
79 * llvm is verbosity when error. Suppress all error output if
82 old_verbose = verbose;
89 if (!llvm_param.clang_bpf_cmd_template)
92 if (!llvm_param.clang_opt)
93 llvm_param.clang_opt = strdup("");
95 err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
96 llvm_param.clang_bpf_cmd_template,
97 old_verbose ? "" : " 2>/dev/null");
100 err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
104 tmpl_old = llvm_param.clang_bpf_cmd_template;
105 llvm_param.clang_bpf_cmd_template = tmpl_new;
106 clang_opt_old = llvm_param.clang_opt;
107 llvm_param.clang_opt = clang_opt_new;
109 err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
111 llvm_param.clang_bpf_cmd_template = tmpl_old;
112 llvm_param.clang_opt = clang_opt_old;
114 verbose = old_verbose;
123 pr_debug("Failed to compile test case: '%s'\n", desc);
127 static int test__llvm(int subtest)
130 void *obj_buf = NULL;
131 size_t obj_buf_sz = 0;
132 bool should_load_fail = false;
134 if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
137 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
138 subtest, false, &should_load_fail);
140 if (ret == TEST_OK && !should_load_fail) {
141 ret = test__bpf_parsing(obj_buf, obj_buf_sz);
142 if (ret != TEST_OK) {
143 pr_debug("Failed to parse test case '%s'\n",
144 bpf_source_table[subtest].desc);
151 #endif //HAVE_LIBBPF_SUPPORT
153 static int test__llvm__bpf_base_prog(struct test_suite *test __maybe_unused,
154 int subtest __maybe_unused)
156 #ifdef HAVE_LIBBPF_SUPPORT
157 return test__llvm(LLVM_TESTCASE_BASE);
159 pr_debug("Skip LLVM test because BPF support is not compiled\n");
164 static int test__llvm__bpf_test_kbuild_prog(struct test_suite *test __maybe_unused,
165 int subtest __maybe_unused)
167 #ifdef HAVE_LIBBPF_SUPPORT
168 return test__llvm(LLVM_TESTCASE_KBUILD);
170 pr_debug("Skip LLVM test because BPF support is not compiled\n");
175 static int test__llvm__bpf_test_prologue_prog(struct test_suite *test __maybe_unused,
176 int subtest __maybe_unused)
178 #ifdef HAVE_LIBBPF_SUPPORT
179 return test__llvm(LLVM_TESTCASE_BPF_PROLOGUE);
181 pr_debug("Skip LLVM test because BPF support is not compiled\n");
186 static int test__llvm__bpf_test_relocation(struct test_suite *test __maybe_unused,
187 int subtest __maybe_unused)
189 #ifdef HAVE_LIBBPF_SUPPORT
190 return test__llvm(LLVM_TESTCASE_BPF_RELOCATION);
192 pr_debug("Skip LLVM test because BPF support is not compiled\n");
198 static struct test_case llvm_tests[] = {
199 #ifdef HAVE_LIBBPF_SUPPORT
200 TEST_CASE("Basic BPF llvm compile", llvm__bpf_base_prog),
201 TEST_CASE("kbuild searching", llvm__bpf_test_kbuild_prog),
202 TEST_CASE("Compile source for BPF prologue generation",
203 llvm__bpf_test_prologue_prog),
204 TEST_CASE("Compile source for BPF relocation", llvm__bpf_test_relocation),
206 TEST_CASE_REASON("Basic BPF llvm compile", llvm__bpf_base_prog, "not compiled in"),
207 TEST_CASE_REASON("kbuild searching", llvm__bpf_test_kbuild_prog, "not compiled in"),
208 TEST_CASE_REASON("Compile source for BPF prologue generation",
209 llvm__bpf_test_prologue_prog, "not compiled in"),
210 TEST_CASE_REASON("Compile source for BPF relocation",
211 llvm__bpf_test_relocation, "not compiled in"),
216 struct test_suite suite__llvm = {
217 .desc = "LLVM search and compile",
218 .test_cases = llvm_tests,