xref: /freebsd/share/mk/bsd.sanitizer.mk (revision d835a2f8be8e18c5410d172a56d94e7c5d63b75d)
17bc797e3SAlex Richardson.include <bsd.opts.mk>
27bc797e3SAlex Richardson
37bc797e3SAlex Richardson_use_sanitizers=	no
47bc797e3SAlex Richardson# Add the necessary sanitizer flags if requested
57bc797e3SAlex Richardson.if ${MK_ASAN} == "yes" && ${NO_SHARED:Uno:tl} == "no"
67bc797e3SAlex RichardsonSANITIZER_CFLAGS+=	-fsanitize=address -fPIC
77bc797e3SAlex Richardson# TODO: remove this once all basic errors have been fixed:
87bc797e3SAlex Richardson# https://github.com/google/sanitizers/wiki/AddressSanitizer#faq
97bc797e3SAlex RichardsonSANITIZER_CFLAGS+=	-fsanitize-recover=address
107bc797e3SAlex RichardsonSANITIZER_LDFLAGS+=	-fsanitize=address
117bc797e3SAlex Richardson_use_sanitizers=	yes
127bc797e3SAlex Richardson.endif # ${MK_ASAN} == "yes"
137bc797e3SAlex Richardson
147bc797e3SAlex Richardson.if ${MK_UBSAN} == "yes" && ${NO_SHARED:Uno:tl} == "no"
157bc797e3SAlex Richardson# Unlike the other sanitizers, UBSan could also work for static libraries.
167bc797e3SAlex Richardson# However, this currently results in linker errors (even with the
177bc797e3SAlex Richardson# -fsanitize-minimal-runtime flag), so only enable it for dynamically linked
187bc797e3SAlex Richardson# code for now.
197bc797e3SAlex RichardsonSANITIZER_CFLAGS+=	-fsanitize=undefined
207bc797e3SAlex RichardsonSANITIZER_CFLAGS+=	-fsanitize-recover=undefined
217bc797e3SAlex RichardsonSANITIZER_LDFLAGS+=	-fsanitize=undefined
227bc797e3SAlex Richardson_use_sanitizers=	yes
237bc797e3SAlex Richardson.endif # ${MK_UBSAN} == "yes"
247bc797e3SAlex Richardson
2517d6371eSAlex Richardson.if !defined(BOOTSTRAPPING) && ${_use_sanitizers} != "no" && \
262a31cd21SEd Maste    ${COMPILER_TYPE} != "clang" && make(all)
27bca92be6SJohn Baldwin.error Sanitizer instrumentation currently only supported with clang
287bc797e3SAlex Richardson.endif
297bc797e3SAlex Richardson
307bc797e3SAlex Richardson# For libraries we only instrument the shared and PIE libraries by setting
317bc797e3SAlex Richardson# SHARED_CFLAGS instead of CFLAGS. We do this since static executables are not
32*d835a2f8SGordon Bergling# compatible with the sanitizers (interceptors do not work).
337bc797e3SAlex Richardson.if ${_use_sanitizers} != "no"
3417d6371eSAlex Richardson.include "../../lib/libclang_rt/compiler-rt-vars.mk"
357bc797e3SAlex Richardson.if target(__<bsd.lib.mk>__)
367bc797e3SAlex RichardsonSHARED_CFLAGS+=	${SANITIZER_CFLAGS}
377bc797e3SAlex RichardsonSOLINKOPTS+=	${SANITIZER_LDFLAGS}
387bc797e3SAlex RichardsonLDFLAGS:=	${LDFLAGS:N-Wl,-no-undefined:N-Wl,--no-undefined}
397bc797e3SAlex Richardson.else
407bc797e3SAlex RichardsonCFLAGS+=	${SANITIZER_CFLAGS}
417bc797e3SAlex RichardsonLDFLAGS+=	${SANITIZER_LDFLAGS}
427bc797e3SAlex Richardson.endif
437bc797e3SAlex Richardson.endif # ${_use_sanitizers} != "no"
44