forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleapi.cpp
More file actions
339 lines (279 loc) · 8.23 KB
/
handleapi.cpp
File metadata and controls
339 lines (279 loc) · 8.23 KB
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
handleapi.cpp
Abstract:
Implementation of the handle management APIs
--*/
#include "pal/handleapi.hpp"
#include "pal/handlemgr.hpp"
#include "pal/thread.hpp"
#include "pal/procobj.hpp"
#include "pal/dbgmsg.h"
#include "pal/process.h"
using namespace CorUnix;
SET_DEFAULT_DEBUG_CHANNEL(HANDLE);
CAllowedObjectTypes aotDuplicateHandle PAL_GLOBAL(TRUE);
PAL_ERROR
CloseSpecialHandle(
HANDLE hObject
);
/*++
Function:
DuplicateHandle
See MSDN doc.
PAL-specific behavior :
-Source and Target process needs to be the current process.
-lpTargetHandle must be non-NULL
-dwDesiredAccess is ignored
-bInheritHandle must be FALSE
-dwOptions must be a combo of DUPLICATE_SAME_ACCESS and
DUPLICATE_CLOSE_SOURCE
--*/
BOOL
PALAPI
DuplicateHandle(
IN HANDLE hSourceProcessHandle,
IN HANDLE hSourceHandle,
IN HANDLE hTargetProcessHandle,
OUT LPHANDLE lpTargetHandle,
IN DWORD dwDesiredAccess,
IN BOOL bInheritHandle,
IN DWORD dwOptions)
{
PAL_ERROR palError;
CPalThread *pThread;
PERF_ENTRY(DuplicateHandle);
ENTRY("DuplicateHandle( hSrcProcHandle=%p, hSrcHandle=%p, "
"hTargetProcHandle=%p, lpTargetHandle=%p, dwAccess=%#x, "
"bInheritHandle=%d, dwOptions=%#x) \n", hSourceProcessHandle,
hSourceHandle, hTargetProcessHandle, lpTargetHandle,
dwDesiredAccess, bInheritHandle, dwOptions);
pThread = InternalGetCurrentThread();
palError = InternalDuplicateHandle(
pThread,
hSourceProcessHandle,
hSourceHandle,
hTargetProcessHandle,
lpTargetHandle,
dwDesiredAccess,
bInheritHandle,
dwOptions
);
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT("DuplicateHandle returns BOOL %d\n", (NO_ERROR == palError));
PERF_EXIT(DuplicateHandle);
return (NO_ERROR == palError);
}
PAL_ERROR
CorUnix::InternalDuplicateHandle(
CPalThread *pThread,
HANDLE hSourceProcess,
HANDLE hSource,
HANDLE hTargetProcess,
LPHANDLE phDuplicate,
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwOptions
)
{
PAL_ERROR palError = NO_ERROR;
IPalObject *pobjSource = NULL;
DWORD source_process_id;
DWORD target_process_id;
DWORD cur_process_id;
cur_process_id = GetCurrentProcessId();
source_process_id = PROCGetProcessIDFromHandle(hSourceProcess);
target_process_id = PROCGetProcessIDFromHandle(hTargetProcess);
/* Check validity of process handles */
if (0 == source_process_id || 0 == target_process_id)
{
ASSERT("Can't duplicate handle: invalid source or destination process");
palError = ERROR_INVALID_PARAMETER;
goto InternalDuplicateHandleExit;
}
/* At least source or target process should be the current process. */
if (source_process_id != cur_process_id
&& target_process_id != cur_process_id)
{
ASSERT("Can't duplicate handle : neither source or destination"
"processes are from current process");
palError = ERROR_INVALID_PARAMETER;
goto InternalDuplicateHandleExit;
}
if (FALSE != bInheritHandle)
{
ASSERT("Can't duplicate handle : bInheritHandle is not FALSE.\n");
palError = ERROR_INVALID_PARAMETER;
goto InternalDuplicateHandleExit;
}
if (dwOptions & ~(DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
{
ASSERT(
"Can't duplicate handle : dwOptions is %#x which is not "
"a subset of (DUPLICATE_SAME_ACCESS|DUPLICATE_CLOSE_SOURCE) "
"(%#x).\n",
dwOptions,
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
palError = ERROR_INVALID_PARAMETER;
goto InternalDuplicateHandleExit;
}
if (0 == (dwOptions & DUPLICATE_SAME_ACCESS))
{
ASSERT(
"Can't duplicate handle : dwOptions is %#x which does not "
"include DUPLICATE_SAME_ACCESS (%#x).\n",
dwOptions,
DUPLICATE_SAME_ACCESS);
palError = ERROR_INVALID_PARAMETER;
goto InternalDuplicateHandleExit;
}
if (NULL == phDuplicate)
{
ASSERT("Can't duplicate handle : lpTargetHandle is NULL.\n");
goto InternalDuplicateHandleExit;
}
/* Since handles can be remoted to others processes using PAL_LocalHsndleToRemote
and PAL_RemoteHandleToLocal, DuplicateHandle needs some special handling
when this scenario occurs.
if hSourceProcessHandle is from another process OR
hTargetProcessHandle is from another process but both aren't
( handled above ) return hSourceHandle.
*/
if (source_process_id != cur_process_id
|| target_process_id != cur_process_id)
{
*phDuplicate = hSource;
palError = NO_ERROR;
goto InternalDuplicateHandleExit;
}
//
// Obtain the source IPalObject
//
if (!HandleIsSpecial(hSource))
{
palError = g_pObjectManager->ReferenceObjectByHandle(
pThread,
hSource,
&aotDuplicateHandle,
dwDesiredAccess,
&pobjSource
);
if (NO_ERROR != palError)
{
ERROR("Unable to get object for source handle %p (%i)\n", hSource, palError);
goto InternalDuplicateHandleExit;
}
}
else if (hPseudoCurrentProcess == hSource)
{
TRACE("Duplicating process pseudo handle(%p)\n", hSource);
pobjSource = g_pobjProcess;
pobjSource->AddReference();
}
else if (hPseudoCurrentThread == hSource)
{
TRACE("Duplicating thread pseudo handle(%p)\n", hSource);
pobjSource = pThread->GetThreadObject();
pobjSource->AddReference();
}
else
{
ASSERT("Duplication not supported for this special handle (%p)\n", hSource);
palError = ERROR_INVALID_HANDLE;
goto InternalDuplicateHandleExit;
}
palError = g_pObjectManager->ObtainHandleForObject(
pThread,
pobjSource,
dwDesiredAccess,
bInheritHandle,
NULL,
phDuplicate
);
InternalDuplicateHandleExit:
if (NULL != pobjSource)
{
pobjSource->ReleaseReference(pThread);
}
if (dwOptions & DUPLICATE_CLOSE_SOURCE)
{
//
// Since DUPLICATE_CLOSE_SOURCE was specified the source handle
// MUST be closed, even if an error occurred during the duplication
// process
//
TRACE("DuplicateHandle closing source handle %p\n", hSource);
InternalCloseHandle(pThread, hSource);
}
return palError;
}
/*++
Function:
CloseHandle
See MSDN doc.
Note : according to MSDN, FALSE is returned in case of error. But also
according to MSDN, closing an invalid handle raises an exception when running a
debugger [or, alternately, if a special registry key is set]. This behavior is
not required in the PAL, so we'll always return FALSE.
--*/
BOOL
PALAPI
CloseHandle(
IN OUT HANDLE hObject)
{
CPalThread *pThread;
PAL_ERROR palError;
PERF_ENTRY(CloseHandle);
ENTRY("CloseHandle (hObject=%p) \n", hObject);
pThread = InternalGetCurrentThread();
palError = InternalCloseHandle(
pThread,
hObject
);
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT("CloseHandle returns BOOL %d\n", (NO_ERROR == palError));
PERF_EXIT(CloseHandle);
return (NO_ERROR == palError);
}
PAL_ERROR
CorUnix::InternalCloseHandle(
CPalThread * pThread,
HANDLE hObject
)
{
PAL_ERROR palError = NO_ERROR;
if (!HandleIsSpecial(hObject))
{
palError = g_pObjectManager->RevokeHandle(
pThread,
hObject
);
}
else
{
palError = CloseSpecialHandle(hObject);
}
return palError;
}
PAL_ERROR
CloseSpecialHandle(
HANDLE hObject
)
{
if ((hObject == hPseudoCurrentThread) ||
(hObject == hPseudoCurrentProcess))
{
return NO_ERROR;
}
return ERROR_INVALID_HANDLE;
}