Skip to content

Commit 784f155

Browse files
authored
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.
1 parent 259ad41 commit 784f155

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

samples/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ all:
2121
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
2222
cd "$(MAKEDIR)\simple"
2323
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
24+
cd "$(MAKEDIR)\simple_safe"
25+
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
2426
cd "$(MAKEDIR)\slept"
2527
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
2628
cd "$(MAKEDIR)\setdll"
@@ -96,6 +98,8 @@ clean:
9698
@$(MAKE) /NOLOGO /$(MAKEFLAGS) clean
9799
cd "$(MAKEDIR)\simple"
98100
@$(MAKE) /NOLOGO /$(MAKEFLAGS) clean
101+
cd "$(MAKEDIR)\simple_safe"
102+
@$(MAKE) /NOLOGO /$(MAKEFLAGS) clean
99103
cd "$(MAKEDIR)\slept"
100104
@$(MAKE) /NOLOGO /$(MAKEFLAGS) clean
101105
cd "$(MAKEDIR)\setdll"
@@ -162,6 +166,8 @@ realclean:
162166
@$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean
163167
cd "$(MAKEDIR)\simple"
164168
@$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean
169+
cd "$(MAKEDIR)\simple_safe"
170+
@$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean
165171
cd "$(MAKEDIR)\slept"
166172
@$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean
167173
cd "$(MAKEDIR)\setdll"
@@ -228,6 +234,8 @@ test:
228234
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
229235
cd "$(MAKEDIR)\simple"
230236
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
237+
cd "$(MAKEDIR)\simple_safe"
238+
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
231239
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
232240
cd "$(MAKEDIR)\slept"
233241
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test

