@@ -200,6 +200,37 @@ endif()
200
200
include (AddThreadsIfNeeded)
201
201
add_threads_if_needed()
202
202
203
+ if (SANITIZERS)
204
+ # First check if the compiler accepts flags. If an incompatible pair like
205
+ # -fsanitize=address,thread is used here, this check will fail. This will also
206
+ # fail if a bad argument is passed, e.g. -fsanitize=undfeined
207
+ try_append_cxx_flags("-fsanitize=${SANITIZERS} " TARGET core
208
+ RESULT_VAR cxx_supports_sanitizers
209
+ )
210
+ if (NOT cxx_supports_sanitizers)
211
+ message (FATAL_ERROR "Compiler did not accept requested flags." )
212
+ endif ()
213
+
214
+ # Some compilers (e.g. GCC) require additional libraries like libasan,
215
+ # libtsan, libubsan, etc. Make sure linking still works with the sanitize
216
+ # flag. This is a separate check so we can give a better error message when
217
+ # the sanitize flags are supported by the compiler but the actual sanitizer
218
+ # libs are missing.
219
+ try_append_linker_flag("-fsanitize=${SANITIZERS} " TARGET core
220
+ SOURCE "
221
+ #include <cstdint>
222
+ #include <cstddef>
223
+ extern \" C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
224
+ __attribute__((weak)) // allow for libFuzzer linking
225
+ int main() { return 0; }
226
+ "
227
+ RESULT_VAR linker_supports_sanitizers
228
+ )
229
+ if (NOT linker_supports_sanitizers)
230
+ message (FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries." )
231
+ endif ()
232
+ endif ()
233
+
203
234
include (AddBoostIfNeeded)
204
235
add_boost_if_needed()
205
236
0 commit comments