Skip to content

Commit 35baba7

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
selftests/bpf: Add BTF_KIND_TAG unit tests
Test good and bad variants of BTF_KIND_TAG encoding. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3df3bd6 commit 35baba7

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed

tools/testing/selftests/bpf/prog_tests/btf.c

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,6 +3661,249 @@ static struct btf_raw_test raw_tests[] = {
36613661
.err_str = "Invalid type_size",
36623662
},
36633663

3664+
{
3665+
.descr = "tag test #1, struct/member, well-formed",
3666+
.raw_types = {
3667+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3668+
BTF_STRUCT_ENC(0, 2, 8), /* [2] */
3669+
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3670+
BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3671+
BTF_TAG_ENC(NAME_TBD, 2, -1),
3672+
BTF_TAG_ENC(NAME_TBD, 2, 0),
3673+
BTF_TAG_ENC(NAME_TBD, 2, 1),
3674+
BTF_END_RAW,
3675+
},
3676+
BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"),
3677+
.map_type = BPF_MAP_TYPE_ARRAY,
3678+
.map_name = "tag_type_check_btf",
3679+
.key_size = sizeof(int),
3680+
.value_size = 8,
3681+
.key_type_id = 1,
3682+
.value_type_id = 2,
3683+
.max_entries = 1,
3684+
},
3685+
{
3686+
.descr = "tag test #2, union/member, well-formed",
3687+
.raw_types = {
3688+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3689+
BTF_UNION_ENC(NAME_TBD, 2, 4), /* [2] */
3690+
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3691+
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3692+
BTF_TAG_ENC(NAME_TBD, 2, -1),
3693+
BTF_TAG_ENC(NAME_TBD, 2, 0),
3694+
BTF_TAG_ENC(NAME_TBD, 2, 1),
3695+
BTF_END_RAW,
3696+
},
3697+
BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
3698+
.map_type = BPF_MAP_TYPE_ARRAY,
3699+
.map_name = "tag_type_check_btf",
3700+
.key_size = sizeof(int),
3701+
.value_size = 4,
3702+
.key_type_id = 1,
3703+
.value_type_id = 2,
3704+
.max_entries = 1,
3705+
},
3706+
{
3707+
.descr = "tag test #3, variable, well-formed",
3708+
.raw_types = {
3709+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3710+
BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
3711+
BTF_VAR_ENC(NAME_TBD, 1, 1), /* [3] */
3712+
BTF_TAG_ENC(NAME_TBD, 2, -1),
3713+
BTF_TAG_ENC(NAME_TBD, 3, -1),
3714+
BTF_END_RAW,
3715+
},
3716+
BTF_STR_SEC("\0local\0global\0tag1\0tag2"),
3717+
.map_type = BPF_MAP_TYPE_ARRAY,
3718+
.map_name = "tag_type_check_btf",
3719+
.key_size = sizeof(int),
3720+
.value_size = 4,
3721+
.key_type_id = 1,
3722+
.value_type_id = 1,
3723+
.max_entries = 1,
3724+
},
3725+
{
3726+
.descr = "tag test #4, func/parameter, well-formed",
3727+
.raw_types = {
3728+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3729+
BTF_FUNC_PROTO_ENC(0, 2), /* [2] */
3730+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3731+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3732+
BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
3733+
BTF_TAG_ENC(NAME_TBD, 3, -1),
3734+
BTF_TAG_ENC(NAME_TBD, 3, 0),
3735+
BTF_TAG_ENC(NAME_TBD, 3, 1),
3736+
BTF_END_RAW,
3737+
},
3738+
BTF_STR_SEC("\0arg1\0arg2\0f\0tag1\0tag2\0tag3"),
3739+
.map_type = BPF_MAP_TYPE_ARRAY,
3740+
.map_name = "tag_type_check_btf",
3741+
.key_size = sizeof(int),
3742+
.value_size = 4,
3743+
.key_type_id = 1,
3744+
.value_type_id = 1,
3745+
.max_entries = 1,
3746+
},
3747+
{
3748+
.descr = "tag test #5, invalid value",
3749+
.raw_types = {
3750+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3751+
BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
3752+
BTF_TAG_ENC(0, 2, -1),
3753+
BTF_END_RAW,
3754+
},
3755+
BTF_STR_SEC("\0local\0tag"),
3756+
.map_type = BPF_MAP_TYPE_ARRAY,
3757+
.map_name = "tag_type_check_btf",
3758+
.key_size = sizeof(int),
3759+
.value_size = 4,
3760+
.key_type_id = 1,
3761+
.value_type_id = 1,
3762+
.max_entries = 1,
3763+
.btf_load_err = true,
3764+
.err_str = "Invalid value",
3765+
},
3766+
{
3767+
.descr = "tag test #6, invalid target type",
3768+
.raw_types = {
3769+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3770+
BTF_TAG_ENC(NAME_TBD, 1, -1),
3771+
BTF_END_RAW,
3772+
},
3773+
BTF_STR_SEC("\0tag1"),
3774+
.map_type = BPF_MAP_TYPE_ARRAY,
3775+
.map_name = "tag_type_check_btf",
3776+
.key_size = sizeof(int),
3777+
.value_size = 4,
3778+
.key_type_id = 1,
3779+
.value_type_id = 1,
3780+
.max_entries = 1,
3781+
.btf_load_err = true,
3782+
.err_str = "Invalid type",
3783+
},
3784+
{
3785+
.descr = "tag test #7, invalid vlen",
3786+
.raw_types = {
3787+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3788+
BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
3789+
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 0, 1), 2), (0),
3790+
BTF_END_RAW,
3791+
},
3792+
BTF_STR_SEC("\0local\0tag1"),
3793+
.map_type = BPF_MAP_TYPE_ARRAY,
3794+
.map_name = "tag_type_check_btf",
3795+
.key_size = sizeof(int),
3796+
.value_size = 4,
3797+
.key_type_id = 1,
3798+
.value_type_id = 1,
3799+
.max_entries = 1,
3800+
.btf_load_err = true,
3801+
.err_str = "vlen != 0",
3802+
},
3803+
{
3804+
.descr = "tag test #8, invalid kflag",
3805+
.raw_types = {
3806+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3807+
BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
3808+
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 0), 2), (-1),
3809+
BTF_END_RAW,
3810+
},
3811+
BTF_STR_SEC("\0local\0tag1"),
3812+
.map_type = BPF_MAP_TYPE_ARRAY,
3813+
.map_name = "tag_type_check_btf",
3814+
.key_size = sizeof(int),
3815+
.value_size = 4,
3816+
.key_type_id = 1,
3817+
.value_type_id = 1,
3818+
.max_entries = 1,
3819+
.btf_load_err = true,
3820+
.err_str = "Invalid btf_info kind_flag",
3821+
},
3822+
{
3823+
.descr = "tag test #9, var, invalid component_idx",
3824+
.raw_types = {
3825+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3826+
BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
3827+
BTF_TAG_ENC(NAME_TBD, 2, 0),
3828+
BTF_END_RAW,
3829+
},
3830+
BTF_STR_SEC("\0local\0tag"),
3831+
.map_type = BPF_MAP_TYPE_ARRAY,
3832+
.map_name = "tag_type_check_btf",
3833+
.key_size = sizeof(int),
3834+
.value_size = 4,
3835+
.key_type_id = 1,
3836+
.value_type_id = 1,
3837+
.max_entries = 1,
3838+
.btf_load_err = true,
3839+
.err_str = "Invalid component_idx",
3840+
},
3841+
{
3842+
.descr = "tag test #10, struct member, invalid component_idx",
3843+
.raw_types = {
3844+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3845+
BTF_STRUCT_ENC(0, 2, 8), /* [2] */
3846+
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3847+
BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3848+
BTF_TAG_ENC(NAME_TBD, 2, 2),
3849+
BTF_END_RAW,
3850+
},
3851+
BTF_STR_SEC("\0m1\0m2\0tag"),
3852+
.map_type = BPF_MAP_TYPE_ARRAY,
3853+
.map_name = "tag_type_check_btf",
3854+
.key_size = sizeof(int),
3855+
.value_size = 8,
3856+
.key_type_id = 1,
3857+
.value_type_id = 2,
3858+
.max_entries = 1,
3859+
.btf_load_err = true,
3860+
.err_str = "Invalid component_idx",
3861+
},
3862+
{
3863+
.descr = "tag test #11, func parameter, invalid component_idx",
3864+
.raw_types = {
3865+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3866+
BTF_FUNC_PROTO_ENC(0, 2), /* [2] */
3867+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3868+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3869+
BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
3870+
BTF_TAG_ENC(NAME_TBD, 3, 2),
3871+
BTF_END_RAW,
3872+
},
3873+
BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3874+
.map_type = BPF_MAP_TYPE_ARRAY,
3875+
.map_name = "tag_type_check_btf",
3876+
.key_size = sizeof(int),
3877+
.value_size = 4,
3878+
.key_type_id = 1,
3879+
.value_type_id = 1,
3880+
.max_entries = 1,
3881+
.btf_load_err = true,
3882+
.err_str = "Invalid component_idx",
3883+
},
3884+
{
3885+
.descr = "tag test #12, < -1 component_idx",
3886+
.raw_types = {
3887+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3888+
BTF_FUNC_PROTO_ENC(0, 2), /* [2] */
3889+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3890+
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3891+
BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
3892+
BTF_TAG_ENC(NAME_TBD, 3, -2),
3893+
BTF_END_RAW,
3894+
},
3895+
BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3896+
.map_type = BPF_MAP_TYPE_ARRAY,
3897+
.map_name = "tag_type_check_btf",
3898+
.key_size = sizeof(int),
3899+
.value_size = 4,
3900+
.key_type_id = 1,
3901+
.value_type_id = 1,
3902+
.max_entries = 1,
3903+
.btf_load_err = true,
3904+
.err_str = "Invalid component_idx",
3905+
},
3906+
36643907
}; /* struct btf_raw_test raw_tests[] */
36653908

36663909
static const char *get_next_str(const char *start, const char *end)
@@ -6801,6 +7044,8 @@ static int btf_type_size(const struct btf_type *t)
68017044
return base_size + sizeof(struct btf_var);
68027045
case BTF_KIND_DATASEC:
68037046
return base_size + vlen * sizeof(struct btf_var_secinfo);
7047+
case BTF_KIND_TAG:
7048+
return base_size + sizeof(struct btf_tag);
68047049
default:
68057050
fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
68067051
return -EINVAL;

tools/testing/selftests/bpf/test_btf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@
6969
#define BTF_TYPE_FLOAT_ENC(name, sz) \
7070
BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
7171

72+
#define BTF_TAG_ENC(value, type, component_idx) \
73+
BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)
74+
7275
#endif /* _TEST_BTF_H */

0 commit comments

Comments
 (0)