Coverage Report

Created: 2025-08-29 06:43

/src/libbpf/src/relo_core.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
/* Copyright (c) 2019 Facebook */
3
4
#ifdef __KERNEL__
5
#include <linux/bpf.h>
6
#include <linux/btf.h>
7
#include <linux/string.h>
8
#include <linux/bpf_verifier.h>
9
#include "relo_core.h"
10
11
static const char *btf_kind_str(const struct btf_type *t)
12
{
13
  return btf_type_str(t);
14
}
15
16
static bool is_ldimm64_insn(struct bpf_insn *insn)
17
{
18
  return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
19
}
20
21
static const struct btf_type *
22
skip_mods_and_typedefs(const struct btf *btf, u32 id, u32 *res_id)
23
{
24
  return btf_type_skip_modifiers(btf, id, res_id);
25
}
26
27
static const char *btf__name_by_offset(const struct btf *btf, u32 offset)
28
{
29
  return btf_name_by_offset(btf, offset);
30
}
31
32
static s64 btf__resolve_size(const struct btf *btf, u32 type_id)
33
{
34
  const struct btf_type *t;
35
  int size;
36
37
  t = btf_type_by_id(btf, type_id);
38
  t = btf_resolve_size(btf, t, &size);
39
  if (IS_ERR(t))
40
    return PTR_ERR(t);
41
  return size;
42
}
43
44
enum libbpf_print_level {
45
  LIBBPF_WARN,
46
  LIBBPF_INFO,
47
  LIBBPF_DEBUG,
48
};
49
50
#undef pr_warn
51
#undef pr_info
52
#undef pr_debug
53
#define pr_warn(fmt, log, ...)  bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
54
#define pr_info(fmt, log, ...)  bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
55
#define pr_debug(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
56
#define libbpf_print(level, fmt, ...) bpf_log((void *)prog_name, fmt, ##__VA_ARGS__)
57
#else
58
#include <stdio.h>
59
#include <string.h>
60
#include <errno.h>
61
#include <ctype.h>
62
#include <linux/err.h>
63
64
#include "libbpf.h"
65
#include "bpf.h"
66
#include "btf.h"
67
#include "str_error.h"
68
#include "libbpf_internal.h"
69
#endif
70
71
static bool is_flex_arr(const struct btf *btf,
72
      const struct bpf_core_accessor *acc,
73
      const struct btf_array *arr)
74
0
{
75
0
  const struct btf_type *t;
76
77
  /* not a flexible array, if not inside a struct or has non-zero size */
78
0
  if (!acc->name || arr->nelems > 0)
79
0
    return false;
80
81
  /* has to be the last member of enclosing struct */
82
0
  t = btf_type_by_id(btf, acc->type_id);
83
0
  return acc->idx == btf_vlen(t) - 1;
84
0
}
85
86
static const char *core_relo_kind_str(enum bpf_core_relo_kind kind)
87
0
{
88
0
  switch (kind) {
89
0
  case BPF_CORE_FIELD_BYTE_OFFSET: return "byte_off";
90
0
  case BPF_CORE_FIELD_BYTE_SIZE: return "byte_sz";
91
0
  case BPF_CORE_FIELD_EXISTS: return "field_exists";
92
0
  case BPF_CORE_FIELD_SIGNED: return "signed";
93
0
  case BPF_CORE_FIELD_LSHIFT_U64: return "lshift_u64";
94
0
  case BPF_CORE_FIELD_RSHIFT_U64: return "rshift_u64";
95
0
  case BPF_CORE_TYPE_ID_LOCAL: return "local_type_id";
96
0
  case BPF_CORE_TYPE_ID_TARGET: return "target_type_id";
97
0
  case BPF_CORE_TYPE_EXISTS: return "type_exists";
98
0
  case BPF_CORE_TYPE_MATCHES: return "type_matches";
99
0
  case BPF_CORE_TYPE_SIZE: return "type_size";
100
0
  case BPF_CORE_ENUMVAL_EXISTS: return "enumval_exists";
101
0
  case BPF_CORE_ENUMVAL_VALUE: return "enumval_value";
102
0
  default: return "unknown";
103
0
  }
104
0
}
105
106
static bool core_relo_is_field_based(enum bpf_core_relo_kind kind)
107
0
{
108
0
  switch (kind) {
109
0
  case BPF_CORE_FIELD_BYTE_OFFSET:
110
0
  case BPF_CORE_FIELD_BYTE_SIZE:
111
0
  case BPF_CORE_FIELD_EXISTS:
112
0
  case BPF_CORE_FIELD_SIGNED:
113
0
  case BPF_CORE_FIELD_LSHIFT_U64:
114
0
  case BPF_CORE_FIELD_RSHIFT_U64:
115
0
    return true;
116
0
  default:
117
0
    return false;
118
0
  }
119
0
}
120
121
static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
122
0
{
123
0
  switch (kind) {
124
0
  case BPF_CORE_TYPE_ID_LOCAL:
125
0
  case BPF_CORE_TYPE_ID_TARGET:
126
0
  case BPF_CORE_TYPE_EXISTS:
127
0
  case BPF_CORE_TYPE_MATCHES:
128
0
  case BPF_CORE_TYPE_SIZE:
129
0
    return true;
130
0
  default:
131
0
    return false;
132
0
  }
133
0
}
134
135
static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind)
136
0
{
137
0
  switch (kind) {
138
0
  case BPF_CORE_ENUMVAL_EXISTS:
139
0
  case BPF_CORE_ENUMVAL_VALUE:
140
0
    return true;
141
0
  default:
142
0
    return false;
143
0
  }
144
0
}
145
146
int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
147
        const struct btf *targ_btf, __u32 targ_id, int level)
148
0
{
149
0
  const struct btf_type *local_type, *targ_type;
150
0
  int depth = 32; /* max recursion depth */
151
152
  /* caller made sure that names match (ignoring flavor suffix) */
153
0
  local_type = btf_type_by_id(local_btf, local_id);
154
0
  targ_type = btf_type_by_id(targ_btf, targ_id);
155
0
  if (!btf_kind_core_compat(local_type, targ_type))
156
0
    return 0;
157
158
0
recur:
159
0
  depth--;
160
0
  if (depth < 0)
161
0
    return -EINVAL;
162
163
0
  local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
164
0
  targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
165
0
  if (!local_type || !targ_type)
166
0
    return -EINVAL;
167
168
0
  if (!btf_kind_core_compat(local_type, targ_type))
169
0
    return 0;
170
171
0
  switch (btf_kind(local_type)) {
172
0
  case BTF_KIND_UNKN:
173
0
  case BTF_KIND_STRUCT:
174
0
  case BTF_KIND_UNION:
175
0
  case BTF_KIND_ENUM:
176
0
  case BTF_KIND_FWD:
177
0
  case BTF_KIND_ENUM64:
178
0
    return 1;
179
0
  case BTF_KIND_INT:
180
    /* just reject deprecated bitfield-like integers; all other
181
     * integers are by default compatible between each other
182
     */
183
0
    return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
184
0
  case BTF_KIND_PTR:
185
0
    local_id = local_type->type;
186
0
    targ_id = targ_type->type;
187
0
    goto recur;
188
0
  case BTF_KIND_ARRAY:
189
0
    local_id = btf_array(local_type)->type;
190
0
    targ_id = btf_array(targ_type)->type;
191
0
    goto recur;
192
0
  case BTF_KIND_FUNC_PROTO: {
193
0
    struct btf_param *local_p = btf_params(local_type);
194
0
    struct btf_param *targ_p = btf_params(targ_type);
195
0
    __u16 local_vlen = btf_vlen(local_type);
196
0
    __u16 targ_vlen = btf_vlen(targ_type);
197
0
    int i, err;
198
199
0
    if (local_vlen != targ_vlen)
200
0
      return 0;
201
202
0
    for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
203
0
      if (level <= 0)
204
0
        return -EINVAL;
205
206
0
      skip_mods_and_typedefs(local_btf, local_p->type, &local_id);
207
0
      skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id);
208
0
      err = __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id,
209
0
                level - 1);
210
0
      if (err <= 0)
211
0
        return err;
212
0
    }
213
214
    /* tail recurse for return type check */
215
0
    skip_mods_and_typedefs(local_btf, local_type->type, &local_id);
216
0
    skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
217
0
    goto recur;
218
0
  }
219
0
  default:
220
0
    pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
221
0
      btf_kind_str(local_type), local_id, targ_id);
222
0
    return 0;
223
0
  }
