Skip to contentSkip to conversation

selftests/bpf: cast explicitly in the arena subprog and callback faults

PATCH Superseded Vineet Gupta 1 participant 1 message Oct 28

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 6/7 selftests/bpf: cast explicitly in the arena subprog and callback faults

Four stream tests fail with BPF_GCC:

test_stream_arena_fault_address:FAIL:stream__open_and_load unexpected error: -13 #479 stream_oversize:FAIL
#480 stream_partial_read:FAIL
#482 stream_syscall:FAIL

They call stream__open_and_load(), which loads every program in the object, and two of those do not verify:

#481/11 stream_success/stream_arena_subprog_fault:FAIL #481/12 stream_success/stream_arena_callback_fault:FAIL

Both dereference an arena pointer and leave the cast to the compiler:

  int __arena *addr = (int __arena *)0xdeadbeef;
  *addr = 1;

Without arena support __arena is only btf_type_tag("arena"), so no addr_space_cast is emitted and the store is to a scalar, which the verifier rejects. GCC splits it into byte stores, 0xdeadbeef not being aligned:

r2=3735928559 ll
*(u8*)(r2+0)=1 <- plain store, no cast

Their siblings in the same file do not rely on the compiler; they use bpf_addr_space_cast(), which emits the instruction byte by byte and works anywhere. Do the same here.

The address space has to come off the local as well. Left on, a compiler that supports arenas casts a second time for the dereference, on a pointer the explicit cast has already converted:

r2 = addr_space_cast(r2, 0, 1)
r3 = addr_space_cast(r2, 0, 1)
*(u32*)(r3+0)=1

With it off, GCC with and without arena support and clang all emit the same thing, which is what the test wants:

r2 = addr_space_cast(r2, 0, 1)
*(u32*)(r2+0)=1

What these two assert is the fault report, not how the pointer was produced, so nothing they were testing is lost.

Measured on bpf-next with a stock bpf-gcc, test_progs before and after. Fifteen results go FAIL -> OK -- every test that loads this object, not just the arena ones:

  stream_arena_fault_address             stream_oversize
  stream_fd        stream_fd/blocking    stream_partial_read
  stream_fd/edge   stream_fd/empty       stream_success
  stream_fd/hup    stream_fd/nmi_epoll   stream_success/stream_arena_callback_fault
  stream_fd/open   stream_fd/nonblock    stream_success/stream_arena_subprog_fault

stream_syscall

and the five stream_arena_fault_address subtests become visible for the first time (read, write, xchg, cmpxchg and load_acquire faults).

---
 tools/testing/selftests/bpf/progs/stream.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/stream.c +4 -3
index 1ae015c4e6b4..dc56779cac06 100644
@@ -5,7 +5,6 @@
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
 #include "bpf_experimental.h"
-#include <bpf_arena_common.h>
 
 struct arr_elem {
 	struct bpf_res_spin_lock lock;
@@ -381,9 +380,10 @@ int stream_arena_cmpxchg_fault(void *ctx)
 
 static __noinline void subprog(void)
 {
-	int __arena *addr = (int __arena *)0xdeadbeef;
+	int *addr = (int *)0xdeadbeef;
 
 	arena_ptr = &arena;
+	bpf_addr_space_cast(addr, 0, 1);
 	*addr = 1;
 }
 
@@ -404,9 +404,10 @@ int stream_arena_subprog_fault(void *ctx)
 
 static __noinline int timer_cb(void *map, int *key, struct bpf_timer *timer)
 {
-	int __arena *addr = (int __arena *)0xdeadbeef;
+	int *addr = (int *)0xdeadbeef;
 
 	arena_ptr = &arena;
+	bpf_addr_space_cast(addr, 0, 1);
 	*addr = 1;
 	return 0;
 }
Signature · 1 line
2.53.0-Meta