libbpf: fix samples/bpf build failure due to undefined UINT32_MAX
Subsystems (4)
PATCH 1/1 libbpf: fix samples/bpf build failure due to undefined UINT32_MAX
Currently, building bpf samples will cause the following error.
./tools/lib/bpf/bpf.h:132:27: error: 'UINT32_MAX' undeclared here (not in a function) ..
#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
^
./samples/bpf/bpf_load.h:31:25: note: in expansion of macro 'BPF_LOG_BUF_SIZE'
extern char bpf_log_buf[BPF_LOG_BUF_SIZE];
^~~~~~~~~~~~~~~~Due to commit 4519efa6f8ea ("libbpf: fix BPF_LOG_BUF_SIZE off-by-one error") hard-coded size of BPF_LOG_BUF_SIZE has been replaced with UINT32_MAX which is defined in <stdint.h> header.
Even with this change, bpf selftests are running fine since these are built with clang and it includes header(-idirafter) from clang/6.0.0/include. (it has <stdint.h>)
clang -I. -I./include/uapi -I../../../include/uapi -idirafter /usr/local/include -idirafter /usr/include \
-idirafter /usr/lib/llvm-6.0/lib/clang/6.0.0/include -idirafter /usr/include/x86_64-linux-gnu \
-Wno-compare-distinct-pointer-types -O2 -target bpf -emit-llvm -c progs/test_sysctl_prog.c -o - | \
llc -march=bpf -mcpu=generic -filetype=obj -o /linux/tools/testing/selftests/bpf/test_sysctl_prog.oBut bpf samples are compiled with GCC, and it only searches and includes headers declared at the target file. As '#include <stdint.h>' hasn't been declared in tools/lib/bpf/bpf.h, it causes build failure of bpf samples.
gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall -Wmissing-prototypes -Wstrict-prototypes \
-O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \
-I./tools/ lib/ -I./tools/include -I./tools/perf -c -o ./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;This commit add declaration of '#include <stdint.h>' to tools/lib/bpf/bpf.h to fix this problem.
--- tools/lib/bpf/bpf.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf.h +1 -0
@@ -26,6 +26,7 @@#include <linux/bpf.h> #include <stdbool.h> #include <stddef.h> +#include <stdint.h> #ifdef __cplusplusextern "C" {
Signature · 1 line
2.17.1
Quoted 44 lines · Daniel T. Lee
Currently, building bpf samples will cause the following error.
./tools/lib/bpf/bpf.h:132:27: error: 'UINT32_MAX' undeclared here (not in a function) .. #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ ^ ./samples/bpf/bpf_load.h:31:25: note: in expansion of macro 'BPF_LOG_BUF_SIZE' extern char bpf_log_buf[BPF_LOG_BUF_SIZE]; ^~~~~~~~~~~~~~~~Due to commit
4519efa6f8ea("libbpf: fixBPF_LOG_BUF_SIZEoff-by-one error") hard-coded size ofBPF_LOG_BUF_SIZEhas been replaced withUINT32_MAXwhich is defined in <stdint.h> header.Even with this change, bpf selftests are running fine since these are built with clang and it includes header(-idirafter) from clang/6.0.0/include. (it has <
stdint.h>)clang -I. -I./include/uapi -I../../../include/uapi -idirafter /usr/local/include -idirafter /usr/include \ -idirafter /usr/lib/llvm-6.0/lib/clang/6.0.0/include -idirafter /usr/include/x86_64-linux-gnu \ -Wno-compare-distinct-pointer-types -O2 -target bpf -emit-llvm -c progs/test_sysctl_prog.c -o - | \ llc -march=bpf -mcpu=generic -filetype=obj -o /linux/tools/testing/selftests/bpf/test_sysctl_prog.oBut bpf samples are compiled with GCC, and it only searches and includes headers declared at the target file. As '#include <
stdint.h>' hasn't been declared intools/lib/bpf/bpf.h, it causes build failure of bpf samples.gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall -Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./tools/include -I./tools/perf -c -o ./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;This commit add declaration of '#include <
stdint.h>' totools/lib/bpf/bpf.hto fix this problem.Signed-off-by Daniel T. Lee <[email protected]>--- tools/lib/bpf/bpf.h | 1 + 1 file changed, 1 insertion(+)tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h line 29
Quoted 4 lines
tools/lib/bpf/bpf.h +1
-0@@ -26,6 +26,7 @@#include <linux/bpf.h> #include <stdbool.h> #include <stddef.h> +#include <stdint.h>
I my case, I have the following in /usr/include/linux/bpf.h:
#include <linux/types.h>
#include <linux/bpf_common.h>
The <linux/bpf.h> is included in tools/lib/bpf/bpf.h.
and at tools/include/linux/types.h, I got
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
So I don't have any issues.
Could you check your system whether any of the above files missing or mismatched during compilation process?
Quoted 6 lines
tools/lib/bpf/bpf.h +0
-1#ifdef __cplusplusextern "C" { --2.17.1
Oh, I've found what's the problem.
About the files you've mentioned, nothing seems to matter on my system. But the problem is the order of the '-I' option matters.
user@host:~/linux/samples/bpf $ make -n
gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall
-Wmissing-prototypes -Wstrict-prototypes \
-O2 -fomit-frame-pointer -std=gnu89 -I./usr/include
-I./tools/lib/ -I./tools/testing/selftests/bpf/ \
-I./tools/ lib/ -I./tools/include -I./tools/perf -c -o
./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;
...
user@host:~/linux $ gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d
-Wall -Wmissing-prototypes -Wstrict-prototypes \
-O2 -fomit-frame-pointer -std=gnu89 -I./usr/include
-I./tools/lib/ -I./tools/testing/selftests/bpf/ \
-I./tools/ lib/ -I./tools/include -I./tools/perf -c -o./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;
./tools/lib/bpf/bpf.h:95:27: error: ‘UINT32_MAX’ undeclared here
(not in a function); ...
#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in
kernels <= 5.1 */
^
./samples/bpf/bpf_load.h:31:25: note: in expansion of macro
‘BPF_LOG_BUF_SIZE’
extern char bpf_log_buf[BPF_LOG_BUF_SIZE];
^~~~~~~~~~~~~~~~As you can see the dry make command in my system, the '-I./usr/include' option precedes than '-I./tools/include'. And in this situation, it gives an error.
But when I switch these two options order, it runs successfully like below.
user@host:~/linux $ gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d
-Wall -Wmissing-prototypes -Wstrict-prototypes \
-O2 -fomit-frame-pointer -std=gnu89 -I./tools/include
-I./tools/lib/ -I./tools/testing/selftests/bpf/ \
-I./tools/ lib/ -I./usr/include -I./tools/perf -c -o./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;
// command runs successfully.
Looks like when the '-I./usr/include' option precedes, it looks up for ./usr/include/linux/types.h. But when '-I./tools/include' comes up earlier, ./tools/include/linux/types.h header is used with compilation.
Since these two <linux/types.h> headers are quite different, it causes an error. I'm not sure but perhaps the version difference of GNU Make is causing the inconsistency of the option order. (v4.1 on my system)
I'm wondering which might be the best ways to handle this problem? (Maybe adding a Make implicit rule for the option order?)
Quoted 72 lines · Y Song
Quoted 44 lines · Daniel T. Lee
Currently, building bpf samples will cause the following error.
./tools/lib/bpf/bpf.h:132:27: error: 'UINT32_MAX' undeclared here (not in a function) .. #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ ^ ./samples/bpf/bpf_load.h:31:25: note: in expansion of macro 'BPF_LOG_BUF_SIZE' extern char bpf_log_buf[BPF_LOG_BUF_SIZE]; ^~~~~~~~~~~~~~~~Due to commit
4519efa6f8ea("libbpf: fixBPF_LOG_BUF_SIZEoff-by-one error") hard-coded size ofBPF_LOG_BUF_SIZEhas been replaced withUINT32_MAXwhich is defined in <stdint.h> header.Even with this change, bpf selftests are running fine since these are built with clang and it includes header(-idirafter) from clang/6.0.0/include. (it has <
stdint.h>)clang -I. -I./include/uapi -I../../../include/uapi -idirafter /usr/local/include -idirafter /usr/include \ -idirafter /usr/lib/llvm-6.0/lib/clang/6.0.0/include -idirafter /usr/include/x86_64-linux-gnu \ -Wno-compare-distinct-pointer-types -O2 -target bpf -emit-llvm -c progs/test_sysctl_prog.c -o - | \ llc -march=bpf -mcpu=generic -filetype=obj -o /linux/tools/testing/selftests/bpf/test_sysctl_prog.oBut bpf samples are compiled with GCC, and it only searches and includes headers declared at the target file. As '#include <
stdint.h>' hasn't been declared intools/lib/bpf/bpf.h, it causes build failure of bpf samples.gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall -Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./tools/include -I./tools/perf -c -o ./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;This commit add declaration of '#include <
stdint.h>' totools/lib/bpf/bpf.hto fix this problem.Signed-off-by Daniel T. Lee <[email protected]>--- tools/lib/bpf/bpf.h | 1 + 1 file changed, 1 insertion(+)tools/lib/bpf/bpf.h +0
-0tools/lib/bpf/bpf.h line 29
Quoted 4 lines
tools/lib/bpf/bpf.h +1
-0@@ -26,6 +26,7 @@#include <linux/bpf.h> #include <stdbool.h> #include <stddef.h> +#include <stdint.h> I my case, I have the following in
/usr/include/linux/bpf.h:
#include <linux/types.h>
#include <linux/bpf_common.h>The <
linux/bpf.h> is included intools/lib/bpf/bpf.h.and at
tools/include/linux/types.h, I got
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>So I don't have any issues.
Could you check your system whether any of the above files missing or mismatched during compilation process?Quoted 6 lines
tools/lib/bpf/bpf.h +0
-1#ifdef __cplusplusextern "C" { --2.17.1
Y Song
Acked-by Yonghong SongQuoted 45 lines · Daniel T. Lee
Oh, I've found what's the problem.
About the files you've mentioned, nothing seems to matter on my system. But the problem is the order of the '-I' option matters.user@host:~/linux/samples/bpf $ make -n
gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall
(quoted diff) +0
-2-Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./tools/include -I./tools/perf -c -o./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c; ... user@host:~/linux $ gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d(quoted diff) +0
-2-Wall -Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./tools/include -I./tools/perf -c -o
./samples/bpf/sockex3_user.o./samples/bpf/sockex3_user.c;
./tools/lib/bpf/bpf.h:95:27: error: ‘UINT32_MAX’ undeclared here
(not in a function); ...#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum inkernels <= 5.1 */
^
./samples/bpf/bpf_load.h:31:25: note: in expansion of macro
‘BPF_LOG_BUF_SIZE’extern char bpf_log_buf[BPF_LOG_BUF_SIZE]; ^~~~~~~~~~~~~~~~As you can see the dry make command in my system, the '-I./usr/include' option precedes than '-I./tools/include'. And in this situation, it gives an error.
But when I switch these two options order, it runs successfully like below.user@host:~/linux $ gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d(quoted diff) +0
-2-Wall -Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./tools/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./usr/include -I./tools/perf -c -o
./samples/bpf/sockex3_user.o./samples/bpf/sockex3_user.c;// command runs successfully.
Quoted 7 lines
Looks like when the '-I./usr/include' option precedes, it looks up for
./usr/include/linux/types.h. But when '-I./tools/include' comes up earlier,./tools/include/linux/types.hheader is used with compilation.Since these two <
linux/types.h> headers are quite different, it causes an error. I'm not sure but perhaps the version difference of GNU Make is causing the inconsistency of the option order. (v4.1 on my system)
I have the same issue with v3.82 make.
We could adjust the include orders in samples/bpf/Makefile
KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include
│······
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/
│······
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
│······
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/
-I$(srctree)/tools/include │······
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/perf
to switch $(objtree)/usr/include and $(srctree)/tools/include, but it looks it has
some side effects on some other programs.
So your solution seems reasonable to me.
Quoted 26 lines · Daniel T. Lee
Currently, building bpf samples will cause the following error.
./tools/lib/bpf/bpf.h:132:27: error: 'UINT32_MAX' undeclared here (not in a function) .. #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ ^ ./samples/bpf/bpf_load.h:31:25: note: in expansion of macro 'BPF_LOG_BUF_SIZE' extern char bpf_log_buf[BPF_LOG_BUF_SIZE]; ^~~~~~~~~~~~~~~~Due to commit
4519efa6f8ea("libbpf: fixBPF_LOG_BUF_SIZEoff-by-one error") hard-coded size ofBPF_LOG_BUF_SIZEhas been replaced withUINT32_MAXwhich is defined in <stdint.h> header.Even with this change, bpf selftests are running fine since these are built with clang and it includes header(-idirafter) from clang/6.0.0/include. (it has <
stdint.h>)clang -I. -I./include/uapi -I../../../include/uapi -idirafter /usr/local/include -idirafter /usr/include \ -idirafter /usr/lib/llvm-6.0/lib/clang/6.0.0/include -idirafter /usr/include/x86_64-linux-gnu \ -Wno-compare-distinct-pointer-types -O2 -target bpf -emit-llvm -c progs/test_sysctl_prog.c -o - | \ llc -march=bpf -mcpu=generic -filetype=obj -o /linux/tools/testing/selftests/bpf/test_sysctl_prog.oBut bpf samples are compiled with GCC, and it only searches and includes headers declared at the target file. As '#include <
stdint.h>' hasn't been declared intools/lib/bpf/bpf.h, it causes build failure of bpf samples.
Quoted 8 lines
gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall -Wmissing-prototypes -Wstrict-prototypes \ -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \ -I./tools/ lib/ -I./tools/include -I./tools/perf -c -o ./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;This commit add declaration of '#include <
stdint.h>' totools/lib/bpf/bpf.hto fix this problem.Signed-off-by Daniel T. Lee <[email protected]>
Applied, thanks.