Skip to content

Commit

Permalink
bpf: enable -nostdinc and other stricter compilation options
Browse files Browse the repository at this point in the history
We want to have our code as close to usual kernel conventions as
possible to ease review, so add a few more switches for clang
where we emit useful warnings in our code if it's not the case,
that is, -std=gnu89 and -Wdeclaration-after-statement.

We also use quite a number of shadow declarations where they are
really useless; I'm not sure whether clang ends up allocating new
stack space for them; in any case lets add -Wshadow given the
current locations are fixed now.

Also, given our headers are all self-contained, add -nostdinc to
fully /guarantee/ that clang does not search the standard include
paths anymore.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
borkmann committed May 4, 2020
1 parent 78fa7d9 commit 81c017d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
9 changes: 6 additions & 3 deletions bpf/Makefile.bpf
Expand Up @@ -3,11 +3,14 @@

FLAGS := -I$(ROOT_DIR)/bpf/include -I$(ROOT_DIR)/bpf -D__NR_CPUS__=$(shell nproc) -O2 -g

CLANG_FLAGS := ${FLAGS} -target bpf -emit-llvm
CLANG_FLAGS := ${FLAGS} -target bpf -std=gnu89 -nostdinc -emit-llvm
# eBPF verifier enforces unaligned access checks where necessary, so don't
# let clang complain too early.
CLANG_FLAGS += -Wall -Wextra -Werror
CLANG_FLAGS += -Wno-address-of-packed-member -Wno-unknown-warning-option -Wno-gnu-variable-sized-type-not-at-end
CLANG_FLAGS += -Wall -Wextra -Werror -Wshadow
CLANG_FLAGS += -Wno-address-of-packed-member
CLANG_FLAGS += -Wno-unknown-warning-option
CLANG_FLAGS += -Wno-gnu-variable-sized-type-not-at-end
CLANG_FLAGS += -Wdeclaration-after-statement
LLC_FLAGS := -march=bpf -mcpu=probe -mattr=dwarfris

# BUILD_PERMUTATIONS_DEP is a dummy file dependency that ensures all targets
Expand Down
5 changes: 3 additions & 2 deletions bpf/init.sh
Expand Up @@ -268,11 +268,12 @@ function bpf_compile()
TYPE=$3
EXTRA_OPTS=$4

clang -O2 -target bpf -emit-llvm \
-Wall -Wextra -Werror \
clang -O2 -target bpf -std=gnu89 -nostdinc -emit-llvm \
-Wall -Wextra -Werror -Wshadow \
-Wno-address-of-packed-member \
-Wno-unknown-warning-option \
-Wno-gnu-variable-sized-type-not-at-end \
-Wdeclaration-after-statement \
-I. -I$DIR -I$LIB -I$LIB/include \
-D__NR_CPUS__=$(nproc) \
-DENABLE_ARP_RESPONDER=1 \
Expand Down
9 changes: 5 additions & 4 deletions pkg/datapath/loader/compile.go
Expand Up @@ -82,12 +82,13 @@ type directoryInfo struct {
}

var (
standardCFlags = []string{"-O2", "-target", "bpf",
fmt.Sprintf("-D__NR_CPUS__=%d", runtime.NumCPU()),
"-Wall", "-Wextra", "-Werror",
standardCFlags = []string{"-O2", "-target", "bpf", "-std=gnu89",
"-nostdinc", fmt.Sprintf("-D__NR_CPUS__=%d", runtime.NumCPU()),
"-Wall", "-Wextra", "-Werror", "-Wshadow",
"-Wno-address-of-packed-member",
"-Wno-unknown-warning-option",
"-Wno-gnu-variable-sized-type-not-at-end"}
"-Wno-gnu-variable-sized-type-not-at-end",
"-Wdeclaration-after-statement"}
standardLDFlags = []string{"-march=bpf"}

// testIncludes allows the unit tests to inject additional include
Expand Down
8 changes: 6 additions & 2 deletions test/bpf/Makefile
Expand Up @@ -4,9 +4,13 @@
include ../../Makefile.defs

FLAGS := -I../../bpf/ -I../../bpf/include -I. -D__NR_CPUS__=$(shell nproc) -O2
FLAGS_CLANG := -Wno-gnu-variable-sized-type-not-at-end
FLAGS_CLANG := -Wall -Wextra -Werror -Wshadow -Wno-unused-parameter
FLAGS_CLANG += -Wno-address-of-packed-member
FLAGS_CLANG += -Wno-unknown-warning-option
FLAGS_CLANG += -Wno-gnu-variable-sized-type-not-at-end
FLAGS_CLANG += -Wdeclaration-after-statement

BPF_CC_FLAGS := ${FLAGS} -target bpf -emit-llvm
BPF_CC_FLAGS := ${FLAGS} -target bpf -std=gnu89 -nostdinc -emit-llvm
BPF_LLC_FLAGS := -march=bpf -mcpu=probe -filetype=obj

LIB := $(shell find ../../bpf/ -name '*.h')
Expand Down

0 comments on commit 81c017d

Please sign in to comment.