-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CMake option to enable sanitizers and build gtest #3555
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8430ae
Add CMake option to enable sanitizer
hcho3 77422b2
Set up gtest
hcho3 eb3eea0
Address reviewer's feedback
hcho3 92952a5
Merge remote-tracking branch 'upstream/master' into add_sanitizer
hcho3 dfaa89e
Address reviewer's feedback
hcho3 433ea47
Update CMakeLists.txt
hcho3 d3239c6
Merge remote-tracking branch 'upstream/master' into add_sanitizer
hcho3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Set appropriate compiler and linker flags for sanitizers. | ||
# | ||
# Usage of this module: | ||
# enable_sanitizers("address;leak") | ||
|
||
# Add flags | ||
macro(enable_sanitizer sanitizer) | ||
if(${sanitizer} MATCHES "address") | ||
find_package(ASan) | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=address") | ||
if (ASan_FOUND) | ||
StrikerRUS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
link_libraries(${ASan_LIBRARY}) | ||
endif (ASan_FOUND) | ||
|
||
elseif(${sanitizer} MATCHES "thread") | ||
find_package(TSan) | ||
set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=thread") | ||
if (TSan_FOUND) | ||
link_libraries(${TSan_LIBRARY}) | ||
endif (TSan_FOUND) | ||
|
||
elseif(${sanitizer} MATCHES "leak") | ||
find_package(LSan) | ||
set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=leak") | ||
if (LSan_FOUND) | ||
link_libraries(${LSan_LIBRARY}) | ||
endif (LSan_FOUND) | ||
|
||
elseif(${sanitizer} MATCHES "undefined") | ||
find_package(UBSan) | ||
set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=undefined -fno-sanitize-recover=undefined") | ||
if (UBSan_FOUND) | ||
link_libraries(${UBSan_LIBRARY}) | ||
endif (UBSan_FOUND) | ||
|
||
else() | ||
message(FATAL_ERROR "Santizer ${sanitizer} not supported.") | ||
endif() | ||
endmacro() | ||
|
||
macro(enable_sanitizers SANITIZERS) | ||
# Check sanitizers compatibility. | ||
foreach ( _san ${SANITIZERS} ) | ||
string(TOLOWER ${_san} _san) | ||
if (_san MATCHES "thread") | ||
if (${_use_other_sanitizers}) | ||
message(FATAL_ERROR | ||
"thread sanitizer is not compatible with ${_san} sanitizer.") | ||
endif() | ||
set(_use_thread_sanitizer 1) | ||
else () | ||
if (${_use_thread_sanitizer}) | ||
message(FATAL_ERROR | ||
"${_san} sanitizer is not compatible with thread sanitizer.") | ||
endif() | ||
set(_use_other_sanitizers 1) | ||
endif() | ||
endforeach() | ||
|
||
message("Sanitizers: ${SANITIZERS}") | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
foreach( _san ${SANITIZERS} ) | ||
string(TOLOWER ${_san} _san) | ||
enable_sanitizer(${_san}) | ||
endforeach() | ||
message("Sanitizers compile flags: ${SAN_COMPILE_FLAGS}") | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_COMPILE_FLAGS}") | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_COMPILE_FLAGS}") | ||
endmacro() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(ASan_LIB_NAME ASan) | ||
|
||
find_library(ASan_LIBRARY | ||
NAMES libasan.so libasan.so.5 libasan.so.4 libasan.so.3 libasan.so.2 libasan.so.1 libasan.so.0 | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PATHS ${SANITIZER_PATH} /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib ${CMAKE_PREFIX_PATH}/lib) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(ASan DEFAULT_MSG | ||
ASan_LIBRARY) | ||
|
||
mark_as_advanced( | ||
ASan_LIBRARY | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ASan_LIB_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(LSan_LIB_NAME lsan) | ||
|
||
find_library(LSan_LIBRARY | ||
NAMES liblsan.so liblsan.so.0 liblsan.so.0.0.0 | ||
PATHS ${SANITIZER_PATH} /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib ${CMAKE_PREFIX_PATH}/lib) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(LSan DEFAULT_MSG | ||
LSan_LIBRARY) | ||
|
||
mark_as_advanced( | ||
LSan_LIBRARY | ||
LSan_LIB_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(TSan_LIB_NAME tsan) | ||
|
||
find_library(TSan_LIBRARY | ||
NAMES libtsan.so libtsan.so.0 libtsan.so.0.0.0 | ||
PATHS ${SANITIZER_PATH} /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib ${CMAKE_PREFIX_PATH}/lib) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(TSan DEFAULT_MSG | ||
TSan_LIBRARY) | ||
|
||
mark_as_advanced( | ||
TSan_LIBRARY | ||
TSan_LIB_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(UBSan_LIB_NAME UBSan) | ||
|
||
find_library(UBSan_LIBRARY | ||
NAMES libubsan.so libubsan.so.5 libubsan.so.4 libubsan.so.3 libubsan.so.2 libubsan.so.1 libubsan.so.0 | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PATHS ${SANITIZER_PATH} /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib ${CMAKE_PREFIX_PATH}/lib) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(UBSan DEFAULT_MSG | ||
UBSan_LIBRARY) | ||
|
||
mark_as_advanced( | ||
UBSan_LIBRARY | ||
UBSan_LIB_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*! | ||
* Copyright (c) 2020 Microsoft Corporation. All rights reserved. | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
*/ | ||
#include <gtest/gtest.h> | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
testing::FLAGS_gtest_death_test_style = "threadsafe"; | ||
return RUN_ALL_TESTS(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on #3555 (comment), this should be
SET
, right?