OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 | 7 |
8 // NaCl inter-module communication primitives. | 8 // NaCl inter-module communication primitives. |
9 | 9 |
10 #ifndef NOMINMAX | 10 #ifndef NOMINMAX |
11 #define NOMINMAX // Disables the generation of the min and max macro in | 11 #define NOMINMAX // Disables the generation of the min and max macro in |
12 // <windows.h>. | 12 // <windows.h>. |
13 #endif | 13 #endif |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 #include <ctype.h> | 16 #include <ctype.h> |
17 #include <limits.h> | 17 #include <limits.h> |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <string> | 19 #include <string> |
20 #include <windows.h> | 20 #include <windows.h> |
21 #include <sys/types.h> | 21 #include <sys/types.h> |
22 | 22 |
23 #include "native_client/src/include/atomic_ops.h" | 23 #include "native_client/src/include/atomic_ops.h" |
24 #include "native_client/src/include/portability.h" | 24 #include "native_client/src/include/portability.h" |
25 #include "native_client/src/shared/imc/nacl_imc.h" | 25 #include "native_client/src/shared/imc/nacl_imc.h" |
26 #include "native_client/src/shared/imc/nacl_imc_c.h" | 26 #include "native_client/src/shared/imc/nacl_imc_c.h" |
27 #include "native_client/src/trusted/handle_pass/handle_lookup.h" | 27 #include "native_client/src/trusted/handle_pass/handle_lookup.h" |
28 | 28 |
29 // Duplicate a Windows HANDLE. | 29 |
30 static NaClBrokerDuplicateHandleFunc g_broker_duplicate_handle_func; | |
sehr (please use chromium)
2012/04/10 21:27:35
I think the general preference is for an anonymous
Mark Seaborn
2012/04/10 21:47:23
Done.
| |
31 | |
32 void NaClSetBrokerDuplicateHandleFunc(NaClBrokerDuplicateHandleFunc func) { | |
33 g_broker_duplicate_handle_func = func; | |
34 } | |
35 | |
36 // Duplicate a Windows HANDLE within the current process. | |
30 NaClHandle NaClDuplicateNaClHandle(NaClHandle handle) { | 37 NaClHandle NaClDuplicateNaClHandle(NaClHandle handle) { |
31 NaClHandle dup_handle; | 38 NaClHandle dup_handle; |
32 if (DuplicateHandle(GetCurrentProcess(), | 39 if (DuplicateHandle(GetCurrentProcess(), |
33 handle, | 40 handle, |
34 GetCurrentProcess(), | 41 GetCurrentProcess(), |
35 &dup_handle, | 42 &dup_handle, |
36 0, | 43 0, |
37 FALSE, | 44 FALSE, |
38 DUPLICATE_SAME_ACCESS)) { | 45 DUPLICATE_SAME_ACCESS)) { |
39 return dup_handle; | 46 return dup_handle; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 } | 263 } |
257 if (0 < message->handle_count && message->handles) { | 264 if (0 < message->handle_count && message->handles) { |
258 // TODO(shiki): On Windows Vista, we can use GetNamedPipeClientProcessId() | 265 // TODO(shiki): On Windows Vista, we can use GetNamedPipeClientProcessId() |
259 // and GetNamedPipeServerProcessId() and probably we can remove | 266 // and GetNamedPipeServerProcessId() and probably we can remove |
260 // kEchoRequest and kEchoResponse completely. | 267 // kEchoRequest and kEchoResponse completely. |
261 if (WriteAll(handle, &header, sizeof header) != sizeof header || | 268 if (WriteAll(handle, &header, sizeof header) != sizeof header || |
262 ReadAll(handle, &header, sizeof header) != sizeof header || | 269 ReadAll(handle, &header, sizeof header) != sizeof header || |
263 header.command != kEchoResponse) { | 270 header.command != kEchoResponse) { |
264 return -1; | 271 return -1; |
265 } | 272 } |
273 HANDLE target; | |
274 if (g_broker_duplicate_handle_func == NULL) { | |
275 // TODO(mseaborn): Remove these non-NACL_STANDALONE cases. | |
276 // Chromium is being changed to supply g_broker_duplicate_handle_func. | |
266 #ifdef NACL_STANDALONE // not in Chrome | 277 #ifdef NACL_STANDALONE // not in Chrome |
267 HANDLE target = OpenProcess(PROCESS_DUP_HANDLE, FALSE, header.pid); | 278 target = OpenProcess(PROCESS_DUP_HANDLE, FALSE, header.pid); |
268 #else | 279 #else |
269 HANDLE target = NaClHandlePassLookupHandle(header.pid); | 280 target = NaClHandlePassLookupHandle(header.pid); |
270 #endif | 281 #endif |
271 if (target == NULL) { | 282 if (target == NULL) { |
272 return -1; | 283 return -1; |
284 } | |
273 } | 285 } |
274 for (uint32_t i = 0; i < message->handle_count; ++i) { | 286 for (uint32_t i = 0; i < message->handle_count; ++i) { |
275 HANDLE temp_remote_handle; | 287 HANDLE temp_remote_handle; |
276 if (DuplicateHandle(GetCurrentProcess(), message->handles[i], | 288 bool success; |
277 target, &temp_remote_handle, | 289 if (g_broker_duplicate_handle_func != NULL) { |
278 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE) { | 290 success = g_broker_duplicate_handle_func(message->handles[i], |
291 header.pid, | |
292 &temp_remote_handle, | |
293 0, DUPLICATE_SAME_ACCESS); | |
294 } else { | |
295 success = DuplicateHandle(GetCurrentProcess(), message->handles[i], | |
296 target, &temp_remote_handle, | |
297 0, FALSE, DUPLICATE_SAME_ACCESS); | |
298 } | |
299 if (!success) { | |
279 // Send the kCancel message to revoke the handles duplicated | 300 // Send the kCancel message to revoke the handles duplicated |
280 // so far in the remote peer. | 301 // so far in the remote peer. |
281 header.command = kCancel; | 302 header.command = kCancel; |
282 header.handle_count = i; | 303 header.handle_count = i; |
283 if (0 < i) { | 304 if (0 < i) { |
284 WriteAll(handle, &header, sizeof header); | 305 WriteAll(handle, &header, sizeof header); |
285 WriteAll(handle, remote_handles, sizeof(uint64_t) * i); | 306 WriteAll(handle, remote_handles, sizeof(uint64_t) * i); |
286 } | 307 } |
308 if (g_broker_duplicate_handle_func == NULL) { | |
287 #ifdef NACL_STANDALONE | 309 #ifdef NACL_STANDALONE |
288 CloseHandle(target); | 310 CloseHandle(target); |
289 #endif | 311 #endif |
312 } | |
290 return -1; | 313 return -1; |
291 } | 314 } |
292 remote_handles[i] = reinterpret_cast<uint64_t>(temp_remote_handle); | 315 remote_handles[i] = reinterpret_cast<uint64_t>(temp_remote_handle); |
293 } | 316 } |
317 if (g_broker_duplicate_handle_func == NULL) { | |
294 #ifdef NACL_STANDALONE | 318 #ifdef NACL_STANDALONE |
295 CloseHandle(target); | 319 CloseHandle(target); |
296 #endif | 320 #endif |
321 } | |
297 } | 322 } |
298 header.command = kMessage; | 323 header.command = kMessage; |
299 header.handle_count = message->handle_count; | 324 header.handle_count = message->handle_count; |
300 for (uint32_t i = 0; i < message->iov_length; ++i) { | 325 for (uint32_t i = 0; i < message->iov_length; ++i) { |
301 if (UINT32_MAX - header.message_length < message->iov[i].length) { | 326 if (UINT32_MAX - header.message_length < message->iov[i].length) { |
302 return -1; | 327 return -1; |
303 } | 328 } |
304 header.message_length += static_cast<uint32_t>(message->iov[i].length); | 329 header.message_length += static_cast<uint32_t>(message->iov[i].length); |
305 } | 330 } |
306 if (WriteAll(handle, &header, sizeof header) != sizeof header) { | 331 if (WriteAll(handle, &header, sizeof header) != sizeof header) { |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 break; | 606 break; |
582 } | 607 } |
583 default: | 608 default: |
584 return -1; | 609 return -1; |
585 break; | 610 break; |
586 } | 611 } |
587 } | 612 } |
588 } | 613 } |
589 | 614 |
590 } // namespace nacl | 615 } // namespace nacl |
OLD | NEW |