Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1 | // Make sure that ASan works with SEH in both Clang and MSVC. MSVC uses a |
| 2 | // different EH personality depending on the -GS setting, so test both -GS+ and |
| 3 | // -GS-. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 4 | // |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 5 | // RUN: cl -c %s -Fo%t.obj -DCOMPILE_SEH |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 6 | // RUN: %clangxx_asan -o %t.exe %s %t.obj |
| 7 | // RUN: %run %t.exe |
| 8 | // |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 9 | // RUN: cl -GS- -c %s -Fo%t.obj -DCOMPILE_SEH |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 10 | // RUN: %clangxx_asan -o %t.exe %s %t.obj |
| 11 | // RUN: %run %t.exe |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 12 | // |
| 13 | // RUN: %clang_cl_asan %s -DCOMPILE_SEH -Fe%t.exe |
| 14 | // RUN: %run %t.exe |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 15 | |
| 16 | #include <windows.h> |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | // Should just "#include <sanitizer/asan_interface.h>" when C++ exceptions are |
| 21 | // supported and we don't need to use CL. |
| 22 | extern "C" bool __asan_address_is_poisoned(void *p); |
| 23 | |
| 24 | void ThrowAndCatch(); |
| 25 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 26 | #if defined(COMPILE_SEH) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | __declspec(noinline) |
| 28 | void Throw() { |
| 29 | int local, zero = 0; |
| 30 | fprintf(stderr, "Throw: %p\n", &local); |
| 31 | local = 5 / zero; |
| 32 | } |
| 33 | |
| 34 | __declspec(noinline) |
| 35 | void ThrowAndCatch() { |
| 36 | int local; |
| 37 | __try { |
| 38 | Throw(); |
| 39 | } __except(EXCEPTION_EXECUTE_HANDLER) { |
| 40 | fprintf(stderr, "__except: %p\n", &local); |
| 41 | } |
| 42 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 43 | #endif |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 44 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 45 | #if defined(__clang__) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 46 | int main() { |
| 47 | char x[32]; |
| 48 | fprintf(stderr, "Before: %p poisoned: %d\n", &x, |
| 49 | __asan_address_is_poisoned(x + 32)); |
| 50 | assert(__asan_address_is_poisoned(x + 32)); |
| 51 | ThrowAndCatch(); |
| 52 | fprintf(stderr, "After: %p poisoned: %d\n", &x, |
| 53 | __asan_address_is_poisoned(x + 32)); |
| 54 | // FIXME: Invert this assertion once we fix |
| 55 | // https://code.google.com/p/address-sanitizer/issues/detail?id=258 |
| 56 | assert(!__asan_address_is_poisoned(x + 32)); |
| 57 | } |
| 58 | #endif |