-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add safe DetoursAttach (and friends) overloads (#178)
Fixes #176 I've also added a sample (a copy of the `simple` sample, but without the `(PVOID&)` casts) to validate the functionality.
- Loading branch information
Showing
8 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
############################################################################## | ||
## | ||
## API Extention to Measure time slept. | ||
## | ||
## Microsoft Research Detours Package | ||
## | ||
## Copyright (c) Microsoft Corporation. All rights reserved. | ||
## | ||
|
||
!include ..\common.mak | ||
|
||
LIBS=$(LIBS) kernel32.lib | ||
CFLAGS=$(CFLAGS) /std:c++14 | ||
|
||
############################################################################## | ||
|
||
all: dirs \ | ||
$(BIND)\simple_safe$(DETOURS_BITS).dll \ | ||
$(BIND)\sleep5.exe \ | ||
\ | ||
!IF $(DETOURS_SOURCE_BROWSING)==1 | ||
$(OBJD)\simple_safe$(DETOURS_BITS).bsc \ | ||
$(OBJD)\sleep5.bsc \ | ||
!ENDIF | ||
option | ||
|
||
############################################################################## | ||
|
||
dirs: | ||
@if not exist $(BIND) mkdir $(BIND) && echo. Created $(BIND) | ||
@if not exist $(OBJD) mkdir $(OBJD) && echo. Created $(OBJD) | ||
|
||
$(OBJD)\simple_safe.obj : simple_safe.cpp | ||
|
||
$(OBJD)\simple_safe.res : simple_safe.rc | ||
|
||
$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\simple_safe$(DETOURS_BITS).lib: \ | ||
$(OBJD)\simple_safe.obj $(OBJD)\simple_safe.res $(DEPS) | ||
cl /LD $(CFLAGS) /Fe$(@R).dll /Fd$(@R).pdb \ | ||
$(OBJD)\simple_safe.obj $(OBJD)\simple_safe.res \ | ||
/link $(LINKFLAGS) /subsystem:console \ | ||
/export:DetourFinishHelperProcess,@1,NONAME \ | ||
/export:TimedSleepEx \ | ||
$(LIBS) | ||
|
||
$(OBJD)\simple_safe$(DETOURS_BITS).bsc : $(OBJD)\simple_safe.obj | ||
bscmake /v /n /o $@ $(OBJD)\simple_safe.sbr | ||
|
||
$(OBJD)\sleep5.obj : sleep5.cpp | ||
|
||
$(BIND)\sleep5.exe : $(OBJD)\sleep5.obj $(DEPS) | ||
cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb $(OBJD)\sleep5.obj \ | ||
/link $(LINKFLAGS) $(LIBS) \ | ||
/subsystem:console | ||
|
||
$(OBJD)\sleep5.bsc : $(OBJD)\sleep5.obj | ||
bscmake /v /n /o $@ $(OBJD)\sleep5.sbr | ||
|
||
############################################################################## | ||
|
||
clean: | ||
-del *~ 2>nul | ||
-del $(BIND)\simple_safe*.* 2>nul | ||
-del $(BIND)\sleep5.* 2>nul | ||
-rmdir /q /s $(OBJD) 2>nul | ||
|
||
realclean: clean | ||
-rmdir /q /s $(OBJDS) 2>nul | ||
|
||
############################################### Install non-bit-size binaries. | ||
|
||
!IF "$(DETOURS_OPTION_PROCESSOR)" != "" | ||
|
||
$(OPTD)\simple_safe$(DETOURS_OPTION_BITS).dll: | ||
$(OPTD)\simple_safe$(DETOURS_OPTION_BITS).pdb: | ||
|
||
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).dll : $(OPTD)\simple_safe$(DETOURS_OPTION_BITS).dll | ||
@if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). | ||
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).pdb : $(OPTD)\simple_safe$(DETOURS_OPTION_BITS).pdb | ||
@if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). | ||
|
||
option: \ | ||
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).dll \ | ||
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).pdb \ | ||
|
||
!ELSE | ||
|
||
option: | ||
|
||
!ENDIF | ||
|
||
############################################################################## | ||
|
||
test: all | ||
@echo -------- Reseting test binaries to initial state. --------------------- | ||
$(BIND)\setdll.exe -r $(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Should not load simple_safe$(DETOURS_BITS).dll ----------------------------------- | ||
$(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Adding simple_safe$(DETOURS_BITS).dll to sleep5.exe ------------------------------ | ||
$(BIND)\setdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Should load simple_safe$(DETOURS_BITS).dll statically ---------------------------- | ||
$(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Removing simple_safe$(DETOURS_BITS).dll from sleep5.exe -------------------------- | ||
$(BIND)\setdll.exe -r $(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Should not load simple_safe$(DETOURS_BITS).dll ----------------------------------- | ||
$(BIND)\sleep5.exe | ||
@echo. | ||
@echo -------- Should load simple_safe$(DETOURS_BITS).dll dynamically using withdll.exe---------- | ||
$(BIND)\withdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe | ||
@echo. | ||
|
||
debug: all | ||
windbg -o $(BIND)\withdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe | ||
|
||
|
||
################################################################# End of File. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Detours Test Program (simple_safe.cpp of simple_safe.dll) | ||
// | ||
// Microsoft Research Detours Package | ||
// | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// | ||
// This DLL will detour the Windows SleepEx API so that TimedSleep function | ||
// gets called instead. TimedSleepEx records the before and after times, and | ||
// calls the real SleepEx API through the TrueSleepEx function pointer. | ||
// | ||
// The difference between simple and simple_safe is that simple_safe | ||
// uses the C++ 14 overloads which help prevent mismatching types. | ||
// | ||
#include <stdio.h> | ||
#include <windows.h> | ||
#include "detours.h" | ||
|
||
static LONG dwSlept = 0; | ||
static DWORD (WINAPI * TrueSleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx; | ||
|
||
DWORD WINAPI TimedSleepEx(DWORD dwMilliseconds, BOOL bAlertable) | ||
{ | ||
DWORD dwBeg = GetTickCount(); | ||
DWORD ret = TrueSleepEx(dwMilliseconds, bAlertable); | ||
DWORD dwEnd = GetTickCount(); | ||
|
||
InterlockedExchangeAdd(&dwSlept, dwEnd - dwBeg); | ||
|
||
return ret; | ||
} | ||
|
||
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) | ||
{ | ||
LONG error; | ||
(void)hinst; | ||
(void)reserved; | ||
|
||
if (DetourIsHelperProcess()) { | ||
return TRUE; | ||
} | ||
|
||
if (dwReason == DLL_PROCESS_ATTACH) { | ||
DetourRestoreAfterWith(); | ||
|
||
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" | ||
" Starting.\n"); | ||
fflush(stdout); | ||
|
||
DetourTransactionBegin(); | ||
DetourUpdateThread(GetCurrentThread()); | ||
DetourAttach(&TrueSleepEx, TimedSleepEx); | ||
error = DetourTransactionCommit(); | ||
|
||
if (error == NO_ERROR) { | ||
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" | ||
" Detoured SleepEx().\n"); | ||
} | ||
else { | ||
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" | ||
" Error detouring SleepEx(): %ld\n", error); | ||
} | ||
} | ||
else if (dwReason == DLL_PROCESS_DETACH) { | ||
DetourTransactionBegin(); | ||
DetourUpdateThread(GetCurrentThread()); | ||
DetourDetach(&TrueSleepEx, TimedSleepEx); | ||
error = DetourTransactionCommit(); | ||
|
||
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" | ||
" Removed SleepEx() (result=%ld), slept %ld ticks.\n", error, dwSlept); | ||
fflush(stdout); | ||
} | ||
return TRUE; | ||
} | ||
|
||
// | ||
///////////////////////////////////////////////////////////////// End of File. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Version information for simple_safe.rc. | ||
// | ||
// Microsoft Research Detours Package | ||
// | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// | ||
|
||
#include "detver.h" | ||
|
||
#define VER_INTERNALNAME_STR "simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) | ||
#define VER_ORIGINALFILENAME_STR "simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll" | ||
#define VER_FILEDESCRIPTION_STR "Detours Test Module" | ||
#define VER_COMPANYNAME_STR "Microsoft Corporation" | ||
|
||
#include "common.ver" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Detours Test Program (sleep5.cpp of sleep5.exe) | ||
// | ||
// Microsoft Research Detours Package | ||
// | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// | ||
|
||
#include <windows.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int __cdecl main(int argc, char ** argv) | ||
{ | ||
if (argc == 2) { | ||
Sleep(atoi(argv[1]) * 1000); | ||
} | ||
else { | ||
printf("sleep5.exe: Starting.\n"); | ||
|
||
Sleep(5000); | ||
|
||
printf("sleep5.exe: Done sleeping.\n"); | ||
} | ||
return 0; | ||
} | ||
// | ||
///////////////////////////////////////////////////////////////// End of File. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.