viralcode / address-sanitizer

Automatically exported from code.google.com/p/address-sanitizer
1 stars 0 forks source link

ASan instrumentation should work with -O0 #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

$ cat try.c
#include <stdio.h>
int main() { printf("Ok\n"); return(0); }

$ clang -o try -g3 -D__APPLE__ -faddress-sanitizer try.c
$ ./try
Ok==33560== 
CHECK failed: summary at asan_thread_registry.cc:99, pthread_self=0x7fff70989cc0
Segmentation fault

$ gdb ./try
(gdb) r
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x000000010000b3af in __asan::AsanThread::ClearShadowForThreadStack 
(this=0x10242a9a0) at asan_thread.cc:51
#2  0x000000010000b6b1 in __asan::AsanThread::~AsanThread (this=0x10242a9a0) at 
asan_thread.cc:45
#3  0x000000010000c3d0 in __asan::AsanThreadRegistry::~AsanThreadRegistry 
(this=0x10042a9a0) at asan_thread_registry.h:29
#4  0x000000010000fe15 in __tcf_0 () at asan_thread_registry.cc:24
#5  0x00007fff8640a374 in __cxa_finalize ()
#6  0x00007fff8640a28c in exit ()
#7  0x0000000100000f53 in start () at asan_thread_registry.h:28

(gdb) up
(gdb) p *this
$1 = {
  static kInvalidTid = -1, 
  summary_ = 0x0, 
  start_routine_ = 0, 
  arg_ = 0x0, 
  stack_top_ = 0, 
  stack_bottom_ = 0, 
  fake_stack_ = {
    static kMinStackFrameSizeLog = 9, 
    static kMaxStackFrameSizeLog = 16, 
    static kMaxStackMallocSize = 65536, 
    static kNumberOfSizeClasses = 8, 
    stack_size_ = 0, 
    alive_ = false, 
    allocated_size_classes_ = {0, 0, 0, 0, 0, 0, 0, 0}, 
...

$ clang --version
clang version 3.1 (trunk 144800)
Target: x86_64-apple-darwin10.8.0
Thread model: posix

The attached patch fixed it for me

Original issue reported on code.google.com by reini.urban on 22 Nov 2011 at 2:21

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by konstant...@gmail.com on 22 Nov 2011 at 2:36

GoogleCodeExporter commented 9 years ago
This happens because ASan does not currently work with -O0. In fact the module 
remains uninstrumented, thus __asan_init() is not called and the runtime 
library is in inconsistent state. This problem cannot be fixed without calling 
__asan_init(), so the proposed patch is invalid.

The short-term workaround is to pass an additional -O1 option to the compiler:
  $ clang try.c -faddress-sanitizer -o try -O1
  $ ./try
  Ok

A proper fix on the ASan side is to allow performing the instrumentation with 
-O0. ASan instrumentation is conceptually different from code optimization, so 
this is correct.

Original comment by ramosian.glider@gmail.com on 22 Nov 2011 at 8:18

GoogleCodeExporter commented 9 years ago
Thanks. This explains why I found no single error in the Perl testsuite and a 
fair number of CPAN modules so far. Too good to be true. No single SIGSEGV. 
With -O1 it works fine

Original comment by reini.urban on 22 Nov 2011 at 2:18

GoogleCodeExporter commented 9 years ago
Attached is a patch for Clang that fixes the problem with -O0 (this is not a 
replacement for llvm/clang.patch, it should be applied separately)

Once someone on the LLVM-dev approves it, we'll commit it into Clang.

Original comment by ramosian.glider@gmail.com on 23 Nov 2011 at 8:11

Attachments:

GoogleCodeExporter commented 9 years ago
Works for me, thanks. At least for the perl Configure probes.

Got now into other problems unrelated to this patch (fails with and without) to 
be handled in a separate ticket.

Original comment by reini.urban on 23 Nov 2011 at 4:41

GoogleCodeExporter commented 9 years ago
The O0 patch has landed. 
The build instructions updated. (note, they are unstable, nothing is guaranteed 
to work until we are done integrating with llvm). 

Original comment by konstant...@gmail.com on 1 Dec 2011 at 5:56