This patch does the followings:
1. Modify libbpf and test_btf to reflect the uapi changes in btf
2. Add test for the btf_header changes
3. Add tests for array->index_type
4. Add err_str check to the tests
5. Fix a 4 bytes hole in "struct test #1" by swapping "m" and "n"

Signed-off-by: Martin KaFai Lau <ka...@fb.com>
---
 tools/lib/bpf/bpf.c                    |   4 +-
 tools/lib/bpf/bpf.h                    |   4 +-
 tools/lib/bpf/btf.c                    |   5 +-
 tools/lib/bpf/libbpf.c                 |  34 +--
 tools/lib/bpf/libbpf.h                 |   4 +-
 tools/testing/selftests/bpf/test_btf.c | 528 ++++++++++++++++++++++++++-------
 6 files changed, 448 insertions(+), 131 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 6a8a00097fd8..442b4cdfeb71 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -89,8 +89,8 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr 
*create_attr)
               min(name_len, BPF_OBJ_NAME_LEN - 1));
        attr.numa_node = create_attr->numa_node;
        attr.btf_fd = create_attr->btf_fd;
-       attr.btf_key_id = create_attr->btf_key_id;
-       attr.btf_value_id = create_attr->btf_value_id;
+       attr.btf_key_type_id = create_attr->btf_key_type_id;
+       attr.btf_value_type_id = create_attr->btf_value_type_id;
        attr.map_ifindex = create_attr->map_ifindex;
 
        return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 15bff7728cf1..d12344f66d4e 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -36,8 +36,8 @@ struct bpf_create_map_attr {
        __u32 max_entries;
        __u32 numa_node;
        __u32 btf_fd;
-       __u32 btf_key_id;
-       __u32 btf_value_id;
+       __u32 btf_key_type_id;
+       __u32 btf_value_type_id;
        __u32 map_ifindex;
 };
 
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 2bac710e3194..8c54a4b6f187 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -35,9 +35,8 @@ struct btf {
 
 static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
 {
-       if (!BTF_STR_TBL_ELF_ID(offset) &&
-           BTF_STR_OFFSET(offset) < btf->hdr->str_len)
-               return &btf->strings[BTF_STR_OFFSET(offset)];
+       if (offset < btf->hdr->str_len)
+               return &btf->strings[offset];
        else
                return NULL;
 }
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3dbe217bf23e..8f1707dbfcfa 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -216,8 +216,8 @@ struct bpf_map {
        size_t offset;
        int map_ifindex;
        struct bpf_map_def def;
-       uint32_t btf_key_id;
-       uint32_t btf_value_id;
+       uint32_t btf_key_type_id;
+       uint32_t btf_value_type_id;
        void *priv;
        bpf_map_clear_priv_t clear_priv;
 };
@@ -1074,8 +1074,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, 
const struct btf *btf)
                return -EINVAL;
        }
 
-       map->btf_key_id = key_id;
-       map->btf_value_id = value_id;
+       map->btf_key_type_id = key_id;
+       map->btf_value_type_id = value_id;
 
        return 0;
 }
@@ -1100,24 +1100,24 @@ bpf_object__create_maps(struct bpf_object *obj)
                create_attr.value_size = def->value_size;
                create_attr.max_entries = def->max_entries;
                create_attr.btf_fd = 0;
-               create_attr.btf_key_id = 0;
-               create_attr.btf_value_id = 0;
+               create_attr.btf_key_type_id = 0;
+               create_attr.btf_value_type_id = 0;
 
                if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
                        create_attr.btf_fd = btf__fd(obj->btf);
-                       create_attr.btf_key_id = map->btf_key_id;
-                       create_attr.btf_value_id = map->btf_value_id;
+                       create_attr.btf_key_type_id = map->btf_key_type_id;
+                       create_attr.btf_value_type_id = map->btf_value_type_id;
                }
 
                *pfd = bpf_create_map_xattr(&create_attr);
