Commit 518fee8b authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: make veristat skip non-BPF and failing-to-open BPF objects

Make veristat ignore non-BPF object files. This allows simpler
mass-verification (e.g., `sudo ./veristat *.bpf.o` in selftests/bpf
directory). Note that `sudo ./veristat *.o` would also work, but with
selftests's multiple copies of BPF object files (.bpf.o and
.bpf.linked{1,2,3}.o) it's 4x slower.

Also, given some of BPF object files could be incomplete in the sense
that they are meant to be statically linked into final BPF object file
(like linked_maps, linked_funcs, linked_vars), note such instances in
stderr, but proceed anyways. This seems like a better trade off between
completely silently ignoring BPF object file and aborting
mass-verification altogether.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20220923175913.3272430-4-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent c2488d70
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <libelf.h>
#include <gelf.h>
enum stat_id { enum stat_id {
VERDICT, VERDICT,
...@@ -78,6 +80,11 @@ static struct env { ...@@ -78,6 +80,11 @@ static struct env {
struct filter *deny_filters; struct filter *deny_filters;
int allow_filter_cnt; int allow_filter_cnt;
int deny_filter_cnt; int deny_filter_cnt;
int files_processed;
int files_skipped;
int progs_processed;
int progs_skipped;
} env; } env;
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
...@@ -226,8 +233,41 @@ static bool should_process_file(const char *filename) ...@@ -226,8 +233,41 @@ static bool should_process_file(const char *filename)
return false; return false;
} }
static bool should_process_prog(const char *filename, const char *prog_name) static bool is_bpf_obj_file(const char *path) {
Elf64_Ehdr *ehdr;
int fd, err = -EINVAL;
Elf *elf = NULL;
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return true; /* we'll fail later and propagate error */
/* ensure libelf is initialized */
(void)elf_version(EV_CURRENT);
elf = elf_begin(fd, ELF_C_READ, NULL);
if (!elf)
goto cleanup;
if (elf_kind(elf) != ELF_K_ELF || gelf_getclass(elf) != ELFCLASS64)
goto cleanup;
ehdr = elf64_getehdr(elf);
/* Old LLVM set e_machine to EM_NONE */
if (!ehdr || ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF))
goto cleanup;
err = 0;
cleanup:
if (elf)
elf_end(elf);
close(fd);
return err == 0;
}
static bool should_process_prog(const char *path, const char *prog_name)
{ {
const char *filename = basename(path);
int i; int i;
if (env.deny_filter_cnt > 0) { if (env.deny_filter_cnt > 0) {
...@@ -303,7 +343,7 @@ static int append_filter_file(const char *path) ...@@ -303,7 +343,7 @@ static int append_filter_file(const char *path)
f = fopen(path, "r"); f = fopen(path, "r");
if (!f) { if (!f) {
err = -errno; err = -errno;
fprintf(stderr, "Failed to open '%s': %d\n", path, err); fprintf(stderr, "Failed to open filters in '%s': %d\n", path, err);
return err; return err;
} }
...@@ -463,8 +503,10 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf ...@@ -463,8 +503,10 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
int err = 0; int err = 0;
void *tmp; void *tmp;
if (!should_process_prog(basename(filename), bpf_program__name(prog))) if (!should_process_prog(filename, bpf_program__name(prog))) {
env.progs_skipped++;
return 0; return 0;
}
tmp = realloc(env.prog_stats, (env.prog_stat_cnt + 1) * sizeof(*env.prog_stats)); tmp = realloc(env.prog_stats, (env.prog_stat_cnt + 1) * sizeof(*env.prog_stats));
if (!tmp) if (!tmp)
...@@ -487,6 +529,7 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf ...@@ -487,6 +529,7 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
verif_log_buf[0] = '\0'; verif_log_buf[0] = '\0';
err = bpf_object__load(obj); err = bpf_object__load(obj);
env.progs_processed++;
stats->file_name = strdup(basename(filename)); stats->file_name = strdup(basename(filename));
stats->prog_name = strdup(bpf_program__name(prog)); stats->prog_name = strdup(bpf_program__name(prog));
...@@ -513,18 +556,37 @@ static int process_obj(const char *filename) ...@@ -513,18 +556,37 @@ static int process_obj(const char *filename)
LIBBPF_OPTS(bpf_object_open_opts, opts); LIBBPF_OPTS(bpf_object_open_opts, opts);
int err = 0, prog_cnt = 0; int err = 0, prog_cnt = 0;
if (!should_process_file(basename(filename))) if (!should_process_file(basename(filename))) {
if (env.verbose)
printf("Skipping '%s' due to filters...\n", filename);
env.files_skipped++;
return 0;
}
if (!is_bpf_obj_file(filename)) {
if (env.verbose)
printf("Skipping '%s' as it's not a BPF object file...\n", filename);
env.files_skipped++;
return 0; return 0;
}
old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn); old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn);
obj = bpf_object__open_file(filename, &opts); obj = bpf_object__open_file(filename, &opts);
if (!obj) { if (!obj) {
err = -errno; /* if libbpf can't open BPF object file, it could be because
fprintf(stderr, "Failed to open '%s': %d\n", filename, err); * that BPF object file is incomplete and has to be statically
* linked into a final BPF object file; instead of bailing
* out, report it into stderr, mark it as skipped, and
* proceeed
*/
fprintf(stderr, "Failed to open '%s': %d\n", filename, -errno);
env.files_skipped++;
err = 0;
goto cleanup; goto cleanup;
} }
env.files_processed++;
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
prog_cnt++; prog_cnt++;
} }
...@@ -732,8 +794,8 @@ static void output_stats(const struct verif_stats *s, enum resfmt fmt, bool last ...@@ -732,8 +794,8 @@ static void output_stats(const struct verif_stats *s, enum resfmt fmt, bool last
if (last && fmt == RESFMT_TABLE) { if (last && fmt == RESFMT_TABLE) {
output_header_underlines(); output_header_underlines();
printf("Done. Processed %d object files, %d programs.\n", printf("Done. Processed %d files, %d programs. Skipped %d files, %d programs.\n",
env.filename_cnt, env.prog_stat_cnt); env.files_processed, env.files_skipped, env.progs_processed, env.progs_skipped);
} }
} }
......
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