缺少LLVM编译器-rt的libclang_rt.san-x86_64.a文件

时间:2015-04-01 13:55:12

标签: c++ clang llvm

我刚刚构建了LLVM / Clang compiler-rt并尝试了-fsanitize选项。但奇怪的是,链接失败,因为找不到libclang_rt.san-x86_64.a

/usr/bin/ld: cannot find /home/hongxu/RESEARCH/llvm-git/obj/bin/../lib/clang/3.7.0/lib/linux/libclang_rt.san-x86_64.a: No such file or directory
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

当我更改到目录/home/hongxu/RESEARCH/llvm-git/obj/bin/../lib/clang/3.7.0/lib/linux/时,我发现还有其他库文件

# AddressSanitizer
libclang_rt.asan_cxx-x86_64.a
libclang_rt.asan-preinit-x86_64.a
libclang_rt.asan-x86_64.a
# DataFlowSanitizer
libclang_rt.dfsan-libc-x86_64.a
libclang_rt.dfsan-x86_64.a
# LeakSanitizer
libclang_rt.lsan-x86_64.a
# MemorySanitizer
libclang_rt.msan-x86_64.a
# ThreadSanitizer
libclang_rt.tsan-x86_64.a
# UndefinedBehaviorSanitizer
libclang_rt.ubsan_cxx-x86_64.a
libclang_rt.ubsan_standalone_cxx-x86_64.a
libclang_rt.ubsan_standalone-x86_64.a
libclang_rt.ubsan-x86_64.a

我可以根据compiler-rt页面从名称中猜出它们的功能。

但是libclang_rt.san-x86_64.a是什么?我怎么能得到它?

3 个答案:

答案 0 :(得分:3)

  

但奇怪的是链接失败,因为它找不到libclang_rt.san-x86_64.a。

是的,make install没有安装一些需要的东西。其他时候,它会将它们安装在非标准位置。

它未安装的其他内容包括asan_symbolize.py,用于表示来自Address Sanitizer(ASan)的转储。


  

但是什么是libclang_rt.san-x86_64.a?我怎么能得到它?

其中一个清洁剂库。你可能拥有它,你只是没有意识到它,因为它在非标准的位置。例如,在我的系统上(我自己构建LLVM / Clang):

$ find /usr -name libclang_rt.san-x86_64.a 2>/dev/null 
/usr/local/lib/clang/3.5.0/lib/linux/libclang_rt.san-x86_64.a

所以你需要做的是使用LD_LIBRARY_PATH(Linux)或DYLD_LIBRARY_PATH(OS X)来确保编译器驱动程序能够找到它。您应该 从不 必须手动添加各种清理程序库 - 编译器驱动程序应始终为您添加它们。

为了完整起见,Clang 3.4在Linux上的/usr/local/lib/clang/3.4/lib/linux/安装了清理程序库;和Clang 3.3在OS X上的/usr/local/lib/clang/3.3/lib/darwin/安装了它们。

您实际上可以更改源代码中的搜索目录,它们将由编译器驱动程序自动选取。我想我必须更改实际来源,因为我找不到添加/usr/local/lib/clang/<version>/lib/linux/等位置的配置选项。看看tools/clang/lib/Frontend/InitHeaderSearch.cpp和朋友们。这就是像.../include/c++/4.2.1这样的路径来自的地方。


顺便说一句,这是使用Address Sanitizer和asan_symbolize.py的方法。首先,运行2to3asan_symbolize.py来修复Python人员与基本I / O相关的内容:

$ find Clang-3.5/ -name asan_symbolize.py
Clang-3.5/llvm/projects/compiler-rt/lib/asan/scripts/asan_symbolize.py
2to3 -w Clang-3.5/llvm/projects/compiler-rt/lib/asan/scripts/asan_symbolize.py

echo "" | Clang-3.5/llvm/projects/compiler-rt/lib/asan/scripts/asan_symbolize.py
# Fix errors 2to3 missed

然后,将其复制到一个众所周知的位置(或将其放在路径上):

sudo cp Clang-3.5/llvm/projects/compiler-rt/lib/asan/scripts/asan_symbolize.py /usr/local/bin

