简体   繁体   English

带有 MinGW-w64 的 Clang 8:如何使用地址和 UB 消毒剂?

[英]Clang 8 with MinGW-w64: How do I use address- & UB sanitizers?

Clang 8 release notes have this promising line: Clang 8 发行说明中有这样一条很有前途的路线:

  • Allow using Address Sanitizer and Undefined Behaviour Sanitizer on MinGW.允许在 MinGW 上使用 Address Sanitizer 和 Undefined Behavior Sanitizer。

However, I unable to figure out how to use those properly.但是,我无法弄清楚如何正确使用它们。

I'm using Clang 8.0.0 with MSYS2 MinGW GCC.我将 Clang 8.0.0 与 MSYS2 MinGW GCC 一起使用。 Exact details are at the bottom of the question.确切的细节在问题的底部。

I'm trying to compile following minimal piece of code:我正在尝试编译以下最少的代码:

1.cpp 1.cpp

#include <iostream>

int main()
{
    // Testing ubsan
    int x = 0x7fffffff;
    x++;
    std::cout << x << std::endl;

    // Testing asan
    int *y = new int;
    delete y;
    std::cout << *y << std::endl;
}

Here are results for -fsanitize=address :以下是-fsanitize=address结果:

# /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)

And here is -fsanitize=undefined :这是-fsanitize=undefined

# /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)

Here are the contents of Z:\\Lander\\LLVM\\lib\\clang\\8.0.0\\lib\\windows\\ where it looks for libraries:以下是Z:\\Lander\\LLVM\\lib\\clang\\8.0.0\\lib\\windows\\中查找库的内容:

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

This doesn't look right, since the MinGW GCC normally works with .a libraries, not .lib .这看起来不对,因为 MinGW GCC 通常使用.a库,而不是.lib

I tried to manually link various libraries from that directory.我尝试从该目录手动链接各种库。

For asan, I managed to get rid of compiler errors, but the asan itself doesn't seem to emit any diagnostics:对于 asan,我设法摆脱了编译器错误,但 asan 本身似乎没有发出任何诊断信息:

# /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         <- expected a diagnostic here

For ubsan, I tried to link against clang_rt.ubsan_standalone-x86_64.lib , but got more undefined references and several Warning: corrupt .drectve at end of def file .对于 ubsan,我尝试链接到clang_rt.ubsan_standalone-x86_64.lib ,但得到了更多未定义的引用和几个Warning: corrupt .drectve at end of def file

I did some research on Warning: corrupt .drectve at end of def file , and this question suggests that it means I'm linking incompatible MSVC libraries.我对Warning: corrupt .drectve at end of def file做了一些研究, 这个问题表明这意味着我正在链接不兼容的 MSVC 库。

What's going on here?这里发生了什么? How am I supposed to use asan & ubsan?我应该如何使用 asan 和 ubsan?


All commands above were run from MSYS2 terminal, running on Windows 7 x64.上面的所有命令都是从 MSYS2 终端运行的,在 Windows 7 x64 上运行。

I'm targeting x86_64 and using latest GCC available in MSYS2:我的目标是 x86_64 并使用 MSYS2 中可用的最新 GCC:

# g++ --version
g++.exe (Rev2, Built by MSYS2 project) 8.3.0

Clang from MSYS2 doesn't seem to have asan & ubsan libraries bundled, so I'm using the official build from llvm.org : MSYS2 的 Clang 似乎没有捆绑 asan 和 ubsan 库,所以我使用的是llvm.org的官方版本:

# /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

I'm using -target x86_64-w64-windows-gnu because otherwise Clang tries to use a MSVC installation, which I don't have.我正在使用-target x86_64-w64-windows-gnu因为否则 Clang 会尝试使用我没有的 MSVC 安装。 This specific triplet is what MSYS2 clang reports in reponse to --version .这个特定的三元组是 MSYS2 clang 响应--version报告的内容。

I've found a way to make UBsan work, but not Asan.我找到了一种让 UBsan 工作的方法,但不是 Asan。

Turns out, UBsan can function even without libubsan.事实证明,即使没有 libubsan,UBsan 也能运行。 You need to use following flags:您需要使用以下标志:

-fsanitize=undefined -fsanitize-undefined-trap-on-error

This way, errors are reported via crashing with 'Illegal instruction' rather by emitting pretty diagnostics, but it's better than nothing.这样,错误是通过“非法指令”崩溃而不是发出漂亮的诊断来报告的,但这总比没有好。

This flag is supported by both GCC and Clang. GCC 和 Clang 都支持此标志。

GCC manual: 海湾合作委员会手册:

-fsanitize-undefined-trap-on-error

The -fsanitize-undefined-trap-on-error option instructs the compiler to report undefined behavior using __builtin_trap rather than a libubsan library routine. -fsanitize-undefined-trap-on-error选项指示编译器使用__builtin_trap而不是 libubsan 库例程报告未定义的行为。 The advantage of this is that the libubsan library is not needed and is not linked in, so this is usable even in freestanding environments.这样做的好处是不需要 libubsan 库,也没有链接,因此即使在独立环境中也可以使用。

ASAN indeed works on Windows. ASAN 确实适用于 Windows。

To avoid errors such as:为了避免错误,例如:

C:/msys64/mingw64/bin/ld: cannot find C:/msys64/mingw64/lib/clang/13.0.0/lib/windows/libclang_rt.asan_dynamic-x86_64.dll.a: No such file or directory
C:/msys64/mingw64/bin/ld: cannot find C:/msys64/mingw64/lib/clang/13.0.0/lib/windows/libclang_rt.asan_dynamic_runtime_thunk-x86_64.a: No such file or directory

use /clang64/bin/clang instead of /mingw64/bin/clang .使用/clang64/bin/clang而不是/mingw64/bin/clang Install it with:安装它:

pacman -S mingw-w64-clang-x86_64-clang

See https://www.msys2.org/docs/environments/https://www.msys2.org/docs/environments/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM