1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020, Oracle and/or its affiliates. */ 3 4 #include "btf_ptr.h" 5 #include <bpf/bpf_helpers.h> 6 #include <bpf/bpf_tracing.h> 7 #include <bpf/bpf_core_read.h> 8 #include "bpf_misc.h" 9 10 #include <errno.h> 11 12 long ret = 0; 13 int num_subtests = 0; 14 int ran_subtests = 0; 15 bool skip = false; 16 17 #define STRSIZE 2048 18 #define EXPECTED_STRSIZE 256 19 20 #if defined(bpf_target_s390) 21 /* NULL points to a readable struct lowcore on s390, so take the last page */ 22 #define BADPTR ((void *)0xFFFFFFFFFFFFF000ULL) 23 #else 24 #define BADPTR 0 25 #endif 26 27 struct { 28 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); 29 __uint(max_entries, 1); 30 __type(key, __u32); 31 __type(value, char[STRSIZE]); 32 } strdata SEC(".maps"); 33 34 static int __strncmp(const void *m1, const void *m2, size_t len) 35 { 36 const unsigned char *s1 = m1; 37 const unsigned char *s2 = m2; 38 int i, delta = 0; 39 40 for (i = 0; i < len; i++) { 41 delta = s1[i] - s2[i]; 42 if (delta || s1[i] == 0 || s2[i] == 0) 43 break; 44 } 45 return delta; 46 } 47 48 #if __has_builtin(__builtin_btf_type_id) 49 #define TEST_BTF(_str, _type, _flags, _expected, ...) \ 50 do { \ 51 static const char _expectedval[EXPECTED_STRSIZE] = \ 52 _expected; \ 53 __u64 _hflags = _flags | BTF_F_COMPACT; \ 54 static _type _ptrdata = __VA_ARGS__; \ 55 static struct btf_ptr _ptr = { }; \ 56 int _cmp; \ 57 \ 58 ++num_subtests; \ 59 if (ret < 0) \ 60 break; \ 61 ++ran_subtests; \ 62 _ptr.ptr = &_ptrdata; \ 63 _ptr.type_id = bpf_core_type_id_kernel(_type); \ 64 if (_ptr.type_id <= 0) { \ 65 ret = -EINVAL; \ 66 break; \ 67 } \ 68 ret = bpf_snprintf_btf(_str, STRSIZE, \ 69 &_ptr, sizeof(_ptr), _hflags); \ 70 if (ret) \ 71 break; \ 72 _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \ 73 if (_cmp != 0) { \ 74 bpf_printk("(%d) got %s", _cmp, _str); \ 75 bpf_printk("(%d) expected %s", _cmp, \ 76 _expectedval); \ 77 ret = -EBADMSG; \ 78 break; \ 79 } \ 80 } while (0) 81 #endif 82 83 /* Use where expected data string matches its stringified declaration */ 84 #define TEST_BTF_C(_str, _type, _flags, ...) \ 85 TEST_BTF(_str, _type, _flags, "(" #_type ")" #__VA_ARGS__, \ 86 __VA_ARGS__) 87 88 /* TRACE_EVENT(netif_receive_skb, 89 * TP_PROTO(struct sk_buff *skb), 90 */ 91 SEC("tp_btf/netif_receive_skb") 92 int BPF_PROG(trace_netif_receive_skb, struct sk_buff *skb) 93 { 94 static __u64 flags[] = { 0, BTF_F_COMPACT, BTF_F_ZERO, BTF_F_PTR_RAW, 95 BTF_F_NONAME, BTF_F_COMPACT | BTF_F_ZERO | 96 BTF_F_PTR_RAW | BTF_F_NONAME }; 97 static struct btf_ptr p = { }; 98 __u32 key = 0; 99 int i, __ret; 100 char *str; 101 102 #if __has_builtin(__builtin_btf_type_id) 103 str = bpf_map_lookup_elem(&strdata, &key); 104 if (!str) 105 return 0; 106 107 /* Ensure we can write skb string representation */ 108 p.type_id = bpf_core_type_id_kernel(struct sk_buff); 109 p.ptr = skb; 110 for (i = 0; i < ARRAY_SIZE(flags); i++) { 111 ++num_subtests; 112 ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0); 113 if (ret < 0) 114 bpf_printk("returned %d when writing skb", ret); 115 ++ran_subtests; 116 } 117 118 /* Check invalid ptr value */ 119 p.ptr = BADPTR; 120 __ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0); 121 if (__ret >= 0) { 122 bpf_printk("printing %llx should generate error, got (%d)", 123 (unsigned long long)BADPTR, __ret); 124 ret = -ERANGE; 125 } 126 127 /* Verify type display for various types. */ 128 129 /* simple int */ 130 TEST_BTF_C(str, int, 0, 1234); 131 TEST_BTF(str, int, BTF_F_NONAME, "1234", 1234); 132 /* zero value should be printed at toplevel */ 133 TEST_BTF(str, int, 0, "(int)0", 0); 134 TEST_BTF(str, int, BTF_F_NONAME, "0", 0); 135 TEST_BTF(str, int, BTF_F_ZERO, "(int)0", 0); 136 TEST_BTF(str, int, BTF_F_NONAME | BTF_F_ZERO, "0", 0); 137 TEST_BTF_C(str, int, 0, -4567); 138 TEST_BTF(str, int, BTF_F_NONAME, "-4567", -4567); 139 140 /* simple char */ 141 TEST_BTF_C(str, char, 0, 100); 142 TEST_BTF(str, char, BTF_F_NONAME, "100", 100); 143 /* zero value should be printed at toplevel */ 144 TEST_BTF(str, char, 0, "(char)0", 0); 145 TEST_BTF(str, char, BTF_F_NONAME, "0", 0); 146 TEST_BTF(str, char, BTF_F_ZERO, "(char)0", 0); 147 TEST_BTF(str, char, BTF_F_NONAME | BTF_F_ZERO, "0", 0); 148 149 /* simple typedef */ 150 TEST_BTF_C(str, uint64_t, 0, 100); 151 TEST_BTF(str, u64, BTF_F_NONAME, "1", 1); 152 /* zero value should be printed at toplevel */ 153 TEST_BTF(str, u64, 0, "(u64)0", 0); 154 TEST_BTF(str, u64, BTF_F_NONAME, "0", 0); 155 TEST_BTF(str, u64, BTF_F_ZERO, "(u64)0", 0); 156 TEST_BTF(str, u64, BTF_F_NONAME|BTF_F_ZERO, "0", 0); 157 158 /* typedef struct */ 159 TEST_BTF_C(str, atomic_t, 0, {.counter = (int)1,}); 160 TEST_BTF(str, atomic_t, BTF_F_NONAME, "{1,}", {.counter = 1,}); 161 /* typedef with 0 value should be printed at toplevel */ 162 TEST_BTF(str, atomic_t, 0, "(atomic_t){}", {.counter = 0,}); 163 TEST_BTF(str, atomic_t, BTF_F_NONAME, "{}", {.counter = 0,}); 164 TEST_BTF(str, atomic_t, BTF_F_ZERO, "(atomic_t){.counter = (int)0,}", 165 {.counter = 0,}); 166 TEST_BTF(str, atomic_t, BTF_F_NONAME|BTF_F_ZERO, 167 "{0,}", {.counter = 0,}); 168 169 /* enum where enum value does (and does not) exist */ 170 TEST_BTF_C(str, enum bpf_cmd, 0, BPF_MAP_CREATE); 171 TEST_BTF(str, enum bpf_cmd, 0, "(enum bpf_cmd)BPF_MAP_CREATE", 0); 172 TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME, "BPF_MAP_CREATE", 173 BPF_MAP_CREATE); 174 TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME|BTF_F_ZERO, 175 "BPF_MAP_CREATE", 0); 176 177 TEST_BTF(str, enum bpf_cmd, BTF_F_ZERO, "(enum bpf_cmd)BPF_MAP_CREATE", 178 BPF_MAP_CREATE); 179 TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME|BTF_F_ZERO, 180 "BPF_MAP_CREATE", BPF_MAP_CREATE); 181 TEST_BTF_C(str, enum bpf_cmd, 0, 2000); 182 TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME, "2000", 2000); 183 184 /* simple struct */ 185 TEST_BTF_C(str, struct btf_enum, 0, 186 {.name_off = (__u32)3,.val = (__s32)-1,}); 187 TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{3,-1,}", 188 { .name_off = 3, .val = -1,}); 189 TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{-1,}", 190 { .name_off = 0, .val = -1,}); 191 TEST_BTF(str, struct btf_enum, BTF_F_NONAME|BTF_F_ZERO, "{0,-1,}", 192 { .name_off = 0, .val = -1,}); 193 /* empty struct should be printed */ 194 TEST_BTF(str, struct btf_enum, 0, "(struct btf_enum){}", 195 { .name_off = 0, .val = 0,}); 196 TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{}", 197 { .name_off = 0, .val = 0,}); 198 TEST_BTF(str, struct btf_enum, BTF_F_ZERO, 199 "(struct btf_enum){.name_off = (__u32)0,.val = (__s32)0,}", 200 { .name_off = 0, .val = 0,}); 201 202 /* struct with pointers */ 203 TEST_BTF(str, struct list_head, BTF_F_PTR_RAW, 204 "(struct list_head){.next = (struct list_head *)0x0000000000000001,}", 205 { .next = (struct list_head *)1 }); 206 /* NULL pointer should not be displayed */ 207 TEST_BTF(str, struct list_head, BTF_F_PTR_RAW, 208 "(struct list_head){}", 209 { .next = (struct list_head *)0 }); 210 211 /* struct with char array */ 212 TEST_BTF(str, struct bpf_prog_info, 0, 213 "(struct bpf_prog_info){.name = (char[])['f','o','o',],}", 214 { .name = "foo",}); 215 TEST_BTF(str, struct bpf_prog_info, BTF_F_NONAME, 216 "{['f','o','o',],}", 217 {.name = "foo",}); 218 /* leading null char means do not display string */ 219 TEST_BTF(str, struct bpf_prog_info, 0, 220 "(struct bpf_prog_info){}", 221 {.name = {'\0', 'f', 'o', 'o'}}); 222 /* handle non-printable characters */ 223 TEST_BTF(str, struct bpf_prog_info, 0, 224 "(struct bpf_prog_info){.name = (char[])[1,2,3,],}", 225 { .name = {1, 2, 3, 0}}); 226 227 /* struct with non-char array */ 228 TEST_BTF(str, struct __sk_buff, 0, 229 "(struct __sk_buff){.cb = (__u32[])[1,2,3,4,5,],}", 230 { .cb = {1, 2, 3, 4, 5,},}); 231 TEST_BTF(str, struct __sk_buff, BTF_F_NONAME, 232 "{[1,2,3,4,5,],}", 233 { .cb = { 1, 2, 3, 4, 5},}); 234 /* For non-char, arrays, show non-zero values only */ 235 TEST_BTF(str, struct __sk_buff, 0, 236 "(struct __sk_buff){.cb = (__u32[])[1,],}", 237 { .cb = { 0, 0, 1, 0, 0},}); 238 239 /* struct with bitfields */ 240 TEST_BTF_C(str, struct bpf_insn, 0, 241 {.code = (__u8)1,.dst_reg = (__u8)0x2,.src_reg = (__u8)0x3,.off = (__s16)4,.imm = (__s32)5,}); 242 TEST_BTF(str, struct bpf_insn, BTF_F_NONAME, "{1,0x2,0x3,4,5,}", 243 {.code = 1, .dst_reg = 0x2, .src_reg = 0x3, .off = 4, 244 .imm = 5,}); 245 #else 246 skip = true; 247 #endif 248 249 return 0; 250 } 251 252 char _license[] SEC("license") = "GPL"; 253