xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/throw_catch.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O %s -o %t && %run %t
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot #include <assert.h>
4*7c3d14c8STreehugger Robot #include <stdio.h>
5*7c3d14c8STreehugger Robot #include <sanitizer/asan_interface.h>
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot __attribute__((noinline))
Throw()8*7c3d14c8STreehugger Robot void Throw() {
9*7c3d14c8STreehugger Robot   int local;
10*7c3d14c8STreehugger Robot   fprintf(stderr, "Throw:  %p\n", &local);
11*7c3d14c8STreehugger Robot   throw 1;
12*7c3d14c8STreehugger Robot }
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot __attribute__((noinline))
ThrowAndCatch()15*7c3d14c8STreehugger Robot void ThrowAndCatch() {
16*7c3d14c8STreehugger Robot   int local;
17*7c3d14c8STreehugger Robot   try {
18*7c3d14c8STreehugger Robot     Throw();
19*7c3d14c8STreehugger Robot   } catch(...) {
20*7c3d14c8STreehugger Robot     fprintf(stderr, "Catch:  %p\n", &local);
21*7c3d14c8STreehugger Robot   }
22*7c3d14c8STreehugger Robot }
23*7c3d14c8STreehugger Robot 
TestThrow()24*7c3d14c8STreehugger Robot void TestThrow() {
25*7c3d14c8STreehugger Robot   char x[32];
26*7c3d14c8STreehugger Robot   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
27*7c3d14c8STreehugger Robot           __asan_address_is_poisoned(x + 32));
28*7c3d14c8STreehugger Robot   assert(__asan_address_is_poisoned(x + 32));
29*7c3d14c8STreehugger Robot   ThrowAndCatch();
30*7c3d14c8STreehugger Robot   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
31*7c3d14c8STreehugger Robot           __asan_address_is_poisoned(x + 32));
32*7c3d14c8STreehugger Robot   // FIXME: Invert this assertion once we fix
33*7c3d14c8STreehugger Robot   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
34*7c3d14c8STreehugger Robot   // This assertion works only w/o UAR.
35*7c3d14c8STreehugger Robot   if (!__asan_get_current_fake_stack())
36*7c3d14c8STreehugger Robot     assert(!__asan_address_is_poisoned(x + 32));
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot 
TestThrowInline()39*7c3d14c8STreehugger Robot void TestThrowInline() {
40*7c3d14c8STreehugger Robot   char x[32];
41*7c3d14c8STreehugger Robot   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
42*7c3d14c8STreehugger Robot           __asan_address_is_poisoned(x + 32));
43*7c3d14c8STreehugger Robot   assert(__asan_address_is_poisoned(x + 32));
44*7c3d14c8STreehugger Robot   try {
45*7c3d14c8STreehugger Robot     Throw();
46*7c3d14c8STreehugger Robot   } catch(...) {
47*7c3d14c8STreehugger Robot     fprintf(stderr, "Catch\n");
48*7c3d14c8STreehugger Robot   }
49*7c3d14c8STreehugger Robot   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
50*7c3d14c8STreehugger Robot           __asan_address_is_poisoned(x + 32));
51*7c3d14c8STreehugger Robot   // FIXME: Invert this assertion once we fix
52*7c3d14c8STreehugger Robot   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
53*7c3d14c8STreehugger Robot   // This assertion works only w/o UAR.
54*7c3d14c8STreehugger Robot   if (!__asan_get_current_fake_stack())
55*7c3d14c8STreehugger Robot     assert(!__asan_address_is_poisoned(x + 32));
56*7c3d14c8STreehugger Robot }
57*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)58*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
59*7c3d14c8STreehugger Robot   TestThrowInline();
60*7c3d14c8STreehugger Robot   TestThrow();
61*7c3d14c8STreehugger Robot }
62