From 05756947290e5a539991c6ae08dd09fd0c62fd67 Mon Sep 17 00:00:00 2001 From: Timur Iskhodzhanov Date: Tue, 5 Aug 2014 13:26:26 +0000 Subject: [PATCH] [ASan/Win] Handle SEH exceptions even with -GS This is a follow-up to r213654, r213656, r213667 and r213668. llvm-svn: 214861 --- compiler-rt/lib/asan/asan_dll_thunk.cc | 11 ++++++++++- compiler-rt/lib/asan/asan_interceptors.cc | 7 +++++++ compiler-rt/test/asan/TestCases/Windows/dll_seh.cc | 6 ++++++ compiler-rt/test/asan/TestCases/Windows/seh.cc | 6 +++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/asan/asan_dll_thunk.cc b/compiler-rt/lib/asan/asan_dll_thunk.cc index 63b6e91..0d81592b 100644 --- a/compiler-rt/lib/asan/asan_dll_thunk.cc +++ b/compiler-rt/lib/asan/asan_dll_thunk.cc @@ -75,7 +75,7 @@ struct FunctionInterceptor<0> { // Special case of hooks -- ASan own interface functions. Those are only called // after __asan_init, thus an empty implementation is sufficient. #define INTERFACE_FUNCTION(name) \ - extern "C" void name() { \ + extern "C" __declspec(noinline) void name() { \ volatile int prevent_icf = (__LINE__ << 8); (void)prevent_icf; \ __debugbreak(); \ } \ @@ -325,6 +325,14 @@ WRAP_W_W(_expand_dbg) INTERCEPT_LIBRARY_FUNCTION(atoi); INTERCEPT_LIBRARY_FUNCTION(atol); INTERCEPT_LIBRARY_FUNCTION(_except_handler3); + +// _except_handler4 checks -GS cookie which is different for each module, so we +// can't use INTERCEPT_LIBRARY_FUNCTION(_except_handler4). +INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) { + __asan_handle_no_return(); + return REAL(_except_handler4)(a, b, c, d); +} + INTERCEPT_LIBRARY_FUNCTION(frexp); INTERCEPT_LIBRARY_FUNCTION(longjmp); INTERCEPT_LIBRARY_FUNCTION(memchr); @@ -348,6 +356,7 @@ INTERCEPT_LIBRARY_FUNCTION(wcslen); // is defined. void InterceptHooks() { INTERCEPT_HOOKS(); + INTERCEPT_FUNCTION(_except_handler4); } // We want to call __asan_init before C/C++ initializers/constructors are diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index dd61d6b..95674a8 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cc +++ b/compiler-rt/lib/asan/asan_interceptors.cc @@ -308,6 +308,12 @@ INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) { __asan_handle_no_return(); return REAL(_except_handler3)(a, b, c, d); } + +INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) { + CHECK(REAL(_except_handler4)); + __asan_handle_no_return(); + return REAL(_except_handler4)(a, b, c, d); +} #endif #if ASAN_INTERCEPT_MLOCKX @@ -745,6 +751,7 @@ void InitializeWindowsInterceptors() { ASAN_INTERCEPT_FUNC(CreateThread); ASAN_INTERCEPT_FUNC(RaiseException); ASAN_INTERCEPT_FUNC(_except_handler3); + ASAN_INTERCEPT_FUNC(_except_handler4); } } // namespace __asan diff --git a/compiler-rt/test/asan/TestCases/Windows/dll_seh.cc b/compiler-rt/test/asan/TestCases/Windows/dll_seh.cc index 54fd9b0..6e4c724 100644 --- a/compiler-rt/test/asan/TestCases/Windows/dll_seh.cc +++ b/compiler-rt/test/asan/TestCases/Windows/dll_seh.cc @@ -4,6 +4,12 @@ // build a large project using "clang-cl -fallback -fsanitize=address". // // RUN: %clang_cl_asan -O0 %p/dll_host.cc -Fe%t +// +// Check both -GS and -GS- builds: +// RUN: cl -LD -c %s -Fo%t.obj +// RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll %t.obj +// RUN: %run %t %t.dll +// // RUN: cl -LD -GS- -c %s -Fo%t.obj // RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll %t.obj // RUN: %run %t %t.dll diff --git a/compiler-rt/test/asan/TestCases/Windows/seh.cc b/compiler-rt/test/asan/TestCases/Windows/seh.cc index 34919d0..50cf6dd 100644 --- a/compiler-rt/test/asan/TestCases/Windows/seh.cc +++ b/compiler-rt/test/asan/TestCases/Windows/seh.cc @@ -3,7 +3,11 @@ // the rest is built with Clang. This represents the typical scenario when we // build a large project using "clang-cl -fallback -fsanitize=address". // -// FIXME: Investigate why -GS- is required. +// Check both -GS and -GS- builds: +// RUN: cl -c %s -Fo%t.obj +// RUN: %clangxx_asan -o %t.exe %s %t.obj +// RUN: %run %t.exe +// // RUN: cl -GS- -c %s -Fo%t.obj // RUN: %clangxx_asan -o %t.exe %s %t.obj // RUN: %run %t.exe -- 2.7.4