带有MinGW-w64的Clang 8:如何使用地址&UB消毒剂? [英] Clang 8 with MinGW-w64: How do I use address- & UB sanitizers?

查看:161
本文介绍了带有MinGW-w64的Clang 8:如何使用地址&UB消毒剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clang 8发行说明包含以下内容有希望的行:

Clang 8 release notes have this promising line:

  • 允许在MinGW上使用Address Sanitizer和Undefined Behavior Sanitizer.

但是,我无法弄清楚如何正确使用它们.

However, I unable to figure out how to use those properly.

我将Clang 8.0.0与MSYS2 MinGW GCC结合使用.确切的细节在问题的底部.

I'm using Clang 8.0.0 with MSYS2 MinGW GCC. Exact details are at the bottom of the question.

我正在尝试用最少的代码来编译:

I'm trying to compile following minimal piece of code:

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;
}

以下是 -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)

这是 -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)

以下是查找库的 Z:\ Lander \ LLVM \ lib \ clang \ 8.0.0 \ lib \ windows \ 的内容:

Here are the contents of Z:\Lander\LLVM\lib\clang\8.0.0\lib\windows\ where it looks for libraries:

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

这看起来不太正确,因为MinGW GCC通常使用 .a 库而不是 .lib .

This doesn't look right, since the MinGW GCC normally works with .a libraries, not .lib.

我试图手动链接该目录中的各种库.

I tried to manually link various libraries from that directory.

对于asan,我设法摆脱了编译器错误,但是asan本身似乎没有发出任何诊断信息:

For asan, I managed to get rid of compiler errors, but the asan itself doesn't seem to emit any diagnostics:

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

对于ubsan,我尝试链接到 clang_rt.ubsan_standalone-x86_64.lib ,但获得了更多未定义的引用和一些警告:def文件末尾的.drectve损坏

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.

我对警告进行了一些研究:def文件末尾的.drectve损坏

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.

这是怎么回事?我应该如何使用asan&ubsan?

What's going on here? How am I supposed to use asan & ubsan?

以上所有命令均在Windows 7 x64上的MSYS2终端上运行.

All commands above were run from MSYS2 terminal, running on Windows 7 x64.

我的目标是x86_64,并使用MSYS2中提供的最新GCC:

I'm targeting x86_64 and using latest GCC available in MSYS2:

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

MSYS2中的Clang似乎没有asan&捆绑了ubsan库,因此我使用的是 llvm.org :

Clang from MSYS2 doesn't seem to have asan & ubsan libraries bundled, so I'm using the official build from 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

我正在使用 -target x86_64-w64-windows-gnu ,因为否则Clang会尝试使用我没有的MSVC安装.这个特定的三元组是MSYS2叮当声响应-version 的报告.

I'm using -target x86_64-w64-windows-gnu 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 --version.

推荐答案

我已经找到一种使UBsan正常工作的方法,而Asan却没有.

I've found a way to make UBsan work, but not Asan.

结果证明,即使没有libubsan,UBsan也可以运行.您需要使用以下标志:

Turns out, UBsan can function even without libubsan. 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.

GCC和Clang均支持此标志.

This flag is supported by both GCC and Clang.

GCC手册:

-fsanitize-undefined-trap-on-error

-fsanitize-undefined-trap-on-error

-fsanitize-undefined-trap-on-error 选项指示编译器使用 __ builtin_trap 而不是libubsan库例程报告未定义的行为.这样做的好处是不需要libubsan库,也不会链接libubsan库,因此即使在独立环境中也可以使用该库.

The -fsanitize-undefined-trap-on-error option instructs the compiler to report undefined behavior using __builtin_trap rather than a libubsan library routine. 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.

这篇关于带有MinGW-w64的Clang 8:如何使用地址&amp;UB消毒剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