Skip to contentSkip to conversation

selftests/bpf: Fix CO-RE relocation of anonymous unions for bpf-gcc

PATCH In review Jiayuan Chen 2 participants 3 messages Nov 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Fix CO-RE relocation of anonymous unions for bpf-gcc

bpf-gcc fails to load test_trace_iter_unix:

Error: #766/6 verifier_sockmap_mutate/test_trace_iter_unix
libbpf: elf: skipping section(3) .data (size 0)
libbpf: elf: skipping unrecognized data section(18) .comment run_subtest:PASS:obj_open_mem 0 nsec
libbpf: prog 'test_trace_iter_unix': relo #0: insn #0 (LDX/ST/STX) accesses field incorrectly. ...
libbpf: prog 'test_trace_iter_unix': BPF program load failed: -EINVAL
libbpf: prog 'test_trace_iter_unix': failed to load: -EINVAL
libbpf: failed to load object 'verifier_sockmap_mutate' run_subtest:PASS:unexpected_load_success 0 nsec
validate_msgs:FAIL:1014 expect_msg
VERIFIER LOG:
=============
0: R1=ctx() R10=fp0
; @ verifier_sockmap_mutate.c:124
0: <invalid CO-RE relocation>
failed to resolve CO-RE relocation <byte_off> [4] struct bpf_iter__unix (0:0 @ offset 0) processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============
EXPECTED SUBSTR: 'cannot update sockmap in this context'

GCC only applies preserve_access_index to types that carry the attribute themselves. The anonymous union holding the sock pointer doesn't, so the CO-RE access stops at the union ("0:0") instead of reaching the pointer ("0:0:0"), and libbpf rejects the relocation. Mark the unions too.

Fixes e16c83fa764f ("selftests/bpf: Add negative test for sockmap update from non-sockmap iter")
---
 tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +2 -2
