llvm.c 4.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5 6 7 8
#include "tests.h"
#include "debug.h"

#ifdef HAVE_LIBBPF_SUPPORT
9 10 11
#include <bpf/libbpf.h>
#include <util/llvm-utils.h>
#include "llvm.h"
12 13 14 15
static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
{
	struct bpf_object *obj;

16
	obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
17
	if (libbpf_get_error(obj))
18
		return TEST_FAIL;
19
	bpf_object__close(obj);
20
	return TEST_OK;
21 22
}

23 24 25
static struct {
	const char *source;
	const char *desc;
26
	bool should_load_fail;
27 28 29
} bpf_source_table[__LLVM_TESTCASE_MAX] = {
	[LLVM_TESTCASE_BASE] = {
		.source = test_llvm__bpf_base_prog,
30
		.desc = "Basic BPF llvm compile",
31
	},
32 33
	[LLVM_TESTCASE_KBUILD] = {
		.source = test_llvm__bpf_test_kbuild_prog,
34
		.desc = "kbuild searching",
35
	},
36 37
	[LLVM_TESTCASE_BPF_PROLOGUE] = {
		.source = test_llvm__bpf_test_prologue_prog,
38
		.desc = "Compile source for BPF prologue generation",
39
	},
40 41
	[LLVM_TESTCASE_BPF_RELOCATION] = {
		.source = test_llvm__bpf_test_relocation,
42
		.desc = "Compile source for BPF relocation",
43 44
		.should_load_fail = true,
	},
45 46 47 48 49
};

int
test_llvm__fetch_bpf_obj(void **p_obj_buf,
			 size_t *p_obj_buf_sz,
50
			 enum test_llvm__testcase idx,
51 52
			 bool force,
			 bool *should_load_fail)
53
{
54 55 56 57 58 59
	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;

60
	if (idx >= __LLVM_TESTCASE_MAX)
61 62
		return TEST_FAIL;

63 64
	source = bpf_source_table[idx].source;
	desc = bpf_source_table[idx].desc;
65 66
	if (should_load_fail)
		*should_load_fail = bpf_source_table[idx].should_load_fail;
67 68 69

	/*
	 * Skip this test if user's .perfconfig doesn't set [llvm] section
70
	 * and clang is not found in $PATH
71
	 */
72
	if (!force && (!llvm_param.user_set_param &&
73
		       llvm__search_clang())) {
74
		pr_debug("No clang, skip this test\n");
75 76 77 78 79 80 81
		return TEST_SKIP;
	}

	/*
	 * llvm is verbosity when error. Suppress all error output if
	 * not 'perf test -v'.
	 */
82
	old_verbose = verbose;
83 84 85
	if (verbose == 0)
		verbose = -1;

86 87 88
	*p_obj_buf = NULL;
	*p_obj_buf_sz = 0;

89
	if (!llvm_param.clang_bpf_cmd_template)
90
		goto out;
91 92 93 94

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

95 96 97
	err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
		       llvm_param.clang_bpf_cmd_template,
		       old_verbose ? "" : " 2>/dev/null");
98
	if (err < 0)
99
		goto out;
100 101
	err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
	if (err < 0)
102
		goto out;
103

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;
108 109 110 111 112

	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;
113 114

	verbose = old_verbose;
115
	if (err)
116 117 118 119 120 121 122 123 124 125
		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;
}
126

127
int test__llvm(struct test *test __maybe_unused, int subtest)
128
{
129 130 131
	int ret;
	void *obj_buf = NULL;
	size_t obj_buf_sz = 0;
132
	bool should_load_fail = false;
133

134 135
	if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
		return TEST_FAIL;
136

137
	ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
138
				       subtest, false, &should_load_fail);
139

140
	if (ret == TEST_OK && !should_load_fail) {
141 142 143 144
		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);
145 146
		}
	}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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;
163
}
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#else //HAVE_LIBBPF_SUPPORT
int test__llvm(struct test *test __maybe_unused, int subtest __maybe_unused)
{
	return TEST_SKIP;
}

int test__llvm_subtest_get_nr(void)
{
	return 0;
}

const char *test__llvm_subtest_get_desc(int subtest __maybe_unused)
{
	return NULL;
}
#endif // HAVE_LIBBPF_SUPPORT