Alexei Starovoitov | 1bc38b8 | 2018-10-05 16:40:00 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2018 Facebook */ |
| 3 | |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 5 | #include <stdlib.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6 | #include <string.h> |
Andrii Nakryiko | 62b8cea | 2019-02-07 11:29:24 -0800 | [diff] [blame] | 7 | #include <strings.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/btf.h> |
| 12 | #include "btf.h" |
| 13 | #include "bpf.h" |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 14 | #include "libbpf.h" |
| 15 | #include "libbpf_util.h" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 16 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 17 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 18 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 19 | |
| 20 | #define BTF_MAX_NR_TYPES 65535 |
| 21 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 22 | #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \ |
| 23 | ((k) == BTF_KIND_VOLATILE) || \ |
| 24 | ((k) == BTF_KIND_CONST) || \ |
| 25 | ((k) == BTF_KIND_RESTRICT)) |
| 26 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 27 | static struct btf_type btf_void; |
| 28 | |
| 29 | struct btf { |
| 30 | union { |
| 31 | struct btf_header *hdr; |
| 32 | void *data; |
| 33 | }; |
| 34 | struct btf_type **types; |
| 35 | const char *strings; |
| 36 | void *nohdr_data; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 37 | __u32 nr_types; |
| 38 | __u32 types_size; |
| 39 | __u32 data_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 40 | int fd; |
| 41 | }; |
| 42 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 43 | struct btf_ext_info { |
| 44 | /* |
| 45 | * info points to a deep copy of the individual info section |
| 46 | * (e.g. func_info and line_info) from the .BTF.ext. |
| 47 | * It does not include the __u32 rec_size. |
| 48 | */ |
| 49 | void *info; |
| 50 | __u32 rec_size; |
| 51 | __u32 len; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 54 | struct btf_ext { |
| 55 | struct btf_ext_info func_info; |
| 56 | struct btf_ext_info line_info; |
| 57 | }; |
| 58 | |
| 59 | struct btf_ext_info_sec { |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 60 | __u32 sec_name_off; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 61 | __u32 num_info; |
| 62 | /* Followed by num_info * record_size number of bytes */ |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 63 | __u8 data[0]; |
| 64 | }; |
| 65 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 66 | /* The minimum bpf_func_info checked by the loader */ |
| 67 | struct bpf_func_info_min { |
Martin KaFai Lau | 84ecc1f | 2018-12-05 17:35:47 -0800 | [diff] [blame] | 68 | __u32 insn_off; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 69 | __u32 type_id; |
| 70 | }; |
| 71 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 72 | /* The minimum bpf_line_info checked by the loader */ |
| 73 | struct bpf_line_info_min { |
| 74 | __u32 insn_off; |
| 75 | __u32 file_name_off; |
| 76 | __u32 line_off; |
| 77 | __u32 line_col; |
| 78 | }; |
| 79 | |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 80 | static inline __u64 ptr_to_u64(const void *ptr) |
| 81 | { |
| 82 | return (__u64) (unsigned long) ptr; |
| 83 | } |
| 84 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 85 | static int btf_add_type(struct btf *btf, struct btf_type *t) |
| 86 | { |
| 87 | if (btf->types_size - btf->nr_types < 2) { |
| 88 | struct btf_type **new_types; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 89 | __u32 expand_by, new_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 90 | |
| 91 | if (btf->types_size == BTF_MAX_NR_TYPES) |
| 92 | return -E2BIG; |
| 93 | |
| 94 | expand_by = max(btf->types_size >> 2, 16); |
| 95 | new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by); |
| 96 | |
| 97 | new_types = realloc(btf->types, sizeof(*new_types) * new_size); |
| 98 | if (!new_types) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | if (btf->nr_types == 0) |
| 102 | new_types[0] = &btf_void; |
| 103 | |
| 104 | btf->types = new_types; |
| 105 | btf->types_size = new_size; |
| 106 | } |
| 107 | |
| 108 | btf->types[++(btf->nr_types)] = t; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 113 | static int btf_parse_hdr(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 114 | { |
| 115 | const struct btf_header *hdr = btf->hdr; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 116 | __u32 meta_left; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 117 | |
| 118 | if (btf->data_size < sizeof(struct btf_header)) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 119 | pr_debug("BTF header not found\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | if (hdr->magic != BTF_MAGIC) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 124 | pr_debug("Invalid BTF magic:%x\n", hdr->magic); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | if (hdr->version != BTF_VERSION) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 129 | pr_debug("Unsupported BTF version:%u\n", hdr->version); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 130 | return -ENOTSUP; |
| 131 | } |
| 132 | |
| 133 | if (hdr->flags) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 134 | pr_debug("Unsupported BTF flags:%x\n", hdr->flags); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 135 | return -ENOTSUP; |
| 136 | } |
| 137 | |
| 138 | meta_left = btf->data_size - sizeof(*hdr); |
| 139 | if (!meta_left) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 140 | pr_debug("BTF has no data\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | if (meta_left < hdr->type_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 145 | pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | if (meta_left < hdr->str_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 150 | pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | if (hdr->type_off >= hdr->str_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 155 | pr_debug("BTF type section offset >= string section offset. No type?\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | } |
| 158 | |
| 159 | if (hdr->type_off & 0x02) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 160 | pr_debug("BTF type section is not aligned to 4 bytes\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | btf->nohdr_data = btf->hdr + 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 169 | static int btf_parse_str_sec(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 170 | { |
| 171 | const struct btf_header *hdr = btf->hdr; |
| 172 | const char *start = btf->nohdr_data + hdr->str_off; |
| 173 | const char *end = start + btf->hdr->str_len; |
| 174 | |
| 175 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || |
| 176 | start[0] || end[-1]) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 177 | pr_debug("Invalid BTF string section\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | btf->strings = start; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 186 | static int btf_type_size(struct btf_type *t) |
| 187 | { |
| 188 | int base_size = sizeof(struct btf_type); |
| 189 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 190 | |
| 191 | switch (BTF_INFO_KIND(t->info)) { |
| 192 | case BTF_KIND_FWD: |
| 193 | case BTF_KIND_CONST: |
| 194 | case BTF_KIND_VOLATILE: |
| 195 | case BTF_KIND_RESTRICT: |
| 196 | case BTF_KIND_PTR: |
| 197 | case BTF_KIND_TYPEDEF: |
| 198 | case BTF_KIND_FUNC: |
| 199 | return base_size; |
| 200 | case BTF_KIND_INT: |
| 201 | return base_size + sizeof(__u32); |
| 202 | case BTF_KIND_ENUM: |
| 203 | return base_size + vlen * sizeof(struct btf_enum); |
| 204 | case BTF_KIND_ARRAY: |
| 205 | return base_size + sizeof(struct btf_array); |
| 206 | case BTF_KIND_STRUCT: |
| 207 | case BTF_KIND_UNION: |
| 208 | return base_size + vlen * sizeof(struct btf_member); |
| 209 | case BTF_KIND_FUNC_PROTO: |
| 210 | return base_size + vlen * sizeof(struct btf_param); |
| 211 | default: |
| 212 | pr_debug("Unsupported BTF_KIND:%u\n", BTF_INFO_KIND(t->info)); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | } |
| 216 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 217 | static int btf_parse_type_sec(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 218 | { |
| 219 | struct btf_header *hdr = btf->hdr; |
| 220 | void *nohdr_data = btf->nohdr_data; |
| 221 | void *next_type = nohdr_data + hdr->type_off; |
| 222 | void *end_type = nohdr_data + hdr->str_off; |
| 223 | |
| 224 | while (next_type < end_type) { |
| 225 | struct btf_type *t = next_type; |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 226 | int type_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 227 | int err; |
| 228 | |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 229 | type_size = btf_type_size(t); |
| 230 | if (type_size < 0) |
| 231 | return type_size; |
| 232 | next_type += type_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 233 | err = btf_add_type(btf, t); |
| 234 | if (err) |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Andrii Nakryiko | 9c65112 | 2019-02-04 17:29:46 -0800 | [diff] [blame] | 241 | __u32 btf__get_nr_types(const struct btf *btf) |
| 242 | { |
| 243 | return btf->nr_types; |
| 244 | } |
| 245 | |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 246 | const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 247 | { |
| 248 | if (type_id > btf->nr_types) |
| 249 | return NULL; |
| 250 | |
| 251 | return btf->types[type_id]; |
| 252 | } |
| 253 | |
| 254 | static bool btf_type_is_void(const struct btf_type *t) |
| 255 | { |
| 256 | return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD; |
| 257 | } |
| 258 | |
| 259 | static bool btf_type_is_void_or_null(const struct btf_type *t) |
| 260 | { |
| 261 | return !t || btf_type_is_void(t); |
| 262 | } |
| 263 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 264 | #define MAX_RESOLVE_DEPTH 32 |
| 265 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 266 | __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 267 | { |
| 268 | const struct btf_array *array; |
| 269 | const struct btf_type *t; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 270 | __u32 nelems = 1; |
| 271 | __s64 size = -1; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 272 | int i; |
| 273 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 274 | t = btf__type_by_id(btf, type_id); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 275 | for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); |
| 276 | i++) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 277 | switch (BTF_INFO_KIND(t->info)) { |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 278 | case BTF_KIND_INT: |
| 279 | case BTF_KIND_STRUCT: |
| 280 | case BTF_KIND_UNION: |
| 281 | case BTF_KIND_ENUM: |
| 282 | size = t->size; |
| 283 | goto done; |
| 284 | case BTF_KIND_PTR: |
| 285 | size = sizeof(void *); |
| 286 | goto done; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 287 | case BTF_KIND_TYPEDEF: |
| 288 | case BTF_KIND_VOLATILE: |
| 289 | case BTF_KIND_CONST: |
| 290 | case BTF_KIND_RESTRICT: |
| 291 | type_id = t->type; |
| 292 | break; |
| 293 | case BTF_KIND_ARRAY: |
| 294 | array = (const struct btf_array *)(t + 1); |
| 295 | if (nelems && array->nelems > UINT32_MAX / nelems) |
| 296 | return -E2BIG; |
| 297 | nelems *= array->nelems; |
| 298 | type_id = array->type; |
| 299 | break; |
| 300 | default: |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 304 | t = btf__type_by_id(btf, type_id); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (size < 0) |
| 308 | return -EINVAL; |
| 309 | |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 310 | done: |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 311 | if (nelems && size > UINT32_MAX / nelems) |
| 312 | return -E2BIG; |
| 313 | |
| 314 | return nelems * size; |
| 315 | } |
| 316 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 317 | int btf__resolve_type(const struct btf *btf, __u32 type_id) |
| 318 | { |
| 319 | const struct btf_type *t; |
| 320 | int depth = 0; |
| 321 | |
| 322 | t = btf__type_by_id(btf, type_id); |
| 323 | while (depth < MAX_RESOLVE_DEPTH && |
| 324 | !btf_type_is_void_or_null(t) && |
| 325 | IS_MODIFIER(BTF_INFO_KIND(t->info))) { |
| 326 | type_id = t->type; |
| 327 | t = btf__type_by_id(btf, type_id); |
| 328 | depth++; |
| 329 | } |
| 330 | |
| 331 | if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t)) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | return type_id; |
| 335 | } |
| 336 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 337 | __s32 btf__find_by_name(const struct btf *btf, const char *type_name) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 338 | { |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 339 | __u32 i; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 340 | |
| 341 | if (!strcmp(type_name, "void")) |
| 342 | return 0; |
| 343 | |
| 344 | for (i = 1; i <= btf->nr_types; i++) { |
| 345 | const struct btf_type *t = btf->types[i]; |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 346 | const char *name = btf__name_by_offset(btf, t->name_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 347 | |
| 348 | if (name && !strcmp(type_name, name)) |
| 349 | return i; |
| 350 | } |
| 351 | |
| 352 | return -ENOENT; |
| 353 | } |
| 354 | |
| 355 | void btf__free(struct btf *btf) |
| 356 | { |
| 357 | if (!btf) |
| 358 | return; |
| 359 | |
| 360 | if (btf->fd != -1) |
| 361 | close(btf->fd); |
| 362 | |
| 363 | free(btf->data); |
| 364 | free(btf->types); |
| 365 | free(btf); |
| 366 | } |
| 367 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 368 | struct btf *btf__new(__u8 *data, __u32 size) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 369 | { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 370 | struct btf *btf; |
| 371 | int err; |
| 372 | |
| 373 | btf = calloc(1, sizeof(struct btf)); |
| 374 | if (!btf) |
| 375 | return ERR_PTR(-ENOMEM); |
| 376 | |
| 377 | btf->fd = -1; |
| 378 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 379 | btf->data = malloc(size); |
| 380 | if (!btf->data) { |
| 381 | err = -ENOMEM; |
| 382 | goto done; |
| 383 | } |
| 384 | |
| 385 | memcpy(btf->data, data, size); |
| 386 | btf->data_size = size; |
| 387 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 388 | err = btf_parse_hdr(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 389 | if (err) |
| 390 | goto done; |
| 391 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 392 | err = btf_parse_str_sec(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 393 | if (err) |
| 394 | goto done; |
| 395 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 396 | err = btf_parse_type_sec(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 397 | |
| 398 | done: |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 399 | if (err) { |
| 400 | btf__free(btf); |
| 401 | return ERR_PTR(err); |
| 402 | } |
| 403 | |
| 404 | return btf; |
| 405 | } |
| 406 | |
Andrii Nakryiko | d29d87f | 2019-02-08 11:19:36 -0800 | [diff] [blame] | 407 | int btf__load(struct btf *btf) |
| 408 | { |
| 409 | __u32 log_buf_size = BPF_LOG_BUF_SIZE; |
| 410 | char *log_buf = NULL; |
| 411 | int err = 0; |
| 412 | |
| 413 | if (btf->fd >= 0) |
| 414 | return -EEXIST; |
| 415 | |
| 416 | log_buf = malloc(log_buf_size); |
| 417 | if (!log_buf) |
| 418 | return -ENOMEM; |
| 419 | |
| 420 | *log_buf = 0; |
| 421 | |
| 422 | btf->fd = bpf_load_btf(btf->data, btf->data_size, |
| 423 | log_buf, log_buf_size, false); |
| 424 | if (btf->fd < 0) { |
| 425 | err = -errno; |
| 426 | pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno); |
| 427 | if (*log_buf) |
| 428 | pr_warning("%s\n", log_buf); |
| 429 | goto done; |
| 430 | } |
| 431 | |
| 432 | done: |
| 433 | free(log_buf); |
| 434 | return err; |
| 435 | } |
| 436 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 437 | int btf__fd(const struct btf *btf) |
| 438 | { |
| 439 | return btf->fd; |
| 440 | } |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 441 | |
Andrii Nakryiko | 02c8744 | 2019-02-08 11:19:37 -0800 | [diff] [blame^] | 442 | const void *btf__get_raw_data(const struct btf *btf, __u32 *size) |
| 443 | { |
| 444 | *size = btf->data_size; |
| 445 | return btf->data; |
| 446 | } |
| 447 | |
Andrii Nakryiko | 9c65112 | 2019-02-04 17:29:46 -0800 | [diff] [blame] | 448 | void btf__get_strings(const struct btf *btf, const char **strings, |
| 449 | __u32 *str_len) |
| 450 | { |
| 451 | *strings = btf->strings; |
| 452 | *str_len = btf->hdr->str_len; |
| 453 | } |
| 454 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 455 | const char *btf__name_by_offset(const struct btf *btf, __u32 offset) |
| 456 | { |
| 457 | if (offset < btf->hdr->str_len) |
| 458 | return &btf->strings[offset]; |
| 459 | else |
| 460 | return NULL; |
| 461 | } |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 462 | |
Martin KaFai Lau | 1d2f44c | 2018-11-23 16:44:32 -0800 | [diff] [blame] | 463 | int btf__get_from_id(__u32 id, struct btf **btf) |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 464 | { |
| 465 | struct bpf_btf_info btf_info = { 0 }; |
| 466 | __u32 len = sizeof(btf_info); |
| 467 | __u32 last_size; |
| 468 | int btf_fd; |
| 469 | void *ptr; |
| 470 | int err; |
| 471 | |
| 472 | err = 0; |
| 473 | *btf = NULL; |
| 474 | btf_fd = bpf_btf_get_fd_by_id(id); |
| 475 | if (btf_fd < 0) |
| 476 | return 0; |
| 477 | |
| 478 | /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so |
| 479 | * let's start with a sane default - 4KiB here - and resize it only if |
| 480 | * bpf_obj_get_info_by_fd() needs a bigger buffer. |
| 481 | */ |
| 482 | btf_info.btf_size = 4096; |
| 483 | last_size = btf_info.btf_size; |
| 484 | ptr = malloc(last_size); |
| 485 | if (!ptr) { |
| 486 | err = -ENOMEM; |
| 487 | goto exit_free; |
| 488 | } |
| 489 | |
| 490 | bzero(ptr, last_size); |
| 491 | btf_info.btf = ptr_to_u64(ptr); |
| 492 | err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len); |
| 493 | |
| 494 | if (!err && btf_info.btf_size > last_size) { |
| 495 | void *temp_ptr; |
| 496 | |
| 497 | last_size = btf_info.btf_size; |
| 498 | temp_ptr = realloc(ptr, last_size); |
| 499 | if (!temp_ptr) { |
| 500 | err = -ENOMEM; |
| 501 | goto exit_free; |
| 502 | } |
| 503 | ptr = temp_ptr; |
| 504 | bzero(ptr, last_size); |
| 505 | btf_info.btf = ptr_to_u64(ptr); |
| 506 | err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len); |
| 507 | } |
| 508 | |
| 509 | if (err || btf_info.btf_size > last_size) { |
| 510 | err = errno; |
| 511 | goto exit_free; |
| 512 | } |
| 513 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 514 | *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size); |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 515 | if (IS_ERR(*btf)) { |
| 516 | err = PTR_ERR(*btf); |
| 517 | *btf = NULL; |
| 518 | } |
| 519 | |
| 520 | exit_free: |
| 521 | close(btf_fd); |
| 522 | free(ptr); |
| 523 | |
| 524 | return err; |
| 525 | } |
| 526 | |
Yonghong Song | a6c109a | 2019-02-05 11:48:22 -0800 | [diff] [blame] | 527 | int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 528 | __u32 expected_key_size, __u32 expected_value_size, |
| 529 | __u32 *key_type_id, __u32 *value_type_id) |
| 530 | { |
| 531 | const struct btf_type *container_type; |
| 532 | const struct btf_member *key, *value; |
| 533 | const size_t max_name = 256; |
| 534 | char container_name[max_name]; |
| 535 | __s64 key_size, value_size; |
| 536 | __s32 container_id; |
| 537 | |
| 538 | if (snprintf(container_name, max_name, "____btf_map_%s", map_name) == |
| 539 | max_name) { |
| 540 | pr_warning("map:%s length of '____btf_map_%s' is too long\n", |
| 541 | map_name, map_name); |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
| 545 | container_id = btf__find_by_name(btf, container_name); |
| 546 | if (container_id < 0) { |
Yonghong Song | f7748e2 | 2019-02-05 21:38:30 -0800 | [diff] [blame] | 547 | pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n", |
| 548 | map_name, container_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 549 | return container_id; |
| 550 | } |
| 551 | |
| 552 | container_type = btf__type_by_id(btf, container_id); |
| 553 | if (!container_type) { |
| 554 | pr_warning("map:%s cannot find BTF type for container_id:%u\n", |
| 555 | map_name, container_id); |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT || |
| 560 | BTF_INFO_VLEN(container_type->info) < 2) { |
| 561 | pr_warning("map:%s container_name:%s is an invalid container struct\n", |
| 562 | map_name, container_name); |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | |
| 566 | key = (struct btf_member *)(container_type + 1); |
| 567 | value = key + 1; |
| 568 | |
| 569 | key_size = btf__resolve_size(btf, key->type); |
| 570 | if (key_size < 0) { |
| 571 | pr_warning("map:%s invalid BTF key_type_size\n", map_name); |
| 572 | return key_size; |
| 573 | } |
| 574 | |
| 575 | if (expected_key_size != key_size) { |
| 576 | pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n", |
| 577 | map_name, (__u32)key_size, expected_key_size); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | value_size = btf__resolve_size(btf, value->type); |
| 582 | if (value_size < 0) { |
| 583 | pr_warning("map:%s invalid BTF value_type_size\n", map_name); |
| 584 | return value_size; |
| 585 | } |
| 586 | |
| 587 | if (expected_value_size != value_size) { |
| 588 | pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n", |
| 589 | map_name, (__u32)value_size, expected_value_size); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | *key_type_id = key->type; |
| 594 | *value_type_id = value->type; |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 599 | struct btf_ext_sec_copy_param { |
| 600 | __u32 off; |
| 601 | __u32 len; |
| 602 | __u32 min_rec_size; |
| 603 | struct btf_ext_info *ext_info; |
| 604 | const char *desc; |
| 605 | }; |
| 606 | |
| 607 | static int btf_ext_copy_info(struct btf_ext *btf_ext, |
| 608 | __u8 *data, __u32 data_size, |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 609 | struct btf_ext_sec_copy_param *ext_sec) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 610 | { |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 611 | const struct btf_ext_header *hdr = (struct btf_ext_header *)data; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 612 | const struct btf_ext_info_sec *sinfo; |
| 613 | struct btf_ext_info *ext_info; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 614 | __u32 info_left, record_size; |
| 615 | /* The start of the info sec (including the __u32 record_size). */ |
| 616 | const void *info; |
| 617 | |
| 618 | /* data and data_size do not include btf_ext_header from now on */ |
| 619 | data = data + hdr->hdr_len; |
| 620 | data_size -= hdr->hdr_len; |
| 621 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 622 | if (ext_sec->off & 0x03) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 623 | pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 624 | ext_sec->desc); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 625 | return -EINVAL; |
| 626 | } |
| 627 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 628 | if (data_size < ext_sec->off || |
| 629 | ext_sec->len > data_size - ext_sec->off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 630 | pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 631 | ext_sec->desc, ext_sec->off, ext_sec->len); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 632 | return -EINVAL; |
| 633 | } |
| 634 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 635 | info = data + ext_sec->off; |
| 636 | info_left = ext_sec->len; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 637 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 638 | /* At least a record size */ |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 639 | if (info_left < sizeof(__u32)) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 640 | pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 641 | return -EINVAL; |
| 642 | } |
| 643 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 644 | /* The record size needs to meet the minimum standard */ |
| 645 | record_size = *(__u32 *)info; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 646 | if (record_size < ext_sec->min_rec_size || |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 647 | record_size & 0x03) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 648 | pr_debug("%s section in .BTF.ext has invalid record size %u\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 649 | ext_sec->desc, record_size); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 650 | return -EINVAL; |
| 651 | } |
| 652 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 653 | sinfo = info + sizeof(__u32); |
| 654 | info_left -= sizeof(__u32); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 655 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 656 | /* If no records, return failure now so .BTF.ext won't be used. */ |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 657 | if (!info_left) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 658 | pr_debug("%s section in .BTF.ext has no records", ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 659 | return -EINVAL; |
| 660 | } |
| 661 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 662 | while (info_left) { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 663 | unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 664 | __u64 total_record_size; |
| 665 | __u32 num_records; |
| 666 | |
| 667 | if (info_left < sec_hdrlen) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 668 | pr_debug("%s section header is not found in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 669 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 670 | return -EINVAL; |
| 671 | } |
| 672 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 673 | num_records = sinfo->num_info; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 674 | if (num_records == 0) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 675 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 676 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | } |
| 679 | |
| 680 | total_record_size = sec_hdrlen + |
| 681 | (__u64)num_records * record_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 682 | if (info_left < total_record_size) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 683 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 684 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 685 | return -EINVAL; |
| 686 | } |
| 687 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 688 | info_left -= total_record_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 689 | sinfo = (void *)sinfo + total_record_size; |
| 690 | } |
| 691 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 692 | ext_info = ext_sec->ext_info; |
| 693 | ext_info->len = ext_sec->len - sizeof(__u32); |
| 694 | ext_info->rec_size = record_size; |
| 695 | ext_info->info = malloc(ext_info->len); |
| 696 | if (!ext_info->info) |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 697 | return -ENOMEM; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 698 | memcpy(ext_info->info, info + sizeof(__u32), ext_info->len); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 699 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 703 | static int btf_ext_copy_func_info(struct btf_ext *btf_ext, |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 704 | __u8 *data, __u32 data_size) |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 705 | { |
| 706 | const struct btf_ext_header *hdr = (struct btf_ext_header *)data; |
| 707 | struct btf_ext_sec_copy_param param = { |
| 708 | .off = hdr->func_info_off, |
| 709 | .len = hdr->func_info_len, |
| 710 | .min_rec_size = sizeof(struct bpf_func_info_min), |
| 711 | .ext_info = &btf_ext->func_info, |
| 712 | .desc = "func_info" |
| 713 | }; |
| 714 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 715 | return btf_ext_copy_info(btf_ext, data, data_size, ¶m); |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static int btf_ext_copy_line_info(struct btf_ext *btf_ext, |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 719 | __u8 *data, __u32 data_size) |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 720 | { |
| 721 | const struct btf_ext_header *hdr = (struct btf_ext_header *)data; |
| 722 | struct btf_ext_sec_copy_param param = { |
| 723 | .off = hdr->line_info_off, |
| 724 | .len = hdr->line_info_len, |
| 725 | .min_rec_size = sizeof(struct bpf_line_info_min), |
| 726 | .ext_info = &btf_ext->line_info, |
| 727 | .desc = "line_info", |
| 728 | }; |
| 729 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 730 | return btf_ext_copy_info(btf_ext, data, data_size, ¶m); |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 733 | static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 734 | { |
| 735 | const struct btf_ext_header *hdr = (struct btf_ext_header *)data; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 736 | |
| 737 | if (data_size < offsetof(struct btf_ext_header, func_info_off) || |
| 738 | data_size < hdr->hdr_len) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 739 | pr_debug("BTF.ext header not found"); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 740 | return -EINVAL; |
| 741 | } |
| 742 | |
| 743 | if (hdr->magic != BTF_MAGIC) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 744 | pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 745 | return -EINVAL; |
| 746 | } |
| 747 | |
| 748 | if (hdr->version != BTF_VERSION) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 749 | pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 750 | return -ENOTSUP; |
| 751 | } |
| 752 | |
| 753 | if (hdr->flags) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 754 | pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 755 | return -ENOTSUP; |
| 756 | } |
| 757 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 758 | if (data_size == hdr->hdr_len) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 759 | pr_debug("BTF.ext has no data\n"); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 760 | return -EINVAL; |
| 761 | } |
| 762 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 763 | return 0; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void btf_ext__free(struct btf_ext *btf_ext) |
| 767 | { |
| 768 | if (!btf_ext) |
| 769 | return; |
| 770 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 771 | free(btf_ext->func_info.info); |
| 772 | free(btf_ext->line_info.info); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 773 | free(btf_ext); |
| 774 | } |
| 775 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 776 | struct btf_ext *btf_ext__new(__u8 *data, __u32 size) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 777 | { |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 778 | struct btf_ext *btf_ext; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 779 | int err; |
| 780 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 781 | err = btf_ext_parse_hdr(data, size); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 782 | if (err) |
| 783 | return ERR_PTR(err); |
| 784 | |
| 785 | btf_ext = calloc(1, sizeof(struct btf_ext)); |
| 786 | if (!btf_ext) |
| 787 | return ERR_PTR(-ENOMEM); |
| 788 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 789 | err = btf_ext_copy_func_info(btf_ext, data, size); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 790 | if (err) { |
| 791 | btf_ext__free(btf_ext); |
| 792 | return ERR_PTR(err); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 795 | err = btf_ext_copy_line_info(btf_ext, data, size); |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 796 | if (err) { |
| 797 | btf_ext__free(btf_ext); |
| 798 | return ERR_PTR(err); |
| 799 | } |
| 800 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 801 | return btf_ext; |
| 802 | } |
| 803 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 804 | static int btf_ext_reloc_info(const struct btf *btf, |
| 805 | const struct btf_ext_info *ext_info, |
| 806 | const char *sec_name, __u32 insns_cnt, |
| 807 | void **info, __u32 *cnt) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 808 | { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 809 | __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec); |
| 810 | __u32 i, record_size, existing_len, records_len; |
| 811 | struct btf_ext_info_sec *sinfo; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 812 | const char *info_sec_name; |
| 813 | __u64 remain_len; |
| 814 | void *data; |
| 815 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 816 | record_size = ext_info->rec_size; |
| 817 | sinfo = ext_info->info; |
| 818 | remain_len = ext_info->len; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 819 | while (remain_len > 0) { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 820 | records_len = sinfo->num_info * record_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 821 | info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off); |
| 822 | if (strcmp(info_sec_name, sec_name)) { |
| 823 | remain_len -= sec_hdrlen + records_len; |
| 824 | sinfo = (void *)sinfo + sec_hdrlen + records_len; |
| 825 | continue; |
| 826 | } |
| 827 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 828 | existing_len = (*cnt) * record_size; |
| 829 | data = realloc(*info, existing_len + records_len); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 830 | if (!data) |
| 831 | return -ENOMEM; |
| 832 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 833 | memcpy(data + existing_len, sinfo->data, records_len); |
Martin KaFai Lau | 84ecc1f | 2018-12-05 17:35:47 -0800 | [diff] [blame] | 834 | /* adjust insn_off only, the rest data will be passed |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 835 | * to the kernel. |
| 836 | */ |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 837 | for (i = 0; i < sinfo->num_info; i++) { |
| 838 | __u32 *insn_off; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 839 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 840 | insn_off = data + existing_len + (i * record_size); |
| 841 | *insn_off = *insn_off / sizeof(struct bpf_insn) + |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 842 | insns_cnt; |
| 843 | } |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 844 | *info = data; |
| 845 | *cnt += sinfo->num_info; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 849 | return -ENOENT; |
| 850 | } |
| 851 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 852 | int btf_ext__reloc_func_info(const struct btf *btf, const struct btf_ext *btf_ext, |
| 853 | const char *sec_name, __u32 insns_cnt, |
| 854 | void **func_info, __u32 *cnt) |
| 855 | { |
| 856 | return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name, |
| 857 | insns_cnt, func_info, cnt); |
| 858 | } |
| 859 | |
| 860 | int btf_ext__reloc_line_info(const struct btf *btf, const struct btf_ext *btf_ext, |
| 861 | const char *sec_name, __u32 insns_cnt, |
| 862 | void **line_info, __u32 *cnt) |
| 863 | { |
| 864 | return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name, |
| 865 | insns_cnt, line_info, cnt); |
| 866 | } |
| 867 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 868 | __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext) |
| 869 | { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 870 | return btf_ext->func_info.rec_size; |
| 871 | } |
| 872 | |
| 873 | __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext) |
| 874 | { |
| 875 | return btf_ext->line_info.rec_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 876 | } |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 877 | |
| 878 | struct btf_dedup; |
| 879 | |
| 880 | static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext, |
| 881 | const struct btf_dedup_opts *opts); |
| 882 | static void btf_dedup_free(struct btf_dedup *d); |
| 883 | static int btf_dedup_strings(struct btf_dedup *d); |
| 884 | static int btf_dedup_prim_types(struct btf_dedup *d); |
| 885 | static int btf_dedup_struct_types(struct btf_dedup *d); |
| 886 | static int btf_dedup_ref_types(struct btf_dedup *d); |
| 887 | static int btf_dedup_compact_types(struct btf_dedup *d); |
| 888 | static int btf_dedup_remap_types(struct btf_dedup *d); |
| 889 | |
| 890 | /* |
| 891 | * Deduplicate BTF types and strings. |
| 892 | * |
| 893 | * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF |
| 894 | * section with all BTF type descriptors and string data. It overwrites that |
| 895 | * memory in-place with deduplicated types and strings without any loss of |
| 896 | * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section |
| 897 | * is provided, all the strings referenced from .BTF.ext section are honored |
| 898 | * and updated to point to the right offsets after deduplication. |
| 899 | * |
| 900 | * If function returns with error, type/string data might be garbled and should |
| 901 | * be discarded. |
| 902 | * |
| 903 | * More verbose and detailed description of both problem btf_dedup is solving, |
| 904 | * as well as solution could be found at: |
| 905 | * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html |
| 906 | * |
| 907 | * Problem description and justification |
| 908 | * ===================================== |
| 909 | * |
| 910 | * BTF type information is typically emitted either as a result of conversion |
| 911 | * from DWARF to BTF or directly by compiler. In both cases, each compilation |
| 912 | * unit contains information about a subset of all the types that are used |
| 913 | * in an application. These subsets are frequently overlapping and contain a lot |
| 914 | * of duplicated information when later concatenated together into a single |
| 915 | * binary. This algorithm ensures that each unique type is represented by single |
| 916 | * BTF type descriptor, greatly reducing resulting size of BTF data. |
| 917 | * |
| 918 | * Compilation unit isolation and subsequent duplication of data is not the only |
| 919 | * problem. The same type hierarchy (e.g., struct and all the type that struct |
| 920 | * references) in different compilation units can be represented in BTF to |
| 921 | * various degrees of completeness (or, rather, incompleteness) due to |
| 922 | * struct/union forward declarations. |
| 923 | * |
| 924 | * Let's take a look at an example, that we'll use to better understand the |
| 925 | * problem (and solution). Suppose we have two compilation units, each using |
| 926 | * same `struct S`, but each of them having incomplete type information about |
| 927 | * struct's fields: |
| 928 | * |
| 929 | * // CU #1: |
| 930 | * struct S; |
| 931 | * struct A { |
| 932 | * int a; |
| 933 | * struct A* self; |
| 934 | * struct S* parent; |
| 935 | * }; |
| 936 | * struct B; |
| 937 | * struct S { |
| 938 | * struct A* a_ptr; |
| 939 | * struct B* b_ptr; |
| 940 | * }; |
| 941 | * |
| 942 | * // CU #2: |
| 943 | * struct S; |
| 944 | * struct A; |
| 945 | * struct B { |
| 946 | * int b; |
| 947 | * struct B* self; |
| 948 | * struct S* parent; |
| 949 | * }; |
| 950 | * struct S { |
| 951 | * struct A* a_ptr; |
| 952 | * struct B* b_ptr; |
| 953 | * }; |
| 954 | * |
| 955 | * In case of CU #1, BTF data will know only that `struct B` exist (but no |
| 956 | * more), but will know the complete type information about `struct A`. While |
| 957 | * for CU #2, it will know full type information about `struct B`, but will |
| 958 | * only know about forward declaration of `struct A` (in BTF terms, it will |
| 959 | * have `BTF_KIND_FWD` type descriptor with name `B`). |
| 960 | * |
| 961 | * This compilation unit isolation means that it's possible that there is no |
| 962 | * single CU with complete type information describing structs `S`, `A`, and |
| 963 | * `B`. Also, we might get tons of duplicated and redundant type information. |
| 964 | * |
| 965 | * Additional complication we need to keep in mind comes from the fact that |
| 966 | * types, in general, can form graphs containing cycles, not just DAGs. |
| 967 | * |
| 968 | * While algorithm does deduplication, it also merges and resolves type |
| 969 | * information (unless disabled throught `struct btf_opts`), whenever possible. |
| 970 | * E.g., in the example above with two compilation units having partial type |
| 971 | * information for structs `A` and `B`, the output of algorithm will emit |
| 972 | * a single copy of each BTF type that describes structs `A`, `B`, and `S` |
| 973 | * (as well as type information for `int` and pointers), as if they were defined |
| 974 | * in a single compilation unit as: |
| 975 | * |
| 976 | * struct A { |
| 977 | * int a; |
| 978 | * struct A* self; |
| 979 | * struct S* parent; |
| 980 | * }; |
| 981 | * struct B { |
| 982 | * int b; |
| 983 | * struct B* self; |
| 984 | * struct S* parent; |
| 985 | * }; |
| 986 | * struct S { |
| 987 | * struct A* a_ptr; |
| 988 | * struct B* b_ptr; |
| 989 | * }; |
| 990 | * |
| 991 | * Algorithm summary |
| 992 | * ================= |
| 993 | * |
| 994 | * Algorithm completes its work in 6 separate passes: |
| 995 | * |
| 996 | * 1. Strings deduplication. |
| 997 | * 2. Primitive types deduplication (int, enum, fwd). |
| 998 | * 3. Struct/union types deduplication. |
| 999 | * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func |
| 1000 | * protos, and const/volatile/restrict modifiers). |
| 1001 | * 5. Types compaction. |
| 1002 | * 6. Types remapping. |
| 1003 | * |
| 1004 | * Algorithm determines canonical type descriptor, which is a single |
| 1005 | * representative type for each truly unique type. This canonical type is the |
| 1006 | * one that will go into final deduplicated BTF type information. For |
| 1007 | * struct/unions, it is also the type that algorithm will merge additional type |
| 1008 | * information into (while resolving FWDs), as it discovers it from data in |
| 1009 | * other CUs. Each input BTF type eventually gets either mapped to itself, if |
| 1010 | * that type is canonical, or to some other type, if that type is equivalent |
| 1011 | * and was chosen as canonical representative. This mapping is stored in |
| 1012 | * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that |
| 1013 | * FWD type got resolved to. |
| 1014 | * |
| 1015 | * To facilitate fast discovery of canonical types, we also maintain canonical |
| 1016 | * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash |
| 1017 | * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types |
| 1018 | * that match that signature. With sufficiently good choice of type signature |
| 1019 | * hashing function, we can limit number of canonical types for each unique type |
| 1020 | * signature to a very small number, allowing to find canonical type for any |
| 1021 | * duplicated type very quickly. |
| 1022 | * |
| 1023 | * Struct/union deduplication is the most critical part and algorithm for |
| 1024 | * deduplicating structs/unions is described in greater details in comments for |
| 1025 | * `btf_dedup_is_equiv` function. |
| 1026 | */ |
| 1027 | int btf__dedup(struct btf *btf, struct btf_ext *btf_ext, |
| 1028 | const struct btf_dedup_opts *opts) |
| 1029 | { |
| 1030 | struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts); |
| 1031 | int err; |
| 1032 | |
| 1033 | if (IS_ERR(d)) { |
| 1034 | pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d)); |
| 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | |
| 1038 | err = btf_dedup_strings(d); |
| 1039 | if (err < 0) { |
| 1040 | pr_debug("btf_dedup_strings failed:%d\n", err); |
| 1041 | goto done; |
| 1042 | } |
| 1043 | err = btf_dedup_prim_types(d); |
| 1044 | if (err < 0) { |
| 1045 | pr_debug("btf_dedup_prim_types failed:%d\n", err); |
| 1046 | goto done; |
| 1047 | } |
| 1048 | err = btf_dedup_struct_types(d); |
| 1049 | if (err < 0) { |
| 1050 | pr_debug("btf_dedup_struct_types failed:%d\n", err); |
| 1051 | goto done; |
| 1052 | } |
| 1053 | err = btf_dedup_ref_types(d); |
| 1054 | if (err < 0) { |
| 1055 | pr_debug("btf_dedup_ref_types failed:%d\n", err); |
| 1056 | goto done; |
| 1057 | } |
| 1058 | err = btf_dedup_compact_types(d); |
| 1059 | if (err < 0) { |
| 1060 | pr_debug("btf_dedup_compact_types failed:%d\n", err); |
| 1061 | goto done; |
| 1062 | } |
| 1063 | err = btf_dedup_remap_types(d); |
| 1064 | if (err < 0) { |
| 1065 | pr_debug("btf_dedup_remap_types failed:%d\n", err); |
| 1066 | goto done; |
| 1067 | } |
| 1068 | |
| 1069 | done: |
| 1070 | btf_dedup_free(d); |
| 1071 | return err; |
| 1072 | } |
| 1073 | |
| 1074 | #define BTF_DEDUP_TABLE_SIZE_LOG 14 |
| 1075 | #define BTF_DEDUP_TABLE_MOD ((1 << BTF_DEDUP_TABLE_SIZE_LOG) - 1) |
| 1076 | #define BTF_UNPROCESSED_ID ((__u32)-1) |
| 1077 | #define BTF_IN_PROGRESS_ID ((__u32)-2) |
| 1078 | |
| 1079 | struct btf_dedup_node { |
| 1080 | struct btf_dedup_node *next; |
| 1081 | __u32 type_id; |
| 1082 | }; |
| 1083 | |
| 1084 | struct btf_dedup { |
| 1085 | /* .BTF section to be deduped in-place */ |
| 1086 | struct btf *btf; |
| 1087 | /* |
| 1088 | * Optional .BTF.ext section. When provided, any strings referenced |
| 1089 | * from it will be taken into account when deduping strings |
| 1090 | */ |
| 1091 | struct btf_ext *btf_ext; |
| 1092 | /* |
| 1093 | * This is a map from any type's signature hash to a list of possible |
| 1094 | * canonical representative type candidates. Hash collisions are |
| 1095 | * ignored, so even types of various kinds can share same list of |
| 1096 | * candidates, which is fine because we rely on subsequent |
| 1097 | * btf_xxx_equal() checks to authoritatively verify type equality. |
| 1098 | */ |
| 1099 | struct btf_dedup_node **dedup_table; |
| 1100 | /* Canonical types map */ |
| 1101 | __u32 *map; |
| 1102 | /* Hypothetical mapping, used during type graph equivalence checks */ |
| 1103 | __u32 *hypot_map; |
| 1104 | __u32 *hypot_list; |
| 1105 | size_t hypot_cnt; |
| 1106 | size_t hypot_cap; |
| 1107 | /* Various option modifying behavior of algorithm */ |
| 1108 | struct btf_dedup_opts opts; |
| 1109 | }; |
| 1110 | |
| 1111 | struct btf_str_ptr { |
| 1112 | const char *str; |
| 1113 | __u32 new_off; |
| 1114 | bool used; |
| 1115 | }; |
| 1116 | |
| 1117 | struct btf_str_ptrs { |
| 1118 | struct btf_str_ptr *ptrs; |
| 1119 | const char *data; |
| 1120 | __u32 cnt; |
| 1121 | __u32 cap; |
| 1122 | }; |
| 1123 | |
| 1124 | static inline __u32 hash_combine(__u32 h, __u32 value) |
| 1125 | { |
| 1126 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ |
| 1127 | #define GOLDEN_RATIO_PRIME 0x9e370001UL |
| 1128 | return h * 37 + value * GOLDEN_RATIO_PRIME; |
| 1129 | #undef GOLDEN_RATIO_PRIME |
| 1130 | } |
| 1131 | |
| 1132 | #define for_each_hash_node(table, hash, node) \ |
| 1133 | for (node = table[hash & BTF_DEDUP_TABLE_MOD]; node; node = node->next) |
| 1134 | |
| 1135 | static int btf_dedup_table_add(struct btf_dedup *d, __u32 hash, __u32 type_id) |
| 1136 | { |
| 1137 | struct btf_dedup_node *node = malloc(sizeof(struct btf_dedup_node)); |
| 1138 | |
| 1139 | if (!node) |
| 1140 | return -ENOMEM; |
| 1141 | node->type_id = type_id; |
| 1142 | node->next = d->dedup_table[hash & BTF_DEDUP_TABLE_MOD]; |
| 1143 | d->dedup_table[hash & BTF_DEDUP_TABLE_MOD] = node; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static int btf_dedup_hypot_map_add(struct btf_dedup *d, |
| 1148 | __u32 from_id, __u32 to_id) |
| 1149 | { |
| 1150 | if (d->hypot_cnt == d->hypot_cap) { |
| 1151 | __u32 *new_list; |
| 1152 | |
| 1153 | d->hypot_cap += max(16, d->hypot_cap / 2); |
| 1154 | new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap); |
| 1155 | if (!new_list) |
| 1156 | return -ENOMEM; |
| 1157 | d->hypot_list = new_list; |
| 1158 | } |
| 1159 | d->hypot_list[d->hypot_cnt++] = from_id; |
| 1160 | d->hypot_map[from_id] = to_id; |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | static void btf_dedup_clear_hypot_map(struct btf_dedup *d) |
| 1165 | { |
| 1166 | int i; |
| 1167 | |
| 1168 | for (i = 0; i < d->hypot_cnt; i++) |
| 1169 | d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; |
| 1170 | d->hypot_cnt = 0; |
| 1171 | } |
| 1172 | |
| 1173 | static void btf_dedup_table_free(struct btf_dedup *d) |
| 1174 | { |
| 1175 | struct btf_dedup_node *head, *tmp; |
| 1176 | int i; |
| 1177 | |
| 1178 | if (!d->dedup_table) |
| 1179 | return; |
| 1180 | |
| 1181 | for (i = 0; i < (1 << BTF_DEDUP_TABLE_SIZE_LOG); i++) { |
| 1182 | while (d->dedup_table[i]) { |
| 1183 | tmp = d->dedup_table[i]; |
| 1184 | d->dedup_table[i] = tmp->next; |
| 1185 | free(tmp); |
| 1186 | } |
| 1187 | |
| 1188 | head = d->dedup_table[i]; |
| 1189 | while (head) { |
| 1190 | tmp = head; |
| 1191 | head = head->next; |
| 1192 | free(tmp); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | free(d->dedup_table); |
| 1197 | d->dedup_table = NULL; |
| 1198 | } |
| 1199 | |
| 1200 | static void btf_dedup_free(struct btf_dedup *d) |
| 1201 | { |
| 1202 | btf_dedup_table_free(d); |
| 1203 | |
| 1204 | free(d->map); |
| 1205 | d->map = NULL; |
| 1206 | |
| 1207 | free(d->hypot_map); |
| 1208 | d->hypot_map = NULL; |
| 1209 | |
| 1210 | free(d->hypot_list); |
| 1211 | d->hypot_list = NULL; |
| 1212 | |
| 1213 | free(d); |
| 1214 | } |
| 1215 | |
| 1216 | static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext, |
| 1217 | const struct btf_dedup_opts *opts) |
| 1218 | { |
| 1219 | struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup)); |
| 1220 | int i, err = 0; |
| 1221 | |
| 1222 | if (!d) |
| 1223 | return ERR_PTR(-ENOMEM); |
| 1224 | |
| 1225 | d->btf = btf; |
| 1226 | d->btf_ext = btf_ext; |
| 1227 | |
| 1228 | d->dedup_table = calloc(1 << BTF_DEDUP_TABLE_SIZE_LOG, |
| 1229 | sizeof(struct btf_dedup_node *)); |
| 1230 | if (!d->dedup_table) { |
| 1231 | err = -ENOMEM; |
| 1232 | goto done; |
| 1233 | } |
| 1234 | |
| 1235 | d->map = malloc(sizeof(__u32) * (1 + btf->nr_types)); |
| 1236 | if (!d->map) { |
| 1237 | err = -ENOMEM; |
| 1238 | goto done; |
| 1239 | } |
| 1240 | /* special BTF "void" type is made canonical immediately */ |
| 1241 | d->map[0] = 0; |
| 1242 | for (i = 1; i <= btf->nr_types; i++) |
| 1243 | d->map[i] = BTF_UNPROCESSED_ID; |
| 1244 | |
| 1245 | d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types)); |
| 1246 | if (!d->hypot_map) { |
| 1247 | err = -ENOMEM; |
| 1248 | goto done; |
| 1249 | } |
| 1250 | for (i = 0; i <= btf->nr_types; i++) |
| 1251 | d->hypot_map[i] = BTF_UNPROCESSED_ID; |
| 1252 | |
| 1253 | d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds; |
| 1254 | |
| 1255 | done: |
| 1256 | if (err) { |
| 1257 | btf_dedup_free(d); |
| 1258 | return ERR_PTR(err); |
| 1259 | } |
| 1260 | |
| 1261 | return d; |
| 1262 | } |
| 1263 | |
| 1264 | typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx); |
| 1265 | |
| 1266 | /* |
| 1267 | * Iterate over all possible places in .BTF and .BTF.ext that can reference |
| 1268 | * string and pass pointer to it to a provided callback `fn`. |
| 1269 | */ |
| 1270 | static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx) |
| 1271 | { |
| 1272 | void *line_data_cur, *line_data_end; |
| 1273 | int i, j, r, rec_size; |
| 1274 | struct btf_type *t; |
| 1275 | |
| 1276 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 1277 | t = d->btf->types[i]; |
| 1278 | r = fn(&t->name_off, ctx); |
| 1279 | if (r) |
| 1280 | return r; |
| 1281 | |
| 1282 | switch (BTF_INFO_KIND(t->info)) { |
| 1283 | case BTF_KIND_STRUCT: |
| 1284 | case BTF_KIND_UNION: { |
| 1285 | struct btf_member *m = (struct btf_member *)(t + 1); |
| 1286 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 1287 | |
| 1288 | for (j = 0; j < vlen; j++) { |
| 1289 | r = fn(&m->name_off, ctx); |
| 1290 | if (r) |
| 1291 | return r; |
| 1292 | m++; |
| 1293 | } |
| 1294 | break; |
| 1295 | } |
| 1296 | case BTF_KIND_ENUM: { |
| 1297 | struct btf_enum *m = (struct btf_enum *)(t + 1); |
| 1298 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 1299 | |
| 1300 | for (j = 0; j < vlen; j++) { |
| 1301 | r = fn(&m->name_off, ctx); |
| 1302 | if (r) |
| 1303 | return r; |
| 1304 | m++; |
| 1305 | } |
| 1306 | break; |
| 1307 | } |
| 1308 | case BTF_KIND_FUNC_PROTO: { |
| 1309 | struct btf_param *m = (struct btf_param *)(t + 1); |
| 1310 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 1311 | |
| 1312 | for (j = 0; j < vlen; j++) { |
| 1313 | r = fn(&m->name_off, ctx); |
| 1314 | if (r) |
| 1315 | return r; |
| 1316 | m++; |
| 1317 | } |
| 1318 | break; |
| 1319 | } |
| 1320 | default: |
| 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | if (!d->btf_ext) |
| 1326 | return 0; |
| 1327 | |
| 1328 | line_data_cur = d->btf_ext->line_info.info; |
| 1329 | line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len; |
| 1330 | rec_size = d->btf_ext->line_info.rec_size; |
| 1331 | |
| 1332 | while (line_data_cur < line_data_end) { |
| 1333 | struct btf_ext_info_sec *sec = line_data_cur; |
| 1334 | struct bpf_line_info_min *line_info; |
| 1335 | __u32 num_info = sec->num_info; |
| 1336 | |
| 1337 | r = fn(&sec->sec_name_off, ctx); |
| 1338 | if (r) |
| 1339 | return r; |
| 1340 | |
| 1341 | line_data_cur += sizeof(struct btf_ext_info_sec); |
| 1342 | for (i = 0; i < num_info; i++) { |
| 1343 | line_info = line_data_cur; |
| 1344 | r = fn(&line_info->file_name_off, ctx); |
| 1345 | if (r) |
| 1346 | return r; |
| 1347 | r = fn(&line_info->line_off, ctx); |
| 1348 | if (r) |
| 1349 | return r; |
| 1350 | line_data_cur += rec_size; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static int str_sort_by_content(const void *a1, const void *a2) |
| 1358 | { |
| 1359 | const struct btf_str_ptr *p1 = a1; |
| 1360 | const struct btf_str_ptr *p2 = a2; |
| 1361 | |
| 1362 | return strcmp(p1->str, p2->str); |
| 1363 | } |
| 1364 | |
| 1365 | static int str_sort_by_offset(const void *a1, const void *a2) |
| 1366 | { |
| 1367 | const struct btf_str_ptr *p1 = a1; |
| 1368 | const struct btf_str_ptr *p2 = a2; |
| 1369 | |
| 1370 | if (p1->str != p2->str) |
| 1371 | return p1->str < p2->str ? -1 : 1; |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem) |
| 1376 | { |
| 1377 | const struct btf_str_ptr *p = pelem; |
| 1378 | |
| 1379 | if (str_ptr != p->str) |
| 1380 | return (const char *)str_ptr < p->str ? -1 : 1; |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx) |
| 1385 | { |
| 1386 | struct btf_str_ptrs *strs; |
| 1387 | struct btf_str_ptr *s; |
| 1388 | |
| 1389 | if (*str_off_ptr == 0) |
| 1390 | return 0; |
| 1391 | |
| 1392 | strs = ctx; |
| 1393 | s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt, |
| 1394 | sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp); |
| 1395 | if (!s) |
| 1396 | return -EINVAL; |
| 1397 | s->used = true; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx) |
| 1402 | { |
| 1403 | struct btf_str_ptrs *strs; |
| 1404 | struct btf_str_ptr *s; |
| 1405 | |
| 1406 | if (*str_off_ptr == 0) |
| 1407 | return 0; |
| 1408 | |
| 1409 | strs = ctx; |
| 1410 | s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt, |
| 1411 | sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp); |
| 1412 | if (!s) |
| 1413 | return -EINVAL; |
| 1414 | *str_off_ptr = s->new_off; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * Dedup string and filter out those that are not referenced from either .BTF |
| 1420 | * or .BTF.ext (if provided) sections. |
| 1421 | * |
| 1422 | * This is done by building index of all strings in BTF's string section, |
| 1423 | * then iterating over all entities that can reference strings (e.g., type |
| 1424 | * names, struct field names, .BTF.ext line info, etc) and marking corresponding |
| 1425 | * strings as used. After that all used strings are deduped and compacted into |
| 1426 | * sequential blob of memory and new offsets are calculated. Then all the string |
| 1427 | * references are iterated again and rewritten using new offsets. |
| 1428 | */ |
| 1429 | static int btf_dedup_strings(struct btf_dedup *d) |
| 1430 | { |
| 1431 | const struct btf_header *hdr = d->btf->hdr; |
| 1432 | char *start = (char *)d->btf->nohdr_data + hdr->str_off; |
| 1433 | char *end = start + d->btf->hdr->str_len; |
| 1434 | char *p = start, *tmp_strs = NULL; |
| 1435 | struct btf_str_ptrs strs = { |
| 1436 | .cnt = 0, |
| 1437 | .cap = 0, |
| 1438 | .ptrs = NULL, |
| 1439 | .data = start, |
| 1440 | }; |
| 1441 | int i, j, err = 0, grp_idx; |
| 1442 | bool grp_used; |
| 1443 | |
| 1444 | /* build index of all strings */ |
| 1445 | while (p < end) { |
| 1446 | if (strs.cnt + 1 > strs.cap) { |
| 1447 | struct btf_str_ptr *new_ptrs; |
| 1448 | |
| 1449 | strs.cap += max(strs.cnt / 2, 16); |
| 1450 | new_ptrs = realloc(strs.ptrs, |
| 1451 | sizeof(strs.ptrs[0]) * strs.cap); |
| 1452 | if (!new_ptrs) { |
| 1453 | err = -ENOMEM; |
| 1454 | goto done; |
| 1455 | } |
| 1456 | strs.ptrs = new_ptrs; |
| 1457 | } |
| 1458 | |
| 1459 | strs.ptrs[strs.cnt].str = p; |
| 1460 | strs.ptrs[strs.cnt].used = false; |
| 1461 | |
| 1462 | p += strlen(p) + 1; |
| 1463 | strs.cnt++; |
| 1464 | } |
| 1465 | |
| 1466 | /* temporary storage for deduplicated strings */ |
| 1467 | tmp_strs = malloc(d->btf->hdr->str_len); |
| 1468 | if (!tmp_strs) { |
| 1469 | err = -ENOMEM; |
| 1470 | goto done; |
| 1471 | } |
| 1472 | |
| 1473 | /* mark all used strings */ |
| 1474 | strs.ptrs[0].used = true; |
| 1475 | err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs); |
| 1476 | if (err) |
| 1477 | goto done; |
| 1478 | |
| 1479 | /* sort strings by context, so that we can identify duplicates */ |
| 1480 | qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content); |
| 1481 | |
| 1482 | /* |
| 1483 | * iterate groups of equal strings and if any instance in a group was |
| 1484 | * referenced, emit single instance and remember new offset |
| 1485 | */ |
| 1486 | p = tmp_strs; |
| 1487 | grp_idx = 0; |
| 1488 | grp_used = strs.ptrs[0].used; |
| 1489 | /* iterate past end to avoid code duplication after loop */ |
| 1490 | for (i = 1; i <= strs.cnt; i++) { |
| 1491 | /* |
| 1492 | * when i == strs.cnt, we want to skip string comparison and go |
| 1493 | * straight to handling last group of strings (otherwise we'd |
| 1494 | * need to handle last group after the loop w/ duplicated code) |
| 1495 | */ |
| 1496 | if (i < strs.cnt && |
| 1497 | !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) { |
| 1498 | grp_used = grp_used || strs.ptrs[i].used; |
| 1499 | continue; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * this check would have been required after the loop to handle |
| 1504 | * last group of strings, but due to <= condition in a loop |
| 1505 | * we avoid that duplication |
| 1506 | */ |
| 1507 | if (grp_used) { |
| 1508 | int new_off = p - tmp_strs; |
| 1509 | __u32 len = strlen(strs.ptrs[grp_idx].str); |
| 1510 | |
| 1511 | memmove(p, strs.ptrs[grp_idx].str, len + 1); |
| 1512 | for (j = grp_idx; j < i; j++) |
| 1513 | strs.ptrs[j].new_off = new_off; |
| 1514 | p += len + 1; |
| 1515 | } |
| 1516 | |
| 1517 | if (i < strs.cnt) { |
| 1518 | grp_idx = i; |
| 1519 | grp_used = strs.ptrs[i].used; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* replace original strings with deduped ones */ |
| 1524 | d->btf->hdr->str_len = p - tmp_strs; |
| 1525 | memmove(start, tmp_strs, d->btf->hdr->str_len); |
| 1526 | end = start + d->btf->hdr->str_len; |
| 1527 | |
| 1528 | /* restore original order for further binary search lookups */ |
| 1529 | qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset); |
| 1530 | |
| 1531 | /* remap string offsets */ |
| 1532 | err = btf_for_each_str_off(d, btf_str_remap_offset, &strs); |
| 1533 | if (err) |
| 1534 | goto done; |
| 1535 | |
| 1536 | d->btf->hdr->str_len = end - start; |
| 1537 | |
| 1538 | done: |
| 1539 | free(tmp_strs); |
| 1540 | free(strs.ptrs); |
| 1541 | return err; |
| 1542 | } |
| 1543 | |
| 1544 | static __u32 btf_hash_common(struct btf_type *t) |
| 1545 | { |
| 1546 | __u32 h; |
| 1547 | |
| 1548 | h = hash_combine(0, t->name_off); |
| 1549 | h = hash_combine(h, t->info); |
| 1550 | h = hash_combine(h, t->size); |
| 1551 | return h; |
| 1552 | } |
| 1553 | |
| 1554 | static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2) |
| 1555 | { |
| 1556 | return t1->name_off == t2->name_off && |
| 1557 | t1->info == t2->info && |
| 1558 | t1->size == t2->size; |
| 1559 | } |
| 1560 | |
| 1561 | /* Calculate type signature hash of INT. */ |
| 1562 | static __u32 btf_hash_int(struct btf_type *t) |
| 1563 | { |
| 1564 | __u32 info = *(__u32 *)(t + 1); |
| 1565 | __u32 h; |
| 1566 | |
| 1567 | h = btf_hash_common(t); |
| 1568 | h = hash_combine(h, info); |
| 1569 | return h; |
| 1570 | } |
| 1571 | |
| 1572 | /* Check structural equality of two INTs. */ |
| 1573 | static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2) |
| 1574 | { |
| 1575 | __u32 info1, info2; |
| 1576 | |
| 1577 | if (!btf_equal_common(t1, t2)) |
| 1578 | return false; |
| 1579 | info1 = *(__u32 *)(t1 + 1); |
| 1580 | info2 = *(__u32 *)(t2 + 1); |
| 1581 | return info1 == info2; |
| 1582 | } |
| 1583 | |
| 1584 | /* Calculate type signature hash of ENUM. */ |
| 1585 | static __u32 btf_hash_enum(struct btf_type *t) |
| 1586 | { |
| 1587 | struct btf_enum *member = (struct btf_enum *)(t + 1); |
| 1588 | __u32 vlen = BTF_INFO_VLEN(t->info); |
| 1589 | __u32 h = btf_hash_common(t); |
| 1590 | int i; |
| 1591 | |
| 1592 | for (i = 0; i < vlen; i++) { |
| 1593 | h = hash_combine(h, member->name_off); |
| 1594 | h = hash_combine(h, member->val); |
| 1595 | member++; |
| 1596 | } |
| 1597 | return h; |
| 1598 | } |
| 1599 | |
| 1600 | /* Check structural equality of two ENUMs. */ |
| 1601 | static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) |
| 1602 | { |
| 1603 | struct btf_enum *m1, *m2; |
| 1604 | __u16 vlen; |
| 1605 | int i; |
| 1606 | |
| 1607 | if (!btf_equal_common(t1, t2)) |
| 1608 | return false; |
| 1609 | |
| 1610 | vlen = BTF_INFO_VLEN(t1->info); |
| 1611 | m1 = (struct btf_enum *)(t1 + 1); |
| 1612 | m2 = (struct btf_enum *)(t2 + 1); |
| 1613 | for (i = 0; i < vlen; i++) { |
| 1614 | if (m1->name_off != m2->name_off || m1->val != m2->val) |
| 1615 | return false; |
| 1616 | m1++; |
| 1617 | m2++; |
| 1618 | } |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs, |
| 1624 | * as referenced type IDs equivalence is established separately during type |
| 1625 | * graph equivalence check algorithm. |
| 1626 | */ |
| 1627 | static __u32 btf_hash_struct(struct btf_type *t) |
| 1628 | { |
| 1629 | struct btf_member *member = (struct btf_member *)(t + 1); |
| 1630 | __u32 vlen = BTF_INFO_VLEN(t->info); |
| 1631 | __u32 h = btf_hash_common(t); |
| 1632 | int i; |
| 1633 | |
| 1634 | for (i = 0; i < vlen; i++) { |
| 1635 | h = hash_combine(h, member->name_off); |
| 1636 | h = hash_combine(h, member->offset); |
| 1637 | /* no hashing of referenced type ID, it can be unresolved yet */ |
| 1638 | member++; |
| 1639 | } |
| 1640 | return h; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type |
| 1645 | * IDs. This check is performed during type graph equivalence check and |
| 1646 | * referenced types equivalence is checked separately. |
| 1647 | */ |
| 1648 | static bool btf_equal_struct(struct btf_type *t1, struct btf_type *t2) |
| 1649 | { |
| 1650 | struct btf_member *m1, *m2; |
| 1651 | __u16 vlen; |
| 1652 | int i; |
| 1653 | |
| 1654 | if (!btf_equal_common(t1, t2)) |
| 1655 | return false; |
| 1656 | |
| 1657 | vlen = BTF_INFO_VLEN(t1->info); |
| 1658 | m1 = (struct btf_member *)(t1 + 1); |
| 1659 | m2 = (struct btf_member *)(t2 + 1); |
| 1660 | for (i = 0; i < vlen; i++) { |
| 1661 | if (m1->name_off != m2->name_off || m1->offset != m2->offset) |
| 1662 | return false; |
| 1663 | m1++; |
| 1664 | m2++; |
| 1665 | } |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Calculate type signature hash of ARRAY, including referenced type IDs, |
| 1671 | * under assumption that they were already resolved to canonical type IDs and |
| 1672 | * are not going to change. |
| 1673 | */ |
| 1674 | static __u32 btf_hash_array(struct btf_type *t) |
| 1675 | { |
| 1676 | struct btf_array *info = (struct btf_array *)(t + 1); |
| 1677 | __u32 h = btf_hash_common(t); |
| 1678 | |
| 1679 | h = hash_combine(h, info->type); |
| 1680 | h = hash_combine(h, info->index_type); |
| 1681 | h = hash_combine(h, info->nelems); |
| 1682 | return h; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | * Check exact equality of two ARRAYs, taking into account referenced |
| 1687 | * type IDs, under assumption that they were already resolved to canonical |
| 1688 | * type IDs and are not going to change. |
| 1689 | * This function is called during reference types deduplication to compare |
| 1690 | * ARRAY to potential canonical representative. |
| 1691 | */ |
| 1692 | static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2) |
| 1693 | { |
| 1694 | struct btf_array *info1, *info2; |
| 1695 | |
| 1696 | if (!btf_equal_common(t1, t2)) |
| 1697 | return false; |
| 1698 | |
| 1699 | info1 = (struct btf_array *)(t1 + 1); |
| 1700 | info2 = (struct btf_array *)(t2 + 1); |
| 1701 | return info1->type == info2->type && |
| 1702 | info1->index_type == info2->index_type && |
| 1703 | info1->nelems == info2->nelems; |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * Check structural compatibility of two ARRAYs, ignoring referenced type |
| 1708 | * IDs. This check is performed during type graph equivalence check and |
| 1709 | * referenced types equivalence is checked separately. |
| 1710 | */ |
| 1711 | static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2) |
| 1712 | { |
| 1713 | struct btf_array *info1, *info2; |
| 1714 | |
| 1715 | if (!btf_equal_common(t1, t2)) |
| 1716 | return false; |
| 1717 | |
| 1718 | info1 = (struct btf_array *)(t1 + 1); |
| 1719 | info2 = (struct btf_array *)(t2 + 1); |
| 1720 | return info1->nelems == info2->nelems; |
| 1721 | } |
| 1722 | |
| 1723 | /* |
| 1724 | * Calculate type signature hash of FUNC_PROTO, including referenced type IDs, |
| 1725 | * under assumption that they were already resolved to canonical type IDs and |
| 1726 | * are not going to change. |
| 1727 | */ |
| 1728 | static inline __u32 btf_hash_fnproto(struct btf_type *t) |
| 1729 | { |
| 1730 | struct btf_param *member = (struct btf_param *)(t + 1); |
| 1731 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 1732 | __u32 h = btf_hash_common(t); |
| 1733 | int i; |
| 1734 | |
| 1735 | for (i = 0; i < vlen; i++) { |
| 1736 | h = hash_combine(h, member->name_off); |
| 1737 | h = hash_combine(h, member->type); |
| 1738 | member++; |
| 1739 | } |
| 1740 | return h; |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * Check exact equality of two FUNC_PROTOs, taking into account referenced |
| 1745 | * type IDs, under assumption that they were already resolved to canonical |
| 1746 | * type IDs and are not going to change. |
| 1747 | * This function is called during reference types deduplication to compare |
| 1748 | * FUNC_PROTO to potential canonical representative. |
| 1749 | */ |
| 1750 | static inline bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2) |
| 1751 | { |
| 1752 | struct btf_param *m1, *m2; |
| 1753 | __u16 vlen; |
| 1754 | int i; |
| 1755 | |
| 1756 | if (!btf_equal_common(t1, t2)) |
| 1757 | return false; |
| 1758 | |
| 1759 | vlen = BTF_INFO_VLEN(t1->info); |
| 1760 | m1 = (struct btf_param *)(t1 + 1); |
| 1761 | m2 = (struct btf_param *)(t2 + 1); |
| 1762 | for (i = 0; i < vlen; i++) { |
| 1763 | if (m1->name_off != m2->name_off || m1->type != m2->type) |
| 1764 | return false; |
| 1765 | m1++; |
| 1766 | m2++; |
| 1767 | } |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
| 1772 | * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type |
| 1773 | * IDs. This check is performed during type graph equivalence check and |
| 1774 | * referenced types equivalence is checked separately. |
| 1775 | */ |
| 1776 | static inline bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2) |
| 1777 | { |
| 1778 | struct btf_param *m1, *m2; |
| 1779 | __u16 vlen; |
| 1780 | int i; |
| 1781 | |
| 1782 | /* skip return type ID */ |
| 1783 | if (t1->name_off != t2->name_off || t1->info != t2->info) |
| 1784 | return false; |
| 1785 | |
| 1786 | vlen = BTF_INFO_VLEN(t1->info); |
| 1787 | m1 = (struct btf_param *)(t1 + 1); |
| 1788 | m2 = (struct btf_param *)(t2 + 1); |
| 1789 | for (i = 0; i < vlen; i++) { |
| 1790 | if (m1->name_off != m2->name_off) |
| 1791 | return false; |
| 1792 | m1++; |
| 1793 | m2++; |
| 1794 | } |
| 1795 | return true; |
| 1796 | } |
| 1797 | |
| 1798 | /* |
| 1799 | * Deduplicate primitive types, that can't reference other types, by calculating |
| 1800 | * their type signature hash and comparing them with any possible canonical |
| 1801 | * candidate. If no canonical candidate matches, type itself is marked as |
| 1802 | * canonical and is added into `btf_dedup->dedup_table` as another candidate. |
| 1803 | */ |
| 1804 | static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) |
| 1805 | { |
| 1806 | struct btf_type *t = d->btf->types[type_id]; |
| 1807 | struct btf_type *cand; |
| 1808 | struct btf_dedup_node *cand_node; |
| 1809 | /* if we don't find equivalent type, then we are canonical */ |
| 1810 | __u32 new_id = type_id; |
| 1811 | __u32 h; |
| 1812 | |
| 1813 | switch (BTF_INFO_KIND(t->info)) { |
| 1814 | case BTF_KIND_CONST: |
| 1815 | case BTF_KIND_VOLATILE: |
| 1816 | case BTF_KIND_RESTRICT: |
| 1817 | case BTF_KIND_PTR: |
| 1818 | case BTF_KIND_TYPEDEF: |
| 1819 | case BTF_KIND_ARRAY: |
| 1820 | case BTF_KIND_STRUCT: |
| 1821 | case BTF_KIND_UNION: |
| 1822 | case BTF_KIND_FUNC: |
| 1823 | case BTF_KIND_FUNC_PROTO: |
| 1824 | return 0; |
| 1825 | |
| 1826 | case BTF_KIND_INT: |
| 1827 | h = btf_hash_int(t); |
| 1828 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 1829 | cand = d->btf->types[cand_node->type_id]; |
| 1830 | if (btf_equal_int(t, cand)) { |
| 1831 | new_id = cand_node->type_id; |
| 1832 | break; |
| 1833 | } |
| 1834 | } |
| 1835 | break; |
| 1836 | |
| 1837 | case BTF_KIND_ENUM: |
| 1838 | h = btf_hash_enum(t); |
| 1839 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 1840 | cand = d->btf->types[cand_node->type_id]; |
| 1841 | if (btf_equal_enum(t, cand)) { |
| 1842 | new_id = cand_node->type_id; |
| 1843 | break; |
| 1844 | } |
| 1845 | } |
| 1846 | break; |
| 1847 | |
| 1848 | case BTF_KIND_FWD: |
| 1849 | h = btf_hash_common(t); |
| 1850 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 1851 | cand = d->btf->types[cand_node->type_id]; |
| 1852 | if (btf_equal_common(t, cand)) { |
| 1853 | new_id = cand_node->type_id; |
| 1854 | break; |
| 1855 | } |
| 1856 | } |
| 1857 | break; |
| 1858 | |
| 1859 | default: |
| 1860 | return -EINVAL; |
| 1861 | } |
| 1862 | |
| 1863 | d->map[type_id] = new_id; |
| 1864 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 1865 | return -ENOMEM; |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
| 1870 | static int btf_dedup_prim_types(struct btf_dedup *d) |
| 1871 | { |
| 1872 | int i, err; |
| 1873 | |
| 1874 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 1875 | err = btf_dedup_prim_type(d, i); |
| 1876 | if (err) |
| 1877 | return err; |
| 1878 | } |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Check whether type is already mapped into canonical one (could be to itself). |
| 1884 | */ |
| 1885 | static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id) |
| 1886 | { |
| 1887 | return d->map[type_id] <= BTF_MAX_TYPE; |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Resolve type ID into its canonical type ID, if any; otherwise return original |
| 1892 | * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow |
| 1893 | * STRUCT/UNION link and resolve it into canonical type ID as well. |
| 1894 | */ |
| 1895 | static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id) |
| 1896 | { |
| 1897 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
| 1898 | type_id = d->map[type_id]; |
| 1899 | return type_id; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original |
| 1904 | * type ID. |
| 1905 | */ |
| 1906 | static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id) |
| 1907 | { |
| 1908 | __u32 orig_type_id = type_id; |
| 1909 | |
| 1910 | if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD) |
| 1911 | return type_id; |
| 1912 | |
| 1913 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
| 1914 | type_id = d->map[type_id]; |
| 1915 | |
| 1916 | if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD) |
| 1917 | return type_id; |
| 1918 | |
| 1919 | return orig_type_id; |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | static inline __u16 btf_fwd_kind(struct btf_type *t) |
| 1924 | { |
| 1925 | return BTF_INFO_KFLAG(t->info) ? BTF_KIND_UNION : BTF_KIND_STRUCT; |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Check equivalence of BTF type graph formed by candidate struct/union (we'll |
| 1930 | * call it "candidate graph" in this description for brevity) to a type graph |
| 1931 | * formed by (potential) canonical struct/union ("canonical graph" for brevity |
| 1932 | * here, though keep in mind that not all types in canonical graph are |
| 1933 | * necessarily canonical representatives themselves, some of them might be |
| 1934 | * duplicates or its uniqueness might not have been established yet). |
| 1935 | * Returns: |
| 1936 | * - >0, if type graphs are equivalent; |
| 1937 | * - 0, if not equivalent; |
| 1938 | * - <0, on error. |
| 1939 | * |
| 1940 | * Algorithm performs side-by-side DFS traversal of both type graphs and checks |
| 1941 | * equivalence of BTF types at each step. If at any point BTF types in candidate |
| 1942 | * and canonical graphs are not compatible structurally, whole graphs are |
| 1943 | * incompatible. If types are structurally equivalent (i.e., all information |
| 1944 | * except referenced type IDs is exactly the same), a mapping from `canon_id` to |
| 1945 | * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`). |
| 1946 | * If a type references other types, then those referenced types are checked |
| 1947 | * for equivalence recursively. |
| 1948 | * |
| 1949 | * During DFS traversal, if we find that for current `canon_id` type we |
| 1950 | * already have some mapping in hypothetical map, we check for two possible |
| 1951 | * situations: |
| 1952 | * - `canon_id` is mapped to exactly the same type as `cand_id`. This will |
| 1953 | * happen when type graphs have cycles. In this case we assume those two |
| 1954 | * types are equivalent. |
| 1955 | * - `canon_id` is mapped to different type. This is contradiction in our |
| 1956 | * hypothetical mapping, because same graph in canonical graph corresponds |
| 1957 | * to two different types in candidate graph, which for equivalent type |
| 1958 | * graphs shouldn't happen. This condition terminates equivalence check |
| 1959 | * with negative result. |
| 1960 | * |
| 1961 | * If type graphs traversal exhausts types to check and find no contradiction, |
| 1962 | * then type graphs are equivalent. |
| 1963 | * |
| 1964 | * When checking types for equivalence, there is one special case: FWD types. |
| 1965 | * If FWD type resolution is allowed and one of the types (either from canonical |
| 1966 | * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind |
| 1967 | * flag) and their names match, hypothetical mapping is updated to point from |
| 1968 | * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully, |
| 1969 | * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently. |
| 1970 | * |
| 1971 | * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution, |
| 1972 | * if there are two exactly named (or anonymous) structs/unions that are |
| 1973 | * compatible structurally, one of which has FWD field, while other is concrete |
| 1974 | * STRUCT/UNION, but according to C sources they are different structs/unions |
| 1975 | * that are referencing different types with the same name. This is extremely |
| 1976 | * unlikely to happen, but btf_dedup API allows to disable FWD resolution if |
| 1977 | * this logic is causing problems. |
| 1978 | * |
| 1979 | * Doing FWD resolution means that both candidate and/or canonical graphs can |
| 1980 | * consists of portions of the graph that come from multiple compilation units. |
| 1981 | * This is due to the fact that types within single compilation unit are always |
| 1982 | * deduplicated and FWDs are already resolved, if referenced struct/union |
| 1983 | * definiton is available. So, if we had unresolved FWD and found corresponding |
| 1984 | * STRUCT/UNION, they will be from different compilation units. This |
| 1985 | * consequently means that when we "link" FWD to corresponding STRUCT/UNION, |
| 1986 | * type graph will likely have at least two different BTF types that describe |
| 1987 | * same type (e.g., most probably there will be two different BTF types for the |
| 1988 | * same 'int' primitive type) and could even have "overlapping" parts of type |
| 1989 | * graph that describe same subset of types. |
| 1990 | * |
| 1991 | * This in turn means that our assumption that each type in canonical graph |
| 1992 | * must correspond to exactly one type in candidate graph might not hold |
| 1993 | * anymore and will make it harder to detect contradictions using hypothetical |
| 1994 | * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION |
| 1995 | * resolution only in canonical graph. FWDs in candidate graphs are never |
| 1996 | * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs |
| 1997 | * that can occur: |
| 1998 | * - Both types in canonical and candidate graphs are FWDs. If they are |
| 1999 | * structurally equivalent, then they can either be both resolved to the |
| 2000 | * same STRUCT/UNION or not resolved at all. In both cases they are |
| 2001 | * equivalent and there is no need to resolve FWD on candidate side. |
| 2002 | * - Both types in canonical and candidate graphs are concrete STRUCT/UNION, |
| 2003 | * so nothing to resolve as well, algorithm will check equivalence anyway. |
| 2004 | * - Type in canonical graph is FWD, while type in candidate is concrete |
| 2005 | * STRUCT/UNION. In this case candidate graph comes from single compilation |
| 2006 | * unit, so there is exactly one BTF type for each unique C type. After |
| 2007 | * resolving FWD into STRUCT/UNION, there might be more than one BTF type |
| 2008 | * in canonical graph mapping to single BTF type in candidate graph, but |
| 2009 | * because hypothetical mapping maps from canonical to candidate types, it's |
| 2010 | * alright, and we still maintain the property of having single `canon_id` |
| 2011 | * mapping to single `cand_id` (there could be two different `canon_id` |
| 2012 | * mapped to the same `cand_id`, but it's not contradictory). |
| 2013 | * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate |
| 2014 | * graph is FWD. In this case we are just going to check compatibility of |
| 2015 | * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll |
| 2016 | * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to |
| 2017 | * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs |
| 2018 | * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from |
| 2019 | * canonical graph. |
| 2020 | */ |
| 2021 | static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, |
| 2022 | __u32 canon_id) |
| 2023 | { |
| 2024 | struct btf_type *cand_type; |
| 2025 | struct btf_type *canon_type; |
| 2026 | __u32 hypot_type_id; |
| 2027 | __u16 cand_kind; |
| 2028 | __u16 canon_kind; |
| 2029 | int i, eq; |
| 2030 | |
| 2031 | /* if both resolve to the same canonical, they must be equivalent */ |
| 2032 | if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id)) |
| 2033 | return 1; |
| 2034 | |
| 2035 | canon_id = resolve_fwd_id(d, canon_id); |
| 2036 | |
| 2037 | hypot_type_id = d->hypot_map[canon_id]; |
| 2038 | if (hypot_type_id <= BTF_MAX_TYPE) |
| 2039 | return hypot_type_id == cand_id; |
| 2040 | |
| 2041 | if (btf_dedup_hypot_map_add(d, canon_id, cand_id)) |
| 2042 | return -ENOMEM; |
| 2043 | |
| 2044 | cand_type = d->btf->types[cand_id]; |
| 2045 | canon_type = d->btf->types[canon_id]; |
| 2046 | cand_kind = BTF_INFO_KIND(cand_type->info); |
| 2047 | canon_kind = BTF_INFO_KIND(canon_type->info); |
| 2048 | |
| 2049 | if (cand_type->name_off != canon_type->name_off) |
| 2050 | return 0; |
| 2051 | |
| 2052 | /* FWD <--> STRUCT/UNION equivalence check, if enabled */ |
| 2053 | if (!d->opts.dont_resolve_fwds |
| 2054 | && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD) |
| 2055 | && cand_kind != canon_kind) { |
| 2056 | __u16 real_kind; |
| 2057 | __u16 fwd_kind; |
| 2058 | |
| 2059 | if (cand_kind == BTF_KIND_FWD) { |
| 2060 | real_kind = canon_kind; |
| 2061 | fwd_kind = btf_fwd_kind(cand_type); |
| 2062 | } else { |
| 2063 | real_kind = cand_kind; |
| 2064 | fwd_kind = btf_fwd_kind(canon_type); |
| 2065 | } |
| 2066 | return fwd_kind == real_kind; |
| 2067 | } |
| 2068 | |
| 2069 | if (cand_type->info != canon_type->info) |
| 2070 | return 0; |
| 2071 | |
| 2072 | switch (cand_kind) { |
| 2073 | case BTF_KIND_INT: |
| 2074 | return btf_equal_int(cand_type, canon_type); |
| 2075 | |
| 2076 | case BTF_KIND_ENUM: |
| 2077 | return btf_equal_enum(cand_type, canon_type); |
| 2078 | |
| 2079 | case BTF_KIND_FWD: |
| 2080 | return btf_equal_common(cand_type, canon_type); |
| 2081 | |
| 2082 | case BTF_KIND_CONST: |
| 2083 | case BTF_KIND_VOLATILE: |
| 2084 | case BTF_KIND_RESTRICT: |
| 2085 | case BTF_KIND_PTR: |
| 2086 | case BTF_KIND_TYPEDEF: |
| 2087 | case BTF_KIND_FUNC: |
| 2088 | return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
| 2089 | |
| 2090 | case BTF_KIND_ARRAY: { |
| 2091 | struct btf_array *cand_arr, *canon_arr; |
| 2092 | |
| 2093 | if (!btf_compat_array(cand_type, canon_type)) |
| 2094 | return 0; |
| 2095 | cand_arr = (struct btf_array *)(cand_type + 1); |
| 2096 | canon_arr = (struct btf_array *)(canon_type + 1); |
| 2097 | eq = btf_dedup_is_equiv(d, |
| 2098 | cand_arr->index_type, canon_arr->index_type); |
| 2099 | if (eq <= 0) |
| 2100 | return eq; |
| 2101 | return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); |
| 2102 | } |
| 2103 | |
| 2104 | case BTF_KIND_STRUCT: |
| 2105 | case BTF_KIND_UNION: { |
| 2106 | struct btf_member *cand_m, *canon_m; |
| 2107 | __u16 vlen; |
| 2108 | |
| 2109 | if (!btf_equal_struct(cand_type, canon_type)) |
| 2110 | return 0; |
| 2111 | vlen = BTF_INFO_VLEN(cand_type->info); |
| 2112 | cand_m = (struct btf_member *)(cand_type + 1); |
| 2113 | canon_m = (struct btf_member *)(canon_type + 1); |
| 2114 | for (i = 0; i < vlen; i++) { |
| 2115 | eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); |
| 2116 | if (eq <= 0) |
| 2117 | return eq; |
| 2118 | cand_m++; |
| 2119 | canon_m++; |
| 2120 | } |
| 2121 | |
| 2122 | return 1; |
| 2123 | } |
| 2124 | |
| 2125 | case BTF_KIND_FUNC_PROTO: { |
| 2126 | struct btf_param *cand_p, *canon_p; |
| 2127 | __u16 vlen; |
| 2128 | |
| 2129 | if (!btf_compat_fnproto(cand_type, canon_type)) |
| 2130 | return 0; |
| 2131 | eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
| 2132 | if (eq <= 0) |
| 2133 | return eq; |
| 2134 | vlen = BTF_INFO_VLEN(cand_type->info); |
| 2135 | cand_p = (struct btf_param *)(cand_type + 1); |
| 2136 | canon_p = (struct btf_param *)(canon_type + 1); |
| 2137 | for (i = 0; i < vlen; i++) { |
| 2138 | eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); |
| 2139 | if (eq <= 0) |
| 2140 | return eq; |
| 2141 | cand_p++; |
| 2142 | canon_p++; |
| 2143 | } |
| 2144 | return 1; |
| 2145 | } |
| 2146 | |
| 2147 | default: |
| 2148 | return -EINVAL; |
| 2149 | } |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * Use hypothetical mapping, produced by successful type graph equivalence |
| 2155 | * check, to augment existing struct/union canonical mapping, where possible. |
| 2156 | * |
| 2157 | * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record |
| 2158 | * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional: |
| 2159 | * it doesn't matter if FWD type was part of canonical graph or candidate one, |
| 2160 | * we are recording the mapping anyway. As opposed to carefulness required |
| 2161 | * for struct/union correspondence mapping (described below), for FWD resolution |
| 2162 | * it's not important, as by the time that FWD type (reference type) will be |
| 2163 | * deduplicated all structs/unions will be deduped already anyway. |
| 2164 | * |
| 2165 | * Recording STRUCT/UNION mapping is purely a performance optimization and is |
| 2166 | * not required for correctness. It needs to be done carefully to ensure that |
| 2167 | * struct/union from candidate's type graph is not mapped into corresponding |
| 2168 | * struct/union from canonical type graph that itself hasn't been resolved into |
| 2169 | * canonical representative. The only guarantee we have is that canonical |
| 2170 | * struct/union was determined as canonical and that won't change. But any |
| 2171 | * types referenced through that struct/union fields could have been not yet |
| 2172 | * resolved, so in case like that it's too early to establish any kind of |
| 2173 | * correspondence between structs/unions. |
| 2174 | * |
| 2175 | * No canonical correspondence is derived for primitive types (they are already |
| 2176 | * deduplicated completely already anyway) or reference types (they rely on |
| 2177 | * stability of struct/union canonical relationship for equivalence checks). |
| 2178 | */ |
| 2179 | static void btf_dedup_merge_hypot_map(struct btf_dedup *d) |
| 2180 | { |
| 2181 | __u32 cand_type_id, targ_type_id; |
| 2182 | __u16 t_kind, c_kind; |
| 2183 | __u32 t_id, c_id; |
| 2184 | int i; |
| 2185 | |
| 2186 | for (i = 0; i < d->hypot_cnt; i++) { |
| 2187 | cand_type_id = d->hypot_list[i]; |
| 2188 | targ_type_id = d->hypot_map[cand_type_id]; |
| 2189 | t_id = resolve_type_id(d, targ_type_id); |
| 2190 | c_id = resolve_type_id(d, cand_type_id); |
| 2191 | t_kind = BTF_INFO_KIND(d->btf->types[t_id]->info); |
| 2192 | c_kind = BTF_INFO_KIND(d->btf->types[c_id]->info); |
| 2193 | /* |
| 2194 | * Resolve FWD into STRUCT/UNION. |
| 2195 | * It's ok to resolve FWD into STRUCT/UNION that's not yet |
| 2196 | * mapped to canonical representative (as opposed to |
| 2197 | * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because |
| 2198 | * eventually that struct is going to be mapped and all resolved |
| 2199 | * FWDs will automatically resolve to correct canonical |
| 2200 | * representative. This will happen before ref type deduping, |
| 2201 | * which critically depends on stability of these mapping. This |
| 2202 | * stability is not a requirement for STRUCT/UNION equivalence |
| 2203 | * checks, though. |
| 2204 | */ |
| 2205 | if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD) |
| 2206 | d->map[c_id] = t_id; |
| 2207 | else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD) |
| 2208 | d->map[t_id] = c_id; |
| 2209 | |
| 2210 | if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) && |
| 2211 | c_kind != BTF_KIND_FWD && |
| 2212 | is_type_mapped(d, c_id) && |
| 2213 | !is_type_mapped(d, t_id)) { |
| 2214 | /* |
| 2215 | * as a perf optimization, we can map struct/union |
| 2216 | * that's part of type graph we just verified for |
| 2217 | * equivalence. We can do that for struct/union that has |
| 2218 | * canonical representative only, though. |
| 2219 | */ |
| 2220 | d->map[t_id] = c_id; |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * Deduplicate struct/union types. |
| 2227 | * |
| 2228 | * For each struct/union type its type signature hash is calculated, taking |
| 2229 | * into account type's name, size, number, order and names of fields, but |
| 2230 | * ignoring type ID's referenced from fields, because they might not be deduped |
| 2231 | * completely until after reference types deduplication phase. This type hash |
| 2232 | * is used to iterate over all potential canonical types, sharing same hash. |
| 2233 | * For each canonical candidate we check whether type graphs that they form |
| 2234 | * (through referenced types in fields and so on) are equivalent using algorithm |
| 2235 | * implemented in `btf_dedup_is_equiv`. If such equivalence is found and |
| 2236 | * BTF_KIND_FWD resolution is allowed, then hypothetical mapping |
| 2237 | * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence |
| 2238 | * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to |
| 2239 | * potentially map other structs/unions to their canonical representatives, |
| 2240 | * if such relationship hasn't yet been established. This speeds up algorithm |
| 2241 | * by eliminating some of the duplicate work. |
| 2242 | * |
| 2243 | * If no matching canonical representative was found, struct/union is marked |
| 2244 | * as canonical for itself and is added into btf_dedup->dedup_table hash map |
| 2245 | * for further look ups. |
| 2246 | */ |
| 2247 | static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id) |
| 2248 | { |
| 2249 | struct btf_dedup_node *cand_node; |
| 2250 | struct btf_type *t; |
| 2251 | /* if we don't find equivalent type, then we are canonical */ |
| 2252 | __u32 new_id = type_id; |
| 2253 | __u16 kind; |
| 2254 | __u32 h; |
| 2255 | |
| 2256 | /* already deduped or is in process of deduping (loop detected) */ |
| 2257 | if (d->map[type_id] <= BTF_MAX_TYPE) |
| 2258 | return 0; |
| 2259 | |
| 2260 | t = d->btf->types[type_id]; |
| 2261 | kind = BTF_INFO_KIND(t->info); |
| 2262 | |
| 2263 | if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION) |
| 2264 | return 0; |
| 2265 | |
| 2266 | h = btf_hash_struct(t); |
| 2267 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 2268 | int eq; |
| 2269 | |
| 2270 | btf_dedup_clear_hypot_map(d); |
| 2271 | eq = btf_dedup_is_equiv(d, type_id, cand_node->type_id); |
| 2272 | if (eq < 0) |
| 2273 | return eq; |
| 2274 | if (!eq) |
| 2275 | continue; |
| 2276 | new_id = cand_node->type_id; |
| 2277 | btf_dedup_merge_hypot_map(d); |
| 2278 | break; |
| 2279 | } |
| 2280 | |
| 2281 | d->map[type_id] = new_id; |
| 2282 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 2283 | return -ENOMEM; |
| 2284 | |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | static int btf_dedup_struct_types(struct btf_dedup *d) |
| 2289 | { |
| 2290 | int i, err; |
| 2291 | |
| 2292 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2293 | err = btf_dedup_struct_type(d, i); |
| 2294 | if (err) |
| 2295 | return err; |
| 2296 | } |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * Deduplicate reference type. |
| 2302 | * |
| 2303 | * Once all primitive and struct/union types got deduplicated, we can easily |
| 2304 | * deduplicate all other (reference) BTF types. This is done in two steps: |
| 2305 | * |
| 2306 | * 1. Resolve all referenced type IDs into their canonical type IDs. This |
| 2307 | * resolution can be done either immediately for primitive or struct/union types |
| 2308 | * (because they were deduped in previous two phases) or recursively for |
| 2309 | * reference types. Recursion will always terminate at either primitive or |
| 2310 | * struct/union type, at which point we can "unwind" chain of reference types |
| 2311 | * one by one. There is no danger of encountering cycles because in C type |
| 2312 | * system the only way to form type cycle is through struct/union, so any chain |
| 2313 | * of reference types, even those taking part in a type cycle, will inevitably |
| 2314 | * reach struct/union at some point. |
| 2315 | * |
| 2316 | * 2. Once all referenced type IDs are resolved into canonical ones, BTF type |
| 2317 | * becomes "stable", in the sense that no further deduplication will cause |
| 2318 | * any changes to it. With that, it's now possible to calculate type's signature |
| 2319 | * hash (this time taking into account referenced type IDs) and loop over all |
| 2320 | * potential canonical representatives. If no match was found, current type |
| 2321 | * will become canonical representative of itself and will be added into |
| 2322 | * btf_dedup->dedup_table as another possible canonical representative. |
| 2323 | */ |
| 2324 | static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id) |
| 2325 | { |
| 2326 | struct btf_dedup_node *cand_node; |
| 2327 | struct btf_type *t, *cand; |
| 2328 | /* if we don't find equivalent type, then we are representative type */ |
| 2329 | __u32 new_id = type_id; |
| 2330 | __u32 h, ref_type_id; |
| 2331 | |
| 2332 | if (d->map[type_id] == BTF_IN_PROGRESS_ID) |
| 2333 | return -ELOOP; |
| 2334 | if (d->map[type_id] <= BTF_MAX_TYPE) |
| 2335 | return resolve_type_id(d, type_id); |
| 2336 | |
| 2337 | t = d->btf->types[type_id]; |
| 2338 | d->map[type_id] = BTF_IN_PROGRESS_ID; |
| 2339 | |
| 2340 | switch (BTF_INFO_KIND(t->info)) { |
| 2341 | case BTF_KIND_CONST: |
| 2342 | case BTF_KIND_VOLATILE: |
| 2343 | case BTF_KIND_RESTRICT: |
| 2344 | case BTF_KIND_PTR: |
| 2345 | case BTF_KIND_TYPEDEF: |
| 2346 | case BTF_KIND_FUNC: |
| 2347 | ref_type_id = btf_dedup_ref_type(d, t->type); |
| 2348 | if (ref_type_id < 0) |
| 2349 | return ref_type_id; |
| 2350 | t->type = ref_type_id; |
| 2351 | |
| 2352 | h = btf_hash_common(t); |
| 2353 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 2354 | cand = d->btf->types[cand_node->type_id]; |
| 2355 | if (btf_equal_common(t, cand)) { |
| 2356 | new_id = cand_node->type_id; |
| 2357 | break; |
| 2358 | } |
| 2359 | } |
| 2360 | break; |
| 2361 | |
| 2362 | case BTF_KIND_ARRAY: { |
| 2363 | struct btf_array *info = (struct btf_array *)(t + 1); |
| 2364 | |
| 2365 | ref_type_id = btf_dedup_ref_type(d, info->type); |
| 2366 | if (ref_type_id < 0) |
| 2367 | return ref_type_id; |
| 2368 | info->type = ref_type_id; |
| 2369 | |
| 2370 | ref_type_id = btf_dedup_ref_type(d, info->index_type); |
| 2371 | if (ref_type_id < 0) |
| 2372 | return ref_type_id; |
| 2373 | info->index_type = ref_type_id; |
| 2374 | |
| 2375 | h = btf_hash_array(t); |
| 2376 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 2377 | cand = d->btf->types[cand_node->type_id]; |
| 2378 | if (btf_equal_array(t, cand)) { |
| 2379 | new_id = cand_node->type_id; |
| 2380 | break; |
| 2381 | } |
| 2382 | } |
| 2383 | break; |
| 2384 | } |
| 2385 | |
| 2386 | case BTF_KIND_FUNC_PROTO: { |
| 2387 | struct btf_param *param; |
| 2388 | __u16 vlen; |
| 2389 | int i; |
| 2390 | |
| 2391 | ref_type_id = btf_dedup_ref_type(d, t->type); |
| 2392 | if (ref_type_id < 0) |
| 2393 | return ref_type_id; |
| 2394 | t->type = ref_type_id; |
| 2395 | |
| 2396 | vlen = BTF_INFO_VLEN(t->info); |
| 2397 | param = (struct btf_param *)(t + 1); |
| 2398 | for (i = 0; i < vlen; i++) { |
| 2399 | ref_type_id = btf_dedup_ref_type(d, param->type); |
| 2400 | if (ref_type_id < 0) |
| 2401 | return ref_type_id; |
| 2402 | param->type = ref_type_id; |
| 2403 | param++; |
| 2404 | } |
| 2405 | |
| 2406 | h = btf_hash_fnproto(t); |
| 2407 | for_each_hash_node(d->dedup_table, h, cand_node) { |
| 2408 | cand = d->btf->types[cand_node->type_id]; |
| 2409 | if (btf_equal_fnproto(t, cand)) { |
| 2410 | new_id = cand_node->type_id; |
| 2411 | break; |
| 2412 | } |
| 2413 | } |
| 2414 | break; |
| 2415 | } |
| 2416 | |
| 2417 | default: |
| 2418 | return -EINVAL; |
| 2419 | } |
| 2420 | |
| 2421 | d->map[type_id] = new_id; |
| 2422 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 2423 | return -ENOMEM; |
| 2424 | |
| 2425 | return new_id; |
| 2426 | } |
| 2427 | |
| 2428 | static int btf_dedup_ref_types(struct btf_dedup *d) |
| 2429 | { |
| 2430 | int i, err; |
| 2431 | |
| 2432 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2433 | err = btf_dedup_ref_type(d, i); |
| 2434 | if (err < 0) |
| 2435 | return err; |
| 2436 | } |
| 2437 | btf_dedup_table_free(d); |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
| 2442 | * Compact types. |
| 2443 | * |
| 2444 | * After we established for each type its corresponding canonical representative |
| 2445 | * type, we now can eliminate types that are not canonical and leave only |
| 2446 | * canonical ones layed out sequentially in memory by copying them over |
| 2447 | * duplicates. During compaction btf_dedup->hypot_map array is reused to store |
| 2448 | * a map from original type ID to a new compacted type ID, which will be used |
| 2449 | * during next phase to "fix up" type IDs, referenced from struct/union and |
| 2450 | * reference types. |
| 2451 | */ |
| 2452 | static int btf_dedup_compact_types(struct btf_dedup *d) |
| 2453 | { |
| 2454 | struct btf_type **new_types; |
| 2455 | __u32 next_type_id = 1; |
| 2456 | char *types_start, *p; |
| 2457 | int i, len; |
| 2458 | |
| 2459 | /* we are going to reuse hypot_map to store compaction remapping */ |
| 2460 | d->hypot_map[0] = 0; |
| 2461 | for (i = 1; i <= d->btf->nr_types; i++) |
| 2462 | d->hypot_map[i] = BTF_UNPROCESSED_ID; |
| 2463 | |
| 2464 | types_start = d->btf->nohdr_data + d->btf->hdr->type_off; |
| 2465 | p = types_start; |
| 2466 | |
| 2467 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2468 | if (d->map[i] != i) |
| 2469 | continue; |
| 2470 | |
| 2471 | len = btf_type_size(d->btf->types[i]); |
| 2472 | if (len < 0) |
| 2473 | return len; |
| 2474 | |
| 2475 | memmove(p, d->btf->types[i], len); |
| 2476 | d->hypot_map[i] = next_type_id; |
| 2477 | d->btf->types[next_type_id] = (struct btf_type *)p; |
| 2478 | p += len; |
| 2479 | next_type_id++; |
| 2480 | } |
| 2481 | |
| 2482 | /* shrink struct btf's internal types index and update btf_header */ |
| 2483 | d->btf->nr_types = next_type_id - 1; |
| 2484 | d->btf->types_size = d->btf->nr_types; |
| 2485 | d->btf->hdr->type_len = p - types_start; |
| 2486 | new_types = realloc(d->btf->types, |
| 2487 | (1 + d->btf->nr_types) * sizeof(struct btf_type *)); |
| 2488 | if (!new_types) |
| 2489 | return -ENOMEM; |
| 2490 | d->btf->types = new_types; |
| 2491 | |
| 2492 | /* make sure string section follows type information without gaps */ |
| 2493 | d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data; |
| 2494 | memmove(p, d->btf->strings, d->btf->hdr->str_len); |
| 2495 | d->btf->strings = p; |
| 2496 | p += d->btf->hdr->str_len; |
| 2497 | |
| 2498 | d->btf->data_size = p - (char *)d->btf->data; |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
| 2502 | /* |
| 2503 | * Figure out final (deduplicated and compacted) type ID for provided original |
| 2504 | * `type_id` by first resolving it into corresponding canonical type ID and |
| 2505 | * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map, |
| 2506 | * which is populated during compaction phase. |
| 2507 | */ |
| 2508 | static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id) |
| 2509 | { |
| 2510 | __u32 resolved_type_id, new_type_id; |
| 2511 | |
| 2512 | resolved_type_id = resolve_type_id(d, type_id); |
| 2513 | new_type_id = d->hypot_map[resolved_type_id]; |
| 2514 | if (new_type_id > BTF_MAX_TYPE) |
| 2515 | return -EINVAL; |
| 2516 | return new_type_id; |
| 2517 | } |
| 2518 | |
| 2519 | /* |
| 2520 | * Remap referenced type IDs into deduped type IDs. |
| 2521 | * |
| 2522 | * After BTF types are deduplicated and compacted, their final type IDs may |
| 2523 | * differ from original ones. The map from original to a corresponding |
| 2524 | * deduped type ID is stored in btf_dedup->hypot_map and is populated during |
| 2525 | * compaction phase. During remapping phase we are rewriting all type IDs |
| 2526 | * referenced from any BTF type (e.g., struct fields, func proto args, etc) to |
| 2527 | * their final deduped type IDs. |
| 2528 | */ |
| 2529 | static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id) |
| 2530 | { |
| 2531 | struct btf_type *t = d->btf->types[type_id]; |
| 2532 | int i, r; |
| 2533 | |
| 2534 | switch (BTF_INFO_KIND(t->info)) { |
| 2535 | case BTF_KIND_INT: |
| 2536 | case BTF_KIND_ENUM: |
| 2537 | break; |
| 2538 | |
| 2539 | case BTF_KIND_FWD: |
| 2540 | case BTF_KIND_CONST: |
| 2541 | case BTF_KIND_VOLATILE: |
| 2542 | case BTF_KIND_RESTRICT: |
| 2543 | case BTF_KIND_PTR: |
| 2544 | case BTF_KIND_TYPEDEF: |
| 2545 | case BTF_KIND_FUNC: |
| 2546 | r = btf_dedup_remap_type_id(d, t->type); |
| 2547 | if (r < 0) |
| 2548 | return r; |
| 2549 | t->type = r; |
| 2550 | break; |
| 2551 | |
| 2552 | case BTF_KIND_ARRAY: { |
| 2553 | struct btf_array *arr_info = (struct btf_array *)(t + 1); |
| 2554 | |
| 2555 | r = btf_dedup_remap_type_id(d, arr_info->type); |
| 2556 | if (r < 0) |
| 2557 | return r; |
| 2558 | arr_info->type = r; |
| 2559 | r = btf_dedup_remap_type_id(d, arr_info->index_type); |
| 2560 | if (r < 0) |
| 2561 | return r; |
| 2562 | arr_info->index_type = r; |
| 2563 | break; |
| 2564 | } |
| 2565 | |
| 2566 | case BTF_KIND_STRUCT: |
| 2567 | case BTF_KIND_UNION: { |
| 2568 | struct btf_member *member = (struct btf_member *)(t + 1); |
| 2569 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 2570 | |
| 2571 | for (i = 0; i < vlen; i++) { |
| 2572 | r = btf_dedup_remap_type_id(d, member->type); |
| 2573 | if (r < 0) |
| 2574 | return r; |
| 2575 | member->type = r; |
| 2576 | member++; |
| 2577 | } |
| 2578 | break; |
| 2579 | } |
| 2580 | |
| 2581 | case BTF_KIND_FUNC_PROTO: { |
| 2582 | struct btf_param *param = (struct btf_param *)(t + 1); |
| 2583 | __u16 vlen = BTF_INFO_VLEN(t->info); |
| 2584 | |
| 2585 | r = btf_dedup_remap_type_id(d, t->type); |
| 2586 | if (r < 0) |
| 2587 | return r; |
| 2588 | t->type = r; |
| 2589 | |
| 2590 | for (i = 0; i < vlen; i++) { |
| 2591 | r = btf_dedup_remap_type_id(d, param->type); |
| 2592 | if (r < 0) |
| 2593 | return r; |
| 2594 | param->type = r; |
| 2595 | param++; |
| 2596 | } |
| 2597 | break; |
| 2598 | } |
| 2599 | |
| 2600 | default: |
| 2601 | return -EINVAL; |
| 2602 | } |
| 2603 | |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
| 2607 | static int btf_dedup_remap_types(struct btf_dedup *d) |
| 2608 | { |
| 2609 | int i, r; |
| 2610 | |
| 2611 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2612 | r = btf_dedup_remap_type(d, i); |
| 2613 | if (r < 0) |
| 2614 | return r; |
| 2615 | } |
| 2616 | return 0; |
| 2617 | } |