1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <test_progs.h>
4 #include <bpf/btf.h>
5 #include "btf_helpers.h"
6
test_btf_write()7 void test_btf_write() {
8 const struct btf_var_secinfo *vi;
9 const struct btf_type *t;
10 const struct btf_member *m;
11 const struct btf_enum *v;
12 const struct btf_param *p;
13 struct btf *btf;
14 int id, err, str_off;
15
16 btf = btf__new_empty();
17 if (!ASSERT_OK_PTR(btf, "new_empty"))
18 return;
19
20 str_off = btf__find_str(btf, "int");
21 ASSERT_EQ(str_off, -ENOENT, "int_str_missing_off");
22
23 str_off = btf__add_str(btf, "int");
24 ASSERT_EQ(str_off, 1, "int_str_off");
25
26 str_off = btf__find_str(btf, "int");
27 ASSERT_EQ(str_off, 1, "int_str_found_off");
28
29 /* BTF_KIND_INT */
30 id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
31 ASSERT_EQ(id, 1, "int_id");
32
33 t = btf__type_by_id(btf, 1);
34 /* should re-use previously added "int" string */
35 ASSERT_EQ(t->name_off, str_off, "int_name_off");
36 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "int", "int_name");
37 ASSERT_EQ(btf_kind(t), BTF_KIND_INT, "int_kind");
38 ASSERT_EQ(t->size, 4, "int_sz");
39 ASSERT_EQ(btf_int_encoding(t), BTF_INT_SIGNED, "int_enc");
40 ASSERT_EQ(btf_int_bits(t), 32, "int_bits");
41 ASSERT_STREQ(btf_type_raw_dump(btf, 1),
42 "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", "raw_dump");
43
44 /* invalid int size */
45 id = btf__add_int(btf, "bad sz int", 7, 0);
46 ASSERT_ERR(id, "int_bad_sz");
47 /* invalid encoding */
48 id = btf__add_int(btf, "bad enc int", 4, 123);
49 ASSERT_ERR(id, "int_bad_enc");
50 /* NULL name */
51 id = btf__add_int(btf, NULL, 4, 0);
52 ASSERT_ERR(id, "int_bad_null_name");
53 /* empty name */
54 id = btf__add_int(btf, "", 4, 0);
55 ASSERT_ERR(id, "int_bad_empty_name");
56
57 /* PTR/CONST/VOLATILE/RESTRICT */
58 id = btf__add_ptr(btf, 1);
59 ASSERT_EQ(id, 2, "ptr_id");
60 t = btf__type_by_id(btf, 2);
61 ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_kind");
62 ASSERT_EQ(t->type, 1, "ptr_type");
63 ASSERT_STREQ(btf_type_raw_dump(btf, 2),
64 "[2] PTR '(anon)' type_id=1", "raw_dump");
65
66 id = btf__add_const(btf, 5); /* points forward to restrict */
67 ASSERT_EQ(id, 3, "const_id");
68 t = btf__type_by_id(btf, 3);
69 ASSERT_EQ(btf_kind(t), BTF_KIND_CONST, "const_kind");
70 ASSERT_EQ(t->type, 5, "const_type");
71 ASSERT_STREQ(btf_type_raw_dump(btf, 3),
72 "[3] CONST '(anon)' type_id=5", "raw_dump");
73
74 id = btf__add_volatile(btf, 3);
75 ASSERT_EQ(id, 4, "volatile_id");
76 t = btf__type_by_id(btf, 4);
77 ASSERT_EQ(btf_kind(t), BTF_KIND_VOLATILE, "volatile_kind");
78 ASSERT_EQ(t->type, 3, "volatile_type");
79 ASSERT_STREQ(btf_type_raw_dump(btf, 4),
80 "[4] VOLATILE '(anon)' type_id=3", "raw_dump");
81
82 id = btf__add_restrict(btf, 4);
83 ASSERT_EQ(id, 5, "restrict_id");
84 t = btf__type_by_id(btf, 5);
85 ASSERT_EQ(btf_kind(t), BTF_KIND_RESTRICT, "restrict_kind");
86 ASSERT_EQ(t->type, 4, "restrict_type");
87 ASSERT_STREQ(btf_type_raw_dump(btf, 5),
88 "[5] RESTRICT '(anon)' type_id=4", "raw_dump");
89
90 /* ARRAY */
91 id = btf__add_array(btf, 1, 2, 10); /* int *[10] */
92 ASSERT_EQ(id, 6, "array_id");
93 t = btf__type_by_id(btf, 6);
94 ASSERT_EQ(btf_kind(t), BTF_KIND_ARRAY, "array_kind");
95 ASSERT_EQ(btf_array(t)->index_type, 1, "array_index_type");
96 ASSERT_EQ(btf_array(t)->type, 2, "array_elem_type");
97 ASSERT_EQ(btf_array(t)->nelems, 10, "array_nelems");
98 ASSERT_STREQ(btf_type_raw_dump(btf, 6),
99 "[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10", "raw_dump");
100
101 /* STRUCT */
102 err = btf__add_field(btf, "field", 1, 0, 0);
103 ASSERT_ERR(err, "no_struct_field");
104 id = btf__add_struct(btf, "s1", 8);
105 ASSERT_EQ(id, 7, "struct_id");
106 err = btf__add_field(btf, "f1", 1, 0, 0);
107 ASSERT_OK(err, "f1_res");
108 err = btf__add_field(btf, "f2", 1, 32, 16);
109 ASSERT_OK(err, "f2_res");
110
111 t = btf__type_by_id(btf, 7);
112 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "s1", "struct_name");
113 ASSERT_EQ(btf_kind(t), BTF_KIND_STRUCT, "struct_kind");
114 ASSERT_EQ(btf_vlen(t), 2, "struct_vlen");
115 ASSERT_EQ(btf_kflag(t), true, "struct_kflag");
116 ASSERT_EQ(t->size, 8, "struct_sz");
117 m = btf_members(t) + 0;
118 ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
119 ASSERT_EQ(m->type, 1, "f1_type");
120 ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
121 ASSERT_EQ(btf_member_bitfield_size(t, 0), 0, "f1_bit_sz");
122 m = btf_members(t) + 1;
123 ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f2", "f2_name");
124 ASSERT_EQ(m->type, 1, "f2_type");
125 ASSERT_EQ(btf_member_bit_offset(t, 1), 32, "f2_bit_off");
126 ASSERT_EQ(btf_member_bitfield_size(t, 1), 16, "f2_bit_sz");
127 ASSERT_STREQ(btf_type_raw_dump(btf, 7),
128 "[7] STRUCT 's1' size=8 vlen=2\n"
129 "\t'f1' type_id=1 bits_offset=0\n"
130 "\t'f2' type_id=1 bits_offset=32 bitfield_size=16", "raw_dump");
131
132 /* UNION */
133 id = btf__add_union(btf, "u1", 8);
134 ASSERT_EQ(id, 8, "union_id");
135
136 /* invalid, non-zero offset */
137 err = btf__add_field(btf, "field", 1, 1, 0);
138 ASSERT_ERR(err, "no_struct_field");
139
140 err = btf__add_field(btf, "f1", 1, 0, 16);
141 ASSERT_OK(err, "f1_res");
142
143 t = btf__type_by_id(btf, 8);
144 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "u1", "union_name");
145 ASSERT_EQ(btf_kind(t), BTF_KIND_UNION, "union_kind");
146 ASSERT_EQ(btf_vlen(t), 1, "union_vlen");
147 ASSERT_EQ(btf_kflag(t), true, "union_kflag");
148 ASSERT_EQ(t->size, 8, "union_sz");
149 m = btf_members(t) + 0;
150 ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
151 ASSERT_EQ(m->type, 1, "f1_type");
152 ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
153 ASSERT_EQ(btf_member_bitfield_size(t, 0), 16, "f1_bit_sz");
154 ASSERT_STREQ(btf_type_raw_dump(btf, 8),
155 "[8] UNION 'u1' size=8 vlen=1\n"
156 "\t'f1' type_id=1 bits_offset=0 bitfield_size=16", "raw_dump");
157
158 /* ENUM */
159 id = btf__add_enum(btf, "e1", 4);
160 ASSERT_EQ(id, 9, "enum_id");
161 err = btf__add_enum_value(btf, "v1", 1);
162 ASSERT_OK(err, "v1_res");
163 err = btf__add_enum_value(btf, "v2", 2);
164 ASSERT_OK(err, "v2_res");
165
166 t = btf__type_by_id(btf, 9);
167 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum_name");
168 ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_kind");
169 ASSERT_EQ(btf_vlen(t), 2, "enum_vlen");
170 ASSERT_EQ(t->size, 4, "enum_sz");
171 v = btf_enum(t) + 0;
172 ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v1", "v1_name");
173 ASSERT_EQ(v->val, 1, "v1_val");
174 v = btf_enum(t) + 1;
175 ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v2", "v2_name");
176 ASSERT_EQ(v->val, 2, "v2_val");
177 ASSERT_STREQ(btf_type_raw_dump(btf, 9),
178 "[9] ENUM 'e1' size=4 vlen=2\n"
179 "\t'v1' val=1\n"
180 "\t'v2' val=2", "raw_dump");
181
182 /* FWDs */
183 id = btf__add_fwd(btf, "struct_fwd", BTF_FWD_STRUCT);
184 ASSERT_EQ(id, 10, "struct_fwd_id");
185 t = btf__type_by_id(btf, 10);
186 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "struct_fwd", "fwd_name");
187 ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
188 ASSERT_EQ(btf_kflag(t), 0, "fwd_kflag");
189 ASSERT_STREQ(btf_type_raw_dump(btf, 10),
190 "[10] FWD 'struct_fwd' fwd_kind=struct", "raw_dump");
191
192 id = btf__add_fwd(btf, "union_fwd", BTF_FWD_UNION);
193 ASSERT_EQ(id, 11, "union_fwd_id");
194 t = btf__type_by_id(btf, 11);
195 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "union_fwd", "fwd_name");
196 ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
197 ASSERT_EQ(btf_kflag(t), 1, "fwd_kflag");
198 ASSERT_STREQ(btf_type_raw_dump(btf, 11),
199 "[11] FWD 'union_fwd' fwd_kind=union", "raw_dump");
200
201 id = btf__add_fwd(btf, "enum_fwd", BTF_FWD_ENUM);
202 ASSERT_EQ(id, 12, "enum_fwd_id");
203 t = btf__type_by_id(btf, 12);
204 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "enum_fwd", "fwd_name");
205 ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_fwd_kind");
206 ASSERT_EQ(btf_vlen(t), 0, "enum_fwd_kind");
207 ASSERT_EQ(t->size, 4, "enum_fwd_sz");
208 ASSERT_STREQ(btf_type_raw_dump(btf, 12),
209 "[12] ENUM 'enum_fwd' size=4 vlen=0", "raw_dump");
210
211 /* TYPEDEF */
212 id = btf__add_typedef(btf, "typedef1", 1);
213 ASSERT_EQ(id, 13, "typedef_fwd_id");
214 t = btf__type_by_id(btf, 13);
215 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "typedef1", "typedef_name");
216 ASSERT_EQ(btf_kind(t), BTF_KIND_TYPEDEF, "typedef_kind");
217 ASSERT_EQ(t->type, 1, "typedef_type");
218 ASSERT_STREQ(btf_type_raw_dump(btf, 13),
219 "[13] TYPEDEF 'typedef1' type_id=1", "raw_dump");
220
221 /* FUNC & FUNC_PROTO */
222 id = btf__add_func(btf, "func1", BTF_FUNC_GLOBAL, 15);
223 ASSERT_EQ(id, 14, "func_id");
224 t = btf__type_by_id(btf, 14);
225 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "func1", "func_name");
226 ASSERT_EQ(t->type, 15, "func_type");
227 ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC, "func_kind");
228 ASSERT_EQ(btf_vlen(t), BTF_FUNC_GLOBAL, "func_vlen");
229 ASSERT_STREQ(btf_type_raw_dump(btf, 14),
230 "[14] FUNC 'func1' type_id=15 linkage=global", "raw_dump");
231
232 id = btf__add_func_proto(btf, 1);
233 ASSERT_EQ(id, 15, "func_proto_id");
234 err = btf__add_func_param(btf, "p1", 1);
235 ASSERT_OK(err, "p1_res");
236 err = btf__add_func_param(btf, "p2", 2);
237 ASSERT_OK(err, "p2_res");
238
239 t = btf__type_by_id(btf, 15);
240 ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC_PROTO, "func_proto_kind");
241 ASSERT_EQ(btf_vlen(t), 2, "func_proto_vlen");
242 ASSERT_EQ(t->type, 1, "func_proto_ret_type");
243 p = btf_params(t) + 0;
244 ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p1", "p1_name");
245 ASSERT_EQ(p->type, 1, "p1_type");
246 p = btf_params(t) + 1;
247 ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p2", "p2_name");
248 ASSERT_EQ(p->type, 2, "p2_type");
249 ASSERT_STREQ(btf_type_raw_dump(btf, 15),
250 "[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
251 "\t'p1' type_id=1\n"
252 "\t'p2' type_id=2", "raw_dump");
253
254 /* VAR */
255 id = btf__add_var(btf, "var1", BTF_VAR_GLOBAL_ALLOCATED, 1);
256 ASSERT_EQ(id, 16, "var_id");
257 t = btf__type_by_id(btf, 16);
258 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "var1", "var_name");
259 ASSERT_EQ(btf_kind(t), BTF_KIND_VAR, "var_kind");
260 ASSERT_EQ(t->type, 1, "var_type");
261 ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_type");
262 ASSERT_STREQ(btf_type_raw_dump(btf, 16),
263 "[16] VAR 'var1' type_id=1, linkage=global-alloc", "raw_dump");
264
265 /* DATASECT */
266 id = btf__add_datasec(btf, "datasec1", 12);
267 ASSERT_EQ(id, 17, "datasec_id");
268 err = btf__add_datasec_var_info(btf, 1, 4, 8);
269 ASSERT_OK(err, "v1_res");
270
271 t = btf__type_by_id(btf, 17);
272 ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "datasec1", "datasec_name");
273 ASSERT_EQ(t->size, 12, "datasec_sz");
274 ASSERT_EQ(btf_kind(t), BTF_KIND_DATASEC, "datasec_kind");
275 ASSERT_EQ(btf_vlen(t), 1, "datasec_vlen");
276 vi = btf_var_secinfos(t) + 0;
277 ASSERT_EQ(vi->type, 1, "v1_type");
278 ASSERT_EQ(vi->offset, 4, "v1_off");
279 ASSERT_EQ(vi->size, 8, "v1_sz");
280 ASSERT_STREQ(btf_type_raw_dump(btf, 17),
281 "[17] DATASEC 'datasec1' size=12 vlen=1\n"
282 "\ttype_id=1 offset=4 size=8", "raw_dump");
283
284 btf__free(btf);
285 }
286