xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/skeleton.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1197448eaSAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2197448eaSAndrii Nakryiko /* Copyright (c) 2019 Facebook */
3197448eaSAndrii Nakryiko 
4197448eaSAndrii Nakryiko #include <test_progs.h>
5*2f968e9fSAndrii Nakryiko #include <sys/mman.h>
6197448eaSAndrii Nakryiko 
7197448eaSAndrii Nakryiko struct s {
8197448eaSAndrii Nakryiko 	int a;
9197448eaSAndrii Nakryiko 	long long b;
10197448eaSAndrii Nakryiko } __attribute__((packed));
11197448eaSAndrii Nakryiko 
12197448eaSAndrii Nakryiko #include "test_skeleton.skel.h"
13197448eaSAndrii Nakryiko 
test_skeleton(void)14197448eaSAndrii Nakryiko void test_skeleton(void)
15197448eaSAndrii Nakryiko {
16197448eaSAndrii Nakryiko 	int duration = 0, err;
17197448eaSAndrii Nakryiko 	struct test_skeleton* skel;
18197448eaSAndrii Nakryiko 	struct test_skeleton__bss *bss;
19caf62492SAndrii Nakryiko 	struct test_skeleton__data *data;
2030c5bd96SAndrii Nakryiko 	struct test_skeleton__data_dyn *data_dyn;
21caf62492SAndrii Nakryiko 	struct test_skeleton__rodata *rodata;
2230c5bd96SAndrii Nakryiko 	struct test_skeleton__rodata_dyn *rodata_dyn;
2381bfdd08SAndrii Nakryiko 	struct test_skeleton__kconfig *kcfg;
24980a1a4cSMatt Smith 	const void *elf_bytes;
25980a1a4cSMatt Smith 	size_t elf_bytes_sz = 0;
26*2f968e9fSAndrii Nakryiko 	void *m;
27*2f968e9fSAndrii Nakryiko 	int i, fd;
28197448eaSAndrii Nakryiko 
295dc7a8b2SAndrii Nakryiko 	skel = test_skeleton__open();
30197448eaSAndrii Nakryiko 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
31197448eaSAndrii Nakryiko 		return;
32197448eaSAndrii Nakryiko 
3381bfdd08SAndrii Nakryiko 	if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n"))
34330a73a7SAndrii Nakryiko 		goto cleanup;
35330a73a7SAndrii Nakryiko 
36caf62492SAndrii Nakryiko 	bss = skel->bss;
37caf62492SAndrii Nakryiko 	data = skel->data;
3830c5bd96SAndrii Nakryiko 	data_dyn = skel->data_dyn;
39caf62492SAndrii Nakryiko 	rodata = skel->rodata;
4030c5bd96SAndrii Nakryiko 	rodata_dyn = skel->rodata_dyn;
4130c5bd96SAndrii Nakryiko 
4230c5bd96SAndrii Nakryiko 	ASSERT_STREQ(bpf_map__name(skel->maps.rodata_dyn), ".rodata.dyn", "rodata_dyn_name");
4330c5bd96SAndrii Nakryiko 	ASSERT_STREQ(bpf_map__name(skel->maps.data_dyn), ".data.dyn", "data_dyn_name");
44caf62492SAndrii Nakryiko 
45caf62492SAndrii Nakryiko 	/* validate values are pre-initialized correctly */
46caf62492SAndrii Nakryiko 	CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1);
47caf62492SAndrii Nakryiko 	CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1);
48caf62492SAndrii Nakryiko 	CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL);
49caf62492SAndrii Nakryiko 	CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL);
50caf62492SAndrii Nakryiko 
51caf62492SAndrii Nakryiko 	CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0);
52caf62492SAndrii Nakryiko 	CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0);
53caf62492SAndrii Nakryiko 	CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
54caf62492SAndrii Nakryiko 	CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
55caf62492SAndrii Nakryiko 
560b20933dSAndrii Nakryiko 	CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
57caf62492SAndrii Nakryiko 	CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
58caf62492SAndrii Nakryiko 
5930c5bd96SAndrii Nakryiko 	ASSERT_EQ(rodata_dyn->in_dynarr_sz, 0, "in_dynarr_sz");
6030c5bd96SAndrii Nakryiko 	for (i = 0; i < 4; i++)
6130c5bd96SAndrii Nakryiko 		ASSERT_EQ(rodata_dyn->in_dynarr[i], -(i + 1), "in_dynarr");
6230c5bd96SAndrii Nakryiko 	for (i = 0; i < 4; i++)
6330c5bd96SAndrii Nakryiko 		ASSERT_EQ(data_dyn->out_dynarr[i], i + 1, "out_dynarr");
6430c5bd96SAndrii Nakryiko 
65caf62492SAndrii Nakryiko 	/* validate we can pre-setup global variables, even in .bss */
66caf62492SAndrii Nakryiko 	data->in1 = 10;
67caf62492SAndrii Nakryiko 	data->in2 = 11;
68caf62492SAndrii Nakryiko 	bss->in3 = 12;
69caf62492SAndrii Nakryiko 	bss->in4 = 13;
700b20933dSAndrii Nakryiko 	rodata->in.in6 = 14;
71caf62492SAndrii Nakryiko 
7230c5bd96SAndrii Nakryiko 	rodata_dyn->in_dynarr_sz = 4;
7330c5bd96SAndrii Nakryiko 	for (i = 0; i < 4; i++)
7430c5bd96SAndrii Nakryiko 		rodata_dyn->in_dynarr[i] = i + 10;
7530c5bd96SAndrii Nakryiko 
76330a73a7SAndrii Nakryiko 	err = test_skeleton__load(skel);
77330a73a7SAndrii Nakryiko 	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
78330a73a7SAndrii Nakryiko 		goto cleanup;
79330a73a7SAndrii Nakryiko 
80caf62492SAndrii Nakryiko 	/* validate pre-setup values are still there */
81caf62492SAndrii Nakryiko 	CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10);
82caf62492SAndrii Nakryiko 	CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
83caf62492SAndrii Nakryiko 	CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
84caf62492SAndrii Nakryiko 	CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
850b20933dSAndrii Nakryiko 	CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
86caf62492SAndrii Nakryiko 
8730c5bd96SAndrii Nakryiko 	ASSERT_EQ(rodata_dyn->in_dynarr_sz, 4, "in_dynarr_sz");
8830c5bd96SAndrii Nakryiko 	for (i = 0; i < 4; i++)
8930c5bd96SAndrii Nakryiko 		ASSERT_EQ(rodata_dyn->in_dynarr[i], i + 10, "in_dynarr");
9030c5bd96SAndrii Nakryiko 
91caf62492SAndrii Nakryiko 	/* now set new values and attach to get them into outX variables */
92caf62492SAndrii Nakryiko 	data->in1 = 1;
93caf62492SAndrii Nakryiko 	data->in2 = 2;
94197448eaSAndrii Nakryiko 	bss->in3 = 3;
95197448eaSAndrii Nakryiko 	bss->in4 = 4;
96197448eaSAndrii Nakryiko 	bss->in5.a = 5;
97197448eaSAndrii Nakryiko 	bss->in5.b = 6;
9881bfdd08SAndrii Nakryiko 	kcfg = skel->kconfig;
99197448eaSAndrii Nakryiko 
10030c5bd96SAndrii Nakryiko 	skel->data_read_mostly->read_mostly_var = 123;
10130c5bd96SAndrii Nakryiko 
102197448eaSAndrii Nakryiko 	err = test_skeleton__attach(skel);
103197448eaSAndrii Nakryiko 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
104197448eaSAndrii Nakryiko 		goto cleanup;
105197448eaSAndrii Nakryiko 
106197448eaSAndrii Nakryiko 	/* trigger tracepoint */
107197448eaSAndrii Nakryiko 	usleep(1);
108197448eaSAndrii Nakryiko 
109caf62492SAndrii Nakryiko 	CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1);
110caf62492SAndrii Nakryiko 	CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2);
111197448eaSAndrii Nakryiko 	CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
112197448eaSAndrii Nakryiko 	CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
113256eab48SAndrii Nakryiko 	CHECK(bss->out5.a != 5, "res5", "got %d != exp %d\n", bss->out5.a, 5);
114256eab48SAndrii Nakryiko 	CHECK(bss->out5.b != 6, "res6", "got %lld != exp %d\n", bss->out5.b, 6);
115caf62492SAndrii Nakryiko 	CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14);
116197448eaSAndrii Nakryiko 
11781bfdd08SAndrii Nakryiko 	CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1",
11881bfdd08SAndrii Nakryiko 	      "got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL);
11981bfdd08SAndrii Nakryiko 	CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
12081bfdd08SAndrii Nakryiko 	      "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);
121330a73a7SAndrii Nakryiko 
12230c5bd96SAndrii Nakryiko 	for (i = 0; i < 4; i++)
12330c5bd96SAndrii Nakryiko 		ASSERT_EQ(data_dyn->out_dynarr[i], i + 10, "out_dynarr");
12430c5bd96SAndrii Nakryiko 
12530c5bd96SAndrii Nakryiko 	ASSERT_EQ(skel->bss->out_mostly_var, 123, "out_mostly_var");
12630c5bd96SAndrii Nakryiko 
12724316461SAndrii Nakryiko 	ASSERT_EQ(bss->huge_arr[ARRAY_SIZE(bss->huge_arr) - 1], 123, "huge_arr");
12824316461SAndrii Nakryiko 
129*2f968e9fSAndrii Nakryiko 	fd = bpf_map__fd(skel->maps.data_non_mmapable);
130*2f968e9fSAndrii Nakryiko 	m = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, fd, 0);
131*2f968e9fSAndrii Nakryiko 	if (!ASSERT_EQ(m, MAP_FAILED, "unexpected_mmap_success"))
132*2f968e9fSAndrii Nakryiko 		munmap(m, getpagesize());
133*2f968e9fSAndrii Nakryiko 
134*2f968e9fSAndrii Nakryiko 	ASSERT_EQ(bpf_map__map_flags(skel->maps.data_non_mmapable), 0, "non_mmap_flags");
135*2f968e9fSAndrii Nakryiko 
136980a1a4cSMatt Smith 	elf_bytes = test_skeleton__elf_bytes(&elf_bytes_sz);
137980a1a4cSMatt Smith 	ASSERT_OK_PTR(elf_bytes, "elf_bytes");
138980a1a4cSMatt Smith 	ASSERT_GE(elf_bytes_sz, 0, "elf_bytes_sz");
139980a1a4cSMatt Smith 
140197448eaSAndrii Nakryiko cleanup:
141197448eaSAndrii Nakryiko 	test_skeleton__destroy(skel);
142197448eaSAndrii Nakryiko }
143