summaryrefslogtreecommitdiff
path: root/share
diff options
context:
space:
mode:
authorBrooks Davis <brooks@FreeBSD.org>2020-11-10 19:15:13 +0000
committerBrooks Davis <brooks@FreeBSD.org>2020-11-10 19:15:13 +0000
commite268fd0a029e620a0cc85c9437ae5dc35a00c699 (patch)
tree46ba7bf0f2d8c42e2827359200519c2ac2474af2 /share
parent9ebe945bd791f25fe5f4774205cbf07eabca348b (diff)
downloadsrc-test-e268fd0a029e620a0cc85c9437ae5dc35a00c699.tar.gz
src-test-e268fd0a029e620a0cc85c9437ae5dc35a00c699.zip
Support initializing stack variables on function entry
There are two options: - WITH_INIT_ALL_ZERO: Zero all variables on the stack. - WITH_INIT_ALL_PATTERN: Initialize variables with well-defined patterns. The exact pattern are a compiler implementation detail and vary by type. They are somewhat documented in the LLVM commit message: https://reviews.llvm.org/rL349442 I've used WITH_INIT_ALL_* to match Microsoft's InitAll feature rather than naming them after the LLVM specific compiler flags. In a range of consumer products, options like these are used in both debug and production builds with debugs builds using patterns (intended to provoke crashes on use of uninitialized values) and production using zeros (deemed more likely to lead to harmless misbehavior or NULL-pointer dereferences). Reviewed by: emaste Obtained from: CheriBSD Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D27131
Notes
Notes: svn path=/head/; revision=367577
Diffstat (limited to 'share')
-rw-r--r--share/mk/bsd.compiler.mk3
-rw-r--r--share/mk/bsd.lib.mk19
-rw-r--r--share/mk/bsd.opts.mk6
-rw-r--r--share/mk/bsd.prog.mk19
4 files changed, 46 insertions, 1 deletions
diff --git a/share/mk/bsd.compiler.mk b/share/mk/bsd.compiler.mk
index 0345ab9395ede..8253669fe2790 100644
--- a/share/mk/bsd.compiler.mk
+++ b/share/mk/bsd.compiler.mk
@@ -24,6 +24,7 @@
# - c++11: supports full (or nearly full) C++11 programming environment.
# - retpoline: supports the retpoline speculative execution vulnerability
# mitigation.
+# - init-all: supports stack variable initialization.
#
# These variables with an X_ prefix will also be provided if XCC is set.
#
@@ -214,7 +215,7 @@ ${X_}COMPILER_FEATURES= c++11 c++14
${X_}COMPILER_FEATURES+= c++17
.endif
.if ${${X_}COMPILER_TYPE} == "clang"
-${X_}COMPILER_FEATURES+= retpoline
+${X_}COMPILER_FEATURES+= retpoline init-all
.endif
.else
diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk
index dca5d80da3a5f..ce29fcdcaa652 100644
--- a/share/mk/bsd.lib.mk
+++ b/share/mk/bsd.lib.mk
@@ -85,6 +85,25 @@ LDFLAGS+= -Wl,-zretpolineplt
.endif
.endif
+# Initialize stack variables on function entry
+.if ${MK_INIT_ALL_ZERO} == "yes"
+.if ${COMPILER_FEATURES:Minit-all}
+CFLAGS+= -ftrivial-auto-var-init=zero \
+ -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
+CXXFLAGS+= -ftrivial-auto-var-init=zero \
+ -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
+.else
+.warning InitAll (zeros) requested but not support by compiler
+.endif
+.elif ${MK_INIT_ALL_PATTERN} == "yes"
+.if ${COMPILER_FEATURES:Minit-all}
+CFLAGS+= -ftrivial-auto-var-init=pattern
+CXXFLAGS+= -ftrivial-auto-var-init=pattern
+.else
+.warning InitAll (pattern) requested but not support by compiler
+.endif
+.endif
+
.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
empty(DEBUG_FLAGS:M-gdwarf*)
CFLAGS+= ${DEBUG_FILES_CFLAGS}
diff --git a/share/mk/bsd.opts.mk b/share/mk/bsd.opts.mk
index c9121c359af94..98e949731e601 100644
--- a/share/mk/bsd.opts.mk
+++ b/share/mk/bsd.opts.mk
@@ -71,6 +71,8 @@ __DEFAULT_NO_OPTIONS = \
BIND_NOW \
CCACHE_BUILD \
CTF \
+ INIT_ALL_PATTERN \
+ INIT_ALL_ZERO \
INSTALL_AS_USER \
PIE \
RETPOLINE \
@@ -85,6 +87,10 @@ __DEFAULT_DEPENDENT_OPTIONS = \
.include <bsd.mkopt.mk>
+.if ${MK_INIT_ALL_PATTERN} == "yes" && ${MK_INIT_ALL_ZERO} == "yes"
+.error WITH_INIT_ALL_PATTERN and WITH_INIT_ALL_ZERO are mutually exclusive.
+.endif
+
#
# Supported NO_* options (if defined, MK_* will be forced to "no",
# regardless of user's setting).
diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk
index fbd75318a3581..401915b3f525d 100644
--- a/share/mk/bsd.prog.mk
+++ b/share/mk/bsd.prog.mk
@@ -60,6 +60,25 @@ LDFLAGS+= -Wl,-zretpolineplt
.endif
.endif
+# Initialize stack variables on function entry
+.if ${MK_INIT_ALL_ZERO} == "yes"
+.if ${COMPILER_FEATURES:Minit-all}
+CFLAGS+= -ftrivial-auto-var-init=zero \
+ -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
+CXXFLAGS+= -ftrivial-auto-var-init=zero \
+ -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
+.else
+.warning InitAll (zeros) requested but not support by compiler
+.endif
+.elif ${MK_INIT_ALL_PATTERN} == "yes"
+.if ${COMPILER_FEATURES:Minit-all}
+CFLAGS+= -ftrivial-auto-var-init=pattern
+CXXFLAGS+= -ftrivial-auto-var-init=pattern
+.else
+.warning InitAll (pattern) requested but not support by compiler
+.endif
+.endif
+
.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == ""
CFLAGS += -mno-relax
.endif