1

I would like to run a bitcode with address sanitizer argument, but I have a problem with that, if I run it, the segmentation fault will happen.

$cat sample.c
#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  return 0;
}

$clang -emit-llvm -fsanitize=address -c -g sample.c

$lli sample.bc
Stack dump:
0.  Program arguments: lli sample.bc
0  lli                      0x000000010c112d9c llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  lli                      0x000000010c11319e SignalHandler(int) + 192
2  libsystem_platform.dylib 0x00007fff603e2b3d _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2680280288
4  lli                      0x000000010be3ff74 llvm::ExecutionEngine::runStaticConstructorsDestructors(llvm::Module&, bool) + 310
5  lli                      0x000000010beac842 llvm::MCJIT::runStaticConstructorsDestructors(bool) + 388
6  lli                      0x000000010bb715c6 main + 8866
7  libdyld.dylib            0x00007fff601f7ed9 start + 1
Segmentation fault: 11
David Tex
  • 87
  • 6
  • 1
    Try running with `LD_PRELOAD=libasan.so.VER`. If it still fails with latest clang you can report bug in https://github.com/google/sanitizers/issues/ – yugr Jan 12 '19 at 06:20
  • @yugr : thank you, It's solved my problem.but I'm a little confused, why clang will not link this automatically ? and libasan is not available in llvm package in MacOS, I did use it from GCC. `DYLD_INSERT_LIBRARIES=/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/libasan.dylib lli sample.bc` do you know why it's not available in llvm ? – David Tex Jan 13 '19 at 09:20
  • 1
    Hm, linking in GCC runtime may not be reliable so better stick with LLVM runtime. It seems to have a different name, can you search for library which looks like "clang_rt.asan.XXX" or something like this? I've added an answer in case you decide to +1 it. – yugr Jan 13 '19 at 11:21

1 Answers1

1

Sanitized code requires special runtime support which is implemented in Asan runtime library. lli does not load this library by default (because users normally don't need it) so you need to request it explicitly via LD_PRELOAD=libasan.so.VER. Note libasan.so is GCC convention, for Clang you may need something like libclang_rt.asan.XXX. You can determine full library paths via

GCC_ASAN_PRELOAD=$(gcc -print-file-name=libasan.so)
CLANG_ASAN_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so)
yugr
  • 19,769
  • 3
  • 51
  • 96