Skip to content

Commit e43207f

Browse files
yonghong-songborkmann
authored andcommitted
tools/bpf: test btf bitfield with >=256 struct member offset
This patch modified test_btf pretty print test to cover the bitfield with struct member equal to or greater 256. Without the previous kernel patch fix, the modified test will fail: $ test_btf -p ...... BTF pretty print array(#1)......unexpected pprint output expected: 0: {0,0,0,0x3,0x0,0x3,{0|[0,0,0,0,0,0,0,0]},ENUM_ZERO,4,0x1} read: 0: {0,0,0,0x3,0x0,0x3,{0|[0,0,0,0,0,0,0,0]},ENUM_ZERO,4,0x0} BTF pretty print array(#2)......unexpected pprint output expected: 0: {0,0,0,0x3,0x0,0x3,{0|[0,0,0,0,0,0,0,0]},ENUM_ZERO,4,0x1} read: 0: {0,0,0,0x3,0x0,0x3,{0|[0,0,0,0,0,0,0,0]},ENUM_ZERO,4,0x0} PASS:6 SKIP:0 FAIL:2 With the kernel fix, the modified test will succeed: $ test_btf -p ...... BTF pretty print array(#1)......OK BTF pretty print array(#2)......OK PASS:8 SKIP:0 FAIL:0 Fixes: 9d5f9f7 ("bpf: btf: fix struct/union/fwd types with kind_flag") Acked-by: Martin KaFai Lau <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 17e3ac8 commit e43207f

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

tools/testing/selftests/bpf/test_btf.c

+21-8
Original file line numberDiff line numberDiff line change
@@ -3526,6 +3526,8 @@ struct pprint_mapv {
35263526
ENUM_TWO,
35273527
ENUM_THREE,
35283528
} aenum;
3529+
uint32_t ui32b;
3530+
uint32_t bits2c:2;
35293531
};
35303532

35313533
static struct btf_raw_test pprint_test_template[] = {
@@ -3568,7 +3570,7 @@ static struct btf_raw_test pprint_test_template[] = {
35683570
BTF_ENUM_ENC(NAME_TBD, 2),
35693571
BTF_ENUM_ENC(NAME_TBD, 3),
35703572
/* struct pprint_mapv */ /* [16] */
3571-
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 8), 32),
3573+
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 10), 40),
35723574
BTF_MEMBER_ENC(NAME_TBD, 11, 0), /* uint32_t ui32 */
35733575
BTF_MEMBER_ENC(NAME_TBD, 10, 32), /* uint16_t ui16 */
35743576
BTF_MEMBER_ENC(NAME_TBD, 12, 64), /* int32_t si32 */
@@ -3577,9 +3579,11 @@ static struct btf_raw_test pprint_test_template[] = {
35773579
BTF_MEMBER_ENC(NAME_TBD, 6, 126), /* unused_bits2b */
35783580
BTF_MEMBER_ENC(0, 14, 128), /* union (anon) */
35793581
BTF_MEMBER_ENC(NAME_TBD, 15, 192), /* aenum */
3582+
BTF_MEMBER_ENC(NAME_TBD, 11, 224), /* uint32_t ui32b */
3583+
BTF_MEMBER_ENC(NAME_TBD, 6, 256), /* bits2c */
35803584
BTF_END_RAW,
35813585
},
3582-
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum"),
3586+
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c"),
35833587
.key_size = sizeof(unsigned int),
35843588
.value_size = sizeof(struct pprint_mapv),
35853589
.key_type_id = 3, /* unsigned int */
@@ -3628,7 +3632,7 @@ static struct btf_raw_test pprint_test_template[] = {
36283632
BTF_ENUM_ENC(NAME_TBD, 2),
36293633
BTF_ENUM_ENC(NAME_TBD, 3),
36303634
/* struct pprint_mapv */ /* [16] */
3631-
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 8), 32),
3635+
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 10), 40),
36323636
BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)), /* uint32_t ui32 */
36333637
BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)), /* uint16_t ui16 */
36343638
BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)), /* int32_t si32 */
@@ -3637,9 +3641,11 @@ static struct btf_raw_test pprint_test_template[] = {
36373641
BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 126)), /* unused_bits2b */
36383642
BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)), /* union (anon) */
36393643
BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)), /* aenum */
3644+
BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)), /* uint32_t ui32b */
3645+
BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 256)), /* bits2c */
36403646
BTF_END_RAW,
36413647
},
3642-
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum"),
3648+
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c"),
36433649
.key_size = sizeof(unsigned int),
36443650
.value_size = sizeof(struct pprint_mapv),
36453651
.key_type_id = 3, /* unsigned int */
@@ -3690,7 +3696,7 @@ static struct btf_raw_test pprint_test_template[] = {
36903696
BTF_ENUM_ENC(NAME_TBD, 2),
36913697
BTF_ENUM_ENC(NAME_TBD, 3),
36923698
/* struct pprint_mapv */ /* [16] */
3693-
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 8), 32),
3699+
BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 10), 40),
36943700
BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)), /* uint32_t ui32 */
36953701
BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)), /* uint16_t ui16 */
36963702
BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)), /* int32_t si32 */
@@ -3699,13 +3705,15 @@ static struct btf_raw_test pprint_test_template[] = {
36993705
BTF_MEMBER_ENC(NAME_TBD, 19, BTF_MEMBER_OFFSET(2, 126)),/* unused_bits2b */
37003706
BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)), /* union (anon) */
37013707
BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)), /* aenum */
3708+
BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)), /* uint32_t ui32b */
3709+
BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 256)), /* bits2c */
37023710
/* typedef unsigned int ___int */ /* [17] */
37033711
BTF_TYPEDEF_ENC(NAME_TBD, 18),
37043712
BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 6), /* [18] */
37053713
BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 15), /* [19] */
37063714
BTF_END_RAW,
37073715
},
3708-
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0___int"),
3716+
BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0___int"),
37093717
.key_size = sizeof(unsigned int),
37103718
.value_size = sizeof(struct pprint_mapv),
37113719
.key_type_id = 3, /* unsigned int */
@@ -3793,6 +3801,8 @@ static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i,
37933801
v->unused_bits2b = 3;
37943802
v->ui64 = i;
37953803
v->aenum = i & 0x03;
3804+
v->ui32b = 4;
3805+
v->bits2c = 1;
37963806
v = (void *)v + rounded_value_size;
37973807
}
37983808
}
@@ -3955,7 +3965,8 @@ static int do_test_pprint(int test_num)
39553965

39563966
nexpected_line = snprintf(expected_line, sizeof(expected_line),
39573967
"%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
3958-
"{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n",
3968+
"{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
3969+
"%u,0x%x}\n",
39593970
percpu_map ? "\tcpu" : "",
39603971
percpu_map ? cpu : next_key,
39613972
cmapv->ui32, cmapv->si32,
@@ -3967,7 +3978,9 @@ static int do_test_pprint(int test_num)
39673978
cmapv->ui8a[2], cmapv->ui8a[3],
39683979
cmapv->ui8a[4], cmapv->ui8a[5],
39693980
cmapv->ui8a[6], cmapv->ui8a[7],
3970-
pprint_enum_str[cmapv->aenum]);
3981+
pprint_enum_str[cmapv->aenum],
3982+
cmapv->ui32b,
3983+
cmapv->bits2c);
39713984

39723985
err = check_line(expected_line, nexpected_line,
39733986
sizeof(expected_line), line);

0 commit comments

Comments
 (0)