llvm.c 4.1 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 70 71

	/*
	 * 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
	 */
72
	if (!force && (verbose <= 0 &&
73 74
		       !llvm_param.user_set_param &&
		       llvm__search_clang())) {
75
		pr_debug("No clang and no verbosive, skip this test\n");
76 77 78 79 80 81 82
		return TEST_SKIP;
	}

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

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

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

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

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

105
	tmpl_old = llvm_param.clang_bpf_cmd_template;
106
	llvm_param.clang_bpf_cmd_template = tmpl_new;
107
	clang_opt_old = llvm_param.clang_opt;
108
	llvm_param.clang_opt = clang_opt_new;
109 110 111 112 113

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

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

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

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

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

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