Skip to content

Commit 86ed4be

Browse files
jrfastabborkmann
authored andcommitted
bpf, selftests: Add tests for ctx access in sock_ops with single register
To verify fix ("bpf: sock_ops ctx access may stomp registers in corner case") we want to force compiler to generate the following code when accessing a field with BPF_TCP_SOCK_GET_COMMON, r1 = *(u32 *)(r1 + 96) // r1 is skops ptr Rather than depend on clang to do this we add the test with inline asm to the tcpbpf test. This saves us from having to create another runner and ensures that if we break this again test_tcpbpf will crash. With above code we get the xlated code, 11: (7b) *(u64 *)(r1 +32) = r9 12: (61) r9 = *(u32 *)(r1 +28) 13: (15) if r9 == 0x0 goto pc+4 14: (79) r9 = *(u64 *)(r1 +32) 15: (79) r1 = *(u64 *)(r1 +0) 16: (61) r1 = *(u32 *)(r1 +2348) 17: (05) goto pc+1 18: (79) r9 = *(u64 *)(r1 +32) We also add the normal case where src_reg != dst_reg so we can compare code generation easily from llvm-objdump and ensure that case continues to work correctly. The normal code is xlated to, 20: (b7) r1 = 0 21: (61) r1 = *(u32 *)(r3 +28) 22: (15) if r1 == 0x0 goto pc+2 23: (79) r1 = *(u64 *)(r3 +0) 24: (61) r1 = *(u32 *)(r1 +2348) Where the temp variable is not used. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Song Liu <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/159718351457.4728.3295119261717842496.stgit@john-Precision-5820-Tower
1 parent 84f44df commit 86ed4be

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ SEC("sockops")
5454
int bpf_testcb(struct bpf_sock_ops *skops)
5555
{
5656
char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)];
57+
struct bpf_sock_ops *reuse = skops;
5758
struct tcphdr *thdr;
5859
int good_call_rv = 0;
5960
int bad_call_rv = 0;
@@ -62,6 +63,18 @@ int bpf_testcb(struct bpf_sock_ops *skops)
6263
int v = 0;
6364
int op;
6465

66+
/* Test reading fields in bpf_sock_ops using single register */
67+
asm volatile (
68+
"%[reuse] = *(u32 *)(%[reuse] +96)"
69+
: [reuse] "+r"(reuse)
70+
:);
71+
72+
asm volatile (
73+
"%[op] = *(u32 *)(%[skops] +96)"
74+
: [op] "+r"(op)
75+
: [skops] "r"(skops)
76+
:);
77+
6578
op = (int) skops->op;
6679

6780
update_event_map(op);

0 commit comments

Comments
 (0)