Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Poison kernel-only integer types

It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.

This patch also fixes a new u32 occurence in libbpf.c, added recently.

Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200110181916.271446-1-andriin@fb.com

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
1d1a3bcf 7a2d070f

+37 -1
+3
tools/lib/bpf/bpf.c
··· 32 32 #include "libbpf.h" 33 33 #include "libbpf_internal.h" 34 34 35 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 36 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 37 + 35 38 /* 36 39 * When building perf, unistd.h is overridden. __NR_bpf is 37 40 * required to be defined explicitly.
+3
tools/lib/bpf/bpf_prog_linfo.c
··· 8 8 #include "libbpf.h" 9 9 #include "libbpf_internal.h" 10 10 11 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 12 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 13 + 11 14 struct bpf_prog_linfo { 12 15 void *raw_linfo; 13 16 void *raw_jited_linfo;
+3
tools/lib/bpf/btf.c
··· 17 17 #include "libbpf_internal.h" 18 18 #include "hashmap.h" 19 19 20 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 21 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 22 + 20 23 #define BTF_MAX_NR_TYPES 0x7fffffff 21 24 #define BTF_MAX_STR_OFFSET 0x7fffffff 22 25
+3
tools/lib/bpf/btf_dump.c
··· 18 18 #include "libbpf.h" 19 19 #include "libbpf_internal.h" 20 20 21 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 22 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 23 + 21 24 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t"; 22 25 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1; 23 26
+3
tools/lib/bpf/hashmap.c
··· 12 12 #include <linux/err.h> 13 13 #include "hashmap.h" 14 14 15 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 16 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 17 + 15 18 /* start with 4 buckets */ 16 19 #define HASHMAP_MIN_CAP_BITS 2 17 20
+4 -1
tools/lib/bpf/libbpf.c
··· 55 55 #include "libbpf_internal.h" 56 56 #include "hashmap.h" 57 57 58 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 59 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 60 + 58 61 #ifndef EM_BPF 59 62 #define EM_BPF 247 60 63 #endif ··· 6478 6475 Elf_Data *symbols; 6479 6476 unsigned int moff; 6480 6477 const char *name; 6481 - u32 member_idx; 6478 + __u32 member_idx; 6482 6479 GElf_Sym sym; 6483 6480 GElf_Rel rel; 6484 6481 int i, nrels;
+3
tools/lib/bpf/libbpf_errno.c
··· 13 13 14 14 #include "libbpf.h" 15 15 16 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 17 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 18 + 16 19 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 17 20 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 18 21 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
+3
tools/lib/bpf/libbpf_probes.c
··· 17 17 #include "libbpf.h" 18 18 #include "libbpf_internal.h" 19 19 20 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 21 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 22 + 20 23 static bool grep(const char *buffer, const char *pattern) 21 24 { 22 25 return !!strstr(buffer, pattern);
+3
tools/lib/bpf/netlink.c
··· 15 15 #include "libbpf_internal.h" 16 16 #include "nlattr.h" 17 17 18 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 19 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 20 + 18 21 #ifndef SOL_NETLINK 19 22 #define SOL_NETLINK 270 20 23 #endif
+3
tools/lib/bpf/nlattr.c
··· 13 13 #include <string.h> 14 14 #include <stdio.h> 15 15 16 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 17 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 18 + 16 19 static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = { 17 20 [LIBBPF_NLA_U8] = sizeof(uint8_t), 18 21 [LIBBPF_NLA_U16] = sizeof(uint16_t),
+3
tools/lib/bpf/str_error.c
··· 4 4 #include <stdio.h> 5 5 #include "str_error.h" 6 6 7 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 8 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 9 + 7 10 /* 8 11 * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl 9 12 * libc, while checking strerror_r() return to avoid having to check this in
+3
tools/lib/bpf/xsk.c
··· 32 32 #include "libbpf_internal.h" 33 33 #include "xsk.h" 34 34 35 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 36 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 37 + 35 38 #ifndef SOL_XDP 36 39 #define SOL_XDP 283 37 40 #endif