224
0
}
225
226
/*
227
 * Turn bpf_core_relo into a low- and high-level spec representation,
228
 * validating correctness along the way, as well as calculating resulting
229
 * field bit offset, specified by accessor string. Low-level spec captures
230
 * every single level of nestedness, including traversing anonymous
231
 * struct/union members. High-level one only captures semantically meaningful
232
 * "turning points": named fields and array indicies.
233
 * E.g., for this case:
234
 *
235
 *   struct sample {
236
 *       int __unimportant;
237
 *       struct {
238
 *           int __1;
239
 *           int __2;
240
 *           int a[7];
241
 *       };
242
 *   };
243
 *
244
 *   struct sample *s = ...;
245
 *
246
 *   int x = &s->a[3]; // access string = '0:1:2:3'
247
 *
248
 * Low-level spec has 1:1 mapping with each element of access string (it's
249
 * just a parsed access string representation): [0, 1, 2, 3].
250
 *
251
 * High-level spec will capture only 3 points:
252
 *   - initial zero-index access by pointer (&s->... is the same as &s[0]...);
253
 *   - field 'a' access (corresponds to '2' in low-level spec);
254
 *   - array element #3 access (corresponds to '3' in low-level spec).
255
 *
256
 * Type-based relocations (TYPE_EXISTS/TYPE_MATCHES/TYPE_SIZE,
257
 * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
258
 * spec and raw_spec are kept empty.
259
 *
260
 * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
261
 * string to specify enumerator's value index that need to be relocated.
262
 */
263
int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
264
      const struct bpf_core_relo *relo,
265
      struct bpf_core_spec *spec)
266
0
{
267
0
  int access_idx, parsed_len, i;
268
0
  struct bpf_core_accessor *acc;
269
0
  const struct btf_type *t;
270
0
  const char *name, *spec_str;
271
0
  __u32 id, name_off;
272
0
  __s64 sz;
273
274
0
  spec_str = btf__name_by_offset(btf, relo->access_str_off);
275
0
  if (str_is_empty(spec_str) || *spec_str == ':')
276
0
    return -EINVAL;
277
278
0
  memset(spec, 0, sizeof(*spec));
279
0
  spec->btf = btf;
280
0
  spec->root_type_id = relo->type_id;
281
0
  spec->relo_kind = relo->kind;
282
283
  /* type-based relocations don't have a field access string */
284
0
  if (core_relo_is_type_based(relo->kind)) {
285
0
    if (strcmp(spec_str, "0"))
286
0
      return -EINVAL;
287
0
    return 0;
288
0
  }
289
290
  /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
291
0
  while (*spec_str) {
292
0
    if (*spec_str == ':')
293
0
      ++spec_str;
294
0
    if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
295
0
      return -EINVAL;
296
0
    if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
297
0
      return -E2BIG;
298
0
    spec_str += parsed_len;
299
0
    spec->raw_spec[spec->raw_len++] = access_idx;
300
0
  }
301
302
0
  if (spec->raw_len == 0)
303
0
    return -EINVAL;
304
305
0
  t = skip_mods_and_typedefs(btf, relo->type_id, &id);
306
0
  if (!t)
307
0
    return -EINVAL;
308
309
0
  access_idx = spec->raw_spec[0];
310
0
  acc = &spec->spec[0];
311
0
  acc->type_id = id;
312
0
  acc->idx = access_idx;
313
0
  spec->len++;
314
315
0
  if (core_relo_is_enumval_based(relo->kind)) {
316
0
    if (!btf_is_any_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
317
0
      return -EINVAL;
318
319
    /* record enumerator name in a first accessor */
320
0
    name_off = btf_is_enum(t) ? btf_enum(t)[access_idx].name_off
321
0
            : btf_enum64(t)[access_idx].name_off;
322
0
    acc->name = btf__name_by_offset(btf, name_off);
323
0
    return 0;
324
0
  }
325
326
0
  if (!core_relo_is_field_based(relo->kind))
327
0
    return -EINVAL;
328
329
0
  sz = btf__resolve_size(btf, id);
330
0
  if (sz < 0)
331
0
    return sz;
332
0
  spec->bit_offset = access_idx * sz * 8;
333
334
0
  for (i = 1; i < spec->raw_len; i++) {
335
0
    t = skip_mods_and_typedefs(btf, id, &id);
336
0
    if (!t)
337
0
      return -EINVAL;
338
339
0
    access_idx = spec->raw_spec[i];
340
0
    acc = &spec->spec[spec->len];
341
342
0
    if (btf_is_composite(t)) {
343
0
      const struct btf_member *m;
344
0
      __u32 bit_offset;
345
346
0
      if (access_idx >= btf_vlen(t))
347
0
        return -EINVAL;
348
349
0
      bit_offset = btf_member_bit_offset(t, access_idx);
350
0
      spec->bit_offset += bit_offset;
351
352
0
      m = btf_members(t) + access_idx;
353
0
      if (m->name_off) {
354
0
        name = btf__name_by_offset(btf, m->name_off);
355
0
        if (str_is_empty(name))
356
0
          return -EINVAL;
357
358
0
        acc->type_id = id;
359
0
        acc->idx = access_idx;
360
0
        acc->name = name;
361
0
        spec->len++;
362
0
      }
363
364
0
      id = m->type;
365
0
    } else if (btf_is_array(t)) {
366
0
      const struct btf_array *a = btf_array(t);
367
0
      bool flex;
368
369
0
      t = skip_mods_and_typedefs(btf, a->type, &id);
370
0
      if (!t)
371
0
        return -EINVAL;
372
373
0
      flex = is_flex_arr(btf, acc - 1, a);
374
0
      if (!flex && access_idx >= a->nelems)
375
0
        return -EINVAL;
376
377
0
      spec->spec[spec->len].type_id = id;
378
0
      spec->spec[spec->len].idx = access_idx;
379
0
      spec->len++;
380
381
0
      sz = btf__resolve_size(btf, id);
382
0
      if (sz < 0)
383
0
        return sz;
384
0
      spec->bit_offset += access_idx * sz * 8;
385
0
    } else {
386
0
      pr_warn("prog '%s': relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
387
0
        prog_name, relo->type_id, spec_str, i, id, btf_kind_str(t));
388
0
      return -EINVAL;
389
0
    }
390
0
  }
391
392
0
  return 0;
393
0
}
394
395
/* Check two types for compatibility for the purpose of field access
396
 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
397
 * are relocating semantically compatible entities:
398
 *   - any two STRUCTs/UNIONs are compatible and can be mixed;
399
 *   - any two FWDs are compatible, if their names match (modulo flavor suffix);
400
 *   - any two PTRs are always compatible;
401
 *   - for ENUMs, names should be the same (ignoring flavor suffix) or at
402
 *     least one of enums should be anonymous;
403
 *   - for ENUMs, check sizes, names are ignored;
404
 *   - for INT, size and signedness are ignored;
405
 *   - any two FLOATs are always compatible;
406
 *   - for ARRAY, dimensionality is ignored, element types are checked for
407
 *     compatibility recursively;
408
 *   - everything else shouldn't be ever a target of relocation.
409
 * These rules are not set in stone and probably will be adjusted as we get
410
 * more experience with using BPF CO-RE relocations.
411
 */
412
static int bpf_core_fields_are_compat(const struct btf *local_btf,
413
              __u32 local_id,
414
              const struct btf *targ_btf,
415
              __u32 targ_id)
416
0
{
417
0
  const struct btf_type *local_type, *targ_type;
418
419
0
recur:
420
0
  local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
421
0
  targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
422
0
  if (!local_type || !targ_type)
423
0
    return -EINVAL;
424
425
0
  if (btf_is_composite(local_type) && btf_is_composite(targ_type))
426
0
    return 1;
427
0
  if (!btf_kind_core_compat(local_type, targ_type))
428
0
    return 0;
429
430
0
  switch (btf_kind(local_type)) {
431
0
  case BTF_KIND_PTR:
432
0
  case BTF_KIND_FLOAT:
433
0
    return 1;
434
0
  case BTF_KIND_FWD:
435
0
  case BTF_KIND_ENUM64:
436
0
  case BTF_KIND_ENUM: {
437
0
    const char *local_name, *targ_name;
438
0
    size_t local_len, targ_len;
439
440
0
    local_name = btf__name_by_offset(local_btf,
441
0
             local_type->name_off);
442
0
    targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
443
0
    local_len = bpf_core_essential_name_len(local_name);
444
0
    targ_len = bpf_core_essential_name_len(targ_name);
445
    /* one of them is anonymous or both w/ same flavor-less names */
446
0
    return local_len == 0 || targ_len == 0 ||
447
0
           (local_len == targ_len &&
448
0
      strncmp(local_name, targ_name, local_len) == 0);
449
0
  }
450
0
  case BTF_KIND_INT:
451
    /* just reject deprecated bitfield-like integers; all other
452
     * integers are by default compatible between each other
453
     */
454
0
    return btf_int_offset(local_type) == 0 &&
455
0
           btf_int_offset(targ_type) == 0;
456
0
  case BTF_KIND_ARRAY:
457
0
    local_id = btf_array(local_type)->type;
458
0
    targ_id = btf_array(targ_type)->type;
459
0
    goto recur;
460
0
  default:
461
0
    return 0;
462
0
  }
463
0
}
464
465
/*
466
 * Given single high-level named field accessor in local type, find
467
 * corresponding high-level accessor for a target type. Along the way,
468
 * maintain low-level spec for target as well. Also keep updating target
469
 * bit offset.
470
 *
471
 * Searching is performed through recursive exhaustive enumeration of all
472
 * fields of a struct/union. If there are any anonymous (embedded)
473
 * structs/unions, they are recursively searched as well. If field with
474
 * desired name is found, check compatibility between local and target types,
475
 * before returning result.
476
 *
477
 * 1 is returned, if field is found.
478
 * 0 is returned if no compatible field is found.
479
 * <0 is returned on error.
480
 */
481
static int bpf_core_match_member(const struct btf *local_btf,
482
         const struct bpf_core_accessor *local_acc,
483
         const struct btf *targ_btf,
484
         __u32 targ_id,
485
         struct bpf_core_spec *spec,
486
         __u32 *next_targ_id)
487
0
{
488
0
  const struct btf_type *local_type, *targ_type;
489
0
  const struct btf_member *local_member, *m;
490
0
  const char *local_name, *targ_name;
491
0
  __u32 local_id;
492
0
  int i, n, found;
493
494
0
  targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
495
0
  if (!targ_type)
496
0
    return -EINVAL;
497
0
  if (!btf_is_composite(targ_type))
498
0
    return 0;
499
500
0
  local_id = local_acc->type_id;
501
0
  local_type = btf_type_by_id(local_btf, local_id);
502
0
  local_member = btf_members(local_type) + local_acc->idx;
503
0
  local_name = btf__name_by_offset(local_btf, local_member->name_off);
504
505
0
  n = btf_vlen(targ_type);
506
0
  m = btf_members(targ_type);
507
0
  for (i = 0; i < n; i++, m++) {
508
0
    __u32 bit_offset;
509
510
0
    bit_offset = btf_member_bit_offset(targ_type, i);
511
512
    /* too deep struct/union/array nesting */
513
0
    if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
514
0
      return -E2BIG;
515
516
    /* speculate this member will be the good one */
517
0
    spec->bit_offset += bit_offset;
518
0
    spec->raw_spec[spec->raw_len++] = i;
519
520
0
    targ_name = btf__name_by_offset(targ_btf, m->name_off);
521
0
    if (str_is_empty(targ_name)) {
522
      /* embedded struct/union, we need to go deeper */
523
0
      found = bpf_core_match_member(local_btf, local_acc,
524
0
                  targ_btf, m->type,
525
0
                  spec, next_targ_id);
526
0
      if (found) /* either found or error */
527
0
        return found;
528
0
    } else if (strcmp(local_name, targ_name) == 0) {
529
      /* matching named field */
530
0
      struct bpf_core_accessor *targ_acc;
531
532
0
      targ_acc = &spec->spec[spec->len++];
533
0
      targ_acc->type_id = targ_id;
534
0
      targ_acc->idx = i;
535
0
      targ_acc->name = targ_name;
536
537
0
      *next_targ_id = m->type;
538
0
      found = bpf_core_fields_are_compat(local_btf,
539
0
                 local_member->type,
540
0
                 targ_btf, m->type);
541
0
      if (!found)
542
0
        spec->len--; /* pop accessor */
543
0
      return found;
544
0
    }
545
    /* member turned out not to be what we looked for */
546
0
    spec->bit_offset -= bit_offset;
547
0
    spec->raw_len--;
548
0
  }
549
550
0
  return 0;
551
0
}
552
553
/*
554
 * Try to match local spec to a target type and, if successful, produce full
555
 * target spec (high-level, low-level + bit offset).
556
 */
557
static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
558
             const struct btf *targ_btf, __u32 targ_id,
559
             struct bpf_core_spec *targ_spec)