-               if (*pfd < 0 && create_attr.btf_key_id) {
+               if (*pfd < 0 && create_attr.btf_key_type_id) {
                        pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). 
Retrying without BTF.\n",
                                   map->name, strerror(errno), errno);
                        create_attr.btf_fd = 0;
-                       create_attr.btf_key_id = 0;
-                       create_attr.btf_value_id = 0;
-                       map->btf_key_id = 0;
-                       map->btf_value_id = 0;
+                       create_attr.btf_key_type_id = 0;
+                       create_attr.btf_value_type_id = 0;
+                       map->btf_key_type_id = 0;
+                       map->btf_value_type_id = 0;
                        *pfd = bpf_create_map_xattr(&create_attr);
                }
 
@@ -2085,14 +2085,14 @@ const char *bpf_map__name(struct bpf_map *map)
        return map ? map->name : NULL;
 }
 
-uint32_t bpf_map__btf_key_id(const struct bpf_map *map)
+uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map)
 {
-       return map ? map->btf_key_id : 0;
+       return map ? map->btf_key_type_id : 0;
 }
 
-uint32_t bpf_map__btf_value_id(const struct bpf_map *map)
+uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map)
 {
-       return map ? map->btf_value_id : 0;
+       return map ? map->btf_value_type_id : 0;
 }
 
 int bpf_map__set_priv(struct bpf_map *map, void *priv,
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index cd3fd8d782c7..09976531aa74 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
 int bpf_map__fd(struct bpf_map *map);
 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
 const char *bpf_map__name(struct bpf_map *map);
-uint32_t bpf_map__btf_key_id(const struct bpf_map *map);
-uint32_t bpf_map__btf_value_id(const struct bpf_map *map);
+uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map);
+uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map);
 
 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
 int bpf_map__set_priv(struct bpf_map *map, void *priv,
diff --git a/tools/testing/selftests/bpf/test_btf.c 
b/tools/testing/selftests/bpf/test_btf.c
index c8bceae7ec02..4635d5557639 100644
--- a/tools/testing/selftests/bpf/test_btf.c
+++ b/tools/testing/selftests/bpf/test_btf.c
@@ -113,22 +113,26 @@ static char btf_log_buf[BTF_LOG_BUF_SIZE];
 static struct btf_header hdr_tmpl = {
        .magic = BTF_MAGIC,
        .version = BTF_VERSION,
+       .hdr_len = sizeof(struct btf_header),
 };
 
 struct btf_raw_test {
        const char *descr;
        const char *str_sec;
        const char *map_name;
+       const char *err_str;
+       int (*special_test)(unsigned int test_num);
        __u32 raw_types[MAX_NR_RAW_TYPES];
        __u32 str_sec_size;
        enum bpf_map_type map_type;
        __u32 key_size;
        __u32 value_size;
-       __u32 key_id;
-       __u32 value_id;
+       __u32 key_type_id;
+       __u32 value_type_id;
        __u32 max_entries;
        bool btf_load_err;
        bool map_create_err;
+       int hdr_len_delta;
        int type_off_delta;
        int str_off_delta;
        int str_len_delta;
@@ -141,8 +145,8 @@ static struct btf_raw_test raw_tests[] = {
  * };
  *
  * struct A {
- *     int m;
- *     unsigned long long n;
+ *     unsigned long long m;
+ *     int n;
  *     char o;
  *     [3 bytes hole]
  *     int p[8];
@@ -160,21 +164,24 @@ static struct btf_raw_test raw_tests[] = {
                /* char */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),   /* [3] */
                /* int[8] */
-               BTF_TYPE_ARRAY_ENC(1, 1, 8),                    /* [4] */
+               BTF_TYPE_ARRAY_ENC(1, 8, 8),                    /* [4] */
                /* struct A { */                                /* [5] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 
180),
-               BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m;               */
-               BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* unsigned long long n;*/
+               BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
+               BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;               */
                BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;              */
                BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]            */
                BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8]         */
                BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r          */
                /* } */
                /* int[4][8] */
-               BTF_TYPE_ARRAY_ENC(4, 1, 4),                    /* [6] */
+               BTF_TYPE_ARRAY_ENC(4, 8, 4),                    /* [6]  */
+               /* enum E */                                    /* [7] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 
sizeof(int)),
                BTF_ENUM_ENC(NAME_TBD, 0),
                BTF_ENUM_ENC(NAME_TBD, 1),
+               /* unsigned int */                              /* [8] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
@@ -183,8 +190,8 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "struct_test1_map",
        .key_size = sizeof(int),
        .value_size = 180,
-       .key_id = 1,
-       .value_id = 5,
+       .key_type_id = 1,
+       .value_type_id = 5,
        .max_entries = 4,
 },
 
@@ -207,7 +214,7 @@ static struct btf_raw_test raw_tests[] = {
                /* int */                                       /* [1] */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
                /* struct b [4] */                              /* [2] */
-               BTF_TYPE_ARRAY_ENC(4, 1, 4),
+               BTF_TYPE_ARRAY_ENC(4, 9, 4),
 
                /* struct A { */                                /* [3] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
@@ -229,7 +236,9 @@ static struct btf_raw_test raw_tests[] = {
                /* const Struct_B */                            /* [7] */
                BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
                /* const Struct_B [4] */                        /* [8] */
-               BTF_TYPE_ARRAY_ENC(7, 1, 4),
+               BTF_TYPE_ARRAY_ENC(7, 9, 4),
+               /* unsigned int */                              /* [9] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
@@ -238,8 +247,8 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "struct_test2_map",
        .key_size = sizeof(int),
        .value_size = 68,
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
 },
 
@@ -258,7 +267,7 @@ static struct btf_raw_test raw_tests[] = {
                /* struct A { */                                /* [2] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 
sizeof(int) * 2 -  1),
                BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
-               BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n; */
+               BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
                /* } */
                BTF_END_RAW,
        },
