Skip to content

Commit 172626e

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
selftests/bpf: Add an usdt_o2 test 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: - add usdt_o2 test case to cover SIB addressing usdt argument spec handling logic Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 4be0534 commit 172626e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ 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+
763764
# Define test_verifier test runner.
764765
# It is much simpler than test_maps/test_progs and sufficiently different from
765766
# them (e.g., test.h is using completely pattern), that it's worth just
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Jiawei Zhao <[email protected]>. */
3+
#include <test_progs.h>
4+
5+
#include "../sdt.h"
6+
#include "test_usdt_o2.skel.h"
7+
8+
#if defined(__GNUC__) && !defined(__clang__)
9+
__attribute__((optimize("O2")))
10+
#endif
11+
12+
#define test_value 0xFEDCBA9876543210ULL
13+
#define SEC(name) __attribute__((section(name), used))
14+
15+
int lets_test_this(int);
16+
static volatile __u64 array[1] = {test_value};
17+
18+
static __always_inline void trigger_func(void)
19+
{
20+
/* Base address + offset + (index * scale) */
21+
for (volatile int i = 0; i <= 0; i++)
22+
STAP_PROBE1(test, usdt1, array[i]);
23+
}
24+
25+
static void basic_sib_usdt(void)
26+
{
27+
LIBBPF_OPTS(bpf_usdt_opts, opts);
28+
struct test_usdt_o2 *skel;
29+
struct test_usdt_o2__bss *bss;
30+
int err;
31+
32+
skel = test_usdt_o2__open_and_load();
33+
if (!ASSERT_OK_PTR(skel, "skel_open"))
34+
return;
35+
36+
bss = skel->bss;
37+
bss->my_pid = getpid();
38+
39+
err = test_usdt_o2__attach(skel);
40+
if (!ASSERT_OK(err, "skel_attach"))
41+
goto cleanup;
42+
43+
/* usdt1 won't be auto-attached */
44+
opts.usdt_cookie = 0xcafedeadbeeffeed;
45+
skel->links.usdt1 = bpf_program__attach_usdt(skel->progs.usdt1,
46+
0 /*self*/, "/proc/self/exe",
47+
"test", "usdt1", &opts);
48+
if (!ASSERT_OK_PTR(skel->links.usdt1, "usdt1_link"))
49+
goto cleanup;
50+
51+
trigger_func();
52+
53+
ASSERT_EQ(bss->usdt1_called, 1, "usdt1_called");
54+
ASSERT_EQ(bss->usdt1_cookie, 0xcafedeadbeeffeed, "usdt1_cookie");
55+
ASSERT_EQ(bss->usdt1_arg_cnt, 1, "usdt1_arg_cnt");
56+
ASSERT_EQ(bss->usdt1_arg, test_value, "usdt1_arg");
57+
ASSERT_EQ(bss->usdt1_arg_ret, 0, "usdt1_arg_ret");
58+
ASSERT_EQ(bss->usdt1_arg_size, sizeof(array[0]), "usdt1_arg_size");
59+
60+
cleanup:
61+
test_usdt_o2__destroy(skel);
62+
}
63+
64+
65+
66+
void test_usdt_o2(void)
67+
{
68+
basic_sib_usdt();
69+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/usdt.bpf.h>
7+
8+
int my_pid;
9+
10+
int usdt1_called;
11+
u64 usdt1_cookie;
12+
int usdt1_arg_cnt;
13+
int usdt1_arg_ret;
14+
u64 usdt1_arg;
15+
int usdt1_arg_size;
16+
17+
SEC("usdt")
18+
int usdt1(struct pt_regs *ctx)
19+
{
20+
long tmp;
21+
22+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
23+
return 0;
24+
25+
__sync_fetch_and_add(&usdt1_called, 1);
26+
27+
usdt1_cookie = bpf_usdt_cookie(ctx);
28+
usdt1_arg_cnt = bpf_usdt_arg_cnt(ctx);
29+
30+
usdt1_arg_ret = bpf_usdt_arg(ctx, 0, &tmp);
31+
usdt1_arg = (u64)tmp;
32+
usdt1_arg_size = bpf_usdt_arg_size(ctx, 0);
33+
34+
return 0;
35+
}
36+
37+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)