xref: /openbmc/linux/tools/lib/bpf/libbpf_internal.h (revision e7f127b2)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Internal libbpf helpers.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 
9 #ifndef __LIBBPF_LIBBPF_INTERNAL_H
10 #define __LIBBPF_LIBBPF_INTERNAL_H
11 
12 #include <stdlib.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <linux/err.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include "libbpf_legacy.h"
19 #include "relo_core.h"
20 
21 /* make sure libbpf doesn't use kernel-only integer typedefs */
22 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
23 
24 /* prevent accidental re-addition of reallocarray() */
25 #pragma GCC poison reallocarray
26 
27 #include "libbpf.h"
28 #include "btf.h"
29 
30 #ifndef EM_BPF
31 #define EM_BPF 247
32 #endif
33 
34 #ifndef R_BPF_64_64
35 #define R_BPF_64_64 1
36 #endif
37 #ifndef R_BPF_64_ABS64
38 #define R_BPF_64_ABS64 2
39 #endif
40 #ifndef R_BPF_64_ABS32
41 #define R_BPF_64_ABS32 3
42 #endif
43 #ifndef R_BPF_64_32
44 #define R_BPF_64_32 10
45 #endif
46 
47 #ifndef SHT_LLVM_ADDRSIG
48 #define SHT_LLVM_ADDRSIG 0x6FFF4C03
49 #endif
50 
51 /* if libelf is old and doesn't support mmap(), fall back to read() */
52 #ifndef ELF_C_READ_MMAP
53 #define ELF_C_READ_MMAP ELF_C_READ
54 #endif
55 
56 /* Older libelf all end up in this expression, for both 32 and 64 bit */
57 #ifndef ELF64_ST_VISIBILITY
58 #define ELF64_ST_VISIBILITY(o) ((o) & 0x03)
59 #endif
60 
61 #define BTF_INFO_ENC(kind, kind_flag, vlen) \
62 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
63 #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
64 #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
65 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
66 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
67 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
68 	BTF_INT_ENC(encoding, bits_offset, bits)
69 #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
70 #define BTF_PARAM_ENC(name, type) (name), (type)
71 #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
72 #define BTF_TYPE_FLOAT_ENC(name, sz) \
73 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
74 #define BTF_TYPE_DECL_TAG_ENC(value, type, component_idx) \
75 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
76 #define BTF_TYPE_TYPE_TAG_ENC(value, type) \
77 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
78 
79 #ifndef likely
80 #define likely(x) __builtin_expect(!!(x), 1)
81 #endif
82 #ifndef unlikely
83 #define unlikely(x) __builtin_expect(!!(x), 0)
84 #endif
85 #ifndef min
86 # define min(x, y) ((x) < (y) ? (x) : (y))
87 #endif
88 #ifndef max
89 # define max(x, y) ((x) < (y) ? (y) : (x))
90 #endif
91 #ifndef offsetofend
92 # define offsetofend(TYPE, FIELD) \
93 	(offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
94 #endif
95 
96 /* Check whether a string `str` has prefix `pfx`, regardless if `pfx` is
97  * a string literal known at compilation time or char * pointer known only at
98  * runtime.
99  */
100 #define str_has_pfx(str, pfx) \
101 	(strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
102 
103 /* Symbol versioning is different between static and shared library.
104  * Properly versioned symbols are needed for shared library, but
105  * only the symbol of the new version is needed for static library.
106  * Starting with GNU C 10, use symver attribute instead of .symver assembler
107  * directive, which works better with GCC LTO builds.
108  */
109 #if defined(SHARED) && defined(__GNUC__) && __GNUC__ >= 10
110 
111 #define DEFAULT_VERSION(internal_name, api_name, version) \
112 	__attribute__((symver(#api_name "@@" #version)))
113 #define COMPAT_VERSION(internal_name, api_name, version) \
114 	__attribute__((symver(#api_name "@" #version)))
115 
116 #elif defined(SHARED)
117 
118 #define COMPAT_VERSION(internal_name, api_name, version) \
119 	asm(".symver " #internal_name "," #api_name "@" #version);
120 #define DEFAULT_VERSION(internal_name, api_name, version) \
121 	asm(".symver " #internal_name "," #api_name "@@" #version);
122 
123 #else /* !SHARED */
124 
125 #define COMPAT_VERSION(internal_name, api_name, version)
126 #define DEFAULT_VERSION(internal_name, api_name, version) \
127 	extern typeof(internal_name) api_name \
128 	__attribute__((alias(#internal_name)));
129 
130 #endif
131 
132 extern void libbpf_print(enum libbpf_print_level level,
133 			 const char *format, ...)
134 	__attribute__((format(printf, 2, 3)));
135 
136 #define __pr(level, fmt, ...)	\
137 do {				\
138 	libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__);	\
139 } while (0)
140 
141 #define pr_warn(fmt, ...)	__pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
142 #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
143 #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
144 
145 #ifndef __has_builtin
146 #define __has_builtin(x) 0
147 #endif
148 /*
149  * Re-implement glibc's reallocarray() for libbpf internal-only use.
150  * reallocarray(), unfortunately, is not available in all versions of glibc,
151  * so requires extra feature detection and using reallocarray() stub from
152  * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
153  * build of libbpf unnecessarily and is just a maintenance burden. Instead,
154  * it's trivial to implement libbpf-specific internal version and use it
155  * throughout libbpf.
156  */
157 static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
158 {
159 	size_t total;
160 
161 #if __has_builtin(__builtin_mul_overflow)
162 	if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
163 		return NULL;
164 #else
165 	if (size == 0 || nmemb > ULONG_MAX / size)
166 		return NULL;
167 	total = nmemb * size;
168 #endif
169 	return realloc(ptr, total);
170 }
171 
172 /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
173  * is zero-terminated string no matter what (unless sz == 0, in which case
174  * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
175  * in what is returned. Given this is internal helper, it's trivial to extend
176  * this, when necessary. Use this instead of strncpy inside libbpf source code.
177  */
178 static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
179 {
180 	size_t i;
181 
182 	if (sz == 0)
183 		return;
184 
185 	sz--;
186 	for (i = 0; i < sz && src[i]; i++)
187 		dst[i] = src[i];
188 	dst[i] = '\0';
189 }
190 
191 __u32 get_kernel_version(void);
192 
193 struct btf;
194 struct btf_type;
195 
196 struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id);
197 const char *btf_kind_str(const struct btf_type *t);
198 const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
199 
200 static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
201 {
202 	return (enum btf_func_linkage)(int)btf_vlen(t);
203 }
204 
205 static inline __u32 btf_type_info(int kind, int vlen, int kflag)
206 {
207 	return (kflag << 31) | (kind << 24) | vlen;
208 }
209 
210 enum map_def_parts {
211 	MAP_DEF_MAP_TYPE	= 0x001,
212 	MAP_DEF_KEY_TYPE	= 0x002,
213 	MAP_DEF_KEY_SIZE	= 0x004,
214 	MAP_DEF_VALUE_TYPE	= 0x008,
215 	MAP_DEF_VALUE_SIZE	= 0x010,
216 	MAP_DEF_MAX_ENTRIES	= 0x020,
217 	MAP_DEF_MAP_FLAGS	= 0x040,
218 	MAP_DEF_NUMA_NODE	= 0x080,
219 	MAP_DEF_PINNING		= 0x100,
220 	MAP_DEF_INNER_MAP	= 0x200,
221 	MAP_DEF_MAP_EXTRA	= 0x400,
222 
223 	MAP_DEF_ALL		= 0x7ff, /* combination of all above */
224 };
225 
226 struct btf_map_def {
227 	enum map_def_parts parts;
228 	__u32 map_type;
229 	__u32 key_type_id;
230 	__u32 key_size;
231 	__u32 value_type_id;
232 	__u32 value_size;
233 	__u32 max_entries;
234 	__u32 map_flags;
235 	__u32 numa_node;
236 	__u32 pinning;
237 	__u64 map_extra;
238 };
239 
240 int parse_btf_map_def(const char *map_name, struct btf *btf,
241 		      const struct btf_type *def_t, bool strict,
242 		      struct btf_map_def *map_def, struct btf_map_def *inner_def);
243 
244 void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
245 		     size_t cur_cnt, size_t max_cnt, size_t add_cnt);
246 int libbpf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt);
247 
248 static inline bool libbpf_is_mem_zeroed(const char *p, ssize_t len)
249 {
250 	while (len > 0) {
251 		if (*p)
252 			return false;
253 		p++;
254 		len--;
255 	}
256 	return true;
257 }
258 
259 static inline bool libbpf_validate_opts(const char *opts,
260 					size_t opts_sz, size_t user_sz,
261 					const char *type_name)
262 {
263 	if (user_sz < sizeof(size_t)) {
264 		pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
265 		return false;
266 	}
267 	if (!libbpf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) {
268 		pr_warn("%s has non-zero extra bytes\n", type_name);
269 		return false;
270 	}
271 	return true;
272 }
273 
274 #define OPTS_VALID(opts, type)						      \
275 	(!(opts) || libbpf_validate_opts((const char *)opts,		      \
276 					 offsetofend(struct type,	      \
277 						     type##__last_field),     \
278 					 (opts)->sz, #type))
279 #define OPTS_HAS(opts, field) \
280 	((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
281 #define OPTS_GET(opts, field, fallback_value) \
282 	(OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
283 #define OPTS_SET(opts, field, value)		\
284 	do {					\
285 		if (OPTS_HAS(opts, field))	\
286 			(opts)->field = value;	\
287 	} while (0)
288 
289 #define OPTS_ZEROED(opts, last_nonzero_field)				      \
290 ({									      \
291 	ssize_t __off = offsetofend(typeof(*(opts)), last_nonzero_field);     \
292 	!(opts) || libbpf_is_mem_zeroed((const void *)opts + __off,	      \
293 					(opts)->sz - __off);		      \
294 })
295 
296 enum kern_feature_id {
297 	/* v4.14: kernel support for program & map names. */
298 	FEAT_PROG_NAME,
299 	/* v5.2: kernel support for global data sections. */
300 	FEAT_GLOBAL_DATA,
301 	/* BTF support */
302 	FEAT_BTF,
303 	/* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
304 	FEAT_BTF_FUNC,
305 	/* BTF_KIND_VAR and BTF_KIND_DATASEC support */
306 	FEAT_BTF_DATASEC,
307 	/* BTF_FUNC_GLOBAL is supported */
308 	FEAT_BTF_GLOBAL_FUNC,
309 	/* BPF_F_MMAPABLE is supported for arrays */
310 	FEAT_ARRAY_MMAP,
311 	/* kernel support for expected_attach_type in BPF_PROG_LOAD */
312 	FEAT_EXP_ATTACH_TYPE,
313 	/* bpf_probe_read_{kernel,user}[_str] helpers */
314 	FEAT_PROBE_READ_KERN,
315 	/* BPF_PROG_BIND_MAP is supported */
316 	FEAT_PROG_BIND_MAP,
317 	/* Kernel support for module BTFs */
318 	FEAT_MODULE_BTF,
319 	/* BTF_KIND_FLOAT support */
320 	FEAT_BTF_FLOAT,
321 	/* BPF perf link support */
322 	FEAT_PERF_LINK,
323 	/* BTF_KIND_DECL_TAG support */
324 	FEAT_BTF_DECL_TAG,
325 	/* BTF_KIND_TYPE_TAG support */
326 	FEAT_BTF_TYPE_TAG,
327 	/* memcg-based accounting for BPF maps and progs */
328 	FEAT_MEMCG_ACCOUNT,
329 	__FEAT_CNT,
330 };
331 
332 int probe_memcg_account(void);
333 bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id);
334 int bump_rlimit_memlock(void);
335 
336 int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
337 int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
338 int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
339 			 const char *str_sec, size_t str_len);
340 int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 log_level);
341 
342 struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
343 void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
344 				const char **prefix, int *kind);
345 
346 struct btf_ext_info {
347 	/*
348 	 * info points to the individual info section (e.g. func_info and
349 	 * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
350 	 */
351 	void *info;
352 	__u32 rec_size;
353 	__u32 len;
354 };
355 
356 #define for_each_btf_ext_sec(seg, sec)					\
357 	for (sec = (seg)->info;						\
358 	     (void *)sec < (seg)->info + (seg)->len;			\
359 	     sec = (void *)sec + sizeof(struct btf_ext_info_sec) +	\
360 		   (seg)->rec_size * sec->num_info)
361 
362 #define for_each_btf_ext_rec(seg, sec, i, rec)				\
363 	for (i = 0, rec = (void *)&(sec)->data;				\
364 	     i < (sec)->num_info;					\
365 	     i++, rec = (void *)rec + (seg)->rec_size)
366 
367 /*
368  * The .BTF.ext ELF section layout defined as
369  *   struct btf_ext_header
370  *   func_info subsection
371  *
372  * The func_info subsection layout:
373  *   record size for struct bpf_func_info in the func_info subsection
374  *   struct btf_sec_func_info for section #1
375  *   a list of bpf_func_info records for section #1
376  *     where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
377  *     but may not be identical
378  *   struct btf_sec_func_info for section #2
379  *   a list of bpf_func_info records for section #2
380  *   ......
381  *
382  * Note that the bpf_func_info record size in .BTF.ext may not
383  * be the same as the one defined in include/uapi/linux/bpf.h.
384  * The loader should ensure that record_size meets minimum
385  * requirement and pass the record as is to the kernel. The
386  * kernel will handle the func_info properly based on its contents.
387  */
388 struct btf_ext_header {
389 	__u16	magic;
390 	__u8	version;
391 	__u8	flags;
392 	__u32	hdr_len;
393 
394 	/* All offsets are in bytes relative to the end of this header */
395 	__u32	func_info_off;
396 	__u32	func_info_len;
397 	__u32	line_info_off;
398 	__u32	line_info_len;
399 
400 	/* optional part of .BTF.ext header */
401 	__u32	core_relo_off;
402 	__u32	core_relo_len;
403 };
404 
405 struct btf_ext {
406 	union {
407 		struct btf_ext_header *hdr;
408 		void *data;
409 	};
410 	struct btf_ext_info func_info;
411 	struct btf_ext_info line_info;
412 	struct btf_ext_info core_relo_info;
413 	__u32 data_size;
414 };
415 
416 struct btf_ext_info_sec {
417 	__u32	sec_name_off;
418 	__u32	num_info;
419 	/* Followed by num_info * record_size number of bytes */
420 	__u8	data[];
421 };
422 
423 /* The minimum bpf_func_info checked by the loader */
424 struct bpf_func_info_min {
425 	__u32   insn_off;
426 	__u32   type_id;
427 };
428 
429 /* The minimum bpf_line_info checked by the loader */
430 struct bpf_line_info_min {
431 	__u32	insn_off;
432 	__u32	file_name_off;
433 	__u32	line_off;
434 	__u32	line_col;
435 };
436 
437 
438 typedef int (*type_id_visit_fn)(__u32 *type_id, void *ctx);
439 typedef int (*str_off_visit_fn)(__u32 *str_off, void *ctx);
440 int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx);
441 int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ctx);
442 int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx);
443 int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx);
444 __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name,
445 				 __u32 kind);
446 
447 extern enum libbpf_strict_mode libbpf_mode;
448 
449 /* handle direct returned errors */
450 static inline int libbpf_err(int ret)
451 {
452 	if (ret < 0)
453 		errno = -ret;
454 	return ret;
455 }
456 
457 /* handle errno-based (e.g., syscall or libc) errors according to libbpf's
458  * strict mode settings
459  */
460 static inline int libbpf_err_errno(int ret)
461 {
462 	if (libbpf_mode & LIBBPF_STRICT_DIRECT_ERRS)
463 		/* errno is already assumed to be set on error */
464 		return ret < 0 ? -errno : ret;
465 
466 	/* legacy: on error return -1 directly and don't touch errno */
467 	return ret;
468 }
469 
470 /* handle error for pointer-returning APIs, err is assumed to be < 0 always */
471 static inline void *libbpf_err_ptr(int err)
472 {
473 	/* set errno on error, this doesn't break anything */
474 	errno = -err;
475 
476 	if (libbpf_mode & LIBBPF_STRICT_CLEAN_PTRS)
477 		return NULL;
478 
479 	/* legacy: encode err as ptr */
480 	return ERR_PTR(err);
481 }
482 
483 /* handle pointer-returning APIs' error handling */
484 static inline void *libbpf_ptr(void *ret)
485 {
486 	/* set errno on error, this doesn't break anything */
487 	if (IS_ERR(ret))
488 		errno = -PTR_ERR(ret);
489 
490 	if (libbpf_mode & LIBBPF_STRICT_CLEAN_PTRS)
491 		return IS_ERR(ret) ? NULL : ret;
492 
493 	/* legacy: pass-through original pointer */
494 	return ret;
495 }
496 
497 static inline bool str_is_empty(const char *s)
498 {
499 	return !s || !s[0];
500 }
501 
502 static inline bool is_ldimm64_insn(struct bpf_insn *insn)
503 {
504 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
505 }
506 
507 /* if fd is stdin, stdout, or stderr, dup to a fd greater than 2
508  * Takes ownership of the fd passed in, and closes it if calling
509  * fcntl(fd, F_DUPFD_CLOEXEC, 3).
510  */
511 static inline int ensure_good_fd(int fd)
512 {
513 	int old_fd = fd, saved_errno;
514 
515 	if (fd < 0)
516 		return fd;
517 	if (fd < 3) {
518 		fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
519 		saved_errno = errno;
520 		close(old_fd);
521 		if (fd < 0) {
522 			pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno);
523 			errno = saved_errno;
524 		}
525 	}
526 	return fd;
527 }
528 
529 #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
530