Skip to content

Commit 286684b

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
selftests/bpf: Force -O2 for USDT selftests to cover SIB handling logic
When using GCC on x86-64 to compile an usdt prog with -O1 or higher optimization, the compiler will generate SIB addressing mode for global array and PC-relative addressing mode for global variable, e.g. "1@-96(%rbp,%rax,8)" and "-1@4+t1(%rip)". In this patch: - force -O2 optimization for usdt.test.o to generate SIB addressing usdt argument spec. - change the global variable t1 to a local variable, to avoid compiler generating PC-relative addressing mode for it. Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 876e27d commit 286684b

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,14 @@ TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
760760
TRUNNER_BPF_CFLAGS :=
761761
$(eval $(call DEFINE_TEST_RUNNER,test_maps))
762762

763+
# Force usdt.c to use -O2 optimization to generate SIB addressing
764+
# Only apply on x86 architecture where SIB addressing is relevant
765+
ifeq ($(ARCH), x86)
766+
$(OUTPUT)/usdt.test.o: CFLAGS:=$(subst O0,O2,$(CFLAGS))
767+
$(OUTPUT)/cpuv4/usdt.test.o: CFLAGS:=$(subst O0,O2,$(CFLAGS))
768+
$(OUTPUT)/no_alu32/usdt.test.o: CFLAGS:=$(subst O0,O2,$(CFLAGS))
769+
endif
770+
763771
# Define test_verifier test runner.
764772
# It is much simpler than test_maps/test_progs and sufficiently different from
765773
# them (e.g., test.h is using completely pattern), that it's worth just

tools/testing/selftests/bpf/prog_tests/usdt.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ static volatile int idx = 2;
1414
static volatile __u64 bla = 0xFEDCBA9876543210ULL;
1515
static volatile short nums[] = {-1, -2, -3, -4};
1616

17-
static volatile struct {
18-
int x;
19-
signed char y;
20-
} t1 = { 1, -127 };
17+
/*
18+
* TODO: At O2 optimization level, t1's USDT argument spec becomes -1@4+t1(%rip).
19+
* Since libbpf doesn't support RIP addressing mode yet, this causes "unrecognized register" errors.
20+
* This test will be re-enabled once libbpf supports RIP addressing mode.
21+
*/
22+
// static volatile struct {
23+
// int x;
24+
// signed char y;
25+
// } t1 = { 1, -127 };
2126

2227
#define SEC(name) __attribute__((section(name), used))
2328

@@ -27,6 +32,7 @@ unsigned short test_usdt12_semaphore SEC(".probes");
2732

2833
static void __always_inline trigger_func(int x) {
2934
long y = 42;
35+
signed char t1 = -127;
3036

3137
if (test_usdt0_semaphore)
3238
STAP_PROBE(test, usdt0);
@@ -36,7 +42,7 @@ static void __always_inline trigger_func(int x) {
3642
STAP_PROBE12(test, usdt12,
3743
x, x + 1, y, x + y, 5,
3844
y / 7, bla, &bla, -9, nums[x],
39-
nums[idx], t1.y);
45+
nums[idx], t1);
4046
}
4147
}
4248

@@ -106,7 +112,7 @@ static void subtest_basic_usdt(void)
106112
ASSERT_EQ(bss->usdt12_args[8], -9, "usdt12_arg9");
107113
ASSERT_EQ(bss->usdt12_args[9], nums[1], "usdt12_arg10");
108114
ASSERT_EQ(bss->usdt12_args[10], nums[idx], "usdt12_arg11");
109-
ASSERT_EQ(bss->usdt12_args[11], t1.y, "usdt12_arg12");
115+
ASSERT_EQ(bss->usdt12_args[11], -127, "usdt12_arg12");
110116

111117
int usdt12_expected_arg_sizes[12] = { 4, 4, 8, 8, 4, 8, 8, 8, 4, 2, 2, 1 };
112118

0 commit comments

Comments
 (0)