4

我正在尝试通过这样的构建来使用未定义的行为清理器

gcc -fsanitize=undefined add.c -o add

clang -fsanitize=undefined -O add.c -o add

在这两种情况下,我都会收到未找到文件的错误:

ld: file not found: /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib

这是我运行时得到的gcc -v输出clang -v

Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

根据这个新闻发布,它在 GCC 中可用,而且它的原始主页说它已被合并到 LLVM 中。链接到文章说 GCC 4.9 有它,我假设我有(至少 - 版本编号似乎不同,但这篇文章是几年前写的,我已经更新了我的系统几次)。

问题:如何构建可执行文件以使用 UBSan?

4

2 回答 2

1

为了使用未定义的行为清理程序,我必须使用 clang 以及 libcxx 和 libcxxabi 安装和构建 LLVM,即使我的 MacOS 已经附带了一个 clang 版本。(结果我现在在我的电脑上安装了两个版本的 clang)。从llvm 入门页面

cd where-you-want-llvm-to-live
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm

cd where-you-want-llvm-to-live
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang

cd where-you-want-llvm-to-live
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi

现在,我可以通过运行使用未定义的行为清理程序

/usr/local/bin/clang -fsanitize=undefined -O add.c -o add
于 2017-04-24T14:19:48.767 回答
1

根据博客Friday Q&A 2015-07-03:Mike Ash 的地址消毒剂

知道为什么 Apple clang 不支持-fsanitize=undefined吗?

当我尝试得到:

clang -fsanitize=undefined ub.c
...
ld: file not found: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/
bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib

斯宾塞在 2016-12-21 01:23:59:

如果你使用-fsanitize=address,undefined它就可以了。希望这可以帮助发现此帖子的其他人像我一样搜索该错误。

它对我们来说不是一个理想的解决方案,因为它们是独立的组件,而且我们对环境和工具的控制有限。我们不得不暂停一些 Address Sanitizer 测试,因为 Asan 使用 GCC 内联汇编和使用 ebp/rbp 作为通用寄存器会产生不正确的结果。在这里,环境和工具由Travis CI提供。


在问题 33201 中向 LLVM 提交了一个错误报告,与 Xcode 8 捆绑的 Apple Clang 不会拒绝 -fsanitize=undefined when it should。由于它是一个 Apple Clang 错误(我们没有 iTunes 帐户向 Apple 提交该错误),因此它可能会因为离题而关闭。

于 2017-05-28T21:59:12.037 回答