Skip to content

Commit f9f92d2

Browse files
committed
Improve a bit sanitizer support.
1 parent 8821441 commit f9f92d2

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

cmake/CompileOptions.cmake

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ set(DEFAULT_INCLUDE_DIRECTORIES)
7070

7171
# ThreadSanitizer is incompatible with AddressSanitizer and LeakSanitizer
7272
if(OPTION_BUILD_THREAD_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
73-
set(DEFAULT_LIBRARIES -ltsan)
73+
set(SANITIZER_LIBRARIES -ltsan)
7474
set(TESTS_SANITIZER_ENVIRONMENT_VARIABLES
7575
"TSAN_OPTIONS=suppressions=${CMAKE_SOURCE_DIR}/source/tests/sanitizer/tsan.supp"
7676
)
@@ -79,11 +79,20 @@ if(OPTION_BUILD_THREAD_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE
7979
)
8080
elseif(OPTION_BUILD_MEMORY_SANITIZER AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
8181
# TODO: This requires much more effort than expected: https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo
82-
set(DEFAULT_LIBRARIES)
82+
set(SANITIZER_LIBRARIES)
8383
set(TESTS_SANITIZER_ENVIRONMENT_VARIABLES)
84-
set(SANITIZER_COMPILE_DEFINITIONS)
84+
set(SANITIZER_COMPILE_DEFINITIONS
85+
"__MEMORY_SANITIZER__=1"
86+
)
87+
elseif(OPTION_BUILD_UB_SANITIZER AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
88+
# TODO
89+
set(SANITIZER_LIBRARIES)
90+
set(TESTS_SANITIZER_ENVIRONMENT_VARIABLES)
91+
set(SANITIZER_COMPILE_DEFINITIONS
92+
"__UB_SANITIZER__=1"
93+
)
8594
elseif(OPTION_BUILD_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
86-
set(DEFAULT_LIBRARIES -lasan -lubsan)
95+
set(SANITIZER_LIBRARIES -lasan -lubsan)
8796
set(TESTS_SANITIZER_ENVIRONMENT_VARIABLES
8897
"LSAN_OPTIONS=verbosity=1:log_threads=1:print_suppressions=false:suppressions=${CMAKE_SOURCE_DIR}/source/tests/sanitizer/lsan.supp"
8998

@@ -100,11 +109,21 @@ elseif(OPTION_BUILD_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BU
100109
"__ADDRESS_SANITIZER__=1"
101110
)
102111
else()
103-
set(DEFAULT_LIBRARIES)
112+
set(SANITIZER_LIBRARIES)
104113
set(TESTS_SANITIZER_ENVIRONMENT_VARIABLES)
105114
set(SANITIZER_COMPILE_DEFINITIONS)
106115
endif()
107116

117+
if(WIN32 AND MSVC)
118+
# MSVC does not require to link manually the sanitizer libraries
119+
set(SANITIZER_LIBRARIES)
120+
endif()
121+
122+
# Set default libraries
123+
set(DEFAULT_LIBRARIES
124+
${SANITIZER_LIBRARIES}
125+
)
126+
108127
#
109128
# Compile definitions
110129
#
@@ -220,6 +239,9 @@ if(WIN32 AND MSVC)
220239
add_compile_options(/fsanitize=address)
221240
elseif(OPTION_BUILD_MEMORY_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
222241
add_compile_options(/fsanitize=memory)
242+
add_compile_options(/fsanitize=leak)
243+
elseif(OPTION_BUILD_UB_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
244+
add_compile_options(/fsanitize=undefined)
223245
endif()
224246
endif()
225247

@@ -275,6 +297,8 @@ if (PROJECT_OS_FAMILY MATCHES "unix")
275297
add_compile_options(-fsanitize=memory)
276298
add_compile_options(-fsanitize-memory-track-origins)
277299
add_compile_options(-fsanitize-memory-use-after-dtor)
300+
elseif(OPTION_BUILD_UB_SANITIZER AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
301+
# TODO
278302
endif()
279303
endif()
280304

source/dynlink/source/dynlink_impl_beos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int dynlink_impl_interface_unload_beos(dynlink handle, dynlink_impl impl)
9494
{
9595
(void)handle;
9696

97-
#if defined(__ADDRESS_SANITIZER__) || defined(__THREAD_SANITIZER__)
97+
#if defined(__ADDRESS_SANITIZER__) || defined(__THREAD_SANITIZER__) || defined(__MEMORY_SANITIZER__)
9898
/* Disable dlclose when running with address sanitizer in order to maintain stacktraces */
9999
(void)impl;
100100
return 0;

source/dynlink/source/dynlink_impl_unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ int dynlink_impl_interface_unload_unix(dynlink handle, dynlink_impl impl)
114114
{
115115
(void)handle;
116116

117-
#if defined(__ADDRESS_SANITIZER__) || defined(__THREAD_SANITIZER__)
117+
#if defined(__ADDRESS_SANITIZER__) || defined(__THREAD_SANITIZER__) || defined(__MEMORY_SANITIZER__) || defined(__UB_SANITIZER__)
118118
/* Disable dlclose when running with address sanitizer in order to maintain stacktraces */
119119
(void)impl;
120120
return 0;

source/ports/js_port/test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void ModulesClear()
323323
FreeLibrary(it->second);
324324
#elif defined(JS_PORT_TEST_UNIX)
325325
/* Disable dlclose when running with address sanitizer in order to maintain stacktraces */
326-
#if !defined(__ADDRESS_SANITIZER__) && !defined(__THREAD_SANITIZER__)
326+
#if !defined(__ADDRESS_SANITIZER__) && !defined(__THREAD_SANITIZER__) && !defined(__MEMORY_SANITIZER__) && !defined(__UB_SANITIZER__)
327327
dlclose(it->second);
328328
#endif
329329
#endif

source/tests/metacall_test/source/metacall_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ TEST_F(metacall_test, DefaultConstructor)
280280

281281
metacall_value_destroy(ret);
282282

283-
#if !defined(__ADDRESS_SANITIZER__) && !defined(__THREAD_SANITIZER__) && !defined(__MEMORY_SANITIZER__)
283+
#if !defined(__ADDRESS_SANITIZER__) && !defined(__THREAD_SANITIZER__) && !defined(__MEMORY_SANITIZER__) && !defined(__UB_SANITIZER__)
284284
/* Testing corrupted value input */
285285
struct
286286
{

0 commit comments

Comments
 (0)