1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020 Facebook */ 3 4 #include <test_progs.h> 5 #include <stdbool.h> 6 #include "test_module_attach.skel.h" 7 #include "testing_helpers.h" 8 9 static int duration; 10 11 static int trigger_module_test_writable(int *val) 12 { 13 int fd, err; 14 char buf[65]; 15 ssize_t rd; 16 17 fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY); 18 err = -errno; 19 if (!ASSERT_GE(fd, 0, "testmode_file_open")) 20 return err; 21 22 rd = read(fd, buf, sizeof(buf) - 1); 23 err = -errno; 24 if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) { 25 close(fd); 26 return err; 27 } 28 29 buf[rd] = '\0'; 30 *val = strtol(buf, NULL, 0); 31 close(fd); 32 33 return 0; 34 } 35 36 void test_module_attach(void) 37 { 38 const int READ_SZ = 456; 39 const int WRITE_SZ = 457; 40 struct test_module_attach* skel; 41 struct test_module_attach__bss *bss; 42 struct bpf_link *link; 43 int err; 44 int writable_val = 0; 45 46 skel = test_module_attach__open(); 47 if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) 48 return; 49 50 err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual, 51 0, "bpf_testmod_test_read"); 52 ASSERT_OK(err, "set_attach_target"); 53 54 err = bpf_program__set_attach_target(skel->progs.handle_fentry_explicit_manual, 55 0, "bpf_testmod:bpf_testmod_test_read"); 56 ASSERT_OK(err, "set_attach_target_explicit"); 57 58 err = test_module_attach__load(skel); 59 if (CHECK(err, "skel_load", "failed to load skeleton\n")) 60 return; 61 62 bss = skel->bss; 63 64 err = test_module_attach__attach(skel); 65 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) 66 goto cleanup; 67 68 /* trigger tracepoint */ 69 ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read"); 70 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write"); 71 72 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp"); 73 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare"); 74 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf"); 75 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry"); 76 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual"); 77 ASSERT_EQ(bss->fentry_explicit_read_sz, READ_SZ, "fentry_explicit"); 78 ASSERT_EQ(bss->fentry_explicit_manual_read_sz, READ_SZ, "fentry_explicit_manual"); 79 ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit"); 80 ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet"); 81 ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret"); 82 83 bss->raw_tp_writable_bare_early_ret = true; 84 bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4; 85 ASSERT_OK(trigger_module_test_writable(&writable_val), 86 "trigger_writable"); 87 ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in"); 88 ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val, 89 "writable_test_out"); 90 91 test_module_attach__detach(skel); 92 93 /* attach fentry/fexit and make sure it get's module reference */ 94 link = bpf_program__attach(skel->progs.handle_fentry); 95 if (!ASSERT_OK_PTR(link, "attach_fentry")) 96 goto cleanup; 97 98 ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod"); 99 bpf_link__destroy(link); 100 101 link = bpf_program__attach(skel->progs.handle_fexit); 102 if (!ASSERT_OK_PTR(link, "attach_fexit")) 103 goto cleanup; 104 105 ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod"); 106 bpf_link__destroy(link); 107 108 link = bpf_program__attach(skel->progs.kprobe_multi); 109 if (!ASSERT_OK_PTR(link, "attach_kprobe_multi")) 110 goto cleanup; 111 112 ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod"); 113 bpf_link__destroy(link); 114 115 cleanup: 116 test_module_attach__destroy(skel); 117 } 118
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.