ld:找不到文件:/Library/Developer/.../libclang_rt.ubsan_osx_dynamic.dylib

ld: file not found: /Library/Developer/.../libclang_rt.ubsan_osx_dynamic.dylib

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

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 -vclang -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

根据 this news release, it is available in GCC and also the original homepage 因为它说它已被合并到 LLVM 中。文章的链接说 GCC 4.9 有它,我想我有(至少——版本编号似乎不同,但这篇文章是几年前写的,我已经更新了我的系统几次)。

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

为了使用未定义的行为消毒剂,我不得不使用 clang 以及 libcxx 和 libcxxabi 安装和构建 LLVM,即使我的 MacOS 已经带有一个 clang 版本。 (结果是我现在在我的电脑上安装了两个版本的 clang)。来自 llvm getting started page

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

根据博客 2015-07-03 星期五问答:Address Sanitizer 迈克·阿什:

Any idea why Apple clang doesn't support -fsanitize=undefined?

When I try that I get:

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 就可以了。希望这个 帮助其他发现此问题的人 post 像我一样搜索该错误。

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

提供

已在 Issue 33201, Apple Clang bundled with Xcode 8 does not reject -fsanitize=undefined when it should 向 LLVM 提交错误报告。由于它是一个 Apple Clang 错误(我们没有 iTunes 帐户来向 Apple 提交错误),它可能会因为跑题而被关闭。