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
btf_dump_printf(void * ctx,const char * fmt,va_list args)10 static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
11 {
12 vfprintf(ctx, fmt, args);
13 }
14
__test_btf_split(bool multi)15 static void __test_btf_split(bool multi)
16 {
17 struct btf_dump *d = NULL;
18 const struct btf_type *t;
19 struct btf *btf1, *btf2, *btf3 = NULL;
20 int str_off, i, err;
21
22 btf1 = btf__new_empty();
23 if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
24 return;
25
26 btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */
27
28 btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
29 btf__add_ptr(btf1, 1); /* [2] ptr to int */
30
31 btf__add_struct(btf1, "s1", 4); /* [3] struct s1 { */
32 btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */
33 /* } */
34
35 btf2 = btf__new_empty_split(btf1);
36 if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
37 goto cleanup;
38
39 /* pointer size should be "inherited" from main BTF */
40 ASSERT_EQ(btf__pointer_size(btf2), 8, "inherit_ptr_sz");
41
42 str_off = btf__find_str(btf2, "int");
43 ASSERT_NEQ(str_off, -ENOENT, "str_int_missing");
44
45 t = btf__type_by_id(btf2, 1);
46 if (!ASSERT_OK_PTR(t, "int_type"))
47 goto cleanup;
48 ASSERT_EQ(btf_is_int(t), true, "int_kind");
49 ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "int", "int_name");
50
51 btf__add_struct(btf2, "s2", 16); /* [4] struct s2 { */
52 btf__add_field(btf2, "f1", 3, 0, 0); /* struct s1 f1; */
53 btf__add_field(btf2, "f2", 1, 32, 0); /* int f2; */
54 btf__add_field(btf2, "f3", 2, 64, 0); /* int *f3; */
55 /* } */
56
57 t = btf__type_by_id(btf1, 4);
58 ASSERT_NULL(t, "split_type_in_main");
59
60 t = btf__type_by_id(btf2, 4);
61 if (!ASSERT_OK_PTR(t, "split_struct_type"))
62 goto cleanup;
63 ASSERT_EQ(btf_is_struct(t), true, "split_struct_kind");
64 ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen");
65 ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name");
66
67 if (multi) {
68 btf3 = btf__new_empty_split(btf2);
69 if (!ASSERT_OK_PTR(btf3, "multi_split_btf"))
70 goto cleanup;
71 } else {
72 btf3 = btf2;
73 }
74
75 btf__add_union(btf3, "u1", 16); /* [5] union u1 { */
76 btf__add_field(btf3, "f1", 4, 0, 0); /* struct s2 f1; */
77 btf__add_field(btf3, "uf2", 1, 0, 0); /* int f2; */
78 /* } */
79
80 if (multi) {
81 t = btf__type_by_id(btf2, 5);
82 ASSERT_NULL(t, "multisplit_type_in_first_split");
83 }
84
85 t = btf__type_by_id(btf3, 5);
86 if (!ASSERT_OK_PTR(t, "split_union_type"))
87 goto cleanup;
88 ASSERT_EQ(btf_is_union(t), true, "split_union_kind");
89 ASSERT_EQ(btf_vlen(t), 2, "split_union_vlen");
90 ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "u1", "split_union_name");
91 ASSERT_EQ(btf__type_cnt(btf3), 6, "split_type_cnt");
92
93 t = btf__type_by_id(btf3, 1);
94 if (!ASSERT_OK_PTR(t, "split_base_type"))
95 goto cleanup;
96 ASSERT_EQ(btf_is_int(t), true, "split_base_int");
97 ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "int", "split_base_type_name");
98
99 /* BTF-to-C dump of split BTF */
100 dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
101 if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
102 return;
103 d = btf_dump__new(btf3, btf_dump_printf, dump_buf_file, NULL);
104 if (!ASSERT_OK_PTR(d, "btf_dump__new"))
105 goto cleanup;
106 for (i = 1; i < btf__type_cnt(btf3); i++) {
107 err = btf_dump__dump_type(d, i);
108 ASSERT_OK(err, "dump_type_ok");
109 }
110 fflush(dump_buf_file);
111 dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */
112 ASSERT_STREQ(dump_buf,
113 "struct s1 {\n"
114 " int f1;\n"
115 "};\n\n"
116 "struct s2 {\n"
117 " struct s1 f1;\n"
118 " int f2;\n"
119 " int *f3;\n"
120 "};\n\n"
121 "union u1 {\n"
122 " struct s2 f1;\n"
123 " int uf2;\n"
124 "};\n\n", "c_dump");
125
126 cleanup:
127 if (dump_buf_file)
128 fclose(dump_buf_file);
129 free(dump_buf);
130 btf_dump__free(d);
131 btf__free(btf1);
132 btf__free(btf2);
133 if (btf2 != btf3)
134 btf__free(btf3);
135 }
136
test_btf_split(void)137 void test_btf_split(void)
138 {
139 if (test__start_subtest("single_split"))
140 __test_btf_split(false);
141 if (test__start_subtest("multi_split"))
142 __test_btf_split(true);
143 }
144