Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / build / autoconf / sanitize.m4
blobbecdf0295d9db8e8b6823311435036ed76eb99e8
1 dnl This Source Code Form is subject to the terms of the Mozilla Public
2 dnl License, v. 2.0. If a copy of the MPL was not distributed with this
3 dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 AC_DEFUN([MOZ_CONFIG_SANITIZE], [
7 dnl ========================================================
8 dnl = Use Address Sanitizer
9 dnl ========================================================
10 if test -n "$MOZ_ASAN"; then
11     if test -n "$CLANG_CL"; then
12         # Look for the ASan runtime binary
13         if test "$TARGET_CPU" = "x86_64"; then
14           MOZ_CLANG_RT_ASAN_LIB=clang_rt.asan_dynamic-x86_64.dll
15         else
16           MOZ_CLANG_RT_ASAN_LIB=clang_rt.asan_dynamic-i386.dll
17         fi
18         # We use MOZ_PATH_PROG in order to get a Windows style path.
19         MOZ_PATH_PROG(MOZ_CLANG_RT_ASAN_LIB_PATH, $MOZ_CLANG_RT_ASAN_LIB)
20         if test -z "$MOZ_CLANG_RT_ASAN_LIB_PATH"; then
21             AC_MSG_ERROR([Couldn't find $MOZ_CLANG_RT_ASAN_LIB.  It should be available in the same location as clang-cl.])
22         fi
23         AC_SUBST(MOZ_CLANG_RT_ASAN_LIB_PATH)
24         # Suppressing errors in recompiled code.
25         if test "$OS_ARCH" = "WINNT"; then
26             CFLAGS="-fsanitize-blacklist=$_topsrcdir/build/sanitizers/asan_blacklist_win.txt $CFLAGS"
27             CXXFLAGS="-fsanitize-blacklist=$_topsrcdir/build/sanitizers/asan_blacklist_win.txt $CXXFLAGS"
28         fi
29     fi
30     ASAN_FLAGS="-fsanitize=address"
31     if test "$OS_TARGET" = Linux; then
32         # -fno-sanitize-address-globals-dead-stripping is used to work around
33         # https://github.com/rust-lang/rust/issues/113404
34         # It forces clang not to use __asan_register_elf_globals/__asan_globals_registered,
35         # avoiding the conflict with rust.
36         ASAN_FLAGS="$ASAN_FLAGS -fno-sanitize-address-globals-dead-stripping"
37     fi
38     CFLAGS="$ASAN_FLAGS $CFLAGS"
39     CXXFLAGS="$ASAN_FLAGS $CXXFLAGS"
40     if test -z "$CLANG_CL"; then
41         LDFLAGS="-fsanitize=address -rdynamic $LDFLAGS"
42     fi
43     AC_DEFINE(MOZ_ASAN)
44     MOZ_PATH_PROG(LLVM_SYMBOLIZER, llvm-symbolizer)
46 AC_SUBST(MOZ_ASAN)
48 dnl ========================================================
49 dnl = Use Memory Sanitizer
50 dnl ========================================================
51 if test -n "$MOZ_MSAN"; then
52     CFLAGS="-fsanitize=memory -fsanitize-memory-track-origins $CFLAGS"
53     CXXFLAGS="-fsanitize=memory -fsanitize-memory-track-origins $CXXFLAGS"
54     if test -z "$CLANG_CL"; then
55         LDFLAGS="-fsanitize=memory -fsanitize-memory-track-origins -rdynamic $LDFLAGS"
56     fi
57     AC_DEFINE(MOZ_MSAN)
58     MOZ_PATH_PROG(LLVM_SYMBOLIZER, llvm-symbolizer)
60 AC_SUBST(MOZ_MSAN)
62 dnl ========================================================
63 dnl = Use Thread Sanitizer
64 dnl ========================================================
65 if test -n "$MOZ_TSAN"; then
66     CFLAGS="-fsanitize=thread $CFLAGS"
67     CXXFLAGS="-fsanitize=thread $CXXFLAGS"
68     if test -z "$CLANG_CL"; then
69         LDFLAGS="-fsanitize=thread -rdynamic $LDFLAGS"
70     fi
71     AC_DEFINE(MOZ_TSAN)
72     MOZ_PATH_PROG(LLVM_SYMBOLIZER, llvm-symbolizer)
74 AC_SUBST(MOZ_TSAN)
76 dnl ========================================================
77 dnl = Use UndefinedBehavior Sanitizer (with custom checks)
78 dnl ========================================================
79 if test -n "$MOZ_UBSAN_CHECKS"; then
80     MOZ_UBSAN=1
81     UBSAN_TXT="$_objdir/ubsan_blacklist.txt"
82     cat $_topsrcdir/build/sanitizers/ubsan_*_blacklist.txt > $UBSAN_TXT
83     UBSAN_FLAGS="-fsanitize=$MOZ_UBSAN_CHECKS -fno-sanitize-recover=$MOZ_UBSAN_CHECKS -fsanitize-blacklist=$UBSAN_TXT"
84     CFLAGS="$UBSAN_FLAGS $CFLAGS"
85     CXXFLAGS="$UBSAN_FLAGS $CXXFLAGS"
86     if test -z "$CLANG_CL"; then
87         LDFLAGS="-fsanitize=undefined -rdynamic $LDFLAGS"
88     fi
89     AC_DEFINE(MOZ_UBSAN)
90     MOZ_PATH_PROG(LLVM_SYMBOLIZER, llvm-symbolizer)
92 AC_SUBST(MOZ_UBSAN)
94 dnl ========================================================
95 dnl = Use UndefinedBehavior Sanitizer to find integer overflows
96 dnl ========================================================
97 if test -n "$MOZ_SIGNED_OVERFLOW_SANITIZE$MOZ_UNSIGNED_OVERFLOW_SANITIZE"; then
98     MOZ_UBSAN=1
99     SANITIZER_BLACKLISTS=""
100     if test -n "$MOZ_SIGNED_OVERFLOW_SANITIZE"; then
101         SANITIZER_BLACKLISTS="-fsanitize-blacklist=$_topsrcdir/build/sanitizers/ubsan_signed_overflow_blacklist.txt $SANITIZER_BLACKLISTS"
102         CFLAGS="-fsanitize=signed-integer-overflow $CFLAGS"
103         CXXFLAGS="-fsanitize=signed-integer-overflow $CXXFLAGS"
104         if test -z "$CLANG_CL"; then
105             LDFLAGS="-fsanitize=signed-integer-overflow -rdynamic $LDFLAGS"
106         fi
107         AC_DEFINE(MOZ_SIGNED_OVERFLOW_SANITIZE)
108     fi
109     if test -n "$MOZ_UNSIGNED_OVERFLOW_SANITIZE"; then
110         SANITIZER_BLACKLISTS="-fsanitize-blacklist=$_topsrcdir/build/sanitizers/ubsan_unsigned_overflow_blacklist.txt $SANITIZER_BLACKLISTS"
111         CFLAGS="-fsanitize=unsigned-integer-overflow $CFLAGS"
112         CXXFLAGS="-fsanitize=unsigned-integer-overflow $CXXFLAGS"
113         if test -z "$CLANG_CL"; then
114             LDFLAGS="-fsanitize=unsigned-integer-overflow -rdynamic $LDFLAGS"
115         fi
116         AC_DEFINE(MOZ_UNSIGNED_OVERFLOW_SANITIZE)
117     fi
118     CFLAGS="$SANITIZER_BLACKLISTS $CFLAGS"
119     CXXFLAGS="$SANITIZER_BLACKLISTS $CXXFLAGS"
120     AC_DEFINE(MOZ_UBSAN)
121     MOZ_PATH_PROG(LLVM_SYMBOLIZER, llvm-symbolizer)
123 AC_SUBST(MOZ_SIGNED_OVERFLOW_SANITIZE)
124 AC_SUBST(MOZ_UNSIGNED_OVERFLOW_SANITIZE)
125 AC_SUBST(MOZ_UBSAN)
127 dnl =======================================================
128 dnl = Required for stand-alone (sanitizer-less) libFuzzer.
129 dnl =======================================================
130 if test -n "$LIBFUZZER"; then
131    LDFLAGS="$LIBFUZZER_FLAGS -rdynamic $LDFLAGS"
134 # The LLVM symbolizer is used by all sanitizers
135 AC_SUBST(LLVM_SYMBOLIZER)
137 dnl ========================================================
138 dnl = Test for whether the compiler is compatible with the
139 dnl = given sanitize options.
140 dnl ========================================================
141 AC_TRY_LINK(,,,AC_MSG_ERROR([compiler is incompatible with sanitize options]))