1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020 Facebook */ 3 #include <test_progs.h> 4 #include "test_global_func1.skel.h" 5 #include "test_global_func2.skel.h" 6 #include "test_global_func3.skel.h" 7 #include "test_global_func4.skel.h" 8 #include "test_global_func5.skel.h" 9 #include "test_global_func6.skel.h" 10 #include "test_global_func7.skel.h" 11 #include "test_global_func8.skel.h" 12 #include "test_global_func9.skel.h" 13 #include "test_global_func10.skel.h" 14 #include "test_global_func11.skel.h" 15 #include "test_global_func12.skel.h" 16 #include "test_global_func13.skel.h" 17 #include "test_global_func14.skel.h" 18 #include "test_global_func15.skel.h" 19 #include "test_global_func16.skel.h" 20 #include "test_global_func17.skel.h" 21 #include "test_global_func_deep_stack.skel.h" 22 #include "test_global_func_ctx_args.skel.h" 23 24 #include "bpf/libbpf_internal.h" 25 #include "btf_helpers.h" 26 27 static void check_ctx_arg_type(const struct btf *btf, const struct btf_param *p) 28 { 29 const struct btf_type *t; 30 const char *s; 31 32 t = btf__type_by_id(btf, p->type); 33 if (!ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_t")) 34 return; 35 36 s = btf_type_raw_dump(btf, t->type); 37 if (!ASSERT_HAS_SUBSTR(s, "STRUCT 'bpf_perf_event_data' size=0 vlen=0", 38 "ctx_struct_t")) 39 return; 40 } 41 42 static void subtest_ctx_arg_rewrite(void) 43 { 44 struct test_global_func_ctx_args *skel = NULL; 45 struct bpf_prog_info info; 46 char func_info_buf[1024] __attribute__((aligned(8))); 47 struct bpf_func_info_min *rec; 48 struct btf *btf = NULL; 49 __u32 info_len = sizeof(info); 50 int err, fd, i; 51 struct btf *kern_btf = NULL; 52 53 kern_btf = btf__load_vmlinux_btf(); 54 if (!ASSERT_OK_PTR(kern_btf, "kern_btf_load")) 55 return; 56 57 /* simple detection of kernel native arg:ctx tag support */ 58 if (btf__find_by_name_kind(kern_btf, "bpf_subprog_arg_info", BTF_KIND_STRUCT) > 0) { 59 test__skip(); 60 btf__free(kern_btf); 61 return; 62 } 63 btf__free(kern_btf); 64 65 skel = test_global_func_ctx_args__open(); 66 if (!ASSERT_OK_PTR(skel, "skel_open")) 67 return; 68 69 bpf_program__set_autoload(skel->progs.arg_tag_ctx_perf, true); 70 71 err = test_global_func_ctx_args__load(skel); 72 if (!ASSERT_OK(err, "skel_load")) 73 goto out; 74 75 memset(&info, 0, sizeof(info)); 76 info.func_info = ptr_to_u64(&func_info_buf); 77 info.nr_func_info = 3; 78 info.func_info_rec_size = sizeof(struct bpf_func_info_min); 79 80 fd = bpf_program__fd(skel->progs.arg_tag_ctx_perf); 81 err = bpf_prog_get_info_by_fd(fd, &info, &info_len); 82 if (!ASSERT_OK(err, "prog_info")) 83 goto out; 84 85 if (!ASSERT_EQ(info.nr_func_info, 3, "nr_func_info")) 86 goto out; 87 88 btf = btf__load_from_kernel_by_id(info.btf_id); 89 if (!ASSERT_OK_PTR(btf, "obj_kern_btf")) 90 goto out; 91 92 rec = (struct bpf_func_info_min *)func_info_buf; 93 for (i = 0; i < info.nr_func_info; i++, rec = (void *)rec + info.func_info_rec_size) { 94 const struct btf_type *fn_t, *proto_t; 95 const char *name; 96 97 if (rec->insn_off == 0) 98 continue; /* main prog, skip */ 99 100 fn_t = btf__type_by_id(btf, rec->type_id); 101 if (!ASSERT_OK_PTR(fn_t, "fn_type")) 102 goto out; 103 if (!ASSERT_EQ(btf_kind(fn_t), BTF_KIND_FUNC, "fn_type_kind")) 104 goto out; 105 proto_t = btf__type_by_id(btf, fn_t->type); 106 if (!ASSERT_OK_PTR(proto_t, "proto_type")) 107 goto out; 108 109 name = btf__name_by_offset(btf, fn_t->name_off); 110 if (strcmp(name, "subprog_ctx_tag") == 0) { 111 /* int subprog_ctx_tag(void *ctx __arg_ctx) */ 112 if (!ASSERT_EQ(btf_vlen(proto_t), 1, "arg_cnt")) 113 goto out; 114 115 /* arg 0 is PTR -> STRUCT bpf_perf_event_data */ 116 check_ctx_arg_type(btf, &btf_params(proto_t)[0]); 117 } else if (strcmp(name, "subprog_multi_ctx_tags") == 0) { 118 /* int subprog_multi_ctx_tags(void *ctx1 __arg_ctx, 119 * struct my_struct *mem, 120 * void *ctx2 __arg_ctx) 121 */ 122 if (!ASSERT_EQ(btf_vlen(proto_t), 3, "arg_cnt")) 123 goto out; 124 125 /* arg 0 is PTR -> STRUCT bpf_perf_event_data */ 126 check_ctx_arg_type(btf, &btf_params(proto_t)[0]); 127 /* arg 2 is PTR -> STRUCT bpf_perf_event_data */ 128 check_ctx_arg_type(btf, &btf_params(proto_t)[2]); 129 } else { 130 ASSERT_FAIL("unexpected subprog %s", name); 131 goto out; 132 } 133 } 134 135 out: 136 btf__free(btf); 137 test_global_func_ctx_args__destroy(skel); 138 } 139 140 void test_test_global_funcs(void) 141 { 142 RUN_TESTS(test_global_func1); 143 RUN_TESTS(test_global_func2); 144 RUN_TESTS(test_global_func3); 145 RUN_TESTS(test_global_func4); 146 RUN_TESTS(test_global_func5); 147 RUN_TESTS(test_global_func6); 148 RUN_TESTS(test_global_func7); 149 RUN_TESTS(test_global_func8); 150 RUN_TESTS(test_global_func9); 151 RUN_TESTS(test_global_func10); 152 RUN_TESTS(test_global_func11); 153 RUN_TESTS(test_global_func12); 154 RUN_TESTS(test_global_func13); 155 RUN_TESTS(test_global_func14); 156 RUN_TESTS(test_global_func15); 157 RUN_TESTS(test_global_func16); 158 RUN_TESTS(test_global_func17); 159 RUN_TESTS(test_global_func_deep_stack); 160 RUN_TESTS(test_global_func_ctx_args); 161 162 if (test__start_subtest("ctx_arg_rewrite")) 163 subtest_ctx_arg_rewrite(); 164 } 165