Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F102059505
D31043.id91774.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
8 KB
Referenced Files
None
Subscribers
None
D31043.id91774.diff
View Options
Index: Makefile.inc1
===================================================================
--- Makefile.inc1
+++ Makefile.inc1
@@ -717,6 +717,8 @@
MK_HTML=no NO_LINT=yes MK_MAN=no MK_MAN_UTILS=yes \
-DNO_PIC MK_PROFILE=no -DNO_SHARED \
-DNO_CPU_CFLAGS MK_WERROR=no MK_CTF=no \
+ MK_ASAN=${BOOTSTRAP_TOOLS_ASAN:Uno} \
+ MK_UBSAN=${BOOTSTRAP_TOOLS_UBSAN:Uno} \
MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no \
MK_INCLUDES=yes
@@ -738,6 +740,8 @@
SSP_CFLAGS= \
-DNO_LINT \
-DNO_CPU_CFLAGS MK_WERROR=no MK_CTF=no \
+ MK_ASAN=${BOOTSTRAP_TOOLS_ASAN:Uno} \
+ MK_UBSAN=${BOOTSTRAP_TOOLS_UBSAN:Uno} \
MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no
@@ -2831,6 +2835,16 @@
.if ${MK_SSP} != "no"
_prereq_libs+= lib/libssp_nonshared
.endif
+.if ${MK_ASAN} != "no"
+_prereq_libs+= lib/libclang_rt/asan
+_prereq_libs+= lib/libclang_rt/asan-preinit
+_prereq_libs+= lib/libclang_rt/asan_cxx
+.endif
+.if ${MK_UBSAN} != "no"
+_prereq_libs+= lib/libclang_rt/ubsan_minimal
+_prereq_libs+= lib/libclang_rt/ubsan_standalone
+_prereq_libs+= lib/libclang_rt/ubsan_standalone_cxx
+.endif
# These dependencies are not automatically generated:
#
Index: lib/Makefile
===================================================================
--- lib/Makefile
+++ lib/Makefile
@@ -180,6 +180,8 @@
${MACHINE_CPUARCH} == "arm" || ${MACHINE_CPUARCH} == "i386" || \
${MACHINE_CPUARCH} == "powerpc")
_libclang_rt= libclang_rt
+.elif ${MK_ASAN} != "no" || ${MK_UBSAN} != "no"
+.error "Requested build with sanitizers but cannot build runtime libraries!"
.endif
.if ${MK_CXX} != "no"
Index: lib/csu/Makefile.inc
===================================================================
--- lib/csu/Makefile.inc
+++ lib/csu/Makefile.inc
@@ -3,6 +3,11 @@
SSP_CFLAGS=
NO_WMISSING_VARIABLE_DECLARATIONS=
+# Can't instrument these files since that breaks non-sanitized programs.
+MK_ASAN:=no
+MK_UBSAN:=no
+# Building these files with SSP makes it impossible to build non-SSP binaries.
+MK_SSP:=no
.include <src.opts.mk>
Index: lib/libclang_rt/Makefile.inc
===================================================================
--- lib/libclang_rt/Makefile.inc
+++ lib/libclang_rt/Makefile.inc
@@ -12,6 +12,8 @@
NO_PIC=
MK_PROFILE= no
+MK_ASAN:= no
+MK_UBSAN:= no
WARNS?= 0
Index: lib/libgcc_s/Makefile
===================================================================
--- lib/libgcc_s/Makefile
+++ lib/libgcc_s/Makefile
@@ -4,6 +4,12 @@
SHLIB_NAME= libgcc_s.so.1
SHLIBDIR?= /lib
+# Enabling UBSan seems to trigger "undefined reference to vtable for __cxxabiv1::__function_type_info"
+MK_UBSAN:=no
+# XXX: asan seems fine?
+#MK_ASAN:=no
+
+
.include <bsd.opts.mk>
MK_SSP= no
Index: libexec/rtld-elf/Makefile
===================================================================
--- libexec/rtld-elf/Makefile
+++ libexec/rtld-elf/Makefile
@@ -11,6 +11,10 @@
MK_PIE= no # Always position independent using local rules
MK_SSP= no
+# Not (yet?) compatible with sanitizer instrumentation.
+MK_ASAN= no
+MK_UBSAN= no
+
CONFS= libmap.conf
PROG?= ld-elf.so.1
.if (${PROG:M*ld-elf32*} != "")
Index: share/mk/bsd.lib.mk
===================================================================
--- share/mk/bsd.lib.mk
+++ share/mk/bsd.lib.mk
@@ -6,6 +6,8 @@
.include <bsd.compiler.mk>
.include <bsd.linker.mk>
+__<bsd.lib.mk>__:
+
.if defined(LIB_CXX) || defined(SHLIB_CXX)
_LD= ${CXX}
.else
@@ -106,6 +108,8 @@
.endif
.endif
+.include "bsd.sanitizer.mk"
+
.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
empty(DEBUG_FLAGS:M-gdwarf*)
CFLAGS+= ${DEBUG_FILES_CFLAGS}
@@ -147,7 +151,7 @@
${CTFCONVERT_CMD}
.c.nossppico:
- ${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS:C/^-fstack-protector.*$//} ${CFLAGS:C/^-fstack-protector.*$//} -c ${.IMPSRC} -o ${.TARGET}
+ ${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS:C/^-fstack-protector.*$//:C/^-fsanitize.*$//} ${CFLAGS:C/^-fstack-protector.*$//:C/^-fsanitize.*$//} -c ${.IMPSRC} -o ${.TARGET}
${CTFCONVERT_CMD}
.c.pieo:
@@ -161,7 +165,7 @@
${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
.cc.nossppico .C.nossppico .cpp.nossppico .cxx.nossppico:
- ${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS:C/^-fstack-protector.*$//} ${CXXFLAGS:C/^-fstack-protector.*$//} -c ${.IMPSRC} -o ${.TARGET}
+ ${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS:C/^-fstack-protector.*$//} ${CXXFLAGS:C/^-fstack-protector.*$//:C/^-fsanitize.*$//} -c ${.IMPSRC} -o ${.TARGET}
.cc.pieo .C.pieo .cpp.pieo .cxx.pieo:
${CXX} ${PIEFLAG} ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
Index: share/mk/bsd.opts.mk
===================================================================
--- share/mk/bsd.opts.mk
+++ share/mk/bsd.opts.mk
@@ -69,6 +69,7 @@
WERROR
__DEFAULT_NO_OPTIONS = \
+ ASAN \
BIND_NOW \
CCACHE_BUILD \
CTF \
@@ -77,7 +78,8 @@
INSTALL_AS_USER \
MANSPLITPKG \
RETPOLINE \
- STALE_STAGED
+ STALE_STAGED \
+ UBSAN
__DEFAULT_DEPENDENT_OPTIONS = \
MAKE_CHECK_USE_SANDBOX/TESTS \
Index: share/mk/bsd.prog.mk
===================================================================
--- share/mk/bsd.prog.mk
+++ share/mk/bsd.prog.mk
@@ -81,6 +81,8 @@
.endif
.endif
+.include "bsd.sanitizer.mk"
+
.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == ""
CFLAGS += -mno-relax
.endif
Index: share/mk/bsd.progs.mk
===================================================================
--- share/mk/bsd.progs.mk
+++ share/mk/bsd.progs.mk
@@ -23,7 +23,7 @@
.if defined(PROG)
# just one of many
PROG_OVERRIDE_VARS += BINDIR BINGRP BINOWN BINMODE CSTD CXXSTD DPSRCS MAN \
- NO_SHARED MK_WERROR PROGNAME SRCS STRIP WARNS
+ NO_SHARED MK_WERROR PROGNAME SRCS STRIP WARNS MK_ASAN MK_UBSAN
PROG_VARS += CFLAGS CXXFLAGS DEBUG_FLAGS DPADD INTERNALPROG LDADD LIBADD \
LINKS LDFLAGS MLINKS ${PROG_OVERRIDE_VARS}
.for v in ${PROG_VARS:O:u}
Index: share/mk/bsd.sanitizer.mk
===================================================================
--- /dev/null
+++ share/mk/bsd.sanitizer.mk
@@ -0,0 +1,43 @@
+.include <bsd.opts.mk>
+
+.include "../../lib/libclang_rt/compiler-rt-vars.mk"
+_use_sanitizers=0
+# Add the necessary sanitizer flags if requested
+.if ${MK_ASAN} == "yes" && (!defined(NO_SHARED) || ${NO_SHARED:tl} == "no")
+SANITIZER_CFLAGS+=-fsanitize=address -fPIC
+# TODO: remove this once all basic errors have been fixed:
+# https://github.com/google/sanitizers/wiki/AddressSanitizer#faq
+SANITIZER_CFLAGS+=-fsanitize-recover=address
+SANITIZER_LDFLAGS+=-fsanitize=address
+_use_sanitizers=1
+.endif # ${MK_ASAN} == "yes"
+
+.if ${MK_UBSAN} == "yes" && (!defined(NO_SHARED) || ${NO_SHARED:tl} == "no")
+# Unlike the other sanitizers, UBSan could also work for static libraries.
+# However, this currently results in linker errors (even with the
+# -fsanitize-minimal-runtime flag), so only enable it for dynamically linked
+# code for now.
+SANITIZER_CFLAGS+=-fsanitize=undefined
+SANITIZER_CFLAGS+=-fsanitize-recover=undefined
+SANITIZER_LDFLAGS+=-fsanitize=undefined
+_use_sanitizers=1
+.endif # ${MK_UBSAN} == "yes"
+
+.if !defined(BOOTSTRAPPING) && ${_use_sanitizers} != 0 && \
+ ${COMPILER_TYPE} != "clang"
+.error "Sanitizer instrumentation currently only supported with clang"
+.endif
+
+# For libraries we only instrument the shared and PIE libraries by setting
+# SHARED_CFLAGS instead of CFLAGS. We do this since static executables are not
+# compatible with the santizers (interceptors do not work).
+.if ${_use_sanitizers} != 0
+.if target(__<bsd.lib.mk>__)
+SHARED_CFLAGS+= ${SANITIZER_CFLAGS}
+SOLINKOPTS+= ${SANITIZER_LDFLAGS}
+LDFLAGS:=${LDFLAGS:N-Wl,-no-undefined:N-Wl,--no-undefined}
+.else
+CFLAGS+= ${SANITIZER_CFLAGS}
+LDFLAGS+= ${SANITIZER_LDFLAGS}
+.endif
+.endif # ${_use_sanitizers} != 0
Index: share/mk/sys.mk
===================================================================
--- share/mk/sys.mk
+++ share/mk/sys.mk
@@ -242,7 +242,7 @@
# compiler driver flags (e.g. -mabi=*) that conflict with flags to LD.
LD ?= ld
LDFLAGS ?=
-_LDFLAGS = ${LDFLAGS:S/-Wl,//g:N-mabi=*:N-fuse-ld=*:N--ld-path=*}
+_LDFLAGS = ${LDFLAGS:S/-Wl,//g:N-mabi=*:N-fuse-ld=*:N--ld-path=*:N-fsanitize=*:N-fno-sanitize=*}
MAKE ?= make
Index: stand/Makefile.inc
===================================================================
--- stand/Makefile.inc
+++ stand/Makefile.inc
@@ -1,5 +1,9 @@
# $FreeBSD$
+# Sanitizers won't work for stand
+MK_ASAN:=no
+MK_UBSAN:=no
+
SUBDIR_PARALLEL= yes
.include "defs.mk"
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 8, 2:56 AM (5 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
14526219
Default Alt Text
D31043.id91774.diff (8 KB)
Attached To
Mode
D31043: Add build system support for ASAN+UBSAN instrumentation
Attached
Detach File
Event Timeline
Log In to Comment