ramosian-glider / sanitizer-issues

test
0 stars 0 forks source link

-faddress-sanitizer causes program with large stack variable to segfault #98

Closed ramosian-glider closed 9 years ago

ramosian-glider commented 9 years ago

Originally reported on Google Code with ID 98

The following program runs fine without -faddress-sanitizer, and segfaults with:

  $ cat > test.cpp
  int main(int argc, char* arv[])
  {
      volatile int arr[2000000] = {0};
      return 0;
  }
  $ clang++ -faddress-sanitizer -o test test.cpp
  $ ./test
  Segmentation Fault

In this case it segfaults 100% of the time.

I have attached a gdb session. I also found some curious behaviour that the segfault
becomes non-determininstic with an array around the size of ~1047273, segfaulting approximately
50% of the time (508/1000 runs). Changing the length of the array appears to modify
the chance of crashing, with larger lengths leading to a higher probability of segfaulting.

I observe this behaviour on both:

  $ clang --version
  clang version 3.1 (branches/release_31)
  Target: x86_64-pc-linux-gnu
  Thread model: posix

and trunk as of yesterday.

Reported by peter.waller on 2012-08-07 09:20:54


ramosian-glider commented 9 years ago
ASan increases the size of stack frames. In your case, the stack size is at least:

   0x0000000000406d4e <+14>:    sub    $0xf42590,%rsp

that is, about 16Mb, so probably you're just hitting the stack size limit.

Reported by samsonov@google.com on 2012-08-07 09:36:30

ramosian-glider commented 9 years ago
This happens because ASan adds redzones to the stack variables, which increases the
footprint. In order to make your program work with ASan you can increase the system
limit on the main thread stack:

$ ulimit -s 81920
$ ./test

If your program needs more stack space for the child threads, refer to pthread_attr_setstacksize().

Reported by ramosian.glider on 2012-08-07 09:45:48

ramosian-glider commented 9 years ago
Great, thanks for the explanation!

Reported by peter.waller on 2012-08-07 12:24:27

ramosian-glider commented 9 years ago
Adding Project:AddressSanitizer as part of GitHub migration.

Reported by ramosian.glider on 2015-07-30 09:12:59