Skip to content

Commit c9231ab

Browse files
committed
cmake: Add SANITIZERS option
1 parent 92968f5 commit c9231ab

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,37 @@ endif()
204204
include(AddThreadsIfNeeded)
205205
add_threads_if_needed()
206206

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

0 commit comments

Comments
 (0)