1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 
4 #include <test_progs.h>
5 
6 struct s {
7 	int a;
8 	long long b;
9 } __attribute__((packed));
10 
11 #include "test_skeleton.skel.h"
12 
13 void test_skeleton(void)
14 {
15 	int duration = 0, err;
16 	struct test_skeleton* skel;
17 	struct test_skeleton__bss *bss;
18 	struct test_skeleton__data *data;
19 	struct test_skeleton__rodata *rodata;
20 	struct test_skeleton__kconfig *kcfg;
21 
22 	skel = test_skeleton__open();
23 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
24 		return;
25 
26 	if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n"))
27 		goto cleanup;
28 
29 	bss = skel->bss;
30 	data = skel->data;
31 	rodata = skel->rodata;
32 
33 	/* validate values are pre-initialized correctly */
34 	CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1);
35 	CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1);
36 	CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL);
37 	CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL);
38 
39 	CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0);
40 	CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0);
41 	CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
42 	CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
43 
44 	CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
45 	CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
46 
47 	/* validate we can pre-setup global variables, even in .bss */
48 	data->in1 = 10;
49 	data->in2 = 11;
50 	bss->in3 = 12;
51 	bss->in4 = 13;
52 	rodata->in.in6 = 14;
53 
54 	err = test_skeleton__load(skel);
55 	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
56 		goto cleanup;
57 
58 	/* validate pre-setup values are still there */
59 	CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10);
60 	CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
61 	CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
62 	CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
63 	CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
64 
65 	/* now set new values and attach to get them into outX variables */
66 	data->in1 = 1;
67 	data->in2 = 2;
68 	bss->in3 = 3;
69 	bss->in4 = 4;
70 	bss->in5.a = 5;
71 	bss->in5.b = 6;
72 	kcfg = skel->kconfig;
73 
74 	err = test_skeleton__attach(skel);
75 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
76 		goto cleanup;
77 
78 	/* trigger tracepoint */
79 	usleep(1);
80 
81 	CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1);
82 	CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2);
83 	CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
84 	CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
85 	CHECK(bss->out5.a != 5, "res5", "got %d != exp %d\n", bss->out5.a, 5);
86 	CHECK(bss->out5.b != 6, "res6", "got %lld != exp %d\n", bss->out5.b, 6);
87 	CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14);
88 
89 	CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1",
90 	      "got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL);
91 	CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
92 	      "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);
93 
94 cleanup:
95 	test_skeleton__destroy(skel);
96 }
97