560
0
{
561
0
  const struct btf_type *targ_type;
562
0
  const struct bpf_core_accessor *local_acc;
563
0
  struct bpf_core_accessor *targ_acc;
564
0
  int i, sz, matched;
565
0
  __u32 name_off;
566
567
0
  memset(targ_spec, 0, sizeof(*targ_spec));
568
0
  targ_spec->btf = targ_btf;
569
0
  targ_spec->root_type_id = targ_id;
570
0
  targ_spec->relo_kind = local_spec->relo_kind;
571
572
0
  if (core_relo_is_type_based(local_spec->relo_kind)) {
573
0
    if (local_spec->relo_kind == BPF_CORE_TYPE_MATCHES)
574
0
      return bpf_core_types_match(local_spec->btf,
575
0
                local_spec->root_type_id,
576
0
                targ_btf, targ_id);
577
0
    else
578
0
      return bpf_core_types_are_compat(local_spec->btf,
579
0
               local_spec->root_type_id,
580
0
               targ_btf, targ_id);
581
0
  }
582
583
0
  local_acc = &local_spec->spec[0];
584
0
  targ_acc = &targ_spec->spec[0];
585
586
0
  if (core_relo_is_enumval_based(local_spec->relo_kind)) {
587
0
    size_t local_essent_len, targ_essent_len;
588
0
    const char *targ_name;
589
590
    /* has to resolve to an enum */
591
0
    targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
592
0
    if (!btf_is_any_enum(targ_type))
593
0
      return 0;
594
595
0
    local_essent_len = bpf_core_essential_name_len(local_acc->name);
596
597
0
    for (i = 0; i < btf_vlen(targ_type); i++) {
598
0
      if (btf_is_enum(targ_type))
599
0
        name_off = btf_enum(targ_type)[i].name_off;
600
0
      else
601
0
        name_off = btf_enum64(targ_type)[i].name_off;
602
603
0
      targ_name = btf__name_by_offset(targ_spec->btf, name_off);
604
0
      targ_essent_len = bpf_core_essential_name_len(targ_name);
605
0
      if (targ_essent_len != local_essent_len)
606
0
        continue;
607
0
      if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
608
0
        targ_acc->type_id = targ_id;
609
0
        targ_acc->idx = i;
610
0
        targ_acc->name = targ_name;
611
0
        targ_spec->len++;
612
0
        targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
613
0
        targ_spec->raw_len++;
614
0
        return 1;
615
0
      }
616
0
    }
617
0
    return 0;
618
0
  }
619
620
0
  if (!core_relo_is_field_based(local_spec->relo_kind))
621
0
    return -EINVAL;
622
623
0
  for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
624
0
    targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
625
0
               &targ_id);
626
0
    if (!targ_type)
627
0
      return -EINVAL;
628
629
0
    if (local_acc->name) {
630
0
      matched = bpf_core_match_member(local_spec->btf,
631
0
              local_acc,
632
0
              targ_btf, targ_id,
633
0
              targ_spec, &targ_id);
634
0
      if (matched <= 0)
635
0
        return matched;
636
0
    } else {
637
      /* for i=0, targ_id is already treated as array element
638
       * type (because it's the original struct), for others
639
       * we should find array element type first
640
       */
641
0
      if (i > 0) {
642
0
        const struct btf_array *a;
643
0
        bool flex;
644
645
0
        if (!btf_is_array(targ_type))
646
0
          return 0;
647
648
0
        a = btf_array(targ_type);
649
0
        flex = is_flex_arr(targ_btf, targ_acc - 1, a);
650
0
        if (!flex && local_acc->idx >= a->nelems)
651
0
          return 0;
652
0
        if (!skip_mods_and_typedefs(targ_btf, a->type,
653
0
                  &targ_id))
654
0
          return -EINVAL;
655
0
      }
656
657
      /* too deep struct/union/array nesting */
658
0
      if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
659
0
        return -E2BIG;
660
661
0
      targ_acc->type_id = targ_id;
662
0
      targ_acc->idx = local_acc->idx;
663
0
      targ_acc->name = NULL;
664
0
      targ_spec->len++;
665
0
      targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
666
0
      targ_spec->raw_len++;
667
668
0
      sz = btf__resolve_size(targ_btf, targ_id);
669
0
      if (sz < 0)
670
0
        return sz;
671
0
      targ_spec->bit_offset += local_acc->idx * sz * 8;
672
0
    }
673
0
  }
674
675
0
  return 1;
676
0
}
677
678
static int bpf_core_calc_field_relo(const char *prog_name,
679
            const struct bpf_core_relo *relo,
680
            const struct bpf_core_spec *spec,
681
            __u64 *val, __u32 *field_sz, __u32 *type_id,
682
            bool *validate)
