Skip to content

Commit 7b6982c

Browse files
WangNan0acmel
authored andcommitted
perf test: Add libbpf relocation checker
There's a bug in LLVM that it can generate unneeded relocation information. See [1] and [2]. Libbpf should check the target section of a relocation symbol. This patch adds a testcase which references a global variable (BPF doesn't support global variables). Before fixing libbpf, the new test case can be loaded into kernel, the global variable acts like the first map. It is incorrect. Result: # ~/perf test BPF 37: Test BPF filter : 37.1: Test basic BPF filtering : Ok 37.2: Test BPF prologue generation : Ok 37.3: Test BPF relocation checker : FAILED! # ~/perf test -v BPF ... libbpf: loading object '[bpf_relocation_test]' from buffer libbpf: section .strtab, size 126, link 0, flags 0, type=3 libbpf: section .text, size 0, link 0, flags 6, type=1 libbpf: section .data, size 0, link 0, flags 3, type=1 libbpf: section .bss, size 0, link 0, flags 3, type=8 libbpf: section func=sys_write, size 104, link 0, flags 6, type=1 libbpf: found program func=sys_write libbpf: section .relfunc=sys_write, size 16, link 10, flags 0, type=9 libbpf: section maps, size 16, link 0, flags 3, type=1 libbpf: maps in [bpf_relocation_test]: 16 bytes libbpf: section license, size 4, link 0, flags 3, type=1 libbpf: license of [bpf_relocation_test] is GPL libbpf: section version, size 4, link 0, flags 3, type=1 libbpf: kernel version of [bpf_relocation_test] is 40400 libbpf: section .symtab, size 144, link 1, flags 0, type=2 libbpf: map 0 is "my_table" libbpf: collecting relocating info for: 'func=sys_write' libbpf: relocation: insn_idx=7 Success unexpectedly: libbpf error when dealing with relocation test child finished with -1 ---- end ---- Test BPF filter subtest 2: FAILED! [1] https://llvm.org/bugs/show_bug.cgi?id=26243 [2] https://patchwork.ozlabs.org/patch/571385/ Signed-off-by: Wang Nan <wangnan0@huawei.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: "David S. Miller" <davem@davemloft.net> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: He Kuang <hekuang@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Li Zefan <lizefan@huawei.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Will Deacon <will.deacon@arm.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1453715801-7732-2-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ab414dc commit 7b6982c

7 files changed

Lines changed: 98 additions & 12 deletions

File tree

‎tools/perf/Makefile.perf‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean
618618
$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 perf-read-vdsox32
619619
$(call QUIET_CLEAN, core-gen) $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)FEATURE-DUMP $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* \
620620
$(OUTPUT)util/intel-pt-decoder/inat-tables.c $(OUTPUT)fixdep \
621-
$(OUTPUT)tests/llvm-src-{base,kbuild,prologue}.c
621+
$(OUTPUT)tests/llvm-src-{base,kbuild,prologue,relocation}.c
622622
$(QUIET_SUBDIR0)Documentation $(QUIET_SUBDIR1) clean
623623
$(python-clean)
624624

‎tools/perf/tests/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
llvm-src-base.c
22
llvm-src-kbuild.c
33
llvm-src-prologue.c
4+
llvm-src-relocation.c

‎tools/perf/tests/Build‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ perf-y += sample-parsing.o
3131
perf-y += parse-no-sample-id-all.o
3232
perf-y += kmod-path.o
3333
perf-y += thread-map.o
34-
perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o
34+
perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o llvm-src-relocation.o
3535
perf-y += bpf.o
3636
perf-y += topology.o
3737
perf-y += cpumap.o
@@ -59,6 +59,13 @@ $(OUTPUT)tests/llvm-src-prologue.c: tests/bpf-script-test-prologue.c tests/Build
5959
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
6060
$(Q)echo ';' >> $@
6161

62+
$(OUTPUT)tests/llvm-src-relocation.c: tests/bpf-script-test-relocation.c tests/Build
63+
$(call rule_mkdir)
64+
$(Q)echo '#include <tests/llvm.h>' > $@
65+
$(Q)echo 'const char test_llvm__bpf_test_relocation[] =' >> $@
66+
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
67+
$(Q)echo ';' >> $@
68+
6269
ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64))
6370
perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
6471
endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* bpf-script-test-relocation.c
3+
* Test BPF loader checking relocation
4+
*/
5+
#ifndef LINUX_VERSION_CODE
6+
# error Need LINUX_VERSION_CODE
7+
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
8+
#endif
9+
#define BPF_ANY 0
10+
#define BPF_MAP_TYPE_ARRAY 2
11+
#define BPF_FUNC_map_lookup_elem 1
12+
#define BPF_FUNC_map_update_elem 2
13+
14+
static void *(*bpf_map_lookup_elem)(void *map, void *key) =
15+
(void *) BPF_FUNC_map_lookup_elem;
16+
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
17+
(void *) BPF_FUNC_map_update_elem;
18+
19+
struct bpf_map_def {
20+
unsigned int type;
21+
unsigned int key_size;
22+
unsigned int value_size;
23+
unsigned int max_entries;
24+
};
25+
26+
#define SEC(NAME) __attribute__((section(NAME), used))
27+
struct bpf_map_def SEC("maps") my_table = {
28+
.type = BPF_MAP_TYPE_ARRAY,
29+
.key_size = sizeof(int),
30+
.value_size = sizeof(int),
31+
.max_entries = 1,
32+
};
33+
34+
int this_is_a_global_val;
35+
36+
SEC("func=sys_write")
37+
int bpf_func__sys_write(void *ctx)
38+
{
39+
int key = 0;
40+
int value = 0;
41+
42+
/*
43+
* Incorrect relocation. Should not allow this program be
44+
* loaded into kernel.
45+
*/
46+
bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);
47+
return 0;
48+
}
49+
char _license[] SEC("license") = "GPL";
50+
int _version SEC("version") = LINUX_VERSION_CODE;

