Skip to content

Commit

Permalink
fix: handle msvc missing environment variables for the sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Nov 23, 2021
1 parent dc4f03c commit 49ccaa7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test format lint

test:
cmake ./test -B ./test/build -DCMAKE_BUILD_TYPE:STRING=Debug -G Ninja
cmake ./test -B ./test/build -DCMAKE_BUILD_TYPE:STRING=Debug
cmake --build ./test/build --config Debug
cd ./test/build && ctest -C Debug --verbose

Expand Down
25 changes: 18 additions & 7 deletions src/Sanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ function(
list(APPEND SANITIZERS "memory")
endif()
endif()

list(
JOIN
SANITIZERS
","
LIST_OF_SANITIZERS)

elseif(MSVC)
if(${ENABLE_SANITIZER_ADDRESS})
list(APPEND SANITIZERS "address")
Expand All @@ -62,6 +55,12 @@ function(
endif()
endif()

list(
JOIN
SANITIZERS
","
LIST_OF_SANITIZERS)

if(LIST_OF_SANITIZERS)
if(NOT
"${LIST_OF_SANITIZERS}"
Expand All @@ -71,7 +70,19 @@ function(
target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
else()
if("$ENV{VCINSTALLDIR}" STREQUAL "")
message(
FATAL_ERROR
"Using MSVC sanitizers requires that you have set the MSVC environment before building the project. Please use the MSVC command prompt and rebuild the project. You can set up the MSVC environment using vcvarsall in cmd:
# find vcvarsall.bat
vswhere -products * -latest -prerelease -find \"**/VC/Auxiliary/Build/vcvarsall.bat\"
# set up the environment for x64
\"path/to/vcvarsall\" x64
")
endif()
target_compile_options(${project_name} INTERFACE /fsanitize=${LIST_OF_SANITIZERS} /Zi /INCREMENTAL:NO)
target_link_options(${project_name} INTERFACE /INCREMENTAL:NO)
endif()
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ProjectOptions(
# ENABLE_USER_LINKER
# ENABLE_BUILD_WITH_TIME_TRACE
ENABLE_UNITY
# ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_ADDRESS
# ENABLE_SANITIZER_LEAK
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
# ENABLE_SANITIZER_THREAD
Expand Down
9 changes: 7 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// test external pac
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <Eigen/Dense>

// test std libraries
Expand All @@ -19,8 +20,12 @@ int main() {
fmt::print("Hello from fmt{}", "!");

// populate an Eigen vector with the values
auto vec = Eigen::VectorXd::LinSpaced(10, 0, 1);
auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1);

// print the vector
fmt::print("{}", vec[0]);
fmt::print("{}", eigen_vec);

// trigger address sanitizer
int *p = nullptr;
*p = 1;
}

0 comments on commit 49ccaa7

Please sign in to comment.