683
0
{
684
0
  const struct bpf_core_accessor *acc;
685
0
  const struct btf_type *t;
686
0
  __u32 byte_off, byte_sz, bit_off, bit_sz, field_type_id, elem_id;
687
0
  const struct btf_member *m;
688
0
  const struct btf_type *mt;
689
0
  bool bitfield;
690
0
  __s64 sz;
691
692
0
  *field_sz = 0;
693
694
0
  if (relo->kind == BPF_CORE_FIELD_EXISTS) {
695
0
    *val = spec ? 1 : 0;
696
0
    return 0;
697
0
  }
698
699
0
  if (!spec)
700
0
    return -EUCLEAN; /* request instruction poisoning */
701
702
0
  acc = &spec->spec[spec->len - 1];
703
0
  t = btf_type_by_id(spec->btf, acc->type_id);
704
705
  /* a[n] accessor needs special handling */
706
0
  if (!acc->name) {
707
0
    if (relo->kind == BPF_CORE_FIELD_BYTE_OFFSET) {
708
0
      *val = spec->bit_offset / 8;
709
      /* remember field size for load/store mem size;
710
       * note, for arrays we care about individual element
711
       * sizes, not the overall array size
712
       */
713
0
      t = skip_mods_and_typedefs(spec->btf, acc->type_id, &elem_id);
714
0
      while (btf_is_array(t))
715
0
        t = skip_mods_and_typedefs(spec->btf, btf_array(t)->type, &elem_id);
716
0
      sz = btf__resolve_size(spec->btf, elem_id);
717
0
      if (sz < 0)
718
0
        return -EINVAL;
719
0
      *field_sz = sz;
720
0
      *type_id = acc->type_id;
721
0
    } else if (relo->kind == BPF_CORE_FIELD_BYTE_SIZE) {
722
0
      sz = btf__resolve_size(spec->btf, acc->type_id);
723
0
      if (sz < 0)
724
0
        return -EINVAL;
725
0
      *val = sz;
726
0
    } else {
727
0
      pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
728
0
        prog_name, relo->kind, relo->insn_off / 8);
729
0
      return -EINVAL;
730
0
    }
731
0
    if (validate)
732
0
      *validate = true;
733
0
    return 0;
734
0
  }
735
736
0
  m = btf_members(t) + acc->idx;
737
0
  mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
738
0
  bit_off = spec->bit_offset;
739
0
  bit_sz = btf_member_bitfield_size(t, acc->idx);
740
741
0
  bitfield = bit_sz > 0;
742
0
  if (bitfield) {
743
0
    byte_sz = mt->size;
744
0
    byte_off = bit_off / 8 / byte_sz * byte_sz;
745
    /* figure out smallest int size necessary for bitfield load */
746
0
    while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
747
0
      if (byte_sz >= 8) {
748
        /* bitfield can't be read with 64-bit read */
749
0
        pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
750
0
          prog_name, relo->kind, relo->insn_off / 8);
751
0
        return -E2BIG;
752
0
      }
753
0
      byte_sz *= 2;
754
0
      byte_off = bit_off / 8 / byte_sz * byte_sz;
755
0
    }
756
0
  } else {
757
0
    sz = btf__resolve_size(spec->btf, field_type_id);
758
0
    if (sz < 0)
759
0
      return -EINVAL;
760
0
    byte_sz = sz;
761
0
    byte_off = spec->bit_offset / 8;
762
0
    bit_sz = byte_sz * 8;
763
0
  }
764
765
  /* for bitfields, all the relocatable aspects are ambiguous and we
766
   * might disagree with compiler, so turn off validation of expected
767
   * value, except for signedness
768
   */
769
0
  if (validate)
770
0
    *validate = !bitfield;
771
772
0
  switch (relo->kind) {
773
0
  case BPF_CORE_FIELD_BYTE_OFFSET:
774
0
    *val = byte_off;
775
0
    if (!bitfield) {
776
      /* remember field size for load/store mem size;
777
       * note, for arrays we care about individual element
778
       * sizes, not the overall array size
779
       */
780
0
      t = skip_mods_and_typedefs(spec->btf, field_type_id, &elem_id);
781
0
      while (btf_is_array(t))
782
0
        t = skip_mods_and_typedefs(spec->btf, btf_array(t)->type, &elem_id);
783
0
      sz = btf__resolve_size(spec->btf, elem_id);
784
0
      if (sz < 0)
785
0
        return -EINVAL;
786
0
      *field_sz = sz;
787
0
      *type_id = field_type_id;
788
0
    }
789
0
    break;
790
0
  case BPF_CORE_FIELD_BYTE_SIZE:
791
0
    *val = byte_sz;
792
0
    break;
793
0
  case BPF_CORE_FIELD_SIGNED:
794
0
    *val = (btf_is_any_enum(mt) && BTF_INFO_KFLAG(mt->info)) ||
795
0
           (btf_is_int(mt) && (btf_int_encoding(mt) & BTF_INT_SIGNED));
796
0
    if (validate)
797
0
      *validate = true; /* signedness is never ambiguous */
798
0
    break;
799
0
  case BPF_CORE_FIELD_LSHIFT_U64:
800
0
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
801
0
    *val = 64 - (bit_off + bit_sz - byte_off  * 8);
802
#else
803
    *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8);
804
#endif
805
0
    break;
806
0
  case BPF_CORE_FIELD_RSHIFT_U64:
807
0
    *val = 64 - bit_sz;
808
0
    if (validate)
809
0
      *validate = true; /* right shift is never ambiguous */
810
0
    break;
811
0
  case BPF_CORE_FIELD_EXISTS:
812
0
  default:
813
0
    return -EOPNOTSUPP;
814
0
  }
815
816
0
  return 0;
817
0
}
818
819
static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
820
           const struct bpf_core_spec *spec,
821
           __u64 *val, bool *validate)
822
0
{
823
0
  __s64 sz;
824
825
  /* by default, always check expected value in bpf_insn */
826
0
  if (validate)
827
0
    *validate = true;
828
829
  /* type-based relos return zero when target type is not found */
830
0
  if (!spec) {
831
0
    *val = 0;
832
0
    return 0;
833
0
  }
834
835
0
  switch (relo->kind) {
836
0
  case BPF_CORE_TYPE_ID_TARGET:
837
0
    *val = spec->root_type_id;
838
    /* type ID, embedded in bpf_insn, might change during linking,
839
     * so enforcing it is pointless
840
     */
841
0
    if (validate)
842
0
      *validate = false;
843
0
    break;
844
0
  case BPF_CORE_TYPE_EXISTS:
845
0
  case BPF_CORE_TYPE_MATCHES:
846
0
    *val = 1;
847
0
    break;
848
0
  case BPF_CORE_TYPE_SIZE:
849
0
    sz = btf__resolve_size(spec->btf, spec->root_type_id);
850
0
    if (sz < 0)
851
0
      return -EINVAL;
852
0
    *val = sz;
853
0
    break;
854
0
  case BPF_CORE_TYPE_ID_LOCAL:
855
  /* BPF_CORE_TYPE_ID_LOCAL is handled specially and shouldn't get here */
856
0
  default:
857
0
    return -EOPNOTSUPP;
858
0
  }
859
860
0
  return 0;
861
0
}
862
863
static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
864
              const struct bpf_core_spec *spec,
865
              __u64 *val)
866
0
{
867
0
  const struct btf_type *t;
868
869
0
  switch (relo->kind) {
870
0
  case BPF_CORE_ENUMVAL_EXISTS:
871
0
    *val = spec ? 1 : 0;
872
0
    break;
873
0
  case BPF_CORE_ENUMVAL_VALUE:
874
0
    if (!spec)
875
0
      return -EUCLEAN; /* request instruction poisoning */
876
0
    t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
877
0
    if (btf_is_enum(t))
878
0
      *val = btf_enum(t)[spec->spec[0].idx].val;
879
0
    else
880
0
      *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
881
0
    break;
882
0
  default:
883
0
    return -EOPNOTSUPP;
884
0
  }
885
886
0
  return 0;
887
0
}
888
889
/* Calculate original and target relocation values, given local and target
890
 * specs and relocation kind. These values are calculated for each candidate.
891
 * If there are multiple candidates, resulting values should all be consistent
892
 * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
893
 * If instruction has to be poisoned, *poison will be set to true.
894
 */
895
static int bpf_core_calc_relo(const char *prog_name,
896
            const struct bpf_core_relo *relo,
897
            int relo_idx,
898
            const struct bpf_core_spec *local_spec,
899
            const struct bpf_core_spec *targ_spec,
900
            struct bpf_core_relo_res *res)
