diff options
Diffstat (limited to 'tools/testing/selftests/bpf/prog_tests/btf_write.c')
| -rw-r--r-- | tools/testing/selftests/bpf/prog_tests/btf_write.c | 159 | 
1 files changed, 144 insertions, 15 deletions
| diff --git a/tools/testing/selftests/bpf/prog_tests/btf_write.c b/tools/testing/selftests/bpf/prog_tests/btf_write.c index 76548eecce2c..b912eeb0b6b4 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_write.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_write.c @@ -4,19 +4,15 @@  #include <bpf/btf.h>  #include "btf_helpers.h" -void test_btf_write() { +static void gen_btf(struct btf *btf) +{  	const struct btf_var_secinfo *vi;  	const struct btf_type *t;  	const struct btf_member *m;  	const struct btf_enum *v;  	const struct btf_param *p; -	struct btf *btf;  	int id, err, str_off; -	btf = btf__new_empty(); -	if (!ASSERT_OK_PTR(btf, "new_empty")) -		return; -  	str_off = btf__find_str(btf, "int");  	ASSERT_EQ(str_off, -ENOENT, "int_str_missing_off"); @@ -281,26 +277,159 @@ void test_btf_write() {  		     "[17] DATASEC 'datasec1' size=12 vlen=1\n"  		     "\ttype_id=1 offset=4 size=8", "raw_dump"); -	/* TAG */ -	id = btf__add_tag(btf, "tag1", 16, -1); +	/* DECL_TAG */ +	id = btf__add_decl_tag(btf, "tag1", 16, -1);  	ASSERT_EQ(id, 18, "tag_id");  	t = btf__type_by_id(btf, 18);  	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag1", "tag_value"); -	ASSERT_EQ(btf_kind(t), BTF_KIND_TAG, "tag_kind"); +	ASSERT_EQ(btf_kind(t), BTF_KIND_DECL_TAG, "tag_kind");  	ASSERT_EQ(t->type, 16, "tag_type"); -	ASSERT_EQ(btf_tag(t)->component_idx, -1, "tag_component_idx"); +	ASSERT_EQ(btf_decl_tag(t)->component_idx, -1, "tag_component_idx");  	ASSERT_STREQ(btf_type_raw_dump(btf, 18), -		     "[18] TAG 'tag1' type_id=16 component_idx=-1", "raw_dump"); +		     "[18] DECL_TAG 'tag1' type_id=16 component_idx=-1", "raw_dump"); -	id = btf__add_tag(btf, "tag2", 14, 1); +	id = btf__add_decl_tag(btf, "tag2", 14, 1);  	ASSERT_EQ(id, 19, "tag_id");  	t = btf__type_by_id(btf, 19);  	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag2", "tag_value"); -	ASSERT_EQ(btf_kind(t), BTF_KIND_TAG, "tag_kind"); +	ASSERT_EQ(btf_kind(t), BTF_KIND_DECL_TAG, "tag_kind");  	ASSERT_EQ(t->type, 14, "tag_type"); -	ASSERT_EQ(btf_tag(t)->component_idx, 1, "tag_component_idx"); +	ASSERT_EQ(btf_decl_tag(t)->component_idx, 1, "tag_component_idx");  	ASSERT_STREQ(btf_type_raw_dump(btf, 19), -		     "[19] TAG 'tag2' type_id=14 component_idx=1", "raw_dump"); +		     "[19] DECL_TAG 'tag2' type_id=14 component_idx=1", "raw_dump"); +} + +static void test_btf_add() +{ +	struct btf *btf; + +	btf = btf__new_empty(); +	if (!ASSERT_OK_PTR(btf, "new_empty")) +		return; + +	gen_btf(btf); + +	VALIDATE_RAW_BTF( +		btf, +		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", +		"[2] PTR '(anon)' type_id=1", +		"[3] CONST '(anon)' type_id=5", +		"[4] VOLATILE '(anon)' type_id=3", +		"[5] RESTRICT '(anon)' type_id=4", +		"[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10", +		"[7] STRUCT 's1' size=8 vlen=2\n" +		"\t'f1' type_id=1 bits_offset=0\n" +		"\t'f2' type_id=1 bits_offset=32 bitfield_size=16", +		"[8] UNION 'u1' size=8 vlen=1\n" +		"\t'f1' type_id=1 bits_offset=0 bitfield_size=16", +		"[9] ENUM 'e1' size=4 vlen=2\n" +		"\t'v1' val=1\n" +		"\t'v2' val=2", +		"[10] FWD 'struct_fwd' fwd_kind=struct", +		"[11] FWD 'union_fwd' fwd_kind=union", +		"[12] ENUM 'enum_fwd' size=4 vlen=0", +		"[13] TYPEDEF 'typedef1' type_id=1", +		"[14] FUNC 'func1' type_id=15 linkage=global", +		"[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n" +		"\t'p1' type_id=1\n" +		"\t'p2' type_id=2", +		"[16] VAR 'var1' type_id=1, linkage=global-alloc", +		"[17] DATASEC 'datasec1' size=12 vlen=1\n" +		"\ttype_id=1 offset=4 size=8", +		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1", +		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1");  	btf__free(btf);  } + +static void test_btf_add_btf() +{ +	struct btf *btf1 = NULL, *btf2 = NULL; +	int id; + +	btf1 = btf__new_empty(); +	if (!ASSERT_OK_PTR(btf1, "btf1")) +		return; + +	btf2 = btf__new_empty(); +	if (!ASSERT_OK_PTR(btf2, "btf2")) +		goto cleanup; + +	gen_btf(btf1); +	gen_btf(btf2); + +	id = btf__add_btf(btf1, btf2); +	if (!ASSERT_EQ(id, 20, "id")) +		goto cleanup; + +	VALIDATE_RAW_BTF( +		btf1, +		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", +		"[2] PTR '(anon)' type_id=1", +		"[3] CONST '(anon)' type_id=5", +		"[4] VOLATILE '(anon)' type_id=3", +		"[5] RESTRICT '(anon)' type_id=4", +		"[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10", +		"[7] STRUCT 's1' size=8 vlen=2\n" +		"\t'f1' type_id=1 bits_offset=0\n" +		"\t'f2' type_id=1 bits_offset=32 bitfield_size=16", +		"[8] UNION 'u1' size=8 vlen=1\n" +		"\t'f1' type_id=1 bits_offset=0 bitfield_size=16", +		"[9] ENUM 'e1' size=4 vlen=2\n" +		"\t'v1' val=1\n" +		"\t'v2' val=2", +		"[10] FWD 'struct_fwd' fwd_kind=struct", +		"[11] FWD 'union_fwd' fwd_kind=union", +		"[12] ENUM 'enum_fwd' size=4 vlen=0", +		"[13] TYPEDEF 'typedef1' type_id=1", +		"[14] FUNC 'func1' type_id=15 linkage=global", +		"[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n" +		"\t'p1' type_id=1\n" +		"\t'p2' type_id=2", +		"[16] VAR 'var1' type_id=1, linkage=global-alloc", +		"[17] DATASEC 'datasec1' size=12 vlen=1\n" +		"\ttype_id=1 offset=4 size=8", +		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1", +		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1", + +		/* types appended from the second BTF */ +		"[20] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", +		"[21] PTR '(anon)' type_id=20", +		"[22] CONST '(anon)' type_id=24", +		"[23] VOLATILE '(anon)' type_id=22", +		"[24] RESTRICT '(anon)' type_id=23", +		"[25] ARRAY '(anon)' type_id=21 index_type_id=20 nr_elems=10", +		"[26] STRUCT 's1' size=8 vlen=2\n" +		"\t'f1' type_id=20 bits_offset=0\n" +		"\t'f2' type_id=20 bits_offset=32 bitfield_size=16", +		"[27] UNION 'u1' size=8 vlen=1\n" +		"\t'f1' type_id=20 bits_offset=0 bitfield_size=16", +		"[28] ENUM 'e1' size=4 vlen=2\n" +		"\t'v1' val=1\n" +		"\t'v2' val=2", +		"[29] FWD 'struct_fwd' fwd_kind=struct", +		"[30] FWD 'union_fwd' fwd_kind=union", +		"[31] ENUM 'enum_fwd' size=4 vlen=0", +		"[32] TYPEDEF 'typedef1' type_id=20", +		"[33] FUNC 'func1' type_id=34 linkage=global", +		"[34] FUNC_PROTO '(anon)' ret_type_id=20 vlen=2\n" +		"\t'p1' type_id=20\n" +		"\t'p2' type_id=21", +		"[35] VAR 'var1' type_id=20, linkage=global-alloc", +		"[36] DATASEC 'datasec1' size=12 vlen=1\n" +		"\ttype_id=20 offset=4 size=8", +		"[37] DECL_TAG 'tag1' type_id=35 component_idx=-1", +		"[38] DECL_TAG 'tag2' type_id=33 component_idx=1"); + +cleanup: +	btf__free(btf1); +	btf__free(btf2); +} + +void test_btf_write() +{ +	if (test__start_subtest("btf_add")) +		test_btf_add(); +	if (test__start_subtest("btf_add_btf")) +		test_btf_add_btf(); +} |