Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/injectsu/detours.h
Views: 11780
//////////////////////////////////////////////////////////////////////////////1//2// Core Detours Functionality (detours.h of detours.lib)3//4// Microsoft Research Detours Package, Version 2.1.5//6// Copyright (c) Microsoft Corporation. All rights reserved.7//89#pragma once10#ifndef _DETOURS_H_11#define _DETOURS_H_1213#define DETOURS_VERSION 20100 // 2.1.01415//////////////////////////////////////////////////////////////////////////////16//1718#if (_MSC_VER < 1299)19typedef LONG LONG_PTR;20typedef ULONG ULONG_PTR;21#endif2223#ifndef __in_z24#define __in_z25#endif2627//////////////////////////////////////////////////////////////////////////////28//29#ifndef GUID_DEFINED30#define GUID_DEFINED31typedef struct _GUID32{33DWORD Data1;34WORD Data2;35WORD Data3;36BYTE Data4[ 8 ];37} GUID;3839#ifdef INITGUID40#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \41const GUID name \42= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }43#else44#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \45const GUID name46#endif // INITGUID47#endif // !GUID_DEFINED4849#if defined(__cplusplus)50#ifndef _REFGUID_DEFINED51#define _REFGUID_DEFINED52#define REFGUID const GUID &53#endif // !_REFGUID_DEFINED54#else // !__cplusplus55#ifndef _REFGUID_DEFINED56#define _REFGUID_DEFINED57#define REFGUID const GUID * const58#endif // !_REFGUID_DEFINED59#endif // !__cplusplus6061//62//////////////////////////////////////////////////////////////////////////////6364#ifdef __cplusplus65extern "C" {66#endif // __cplusplus6768/////////////////////////////////////////////////// Instruction Target Macros.69//70#define DETOUR_INSTRUCTION_TARGET_NONE ((PVOID)0)71#define DETOUR_INSTRUCTION_TARGET_DYNAMIC ((PVOID)(LONG_PTR)-1)72#define DETOUR_SECTION_HEADER_SIGNATURE 0x00727444 // "Dtr\0"7374extern const GUID DETOUR_EXE_RESTORE_GUID;7576#define DETOUR_TRAMPOLINE_SIGNATURE 0x21727444 // Dtr!77typedef struct _DETOUR_TRAMPOLINE DETOUR_TRAMPOLINE, *PDETOUR_TRAMPOLINE;7879/////////////////////////////////////////////////////////// Binary Structures.80//81#pragma pack(push, 8)82typedef struct _DETOUR_SECTION_HEADER83{84DWORD cbHeaderSize;85DWORD nSignature;86DWORD nDataOffset;87DWORD cbDataSize;8889DWORD nOriginalImportVirtualAddress;90DWORD nOriginalImportSize;91DWORD nOriginalBoundImportVirtualAddress;92DWORD nOriginalBoundImportSize;9394DWORD nOriginalIatVirtualAddress;95DWORD nOriginalIatSize;96DWORD nOriginalSizeOfImage;97DWORD cbPrePE;9899DWORD nOriginalClrFlags;100DWORD reserved1;101DWORD reserved2;102DWORD reserved3;103104// Followed by cbPrePE bytes of data.105} DETOUR_SECTION_HEADER, *PDETOUR_SECTION_HEADER;106107typedef struct _DETOUR_SECTION_RECORD108{109DWORD cbBytes;110DWORD nReserved;111GUID guid;112} DETOUR_SECTION_RECORD, *PDETOUR_SECTION_RECORD;113114typedef struct _DETOUR_CLR_HEADER115{116// Header versioning117ULONG cb;118USHORT MajorRuntimeVersion;119USHORT MinorRuntimeVersion;120121// Symbol table and startup information122IMAGE_DATA_DIRECTORY MetaData;123ULONG Flags;124125// Followed by the rest of the header.126} DETOUR_CLR_HEADER, *PDETOUR_CLR_HEADER;127128typedef struct _DETOUR_EXE_RESTORE129{130ULONG cb;131132PIMAGE_DOS_HEADER pidh;133PIMAGE_NT_HEADERS pinh;134PULONG pclrFlags;135DWORD impDirProt;136137IMAGE_DOS_HEADER idh;138IMAGE_NT_HEADERS inh;139ULONG clrFlags;140} DETOUR_EXE_RESTORE, *PDETOUR_EXE_RESTORE;141142#pragma pack(pop)143144#define DETOUR_SECTION_HEADER_DECLARE(cbSectionSize) \145{ \146sizeof(DETOUR_SECTION_HEADER),\147DETOUR_SECTION_HEADER_SIGNATURE,\148sizeof(DETOUR_SECTION_HEADER),\149(cbSectionSize),\150\1510,\1520,\1530,\1540,\155\1560,\1570,\1580,\1590,\160}161162///////////////////////////////////////////////////////////// Binary Typedefs.163//164typedef BOOL (CALLBACK *PF_DETOUR_BINARY_BYWAY_CALLBACK)(PVOID pContext,165PCHAR pszFile,166PCHAR *ppszOutFile);167168typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FILE_CALLBACK)(PVOID pContext,169PCHAR pszOrigFile,170PCHAR pszFile,171PCHAR *ppszOutFile);172173typedef BOOL (CALLBACK *PF_DETOUR_BINARY_SYMBOL_CALLBACK)(PVOID pContext,174ULONG nOrigOrdinal,175ULONG nOrdinal,176ULONG *pnOutOrdinal,177PCHAR pszOrigSymbol,178PCHAR pszSymbol,179PCHAR *ppszOutSymbol);180181typedef BOOL (CALLBACK *PF_DETOUR_BINARY_COMMIT_CALLBACK)(PVOID pContext);182183typedef BOOL (CALLBACK *PF_DETOUR_ENUMERATE_EXPORT_CALLBACK)(PVOID pContext,184ULONG nOrdinal,185PCHAR pszName,186PVOID pCode);187188typedef VOID * PDETOUR_BINARY;189typedef VOID * PDETOUR_LOADED_BINARY;190191//////////////////////////////////////////////////////////// Detours 2.1 APIs.192//193194LONG WINAPI DetourTransactionBegin();195LONG WINAPI DetourTransactionAbort();196LONG WINAPI DetourTransactionCommit();197LONG WINAPI DetourTransactionCommitEx(PVOID **pppFailedPointer);198199LONG WINAPI DetourUpdateThread(HANDLE hThread);200201LONG WINAPI DetourAttach(PVOID *ppPointer,202PVOID pDetour);203204LONG WINAPI DetourAttachEx(PVOID *ppPointer,205PVOID pDetour,206PDETOUR_TRAMPOLINE *ppRealTrampoline,207PVOID *ppRealTarget,208PVOID *ppRealDetour);209210LONG WINAPI DetourDetach(PVOID *ppPointer,211PVOID pDetour);212213VOID WINAPI DetourSetIgnoreTooSmall(BOOL fIgnore);214215////////////////////////////////////////////////////////////// Code Functions.216//217PVOID WINAPI DetourFindFunction(PCSTR pszModule, PCSTR pszFunction);218PVOID WINAPI DetourCodeFromPointer(PVOID pPointer, PVOID *ppGlobals);219220PVOID WINAPI DetourCopyInstruction(PVOID pDst, PVOID pSrc, PVOID *ppTarget);221PVOID WINAPI DetourCopyInstructionEx(PVOID pDst,222PVOID pSrc,223PVOID *ppTarget,224LONG *plExtra);225226///////////////////////////////////////////////////// Loaded Binary Functions.227//228HMODULE WINAPI DetourEnumerateModules(HMODULE hModuleLast);229PVOID WINAPI DetourGetEntryPoint(HMODULE hModule);230ULONG WINAPI DetourGetModuleSize(HMODULE hModule);231BOOL WINAPI DetourEnumerateExports(HMODULE hModule,232PVOID pContext,233PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport);234235PVOID WINAPI DetourFindPayload(HMODULE hModule, REFGUID rguid, DWORD *pcbData);236DWORD WINAPI DetourGetSizeOfPayloads(HMODULE hModule);237238///////////////////////////////////////////////// Persistent Binary Functions.239//240241PDETOUR_BINARY WINAPI DetourBinaryOpen(HANDLE hFile);242PVOID WINAPI DetourBinaryEnumeratePayloads(PDETOUR_BINARY pBinary,243GUID *pGuid,244DWORD *pcbData,245DWORD *pnIterator);246PVOID WINAPI DetourBinaryFindPayload(PDETOUR_BINARY pBinary,247REFGUID rguid,248DWORD *pcbData);249PVOID WINAPI DetourBinarySetPayload(PDETOUR_BINARY pBinary,250REFGUID rguid,251PVOID pData,252DWORD cbData);253BOOL WINAPI DetourBinaryDeletePayload(PDETOUR_BINARY pBinary, REFGUID rguid);254BOOL WINAPI DetourBinaryPurgePayloads(PDETOUR_BINARY pBinary);255BOOL WINAPI DetourBinaryResetImports(PDETOUR_BINARY pBinary);256BOOL WINAPI DetourBinaryEditImports(PDETOUR_BINARY pBinary,257PVOID pContext,258PF_DETOUR_BINARY_BYWAY_CALLBACK pfByway,259PF_DETOUR_BINARY_FILE_CALLBACK pfFile,260PF_DETOUR_BINARY_SYMBOL_CALLBACK pfSymbol,261PF_DETOUR_BINARY_COMMIT_CALLBACK pfCommit);262BOOL WINAPI DetourBinaryWrite(PDETOUR_BINARY pBinary, HANDLE hFile);263BOOL WINAPI DetourBinaryClose(PDETOUR_BINARY pBinary);264265/////////////////////////////////////////////////// Create Process & Load Dll.266//267typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEA)268(LPCSTR lpApplicationName,269LPSTR lpCommandLine,270LPSECURITY_ATTRIBUTES lpProcessAttributes,271LPSECURITY_ATTRIBUTES lpThreadAttributes,272BOOL bInheritHandles,273DWORD dwCreationFlags,274LPVOID lpEnvironment,275LPCSTR lpCurrentDirectory,276LPSTARTUPINFOA lpStartupInfo,277LPPROCESS_INFORMATION lpProcessInformation);278279typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEW)280(LPCWSTR lpApplicationName,281LPWSTR lpCommandLine,282LPSECURITY_ATTRIBUTES lpProcessAttributes,283LPSECURITY_ATTRIBUTES lpThreadAttributes,284BOOL bInheritHandles,285DWORD dwCreationFlags,286LPVOID lpEnvironment,287LPCWSTR lpCurrentDirectory,288LPSTARTUPINFOW lpStartupInfo,289LPPROCESS_INFORMATION lpProcessInformation);290291BOOL WINAPI DetourCreateProcessWithDllA(LPCSTR lpApplicationName,292__in_z LPSTR lpCommandLine,293LPSECURITY_ATTRIBUTES lpProcessAttributes,294LPSECURITY_ATTRIBUTES lpThreadAttributes,295BOOL bInheritHandles,296DWORD dwCreationFlags,297LPVOID lpEnvironment,298LPCSTR lpCurrentDirectory,299LPSTARTUPINFOA lpStartupInfo,300LPPROCESS_INFORMATION lpProcessInformation,301LPCSTR lpDetouredDllFullName,302LPCSTR lpDllName,303PDETOUR_CREATE_PROCESS_ROUTINEA304pfCreateProcessA);305306BOOL WINAPI DetourCreateProcessWithDllW(LPCWSTR lpApplicationName,307__in_z LPWSTR lpCommandLine,308LPSECURITY_ATTRIBUTES lpProcessAttributes,309LPSECURITY_ATTRIBUTES lpThreadAttributes,310BOOL bInheritHandles,311DWORD dwCreationFlags,312LPVOID lpEnvironment,313LPCWSTR lpCurrentDirectory,314LPSTARTUPINFOW lpStartupInfo,315LPPROCESS_INFORMATION lpProcessInformation,316LPCSTR lpDetouredDllFullName,317LPCSTR lpDllName,318PDETOUR_CREATE_PROCESS_ROUTINEW319pfCreateProcessW);320321#ifdef UNICODE322#define DetourCreateProcessWithDll DetourCreateProcessWithDllW323#define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEW324#else325#define DetourCreateProcessWithDll DetourCreateProcessWithDllA326#define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEA327#endif // !UNICODE328329BOOL WINAPI DetourCopyPayloadToProcess(HANDLE hProcess,330REFGUID rguid,331PVOID pvData,332DWORD cbData);333BOOL WINAPI DetourRestoreAfterWith();334BOOL WINAPI DetourRestoreAfterWithEx(PVOID pvData, DWORD cbData);335336HMODULE WINAPI DetourGetDetouredMarker();337338//339//////////////////////////////////////////////////////////////////////////////340#ifdef __cplusplus341}342#endif // __cplusplus343344//////////////////////////////////////////////// Detours Internal Definitions.345//346#ifdef __cplusplus347#ifdef DETOURS_INTERNAL348349#ifndef __deref_out350#define __deref_out351#endif352353#ifndef __deref354#define __deref355#endif356357//////////////////////////////////////////////////////////////////////////////358//359#if (_MSC_VER < 1299)360#include <imagehlp.h>361typedef IMAGEHLP_MODULE IMAGEHLP_MODULE64;362typedef PIMAGEHLP_MODULE PIMAGEHLP_MODULE64;363typedef IMAGEHLP_SYMBOL SYMBOL_INFO;364typedef PIMAGEHLP_SYMBOL PSYMBOL_INFO;365366static inline367LONG InterlockedCompareExchange(LONG *ptr, LONG nval, LONG oval)368{369return (LONG)::InterlockedCompareExchange((PVOID*)ptr, (PVOID)nval, (PVOID)oval);370}371#else372#include <dbghelp.h>373#endif374375#ifdef IMAGEAPI // defined by DBGHELP.H376typedef LPAPI_VERSION (NTAPI *PF_ImagehlpApiVersionEx)(LPAPI_VERSION AppVersion);377378typedef BOOL (NTAPI *PF_SymInitialize)(IN HANDLE hProcess,379IN LPCSTR UserSearchPath,380IN BOOL fInvadeProcess);381typedef DWORD (NTAPI *PF_SymSetOptions)(IN DWORD SymOptions);382typedef DWORD (NTAPI *PF_SymGetOptions)(VOID);383typedef DWORD64 (NTAPI *PF_SymLoadModule64)(IN HANDLE hProcess,384IN HANDLE hFile,385IN PSTR ImageName,386IN PSTR ModuleName,387IN DWORD64 BaseOfDll,388IN DWORD SizeOfDll);389typedef BOOL (NTAPI *PF_SymGetModuleInfo64)(IN HANDLE hProcess,390IN DWORD64 qwAddr,391OUT PIMAGEHLP_MODULE64 ModuleInfo);392typedef BOOL (NTAPI *PF_SymFromName)(IN HANDLE hProcess,393IN LPSTR Name,394OUT PSYMBOL_INFO Symbol);395396typedef struct _DETOUR_SYM_INFO397{398HANDLE hProcess;399HMODULE hDbgHelp;400PF_ImagehlpApiVersionEx pfImagehlpApiVersionEx;401PF_SymInitialize pfSymInitialize;402PF_SymSetOptions pfSymSetOptions;403PF_SymGetOptions pfSymGetOptions;404PF_SymLoadModule64 pfSymLoadModule64;405PF_SymGetModuleInfo64 pfSymGetModuleInfo64;406PF_SymFromName pfSymFromName;407} DETOUR_SYM_INFO, *PDETOUR_SYM_INFO;408409PDETOUR_SYM_INFO DetourLoadDbgHelp(VOID);410411#endif // IMAGEAPI412413#ifndef DETOUR_TRACE414#if DETOUR_DEBUG415#define DETOUR_TRACE(x) printf x416#define DETOUR_BREAK() DebugBreak()417#include <stdio.h>418#include <limits.h>419#else420#define DETOUR_TRACE(x)421#define DETOUR_BREAK()422#endif423#endif424425#ifdef DETOURS_IA64426__declspec(align(16)) struct DETOUR_IA64_BUNDLE427{428public:429union430{431BYTE data[16];432UINT64 wide[2];433};434435public:436struct DETOUR_IA64_METADATA;437438typedef BOOL (DETOUR_IA64_BUNDLE::* DETOUR_IA64_METACOPY)439(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;440441enum {442A_UNIT = 1u,443I_UNIT = 2u,444M_UNIT = 3u,445B_UNIT = 4u,446F_UNIT = 5u,447L_UNIT = 6u,448X_UNIT = 7u,449UNIT_MASK = 7u,450STOP = 8u451};452struct DETOUR_IA64_METADATA453{454ULONG nTemplate : 8; // Instruction template.455ULONG nUnit0 : 4; // Unit for slot 0456ULONG nUnit1 : 4; // Unit for slot 1457ULONG nUnit2 : 4; // Unit for slot 2458DETOUR_IA64_METACOPY pfCopy; // Function pointer.459};460461protected:462BOOL CopyBytes(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;463BOOL CopyBytesMMB(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;464BOOL CopyBytesMBB(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;465BOOL CopyBytesBBB(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;466BOOL CopyBytesMLX(const DETOUR_IA64_METADATA *pMeta, DETOUR_IA64_BUNDLE *pDst) const;467468static const DETOUR_IA64_METADATA s_rceCopyTable[33];469470public:471// 120 112 104 96 88 80 72 64 56 48 40 32 24 16 8 0472// f. e. d. c. b. a. 9. 8. 7. 6. 5. 4. 3. 2. 1. 0.473474// 00475// f.e. d.c. b.a. 9.8. 7.6. 5.4. 3.2. 1.0.476// 0000 0000 0000 0000 0000 0000 0000 001f : Template [4..0]477// 0000 0000 0000 0000 0000 03ff ffff ffe0 : Zero [ 41.. 5]478// 0000 0000 0000 0000 0000 3c00 0000 0000 : Zero [ 45.. 42]479// 0000 0000 0007 ffff ffff c000 0000 0000 : One [ 82.. 46]480// 0000 0000 0078 0000 0000 0000 0000 0000 : One [ 86.. 83]481// 0fff ffff ff80 0000 0000 0000 0000 0000 : Two [123.. 87]482// f000 0000 0000 0000 0000 0000 0000 0000 : Two [127..124]483BYTE GetTemplate() const;484BYTE GetInst0() const;485BYTE GetInst1() const;486BYTE GetInst2() const;487BYTE GetUnit0() const;488BYTE GetUnit1() const;489BYTE GetUnit2() const;490UINT64 GetData0() const;491UINT64 GetData1() const;492UINT64 GetData2() const;493494public:495BOOL IsBrl() const;496VOID SetBrl();497VOID SetBrl(UINT64 target);498UINT64 GetBrlTarget() const;499VOID SetBrlTarget(UINT64 target);500VOID SetBrlImm(UINT64 imm);501UINT64 GetBrlImm() const;502503BOOL IsMovlGp() const;504UINT64 GetMovlGp() const;505VOID SetMovlGp(UINT64 gp);506507VOID SetInst0(BYTE nInst);508VOID SetInst1(BYTE nInst);509VOID SetInst2(BYTE nInst);510VOID SetData0(UINT64 nData);511VOID SetData1(UINT64 nData);512VOID SetData2(UINT64 nData);513BOOL SetNop0();514BOOL SetNop1();515BOOL SetNop2();516BOOL SetStop();517518BOOL Copy(DETOUR_IA64_BUNDLE *pDst) const;519};520#endif // DETOURS_IA64521522//////////////////////////////////////////////////////////////////////////////523524#endif // DETOURS_INTERNAL525#endif // __cplusplus526527#endif // _DETOURS_H_528//529//////////////////////////////////////////////////////////////// End of File.530531532