llvm.c 4.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <stdio.h>
3
#include <stdlib.h>
4 5 6
#include <bpf/libbpf.h>
#include <util/llvm-utils.h>
#include <util/cache.h>
7
#include "llvm.h"
8 9
#include "tests.h"
#include "debug.h"
10
#include "util.h"
11 12 13 14 15 16

#ifdef HAVE_LIBBPF_SUPPORT
static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
{
	struct bpf_object *obj;

17
	obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
18
	if (libbpf_get_error(obj))
19
		return TEST_FAIL;
20
	bpf_object__close(obj);
21
	return TEST_OK;
22 23 24 25 26
}
#else
static int test__bpf_parsing(void *obj_buf __maybe_unused,
			     size_t obj_buf_sz __maybe_unused)
{
27
	pr_debug("Skip bpf parsing\n");
28
	return TEST_OK;
29 30 31
}
#endif

32 33 34
static struct {
	const char *source;
	const char *desc;
35
	bool should_load_fail;
36 37 38
} bpf_source_table[__LLVM_TESTCASE_MAX] = {
	[LLVM_TESTCASE_BASE] = {
		.source = test_llvm__bpf_base_prog,
39
		.desc = "Basic BPF llvm compile",
40
	},
41 42
	[LLVM_TESTCASE_KBUILD] = {
		.source = test_llvm__bpf_test_kbuild_prog,
43
		.desc = "kbuild searching",
44
	},
45 46
	[LLVM_TESTCASE_BPF_PROLOGUE] = {
		.source = test_llvm__bpf_test_prologue_prog,
47
		.desc = "Compile source for BPF prologue generation",
48
	},
49 50
	[LLVM_TESTCASE_BPF_RELOCATION] = {
		.source = test_llvm__bpf_test_relocation,
51
		.desc = "Compile source for BPF relocation",
52 53
		.should_load_fail = true,
	},
54 55 56 57 58
};

int
test_llvm__fetch_bpf_obj(void **p_obj_buf,
			 size_t *p_obj_buf_sz,
59
			 enum test_llvm__testcase idx,
60 61
			 bool force,
			 bool *should_load_fail)
62
{
63 64 65 66 67 68
	const char *source;
	const char *desc;
	const char *tmpl_old, *clang_opt_old;
	char *tmpl_new = NULL, *clang_opt_new = NULL;
	int err, old_verbose, ret = TEST_FAIL;

69
	if (idx >= __LLVM_TESTCASE_MAX)
70 71
		return TEST_FAIL;

72 73
	source = bpf_source_table[idx].source;
	desc = bpf_source_table[idx].desc;
74 75
	if (should_load_fail)
		*should_load_fail = bpf_source_table[idx].should_load_fail;
76 77 78 79 80

	/*
	 * Skip this test if user's .perfconfig doesn't set [llvm] section
	 * and clang is not found in $PATH, and this is not perf test -v
	 */
81
	if (!force && (verbose <= 0 &&
82 83
		       !llvm_param.user_set_param &&
		       llvm__search_clang())) {
84
		pr_debug("No clang and no verbosive, skip this test\n");
85 86 87 88 89 90 91
		return TEST_SKIP;
	}

	/*
	 * llvm is verbosity when error. Suppress all error output if
	 * not 'perf test -v'.
	 */
92
	old_verbose = verbose;
93 94 95
	if (verbose == 0)
		verbose = -1;

96 97 98
	*p_obj_buf = NULL;
	*p_obj_buf_sz = 0;

99
	if (!llvm_param.clang_bpf_cmd_template)
100
		goto out;
101 102 103 104

	if (!llvm_param.clang_opt)
		llvm_param.clang_opt = strdup("");

105 106 107
	err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
		       llvm_param.clang_bpf_cmd_template,
		       old_verbose ? "" : " 2>/dev/null");
108
	if (err < 0)
109
		goto out;
110 111
	err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
	if (err < 0)
112
		goto out;
113

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;
118 119 120 121 122

	err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);

	llvm_param.clang_bpf_cmd_template = tmpl_old;
	llvm_param.clang_opt = clang_opt_old;
123 124

	verbose = old_verbose;
125
	if (err)
126 127 128 129 130 131 132 133 134 135
		goto out;

	ret = TEST_OK;
out:
	free(tmpl_new);
	free(clang_opt_new);
	if (ret != TEST_OK)
		pr_debug("Failed to compile test case: '%s'\n", desc);
	return ret;
}
136

137
int test__llvm(struct test *test __maybe_unused, int subtest)
138
{
139 140 141
	int ret;
	void *obj_buf = NULL;
	size_t obj_buf_sz = 0;
142
	bool should_load_fail = false;
143

144 145
	if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
		return TEST_FAIL;
146

147
	ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
148
				       subtest, false, &should_load_fail);
149

150
	if (ret == TEST_OK && !should_load_fail) {
151 152 153 154
		ret = test__bpf_parsing(obj_buf, obj_buf_sz);
		if (ret != TEST_OK) {
			pr_debug("Failed to parse test case '%s'\n",
				 bpf_source_table[subtest].desc);
155 156
		}
	}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	free(obj_buf);

	return ret;
}

int test__llvm_subtest_get_nr(void)
{
	return __LLVM_TESTCASE_MAX;
}

const char *test__llvm_subtest_get_desc(int subtest)
{
	if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
		return NULL;

	return bpf_source_table[subtest].desc;
173
}