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