@@ -268,10 +277,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "size_check1_map",
        .key_size = sizeof(int),
        .value_size = 1,
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Member exceeds struct_size",
 },
 
 /* Test member exeeds the size of struct
@@ -287,12 +297,14 @@ static struct btf_raw_test raw_tests[] = {
                /* int */                                       /* [1] */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
                /* int[2] */                                    /* [2] */
-               BTF_TYPE_ARRAY_ENC(1, 1, 2),
+               BTF_TYPE_ARRAY_ENC(1, 4, 2),
                /* struct A { */                                /* [3] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 
sizeof(int) * 3 - 1),
                BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
                BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
                /* } */
+               /* unsigned int */                              /* [4] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0A\0m\0n",
@@ -301,11 +313,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "size_check2_map",
        .key_size = sizeof(int),
        .value_size = 1,
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
        .btf_load_err = true,
-
+       .err_str = "Member exceeds struct_size",
 },
 
 /* Test member exeeds the size of struct
@@ -335,10 +347,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "size_check3_map",
        .key_size = sizeof(int),
        .value_size = 1,
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Member exceeds struct_size",
 },
 
 /* Test member exceeds the size of struct
@@ -376,10 +389,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "size_check4_map",
        .key_size = sizeof(int),
        .value_size = 1,
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Member exceeds struct_size",
 },
 
 /* typedef const void * const_void_ptr;
@@ -411,8 +425,8 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "void_test1_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *),
-       .key_id = 1,
-       .value_id = 4,
+       .key_type_id = 1,
+       .value_type_id = 4,
        .max_entries = 4,
 },
 
@@ -440,10 +454,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "void_test2_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *),
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Invalid member",
 },
 
 /* typedef const void * const_void_ptr;
@@ -458,10 +473,12 @@ static struct btf_raw_test raw_tests[] = {
                BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
                /* const void* */       /* [3] */
                BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
