From d88eb949b188c6112c7d0e9c3f6eb669f0736dec Mon Sep 17 00:00:00 2001 From: Jackwike Date: Tue, 28 Nov 2023 19:35:03 +0800 Subject: [PATCH] Calculate type-dependent redirects --- libbpf/src/relo_core.c | 338 ++++++++++++++++++++++++++++------------- 1 file changed, 236 insertions(+), 102 deletions(-) diff --git a/libbpf/src/relo_core.c b/libbpf/src/relo_core.c index a47acade..be00c5fb 100644 --- a/libbpf/src/relo_core.c +++ b/libbpf/src/relo_core.c @@ -41,7 +41,8 @@ static s64 btf__resolve_size(const struct btf *btf, u32 type_id) return size; } -enum libbpf_print_level { +enum libbpf_print_level +{ LIBBPF_WARN, LIBBPF_INFO, LIBBPF_DEBUG, @@ -50,10 +51,10 @@ enum libbpf_print_level { #undef pr_warn #undef pr_info #undef pr_debug -#define pr_warn(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) -#define pr_info(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) -#define pr_debug(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) -#define libbpf_print(level, fmt, ...) bpf_log((void *)prog_name, fmt, ##__VA_ARGS__) +#define pr_warn(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) +#define pr_info(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) +#define pr_debug(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__) +#define libbpf_print(level, fmt, ...) bpf_log((void *)prog_name, fmt, ##__VA_ARGS__) #else #include #include @@ -69,8 +70,8 @@ enum libbpf_print_level { #endif static bool is_flex_arr(const struct btf *btf, - const struct bpf_core_accessor *acc, - const struct btf_array *arr) + const struct bpf_core_accessor *acc, + const struct btf_array *arr) { const struct btf_type *t; @@ -85,27 +86,43 @@ static bool is_flex_arr(const struct btf *btf, static const char *core_relo_kind_str(enum bpf_core_relo_kind kind) { - switch (kind) { - case BPF_CORE_FIELD_BYTE_OFFSET: return "byte_off"; - case BPF_CORE_FIELD_BYTE_SIZE: return "byte_sz"; - case BPF_CORE_FIELD_EXISTS: return "field_exists"; - case BPF_CORE_FIELD_SIGNED: return "signed"; - case BPF_CORE_FIELD_LSHIFT_U64: return "lshift_u64"; - case BPF_CORE_FIELD_RSHIFT_U64: return "rshift_u64"; - case BPF_CORE_TYPE_ID_LOCAL: return "local_type_id"; - case BPF_CORE_TYPE_ID_TARGET: return "target_type_id"; - case BPF_CORE_TYPE_EXISTS: return "type_exists"; - case BPF_CORE_TYPE_MATCHES: return "type_matches"; - case BPF_CORE_TYPE_SIZE: return "type_size"; - case BPF_CORE_ENUMVAL_EXISTS: return "enumval_exists"; - case BPF_CORE_ENUMVAL_VALUE: return "enumval_value"; - default: return "unknown"; + switch (kind) + { + case BPF_CORE_FIELD_BYTE_OFFSET: + return "byte_off"; + case BPF_CORE_FIELD_BYTE_SIZE: + return "byte_sz"; + case BPF_CORE_FIELD_EXISTS: + return "field_exists"; + case BPF_CORE_FIELD_SIGNED: + return "signed"; + case BPF_CORE_FIELD_LSHIFT_U64: + return "lshift_u64"; + case BPF_CORE_FIELD_RSHIFT_U64: + return "rshift_u64"; + case BPF_CORE_TYPE_ID_LOCAL: + return "local_type_id"; + case BPF_CORE_TYPE_ID_TARGET: + return "target_type_id"; + case BPF_CORE_TYPE_EXISTS: + return "type_exists"; + case BPF_CORE_TYPE_MATCHES: + return "type_matches"; + case BPF_CORE_TYPE_SIZE: + return "type_size"; + case BPF_CORE_ENUMVAL_EXISTS: + return "enumval_exists"; + case BPF_CORE_ENUMVAL_VALUE: + return "enumval_value"; + default: + return "unknown"; } } static bool core_relo_is_field_based(enum bpf_core_relo_kind kind) { - switch (kind) { + switch (kind) + { case BPF_CORE_FIELD_BYTE_OFFSET: case BPF_CORE_FIELD_BYTE_SIZE: case BPF_CORE_FIELD_EXISTS: @@ -120,7 +137,8 @@ static bool core_relo_is_field_based(enum bpf_core_relo_kind kind) static bool core_relo_is_type_based(enum bpf_core_relo_kind kind) { - switch (kind) { + switch (kind) + { case BPF_CORE_TYPE_ID_LOCAL: case BPF_CORE_TYPE_ID_TARGET: case BPF_CORE_TYPE_EXISTS: @@ -134,7 +152,8 @@ static bool core_relo_is_type_based(enum bpf_core_relo_kind kind) static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind) { - switch (kind) { + switch (kind) + { case BPF_CORE_ENUMVAL_EXISTS: case BPF_CORE_ENUMVAL_VALUE: return true; @@ -144,7 +163,7 @@ static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind) } int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, - const struct btf *targ_btf, __u32 targ_id, int level) + const struct btf *targ_btf, __u32 targ_id, int level) { const struct btf_type *local_type, *targ_type; int depth = 32; /* max recursion depth */ @@ -168,7 +187,8 @@ recur: if (!btf_kind_core_compat(local_type, targ_type)) return 0; - switch (btf_kind(local_type)) { + switch (btf_kind(local_type)) + { case BTF_KIND_UNKN: case BTF_KIND_STRUCT: case BTF_KIND_UNION: @@ -189,7 +209,8 @@ recur: local_id = btf_array(local_type)->type; targ_id = btf_array(targ_type)->type; goto recur; - case BTF_KIND_FUNC_PROTO: { + case BTF_KIND_FUNC_PROTO: + { struct btf_param *local_p = btf_params(local_type); struct btf_param *targ_p = btf_params(targ_type); __u16 local_vlen = btf_vlen(local_type); @@ -199,14 +220,15 @@ recur: if (local_vlen != targ_vlen) return 0; - for (i = 0; i < local_vlen; i++, local_p++, targ_p++) { + for (i = 0; i < local_vlen; i++, local_p++, targ_p++) + { if (level <= 0) return -EINVAL; skip_mods_and_typedefs(local_btf, local_p->type, &local_id); skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id); err = __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, - level - 1); + level - 1); if (err <= 0) return err; } @@ -218,14 +240,14 @@ recur: } default: pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n", - btf_kind_str(local_type), local_id, targ_id); + btf_kind_str(local_type), local_id, targ_id); return 0; } } int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, - const struct bpf_core_relo *relo, - struct bpf_core_spec *spec) + const struct bpf_core_relo *relo, + struct bpf_core_spec *spec) { int access_idx, parsed_len, i; struct bpf_core_accessor *acc; @@ -244,14 +266,16 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, spec->relo_kind = relo->kind; /* type-based relocations don't have a field access string */ - if (core_relo_is_type_based(relo->kind)) { + if (core_relo_is_type_based(relo->kind)) + { if (strcmp(spec_str, "0")) return -EINVAL; return 0; } /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */ - while (*spec_str) { + while (*spec_str) + { if (*spec_str == ':') ++spec_str; if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1) @@ -275,13 +299,14 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, acc->idx = access_idx; spec->len++; - if (core_relo_is_enumval_based(relo->kind)) { + if (core_relo_is_enumval_based(relo->kind)) + { if (!btf_is_any_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t)) return -EINVAL; /* record enumerator name in a first accessor */ name_off = btf_is_enum(t) ? btf_enum(t)[access_idx].name_off - : btf_enum64(t)[access_idx].name_off; + : btf_enum64(t)[access_idx].name_off; acc->name = btf__name_by_offset(btf, name_off); return 0; } @@ -294,7 +319,8 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, return sz; spec->bit_offset = access_idx * sz * 8; - for (i = 1; i < spec->raw_len; i++) { + for (i = 1; i < spec->raw_len; i++) + { t = skip_mods_and_typedefs(btf, id, &id); if (!t) return -EINVAL; @@ -302,7 +328,8 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, access_idx = spec->raw_spec[i]; acc = &spec->spec[spec->len]; - if (btf_is_composite(t)) { + if (btf_is_composite(t)) + { const struct btf_member *m; __u32 bit_offset; @@ -313,7 +340,8 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, spec->bit_offset += bit_offset; m = btf_members(t) + access_idx; - if (m->name_off) { + if (m->name_off) + { name = btf__name_by_offset(btf, m->name_off); if (str_is_empty(name)) return -EINVAL; @@ -325,7 +353,9 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, } id = m->type; - } else if (btf_is_array(t)) { + } + else if (btf_is_array(t)) + { const struct btf_array *a = btf_array(t); bool flex; @@ -345,9 +375,11 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, if (sz < 0) return sz; spec->bit_offset += access_idx * sz * 8; - } else { + } + else + { pr_warn("prog '%s': relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n", - prog_name, relo->type_id, spec_str, i, id, btf_kind_str(t)); + prog_name, relo->type_id, spec_str, i, id, btf_kind_str(t)); return -EINVAL; } } @@ -356,9 +388,9 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, } static int bpf_core_fields_are_compat(const struct btf *local_btf, - __u32 local_id, - const struct btf *targ_btf, - __u32 targ_id) + __u32 local_id, + const struct btf *targ_btf, + __u32 targ_id) { const struct btf_type *local_type, *targ_type; @@ -373,32 +405,34 @@ recur: if (!btf_kind_core_compat(local_type, targ_type)) return 0; - switch (btf_kind(local_type)) { + switch (btf_kind(local_type)) + { case BTF_KIND_PTR: case BTF_KIND_FLOAT: return 1; case BTF_KIND_FWD: case BTF_KIND_ENUM64: - case BTF_KIND_ENUM: { + case BTF_KIND_ENUM: + { const char *local_name, *targ_name; size_t local_len, targ_len; local_name = btf__name_by_offset(local_btf, - local_type->name_off); + local_type->name_off); targ_name = btf__name_by_offset(targ_btf, targ_type->name_off); local_len = bpf_core_essential_name_len(local_name); targ_len = bpf_core_essential_name_len(targ_name); /* one of them is anonymous or both w/ same flavor-less names */ return local_len == 0 || targ_len == 0 || - (local_len == targ_len && - strncmp(local_name, targ_name, local_len) == 0); + (local_len == targ_len && + strncmp(local_name, targ_name, local_len) == 0); } case BTF_KIND_INT: /* just reject deprecated bitfield-like integers; all other * integers are by default compatible between each other */ return btf_int_offset(local_type) == 0 && - btf_int_offset(targ_type) == 0; + btf_int_offset(targ_type) == 0; case BTF_KIND_ARRAY: local_id = btf_array(local_type)->type; targ_id = btf_array(targ_type)->type; @@ -409,11 +443,11 @@ recur: } static int bpf_core_match_member(const struct btf *local_btf, - const struct bpf_core_accessor *local_acc, - const struct btf *targ_btf, - __u32 targ_id, - struct bpf_core_spec *spec, - __u32 *next_targ_id) + const struct bpf_core_accessor *local_acc, + const struct btf *targ_btf, + __u32 targ_id, + struct bpf_core_spec *spec, + __u32 *next_targ_id) { const struct btf_type *local_type, *targ_type; const struct btf_member *local_member, *m; @@ -434,7 +468,8 @@ static int bpf_core_match_member(const struct btf *local_btf, n = btf_vlen(targ_type); m = btf_members(targ_type); - for (i = 0; i < n; i++, m++) { + for (i = 0; i < n; i++, m++) + { __u32 bit_offset; bit_offset = btf_member_bit_offset(targ_type, i); @@ -448,14 +483,17 @@ static int bpf_core_match_member(const struct btf *local_btf, spec->raw_spec[spec->raw_len++] = i; targ_name = btf__name_by_offset(targ_btf, m->name_off); - if (str_is_empty(targ_name)) { + if (str_is_empty(targ_name)) + { /* embedded struct/union, we need to go deeper */ found = bpf_core_match_member(local_btf, local_acc, - targ_btf, m->type, - spec, next_targ_id); + targ_btf, m->type, + spec, next_targ_id); if (found) /* either found or error */ return found; - } else if (strcmp(local_name, targ_name) == 0) { + } + else if (strcmp(local_name, targ_name) == 0) + { /* matching named field */ struct bpf_core_accessor *targ_acc; @@ -466,8 +504,8 @@ static int bpf_core_match_member(const struct btf *local_btf, *next_targ_id = m->type; found = bpf_core_fields_are_compat(local_btf, - local_member->type, - targ_btf, m->type); + local_member->type, + targ_btf, m->type); if (!found) spec->len--; /* pop accessor */ return found; @@ -481,8 +519,8 @@ static int bpf_core_match_member(const struct btf *local_btf, } static int bpf_core_spec_match(struct bpf_core_spec *local_spec, - const struct btf *targ_btf, __u32 targ_id, - struct bpf_core_spec *targ_spec) + const struct btf *targ_btf, __u32 targ_id, + struct bpf_core_spec *targ_spec) { const struct btf_type *targ_type; const struct bpf_core_accessor *local_acc; @@ -495,21 +533,23 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, targ_spec->root_type_id = targ_id; targ_spec->relo_kind = local_spec->relo_kind; - if (core_relo_is_type_based(local_spec->relo_kind)) { + if (core_relo_is_type_based(local_spec->relo_kind)) + { if (local_spec->relo_kind == BPF_CORE_TYPE_MATCHES) return bpf_core_types_match(local_spec->btf, - local_spec->root_type_id, - targ_btf, targ_id); + local_spec->root_type_id, + targ_btf, targ_id); else return bpf_core_types_are_compat(local_spec->btf, - local_spec->root_type_id, - targ_btf, targ_id); + local_spec->root_type_id, + targ_btf, targ_id); } local_acc = &local_spec->spec[0]; targ_acc = &targ_spec->spec[0]; - if (core_relo_is_enumval_based(local_spec->relo_kind)) { + if (core_relo_is_enumval_based(local_spec->relo_kind)) + { size_t local_essent_len, targ_essent_len; const char *targ_name; @@ -520,7 +560,8 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, local_essent_len = bpf_core_essential_name_len(local_acc->name); - for (i = 0; i < btf_vlen(targ_type); i++) { + for (i = 0; i < btf_vlen(targ_type); i++) + { if (btf_is_enum(targ_type)) name_off = btf_enum(targ_type)[i].name_off; else @@ -530,7 +571,8 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, targ_essent_len = bpf_core_essential_name_len(targ_name); if (targ_essent_len != local_essent_len) continue; - if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) { + if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) + { targ_acc->type_id = targ_id; targ_acc->idx = i; targ_acc->name = targ_name; @@ -546,25 +588,30 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, if (!core_relo_is_field_based(local_spec->relo_kind)) return -EINVAL; - for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) { + for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) + { targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, - &targ_id); + &targ_id); if (!targ_type) return -EINVAL; - if (local_acc->name) { + if (local_acc->name) + { matched = bpf_core_match_member(local_spec->btf, - local_acc, - targ_btf, targ_id, - targ_spec, &targ_id); + local_acc, + targ_btf, targ_id, + targ_spec, &targ_id); if (matched <= 0) return matched; - } else { + } + else + { /* for i=0, targ_id is already treated as array element * type (because it's the original struct), for others * we should find array element type first */ - if (i > 0) { + if (i > 0) + { const struct btf_array *a; bool flex; @@ -576,7 +623,7 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, if (!flex && local_acc->idx >= a->nelems) return 0; if (!skip_mods_and_typedefs(targ_btf, a->type, - &targ_id)) + &targ_id)) return -EINVAL; } @@ -602,10 +649,10 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, } static int bpf_core_calc_field_relo(const char *prog_name, - const struct bpf_core_relo *relo, - const struct bpf_core_spec *spec, - __u64 *val, __u32 *field_sz, __u32 *type_id, - bool *validate) + const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, + __u64 *val, __u32 *field_sz, __u32 *type_id, + bool *validate) { const struct bpf_core_accessor *acc; const struct btf_type *t; @@ -617,7 +664,8 @@ static int bpf_core_calc_field_relo(const char *prog_name, *field_sz = 0; - if (relo->kind == BPF_CORE_FIELD_EXISTS) { + if (relo->kind == BPF_CORE_FIELD_EXISTS) + { *val = spec ? 1 : 0; return 0; } @@ -629,8 +677,10 @@ static int bpf_core_calc_field_relo(const char *prog_name, t = btf_type_by_id(spec->btf, acc->type_id); /* a[n] accessor needs special handling */ - if (!acc->name) { - if (relo->kind == BPF_CORE_FIELD_BYTE_OFFSET) { + if (!acc->name) + { + if (relo->kind == BPF_CORE_FIELD_BYTE_OFFSET) + { *val = spec->bit_offset / 8; /* remember field size for load/store mem size */ sz = btf__resolve_size(spec->btf, acc->type_id); @@ -638,14 +688,18 @@ static int bpf_core_calc_field_relo(const char *prog_name, return -EINVAL; *field_sz = sz; *type_id = acc->type_id; - } else if (relo->kind == BPF_CORE_FIELD_BYTE_SIZE) { + } + else if (relo->kind == BPF_CORE_FIELD_BYTE_SIZE) + { sz = btf__resolve_size(spec->btf, acc->type_id); if (sz < 0) return -EINVAL; *val = sz; - } else { + } + else + { pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n", - prog_name, relo->kind, relo->insn_off / 8); + prog_name, relo->kind, relo->insn_off / 8); return -EINVAL; } if (validate) @@ -659,21 +713,26 @@ static int bpf_core_calc_field_relo(const char *prog_name, bit_sz = btf_member_bitfield_size(t, acc->idx); bitfield = bit_sz > 0; - if (bitfield) { + if (bitfield) + { byte_sz = mt->size; byte_off = bit_off / 8 / byte_sz * byte_sz; /* figure out smallest int size necessary for bitfield load */ - while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) { - if (byte_sz >= 8) { + while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) + { + if (byte_sz >= 8) + { /* bitfield can't be read with 64-bit read */ pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n", - prog_name, relo->kind, relo->insn_off / 8); + prog_name, relo->kind, relo->insn_off / 8); return -E2BIG; } byte_sz *= 2; byte_off = bit_off / 8 / byte_sz * byte_sz; } - } else { + } + else + { sz = btf__resolve_size(spec->btf, field_type_id); if (sz < 0) return -EINVAL; @@ -689,10 +748,12 @@ static int bpf_core_calc_field_relo(const char *prog_name, if (validate) *validate = !bitfield; - switch (relo->kind) { + switch (relo->kind) + { case BPF_CORE_FIELD_BYTE_OFFSET: *val = byte_off; - if (!bitfield) { + if (!bitfield) + { *field_sz = byte_sz; *type_id = field_type_id; } @@ -702,13 +763,13 @@ static int bpf_core_calc_field_relo(const char *prog_name, break; case BPF_CORE_FIELD_SIGNED: *val = (btf_is_any_enum(mt) && BTF_INFO_KFLAG(mt->info)) || - (btf_int_encoding(mt) & BTF_INT_SIGNED); + (btf_int_encoding(mt) & BTF_INT_SIGNED); if (validate) *validate = true; /* signedness is never ambiguous */ break; case BPF_CORE_FIELD_LSHIFT_U64: #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - *val = 64 - (bit_off + bit_sz - byte_off * 8); + *val = 64 - (bit_off + bit_sz - byte_off * 8); #else *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8); #endif @@ -723,5 +784,78 @@ static int bpf_core_calc_field_relo(const char *prog_name, return -EOPNOTSUPP; } + return 0; +} + +static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, + __u64 *val, bool *validate) +{ + __s64 sz; + + /* by default, always check expected value in bpf_insn */ + if (validate) + *validate = true; + + /* type-based relos return zero when target type is not found */ + if (!spec) + { + *val = 0; + return 0; + } + + switch (relo->kind) + { + case BPF_CORE_TYPE_ID_TARGET: + *val = spec->root_type_id; + /* type ID, embedded in bpf_insn, might change during linking, + * so enforcing it is pointless + */ + if (validate) + *validate = false; + break; + case BPF_CORE_TYPE_EXISTS: + case BPF_CORE_TYPE_MATCHES: + *val = 1; + break; + case BPF_CORE_TYPE_SIZE: + sz = btf__resolve_size(spec->btf, spec->root_type_id); + if (sz < 0) + return -EINVAL; + *val = sz; + break; + case BPF_CORE_TYPE_ID_LOCAL: + /* BPF_CORE_TYPE_ID_LOCAL is handled specially and shouldn't get here */ + default: + return -EOPNOTSUPP; + } + + return 0; +} + +static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, + __u64 *val) +{ + const struct btf_type *t; + + switch (relo->kind) + { + case BPF_CORE_ENUMVAL_EXISTS: + *val = spec ? 1 : 0; + break; + case BPF_CORE_ENUMVAL_VALUE: + if (!spec) + return -EUCLEAN; /* request instruction poisoning */ + t = btf_type_by_id(spec->btf, spec->spec[0].type_id); + if (btf_is_enum(t)) + *val = btf_enum(t)[spec->spec[0].idx].val; + else + *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx); + break; + default: + return -EOPNOTSUPP; + } + return 0; } \ No newline at end of file -- Gitee