1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Google */
4 #include <test_progs.h>
5 #include <bpf/libbpf.h>
7 #include "test_ksyms_btf.skel.h"
8 #include "test_ksyms_btf_null_check.skel.h"
12 static void test_basic(void)
14 __u64 runqueues_addr
, bpf_prog_active_addr
;
16 int this_bpf_prog_active
;
17 struct test_ksyms_btf
*skel
= NULL
;
18 struct test_ksyms_btf__data
*data
;
21 err
= kallsyms_find("runqueues", &runqueues_addr
);
22 if (CHECK(err
== -EINVAL
, "kallsyms_fopen", "failed to open: %d\n", errno
))
24 if (CHECK(err
== -ENOENT
, "ksym_find", "symbol 'runqueues' not found\n"))
27 err
= kallsyms_find("bpf_prog_active", &bpf_prog_active_addr
);
28 if (CHECK(err
== -EINVAL
, "kallsyms_fopen", "failed to open: %d\n", errno
))
30 if (CHECK(err
== -ENOENT
, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
33 skel
= test_ksyms_btf__open_and_load();
34 if (CHECK(!skel
, "skel_open", "failed to open and load skeleton\n"))
37 err
= test_ksyms_btf__attach(skel
);
38 if (CHECK(err
, "skel_attach", "skeleton attach failed: %d\n", err
))
41 /* trigger tracepoint */
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
);
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
);
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
);
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
);
71 test_ksyms_btf__destroy(skel
);
74 static void test_null_check(void)
76 struct test_ksyms_btf_null_check
*skel
;
78 skel
= test_ksyms_btf_null_check__open_and_load();
79 CHECK(skel
, "skel_open", "unexpected load of a prog missing null check\n");
81 test_ksyms_btf_null_check__destroy(skel
);
84 void test_ksyms_btf(void)
89 btf
= libbpf_find_kernel_btf();
90 if (CHECK(IS_ERR(btf
), "btf_exists", "failed to load kernel BTF: %ld\n",
94 percpu_datasec
= btf__find_by_name_kind(btf
, ".data..percpu",
97 if (percpu_datasec
< 0) {
98 printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
104 if (test__start_subtest("basic"))
107 if (test__start_subtest("null_check"))