2015-08-28 15:33:40 +00:00
|
|
|
//===-- asan_win_dll_thunk.cc ---------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// This file defines a family of thunks that should be statically linked into
|
|
|
|
// the DLLs that have ASan instrumentation in order to delegate the calls to the
|
|
|
|
// shared runtime that lives in the main binary.
|
2017-10-07 00:16:47 +00:00
|
|
|
// See https://github.com/google/sanitizers/issues/209 for the details.
|
2015-08-28 15:33:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
#ifdef SANITIZER_DLL_THUNK
|
2015-08-28 15:33:40 +00:00
|
|
|
#include "asan_init_version.h"
|
2017-04-10 11:32:00 +00:00
|
|
|
#include "interception/interception.h"
|
2018-12-28 15:30:48 +00:00
|
|
|
#include "sanitizer_common/sanitizer_win_defs.h"
|
|
|
|
#include "sanitizer_common/sanitizer_win_dll_thunk.h"
|
2017-10-07 00:16:47 +00:00
|
|
|
#include "sanitizer_common/sanitizer_platform_interceptors.h"
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
// ASan own interface functions.
|
|
|
|
#define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name)
|
|
|
|
#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
|
|
|
|
#include "asan_interface.inc"
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
// Memory allocation functions.
|
|
|
|
INTERCEPT_WRAP_V_W(free)
|
|
|
|
INTERCEPT_WRAP_V_W(_free_base)
|
|
|
|
INTERCEPT_WRAP_V_WW(_free_dbg)
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_WRAP_W_W(malloc)
|
|
|
|
INTERCEPT_WRAP_W_W(_malloc_base)
|
|
|
|
INTERCEPT_WRAP_W_WWWW(_malloc_dbg)
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_WRAP_W_WW(calloc)
|
|
|
|
INTERCEPT_WRAP_W_WW(_calloc_base)
|
|
|
|
INTERCEPT_WRAP_W_WWWWW(_calloc_dbg)
|
|
|
|
INTERCEPT_WRAP_W_WWW(_calloc_impl)
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_WRAP_W_WW(realloc)
|
|
|
|
INTERCEPT_WRAP_W_WW(_realloc_base)
|
|
|
|
INTERCEPT_WRAP_W_WWW(_realloc_dbg)
|
|
|
|
INTERCEPT_WRAP_W_WWW(_recalloc)
|
|
|
|
INTERCEPT_WRAP_W_WWW(_recalloc_base)
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_WRAP_W_W(_msize)
|
|
|
|
INTERCEPT_WRAP_W_W(_expand)
|
|
|
|
INTERCEPT_WRAP_W_W(_expand_dbg)
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
// TODO(timurrrr): Might want to add support for _aligned_* allocation
|
|
|
|
// functions to detect a bit more bugs. Those functions seem to wrap malloc().
|
|
|
|
|
|
|
|
// TODO(timurrrr): Do we need to add _Crt* stuff here? (see asan_malloc_win.cc).
|
|
|
|
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(atoi);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(atol);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(frexp);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(longjmp);
|
2017-10-07 00:16:47 +00:00
|
|
|
#if SANITIZER_INTERCEPT_MEMCHR
|
2015-08-28 15:33:40 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(memchr);
|
2017-10-07 00:16:47 +00:00
|
|
|
#endif
|
2015-08-28 15:33:40 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(memcmp);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(memcpy);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(memmove);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(memset);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strcat); // NOLINT
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strchr);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strcmp);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strcpy); // NOLINT
|
2017-04-10 11:32:00 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strcspn);
|
2017-10-07 00:16:47 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strdup);
|
2015-08-28 15:33:40 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strlen);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strncat);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strncmp);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strncpy);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strnlen);
|
2017-04-10 11:32:00 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strpbrk);
|
2017-10-07 00:16:47 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strrchr);
|
2017-04-10 11:32:00 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strspn);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strstr);
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strtok);
|
2015-08-28 15:33:40 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(strtol);
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(wcslen);
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_LIBRARY_FUNCTION(wcsnlen);
|
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
INTERCEPT_LIBRARY_FUNCTION(__C_specific_handler);
|
|
|
|
#else
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
// Windows specific functions not included in asan_interface.inc.
|
2018-12-28 15:30:48 +00:00
|
|
|
INTERCEPT_WRAP_W_V(__asan_should_detect_stack_use_after_return)
|
|
|
|
INTERCEPT_WRAP_W_V(__asan_get_shadow_memory_dynamic_address)
|
|
|
|
INTERCEPT_WRAP_W_W(__asan_unhandled_exception_filter)
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
int __asan_option_detect_stack_use_after_return;
|
|
|
|
uptr __asan_shadow_memory_dynamic_address;
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
static int asan_dll_thunk_init() {
|
|
|
|
typedef void (*fntype)();
|
|
|
|
static fntype fn = 0;
|
|
|
|
// asan_dll_thunk_init is expected to be called by only one thread.
|
|
|
|
if (fn) return 0;
|
|
|
|
|
|
|
|
// Ensure all interception was executed.
|
|
|
|
__dll_thunk_init();
|
|
|
|
|
|
|
|
fn = (fntype) dllThunkGetRealAddrOrDie("__asan_init");
|
|
|
|
fn();
|
|
|
|
__asan_option_detect_stack_use_after_return =
|
|
|
|
(__asan_should_detect_stack_use_after_return() != 0);
|
|
|
|
__asan_shadow_memory_dynamic_address =
|
|
|
|
(uptr)__asan_get_shadow_memory_dynamic_address();
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
#ifndef _WIN64
|
2015-08-28 15:33:40 +00:00
|
|
|
INTERCEPT_FUNCTION(_except_handler4);
|
2017-10-07 00:16:47 +00:00
|
|
|
#endif
|
2018-12-28 15:30:48 +00:00
|
|
|
// In DLLs, the callbacks are expected to return 0,
|
|
|
|
// otherwise CRT initialization fails.
|
|
|
|
return 0;
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
#pragma section(".CRT$XIB", long, read) // NOLINT
|
|
|
|
__declspec(allocate(".CRT$XIB")) int (*__asan_preinit)() = asan_dll_thunk_init;
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
static void WINAPI asan_thread_init(void *mod, unsigned long reason,
|
|
|
|
void *reserved) {
|
|
|
|
if (reason == /*DLL_PROCESS_ATTACH=*/1) asan_dll_thunk_init();
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
#pragma section(".CRT$XLAB", long, read) // NOLINT
|
|
|
|
__declspec(allocate(".CRT$XLAB")) void (WINAPI *__asan_tls_init)(void *,
|
|
|
|
unsigned long, void *) = asan_thread_init;
|
|
|
|
|
|
|
|
WIN_FORCE_LINK(__asan_dso_reg_hook)
|
|
|
|
|
|
|
|
#endif // SANITIZER_DLL_THUNK
|