blob: 4cb0c55bc773026a69b6665a19be2270b5a82b62 [file] [log] [blame]
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001// 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 Hines6d186232014-11-26 17:56:19 -08004//
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08005// RUN: cl -c %s -Fo%t.obj -DCOMPILE_SEH
Stephen Hines6d186232014-11-26 17:56:19 -08006// RUN: %clangxx_asan -o %t.exe %s %t.obj
7// RUN: %run %t.exe
8//
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08009// RUN: cl -GS- -c %s -Fo%t.obj -DCOMPILE_SEH
Stephen Hines6d186232014-11-26 17:56:19 -080010// RUN: %clangxx_asan -o %t.exe %s %t.obj
11// RUN: %run %t.exe
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080012//
13// RUN: %clang_cl_asan %s -DCOMPILE_SEH -Fe%t.exe
14// RUN: %run %t.exe
Stephen Hines6d186232014-11-26 17:56:19 -080015
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.
22extern "C" bool __asan_address_is_poisoned(void *p);
23
24void ThrowAndCatch();
25
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080026#if defined(COMPILE_SEH)
Stephen Hines6d186232014-11-26 17:56:19 -080027__declspec(noinline)
28void Throw() {
29 int local, zero = 0;
30 fprintf(stderr, "Throw: %p\n", &local);
31 local = 5 / zero;
32}
33
34__declspec(noinline)
35void ThrowAndCatch() {
36 int local;
37 __try {
38 Throw();
39 } __except(EXCEPTION_EXECUTE_HANDLER) {
40 fprintf(stderr, "__except: %p\n", &local);
41 }
42}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080043#endif
Stephen Hines6d186232014-11-26 17:56:19 -080044
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080045#if defined(__clang__)
Stephen Hines6d186232014-11-26 17:56:19 -080046int 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