Reputation: 812
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!
Upvotes: 17
Views: 14223
Reputation: 66
In 2019 Microsoft gave a list of the DLLs that are required, either in a directory that is locatable via the PATH
environment variable or side-by-side in your application's own directory (i.e. the normal way that DLLs are found). The DLL names in the 2019 announcement are out of date now, however.
As explained by Microsoft here in a 2020 update, you may also need to carry llvm-symbolizer.exe
from the same folder. The sanitizer runtime is using it to symbolize the stack traces in case of errors found.
As further explained in 2023 the library names changed again as of Visual Studio 17.7 Preview 3, when things were consolidated to support multiple compiler configurations. The "Troubleshooting" section of that announcement describes some of the errors that might result from missing DLLs now.
The different DLL names before and after Visual Studio 17.7 Preview 3 are also given by Microsoft in two tables here.
Upvotes: 2
Reputation: 6288
In Visual Studio 22 the location of the dll has changed. In my case I needed the i386 version. The location for clang_rt.asan_dbg_dynamic-i386.dll
is:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<version>\bin\Hostx86\x86
.C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\<version>\bin\Hostx64\x64\
This must be added to the PATH variable (or copied to the exe dir) to prevent this error. Take the 64 bit version if debugging for x64.
Upvotes: 12
Reputation: 85341
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 ..
Upvotes: 29
Reputation: 112
QtCreator has an option in settings that is enabled by default that adds proper environment variables from your toolchain, I guess VSCode CMake Tools extension does this too, I have used both, anyways with CLion, I had to add this, I hate it is manual but yeah,
Go to File -> Settings -> Toolchains -> Visual Studio,
Then click on "Add environment",
Choose "From file"
and paste in the path to vcvars64.bat or other bat files in
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build
that match your build config, I really hope there is an automatic way for this
Upvotes: 1