I am trying to use address sanitizer with MSVC.
Visual Studio Installer says I have "Visual Studio Community 2019" version 16.9.0.
I have the most basic C++ program:
int main() {
return 0;
}
The CMakeLists.txt is also very basic:
cmake_minimum_required(VERSION 3.14)
project(untitled VERSION 1.0.0)
add_executable(${PROJECT_NAME} main.cpp)
add_definitions(/fsanitize=address /Zi)
The program compiles and links correctly, both in debug and release mode. In release mode, I have the following warning, which seems perfectly legit:
LINK : warning LNK4302: please add '/DEBUG' to the link command line for better ASAN error reporting
So far, so good (?).
However, when I run the application, the return code is -1073741515 (0xC0000135). Google tells me this is likely to indicate a missing library.
If I remove add_definitions(/fsanitize=address /Zi) from the build, the return is 0. The missing library is very likely the address sanitizer itself.
How can I configure my system properly to solve this issue? Thanks!
ASAN is a debugging feature. For this reason the clang_rt.asan_*.dll DLLs are not installed in System32 as part of the VC++ redistributable package.
As explained here, when using ASAN in shared CRT mode (/MDd), you need to ensure clang_rt.asan_dbg_dynamic-x86_64.dll and clang_rt.asan_dynamic-x86_64.dll are on the PATH.
You can add C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64 to the PATH or copy the DLLs to your project's output folder (side-by-side with your .exe).
Prior to Visual Studio 2022 version 17.7 Preview 3, you could alternatively build in static CRT mode (e.g. /MTd) which would embed the ASAN lib into the .exe. Newer version of Visual Studio always require the separate DLL.
Finally, as the warning suggests, use ASAN with Debug build type for better coverage:
cmake -DCMAKE_BUILD_TYPE=Debug ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With