| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "sandbox/win/src/sharedmem_ipc_server.h" | 8 #include "sandbox/win/src/sharedmem_ipc_server.h" |
| 9 #include "sandbox/win/src/sharedmem_ipc_client.h" | 9 #include "sandbox/win/src/sharedmem_ipc_client.h" |
| 10 #include "sandbox/win/src/sandbox.h" | 10 #include "sandbox/win/src/sandbox.h" |
| 11 #include "sandbox/win/src/sandbox_types.h" | 11 #include "sandbox/win/src/sandbox_types.h" |
| 12 #include "sandbox/win/src/crosscall_params.h" | 12 #include "sandbox/win/src/crosscall_params.h" |
| 13 #include "sandbox/win/src/crosscall_server.h" | 13 #include "sandbox/win/src/crosscall_server.h" |
| 14 | 14 |
| 15 namespace { |
| 16 // This handle must not be closed. |
| 17 volatile HANDLE g_alive_mutex = NULL; |
| 18 } |
| 19 |
| 15 namespace sandbox { | 20 namespace sandbox { |
| 16 | 21 |
| 17 SharedMemIPCServer::SharedMemIPCServer(HANDLE target_process, | 22 SharedMemIPCServer::SharedMemIPCServer(HANDLE target_process, |
| 18 DWORD target_process_id, | 23 DWORD target_process_id, |
| 19 HANDLE target_job, | 24 HANDLE target_job, |
| 20 ThreadProvider* thread_provider, | 25 ThreadProvider* thread_provider, |
| 21 Dispatcher* dispatcher) | 26 Dispatcher* dispatcher) |
| 22 : client_control_(NULL), | 27 : client_control_(NULL), |
| 23 thread_provider_(thread_provider), | 28 thread_provider_(thread_provider), |
| 24 target_process_(target_process), | 29 target_process_(target_process), |
| 25 target_process_id_(target_process_id), | 30 target_process_id_(target_process_id), |
| 26 target_job_object_(target_job), | 31 target_job_object_(target_job), |
| 27 call_dispatcher_(dispatcher) { | 32 call_dispatcher_(dispatcher) { |
| 33 // We create a initially owned mutex. If the server dies unexpectedly, |
| 34 // the thread that owns it will fail to release the lock and windows will |
| 35 // report to the target (when it tries to acquire it) that the wait was |
| 36 // abandoned. Note: We purposely leak the local handle because we want it to |
| 37 // be closed by Windows itself so it is properly marked as abandoned if the |
| 38 // server dies. |
| 39 if (!g_alive_mutex) { |
| 40 HANDLE mutex = ::CreateMutexW(NULL, TRUE, NULL); |
| 41 if (::InterlockedCompareExchangePointer(&g_alive_mutex, mutex, NULL)) { |
| 42 // We lost the race to create the mutex. |
| 43 ::CloseHandle(mutex); |
| 44 } |
| 45 } |
| 28 } | 46 } |
| 29 | 47 |
| 30 SharedMemIPCServer::~SharedMemIPCServer() { | 48 SharedMemIPCServer::~SharedMemIPCServer() { |
| 31 // Free the wait handles associated with the thread pool. | 49 // Free the wait handles associated with the thread pool. |
| 32 if (!thread_provider_->UnRegisterWaits(this)) { | 50 if (!thread_provider_->UnRegisterWaits(this)) { |
| 33 // Better to leak than to crash. | 51 // Better to leak than to crash. |
| 34 return; | 52 return; |
| 35 } | 53 } |
| 36 // Free the IPC signal events. | 54 // Free the IPC signal events. |
| 37 ServerContexts::iterator it; | 55 ServerContexts::iterator it; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 service_context->dispatcher = call_dispatcher_; | 119 service_context->dispatcher = call_dispatcher_; |
| 102 service_context->target_info.process = target_process_; | 120 service_context->target_info.process = target_process_; |
| 103 service_context->target_info.process_id = target_process_id_; | 121 service_context->target_info.process_id = target_process_id_; |
| 104 service_context->target_info.job_object = target_job_object_; | 122 service_context->target_info.job_object = target_job_object_; |
| 105 // Advance to the next channel. | 123 // Advance to the next channel. |
| 106 base_start += channel_size; | 124 base_start += channel_size; |
| 107 // Register the ping event with the threadpool. | 125 // Register the ping event with the threadpool. |
| 108 thread_provider_->RegisterWait(this, service_context->ping_event, | 126 thread_provider_->RegisterWait(this, service_context->ping_event, |
| 109 ThreadPingEventReady, service_context); | 127 ThreadPingEventReady, service_context); |
| 110 } | 128 } |
| 111 | 129 if (!::DuplicateHandle(::GetCurrentProcess(), g_alive_mutex, |
| 112 // We create a mutex that the server locks. If the server dies unexpectedly, | |
| 113 // the thread that owns it will fail to release the lock and windows will | |
| 114 // report to the target (when it tries to acquire it) that the wait was | |
| 115 // abandoned. Note: We purposely leak the local handle because we want it to | |
| 116 // be closed by Windows itself so it is properly marked as abandoned if the | |
| 117 // server dies. | |
| 118 if (!::DuplicateHandle(::GetCurrentProcess(), | |
| 119 ::CreateMutexW(NULL, TRUE, NULL), | |
| 120 target_process_, &client_control_->server_alive, | 130 target_process_, &client_control_->server_alive, |
| 121 SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, 0)) { | 131 SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, 0)) { |
| 122 return false; | 132 return false; |
| 123 } | 133 } |
| 124 // This last setting indicates to the client all is setup. | 134 // This last setting indicates to the client all is setup. |
| 125 client_control_->channels_count = channel_count; | 135 client_control_->channels_count = channel_count; |
| 126 return true; | 136 return true; |
| 127 } | 137 } |
| 128 | 138 |
| 129 // Releases memory allocated for IPC arguments, if needed. | 139 // Releases memory allocated for IPC arguments, if needed. |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 411 } |
| 402 *server_pong = ::CreateEventW(NULL, FALSE, FALSE, NULL); | 412 *server_pong = ::CreateEventW(NULL, FALSE, FALSE, NULL); |
| 403 if (!::DuplicateHandle(::GetCurrentProcess(), *server_pong, target_process_, | 413 if (!::DuplicateHandle(::GetCurrentProcess(), *server_pong, target_process_, |
| 404 client_pong, kDesiredAccess, FALSE, 0)) { | 414 client_pong, kDesiredAccess, FALSE, 0)) { |
| 405 return false; | 415 return false; |
| 406 } | 416 } |
| 407 return true; | 417 return true; |
| 408 } | 418 } |
| 409 | 419 |
| 410 } // namespace sandbox | 420 } // namespace sandbox |
| OLD | NEW |