linux-stable/tools/lib/bpf
Andrii Nakryiko 9fdc4273b8 libbpf: Fix up verifier log for unguarded failed CO-RE relos
Teach libbpf to post-process BPF verifier log on BPF program load
failure and detect known error patterns to provide user with more
context.

Currently there is one such common situation: an "unguarded" failed BPF
CO-RE relocation. While failing CO-RE relocation is expected, it is
expected to be property guarded in BPF code such that BPF verifier
always eliminates BPF instructions corresponding to such failed CO-RE
relos as dead code. In cases when user failed to take such precautions,
BPF verifier provides the best log it can:

  123: (85) call unknown#195896080
  invalid func unknown#195896080

Such incomprehensible log error is due to libbpf "poisoning" BPF
instruction that corresponds to failed CO-RE relocation by replacing it
with invalid `call 0xbad2310` instruction (195896080 == 0xbad2310 reads
"bad relo" if you squint hard enough).

Luckily, libbpf has all the necessary information to look up CO-RE
relocation that failed and provide more human-readable description of
what's going on:

  5: <invalid CO-RE relocation>
  failed to resolve CO-RE relocation <byte_off> [6] struct task_struct___bad.fake_field_subprog (0:2 @ offset 8)

This hopefully makes it much easier to understand what's wrong with
user's BPF program without googling magic constants.

This BPF verifier log fixup is setup to be extensible and is going to be
used for at least one other upcoming feature of libbpf in follow up patches.
Libbpf is parsing lines of BPF verifier log starting from the very end.
Currently it processes up to 10 lines of code looking for familiar
patterns. This avoids wasting lots of CPU processing huge verifier logs
(especially for log_level=2 verbosity level). Actual verification error
should normally be found in last few lines, so this should work
reliably.

If libbpf needs to expand log beyond available log_buf_size, it
truncates the end of the verifier log. Given verifier log normally ends
with something like:

  processed 2 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0

... truncating this on program load error isn't too bad (end user can
always increase log size, if it needs to get complete log).

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220426004511.2691730-10-andrii@kernel.org
2022-04-26 15:41:46 -07:00
..
.gitignore libbpf: Make libbpf_version.h non-auto-generated 2021-09-13 15:36:47 -07:00
bpf.c libbpf: Teach bpf_link_create() to fallback to bpf_raw_tracepoint_open() 2022-04-23 00:37:02 +02:00
bpf.h libbpf: Add bpf_link_create support for multi kprobes 2022-03-17 20:17:19 -07:00
bpf_core_read.h libbpf: Fix endianness detection in BPF_CORE_READ_BITFIELD_PROBED() 2021-10-25 20:39:41 -07:00
bpf_endian.h libbpf: Make bpf_endian co-exist with vmlinux.h 2020-07-01 09:06:12 +02:00
bpf_gen_internal.h Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next 2021-12-10 15:56:13 -08:00
bpf_helpers.h libbpf: Add kptr type tag macros to bpf_helpers.h 2022-04-25 20:26:44 -07:00
bpf_prog_linfo.c libbpf: Streamline error reporting for high-level APIs 2021-05-25 17:32:35 -07:00
bpf_tracing.h libbpf: Add ARC support to bpf_tracing.h 2022-04-10 18:53:37 -07:00
btf.c libbpf: Avoid joining .BTF.ext data with BPF programs by section name 2022-04-26 15:41:46 -07:00
btf.h libbpf: Deprecate forgotten btf__get_map_kv_tids() 2022-02-04 01:07:16 +01:00
btf_dump.c libbpf: Skip forward declaration when counting duplicated type names 2022-03-01 13:40:57 +01:00
Build libbpf: Wire up USDT API and bpf_link integration 2022-04-05 13:16:07 -07:00
gen_loader.c libbpf: Prepare light skeleton for the kernel. 2022-02-10 23:31:51 +01:00
hashmap.c libbpf: Use IS_ERR_OR_NULL() in hashmap__free() 2022-01-12 17:01:36 -08:00
hashmap.h libbpf, hashmap: Fix undefined behavior in hash_bits 2020-11-02 23:33:51 +01:00
libbpf.c libbpf: Fix up verifier log for unguarded failed CO-RE relos 2022-04-26 15:41:46 -07:00
libbpf.h libbpf: Add documentation to API functions 2022-04-21 16:31:07 +02:00
libbpf.map libbpf: Wire up USDT API and bpf_link integration 2022-04-05 13:16:07 -07:00
libbpf.pc.template libbpf: Add zlib as a dependency in pkg-config template 2019-12-16 14:55:29 -08:00
libbpf_common.h libbpf: Deprecate bpf_prog_load_xattr() API 2021-12-02 15:23:41 -08:00
libbpf_errno.c libbpf: Streamline error reporting for high-level APIs 2021-05-25 17:32:35 -07:00
libbpf_internal.h libbpf: Avoid joining .BTF.ext data with BPF programs by section name 2022-04-26 15:41:46 -07:00
libbpf_legacy.h libbpf: .text routines are subprograms in strict mode 2022-03-17 23:11:15 -07:00
libbpf_probes.c libbpf: Improve LINUX_VERSION_CODE detection 2021-12-28 19:20:31 -08:00
libbpf_version.h libbpf: Support custom SEC() handlers 2022-03-05 09:38:15 -08:00
linker.c libbpf: Fix using invalidated memory in bpf_linker 2021-11-26 00:15:03 +01:00
Makefile Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next 2022-04-08 17:07:29 -07:00
netlink.c libbpf: Fix memleak in libbpf_netlink_recv() 2022-02-17 16:09:07 +01:00
nlattr.c libbpf: Switch to void * casting in netlink helpers 2021-06-22 17:04:02 +02:00
nlattr.h libbpf: Switch to void * casting in netlink helpers 2021-06-22 17:04:02 +02:00
relo_core.c libbpf: Fix up verifier log for unguarded failed CO-RE relos 2022-04-26 15:41:46 -07:00
relo_core.h libbpf: Fix up verifier log for unguarded failed CO-RE relos 2022-04-26 15:41:46 -07:00
ringbuf.c libbpf: Streamline error reporting for high-level APIs 2021-05-25 17:32:35 -07:00
skel_internal.h libbpf: Prepare light skeleton for the kernel. 2022-02-10 23:31:51 +01:00
str_error.c libbpf: Poison kernel-only integer types 2020-01-10 10:38:00 -08:00
str_error.h
strset.c libbpf: Fix memory leak in strset 2021-10-01 22:54:38 +02:00
strset.h libbpf: Extract internal set-of-strings datastructure APIs 2021-03-18 16:14:22 -07:00
usdt.bpf.h libbpf: Use weak hidden modifier for USDT BPF-side API functions 2022-04-08 22:24:15 +02:00
usdt.c libbpf: Support riscv USDT argument parsing logic 2022-04-19 21:59:35 -07:00
xsk.c libbpf: Fix array_size.cocci warning 2022-03-07 22:13:00 -08:00
xsk.h libbpf: Deprecate AF_XDP support 2021-11-01 18:12:44 -07:00