Skip to content

Commit c56b9ab

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
selftests/bpf: Enrich subtest_basic_usdt case in 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: - enrich subtest_basic_usdt test case to cover SIB addressing usdt argument spec handling logic Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 96b976b commit c56b9ab

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010

1111
int lets_test_this(int);
1212

13+
#if defined(__x86_64__) || defined(__i386__)
14+
/*
15+
* SIB (Scale-Index-Base) addressing format:
16+
* "size@(base_reg, index_reg, scale)"
17+
* - 'size' is the size in bytes of the array element, and its sign indicates
18+
* whether the type is signed (negative) or unsigned (positive).
19+
* - 'base_reg' is the register holding the base address, normally rdx or edx
20+
* - 'index_reg' is the register holding the index, normally rax or eax
21+
* - 'scale' is the scaling factor (typically 1, 2, 4, or 8), which matches the
22+
* size of the element type.
23+
*
24+
* For example, for an array of 'short' (signed 2-byte elements), the SIB spec would be:
25+
* - size: -2 (negative because 'short' is signed)
26+
* - scale: 2 (since sizeof(short) == 2)
27+
* The resulting SIB format: "-2@(%%rdx,%%rax,2)"
28+
*/
29+
static volatile short array[] = {-1, -2, -3, -4};
30+
#define USDT_SIB_ARG_SPEC -2@(%%rdx,%%rax,2)
31+
#endif
32+
1333
static volatile int idx = 2;
1434
static volatile __u64 bla = 0xFEDCBA9876543210ULL;
1535
static volatile short nums[] = {-1, -2, -3, -4};
@@ -25,6 +45,10 @@ unsigned short test_usdt0_semaphore SEC(".probes");
2545
unsigned short test_usdt3_semaphore SEC(".probes");
2646
unsigned short test_usdt12_semaphore SEC(".probes");
2747

48+
#if defined(__x86_64__) || defined(__i386__)
49+
unsigned short test_usdt_sib_semaphore SEC(".probes");
50+
#endif
51+
2852
static void __always_inline trigger_func(int x) {
2953
long y = 42;
3054

@@ -40,12 +64,27 @@ static void __always_inline trigger_func(int x) {
4064
}
4165
}
4266

67+
#if defined(__x86_64__) || defined(__i386__)
68+
static void trigger_sib_spec(void)
69+
{
70+
/* Base address + offset + (index * scale) */
71+
/* Force SIB addressing with inline assembly */
72+
asm volatile(
73+
STAP_PROBE_ASM(test, usdt_sib, USDT_SIB_ARG_SPEC)
74+
:
75+
: "d"(array), "a"(0)
76+
: "memory"
77+
);
78+
}
79+
#endif
80+
4381
static void subtest_basic_usdt(void)
4482
{
4583
LIBBPF_OPTS(bpf_usdt_opts, opts);
4684
struct test_usdt *skel;
4785
struct test_usdt__bss *bss;
4886
int err, i;
87+
const __u64 expected_cookie = 0xcafedeadbeeffeed;
4988

5089
skel = test_usdt__open_and_load();
5190
if (!ASSERT_OK_PTR(skel, "skel_open"))
@@ -59,20 +98,29 @@ static void subtest_basic_usdt(void)
5998
goto cleanup;
6099

61100
/* usdt0 won't be auto-attached */
62-
opts.usdt_cookie = 0xcafedeadbeeffeed;
101+
opts.usdt_cookie = expected_cookie;
63102
skel->links.usdt0 = bpf_program__attach_usdt(skel->progs.usdt0,
64103
0 /*self*/, "/proc/self/exe",
65104
"test", "usdt0", &opts);
66105
if (!ASSERT_OK_PTR(skel->links.usdt0, "usdt0_link"))
67106
goto cleanup;
68107

108+
#if defined(__x86_64__) || defined(__i386__)
109+
opts.usdt_cookie = expected_cookie;
110+
skel->links.usdt_sib = bpf_program__attach_usdt(skel->progs.usdt_sib,
111+
0 /*self*/, "/proc/self/exe",
112+
"test", "usdt_sib", &opts);
113+
if (!ASSERT_OK_PTR(skel->links.usdt_sib, "usdt_sib_link"))
114+
goto cleanup;
115+
#endif
116+
69117
trigger_func(1);
70118

71119
ASSERT_EQ(bss->usdt0_called, 1, "usdt0_called");
72120
ASSERT_EQ(bss->usdt3_called, 1, "usdt3_called");
73121
ASSERT_EQ(bss->usdt12_called, 1, "usdt12_called");
74122

75-
ASSERT_EQ(bss->usdt0_cookie, 0xcafedeadbeeffeed, "usdt0_cookie");
123+
ASSERT_EQ(bss->usdt0_cookie, expected_cookie, "usdt0_cookie");
76124
ASSERT_EQ(bss->usdt0_arg_cnt, 0, "usdt0_arg_cnt");
77125
ASSERT_EQ(bss->usdt0_arg_ret, -ENOENT, "usdt0_arg_ret");
78126
ASSERT_EQ(bss->usdt0_arg_size, -ENOENT, "usdt0_arg_size");
@@ -156,6 +204,16 @@ static void subtest_basic_usdt(void)
156204
ASSERT_EQ(bss->usdt3_args[1], 42, "usdt3_arg2");
157205
ASSERT_EQ(bss->usdt3_args[2], (uintptr_t)&bla, "usdt3_arg3");
158206

207+
#if defined(__x86_64__) || defined(__i386__)
208+
trigger_sib_spec();
209+
ASSERT_EQ(bss->usdt_sib_called, 1, "usdt_sib_called");
210+
ASSERT_EQ(bss->usdt_sib_cookie, expected_cookie, "usdt_sib_cookie");
211+
ASSERT_EQ(bss->usdt_sib_arg_cnt, 1, "usdt_sib_arg_cnt");
212+
ASSERT_EQ(bss->usdt_sib_arg, nums[0], "usdt_sib_arg");
213+
ASSERT_EQ(bss->usdt_sib_arg_ret, 0, "usdt_sib_arg_ret");
214+
ASSERT_EQ(bss->usdt_sib_arg_size, sizeof(nums[0]), "usdt_sib_arg_size");
215+
#endif
216+
159217
cleanup:
160218
test_usdt__destroy(skel);
161219
}

tools/testing/selftests/bpf/progs/test_usdt.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,36 @@ int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
107107
return 0;
108108
}
109109

110+
111+
int usdt_sib_called;
112+
u64 usdt_sib_cookie;
113+
int usdt_sib_arg_cnt;
114+
int usdt_sib_arg_ret;
115+
u64 usdt_sib_arg;
116+
int usdt_sib_arg_size;
117+
118+
/*
119+
* usdt_sib is only tested on x86-related architectures, so it requires
120+
* manual attach since auto-attach will panic tests under other architectures
121+
*/
122+
SEC("usdt")
123+
int usdt_sib(struct pt_regs *ctx)
124+
{
125+
long tmp;
126+
127+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
128+
return 0;
129+
130+
__sync_fetch_and_add(&usdt_sib_called, 1);
131+
132+
usdt_sib_cookie = bpf_usdt_cookie(ctx);
133+
usdt_sib_arg_cnt = bpf_usdt_arg_cnt(ctx);
134+
135+
usdt_sib_arg_ret = bpf_usdt_arg(ctx, 0, &tmp);
136+
usdt_sib_arg = (short)tmp;
137+
usdt_sib_arg_size = bpf_usdt_arg_size(ctx, 0);
138+
139+
return 0;
140+
}
141+
110142
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)