-               /* typedef const void * const_void_ptr */
+               /* typedef const void * const_void_ptr */       /* [4] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
-               /* const_void_ptr[4] */ /* [4] */
-               BTF_TYPE_ARRAY_ENC(3, 1, 4),
+               /* const_void_ptr[4] */ /* [5] */
+               BTF_TYPE_ARRAY_ENC(3, 6, 4),
+               /* unsigned int */      /* [6] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0const_void_ptr",
@@ -470,8 +487,8 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "void_test3_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *) * 4,
-       .key_id = 1,
-       .value_id = 4,
+       .key_type_id = 1,
+       .value_type_id = 4,
        .max_entries = 4,
 },
 
@@ -484,7 +501,9 @@ static struct btf_raw_test raw_tests[] = {
                /* const void */        /* [2] */
                BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
                /* const void[4] */     /* [3] */
-               BTF_TYPE_ARRAY_ENC(2, 1, 4),
+               BTF_TYPE_ARRAY_ENC(2, 4, 4),
+               /* unsigned int */      /* [4] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0A\0m",
@@ -493,10 +512,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "void_test4_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *) * 4,
-       .key_id = 1,
-       .value_id = 3,
+       .key_type_id = 1,
+       .value_type_id = 3,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Invalid elem",
 },
 
 /* Array_A  <------------------+
@@ -512,9 +532,11 @@ static struct btf_raw_test raw_tests[] = {
                /* int */                       /* [1] */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
                /* Array_A */                   /* [2] */
-               BTF_TYPE_ARRAY_ENC(3, 1, 8),
+               BTF_TYPE_ARRAY_ENC(3, 4, 8),
                /* Array_B */                   /* [3] */
-               BTF_TYPE_ARRAY_ENC(2, 1, 8),
+               BTF_TYPE_ARRAY_ENC(2, 4, 8),
+               /* unsigned int */              /* [4] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "",
@@ -523,10 +545,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test1_map",
        .key_size = sizeof(int),
        .value_size = sizeof(sizeof(int) * 8),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 /* typedef is _before_ the BTF type of Array_A and Array_B
@@ -548,10 +571,11 @@ static struct btf_raw_test raw_tests[] = {
                /* typedef Array_B int_array */
                BTF_TYPEDEF_ENC(1, 4),                          /* [2] */
                /* Array_A */
-               BTF_TYPE_ARRAY_ENC(2, 1, 8),                    /* [3] */
+               BTF_TYPE_ARRAY_ENC(2, 5, 8),                    /* [3] */
                /* Array_B */
-               BTF_TYPE_ARRAY_ENC(3, 1, 8),                    /* [4] */
-
+               BTF_TYPE_ARRAY_ENC(3, 5, 8),                    /* [4] */
+               /* unsigned int */                              /* [5] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0int_array\0",
@@ -560,10 +584,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test2_map",
        .key_size = sizeof(int),
        .value_size = sizeof(sizeof(int) * 8),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 /* Array_A  <------------------+
@@ -579,10 +604,11 @@ static struct btf_raw_test raw_tests[] = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
                /* Array_A */                           /* [2] */
-               BTF_TYPE_ARRAY_ENC(3, 1, 8),
+               BTF_TYPE_ARRAY_ENC(3, 4, 8),
                /* Array_B */                           /* [3] */
-               BTF_TYPE_ARRAY_ENC(2, 1, 8),
-
+               BTF_TYPE_ARRAY_ENC(2, 4, 8),
+               /* unsigned int */                      /* [4] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "",
@@ -591,10 +617,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test3_map",
        .key_size = sizeof(int),
        .value_size = sizeof(sizeof(int) * 8),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 /* typedef is _between_ the BTF type of Array_A and Array_B
@@ -614,11 +641,13 @@ static struct btf_raw_test raw_tests[] = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
                /* Array_A */                           /* [2] */
-               BTF_TYPE_ARRAY_ENC(3, 1, 8),
+               BTF_TYPE_ARRAY_ENC(3, 5, 8),
                /* typedef Array_B int_array */         /* [3] */
                BTF_TYPEDEF_ENC(NAME_TBD, 4),
                /* Array_B */                           /* [4] */
