Add a higher-level test (C BPF program) for the new functionality - variable access stack reads and writes. Signed-off-by: Andrei Matei <andreimatei1@xxxxxxxxx> --- .../selftests/bpf/prog_tests/stack_var_off.c | 56 +++++++++++++++++++ .../selftests/bpf/progs/test_stack_var_off.c | 43 ++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/stack_var_off.c create mode 100644 tools/testing/selftests/bpf/progs/test_stack_var_off.c diff --git a/tools/testing/selftests/bpf/prog_tests/stack_var_off.c b/tools/testing/selftests/bpf/prog_tests/stack_var_off.c new file mode 100644 index 000000000000..c4c47fb0f0af --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/stack_var_off.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "test_stack_var_off.skel.h" + +int dummy; + +noinline void uprobed_function(char *s, int len) +{ + /* Do something to keep the compiler from removing the function. + */ + dummy++; +} + +void test_stack_var_off(void) +{ + int duration = 0; + struct bpf_link *uprobe_link; + struct test_stack_var_off *skel; + size_t uprobe_offset; + ssize_t base_addr; + char s[100]; + + base_addr = get_base_addr(); + if (CHECK(base_addr < 0, "get_base_addr", + "failed to find base addr: %zd", base_addr)) + return; + uprobe_offset = (size_t)&uprobed_function - base_addr; + + skel = test_stack_var_off__open_and_load(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + return; + if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n")) + goto cleanup; + + uprobe_link = bpf_program__attach_uprobe(skel->progs.uprobe, + false /* retprobe */, + 0 /* self pid */, + "/proc/self/exe", + uprobe_offset); + if (CHECK(IS_ERR(uprobe_link), "attach_uprobe", + "err %ld\n", PTR_ERR(uprobe_link))) + goto cleanup; + skel->links.uprobe = uprobe_link; + + /* trigger uprobe */ + s[0] = 1; + s[1] = 10; + uprobed_function(&s[0], 2); + + if (CHECK(skel->bss->uprobe_res != 10, "check_uprobe_res", + "wrong uprobe res: %d\n", skel->bss->uprobe_res)) + goto cleanup; + +cleanup: + test_stack_var_off__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_stack_var_off.c b/tools/testing/selftests/bpf/progs/test_stack_var_off.c new file mode 100644 index 000000000000..44f982684541 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_stack_var_off.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2017 Facebook + +#include <linux/ptrace.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +int uprobe_res; + +SEC("uprobe/func") +int BPF_KPROBE(uprobe, char *s, int len) +{ + /* This BPF program performs variable-offset reads and writes on a + * stack-allocated buffer. + */ + char buf[16]; + unsigned long idx; + char out; + + /* Zero-out the buffer so we can read anywhere inside it. */ + __builtin_memset(&buf, 0, 16); + /* Copy the contents of s from user-space. */ + len &= 0xf; + if (bpf_probe_read_user(&buf, len, s)) { + bpf_printk("error reading user mem\n"); + return 1; + } + /* Index into the buffer at an unknown offset that comes from the + * buffer itself. This is a variable-offset stack read. + */ + idx = buf[0]; + idx &= 0xf; + out = buf[idx]; + /* Append something to the buffer. The position where we append it + * is unknown. This is a variable-offset stack write. + */ + buf[len] = buf[idx]; + uprobe_res = out; + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.27.0