samples/simple_safe/Makefile

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
##############################################################################
2+
##
3+
## API Extention to Measure time slept.
4+
##
5+
## Microsoft Research Detours Package
6+
##
7+
## Copyright (c) Microsoft Corporation. All rights reserved.
8+
##
9+
10+
!include ..\common.mak
11+
12+
LIBS=$(LIBS) kernel32.lib
13+
CFLAGS=$(CFLAGS) /std:c++14
14+
15+
##############################################################################
16+
17+
all: dirs \
18+
$(BIND)\simple_safe$(DETOURS_BITS).dll \
19+
$(BIND)\sleep5.exe \
20+
\
21+
!IF $(DETOURS_SOURCE_BROWSING)==1
22+
$(OBJD)\simple_safe$(DETOURS_BITS).bsc \
23+
$(OBJD)\sleep5.bsc \
24+
!ENDIF
25+
option
26+
27+
##############################################################################
28+
29+
dirs:
30+
@if not exist $(BIND) mkdir $(BIND) && echo. Created $(BIND)
31+
@if not exist $(OBJD) mkdir $(OBJD) && echo. Created $(OBJD)
32+
33+
$(OBJD)\simple_safe.obj : simple_safe.cpp
34+
35+
$(OBJD)\simple_safe.res : simple_safe.rc
36+
37+
$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\simple_safe$(DETOURS_BITS).lib: \
38+
$(OBJD)\simple_safe.obj $(OBJD)\simple_safe.res $(DEPS)
39+
cl /LD $(CFLAGS) /Fe$(@R).dll /Fd$(@R).pdb \
40+
$(OBJD)\simple_safe.obj $(OBJD)\simple_safe.res \
41+
/link $(LINKFLAGS) /subsystem:console \
42+
/export:DetourFinishHelperProcess,@1,NONAME \
43+
/export:TimedSleepEx \
44+
$(LIBS)
45+
46+
$(OBJD)\simple_safe$(DETOURS_BITS).bsc : $(OBJD)\simple_safe.obj
47+
bscmake /v /n /o $@ $(OBJD)\simple_safe.sbr
48+
49+
$(OBJD)\sleep5.obj : sleep5.cpp
50+
51+
$(BIND)\sleep5.exe : $(OBJD)\sleep5.obj $(DEPS)
52+
cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb $(OBJD)\sleep5.obj \
53+
/link $(LINKFLAGS) $(LIBS) \
54+
/subsystem:console
55+
56+
$(OBJD)\sleep5.bsc : $(OBJD)\sleep5.obj
57+
bscmake /v /n /o $@ $(OBJD)\sleep5.sbr
58+
59+
##############################################################################
60+
61+
clean:
62+
-del *~ 2>nul
63+
-del $(BIND)\simple_safe*.* 2>nul
64+
-del $(BIND)\sleep5.* 2>nul
65+
-rmdir /q /s $(OBJD) 2>nul
66+
67+
realclean: clean
68+
-rmdir /q /s $(OBJDS) 2>nul
69+
70+
############################################### Install non-bit-size binaries.
71+
72+
!IF "$(DETOURS_OPTION_PROCESSOR)" != ""
73+
74+
$(OPTD)\simple_safe$(DETOURS_OPTION_BITS).dll:
75+
$(OPTD)\simple_safe$(DETOURS_OPTION_BITS).pdb:
76+
77+
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).dll : $(OPTD)\simple_safe$(DETOURS_OPTION_BITS).dll
78+
@if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR).
79+
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).pdb : $(OPTD)\simple_safe$(DETOURS_OPTION_BITS).pdb
80+
@if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR).
81+
82+
option: \
83+
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).dll \
84+
$(BIND)\simple_safe$(DETOURS_OPTION_BITS).pdb \
85+
86+
!ELSE
87+
88+
option:
89+
90+
!ENDIF
91+
92+
##############################################################################
93+
94+
test: all
95+
@echo -------- Reseting test binaries to initial state. ---------------------
96+
$(BIND)\setdll.exe -r $(BIND)\sleep5.exe
97+
@echo.
98+
@echo -------- Should not load simple_safe$(DETOURS_BITS).dll -----------------------------------
99+
$(BIND)\sleep5.exe
100+
@echo.
101+
@echo -------- Adding simple_safe$(DETOURS_BITS).dll to sleep5.exe ------------------------------
102+
$(BIND)\setdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe
103+
@echo.
104+
@echo -------- Should load simple_safe$(DETOURS_BITS).dll statically ----------------------------
105+
$(BIND)\sleep5.exe
106+
@echo.
107+
@echo -------- Removing simple_safe$(DETOURS_BITS).dll from sleep5.exe --------------------------
108+
$(BIND)\setdll.exe -r $(BIND)\sleep5.exe
109+
@echo.
110+
@echo -------- Should not load simple_safe$(DETOURS_BITS).dll -----------------------------------
111+
$(BIND)\sleep5.exe
112+
@echo.
113+
@echo -------- Should load simple_safe$(DETOURS_BITS).dll dynamically using withdll.exe----------
114+
$(BIND)\withdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe
115+
@echo.
116+
117+
debug: all
118+
windbg -o $(BIND)\withdll.exe -d:$(BIND)\simple_safe$(DETOURS_BITS).dll $(BIND)\sleep5.exe
119+
120+
121+
################################################################# End of File.

