blob: a039f93199e56cfb57ae04f37fd4fae7db03c1fd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Wang Nan9bc898c2015-07-08 10:04:02 +00002#include <stdio.h>
3#include <bpf/libbpf.h>
4#include <util/llvm-utils.h>
5#include <util/cache.h>
Wang Nanb31de012015-11-06 13:49:41 +00006#include "llvm.h"
Wang Nan9bc898c2015-07-08 10:04:02 +00007#include "tests.h"
8#include "debug.h"
Arnaldo Carvalho de Melo175729f2016-07-07 11:38:09 -03009#include "util.h"
Wang Nan9bc898c2015-07-08 10:04:02 +000010
Wang Nan9bc898c2015-07-08 10:04:02 +000011#ifdef HAVE_LIBBPF_SUPPORT
12static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
13{
14 struct bpf_object *obj;
15
Wang Nanacf860a2015-08-27 02:30:55 +000016 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
Joe Stringere28ff1a2017-01-22 17:11:25 -080017 if (libbpf_get_error(obj))
Wang Nanb31de012015-11-06 13:49:41 +000018 return TEST_FAIL;
Wang Nan9bc898c2015-07-08 10:04:02 +000019 bpf_object__close(obj);
Wang Nanb31de012015-11-06 13:49:41 +000020 return TEST_OK;
Wang Nan9bc898c2015-07-08 10:04:02 +000021}
22#else
23static int test__bpf_parsing(void *obj_buf __maybe_unused,
24 size_t obj_buf_sz __maybe_unused)
25{
Wang Nan597bdeb2015-11-03 10:44:42 +000026 pr_debug("Skip bpf parsing\n");
Wang Nanb31de012015-11-06 13:49:41 +000027 return TEST_OK;
Wang Nan9bc898c2015-07-08 10:04:02 +000028}
29#endif
30
Wang Nanb31de012015-11-06 13:49:41 +000031static struct {
32 const char *source;
33 const char *desc;
Wang Nan7b6982c2016-01-25 09:55:48 +000034 bool should_load_fail;
Wang Nanb31de012015-11-06 13:49:41 +000035} bpf_source_table[__LLVM_TESTCASE_MAX] = {
36 [LLVM_TESTCASE_BASE] = {
37 .source = test_llvm__bpf_base_prog,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030038 .desc = "Basic BPF llvm compile",
Wang Nanb31de012015-11-06 13:49:41 +000039 },
Wang Nan7af3f3d2015-11-06 13:49:42 +000040 [LLVM_TESTCASE_KBUILD] = {
41 .source = test_llvm__bpf_test_kbuild_prog,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030042 .desc = "kbuild searching",
Wang Nan7af3f3d2015-11-06 13:49:42 +000043 },
Wang Nanbbb7d492015-11-16 12:10:14 +000044 [LLVM_TESTCASE_BPF_PROLOGUE] = {
45 .source = test_llvm__bpf_test_prologue_prog,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030046 .desc = "Compile source for BPF prologue generation",
Wang Nanbbb7d492015-11-16 12:10:14 +000047 },
Wang Nan7b6982c2016-01-25 09:55:48 +000048 [LLVM_TESTCASE_BPF_RELOCATION] = {
49 .source = test_llvm__bpf_test_relocation,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030050 .desc = "Compile source for BPF relocation",
Wang Nan7b6982c2016-01-25 09:55:48 +000051 .should_load_fail = true,
52 },
Wang Nanb31de012015-11-06 13:49:41 +000053};
54
Wang Nanb31de012015-11-06 13:49:41 +000055int
56test_llvm__fetch_bpf_obj(void **p_obj_buf,
57 size_t *p_obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030058 enum test_llvm__testcase idx,
Wang Nan7b6982c2016-01-25 09:55:48 +000059 bool force,
60 bool *should_load_fail)
Wang Nan9bc898c2015-07-08 10:04:02 +000061{
Wang Nanb31de012015-11-06 13:49:41 +000062 const char *source;
63 const char *desc;
64 const char *tmpl_old, *clang_opt_old;
65 char *tmpl_new = NULL, *clang_opt_new = NULL;
66 int err, old_verbose, ret = TEST_FAIL;
67
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030068 if (idx >= __LLVM_TESTCASE_MAX)
Wang Nanb31de012015-11-06 13:49:41 +000069 return TEST_FAIL;
70
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030071 source = bpf_source_table[idx].source;
72 desc = bpf_source_table[idx].desc;
Wang Nan7b6982c2016-01-25 09:55:48 +000073 if (should_load_fail)
74 *should_load_fail = bpf_source_table[idx].should_load_fail;
Wang Nan9bc898c2015-07-08 10:04:02 +000075
Wang Nan9bc898c2015-07-08 10:04:02 +000076 /*
77 * Skip this test if user's .perfconfig doesn't set [llvm] section
78 * and clang is not found in $PATH, and this is not perf test -v
79 */
Namhyung Kimbb963e12017-02-17 17:17:38 +090080 if (!force && (verbose <= 0 &&
Wang Nanb31de012015-11-06 13:49:41 +000081 !llvm_param.user_set_param &&
82 llvm__search_clang())) {
Wang Nan597bdeb2015-11-03 10:44:42 +000083 pr_debug("No clang and no verbosive, skip this test\n");
Wang Nan9bc898c2015-07-08 10:04:02 +000084 return TEST_SKIP;
85 }
86
Wang Nan9bc898c2015-07-08 10:04:02 +000087 /*
88 * llvm is verbosity when error. Suppress all error output if
89 * not 'perf test -v'.
90 */
Wang Nanb31de012015-11-06 13:49:41 +000091 old_verbose = verbose;
Wang Nan9bc898c2015-07-08 10:04:02 +000092 if (verbose == 0)
93 verbose = -1;
94
Wang Nanb31de012015-11-06 13:49:41 +000095 *p_obj_buf = NULL;
96 *p_obj_buf_sz = 0;
97
Wang Nan9bc898c2015-07-08 10:04:02 +000098 if (!llvm_param.clang_bpf_cmd_template)
Wang Nanb31de012015-11-06 13:49:41 +000099 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000100
101 if (!llvm_param.clang_opt)
102 llvm_param.clang_opt = strdup("");
103
Wang Nanb31de012015-11-06 13:49:41 +0000104 err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
105 llvm_param.clang_bpf_cmd_template,
106 old_verbose ? "" : " 2>/dev/null");
Wang Nan9bc898c2015-07-08 10:04:02 +0000107 if (err < 0)
Wang Nanb31de012015-11-06 13:49:41 +0000108 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000109 err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
110 if (err < 0)
Wang Nanb31de012015-11-06 13:49:41 +0000111 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000112
Wang Nanb31de012015-11-06 13:49:41 +0000113 tmpl_old = llvm_param.clang_bpf_cmd_template;
Wang Nan9bc898c2015-07-08 10:04:02 +0000114 llvm_param.clang_bpf_cmd_template = tmpl_new;
Wang Nanb31de012015-11-06 13:49:41 +0000115 clang_opt_old = llvm_param.clang_opt;
Wang Nan9bc898c2015-07-08 10:04:02 +0000116 llvm_param.clang_opt = clang_opt_new;
Wang Nanb31de012015-11-06 13:49:41 +0000117
118 err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
119
120 llvm_param.clang_bpf_cmd_template = tmpl_old;
121 llvm_param.clang_opt = clang_opt_old;
Wang Nan9bc898c2015-07-08 10:04:02 +0000122
123 verbose = old_verbose;
Wang Nan597bdeb2015-11-03 10:44:42 +0000124 if (err)
Wang Nanb31de012015-11-06 13:49:41 +0000125 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000126
Wang Nanb31de012015-11-06 13:49:41 +0000127 ret = TEST_OK;
128out:
129 free(tmpl_new);
130 free(clang_opt_new);
131 if (ret != TEST_OK)
132 pr_debug("Failed to compile test case: '%s'\n", desc);
133 return ret;
134}
135
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300136int test__llvm(struct test *test __maybe_unused, int subtest)
Wang Nanb31de012015-11-06 13:49:41 +0000137{
Wang Nane8c6d502015-11-17 08:32:48 +0000138 int ret;
139 void *obj_buf = NULL;
140 size_t obj_buf_sz = 0;
Wang Nan7b6982c2016-01-25 09:55:48 +0000141 bool should_load_fail = false;
Wang Nanb31de012015-11-06 13:49:41 +0000142
Wang Nane8c6d502015-11-17 08:32:48 +0000143 if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
144 return TEST_FAIL;
Wang Nanb31de012015-11-06 13:49:41 +0000145
Wang Nane8c6d502015-11-17 08:32:48 +0000146 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
Wang Nan7b6982c2016-01-25 09:55:48 +0000147 subtest, false, &should_load_fail);
Wang Nanb31de012015-11-06 13:49:41 +0000148
Wang Nan7b6982c2016-01-25 09:55:48 +0000149 if (ret == TEST_OK && !should_load_fail) {
Wang Nane8c6d502015-11-17 08:32:48 +0000150 ret = test__bpf_parsing(obj_buf, obj_buf_sz);
151 if (ret != TEST_OK) {
152 pr_debug("Failed to parse test case '%s'\n",
153 bpf_source_table[subtest].desc);
Wang Nanb31de012015-11-06 13:49:41 +0000154 }
155 }
Wang Nane8c6d502015-11-17 08:32:48 +0000156 free(obj_buf);
157
158 return ret;
159}
160
161int test__llvm_subtest_get_nr(void)
162{
163 return __LLVM_TESTCASE_MAX;
164}
165
166const char *test__llvm_subtest_get_desc(int subtest)
167{
168 if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
169 return NULL;
170
171 return bpf_source_table[subtest].desc;
Wang Nan9bc898c2015-07-08 10:04:02 +0000172}