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