blob: 7e4c4c359ee9f07e8bff5acad68465c48f329b07 [file] [log] [blame]
[email protected]b39c6d92012-01-31 16:38:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d032f492009-09-29 00:33:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e15a4fa2010-02-11 23:09:295#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]d032f492009-09-29 00:33:466
[email protected]a82af392012-02-24 04:40:207#include <string>
8#include <vector>
[email protected]d032f492009-09-29 00:33:469
[email protected]30c1eea2011-10-17 18:40:3010#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0911#include "base/command_line.h"
[email protected]4734d0b2011-12-03 07:10:4412#include "base/memory/singleton.h"
[email protected]338466a82011-05-03 04:27:4313#include "base/path_service.h"
[email protected]a0a69bf2011-09-23 21:40:2814#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2215#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3716#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2017#include "build/build_config.h"
[email protected]31a665e72012-03-11 12:37:4618#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4319#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4620#include "chrome/common/chrome_switches.h"
21#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0922#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4623#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3024#include "chrome/common/render_messages.h"
[email protected]92d56412011-03-24 20:53:5225#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]4967f792012-01-20 22:14:4026#include "content/public/browser/browser_child_process_host.h"
27#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4428#include "content/public/common/child_process_host.h"
[email protected]d032f492009-09-29 00:33:4629#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5230#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]d032f492009-09-29 00:33:4631
[email protected]d032f492009-09-29 00:33:4632#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2033#include <fcntl.h>
34
[email protected]d032f492009-09-29 00:33:4635#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3536#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2037#include <windows.h>
38
[email protected]b39c6d92012-01-31 16:38:4139#include "base/threading/thread.h"
40#include "base/process_util.h"
[email protected]4bdde602010-06-16 03:17:3541#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]b39c6d92012-01-31 16:38:4142#include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h"
[email protected]d032f492009-09-29 00:33:4643#endif
44
[email protected]631bb742011-11-02 11:29:3945using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4046using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4447using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3948
[email protected]b39c6d92012-01-31 16:38:4149#if defined(OS_WIN)
50class NaClProcessHost::DebugContext
51 : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> {
52 public:
53 DebugContext()
54 : can_send_start_msg_(false),
55 child_process_host_(NULL) {
56 }
57
58 ~DebugContext() {
59 }
60
61 void AttachDebugger(int pid, base::ProcessHandle process);
62
63 // 6 methods below must be called on Browser::IO thread.
64 void SetStartMessage(IPC::Message* start_msg);
65 void SetChildProcessHost(content::ChildProcessHost* child_process_host);
66 void SetDebugThread(base::Thread* thread_);
67
68 // Start message is sent from 2 flows of execution. The first flow is
69 // NaClProcessHost::SendStart. The second flow is
70 // NaClProcessHost::OnChannelConnected and
71 // NaClProcessHost::DebugContext::AttachThread. The message itself is created
72 // by first flow. But the moment it can be sent is determined by second flow.
73 // So first flow executes SetStartMessage and SendStartMessage while second
74 // flow uses AllowAndSendStartMessage to either send potentially pending
75 // start message or set the flag that allows the first flow to do this.
76
77 // Clears the flag that prevents sending start message.
78 void AllowToSendStartMsg();
79 // Send start message to the NaCl process or do nothing if message is not
80 // set or not allowed to be send. If message is sent, it is cleared and
81 // repeated calls do nothing.
82 void SendStartMessage();
83 // Clear the flag that prevents further sending start message and send start
84 // message if it is set.
85 void AllowAndSendStartMessage();
86 private:
87 void StopThread();
88 // These 4 fields are accessed only from Browser::IO thread.
89 scoped_ptr<base::Thread> thread_;
90 scoped_ptr<IPC::Message> start_msg_;
91 // Debugger is attached or exception handling is not switched on.
92 // This means that start message can be sent to the NaCl process.
93 bool can_send_start_msg_;
94 content::ChildProcessHost* child_process_host_;
95};
96
97void NaClProcessHost::DebugContext::AttachDebugger(
98 int pid, base::ProcessHandle process) {
99 BOOL attached;
100 DWORD exit_code;
101 attached = DebugActiveProcess(pid);
102 if (!attached) {
103 LOG(ERROR) << "Failed to connect to the process";
104 }
105 BrowserThread::PostTask(
106 BrowserThread::IO, FROM_HERE,
107 base::Bind(
108 &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this));
109 if (attached) {
110 // debug the process
111 NaClDebugLoop(process, &exit_code);
112 base::CloseProcessHandle(process);
113 }
114 BrowserThread::PostTask(
115 BrowserThread::IO, FROM_HERE,
116 base::Bind(&NaClProcessHost::DebugContext::StopThread, this));
117}
118
119void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) {
120 start_msg_.reset(start_msg);
121}
122
123void NaClProcessHost::DebugContext::SetChildProcessHost(
124 content::ChildProcessHost* child_process_host) {
125 child_process_host_ = child_process_host;
126}
127
128void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) {
129 thread_.reset(thread);
130}
131
132void NaClProcessHost::DebugContext::AllowToSendStartMsg() {
133 can_send_start_msg_ = true;
134}
135
136void NaClProcessHost::DebugContext::SendStartMessage() {
137 if (start_msg_.get() && can_send_start_msg_) {
138 if (child_process_host_) {
139 if (!child_process_host_->Send(start_msg_.release())) {
140 LOG(ERROR) << "Failed to send start message";
141 }
142 }
143 }
144}
145
146void NaClProcessHost::DebugContext::AllowAndSendStartMessage() {
147 AllowToSendStartMsg();
148 SendStartMessage();
149}
150
151void NaClProcessHost::DebugContext::StopThread() {
152 thread_->Stop();
153 thread_.reset();
154}
155#endif
156
[email protected]c47ec402010-07-29 10:20:49157namespace {
158
159void SetCloseOnExec(nacl::Handle fd) {
160#if defined(OS_POSIX)
161 int flags = fcntl(fd, F_GETFD);
[email protected]773ebb92011-11-15 19:06:52162 CHECK_NE(flags, -1);
[email protected]c47ec402010-07-29 10:20:49163 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
[email protected]773ebb92011-11-15 19:06:52164 CHECK_EQ(rc, 0);
[email protected]c47ec402010-07-29 10:20:49165#endif
166}
167
[email protected]773ebb92011-11-15 19:06:52168// Represents shared state for all NaClProcessHost objects in the browser.
169// Currently this just handles holding onto the file descriptor for the IRT.
170class NaClBrowser {
171 public:
172 static NaClBrowser* GetInstance() {
173 return Singleton<NaClBrowser>::get();
174 }
175
176 bool IrtAvailable() const {
177 return irt_platform_file_ != base::kInvalidPlatformFileValue;
178 }
179
180 base::PlatformFile IrtFile() const {
181 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
182 return irt_platform_file_;
183 }
184
185 // Asynchronously attempt to get the IRT open.
186 bool EnsureIrtAvailable();
187
188 // Make sure the IRT gets opened and follow up with the reply when it's ready.
189 bool MakeIrtAvailable(const base::Closure& reply);
190
[email protected]31a665e72012-03-11 12:37:46191 // Path to IRT. Available even before IRT is loaded.
192 const FilePath& GetIrtFilePath();
193
[email protected]773ebb92011-11-15 19:06:52194 private:
195 base::PlatformFile irt_platform_file_;
196
[email protected]31a665e72012-03-11 12:37:46197 FilePath irt_filepath_;
198
[email protected]773ebb92011-11-15 19:06:52199 friend struct DefaultSingletonTraits<NaClBrowser>;
200
201 NaClBrowser()
[email protected]31a665e72012-03-11 12:37:46202 : irt_platform_file_(base::kInvalidPlatformFileValue),
203 irt_filepath_() {
204 InitIrtFilePath();
205 }
[email protected]773ebb92011-11-15 19:06:52206
207 ~NaClBrowser() {
208 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
209 base::ClosePlatformFile(irt_platform_file_);
210 }
211
[email protected]31a665e72012-03-11 12:37:46212 void InitIrtFilePath();
213
[email protected]773ebb92011-11-15 19:06:52214 void OpenIrtLibraryFile();
215
216 static void DoOpenIrtLibraryFile() {
217 GetInstance()->OpenIrtLibraryFile();
218 }
219
220 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
221};
222
[email protected]c47ec402010-07-29 10:20:49223} // namespace
224
[email protected]1d8a3d1f2011-02-19 07:11:52225struct NaClProcessHost::NaClInternal {
226 std::vector<nacl::Handle> sockets_for_renderer;
227 std::vector<nacl::Handle> sockets_for_sel_ldr;
228};
229
[email protected]773ebb92011-11-15 19:06:52230#if defined(OS_WIN)
[email protected]5804cb5d2011-12-19 21:55:35231static bool RunningOnWOW64() {
[email protected]773ebb92011-11-15 19:06:52232 return (base::win::OSInfo::GetInstance()->wow64_status() ==
233 base::win::OSInfo::WOW64_ENABLED);
[email protected]773ebb92011-11-15 19:06:52234}
[email protected]5804cb5d2011-12-19 21:55:35235#endif
[email protected]773ebb92011-11-15 19:06:52236
[email protected]1dbaaa702011-04-06 17:43:42237NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]a575da52012-03-22 13:08:36238 :
239#if defined(OS_WIN)
240 process_launched_by_broker_(false),
241#endif
242 reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52243 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06244 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
245 enable_exception_handling_(false) {
[email protected]4967f792012-01-20 22:14:40246 process_.reset(content::BrowserChildProcessHost::Create(
247 content::PROCESS_TYPE_NACL_LOADER, this));
248 process_->SetName(WideToUTF16Hack(url));
[email protected]5ca93be2012-03-21 20:04:06249
250 // We allow untrusted hardware exception handling to be enabled via
251 // an env var for consistency with the standalone build of NaCl.
252 if (CommandLine::ForCurrentProcess()->HasSwitch(
253 switches::kEnableNaClExceptionHandling) ||
254 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
255 enable_exception_handling_ = true;
[email protected]b39c6d92012-01-31 16:38:41256#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06257 if (!RunningOnWOW64()) {
258 debug_context_ = new DebugContext();
259 }
[email protected]b39c6d92012-01-31 16:38:41260#endif
[email protected]5ca93be2012-03-21 20:04:06261 }
[email protected]d032f492009-09-29 00:33:46262}
263
[email protected]fb1277e82009-11-21 20:32:30264NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49265 int exit_code;
[email protected]4967f792012-01-20 22:14:40266 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49267 std::string message =
268 base::StringPrintf("NaCl process exited with status %i (0x%x)",
269 exit_code, exit_code);
270 if (exit_code == 0) {
271 LOG(INFO) << message;
272 } else {
273 LOG(ERROR) << message;
274 }
275
[email protected]1d8a3d1f2011-02-19 07:11:52276 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04277 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
278 LOG(ERROR) << "nacl::Close() failed";
279 }
[email protected]c47ec402010-07-29 10:20:49280 }
[email protected]1d8a3d1f2011-02-19 07:11:52281 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04282 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
283 LOG(ERROR) << "nacl::Close() failed";
284 }
[email protected]c47ec402010-07-29 10:20:49285 }
286
[email protected]909c2402011-05-09 11:39:04287 if (reply_msg_) {
288 // The process failed to launch for some reason.
289 // Don't keep the renderer hanging.
290 reply_msg_->set_reply_error();
291 chrome_render_message_filter_->Send(reply_msg_);
292 }
[email protected]b39c6d92012-01-31 16:38:41293#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36294 if (process_launched_by_broker_) {
295 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06296 }
297 if (debug_context_ != NULL) {
[email protected]b39c6d92012-01-31 16:38:41298 debug_context_->SetChildProcessHost(NULL);
299 }
300#endif
[email protected]fb1277e82009-11-21 20:32:30301}
302
[email protected]773ebb92011-11-15 19:06:52303// Attempt to ensure the IRT will be available when we need it, but don't wait.
304bool NaClBrowser::EnsureIrtAvailable() {
305 if (IrtAvailable())
306 return true;
307
308 return BrowserThread::PostTask(
309 BrowserThread::FILE, FROM_HERE,
310 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
311}
312
313// We really need the IRT to be available now, so make sure that it is.
314// When it's ready, we'll run the reply closure.
315bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
316 return BrowserThread::PostTaskAndReply(
317 BrowserThread::FILE, FROM_HERE,
318 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
319}
320
321// This is called at browser startup.
322// static
323void NaClProcessHost::EarlyStartup() {
324#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
325 // Open the IRT file early to make sure that it isn't replaced out from
326 // under us by autoupdate.
327 NaClBrowser::GetInstance()->EnsureIrtAvailable();
328#endif
329}
330
[email protected]a575da52012-03-22 13:08:36331void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52332 ChromeRenderMessageFilter* chrome_render_message_filter,
333 int socket_count,
334 IPC::Message* reply_msg) {
[email protected]a575da52012-03-22 13:08:36335 chrome_render_message_filter_ = chrome_render_message_filter;
336 reply_msg_ = reply_msg;
337
[email protected]c47ec402010-07-29 10:20:49338 // Place an arbitrary limit on the number of sockets to limit
339 // exposure in case the renderer is compromised. We can increase
340 // this if necessary.
341 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36342 delete this;
343 return;
[email protected]c47ec402010-07-29 10:20:49344 }
345
[email protected]773ebb92011-11-15 19:06:52346 // Start getting the IRT open asynchronously while we launch the NaCl process.
347 // We'll make sure this actually finished in OnProcessLaunched, below.
348 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
349 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36350 delete this;
351 return;
[email protected]773ebb92011-11-15 19:06:52352 }
353
[email protected]c47ec402010-07-29 10:20:49354 // Rather than creating a socket pair in the renderer, and passing
355 // one side through the browser to sel_ldr, socket pairs are created
356 // in the browser and then passed to the renderer and sel_ldr.
357 //
358 // This is mainly for the benefit of Windows, where sockets cannot
359 // be passed in messages, but are copied via DuplicateHandle().
360 // This means the sandboxed renderer cannot send handles to the
361 // browser process.
362
363 for (int i = 0; i < socket_count; i++) {
364 nacl::Handle pair[2];
365 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36366 if (nacl::SocketPair(pair) == -1) {
367 delete this;
368 return;
369 }
[email protected]1d8a3d1f2011-02-19 07:11:52370 internal_->sockets_for_renderer.push_back(pair[0]);
371 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49372 SetCloseOnExec(pair[0]);
373 SetCloseOnExec(pair[1]);
374 }
[email protected]d032f492009-09-29 00:33:46375
376 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30377 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36378 delete this;
[email protected]d032f492009-09-29 00:33:46379 }
[email protected]d032f492009-09-29 00:33:46380}
381
[email protected]8ae7bd6f2012-03-14 03:23:02382scoped_ptr<CommandLine> NaClProcessHost::LaunchWithNaClGdb(
383 const FilePath& nacl_gdb, CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46384 CommandLine* cmd_line = new CommandLine(nacl_gdb);
385 // We can't use PrependWrapper because our parameters contain spaces.
386 cmd_line->AppendArg("--eval-command");
387 const FilePath::StringType& irt_path =
388 NaClBrowser::GetInstance()->GetIrtFilePath().value();
389 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
390 cmd_line->AppendArg("--args");
391 const CommandLine::StringVector& argv = line->argv();
392 for (size_t i = 0; i < argv.size(); i++) {
393 cmd_line->AppendArgNative(argv[i]);
394 }
395 return scoped_ptr<CommandLine>(cmd_line);
396}
397
[email protected]fb1277e82009-11-21 20:32:30398bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40399 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44400 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46401 return false;
402
[email protected]e3fc75a2011-05-05 08:20:42403 CommandLine::StringType nacl_loader_prefix;
404#if defined(OS_POSIX)
405 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
406 switches::kNaClLoaderCmdPrefix);
407#endif // defined(OS_POSIX)
408
[email protected]d032f492009-09-29 00:33:46409 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36410
411#if defined(OS_MACOSX)
412 // The Native Client process needs to be able to allocate a 1GB contiguous
413 // region to use as the client environment's virtual address space. ASLR
414 // (PIE) interferes with this by making it possible that no gap large enough
415 // to accomodate this request will exist in the child process' address
416 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
417 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49418 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36419#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49420 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
421 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36422#else
[email protected]4cb43102011-12-02 20:24:49423 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36424#endif
425
[email protected]4cb43102011-12-02 20:24:49426 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30427 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46428 return false;
429
[email protected]31a665e72012-03-11 12:37:46430#if defined(OS_WIN)
431 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
432 if (RunningOnWOW64()) {
433 FilePath module_path;
434 if (!PathService::Get(base::FILE_MODULE, &module_path))
435 return false;
436 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
437 }
438#endif
439
[email protected]33a05af2012-03-02 18:15:51440 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
441 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13442
[email protected]05076ba22010-07-30 05:59:57443 cmd_line->AppendSwitchASCII(switches::kProcessType,
444 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44445 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44446 if (logging::DialogsAreSuppressed())
447 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46448
[email protected]e3fc75a2011-05-05 08:20:42449 if (!nacl_loader_prefix.empty())
450 cmd_line->PrependWrapper(nacl_loader_prefix);
451
[email protected]31a665e72012-03-11 12:37:46452 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
453 switches::kNaClGdb);
454 if (!nacl_gdb.empty()) {
455 cmd_line->AppendSwitch(switches::kNoSandbox);
456 scoped_ptr<CommandLine> gdb_cmd_line(
457 LaunchWithNaClGdb(nacl_gdb, cmd_line.get()));
458 // We can't use process_->Launch() because OnProcessLaunched will be called
459 // with process_->GetData().handle filled by handle of gdb process. This
460 // handle will be used to duplicate handles for NaCl process and as
461 // a result NaCl process will not be able to use them.
462 //
463 // So we don't fill process_->GetData().handle and wait for
464 // OnChannelConnected to get handle of NaCl process from its pid. Then we
465 // call OnProcessLaunched.
466 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
467 }
468
[email protected]103607e2010-02-01 18:57:09469 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46470#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52471 if (RunningOnWOW64()) {
[email protected]1dbaaa702011-04-06 17:43:42472 return NaClBrokerService::GetInstance()->LaunchLoader(
[email protected]4734d0b2011-12-03 07:10:44473 this, ASCIIToWide(channel_id));
[email protected]4bdde602010-06-16 03:17:35474 } else {
[email protected]33a05af2012-03-02 18:15:51475 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35476 }
[email protected]103607e2010-02-01 18:57:09477#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40478 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20479 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51480 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09481#endif
[email protected]d032f492009-09-29 00:33:46482
[email protected]fb1277e82009-11-21 20:32:30483 return true;
[email protected]d032f492009-09-29 00:33:46484}
485
[email protected]103607e2010-02-01 18:57:09486void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
[email protected]a575da52012-03-22 13:08:36487#if defined(OS_WIN)
488 process_launched_by_broker_ = true;
489#endif
[email protected]4967f792012-01-20 22:14:40490 process_->SetHandle(handle);
[email protected]103607e2010-02-01 18:57:09491 OnProcessLaunched();
492}
493
[email protected]463ea5f2011-12-03 06:57:47494void NaClProcessHost::OnProcessCrashed(int exit_code) {
495 std::string message = base::StringPrintf(
496 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
497 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09498}
499
[email protected]75e0c502011-12-16 21:53:01500namespace {
501
502// Determine the name of the IRT file based on the architecture.
503
504#define NACL_IRT_FILE_NAME(arch_string) \
505 (FILE_PATH_LITERAL("nacl_irt_") \
506 FILE_PATH_LITERAL(arch_string) \
507 FILE_PATH_LITERAL(".nexe"))
508
509const FilePath::StringType NaClIrtName() {
510#if defined(ARCH_CPU_X86_FAMILY)
[email protected]75e0c502011-12-16 21:53:01511#if defined(ARCH_CPU_X86_64)
[email protected]5804cb5d2011-12-19 21:55:35512 bool is64 = true;
513#elif defined(OS_WIN)
514 bool is64 = RunningOnWOW64();
515#else
516 bool is64 = false;
[email protected]75e0c502011-12-16 21:53:01517#endif
518 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
519#elif defined(ARCH_CPU_ARMEL)
520 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
521 // That may need to be based on the actual nexe rather than a static
522 // choice, which would require substantial refactoring.
523 return NACL_IRT_FILE_NAME("arm");
524#else
525#error Add support for your architecture to NaCl IRT file selection
526#endif
527}
528
529} // namespace
530
[email protected]31a665e72012-03-11 12:37:46531void NaClBrowser::InitIrtFilePath() {
[email protected]88e15832011-07-19 01:18:24532 // Allow the IRT library to be overridden via an environment
533 // variable. This allows the NaCl/Chromium integration bot to
534 // specify a newly-built IRT rather than using a prebuilt one
535 // downloaded via Chromium's DEPS file. We use the same environment
536 // variable that the standalone NaCl PPAPI plugin accepts.
537 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
538 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52539 FilePath::StringType path_string(
540 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
[email protected]31a665e72012-03-11 12:37:46541 irt_filepath_ = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24542 } else {
543 FilePath plugin_dir;
544 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
545 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24546 return;
547 }
[email protected]773ebb92011-11-15 19:06:52548
[email protected]31a665e72012-03-11 12:37:46549 irt_filepath_ = plugin_dir.Append(NaClIrtName());
[email protected]338466a82011-05-03 04:27:43550 }
[email protected]31a665e72012-03-11 12:37:46551}
552
553const FilePath& NaClBrowser::GetIrtFilePath() {
554 return irt_filepath_;
555}
556
557// This only ever runs on the BrowserThread::FILE thread.
558// If multiple tasks are posted, the later ones are no-ops.
559void NaClBrowser::OpenIrtLibraryFile() {
560 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
561 // We've already run.
562 return;
[email protected]88e15832011-07-19 01:18:24563
[email protected]773ebb92011-11-15 19:06:52564 base::PlatformFileError error_code;
[email protected]31a665e72012-03-11 12:37:46565 irt_platform_file_ = base::CreatePlatformFile(irt_filepath_,
[email protected]773ebb92011-11-15 19:06:52566 base::PLATFORM_FILE_OPEN |
567 base::PLATFORM_FILE_READ,
568 NULL,
569 &error_code);
570 if (error_code != base::PLATFORM_FILE_OK) {
571 LOG(ERROR) << "Failed to open NaCl IRT file \""
[email protected]31a665e72012-03-11 12:37:46572 << irt_filepath_.LossyDisplayName()
[email protected]773ebb92011-11-15 19:06:52573 << "\": " << error_code;
574 }
575}
576
577void NaClProcessHost::OnProcessLaunched() {
578 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
579
580 if (nacl_browser->IrtAvailable()) {
581 // The IRT is already open. Away we go.
582 SendStart(nacl_browser->IrtFile());
583 } else {
584 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43585 if (!nacl_browser->MakeIrtAvailable(
586 base::Bind(&NaClProcessHost::IrtReady,
587 weak_factory_.GetWeakPtr())))
588 delete this;
[email protected]773ebb92011-11-15 19:06:52589 }
590}
591
592// The asynchronous attempt to get the IRT file open has completed.
593void NaClProcessHost::IrtReady() {
594 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
595
596 if (nacl_browser->IrtAvailable()) {
597 SendStart(nacl_browser->IrtFile());
598 } else {
599 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43600 delete this;
601 }
602}
603
[email protected]b39c6d92012-01-31 16:38:41604#if defined(OS_WIN)
[email protected]b39c6d92012-01-31 16:38:41605void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]31a665e72012-03-11 12:37:46606 // Set process handle, if it was not set previously.
607 // This is needed when NaCl process is launched with nacl-gdb.
608 if (process_->GetData().handle == base::kNullProcessHandle) {
609 base::ProcessHandle process;
610 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
611 switches::kNaClGdb).empty());
612 if (base::OpenProcessHandleWithAccess(
613 peer_pid,
614 base::kProcessAccessDuplicateHandle |
[email protected]59261242012-03-19 13:08:59615 base::kProcessAccessQueryInformation |
[email protected]31a665e72012-03-11 12:37:46616 base::kProcessAccessWaitForTermination,
617 &process)) {
618 process_->SetHandle(process);
619 OnProcessLaunched();
620 } else {
621 LOG(ERROR) << "Failed to get process handle";
622 }
623 }
[email protected]5ca93be2012-03-21 20:04:06624 if (debug_context_ == NULL) {
[email protected]b39c6d92012-01-31 16:38:41625 return;
626 }
627 // Start new thread for debug loop
628 debug_context_->SetChildProcessHost(process_->GetHost());
629 // We can't use process_->GetData().handle because it doesn't have necessary
630 // access rights.
631 base::ProcessHandle process;
632 if (!base::OpenProcessHandleWithAccess(
633 peer_pid,
634 base::kProcessAccessQueryInformation |
635 base::kProcessAccessSuspendResume |
636 base::kProcessAccessTerminate |
637 base::kProcessAccessVMOperation |
638 base::kProcessAccessVMRead |
639 base::kProcessAccessVMWrite |
640 base::kProcessAccessWaitForTermination,
641 &process)) {
642 LOG(ERROR) << "Failed to open the process";
643 debug_context_->AllowAndSendStartMessage();
644 return;
645 }
646 base::Thread* dbg_thread = new base::Thread("Debug thread");
647 if (!dbg_thread->Start()) {
648 LOG(ERROR) << "Debug thread not started";
649 debug_context_->AllowAndSendStartMessage();
650 base::CloseProcessHandle(process);
651 return;
652 }
653 debug_context_->SetDebugThread(dbg_thread);
654 // System can not reallocate pid until we close process handle. So using
655 // pid in different thread is fine.
656 dbg_thread->message_loop()->PostTask(FROM_HERE,
657 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
[email protected]5ca93be2012-03-21 20:04:06658 debug_context_, peer_pid, process));
[email protected]b39c6d92012-01-31 16:38:41659}
660#else
661void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
662}
663#endif
664
665
[email protected]773ebb92011-11-15 19:06:52666static bool SendHandleToSelLdr(
667 base::ProcessHandle processh,
668 nacl::Handle sourceh, bool close_source,
669 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
670#if defined(OS_WIN)
671 HANDLE channel;
672 int flags = DUPLICATE_SAME_ACCESS;
673 if (close_source)
674 flags |= DUPLICATE_CLOSE_SOURCE;
675 if (!DuplicateHandle(GetCurrentProcess(),
676 reinterpret_cast<HANDLE>(sourceh),
677 processh,
678 &channel,
679 0, // Unused given DUPLICATE_SAME_ACCESS.
680 FALSE,
681 flags)) {
682 LOG(ERROR) << "DuplicateHandle() failed";
683 return false;
684 }
685 handles_for_sel_ldr->push_back(
686 reinterpret_cast<nacl::FileDescriptor>(channel));
687#else
688 nacl::FileDescriptor channel;
689 channel.fd = sourceh;
690 channel.auto_close = close_source;
691 handles_for_sel_ldr->push_back(channel);
692#endif
693 return true;
694}
695
696void NaClProcessHost::SendStart(base::PlatformFile irt_file) {
697 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
698
[email protected]c47ec402010-07-29 10:20:49699 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30700 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30701
[email protected]1d8a3d1f2011-02-19 07:11:52702 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49703#if defined(OS_WIN)
704 // Copy the handle into the renderer process.
705 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04706 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
707 reinterpret_cast<HANDLE>(
708 internal_->sockets_for_renderer[i]),
709 chrome_render_message_filter_->peer_handle(),
710 &handle_in_renderer,
711 0, // Unused given DUPLICATE_SAME_ACCESS.
712 FALSE,
713 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
714 LOG(ERROR) << "DuplicateHandle() failed";
715 delete this;
716 return;
717 }
[email protected]c47ec402010-07-29 10:20:49718 handles_for_renderer.push_back(
719 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
720#else
721 // No need to dup the imc_handle - we don't pass it anywhere else so
722 // it cannot be closed.
723 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52724 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49725 imc_handle.auto_close = true;
726 handles_for_renderer.push_back(imc_handle);
727#endif
728 }
729
[email protected]4967f792012-01-20 22:14:40730 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49731#if defined(OS_WIN)
732 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04733 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40734 data.handle,
[email protected]909c2402011-05-09 11:39:04735 chrome_render_message_filter_->peer_handle(),
736 &nacl_process_handle,
737 PROCESS_DUP_HANDLE,
738 FALSE,
739 0)) {
740 LOG(ERROR) << "DuplicateHandle() failed";
741 delete this;
742 return;
743 }
[email protected]fb1277e82009-11-21 20:32:30744#else
[email protected]fb1277e82009-11-21 20:32:30745 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40746 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30747#endif
748
749 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40750 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30751
[email protected]2ccf45c2011-08-19 23:35:50752 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49753 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52754 chrome_render_message_filter_->Send(reply_msg_);
755 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30756 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52757 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30758
[email protected]c47ec402010-07-29 10:20:49759 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52760 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40761 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52762 internal_->sockets_for_sel_ldr[i], true,
763 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04764 delete this;
[email protected]c47ec402010-07-29 10:20:49765 return;
766 }
[email protected]773ebb92011-11-15 19:06:52767 }
768
769 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40770 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52771 delete this;
772 return;
[email protected]c47ec402010-07-29 10:20:49773 }
774
[email protected]ab88d1542011-11-18 22:52:00775#if defined(OS_MACOSX)
776 // For dynamic loading support, NaCl requires a file descriptor that
777 // was created in /tmp, since those created with shm_open() are not
778 // mappable with PROT_EXEC. Rather than requiring an extra IPC
779 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25780 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31781 base::SharedMemoryCreateOptions options;
782 options.size = 1;
783 options.executable = true;
784 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30785 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04786 delete this;
[email protected]2c68bf032010-11-11 23:16:30787 return;
788 }
[email protected]cbbe7842011-11-17 22:01:25789 nacl::FileDescriptor memory_fd;
790 memory_fd.fd = dup(memory_buffer.handle().fd);
791 if (memory_fd.fd < 0) {
792 LOG(ERROR) << "Failed to dup() a file descriptor";
793 delete this;
794 return;
795 }
796 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30797 handles_for_sel_ldr.push_back(memory_fd);
798#endif
799
[email protected]5ca93be2012-03-21 20:04:06800 IPC::Message* start_message =
801 new NaClProcessMsg_Start(handles_for_sel_ldr, enable_exception_handling_);
[email protected]b39c6d92012-01-31 16:38:41802#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06803 if (debug_context_ != NULL) {
804 debug_context_->SetStartMessage(start_message);
[email protected]b39c6d92012-01-31 16:38:41805 debug_context_->SendStartMessage();
806 } else {
[email protected]5ca93be2012-03-21 20:04:06807 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41808 }
809#else
[email protected]5ca93be2012-03-21 20:04:06810 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41811#endif
812
[email protected]1d8a3d1f2011-02-19 07:11:52813 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46814}
815
[email protected]a95986a82010-12-24 06:19:28816bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]d032f492009-09-29 00:33:46817 NOTREACHED() << "Invalid message with type = " << msg.type();
[email protected]a95986a82010-12-24 06:19:28818 return false;
[email protected]d032f492009-09-29 00:33:46819}