Coverage Report

Created: 2026-10-31 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libbpf/src/libbpf_internal.h
Line
Count
Source
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 <byteswap.h>
14
#include <limits.h>
15
#include <errno.h>
16
#include <linux/err.h>
17
#include <fcntl.h>
18
#include <unistd.h>
19
#include <sys/syscall.h>
20
#include <libelf.h>
21
#include "relo_core.h"
22
23
/* Android's libc doesn't support AT_EACCESS in faccessat() implementation
24
 * ([0]), and just returns -EINVAL even if file exists and is accessible.
25
 * See [1] for issues caused by this.
26
 *
27
 * So just redefine it to 0 on Android.
28
 *
29
 * [0] https://android.googlesource.com/platform/bionic/+/refs/heads/android13-release/libc/bionic/faccessat.cpp#50
30
 * [1] https://github.com/libbpf/libbpf-bootstrap/issues/250#issuecomment-1911324250
31
 */
32
#ifdef __ANDROID__
33
#undef AT_EACCESS
34
#define AT_EACCESS 0
35
#endif
36
37
/* make sure libbpf doesn't use kernel-only integer typedefs */
38
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
39
40
/* prevent accidental re-addition of reallocarray() */
41
#pragma GCC poison reallocarray
42
43
#include "libbpf.h"
44
#include "btf.h"
45
46
#ifndef EM_BPF
47
#define EM_BPF 247
48
#endif
49
50
#ifndef R_BPF_64_64
51
#define R_BPF_64_64 1
52
#endif
53
#ifndef R_BPF_64_ABS64
54
11.7k
#define R_BPF_64_ABS64 2
55
#endif
56
#ifndef R_BPF_64_ABS32
57
#define R_BPF_64_ABS32 3
58
#endif
59
#ifndef R_BPF_64_32
60
#define R_BPF_64_32 10
61
#endif
62
63
#ifndef SHT_LLVM_ADDRSIG
64
62.4k
#define SHT_LLVM_ADDRSIG 0x6FFF4C03
65
#endif
66
67
/* if libelf is old and doesn't support mmap(), fall back to read() */
68
#ifndef ELF_C_READ_MMAP
69
0
#define ELF_C_READ_MMAP ELF_C_READ
70
#endif
71
72
/* Older libelf all end up in this expression, for both 32 and 64 bit */
73
#ifndef ELF64_ST_VISIBILITY
74
#define ELF64_ST_VISIBILITY(o) ((o) & 0x03)
75
#endif
76
77
2.12k
#define JUMPTABLES_SEC ".jumptables"
78
79
#define BTF_INFO_ENC(kind, kind_flag, vlen) \
80
0
  (((__u32)(!!(kind_flag)) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
81
0
#define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
82
#define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
83
0
  ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
84
#define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
85
0
  BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
86
0
  BTF_INT_ENC(encoding, bits_offset, bits)
87
0
#define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
88
0
#define BTF_PARAM_ENC(name, type) (name), (type)
89
0
#define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
90
#define BTF_TYPE_FLOAT_ENC(name, sz) \
91
0
  BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
92
#define BTF_TYPE_DECL_TAG_ENC(value, type, component_idx) \
93
0
  BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
94
#define BTF_TYPE_TYPE_TAG_ENC(value, type) \
95
0
  BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
96
97
#ifndef likely
98
#define likely(x) __builtin_expect(!!(x), 1)
99
#endif
100
#ifndef unlikely
101
0
#define unlikely(x) __builtin_expect(!!(x), 0)
102
#endif
103
#ifndef min
104
# define min(x, y) ((x) < (y) ? (x) : (y))
105
#endif
106
#ifndef max
107
# define max(x, y) ((x) < (y) ? (y) : (x))
108
#endif
109
#ifndef offsetofend
110
# define offsetofend(TYPE, FIELD) \
111
239k
  (offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
112
#endif
113
#ifndef __alias
114
#define __alias(symbol) __attribute__((alias(#symbol)))
115
#endif
116
117
/* Check whether a string `str` has prefix `pfx`, regardless if `pfx` is
118
 * a string literal known at compilation time or char * pointer known only at
119
 * runtime.
120
 */
121
#define str_has_pfx(str, pfx) \
122
158k
  (strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
123
124
/* suffix check */
125
static inline bool str_has_sfx(const char *str, const char *sfx)
126
0
{
127
0
  size_t str_len = strlen(str);
128
0
  size_t sfx_len = strlen(sfx);
129
130
0
  if (sfx_len > str_len)
131
0
    return false;
132
0
  return strcmp(str + str_len - sfx_len, sfx) == 0;
133
0
}
Unexecuted instantiation: libbpf.c:str_has_sfx
Unexecuted instantiation: libbpf_utils.c:str_has_sfx
Unexecuted instantiation: libbpf_probes.c:str_has_sfx
Unexecuted instantiation: gen_loader.c:str_has_sfx
Unexecuted instantiation: relo_core.c:str_has_sfx
Unexecuted instantiation: usdt.c:str_has_sfx
Unexecuted instantiation: zip.c:str_has_sfx
Unexecuted instantiation: elf.c:str_has_sfx
Unexecuted instantiation: features.c:str_has_sfx
Unexecuted instantiation: bpf.c:str_has_sfx
Unexecuted instantiation: btf.c:str_has_sfx
Unexecuted instantiation: strset.c:str_has_sfx
Unexecuted instantiation: btf_iter.c:str_has_sfx
Unexecuted instantiation: btf_relocate.c:str_has_sfx
134
135
/* Symbol versioning is different between static and shared library.
136
 * Properly versioned symbols are needed for shared library, but
137
 * only the symbol of the new version is needed for static library.
138
 * Starting with GNU C 10, use symver attribute instead of .symver assembler
139
 * directive, which works better with GCC LTO builds.
140
 */
141
#if defined(SHARED) && defined(__GNUC__) && __GNUC__ >= 10
142
143
#define DEFAULT_VERSION(internal_name, api_name, version) \
144
  __attribute__((symver(#api_name "@@" #version)))
145
#define COMPAT_VERSION(internal_name, api_name, version) \
146
  __attribute__((symver(#api_name "@" #version)))
147
148
#elif defined(SHARED)
149
150
#define COMPAT_VERSION(internal_name, api_name, version) \
151
  asm(".symver " #internal_name "," #api_name "@" #version);
152
#define DEFAULT_VERSION(internal_name, api_name, version) \
153
  asm(".symver " #internal_name "," #api_name "@@" #version);
154
155
#else /* !SHARED */
156
157
#define COMPAT_VERSION(internal_name, api_name, version)
158
#define DEFAULT_VERSION(internal_name, api_name, version) \
159
  extern typeof(internal_name) api_name \
160
  __attribute__((alias(#internal_name)));
161
162
#endif
163
164
extern void libbpf_print(enum libbpf_print_level level,
165
       const char *format, ...)
166
  __attribute__((format(printf, 2, 3)));
167
168
176k
#define __pr(level, fmt, ...) \
169
176k
do {       \
170
176k
  libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__);  \
171
176k
} while (0)
172
173
18.3k
#define pr_warn(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
174
34.5k
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
175
123k
#define pr_debug(fmt, ...)  __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
176
177
/**
178
 * @brief **libbpf_errstr()** returns string corresponding to numeric errno
179
 * @param err negative numeric errno
180
 * @return pointer to string representation of the errno, that is invalidated
181
 * upon the next call.
182
 */
183
const char *libbpf_errstr(int err);
184
185
1.80k
#define errstr(err) libbpf_errstr(err)
186
187
#ifndef __has_builtin
188
#define __has_builtin(x) 0
189
#endif
190
191
struct bpf_link {
192
  int (*detach)(struct bpf_link *link);
193
  void (*dealloc)(struct bpf_link *link);
194
  char *pin_path;   /* NULL, if not pinned */
195
  int fd;     /* hook FD, -1 if not applicable */
196
  bool disconnected;
197
};
198
199
/*
200
 * Re-implement glibc's reallocarray() for libbpf internal-only use.
201
 * reallocarray(), unfortunately, is not available in all versions of glibc,
202
 * so requires extra feature detection and using reallocarray() stub from
203
 * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
204
 * build of libbpf unnecessarily and is just a maintenance burden. Instead,
205
 * it's trivial to implement libbpf-specific internal version and use it
206
 * throughout libbpf.
207
 */
208
static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
209
17.4k
{
210
17.4k
  size_t total;
211
212
17.4k
#if __has_builtin(__builtin_mul_overflow)
213
17.4k
  if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
214
0
    return NULL;
215
#else
216
  if (size == 0 || nmemb > ULONG_MAX / size)
217
    return NULL;
218
  total = nmemb * size;
219
#endif
220
17.4k
  return realloc(ptr, total);
221
17.4k
}
libbpf.c:libbpf_reallocarray
Line
Count
Source
209
11.1k
{
210
11.1k
  size_t total;
211
212
11.1k
#if __has_builtin(__builtin_mul_overflow)
213
11.1k
  if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
214
0
    return NULL;
215
#else
216
  if (size == 0 || nmemb > ULONG_MAX / size)
217
    return NULL;
218
  total = nmemb * size;
219
#endif
220
11.1k
  return realloc(ptr, total);
221
11.1k
}
Unexecuted instantiation: libbpf_utils.c:libbpf_reallocarray
Unexecuted instantiation: libbpf_probes.c:libbpf_reallocarray
Unexecuted instantiation: gen_loader.c:libbpf_reallocarray
Unexecuted instantiation: relo_core.c:libbpf_reallocarray
Unexecuted instantiation: usdt.c:libbpf_reallocarray
Unexecuted instantiation: zip.c:libbpf_reallocarray
Unexecuted instantiation: elf.c:libbpf_reallocarray
Unexecuted instantiation: features.c:libbpf_reallocarray
Unexecuted instantiation: bpf.c:libbpf_reallocarray
btf.c:libbpf_reallocarray
Line
Count
Source
209
6.30k
{
210
6.30k
  size_t total;
211
212
6.30k
#if __has_builtin(__builtin_mul_overflow)
213
6.30k
  if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
214
0
    return NULL;
215
#else
216
  if (size == 0 || nmemb > ULONG_MAX / size)
217
    return NULL;
218
  total = nmemb * size;
219
#endif
220
6.30k
  return realloc(ptr, total);
221
6.30k
}
Unexecuted instantiation: strset.c:libbpf_reallocarray
Unexecuted instantiation: btf_iter.c:libbpf_reallocarray
Unexecuted instantiation: btf_relocate.c:libbpf_reallocarray
222
223
/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
224
 * is zero-terminated string no matter what (unless sz == 0, in which case
225
 * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
226
 * in what is returned. Given this is internal helper, it's trivial to extend
227
 * this, when necessary. Use this instead of strncpy inside libbpf source code.
228
 */
229
static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
230
12.6k
{
231
12.6k
  size_t i;
232
233
12.6k
  if (sz == 0)
234
0
    return;
235
236
12.6k
  sz--;
237
146k
  for (i = 0; i < sz && src[i]; i++)
238
134k
    dst[i] = src[i];
239
12.6k
  dst[i] = '\0';
240
12.6k
}
libbpf.c:libbpf_strlcpy
Line
Count
Source
230
12.6k
{
231
12.6k
  size_t i;
232
233
12.6k
  if (sz == 0)
234
0
    return;
235
236
12.6k
  sz--;
237
146k
  for (i = 0; i < sz && src[i]; i++)
238
134k
    dst[i] = src[i];
239
12.6k
  dst[i] = '\0';
240
12.6k
}
Unexecuted instantiation: libbpf_utils.c:libbpf_strlcpy
Unexecuted instantiation: libbpf_probes.c:libbpf_strlcpy
Unexecuted instantiation: gen_loader.c:libbpf_strlcpy
Unexecuted instantiation: relo_core.c:libbpf_strlcpy
Unexecuted instantiation: usdt.c:libbpf_strlcpy
Unexecuted instantiation: zip.c:libbpf_strlcpy
Unexecuted instantiation: elf.c:libbpf_strlcpy
Unexecuted instantiation: features.c:libbpf_strlcpy
Unexecuted instantiation: bpf.c:libbpf_strlcpy
Unexecuted instantiation: btf.c:libbpf_strlcpy
Unexecuted instantiation: strset.c:libbpf_strlcpy
Unexecuted instantiation: btf_iter.c:libbpf_strlcpy
Unexecuted instantiation: btf_relocate.c:libbpf_strlcpy
241
242
__u32 get_kernel_version(void);
243
244
struct btf;
245
struct btf_type;
246
247
struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id);
248
const char *btf_kind_str(const struct btf_type *t);
249
const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
250
const struct btf_header *btf_header(const struct btf *btf);
251
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
252
int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);
253
bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t);
254
255
static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
256
199
{
257
199
  return (enum btf_func_linkage)(int)btf_vlen(t);
258
199
}
libbpf.c:btf_func_linkage
Line
Count
Source
256
199
{
257
199
  return (enum btf_func_linkage)(int)btf_vlen(t);
258
199
}
Unexecuted instantiation: libbpf_utils.c:btf_func_linkage
Unexecuted instantiation: libbpf_probes.c:btf_func_linkage
Unexecuted instantiation: gen_loader.c:btf_func_linkage
Unexecuted instantiation: relo_core.c:btf_func_linkage
Unexecuted instantiation: usdt.c:btf_func_linkage
Unexecuted instantiation: zip.c:btf_func_linkage
Unexecuted instantiation: elf.c:btf_func_linkage
Unexecuted instantiation: features.c:btf_func_linkage
Unexecuted instantiation: bpf.c:btf_func_linkage
Unexecuted instantiation: btf.c:btf_func_linkage
Unexecuted instantiation: strset.c:btf_func_linkage
Unexecuted instantiation: btf_iter.c:btf_func_linkage
Unexecuted instantiation: btf_relocate.c:btf_func_linkage
259
260
static inline __u32 btf_type_info(int kind, int vlen, int kflag)
261
390
{
262
390
  return ((__u32)kflag << 31) | (kind << 24) | vlen;
263
390
}
Unexecuted instantiation: libbpf.c:btf_type_info
Unexecuted instantiation: libbpf_utils.c:btf_type_info
Unexecuted instantiation: libbpf_probes.c:btf_type_info
Unexecuted instantiation: gen_loader.c:btf_type_info
Unexecuted instantiation: relo_core.c:btf_type_info
Unexecuted instantiation: usdt.c:btf_type_info
Unexecuted instantiation: zip.c:btf_type_info
Unexecuted instantiation: elf.c:btf_type_info
Unexecuted instantiation: features.c:btf_type_info
Unexecuted instantiation: bpf.c:btf_type_info
btf.c:btf_type_info
Line
Count
Source
261
390
{
262
390
  return ((__u32)kflag << 31) | (kind << 24) | vlen;
263
390
}
Unexecuted instantiation: strset.c:btf_type_info
Unexecuted instantiation: btf_iter.c:btf_type_info
Unexecuted instantiation: btf_relocate.c:btf_type_info
264
265
enum map_def_parts {
266
  MAP_DEF_MAP_TYPE  = 0x001,
267
  MAP_DEF_KEY_TYPE  = 0x002,
268
  MAP_DEF_KEY_SIZE  = 0x004,
269
  MAP_DEF_VALUE_TYPE  = 0x008,
270
  MAP_DEF_VALUE_SIZE  = 0x010,
271
  MAP_DEF_MAX_ENTRIES = 0x020,
272
  MAP_DEF_MAP_FLAGS = 0x040,
273
  MAP_DEF_NUMA_NODE = 0x080,
274
  MAP_DEF_PINNING   = 0x100,
275
  MAP_DEF_INNER_MAP = 0x200,
276
  MAP_DEF_MAP_EXTRA = 0x400,
277
278
  MAP_DEF_ALL   = 0x7ff, /* combination of all above */
279
};
280
281
struct btf_map_def {
282
  enum map_def_parts parts;
283
  __u32 map_type;
284
  __u32 key_type_id;
285
  __u32 key_size;
286
  __u32 value_type_id;
287
  __u32 value_size;
288
  __u32 max_entries;
289
  __u32 map_flags;
290
  __u32 numa_node;
291
  __u32 pinning;
292
  __u64 map_extra;
293
};
294
295
int parse_btf_map_def(const char *map_name, struct btf *btf,
296
          const struct btf_type *def_t, bool strict,
297
          struct btf_map_def *map_def, struct btf_map_def *inner_def);
298
299
void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
300
         size_t cur_cnt, size_t max_cnt, size_t add_cnt);
301
int libbpf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt);
302
303
static inline bool libbpf_is_mem_zeroed(const char *p, ssize_t len)
304
11.7k
{
305
11.7k
  while (len > 0) {
306
0
    if (*p)
307
0
      return false;
308
0
    p++;
309
0
    len--;
310
0
  }
311
11.7k
  return true;
312
11.7k
}
libbpf.c:libbpf_is_mem_zeroed
Line
Count
Source
304
11.7k
{
305
11.7k
  while (len > 0) {
306
0
    if (*p)
307
0
      return false;
308
0
    p++;
309
0
    len--;
310
0
  }
311
11.7k
  return true;
312
11.7k
}
Unexecuted instantiation: libbpf_utils.c:libbpf_is_mem_zeroed
Unexecuted instantiation: libbpf_probes.c:libbpf_is_mem_zeroed
Unexecuted instantiation: gen_loader.c:libbpf_is_mem_zeroed
Unexecuted instantiation: relo_core.c:libbpf_is_mem_zeroed
Unexecuted instantiation: usdt.c:libbpf_is_mem_zeroed
Unexecuted instantiation: zip.c:libbpf_is_mem_zeroed
Unexecuted instantiation: elf.c:libbpf_is_mem_zeroed
Unexecuted instantiation: features.c:libbpf_is_mem_zeroed
Unexecuted instantiation: bpf.c:libbpf_is_mem_zeroed
Unexecuted instantiation: btf.c:libbpf_is_mem_zeroed
Unexecuted instantiation: strset.c:libbpf_is_mem_zeroed
Unexecuted instantiation: btf_iter.c:libbpf_is_mem_zeroed
Unexecuted instantiation: btf_relocate.c:libbpf_is_mem_zeroed
313
314
static inline bool libbpf_validate_opts(const char *opts,
315
          size_t opts_sz, size_t user_sz,
316
          const char *type_name)
317
11.7k
{
318
11.7k
  if (user_sz < sizeof(size_t)) {
319
0
    pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
320
0
    return false;
321
0
  }
322
11.7k
  if (!libbpf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) {
323
0
    pr_warn("%s has non-zero extra bytes\n", type_name);
324
0
    return false;
325
0
  }
326
11.7k
  return true;
327
11.7k
}
libbpf.c:libbpf_validate_opts
Line
Count
Source
317
11.7k
{
318
11.7k
  if (user_sz < sizeof(size_t)) {
319
0
    pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
320
0
    return false;
321
0
  }
322
11.7k
  if (!libbpf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) {
323
0
    pr_warn("%s has non-zero extra bytes\n", type_name);
324
0
    return false;
325
0
  }
326
11.7k
  return true;
327
11.7k
}
Unexecuted instantiation: libbpf_utils.c:libbpf_validate_opts
Unexecuted instantiation: libbpf_probes.c:libbpf_validate_opts
Unexecuted instantiation: gen_loader.c:libbpf_validate_opts
Unexecuted instantiation: relo_core.c:libbpf_validate_opts
Unexecuted instantiation: usdt.c:libbpf_validate_opts
Unexecuted instantiation: zip.c:libbpf_validate_opts
Unexecuted instantiation: elf.c:libbpf_validate_opts
Unexecuted instantiation: features.c:libbpf_validate_opts
Unexecuted instantiation: bpf.c:libbpf_validate_opts
Unexecuted instantiation: btf.c:libbpf_validate_opts
Unexecuted instantiation: strset.c:libbpf_validate_opts
Unexecuted instantiation: btf_iter.c:libbpf_validate_opts
Unexecuted instantiation: btf_relocate.c:libbpf_validate_opts
328
329
#define OPTS_VALID(opts, type)                  \
330
11.7k
  (!(opts) || libbpf_validate_opts((const char *)opts,          \
331
11.7k
           offsetofend(struct type,       \
332
11.7k
                 type##__last_field),     \
333
11.7k
           (opts)->sz, #type))
334
#define OPTS_HAS(opts, field) \
335
110k
  ((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
336
#define OPTS_GET(opts, field, fallback_value) \
337
110k
  (OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
338
#define OPTS_SET(opts, field, value)    \
339
0
  do {         \
340
0
    if (OPTS_HAS(opts, field))  \
341
0
      (opts)->field = value; \
342
0
  } while (0)
343
344
0
#define OPTS_ZEROED(opts, last_nonzero_field)             \
345
0
({                        \
346
0
  ssize_t __off = offsetofend(typeof(*(opts)), last_nonzero_field);     \
347
0
  !(opts) || libbpf_is_mem_zeroed((const void *)opts + __off,        \
348
0
          (opts)->sz - __off);         \
349
0
})
350
351
enum kern_feature_id {
352
  /* v4.14: kernel support for program & map names. */
353
  FEAT_PROG_NAME,
354
  /* v5.2: kernel support for global data sections. */
355
  FEAT_GLOBAL_DATA,
356
  /* BTF support */
357
  FEAT_BTF,
358
  /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
359
  FEAT_BTF_FUNC,
360
  /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
361
  FEAT_BTF_DATASEC,
362
  /* BTF_FUNC_GLOBAL is supported */
363
  FEAT_BTF_GLOBAL_FUNC,
364
  /* BPF_F_MMAPABLE is supported for arrays */
365
  FEAT_ARRAY_MMAP,
366
  /* kernel support for expected_attach_type in BPF_PROG_LOAD */
367
  FEAT_EXP_ATTACH_TYPE,
368
  /* bpf_probe_read_{kernel,user}[_str] helpers */
369
  FEAT_PROBE_READ_KERN,
370
  /* BPF_PROG_BIND_MAP is supported */
371
  FEAT_PROG_BIND_MAP,
372
  /* Kernel support for module BTFs */
373
  FEAT_MODULE_BTF,
374
  /* BTF_KIND_FLOAT support */
375
  FEAT_BTF_FLOAT,
376
  /* BPF perf link support */
377
  FEAT_PERF_LINK,
378
  /* BTF_KIND_DECL_TAG support */
379
  FEAT_BTF_DECL_TAG,
380
  /* BTF_KIND_TYPE_TAG support */
381
  FEAT_BTF_TYPE_TAG,
382
  /* memcg-based accounting for BPF maps and progs */
383
  FEAT_MEMCG_ACCOUNT,
384
  /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
385
  FEAT_BPF_COOKIE,
386
  /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
387
  FEAT_BTF_ENUM64,
388
  /* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */
389
  FEAT_SYSCALL_WRAPPER,
390
  /* BPF multi-uprobe link support */
391
  FEAT_UPROBE_MULTI_LINK,
392
  /* Kernel supports arg:ctx tag (__arg_ctx) for global subprogs natively */
393
  FEAT_ARG_CTX_TAG,
394
  /* Kernel supports '?' at the front of datasec names */
395
  FEAT_BTF_QMARK_DATASEC,
396
  /* Kernel supports LDIMM64 imm offsets past 512 MiB. */
397
  FEAT_LDIMM64_FULL_RANGE_OFF,
398
  /* Kernel supports uprobe syscall */
399
  FEAT_UPROBE_SYSCALL,
400
  /* Kernel supports BTF layout information */
401
  FEAT_BTF_LAYOUT,
402
  /* Kernel supports BPF syscall common attributes */
403
  FEAT_BPF_SYSCALL_COMMON_ATTRS,
404
  /* Kernel supports percpu data */
405
  FEAT_PERCPU_DATA,
406
  __FEAT_CNT,
407
};
408
409
enum kern_feature_result {
410
  FEAT_UNKNOWN = 0,
411
  FEAT_SUPPORTED = 1,
412
  FEAT_MISSING = 2,
413
};
414
415
struct kern_feature_cache {
416
  enum kern_feature_result res[__FEAT_CNT];
417
  int token_fd;
418
};
419
420
bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id);
421
bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id);
422
void bpf_object_set_feat_cache(struct bpf_object *obj, struct kern_feature_cache *cache);
423
424
int probe_kern_syscall_wrapper(int token_fd);
425
int probe_memcg_account(int token_fd);
426
int bump_rlimit_memlock(void);
427
428
int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
429
int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
430
int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
431
       const char *str_sec, size_t str_len,
432
       int token_fd);
433
int libbpf__load_raw_btf_hdr(const struct btf_header *hdr,
434
           const char *raw_types, const char *str_sec,
435
           const char *layout_sec, int token_fd);
436
struct btf *bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *orig_btf);
437
int btf_load_into_kernel(struct btf *btf,
438
       char *log_buf, size_t log_sz, __u32 log_level,
439
       int token_fd);
440
struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd);
441
442
struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
443
void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
444
        const char **prefix, int *kind);
445
446
struct btf_ext_info {
447
  /*
448
   * info points to the individual info section (e.g. func_info and
449
   * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
450
   */
451
  void *info;
452
  __u32 rec_size;
453
  __u32 len;
454
  /* optional (maintained internally by libbpf) mapping between .BTF.ext
455
   * section and corresponding ELF section. This is used to join
456
   * information like CO-RE relocation records with corresponding BPF
457
   * programs defined in ELF sections
458
   */
459
  __u32 *sec_idxs;
460
  int sec_cnt;
461
};
462
463
#define for_each_btf_ext_sec(seg, sec)          \
464
108
  for (sec = (seg)->info;           \
465
303
       (void *)sec < (seg)->info + (seg)->len;     \
466
195
       sec = (void *)sec + sizeof(struct btf_ext_info_sec) + \
467
195
       (seg)->rec_size * sec->num_info)
468
469
#define for_each_btf_ext_rec(seg, sec, i, rec)        \
470
0
  for (i = 0, rec = (void *)&(sec)->data;       \
471
0
       i < (sec)->num_info;         \
472
0
       i++, rec = (void *)rec + (seg)->rec_size)
473
474
/*
475
 * The .BTF.ext ELF section layout defined as
476
 *   struct btf_ext_header
477
 *   func_info subsection
478
 *
479
 * The func_info subsection layout:
480
 *   record size for struct bpf_func_info in the func_info subsection
481
 *   struct btf_ext_info_sec for section #1
482
 *   a list of bpf_func_info records for section #1
483
 *     where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
484
 *     but may not be identical
485
 *   struct btf_ext_info_sec for section #2
486
 *   a list of bpf_func_info records for section #2
487
 *   ......
488
 *
489
 * Note that the bpf_func_info record size in .BTF.ext may not
490
 * be the same as the one defined in include/uapi/linux/bpf.h.
491
 * The loader should ensure that record_size meets minimum
492
 * requirement and pass the record as is to the kernel. The
493
 * kernel will handle the func_info properly based on its contents.
494
 */
495
struct btf_ext_header {
496
  __u16 magic;
497
  __u8  version;
498
  __u8  flags;
499
  __u32 hdr_len;
500
501
  /* All offsets are in bytes relative to the end of this header */
502
  __u32 func_info_off;
503
  __u32 func_info_len;
504
  __u32 line_info_off;
505
  __u32 line_info_len;
506
507
  /* optional part of .BTF.ext header */
508
  __u32 core_relo_off;
509
  __u32 core_relo_len;
510
};
511
512
struct btf_ext {
513
  union {
514
    struct btf_ext_header *hdr;
515
    void *data;
516
  };
517
  void *data_swapped;
518
  bool swapped_endian;
519
  struct btf_ext_info func_info;
520
  struct btf_ext_info line_info;
521
  struct btf_ext_info core_relo_info;
522
  __u32 data_size;
523
};
524
525
struct btf_ext_info_sec {
526
  __u32 sec_name_off;
527
  __u32 num_info;
528
  /* Followed by num_info * record_size number of bytes */
529
  __u8  data[];
530
};
531
532
/* The minimum bpf_func_info checked by the loader */
533
struct bpf_func_info_min {
534
  __u32   insn_off;
535
  __u32   type_id;
536
};
537
538
/* The minimum bpf_line_info checked by the loader */
539
struct bpf_line_info_min {
540
  __u32 insn_off;
541
  __u32 file_name_off;
542
  __u32 line_off;
543
  __u32 line_col;
544
};
545
546
/* Functions to byte-swap info records */
547
548
typedef void (*info_rec_bswap_fn)(void *);
549
550
static inline void bpf_func_info_bswap(struct bpf_func_info *i)
551
52
{
552
52
  i->insn_off = bswap_32(i->insn_off);
553
52
  i->type_id = bswap_32(i->type_id);
554
52
}
Unexecuted instantiation: libbpf.c:bpf_func_info_bswap
Unexecuted instantiation: libbpf_utils.c:bpf_func_info_bswap
Unexecuted instantiation: libbpf_probes.c:bpf_func_info_bswap
Unexecuted instantiation: gen_loader.c:bpf_func_info_bswap
Unexecuted instantiation: relo_core.c:bpf_func_info_bswap
Unexecuted instantiation: usdt.c:bpf_func_info_bswap
Unexecuted instantiation: zip.c:bpf_func_info_bswap
Unexecuted instantiation: elf.c:bpf_func_info_bswap
Unexecuted instantiation: features.c:bpf_func_info_bswap
Unexecuted instantiation: bpf.c:bpf_func_info_bswap
btf.c:bpf_func_info_bswap
Line
Count
Source
551
52
{
552
52
  i->insn_off = bswap_32(i->insn_off);
553
  i->type_id = bswap_32(i->type_id);
554
52
}
Unexecuted instantiation: strset.c:bpf_func_info_bswap
Unexecuted instantiation: btf_iter.c:bpf_func_info_bswap
Unexecuted instantiation: btf_relocate.c:bpf_func_info_bswap
555
556
static inline void bpf_line_info_bswap(struct bpf_line_info *i)
557
0
{
558
0
  i->insn_off = bswap_32(i->insn_off);
559
0
  i->file_name_off = bswap_32(i->file_name_off);
560
0
  i->line_off = bswap_32(i->line_off);
561
0
  i->line_col = bswap_32(i->line_col);
562
0
}
Unexecuted instantiation: libbpf.c:bpf_line_info_bswap
Unexecuted instantiation: libbpf_utils.c:bpf_line_info_bswap
Unexecuted instantiation: libbpf_probes.c:bpf_line_info_bswap
Unexecuted instantiation: gen_loader.c:bpf_line_info_bswap
Unexecuted instantiation: relo_core.c:bpf_line_info_bswap
Unexecuted instantiation: usdt.c:bpf_line_info_bswap
Unexecuted instantiation: zip.c:bpf_line_info_bswap
Unexecuted instantiation: elf.c:bpf_line_info_bswap
Unexecuted instantiation: features.c:bpf_line_info_bswap
Unexecuted instantiation: bpf.c:bpf_line_info_bswap
Unexecuted instantiation: btf.c:bpf_line_info_bswap
Unexecuted instantiation: strset.c:bpf_line_info_bswap
Unexecuted instantiation: btf_iter.c:bpf_line_info_bswap
Unexecuted instantiation: btf_relocate.c:bpf_line_info_bswap
563
564
static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)
565
0
{
566
0
  i->insn_off = bswap_32(i->insn_off);
567
0
  i->type_id = bswap_32(i->type_id);
568
0
  i->access_str_off = bswap_32(i->access_str_off);
569
0
  i->kind = bswap_32(i->kind);
570
0
}
Unexecuted instantiation: libbpf.c:bpf_core_relo_bswap
Unexecuted instantiation: libbpf_utils.c:bpf_core_relo_bswap
Unexecuted instantiation: libbpf_probes.c:bpf_core_relo_bswap
Unexecuted instantiation: gen_loader.c:bpf_core_relo_bswap
Unexecuted instantiation: relo_core.c:bpf_core_relo_bswap
Unexecuted instantiation: usdt.c:bpf_core_relo_bswap
Unexecuted instantiation: zip.c:bpf_core_relo_bswap
Unexecuted instantiation: elf.c:bpf_core_relo_bswap
Unexecuted instantiation: features.c:bpf_core_relo_bswap
Unexecuted instantiation: bpf.c:bpf_core_relo_bswap
Unexecuted instantiation: btf.c:bpf_core_relo_bswap
Unexecuted instantiation: strset.c:bpf_core_relo_bswap
Unexecuted instantiation: btf_iter.c:bpf_core_relo_bswap
Unexecuted instantiation: btf_relocate.c:bpf_core_relo_bswap
571
572
enum btf_field_iter_kind {
573
  BTF_FIELD_ITER_IDS,
574
  BTF_FIELD_ITER_STRS,
575
};
576
577
struct btf_field_desc {
578
  /* once-per-type offsets */
579
  int t_off_cnt, t_offs[2];
580
  /* member struct size, or zero, if no members */
581
  int m_sz;
582
  /* repeated per-member offsets */
583
  int m_off_cnt, m_offs[2];
584
};
585
586
struct btf_field_iter {
587
  struct btf_field_desc desc;
588
  void *p;
589
  int m_idx;
590
  int off_idx;
591
  int vlen;
592
};
593
594
int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t, enum btf_field_iter_kind iter_kind);
595
__u32 *btf_field_iter_next(struct btf_field_iter *it);
596
597
typedef int (*type_id_visit_fn)(__u32 *type_id, void *ctx);
598
typedef int (*str_off_visit_fn)(__u32 *str_off, void *ctx);
599
int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx);
600
int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx);
601
602
/* handle direct returned errors */
603
static inline int libbpf_err(int ret)
604
3.14k
{
605
3.14k
  if (ret < 0)
606
3.14k
    errno = -ret;
607
3.14k
  return ret;
608
3.14k
}
Unexecuted instantiation: libbpf.c:libbpf_err
Unexecuted instantiation: libbpf_utils.c:libbpf_err
Unexecuted instantiation: libbpf_probes.c:libbpf_err
Unexecuted instantiation: gen_loader.c:libbpf_err
Unexecuted instantiation: relo_core.c:libbpf_err
Unexecuted instantiation: usdt.c:libbpf_err
Unexecuted instantiation: zip.c:libbpf_err
Unexecuted instantiation: elf.c:libbpf_err
Unexecuted instantiation: features.c:libbpf_err
Unexecuted instantiation: bpf.c:libbpf_err
btf.c:libbpf_err
Line
Count
Source
604
3.14k
{
605
3.14k
  if (ret < 0)
606
3.14k
    errno = -ret;
607
3.14k
  return ret;
608
3.14k
}
Unexecuted instantiation: strset.c:libbpf_err
Unexecuted instantiation: btf_iter.c:libbpf_err
Unexecuted instantiation: btf_relocate.c:libbpf_err
609
610
/* handle errno-based (e.g., syscall or libc) errors according to libbpf's
611
 * strict mode settings
612
 */
613
static inline int libbpf_err_errno(int ret)
614
0
{
615
  /* errno is already assumed to be set on error */
616
0
  return ret < 0 ? -errno : ret;
617
0
}
Unexecuted instantiation: libbpf.c:libbpf_err_errno
Unexecuted instantiation: libbpf_utils.c:libbpf_err_errno
Unexecuted instantiation: libbpf_probes.c:libbpf_err_errno
Unexecuted instantiation: gen_loader.c:libbpf_err_errno
Unexecuted instantiation: relo_core.c:libbpf_err_errno
Unexecuted instantiation: usdt.c:libbpf_err_errno
Unexecuted instantiation: zip.c:libbpf_err_errno
Unexecuted instantiation: elf.c:libbpf_err_errno
Unexecuted instantiation: features.c:libbpf_err_errno
Unexecuted instantiation: bpf.c:libbpf_err_errno
Unexecuted instantiation: btf.c:libbpf_err_errno
Unexecuted instantiation: strset.c:libbpf_err_errno
Unexecuted instantiation: btf_iter.c:libbpf_err_errno
Unexecuted instantiation: btf_relocate.c:libbpf_err_errno
618
619
/* handle error for pointer-returning APIs, err is assumed to be < 0 always */
620
static inline void *libbpf_err_ptr(int err)
621
398
{
622
  /* set errno on error, this doesn't break anything */
623
398
  errno = -err;
624
398
  return NULL;
625
398
}
Unexecuted instantiation: libbpf.c:libbpf_err_ptr
Unexecuted instantiation: libbpf_utils.c:libbpf_err_ptr
Unexecuted instantiation: libbpf_probes.c:libbpf_err_ptr
Unexecuted instantiation: gen_loader.c:libbpf_err_ptr
Unexecuted instantiation: relo_core.c:libbpf_err_ptr
Unexecuted instantiation: usdt.c:libbpf_err_ptr
Unexecuted instantiation: zip.c:libbpf_err_ptr
Unexecuted instantiation: elf.c:libbpf_err_ptr
Unexecuted instantiation: features.c:libbpf_err_ptr
Unexecuted instantiation: bpf.c:libbpf_err_ptr
btf.c:libbpf_err_ptr
Line
Count
Source
621
398
{
622
  /* set errno on error, this doesn't break anything */
623
398
  errno = -err;
624
  return NULL;
625
398
}
Unexecuted instantiation: strset.c:libbpf_err_ptr
Unexecuted instantiation: btf_iter.c:libbpf_err_ptr
Unexecuted instantiation: btf_relocate.c:libbpf_err_ptr
626
627
/* handle pointer-returning APIs' error handling */
628
static inline void *libbpf_ptr(void *ret)
629
15.8k
{
630
  /* set errno on error, this doesn't break anything */
631
15.8k
  if (IS_ERR(ret))
632
15.8k
    errno = -PTR_ERR(ret);
633
634
15.8k
  return IS_ERR(ret) ? NULL : ret;
635
15.8k
}
libbpf.c:libbpf_ptr
Line
Count
Source
629
11.7k
{
630
  /* set errno on error, this doesn't break anything */
631
11.7k
  if (IS_ERR(ret))
632
11.7k
    errno = -PTR_ERR(ret);
633
634
11.7k
  return IS_ERR(ret) ? NULL : ret;
635
11.7k
}
Unexecuted instantiation: libbpf_utils.c:libbpf_ptr
Unexecuted instantiation: libbpf_probes.c:libbpf_ptr
Unexecuted instantiation: gen_loader.c:libbpf_ptr
Unexecuted instantiation: relo_core.c:libbpf_ptr
Unexecuted instantiation: usdt.c:libbpf_ptr
Unexecuted instantiation: zip.c:libbpf_ptr
Unexecuted instantiation: elf.c:libbpf_ptr
Unexecuted instantiation: features.c:libbpf_ptr
Unexecuted instantiation: bpf.c:libbpf_ptr
btf.c:libbpf_ptr
Line
Count
Source
629
4.08k
{
630
  /* set errno on error, this doesn't break anything */
631
4.08k
  if (IS_ERR(ret))
632
4.08k
    errno = -PTR_ERR(ret);
633
634
4.08k
  return IS_ERR(ret) ? NULL : ret;
635
4.08k
}
Unexecuted instantiation: strset.c:libbpf_ptr
Unexecuted instantiation: btf_iter.c:libbpf_ptr
Unexecuted instantiation: btf_relocate.c:libbpf_ptr
636
637
static inline bool str_is_empty(const char *s)
638
3.85k
{
639
3.85k
  return !s || !s[0];
640
3.85k
}
libbpf.c:str_is_empty
Line
Count
Source
638
3.46k
{
639
3.46k
  return !s || !s[0];
640
3.46k
}
Unexecuted instantiation: libbpf_utils.c:str_is_empty
Unexecuted instantiation: libbpf_probes.c:str_is_empty
Unexecuted instantiation: gen_loader.c:str_is_empty
Unexecuted instantiation: relo_core.c:str_is_empty
Unexecuted instantiation: usdt.c:str_is_empty
Unexecuted instantiation: zip.c:str_is_empty
Unexecuted instantiation: elf.c:str_is_empty
Unexecuted instantiation: features.c:str_is_empty
Unexecuted instantiation: bpf.c:str_is_empty
btf.c:str_is_empty
Line
Count
Source
638
390
{
639
390
  return !s || !s[0];
640
390
}
Unexecuted instantiation: strset.c:str_is_empty
Unexecuted instantiation: btf_iter.c:str_is_empty
Unexecuted instantiation: btf_relocate.c:str_is_empty
641
642
static inline bool is_ldimm64_insn(struct bpf_insn *insn)
643
1.44k
{
644
1.44k
  return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
645
1.44k
}
libbpf.c:is_ldimm64_insn
Line
Count
Source
643
1.44k
{
644
1.44k
  return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
645
1.44k
}
Unexecuted instantiation: libbpf_utils.c:is_ldimm64_insn
Unexecuted instantiation: libbpf_probes.c:is_ldimm64_insn
Unexecuted instantiation: gen_loader.c:is_ldimm64_insn
Unexecuted instantiation: relo_core.c:is_ldimm64_insn
Unexecuted instantiation: usdt.c:is_ldimm64_insn
Unexecuted instantiation: zip.c:is_ldimm64_insn
Unexecuted instantiation: elf.c:is_ldimm64_insn
Unexecuted instantiation: features.c:is_ldimm64_insn
Unexecuted instantiation: bpf.c:is_ldimm64_insn
Unexecuted instantiation: btf.c:is_ldimm64_insn
Unexecuted instantiation: strset.c:is_ldimm64_insn
Unexecuted instantiation: btf_iter.c:is_ldimm64_insn
Unexecuted instantiation: btf_relocate.c:is_ldimm64_insn
646
647
static inline void bpf_insn_bswap(struct bpf_insn *insn)
648
18.1k
{
649
18.1k
  __u8 tmp_reg = insn->dst_reg;
650
651
18.1k
  insn->dst_reg = insn->src_reg;
652
18.1k
  insn->src_reg = tmp_reg;
653
18.1k
  insn->off = bswap_16(insn->off);
654
18.1k
  insn->imm = bswap_32(insn->imm);
655
18.1k
}
libbpf.c:bpf_insn_bswap
Line
Count
Source
648
18.1k
{
649
18.1k
  __u8 tmp_reg = insn->dst_reg;
650
651
18.1k
  insn->dst_reg = insn->src_reg;
652
18.1k
  insn->src_reg = tmp_reg;
653
18.1k
  insn->off = bswap_16(insn->off);
654
  insn->imm = bswap_32(insn->imm);
655
18.1k
}
Unexecuted instantiation: libbpf_utils.c:bpf_insn_bswap
Unexecuted instantiation: libbpf_probes.c:bpf_insn_bswap
Unexecuted instantiation: gen_loader.c:bpf_insn_bswap
Unexecuted instantiation: relo_core.c:bpf_insn_bswap
Unexecuted instantiation: usdt.c:bpf_insn_bswap
Unexecuted instantiation: zip.c:bpf_insn_bswap
Unexecuted instantiation: elf.c:bpf_insn_bswap
Unexecuted instantiation: features.c:bpf_insn_bswap
Unexecuted instantiation: bpf.c:bpf_insn_bswap
Unexecuted instantiation: btf.c:bpf_insn_bswap
Unexecuted instantiation: strset.c:bpf_insn_bswap
Unexecuted instantiation: btf_iter.c:bpf_insn_bswap
Unexecuted instantiation: btf_relocate.c:bpf_insn_bswap
656
657
/* Unconditionally dup FD, ensuring it doesn't use [0, 2] range.
658
 * Original FD is not closed or altered in any other way.
659
 * Preserves original FD value, if it's invalid (negative).
660
 */
661
static inline int dup_good_fd(int fd)
662
0
{
663
0
  if (fd < 0)
664
0
    return fd;
665
0
  return fcntl(fd, F_DUPFD_CLOEXEC, 3);
666
0
}
Unexecuted instantiation: libbpf.c:dup_good_fd
Unexecuted instantiation: libbpf_utils.c:dup_good_fd
Unexecuted instantiation: libbpf_probes.c:dup_good_fd
Unexecuted instantiation: gen_loader.c:dup_good_fd
Unexecuted instantiation: relo_core.c:dup_good_fd
Unexecuted instantiation: usdt.c:dup_good_fd
Unexecuted instantiation: zip.c:dup_good_fd
Unexecuted instantiation: elf.c:dup_good_fd
Unexecuted instantiation: features.c:dup_good_fd
Unexecuted instantiation: bpf.c:dup_good_fd
Unexecuted instantiation: btf.c:dup_good_fd
Unexecuted instantiation: strset.c:dup_good_fd
Unexecuted instantiation: btf_iter.c:dup_good_fd
Unexecuted instantiation: btf_relocate.c:dup_good_fd
667
668
/* if fd is stdin, stdout, or stderr, dup to a fd greater than 2
669
 * Takes ownership of the fd passed in, and closes it if calling
670
 * fcntl(fd, F_DUPFD_CLOEXEC, 3).
671
 */
672
static inline int ensure_good_fd(int fd)
673
6.43k
{
674
6.43k
  int old_fd = fd, saved_errno;
675
676
6.43k
  if (fd < 0)
677
3.21k
    return fd;
678
3.21k
  if (fd < 3) {
679
0
    fd = dup_good_fd(fd);
680
0
    saved_errno = errno;
681
0
    close(old_fd);
682
0
    errno = saved_errno;
683
0
    if (fd < 0) {
684
0
      pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno);
685
0
      errno = saved_errno;
686
0
    }
687
0
  }
688
3.21k
  return fd;
689
6.43k
}
libbpf.c:ensure_good_fd
Line
Count
Source
673
6.43k
{
674
6.43k
  int old_fd = fd, saved_errno;
675
676
6.43k
  if (fd < 0)
677
3.21k
    return fd;
678
3.21k
  if (fd < 3) {
679
0
    fd = dup_good_fd(fd);
680
0
    saved_errno = errno;
681
0
    close(old_fd);
682
0
    errno = saved_errno;
683
0
    if (fd < 0) {
684
0
      pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno);
685
      errno = saved_errno;
686
0
    }
687
0
  }
688
3.21k
  return fd;
689
6.43k
}
Unexecuted instantiation: libbpf_utils.c:ensure_good_fd
Unexecuted instantiation: libbpf_probes.c:ensure_good_fd
Unexecuted instantiation: gen_loader.c:ensure_good_fd
Unexecuted instantiation: relo_core.c:ensure_good_fd
Unexecuted instantiation: usdt.c:ensure_good_fd
Unexecuted instantiation: zip.c:ensure_good_fd
Unexecuted instantiation: elf.c:ensure_good_fd
Unexecuted instantiation: features.c:ensure_good_fd
Unexecuted instantiation: bpf.c:ensure_good_fd
Unexecuted instantiation: btf.c:ensure_good_fd
Unexecuted instantiation: strset.c:ensure_good_fd
Unexecuted instantiation: btf_iter.c:ensure_good_fd
Unexecuted instantiation: btf_relocate.c:ensure_good_fd
690
691
static inline int sys_dup3(int oldfd, int newfd, int flags)
692
0
{
693
0
  return syscall(__NR_dup3, oldfd, newfd, flags);
694
0
}
Unexecuted instantiation: libbpf.c:sys_dup3
Unexecuted instantiation: libbpf_utils.c:sys_dup3
Unexecuted instantiation: libbpf_probes.c:sys_dup3
Unexecuted instantiation: gen_loader.c:sys_dup3
Unexecuted instantiation: relo_core.c:sys_dup3
Unexecuted instantiation: usdt.c:sys_dup3
Unexecuted instantiation: zip.c:sys_dup3
Unexecuted instantiation: elf.c:sys_dup3
Unexecuted instantiation: features.c:sys_dup3
Unexecuted instantiation: bpf.c:sys_dup3
Unexecuted instantiation: btf.c:sys_dup3
Unexecuted instantiation: strset.c:sys_dup3
Unexecuted instantiation: btf_iter.c:sys_dup3
Unexecuted instantiation: btf_relocate.c:sys_dup3
695
696
/* Some versions of Android don't provide memfd_create() in their libc
697
 * implementation, so avoid complications and just go straight to Linux
698
 * syscall.
699
 */
700
static inline int sys_memfd_create(const char *name, unsigned flags)
701
6.43k
{
702
6.43k
  return syscall(__NR_memfd_create, name, flags);
703
6.43k
}
libbpf.c:sys_memfd_create
Line
Count
Source
701
6.43k
{
702
  return syscall(__NR_memfd_create, name, flags);
703
6.43k
}
Unexecuted instantiation: libbpf_utils.c:sys_memfd_create
Unexecuted instantiation: libbpf_probes.c:sys_memfd_create
Unexecuted instantiation: gen_loader.c:sys_memfd_create
Unexecuted instantiation: relo_core.c:sys_memfd_create
Unexecuted instantiation: usdt.c:sys_memfd_create
Unexecuted instantiation: zip.c:sys_memfd_create
Unexecuted instantiation: elf.c:sys_memfd_create
Unexecuted instantiation: features.c:sys_memfd_create
Unexecuted instantiation: bpf.c:sys_memfd_create
Unexecuted instantiation: btf.c:sys_memfd_create
Unexecuted instantiation: strset.c:sys_memfd_create
Unexecuted instantiation: btf_iter.c:sys_memfd_create
Unexecuted instantiation: btf_relocate.c:sys_memfd_create
704
705
/* Point *fixed_fd* to the same file that *tmp_fd* points to.
706
 * Regardless of success, *tmp_fd* is closed.
707
 * Whatever *fixed_fd* pointed to is closed silently.
708
 */
709
static inline int reuse_fd(int fixed_fd, int tmp_fd)
710
0
{
711
0
  int err;
712
713
0
  err = sys_dup3(tmp_fd, fixed_fd, O_CLOEXEC);
714
0
  err = err < 0 ? -errno : 0;
715
0
  close(tmp_fd); /* clean up temporary FD */
716
0
  return err;
717
0
}
Unexecuted instantiation: libbpf.c:reuse_fd
Unexecuted instantiation: libbpf_utils.c:reuse_fd
Unexecuted instantiation: libbpf_probes.c:reuse_fd
Unexecuted instantiation: gen_loader.c:reuse_fd
Unexecuted instantiation: relo_core.c:reuse_fd
Unexecuted instantiation: usdt.c:reuse_fd
Unexecuted instantiation: zip.c:reuse_fd
Unexecuted instantiation: elf.c:reuse_fd
Unexecuted instantiation: features.c:reuse_fd
Unexecuted instantiation: bpf.c:reuse_fd
Unexecuted instantiation: btf.c:reuse_fd
Unexecuted instantiation: strset.c:reuse_fd
Unexecuted instantiation: btf_iter.c:reuse_fd
Unexecuted instantiation: btf_relocate.c:reuse_fd
718
719
/* The following two functions are exposed to bpftool */
720
int bpf_core_add_cands(struct bpf_core_cand *local_cand,
721
           size_t local_essent_len,
722
           const struct btf *targ_btf,
723
           const char *targ_btf_name,
724
           int targ_start_id,
725
           struct bpf_core_cand_list *cands);
726
void bpf_core_free_cands(struct bpf_core_cand_list *cands);
727
728
struct usdt_manager *usdt_manager_new(struct bpf_object *obj);
729
void usdt_manager_free(struct usdt_manager *man);
730
struct bpf_link * usdt_manager_attach_usdt(struct usdt_manager *man,
731
             const struct bpf_program *prog,
732
             pid_t pid, const char *path,
733
             const char *usdt_provider, const char *usdt_name,
734
             __u64 usdt_cookie);
735
736
static inline bool is_pow_of_2(size_t x)
737
0
{
738
0
  return x && (x & (x - 1)) == 0;
739
0
}
Unexecuted instantiation: libbpf.c:is_pow_of_2
Unexecuted instantiation: libbpf_utils.c:is_pow_of_2
Unexecuted instantiation: libbpf_probes.c:is_pow_of_2
Unexecuted instantiation: gen_loader.c:is_pow_of_2
Unexecuted instantiation: relo_core.c:is_pow_of_2
Unexecuted instantiation: usdt.c:is_pow_of_2
Unexecuted instantiation: zip.c:is_pow_of_2
Unexecuted instantiation: elf.c:is_pow_of_2
Unexecuted instantiation: features.c:is_pow_of_2
Unexecuted instantiation: bpf.c:is_pow_of_2
Unexecuted instantiation: btf.c:is_pow_of_2
Unexecuted instantiation: strset.c:is_pow_of_2
Unexecuted instantiation: btf_iter.c:is_pow_of_2
Unexecuted instantiation: btf_relocate.c:is_pow_of_2
740
741
static inline __u32 ror32(__u32 v, int bits)
742
0
{
743
0
  return (v >> bits) | (v << (32 - bits));
744
0
}
Unexecuted instantiation: libbpf.c:ror32
Unexecuted instantiation: libbpf_utils.c:ror32
Unexecuted instantiation: libbpf_probes.c:ror32
Unexecuted instantiation: gen_loader.c:ror32
Unexecuted instantiation: relo_core.c:ror32
Unexecuted instantiation: usdt.c:ror32
Unexecuted instantiation: zip.c:ror32
Unexecuted instantiation: elf.c:ror32
Unexecuted instantiation: features.c:ror32
Unexecuted instantiation: bpf.c:ror32
Unexecuted instantiation: btf.c:ror32
Unexecuted instantiation: strset.c:ror32
Unexecuted instantiation: btf_iter.c:ror32
Unexecuted instantiation: btf_relocate.c:ror32
745
746
0
#define PROG_LOAD_ATTEMPTS 5
747
int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
748
749
bool glob_match(const char *str, const char *pat);
750
751
long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name);
752
long elf_find_func_offset_from_file(const char *binary_path, const char *name);
753
754
struct elf_fd {
755
  Elf *elf;
756
  int fd;
757
};
758
759
int elf_open(const char *binary_path, struct elf_fd *elf_fd);
760
void elf_close(struct elf_fd *elf_fd);
761
762
int elf_resolve_syms_offsets(const char *binary_path, int cnt,
763
           const char **syms, unsigned long **poffsets,
764
           int st_type);
765
int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
766
         unsigned long **poffsets, size_t *pcnt);
767
768
int probe_fd(int fd);
769
770
0
#define SHA256_DIGEST_LENGTH 32
771
772
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]);
773
int probe_sys_bpf_ext(void);
774
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */