Application crashes when using address sanitizer with MSVC

Viewed 2849

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!

2 Answers

ASAN is still an experimental feature in MSVC. 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 either add C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\lib\x64 to the PATH or copy the DLLs to your project's output folder (side-by-side with your .exe).

Alternatively you can build in static CRT mode (e.g. /MTd), then also the ASAN lib will be embedded into the .exe.

Finally, as the warning suggests, use ASAN with Debug build type for better coverage:
cmake -DCMAKE_BUILD_TYPE=Debug ..

You may also need to carry llvm-symbolizer.exe from the same folder. Sanitizer runtime is using it to symbolize the stack traces in case of errors found

Related