-               BTF_TYPE_ARRAY_ENC(2, 1, 8),
+               BTF_TYPE_ARRAY_ENC(2, 5, 8),
+               /* unsigned int */                      /* [5] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0int_array\0",
@@ -627,10 +656,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test4_map",
        .key_size = sizeof(int),
        .value_size = sizeof(sizeof(int) * 8),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 /* typedef struct B Struct_B
@@ -668,10 +698,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test5_map",
        .key_size = sizeof(int),
        .value_size = 8,
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 /* struct A {
@@ -684,11 +715,13 @@ static struct btf_raw_test raw_tests[] = {
        .raw_types = {
                /* int */
                BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
-               BTF_TYPE_ARRAY_ENC(3, 1, 4),                    /* [2] */
+               BTF_TYPE_ARRAY_ENC(3, 4, 4),                    /* [2] */
                /* struct A */                                  /* [3] */
                BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
                BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x;               */
                BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4]; */
+               /* unsigned int */                              /* [4] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
                BTF_END_RAW,
        },
        .str_sec = "\0A\0x\0y",
@@ -697,10 +730,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test6_map",
        .key_size = sizeof(int),
        .value_size = 8,
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 {
@@ -724,10 +758,11 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test7_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "Loop detected",
 },
 
 {
@@ -759,14 +794,73 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "loop_test8_map",
        .key_size = sizeof(int),
        .value_size = sizeof(void *),
-       .key_id = 1,
-       .value_id = 2,
+       .key_type_id = 1,
+       .value_type_id = 2,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Loop detected",
+},
+
+{
+       .descr = "string section does not end with null",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+               BTF_END_RAW,
+       },
+       .str_sec = "\0int",
+       .str_sec_size = sizeof("\0int") - 1,
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "hdr_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid string section",
+},
+
+{
+       .descr = "empty string section",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = 0,
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "hdr_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid string section",
+},
+
+{
+       .descr = "empty type section",
+       .raw_types = {
+               BTF_END_RAW,
+       },
+       .str_sec = "\0int",
+       .str_sec_size = sizeof("\0int"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "hdr_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
+       .err_str = "No type found",
 },
 
 {
-       .descr = "type_off == str_off",
+       .descr = "btf_header test #1. Longer hdr_len",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -778,15 +872,16 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .type_off_delta = sizeof(struct btf_type) + sizeof(int) + 
sizeof("\0int"),
+       .hdr_len_delta = 4,
+       .err_str = "Unsupported btf_header",
 },
 
 {
-       .descr = "Unaligned type_off",
+       .descr = "btf_header test #2. Gap between hdr and type",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -798,15 +893,16 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .type_off_delta = 1,
+       .type_off_delta = 4,
+       .err_str = "Unsupported section found",
 },
 
 {
-       .descr = "str_off beyonds btf size",
+       .descr = "btf_header test #3. Gap between type and str",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -818,15 +914,16 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .str_off_delta = sizeof("\0int") + 1,
+       .str_off_delta = 4,
+       .err_str = "Unsupported section found",
 },
 
 {
-       .descr = "str_len beyonds btf size",
+       .descr = "btf_header test #4. Overlap between type and str",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -838,15 +935,16 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .str_len_delta = 1,
+       .str_off_delta = -4,
+       .err_str = "Section overlap found",
 },
 
 {
-       .descr = "String section does not end with null",
+       .descr = "btf_header test #5. Larger BTF size",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -858,15 +956,16 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .str_len_delta = -1,
+       .str_len_delta = -4,
+       .err_str = "Unsupported section found",
 },
 
 {
-       .descr = "Empty string section",
+       .descr = "btf_header test #6. Smaller BTF size",
        .raw_types = {
                /* int */                               /* [1] */
                BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