901
0
{
902
0
  int err = -EOPNOTSUPP;
903
904
0
  res->orig_val = 0;
905
0
  res->new_val = 0;
906
0
  res->poison = false;
907
0
  res->validate = true;
908
0
  res->fail_memsz_adjust = false;
909
0
  res->orig_sz = res->new_sz = 0;
910
0
  res->orig_type_id = res->new_type_id = 0;
911
912
0
  if (core_relo_is_field_based(relo->kind)) {
913
0
    err = bpf_core_calc_field_relo(prog_name, relo, local_spec,
914
0
                 &res->orig_val, &res->orig_sz,
915
0
                 &res->orig_type_id, &res->validate);
916
0
    err = err ?: bpf_core_calc_field_relo(prog_name, relo, targ_spec,
917
0
                  &res->new_val, &res->new_sz,
918
0
                  &res->new_type_id, NULL);
919
0
    if (err)
920
0
      goto done;
921
    /* Validate if it's safe to adjust load/store memory size.
922
     * Adjustments are performed only if original and new memory
923
     * sizes differ.
924
     */
925
0
    res->fail_memsz_adjust = false;
926
0
    if (res->orig_sz != res->new_sz) {
927
0
      const struct btf_type *orig_t, *new_t;
928
929
0
      orig_t = btf_type_by_id(local_spec->btf, res->orig_type_id);
930
0
      new_t = btf_type_by_id(targ_spec->btf, res->new_type_id);
931
932
      /* There are two use cases in which it's safe to
933
       * adjust load/store's mem size:
934
       *   - reading a 32-bit kernel pointer, while on BPF
935
       *   size pointers are always 64-bit; in this case
936
       *   it's safe to "downsize" instruction size due to
937
       *   pointer being treated as unsigned integer with
938
       *   zero-extended upper 32-bits;
939
       *   - reading unsigned integers, again due to
940
       *   zero-extension is preserving the value correctly.
941
       *
942
       * In all other cases it's incorrect to attempt to
943
       * load/store field because read value will be
944
       * incorrect, so we poison relocated instruction.
945
       */
946
0
      if (btf_is_ptr(orig_t) && btf_is_ptr(new_t))
947
0
        goto done;
948
0
      if (btf_is_int(orig_t) && btf_is_int(new_t) &&
949
0
          btf_int_encoding(orig_t) != BTF_INT_SIGNED &&
950
0
          btf_int_encoding(new_t) != BTF_INT_SIGNED)
951
0
        goto done;
952
953
      /* mark as invalid mem size adjustment, but this will
954
       * only be checked for LDX/STX/ST insns
955
       */
956
0
      res->fail_memsz_adjust = true;
957
0
    }
958
0
  } else if (core_relo_is_type_based(relo->kind)) {
959
0
    err = bpf_core_calc_type_relo(relo, local_spec, &res->orig_val, &res->validate);
960
0
    err = err ?: bpf_core_calc_type_relo(relo, targ_spec, &res->new_val, NULL);
961
0
  } else if (core_relo_is_enumval_based(relo->kind)) {
962
0
    err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
963
0
    err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
964
0
  }
965
966
0
done:
967
0
  if (err == -EUCLEAN) {
968
    /* EUCLEAN is used to signal instruction poisoning request */
969
0
    res->poison = true;
970
0
    err = 0;
971
0
  } else if (err == -EOPNOTSUPP) {
972
    /* EOPNOTSUPP means unknown/unsupported relocation */
973
0
    pr_warn("prog '%s': relo #%d: unrecognized CO-RE relocation %s (%d) at insn #%d\n",
974
0
      prog_name, relo_idx, core_relo_kind_str(relo->kind),
975
0
      relo->kind, relo->insn_off / 8);
976
0
  }
977
978
0
  return err;
979
0
}
980
981
/*
982
 * Turn instruction for which CO_RE relocation failed into invalid one with
983
 * distinct signature.
984
 */
985
static void bpf_core_poison_insn(const char *prog_name, int relo_idx,
986
         int insn_idx, struct bpf_insn *insn)
987
0
{
988
0
  pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
989
0
     prog_name, relo_idx, insn_idx);
990
0
  insn->code = BPF_JMP | BPF_CALL;
991
0
  insn->dst_reg = 0;
992
0
  insn->src_reg = 0;
993
0
  insn->off = 0;
994
  /* if this instruction is reachable (not a dead code),
995
   * verifier will complain with the following message:
996
   * invalid func unknown#195896080
997
   */
998
0
  insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */
999
0
}
1000
1001
static int insn_bpf_size_to_bytes(struct bpf_insn *insn)
1002
0
{
1003
0
  switch (BPF_SIZE(insn->code)) {
1004
0
  case BPF_DW: return 8;
1005
0
  case BPF_W: return 4;
1006
0
  case BPF_H: return 2;
1007
0
  case BPF_B: return 1;
1008
0
  default: return -1;
1009
0
  }
1010
0
}
1011
1012
static int insn_bytes_to_bpf_size(__u32 sz)
1013
0
{
1014
0
  switch (sz) {
1015
0
  case 8: return BPF_DW;
1016
0
  case 4: return BPF_W;
1017
0
  case 2: return BPF_H;
1018
0
  case 1: return BPF_B;
1019
0
  default: return -1;
1020
0
  }
1021
0
}
1022
1023
/*
1024
 * Patch relocatable BPF instruction.
1025
 *
1026
 * Patched value is determined by relocation kind and target specification.
1027
 * For existence relocations target spec will be NULL if field/type is not found.
1028
 * Expected insn->imm value is determined using relocation kind and local
1029
 * spec, and is checked before patching instruction. If actual insn->imm value
1030
 * is wrong, bail out with error.
1031
 *
1032
 * Currently supported classes of BPF instruction are:
1033
 * 1. rX = <imm> (assignment with immediate operand);
1034
 * 2. rX += <imm> (arithmetic operations with immediate operand);
1035
 * 3. rX = <imm64> (load with 64-bit immediate value);
1036
 * 4. rX = *(T *)(rY + <off>), where T is one of {u8, u16, u32, u64};
1037
 * 5. *(T *)(rX + <off>) = rY, where T is one of {u8, u16, u32, u64};
1038
 * 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}.
1039
 */
1040
int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
1041
      int insn_idx, const struct bpf_core_relo *relo,
1042
      int relo_idx, const struct bpf_core_relo_res *res)
1043
0
{
1044
0
  __u64 orig_val, new_val;
1045
0
  __u8 class;
1046
1047
0
  class = BPF_CLASS(insn->code);
1048
1049
0
  if (res->poison) {
1050
0
poison:
1051
    /* poison second part of ldimm64 to avoid confusing error from
1052
     * verifier about "unknown opcode 00"
1053
     */
1054
0
    if (is_ldimm64_insn(insn))
1055
0
      bpf_core_poison_insn(prog_name, relo_idx, insn_idx + 1, insn + 1);
1056
0
    bpf_core_poison_insn(prog_name, relo_idx, insn_idx, insn);
1057
0
    return 0;
1058
0
  }
1059
1060
0
  orig_val = res->orig_val;
1061
0
  new_val = res->new_val;
1062
1063
0
  switch (class) {
1064
0
  case BPF_ALU:
1065
0
  case BPF_ALU64:
1066
0
    if (BPF_SRC(insn->code) != BPF_K)
1067
0
      return -EINVAL;
1068
0
    if (res->validate && insn->imm != orig_val) {
1069
0
      pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %llu -> %llu\n",
1070
0
        prog_name, relo_idx,
1071
0
        insn_idx, insn->imm, (unsigned long long)orig_val,
1072
0
        (unsigned long long)new_val);
1073
0
      return -EINVAL;
1074
0
    }
1075
0
    orig_val = insn->imm;
1076
0
    insn->imm = new_val;
1077
0
    pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
1078
0
       prog_name, relo_idx, insn_idx,
1079
0
       (unsigned long long)orig_val, (unsigned long long)new_val);
1080
0
    break;
1081
0
  case BPF_LDX:
1082
0
  case BPF_ST:
1083
0
  case BPF_STX:
1084
0
    if (res->validate && insn->off != orig_val) {
1085
0
      pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %llu -> %llu\n",
1086
0
        prog_name, relo_idx, insn_idx, insn->off, (unsigned long long)orig_val,
1087
0
        (unsigned long long)new_val);
1088
0
      return -EINVAL;
1089
0
    }
1090
0
    if (new_val > SHRT_MAX) {
1091
0
      pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %llu\n",
1092
0
        prog_name, relo_idx, insn_idx, (unsigned long long)new_val);
1093
0
      return -ERANGE;
1094
0
    }
1095
0
    if (res->fail_memsz_adjust) {
1096
0
      pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) accesses field incorrectly. "
1097
0
        "Make sure you are accessing pointers, unsigned integers, or fields of matching type and size.\n",
1098
0
        prog_name, relo_idx, insn_idx);
1099
0
      goto poison;
1100
0
    }
1101
1102
0
    orig_val = insn->off;
1103
0
    insn->off = new_val;
1104
0
    pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %llu -> %llu\n",
1105
0
       prog_name, relo_idx, insn_idx, (unsigned long long)orig_val,
1106
0
       (unsigned long long)new_val);
1107
1108
0
    if (res->new_sz != res->orig_sz) {
1109
0
      int insn_bytes_sz, insn_bpf_sz;
1110
1111
0
      insn_bytes_sz = insn_bpf_size_to_bytes(insn);
1112
0
      if (insn_bytes_sz != res->orig_sz) {
1113
0
        pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) unexpected mem size: got %d, exp %u\n",
1114
0
          prog_name, relo_idx, insn_idx, insn_bytes_sz, res->orig_sz);
