Skip to content

Commit

Permalink
fix compile of error inject
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Nov 22, 2023
1 parent 12ebcfd commit 227c0ba
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 10,597 deletions.
2 changes: 2 additions & 0 deletions example/error-inject/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error_inject
.output/
141 changes: 141 additions & 0 deletions example/error-inject/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../third_party/libbpf/src)
BPFTOOL_SRC := $(abspath ../../third_party/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
| sed 's/aarch64/arm64/' \
| sed 's/ppc64le/powerpc/' \
| sed 's/mips.*/mips/' \
| sed 's/riscv64/riscv/' \
| sed 's/loongarch64/loongarch/')
VMLINUX := ../../third_party/vmlinux/$(ARCH)/vmlinux.h
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
INCLUDES := -I$(OUTPUT) -I../../third_party/libbpf/include/uapi -I$(dir $(VMLINUX))
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS = error_inject # uprobe_legacy uprobe kprobe fentry usdt sockfilter tc ksyscall

CARGO ?= $(shell which cargo)
ifeq ($(strip $(CARGO)),)
BZS_APPS :=
else
BZS_APPS := # profile
APPS += $(BZS_APPS)
# Required by libblazesym
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
endif

# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
# architecture-specific dirs will be "missing" on some architectures/distros -
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
# sys/cdefs.h etc. might be missing.
#
# Use '-idirafter': Don't interfere with include mechanics except where the
# build would have failed anyways.
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

ifeq ($(V),1)
Q =
msg =
else
Q = @
msg = @printf ' %-8s %s%s\n' \
"$(1)" \
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
"$(if $(3), $(3))";
MAKEFLAGS += --no-print-directory
endif

define allow-override
$(if $(or $(findstring environment,$(origin $(1))),\
$(findstring command line,$(origin $(1)))),,\
$(eval $(1) = $(2)))
endef

$(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS) victim

victim: victim.c
gcc victim.c -o victim -Wall -g

.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS)

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
$(Q)mkdir -p $@

# Build libbpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
$(call msg,LIB,$@)
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
INCLUDEDIR= LIBDIR= UAPIDIR= \
install

# Build bpftool
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(call msg,BPFTOOL,$@)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap


$(LIBBLAZESYM_SRC)/target/release/libblazesym.a::
$(Q)cd $(LIBBLAZESYM_SRC) && $(CARGO) build --features=cheader,dont-generate-test-files --release

