blob: bd4449aaa188f3119a185482624b0a057bede4a8 [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]5b974952012-04-05 18:18:2312#include "base/message_loop.h"
[email protected]338466a82011-05-03 04:27:4313#include "base/path_service.h"
[email protected]5b974952012-04-05 18:18:2314#include "base/string_number_conversions.h"
15#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3316#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2817#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2218#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3719#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2020#include "build/build_config.h"
[email protected]8f42b4d2012-03-24 14:12:3321#include "chrome/browser/extensions/extension_info_map.h"
[email protected]2e5f2ea42012-05-09 21:39:2122#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3323#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4624#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4325#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4626#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0027#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4628#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0929#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4630#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3031#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3332#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4033#include "content/public/browser/browser_child_process_host.h"
34#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4435#include "content/public/common/child_process_host.h"
[email protected]d032f492009-09-29 00:33:4636#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5237#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1638#include "net/base/net_util.h"
[email protected]d032f492009-09-29 00:33:4639
[email protected]d032f492009-09-29 00:33:4640#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2041#include <fcntl.h>
42
[email protected]d032f492009-09-29 00:33:4643#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3544#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2045#include <windows.h>
46
[email protected]b39c6d92012-01-31 16:38:4147#include "base/threading/thread.h"
48#include "base/process_util.h"
[email protected]4c65fb632012-04-27 00:42:2549#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3550#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3851#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5052#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4653#endif
54
[email protected]631bb742011-11-02 11:29:3955using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4056using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4457using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3958
[email protected]646e15552012-04-06 22:01:0459namespace {
60
61#if defined(OS_WIN)
62bool RunningOnWOW64() {
63 return (base::win::OSInfo::GetInstance()->wow64_status() ==
64 base::win::OSInfo::WOW64_ENABLED);
65}
66#endif
67
[email protected]646e15552012-04-06 22:01:0468void SetCloseOnExec(nacl::Handle fd) {
69#if defined(OS_POSIX)
70 int flags = fcntl(fd, F_GETFD);
71 CHECK_NE(flags, -1);
72 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
73 CHECK_EQ(rc, 0);
74#endif
75}
76
[email protected]00d99542012-04-17 22:48:0277bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0478 base::ProcessHandle processh,
79 nacl::Handle sourceh,
80 bool close_source,
81 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
82#if defined(OS_WIN)
83 HANDLE channel;
84 int flags = DUPLICATE_SAME_ACCESS;
85 if (close_source)
86 flags |= DUPLICATE_CLOSE_SOURCE;
87 if (!DuplicateHandle(GetCurrentProcess(),
88 reinterpret_cast<HANDLE>(sourceh),
89 processh,
90 &channel,
91 0, // Unused given DUPLICATE_SAME_ACCESS.
92 FALSE,
93 flags)) {
[email protected]09afc2e2012-04-10 17:29:0394 DLOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:0495 return false;
96 }
97 handles_for_sel_ldr->push_back(
98 reinterpret_cast<nacl::FileDescriptor>(channel));
99#else
100 nacl::FileDescriptor channel;
101 channel.fd = sourceh;
102 channel.auto_close = close_source;
103 handles_for_sel_ldr->push_back(channel);
104#endif
105 return true;
106}
107
[email protected]646e15552012-04-06 22:01:04108} // namespace
109
[email protected]1d8a3d1f2011-02-19 07:11:52110struct NaClProcessHost::NaClInternal {
111 std::vector<nacl::Handle> sockets_for_renderer;
112 std::vector<nacl::Handle> sockets_for_sel_ldr;
113};
114
[email protected]646e15552012-04-06 22:01:04115// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52116
[email protected]57473ebc2012-05-30 07:44:52117NaClProcessHost::NaClProcessHost(const GURL& manifest_url, bool off_the_record)
[email protected]87f35592012-04-08 00:49:16118 : manifest_url_(manifest_url),
[email protected]a575da52012-03-22 13:08:36119#if defined(OS_WIN)
120 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23121#elif defined(OS_LINUX)
122 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36123#endif
124 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38125#if defined(OS_WIN)
126 debug_exception_handler_requested_(false),
127#endif
[email protected]1d8a3d1f2011-02-19 07:11:52128 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06129 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52130 enable_exception_handling_(false),
131 off_the_record_(off_the_record) {
[email protected]4967f792012-01-20 22:14:40132 process_.reset(content::BrowserChildProcessHost::Create(
133 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16134
135 // Set the display name so the user knows what plugin the process is running.
136 // We aren't on the UI thread so getting the pref locale for language
137 // formatting isn't possible, so IDN will be lost, but this is probably OK
138 // for this use case.
139 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06140
141 // We allow untrusted hardware exception handling to be enabled via
142 // an env var for consistency with the standalone build of NaCl.
143 if (CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kEnableNaClExceptionHandling) ||
145 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
146 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06147 }
[email protected]d032f492009-09-29 00:33:46148}
149
[email protected]fb1277e82009-11-21 20:32:30150NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49151 int exit_code;
[email protected]4967f792012-01-20 22:14:40152 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49153 std::string message =
154 base::StringPrintf("NaCl process exited with status %i (0x%x)",
155 exit_code, exit_code);
156 if (exit_code == 0) {
157 LOG(INFO) << message;
158 } else {
159 LOG(ERROR) << message;
160 }
161
[email protected]1d8a3d1f2011-02-19 07:11:52162 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04163 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03164 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04165 }
[email protected]c47ec402010-07-29 10:20:49166 }
[email protected]1d8a3d1f2011-02-19 07:11:52167 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04168 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03169 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04170 }
[email protected]c47ec402010-07-29 10:20:49171 }
172
[email protected]909c2402011-05-09 11:39:04173 if (reply_msg_) {
174 // The process failed to launch for some reason.
175 // Don't keep the renderer hanging.
176 reply_msg_->set_reply_error();
177 chrome_render_message_filter_->Send(reply_msg_);
178 }
[email protected]b39c6d92012-01-31 16:38:41179#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36180 if (process_launched_by_broker_) {
181 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06182 }
[email protected]b39c6d92012-01-31 16:38:41183#endif
[email protected]fb1277e82009-11-21 20:32:30184}
185
[email protected]773ebb92011-11-15 19:06:52186// This is called at browser startup.
187// static
188void NaClProcessHost::EarlyStartup() {
189#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
190 // Open the IRT file early to make sure that it isn't replaced out from
191 // under us by autoupdate.
192 NaClBrowser::GetInstance()->EnsureIrtAvailable();
193#endif
194}
195
[email protected]a575da52012-03-22 13:08:36196void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52197 ChromeRenderMessageFilter* chrome_render_message_filter,
198 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33199 IPC::Message* reply_msg,
200 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36201 chrome_render_message_filter_ = chrome_render_message_filter;
202 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33203 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36204
[email protected]c47ec402010-07-29 10:20:49205 // Place an arbitrary limit on the number of sockets to limit
206 // exposure in case the renderer is compromised. We can increase
207 // this if necessary.
208 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36209 delete this;
210 return;
[email protected]c47ec402010-07-29 10:20:49211 }
212
[email protected]773ebb92011-11-15 19:06:52213 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03214 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06215 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
216 nacl_browser->EnsureAllResourcesAvailable();
217 if (!nacl_browser->IsOk()) {
218 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]a575da52012-03-22 13:08:36219 delete this;
220 return;
[email protected]773ebb92011-11-15 19:06:52221 }
222
[email protected]c47ec402010-07-29 10:20:49223 // Rather than creating a socket pair in the renderer, and passing
224 // one side through the browser to sel_ldr, socket pairs are created
225 // in the browser and then passed to the renderer and sel_ldr.
226 //
227 // This is mainly for the benefit of Windows, where sockets cannot
228 // be passed in messages, but are copied via DuplicateHandle().
229 // This means the sandboxed renderer cannot send handles to the
230 // browser process.
231
232 for (int i = 0; i < socket_count; i++) {
233 nacl::Handle pair[2];
234 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36235 if (nacl::SocketPair(pair) == -1) {
236 delete this;
237 return;
238 }
[email protected]1d8a3d1f2011-02-19 07:11:52239 internal_->sockets_for_renderer.push_back(pair[0]);
240 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49241 SetCloseOnExec(pair[0]);
242 SetCloseOnExec(pair[1]);
243 }
[email protected]d032f492009-09-29 00:33:46244
245 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30246 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36247 delete this;
[email protected]d032f492009-09-29 00:33:46248 }
[email protected]d032f492009-09-29 00:33:46249}
[email protected]646e15552012-04-06 22:01:04250
[email protected]5b974952012-04-05 18:18:23251#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04252void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
253 // Set process handle, if it was not set previously.
254 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40255 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
256 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04257 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40258 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04259 if (base::OpenProcessHandleWithAccess(
260 peer_pid,
261 base::kProcessAccessDuplicateHandle |
262 base::kProcessAccessQueryInformation |
263 base::kProcessAccessWaitForTermination,
264 &process)) {
265 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03266 if (!StartWithLaunchedProcess()) {
267 delete this;
268 return;
269 }
[email protected]646e15552012-04-06 22:01:04270 } else {
[email protected]09afc2e2012-04-10 17:29:03271 DLOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04272 }
273 }
[email protected]646e15552012-04-06 22:01:04274}
275#else
276void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
277}
278#endif
279
280#if defined(OS_WIN)
281void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
282 process_launched_by_broker_ = true;
283 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03284 if (!StartWithLaunchedProcess())
285 delete this;
[email protected]646e15552012-04-06 22:01:04286}
287
[email protected]ea6588842012-05-03 05:39:38288void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
289 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
290 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
291 Send(reply);
[email protected]646e15552012-04-06 22:01:04292}
293#endif
294
295// Needed to handle sync messages in OnMessageRecieved.
296bool NaClProcessHost::Send(IPC::Message* msg) {
297 return process_->Send(msg);
298}
299
300#if defined(OS_WIN)
301scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33302 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23303 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46304 CommandLine* cmd_line = new CommandLine(nacl_gdb);
305 // We can't use PrependWrapper because our parameters contain spaces.
306 cmd_line->AppendArg("--eval-command");
307 const FilePath::StringType& irt_path =
308 NaClBrowser::GetInstance()->GetIrtFilePath().value();
309 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23310 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33311 if (!manifest_path.empty()) {
312 cmd_line->AppendArg("--eval-command");
313 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
314 manifest_path.value());
315 }
[email protected]31a665e72012-03-11 12:37:46316 cmd_line->AppendArg("--args");
317 const CommandLine::StringVector& argv = line->argv();
318 for (size_t i = 0; i < argv.size(); i++) {
319 cmd_line->AppendArgNative(argv[i]);
320 }
321 return scoped_ptr<CommandLine>(cmd_line);
322}
[email protected]5b974952012-04-05 18:18:23323#elif defined(OS_LINUX)
324namespace {
325class NaClGdbWatchDelegate : public MessageLoopForIO::Watcher {
326 public:
327 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
328 NaClGdbWatchDelegate(int fd_read, int fd_write,
329 const base::Closure& reply)
330 : fd_read_(fd_read),
331 fd_write_(fd_write),
332 reply_(reply) {}
333
334 ~NaClGdbWatchDelegate() {
335 if (HANDLE_EINTR(close(fd_read_)) != 0)
336 DLOG(ERROR) << "close(fd_read_) failed";
337 if (HANDLE_EINTR(close(fd_write_)) != 0)
338 DLOG(ERROR) << "close(fd_write_) failed";
339 }
340
341 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
342 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
343
344 private:
345 int fd_read_;
346 int fd_write_;
347 base::Closure reply_;
348};
349
350void NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(int fd) {
351 char buf;
352 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
353 LOG(ERROR) << "Failed to sync with nacl-gdb";
354 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
355}
356} // namespace
357
358bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
359 CommandLine::StringType nacl_gdb =
360 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
361 switches::kNaClGdb);
362 CommandLine::StringVector argv;
363 // We don't support spaces inside arguments in --nacl-gdb switch.
364 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
365 CommandLine cmd_line(argv);
366 cmd_line.AppendArg("--eval-command");
367 const FilePath::StringType& irt_path =
368 NaClBrowser::GetInstance()->GetIrtFilePath().value();
369 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
370 FilePath manifest_path = GetManifestPath();
371 if (!manifest_path.empty()) {
372 cmd_line.AppendArg("--eval-command");
373 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
374 manifest_path.value());
375 }
376 cmd_line.AppendArg("--eval-command");
377 cmd_line.AppendArg("attach " + base::IntToString(pid));
378 int fds[2];
379 if (pipe(fds) != 0)
380 return false;
381 // Tell the debugger to send a byte to the writable end of the pipe.
382 // We use a file descriptor in our process because the debugger will be
383 // typically launched in a separate terminal, and a lot of terminals close all
384 // file descriptors before launching external programs.
385 cmd_line.AppendArg("--eval-command");
386 cmd_line.AppendArg("dump binary value /proc/" +
387 base::IntToString(base::GetCurrentProcId()) +
388 "/fd/" + base::IntToString(fds[1]) + " (char)0");
389 // wait on fds[0]
390 // If the debugger crashes before attaching to the NaCl process, the user can
391 // release resources by terminating the NaCl loader in Chrome Task Manager.
392 nacl_gdb_watcher_delegate_.reset(
393 new NaClGdbWatchDelegate(
394 fds[0], fds[1],
395 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
396 weak_factory_.GetWeakPtr())));
397 MessageLoopForIO::current()->WatchFileDescriptor(
398 fds[0],
399 true,
400 MessageLoopForIO::WATCH_READ,
401 &nacl_gdb_watcher_,
402 nacl_gdb_watcher_delegate_.get());
403 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
404}
405
406void NaClProcessHost::OnNaClGdbAttached() {
407 wait_for_nacl_gdb_ = false;
408 nacl_gdb_watcher_.StopWatchingFileDescriptor();
409 nacl_gdb_watcher_delegate_.reset();
410 OnProcessLaunched();
411}
412#endif
413
414FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34415 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16416 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
417 if (extension != NULL && manifest_url_.SchemeIs(chrome::kExtensionScheme)) {
418 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23419 TrimString(path, "/", &path); // Remove first slash
420 return extension->path().AppendASCII(path);
421 }
422 return FilePath();
423}
[email protected]31a665e72012-03-11 12:37:46424
[email protected]fb1277e82009-11-21 20:32:30425bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40426 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44427 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46428 return false;
429
[email protected]e3fc75a2011-05-05 08:20:42430 CommandLine::StringType nacl_loader_prefix;
431#if defined(OS_POSIX)
432 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
433 switches::kNaClLoaderCmdPrefix);
434#endif // defined(OS_POSIX)
435
[email protected]d032f492009-09-29 00:33:46436 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36437
438#if defined(OS_MACOSX)
439 // The Native Client process needs to be able to allocate a 1GB contiguous
440 // region to use as the client environment's virtual address space. ASLR
441 // (PIE) interferes with this by making it possible that no gap large enough
442 // to accomodate this request will exist in the child process' address
443 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
444 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49445 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36446#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49447 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
448 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36449#else
[email protected]4cb43102011-12-02 20:24:49450 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36451#endif
452
[email protected]4cb43102011-12-02 20:24:49453 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30454 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46455 return false;
456
[email protected]31a665e72012-03-11 12:37:46457#if defined(OS_WIN)
458 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
459 if (RunningOnWOW64()) {
460 FilePath module_path;
461 if (!PathService::Get(base::FILE_MODULE, &module_path))
462 return false;
463 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
464 }
465#endif
466
[email protected]33a05af2012-03-02 18:15:51467 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
468 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13469
[email protected]05076ba22010-07-30 05:59:57470 cmd_line->AppendSwitchASCII(switches::kProcessType,
471 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44472 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44473 if (logging::DialogsAreSuppressed())
474 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46475
[email protected]e3fc75a2011-05-05 08:20:42476 if (!nacl_loader_prefix.empty())
477 cmd_line->PrependWrapper(nacl_loader_prefix);
478
[email protected]31a665e72012-03-11 12:37:46479 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
480 switches::kNaClGdb);
481 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23482#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46483 cmd_line->AppendSwitch(switches::kNoSandbox);
484 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04485 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46486 // We can't use process_->Launch() because OnProcessLaunched will be called
487 // with process_->GetData().handle filled by handle of gdb process. This
488 // handle will be used to duplicate handles for NaCl process and as
489 // a result NaCl process will not be able to use them.
490 //
491 // So we don't fill process_->GetData().handle and wait for
492 // OnChannelConnected to get handle of NaCl process from its pid. Then we
493 // call OnProcessLaunched.
494 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23495#elif defined(OS_LINUX)
496 wait_for_nacl_gdb_ = true;
497#endif
[email protected]31a665e72012-03-11 12:37:46498 }
499
[email protected]103607e2010-02-01 18:57:09500 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46501#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52502 if (RunningOnWOW64()) {
[email protected]26b3c002012-04-26 19:38:34503 return NaClBrokerService::GetInstance()->LaunchLoader(
504 weak_factory_.GetWeakPtr(), channel_id);
[email protected]4bdde602010-06-16 03:17:35505 } else {
[email protected]33a05af2012-03-02 18:15:51506 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35507 }
[email protected]103607e2010-02-01 18:57:09508#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40509 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20510 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51511 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09512#endif
[email protected]d032f492009-09-29 00:33:46513
[email protected]fb1277e82009-11-21 20:32:30514 return true;
[email protected]d032f492009-09-29 00:33:46515}
516
[email protected]646e15552012-04-06 22:01:04517bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
518 bool handled = true;
519 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
520 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
521 OnQueryKnownToValidate)
522 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
523 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38524#if defined(OS_WIN)
525 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
526 OnAttachDebugExceptionHandler)
527#endif
[email protected]646e15552012-04-06 22:01:04528 IPC_MESSAGE_UNHANDLED(handled = false)
529 IPC_END_MESSAGE_MAP()
530 return handled;
[email protected]103607e2010-02-01 18:57:09531}
532
[email protected]773ebb92011-11-15 19:06:52533void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03534 if (!StartWithLaunchedProcess())
535 delete this;
[email protected]773ebb92011-11-15 19:06:52536}
537
[email protected]e97990f2012-05-15 22:06:06538// Called when the NaClBrowser singleton has been fully initialized.
539void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52540 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06541 if (!nacl_browser->IsReady() || !SendStart()) {
[email protected]09afc2e2012-04-10 17:29:03542 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]338466a82011-05-03 04:27:43543 delete this;
544 }
545}
546
[email protected]00d99542012-04-17 22:48:02547bool NaClProcessHost::ReplyToRenderer() {
[email protected]c47ec402010-07-29 10:20:49548 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]1d8a3d1f2011-02-19 07:11:52549 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49550#if defined(OS_WIN)
551 // Copy the handle into the renderer process.
552 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04553 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
554 reinterpret_cast<HANDLE>(
555 internal_->sockets_for_renderer[i]),
556 chrome_render_message_filter_->peer_handle(),
557 &handle_in_renderer,
558 0, // Unused given DUPLICATE_SAME_ACCESS.
559 FALSE,
560 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]09afc2e2012-04-10 17:29:03561 DLOG(ERROR) << "DuplicateHandle() failed";
562 return false;
[email protected]909c2402011-05-09 11:39:04563 }
[email protected]c47ec402010-07-29 10:20:49564 handles_for_renderer.push_back(
565 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
566#else
567 // No need to dup the imc_handle - we don't pass it anywhere else so
568 // it cannot be closed.
569 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52570 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49571 imc_handle.auto_close = true;
572 handles_for_renderer.push_back(imc_handle);
573#endif
574 }
575
576#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50577 // If we are on 64-bit Windows, the NaCl process's sandbox is
578 // managed by a different process from the renderer's sandbox. We
579 // need to inform the renderer's sandbox about the NaCl process so
580 // that the renderer can send handles to the NaCl process using
581 // BrokerDuplicateHandle().
582 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01583 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]e4f6eb0232012-04-17 00:47:50584 DLOG(ERROR) << "Failed to add NaCl process PID";
585 return false;
586 }
587 }
[email protected]fb1277e82009-11-21 20:32:30588#endif
589
[email protected]2ccf45c2011-08-19 23:35:50590 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]171fa98d2012-04-23 21:34:01591 reply_msg_, handles_for_renderer);
[email protected]92d56412011-03-24 20:53:52592 chrome_render_message_filter_->Send(reply_msg_);
593 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30594 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52595 internal_->sockets_for_renderer.clear();
[email protected]00d99542012-04-17 22:48:02596 return true;
597}
[email protected]fb1277e82009-11-21 20:32:30598
[email protected]00d99542012-04-17 22:48:02599bool NaClProcessHost::StartNaClExecution() {
600 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
601
602 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12603 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
604 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02605 params.version = chrome::VersionInfo().CreateVersionString();
606 params.enable_exception_handling = enable_exception_handling_;
607
608 base::PlatformFile irt_file = nacl_browser->IrtFile();
609 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
610
611 const ChildProcessData& data = process_->GetData();
[email protected]1d8a3d1f2011-02-19 07:11:52612 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]00d99542012-04-17 22:48:02613 if (!ShareHandleToSelLdr(data.handle,
614 internal_->sockets_for_sel_ldr[i], true,
615 &params.handles)) {
[email protected]09afc2e2012-04-10 17:29:03616 return false;
[email protected]c47ec402010-07-29 10:20:49617 }
[email protected]773ebb92011-11-15 19:06:52618 }
619
620 // Send over the IRT file handle. We don't close our own copy!
[email protected]00d99542012-04-17 22:48:02621 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
[email protected]09afc2e2012-04-10 17:29:03622 return false;
[email protected]c47ec402010-07-29 10:20:49623
[email protected]ab88d1542011-11-18 22:52:00624#if defined(OS_MACOSX)
625 // For dynamic loading support, NaCl requires a file descriptor that
626 // was created in /tmp, since those created with shm_open() are not
627 // mappable with PROT_EXEC. Rather than requiring an extra IPC
628 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25629 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31630 base::SharedMemoryCreateOptions options;
631 options.size = 1;
632 options.executable = true;
633 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03634 DLOG(ERROR) << "Failed to allocate memory buffer";
635 return false;
[email protected]2c68bf032010-11-11 23:16:30636 }
[email protected]cbbe7842011-11-17 22:01:25637 nacl::FileDescriptor memory_fd;
638 memory_fd.fd = dup(memory_buffer.handle().fd);
639 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03640 DLOG(ERROR) << "Failed to dup() a file descriptor";
641 return false;
[email protected]cbbe7842011-11-17 22:01:25642 }
643 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02644 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30645#endif
646
[email protected]ea6588842012-05-03 05:39:38647 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41648
[email protected]1d8a3d1f2011-02-19 07:11:52649 internal_->sockets_for_sel_ldr.clear();
[email protected]09afc2e2012-04-10 17:29:03650 return true;
651}
652
[email protected]00d99542012-04-17 22:48:02653bool NaClProcessHost::SendStart() {
654 return ReplyToRenderer() && StartNaClExecution();
655}
656
[email protected]09afc2e2012-04-10 17:29:03657bool NaClProcessHost::StartWithLaunchedProcess() {
658#if defined(OS_LINUX)
659 if (wait_for_nacl_gdb_) {
660 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
661 // We will be called with wait_for_nacl_gdb_ = false once debugger is
662 // attached to the program.
663 return true;
664 }
665 DLOG(ERROR) << "Failed to launch debugger";
666 // Continue execution without debugger.
667 }
668#endif
669
670 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03671
[email protected]e97990f2012-05-15 22:06:06672 if (nacl_browser->IsReady()) {
673 return SendStart();
674 } else if (nacl_browser->IsOk()) {
675 nacl_browser->WaitForResources(
676 base::Bind(&NaClProcessHost::OnResourcesReady,
677 weak_factory_.GetWeakPtr()));
678 return true;
679 } else {
680 return false;
681 }
[email protected]d032f492009-09-29 00:33:46682}
683
[email protected]4a0141b2012-03-27 01:15:30684void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
685 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47686 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52687 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30688}
689
690void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52691 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30692}
[email protected]ea6588842012-05-03 05:39:38693
694#if defined(OS_WIN)
695void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
696 IPC::Message* reply_msg) {
697 if (!AttachDebugExceptionHandler(info, reply_msg)) {
698 // Send failure message.
699 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
700 false);
701 Send(reply_msg);
702 }
703}
704
705bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
706 IPC::Message* reply_msg) {
707 if (!enable_exception_handling_) {
708 DLOG(ERROR) <<
709 "Exception handling requested by NaCl process when not enabled";
710 return false;
711 }
712 if (debug_exception_handler_requested_) {
713 // The NaCl process should not request this multiple times.
714 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
715 return false;
716 }
717 debug_exception_handler_requested_ = true;
718
719 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
720 base::win::ScopedHandle process_handle;
721 // We cannot use process_->GetData().handle because it does not have
722 // the necessary access rights. We open the new handle here rather
723 // than in the NaCl broker process in case the NaCl loader process
724 // dies before the NaCl broker process receives the message we send.
725 // The debug exception handler uses DebugActiveProcess() to attach,
726 // but this takes a PID. We need to prevent the NaCl loader's PID
727 // from being reused before DebugActiveProcess() is called, and
728 // holding a process handle open achieves this.
729 if (!base::OpenProcessHandleWithAccess(
730 nacl_pid,
731 base::kProcessAccessQueryInformation |
732 base::kProcessAccessSuspendResume |
733 base::kProcessAccessTerminate |
734 base::kProcessAccessVMOperation |
735 base::kProcessAccessVMRead |
736 base::kProcessAccessVMWrite |
737 base::kProcessAccessWaitForTermination,
738 process_handle.Receive())) {
739 LOG(ERROR) << "Failed to get process handle";
740 return false;
741 }
742
743 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
744 // If the NaCl loader is 64-bit, the process running its debug
745 // exception handler must be 64-bit too, so we use the 64-bit NaCl
746 // broker process for this. Otherwise, on a 32-bit system, we use
747 // the 32-bit browser process to run the debug exception handler.
748 if (RunningOnWOW64()) {
749 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48750 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38751 } else {
752 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48753 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38754 base::MessageLoopProxy::current(),
755 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
756 weak_factory_.GetWeakPtr()));
757 return true;
758 }
759}
760#endif