@@ -878,11 +977,223 @@ static struct btf_raw_test raw_tests[] = {
        .map_name = "hdr_test_map",
        .key_size = sizeof(int),
        .value_size = sizeof(int),
-       .key_id = 1,
-       .value_id = 1,
+       .key_type_id = 1,
+       .value_type_id = 1,
        .max_entries = 4,
        .btf_load_err = true,
-       .str_len_delta = 0 - (int)sizeof("\0int"),
+       .str_len_delta = 4,
+       .err_str = "Total section length too long",
+},
+
+{
+       .descr = "array test #1. index_type \"unsigned int\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* unsigned int */                      /* [2] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
+               /* unsigned int[16] */                  /* [3] */
+               BTF_TYPE_ARRAY_ENC(1, 2, 16),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+},
+
+{
+       .descr = "array test #2. index_type \"const unsigned int\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* unsigned int */                      /* [2] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
+               /* int[16] */                           /* [3] */
+               BTF_TYPE_ARRAY_ENC(1, 4, 16),
+               /* CONST type_id=2 */                   /* [4] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+},
+
+{
+       .descr = "array test #3. index_type \"int\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* int[16] */                           /* [2] */
+               BTF_TYPE_ARRAY_ENC(1, 1, 16),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid index",
+},
+
+{
+       .descr = "array test #3. index_type \"const int\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* int[16] */                           /* [2] */
+               BTF_TYPE_ARRAY_ENC(1, 3, 16),
+               /* CONST type_id=1 */                   /* [3] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid index",
+},
+
+{
+       .descr = "array test #4. index_type \"void\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* int[16] */                           /* [2] */
+               BTF_TYPE_ARRAY_ENC(1, 0, 16),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid index",
+},
+
+{
+       .descr = "array test #5. index_type \"const void\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* int[16] */                           /* [2] */
+               BTF_TYPE_ARRAY_ENC(1, 3, 16),
+               /* CONST type_id=0 (void) */            /* [3] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid index",
+},
+
+{
+       .descr = "array test #6. elem_type \"const void *\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* const void *[16] */                  /* [2] */
+               BTF_TYPE_ARRAY_ENC(3, 5, 16),
+               /* CONST type_id=4 */                   /* [3] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
+               /* void* */                             /* [4] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
+               /* unsigned int */                      /* [5] */
+               BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+},
+
+{
+       .descr = "array test #7. index_type \"const void *\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* const void *[16] */                  /* [2] */
+               BTF_TYPE_ARRAY_ENC(3, 3, 16),
+               /* CONST type_id=4 */                   /* [3] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
+               /* void* */                             /* [4] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid index",
+},
+
+{
+       .descr = "invalid BTF_INFO",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               BTF_TYPE_ENC(0, 0x10000000, 4),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "Invalid btf_info",
 },
 
 }; /* struct btf_raw_test raw_tests[] */
@@ -951,6 +1262,7 @@ static void *btf_raw_create(const struct btf_header *hdr,
        memcpy(raw_btf + offset, str, str_sec_size);
 
        ret_hdr = (struct btf_header *)raw_btf;
+       ret_hdr->type_len = type_sec_size;
        ret_hdr->str_off = type_sec_size;
        ret_hdr->str_len = str_sec_size;
 
@@ -981,6 +1293,7 @@ static int do_test_raw(unsigned int test_num)
 
        hdr = raw_btf;
 
+       hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
        hdr->type_off = (int)hdr->type_off + test->type_off_delta;
        hdr->str_off = (int)hdr->str_off + test->str_off_delta;
        hdr->str_len = (int)hdr->str_len + test->str_len_delta;
@@ -992,8 +1305,13 @@ static int do_test_raw(unsigned int test_num)
        free(raw_btf);
 
        err = ((btf_fd == -1) != test->btf_load_err);
-       CHECK(err, "btf_fd:%d test->btf_load_err:%u",
-             btf_fd, test->btf_load_err);
+       if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
+                 btf_fd, test->btf_load_err) ||
+           CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
+                 "expected err_str:%s", test->err_str)) {
+               err = -1;
+               goto done;
+       }
 
        if (err || btf_fd == -1)
                goto done;
