selftests/bpf: Add type-match checks to type-based tests
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / progs / test_core_reloc_type_based.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3
4 #include <linux/bpf.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_core_read.h>
9
10 char _license[] SEC("license") = "GPL";
11
12 struct {
13         char in[256];
14         char out[256];
15         bool skip;
16 } data = {};
17
18 struct a_struct {
19         int x;
20 };
21
22 union a_union {
23         int y;
24         int z;
25 };
26
27 typedef struct a_struct named_struct_typedef;
28
29 typedef struct { int x, y, z; } anon_struct_typedef;
30
31 typedef struct {
32         int a, b, c;
33 } *struct_ptr_typedef;
34
35 enum an_enum {
36         AN_ENUM_VAL1 = 1,
37         AN_ENUM_VAL2 = 2,
38         AN_ENUM_VAL3 = 3,
39 };
40
41 typedef int int_typedef;
42
43 typedef enum { TYPEDEF_ENUM_VAL1, TYPEDEF_ENUM_VAL2 } enum_typedef;
44
45 typedef void *void_ptr_typedef;
46
47 typedef int (*func_proto_typedef)(long);
48
49 typedef char arr_typedef[20];
50
51 struct core_reloc_type_based_output {
52         bool struct_exists;
53         bool union_exists;
54         bool enum_exists;
55         bool typedef_named_struct_exists;
56         bool typedef_anon_struct_exists;
57         bool typedef_struct_ptr_exists;
58         bool typedef_int_exists;
59         bool typedef_enum_exists;
60         bool typedef_void_ptr_exists;
61         bool typedef_func_proto_exists;
62         bool typedef_arr_exists;
63
64         bool struct_matches;
65         bool union_matches;
66         bool enum_matches;
67         bool typedef_named_struct_matches;
68         bool typedef_anon_struct_matches;
69         bool typedef_struct_ptr_matches;
70         bool typedef_int_matches;
71         bool typedef_enum_matches;
72         bool typedef_void_ptr_matches;
73         bool typedef_func_proto_matches;
74         bool typedef_arr_matches;
75
76         int struct_sz;
77         int union_sz;
78         int enum_sz;
79         int typedef_named_struct_sz;
80         int typedef_anon_struct_sz;
81         int typedef_struct_ptr_sz;
82         int typedef_int_sz;
83         int typedef_enum_sz;
84         int typedef_void_ptr_sz;
85         int typedef_func_proto_sz;
86         int typedef_arr_sz;
87 };
88
89 SEC("raw_tracepoint/sys_enter")
90 int test_core_type_based(void *ctx)
91 {
92         /* Support for the BPF_TYPE_MATCHES argument to the
93          * __builtin_preserve_type_info builtin was added at some point during
94          * development of clang 15 and it's what we require for this test. Part of it
95          * could run with merely __builtin_preserve_type_info (which could be checked
96          * separately), but we have to find an upper bound.
97          */
98 #if __has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15
99         struct core_reloc_type_based_output *out = (void *)&data.out;
100
101         out->struct_exists = bpf_core_type_exists(struct a_struct);
102         out->union_exists = bpf_core_type_exists(union a_union);
103         out->enum_exists = bpf_core_type_exists(enum an_enum);
104         out->typedef_named_struct_exists = bpf_core_type_exists(named_struct_typedef);
105         out->typedef_anon_struct_exists = bpf_core_type_exists(anon_struct_typedef);
106         out->typedef_struct_ptr_exists = bpf_core_type_exists(struct_ptr_typedef);
107         out->typedef_int_exists = bpf_core_type_exists(int_typedef);
108         out->typedef_enum_exists = bpf_core_type_exists(enum_typedef);
109         out->typedef_void_ptr_exists = bpf_core_type_exists(void_ptr_typedef);
110         out->typedef_func_proto_exists = bpf_core_type_exists(func_proto_typedef);
111         out->typedef_arr_exists = bpf_core_type_exists(arr_typedef);
112
113         out->struct_matches = bpf_core_type_matches(struct a_struct);
114         out->union_matches = bpf_core_type_matches(union a_union);
115         out->enum_matches = bpf_core_type_matches(enum an_enum);
116         out->typedef_named_struct_matches = bpf_core_type_matches(named_struct_typedef);
117         out->typedef_anon_struct_matches = bpf_core_type_matches(anon_struct_typedef);
118         out->typedef_struct_ptr_matches = bpf_core_type_matches(struct_ptr_typedef);
119         out->typedef_int_matches = bpf_core_type_matches(int_typedef);
120         out->typedef_enum_matches = bpf_core_type_matches(enum_typedef);
121         out->typedef_void_ptr_matches = bpf_core_type_matches(void_ptr_typedef);
122         out->typedef_func_proto_matches = bpf_core_type_matches(func_proto_typedef);
123         out->typedef_arr_matches = bpf_core_type_matches(arr_typedef);
124
125         out->struct_sz = bpf_core_type_size(struct a_struct);
126         out->union_sz = bpf_core_type_size(union a_union);
127         out->enum_sz = bpf_core_type_size(enum an_enum);
128         out->typedef_named_struct_sz = bpf_core_type_size(named_struct_typedef);
129         out->typedef_anon_struct_sz = bpf_core_type_size(anon_struct_typedef);
130         out->typedef_struct_ptr_sz = bpf_core_type_size(struct_ptr_typedef);
131         out->typedef_int_sz = bpf_core_type_size(int_typedef);
132         out->typedef_enum_sz = bpf_core_type_size(enum_typedef);
133         out->typedef_void_ptr_sz = bpf_core_type_size(void_ptr_typedef);
134         out->typedef_func_proto_sz = bpf_core_type_size(func_proto_typedef);
135         out->typedef_arr_sz = bpf_core_type_size(arr_typedef);
136 #else
137         data.skip = true;
138 #endif
139         return 0;
140 }