[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e15a4fa | 2010-02-11 23:09:29 | [diff] [blame] | 5 | #include "chrome/browser/nacl_host/nacl_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 6 | |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame^] | 7 | #include <string> |
| 8 | #include <vector> |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 9 | |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 12 | #include "base/memory/singleton.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | a0a69bf | 2011-09-23 21:40:28 | [diff] [blame] | 14 | #include "base/stringprintf.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 16 | #include "base/win/windows_version.h" |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame^] | 17 | #include "build/build_config.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 18 | #include "chrome/common/chrome_paths.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
| 20 | #include "chrome/common/logging_chrome.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 21 | #include "chrome/common/nacl_cmd_line.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 22 | #include "chrome/common/nacl_messages.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 23 | #include "chrome/common/render_messages.h" |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 24 | #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 25 | #include "content/public/browser/browser_child_process_host.h" |
| 26 | #include "content/public/browser/child_process_data.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 27 | #include "content/public/common/child_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 28 | #include "ipc/ipc_switches.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 29 | #include "native_client/src/shared/imc/nacl_imc.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 30 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 31 | #if defined(OS_POSIX) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame^] | 32 | #include <fcntl.h> |
| 33 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 34 | #include "ipc/ipc_channel_posix.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 35 | #elif defined(OS_WIN) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame^] | 36 | #include <windows.h> |
| 37 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 38 | #include "base/threading/thread.h" |
| 39 | #include "base/process_util.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 40 | #include "chrome/browser/nacl_host/nacl_broker_service_win.h" |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 41 | #include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 42 | #endif |
| 43 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 44 | using content::BrowserThread; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 45 | using content::ChildProcessData; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 46 | using content::ChildProcessHost; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 47 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 48 | #if defined(OS_WIN) |
| 49 | class NaClProcessHost::DebugContext |
| 50 | : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> { |
| 51 | public: |
| 52 | DebugContext() |
| 53 | : can_send_start_msg_(false), |
| 54 | child_process_host_(NULL) { |
| 55 | } |
| 56 | |
| 57 | ~DebugContext() { |
| 58 | } |
| 59 | |
| 60 | void AttachDebugger(int pid, base::ProcessHandle process); |
| 61 | |
| 62 | // 6 methods below must be called on Browser::IO thread. |
| 63 | void SetStartMessage(IPC::Message* start_msg); |
| 64 | void SetChildProcessHost(content::ChildProcessHost* child_process_host); |
| 65 | void SetDebugThread(base::Thread* thread_); |
| 66 | |
| 67 | // Start message is sent from 2 flows of execution. The first flow is |
| 68 | // NaClProcessHost::SendStart. The second flow is |
| 69 | // NaClProcessHost::OnChannelConnected and |
| 70 | // NaClProcessHost::DebugContext::AttachThread. The message itself is created |
| 71 | // by first flow. But the moment it can be sent is determined by second flow. |
| 72 | // So first flow executes SetStartMessage and SendStartMessage while second |
| 73 | // flow uses AllowAndSendStartMessage to either send potentially pending |
| 74 | // start message or set the flag that allows the first flow to do this. |
| 75 | |
| 76 | // Clears the flag that prevents sending start message. |
| 77 | void AllowToSendStartMsg(); |
| 78 | // Send start message to the NaCl process or do nothing if message is not |
| 79 | // set or not allowed to be send. If message is sent, it is cleared and |
| 80 | // repeated calls do nothing. |
| 81 | void SendStartMessage(); |
| 82 | // Clear the flag that prevents further sending start message and send start |
| 83 | // message if it is set. |
| 84 | void AllowAndSendStartMessage(); |
| 85 | private: |
| 86 | void StopThread(); |
| 87 | // These 4 fields are accessed only from Browser::IO thread. |
| 88 | scoped_ptr<base::Thread> thread_; |
| 89 | scoped_ptr<IPC::Message> start_msg_; |
| 90 | // Debugger is attached or exception handling is not switched on. |
| 91 | // This means that start message can be sent to the NaCl process. |
| 92 | bool can_send_start_msg_; |
| 93 | content::ChildProcessHost* child_process_host_; |
| 94 | }; |
| 95 | |
| 96 | void NaClProcessHost::DebugContext::AttachDebugger( |
| 97 | int pid, base::ProcessHandle process) { |
| 98 | BOOL attached; |
| 99 | DWORD exit_code; |
| 100 | attached = DebugActiveProcess(pid); |
| 101 | if (!attached) { |
| 102 | LOG(ERROR) << "Failed to connect to the process"; |
| 103 | } |
| 104 | BrowserThread::PostTask( |
| 105 | BrowserThread::IO, FROM_HERE, |
| 106 | base::Bind( |
| 107 | &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this)); |
| 108 | if (attached) { |
| 109 | // debug the process |
| 110 | NaClDebugLoop(process, &exit_code); |
| 111 | base::CloseProcessHandle(process); |
| 112 | } |
| 113 | BrowserThread::PostTask( |
| 114 | BrowserThread::IO, FROM_HERE, |
| 115 | base::Bind(&NaClProcessHost::DebugContext::StopThread, this)); |
| 116 | } |
| 117 | |
| 118 | void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) { |
| 119 | start_msg_.reset(start_msg); |
| 120 | } |
| 121 | |
| 122 | void NaClProcessHost::DebugContext::SetChildProcessHost( |
| 123 | content::ChildProcessHost* child_process_host) { |
| 124 | child_process_host_ = child_process_host; |
| 125 | } |
| 126 | |
| 127 | void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) { |
| 128 | thread_.reset(thread); |
| 129 | } |
| 130 | |
| 131 | void NaClProcessHost::DebugContext::AllowToSendStartMsg() { |
| 132 | can_send_start_msg_ = true; |
| 133 | } |
| 134 | |
| 135 | void NaClProcessHost::DebugContext::SendStartMessage() { |
| 136 | if (start_msg_.get() && can_send_start_msg_) { |
| 137 | if (child_process_host_) { |
| 138 | if (!child_process_host_->Send(start_msg_.release())) { |
| 139 | LOG(ERROR) << "Failed to send start message"; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void NaClProcessHost::DebugContext::AllowAndSendStartMessage() { |
| 146 | AllowToSendStartMsg(); |
| 147 | SendStartMessage(); |
| 148 | } |
| 149 | |
| 150 | void NaClProcessHost::DebugContext::StopThread() { |
| 151 | thread_->Stop(); |
| 152 | thread_.reset(); |
| 153 | } |
| 154 | #endif |
| 155 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 156 | namespace { |
| 157 | |
| 158 | void SetCloseOnExec(nacl::Handle fd) { |
| 159 | #if defined(OS_POSIX) |
| 160 | int flags = fcntl(fd, F_GETFD); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 161 | CHECK_NE(flags, -1); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 162 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 163 | CHECK_EQ(rc, 0); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 164 | #endif |
| 165 | } |
| 166 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 167 | // Represents shared state for all NaClProcessHost objects in the browser. |
| 168 | // Currently this just handles holding onto the file descriptor for the IRT. |
| 169 | class NaClBrowser { |
| 170 | public: |
| 171 | static NaClBrowser* GetInstance() { |
| 172 | return Singleton<NaClBrowser>::get(); |
| 173 | } |
| 174 | |
| 175 | bool IrtAvailable() const { |
| 176 | return irt_platform_file_ != base::kInvalidPlatformFileValue; |
| 177 | } |
| 178 | |
| 179 | base::PlatformFile IrtFile() const { |
| 180 | CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue); |
| 181 | return irt_platform_file_; |
| 182 | } |
| 183 | |
| 184 | // Asynchronously attempt to get the IRT open. |
| 185 | bool EnsureIrtAvailable(); |
| 186 | |
| 187 | // Make sure the IRT gets opened and follow up with the reply when it's ready. |
| 188 | bool MakeIrtAvailable(const base::Closure& reply); |
| 189 | |
| 190 | private: |
| 191 | base::PlatformFile irt_platform_file_; |
| 192 | |
| 193 | friend struct DefaultSingletonTraits<NaClBrowser>; |
| 194 | |
| 195 | NaClBrowser() |
| 196 | : irt_platform_file_(base::kInvalidPlatformFileValue) |
| 197 | {} |
| 198 | |
| 199 | ~NaClBrowser() { |
| 200 | if (irt_platform_file_ != base::kInvalidPlatformFileValue) |
| 201 | base::ClosePlatformFile(irt_platform_file_); |
| 202 | } |
| 203 | |
| 204 | void OpenIrtLibraryFile(); |
| 205 | |
| 206 | static void DoOpenIrtLibraryFile() { |
| 207 | GetInstance()->OpenIrtLibraryFile(); |
| 208 | } |
| 209 | |
| 210 | DISALLOW_COPY_AND_ASSIGN(NaClBrowser); |
| 211 | }; |
| 212 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 213 | } // namespace |
| 214 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 215 | struct NaClProcessHost::NaClInternal { |
| 216 | std::vector<nacl::Handle> sockets_for_renderer; |
| 217 | std::vector<nacl::Handle> sockets_for_sel_ldr; |
| 218 | }; |
| 219 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 220 | #if defined(OS_WIN) |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 221 | static bool RunningOnWOW64() { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 222 | return (base::win::OSInfo::GetInstance()->wow64_status() == |
| 223 | base::win::OSInfo::WOW64_ENABLED); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 224 | } |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 225 | #endif |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 226 | |
[email protected] | 1dbaaa70 | 2011-04-06 17:43:42 | [diff] [blame] | 227 | NaClProcessHost::NaClProcessHost(const std::wstring& url) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 228 | : reply_msg_(NULL), |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 229 | internal_(new NaClInternal()), |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 230 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 231 | process_.reset(content::BrowserChildProcessHost::Create( |
| 232 | content::PROCESS_TYPE_NACL_LOADER, this)); |
| 233 | process_->SetName(WideToUTF16Hack(url)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 234 | #if defined(OS_WIN) |
| 235 | if (!RunningOnWOW64()) { |
| 236 | debug_context_ = new DebugContext(); |
| 237 | } |
| 238 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 241 | NaClProcessHost::~NaClProcessHost() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 242 | int exit_code; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 243 | process_->GetTerminationStatus(&exit_code); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 244 | std::string message = |
| 245 | base::StringPrintf("NaCl process exited with status %i (0x%x)", |
| 246 | exit_code, exit_code); |
| 247 | if (exit_code == 0) { |
| 248 | LOG(INFO) << message; |
| 249 | } else { |
| 250 | LOG(ERROR) << message; |
| 251 | } |
| 252 | |
| 253 | #if defined(OS_WIN) |
| 254 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
| 255 | #endif |
| 256 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 257 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 258 | if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) { |
| 259 | LOG(ERROR) << "nacl::Close() failed"; |
| 260 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 261 | } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 262 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 263 | if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) { |
| 264 | LOG(ERROR) << "nacl::Close() failed"; |
| 265 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 268 | if (reply_msg_) { |
| 269 | // The process failed to launch for some reason. |
| 270 | // Don't keep the renderer hanging. |
| 271 | reply_msg_->set_reply_error(); |
| 272 | chrome_render_message_filter_->Send(reply_msg_); |
| 273 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 274 | #if defined(OS_WIN) |
| 275 | if (!RunningOnWOW64()) { |
| 276 | debug_context_->SetChildProcessHost(NULL); |
| 277 | } |
| 278 | #endif |
[email protected] | 463ea5f | 2011-12-03 06:57:47 | [diff] [blame] | 279 | |
| 280 | #if defined(OS_WIN) |
| 281 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
| 282 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 285 | // Attempt to ensure the IRT will be available when we need it, but don't wait. |
| 286 | bool NaClBrowser::EnsureIrtAvailable() { |
| 287 | if (IrtAvailable()) |
| 288 | return true; |
| 289 | |
| 290 | return BrowserThread::PostTask( |
| 291 | BrowserThread::FILE, FROM_HERE, |
| 292 | base::Bind(&NaClBrowser::DoOpenIrtLibraryFile)); |
| 293 | } |
| 294 | |
| 295 | // We really need the IRT to be available now, so make sure that it is. |
| 296 | // When it's ready, we'll run the reply closure. |
| 297 | bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) { |
| 298 | return BrowserThread::PostTaskAndReply( |
| 299 | BrowserThread::FILE, FROM_HERE, |
| 300 | base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply); |
| 301 | } |
| 302 | |
| 303 | // This is called at browser startup. |
| 304 | // static |
| 305 | void NaClProcessHost::EarlyStartup() { |
| 306 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 307 | // Open the IRT file early to make sure that it isn't replaced out from |
| 308 | // under us by autoupdate. |
| 309 | NaClBrowser::GetInstance()->EnsureIrtAvailable(); |
| 310 | #endif |
| 311 | } |
| 312 | |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 313 | bool NaClProcessHost::Launch( |
| 314 | ChromeRenderMessageFilter* chrome_render_message_filter, |
| 315 | int socket_count, |
| 316 | IPC::Message* reply_msg) { |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 317 | // Place an arbitrary limit on the number of sockets to limit |
| 318 | // exposure in case the renderer is compromised. We can increase |
| 319 | // this if necessary. |
| 320 | if (socket_count > 8) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 321 | return false; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 324 | // Start getting the IRT open asynchronously while we launch the NaCl process. |
| 325 | // We'll make sure this actually finished in OnProcessLaunched, below. |
| 326 | if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) { |
| 327 | LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed"; |
| 328 | return false; |
| 329 | } |
| 330 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 331 | // Rather than creating a socket pair in the renderer, and passing |
| 332 | // one side through the browser to sel_ldr, socket pairs are created |
| 333 | // in the browser and then passed to the renderer and sel_ldr. |
| 334 | // |
| 335 | // This is mainly for the benefit of Windows, where sockets cannot |
| 336 | // be passed in messages, but are copied via DuplicateHandle(). |
| 337 | // This means the sandboxed renderer cannot send handles to the |
| 338 | // browser process. |
| 339 | |
| 340 | for (int i = 0; i < socket_count; i++) { |
| 341 | nacl::Handle pair[2]; |
| 342 | // Create a connected socket |
| 343 | if (nacl::SocketPair(pair) == -1) |
| 344 | return false; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 345 | internal_->sockets_for_renderer.push_back(pair[0]); |
| 346 | internal_->sockets_for_sel_ldr.push_back(pair[1]); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 347 | SetCloseOnExec(pair[0]); |
| 348 | SetCloseOnExec(pair[1]); |
| 349 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 350 | |
| 351 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 352 | if (!LaunchSelLdr()) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 353 | return false; |
| 354 | } |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 355 | |
[email protected] | 98999e80 | 2011-12-21 21:54:43 | [diff] [blame] | 356 | chrome_render_message_filter_ = chrome_render_message_filter; |
| 357 | |
| 358 | // On success, we take responsibility for sending the reply. |
| 359 | reply_msg_ = reply_msg; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 363 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 364 | std::string channel_id = process_->GetHost()->CreateChannel(); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 365 | if (channel_id.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 366 | return false; |
| 367 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 368 | CommandLine::StringType nacl_loader_prefix; |
| 369 | #if defined(OS_POSIX) |
| 370 | nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 371 | switches::kNaClLoaderCmdPrefix); |
| 372 | #endif // defined(OS_POSIX) |
| 373 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 374 | // Build command line for nacl. |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 375 | |
| 376 | #if defined(OS_MACOSX) |
| 377 | // The Native Client process needs to be able to allocate a 1GB contiguous |
| 378 | // region to use as the client environment's virtual address space. ASLR |
| 379 | // (PIE) interferes with this by making it possible that no gap large enough |
| 380 | // to accomodate this request will exist in the child process' address |
| 381 | // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and |
| 382 | // http://code.google.com/p/nativeclient/issues/detail?id=2043. |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 383 | int flags = ChildProcessHost::CHILD_NO_PIE; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 384 | #elif defined(OS_LINUX) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 385 | int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF : |
| 386 | ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 387 | #else |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 388 | int flags = ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 389 | #endif |
| 390 | |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 391 | FilePath exe_path = ChildProcessHost::GetChildPath(flags); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 392 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 393 | return false; |
| 394 | |
[email protected] | 662a017 | 2011-12-21 04:18:19 | [diff] [blame] | 395 | CommandLine* cmd_line = new CommandLine(exe_path); |
| 396 | nacl::CopyNaClCommandLineArguments(cmd_line); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 397 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 398 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 399 | switches::kNaClLoaderProcess); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 400 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
[email protected] | 93156cec | 2011-09-12 21:14:44 | [diff] [blame] | 401 | if (logging::DialogsAreSuppressed()) |
| 402 | cmd_line->AppendSwitch(switches::kNoErrorDialogs); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 403 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 404 | if (!nacl_loader_prefix.empty()) |
| 405 | cmd_line->PrependWrapper(nacl_loader_prefix); |
| 406 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 407 | // On Windows we might need to start the broker process to launch a new loader |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 408 | #if defined(OS_WIN) |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 409 | if (RunningOnWOW64()) { |
[email protected] | 1dbaaa70 | 2011-04-06 17:43:42 | [diff] [blame] | 410 | return NaClBrokerService::GetInstance()->LaunchLoader( |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 411 | this, ASCIIToWide(channel_id)); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 412 | } else { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 413 | process_->Launch(FilePath(), cmd_line); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 414 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 415 | #elif defined(OS_POSIX) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 416 | process_->Launch(nacl_loader_prefix.empty(), // use_zygote |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame^] | 417 | base::EnvironmentVector(), |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 418 | cmd_line); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 419 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 420 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 421 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 422 | } |
| 423 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 424 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 425 | process_->SetHandle(handle); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 426 | OnProcessLaunched(); |
| 427 | } |
| 428 | |
[email protected] | 463ea5f | 2011-12-03 06:57:47 | [diff] [blame] | 429 | void NaClProcessHost::OnProcessCrashed(int exit_code) { |
| 430 | std::string message = base::StringPrintf( |
| 431 | "NaCl process exited with status %i (0x%x)", exit_code, exit_code); |
| 432 | LOG(ERROR) << message; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 433 | } |
| 434 | |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 435 | namespace { |
| 436 | |
| 437 | // Determine the name of the IRT file based on the architecture. |
| 438 | |
| 439 | #define NACL_IRT_FILE_NAME(arch_string) \ |
| 440 | (FILE_PATH_LITERAL("nacl_irt_") \ |
| 441 | FILE_PATH_LITERAL(arch_string) \ |
| 442 | FILE_PATH_LITERAL(".nexe")) |
| 443 | |
| 444 | const FilePath::StringType NaClIrtName() { |
| 445 | #if defined(ARCH_CPU_X86_FAMILY) |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 446 | #if defined(ARCH_CPU_X86_64) |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 447 | bool is64 = true; |
| 448 | #elif defined(OS_WIN) |
| 449 | bool is64 = RunningOnWOW64(); |
| 450 | #else |
| 451 | bool is64 = false; |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 452 | #endif |
| 453 | return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32"); |
| 454 | #elif defined(ARCH_CPU_ARMEL) |
| 455 | // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2. |
| 456 | // That may need to be based on the actual nexe rather than a static |
| 457 | // choice, which would require substantial refactoring. |
| 458 | return NACL_IRT_FILE_NAME("arm"); |
| 459 | #else |
| 460 | #error Add support for your architecture to NaCl IRT file selection |
| 461 | #endif |
| 462 | } |
| 463 | |
| 464 | } // namespace |
| 465 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 466 | // This only ever runs on the BrowserThread::FILE thread. |
| 467 | // If multiple tasks are posted, the later ones are no-ops. |
| 468 | void NaClBrowser::OpenIrtLibraryFile() { |
| 469 | if (irt_platform_file_ != base::kInvalidPlatformFileValue) |
| 470 | // We've already run. |
| 471 | return; |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 472 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 473 | FilePath irt_filepath; |
| 474 | |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 475 | // Allow the IRT library to be overridden via an environment |
| 476 | // variable. This allows the NaCl/Chromium integration bot to |
| 477 | // specify a newly-built IRT rather than using a prebuilt one |
| 478 | // downloaded via Chromium's DEPS file. We use the same environment |
| 479 | // variable that the standalone NaCl PPAPI plugin accepts. |
| 480 | const char* irt_path_var = getenv("NACL_IRT_LIBRARY"); |
| 481 | if (irt_path_var != NULL) { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 482 | FilePath::StringType path_string( |
| 483 | irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0'))); |
| 484 | irt_filepath = FilePath(path_string); |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 485 | } else { |
| 486 | FilePath plugin_dir; |
| 487 | if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) { |
| 488 | LOG(ERROR) << "Failed to locate the plugins directory"; |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 489 | return; |
| 490 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 491 | |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 492 | irt_filepath = plugin_dir.Append(NaClIrtName()); |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 493 | } |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 494 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 495 | base::PlatformFileError error_code; |
| 496 | irt_platform_file_ = base::CreatePlatformFile(irt_filepath, |
| 497 | base::PLATFORM_FILE_OPEN | |
| 498 | base::PLATFORM_FILE_READ, |
| 499 | NULL, |
| 500 | &error_code); |
| 501 | if (error_code != base::PLATFORM_FILE_OK) { |
| 502 | LOG(ERROR) << "Failed to open NaCl IRT file \"" |
| 503 | << irt_filepath.LossyDisplayName() |
| 504 | << "\": " << error_code; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void NaClProcessHost::OnProcessLaunched() { |
| 509 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 510 | |
| 511 | if (nacl_browser->IrtAvailable()) { |
| 512 | // The IRT is already open. Away we go. |
| 513 | SendStart(nacl_browser->IrtFile()); |
| 514 | } else { |
| 515 | // We're waiting for the IRT to be open. |
[email protected] | 98999e80 | 2011-12-21 21:54:43 | [diff] [blame] | 516 | if (!nacl_browser->MakeIrtAvailable( |
| 517 | base::Bind(&NaClProcessHost::IrtReady, |
| 518 | weak_factory_.GetWeakPtr()))) |
| 519 | delete this; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | // The asynchronous attempt to get the IRT file open has completed. |
| 524 | void NaClProcessHost::IrtReady() { |
| 525 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 526 | |
| 527 | if (nacl_browser->IrtAvailable()) { |
| 528 | SendStart(nacl_browser->IrtFile()); |
| 529 | } else { |
| 530 | LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed"; |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 531 | delete this; |
| 532 | } |
| 533 | } |
| 534 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 535 | #if defined(OS_WIN) |
| 536 | bool NaClProcessHost::IsHardwareExceptionHandlingEnabled() { |
| 537 | return CommandLine::ForCurrentProcess()->HasSwitch( |
| 538 | switches::kEnableNaClExceptionHandling) && !RunningOnWOW64(); |
| 539 | } |
| 540 | |
| 541 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
| 542 | if (!IsHardwareExceptionHandlingEnabled()) { |
| 543 | return; |
| 544 | } |
| 545 | // Start new thread for debug loop |
| 546 | debug_context_->SetChildProcessHost(process_->GetHost()); |
| 547 | // We can't use process_->GetData().handle because it doesn't have necessary |
| 548 | // access rights. |
| 549 | base::ProcessHandle process; |
| 550 | if (!base::OpenProcessHandleWithAccess( |
| 551 | peer_pid, |
| 552 | base::kProcessAccessQueryInformation | |
| 553 | base::kProcessAccessSuspendResume | |
| 554 | base::kProcessAccessTerminate | |
| 555 | base::kProcessAccessVMOperation | |
| 556 | base::kProcessAccessVMRead | |
| 557 | base::kProcessAccessVMWrite | |
| 558 | base::kProcessAccessWaitForTermination, |
| 559 | &process)) { |
| 560 | LOG(ERROR) << "Failed to open the process"; |
| 561 | debug_context_->AllowAndSendStartMessage(); |
| 562 | return; |
| 563 | } |
| 564 | base::Thread* dbg_thread = new base::Thread("Debug thread"); |
| 565 | if (!dbg_thread->Start()) { |
| 566 | LOG(ERROR) << "Debug thread not started"; |
| 567 | debug_context_->AllowAndSendStartMessage(); |
| 568 | base::CloseProcessHandle(process); |
| 569 | return; |
| 570 | } |
| 571 | debug_context_->SetDebugThread(dbg_thread); |
| 572 | // System can not reallocate pid until we close process handle. So using |
| 573 | // pid in different thread is fine. |
| 574 | dbg_thread->message_loop()->PostTask(FROM_HERE, |
| 575 | base::Bind(&NaClProcessHost::DebugContext::AttachDebugger, |
| 576 | debug_context_, peer_pid, process)); |
| 577 | } |
| 578 | #else |
| 579 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
| 580 | } |
| 581 | #endif |
| 582 | |
| 583 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 584 | static bool SendHandleToSelLdr( |
| 585 | base::ProcessHandle processh, |
| 586 | nacl::Handle sourceh, bool close_source, |
| 587 | std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) { |
| 588 | #if defined(OS_WIN) |
| 589 | HANDLE channel; |
| 590 | int flags = DUPLICATE_SAME_ACCESS; |
| 591 | if (close_source) |
| 592 | flags |= DUPLICATE_CLOSE_SOURCE; |
| 593 | if (!DuplicateHandle(GetCurrentProcess(), |
| 594 | reinterpret_cast<HANDLE>(sourceh), |
| 595 | processh, |
| 596 | &channel, |
| 597 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 598 | FALSE, |
| 599 | flags)) { |
| 600 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 601 | return false; |
| 602 | } |
| 603 | handles_for_sel_ldr->push_back( |
| 604 | reinterpret_cast<nacl::FileDescriptor>(channel)); |
| 605 | #else |
| 606 | nacl::FileDescriptor channel; |
| 607 | channel.fd = sourceh; |
| 608 | channel.auto_close = close_source; |
| 609 | handles_for_sel_ldr->push_back(channel); |
| 610 | #endif |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | void NaClProcessHost::SendStart(base::PlatformFile irt_file) { |
| 615 | CHECK_NE(irt_file, base::kInvalidPlatformFileValue); |
| 616 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 617 | std::vector<nacl::FileDescriptor> handles_for_renderer; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 618 | base::ProcessHandle nacl_process_handle; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 619 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 620 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 621 | #if defined(OS_WIN) |
| 622 | // Copy the handle into the renderer process. |
| 623 | HANDLE handle_in_renderer; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 624 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
| 625 | reinterpret_cast<HANDLE>( |
| 626 | internal_->sockets_for_renderer[i]), |
| 627 | chrome_render_message_filter_->peer_handle(), |
| 628 | &handle_in_renderer, |
| 629 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 630 | FALSE, |
| 631 | DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
| 632 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 633 | delete this; |
| 634 | return; |
| 635 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 636 | handles_for_renderer.push_back( |
| 637 | reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer)); |
| 638 | #else |
| 639 | // No need to dup the imc_handle - we don't pass it anywhere else so |
| 640 | // it cannot be closed. |
| 641 | nacl::FileDescriptor imc_handle; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 642 | imc_handle.fd = internal_->sockets_for_renderer[i]; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 643 | imc_handle.auto_close = true; |
| 644 | handles_for_renderer.push_back(imc_handle); |
| 645 | #endif |
| 646 | } |
| 647 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 648 | const ChildProcessData& data = process_->GetData(); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 649 | #if defined(OS_WIN) |
| 650 | // Copy the process handle into the renderer process. |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 651 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 652 | data.handle, |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 653 | chrome_render_message_filter_->peer_handle(), |
| 654 | &nacl_process_handle, |
| 655 | PROCESS_DUP_HANDLE, |
| 656 | FALSE, |
| 657 | 0)) { |
| 658 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 659 | delete this; |
| 660 | return; |
| 661 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 662 | #else |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 663 | // We use pid as process handle on Posix |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 664 | nacl_process_handle = data.handle; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 665 | #endif |
| 666 | |
| 667 | // Get the pid of the NaCl process |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 668 | base::ProcessId nacl_process_id = base::GetProcId(data.handle); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 669 | |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 670 | ChromeViewHostMsg_LaunchNaCl::WriteReplyParams( |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 671 | reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id); |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 672 | chrome_render_message_filter_->Send(reply_msg_); |
| 673 | chrome_render_message_filter_ = NULL; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 674 | reply_msg_ = NULL; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 675 | internal_->sockets_for_renderer.clear(); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 676 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 677 | std::vector<nacl::FileDescriptor> handles_for_sel_ldr; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 678 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 679 | if (!SendHandleToSelLdr(data.handle, |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 680 | internal_->sockets_for_sel_ldr[i], true, |
| 681 | &handles_for_sel_ldr)) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 682 | delete this; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 683 | return; |
| 684 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // Send over the IRT file handle. We don't close our own copy! |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 688 | if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 689 | delete this; |
| 690 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | ab88d154 | 2011-11-18 22:52:00 | [diff] [blame] | 693 | #if defined(OS_MACOSX) |
| 694 | // For dynamic loading support, NaCl requires a file descriptor that |
| 695 | // was created in /tmp, since those created with shm_open() are not |
| 696 | // mappable with PROT_EXEC. Rather than requiring an extra IPC |
| 697 | // round trip out of the sandbox, we create an FD here. |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 698 | base::SharedMemory memory_buffer; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 699 | base::SharedMemoryCreateOptions options; |
| 700 | options.size = 1; |
| 701 | options.executable = true; |
| 702 | if (!memory_buffer.Create(options)) { |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 703 | LOG(ERROR) << "Failed to allocate memory buffer"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 704 | delete this; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 705 | return; |
| 706 | } |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 707 | nacl::FileDescriptor memory_fd; |
| 708 | memory_fd.fd = dup(memory_buffer.handle().fd); |
| 709 | if (memory_fd.fd < 0) { |
| 710 | LOG(ERROR) << "Failed to dup() a file descriptor"; |
| 711 | delete this; |
| 712 | return; |
| 713 | } |
| 714 | memory_fd.auto_close = true; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 715 | handles_for_sel_ldr.push_back(memory_fd); |
| 716 | #endif |
| 717 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 718 | #if defined(OS_WIN) |
| 719 | if (IsHardwareExceptionHandlingEnabled()) { |
| 720 | debug_context_->SetStartMessage( |
| 721 | new NaClProcessMsg_Start(handles_for_sel_ldr)); |
| 722 | debug_context_->SendStartMessage(); |
| 723 | } else { |
| 724 | process_->Send(new NaClProcessMsg_Start(handles_for_sel_ldr)); |
| 725 | } |
| 726 | #else |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 727 | process_->Send(new NaClProcessMsg_Start(handles_for_sel_ldr)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 728 | #endif |
| 729 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 730 | internal_->sockets_for_sel_ldr.clear(); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 731 | } |
| 732 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 733 | bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 734 | NOTREACHED() << "Invalid message with type = " << msg.type(); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 735 | return false; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 736 | } |