Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Google */
0003 
0004 #include <test_progs.h>
0005 #include <bpf/libbpf.h>
0006 #include <bpf/btf.h>
0007 #include "test_ksyms_btf.skel.h"
0008 #include "test_ksyms_btf_null_check.skel.h"
0009 #include "test_ksyms_weak.skel.h"
0010 #include "test_ksyms_weak.lskel.h"
0011 #include "test_ksyms_btf_write_check.skel.h"
0012 
0013 static int duration;
0014 
0015 static void test_basic(void)
0016 {
0017     __u64 runqueues_addr, bpf_prog_active_addr;
0018     __u32 this_rq_cpu;
0019     int this_bpf_prog_active;
0020     struct test_ksyms_btf *skel = NULL;
0021     struct test_ksyms_btf__data *data;
0022     int err;
0023 
0024     err = kallsyms_find("runqueues", &runqueues_addr);
0025     if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
0026         return;
0027     if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n"))
0028         return;
0029 
0030     err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr);
0031     if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
0032         return;
0033     if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
0034         return;
0035 
0036     skel = test_ksyms_btf__open_and_load();
0037     if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
0038         goto cleanup;
0039 
0040     err = test_ksyms_btf__attach(skel);
0041     if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
0042         goto cleanup;
0043 
0044     /* trigger tracepoint */
0045     usleep(1);
0046 
0047     data = skel->data;
0048     CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr",
0049           "got %llu, exp %llu\n",
0050           (unsigned long long)data->out__runqueues_addr,
0051           (unsigned long long)runqueues_addr);
0052     CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr",
0053           "got %llu, exp %llu\n",
0054           (unsigned long long)data->out__bpf_prog_active_addr,
0055           (unsigned long long)bpf_prog_active_addr);
0056 
0057     CHECK(data->out__rq_cpu == -1, "rq_cpu",
0058           "got %u, exp != -1\n", data->out__rq_cpu);
0059     CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active",
0060           "got %d, exp >= 0\n", data->out__bpf_prog_active);
0061     CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu",
0062           "got %u, exp 0\n", data->out__cpu_0_rq_cpu);
0063 
0064     this_rq_cpu = data->out__this_rq_cpu;
0065     CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu",
0066           "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu);
0067 
0068     this_bpf_prog_active = data->out__this_bpf_prog_active;
0069     CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active",
0070           "got %d, exp %d\n", this_bpf_prog_active,
0071           data->out__bpf_prog_active);
0072 
0073 cleanup:
0074     test_ksyms_btf__destroy(skel);
0075 }
0076 
0077 static void test_null_check(void)
0078 {
0079     struct test_ksyms_btf_null_check *skel;
0080 
0081     skel = test_ksyms_btf_null_check__open_and_load();
0082     CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
0083 
0084     test_ksyms_btf_null_check__destroy(skel);
0085 }
0086 
0087 static void test_weak_syms(void)
0088 {
0089     struct test_ksyms_weak *skel;
0090     struct test_ksyms_weak__data *data;
0091     int err;
0092 
0093     skel = test_ksyms_weak__open_and_load();
0094     if (!ASSERT_OK_PTR(skel, "test_ksyms_weak__open_and_load"))
0095         return;
0096 
0097     err = test_ksyms_weak__attach(skel);
0098     if (!ASSERT_OK(err, "test_ksyms_weak__attach"))
0099         goto cleanup;
0100 
0101     /* trigger tracepoint */
0102     usleep(1);
0103 
0104     data = skel->data;
0105     ASSERT_EQ(data->out__existing_typed, 0, "existing typed ksym");
0106     ASSERT_NEQ(data->out__existing_typeless, -1, "existing typeless ksym");
0107     ASSERT_EQ(data->out__non_existent_typeless, 0, "nonexistent typeless ksym");
0108     ASSERT_EQ(data->out__non_existent_typed, 0, "nonexistent typed ksym");
0109 
0110 cleanup:
0111     test_ksyms_weak__destroy(skel);
0112 }
0113 
0114 static void test_weak_syms_lskel(void)
0115 {
0116     struct test_ksyms_weak_lskel *skel;
0117     struct test_ksyms_weak_lskel__data *data;
0118     int err;
0119 
0120     skel = test_ksyms_weak_lskel__open_and_load();
0121     if (!ASSERT_OK_PTR(skel, "test_ksyms_weak_lskel__open_and_load"))
0122         return;
0123 
0124     err = test_ksyms_weak_lskel__attach(skel);
0125     if (!ASSERT_OK(err, "test_ksyms_weak_lskel__attach"))
0126         goto cleanup;
0127 
0128     /* trigger tracepoint */
0129     usleep(1);
0130 
0131     data = skel->data;
0132     ASSERT_EQ(data->out__existing_typed, 0, "existing typed ksym");
0133     ASSERT_NEQ(data->out__existing_typeless, -1, "existing typeless ksym");
0134     ASSERT_EQ(data->out__non_existent_typeless, 0, "nonexistent typeless ksym");
0135     ASSERT_EQ(data->out__non_existent_typed, 0, "nonexistent typed ksym");
0136 
0137 cleanup:
0138     test_ksyms_weak_lskel__destroy(skel);
0139 }
0140 
0141 static void test_write_check(bool test_handler1)
0142 {
0143     struct test_ksyms_btf_write_check *skel;
0144 
0145     skel = test_ksyms_btf_write_check__open();
0146     if (!ASSERT_OK_PTR(skel, "test_ksyms_btf_write_check__open"))
0147         return;
0148     bpf_program__set_autoload(test_handler1 ? skel->progs.handler2 : skel->progs.handler1, false);
0149     ASSERT_ERR(test_ksyms_btf_write_check__load(skel),
0150            "unexpected load of a prog writing to ksym memory\n");
0151 
0152     test_ksyms_btf_write_check__destroy(skel);
0153 }
0154 
0155 void test_ksyms_btf(void)
0156 {
0157     int percpu_datasec;
0158     struct btf *btf;
0159 
0160     btf = libbpf_find_kernel_btf();
0161     if (!ASSERT_OK_PTR(btf, "btf_exists"))
0162         return;
0163 
0164     percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
0165                         BTF_KIND_DATASEC);
0166     btf__free(btf);
0167     if (percpu_datasec < 0) {
0168         printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
0169                __func__);
0170         test__skip();
0171         return;
0172     }
0173 
0174     if (test__start_subtest("basic"))
0175         test_basic();
0176 
0177     if (test__start_subtest("null_check"))
0178         test_null_check();
0179 
0180     if (test__start_subtest("weak_ksyms"))
0181         test_weak_syms();
0182 
0183     if (test__start_subtest("weak_ksyms_lskel"))
0184         test_weak_syms_lskel();
0185 
0186     if (test__start_subtest("write_check1"))
0187         test_write_check(true);
0188 
0189     if (test__start_subtest("write_check2"))
0190         test_write_check(false);
0191 }