1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020 Facebook */ 3 #include <test_progs.h> 4 #include <bpf/btf.h> 5 6 static char *dump_buf; 7 static size_t dump_buf_sz; 8 static FILE *dump_buf_file; 9 10 static void btf_dump_printf(void *ctx, const char *fmt, va_list args) 11 { 12 vfprintf(ctx, fmt, args); 13 } 14 15 /* Write raw BTF to file, return number of bytes written or negative errno */ 16 static ssize_t btf_raw_write(struct btf *btf, char *file) 17 { 18 ssize_t written = 0; 19 const void *data; 20 __u32 size = 0; 21 int fd, ret; 22 23 fd = mkstemp(file); 24 if (!ASSERT_GE(fd, 0, "create_file")) 25 return -errno; 26 27 data = btf__raw_data(btf, &size); 28 if (!ASSERT_OK_PTR(data, "btf__raw_data")) { 29 close(fd); 30 return -EINVAL; 31 } 32 while (written < size) { 33 ret = write(fd, data + written, size - written); 34 if (!ASSERT_GE(ret, 0, "write succeeded")) { 35 close(fd); 36 return -errno; 37 } 38 written += ret; 39 } 40 close(fd); 41 return written; 42 } 43 44 static void __test_btf_split(bool multi) 45 { 46 char multisplit_btf_file[] = "/tmp/test_btf_multisplit.XXXXXX"; 47 char split_btf_file[] = "/tmp/test_btf_split.XXXXXX"; 48 char base_btf_file[] = "/tmp/test_btf_base.XXXXXX"; 49 ssize_t multisplit_btf_sz = 0, split_btf_sz = 0, base_btf_sz = 0; 50 struct btf_dump *d = NULL; 51 const struct btf_type *t, *ot; 52 struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL; 53 struct btf *btf4 = NULL, *btf5 = NULL, *btf6 = NULL; 54 int str_off, i, err; 55 56 btf1 = btf__new_empty(); 57 if (!ASSERT_OK_PTR(btf1, "empty_main_btf")) 58 return; 59 60 btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */ 61 62 btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */ 63 btf__add_ptr(btf1, 1); /* [2] ptr to int */ 64 65 btf__add_struct(btf1, "s1", 4); /* [3] struct s1 { */ 66 btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */ 67 /* } */ 68 69 btf2 = btf__new_empty_split(btf1); 70 if (!ASSERT_OK_PTR(btf2, "empty_split_btf")) 71 goto cleanup; 72 73 /* pointer size should be "inherited" from main BTF */ 74 ASSERT_EQ(btf__pointer_size(btf2), 8, "inherit_ptr_sz"); 75 76 str_off = btf__find_str(btf2, "int"); 77 ASSERT_NEQ(str_off, -ENOENT, "str_int_missing"); 78 79 t = btf__type_by_id(btf2, 1); 80 if (!ASSERT_OK_PTR(t, "int_type")) 81 goto cleanup; 82 ASSERT_EQ(btf_is_int(t), true, "int_kind"); 83 ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "int", "int_name"); 84 85 btf__add_struct(btf2, "s2", 16); /* [4] struct s2 { */ 86 btf__add_field(btf2, "f1", 3, 0, 0); /* struct s1 f1; */ 87 btf__add_field(btf2, "f2", 1, 32, 0); /* int f2; */ 88 btf__add_field(btf2, "f3", 2, 64, 0); /* int *f3; */ 89 /* } */ 90 91 t = btf__type_by_id(btf1, 4); 92 ASSERT_NULL(t, "split_type_in_main"); 93 94 t = btf__type_by_id(btf2, 4); 95 if (!ASSERT_OK_PTR(t, "split_struct_type")) 96 goto cleanup; 97 ASSERT_EQ(btf_is_struct(t), true, "split_struct_kind"); 98 ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen"); 99 ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name"); 100 101 if (multi) { 102 btf3 = btf__new_empty_split(btf2); 103 if (!ASSERT_OK_PTR(btf3, "multi_split_btf")) 104 goto cleanup; 105 } else { 106 btf3 = btf2; 107 } 108 109 btf__add_union(btf3, "u1", 16); /* [5] union u1 { */ 110 btf__add_field(btf3, "f1", 4, 0, 0); /* struct s2 f1; */ 111 btf__add_field(btf3, "uf2", 1, 0, 0); /* int f2; */ 112 /* } */ 113 114 if (multi) { 115 t = btf__type_by_id(btf2, 5); 116 ASSERT_NULL(t, "multisplit_type_in_first_split"); 117 } 118 119 t = btf__type_by_id(btf3, 5); 120 if (!ASSERT_OK_PTR(t, "split_union_type")) 121 goto cleanup; 122 ASSERT_EQ(btf_is_union(t), true, "split_union_kind"); 123 ASSERT_EQ(btf_vlen(t), 2, "split_union_vlen"); 124 ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "u1", "split_union_name"); 125 ASSERT_EQ(btf__type_cnt(btf3), 6, "split_type_cnt"); 126 127 t = btf__type_by_id(btf3, 1); 128 if (!ASSERT_OK_PTR(t, "split_base_type")) 129 goto cleanup; 130 ASSERT_EQ(btf_is_int(t), true, "split_base_int"); 131 ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "int", "split_base_type_name"); 132 133 /* BTF-to-C dump of split BTF */ 134 dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz); 135 if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream")) 136 return; 137 d = btf_dump__new(btf3, btf_dump_printf, dump_buf_file, NULL); 138 if (!ASSERT_OK_PTR(d, "btf_dump__new")) 139 goto cleanup; 140 for (i = 1; i < btf__type_cnt(btf3); i++) { 141 err = btf_dump__dump_type(d, i); 142 ASSERT_OK(err, "dump_type_ok"); 143 } 144 fflush(dump_buf_file); 145 dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */ 146 ASSERT_STREQ(dump_buf, 147 "struct s1 {\n" 148 " int f1;\n" 149 "};\n\n" 150 "struct s2 {\n" 151 " struct s1 f1;\n" 152 " int f2;\n" 153 " int *f3;\n" 154 "};\n\n" 155 "union u1 {\n" 156 " struct s2 f1;\n" 157 " int uf2;\n" 158 "};\n\n", "c_dump"); 159 160 /* write base, split BTFs to files and ensure parsing succeeds */ 161 base_btf_sz = btf_raw_write(btf1, base_btf_file); 162 if (base_btf_sz < 0) 163 goto cleanup; 164 split_btf_sz = btf_raw_write(btf2, split_btf_file); 165 if (split_btf_sz < 0) 166 goto cleanup; 167 btf4 = btf__parse(base_btf_file, NULL); 168 if (!ASSERT_OK_PTR(btf4, "parse_base")) 169 goto cleanup; 170 btf5 = btf__parse_split(split_btf_file, btf4); 171 if (!ASSERT_OK_PTR(btf5, "parse_split")) 172 goto cleanup; 173 if (multi) { 174 multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file); 175 if (multisplit_btf_sz < 0) 176 goto cleanup; 177 btf6 = btf__parse_split(multisplit_btf_file, btf5); 178 if (!ASSERT_OK_PTR(btf6, "parse_multisplit")) 179 goto cleanup; 180 } else { 181 btf6 = btf5; 182 } 183 184 if (!ASSERT_EQ(btf__type_cnt(btf3), btf__type_cnt(btf6), "cmp_type_cnt")) 185 goto cleanup; 186 187 /* compare parsed to original BTF */ 188 for (i = 1; i < btf__type_cnt(btf6); i++) { 189 t = btf__type_by_id(btf6, i); 190 if (!ASSERT_OK_PTR(t, "type_in_parsed_btf")) 191 goto cleanup; 192 ot = btf__type_by_id(btf3, i); 193 if (!ASSERT_OK_PTR(ot, "type_in_orig_btf")) 194 goto cleanup; 195 if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf")) 196 goto cleanup; 197 } 198 199 cleanup: 200 if (dump_buf_file) 201 fclose(dump_buf_file); 202 free(dump_buf); 203 btf_dump__free(d); 204 btf__free(btf1); 205 btf__free(btf2); 206 if (btf2 != btf3) 207 btf__free(btf3); 208 btf__free(btf4); 209 btf__free(btf5); 210 if (btf5 != btf6) 211 btf__free(btf6); 212 if (base_btf_sz > 0) 213 unlink(base_btf_file); 214 if (split_btf_sz > 0) 215 unlink(split_btf_file); 216 if (multisplit_btf_sz > 0) 217 unlink(multisplit_btf_file); 218 } 219 220 void test_btf_split(void) 221 { 222 if (test__start_subtest("single_split")) 223 __test_btf_split(false); 224 if (test__start_subtest("multi_split")) 225 __test_btf_split(true); 226 } 227