Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing libclang_rt.san-x86_64.a file for LLVM compiler-rt

Tags:

c++

llvm

clang

I've just built LLVM/Clang compiler-rt and tried the -fsanitize option. But strangely the link failed since it cannot find 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)

When I changed into directory /home/hongxu/RESEARCH/llvm-git/obj/bin/../lib/clang/3.7.0/lib/linux/, I found that there are other library files

# 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

And I can guess their functionalities from the name according to compiler-rt page.

But what is libclang_rt.san-x86_64.a? And how can I get it?

like image 904
Hongxu Chen Avatar asked Apr 01 '15 13:04

Hongxu Chen


3 Answers

But strangely the link failed since it cannot find libclang_rt.san-x86_64.a.

Yeah, make install does not install some things that are needed. Other times, it installs them in non-standard locations.

Other things it does not install includes asan_symbolize.py, which is used to symbolicate dumps from Address Sanitizer (ASan).


But what is libclang_rt.san-x86_64.a? And how can I get it?

Its one of the sanitizer libraries. You probably have it, you just don't realize it because its in a non-standard location. For example, on my system (where I build LLVM/Clang myself):

$ 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

So what you have to do is use either LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (OS X) to ensure the compiler driver can find it. You should never have to manually add the various sanitizer libraries - the compiler driver should always add them for you.

For completeness, Clang 3.4 installed the sanitizers libraries at /usr/local/lib/clang/3.4/lib/linux/ on Linux; and Clang 3.3 installed them at /usr/local/lib/clang/3.3/lib/darwin/ on OS X.

You can actually change search directories in the source code and they will be picked up automatically by the compiler driver. I think I had to change the actual sources because I could not find a configure option to add locations like /usr/local/lib/clang/<version>/lib/linux/. Take a look at tools/clang/lib/Frontend/InitHeaderSearch.cpp and friends. That's where paths like .../include/c++/4.2.1 come from.


By the way, here's how to use Address Sanitizer and asan_symbolize.py. First, run 2to3 and asan_symbolize.py to fix what the Python folks broke related to basic 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

Then, copy it to a well known location (or put it on path):

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

Then, for your project:

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

CPPFLAGS is actually quite important for an Autotools project. Otherwise, you get the dreaded C compiler cannot create executables error.

When you have a ASan error, you will see similar to:

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

There's a more complete writeup of the LLVM/Clang build process and using the santizers at Python's Dynamic Analysis with Clang. I wrote it a while ago, so the version and recipe are stale. But the concepts are the same.

like image 59
jww Avatar answered Nov 09 '22 09:11

jww


Thanks for @jww's answer; but my problem is different.

I've got replies from llvmdev mailing list (see the thread) and they said that:

libclang_rt.san is now gone. Fresh compiler-rt build doesn't contain this library, and fresh Clang is not using it.

And I failed to compile since I didn't sync the other llvm projects before compiling (I only put compiler-rt inside llvm/projects directory and build from llvm build root directory).

And the solution is simple:

Trying building from scratch (remove the build directory, sync all llvm subprojects to the same revision, build again).

like image 34
Hongxu Chen Avatar answered Nov 09 '22 08:11

Hongxu Chen


The library is still used with clang 3.7 (when sanitizers are enabled) but if compiled using autotools, it will be missing. I found this thread by looking for a simple solution, but there doesnt seem to be any, except building all yourself with cmake.

Also see LLVM Issue 22757 - libclang_rt.asan missing in clang-3.8 deb packages.

The other option, which I am using, is to use clang 3.6.

like image 38
Norbert Lange Avatar answered Nov 09 '22 08:11

Norbert Lange