]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/test/asan/TestCases/Windows/seh.cc
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / test / asan / TestCases / Windows / seh.cc
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-.
4 //
5 // RUN: cl -c %s -Fo%t.obj -DCOMPILE_SEH
6 // RUN: %clangxx_asan -o %t.exe %s %t.obj
7 // RUN: %run %t.exe
8 //
9 // RUN: cl -GS- -c %s -Fo%t.obj -DCOMPILE_SEH
10 // RUN: %clangxx_asan -o %t.exe %s %t.obj
11 // RUN: %run %t.exe
12 //
13 // RUN: %clang_cl_asan %s -DCOMPILE_SEH -Fe%t.exe
14 // RUN: %run %t.exe
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
26 #if defined(COMPILE_SEH)
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 }
43 #endif
44
45 #if defined(__clang__)
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