然后,对于您的项目:

export CPPFLAGS="-fsanitze=undefined -fsanitize=address"
export CFLAGS="-fsanitze=undefined -fsanitize=address"
export CXXFLAGS="-fsanitze=undefined -fsanitize=address -fno-sanitize=vptr"
export CC=/usr/local/bin/clang
export CXX=/usr/local/bin/clang++
export LD_LIBRARY_PATH=/usr/local/lib/clang/3.5.0/lib/linux

./configure
make
make check 2>&1 | asan_symbolize.py
对于Autotools项目,

CPPFLAGS实际上非常重要。否则,你会得到可怕的 C编译器无法创建可执行文件错误。

当您出现ASan错误时,您会看到类似于:

make test 2>&1 | asan_symbolize.py
...

/usr/local/bin/clang -fsanitize=address -Xlinker -export-dynamic
    -o python Modules/python.o libpython3.3m.a -ldl -lutil
    /usr/local/ssl/lib/libssl.a /usr/local/ssl/lib/libcrypto.a -lm
./python -E -S -m sysconfig --generate-posix-vars
=================================================================
==24064==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x619000004020 at pc 0x4ed4b2 bp 0x7fff80fff010 sp 0x7fff80fff008
READ of size 4 at 0x619000004020 thread T0
  #0 0x4ed4b1 in PyObject_Free Python-3.3.5/./Objects/obmalloc.c:987
  #1 0x7a2141 in code_dealloc Python-3.3.5/./Objects/codeobject.c:359
  #2 0x620c00 in PyImport_ImportFrozenModuleObject
       Python-3.3.5/./Python/import.c:1098
  #3 0x620d5c in PyImport_ImportFrozenModule
       Python-3.3.5/./Python/import.c:1114
  #4 0x63fd07 in import_init Python-3.3.5/./Python/pythonrun.c:206
  #5 0x63f636 in _Py_InitializeEx_Private
       Python-3.3.5/./Python/pythonrun.c:369
  #6 0x681d77 in Py_Main Python-3.3.5/./Modules/main.c:648
  #7 0x4e6894 in main Python-3.3.5/././Modules/python.c:62
  #8 0x2abf9a525eac in __libc_start_main
       /home/aurel32/eglibc/eglibc-2.13/csu/libc-start.c:244
  #9 0x4e664c in _start (Python-3.3.5/./python+0x4e664c)

AddressSanitizer can not describe address in more detail (wild
memory access suspected).
SUMMARY: AddressSanitizer: heap-buffer-overflow
  Python-3.3.5/./Objects/obmalloc.c:987 PyObject_Free
Shadow bytes around the buggy address:
  0x0c327fff87b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff87c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff87d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff87e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff87f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c327fff8800: fa fa fa fa[fa]fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8810: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8820: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8830: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8840: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8850: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:     fa
  Heap right redzone:    fb
  Freed heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==24064==ABORTING
make: *** [pybuilddir.txt] Error 1

有一个更完整的LLVM / Clang构建过程的编写,并在Python的Dynamic Analysis with Clang使用santizers。我刚才写过,所以版本和配方都是陈旧的。但概念是一样的。

答案 1 :(得分:3)

感谢@ jww的回答;但我的问题不同。

我收到了llvmdev邮件列表的回复(见the thread),他们说:

  

libclang_rt.san现在已经消失了。新编译器-rt构建不包含此库,而新的Clang不使用它。

我编译失败,因为我在编译之前没有同步其他llvm项目(我只在llvm / projects目录中放入compiler-rt并从llvm build根目录构建)。

解决方案很简单:

  

尝试从头开始构建(删除构建目录,将所有llvm子项目同步到同一版本,再次构建)。

答案 2 :(得分:3)

该库仍然与clang 3.7一起使用(当启用消毒剂时)但如果使用autotools编译,它将丢失。我通过寻找一个简单的解决方案找到了这个线程,但似乎没有,除了用cmake构建所有。

另见LLVM Issue 22757 - libclang_rt.asan missing in clang-3.8 deb packages

我正在使用的另一个选项是使用clang 3.6。