Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #include <test_progs.h>
0004 #include <sys/types.h>
0005 #include <unistd.h>
0006 #include "find_vma.skel.h"
0007 #include "find_vma_fail1.skel.h"
0008 #include "find_vma_fail2.skel.h"
0009 
0010 static void test_and_reset_skel(struct find_vma *skel, int expected_find_zero_ret, bool need_test)
0011 {
0012     if (need_test) {
0013         ASSERT_EQ(skel->bss->found_vm_exec, 1, "found_vm_exec");
0014         ASSERT_EQ(skel->data->find_addr_ret, 0, "find_addr_ret");
0015         ASSERT_EQ(skel->data->find_zero_ret, expected_find_zero_ret, "find_zero_ret");
0016         ASSERT_OK_PTR(strstr(skel->bss->d_iname, "test_progs"), "find_test_progs");
0017     }
0018 
0019     skel->bss->found_vm_exec = 0;
0020     skel->data->find_addr_ret = -1;
0021     skel->data->find_zero_ret = -1;
0022     skel->bss->d_iname[0] = 0;
0023 }
0024 
0025 static int open_pe(void)
0026 {
0027     struct perf_event_attr attr = {0};
0028     int pfd;
0029 
0030     /* create perf event */
0031     attr.size = sizeof(attr);
0032     attr.type = PERF_TYPE_HARDWARE;
0033     attr.config = PERF_COUNT_HW_CPU_CYCLES;
0034     attr.freq = 1;
0035     attr.sample_freq = 1000;
0036     pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
0037 
0038     return pfd >= 0 ? pfd : -errno;
0039 }
0040 
0041 static bool find_vma_pe_condition(struct find_vma *skel)
0042 {
0043     return skel->bss->found_vm_exec == 0 ||
0044         skel->data->find_addr_ret != 0 ||
0045         skel->data->find_zero_ret == -1 ||
0046         strcmp(skel->bss->d_iname, "test_progs") != 0;
0047 }
0048 
0049 static void test_find_vma_pe(struct find_vma *skel)
0050 {
0051     struct bpf_link *link = NULL;
0052     volatile int j = 0;
0053     int pfd, i;
0054     const int one_bn = 1000000000;
0055 
0056     pfd = open_pe();
0057     if (pfd < 0) {
0058         if (pfd == -ENOENT || pfd == -EOPNOTSUPP) {
0059             printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
0060             test__skip();
0061             goto cleanup;
0062         }
0063         if (!ASSERT_GE(pfd, 0, "perf_event_open"))
0064             goto cleanup;
0065     }
0066 
0067     link = bpf_program__attach_perf_event(skel->progs.handle_pe, pfd);
0068     if (!ASSERT_OK_PTR(link, "attach_perf_event"))
0069         goto cleanup;
0070 
0071     for (i = 0; i < one_bn && find_vma_pe_condition(skel); ++i)
0072         ++j;
0073 
0074     test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */, i == one_bn);
0075 cleanup:
0076     bpf_link__destroy(link);
0077     close(pfd);
0078 }
0079 
0080 static void test_find_vma_kprobe(struct find_vma *skel)
0081 {
0082     int err;
0083 
0084     err = find_vma__attach(skel);
0085     if (!ASSERT_OK(err, "get_branch_snapshot__attach"))
0086         return;
0087 
0088     getpgid(skel->bss->target_pid);
0089     test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */, true);
0090 }
0091 
0092 static void test_illegal_write_vma(void)
0093 {
0094     struct find_vma_fail1 *skel;
0095 
0096     skel = find_vma_fail1__open_and_load();
0097     if (!ASSERT_ERR_PTR(skel, "find_vma_fail1__open_and_load"))
0098         find_vma_fail1__destroy(skel);
0099 }
0100 
0101 static void test_illegal_write_task(void)
0102 {
0103     struct find_vma_fail2 *skel;
0104 
0105     skel = find_vma_fail2__open_and_load();
0106     if (!ASSERT_ERR_PTR(skel, "find_vma_fail2__open_and_load"))
0107         find_vma_fail2__destroy(skel);
0108 }
0109 
0110 void serial_test_find_vma(void)
0111 {
0112     struct find_vma *skel;
0113 
0114     skel = find_vma__open_and_load();
0115     if (!ASSERT_OK_PTR(skel, "find_vma__open_and_load"))
0116         return;
0117 
0118     skel->bss->target_pid = getpid();
0119     skel->bss->addr = (__u64)(uintptr_t)test_find_vma_pe;
0120 
0121     test_find_vma_pe(skel);
0122     test_find_vma_kprobe(skel);
0123 
0124     find_vma__destroy(skel);
0125     test_illegal_write_vma();
0126     test_illegal_write_task();
0127 }