$(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
$(call msg,LIB, $@)
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym.a $@

$(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
$(call msg,LIB,$@)
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/blazesym.h $@

# Build BPF code
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
$(call msg,BPF,$@)
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)

# Generate BPF skeletons
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

# Build user-space code
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h

$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@

$(patsubst %,$(OUTPUT)/%.o,$(BZS_APPS)): $(LIBBLAZESYM_HEADER)

$(BZS_APPS): $(LIBBLAZESYM_OBJ)

# Build application binary
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@

# delete failed targets
.DELETE_ON_ERROR:

# keep intermediate (.skel.h, .bpf.o, etc) targets
.SECONDARY:
81 changes: 81 additions & 0 deletions example/error-inject/attach_override.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifndef BPFTIME_UREPLACE_ATTACH_H
#define BPFTIME_UREPLACE_ATTACH_H

#include <unistd.h>
#include <stdlib.h>
#include <syscall.h>
#include <linux/perf_event.h>
#include <linux/bpf.h>
#include <bpf/bpf.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

long elf_find_func_offset_from_file(const char *binary_path, const char *name);

#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
#define BPF_TYPE_UPROBE_OVERRIDE 8

static inline __u64 ptr_to_u64(const void *ptr)
{
return (__u64)(unsigned long)ptr;
}

static int perf_event_open_with_override(const char *name, uint64_t offset,
int pid, size_t ref_ctr_off, int type)
{
const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr;
int pfd;

if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
return -EINVAL;

memset(&attr, 0, attr_sz);

attr.size = attr_sz;
attr.type = type;
attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
attr.config2 = offset; /* kprobe_addr or probe_offset */

/* pid filter is meaningful only for uprobes */
pfd = syscall(__NR_perf_event_open, &attr, pid < 0 ? -1 : pid /* pid */,
pid == -1 ? 0 : -1 /* cpu */, -1 /* group_fd */,
PERF_FLAG_FD_CLOEXEC);
return pfd >= 0 ? pfd : -errno;
}

static int bpf_prog_attach_with_override(int prog_fd, const char *binary_path,
const char *name, int type)
{
int offset = elf_find_func_offset_from_file(binary_path, name);
if (offset < 0) {
return offset;
}
printf("offset: %d", offset);
int res =
perf_event_open_with_override(binary_path, offset, -1, 0, type);
if (res < 0) {
printf("perf_event_open_error_inject failed: %d\n", res);
return res;
}
res = bpf_prog_attach(prog_fd, res, BPF_MODIFY_RETURN, 0);
if (res < 0) {
printf("bpf_prog_attach failed: %d\n", res);
return res;
}
return 0;
}

static int bpf_prog_attach_uprobe_with_override(int prog_fd,
const char *binary_path,
const char *name)
{
return bpf_prog_attach_with_override(prog_fd, binary_path, name,
BPF_TYPE_UPROBE_OVERRIDE);
}

#endif // BPFTIME_UREPLACE_ATTACH_H
14 changes: 14 additions & 0 deletions example/error-inject/error_inject.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define BPF_NO_GLOBAL_DATA
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

SEC("uprobe")
int do_error_inject_patch(struct pt_regs *ctx)
{
bpf_printk("target_func called is overrided.\n");
bpf_override_return(ctx, 0);
return 0;
}

char LICENSE[] SEC("license") = "GPL";
70 changes: 70 additions & 0 deletions example/error-inject/error_inject.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2020 Facebook */
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include <unistd.h>
#include <stdlib.h>
#include "error_inject.skel.h"
#include <inttypes.h>
#include "attach_override.h"

#define warn(...) fprintf(stderr, __VA_ARGS__)

static int libbpf_print_fn(enum libbpf_print_level level, const char *format,
va_list args)
{
return vfprintf(stderr, format, args);
}

static volatile bool exiting = false;

static void sig_handler(int sig)
{
exiting = true;
}

int main(int argc, char **argv)
{
struct error_inject_bpf *skel;
int err;

/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);

/* Cleaner handling of Ctrl-C */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);

/* Load and verify BPF application */
skel = error_inject_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open and load BPF skeleton\n");
return 1;
}

/* Load & verify BPF programs */
err = error_inject_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}
err = bpf_prog_attach_uprobe_with_override(
bpf_program__fd(skel->progs.do_error_inject_patch), "./victim",
"target_func");
if (err) {
fprintf(stderr, "Failed to attach BPF program\n");
goto cleanup;
}
while (!exiting) {
sleep(1);
}
cleanup:
/* Clean up */
error_inject_bpf__destroy(skel);
return err < 0 ? -err : 0;
}
21 changes: 21 additions & 0 deletions example/fs-filter-cache/fs-filter-cache.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ int tracepoint__syscalls__sys_enter_openat(struct trace_event_raw_sys_enter *ctx
return 0;
}

SEC("tracepoint/syscalls/sys_enter_newfstatat")
int tracepoint__syscalls__sys_enter_newfstatat(struct trace_event_raw_sys_enter *ctx)
{
bpf_printk("trace_enter sys_enter_newfstatat\n");
return 0;
}

SEC("tracepoint/syscalls/sys_enter_statfs")
int tracepoint__syscalls__sys_enter_statfs(struct trace_event_raw_sys_enter *ctx)
{
bpf_printk("trace_enter sys_enter_statfs\n");
return 0;
}

SEC("tracepoint/syscalls/sys_enter_getdents64")
int tracepoint__syscalls__sys_enter_getdents64(struct trace_event_raw_sys_enter *ctx)
{
bpf_printk("trace_enter sys_enter_getdents64\n");
return 0;
}

static __always_inline int trace_exit(struct trace_event_raw_sys_exit *ctx)
{
bpf_printk("trace_exit\n");
Expand Down
2 changes: 1 addition & 1 deletion runtime/test/bpf/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.o
*.btf
*.a
/.output
.output
Loading

0 comments on commit 227c0ba

Please sign in to comment.