Commit b6b466a8 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'libbpf: split BTF support'

Andrii Nakryiko says:

====================
This patch set adds support for generating and deduplicating split BTF. This
is an enhancement to the BTF, which allows to designate one BTF as the "base
BTF" (e.g., vmlinux BTF), and one or more other BTFs as "split BTF" (e.g.,
kernel module BTF), which are building upon and extending base BTF with extra
types and strings.

Once loaded, split BTF appears as a single unified BTF superset of base BTF,
with continuous and transparent numbering scheme. This allows all the existing
users of BTF to work correctly and stay agnostic to the base/split BTFs
composition.  The only difference is in how to instantiate split BTF: it
requires base BTF to be alread instantiated and passed to btf__new_xxx_split()
or btf__parse_xxx_split() "constructors" explicitly.

This split approach is necessary if we are to have a reasonably-sized kernel
module BTFs. By deduping each kernel module's BTF individually, resulting
module BTFs contain copies of a lot of kernel types that are already present
in vmlinux BTF. Even those single copies result in a big BTF size bloat. On my
kernel configuration with 700 modules built, non-split BTF approach results in
115MBs of BTFs across all modules. With split BTF deduplication approach,
total size is down to 5.2MBs total, which is on part with vmlinux BTF (at
around 4MBs). This seems reasonable and practical. As to why we'd need kernel
module BTFs, that should be pretty obvious to anyone using BPF at this point,
as it allows all the BTF-powered features to be used with kernel modules:
tp_btf, fentry/fexit/fmod_ret, lsm, bpf_iter, etc.

This patch set is a pre-requisite to adding split BTF support to pahole, which
is a prerequisite to integrating split BTF into the Linux kernel build setup
to generate BTF for kernel modules. The latter will come as a follow-up patch
series once this series makes it to the libbpf and pahole makes use of it.

Patch #4 introduces necessary basic support for split BTF into libbpf APIs.
Patch #8 implements minimal changes to BTF dedup algorithm to allow
deduplicating split BTFs. Patch #11 adds extra -B flag to bpftool to allow to
specify the path to base BTF for cases when one wants to dump or inspect split
BTF. All the rest are refactorings, clean ups, bug fixes and selftests.

