Skip to content

Commit 6254683

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
selftests/bpf: make usdt_o2 reliably generate SIB USDT arg spec
usdt_o2 is intended to exercise the SIB (Scale-Index-Base) argument handling in libbpf's USDT path. With GCC 13 this reliably produced a SIB-form argument (e.g. 8@(%rdx,%rax,8)), but with newer GCC (e.g. 15) the compiler frequently optimizes the probe argument into a plain register (e.g. 8@%rax) or a stack slot, so the test stops covering the SIB code path and becomes flaky across toolchains. Force a SIB memory operand in the probe by: * placing the base pointer into %rdx and the index into %rax using an empty inline asm with output constraints ("=d", "=a") and matching inputs * immediately passing base[idx] to STAP_PROBE1. * only enable on x86 platform. This makes the compiler encode the operand as SIB (base + index8), which in .note.stapsdt shows up as 8@(%rdx,%rax,8) regardless of GCC version. A memory clobber and noinline prevent reordering/re-allocation around the probe site. This change is x86_64-specific and does not alter program semantics; it only stabilizes the USDT argument shape so the test consistently validates SIB handling. Clang historically prefers stack temporaries for such operands, but the selftests build with GCC, and this keeps behavior stable across GCC versions without introducing a separate .S file. Signed-off-by: Jiawei Zhao <[email protected]>
1 parent bcaef48 commit 6254683

File tree

1 file changed

+15
-5
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ __attribute__((optimize("O2")))
1515
int lets_test_this(int);
1616
static volatile __u64 array[1] = {test_value};
1717

18-
static __always_inline void trigger_func(void)
18+
static noinline void trigger_func(void)
1919
{
20+
#if defined(__x86_64__) || defined(__i386__)
2021
/* Base address + offset + (index * scale) */
21-
for (volatile int i = 0; i <= 0; i++)
22-
STAP_PROBE1(test, usdt1, array[i]);
22+
/* Force SIB addressing with inline assembly */
23+
const __u64 *base;
24+
__u32 idx;
25+
/* binding base to %rdx and idx to %rax */
26+
asm volatile("" : "=d"(base), "=a"(idx) : "0"(array), "1"((__u32)0) : "memory");
27+
STAP_PROBE1(test, usdt1, base[idx]);
28+
#else
29+
STAP_PROBE1(test, usdt1, array[0]);
30+
#endif
2331
}
2432

2533
static void basic_sib_usdt(void)
@@ -61,9 +69,11 @@ static void basic_sib_usdt(void)
6169
test_usdt_o2__destroy(skel);
6270
}
6371

64-
65-
6672
void test_usdt_o2(void)
6773
{
74+
#if !defined(__x86_64__) && !defined(__i386__)
75+
test__skip();
76+
return;
77+
#endif
6878
basic_sib_usdt();
6979
}

0 commit comments

Comments
 (0)