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