samples/simple_safe/simple_safe.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Detours Test Program (simple_safe.cpp of simple_safe.dll)
4+
//
5+
// Microsoft Research Detours Package
6+
//
7+
// Copyright (c) Microsoft Corporation. All rights reserved.
8+
//
9+
// This DLL will detour the Windows SleepEx API so that TimedSleep function
10+
// gets called instead. TimedSleepEx records the before and after times, and
11+
// calls the real SleepEx API through the TrueSleepEx function pointer.
12+
//
13+
// The difference between simple and simple_safe is that simple_safe
14+
// uses the C++ 14 overloads which help prevent mismatching types.
15+
//
16+
#include <stdio.h>
17+
#include <windows.h>
18+
#include "detours.h"
19+
20+
static LONG dwSlept = 0;
21+
static DWORD (WINAPI * TrueSleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx;
22+
23+
DWORD WINAPI TimedSleepEx(DWORD dwMilliseconds, BOOL bAlertable)
24+
{
25+
DWORD dwBeg = GetTickCount();
26+
DWORD ret = TrueSleepEx(dwMilliseconds, bAlertable);
27+
DWORD dwEnd = GetTickCount();
28+
29+
InterlockedExchangeAdd(&dwSlept, dwEnd - dwBeg);
30+
31+
return ret;
32+
}
33+
34+
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
35+
{
36+
LONG error;
37+
(void)hinst;
38+
(void)reserved;
39+
40+
if (DetourIsHelperProcess()) {
41+
return TRUE;
42+
}
43+
44+
if (dwReason == DLL_PROCESS_ATTACH) {
45+
DetourRestoreAfterWith();
46+
47+
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
48+
" Starting.\n");
49+
fflush(stdout);
50+
51+
DetourTransactionBegin();
52+
DetourUpdateThread(GetCurrentThread());
53+
DetourAttach(&TrueSleepEx, TimedSleepEx);
54+
error = DetourTransactionCommit();
55+
56+
if (error == NO_ERROR) {
57+
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
58+
" Detoured SleepEx().\n");
59+
}
60+
else {
61+
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
62+
" Error detouring SleepEx(): %ld\n", error);
63+
}
64+
}
65+
else if (dwReason == DLL_PROCESS_DETACH) {
66+
DetourTransactionBegin();
67+
DetourUpdateThread(GetCurrentThread());
68+
DetourDetach(&TrueSleepEx, TimedSleepEx);
69+
error = DetourTransactionCommit();
70+
71+
printf("simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
72+
" Removed SleepEx() (result=%ld), slept %ld ticks.\n", error, dwSlept);
73+
fflush(stdout);
74+
}
75+
return TRUE;
76+
}
77+
78+
//
79+
///////////////////////////////////////////////////////////////// End of File.

samples/simple_safe/simple_safe.rc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Version information for simple_safe.rc.
4+
//
5+
// Microsoft Research Detours Package
6+
//
7+
// Copyright (c) Microsoft Corporation. All rights reserved.
8+
//
9+
10+
#include "detver.h"
11+
12+
#define VER_INTERNALNAME_STR "simple_safe" DETOURS_STRINGIFY(DETOURS_BITS)
13+
#define VER_ORIGINALFILENAME_STR "simple_safe" DETOURS_STRINGIFY(DETOURS_BITS) ".dll"
14+
#define VER_FILEDESCRIPTION_STR "Detours Test Module"
15+
#define VER_COMPANYNAME_STR "Microsoft Corporation"
16+
17+
#include "common.ver"

samples/simple_safe/sleep5.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Detours Test Program (sleep5.cpp of sleep5.exe)
4+
//
5+
// Microsoft Research Detours Package
6+
//
7+
// Copyright (c) Microsoft Corporation. All rights reserved.
8+
//
9+
10+
#include <windows.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
int __cdecl main(int argc, char ** argv)
15+
{
16+
if (argc == 2) {
17+
Sleep(atoi(argv[1]) * 1000);
18+
}
19+
else {
20+
printf("sleep5.exe: Starting.\n");
21+
22+
Sleep(5000);
23+
24+
printf("sleep5.exe: Done sleeping.\n");
25+
}
26+
return 0;
27+
}
28+
//
29+
///////////////////////////////////////////////////////////////// End of File.

src/detours.h

+54
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,60 @@ VOID CALLBACK DetourFinishHelperProcess(_In_ HWND,
844844
}
845845
#endif // __cplusplus
846846

