Skip to content

Commit 1d1a3bc

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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: 590a008 ("bpf: libbpf: Add STRUCT_OPS support") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 7a2d070 commit 1d1a3bc

12 files changed

+37
-1
lines changed

tools/lib/bpf/bpf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "libbpf.h"
3333
#include "libbpf_internal.h"
3434

35+
/* make sure libbpf doesn't use kernel-only integer typedefs */
36+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
37+
3538
/*
3639
* When building perf, unistd.h is overridden. __NR_bpf is
3740
* required to be defined explicitly.

tools/lib/bpf/bpf_prog_linfo.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "libbpf.h"
99
#include "libbpf_internal.h"
1010

11+
/* make sure libbpf doesn't use kernel-only integer typedefs */
12+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
13+
1114
struct bpf_prog_linfo {
1215
void *raw_linfo;
1316
void *raw_jited_linfo;

tools/lib/bpf/btf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "libbpf_internal.h"
1818
#include "hashmap.h"
1919

20+
/* make sure libbpf doesn't use kernel-only integer typedefs */
21+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
22+
2023
#define BTF_MAX_NR_TYPES 0x7fffffff
2124
#define BTF_MAX_STR_OFFSET 0x7fffffff
2225

tools/lib/bpf/btf_dump.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "libbpf.h"
1919
#include "libbpf_internal.h"
2020

21+
/* make sure libbpf doesn't use kernel-only integer typedefs */
22+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
23+
2124
static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
2225
static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
2326

tools/lib/bpf/hashmap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <linux/err.h>
1313
#include "hashmap.h"
1414

15+
/* make sure libbpf doesn't use kernel-only integer typedefs */
16+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
17+
1518
/* start with 4 buckets */
1619
#define HASHMAP_MIN_CAP_BITS 2
1720

tools/lib/bpf/libbpf.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#include "libbpf_internal.h"
5656
#include "hashmap.h"
5757

58+
/* make sure libbpf doesn't use kernel-only integer typedefs */
59+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
60+
5861
#ifndef EM_BPF
5962
#define EM_BPF 247
6063
#endif
@@ -6475,7 +6478,7 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
64756478
Elf_Data *symbols;
64766479
unsigned int moff;
64776480
const char *name;
6478-
u32 member_idx;
6481+
__u32 member_idx;
64796482
GElf_Sym sym;
64806483
GElf_Rel rel;
64816484
int i, nrels;

tools/lib/bpf/libbpf_errno.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
#include "libbpf.h"
1515

16+
/* make sure libbpf doesn't use kernel-only integer typedefs */
17+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
18+
1619
#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
1720
#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
1821
#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)

tools/lib/bpf/libbpf_probes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "libbpf.h"
1818
#include "libbpf_internal.h"
1919

20+
/* make sure libbpf doesn't use kernel-only integer typedefs */
21+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
22+
2023
static bool grep(const char *buffer, const char *pattern)
2124
{
2225
return !!strstr(buffer, pattern);

tools/lib/bpf/netlink.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "libbpf_internal.h"
1616
#include "nlattr.h"
1717

18+
/* make sure libbpf doesn't use kernel-only integer typedefs */
19+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
20+
1821
#ifndef SOL_NETLINK
1922
#define SOL_NETLINK 270
2023
#endif

tools/lib/bpf/nlattr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <string.h>
1414
#include <stdio.h>
1515

16+
/* make sure libbpf doesn't use kernel-only integer typedefs */
17+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
18+
1619
static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
1720
[LIBBPF_NLA_U8] = sizeof(uint8_t),
1821
[LIBBPF_NLA_U16] = sizeof(uint16_t),

tools/lib/bpf/str_error.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <stdio.h>
55
#include "str_error.h"
66

7+
/* make sure libbpf doesn't use kernel-only integer typedefs */
8+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
9+
710
/*
811
* Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl
912
* libc, while checking strerror_r() return to avoid having to check this in

tools/lib/bpf/xsk.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "libbpf_internal.h"
3333
#include "xsk.h"
3434

35+
/* make sure libbpf doesn't use kernel-only integer typedefs */
36+
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
37+
3538
#ifndef SOL_XDP
3639
#define SOL_XDP 283
3740
#endif

0 commit comments

Comments
 (0)