Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: bpf_rdonly_cast u{8,16,32,64} access tests

Tests with aligned and misaligned memory access of different sizes via
pointer returned by bpf_rdonly_cast().

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20250627015539.1439656-1-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
c4b1be92 ffaff180

+41
+41
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
··· 133 133 return *p; 134 134 } 135 135 136 + __attribute__((__aligned__(8))) 137 + u8 global[] = { 138 + 0x11, 0x22, 0x33, 0x44, 139 + 0x55, 0x66, 0x77, 0x88, 140 + 0x99 141 + }; 142 + 143 + __always_inline 144 + static u64 combine(void *p) 145 + { 146 + u64 acc; 147 + 148 + acc = 0; 149 + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 150 + acc |= (*(u64 *)p >> 56) << 24; 151 + acc |= (*(u32 *)p >> 24) << 16; 152 + acc |= (*(u16 *)p >> 8) << 8; 153 + acc |= *(u8 *)p; 154 + #else 155 + acc |= (*(u64 *)p & 0xff) << 24; 156 + acc |= (*(u32 *)p & 0xff) << 16; 157 + acc |= (*(u16 *)p & 0xff) << 8; 158 + acc |= *(u8 *)p; 159 + #endif 160 + return acc; 161 + } 162 + 163 + SEC("socket") 164 + __retval(0x88442211) 165 + int diff_size_access(void *ctx) 166 + { 167 + return combine(bpf_rdonly_cast(&global, 0)); 168 + } 169 + 170 + SEC("socket") 171 + __retval(0x99553322) 172 + int misaligned_access(void *ctx) 173 + { 174 + return combine(bpf_rdonly_cast(&global, 0) + 1); 175 + } 176 + 136 177 char _license[] SEC("license") = "GPL";