10b57cec5SDimitry Andric //===-- interception_linux.h ------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // Windows-specific interception methods. 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #if SANITIZER_WINDOWS 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #if !defined(INCLUDED_FROM_INTERCEPTION_LIB) 170b57cec5SDimitry Andric # error "interception_win.h should be included from interception library only" 180b57cec5SDimitry Andric #endif 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #ifndef INTERCEPTION_WIN_H 210b57cec5SDimitry Andric #define INTERCEPTION_WIN_H 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric namespace __interception { 240b57cec5SDimitry Andric // All the functions in the OverrideFunction() family return true on success, 250b57cec5SDimitry Andric // false on failure (including "couldn't find the function"). 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // Overrides a function by its address. 280b57cec5SDimitry Andric bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func = 0); 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric // Overrides a function in a system DLL or DLL CRT by its exported name. 310b57cec5SDimitry Andric bool OverrideFunction(const char *name, uptr new_func, uptr *orig_old_func = 0); 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric // Windows-only replacement for GetProcAddress. Useful for some sanitizers. 340b57cec5SDimitry Andric uptr InternalGetProcAddress(void *module, const char *func_name); 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // Overrides a function only when it is called from a specific DLL. For example, 370b57cec5SDimitry Andric // this is used to override calls to HeapAlloc/HeapFree from ucrtbase without 380b57cec5SDimitry Andric // affecting other third party libraries. 390b57cec5SDimitry Andric bool OverrideImportedFunction(const char *module_to_patch, 400b57cec5SDimitry Andric const char *imported_module, 410b57cec5SDimitry Andric const char *function_name, uptr new_function, 420b57cec5SDimitry Andric uptr *orig_old_func); 430b57cec5SDimitry Andric 44*06c3fb27SDimitry Andric // Sets a callback to be used for reporting errors by interception_win. The 45*06c3fb27SDimitry Andric // callback will be called with printf-like arguments. Intended to be used with 46*06c3fb27SDimitry Andric // __sanitizer::Report. Pass nullptr to disable error reporting (default). 47*06c3fb27SDimitry Andric void SetErrorReportCallback(void (*callback)(const char *format, ...)); 48*06c3fb27SDimitry Andric 490b57cec5SDimitry Andric #if !SANITIZER_WINDOWS64 500b57cec5SDimitry Andric // Exposed for unittests 510b57cec5SDimitry Andric bool OverrideFunctionWithDetour( 520b57cec5SDimitry Andric uptr old_func, uptr new_func, uptr *orig_old_func); 530b57cec5SDimitry Andric #endif 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Exposed for unittests 560b57cec5SDimitry Andric bool OverrideFunctionWithRedirectJump( 570b57cec5SDimitry Andric uptr old_func, uptr new_func, uptr *orig_old_func); 580b57cec5SDimitry Andric bool OverrideFunctionWithHotPatch( 590b57cec5SDimitry Andric uptr old_func, uptr new_func, uptr *orig_old_func); 600b57cec5SDimitry Andric bool OverrideFunctionWithTrampoline( 610b57cec5SDimitry Andric uptr old_func, uptr new_func, uptr *orig_old_func); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // Exposed for unittests 640b57cec5SDimitry Andric void TestOnlyReleaseTrampolineRegions(); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric } // namespace __interception 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric #if defined(INTERCEPTION_DYNAMIC_CRT) 690b57cec5SDimitry Andric #define INTERCEPT_FUNCTION_WIN(func) \ 700b57cec5SDimitry Andric ::__interception::OverrideFunction(#func, \ 710b57cec5SDimitry Andric (::__interception::uptr)WRAP(func), \ 720b57cec5SDimitry Andric (::__interception::uptr *)&REAL(func)) 730b57cec5SDimitry Andric #else 740b57cec5SDimitry Andric #define INTERCEPT_FUNCTION_WIN(func) \ 750b57cec5SDimitry Andric ::__interception::OverrideFunction((::__interception::uptr)func, \ 760b57cec5SDimitry Andric (::__interception::uptr)WRAP(func), \ 770b57cec5SDimitry Andric (::__interception::uptr *)&REAL(func)) 780b57cec5SDimitry Andric #endif 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric #define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func) 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric #define INTERCEPT_FUNCTION_DLLIMPORT(user_dll, provider_dll, func) \ 830b57cec5SDimitry Andric ::__interception::OverrideImportedFunction( \ 840b57cec5SDimitry Andric user_dll, provider_dll, #func, (::__interception::uptr)WRAP(func), \ 850b57cec5SDimitry Andric (::__interception::uptr *)&REAL(func)) 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric #endif // INTERCEPTION_WIN_H 880b57cec5SDimitry Andric #endif // SANITIZER_WINDOWS 89