selftests/bpf: Test BTF_KIND_TAG for deduplication
authorYonghong Song <yhs@fb.com>
Tue, 14 Sep 2021 22:30:52 +0000 (15:30 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 15 Sep 2021 01:45:52 +0000 (18:45 -0700)
Add unit tests for BTF_KIND_TAG deduplication for
  - struct and struct member
  - variable
  - func and func argument

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210914223052.248535-1-yhs@fb.com
tools/testing/selftests/bpf/prog_tests/btf.c

index b4e86a8..9c85d7d 100644 (file)
@@ -6664,27 +6664,33 @@ const struct btf_dedup_test dedup_tests[] = {
                                BTF_MEMBER_ENC(NAME_NTH(4), 5, 64),     /* const int *a;        */
                                BTF_MEMBER_ENC(NAME_NTH(5), 2, 128),    /* int b[16];           */
                                BTF_MEMBER_ENC(NAME_NTH(6), 1, 640),    /* int c;               */
-                               BTF_MEMBER_ENC(NAME_NTH(8), 13, 672),   /* float d;             */
+                               BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),   /* float d;             */
                        /* ptr -> [3] struct s */
                        BTF_PTR_ENC(3),                                                 /* [4] */
                        /* ptr -> [6] const int */
                        BTF_PTR_ENC(6),                                                 /* [5] */
                        /* const -> [1] int */
                        BTF_CONST_ENC(1),                                               /* [6] */
+                       /* tag -> [3] struct s */
+                       BTF_TAG_ENC(NAME_NTH(2), 3, -1),                                /* [7] */
+                       /* tag -> [3] struct s, member 1 */
+                       BTF_TAG_ENC(NAME_NTH(2), 3, 1),                                 /* [8] */
 
                        /* full copy of the above */
-                       BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),        /* [7] */
-                       BTF_TYPE_ARRAY_ENC(7, 7, 16),                                   /* [8] */
-                       BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),                             /* [9] */
-                               BTF_MEMBER_ENC(NAME_NTH(3), 10, 0),
-                               BTF_MEMBER_ENC(NAME_NTH(4), 11, 64),
-                               BTF_MEMBER_ENC(NAME_NTH(5), 8, 128),
-                               BTF_MEMBER_ENC(NAME_NTH(6), 7, 640),
-                               BTF_MEMBER_ENC(NAME_NTH(8), 13, 672),
-                       BTF_PTR_ENC(9),                                                 /* [10] */
-                       BTF_PTR_ENC(12),                                                /* [11] */
-                       BTF_CONST_ENC(7),                                               /* [12] */
-                       BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),                             /* [13] */
+                       BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),        /* [9] */
+                       BTF_TYPE_ARRAY_ENC(9, 9, 16),                                   /* [10] */
+                       BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),                             /* [11] */
+                               BTF_MEMBER_ENC(NAME_NTH(3), 12, 0),
+                               BTF_MEMBER_ENC(NAME_NTH(4), 13, 64),
+                               BTF_MEMBER_ENC(NAME_NTH(5), 10, 128),
+                               BTF_MEMBER_ENC(NAME_NTH(6), 9, 640),
+                               BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),
+                       BTF_PTR_ENC(11),                                                /* [12] */
+                       BTF_PTR_ENC(14),                                                /* [13] */
+                       BTF_CONST_ENC(9),                                               /* [14] */
+                       BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),                             /* [15] */
+                       BTF_TAG_ENC(NAME_NTH(2), 11, -1),                               /* [16] */
+                       BTF_TAG_ENC(NAME_NTH(2), 11, 1),                                /* [17] */
                        BTF_END_RAW,
                },
                BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0float\0d"),
