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_existence_output {
17 	int a_exists;
18 	int a_value;
19 	int b_exists;
20 	int b_value;
21 	int c_exists;
22 	int c_value;
23 	int arr_exists;
24 	int arr_value;
25 	int s_exists;
26 	int s_value;
27 };
28 
29 struct core_reloc_existence {
30 	struct {
31 		int x;
32 	} s;
33 	int arr[1];
34 	int a;
35 	struct {
36 		int b;
37 	};
38 	int c;
39 };
40 
41 SEC("raw_tracepoint/sys_enter")
test_core_existence(void * ctx)42 int test_core_existence(void *ctx)
43 {
44 	struct core_reloc_existence *in = (void *)&data.in;
45 	struct core_reloc_existence_output *out = (void *)&data.out;
46 
47 	out->a_exists = bpf_core_field_exists(in->a);
48 	if (bpf_core_field_exists(struct core_reloc_existence, a))
49 		out->a_value = BPF_CORE_READ(in, a);
50 	else
51 		out->a_value = 0xff000001u;
52 
53 	out->b_exists = bpf_core_field_exists(in->b);
54 	if (bpf_core_field_exists(struct core_reloc_existence, b))
55 		out->b_value = BPF_CORE_READ(in, b);
56 	else
57 		out->b_value = 0xff000002u;
58 
59 	out->c_exists = bpf_core_field_exists(in->c);
60 	if (bpf_core_field_exists(struct core_reloc_existence, c))
61 		out->c_value = BPF_CORE_READ(in, c);
62 	else
63 		out->c_value = 0xff000003u;
64 
65 	out->arr_exists = bpf_core_field_exists(in->arr);
66 	if (bpf_core_field_exists(struct core_reloc_existence, arr))
67 		out->arr_value = BPF_CORE_READ(in, arr[0]);
68 	else
69 		out->arr_value = 0xff000004u;
70 
71 	out->s_exists = bpf_core_field_exists(in->s);
72 	if (bpf_core_field_exists(struct core_reloc_existence, s))
73 		out->s_value = BPF_CORE_READ(in, s.x);
74 	else
75 		out->s_value = 0xff000005u;
76 
77 	return 0;
78 }
79