]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
selftests/bpf: Add global variables linking selftest
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 23 Apr 2021 18:13:46 +0000 (11:13 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 23 Apr 2021 21:05:27 +0000 (14:05 -0700)
Add selftest validating various aspects of statically linking global
variables:
  - correct resolution of extern variables across .bss, .data, and .rodata
    sections;
  - correct handling of weak definitions;
  - correct de-duplication of repeating special externs (.kconfig, .ksyms).

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-17-andrii@kernel.org
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/prog_tests/linked_vars.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/linked_vars1.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/linked_vars2.c [new file with mode: 0644]

index ab7b129a8408741621c9883e307aa37e163b1beb..411e2cf07ba58525b935e5c475031af0e19e103a 100644 (file)
@@ -309,10 +309,11 @@ endef
 
 SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
 
-LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h
+LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h linked_vars.skel.h
 
 test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
 linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
+linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
 
 LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
 
diff --git a/tools/testing/selftests/bpf/prog_tests/linked_vars.c b/tools/testing/selftests/bpf/prog_tests/linked_vars.c
new file mode 100644 (file)
index 0000000..267166a
--- /dev/null
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include <test_progs.h>
+#include <sys/syscall.h>
+#include "linked_vars.skel.h"
+
+void test_linked_vars(void)
+{
+       int err;
+       struct linked_vars *skel;
+
+       skel = linked_vars__open();
+       if (!ASSERT_OK_PTR(skel, "skel_open"))
+               return;
+
+       skel->bss->input_bss1 = 1000;
+       skel->bss->input_bss2 = 2000;
+       skel->bss->input_bss_weak = 3000;
+
+       err = linked_vars__load(skel);
+       if (!ASSERT_OK(err, "skel_load"))
+               goto cleanup;
+
+       err = linked_vars__attach(skel);
+       if (!ASSERT_OK(err, "skel_attach"))
+               goto cleanup;
+
+       /* trigger */
+       syscall(SYS_getpgid);
+
+       ASSERT_EQ(skel->bss->output_bss1, 1000 + 2000 + 3000, "output_bss1");
+       ASSERT_EQ(skel->bss->output_bss2, 1000 + 2000 + 3000, "output_bss2");
+       /* 10 comes from "winner" input_data_weak in first obj file */
+       ASSERT_EQ(skel->bss->output_data1, 1 + 2 + 10, "output_bss1");
+       ASSERT_EQ(skel->bss->output_data2, 1 + 2 + 10, "output_bss2");
+       /* 100 comes from "winner" input_rodata_weak in first obj file */
+       ASSERT_EQ(skel->bss->output_rodata1, 11 + 22 + 100, "output_weak1");
+       ASSERT_EQ(skel->bss->output_rodata2, 11 + 22 + 100, "output_weak2");
+
+cleanup:
+       linked_vars__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/linked_vars1.c b/tools/testing/selftests/bpf/progs/linked_vars1.c
new file mode 100644 (file)
index 0000000..ef9e9d0
--- /dev/null
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+extern int LINUX_KERNEL_VERSION __kconfig;
+/* this weak extern will be strict due to the other file's strong extern */
+extern bool CONFIG_BPF_SYSCALL __kconfig __weak;
+extern const void bpf_link_fops __ksym __weak;
+
+int input_bss1;
+int input_data1 = 1;
+const volatile int input_rodata1 = 11;
+
+int input_bss_weak __weak;
+/* these two definitions should win */
+int input_data_weak __weak = 10;
+const volatile int input_rodata_weak __weak = 100;
+
+extern int input_bss2;
+extern int input_data2;
+extern const int input_rodata2;
+
+int output_bss1;
+int output_data1;
+int output_rodata1;
+
+long output_sink1;
+
+static __noinline int get_bss_res(void)
+{
+       /* just make sure all the relocations work against .text as well */
+       return input_bss1 + input_bss2 + input_bss_weak;
+}
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(handler1)
+{
+       output_bss1 = get_bss_res();
+       output_data1 = input_data1 + input_data2 + input_data_weak;
+       output_rodata1 = input_rodata1 + input_rodata2 + input_rodata_weak;
+
+       /* make sure we actually use above special externs, otherwise compiler
+        * will optimize them out
+        */
+       output_sink1 = LINUX_KERNEL_VERSION
+                      + CONFIG_BPF_SYSCALL
+                      + (long)&bpf_link_fops;
+       return 0;
+}
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/linked_vars2.c b/tools/testing/selftests/bpf/progs/linked_vars2.c
new file mode 100644 (file)
index 0000000..e4f5bd3
--- /dev/null
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+extern int LINUX_KERNEL_VERSION __kconfig;
+/* when an extern is defined as both strong and weak, resulting symbol will be strong */
+extern bool CONFIG_BPF_SYSCALL __kconfig;
+extern const void __start_BTF __ksym;
+
+int input_bss2;
+int input_data2 = 2;
+const volatile int input_rodata2 = 22;
+
+int input_bss_weak __weak;
+/* these two weak variables should lose */
+int input_data_weak __weak = 20;
+const volatile int input_rodata_weak __weak = 200;
+
+extern int input_bss1;
+extern int input_data1;
+extern const int input_rodata1;
+
+int output_bss2;
+int output_data2;
+int output_rodata2;
+
+int output_sink2;
+
+static __noinline int get_data_res(void)
+{
+       /* just make sure all the relocations work against .text as well */
+       return input_data1 + input_data2 + input_data_weak;
+}
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(handler2)
+{
+       output_bss2 = input_bss1 + input_bss2 + input_bss_weak;
+       output_data2 = get_data_res();
+       output_rodata2 = input_rodata1 + input_rodata2 + input_rodata_weak;
+
+       /* make sure we actually use above special externs, otherwise compiler
+        * will optimize them out
+        */
+       output_sink2 = LINUX_KERNEL_VERSION
+                      + CONFIG_BPF_SYSCALL
+                      + (long)&__start_BTF;
+
+       return 0;
+}
+
+char LICENSE[] SEC("license") = "GPL";