v1->v2:
  - addressed Song's feedback.
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents d0b3d2d7 75fa1777
...@@ -358,8 +358,12 @@ static int dump_btf_raw(const struct btf *btf, ...@@ -358,8 +358,12 @@ static int dump_btf_raw(const struct btf *btf,
} }
} else { } else {
int cnt = btf__get_nr_types(btf); int cnt = btf__get_nr_types(btf);
int start_id = 1;
for (i = 1; i <= cnt; i++) { if (base_btf)
start_id = btf__get_nr_types(base_btf) + 1;
for (i = start_id; i <= cnt; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
dump_btf_type(btf, i, t); dump_btf_type(btf, i, t);
} }
...@@ -438,7 +442,6 @@ static int do_dump(int argc, char **argv) ...@@ -438,7 +442,6 @@ static int do_dump(int argc, char **argv)
return -1; return -1;
} }
src = GET_ARG(); src = GET_ARG();
if (is_prefix(src, "map")) { if (is_prefix(src, "map")) {
struct bpf_map_info info = {}; struct bpf_map_info info = {};
__u32 len = sizeof(info); __u32 len = sizeof(info);
...@@ -499,7 +502,7 @@ static int do_dump(int argc, char **argv) ...@@ -499,7 +502,7 @@ static int do_dump(int argc, char **argv)
} }
NEXT_ARG(); NEXT_ARG();
} else if (is_prefix(src, "file")) { } else if (is_prefix(src, "file")) {
btf = btf__parse(*argv, NULL); btf = btf__parse_split(*argv, base_btf);
if (IS_ERR(btf)) { if (IS_ERR(btf)) {
err = -PTR_ERR(btf); err = -PTR_ERR(btf);
btf = NULL; btf = NULL;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/btf.h>
#include "main.h" #include "main.h"
...@@ -28,6 +29,7 @@ bool show_pinned; ...@@ -28,6 +29,7 @@ bool show_pinned;
bool block_mount; bool block_mount;
bool verifier_logs; bool verifier_logs;
bool relaxed_maps; bool relaxed_maps;
struct btf *base_btf;
struct pinned_obj_table prog_table; struct pinned_obj_table prog_table;
struct pinned_obj_table map_table; struct pinned_obj_table map_table;
struct pinned_obj_table link_table; struct pinned_obj_table link_table;
...@@ -391,6 +393,7 @@ int main(int argc, char **argv) ...@@ -391,6 +393,7 @@ int main(int argc, char **argv)
{ "mapcompat", no_argument, NULL, 'm' }, { "mapcompat", no_argument, NULL, 'm' },
{ "nomount", no_argument, NULL, 'n' }, { "nomount", no_argument, NULL, 'n' },
{ "debug", no_argument, NULL, 'd' }, { "debug", no_argument, NULL, 'd' },
{ "base-btf", required_argument, NULL, 'B' },
{ 0 } { 0 }
}; };
int opt, ret; int opt, ret;
...@@ -407,7 +410,7 @@ int main(int argc, char **argv) ...@@ -407,7 +410,7 @@ int main(int argc, char **argv)
hash_init(link_table.table); hash_init(link_table.table);
opterr = 0; opterr = 0;
while ((opt = getopt_long(argc, argv, "Vhpjfmnd", while ((opt = getopt_long(argc, argv, "VhpjfmndB:",
options, NULL)) >= 0) { options, NULL)) >= 0) {
switch (opt) { switch (opt) {
case 'V': case 'V':
...@@ -441,6 +444,15 @@ int main(int argc, char **argv) ...@@ -441,6 +444,15 @@ int main(int argc, char **argv)
libbpf_set_print(print_all_levels); libbpf_set_print(print_all_levels);
verifier_logs = true; verifier_logs = true;
break; break;
case 'B':
base_btf = btf__parse(optarg, NULL);
if (libbpf_get_error(base_btf)) {
p_err("failed to parse base BTF at '%s': %ld\n",
optarg, libbpf_get_error(base_btf));
base_btf = NULL;
return -1;
}
break;
default: default:
p_err("unrecognized option '%s'", argv[optind - 1]); p_err("unrecognized option '%s'", argv[optind - 1]);
if (json_output) if (json_output)
...@@ -465,6 +477,7 @@ int main(int argc, char **argv) ...@@ -465,6 +477,7 @@ int main(int argc, char **argv)
delete_pinned_obj_table(&map_table); delete_pinned_obj_table(&map_table);
delete_pinned_obj_table(&link_table); delete_pinned_obj_table(&link_table);
} }
btf__free(base_btf);
return ret; return ret;
} }
...@@ -90,6 +90,7 @@ extern bool show_pids; ...@@ -90,6 +90,7 @@ extern bool show_pids;
extern bool block_mount; extern bool block_mount;
extern bool verifier_logs; extern bool verifier_logs;
extern bool relaxed_maps; extern bool relaxed_maps;
extern struct btf *base_btf;
extern struct pinned_obj_table prog_table; extern struct pinned_obj_table prog_table;
extern struct pinned_obj_table map_table; extern struct pinned_obj_table map_table;
extern struct pinned_obj_table link_table; extern struct pinned_obj_table link_table;
......
This diff is collapsed.
...@@ -31,11 +31,19 @@ enum btf_endianness { ...@@ -31,11 +31,19 @@ enum btf_endianness {
}; };
LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API void btf__free(struct btf *btf);
LIBBPF_API struct btf *btf__new(const void *data, __u32 size); LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
LIBBPF_API struct btf *btf__new_empty(void); LIBBPF_API struct btf *btf__new_empty(void);
LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext); LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext); LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
LIBBPF_API struct btf *btf__parse_raw(const char *path); LIBBPF_API struct btf *btf__parse_raw(const char *path);
LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
LIBBPF_API int btf__load(struct btf *btf); LIBBPF_API int btf__load(struct btf *btf);
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
......
...@@ -337,3 +337,12 @@ LIBBPF_0.2.0 { ...@@ -337,3 +337,12 @@ LIBBPF_0.2.0 {
perf_buffer__consume_buffer; perf_buffer__consume_buffer;
xsk_socket__create_shared; xsk_socket__create_shared;
} LIBBPF_0.1.0; } LIBBPF_0.1.0;
LIBBPF_0.3.0 {
global:
btf__parse_elf_split;
btf__parse_raw_split;
btf__parse_split;
btf__new_empty_split;
btf__new_split;
} LIBBPF_0.2.0;
...@@ -386,7 +386,7 @@ TRUNNER_TESTS_DIR := prog_tests ...@@ -386,7 +386,7 @@ TRUNNER_TESTS_DIR := prog_tests
TRUNNER_BPF_PROGS_DIR := progs TRUNNER_BPF_PROGS_DIR := progs
TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
network_helpers.c testing_helpers.c \ network_helpers.c testing_helpers.c \
flow_dissector_load.h btf_helpers.c flow_dissector_load.h
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
$(wildcard progs/btf_dump_test_case_*.c) $(wildcard progs/btf_dump_test_case_*.c)
TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
......
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include <stdio.h>
#include <errno.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h>
#include "test_progs.h"
static const char * const btf_kind_str_mapping[] = {
[BTF_KIND_UNKN] = "UNKNOWN",
[BTF_KIND_INT] = "INT",
[BTF_KIND_PTR] = "PTR",
[BTF_KIND_ARRAY] = "ARRAY",
[BTF_KIND_STRUCT] = "STRUCT",
[BTF_KIND_UNION] = "UNION",
[BTF_KIND_ENUM] = "ENUM",
[BTF_KIND_FWD] = "FWD",
[BTF_KIND_TYPEDEF] = "TYPEDEF",
[BTF_KIND_VOLATILE] = "VOLATILE",
[BTF_KIND_CONST] = "CONST",
[BTF_KIND_RESTRICT] = "RESTRICT",
[BTF_KIND_FUNC] = "FUNC",
[BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
[BTF_KIND_VAR] = "VAR",
[BTF_KIND_DATASEC] = "DATASEC",
};
static const char *btf_kind_str(__u16 kind)
{
if (kind > BTF_KIND_DATASEC)
return "UNKNOWN";
return btf_kind_str_mapping[kind];
}
static const char *btf_int_enc_str(__u8 encoding)
{
switch (encoding) {
case 0:
return "(none)";
case BTF_INT_SIGNED:
return "SIGNED";
case BTF_INT_CHAR:
return "CHAR";
case BTF_INT_BOOL:
return "BOOL";
default:
return "UNKN";
}
}
static const char *btf_var_linkage_str(__u32 linkage)
{
switch (linkage) {
case BTF_VAR_STATIC:
return "static";
case BTF_VAR_GLOBAL_ALLOCATED:
return "global-alloc";
default:
return "(unknown)";
}
}
static const char *btf_func_linkage_str(const struct btf_type *t)
{
switch (btf_vlen(t)) {
case BTF_FUNC_STATIC:
return "static";
case BTF_FUNC_GLOBAL:
return "global";
case BTF_FUNC_EXTERN:
return "extern";
default:
return "(unknown)";
}
}
static const char *btf_str(const struct btf *btf, __u32 off)
{
if (!off)
return "(anon)";
return btf__str_by_offset(btf, off) ?: "(invalid)";
}
int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
{
const struct btf_type *t;
int kind, i;
__u32 vlen;
t = btf__type_by_id(btf, id);
if (!t)
return -EINVAL;
vlen = btf_vlen(t);
kind = btf_kind(t);
fprintf(out, "[%u] %s '%s'", id, btf_kind_str(kind), btf_str(btf, t->name_off));
switch (kind) {
case BTF_KIND_INT:
fprintf(out, " size=%u bits_offset=%u nr_bits=%u encoding=%s",
t->size, btf_int_offset(t), btf_int_bits(t),
btf_int_enc_str(btf_int_encoding(t)));
break;
case BTF_KIND_PTR:
case BTF_KIND_CONST:
case BTF_KIND_VOLATILE:
case BTF_KIND_RESTRICT:
case BTF_KIND_TYPEDEF:
fprintf(out, " type_id=%u", t->type);
break;
case BTF_KIND_ARRAY: {
const struct btf_array *arr = btf_array(t);
fprintf(out, " type_id=%u index_type_id=%u nr_elems=%u",
arr->type, arr->index_type, arr->nelems);
break;
}
case BTF_KIND_STRUCT:
case BTF_KIND_UNION: {
const struct btf_member *m = btf_members(t);
fprintf(out, " size=%u vlen=%u", t->size, vlen);
for (i = 0; i < vlen; i++, m++) {
__u32 bit_off, bit_sz;
bit_off = btf_member_bit_offset(t, i);
bit_sz = btf_member_bitfield_size(t, i);
fprintf(out, "\n\t'%s' type_id=%u bits_offset=%u",
btf_str(btf, m->name_off), m->type, bit_off);
if (bit_sz)
fprintf(out, " bitfield_size=%u", bit_sz);
}
break;
}
case BTF_KIND_ENUM: {
const struct btf_enum *v = btf_enum(t);
fprintf(out, " size=%u vlen=%u", t->size, vlen);
for (i = 0; i < vlen; i++, v++) {
fprintf(out, "\n\t'%s' val=%u",
btf_str(btf, v->name_off), v->val);
}
break;
}
case BTF_KIND_FWD:
fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");
break;
case BTF_KIND_FUNC:
fprintf(out, " type_id=%u linkage=%s", t->type, btf_func_linkage_str(t));
break;
case BTF_KIND_FUNC_PROTO: {
const struct btf_param *p = btf_params(t);
fprintf(out, " ret_type_id=%u vlen=%u", t->type, vlen);
for (i = 0; i < vlen; i++, p++) {
fprintf(out, "\n\t'%s' type_id=%u",
btf_str(btf, p->name_off), p->type);
}
break;
}
case BTF_KIND_VAR:
fprintf(out, " type_id=%u, linkage=%s",
t->type, btf_var_linkage_str(btf_var(t)->linkage));
break;
case BTF_KIND_DATASEC: {
const struct btf_var_secinfo *v = btf_var_secinfos(t);
fprintf(out, " size=%u vlen=%u", t->size, vlen);
for (i = 0; i < vlen; i++, v++) {
fprintf(out, "\n\ttype_id=%u offset=%u size=%u",
v->type, v->offset, v->size);
}
break;
}
default:
break;
}
return 0;
}
/* Print raw BTF type dump into a local buffer and return string pointer back.
* Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
*/
const char *btf_type_raw_dump(const struct btf *btf, int type_id)
{
static char buf[16 * 1024];
FILE *buf_file;
buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
if (!buf_file) {
fprintf(stderr, "Failed to open memstream: %d\n", errno);
return NULL;
}
fprintf_btf_type_raw(buf_file, btf, type_id);
fflush(buf_file);
fclose(buf_file);
return buf;
}
int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
{
int i;
bool ok = true;
ASSERT_EQ(btf__get_nr_types(btf), nr_types, "btf_nr_types");
for (i = 1; i <= nr_types; i++) {
if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
ok = false;
}
return ok;
}
static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
{
vfprintf(ctx, fmt, args);
}
/* Print BTF-to-C dump into a local buffer and return string pointer back.
* Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
*/
const char *btf_type_c_dump(const struct btf *btf)
{
static char buf[16 * 1024];
FILE *buf_file;
struct btf_dump *d = NULL;
struct btf_dump_opts opts = {};
int err, i;
buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
if (!buf_file) {
fprintf(stderr, "Failed to open memstream: %d\n", errno);
return NULL;
}
opts.ctx = buf_file;
d = btf_dump__new(btf, NULL, &opts, btf_dump_printf);
if (libbpf_get_error(d)) {
fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
return NULL;
}
for (i = 1; i <= btf__get_nr_types(btf); i++) {
err = btf_dump__dump_type(d, i);
if (err) {
fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
return NULL;
}
}
fflush(buf_file);
fclose(buf_file);
return buf;
}
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2020 Facebook */
#ifndef __BTF_HELPERS_H
#define __BTF_HELPERS_H
#include <stdio.h>
#include <bpf/btf.h>
int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id);
const char *btf_type_raw_dump(const struct btf *btf, int type_id);
int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[]);
#define VALIDATE_RAW_BTF(btf, raw_types...) \
btf_validate_raw(btf, \
sizeof((const char *[]){raw_types})/sizeof(void *),\
(const char *[]){raw_types})
const char *btf_type_c_dump(const struct btf *btf);
#endif
...@@ -6652,7 +6652,7 @@ static void do_test_dedup(unsigned int test_num) ...@@ -6652,7 +6652,7 @@ static void do_test_dedup(unsigned int test_num)
const void *test_btf_data, *expect_btf_data; const void *test_btf_data, *expect_btf_data;
const char *ret_test_next_str, *ret_expect_next_str; const char *ret_test_next_str, *ret_expect_next_str;
const char *test_strs, *expect_strs; const char *test_strs, *expect_strs;
const char *test_str_cur, *test_str_end; const char *test_str_cur;
const char *expect_str_cur, *expect_str_end; const char *expect_str_cur, *expect_str_end;
unsigned int raw_btf_size; unsigned int raw_btf_size;
void *raw_btf; void *raw_btf;
...@@ -6719,12 +6719,18 @@ static void do_test_dedup(unsigned int test_num) ...@@ -6719,12 +6719,18 @@ static void do_test_dedup(unsigned int test_num)
goto done; goto done;
} }
test_str_cur = test_strs;
test_str_end = test_strs + test_hdr->str_len;
expect_str_cur = expect_strs; expect_str_cur = expect_strs;
expect_str_end = expect_strs + expect_hdr->str_len; expect_str_end = expect_strs + expect_hdr->str_len;
while (test_str_cur < test_str_end && expect_str_cur < expect_str_end) { while (expect_str_cur < expect_str_end) {
size_t test_len, expect_len; size_t test_len, expect_len;
int off;
off = btf__find_str(test_btf, expect_str_cur);
if (CHECK(off < 0, "exp str '%s' not found: %d\n", expect_str_cur, off)) {
err = -1;
goto done;
}
test_str_cur = btf__str_by_offset(test_btf, off);
test_len = strlen(test_str_cur); test_len = strlen(test_str_cur);
expect_len = strlen(expect_str_cur); expect_len = strlen(expect_str_cur);
...@@ -6741,15 +6747,8 @@ static void do_test_dedup(unsigned int test_num) ...@@ -6741,15 +6747,8 @@ static void do_test_dedup(unsigned int test_num)
err = -1; err = -1;
goto done; goto done;
} }
test_str_cur += test_len + 1;
expect_str_cur += expect_len + 1; expect_str_cur += expect_len + 1;
} }
if (CHECK(test_str_cur != test_str_end,
"test_str_cur:%p != test_str_end:%p",
test_str_cur, test_str_end)) {
err = -1;
goto done;
}
test_nr_types = btf__get_nr_types(test_btf); test_nr_types = btf__get_nr_types(test_btf);
expect_nr_types = btf__get_nr_types(expect_btf); expect_nr_types = btf__get_nr_types(expect_btf);
...@@ -6775,10 +6774,21 @@ static void do_test_dedup(unsigned int test_num) ...@@ -6775,10 +6774,21 @@ static void do_test_dedup(unsigned int test_num)
err = -1; err = -1;
goto done; goto done;
} }
if (CHECK(memcmp((void *)test_type, if (CHECK(btf_kind(test_type) != btf_kind(expect_type),
(void *)expect_type, "type %d kind: exp %d != got %u\n",
test_size), i, btf_kind(expect_type), btf_kind(test_type))) {
"type #%d: contents differ", i)) { err = -1;
goto done;
}
if (CHECK(test_type->info != expect_type->info,
"type %d info: exp %d != got %u\n",
i, expect_type->info, test_type->info)) {
err = -1;
goto done;
}
if (CHECK(test_type->size != expect_type->size,
"type %d size/type: exp %d != got %u\n",
i, expect_type->size, test_type->size)) {
err = -1; err = -1;
goto done; goto done;
} }
......
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include <test_progs.h>
#include <bpf/btf.h>
static char *dump_buf;
static size_t dump_buf_sz;
static FILE *dump_buf_file;
static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
{
vfprintf(ctx, fmt, args);
}
void test_btf_split() {
struct btf_dump_opts opts;
struct btf_dump *d = NULL;
const struct btf_type *t;
struct btf *btf1, *btf2;
int str_off, i, err;
btf1 = btf__new_empty();
if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
return;
btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */
btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
btf__add_ptr(btf1, 1); /* [2] ptr to int */
btf__add_struct(btf1, "s1", 4); /* [3] struct s1 { */
btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */
/* } */
btf2 = btf__new_empty_split(btf1);
if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
goto cleanup;
/* pointer size should be "inherited" from main BTF */
ASSERT_EQ(btf__pointer_size(btf2), 8, "inherit_ptr_sz");
str_off = btf__find_str(btf2, "int");
ASSERT_NEQ(str_off, -ENOENT, "str_int_missing");
t = btf__type_by_id(btf2, 1);
if (!ASSERT_OK_PTR(t, "int_type"))
goto cleanup;
ASSERT_EQ(btf_is_int(t), true, "int_kind");
ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "int", "int_name");
btf__add_struct(btf2, "s2", 16); /* [4] struct s2 { */
btf__add_field(btf2, "f1", 3, 0, 0); /* struct s1 f1; */
btf__add_field(btf2, "f2", 1, 32, 0); /* int f2; */
btf__add_field(btf2, "f3", 2, 64, 0); /* int *f3; */
/* } */
t = btf__type_by_id(btf1, 4);
ASSERT_NULL(t, "split_type_in_main");
t = btf__type_by_id(btf2, 4);
if (!ASSERT_OK_PTR(t, "split_struct_type"))
goto cleanup;
ASSERT_EQ(btf_is_struct(t), true, "split_struct_kind");
ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen");
ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name");
/* BTF-to-C dump of split BTF */
dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
return;
opts.ctx = dump_buf_file;
d = btf_dump__new(btf2, NULL, &opts, btf_dump_printf);
if (!ASSERT_OK_PTR(d, "btf_dump__new"))
goto cleanup;
for (i = 1; i <= btf__get_nr_types(btf2); i++) {
err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok");
}
fflush(dump_buf_file);
dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */
ASSERT_STREQ(dump_buf,
"struct s1 {\n"
" int f1;\n"
"};\n"
"\n"
"struct s2 {\n"
" struct s1 f1;\n"
" int f2;\n"
" int *f3;\n"
"};\n\n", "c_dump");
cleanup:
if (dump_buf_file)
fclose(dump_buf_file);
free(dump_buf);
btf_dump__free(d);
btf__free(btf1);
btf__free(btf2);
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* Copyright (c) 2020 Facebook */ /* Copyright (c) 2020 Facebook */
#include <test_progs.h> #include <test_progs.h>
#include <bpf/btf.h> #include <bpf/btf.h>
#include "btf_helpers.h"
static int duration = 0; static int duration = 0;
...@@ -39,6 +40,8 @@ void test_btf_write() { ...@@ -39,6 +40,8 @@ void test_btf_write() {
ASSERT_EQ(t->size, 4, "int_sz"); ASSERT_EQ(t->size, 4, "int_sz");
ASSERT_EQ(btf_int_encoding(t), BTF_INT_SIGNED, "int_enc"); ASSERT_EQ(btf_int_encoding(t), BTF_INT_SIGNED, "int_enc");
ASSERT_EQ(btf_int_bits(t), 32, "int_bits"); ASSERT_EQ(btf_int_bits(t), 32, "int_bits");
ASSERT_STREQ(btf_type_raw_dump(btf, 1),
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", "raw_dump");
/* invalid int size */ /* invalid int size */
id = btf__add_int(btf, "bad sz int", 7, 0); id = btf__add_int(btf, "bad sz int", 7, 0);
...@@ -59,24 +62,32 @@ void test_btf_write() { ...@@ -59,24 +62,32 @@ void test_btf_write() {
t = btf__type_by_id(btf, 2); t = btf__type_by_id(btf, 2);
ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_kind");
ASSERT_EQ(t->type, 1, "ptr_type"); ASSERT_EQ(t->type, 1, "ptr_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 2),
"[2] PTR '(anon)' type_id=1", "raw_dump");
id = btf__add_const(btf, 5); /* points forward to restrict */ id = btf__add_const(btf, 5); /* points forward to restrict */
ASSERT_EQ(id, 3, "const_id"); ASSERT_EQ(id, 3, "const_id");
t = btf__type_by_id(btf, 3); t = btf__type_by_id(btf, 3);
ASSERT_EQ(btf_kind(t), BTF_KIND_CONST, "const_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_CONST, "const_kind");
ASSERT_EQ(t->type, 5, "const_type"); ASSERT_EQ(t->type, 5, "const_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 3),
"[3] CONST '(anon)' type_id=5", "raw_dump");
id = btf__add_volatile(btf, 3); id = btf__add_volatile(btf, 3);
ASSERT_EQ(id, 4, "volatile_id"); ASSERT_EQ(id, 4, "volatile_id");
t = btf__type_by_id(btf, 4); t = btf__type_by_id(btf, 4);
ASSERT_EQ(btf_kind(t), BTF_KIND_VOLATILE, "volatile_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_VOLATILE, "volatile_kind");
ASSERT_EQ(t->type, 3, "volatile_type"); ASSERT_EQ(t->type, 3, "volatile_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 4),
"[4] VOLATILE '(anon)' type_id=3", "raw_dump");
id = btf__add_restrict(btf, 4); id = btf__add_restrict(btf, 4);
ASSERT_EQ(id, 5, "restrict_id"); ASSERT_EQ(id, 5, "restrict_id");
t = btf__type_by_id(btf, 5); t = btf__type_by_id(btf, 5);
ASSERT_EQ(btf_kind(t), BTF_KIND_RESTRICT, "restrict_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_RESTRICT, "restrict_kind");
ASSERT_EQ(t->type, 4, "restrict_type"); ASSERT_EQ(t->type, 4, "restrict_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 5),
"[5] RESTRICT '(anon)' type_id=4", "raw_dump");
/* ARRAY */ /* ARRAY */
id = btf__add_array(btf, 1, 2, 10); /* int *[10] */ id = btf__add_array(btf, 1, 2, 10); /* int *[10] */
...@@ -86,6 +97,8 @@ void test_btf_write() { ...@@ -86,6 +97,8 @@ void test_btf_write() {
ASSERT_EQ(btf_array(t)->index_type, 1, "array_index_type"); ASSERT_EQ(btf_array(t)->index_type, 1, "array_index_type");
ASSERT_EQ(btf_array(t)->type, 2, "array_elem_type"); ASSERT_EQ(btf_array(t)->type, 2, "array_elem_type");
ASSERT_EQ(btf_array(t)->nelems, 10, "array_nelems"); ASSERT_EQ(btf_array(t)->nelems, 10, "array_nelems");
ASSERT_STREQ(btf_type_raw_dump(btf, 6),
"[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10", "raw_dump");
/* STRUCT */ /* STRUCT */
err = btf__add_field(btf, "field", 1, 0, 0); err = btf__add_field(btf, "field", 1, 0, 0);
...@@ -113,6 +126,10 @@ void test_btf_write() { ...@@ -113,6 +126,10 @@ void test_btf_write() {
ASSERT_EQ(m->type, 1, "f2_type"); ASSERT_EQ(m->type, 1, "f2_type");
ASSERT_EQ(btf_member_bit_offset(t, 1), 32, "f2_bit_off"); ASSERT_EQ(btf_member_bit_offset(t, 1), 32, "f2_bit_off");
ASSERT_EQ(btf_member_bitfield_size(t, 1), 16, "f2_bit_sz"); ASSERT_EQ(btf_member_bitfield_size(t, 1), 16, "f2_bit_sz");
ASSERT_STREQ(btf_type_raw_dump(btf, 7),
"[7] STRUCT 's1' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32 bitfield_size=16", "raw_dump");
/* UNION */ /* UNION */
id = btf__add_union(btf, "u1", 8); id = btf__add_union(btf, "u1", 8);
...@@ -136,6 +153,9 @@ void test_btf_write() { ...@@ -136,6 +153,9 @@ void test_btf_write() {
ASSERT_EQ(m->type, 1, "f1_type"); ASSERT_EQ(m->type, 1, "f1_type");
ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off"); ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
ASSERT_EQ(btf_member_bitfield_size(t, 0), 16, "f1_bit_sz"); ASSERT_EQ(btf_member_bitfield_size(t, 0), 16, "f1_bit_sz");
ASSERT_STREQ(btf_type_raw_dump(btf, 8),
"[8] UNION 'u1' size=8 vlen=1\n"
"\t'f1' type_id=1 bits_offset=0 bitfield_size=16", "raw_dump");
/* ENUM */ /* ENUM */
id = btf__add_enum(btf, "e1", 4); id = btf__add_enum(btf, "e1", 4);
...@@ -156,6 +176,10 @@ void test_btf_write() { ...@@ -156,6 +176,10 @@ void test_btf_write() {
v = btf_enum(t) + 1; v = btf_enum(t) + 1;
ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v2", "v2_name"); ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v2", "v2_name");
ASSERT_EQ(v->val, 2, "v2_val"); ASSERT_EQ(v->val, 2, "v2_val");
ASSERT_STREQ(btf_type_raw_dump(btf, 9),
"[9] ENUM 'e1' size=4 vlen=2\n"
"\t'v1' val=1\n"
"\t'v2' val=2", "raw_dump");
/* FWDs */ /* FWDs */
id = btf__add_fwd(btf, "struct_fwd", BTF_FWD_STRUCT); id = btf__add_fwd(btf, "struct_fwd", BTF_FWD_STRUCT);
...@@ -164,6 +188,8 @@ void test_btf_write() { ...@@ -164,6 +188,8 @@ void test_btf_write() {
ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "struct_fwd", "fwd_name"); ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "struct_fwd", "fwd_name");
ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
ASSERT_EQ(btf_kflag(t), 0, "fwd_kflag"); ASSERT_EQ(btf_kflag(t), 0, "fwd_kflag");
ASSERT_STREQ(btf_type_raw_dump(btf, 10),
"[10] FWD 'struct_fwd' fwd_kind=struct", "raw_dump");
id = btf__add_fwd(btf, "union_fwd", BTF_FWD_UNION); id = btf__add_fwd(btf, "union_fwd", BTF_FWD_UNION);
ASSERT_EQ(id, 11, "union_fwd_id"); ASSERT_EQ(id, 11, "union_fwd_id");
...@@ -171,6 +197,8 @@ void test_btf_write() { ...@@ -171,6 +197,8 @@ void test_btf_write() {
ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "union_fwd", "fwd_name"); ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "union_fwd", "fwd_name");
ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
ASSERT_EQ(btf_kflag(t), 1, "fwd_kflag"); ASSERT_EQ(btf_kflag(t), 1, "fwd_kflag");
ASSERT_STREQ(btf_type_raw_dump(btf, 11),
"[11] FWD 'union_fwd' fwd_kind=union", "raw_dump");
id = btf__add_fwd(btf, "enum_fwd", BTF_FWD_ENUM); id = btf__add_fwd(btf, "enum_fwd", BTF_FWD_ENUM);
ASSERT_EQ(id, 12, "enum_fwd_id"); ASSERT_EQ(id, 12, "enum_fwd_id");
...@@ -179,6 +207,8 @@ void test_btf_write() { ...@@ -179,6 +207,8 @@ void test_btf_write() {
ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_fwd_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_fwd_kind");
ASSERT_EQ(btf_vlen(t), 0, "enum_fwd_kind"); ASSERT_EQ(btf_vlen(t), 0, "enum_fwd_kind");
ASSERT_EQ(t->size, 4, "enum_fwd_sz"); ASSERT_EQ(t->size, 4, "enum_fwd_sz");
ASSERT_STREQ(btf_type_raw_dump(btf, 12),
"[12] ENUM 'enum_fwd' size=4 vlen=0", "raw_dump");
/* TYPEDEF */ /* TYPEDEF */
id = btf__add_typedef(btf, "typedef1", 1); id = btf__add_typedef(btf, "typedef1", 1);
...@@ -187,6 +217,8 @@ void test_btf_write() { ...@@ -187,6 +217,8 @@ void test_btf_write() {
ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "typedef1", "typedef_name"); ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "typedef1", "typedef_name");
ASSERT_EQ(btf_kind(t), BTF_KIND_TYPEDEF, "typedef_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_TYPEDEF, "typedef_kind");
ASSERT_EQ(t->type, 1, "typedef_type"); ASSERT_EQ(t->type, 1, "typedef_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 13),
"[13] TYPEDEF 'typedef1' type_id=1", "raw_dump");
/* FUNC & FUNC_PROTO */ /* FUNC & FUNC_PROTO */
id = btf__add_func(btf, "func1", BTF_FUNC_GLOBAL, 15); id = btf__add_func(btf, "func1", BTF_FUNC_GLOBAL, 15);
...@@ -196,6 +228,8 @@ void test_btf_write() { ...@@ -196,6 +228,8 @@ void test_btf_write() {
ASSERT_EQ(t->type, 15, "func_type"); ASSERT_EQ(t->type, 15, "func_type");
ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC, "func_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC, "func_kind");
ASSERT_EQ(btf_vlen(t), BTF_FUNC_GLOBAL, "func_vlen"); ASSERT_EQ(btf_vlen(t), BTF_FUNC_GLOBAL, "func_vlen");
ASSERT_STREQ(btf_type_raw_dump(btf, 14),
"[14] FUNC 'func1' type_id=15 linkage=global", "raw_dump");
id = btf__add_func_proto(btf, 1); id = btf__add_func_proto(btf, 1);
ASSERT_EQ(id, 15, "func_proto_id"); ASSERT_EQ(id, 15, "func_proto_id");
...@@ -214,6 +248,10 @@ void test_btf_write() { ...@@ -214,6 +248,10 @@ void test_btf_write() {
p = btf_params(t) + 1; p = btf_params(t) + 1;
ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p2", "p2_name"); ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p2", "p2_name");
ASSERT_EQ(p->type, 2, "p2_type"); ASSERT_EQ(p->type, 2, "p2_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 15),
"[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
"\t'p1' type_id=1\n"
"\t'p2' type_id=2", "raw_dump");
/* VAR */ /* VAR */
id = btf__add_var(btf, "var1", BTF_VAR_GLOBAL_ALLOCATED, 1); id = btf__add_var(btf, "var1", BTF_VAR_GLOBAL_ALLOCATED, 1);
...@@ -223,6 +261,8 @@ void test_btf_write() { ...@@ -223,6 +261,8 @@ void test_btf_write() {
ASSERT_EQ(btf_kind(t), BTF_KIND_VAR, "var_kind"); ASSERT_EQ(btf_kind(t), BTF_KIND_VAR, "var_kind");
ASSERT_EQ(t->type, 1, "var_type"); ASSERT_EQ(t->type, 1, "var_type");
ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_type"); ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_type");
ASSERT_STREQ(btf_type_raw_dump(btf, 16),
"[16] VAR 'var1' type_id=1, linkage=global-alloc", "raw_dump");
/* DATASECT */ /* DATASECT */
id = btf__add_datasec(btf, "datasec1", 12); id = btf__add_datasec(btf, "datasec1", 12);
...@@ -239,6 +279,9 @@ void test_btf_write() { ...@@ -239,6 +279,9 @@ void test_btf_write() {
ASSERT_EQ(vi->type, 1, "v1_type"); ASSERT_EQ(vi->type, 1, "v1_type");
ASSERT_EQ(vi->offset, 4, "v1_off"); ASSERT_EQ(vi->offset, 4, "v1_off");
ASSERT_EQ(vi->size, 8, "v1_sz"); ASSERT_EQ(vi->size, 8, "v1_sz");
ASSERT_STREQ(btf_type_raw_dump(btf, 17),
"[17] DATASEC 'datasec1' size=12 vlen=1\n"
"\ttype_id=1 offset=4 size=8", "raw_dump");
btf__free(btf); btf__free(btf);
} }
...@@ -141,6 +141,17 @@ extern int test__join_cgroup(const char *path); ...@@ -141,6 +141,17 @@ extern int test__join_cgroup(const char *path);
___ok; \ ___ok; \
}) })
#define ASSERT_NEQ(actual, expected, name) ({ \
static int duration = 0; \
typeof(actual) ___act = (actual); \
typeof(expected) ___exp = (expected); \
bool ___ok = ___act != ___exp; \
CHECK(!___ok, (name), \
"unexpected %s: actual %lld == expected %lld\n", \
(name), (long long)(___act), (long long)(___exp)); \
___ok; \
})
#define ASSERT_STREQ(actual, expected, name) ({ \ #define ASSERT_STREQ(actual, expected, name) ({ \
static int duration = 0; \ static int duration = 0; \
const char *___act = actual; \ const char *___act = actual; \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment