-
-
Notifications
You must be signed in to change notification settings - Fork 565
/
HandleKeeper.cpp
352 lines (305 loc) · 9.42 KB
/
HandleKeeper.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright (c) 2015-2017 Maximus5
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define HIDE_USE_EXCEPTION_INFO
#include <windows.h>
#include "HandleKeeper.h"
#include "WErrGuard.h"
#ifdef _DEBUG
#include "MStrDup.h"
#endif
namespace HandleKeeper
{
static MMap<HANDLE,HandleInformation>* gp_Handles = NULL;
static bool gb_IsConnector = false;
static bool InitHandleStorage();
static void FillHandleInfo(HandleInformation& Info, DWORD access);
static bool CheckConName(HandleSource source, DWORD access, const VOID* as_name, HandleInformation& Info);
};
// MT-Safe, lock-free
static bool HandleKeeper::InitHandleStorage()
{
if (gp_Handles == NULL)
{
MMap<HANDLE,HandleInformation>* pHandles = (MMap<HANDLE,HandleInformation>*)malloc(sizeof(*pHandles));
if (pHandles)
{
if (!pHandles->Init(256/*may be auto-enlarged*/, true/*OnCreate*/))
{
pHandles->Release();
free(pHandles);
}
else
{
MMap<HANDLE,HandleInformation>* p = (MMap<HANDLE,HandleInformation>*)InterlockedCompareExchangePointer((LPVOID*)&gp_Handles, pHandles, NULL);
_ASSERTE(p == NULL || p != pHandles);
if (pHandles != gp_Handles)
{
pHandles->Release();
free(pHandles);
}
}
}
}
return (gp_Handles != NULL);
}
void HandleKeeper::ReleaseHandleStorage()
{
MMap<HANDLE,HandleInformation>* p = (MMap<HANDLE,HandleInformation>*)InterlockedExchangePointer((LPVOID*)&gp_Handles, NULL);
if (p)
{
p->Release();
free(p);
}
}
void HandleKeeper::SetConnectorMode(bool isConnector)
{
gb_IsConnector = isConnector;
}
// Check if handle has console capabilities
static void HandleKeeper::FillHandleInfo(HandleInformation& Info, DWORD access)
{
// We can't set is_input/is_output/is_error by Info.source (hs_StdXXX)
// because STD_XXX handles may be pipes or files
// Is handle capable to full set of console API?
BOOL b1, b2; DWORD m; CONSOLE_SCREEN_BUFFER_INFO csbi = {}; DEBUGTEST(DWORD err;)
b1 = GetConsoleMode(Info.h, &m);
if (!b1)
{
#ifdef _DEBUG
Info.errorPlace = 1;
Info.errorCode = GetLastError();
#endif
// In connector mode all handles are expected to be AnsiCapable!
_ASSERTE(!gb_IsConnector);
}
else
{
b2 = GetConsoleScreenBufferInfo(Info.h, &csbi);
DEBUGTEST(err = GetLastError());
if (b2)
{
if (!Info.is_output && !Info.is_error)
{
if (Info.source == hs_StdErr)
Info.is_error = true;
else
Info.is_output = true;
}
_ASSERTE(Info.is_input==false);
Info.is_input = false;
Info.is_ansi = true;
#ifdef _DEBUG
Info.csbi = csbi;
#endif
}
else
{
#ifdef _DEBUG
Info.errorPlace = 2;
Info.errorCode = GetLastError();
#endif
if (Info.source == hs_StdIn && !Info.is_input)
Info.is_input = true;
// In connector mode all handles are expected to be AnsiCapable!
_ASSERTE(!gb_IsConnector);
_ASSERTE(Info.is_ansi == false);
//_ASSERTE(Info.is_output == false && Info.is_error == false); -- STD_OUTPUT_HANDLE can be "virtual"
}
}
// Input/Output handles must have either GENERIC_READ or GENERIC_WRITE rights
if ((Info.source > hs_StdErr) && !(access & (GENERIC_WRITE|GENERIC_READ)))
{
Info.is_invalid = true;
}
// Generic handle "CON" opened for output (redirection to console)
else if (Info.is_con && (access & GENERIC_WRITE))
{
Info.is_output = true;
}
}
// Check if as_name is "CON", "CONIN$" or "CONOUT$"
static bool HandleKeeper::CheckConName(HandleSource source, DWORD access, const VOID* as_name, HandleInformation& Info)
{
// Just a simple check for "string" validity
if (as_name && (((DWORD_PTR)as_name) & ~0xFFFF))
{
if (source == hs_CreateFileW)
{
if (access & (GENERIC_READ|GENERIC_WRITE))
{
Info.is_con = (lstrcmpiW((LPCWSTR)as_name, L"CON") == 0);
Info.is_input = (lstrcmpiW((LPCWSTR)as_name, L"CONIN$") == 0);
Info.is_output = (lstrcmpiW((LPCWSTR)as_name, L"CONOUT$") == 0);
}
#ifdef _DEBUG
Info.file_name_w = lstrdup((LPCWSTR)as_name); // debugging and informational purposes only
#endif
return (Info.is_con || Info.is_input || Info.is_output);
}
else if (source == hs_CreateFileA)
{
if (access & (GENERIC_READ|GENERIC_WRITE))
{
Info.is_con = (lstrcmpiA((LPCSTR)as_name, "CON") == 0);
Info.is_input = (lstrcmpiA((LPCSTR)as_name, "CONIN$") == 0);
Info.is_output = (lstrcmpiA((LPCSTR)as_name, "CONOUT$") == 0);
}
#ifdef _DEBUG
Info.file_name_a = lstrdup((LPCSTR)as_name); // debugging and informational purposes only
#endif
return (Info.is_con || Info.is_input || Info.is_output);
}
}
return false;
}
bool HandleKeeper::PreCreateHandle(HandleSource source, DWORD& dwDesiredAccess, DWORD& dwShareMode, const void** as_name)
{
if ((source != hs_CreateFileA) && (source != hs_CreateFileW))
{
_ASSERTE((source == hs_CreateFileA) || (source == hs_CreateFileW));
return false;
}
if (!as_name || !*as_name)
{
_ASSERTE(as_name && *as_name);
return false;
}
HandleInformation Info = {};
// Check if as_name is "CON", "CONIN$" or "CONOUT$"
if (CheckConName(source, dwDesiredAccess, *as_name, Info))
{
// If handle is opened with limited right, console API would be very limited
// and we even can't query current console window properties (cursor pos, buffer size, etc.)
// This hack may be removed if we rewrite all console API functions completely
if (Info.is_output
|| (Info.is_con && (dwDesiredAccess & GENERIC_WRITE))
)
{
if ((dwDesiredAccess & (GENERIC_READ|GENERIC_WRITE)) != (GENERIC_READ|GENERIC_WRITE))
dwDesiredAccess |= (GENERIC_READ|GENERIC_WRITE);
if ((dwShareMode & (FILE_SHARE_READ|FILE_SHARE_WRITE)) != (FILE_SHARE_READ|FILE_SHARE_WRITE))
dwShareMode |= (FILE_SHARE_READ|FILE_SHARE_WRITE);
// We have to change opening name to 'CONOUT$' or CreateFile will fail
if (Info.is_con)
{
static char szConOut[] = "CONOUT$";
static wchar_t wszConOut[] = L"CONOUT$";
if (source == hs_CreateFileA)
*as_name = szConOut;
else if (source == hs_CreateFileW)
*as_name = wszConOut;
}
}
}
#ifdef _DEBUG
SafeFree(Info.file_name_ptr);
#endif
return false;
}
bool HandleKeeper::AllocHandleInfo(HANDLE h, HandleSource source, DWORD access, const void* as_name, HandleInformation* pInfo /*= NULL*/)
{
if (!h || (h == INVALID_HANDLE_VALUE))
return false;
if (!InitHandleStorage())
return false;
HandleInformation Info = {h};
Info.source = source;
// Check if as_name is "CON", "CONIN$" or "CONOUT$"
if ((((source == hs_CreateFileA) || (source == hs_CreateFileW))
&& CheckConName(source, access, as_name, Info))
|| (source <= hs_CreateConsoleScreenBuffer)
)
{
// Check if handle has console capabilities
FillHandleInfo(Info, access);
}
if (pInfo)
*pInfo = Info;
gp_Handles->Set(h, Info);
return true;
}
bool HandleKeeper::QueryHandleInfo(HANDLE h, HandleInformation& Info, bool AnsiOutExpected)
{
CLastErrorGuard errGuard;
if (!h || (h == INVALID_HANDLE_VALUE))
return false;
bool bFound = false;
if (gp_Handles)
{
bFound = gp_Handles->Get(h, &Info);
}
if (bFound && AnsiOutExpected)
{
// Force to refresh handle information
if (gb_IsConnector && (!Info.is_ansi || !(Info.is_output || Info.is_error)))
{
// In connector mode all handles are expected to be AnsiCapable!
_ASSERTE((Info.is_ansi && (Info.is_output || Info.is_error)) || !gb_IsConnector);
bFound = false;
}
}
if (!bFound)
bFound = AllocHandleInfo(h, hs_Unknown, 0, NULL, &Info);
return bFound;
}
void HandleKeeper::CloseHandleInfo(HANDLE h)
{
HandleInformation Info = {};
if (gp_Handles)
{
if (gp_Handles->Get(h, &Info, true))
{
#ifdef _DEBUG
if (Info.file_name_ptr)
free(Info.file_name_ptr)
#endif
;
}
}
}
bool HandleKeeper::IsAnsiCapable(HANDLE hFile, bool *pbIsConOut)
{
HandleInformation Info = {};
if (!QueryHandleInfo(hFile, Info, true))
return false;
if (pbIsConOut)
*pbIsConOut = (Info.is_output || Info.is_error);
return (Info.is_ansi);
}
bool HandleKeeper::IsOutputHandle(HANDLE hFile)
{
HandleInformation Info = {};
if (!QueryHandleInfo(hFile, Info, true))
return false;
return (Info.is_output || Info.is_error);
}
bool HandleKeeper::IsInputHandle(HANDLE hFile)
{
HandleInformation Info = {};
if (!QueryHandleInfo(hFile, Info, false))
return false;
return (Info.is_input);
}