847+
/////////////////////////////////////////////////// Type-safe overloads for C++
848+
//
849+
#if __cplusplus >= 201402L || _MSVC_LANG >= 201402L
850+
#include <type_traits>
851+
852+
template<typename T>
853+
struct DetoursIsFunctionPointer : std::false_type {};
854+
855+
template<typename T>
856+
struct DetoursIsFunctionPointer<T*> : std::is_function<std::remove_pointer_t<T>> {};
857+
858+
template<
859+
typename T,
860+
std::enable_if_t<DetoursIsFunctionPointer<T>::value, int> = 0>
861+
LONG DetourAttach(_Inout_ T *ppPointer,
862+
_In_ T pDetour) noexcept
863+
{
864+
return DetourAttach(
865+
reinterpret_cast<void**>(ppPointer),
866+
reinterpret_cast<void*>(pDetour));
867+
}
868+
869+
template<
870+
typename T,
871+
std::enable_if_t<DetoursIsFunctionPointer<T>::value, int> = 0>
872+
LONG DetourAttachEx(_Inout_ T *ppPointer,
873+
_In_ T pDetour,
874+
_Out_opt_ PDETOUR_TRAMPOLINE *ppRealTrampoline,
875+
_Out_opt_ T *ppRealTarget,
876+
_Out_opt_ T *ppRealDetour) noexcept
877+
{
878+
return DetourAttachEx(
879+
reinterpret_cast<void**>(ppPointer),
880+
reinterpret_cast<void*>(pDetour),
881+
ppRealTrampoline,
882+
reinterpret_cast<void**>(ppRealTarget),
883+
reinterpret_cast<void**>(ppRealDetour));
884+
}
885+
886+
template<
887+
typename T,
888+
std::enable_if_t<DetoursIsFunctionPointer<T>::value, int> = 0>
889+
LONG DetourDetach(_Inout_ T *ppPointer,
890+
_In_ T pDetour) noexcept
891+
{
892+
return DetourDetach(
893+
reinterpret_cast<void**>(ppPointer),
894+
reinterpret_cast<void*>(pDetour));
895+
}
896+
897+
#endif // __cplusplus >= 201402L || _MSVC_LANG >= 201402L
898+
//
899+
//////////////////////////////////////////////////////////////////////////////
900+
847901
//////////////////////////////////////////////// Detours Internal Definitions.
848902
//
849903
#ifdef __cplusplus

vc/Detours.vcxproj

+4
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ nmake</Command>
452452
<ClCompile Include="..\samples\setdll\setdll.cpp" />
453453
<ClCompile Include="..\samples\simple\simple.cpp" />
454454
<ClCompile Include="..\samples\simple\sleep5.cpp" />
455+
<ClCompile Include="..\samples\simple_safe\simple_safe.cpp" />
456+
<ClCompile Include="..\samples\simple_safe\sleep5.cpp" />
455457
<ClCompile Include="..\samples\slept\dslept.cpp" />
456458
<ClCompile Include="..\samples\slept\sleepbed.cpp" />
457459
<ClCompile Include="..\samples\slept\sleepnew.cpp" />
@@ -542,6 +544,7 @@ nmake</Command>
542544
<None Include="..\samples\region\Makefile" />
543545
<None Include="..\samples\setdll\Makefile" />
544546
<None Include="..\samples\simple\Makefile" />
547+
<None Include="..\samples\simple_safe\Makefile" />
545548
<None Include="..\samples\slept\Makefile" />
546549
<None Include="..\samples\syelog\Makefile" />
547550
<None Include="..\samples\talloc\Makefile" />
@@ -579,6 +582,7 @@ nmake</Command>
579582
<ResourceCompile Include="..\samples\findfunc\target.rc" />
580583
<ResourceCompile Include="..\samples\opengl\ogldet.rc" />
581584
<ResourceCompile Include="..\samples\simple\simple.rc" />
585+
<ResourceCompile Include="..\samples\simple_safe\simple_safe.rc" />
582586
<ResourceCompile Include="..\samples\slept\dslept.rc" />
583587
<ResourceCompile Include="..\samples\slept\slept.rc" />
584588
<ResourceCompile Include="..\samples\traceapi\trcapi.rc" />

0 commit comments

Comments
 (0)