mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

Add a set of tests validating libbpf-provided extern variables. One crucial feature that's tested is dead code elimination together with using invalid BPF helper. CONFIG_MISSING is not supposed to exist and should always be specified by libbpf as zero, which allows BPF verifier to correctly do branch pruning and not fail validation, when invalid BPF helper is called from dead if branch. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20191214014710.3449601-5-andriin@fb.com
67 lines
1.8 KiB
C
67 lines
1.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2019 Facebook */
|
|
|
|
#include <test_progs.h>
|
|
|
|
struct s {
|
|
int a;
|
|
long long b;
|
|
} __attribute__((packed));
|
|
|
|
#include "test_skeleton.skel.h"
|
|
|
|
BPF_EMBED_OBJ(skeleton, "test_skeleton.o");
|
|
|
|
void test_skeleton(void)
|
|
{
|
|
int duration = 0, err;
|
|
struct test_skeleton* skel;
|
|
struct test_skeleton__bss *bss;
|
|
struct test_skeleton__externs *exts;
|
|
|
|
skel = test_skeleton__open(&skeleton_embed);
|
|
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
|
|
return;
|
|
|
|
printf("EXTERNS BEFORE: %p\n", skel->externs);
|
|
if (CHECK(skel->externs, "skel_externs", "externs are mmaped()!\n"))
|
|
goto cleanup;
|
|
|
|
err = test_skeleton__load(skel);
|
|
if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
|
|
goto cleanup;
|
|
printf("EXTERNS AFTER: %p\n", skel->externs);
|
|
|
|
bss = skel->bss;
|
|
bss->in1 = 1;
|
|
bss->in2 = 2;
|
|
bss->in3 = 3;
|
|
bss->in4 = 4;
|
|
bss->in5.a = 5;
|
|
bss->in5.b = 6;
|
|
exts = skel->externs;
|
|
|
|
err = test_skeleton__attach(skel);
|
|
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
|
|
goto cleanup;
|
|
|
|
/* trigger tracepoint */
|
|
usleep(1);
|
|
|
|
CHECK(bss->out1 != 1, "res1", "got %d != exp %d\n", bss->out1, 1);
|
|
CHECK(bss->out2 != 2, "res2", "got %lld != exp %d\n", bss->out2, 2);
|
|
CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
|
|
CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
|
|
CHECK(bss->handler_out5.a != 5, "res5", "got %d != exp %d\n",
|
|
bss->handler_out5.a, 5);
|
|
CHECK(bss->handler_out5.b != 6, "res6", "got %lld != exp %d\n",
|
|
bss->handler_out5.b, 6);
|
|
|
|
CHECK(bss->bpf_syscall != exts->CONFIG_BPF_SYSCALL, "ext1",
|
|
"got %d != exp %d\n", bss->bpf_syscall, exts->CONFIG_BPF_SYSCALL);
|
|
CHECK(bss->kern_ver != exts->LINUX_KERNEL_VERSION, "ext2",
|
|
"got %d != exp %d\n", bss->kern_ver, exts->LINUX_KERNEL_VERSION);
|
|
|
|
cleanup:
|
|
test_skeleton__destroy(skel);
|
|
}
|