Skip to content

Commit dda1ec9

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Fix libbpf build on compilers missing __builtin_mul_overflow
GCC compilers older than version 5 don't support __builtin_mul_overflow yet. Given GCC 4.9 is the minimal supported compiler for building kernel and the fact that libbpf is a dependency of resolve_btfids, which is dependency of CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue by falling back to slower detection of integer overflow in such cases. Fixes: 029258d ("libbpf: Remove any use of reallocarray() in libbpf") Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200820061411.1755905-2-andriin@fb.com
1 parent 9b2f6fe commit dda1ec9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tools/lib/bpf/libbpf_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define __LIBBPF_LIBBPF_INTERNAL_H
1111

1212
#include <stdlib.h>
13+
#include <limits.h>
1314

1415
/* make sure libbpf doesn't use kernel-only integer typedefs */
1516
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
@@ -77,6 +78,9 @@ do { \
7778
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
7879
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
7980

81+
#ifndef __has_builtin
82+
#define __has_builtin(x) 0
83+
#endif
8084
/*
8185
* Re-implement glibc's reallocarray() for libbpf internal-only use.
8286
* reallocarray(), unfortunately, is not available in all versions of glibc,
@@ -90,8 +94,14 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
9094
{
9195
size_t total;
9296

97+
#if __has_builtin(__builtin_mul_overflow)
9398
if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
9499
return NULL;
100+
#else
101+
if (size == 0 || nmemb > ULONG_MAX / size)
102+
return NULL;
103+
total = nmemb * size;
104+
#endif
95105
return realloc(ptr, total);
96106
}
97107

0 commit comments

Comments
 (0)