Re: [PATCH bpf 2/2] selftests/bpf: Add tests for uprobe nop5 red zone clobbering
Andrii Nakryiko <[email protected]> Mon, 11 May 2026 18:58:59 +0200
| Newsgroups | dev.linux.lists.sashiko,org.kernel.vger.bpf |
|---|---|
| Message-ID | <CAEf4BzZ-nASpoF+-vLYZ1D8eWRVd5JWAuhrwx8XZNrX7ZPY5_Q@mail.gmail.com> |
On Fri, May 8, 2026 at 7:12 PM <[email protected]> wrote: > > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: > - [Medium] The `optimized_red_zone` USDT subtest is brittle and may produce false positive passes because it relies on the compiler arbitrarily placing operands in the stack's red zone. > -- > > commit 1fbbd69b145cd2d80be83d6482e65179431782ac > Author: Andrii Nakryiko <[email protected]> > > selftests/bpf: Add tests for uprobe nop5 red zone clobbering > > This commit adds tests to verify that the uprobe nop5 CALL optimization does > not clobber the x86-64 stack red zone. It introduces an assembly-level test > and a USDT subtest which intends to trigger a probe in a function where the > compiler places operands in the red zone. > > > diff --git a/tools/testing/selftests/bpf/usdt_2.c b/tools/testing/selftests/bpf/usdt_2.c > > index 789883aaca4c3..fc7e6d220a38b 100644 > > --- a/tools/testing/selftests/bpf/usdt_2.c > > +++ b/tools/testing/selftests/bpf/usdt_2.c > > [ ... ] > > > @@ -13,4 +13,17 @@ void usdt_2(void) > > USDT(optimized_attach, usdt_2); > > } > > > > +static volatile unsigned long usdt_red_zone_arg1 = 0xDEADBEEF; > > +static volatile unsigned long usdt_red_zone_arg2 = 0xCAFEBABE; > > +static volatile unsigned long usdt_red_zone_arg3 = 0xFEEDFACE; > > + > > +void __attribute__((noinline)) usdt_red_zone_trigger(void) > > +{ > > + unsigned long a1 = usdt_red_zone_arg1; > > + unsigned long a2 = usdt_red_zone_arg2; > > + unsigned long a3 = usdt_red_zone_arg3; > > + > > + USDT(optimized_attach, usdt_red_zone, a1, a2, a3); > > Is this test reliable across different compiler versions and optimizations? > No it's not, which is why I added an assembly-based test as well. It doesn't hurt to have a C-level example which does trigger a red zone issue in practice to show the issue is not hypothetical. But I can also drop it, if people think it's unnecessary. > The x86-64 USDT macro uses the "nor" inline assembly constraint, which allows > the compiler to allocate these operands to CPU registers. Because there is no > register pressure in usdt_red_zone_trigger(), modern compilers might allocate > the local variables a1, a2, and a3 directly to registers rather than the > stack's red zone. we compile selftests in -O0 mode, so unlikely, but in release mode it will definitely be optimized like that > > If the operands are placed in registers, the uprobe CALL optimization > clobbering [rsp-8] will not corrupt them. This could cause the test to pass > even on buggy kernels, creating a false positive. > > Would it be safer to explicitly force these operands into the red zone > using inline assembly constraints, rather than depending on the compiler's > register allocator? > > > +} > > + > > #endif > > -- > Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2