Search code examples
xcodemacosclangubsan

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


I'm trying to use the Undefined Behavior Sanitizer by building like this

gcc -fsanitize=undefined add.c -o add

also

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

In both cases, I'm getting a file not found error:

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

This is the output I get when running gcc -v and 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

According to this news release, it is available in GCC and also the original homepage for it says it's been merged into LLVM. The linked to article says GCC 4.9 has it, which I assume I have (at least --the version numbering seems different but the article was written a few years ago and I have updated my system several times).

Question: how can I build an executable to use UBSan?


Solution

  • According to blog Friday Q&A 2015-07-03: Address Sanitizer by Mike Ash:

    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
    

    Spencer at 2016-12-21 01:23:59:

    If you use -fsanitize=address,undefined it works. Hopefully this helps anyone else who finds this post search for that error like I did.

    Its not an ideal solution for us since they are separate components, and we have limited control over the environment and tools. We had to suspend some Address Sanitizer testing because Asan produces incorrect results with GCC inline assembly and use of ebp/rbp as a general purpose register. And here, the environment and tools are provided by Travis CI.


    A bug report was filed with LLVM at Issue 33201, Apple Clang bundled with Xcode 8 does not reject -fsanitize=undefined when it should. It may be closed as off-topic since its an Apple Clang bug (we don't have an iTunes account to file the bug with Apple).