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/bpf_helpers.h>
7 #include <bpf/bpf_core_read.h>
8
9 char _license[] SEC("license") = "GPL";
10
11 struct {
12 char in[256];
13 char out[256];
14 } data = {};
15
16 struct core_reloc_misc_output {
17 int a, b, c;
18 };
19
20 struct core_reloc_misc___a {
21 int a1;
22 int a2;
23 };
24
25 struct core_reloc_misc___b {
26 int b1;
27 int b2;
28 };
29
30 /* fixed two first members, can be extended with new fields */
31 struct core_reloc_misc_extensible {
32 int a;
33 int b;
34 };
35
36 #define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
37
38 SEC("raw_tracepoint/sys_enter")
test_core_misc(void * ctx)39 int test_core_misc(void *ctx)
40 {
41 struct core_reloc_misc___a *in_a = (void *)&data.in;
42 struct core_reloc_misc___b *in_b = (void *)&data.in;
43 struct core_reloc_misc_extensible *in_ext = (void *)&data.in;
44 struct core_reloc_misc_output *out = (void *)&data.out;
45
46 /* record two different relocations with the same accessor string */
47 if (CORE_READ(&out->a, &in_a->a1) || /* accessor: 0:0 */
48 CORE_READ(&out->b, &in_b->b1)) /* accessor: 0:0 */
49 return 1;
50
51 /* Validate relocations capture array-only accesses for structs with
52 * fixed header, but with potentially extendable tail. This will read
53 * first 4 bytes of 2nd element of in_ext array of potentially
54 * variably sized struct core_reloc_misc_extensible. */
55 if (CORE_READ(&out->c, &in_ext[2])) /* accessor: 2 */
56 return 1;
57
58 return 0;
59 }
60
61