Commit 029258d7 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Remove any use of reallocarray() in libbpf

Re-implement glibc's reallocarray() for libbpf internal-only use.
reallocarray(), unfortunately, is not available in all versions of glibc, so
requires extra feature detection and using reallocarray() stub from
<tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates build
of libbpf unnecessarily and is just a maintenance burden. Instead, it's
trivial to implement libbpf-specific internal version and use it throughout
libbpf.

Which is what this patch does, along with converting some realloc() uses that
should really have been reallocarray() in the first place.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200819013607.3607269-2-andriin@fb.com
parent 00b2e953
...@@ -56,7 +56,7 @@ ifndef VERBOSE ...@@ -56,7 +56,7 @@ ifndef VERBOSE
endif endif
FEATURE_USER = .libbpf FEATURE_USER = .libbpf
FEATURE_TESTS = libelf libelf-mmap zlib bpf reallocarray FEATURE_TESTS = libelf libelf-mmap zlib bpf
FEATURE_DISPLAY = libelf zlib bpf FEATURE_DISPLAY = libelf zlib bpf
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
...@@ -102,10 +102,6 @@ ifeq ($(feature-libelf-mmap), 1) ...@@ -102,10 +102,6 @@ ifeq ($(feature-libelf-mmap), 1)
override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
endif endif
ifeq ($(feature-reallocarray), 0)
override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif
# Append required CFLAGS # Append required CFLAGS
override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
override CFLAGS += -Werror -Wall override CFLAGS += -Werror -Wall
......
...@@ -61,7 +61,7 @@ static int btf_add_type(struct btf *btf, struct btf_type *t) ...@@ -61,7 +61,7 @@ static int btf_add_type(struct btf *btf, struct btf_type *t)
expand_by = max(btf->types_size >> 2, 16U); expand_by = max(btf->types_size >> 2, 16U);
new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by); new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
new_types = realloc(btf->types, sizeof(*new_types) * new_size); new_types = libbpf_reallocarray(btf->types, new_size, sizeof(*new_types));
if (!new_types) if (!new_types)
return -ENOMEM; return -ENOMEM;
...@@ -1574,7 +1574,7 @@ static int btf_dedup_hypot_map_add(struct btf_dedup *d, ...@@ -1574,7 +1574,7 @@ static int btf_dedup_hypot_map_add(struct btf_dedup *d,
__u32 *new_list; __u32 *new_list;
d->hypot_cap += max((size_t)16, d->hypot_cap / 2); d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap); new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
if (!new_list) if (!new_list)
return -ENOMEM; return -ENOMEM;
d->hypot_list = new_list; d->hypot_list = new_list;
...@@ -1870,8 +1870,7 @@ static int btf_dedup_strings(struct btf_dedup *d) ...@@ -1870,8 +1870,7 @@ static int btf_dedup_strings(struct btf_dedup *d)
struct btf_str_ptr *new_ptrs; struct btf_str_ptr *new_ptrs;
strs.cap += max(strs.cnt / 2, 16U); strs.cap += max(strs.cnt / 2, 16U);
new_ptrs = realloc(strs.ptrs, new_ptrs = libbpf_reallocarray(strs.ptrs, strs.cap, sizeof(strs.ptrs[0]));
sizeof(strs.ptrs[0]) * strs.cap);
if (!new_ptrs) { if (!new_ptrs) {
err = -ENOMEM; err = -ENOMEM;
goto done; goto done;
...@@ -2956,8 +2955,8 @@ static int btf_dedup_compact_types(struct btf_dedup *d) ...@@ -2956,8 +2955,8 @@ static int btf_dedup_compact_types(struct btf_dedup *d)
d->btf->nr_types = next_type_id - 1; d->btf->nr_types = next_type_id - 1;
d->btf->types_size = d->btf->nr_types; d->btf->types_size = d->btf->nr_types;
d->btf->hdr->type_len = p - types_start; d->btf->hdr->type_len = p - types_start;
new_types = realloc(d->btf->types, new_types = libbpf_reallocarray(d->btf->types, (1 + d->btf->nr_types),
(1 + d->btf->nr_types) * sizeof(struct btf_type *)); sizeof(struct btf_type *));
if (!new_types) if (!new_types)
return -ENOMEM; return -ENOMEM;
d->btf->types = new_types; d->btf->types = new_types;
......
...@@ -323,8 +323,7 @@ static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id) ...@@ -323,8 +323,7 @@ static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
if (d->emit_queue_cnt >= d->emit_queue_cap) { if (d->emit_queue_cnt >= d->emit_queue_cap) {
new_cap = max(16, d->emit_queue_cap * 3 / 2); new_cap = max(16, d->emit_queue_cap * 3 / 2);
new_queue = realloc(d->emit_queue, new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
new_cap * sizeof(new_queue[0]));
if (!new_queue) if (!new_queue)
return -ENOMEM; return -ENOMEM;
d->emit_queue = new_queue; d->emit_queue = new_queue;
...@@ -1003,8 +1002,7 @@ static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id) ...@@ -1003,8 +1002,7 @@ static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
if (d->decl_stack_cnt >= d->decl_stack_cap) { if (d->decl_stack_cnt >= d->decl_stack_cap) {
new_cap = max(16, d->decl_stack_cap * 3 / 2); new_cap = max(16, d->decl_stack_cap * 3 / 2);
new_stack = realloc(d->decl_stack, new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
new_cap * sizeof(new_stack[0]));
if (!new_stack) if (!new_stack)
return -ENOMEM; return -ENOMEM;
d->decl_stack = new_stack; d->decl_stack = new_stack;
......
...@@ -44,7 +44,6 @@ ...@@ -44,7 +44,6 @@
#include <sys/vfs.h> #include <sys/vfs.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <tools/libc_compat.h>
#include <libelf.h> #include <libelf.h>
#include <gelf.h> #include <gelf.h>
#include <zlib.h> #include <zlib.h>
...@@ -567,7 +566,7 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size, ...@@ -567,7 +566,7 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
progs = obj->programs; progs = obj->programs;
nr_progs = obj->nr_programs; nr_progs = obj->nr_programs;
progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0])); progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
if (!progs) { if (!progs) {
/* /*
* In this case the original obj->programs * In this case the original obj->programs
...@@ -1292,7 +1291,7 @@ static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) ...@@ -1292,7 +1291,7 @@ static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
return &obj->maps[obj->nr_maps++]; return &obj->maps[obj->nr_maps++];
new_cap = max((size_t)4, obj->maps_cap * 3 / 2); new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps)); new_maps = libbpf_reallocarray(obj->maps, new_cap, sizeof(*obj->maps));
if (!new_maps) { if (!new_maps) {
pr_warn("alloc maps for object failed\n"); pr_warn("alloc maps for object failed\n");
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -2721,7 +2720,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) ...@@ -2721,7 +2720,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
continue; continue;
} }
sects = reallocarray(sects, nr_sects + 1, sects = libbpf_reallocarray(sects, nr_sects + 1,
sizeof(*obj->efile.reloc_sects)); sizeof(*obj->efile.reloc_sects));
if (!sects) { if (!sects) {
pr_warn("reloc_sects realloc failed\n"); pr_warn("reloc_sects realloc failed\n");
...@@ -2925,7 +2924,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj) ...@@ -2925,7 +2924,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
continue; continue;
ext = obj->externs; ext = obj->externs;
ext = reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
if (!ext) if (!ext)
return -ENOMEM; return -ENOMEM;
obj->externs = ext; obj->externs = ext;
...@@ -4362,7 +4361,7 @@ static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf, ...@@ -4362,7 +4361,7 @@ static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s\n", pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s\n",
local_type_id, btf_kind_str(local_t), local_type_id, btf_kind_str(local_t),
local_name, i, targ_kind, targ_name); local_name, i, targ_kind, targ_name);
new_ids = reallocarray(cand_ids->data, new_ids = libbpf_reallocarray(cand_ids->data,
cand_ids->len + 1, cand_ids->len + 1,
sizeof(*cand_ids->data)); sizeof(*cand_ids->data));
if (!new_ids) { if (!new_ids) {
...@@ -5231,7 +5230,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, ...@@ -5231,7 +5230,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
return -LIBBPF_ERRNO__RELOC; return -LIBBPF_ERRNO__RELOC;
} }
new_cnt = prog->insns_cnt + text->insns_cnt; new_cnt = prog->insns_cnt + text->insns_cnt;
new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn)); new_insn = libbpf_reallocarray(prog->insns, new_cnt, sizeof(*insn));
if (!new_insn) { if (!new_insn) {
pr_warn("oom in prog realloc\n"); pr_warn("oom in prog realloc\n");
return -ENOMEM; return -ENOMEM;
...@@ -5473,7 +5472,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj, ...@@ -5473,7 +5472,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
moff /= bpf_ptr_sz; moff /= bpf_ptr_sz;
if (moff >= map->init_slots_sz) { if (moff >= map->init_slots_sz) {
new_sz = moff + 1; new_sz = moff + 1;
tmp = realloc(map->init_slots, new_sz * host_ptr_sz); tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
if (!tmp) if (!tmp)
return -ENOMEM; return -ENOMEM;
map->init_slots = tmp; map->init_slots = tmp;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#ifndef __LIBBPF_LIBBPF_INTERNAL_H #ifndef __LIBBPF_LIBBPF_INTERNAL_H
#define __LIBBPF_LIBBPF_INTERNAL_H #define __LIBBPF_LIBBPF_INTERNAL_H
#include <stdlib.h>
#include "libbpf.h" #include "libbpf.h"
#define BTF_INFO_ENC(kind, kind_flag, vlen) \ #define BTF_INFO_ENC(kind, kind_flag, vlen) \
...@@ -23,6 +24,12 @@ ...@@ -23,6 +24,12 @@
#define BTF_PARAM_ENC(name, type) (name), (type) #define BTF_PARAM_ENC(name, type) (name), (type)
#define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size) #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#ifndef min #ifndef min
# define min(x, y) ((x) < (y) ? (x) : (y)) # define min(x, y) ((x) < (y) ? (x) : (y))
#endif #endif
...@@ -63,6 +70,24 @@ do { \ ...@@ -63,6 +70,24 @@ do { \
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__) #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__) #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
/*
* Re-implement glibc's reallocarray() for libbpf internal-only use.
* reallocarray(), unfortunately, is not available in all versions of glibc,
* so requires extra feature detection and using reallocarray() stub from
* <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
* build of libbpf unnecessarily and is just a maintenance burden. Instead,
* it's trivial to implement libbpf-specific internal version and use it
* throughout libbpf.
*/
static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
{
size_t total;
if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
return NULL;
return realloc(ptr, total);
}
static inline bool libbpf_validate_opts(const char *opts, static inline bool libbpf_validate_opts(const char *opts,
size_t opts_sz, size_t user_sz, size_t opts_sz, size_t user_sz,
const char *type_name) const char *type_name)
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <asm/barrier.h> #include <asm/barrier.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <tools/libc_compat.h>
#include "libbpf.h" #include "libbpf.h"
#include "libbpf_internal.h" #include "libbpf_internal.h"
...@@ -82,12 +81,12 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, ...@@ -82,12 +81,12 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
return -EINVAL; return -EINVAL;
} }
tmp = reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings)); tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings));
if (!tmp) if (!tmp)
return -ENOMEM; return -ENOMEM;
rb->rings = tmp; rb->rings = tmp;
tmp = reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events)); tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events));
if (!tmp) if (!tmp)
return -ENOMEM; return -ENOMEM;
rb->events = tmp; rb->events = tmp;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment