1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Google */
3 
4 #include <test_progs.h>
5 #include <bpf/libbpf.h>
6 #include <bpf/btf.h>
7 #include "test_ksyms_btf.skel.h"
8 #include "test_ksyms_btf_null_check.skel.h"
9 
10 static int duration;
11 
12 static void test_basic(void)
13 {
14 	__u64 runqueues_addr, bpf_prog_active_addr;
15 	__u32 this_rq_cpu;
16 	int this_bpf_prog_active;
17 	struct test_ksyms_btf *skel = NULL;
18 	struct test_ksyms_btf__data *data;
19 	int err;
20 
21 	err = kallsyms_find("runqueues", &runqueues_addr);
22 	if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
23 		return;
24 	if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n"))
25 		return;
26 
27 	err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr);
28 	if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
29 		return;
30 	if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
31 		return;
32 
33 	skel = test_ksyms_btf__open_and_load();
34 	if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
35 		goto cleanup;
36 
37 	err = test_ksyms_btf__attach(skel);
38 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
39 		goto cleanup;
40 
41 	/* trigger tracepoint */
42 	usleep(1);
43 
44 	data = skel->data;
45 	CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr",
46 	      "got %llu, exp %llu\n",
47 	      (unsigned long long)data->out__runqueues_addr,
48 	      (unsigned long long)runqueues_addr);
49 	CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr",
50 	      "got %llu, exp %llu\n",
51 	      (unsigned long long)data->out__bpf_prog_active_addr,
52 	      (unsigned long long)bpf_prog_active_addr);
53 
54 	CHECK(data->out__rq_cpu == -1, "rq_cpu",
55 	      "got %u, exp != -1\n", data->out__rq_cpu);
56 	CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active",
57 	      "got %d, exp >= 0\n", data->out__bpf_prog_active);
58 	CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu",
59 	      "got %u, exp 0\n", data->out__cpu_0_rq_cpu);
60 
61 	this_rq_cpu = data->out__this_rq_cpu;
62 	CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu",
63 	      "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu);
64 
65 	this_bpf_prog_active = data->out__this_bpf_prog_active;
66 	CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active",
67 	      "got %d, exp %d\n", this_bpf_prog_active,
68 	      data->out__bpf_prog_active);
69 
70 cleanup:
71 	test_ksyms_btf__destroy(skel);
72 }
73 
74 static void test_null_check(void)
75 {
76 	struct test_ksyms_btf_null_check *skel;
77 
78 	skel = test_ksyms_btf_null_check__open_and_load();
79 	CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
80 
81 	test_ksyms_btf_null_check__destroy(skel);
82 }
83 
84 void test_ksyms_btf(void)
85 {
86 	int percpu_datasec;
87 	struct btf *btf;
88 
89 	btf = libbpf_find_kernel_btf();
90 	if (!ASSERT_OK_PTR(btf, "btf_exists"))
91 		return;
92 
93 	percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
94 						BTF_KIND_DATASEC);
95 	btf__free(btf);
96 	if (percpu_datasec < 0) {
97 		printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
98 		       __func__);
99 		test__skip();
100 		return;
101 	}
102 
103 	if (test__start_subtest("basic"))
104 		test_basic();
105 
106 	if (test__start_subtest("null_check"))
107 		test_null_check();
108 }
109