@@ -6701,14 +6707,16 @@ const struct btf_dedup_test dedup_tests[] = {
                                BTF_MEMBER_ENC(NAME_NTH(1), 5, 64),     /* const int *a;        */
                                BTF_MEMBER_ENC(NAME_NTH(2), 2, 128),    /* int b[16];           */
                                BTF_MEMBER_ENC(NAME_NTH(3), 1, 640),    /* int c;               */
-                               BTF_MEMBER_ENC(NAME_NTH(4), 7, 672),    /* float d;             */
+                               BTF_MEMBER_ENC(NAME_NTH(4), 9, 672),    /* float d;             */
                        /* ptr -> [3] struct s */
                        BTF_PTR_ENC(3),                                                 /* [4] */
                        /* ptr -> [6] const int */
                        BTF_PTR_ENC(6),                                                 /* [5] */
                        /* const -> [1] int */
                        BTF_CONST_ENC(1),                                               /* [6] */
-                       BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),                             /* [7] */
+                       BTF_TAG_ENC(NAME_NTH(2), 3, -1),                                /* [7] */
+                       BTF_TAG_ENC(NAME_NTH(2), 3, 1),                                 /* [8] */
+                       BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),                             /* [9] */
                        BTF_END_RAW,
                },
                BTF_STR_SEC("\0a\0b\0c\0d\0int\0float\0next\0s"),
@@ -6833,9 +6841,11 @@ const struct btf_dedup_test dedup_tests[] = {
                                BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
                        BTF_FUNC_ENC(NAME_TBD, 12),                                     /* [13] func */
                        BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),                                /* [14] float */
+                       BTF_TAG_ENC(NAME_TBD, 13, -1),                                  /* [15] tag */
+                       BTF_TAG_ENC(NAME_TBD, 13, 1),                                   /* [16] tag */
                        BTF_END_RAW,
                },
-               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N"),
+               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"),
        },
        .expect = {
                .raw_types = {
@@ -6859,9 +6869,11 @@ const struct btf_dedup_test dedup_tests[] = {
                                BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
                        BTF_FUNC_ENC(NAME_TBD, 12),                                     /* [13] func */
                        BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),                                /* [14] float */
+                       BTF_TAG_ENC(NAME_TBD, 13, -1),                                  /* [15] tag */
+                       BTF_TAG_ENC(NAME_TBD, 13, 1),                                   /* [16] tag */
                        BTF_END_RAW,
                },
-               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N"),
+               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"),
        },
        .opts = {
                .dont_resolve_fwds = false,
@@ -7010,6 +7022,152 @@ const struct btf_dedup_test dedup_tests[] = {
                .dedup_table_size = 1
        },
 },
