mirror of
https://github.com/vosen/ZLUDA.git
synced 2025-07-29 12:28:38 +00:00
Merge commit 'dabc40cb19
' as 'ext/detours'
This commit is contained in:
commit
77523940b3
178 changed files with 102613 additions and 0 deletions
47
ext/detours/samples/excep/Makefile
Normal file
47
ext/detours/samples/excep/Makefile
Normal file
|
@ -0,0 +1,47 @@
|
|||
##############################################################################
|
||||
##
|
||||
## Makefile for Detours Test Programs.
|
||||
##
|
||||
## Microsoft Research Detours Package
|
||||
##
|
||||
## Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
##
|
||||
|
||||
!include ..\common.mak
|
||||
|
||||
LIBS=$(LIBS) kernel32.lib
|
||||
|
||||
all: dirs \
|
||||
$(BIND)\excep.exe \
|
||||
!IF $(DETOURS_SOURCE_BROWSING)==1
|
||||
$(OBJD)\excep.bsc
|
||||
!ENDIF
|
||||
|
||||
clean:
|
||||
-del *~ 2>nul
|
||||
-del $(BIND)\excep.* 2>nul
|
||||
-rmdir /q /s $(OBJD) 2>nul
|
||||
|
||||
realclean: clean
|
||||
-rmdir /q /s $(OBJDS) 2>nul
|
||||
|
||||
dirs:
|
||||
@if not exist $(BIND) mkdir $(BIND) && echo. Created $(BIND)
|
||||
@if not exist $(OBJD) mkdir $(OBJD) && echo. Created $(OBJD)
|
||||
|
||||
$(OBJD)\excep.obj : excep.cpp
|
||||
$(OBJD)\firstexc.obj : firstexc.cpp
|
||||
|
||||
$(BIND)\excep.exe : $(OBJD)\excep.obj $(OBJD)\firstexc.obj $(DEPS)
|
||||
cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb $(OBJD)\excep.obj $(OBJD)\firstexc.obj \
|
||||
/link $(LINKFLAGS) $(LIBS) /subsystem:console /entry:WinMainCRTStartup
|
||||
|
||||
$(OBJD)\excep.bsc : $(OBJD)\excep.obj
|
||||
bscmake /v /n /o $@ $(OBJD)\excep.sbr
|
||||
|
||||
##############################################################################
|
||||
|
||||
test: $(BIND)\excep.exe
|
||||
$(BIND)\excep.exe
|
||||
|
||||
################################################################# End of File.
|
125
ext/detours/samples/excep/excep.cpp
Normal file
125
ext/detours/samples/excep/excep.cpp
Normal file
|
@ -0,0 +1,125 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// First Chance Exception Handling Test Program (excep.cpp of excep.exe)
|
||||
//
|
||||
// Microsoft Research Detours Package
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// For more information on exception handling, see "A Crash Course on the
|
||||
// Depths of Win32 Structured Exception Handling," by Matt Pietrek in the
|
||||
// January 1997 issue of Microsoft Systems Journal.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#include <detours.h>
|
||||
#include "firstexc.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
static LPVOID s_pvData = NULL;
|
||||
static DWORD s_dwDataPerm = 0;
|
||||
|
||||
static LONG ExceptCatch(LONG nTry, DWORD dwException, LPEXCEPTION_POINTERS pinfo)
|
||||
{
|
||||
printf(" ExceptCatch(%ld, %08lx, %08lx)\n", nTry, dwException, (ULONG)pinfo);
|
||||
#ifdef INCLUDE_THIS
|
||||
if (nTry == 0) {
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
#endif
|
||||
return EXCEPTION_EXECUTE_HANDLER;
|
||||
}
|
||||
|
||||
static int BadCode(int nTry)
|
||||
{
|
||||
printf(" BadCode(Try:%d)\n", nTry);
|
||||
printf(" BadCode -> %ld\n", *(PULONG)s_pvData);
|
||||
((PULONG)s_pvData)[0] = 0;
|
||||
printf(" BadCode -> %ld\n", *(PULONG)s_pvData);
|
||||
((PULONG)s_pvData)[-1] = 0;
|
||||
printf(" BadCode -> %ld\n", *(PULONG)s_pvData);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void safe(int nTry)
|
||||
{
|
||||
__try {
|
||||
printf(" try(%d)\n", nTry);
|
||||
BadCode(nTry);
|
||||
printf(" good(%d)\n", nTry);
|
||||
} __except(ExceptCatch(nTry,
|
||||
GetExceptionCode(),
|
||||
GetExceptionInformation())) {
|
||||
DWORD dwExcept = GetExceptionCode();
|
||||
|
||||
printf(" handler(%d) : %08lx\n", nTry, dwExcept);
|
||||
}
|
||||
}
|
||||
|
||||
void raw(int nTry)
|
||||
{
|
||||
BadCode(nTry);
|
||||
}
|
||||
|
||||
LONG WINAPI MyVirtualFaultFilter(PEXCEPTION_POINTERS pException)
|
||||
{
|
||||
PEXCEPTION_RECORD pExceptRec = pException->ExceptionRecord;
|
||||
|
||||
if (pExceptRec->ExceptionCode == 0xc0000005) {
|
||||
printf("-- Memory access exception.\n");
|
||||
if (pExceptRec->NumberParameters >= 2 &&
|
||||
pExceptRec->ExceptionInformation[1] >= (ULONG)s_pvData &&
|
||||
pExceptRec->ExceptionInformation[1] <= (ULONG)s_pvData + sizeof(ULONG)) {
|
||||
|
||||
VirtualProtect(s_pvData, sizeof(ULONG), PAGE_READWRITE, &s_dwDataPerm);
|
||||
printf("-- Changed permissions.\n");
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
}
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmdShow)
|
||||
{
|
||||
(void)hinst;
|
||||
(void)hprev;
|
||||
(void)lpszCmdLine;
|
||||
(void)nCmdShow;
|
||||
|
||||
s_pvData = VirtualAlloc(NULL, sizeof(ULONG), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
||||
if (s_pvData == NULL) {
|
||||
printf("VirtualAlloc failed: %ld\n", GetLastError());
|
||||
return 0;
|
||||
}
|
||||
*(PULONG)s_pvData = 1;
|
||||
|
||||
VirtualProtect(s_pvData, sizeof(ULONG), PAGE_READONLY, &s_dwDataPerm);
|
||||
|
||||
DetourFirstChanceExceptionFilter(MyVirtualFaultFilter);
|
||||
|
||||
printf("main\n");
|
||||
printf("--------------------------------------------------\n");
|
||||
int nTry = 0;
|
||||
for (; nTry < 1; nTry++) {
|
||||
// safe(nTry);
|
||||
}
|
||||
printf("-- safe ------------------------------------------\n");
|
||||
safe(nTry);
|
||||
VirtualProtect(s_pvData, sizeof(ULONG), PAGE_READWRITE, &s_dwDataPerm);
|
||||
*(PULONG)s_pvData = 1;
|
||||
VirtualProtect(s_pvData, sizeof(ULONG), PAGE_READONLY, &s_dwDataPerm);
|
||||
|
||||
printf("-- raw -------------------------------------------\n");
|
||||
printf("*\n");
|
||||
printf("* NB: The second attempt to write will fail because it isn't handled.\n");
|
||||
printf("*\n");
|
||||
raw(nTry);
|
||||
printf("--------------------------------------------------\n");
|
||||
printf("exit\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
///////////////////////////////////////////////////////////////// End of File.
|
191
ext/detours/samples/excep/firstexc.cpp
Normal file
191
ext/detours/samples/excep/firstexc.cpp
Normal file
|
@ -0,0 +1,191 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Detours Test Program (firstexc.cpp of firstexc.lib)
|
||||
//
|
||||
// Microsoft Research Detours Package
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// For more information on exception handling, see "A Crash Course on the
|
||||
// Depths of Win32 Structured Exception Handling," by Matt Pietrek in the
|
||||
// January 1997 issue of Microsoft Systems Journal.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#include "detours.h"
|
||||
#include "firstexc.h"
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma warning(disable: 4740)
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
static BOOL s_bExceptionDetourInstalled = FALSE;
|
||||
static LPTOP_LEVEL_EXCEPTION_FILTER s_pFirstChanceFilter = NULL;
|
||||
|
||||
ULONG (NTAPI *Real_NtContinue)(IN PCONTEXT ContextRecord,
|
||||
IN BOOLEAN TestAlerts) = NULL;
|
||||
|
||||
VOID (NTAPI *Real_KiUserExceptionDispatcher)(IN PEXCEPTION_RECORD ExceptionRecord,
|
||||
IN PCONTEXT ContextFrame) = NULL;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// This function effectively removes all try..catch frames for the current
|
||||
// stack. It forces all exceptions to be treated as unhandled exceptions.
|
||||
//
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4733)
|
||||
static VOID WINAPI RemoveAllExceptionHandlers(VOID)
|
||||
{
|
||||
// The basic, OS defined exception frame
|
||||
struct EXCEPTION_REGISTRATION
|
||||
{
|
||||
EXCEPTION_REGISTRATION* prev;
|
||||
FARPROC handler;
|
||||
};
|
||||
|
||||
EXCEPTION_REGISTRATION * pVCExcRec = NULL;
|
||||
EXCEPTION_REGISTRATION * pLastGood = NULL;
|
||||
|
||||
__asm mov eax, FS:[0];
|
||||
__asm mov [pVCExcRec], eax;
|
||||
|
||||
for (pLastGood = pVCExcRec; (ULONG)pVCExcRec != ~0ul; ) {
|
||||
if ((ULONG)pVCExcRec >= 0x30000000)
|
||||
break;
|
||||
|
||||
pLastGood = pVCExcRec;
|
||||
pVCExcRec = (EXCEPTION_REGISTRATION *)(pVCExcRec->prev);
|
||||
}
|
||||
|
||||
__asm mov eax, [pLastGood];
|
||||
__asm mov FS:[0], eax;
|
||||
}
|
||||
#pragma warning(pop)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Routine Description:
|
||||
//
|
||||
// This routine is entered on return from kernel mode to dispatch a user
|
||||
// mode exception. If a frame based handler handles the exception, then
|
||||
// the execution is continued. Else last chance processing is performed.
|
||||
//
|
||||
// NOTE: This procedure is not called, but rather dispatched to.
|
||||
// It depends on there not being a return address on the stack
|
||||
// (assumption w.r.t. argument offsets.)
|
||||
//
|
||||
// Arguments:
|
||||
// ExceptionRecord (esp+0) - Supplies a pointer to an exception record.
|
||||
// ContextRecord (esp+4) - Supplies a pointer to a context frame.
|
||||
//
|
||||
// Return Value:
|
||||
// None.
|
||||
//
|
||||
static VOID __declspec(naked) NTAPI
|
||||
Detour_KiUserExceptionDispatcher(PEXCEPTION_RECORD pExceptRec,
|
||||
CONTEXT *pContext)
|
||||
{
|
||||
__asm {
|
||||
xor eax, eax ; // Create fake return address on stack.
|
||||
push eax ; // (Generally, we are called by the kernel.)
|
||||
|
||||
push ebp ; // Prolog
|
||||
mov ebp, esp ;
|
||||
sub esp, __LOCAL_SIZE ;
|
||||
}
|
||||
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER pFirstChanceFilter;
|
||||
EXCEPTION_POINTERS ep;
|
||||
DWORD dwReturn;
|
||||
DWORD dwError;
|
||||
|
||||
ep.ExceptionRecord = pExceptRec;
|
||||
ep.ContextRecord = pContext;
|
||||
pFirstChanceFilter = s_pFirstChanceFilter;
|
||||
dwReturn = EXCEPTION_CONTINUE_SEARCH;
|
||||
dwError = 0;
|
||||
|
||||
if (s_pFirstChanceFilter) {
|
||||
dwReturn = pFirstChanceFilter(&ep);
|
||||
}
|
||||
|
||||
if (dwReturn == EXCEPTION_CONTINUE_EXECUTION) {
|
||||
dwError = Real_NtContinue(pContext, 0);
|
||||
// This call should *NEVER* return. If it does, we want to fail to the debugger.
|
||||
RemoveAllExceptionHandlers();
|
||||
}
|
||||
|
||||
if (dwReturn == EXCEPTION_EXECUTE_HANDLER) { // Special: Call debugger.
|
||||
RemoveAllExceptionHandlers();
|
||||
}
|
||||
|
||||
__asm {
|
||||
mov ebx, pExceptRec ;
|
||||
mov ecx, pContext ;
|
||||
push ecx ;
|
||||
push ebx ;
|
||||
mov eax, [Real_KiUserExceptionDispatcher];
|
||||
jmp eax ;
|
||||
;
|
||||
; The above code should never return.
|
||||
;
|
||||
int 3 ; // Break!
|
||||
;
|
||||
mov esp, ebp ; // Epilog
|
||||
pop ebp ;
|
||||
ret ;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Set the first-chance exception filter.
|
||||
//
|
||||
// Returns the pointer to the last first-chance exception filter if there
|
||||
// was one. If this is the first first-chance exception filter, installs
|
||||
// the necessary detour and acquires the appropriate function pointers.
|
||||
// If the parameter is NULL, first-chance exception filtering is disabled.
|
||||
//
|
||||
// A first-chance exception filter should always return one of three
|
||||
// possible codes:
|
||||
//
|
||||
// EXCEPTION_CONTINUE_SEARCH:
|
||||
// The exception was not handled by this filter; continue the
|
||||
// search for the appropriate exception handler.
|
||||
//
|
||||
// EXCEPTION_CONTINUE_EXECUTION:
|
||||
// The exception was handled by this filter; continue execution
|
||||
// at the point were the exception was thrown.
|
||||
//
|
||||
// EXCEPTION_EXECUTE_HANDLER:
|
||||
// Drastic failure in the exception filter. Process the
|
||||
// exception as if no exception handlers were installed.
|
||||
// (i.e. Give the user a chance to invoke the debugger.)
|
||||
//
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
|
||||
DetourFirstChanceExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER pNewFirstChanceFilter)
|
||||
{
|
||||
if (!s_bExceptionDetourInstalled) {
|
||||
s_bExceptionDetourInstalled = TRUE;
|
||||
|
||||
Real_NtContinue = (ULONG (NTAPI *)(IN PCONTEXT, IN BOOLEAN))
|
||||
DetourFindFunction("ntdll.dll", "NtContinue");
|
||||
Real_KiUserExceptionDispatcher =
|
||||
(VOID (NTAPI *)(IN PEXCEPTION_RECORD, IN PCONTEXT))
|
||||
DetourFindFunction("ntdll.dll", "KiUserExceptionDispatcher");
|
||||
|
||||
DetourTransactionBegin();
|
||||
DetourUpdateThread(GetCurrentThread());
|
||||
DetourAttach(&(PVOID&)Real_KiUserExceptionDispatcher,
|
||||
Detour_KiUserExceptionDispatcher);
|
||||
DetourTransactionCommit();
|
||||
}
|
||||
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER pOldFirstChanceFilter = s_pFirstChanceFilter;
|
||||
s_pFirstChanceFilter = pNewFirstChanceFilter;
|
||||
return pOldFirstChanceFilter;
|
||||
}
|
||||
//
|
||||
///////////////////////////////////////////////////////////////// End of File.
|
21
ext/detours/samples/excep/firstexc.h
Normal file
21
ext/detours/samples/excep/firstexc.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Detours Test Program (firstexc.h of firstexc.exe)
|
||||
//
|
||||
// Microsoft Research Detours Package
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#ifndef _FIRSTEXC_H_
|
||||
#define _FIRSTEXC_H_
|
||||
|
||||
/////////////////////////////////////////////// First Chance Exception Filter.
|
||||
//
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
|
||||
DetourFirstChanceExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelFilter);
|
||||
|
||||
#endif // _FIRSTEXC_H_
|
||||
//
|
||||
//////////////////////////////////////////////////////////////// End of File.
|
Loading…
Add table
Add a link
Reference in a new issue