‎tools/perf/tests/bpf.c‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ static struct {
7171
(NR_ITERS + 1) / 4,
7272
},
7373
#endif
74+
{
75+
LLVM_TESTCASE_BPF_RELOCATION,
76+
"Test BPF relocation checker",
77+
"[bpf_relocation_test]",
78+
"fix 'perf test LLVM' first",
79+
"libbpf error when dealing with relocation",
80+
NULL,
81+
0,
82+
},
7483
};
7584

7685
static int do_test(struct bpf_object *obj, int (*func)(void),
@@ -190,7 +199,7 @@ static int __test__bpf(int idx)
190199

191200
ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
192201
bpf_testcase_table[idx].prog_id,
193-
true);
202+
true, NULL);
194203
if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
195204
pr_debug("Unable to get BPF object, %s\n",
196205
bpf_testcase_table[idx].msg_compile_fail);
@@ -202,14 +211,21 @@ static int __test__bpf(int idx)
202211

203212
obj = prepare_bpf(obj_buf, obj_buf_sz,
204213
bpf_testcase_table[idx].name);
205-
if (!obj) {
214+
if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
215+
if (!obj)
216+
pr_debug("Fail to load BPF object: %s\n",
217+
bpf_testcase_table[idx].msg_load_fail);
218+
else
219+
pr_debug("Success unexpectedly: %s\n",
220+
bpf_testcase_table[idx].msg_load_fail);
206221
ret = TEST_FAIL;
207222
goto out;
208223
}
209224

210-
ret = do_test(obj,
211-
bpf_testcase_table[idx].target_func,
212-
bpf_testcase_table[idx].expect_result);
225+
if (obj)
226+
ret = do_test(obj,
227+
bpf_testcase_table[idx].target_func,
228+
bpf_testcase_table[idx].expect_result);
213229
out:
214230
bpf__clear();
215231
return ret;

‎tools/perf/tests/llvm.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int test__bpf_parsing(void *obj_buf __maybe_unused,
3535
static struct {
3636
const char *source;
3737
const char *desc;
38+
bool should_load_fail;
3839
} bpf_source_table[__LLVM_TESTCASE_MAX] = {
3940
[LLVM_TESTCASE_BASE] = {
4041
.source = test_llvm__bpf_base_prog,
@@ -48,14 +49,19 @@ static struct {
4849
.source = test_llvm__bpf_test_prologue_prog,
4950
.desc = "Compile source for BPF prologue generation test",
5051
},
52+
[LLVM_TESTCASE_BPF_RELOCATION] = {
53+
.source = test_llvm__bpf_test_relocation,
54+
.desc = "Compile source for BPF relocation test",
55+
.should_load_fail = true,
56+
},
5157
};
5258

53-
5459
int
5560
test_llvm__fetch_bpf_obj(void **p_obj_buf,
5661
size_t *p_obj_buf_sz,
5762
enum test_llvm__testcase idx,
58-
bool force)
63+
bool force,
64+
bool *should_load_fail)
5965
{
6066
const char *source;
6167
const char *desc;
@@ -68,6 +74,8 @@ test_llvm__fetch_bpf_obj(void **p_obj_buf,
6874

6975
source = bpf_source_table[idx].source;
7076
desc = bpf_source_table[idx].desc;
77+
if (should_load_fail)
78+
*should_load_fail = bpf_source_table[idx].should_load_fail;
7179

7280
perf_config(perf_config_cb, NULL);
7381

@@ -136,14 +144,15 @@ int test__llvm(int subtest)
136144
int ret;
137145
void *obj_buf = NULL;
138146
size_t obj_buf_sz = 0;
147+
bool should_load_fail = false;
139148

140149
if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
141150
return TEST_FAIL;
142151

143152
ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
144-
subtest, false);
153+
subtest, false, &should_load_fail);
145154

146-
if (ret == TEST_OK) {
155+
if (ret == TEST_OK && !should_load_fail) {
147156
ret = test__bpf_parsing(obj_buf, obj_buf_sz);
148157
if (ret != TEST_OK) {
149158
pr_debug("Failed to parse test case '%s'\n",

‎tools/perf/tests/llvm.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
extern const char test_llvm__bpf_base_prog[];
88
extern const char test_llvm__bpf_test_kbuild_prog[];
99
extern const char test_llvm__bpf_test_prologue_prog[];
10+
extern const char test_llvm__bpf_test_relocation[];
1011

1112
enum test_llvm__testcase {
1213
LLVM_TESTCASE_BASE,
1314
LLVM_TESTCASE_KBUILD,
1415
LLVM_TESTCASE_BPF_PROLOGUE,
16+
LLVM_TESTCASE_BPF_RELOCATION,
1517
__LLVM_TESTCASE_MAX,
1618
};
1719

1820
int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz,
19-
enum test_llvm__testcase index, bool force);
21+
enum test_llvm__testcase index, bool force,
22+
bool *should_load_fail);
2023
#endif

0 commit comments

Comments
 (0)