1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_core_read.h>
10 char _license[] SEC("license") = "GPL";
27 typedef struct a_struct named_struct_typedef;
29 typedef struct { int x, y, z; } anon_struct_typedef;
33 } *struct_ptr_typedef;
41 typedef int int_typedef;
43 typedef enum { TYPEDEF_ENUM_VAL1, TYPEDEF_ENUM_VAL2 } enum_typedef;
45 typedef void *void_ptr_typedef;
47 typedef int (*func_proto_typedef)(long);
49 typedef char arr_typedef[20];
51 struct core_reloc_type_based_output {
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;
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;
79 int typedef_named_struct_sz;
80 int typedef_anon_struct_sz;
81 int typedef_struct_ptr_sz;
84 int typedef_void_ptr_sz;
85 int typedef_func_proto_sz;
89 SEC("raw_tracepoint/sys_enter")
90 int test_core_type_based(void *ctx)
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.
98 #if __has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15
99 struct core_reloc_type_based_output *out = (void *)&data.out;
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);
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);
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);