Subversion Repositories WoWGM

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 tristanc 1
Microsoft Research Detours Package
2
Detours Version 3.0 Build_343
3
 
4
DISCLAIMER AND LICENSE:
5
=======================
6
The entire Detours package is covered by copyright law.
7
Copyright (c) Microsoft Corporation.  All rights reserved.
8
Portions are covered by patents owned by Microsoft Corporation.
9
 
10
Usage of the Detours package is covered under the End User License Agreement.
11
Your usage of Detours implies your acceptance of the End User License Agreement.
12
 
13
Detours 3.0 Professional, which includes rights to use Detours in commerical
14
products and production use is available through the online Microsoft Store:
15
http://www.microsoftstore.com/store/msstore/en_US/pd/productID.216531800
16
 
17
 
18
1. INTRODUCTION:
19
================
20
This document describes the installation and usage of this version of the
21
Detours package.  In particular, it provides an updated API table.
22
 
23
Complete documentation for the Detours package, including a detailed API
24
reference can be found in the Detours.chm file.
25
 
26
 
27
2. BUILD INSTRUCTIONS:
28
======================
29
If you installed Detours under the "Program Files" directory, copy the entire
30
contents of the detours directory to some other location where your account
31
has write access before attempting to build.
32
 
33
To build the libraries and the sample applications, type "nmake" in the
34
root directory of your Detours distribution.
35
 
36
If you are using Detours for the first time, a good practice is to start
37
by modifying one of the samples that is closest to your desired usage.
38
 
39
 
40
3. VERIFYING THE INSTALL AND BUILD:
41
===================================
42
After building the libraries and sample applications, you can verify that
43
the Detours packet works on your Windows OS by typing "nmake test" in the
44
samples\slept directory.  The output of "namke test" should be similar
45
to that contained in the file samples\slept\NORMAL.TXT.
46
 
47
 
48
4. CHANGES IN VERSION 3.0:
49
==========================
50
The following major changes were made in Detours 3.0 from Detours 2.x:
51
 * Support for scenarios that mix both 32-bit and 64-bit processes.
52
 * Support for ARM processors (in addition to support for X86, X64, and IA64).
53
 * Removal of the detoured.dll marker binary.
54
 * Compatibility improvements, especially on x64 processors.
55
 * Addition of APIs to enumerate PE binary Imports and to determine the
56
   module referenced by a function pointer.
57
 * Improved support for highly-optimized code on X64 processors.
58
 * Improved support for detouring hot-patchable binaries.
59
 * Improved algorithm for allocation of trampolines.
60
 
61
 
62
4.1. SUPPORT FOR MIXING 32-BIT AND 64-BIT PROCESSES:
63
====================================================
64
Previous version of Detours only supported pure 64-bit or pure 32-bit
65
environments.  Detours 3.0 includes support for creating parallel 32-bit
66
and 64-bit DLLs that can be loaded dynamically into target processes with
67
Detours automatically selecting the correct architectur DLL. The
68
DetourCreateProcessWithDllEx function selects the correct DLL based on
69
the word size (32-bit or 64-bit) of the target process.  For more
70
information see the "Detouring 32-bit and 64-bit Processes" section of
71
the Detours documentation (Detours.chm).
72
 
73
 
74
4.2. ARM SUPPORT:
75
=================
76
Detours 3.0 includes support for detouring functions on ARM processors
77
using the Thumb-2 instruction set.
78
 
79
 
80
4.3. REMOVAL OF DETOURED.DLL:
81
=============================
82
Products shipping with Detours 3.0 no longer need to include detoured.dll
83
in their dependencies.  Prior to Detours 3.0, Detours loaded the detoured.dll
84
shared library stub into any process which was modified by the insertion of
85
a detour.  This allowed the Microsoft Customer Support Services (CSS) and the
86
Microsoft Online Crash Analysis (OCA) teams to quickly and accurately
87
determine that the behavior of a process has been altered by a detour.
88
Microsoft does not provide customer assistance on detoured products.
89
 
90
With Detours 3.0, detoured.dll has been removed.  Advances in recent versions
91
of Windows allow CSS and OCA to accurately track third-party code that has
92
been loaded into a process, thus removing the need for detoured.dll.  One
93
side effect of this change is that the path to the detoured.dll is no longer
94
provided as an argument to DetourCreateProcessWithDll, reducing it's argument
95
count by one.
96
 
97
 
98
4.4. COMPATIBILITY IMPROVEMENTS:
99
================================
100
Fixes have been made in Detours 3.0 to improve support for target binaries
101
containing no DLL imports, DLL binaries compiled for online-patching,
102
binaries generated with hot-patching support, and 64-bit PE binaries
103
containing managed code.
104
 
105
 
