xref: /llvm-project/compiler-rt/test/asan/TestCases/throw_call_test.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
1*673dc3d4SNico Weber // RUN: %clangxx_asan %s -o %t && %run %t
2*673dc3d4SNico Weber // http://code.google.com/p/address-sanitizer/issues/detail?id=147 (not fixed).
3*673dc3d4SNico Weber // BROKEN: %clangxx_asan %s -o %t -static-libstdc++ && %run %t
4*673dc3d4SNico Weber 
5*673dc3d4SNico Weber #include <stdio.h>
6*673dc3d4SNico Weber static volatile int zero = 0;
pretend_to_do_something(void * x)7*673dc3d4SNico Weber inline void pretend_to_do_something(void *x) {
8*673dc3d4SNico Weber   __asm__ __volatile__("" : : "r" (x) : "memory");
9*673dc3d4SNico Weber }
10*673dc3d4SNico Weber 
11*673dc3d4SNico Weber __attribute__((noinline, no_sanitize_address))
ReallyThrow()12*673dc3d4SNico Weber void ReallyThrow() {
13*673dc3d4SNico Weber   fprintf(stderr, "ReallyThrow\n");
14*673dc3d4SNico Weber   if (zero == 0)
15*673dc3d4SNico Weber     throw 42;
16*673dc3d4SNico Weber }
17*673dc3d4SNico Weber 
18*673dc3d4SNico Weber __attribute__((noinline))
Throw()19*673dc3d4SNico Weber void Throw() {
20*673dc3d4SNico Weber   int a, b, c, d, e, f, g, h;
21*673dc3d4SNico Weber   pretend_to_do_something(&a);
22*673dc3d4SNico Weber   pretend_to_do_something(&b);
23*673dc3d4SNico Weber   pretend_to_do_something(&c);
24*673dc3d4SNico Weber   pretend_to_do_something(&d);
25*673dc3d4SNico Weber   pretend_to_do_something(&e);
26*673dc3d4SNico Weber   pretend_to_do_something(&f);
27*673dc3d4SNico Weber   pretend_to_do_something(&g);
28*673dc3d4SNico Weber   pretend_to_do_something(&h);
29*673dc3d4SNico Weber   fprintf(stderr, "Throw stack = %p\n", &a);
30*673dc3d4SNico Weber   ReallyThrow();
31*673dc3d4SNico Weber }
32*673dc3d4SNico Weber 
33*673dc3d4SNico Weber __attribute__((noinline))
CheckStack()34*673dc3d4SNico Weber void CheckStack() {
35*673dc3d4SNico Weber   int ar[100];
36*673dc3d4SNico Weber   pretend_to_do_something(ar);
37*673dc3d4SNico Weber   fprintf(stderr, "CheckStack stack = %p, %p\n", ar, ar + 100);
38*673dc3d4SNico Weber   for (int i = 0; i < 100; i++)
39*673dc3d4SNico Weber     ar[i] = i;
40*673dc3d4SNico Weber }
41*673dc3d4SNico Weber 
main(int argc,char ** argv)42*673dc3d4SNico Weber int main(int argc, char** argv) {
43*673dc3d4SNico Weber   try {
44*673dc3d4SNico Weber     Throw();
45*673dc3d4SNico Weber   } catch(int a) {
46*673dc3d4SNico Weber     fprintf(stderr, "a = %d\n", a);
47*673dc3d4SNico Weber   }
48*673dc3d4SNico Weber   CheckStack();
49*673dc3d4SNico Weber }
50