xref: /freebsd/share/mk/bsd.sanitizer.mk (revision d835a2f8)
1.include <bsd.opts.mk>
2
3_use_sanitizers=	no
4# Add the necessary sanitizer flags if requested
5.if ${MK_ASAN} == "yes" && ${NO_SHARED:Uno:tl} == "no"
6SANITIZER_CFLAGS+=	-fsanitize=address -fPIC
7# TODO: remove this once all basic errors have been fixed:
8# https://github.com/google/sanitizers/wiki/AddressSanitizer#faq
9SANITIZER_CFLAGS+=	-fsanitize-recover=address
10SANITIZER_LDFLAGS+=	-fsanitize=address
11_use_sanitizers=	yes
12.endif # ${MK_ASAN} == "yes"
13
14.if ${MK_UBSAN} == "yes" && ${NO_SHARED:Uno:tl} == "no"
15# Unlike the other sanitizers, UBSan could also work for static libraries.
16# However, this currently results in linker errors (even with the
17# -fsanitize-minimal-runtime flag), so only enable it for dynamically linked
18# code for now.
19SANITIZER_CFLAGS+=	-fsanitize=undefined
20SANITIZER_CFLAGS+=	-fsanitize-recover=undefined
21SANITIZER_LDFLAGS+=	-fsanitize=undefined
22_use_sanitizers=	yes
23.endif # ${MK_UBSAN} == "yes"
24
25.if !defined(BOOTSTRAPPING) && ${_use_sanitizers} != "no" && \
26    ${COMPILER_TYPE} != "clang" && make(all)
27.error Sanitizer instrumentation currently only supported with clang
28.endif
29
30# For libraries we only instrument the shared and PIE libraries by setting
31# SHARED_CFLAGS instead of CFLAGS. We do this since static executables are not
32# compatible with the sanitizers (interceptors do not work).
33.if ${_use_sanitizers} != "no"
34.include "../../lib/libclang_rt/compiler-rt-vars.mk"
35.if target(__<bsd.lib.mk>__)
36SHARED_CFLAGS+=	${SANITIZER_CFLAGS}
37SOLINKOPTS+=	${SANITIZER_LDFLAGS}
38LDFLAGS:=	${LDFLAGS:N-Wl,-no-undefined:N-Wl,--no-undefined}
39.else
40CFLAGS+=	${SANITIZER_CFLAGS}
41LDFLAGS+=	${SANITIZER_LDFLAGS}
42.endif
43.endif # ${_use_sanitizers} != "no"
44