Skip to content

Commit c4b1be9

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
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>
1 parent ffaff18 commit c4b1be9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,45 @@ int mixed_mem_type(void *ctx)
133133
return *p;
134134
}
135135

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+
136177
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)