1197389daSAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2197389daSAndrii Nakryiko /* Copyright (c) 2020 Facebook */
3197389daSAndrii Nakryiko #include <test_progs.h>
4197389daSAndrii Nakryiko #include <bpf/btf.h>
5197389daSAndrii Nakryiko
6197389daSAndrii Nakryiko static char *dump_buf;
7197389daSAndrii Nakryiko static size_t dump_buf_sz;
8197389daSAndrii Nakryiko static FILE *dump_buf_file;
9197389daSAndrii Nakryiko
btf_dump_printf(void * ctx,const char * fmt,va_list args)10197389daSAndrii Nakryiko static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
11197389daSAndrii Nakryiko {
12197389daSAndrii Nakryiko vfprintf(ctx, fmt, args);
13197389daSAndrii Nakryiko }
14197389daSAndrii Nakryiko
test_btf_split()15197389daSAndrii Nakryiko void test_btf_split() {
16197389daSAndrii Nakryiko struct btf_dump *d = NULL;
17197389daSAndrii Nakryiko const struct btf_type *t;
18197389daSAndrii Nakryiko struct btf *btf1, *btf2;
19197389daSAndrii Nakryiko int str_off, i, err;
20197389daSAndrii Nakryiko
21197389daSAndrii Nakryiko btf1 = btf__new_empty();
22197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
23197389daSAndrii Nakryiko return;
24197389daSAndrii Nakryiko
25197389daSAndrii Nakryiko btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */
26197389daSAndrii Nakryiko
27197389daSAndrii Nakryiko btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
28197389daSAndrii Nakryiko btf__add_ptr(btf1, 1); /* [2] ptr to int */
29197389daSAndrii Nakryiko
30197389daSAndrii Nakryiko btf__add_struct(btf1, "s1", 4); /* [3] struct s1 { */
31197389daSAndrii Nakryiko btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */
32197389daSAndrii Nakryiko /* } */
33197389daSAndrii Nakryiko
34197389daSAndrii Nakryiko btf2 = btf__new_empty_split(btf1);
35197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
36197389daSAndrii Nakryiko goto cleanup;
37197389daSAndrii Nakryiko
38197389daSAndrii Nakryiko /* pointer size should be "inherited" from main BTF */
39197389daSAndrii Nakryiko ASSERT_EQ(btf__pointer_size(btf2), 8, "inherit_ptr_sz");
40197389daSAndrii Nakryiko
41197389daSAndrii Nakryiko str_off = btf__find_str(btf2, "int");
42197389daSAndrii Nakryiko ASSERT_NEQ(str_off, -ENOENT, "str_int_missing");
43197389daSAndrii Nakryiko
44197389daSAndrii Nakryiko t = btf__type_by_id(btf2, 1);
45197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(t, "int_type"))
46197389daSAndrii Nakryiko goto cleanup;
47197389daSAndrii Nakryiko ASSERT_EQ(btf_is_int(t), true, "int_kind");
48197389daSAndrii Nakryiko ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "int", "int_name");
49197389daSAndrii Nakryiko
50197389daSAndrii Nakryiko btf__add_struct(btf2, "s2", 16); /* [4] struct s2 { */
51197389daSAndrii Nakryiko btf__add_field(btf2, "f1", 3, 0, 0); /* struct s1 f1; */
52197389daSAndrii Nakryiko btf__add_field(btf2, "f2", 1, 32, 0); /* int f2; */
53197389daSAndrii Nakryiko btf__add_field(btf2, "f3", 2, 64, 0); /* int *f3; */
54197389daSAndrii Nakryiko /* } */
55197389daSAndrii Nakryiko
56197389daSAndrii Nakryiko t = btf__type_by_id(btf1, 4);
57197389daSAndrii Nakryiko ASSERT_NULL(t, "split_type_in_main");
58197389daSAndrii Nakryiko
59197389daSAndrii Nakryiko t = btf__type_by_id(btf2, 4);
60197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(t, "split_struct_type"))
61197389daSAndrii Nakryiko goto cleanup;
62197389daSAndrii Nakryiko ASSERT_EQ(btf_is_struct(t), true, "split_struct_kind");
63197389daSAndrii Nakryiko ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen");
64197389daSAndrii Nakryiko ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name");
65197389daSAndrii Nakryiko
66197389daSAndrii Nakryiko /* BTF-to-C dump of split BTF */
67197389daSAndrii Nakryiko dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
68197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
69197389daSAndrii Nakryiko return;
70*60ba87bbSAndrii Nakryiko d = btf_dump__new(btf2, btf_dump_printf, dump_buf_file, NULL);
71197389daSAndrii Nakryiko if (!ASSERT_OK_PTR(d, "btf_dump__new"))
72197389daSAndrii Nakryiko goto cleanup;
73487ef148SHengqi Chen for (i = 1; i < btf__type_cnt(btf2); i++) {
74197389daSAndrii Nakryiko err = btf_dump__dump_type(d, i);
75197389daSAndrii Nakryiko ASSERT_OK(err, "dump_type_ok");
76197389daSAndrii Nakryiko }
77197389daSAndrii Nakryiko fflush(dump_buf_file);
78197389daSAndrii Nakryiko dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */
79197389daSAndrii Nakryiko ASSERT_STREQ(dump_buf,
80197389daSAndrii Nakryiko "struct s1 {\n"
81197389daSAndrii Nakryiko " int f1;\n"
82197389daSAndrii Nakryiko "};\n"
83197389daSAndrii Nakryiko "\n"
84197389daSAndrii Nakryiko "struct s2 {\n"
85197389daSAndrii Nakryiko " struct s1 f1;\n"
86197389daSAndrii Nakryiko " int f2;\n"
87197389daSAndrii Nakryiko " int *f3;\n"
88197389daSAndrii Nakryiko "};\n\n", "c_dump");
89197389daSAndrii Nakryiko
90197389daSAndrii Nakryiko cleanup:
91197389daSAndrii Nakryiko if (dump_buf_file)
92197389daSAndrii Nakryiko fclose(dump_buf_file);
93197389daSAndrii Nakryiko free(dump_buf);
94197389daSAndrii Nakryiko btf_dump__free(d);
95197389daSAndrii Nakryiko btf__free(btf1);
96197389daSAndrii Nakryiko btf__free(btf2);
97197389daSAndrii Nakryiko }
98