106
4.5. APIS TO ENUMERATE PE BINARY IMPORTS:
107
=========================================
108
Added DetourEnumerateImports API to enumerate the functions imported by a
109
EXE or DLL.  Given a pointer to a function, the DetourGetContainingModule
110
API will return the HMODULE of the binary within which it resides.
111
 
112
 
113
4.6. TRANSACTIONAL MODEL AND THREAD UPDATE:
114
===========================================
115
Typically, a developer uses the Detours package to detour a family of
116
functions.  Race conditions can be introduced into the detour code as the
117
target functions are detoured one by one.  Also, the developer typically
118
wants a error model in which all target functions are detoured entirely or
119
none of the target functions are detoured if a particular function can't be
120
detoured.  In previous version of Detours, programmers either ignored
121
these race and error conditions, or attempted to avoid them by carefully
122
timing the insertion and deletion of detours.
123
 
124
To simplify the development model, Detours 3.0 uses a transactional model for
125
attaching and detaching detours.  Your code should call DetourTransactionBegin
126
to begin a transaction, issue a group of DetourAttach or DetourDetach calls to
127
affect the desired target functions, call DetourUpdateThread to mark threads
128
which may be affected by the updates, and then call
129
DetourTransactionCommit to complete the operation.
130
 
131
When DetourTransactionCommit is called, Detours suspends all effected
132
threads (except the calling thread), insert or removes the detours as
133
specified, updates the program counter for any threads that were running
134
inside the affected functions, then resumes the affected threads. If an error
135
occurs during the transaction, or if DetourTransactioAbort is called, Detours
136
safely aborts all of the operations within the transaction. From the perspective
137
of all threads marks marked for update, the entire transaction is atomic,
138
either all threads and functions are modified, or none are modified.
139
 
140
 
141
4.7. 64-BIT SUPPORT:
142
====================
143
Detours includes support for 64-bit execution on X64 and IA64 processors.
144
Detours understands the new 64-bit instructions of the X64 and IA64 and can
145
detour 64-bit code when used in a 64-bit process.
146
 
147
 
148
4.8. ALLOCATION OF TRAMPOLINES:
149
===============================
150
To intercept calls, Detours copies the first few instructions of the target
151
function into a block of memory called a trampoline.  In previous versions
152
of Detours, trampolines were allocated a close as possible to the target
153
code.  In some cases, the trampoline memory would conflict with later
154
DLL loads in the same process.  This would hurt performance by causing
155
later DLLs to be dynamically rebased.  Detours 3.0 includes a new algorithm
156
for allocating trampolines.  The new algorithm attempts to place trampolines
157
roughly 1GB above or below the target code in the address space.
158
 
159
 
160
5. API SUMMARY:
161
===============
162
 
163
5.1. APIS FOR DETOURING TARGET FUNCTIONS:
164
=========================================
165
DetourTransactionBegin()    - Begin a new detour transaction.
166
 
167
DetourUpdateThread()        - Mark a thread that should be included in the
168
                              current detour transaction.
169
 
170
DetourAttach()              - Attach a detour to a target function as part
171
                              of the current detour transaction.
172
 
173
DetourAttachEx()            - Attach a detour to a target function and
174
                              retrieved additional detail about the ultimate
175
                              target as part of the current detour transaction.
176
 
177
DetourDetach()              - Detach a detour from a target function as part
178
                              of the current detour transaction.
179
 
180
DetourSetIgnoreTooSmall()   - Set the flag to determine if failure to detour
181
                              a target function that is too small for detouring
182
                              is sufficient error to cause abort of the current
183
                              detour transaction.
184
 
185
DetourTransactionAbort()    - Abort the current detour transaction.
186
 
187
DetourTransactionCommit()   - Attempt to commit the current detour transaction.
188
 
189
DetourTransactionCommitEx() - Attempt to commit the current transaction, if
190
                              transaction fails, retrieve error information.
191
 
192
 
193
5.2. APIS FOR FINDING TARGETS:
194
==============================
195
DetourFindFunction()        - Tries to retrieve a function pointer for a named
196
                              function through the dynamic linking export
197
                              tables for the named module and then, if that
198
                              fails, through debugging symbols if available.
199
 
200
DetourCodeFromPointer()     - Given a function pointer, returns a pointer to the
201
                              code implementing the function.  Skips over extra
202
                              code often inserted by linkers or compilers for
203
                              cross-DLL calls.
204
 
205
 
206
5.3. APIS FOR FINDING ACCESSING LOADED BINARIES AND PAYLOADS:
207
=============================================================
208
DetourEnumerateModules()    - Enumerates all of the PE binaries loaded into a
209
                              process.
210
 
211
DetourGetContainingModule() - Return the module containing a function.
212
 
