Page MenuHomeFreeBSD

D31043.diff
No OneTemporary

D31043.diff

diff --git a/Makefile.inc1 b/Makefile.inc1
--- a/Makefile.inc1
+++ b/Makefile.inc1
@@ -718,6 +718,7 @@
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=no MK_UBSAN=no \
MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no \
MK_INCLUDES=yes
@@ -739,6 +740,7 @@
SSP_CFLAGS= \
-DNO_LINT \
-DNO_CPU_CFLAGS MK_WERROR=no MK_CTF=no \
+ MK_ASAN=no MK_UBSAN=no \
MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no
@@ -2844,6 +2846,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:
#
diff --git a/Makefile.libcompat b/Makefile.libcompat
--- a/Makefile.libcompat
+++ b/Makefile.libcompat
@@ -104,6 +104,7 @@
OBJROOT='$${OBJTOP}/' \
MAKEOBJDIRPREFIX= \
DIRPRFX=${_dir}/ -DNO_LINT -DNO_CPU_CFLAGS \
+ MK_ASAN=no MK_UBSAN=no \
MK_CTF=no MK_RETPOLINE=no MK_WERROR=no \
${_t}
.endfor
diff --git a/lib/Makefile b/lib/Makefile
--- a/lib/Makefile
+++ b/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"
diff --git a/lib/csu/Makefile.inc b/lib/csu/Makefile.inc
--- a/lib/csu/Makefile.inc
+++ b/lib/csu/Makefile.inc
@@ -3,6 +3,9 @@
SSP_CFLAGS=
NO_WMISSING_VARIABLE_DECLARATIONS=
+# Can't instrument these files since that breaks non-sanitized programs.
+MK_ASAN:= no
+MK_UBSAN:= no
.include <src.opts.mk>
diff --git a/lib/libclang_rt/Makefile.inc b/lib/libclang_rt/Makefile.inc
--- a/lib/libclang_rt/Makefile.inc
+++ b/lib/libclang_rt/Makefile.inc
@@ -12,6 +12,8 @@
NO_PIC=
MK_PROFILE= no
+MK_ASAN:= no
+MK_UBSAN:= no
WARNS?= 0
diff --git a/lib/libgcc_eh/Makefile.inc b/lib/libgcc_eh/Makefile.inc
--- a/lib/libgcc_eh/Makefile.inc
+++ b/lib/libgcc_eh/Makefile.inc
@@ -20,9 +20,17 @@
SRCS+= ${SRCS_EXC}
.for file in ${SRCS_EXC:M*.c}
CFLAGS.${file}+= -fno-exceptions -funwind-tables
+.if ${MK_ASAN} != "no"
+# False-positives during stack unwinding
+CFLAGS.${file}+= -fno-sanitize=address
+.endif
.endfor
.for file in ${SRCS_EXC:M*.cpp}
CXXFLAGS.${file}+= -fno-exceptions -funwind-tables
+.if ${MK_ASAN} != "no"
+# False-positives during stack unwinding
+CXXFLAGS.${file}+= -fno-sanitize=address
+.endif
.endfor
CFLAGS+= -I${UNWINDINCDIR}
diff --git a/lib/libgcc_s/Makefile b/lib/libgcc_s/Makefile
--- a/lib/libgcc_s/Makefile
+++ b/lib/libgcc_s/Makefile
@@ -4,6 +4,9 @@
SHLIB_NAME= libgcc_s.so.1
SHLIBDIR?= /lib
+# Enabling UBSan triggers "undefined reference to vtable for __cxxabiv1::__function_type_info"
+MK_UBSAN:= no
+
.include <bsd.opts.mk>
MK_SSP= no
diff --git a/libexec/rtld-elf/Makefile b/libexec/rtld-elf/Makefile
--- a/libexec/rtld-elf/Makefile
+++ b/libexec/rtld-elf/Makefile
@@ -9,7 +9,10 @@
.include <src.opts.mk>
PACKAGE= clibs
MK_PIE= no # Always position independent using local rules
+# Not compatible with sanitizer instrumentation or SSP.
+MK_ASAN= no
MK_SSP= no
+MK_UBSAN= no
CONFS= libmap.conf
PROG?= ld-elf.so.1
diff --git a/share/mk/bsd.README b/share/mk/bsd.README
--- a/share/mk/bsd.README
+++ b/share/mk/bsd.README
@@ -118,7 +118,7 @@
the regular libraries. A new suffix, ".po", is used to denote a profiled
object, and ".pico" denotes a position-independent relocatable object.
".nossppico" denotes a position-independent relocatable object without
-stack smashing protection.
+stack smashing protection and without sanitizer instrumentation.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
diff --git a/share/mk/bsd.compat.mk b/share/mk/bsd.compat.mk
--- a/share/mk/bsd.compat.mk
+++ b/share/mk/bsd.compat.mk
@@ -162,6 +162,7 @@
LIBDIR_BASE:= /usr/lib${libcompat}
_LIB_OBJTOP= ${LIBCOMPAT_OBJTOP}
LIBDESTDIR:= ${LIBCOMPATTMP}
+SYSROOT:= ${LIBCOMPATTMP}
CFLAGS+= ${LIBCOMPATCFLAGS}
LDFLAGS+= ${CFLAGS} ${LIBCOMPATLDFLAGS}
MACHINE= ${LIBCOMPAT_MACHINE}
diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk
--- a/share/mk/bsd.lib.mk
+++ b/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.*$//:C/^-fsanitize.*$//} ${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}
diff --git a/share/mk/bsd.opts.mk b/share/mk/bsd.opts.mk
--- a/share/mk/bsd.opts.mk
+++ b/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 \
diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk
--- a/share/mk/bsd.prog.mk
+++ b/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
diff --git a/share/mk/bsd.progs.mk b/share/mk/bsd.progs.mk
--- a/share/mk/bsd.progs.mk
+++ b/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}
diff --git a/share/mk/bsd.sanitizer.mk b/share/mk/bsd.sanitizer.mk
new file mode 100644
--- /dev/null
+++ b/share/mk/bsd.sanitizer.mk
@@ -0,0 +1,43 @@
+.include <bsd.opts.mk>
+
+.include "../../lib/libclang_rt/compiler-rt-vars.mk"
+_use_sanitizers= no
+# Add the necessary sanitizer flags if requested
+.if ${MK_ASAN} == "yes" && ${NO_SHARED:Uno: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= yes
+.endif # ${MK_ASAN} == "yes"
+
+.if ${MK_UBSAN} == "yes" && ${NO_SHARED:Uno: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= yes
+.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} != "no"
+.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} != "no"
diff --git a/share/mk/sys.mk b/share/mk/sys.mk
--- a/share/mk/sys.mk
+++ b/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

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 8, 9:31 PM (21 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
14543326
Default Alt Text
D31043.diff (9 KB)

Event Timeline