Skip to content

Commit 4bddb57

Browse files
iii-igregkh
authored andcommitted
selftests/bpf: Fix test_core_autosize on big-endian machines
[ Upstream commit d164dd9 ] The "probed" part of test_core_autosize copies an integer using bpf_core_read() into an integer of a potentially different size. On big-endian machines a destination offset is required for this to produce a sensible result. Fixes: 888d83b ("selftests/bpf: Validate libbpf's auto-sizing of LD/ST/STX instructions") Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20210812224814.187460-1-iii@linux.ibm.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 0f625e1 commit 4bddb57

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎tools/testing/selftests/bpf/progs/test_core_autosize.c‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,40 @@ int handle_downsize(void *ctx)
125125
return 0;
126126
}
127127

128+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
129+
#define bpf_core_read_int bpf_core_read
130+
#else
131+
#define bpf_core_read_int(dst, sz, src) ({ \
132+
/* Prevent "subtraction from stack pointer prohibited" */ \
133+
volatile long __off = sizeof(*dst) - (sz); \
134+
bpf_core_read((char *)(dst) + __off, sz, src); \
135+
})
136+
#endif
137+
128138
SEC("raw_tp/sys_enter")
129139
int handle_probed(void *ctx)
130140
{
131141
struct test_struct___downsize *in = (void *)&input;
132142
__u64 tmp;
133143

134144
tmp = 0;
135-
bpf_core_read(&tmp, bpf_core_field_size(in->ptr), &in->ptr);
145+
bpf_core_read_int(&tmp, bpf_core_field_size(in->ptr), &in->ptr);
136146
ptr_probed = tmp;
137147

138148
tmp = 0;
139-
bpf_core_read(&tmp, bpf_core_field_size(in->val1), &in->val1);
149+
bpf_core_read_int(&tmp, bpf_core_field_size(in->val1), &in->val1);
140150
val1_probed = tmp;
141151

142152
tmp = 0;
143-
bpf_core_read(&tmp, bpf_core_field_size(in->val2), &in->val2);
153+
bpf_core_read_int(&tmp, bpf_core_field_size(in->val2), &in->val2);
144154
val2_probed = tmp;
145155

146156
tmp = 0;
147-
bpf_core_read(&tmp, bpf_core_field_size(in->val3), &in->val3);
157+
bpf_core_read_int(&tmp, bpf_core_field_size(in->val3), &in->val3);
148158
val3_probed = tmp;
149159

150160
tmp = 0;
151-
bpf_core_read(&tmp, bpf_core_field_size(in->val4), &in->val4);
161+
bpf_core_read_int(&tmp, bpf_core_field_size(in->val4), &in->val4);
152162
val4_probed = tmp;
153163

154164
return 0;

0 commit comments

Comments
 (0)