libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal libbpf helpers.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Facebook
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LIBBPF_LIBBPF_INTERNAL_H
|
|
|
|
#define __LIBBPF_LIBBPF_INTERNAL_H
|
|
|
|
|
2019-05-24 18:58:56 +00:00
|
|
|
#include "libbpf.h"
|
|
|
|
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
#define BTF_INFO_ENC(kind, kind_flag, vlen) \
|
|
|
|
((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
|
|
|
|
#define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
|
|
|
|
#define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
|
|
|
|
((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
|
|
|
|
#define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
|
|
|
|
BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
|
|
|
|
BTF_INT_ENC(encoding, bits_offset, bits)
|
|
|
|
#define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
|
|
|
|
#define BTF_PARAM_ENC(name, type) (name), (type)
|
|
|
|
#define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
|
|
|
|
|
2019-06-17 19:26:50 +00:00
|
|
|
#ifndef min
|
|
|
|
# define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
#ifndef max
|
|
|
|
# define max(x, y) ((x) < (y) ? (y) : (x))
|
|
|
|
#endif
|
2019-08-07 21:39:50 +00:00
|
|
|
#ifndef offsetofend
|
|
|
|
# define offsetofend(TYPE, FIELD) \
|
|
|
|
(offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
|
|
|
|
#endif
|
2019-06-17 19:26:50 +00:00
|
|
|
|
2019-05-16 03:39:27 +00:00
|
|
|
extern void libbpf_print(enum libbpf_print_level level,
|
|
|
|
const char *format, ...)
|
|
|
|
__attribute__((format(printf, 2, 3)));
|
|
|
|
|
|
|
|
#define __pr(level, fmt, ...) \
|
|
|
|
do { \
|
|
|
|
libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define pr_warning(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
|
|
|
|
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
|
|
|
|
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
|
|
|
|
|
libbpf: add bpf_object__open_{file, mem} w/ extensible opts
Add new set of bpf_object__open APIs using new approach to optional
parameters extensibility allowing simpler ABI compatibility approach.
This patch demonstrates an approach to implementing libbpf APIs that
makes it easy to extend existing APIs with extra optional parameters in
such a way, that ABI compatibility is preserved without having to do
symbol versioning and generating lots of boilerplate code to handle it.
To facilitate succinct code for working with options, add OPTS_VALID,
OPTS_HAS, and OPTS_GET macros that hide all the NULL, size, and zero
checks.
Additionally, newly added libbpf APIs are encouraged to follow similar
pattern of having all mandatory parameters as formal function parameters
and always have optional (NULL-able) xxx_opts struct, which should
always have real struct size as a first field and the rest would be
optional parameters added over time, which tune the behavior of existing
API, if specified by user.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-10-04 22:40:35 +00:00
|
|
|
static inline bool libbpf_validate_opts(const char *opts,
|
|
|
|
size_t opts_sz, size_t user_sz,
|
|
|
|
const char *type_name)
|
|
|
|
{
|
|
|
|
if (user_sz < sizeof(size_t)) {
|
|
|
|
pr_warning("%s size (%zu) is too small\n", type_name, user_sz);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (user_sz > opts_sz) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = opts_sz; i < user_sz; i++) {
|
|
|
|
if (opts[i]) {
|
|
|
|
pr_warning("%s has non-zero extra bytes",
|
|
|
|
type_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPTS_VALID(opts, type) \
|
|
|
|
(!(opts) || libbpf_validate_opts((const char *)opts, \
|
|
|
|
offsetofend(struct type, \
|
|
|
|
type##__last_field), \
|
|
|
|
(opts)->sz, #type))
|
|
|
|
#define OPTS_HAS(opts, field) \
|
|
|
|
((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
|
|
|
|
#define OPTS_GET(opts, field, fallback_value) \
|
|
|
|
(OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
|
|
|
|
|
2019-05-29 18:31:09 +00:00
|
|
|
int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
|
|
|
const char *str_sec, size_t str_len);
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
|
2019-08-07 21:39:50 +00:00
|
|
|
struct btf_ext_info {
|
|
|
|
/*
|
|
|
|
* info points to the individual info section (e.g. func_info and
|
|
|
|
* line_info) from the .BTF.ext. It does not include the __u32 rec_size.
|
|
|
|
*/
|
|
|
|
void *info;
|
|
|
|
__u32 rec_size;
|
|
|
|
__u32 len;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define for_each_btf_ext_sec(seg, sec) \
|
|
|
|
for (sec = (seg)->info; \
|
|
|
|
(void *)sec < (seg)->info + (seg)->len; \
|
|
|
|
sec = (void *)sec + sizeof(struct btf_ext_info_sec) + \
|
|
|
|
(seg)->rec_size * sec->num_info)
|
|
|
|
|
|
|
|
#define for_each_btf_ext_rec(seg, sec, i, rec) \
|
|
|
|
for (i = 0, rec = (void *)&(sec)->data; \
|
|
|
|
i < (sec)->num_info; \
|
|
|
|
i++, rec = (void *)rec + (seg)->rec_size)
|
|
|
|
|
|
|
|
struct btf_ext {
|
|
|
|
union {
|
|
|
|
struct btf_ext_header *hdr;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
struct btf_ext_info func_info;
|
|
|
|
struct btf_ext_info line_info;
|
|
|
|
struct btf_ext_info offset_reloc_info;
|
|
|
|
__u32 data_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btf_ext_info_sec {
|
|
|
|
__u32 sec_name_off;
|
|
|
|
__u32 num_info;
|
|
|
|
/* Followed by num_info * record_size number of bytes */
|
|
|
|
__u8 data[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The minimum bpf_func_info checked by the loader */
|
|
|
|
struct bpf_func_info_min {
|
|
|
|
__u32 insn_off;
|
|
|
|
__u32 type_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The minimum bpf_line_info checked by the loader */
|
|
|
|
struct bpf_line_info_min {
|
|
|
|
__u32 insn_off;
|
|
|
|
__u32 file_name_off;
|
|
|
|
__u32 line_off;
|
|
|
|
__u32 line_col;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The minimum bpf_offset_reloc checked by the loader
|
|
|
|
*
|
|
|
|
* Offset relocation captures the following data:
|
|
|
|
* - insn_off - instruction offset (in bytes) within a BPF program that needs
|
|
|
|
* its insn->imm field to be relocated with actual offset;
|
|
|
|
* - type_id - BTF type ID of the "root" (containing) entity of a relocatable
|
|
|
|
* offset;
|
|
|
|
* - access_str_off - offset into corresponding .BTF string section. String
|
|
|
|
* itself encodes an accessed field using a sequence of field and array
|
|
|
|
* indicies, separated by colon (:). It's conceptually very close to LLVM's
|
|
|
|
* getelementptr ([0]) instruction's arguments for identifying offset to
|
|
|
|
* a field.
|
|
|
|
*
|
|
|
|
* Example to provide a better feel.
|
|
|
|
*
|
|
|
|
* struct sample {
|
|
|
|
* int a;
|
|
|
|
* struct {
|
|
|
|
* int b[10];
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* struct sample *s = ...;
|
|
|
|
* int x = &s->a; // encoded as "0:0" (a is field #0)
|
|
|
|
* int y = &s->b[5]; // encoded as "0:1:0:5" (anon struct is field #1,
|
|
|
|
* // b is field #0 inside anon struct, accessing elem #5)
|
|
|
|
* int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
|
|
|
|
*
|
|
|
|
* type_id for all relocs in this example will capture BTF type id of
|
|
|
|
* `struct sample`.
|
|
|
|
*
|
|
|
|
* Such relocation is emitted when using __builtin_preserve_access_index()
|
|
|
|
* Clang built-in, passing expression that captures field address, e.g.:
|
|
|
|
*
|
|
|
|
* bpf_probe_read(&dst, sizeof(dst),
|
|
|
|
* __builtin_preserve_access_index(&src->a.b.c));
|
|
|
|
*
|
|
|
|
* In this case Clang will emit offset relocation recording necessary data to
|
|
|
|
* be able to find offset of embedded `a.b.c` field within `src` struct.
|
|
|
|
*
|
|
|
|
* [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
|
|
|
|
*/
|
|
|
|
struct bpf_offset_reloc {
|
|
|
|
__u32 insn_off;
|
|
|
|
__u32 type_id;
|
|
|
|
__u32 access_str_off;
|
|
|
|
};
|
|
|
|
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
|