-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDetourLoadLibrary.cpp
208 lines (157 loc) · 6 KB
/
DetourLoadLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//==============================================================================
// Copyright (c) 2015 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief This file contains the functions to detour LoadLibrary.
//==============================================================================
#include <string>
#include <windows.h>
#include <tchar.h>
#include <sstream>
#include "Interceptor.h"
#include "PluginInfo.h"
#include "Fileutils.h"
#include "Logger.h"
using namespace GPULogger;
static bool s_bLoadLibraryAttached = false;
typedef BOOL (WINAPI* FreeLibrary_type)(HMODULE hLibModule);
static FreeLibrary_type Real_FreeLibrary = FreeLibrary;
typedef HMODULE(WINAPI* LoadLibraryA_type)(LPCSTR lpLibFileName);
static LoadLibraryA_type Real_LoadLibraryA = LoadLibraryA;
typedef HMODULE(WINAPI* LoadLibraryExA_type)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
static LoadLibraryExA_type Real_LoadLibraryExA = LoadLibraryExA;
typedef HMODULE(WINAPI* LoadLibraryW_type)(LPCWSTR lpLibFileName);
static LoadLibraryW_type Real_LoadLibraryW = LoadLibraryW;
typedef HMODULE(WINAPI* LoadLibraryExW_type)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
static LoadLibraryExW_type Real_LoadLibraryExW = LoadLibraryExW;
static bool s_bD3D11CreateDeviceAttached = false;
static bool s_bD3DX11CompileShaderAttached = false;
static BOOL WINAPI Mine_FreeLibrary(HMODULE hLibModule)
{
Log(traceMESSAGE, "Detoured FreeLibrary called, HOMDULE: %p", hLibModule);
BOOL retVal = Real_FreeLibrary(hLibModule);
DWORD realError = GetLastError();
CheckOnFreeLibrary();
SetLastError(realError);
return retVal;
}
static HMODULE WINAPI Mine_LoadLibraryA(LPCSTR lpLibFileName)
{
HMODULE res = Real_LoadLibraryA(lpLibFileName);
Log(traceMESSAGE, "Detoured LoadLibraryA called: %s, HOMDULE: %p\n", lpLibFileName, res);
DWORD realError = GetLastError();
#ifdef _DEBUG
if (lstrcmpA(lpLibFileName, "comctl32.dll") != 0)
{
// This function uses MessageBox - which will load comctl32.dll if necessary
// To avoid recursion problems, only call it when a different DLL is being loaded.
FileUtils::CheckForDebuggerAttach();
}
#endif
CheckOnLoadLibrary();
SetLastError(realError);
return res;
}
static HMODULE WINAPI Mine_LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
HMODULE res = Real_LoadLibraryExA(lpLibFileName, hFile, dwFlags);
Log(traceMESSAGE, "Detoured LoadLibraryExA called: %s, HOMDULE: %p\n", lpLibFileName, res);
DWORD realError = GetLastError();
#ifdef _DEBUG
if (lstrcmpA(lpLibFileName, "comctl32.dll") != 0)
{
// This function uses MessageBox - which will load comctl32.dll if necessary
// To avoid recursion problems, only call it when a differenct DLL is being loaded.
FileUtils::CheckForDebuggerAttach();
}
#endif
CheckOnLoadLibrary();
SetLastError(realError);
return res;
}
static HMODULE WINAPI Mine_LoadLibraryW(LPCWSTR lpLibFileName)
{
HMODULE res = Real_LoadLibraryW(lpLibFileName);
LogW(traceMESSAGE, L"Detoured LoadLibraryW called: %s, HOMDULE: %p\n", lpLibFileName, res);
DWORD realError = GetLastError();
#ifdef _DEBUG
if (lstrcmpW(lpLibFileName, L"comctl32.dll") != 0)
{
// This function uses MessageBox - which will load comctl32.dll if necessary
// To avoid recursion problems, only call it when a differenct DLL is being loaded.
FileUtils::CheckForDebuggerAttach();
}
#endif
CheckOnLoadLibrary();
SetLastError(realError);
return res;
}
static HMODULE WINAPI Mine_LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
HMODULE res = Real_LoadLibraryExW(lpLibFileName, hFile, dwFlags);
LogW(traceMESSAGE, L"Detoured LoadLibraryExW called: %s, HOMDULE: %p\n", lpLibFileName, res);
DWORD realError = GetLastError();
#ifdef _DEBUG
if (lstrcmpW(lpLibFileName, L"comctl32.dll") != 0)
{
// This function uses MessageBox - which will load comctl32.dll if necessary
// To avoid recursion problems, only call it when a differenct DLL is being loaded.
FileUtils::CheckForDebuggerAttach();
}
#endif
CheckOnLoadLibrary();
SetLastError(realError);
return res;
}
bool DetoursAttachLoadLibrary()
{
if (s_bLoadLibraryAttached)
{
return true;
}
LONG error = AMDT::BeginHook();
if (NO_ERROR == error)
{
error |= AMDT::HookAPICall(&(PVOID&)Real_LoadLibraryA, Mine_LoadLibraryA);
error |= AMDT::HookAPICall(&(PVOID&)Real_LoadLibraryExA, Mine_LoadLibraryExA);
error |= AMDT::HookAPICall(&(PVOID&)Real_LoadLibraryW, Mine_LoadLibraryW);
error |= AMDT::HookAPICall(&(PVOID&)Real_LoadLibraryExW, Mine_LoadLibraryExW);
error |= AMDT::HookAPICall(&(PVOID&)Real_FreeLibrary, Mine_FreeLibrary);
error |= AMDT::EndHook();
}
s_bLoadLibraryAttached = true;
if (NO_ERROR != error)
{
return false;
}
return true;
}
bool DetoursDetachLoadLibrary()
{
if (!s_bLoadLibraryAttached)
{
return true;
}
LONG error = AMDT::BeginHook();
if (NO_ERROR == error)
{
error |= AMDT::UnhookAPICall(&(PVOID&)Real_LoadLibraryA, Mine_LoadLibraryA);
error |= AMDT::UnhookAPICall(&(PVOID&)Real_LoadLibraryExA, Mine_LoadLibraryExA);
error |= AMDT::UnhookAPICall(&(PVOID&)Real_LoadLibraryW, Mine_LoadLibraryW);
error |= AMDT::UnhookAPICall(&(PVOID&)Real_LoadLibraryExW, Mine_LoadLibraryExW);
error |= AMDT::UnhookAPICall(&(PVOID&)Real_FreeLibrary, Mine_FreeLibrary);
error |= AMDT::EndHook();
}
if (NO_ERROR != error)
{
return false;
}
/* Restore detoured functions to original values to work around a detours bug */
Real_LoadLibraryA = LoadLibraryA;
Real_LoadLibraryExA = LoadLibraryExA;
Real_LoadLibraryW = LoadLibraryW;
Real_LoadLibraryExW = LoadLibraryExW;
Real_FreeLibrary = FreeLibrary;
s_bLoadLibraryAttached = false;
return true;
}