Skip to content

Commit a19df71

Browse files
mauriciovasquezbernalanakryiko
authored andcommitted
bpftool: Remove usage of reallocarray()
This commit fixes a compilation error on systems with glibc < 2.26 [0]: ``` In file included from main.h:14:0, from gen.c:24: linux/tools/include/tools/libc_compat.h:11:21: error: attempt to use poisoned "reallocarray" static inline void *reallocarray(void *ptr, size_t nmemb, size_t size) ``` This happens because gen.c pulls <bpf/libbpf_internal.h>, and then <tools/libc_compat.h> (through main.h). When COMPAT_NEED_REALLOCARRAY is set, libc_compat.h defines reallocarray() which libbpf_internal.h poisons with a GCC pragma. This commit reuses libbpf_reallocarray() implemented in commit 029258d ("libbpf: Remove any use of reallocarray() in libbpf"). v1 -> v2: - reuse libbpf_reallocarray() instead of reimplementing it Fixes: a9caaba ("bpftool: Implement "gen min_core_btf" logic") Reported-by: Quentin Monnet <[email protected]> Signed-off-by: Mauricio Vásquez <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] [0]: https://lore.kernel.org/bpf/[email protected]/
1 parent b4f7278 commit a19df71

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

tools/bpf/bpftool/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ INSTALL ?= install
9393
RM ?= rm -f
9494

9595
FEATURE_USER = .bpftool
96-
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
96+
FEATURE_TESTS = libbfd disassembler-four-args zlib libcap \
9797
clang-bpf-co-re
9898
FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
9999
clang-bpf-co-re
@@ -118,10 +118,6 @@ ifeq ($(feature-disassembler-four-args), 1)
118118
CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
119119
endif
120120

121-
ifeq ($(feature-reallocarray), 0)
122-
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
123-
endif
124-
125121
LIBS = $(LIBBPF) -lelf -lz
126122
LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
127123
ifeq ($(feature-libcap), 1)

tools/bpf/bpftool/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#undef GCC_VERSION
99
#include <stdbool.h>
1010
#include <stdio.h>
11+
#include <stdlib.h>
1112
#include <linux/bpf.h>
1213
#include <linux/compiler.h>
1314
#include <linux/kernel.h>
14-
#include <tools/libc_compat.h>
1515

1616
#include <bpf/hashmap.h>
1717
#include <bpf/libbpf.h>

tools/bpf/bpftool/prog.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <bpf/btf.h>
2727
#include <bpf/hashmap.h>
2828
#include <bpf/libbpf.h>
29+
#include <bpf/libbpf_internal.h>
2930
#include <bpf/skel_internal.h>
3031

3132
#include "cfg.h"
@@ -1558,9 +1559,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
15581559
if (fd < 0)
15591560
goto err_free_reuse_maps;
15601561

1561-
new_map_replace = reallocarray(map_replace,
1562-
old_map_fds + 1,
1563-
sizeof(*map_replace));
1562+
new_map_replace = libbpf_reallocarray(map_replace,
1563+
old_map_fds + 1,
1564+
sizeof(*map_replace));
15641565
if (!new_map_replace) {
15651566
p_err("mem alloc failed");
15661567
goto err_free_reuse_maps;

tools/bpf/bpftool/xlated_dumper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include <sys/types.h>
1010
#include <bpf/libbpf.h>
11+
#include <bpf/libbpf_internal.h>
1112

1213
#include "disasm.h"
1314
#include "json_writer.h"
@@ -32,8 +33,8 @@ void kernel_syms_load(struct dump_data *dd)
3233
return;
3334

3435
while (fgets(buff, sizeof(buff), fp)) {
35-
tmp = reallocarray(dd->sym_mapping, dd->sym_count + 1,
36-
sizeof(*dd->sym_mapping));
36+
tmp = libbpf_reallocarray(dd->sym_mapping, dd->sym_count + 1,
37+
sizeof(*dd->sym_mapping));
3738
if (!tmp) {
3839
out:
3940
free(dd->sym_mapping);

0 commit comments

Comments
 (0)