@@ -1004,8 +1322,8 @@ static int do_test_raw(unsigned int test_num)
        create_attr.value_size = test->value_size;
        create_attr.max_entries = test->max_entries;
        create_attr.btf_fd = btf_fd;
-       create_attr.btf_key_id = test->key_id;
-       create_attr.btf_value_id = test->value_id;
+       create_attr.btf_key_type_id = test->key_type_id;
+       create_attr.btf_value_type_id = test->value_type_id;
 
        map_fd = bpf_create_map_xattr(&create_attr);
 
@@ -1267,8 +1585,8 @@ static int test_btf_id(unsigned int test_num)
        create_attr.value_size = sizeof(unsigned int);
        create_attr.max_entries = 4;
        create_attr.btf_fd = btf_fd[0];
-       create_attr.btf_key_id = 1;
-       create_attr.btf_value_id = 2;
+       create_attr.btf_key_type_id = 1;
+       create_attr.btf_value_type_id = 2;
 
        map_fd = bpf_create_map_xattr(&create_attr);
        if (CHECK(map_fd == -1, "errno:%d", errno)) {
@@ -1279,10 +1597,10 @@ static int test_btf_id(unsigned int test_num)
        info_len = sizeof(map_info);
        err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
        if (CHECK(err || map_info.btf_id != info[0].id ||
-                 map_info.btf_key_id != 1 || map_info.btf_value_id != 2,
-                 "err:%d errno:%d info.id:%u btf_id:%u btf_key_id:%u 
btf_value_id:%u",
-                 err, errno, info[0].id, map_info.btf_id, map_info.btf_key_id,
-                 map_info.btf_value_id)) {
+                 map_info.btf_key_type_id != 1 || map_info.btf_value_type_id 
!= 2,
+                 "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u 
btf_value_type_id:%u",
+                 err, errno, info[0].id, map_info.btf_id, 
map_info.btf_key_type_id,
+                 map_info.btf_value_type_id)) {
                err = -1;
                goto done;
        }
@@ -1542,10 +1860,10 @@ static int do_test_file(unsigned int test_num)
                goto done;
        }
 
-       err = (bpf_map__btf_key_id(map) == 0 || bpf_map__btf_value_id(map) == 0)
+       err = (bpf_map__btf_key_type_id(map) == 0 || 
bpf_map__btf_value_type_id(map) == 0)
                != test->btf_kv_notfound;
-       if (CHECK(err, "btf_key_id:%u btf_value_id:%u test->btf_kv_notfound:%u",
-                 bpf_map__btf_key_id(map), bpf_map__btf_value_id(map),
+       if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u 
test->btf_kv_notfound:%u",
+                 bpf_map__btf_key_type_id(map), 
bpf_map__btf_value_type_id(map),
                  test->btf_kv_notfound))
                goto done;
 
@@ -1654,8 +1972,8 @@ static struct btf_raw_test pprint_test = {
        .map_name = "pprint_test",
        .key_size = sizeof(unsigned int),
        .value_size = sizeof(struct pprint_mapv),
-       .key_id = 3,    /* unsigned int */
-       .value_id = 16, /* struct pprint_mapv */
+       .key_type_id = 3,       /* unsigned int */
+       .value_type_id = 16,    /* struct pprint_mapv */
        .max_entries = 128 * 1024,
 };
 
@@ -1712,8 +2030,8 @@ static int test_pprint(void)
        create_attr.value_size = test->value_size;
        create_attr.max_entries = test->max_entries;
        create_attr.btf_fd = btf_fd;
-       create_attr.btf_key_id = test->key_id;
-       create_attr.btf_value_id = test->value_id;
+       create_attr.btf_key_type_id = test->key_type_id;
+       create_attr.btf_value_type_id = test->value_type_id;
 
        map_fd = bpf_create_map_xattr(&create_attr);
        if (CHECK(map_fd == -1, "errno:%d", errno)) {
-- 
2.9.5

Reply via email to