Andrei Matei 0fd7562af1 selftest/bpf: Add test for var-offset stack access
Add a higher-level test (C BPF program) for the new functionality -
variable access stack reads and writes.

Signed-off-by: Andrei Matei <andreimatei1@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210207011027.676572-5-andreimatei1@gmail.com
2021-02-10 11:05:34 -08:00

36 lines
925 B
C

// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include "test_stack_var_off.skel.h"
/* Test read and writes to the stack performed with offsets that are not
* statically known.
*/
void test_stack_var_off(void)
{
int duration = 0;
struct test_stack_var_off *skel;
skel = test_stack_var_off__open_and_load();
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
return;
/* Give pid to bpf prog so it doesn't trigger for anyone else. */
skel->bss->test_pid = getpid();
/* Initialize the probe's input. */
skel->bss->input[0] = 2;
skel->bss->input[1] = 42; /* This will be returned in probe_res. */
if (!ASSERT_OK(test_stack_var_off__attach(skel), "skel_attach"))
goto cleanup;
/* Trigger probe. */
usleep(1);
if (CHECK(skel->bss->probe_res != 42, "check_probe_res",
"wrong probe res: %d\n", skel->bss->probe_res))
goto cleanup;
cleanup:
test_stack_var_off__destroy(skel);
}