index 1516fe1524bb..68b1a5323470 100644
@@ -16,13 +16,13 @@ struct sock {
 struct bpf_iter__sockmap {
 	union {
 		struct sock *sk;
-	};
+	} __attribute__((preserve_access_index));
 } __attribute__((preserve_access_index));
 
 struct bpf_iter__unix {
 	union {
 		struct sock *unix_sk;
-	};
+	} __attribute__((preserve_access_index));
 } __attribute__((preserve_access_index));
 
 struct {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 43 lines · Jiayuan Chen

bpf-gcc fails to load test_trace_iter_unix:

Error: #766/6 verifier_sockmap_mutate/test_trace_iter_unix
libbpf: elf: skipping section(3) .data (size 0)
libbpf: elf: skipping unrecognized data section(18) .comment run_subtest:PASS:obj_open_mem 0 nsec
libbpf: prog 'test_trace_iter_unix': relo #0: insn #0 (LDX/ST/STX) accesses field incorrectly. ...
libbpf: prog 'test_trace_iter_unix': BPF program load failed: -EINVAL
libbpf: prog 'test_trace_iter_unix': failed to load: -EINVAL
libbpf: failed to load object 'verifier_sockmap_mutate' run_subtest:PASS:unexpected_load_success 0 nsec
validate_msgs:FAIL:1014 expect_msg
VERIFIER LOG:
=============
0: R1=ctx() R10=fp0
; @ verifier_sockmap_mutate.c:124
0: <invalid CO-RE relocation>
failed to resolve CO-RE relocation <byte_off> [4] struct bpf_iter__unix (0:0 @ offset 0) processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============
EXPECTED SUBSTR: 'cannot update sockmap in this context'

GCC only applies preserve_access_index to types that carry the attribute themselves. The anonymous union holding the sock pointer doesn't, so the CO-RE access stops at the union ("0:0") instead of reaching the pointer ("0:0:0"), and libbpf rejects the relocation. Mark the unions too.

Fixes e16c83fa764f ("selftests/bpf: Add negative test for sockmap update from non-sockmap iter")
---
 tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +1 -1
index 1516fe1524bb..68b1a5323470 100644
@@ -16,13 +16,13 @@ struct sock {
 struct bpf_iter__sockmap {
 	union {
 		struct sock *sk;
-	};
+	} __attribute__((preserve_access_index));

tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c struct sock { · line 28

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +1 -1
 } __attribute__((preserve_access_index));
 
 struct bpf_iter__unix {
 	union {
 		struct sock *unix_sk;
-	};
+	} __attribute__((preserve_access_index));
 } __attribute__((preserve_access_index));
 
 struct {

What about dropping the unions?

-struct sock {
-} __attribute__((preserve_access_index));
-
struct bpf_iter__sockmap {
- union {
- struct sock *sk;
- };
+ struct sock *sk;
} __attribute__((preserve_access_index));

struct bpf_iter__unix {
- union {
- struct sock *unix_sk;
- };
+ struct sock *unix_sk;
} __attribute__((preserve_access_index));

Works under clang.

#2 lore raw
Quoted 73 lines · Michal Luczaj
Quoted 43 lines · Jiayuan Chen

bpf-gcc fails to load test_trace_iter_unix:

Error: #766/6 verifier_sockmap_mutate/test_trace_iter_unix
libbpf: elf: skipping section(3) .data (size 0)
libbpf: elf: skipping unrecognized data section(18) .comment run_subtest:PASS:obj_open_mem 0 nsec
libbpf: prog 'test_trace_iter_unix': relo #0: insn #0 (LDX/ST/STX) accesses field incorrectly. ...
libbpf: prog 'test_trace_iter_unix': BPF program load failed: -EINVAL
libbpf: prog 'test_trace_iter_unix': failed to load: -EINVAL
libbpf: failed to load object 'verifier_sockmap_mutate' run_subtest:PASS:unexpected_load_success 0 nsec
validate_msgs:FAIL:1014 expect_msg
VERIFIER LOG:
=============
0: R1=ctx() R10=fp0
; @ verifier_sockmap_mutate.c:124
0: <invalid CO-RE relocation>
failed to resolve CO-RE relocation <byte_off> [4] struct bpf_iter__unix (0:0 @ offset 0) processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============
EXPECTED SUBSTR: 'cannot update sockmap in this context'

GCC only applies preserve_access_index to types that carry the attribute themselves. The anonymous union holding the sock pointer doesn't, so the CO-RE access stops at the union ("0:0") instead of reaching the pointer ("0:0:0"), and libbpf rejects the relocation. Mark the unions too.

Fixes e16c83fa764f ("selftests/bpf: Add negative test for sockmap update from non-sockmap iter")
---
 tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +1 -1
index 1516fe1524bb..68b1a5323470 100644
@@ -16,13 +16,13 @@ struct sock {
 struct bpf_iter__sockmap {
 	union {
 		struct sock *sk;
-	};
+	} __attribute__((preserve_access_index));

tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c struct sock { · line 28

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +1 -1
 } __attribute__((preserve_access_index));
 
 struct bpf_iter__unix {
 	union {
 		struct sock *unix_sk;
-	};
+	} __attribute__((preserve_access_index));
 } __attribute__((preserve_access_index));
 
 struct {

What about dropping the unions?

(quoted diff) +2 -9
-struct sock {
-} __attribute__((preserve_access_index));
-
 struct bpf_iter__sockmap {
-       union {
-               struct sock *sk;
-       };
+       struct sock *sk;
 } __attribute__((preserve_access_index));
 
 struct bpf_iter__unix {
-       union {
-               struct sock *unix_sk;
-       };
+       struct sock *unix_sk;
 } __attribute__((preserve_access_index));
Quoted 1 line

Works under clang.

Right. I grepped the tree and verifier_sockmap_mutate.c is the only place that puts an anonymous union inside a hand-written preserve_access_index struct, so the test doesn't need it.