From 49ccaa781adb04741faedf017f75ea807fceba6b Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 23 Nov 2021 14:30:43 -0600 Subject: [PATCH] fix: handle msvc missing environment variables for the sanitizer --- Makefile | 2 +- src/Sanitizers.cmake | 25 ++++++++++++++++++------- test/CMakeLists.txt | 2 +- test/main.cpp | 9 +++++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 4b940a34..0d5e30a2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/Sanitizers.cmake b/src/Sanitizers.cmake index bea8f181..c355b662 100644 --- a/src/Sanitizers.cmake +++ b/src/Sanitizers.cmake @@ -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") @@ -62,6 +55,12 @@ function( endif() endif() + list( + JOIN + SANITIZERS + "," + LIST_OF_SANITIZERS) + if(LIST_OF_SANITIZERS) if(NOT "${LIST_OF_SANITIZERS}" @@ -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() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 09b71e8e..92189712 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/main.cpp b/test/main.cpp index e87d3b7c..ebc0da14 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,5 +1,6 @@ // test external pac #include +#include #include // test std libraries @@ -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; }