1115
0
        return -EINVAL;
1116
0
      }
1117
1118
0
      insn_bpf_sz = insn_bytes_to_bpf_size(res->new_sz);
1119
0
      if (insn_bpf_sz < 0) {
1120
0
        pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) invalid new mem size: %u\n",
1121
0
          prog_name, relo_idx, insn_idx, res->new_sz);
1122
0
        return -EINVAL;
1123
0
      }
1124
1125
0
      insn->code = BPF_MODE(insn->code) | insn_bpf_sz | BPF_CLASS(insn->code);
1126
0
      pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) mem_sz %u -> %u\n",
1127
0
         prog_name, relo_idx, insn_idx, res->orig_sz, res->new_sz);
1128
0
    }
1129
0
    break;
1130
0
  case BPF_LD: {
1131
0
    __u64 imm;
1132
1133
0
    if (!is_ldimm64_insn(insn) ||
1134
0
        insn[0].src_reg != 0 || insn[0].off != 0 ||
1135
0
        insn[1].code != 0 || insn[1].dst_reg != 0 ||
1136
0
        insn[1].src_reg != 0 || insn[1].off != 0) {
1137
0
      pr_warn("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n",
1138
0
        prog_name, relo_idx, insn_idx);
1139
0
      return -EINVAL;
1140
0
    }
1141
1142
0
    imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
1143
0
    if (res->validate && imm != orig_val) {
1144
0
      pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n",
1145
0
        prog_name, relo_idx,
1146
0
        insn_idx, (unsigned long long)imm,
1147
0
        (unsigned long long)orig_val, (unsigned long long)new_val);
1148
0
      return -EINVAL;
1149
0
    }
1150
1151
0
    insn[0].imm = new_val;
1152
0
    insn[1].imm = new_val >> 32;
1153
0
    pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
1154
0
       prog_name, relo_idx, insn_idx,
1155
0
       (unsigned long long)imm, (unsigned long long)new_val);
1156
0
    break;
1157
0
  }
1158
0
  default:
1159
0
    pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:0x%x, src:0x%x, dst:0x%x, off:0x%x, imm:0x%x\n",
1160
0
      prog_name, relo_idx, insn_idx, insn->code,
1161
0
      insn->src_reg, insn->dst_reg, insn->off, insn->imm);
1162
0
    return -EINVAL;
1163
0
  }
1164
1165
0
  return 0;
1166
0
}
1167
1168
/* Output spec definition in the format:
1169
 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
1170
 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
1171
 */
1172
int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec)
1173
0
{
1174
0
  const struct btf_type *t;
1175
0
  const char *s;
1176
0
  __u32 type_id;
1177
0
  int i, len = 0;
1178
1179
0
#define append_buf(fmt, args...)        \
1180
0
  ({              \
1181
0
    int r;            \
1182
0
    r = snprintf(buf, buf_sz, fmt, ##args);    \
1183
0
    len += r;         \
1184
0
    if (r >= buf_sz)       \
1185
0
      r = buf_sz;       \
1186
0
    buf += r;         \
1187
0
    buf_sz -= r;          \
1188
0
  })
1189
1190
0
  type_id = spec->root_type_id;
1191
0
  t = btf_type_by_id(spec->btf, type_id);
1192
0
  s = btf__name_by_offset(spec->btf, t->name_off);
1193
1194
0
  append_buf("<%s> [%u] %s %s",
1195
0
       core_relo_kind_str(spec->relo_kind),
1196
0
       type_id, btf_kind_str(t), str_is_empty(s) ? "<anon>" : s);
1197
1198
0
  if (core_relo_is_type_based(spec->relo_kind))
1199
0
    return len;
1200
1201
0
  if (core_relo_is_enumval_based(spec->relo_kind)) {
1202
0
    t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
1203
0
    if (btf_is_enum(t)) {
1204
0
      const struct btf_enum *e;
1205
0
      const char *fmt_str;
1206
1207
0
      e = btf_enum(t) + spec->raw_spec[0];
1208
0
      s = btf__name_by_offset(spec->btf, e->name_off);
1209
0
      fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %d" : "::%s = %u";
1210
0
      append_buf(fmt_str, s, e->val);
1211
0
    } else {
1212
0
      const struct btf_enum64 *e;
1213
0
      const char *fmt_str;
1214
1215
0
      e = btf_enum64(t) + spec->raw_spec[0];
1216
0
      s = btf__name_by_offset(spec->btf, e->name_off);
1217
0
      fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %lld" : "::%s = %llu";
1218
0
      append_buf(fmt_str, s, (unsigned long long)btf_enum64_value(e));
1219
0
    }
1220
0
    return len;
1221
0
  }
1222
1223
0
  if (core_relo_is_field_based(spec->relo_kind)) {
1224
0
    for (i = 0; i < spec->len; i++) {
1225
0
      if (spec->spec[i].name)
1226
0
        append_buf(".%s", spec->spec[i].name);
1227
0
      else if (i > 0 || spec->spec[i].idx > 0)
1228
0
        append_buf("[%u]", spec->spec[i].idx);
1229
0
    }
1230
1231
0
    append_buf(" (");
1232
0
    for (i = 0; i < spec->raw_len; i++)
1233
0
      append_buf("%s%d", i == 0 ? "" : ":", spec->raw_spec[i]);
1234
1235
0
    if (spec->bit_offset % 8)
1236
0
      append_buf(" @ offset %u.%u)", spec->bit_offset / 8, spec->bit_offset % 8);
1237
0
    else
1238
0
      append_buf(" @ offset %u)", spec->bit_offset / 8);
1239
0
    return len;
1240
0
  }
1241
1242
0
  return len;
1243
0
#undef append_buf
1244
0
}
1245
1246
/*
1247
 * Calculate CO-RE relocation target result.
1248
 *
1249
 * The outline and important points of the algorithm:
1250
 * 1. For given local type, find corresponding candidate target types.
1251
 *    Candidate type is a type with the same "essential" name, ignoring
1252
 *    everything after last triple underscore (___). E.g., `sample`,
1253
 *    `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
1254
 *    for each other. Names with triple underscore are referred to as
1255
 *    "flavors" and are useful, among other things, to allow to
1256
 *    specify/support incompatible variations of the same kernel struct, which
1257
 *    might differ between different kernel versions and/or build
1258
 *    configurations.
1259
 *
1260
 *    N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
1261
 *    converter, when deduplicated BTF of a kernel still contains more than
1262
 *    one different types with the same name. In that case, ___2, ___3, etc
1263
 *    are appended starting from second name conflict. But start flavors are
1264
 *    also useful to be defined "locally", in BPF program, to extract same
1265
 *    data from incompatible changes between different kernel
1266
 *    versions/configurations. For instance, to handle field renames between
1267
 *    kernel versions, one can use two flavors of the struct name with the
1268
 *    same common name and use conditional relocations to extract that field,
1269
 *    depending on target kernel version.
1270
 * 2. For each candidate type, try to match local specification to this
1271
 *    candidate target type. Matching involves finding corresponding
1272
 *    high-level spec accessors, meaning that all named fields should match,
1273
 *    as well as all array accesses should be within the actual bounds. Also,
1274
 *    types should be compatible (see bpf_core_fields_are_compat for details).
1275
 * 3. It is supported and expected that there might be multiple flavors
1276
 *    matching the spec. As long as all the specs resolve to the same set of
1277
 *    offsets across all candidates, there is no error. If there is any
1278
 *    ambiguity, CO-RE relocation will fail. This is necessary to accommodate
1279
 *    imperfection of BTF deduplication, which can cause slight duplication of
1280
 *    the same BTF type, if some directly or indirectly referenced (by
1281
 *    pointer) type gets resolved to different actual types in different
1282
 *    object files. If such a situation occurs, deduplicated BTF will end up
1283
 *    with two (or more) structurally identical types, which differ only in
1284
 *    types they refer to through pointer. This should be OK in most cases and
1285
 *    is not an error.
1286
 * 4. Candidate types search is performed by linearly scanning through all
1287
 *    types in target BTF. It is anticipated that this is overall more
1288
 *    efficient memory-wise and not significantly worse (if not better)
1289
 *    CPU-wise compared to prebuilding a map from all local type names to
1290
 *    a list of candidate type names. It's also sped up by caching resolved
1291
 *    list of matching candidates per each local "root" type ID, that has at
1292
 *    least one bpf_core_relo associated with it. This list is shared
1293
 *    between multiple relocations for the same type ID and is updated as some
1294
 *    of the candidates are pruned due to structural incompatibility.
1295
 */
1296
int bpf_core_calc_relo_insn(const char *prog_name,
1297
          const struct bpf_core_relo *relo,
1298
          int relo_idx,
1299
          const struct btf *local_btf,
1300
          struct bpf_core_cand_list *cands,
1301
          struct bpf_core_spec *specs_scratch,
1302
          struct bpf_core_relo_res *targ_res)
1303
0
{
1304
0
  struct bpf_core_spec *local_spec = &specs_scratch[0];
1305
0
  struct bpf_core_spec *cand_spec = &specs_scratch[1];
1306
0
  struct bpf_core_spec *targ_spec = &specs_scratch[2];
1307
0
  struct bpf_core_relo_res cand_res;
1308
0
  const struct btf_type *local_type;
1309
0
  const char *local_name;
1310
0
  __u32 local_id;
1311
0
  char spec_buf[256];
1312
0
  int i, j, err;
1313
1314
0
  local_id = relo->type_id;
1315
0
  local_type = btf_type_by_id(local_btf, local_id);
1316
0
  local_name = btf__name_by_offset(local_btf, local_type->name_off);
1317
0
  if (!local_name)
1318
0
    return -EINVAL;
1319
1320
0
  err = bpf_core_parse_spec(prog_name, local_btf, relo, local_spec);
1321
0
  if (err) {
1322
0
    const char *spec_str;
1323
1324
0
    spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
1325
0
    pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
1326
0
      prog_name, relo_idx, local_id, btf_kind_str(local_type),
1327
0
      str_is_empty(local_name) ? "<anon>" : local_name,
1328
0
      spec_str ?: "<?>", err);
1329
0
    return -EINVAL;
1330
0
  }
1331
1332
0
  bpf_core_format_spec(spec_buf, sizeof(spec_buf), local_spec);
1333
0
  pr_debug("prog '%s': relo #%d: %s\n", prog_name, relo_idx, spec_buf);
1334
1335
  /* TYPE_ID_LOCAL relo is special and doesn't need candidate search */
1336
0
  if (relo->kind == BPF_CORE_TYPE_ID_LOCAL) {
1337
    /* bpf_insn's imm value could get out of sync during linking */
1338
0
    memset(targ_res, 0, sizeof(*targ_res));
1339
0
    targ_res->validate = false;
1340
0
    targ_res->poison = false;
1341
0
    targ_res->orig_val = local_spec->root_type_id;
1342
0
    targ_res->new_val = local_spec->root_type_id;
1343
0
    return 0;
1344
0
  }
1345
1346
  /* libbpf doesn't support candidate search for anonymous types */
1347
0
  if (str_is_empty(local_name)) {
1348
0
    pr_warn("prog '%s': relo #%d: <%s> (%d) relocation doesn't support anonymous types\n",
1349
0
      prog_name, relo_idx, core_relo_kind_str(relo->kind), relo->kind);
1350
0
    return -EOPNOTSUPP;
1351
0
  }
1352
1353
0
  for (i = 0, j = 0; i < cands->len; i++) {
1354
0
    err = bpf_core_spec_match(local_spec, cands->cands[i].btf,
1355
0
            cands->cands[i].id, cand_spec);
1356
0
    if (err < 0) {
1357
0
      bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec);
1358
0
      pr_warn("prog '%s': relo #%d: error matching candidate #%d %s: %d\n",
1359
0
        prog_name, relo_idx, i, spec_buf, err);
1360
0
      return err;
1361
0
    }
1362
1363
0
    bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec);