+{
+       .descr = "dedup: func/func_arg/var tags",
+       .input = {
+               .raw_types = {
+                       /* int */
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       /* static int t */
+                       BTF_VAR_ENC(NAME_NTH(1), 1, 0),                 /* [2] */
+                       /* void f(int a1, int a2) */
+                       BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
+                       BTF_FUNC_ENC(NAME_NTH(4), 2),                   /* [4] */
+                       /* tag -> t */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, -1),                /* [5] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, -1),                /* [6] */
+                       /* tag -> func */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, -1),                /* [7] */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, -1),                /* [8] */
+                       /* tag -> func arg a1 */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, 1),                 /* [9] */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, 1),                 /* [10] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
+       },
+       .expect = {
+               .raw_types = {
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       BTF_VAR_ENC(NAME_NTH(1), 1, 0),                 /* [2] */
+                       BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
+                       BTF_FUNC_ENC(NAME_NTH(4), 2),                   /* [4] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, -1),                /* [5] */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, -1),                /* [6] */
+                       BTF_TAG_ENC(NAME_NTH(5), 4, 1),                 /* [7] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
+       },
+       .opts = {
+               .dont_resolve_fwds = false,
+       },
+},
+{
+       .descr = "dedup: func/func_param tags",
+       .input = {
+               .raw_types = {
+                       /* int */
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       /* void f(int a1, int a2) */
+                       BTF_FUNC_PROTO_ENC(0, 2),                       /* [2] */
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
+                       BTF_FUNC_ENC(NAME_NTH(3), 2),                   /* [3] */
+                       /* void f(int a1, int a2) */
+                       BTF_FUNC_PROTO_ENC(0, 2),                       /* [4] */
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
+                       BTF_FUNC_ENC(NAME_NTH(3), 4),                   /* [5] */
+                       /* tag -> f: tag1, tag2 */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, -1),                /* [6] */
+                       BTF_TAG_ENC(NAME_NTH(5), 3, -1),                /* [7] */
+                       /* tag -> f/a2: tag1, tag2 */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, 1),                 /* [8] */
+                       BTF_TAG_ENC(NAME_NTH(5), 3, 1),                 /* [9] */
+                       /* tag -> f: tag1, tag3 */
+                       BTF_TAG_ENC(NAME_NTH(4), 5, -1),                /* [10] */
+                       BTF_TAG_ENC(NAME_NTH(6), 5, -1),                /* [11] */
+                       /* tag -> f/a2: tag1, tag3 */
+                       BTF_TAG_ENC(NAME_NTH(4), 5, 1),                 /* [12] */
+                       BTF_TAG_ENC(NAME_NTH(6), 5, 1),                 /* [13] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
+       },
+       .expect = {
+               .raw_types = {
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       BTF_FUNC_PROTO_ENC(0, 2),                       /* [2] */
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
+                               BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
+                       BTF_FUNC_ENC(NAME_NTH(3), 2),                   /* [3] */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, -1),                /* [4] */
+                       BTF_TAG_ENC(NAME_NTH(5), 3, -1),                /* [5] */
+                       BTF_TAG_ENC(NAME_NTH(6), 3, -1),                /* [6] */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, 1),                 /* [7] */
+                       BTF_TAG_ENC(NAME_NTH(5), 3, 1),                 /* [8] */
+                       BTF_TAG_ENC(NAME_NTH(6), 3, 1),                 /* [9] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
+       },
+       .opts = {
+               .dont_resolve_fwds = false,
+       },
+},
+{
+       .descr = "dedup: struct/struct_member tags",
+       .input = {
+               .raw_types = {
+                       /* int */
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),              /* [2] */
+                               BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
+                               BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
+                       BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),              /* [3] */
+                               BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
+                               BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
+                       /* tag -> t: tag1, tag2 */
+                       BTF_TAG_ENC(NAME_NTH(4), 2, -1),                /* [4] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, -1),                /* [5] */
+                       /* tag -> t/m2: tag1, tag2 */
+                       BTF_TAG_ENC(NAME_NTH(4), 2, 1),                 /* [6] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, 1),                 /* [7] */
+                       /* tag -> t: tag1, tag3 */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, -1),                /* [8] */
+                       BTF_TAG_ENC(NAME_NTH(6), 3, -1),                /* [9] */
+                       /* tag -> t/m2: tag1, tag3 */
+                       BTF_TAG_ENC(NAME_NTH(4), 3, 1),                 /* [10] */
+                       BTF_TAG_ENC(NAME_NTH(6), 3, 1),                 /* [11] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
+       },
+       .expect = {
+               .raw_types = {
+                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+                       BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),              /* [2] */
+                               BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
+                               BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
+                       BTF_TAG_ENC(NAME_NTH(4), 2, -1),                /* [3] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, -1),                /* [4] */
+                       BTF_TAG_ENC(NAME_NTH(6), 2, -1),                /* [5] */
+                       BTF_TAG_ENC(NAME_NTH(4), 2, 1),                 /* [6] */
+                       BTF_TAG_ENC(NAME_NTH(5), 2, 1),                 /* [7] */
+                       BTF_TAG_ENC(NAME_NTH(6), 2, 1),                 /* [8] */
+                       BTF_END_RAW,
+               },
+               BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
+       },
+       .opts = {
+               .dont_resolve_fwds = false,
+       },
+},
 
 };