gcc/libsanitizer/sanitizer_common/sanitizer_win_dll_thunk.h

182 lines
11 KiB
C
Raw Normal View History

ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins... * ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins, store max (log2 (align), 0) into uchar field instead of align into uptr field. (ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins, store uchar 0 field instead of uptr 0 field. (instrument_nonnull_return): Use _v1 suffixed nonnull return builtin, instead of passing one address of struct with 2 locations pass two addresses of structs with 1 location each. * sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed. (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins. * c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword from expected output regexps. * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-3.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-5.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-6.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-8.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-9.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-10.c: Likewise. * g++.dg/ubsan/float-cast-overflow-bf.C: Likewise. * gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise. * g++.dg/asan/default-options-1.C (__asan_default_options): Add used attribute. * g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2 in the environment. * All source files: Merge from upstream 315899. * asan/Makefile.am (nodist_saninclude_HEADERS): Add include/sanitizer/tsan_interface.h. * asan/libtool-version: Bump the libasan SONAME. * lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc. (lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc. * sanitizer_common/Makefile.am (sanitizer_common_files): Add sancov_flags.cc, sanitizer_allocator_checks.cc, sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc, sanitizer_file.cc, sanitizer_mac_libcdep.cc and sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc and sanitizer_coverage_mapping_libcdep.cc. * tsan/Makefile.am (tsan_files): Add tsan_external.cc. * ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1. (ubsan_files): Add ubsan_init_standalone.cc and ubsan_signals_standalone.cc. * ubsan/libtool-version: Bump the libubsan SONAME. * asan/Makefile.in: Regenerate. * lsan/Makefile.in: Regenerate. * sanitizer_common/Makefile.in: Regenerate. * tsan/Makefile.in: Regenerate. * ubsan/Makefile.in: Regenerate. From-SVN: r253887
2017-10-19 13:23:59 +02:00
//===-- sanitizer_win_dll_thunk.h -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins... * ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins, store max (log2 (align), 0) into uchar field instead of align into uptr field. (ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins, store uchar 0 field instead of uptr 0 field. (instrument_nonnull_return): Use _v1 suffixed nonnull return builtin, instead of passing one address of struct with 2 locations pass two addresses of structs with 1 location each. * sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed. (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins. * c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword from expected output regexps. * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-3.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-5.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-6.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-8.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-9.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-10.c: Likewise. * g++.dg/ubsan/float-cast-overflow-bf.C: Likewise. * gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise. * g++.dg/asan/default-options-1.C (__asan_default_options): Add used attribute. * g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2 in the environment. * All source files: Merge from upstream 315899. * asan/Makefile.am (nodist_saninclude_HEADERS): Add include/sanitizer/tsan_interface.h. * asan/libtool-version: Bump the libasan SONAME. * lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc. (lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc. * sanitizer_common/Makefile.am (sanitizer_common_files): Add sancov_flags.cc, sanitizer_allocator_checks.cc, sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc, sanitizer_file.cc, sanitizer_mac_libcdep.cc and sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc and sanitizer_coverage_mapping_libcdep.cc. * tsan/Makefile.am (tsan_files): Add tsan_external.cc. * ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1. (ubsan_files): Add ubsan_init_standalone.cc and ubsan_signals_standalone.cc. * ubsan/libtool-version: Bump the libubsan SONAME. * asan/Makefile.in: Regenerate. * lsan/Makefile.in: Regenerate. * sanitizer_common/Makefile.in: Regenerate. * tsan/Makefile.in: Regenerate. * ubsan/Makefile.in: Regenerate. From-SVN: r253887
2017-10-19 13:23:59 +02:00
//
//===----------------------------------------------------------------------===//
// This header provide helper macros to delegate calls to the shared runtime
// that lives in the main executable. It should be included to dll_thunks that
// will be linked to the dlls, when the sanitizer is a static library included
// in the main executable.
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_WIN_DLL_THUNK_H
#define SANITIZER_WIN_DLL_THUNK_H
#include "sanitizer_internal_defs.h"
namespace __sanitizer {
uptr dllThunkGetRealAddrOrDie(const char *name);
int dllThunkIntercept(const char* main_function, uptr dll_function);
int dllThunkInterceptWhenPossible(const char* main_function,
const char* default_function, uptr dll_function);
}
extern "C" int __dll_thunk_init();
// ----------------- Function interception helper macros -------------------- //
// Override dll_function with main_function from main executable.
#define INTERCEPT_OR_DIE(main_function, dll_function) \
static int intercept_##dll_function() { \
return __sanitizer::dllThunkIntercept(main_function, (__sanitizer::uptr) \
dll_function); \
} \
__pragma(section(".DLLTH$M", long, read)) \
__declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
intercept_##dll_function;
// Try to override dll_function with main_function from main executable.
// If main_function is not present, override dll_function with default_function.
#define INTERCEPT_WHEN_POSSIBLE(main_function, default_function, dll_function) \
static int intercept_##dll_function() { \
return __sanitizer::dllThunkInterceptWhenPossible(main_function, \
default_function, (__sanitizer::uptr)dll_function); \
} \
__pragma(section(".DLLTH$M", long, read)) \
__declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
intercept_##dll_function;
// -------------------- Function interception macros ------------------------ //
// Special case of hooks -- ASan own interface functions. Those are only called
// after __asan_init, thus an empty implementation is sufficient.
#define INTERCEPT_SANITIZER_FUNCTION(name) \
extern "C" __declspec(noinline) void name() { \
volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
static const char function_name[] = #name; \
for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
prevent_icf ^= *ptr; \
(void)prevent_icf; \
__debugbreak(); \
} \
INTERCEPT_OR_DIE(#name, name)
// Special case of hooks -- Weak functions, could be redefined in the main
// executable, but that is not necessary, so we shouldn't die if we can not find
// a reference. Instead, when the function is not present in the main executable
// we consider the default impl provided by asan library.
#define INTERCEPT_SANITIZER_WEAK_FUNCTION(name) \
extern "C" __declspec(noinline) void name() { \
volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
static const char function_name[] = #name; \
for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
prevent_icf ^= *ptr; \
(void)prevent_icf; \
__debugbreak(); \
} \
INTERCEPT_WHEN_POSSIBLE(#name, STRINGIFY(WEAK_EXPORT_NAME(name)), name)
// We can't define our own version of strlen etc. because that would lead to
// link-time or even type mismatch errors. Instead, we can declare a function
// just to be able to get its address. Me may miss the first few calls to the
// functions since it can be called before __dll_thunk_init, but that would lead
// to false negatives in the startup code before user's global initializers,
// which isn't a big deal.
#define INTERCEPT_LIBRARY_FUNCTION(name) \
extern "C" void name(); \
INTERCEPT_OR_DIE(WRAPPER_NAME(name), name)
// Use these macros for functions that could be called before __dll_thunk_init()
// is executed and don't lead to errors if defined (free, malloc, etc).
#define INTERCEPT_WRAP_V_V(name) \
extern "C" void name() { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
fn(); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_V_W(name) \
extern "C" void name(void *arg) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
fn(arg); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_V_WW(name) \
extern "C" void name(void *arg1, void *arg2) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
fn(arg1, arg2); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_V_WWW(name) \
extern "C" void name(void *arg1, void *arg2, void *arg3) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
fn(arg1, arg2, arg3); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_V(name) \
extern "C" void *name() { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_W(name) \
extern "C" void *name(void *arg) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_WW(name) \
extern "C" void *name(void *arg1, void *arg2) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg1, arg2); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_WWW(name) \
extern "C" void *name(void *arg1, void *arg2, void *arg3) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg1, arg2, arg3); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_WWWW(name) \
extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg1, arg2, arg3, arg4); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_WWWWW(name) \
extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
void *arg5) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg1, arg2, arg3, arg4, arg5); \
} \
INTERCEPT_OR_DIE(#name, name);
#define INTERCEPT_WRAP_W_WWWWWW(name) \
extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
void *arg5, void *arg6) { \
typedef decltype(name) *fntype; \
static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
} \
INTERCEPT_OR_DIE(#name, name);
#endif // SANITIZER_WIN_DLL_THUNK_H