1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2021 Facebook */ 3 #include <test_progs.h> 4 #include <sys/types.h> 5 #include <unistd.h> 6 #include "find_vma.skel.h" 7 #include "find_vma_fail1.skel.h" 8 #include "find_vma_fail2.skel.h" 9 10 static void test_and_reset_skel(struct find_vma *skel, int expected_find_zero_ret) 11 { 12 ASSERT_EQ(skel->bss->found_vm_exec, 1, "found_vm_exec"); 13 ASSERT_EQ(skel->data->find_addr_ret, 0, "find_addr_ret"); 14 ASSERT_EQ(skel->data->find_zero_ret, expected_find_zero_ret, "find_zero_ret"); 15 ASSERT_OK_PTR(strstr(skel->bss->d_iname, "test_progs"), "find_test_progs"); 16 17 skel->bss->found_vm_exec = 0; 18 skel->data->find_addr_ret = -1; 19 skel->data->find_zero_ret = -1; 20 skel->bss->d_iname[0] = 0; 21 } 22 23 static int open_pe(void) 24 { 25 struct perf_event_attr attr = {0}; 26 int pfd; 27 28 /* create perf event */ 29 attr.size = sizeof(attr); 30 attr.type = PERF_TYPE_HARDWARE; 31 attr.config = PERF_COUNT_HW_CPU_CYCLES; 32 attr.freq = 1; 33 attr.sample_freq = 4000; 34 pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC); 35 36 return pfd >= 0 ? pfd : -errno; 37 } 38 39 static void test_find_vma_pe(struct find_vma *skel) 40 { 41 struct bpf_link *link = NULL; 42 volatile int j = 0; 43 int pfd, i; 44 45 pfd = open_pe(); 46 if (pfd < 0) { 47 if (pfd == -ENOENT || pfd == -EOPNOTSUPP) { 48 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__); 49 test__skip(); 50 goto cleanup; 51 } 52 if (!ASSERT_GE(pfd, 0, "perf_event_open")) 53 goto cleanup; 54 } 55 56 link = bpf_program__attach_perf_event(skel->progs.handle_pe, pfd); 57 if (!ASSERT_OK_PTR(link, "attach_perf_event")) 58 goto cleanup; 59 60 for (i = 0; i < 1000000; ++i) 61 ++j; 62 63 test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */); 64 cleanup: 65 bpf_link__destroy(link); 66 close(pfd); 67 } 68 69 static void test_find_vma_kprobe(struct find_vma *skel) 70 { 71 int err; 72 73 err = find_vma__attach(skel); 74 if (!ASSERT_OK(err, "get_branch_snapshot__attach")) 75 return; 76 77 getpgid(skel->bss->target_pid); 78 test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */); 79 } 80 81 static void test_illegal_write_vma(void) 82 { 83 struct find_vma_fail1 *skel; 84 85 skel = find_vma_fail1__open_and_load(); 86 if (!ASSERT_ERR_PTR(skel, "find_vma_fail1__open_and_load")) 87 find_vma_fail1__destroy(skel); 88 } 89 90 static void test_illegal_write_task(void) 91 { 92 struct find_vma_fail2 *skel; 93 94 skel = find_vma_fail2__open_and_load(); 95 if (!ASSERT_ERR_PTR(skel, "find_vma_fail2__open_and_load")) 96 find_vma_fail2__destroy(skel); 97 } 98 99 void serial_test_find_vma(void) 100 { 101 struct find_vma *skel; 102 103 skel = find_vma__open_and_load(); 104 if (!ASSERT_OK_PTR(skel, "find_vma__open_and_load")) 105 return; 106 107 skel->bss->target_pid = getpid(); 108 skel->bss->addr = (__u64)(uintptr_t)test_find_vma_pe; 109 110 test_find_vma_pe(skel); 111 usleep(100000); /* allow the irq_work to finish */ 112 test_find_vma_kprobe(skel); 113 114 find_vma__destroy(skel); 115 test_illegal_write_vma(); 116 test_illegal_write_task(); 117 } 118