xref: /linux/tools/testing/selftests/bpf/prog_tests/subskeleton.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include "test_subskeleton.skel.h"
6 #include "test_subskeleton_lib.subskel.h"
7 
8 static void subskeleton_lib_setup(struct bpf_object *obj)
9 {
10 	struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj);
11 
12 	if (!ASSERT_OK_PTR(lib, "open subskeleton"))
13 		return;
14 
15 	*lib->rodata.var1 = 1;
16 	*lib->data.var2 = 2;
17 	lib->bss.var3->var3_1 = 3;
18 	lib->bss.var3->var3_2 = 4;
19 
20 	test_subskeleton_lib__destroy(lib);
21 }
22 
23 static int subskeleton_lib_subresult(struct bpf_object *obj)
24 {
25 	struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj);
26 	int result;
27 
28 	if (!ASSERT_OK_PTR(lib, "open subskeleton"))
29 		return -EINVAL;
30 
31 	result = *lib->bss.libout1;
32 	ASSERT_EQ(result, 1 + 2 + 3 + 4 + 5 + 6, "lib subresult");
33 
34 	ASSERT_OK_PTR(lib->progs.lib_perf_handler, "lib_perf_handler");
35 	ASSERT_STREQ(bpf_program__name(lib->progs.lib_perf_handler),
36 		     "lib_perf_handler", "program name");
37 
38 	ASSERT_OK_PTR(lib->maps.map1, "map1");
39 	ASSERT_STREQ(bpf_map__name(lib->maps.map1), "map1", "map name");
40 
41 	ASSERT_EQ(*lib->data.var5, 5, "__weak var5");
42 	ASSERT_EQ(*lib->data.var6, 6, "extern var6");
43 	ASSERT_TRUE(*lib->kconfig.CONFIG_BPF_SYSCALL, "CONFIG_BPF_SYSCALL");
44 
45 	test_subskeleton_lib__destroy(lib);
46 	return result;
47 }
48 
49 void test_subskeleton(void)
50 {
51 	int err, result;
52 	struct test_subskeleton *skel;
53 
54 	skel = test_subskeleton__open();
55 	if (!ASSERT_OK_PTR(skel, "skel_open"))
56 		return;
57 
58 	skel->rodata->rovar1 = 10;
59 	skel->rodata->var1 = 1;
60 	subskeleton_lib_setup(skel->obj);
61 
62 	err = test_subskeleton__load(skel);
63 	if (!ASSERT_OK(err, "skel_load"))
64 		goto cleanup;
65 
66 	err = test_subskeleton__attach(skel);
67 	if (!ASSERT_OK(err, "skel_attach"))
68 		goto cleanup;
69 
70 	/* trigger tracepoint */
71 	usleep(1);
72 
73 	result = subskeleton_lib_subresult(skel->obj) * 10;
74 	ASSERT_EQ(skel->bss->out1, result, "unexpected calculation");
75 
76 cleanup:
77 	test_subskeleton__destroy(skel);
78 }
79