xref: /linux/tools/testing/selftests/bpf/prog_tests/skeleton.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 
4 #include <test_progs.h>
5 #include <sys/mman.h>
6 
7 struct s {
8 	int a;
9 	long long b;
10 } __attribute__((packed));
11 
12 #include "test_skeleton.skel.h"
13 
14 void test_skeleton(void)
15 {
16 	int duration = 0, err;
17 	struct test_skeleton* skel;
18 	struct test_skeleton__bss *bss;
19 	struct test_skeleton__data *data;
20 	struct test_skeleton__data_dyn *data_dyn;
21 	struct test_skeleton__rodata *rodata;
22 	struct test_skeleton__rodata_dyn *rodata_dyn;
23 	struct test_skeleton__kconfig *kcfg;
24 	const void *elf_bytes;
25 	size_t elf_bytes_sz = 0;
26 	void *m;
27 	int i, fd;
28 
29 	skel = test_skeleton__open();
30 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
31 		return;
32 
33 	if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n"))
34 		goto cleanup;
35 
36 	bss = skel->bss;
37 	data = skel->data;
38 	data_dyn = skel->data_dyn;
39 	rodata = skel->rodata;
40 	rodata_dyn = skel->rodata_dyn;
41 
42 	ASSERT_STREQ(bpf_map__name(skel->maps.rodata_dyn), ".rodata.dyn", "rodata_dyn_name");
43 	ASSERT_STREQ(bpf_map__name(skel->maps.data_dyn), ".data.dyn", "data_dyn_name");
44 
45 	/* validate values are pre-initialized correctly */
46 	CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1);
47 	CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1);
48 	CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL);
49 	CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL);
50 
51 	CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0);
52 	CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0);
53 	CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
54 	CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
55 
56 	CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
57 	CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
58 
59 	ASSERT_EQ(rodata_dyn->in_dynarr_sz, 0, "in_dynarr_sz");
60 	for (i = 0; i < 4; i++)
61 		ASSERT_EQ(rodata_dyn->in_dynarr[i], -(i + 1), "in_dynarr");
62 	for (i = 0; i < 4; i++)
63 		ASSERT_EQ(data_dyn->out_dynarr[i], i + 1, "out_dynarr");
64 
65 	/* validate we can pre-setup global variables, even in .bss */
66 	data->in1 = 10;
67 	data->in2 = 11;
68 	bss->in3 = 12;
69 	bss->in4 = 13;
70 	rodata->in.in6 = 14;
71 
72 	rodata_dyn->in_dynarr_sz = 4;
73 	for (i = 0; i < 4; i++)
74 		rodata_dyn->in_dynarr[i] = i + 10;
75 
76 	err = test_skeleton__load(skel);
77 	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
78 		goto cleanup;
79 
80 	/* validate pre-setup values are still there */
81 	CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10);
82 	CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
83 	CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
84 	CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
85 	CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
86 
87 	ASSERT_EQ(rodata_dyn->in_dynarr_sz, 4, "in_dynarr_sz");
88 	for (i = 0; i < 4; i++)
89 		ASSERT_EQ(rodata_dyn->in_dynarr[i], i + 10, "in_dynarr");
90 
91 	/* now set new values and attach to get them into outX variables */
92 	data->in1 = 1;
93 	data->in2 = 2;
94 	bss->in3 = 3;
95 	bss->in4 = 4;
96 	bss->in5.a = 5;
97 	bss->in5.b = 6;
98 	kcfg = skel->kconfig;
99 
100 	skel->data_read_mostly->read_mostly_var = 123;
101 
102 	err = test_skeleton__attach(skel);
103 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
104 		goto cleanup;
105 
106 	/* trigger tracepoint */
107 	usleep(1);
108 
109 	CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1);
110 	CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2);
111 	CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
112 	CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
113 	CHECK(bss->out5.a != 5, "res5", "got %d != exp %d\n", bss->out5.a, 5);
114 	CHECK(bss->out5.b != 6, "res6", "got %lld != exp %d\n", bss->out5.b, 6);
115 	CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14);
116 
117 	CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1",
118 	      "got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL);
119 	CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
120 	      "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);
121 
122 	for (i = 0; i < 4; i++)
123 		ASSERT_EQ(data_dyn->out_dynarr[i], i + 10, "out_dynarr");
124 
125 	ASSERT_EQ(skel->bss->out_mostly_var, 123, "out_mostly_var");
126 
127 	ASSERT_EQ(bss->huge_arr[ARRAY_SIZE(bss->huge_arr) - 1], 123, "huge_arr");
128 
129 	fd = bpf_map__fd(skel->maps.data_non_mmapable);
130 	m = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, fd, 0);
131 	if (!ASSERT_EQ(m, MAP_FAILED, "unexpected_mmap_success"))
132 		munmap(m, getpagesize());
133 
134 	ASSERT_EQ(bpf_map__map_flags(skel->maps.data_non_mmapable), 0, "non_mmap_flags");
135 
136 	elf_bytes = test_skeleton__elf_bytes(&elf_bytes_sz);
137 	ASSERT_OK_PTR(elf_bytes, "elf_bytes");
138 	ASSERT_GE(elf_bytes_sz, 0, "elf_bytes_sz");
139 
140 cleanup:
141 	test_skeleton__destroy(skel);
142 }
143