title: Clang 8 with MinGW-w64: How do I use address- & UB sanitizers?
body: <p><a href="http://releases.llvm.org/8.0.0/tools/clang/docs/ReleaseNotes.html#id9" rel="noreferrer">Clang 8 release notes</a> have this promising line:</p>
  
  <blockquote>
    <ul>
    <li>Allow using Address Sanitizer and Undefined Behaviour Sanitizer on MinGW.</li>
    </ul>
  </blockquote>
  
  <p>However, I unable to figure out how to use those properly.</p>
  
  <p>I'm using Clang 8.0.0 with MSYS2 MinGW GCC. Exact details are at the bottom of the question.</p>
  
  <p>I'm trying to compile following minimal piece of code:</p>
  
  <p><strong>1.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      // Testing ubsan
      int x = 0x7fffffff;
      x++;
      std::cout &lt;&lt; x &lt;&lt; std::endl;
  
      // Testing asan
      int *y = new int;
      delete y;
      std::cout &lt;&lt; *y &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Here are results for <code>-fsanitize=address</code>:</p>
  
  <pre><code># /z/Lander/LLVM/bin/clang++ -target x86_64-w64-windows-gnu -fsanitize=address 1.cpp
  Z:\Lander\msys2\mingw64\bin\ld.exe: cannot find Z:\Lander\LLVM\lib\clang\8.0.0\lib\windows\libclang_rt.asan_dynamic-x86_64.dll.a: No such file or directory
  Z:\Lander\msys2\mingw64\bin\ld.exe: cannot find Z:\Lander\LLVM\lib\clang\8.0.0\lib\windows\libclang_rt.asan_dynamic_runtime_thunk-x86_64.a: No such file or directory
  Z:\Lander\msys2\mingw64\bin\ld.exe: cannot find Z:\Lander\LLVM\lib\clang\8.0.0\lib\windows\libclang_rt.asan_dynamic_runtime_thunk-x86_64.a: No such file or directory
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>And here is <code>-fsanitize=undefined</code>:</p>
  
  <pre><code># /z/Lander/LLVM/bin/clang++ -target x86_64-w64-windows-gnu -fsanitize=undefined 1.cpp
  Warning: corrupt .drectve at end of def file
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0x9f): undefined reference to `__ubsan_handle_add_overflow'
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0xef): undefined reference to `__ubsan_handle_type_mismatch_v1'
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0x148): undefined reference to `__ubsan_handle_type_mismatch_v1'
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0x196): undefined reference to `__ubsan_handle_type_mismatch_v1'
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0x1df): undefined reference to `__ubsan_handle_type_mismatch_v1'
  Z:\Lander\msys2\tmp\1-13f09e.o:1.cpp:(.text+0x22c): undefined reference to `__ubsan_handle_type_mismatch_v1'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>Here are the contents of <code>Z:\Lander\LLVM\lib\clang\8.0.0\lib\windows\</code> where it looks for libraries:</p>
  
  <pre><code>clang_rt.asan-preinit-x86_64.lib
  clang_rt.asan-x86_64.lib
  clang_rt.asan_cxx-x86_64.lib
  clang_rt.asan_dll_thunk-x86_64.lib
  clang_rt.asan_dynamic-x86_64.dll
  clang_rt.asan_dynamic-x86_64.lib
  clang_rt.asan_dynamic_runtime_thunk-x86_64.lib
  clang_rt.builtins-x86_64.lib
  clang_rt.fuzzer-x86_64.lib
  clang_rt.fuzzer_no_main-x86_64.lib
  clang_rt.profile-x86_64.lib
  clang_rt.stats-x86_64.lib
  clang_rt.stats_client-x86_64.lib
  clang_rt.ubsan_standalone-x86_64.lib
  clang_rt.ubsan_standalone_cxx-x86_64.lib
  </code></pre>
  
  <p>This doesn't look right, since the MinGW GCC normally works with <code>.a</code> libraries, not <code>.lib</code>.</p>
  
  <p>I tried to manually link various libraries from that directory.</p>
  
  <p>For asan, I managed to get rid of compiler errors, but the asan itself doesn't seem to emit any diagnostics:</p>
  
  <pre><code># /z/Lander/LLVM/bin/clang++ -target x86_64-w64-windows-gnu -fsanitize=address 1.cpp -c
  # /z/Lander/LLVM/bin/clang++ -target x86_64-w64-windows-gnu 1.o /z/Lander/LLVM/lib/clang/8.0.0/lib/windows/clang_rt.asan_dynamic-x86_64.lib
  # ./a.exe
  -2147483648
  5089296         &lt;- expected a diagnostic here
  </code></pre>
  
  <p>For ubsan, I tried to link against <code>clang_rt.ubsan_standalone-x86_64.lib</code>, but got more undefined references and several <code>Warning: corrupt .drectve at end of def file</code>.</p>
  
  <p>I did some research on <code>Warning: corrupt .drectve at end of def file</code>, and <a href="https://stackoverflow.com/questions/25161814/warning-corrupt-drectve-at-end-of-def-file">this question</a> suggests that it means I'm linking incompatible MSVC libraries.</p>
  
  <p>What's going on here? How am I supposed to use asan &amp; ubsan?</p>
  
  <hr>
  
  <p>All commands above were run from MSYS2 terminal, running on Windows 7 x64.</p>
  
  <p>I'm targeting x86_64 and using latest GCC available in MSYS2:</p>
  
  <pre><code># g++ --version
  g++.exe (Rev2, Built by MSYS2 project) 8.3.0
  </code></pre>
  
  <p>Clang from MSYS2 doesn't seem to have asan &amp; ubsan libraries bundled, so I'm using the official build from <a href="http://releases.llvm.org/download.html" rel="noreferrer">llvm.org</a>:</p>
  
  <pre><code># /z/Lander/LLVM/bin/clang++ --version
  clang version 8.0.0 (tags/RELEASE_800/final)
  Target: x86_64-pc-windows-msvc
  Thread model: posix
  InstalledDir: Z:\Lander\LLVM\bin
  </code></pre>
  
  <p>I'm using <code>-target x86_64-w64-windows-gnu</code> because otherwise Clang tries to use a MSVC installation, which I don't have. This specific triplet is what MSYS2 clang reports in reponse to <code>--version</code>.</p>