1364
0
    pr_debug("prog '%s': relo #%d: %s candidate #%d %s\n", prog_name,
1365
0
       relo_idx, err == 0 ? "non-matching" : "matching", i, spec_buf);
1366
1367
0
    if (err == 0)
1368
0
      continue;
1369
1370
0
    err = bpf_core_calc_relo(prog_name, relo, relo_idx, local_spec, cand_spec, &cand_res);
1371
0
    if (err)
1372
0
      return err;
1373
1374
0
    if (j == 0) {
1375
0
      *targ_res = cand_res;
1376
0
      *targ_spec = *cand_spec;
1377
0
    } else if (cand_spec->bit_offset != targ_spec->bit_offset) {
1378
      /* if there are many field relo candidates, they
1379
       * should all resolve to the same bit offset
1380
       */
1381
0
      pr_warn("prog '%s': relo #%d: field offset ambiguity: %u != %u\n",
1382
0
        prog_name, relo_idx, cand_spec->bit_offset,
1383
0
        targ_spec->bit_offset);
1384
0
      return -EINVAL;
1385
0
    } else if (cand_res.poison != targ_res->poison ||
1386
0
         cand_res.new_val != targ_res->new_val) {
1387
      /* all candidates should result in the same relocation
1388
       * decision and value, otherwise it's dangerous to
1389
       * proceed due to ambiguity
1390
       */
1391
0
      pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %llu != %s %llu\n",
1392
0
        prog_name, relo_idx,
1393
0
        cand_res.poison ? "failure" : "success",
1394
0
        (unsigned long long)cand_res.new_val,
1395
0
        targ_res->poison ? "failure" : "success",
1396
0
        (unsigned long long)targ_res->new_val);
1397
0
      return -EINVAL;
1398
0
    }
1399
1400
0
    cands->cands[j++] = cands->cands[i];
1401
0
  }
1402
1403
  /*
1404
   * For BPF_CORE_FIELD_EXISTS relo or when used BPF program has field
1405
   * existence checks or kernel version/config checks, it's expected
1406
   * that we might not find any candidates. In this case, if field
1407
   * wasn't found in any candidate, the list of candidates shouldn't
1408
   * change at all, we'll just handle relocating appropriately,
1409
   * depending on relo's kind.
1410
   */
1411
0
  if (j > 0)
1412
0
    cands->len = j;
1413
1414
  /*
1415
   * If no candidates were found, it might be both a programmer error,
1416
   * as well as expected case, depending whether instruction w/
1417
   * relocation is guarded in some way that makes it unreachable (dead
1418
   * code) if relocation can't be resolved. This is handled in
1419
   * bpf_core_patch_insn() uniformly by replacing that instruction with
1420
   * BPF helper call insn (using invalid helper ID). If that instruction
1421
   * is indeed unreachable, then it will be ignored and eliminated by
1422
   * verifier. If it was an error, then verifier will complain and point
1423
   * to a specific instruction number in its log.
1424
   */
1425
0
  if (j == 0) {
1426
0
    pr_debug("prog '%s': relo #%d: no matching targets found\n",
1427
0
       prog_name, relo_idx);
1428
1429
    /* calculate single target relo result explicitly */
1430
0
    err = bpf_core_calc_relo(prog_name, relo, relo_idx, local_spec, NULL, targ_res);
1431
0
    if (err)
1432
0
      return err;
1433
0
  }
1434
1435
0
  return 0;
1436
0
}
1437
1438
static bool bpf_core_names_match(const struct btf *local_btf, size_t local_name_off,
1439
         const struct btf *targ_btf, size_t targ_name_off)
1440
0
{
1441
0
  const char *local_n, *targ_n;
1442
0
  size_t local_len, targ_len;
1443
1444
0
  local_n = btf__name_by_offset(local_btf, local_name_off);
1445
0
  targ_n = btf__name_by_offset(targ_btf, targ_name_off);
1446
1447
0
  if (str_is_empty(targ_n))
1448
0
    return str_is_empty(local_n);
1449
1450
0
  targ_len = bpf_core_essential_name_len(targ_n);
1451
0
  local_len = bpf_core_essential_name_len(local_n);
1452
1453
0
  return targ_len == local_len && strncmp(local_n, targ_n, local_len) == 0;
1454
0
}
1455
1456
static int bpf_core_enums_match(const struct btf *local_btf, const struct btf_type *local_t,
1457
        const struct btf *targ_btf, const struct btf_type *targ_t)
1458
0
{
1459
0
  __u16 local_vlen = btf_vlen(local_t);
1460
0
  __u16 targ_vlen = btf_vlen(targ_t);
1461
0
  int i, j;
1462
1463
0
  if (local_t->size != targ_t->size)
1464
0
    return 0;
1465
1466
0
  if (local_vlen > targ_vlen)
1467
0
    return 0;
1468
1469
  /* iterate over the local enum's variants and make sure each has
1470
   * a symbolic name correspondent in the target
1471
   */
1472
0
  for (i = 0; i < local_vlen; i++) {
1473
0
    bool matched = false;
1474
0
    __u32 local_n_off, targ_n_off;
1475
1476
0
    local_n_off = btf_is_enum(local_t) ? btf_enum(local_t)[i].name_off :
1477
0
                 btf_enum64(local_t)[i].name_off;
1478
1479
0
    for (j = 0; j < targ_vlen; j++) {
1480
0
      targ_n_off = btf_is_enum(targ_t) ? btf_enum(targ_t)[j].name_off :
1481
0
                 btf_enum64(targ_t)[j].name_off;
1482
1483
0
      if (bpf_core_names_match(local_btf, local_n_off, targ_btf, targ_n_off)) {
1484
0
        matched = true;
1485
0
        break;
1486
0
      }
1487
0
    }
1488
1489
0
    if (!matched)
1490
0
      return 0;
1491
0
  }
1492
0
  return 1;
1493
0
}
1494
1495
static int bpf_core_composites_match(const struct btf *local_btf, const struct btf_type *local_t,
1496
             const struct btf *targ_btf, const struct btf_type *targ_t,
1497
             bool behind_ptr, int level)
