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

selftests/bpf: add CO-RE relocs ptr-as-array tests

Add test validating correct relocation handling for cases where pointer
to something is used as an array. E.g.:

int *ptr = ...;
int x = ptr[42];

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
d698f9db 9654e2ae

+69
+20
tools/testing/selftests/bpf/prog_tests/core_reloc.c
··· 129 129 .output_len = sizeof(struct core_reloc_mods_output), \ 130 130 } 131 131 132 + #define PTR_AS_ARR_CASE(name) { \ 133 + .case_name = #name, \ 134 + .bpf_obj_file = "test_core_reloc_ptr_as_arr.o", \ 135 + .btf_src_file = "btf__core_reloc_" #name ".o", \ 136 + .input = (const char *)&(struct core_reloc_##name []){ \ 137 + { .a = 1 }, \ 138 + { .a = 2 }, \ 139 + { .a = 3 }, \ 140 + }, \ 141 + .input_len = 3 * sizeof(struct core_reloc_##name), \ 142 + .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) { \ 143 + .a = 3, \ 144 + }, \ 145 + .output_len = sizeof(struct core_reloc_ptr_as_arr), \ 146 + } 147 + 132 148 struct core_reloc_test_case { 133 149 const char *case_name; 134 150 const char *bpf_obj_file; ··· 216 200 MODS_CASE(mods), 217 201 MODS_CASE(mods___mod_swap), 218 202 MODS_CASE(mods___typedefs), 203 + 204 + /* handling "ptr is an array" semantics */ 205 + PTR_AS_ARR_CASE(ptr_as_arr), 206 + PTR_AS_ARR_CASE(ptr_as_arr___diff_sz), 219 207 }; 220 208 221 209 struct data {
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ptr_as_arr.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ptr_as_arr x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ptr_as_arr___diff_sz.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ptr_as_arr___diff_sz x) {}
+13
tools/testing/selftests/bpf/progs/core_reloc_types.h
··· 526 526 int3_t b; 527 527 int3_t a; 528 528 }; 529 + 530 + /* 531 + * PTR_AS_ARR 532 + */ 533 + struct core_reloc_ptr_as_arr { 534 + int a; 535 + }; 536 + 537 + struct core_reloc_ptr_as_arr___diff_sz { 538 + int :32; /* padding */ 539 + char __some_more_padding; 540 + int a; 541 + };
+30
tools/testing/selftests/bpf/progs/test_core_reloc_ptr_as_arr.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2019 Facebook 3 + 4 + #include <linux/bpf.h> 5 + #include <stdint.h> 6 + #include "bpf_helpers.h" 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + static volatile struct data { 11 + char in[256]; 12 + char out[256]; 13 + } data; 14 + 15 + struct core_reloc_ptr_as_arr { 16 + int a; 17 + }; 18 + 19 + SEC("raw_tracepoint/sys_enter") 20 + int test_core_ptr_as_arr(void *ctx) 21 + { 22 + struct core_reloc_ptr_as_arr *in = (void *)&data.in; 23 + struct core_reloc_ptr_as_arr *out = (void *)&data.out; 24 + 25 + if (BPF_CORE_READ(&out->a, &in[2].a)) 26 + return 1; 27 + 28 + return 0; 29 + } 30 +