1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 4 #include <test_progs.h> 5 6 struct s { 7 int a; 8 long long b; 9 } __attribute__((packed)); 10 11 #include "test_skeleton.skel.h" 12 13 void test_skeleton(void) 14 { 15 int duration = 0, err; 16 struct test_skeleton* skel; 17 struct test_skeleton__bss *bss; 18 struct test_skeleton__data *data; 19 struct test_skeleton__data_dyn *data_dyn; 20 struct test_skeleton__rodata *rodata; 21 struct test_skeleton__rodata_dyn *rodata_dyn; 22 struct test_skeleton__kconfig *kcfg; 23 const void *elf_bytes; 24 size_t elf_bytes_sz = 0; 25 int i; 26 27 skel = test_skeleton__open(); 28 if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) 29 return; 30 31 if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n")) 32 goto cleanup; 33 34 bss = skel->bss; 35 data = skel->data; 36 data_dyn = skel->data_dyn; 37 rodata = skel->rodata; 38 rodata_dyn = skel->rodata_dyn; 39 40 ASSERT_STREQ(bpf_map__name(skel->maps.rodata_dyn), ".rodata.dyn", "rodata_dyn_name"); 41 ASSERT_STREQ(bpf_map__name(skel->maps.data_dyn), ".data.dyn", "data_dyn_name"); 42 43 /* validate values are pre-initialized correctly */ 44 CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1); 45 CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1); 46 CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL); 47 CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL); 48 49 CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0); 50 CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0); 51 CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL); 52 CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL); 53 54 CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0); 55 CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0); 56 57 ASSERT_EQ(rodata_dyn->in_dynarr_sz, 0, "in_dynarr_sz"); 58 for (i = 0; i < 4; i++) 59 ASSERT_EQ(rodata_dyn->in_dynarr[i], -(i + 1), "in_dynarr"); 60 for (i = 0; i < 4; i++) 61 ASSERT_EQ(data_dyn->out_dynarr[i], i + 1, "out_dynarr"); 62 63 /* validate we can pre-setup global variables, even in .bss */ 64 data->in1 = 10; 65 data->in2 = 11; 66 bss->in3 = 12; 67 bss->in4 = 13; 68 rodata->in.in6 = 14; 69 70 rodata_dyn->in_dynarr_sz = 4; 71 for (i = 0; i < 4; i++) 72 rodata_dyn->in_dynarr[i] = i + 10; 73 74 err = test_skeleton__load(skel); 75 if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err)) 76 goto cleanup; 77 78 /* validate pre-setup values are still there */ 79 CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10); 80 CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL); 81 CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12); 82 CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL); 83 CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14); 84 85 ASSERT_EQ(rodata_dyn->in_dynarr_sz, 4, "in_dynarr_sz"); 86 for (i = 0; i < 4; i++) 87 ASSERT_EQ(rodata_dyn->in_dynarr[i], i + 10, "in_dynarr"); 88 89 /* now set new values and attach to get them into outX variables */ 90 data->in1 = 1; 91 data->in2 = 2; 92 bss->in3 = 3; 93 bss->in4 = 4; 94 bss->in5.a = 5; 95 bss->in5.b = 6; 96 kcfg = skel->kconfig; 97 98 skel->data_read_mostly->read_mostly_var = 123; 99 100 err = test_skeleton__attach(skel); 101 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) 102 goto cleanup; 103 104 /* trigger tracepoint */ 105 usleep(1); 106 107 CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1); 108 CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2); 109 CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3); 110 CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4); 111 CHECK(bss->out5.a != 5, "res5", "got %d != exp %d\n", bss->out5.a, 5); 112 CHECK(bss->out5.b != 6, "res6", "got %lld != exp %d\n", bss->out5.b, 6); 113 CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14); 114 115 CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1", 116 "got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL); 117 CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2", 118 "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION); 119 120 for (i = 0; i < 4; i++) 121 ASSERT_EQ(data_dyn->out_dynarr[i], i + 10, "out_dynarr"); 122 123 ASSERT_EQ(skel->bss->out_mostly_var, 123, "out_mostly_var"); 124 125 elf_bytes = test_skeleton__elf_bytes(&elf_bytes_sz); 126 ASSERT_OK_PTR(elf_bytes, "elf_bytes"); 127 ASSERT_GE(elf_bytes_sz, 0, "elf_bytes_sz"); 128 129 cleanup: 130 test_skeleton__destroy(skel); 131 } 132