1498
0
{
1499
0
  const struct btf_member *local_m = btf_members(local_t);
1500
0
  __u16 local_vlen = btf_vlen(local_t);
1501
0
  __u16 targ_vlen = btf_vlen(targ_t);
1502
0
  int i, j, err;
1503
1504
0
  if (local_vlen > targ_vlen)
1505
0
    return 0;
1506
1507
  /* check that all local members have a match in the target */
1508
0
  for (i = 0; i < local_vlen; i++, local_m++) {
1509
0
    const struct btf_member *targ_m = btf_members(targ_t);
1510
0
    bool matched = false;
1511
1512
0
    for (j = 0; j < targ_vlen; j++, targ_m++) {
1513
0
      if (!bpf_core_names_match(local_btf, local_m->name_off,
1514
0
              targ_btf, targ_m->name_off))
1515
0
        continue;
1516
1517
0
      err = __bpf_core_types_match(local_btf, local_m->type, targ_btf,
1518
0
                 targ_m->type, behind_ptr, level - 1);
1519
0
      if (err < 0)
1520
0
        return err;
1521
0
      if (err > 0) {
1522
0
        matched = true;
1523
0
        break;
1524
0
      }
1525
0
    }
1526
1527
0
    if (!matched)
1528
0
      return 0;
1529
0
  }
1530
0
  return 1;
1531
0
}
1532
1533
/* Check that two types "match". This function assumes that root types were
1534
 * already checked for name match.
1535
 *
1536
 * The matching relation is defined as follows:
1537
 * - modifiers and typedefs are stripped (and, hence, effectively ignored)
1538
 * - generally speaking types need to be of same kind (struct vs. struct, union
1539
 *   vs. union, etc.)
1540
 *   - exceptions are struct/union behind a pointer which could also match a
1541
 *     forward declaration of a struct or union, respectively, and enum vs.
1542
 *     enum64 (see below)
1543
 * Then, depending on type:
1544
 * - integers:
1545
 *   - match if size and signedness match
1546
 * - arrays & pointers:
1547
 *   - target types are recursively matched
1548
 * - structs & unions:
1549
 *   - local members need to exist in target with the same name
1550
 *   - for each member we recursively check match unless it is already behind a
1551
 *     pointer, in which case we only check matching names and compatible kind
1552
 * - enums:
1553
 *   - local variants have to have a match in target by symbolic name (but not
1554
 *     numeric value)
1555
 *   - size has to match (but enum may match enum64 and vice versa)
1556
 * - function pointers:
1557
 *   - number and position of arguments in local type has to match target
1558
 *   - for each argument and the return value we recursively check match
1559
 */
1560
int __bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const struct btf *targ_btf,
1561
         __u32 targ_id, bool behind_ptr, int level)
1562
0
{
1563
0
  const struct btf_type *local_t, *targ_t;
1564
0
  int depth = 32; /* max recursion depth */
1565
0
  __u16 local_k, targ_k;
1566
1567
0
  if (level <= 0)
1568
0
    return -EINVAL;
1569
1570
0
recur:
1571
0
  depth--;
1572
0
  if (depth < 0)
1573
0
    return -EINVAL;
1574
1575
0
  local_t = skip_mods_and_typedefs(local_btf, local_id, &local_id);
1576
0
  targ_t = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
1577
0
  if (!local_t || !targ_t)
1578
0
    return -EINVAL;
1579
1580
  /* While the name check happens after typedefs are skipped, root-level
1581
   * typedefs would still be name-matched as that's the contract with
1582
   * callers.
1583
   */
1584
0
  if (!bpf_core_names_match(local_btf, local_t->name_off, targ_btf, targ_t->name_off))
1585
0
    return 0;
1586
1587
0
  local_k = btf_kind(local_t);
1588
0
  targ_k = btf_kind(targ_t);
1589
1590
0
  switch (local_k) {
1591
0
  case BTF_KIND_UNKN:
1592
0
    return local_k == targ_k;
1593
0
  case BTF_KIND_FWD: {
1594
0
    bool local_f = BTF_INFO_KFLAG(local_t->info);
1595
1596
0
    if (behind_ptr) {
1597
0
      if (local_k == targ_k)
1598
0
        return local_f == BTF_INFO_KFLAG(targ_t->info);
1599
1600
      /* for forward declarations kflag dictates whether the
1601
       * target is a struct (0) or union (1)
1602
       */
1603
0
      return (targ_k == BTF_KIND_STRUCT && !local_f) ||
1604
0
             (targ_k == BTF_KIND_UNION && local_f);
1605
0
    } else {
1606
0
      if (local_k != targ_k)
1607
0
        return 0;
1608
1609
      /* match if the forward declaration is for the same kind */
1610
0
      return local_f == BTF_INFO_KFLAG(targ_t->info);
1611
0
    }
1612
0
  }
1613
0
  case BTF_KIND_ENUM:
1614
0
  case BTF_KIND_ENUM64:
1615
0
    if (!btf_is_any_enum(targ_t))
1616
0
      return 0;
1617
1618
0
    return bpf_core_enums_match(local_btf, local_t, targ_btf, targ_t);
1619
0
  case BTF_KIND_STRUCT:
1620
0
  case BTF_KIND_UNION:
1621
0
    if (behind_ptr) {
1622
0
      bool targ_f = BTF_INFO_KFLAG(targ_t->info);
1623
1624
0
      if (local_k == targ_k)
1625
0
        return 1;
1626
1627
0
      if (targ_k != BTF_KIND_FWD)
1628
0
        return 0;
1629
1630
0
      return (local_k == BTF_KIND_UNION) == targ_f;
1631
0
    } else {
1632
0
      if (local_k != targ_k)
1633
0
        return 0;
1634
1635
0
      return bpf_core_composites_match(local_btf, local_t, targ_btf, targ_t,
1636
0
               behind_ptr, level);
1637
0
    }
1638
0
  case BTF_KIND_INT: {
1639
0
    __u8 local_sgn;
1640
0
    __u8 targ_sgn;
1641
1642
0
    if (local_k != targ_k)
1643
0
      return 0;
1644
1645
0
    local_sgn = btf_int_encoding(local_t) & BTF_INT_SIGNED;
1646
0
    targ_sgn = btf_int_encoding(targ_t) & BTF_INT_SIGNED;
1647
1648
0
    return local_t->size == targ_t->size && local_sgn == targ_sgn;
1649
0
  }
1650
0
  case BTF_KIND_PTR:
1651
0
    if (local_k != targ_k)
1652
0
      return 0;
1653
1654
0
    behind_ptr = true;
1655
1656
0
    local_id = local_t->type;
1657
0
    targ_id = targ_t->type;
1658
0
    goto recur;
1659
0
  case BTF_KIND_ARRAY: {
1660
0
    const struct btf_array *local_array = btf_array(local_t);
1661
0
    const struct btf_array *targ_array = btf_array(targ_t);
1662
1663
0
    if (local_k != targ_k)
1664
0
      return 0;
1665
1666
0
    if (local_array->nelems != targ_array->nelems)
1667
0
      return 0;
1668
1669
0
    local_id = local_array->type;
1670
0
    targ_id = targ_array->type;
1671
0
    goto recur;
1672
0
  }
1673
0
  case BTF_KIND_FUNC_PROTO: {
1674
0
    struct btf_param *local_p = btf_params(local_t);
1675
0
    struct btf_param *targ_p = btf_params(targ_t);
1676
0
    __u16 local_vlen = btf_vlen(local_t);
1677
0
    __u16 targ_vlen = btf_vlen(targ_t);
1678
0
    int i, err;
1679
1680
0
    if (local_k != targ_k)
1681
0
      return 0;
1682
1683
0
    if (local_vlen != targ_vlen)
1684
0
      return 0;
1685
1686
0
    for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
1687
0
      err = __bpf_core_types_match(local_btf, local_p->type, targ_btf,
1688
0
                 targ_p->type, behind_ptr, level - 1);
1689
0
      if (err <= 0)
1690
0
        return err;
1691
0
    }
1692
1693
    /* tail recurse for return type check */
1694
0
    local_id = local_t->type;
1695
0
    targ_id = targ_t->type;
1696
0
    goto recur;
1697
0
  }
1698
0
  default:
1699
0
    pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
1700
0
      btf_kind_str(local_t), local_id, targ_id);
1701
0
    return 0;
1702
0
  }
1703
0
}