213
DetourGetEntryPoint()       - Returns a pointer the entry point for a module.
214
 
215
DetourGetModuleSize()       - Returns the load size of a module.
216
 
217
DetourEnumerateExports()    - Enumerates all exports from a module.
218
 
219
DetourEnumerateImports()    - Enumerates all import dependencies of a module.
220
 
221
DetourFindPayload()         - Finds the address of the specified payload
222
                              within a module.
223
 
224
DetourFindPayloadEx()       - Finds the specified payload if it exists anywhere
225
                              in the process.
226
 
227
DetourGetSizeOfPayloads()   - Returns the size of all payloads within a
228
                              module.
229
 
230
 
231
5.4. APIS FOR MODIFYING BINARIES:
232
=================================
233
DetourBinaryOpen()          - Open a binary for in-memory update.
234
 
235
DetourBinaryEnumeratePayloads() - Enumerats all of the payloads in a binary.
236
 
237
DetourBinaryFindPayload()   - Finds a specific payload within a binary.
238
 
239
DetourBinarySetPayload()    - Attaches a payload to a binary.
240
 
241
DetourBinaryDeletePayload() - Removes a payload from a binary.
242
 
243
DetourBinaryPurgePayloads() - Removes all payloads from a binary.
244
 
245
DetourBinaryEditImports()   - Edits the import tables of a binary.
246
 
247
DetourBinaryResetImports()  - Removes all edits to the import tables of a
248
                              binary including any edits made by previous
249
                              programs using the Detours package.
250
 
251
DetourBinaryWrite()         - Writes the updated binary to a file.
252
 
253
DetourBinaryClose()         - Release the in-memory updates for a binary.
254
 
255
DetourBinaryBind()          - Binds the DLL imports for a named binary file.
256
 
257
 
258
5.5. APIS FOR INSERTING DLLS INTO PROCESSES:
259
============================================
260
DetourCreateProcessWithDll() - Creates a new process with the specified
261
                               DLL inserted into it.
262
 
263
DetourCreateProcessWithDllEx() - Creates a new process with a DLL inserted,
264
                                 selecting a 32-bit DLL for a 32-bit target
265
                                 process or a 64-bit DLL for a 64-bit target
266
                                 process.
267
 
268
DetourRestoreAfterWith()     - Restores the contents of the in memory import
269
                               table after a process was started with
270
                               DetourCreateProcessWithDll.
271
 
272
DetourIsHelperProcess()      - Determines if a DLL is being loaded by rundll32
273
                               in a helper process in order to make the
274
                               transition from a 32-bit process to a 64-bit
275
                               process or from a 64-bit process to a 32-bit
276
                               process.
277
 
278
DetourFinishHelperProcess()  - Completes the operations of a helper process.
279
 
280
 
281
6. COMPATIBILITY:
282
=================
283
All Detours functions are compatible with all versions of Microsoft Windows
284
Operating Systems with the supported service packs installed. This includes
285
Windows XP, Windows Server 2003, Windows Server 2003 R2, Windows Vista,
286
Windows Server 2008, Windows 7, Windows Server 2008 R2, and Windows 8.
287
 
288
 
289
7. MANIFEST:
290
============
291
The Detours package consists of the Detours library source code and a number
292
of sample programs.  Descriptions of the sample programs can be found in
293
the help file.
294
 
295
 
296
8. NOTES:
297
=========
298
When writing detour functions, it is imperative that the binary-calling
299
convention of the detour and trampoline functions match exactly the
300
binary-calling convention of the target function.
301
 
302
In a few cases, when the sizeof() a return value is smaller than sizeof(int),
303
C or C++ compilers will generate non-compatible binary-calling conventions by
304
not widening the return value to an int as is customary for small return values.
305
The result is a syntactically-identical, but not binary-compatible, detour
306
function.  In most cases, the problem can be fixed be having the detour function
307
return a value widened to a sizeof(int) type.  Developers are urged to exercise
308
caution, and should insure that correct code is generated by their C or C++
309
compiler for detour functions with small return values.
310
 
311
When attaching a DLL to a binary with Detours DLL import APIs, the DLL must
312
export one procedure with export ordinal 1.  The exported procedure is not
313
called by the application, but it used as the import target.
314
 
315
Detours requires a compiler compatible with Visual C++.NET or later.
316
 
317
 
318
9. BUG REPORTS:
319
===============
320
Please send detailed bug reports to detours@microsoft.com.  Submitted bug
321
reports may be used to fix bugs in future versions of the Detours package.
322
Please include the text "DETOURS BUG REPORT" in the subject line. Please
323
also include the first line of this README.TXT file containing the full
324
version description information.  The detours@microsoft.com email address
325
is not a product support line.