blob: 4f42e1319c480025e3bc8ca0ed6021afde1c7ca7 [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]9addd1c2012-09-15 14:28:2413#include "base/metrics/histogram.h"
[email protected]338466a82011-05-03 04:27:4314#include "base/path_service.h"
[email protected]108fd342013-01-04 20:46:5415#include "base/process_util.h"
[email protected]5b974952012-04-05 18:18:2316#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3317#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2818#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4119#include "base/strings/string_number_conversions.h"
[email protected]be1ce6a72010-08-03 14:35:2220#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3721#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2022#include "build/build_config.h"
[email protected]8510d282012-08-30 19:47:3823#include "chrome/browser/browser_process.h"
[email protected]8f42b4d2012-03-24 14:12:3324#include "chrome/browser/extensions/extension_info_map.h"
[email protected]8510d282012-08-30 19:47:3825#include "chrome/browser/io_thread.h"
[email protected]2e5f2ea42012-05-09 21:39:2126#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3327#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4628#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4329#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4630#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0031#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4632#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0933#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4634#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3035#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3336#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4037#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0338#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4039#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4440#include "content/public/common/child_process_host.h"
[email protected]9e141212013-01-21 15:21:5641#include "content/public/common/process_type.h"
[email protected]885c0e92012-11-13 20:27:4242#include "extensions/common/constants.h"
43#include "extensions/common/url_pattern.h"
[email protected]8510d282012-08-30 19:47:3844#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4645#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5246#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1647#include "net/base/net_util.h"
[email protected]d571c032012-09-14 12:39:1848#include "net/base/tcp_listen_socket.h"
[email protected]8510d282012-08-30 19:47:3849#include "ppapi/proxy/ppapi_messages.h"
[email protected]d032f492009-09-29 00:33:4650
[email protected]d032f492009-09-29 00:33:4651#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2052#include <fcntl.h>
53
[email protected]d032f492009-09-29 00:33:4654#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3555#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2056#include <windows.h>
57
[email protected]b39c6d92012-01-31 16:38:4158#include "base/process_util.h"
[email protected]885c0e92012-11-13 20:27:4259#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2560#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3561#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3862#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5063#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4664#endif
65
[email protected]631bb742011-11-02 11:29:3966using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4067using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4468using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3869using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3970
[email protected]646e15552012-04-06 22:01:0471namespace {
72
73#if defined(OS_WIN)
74bool RunningOnWOW64() {
75 return (base::win::OSInfo::GetInstance()->wow64_status() ==
76 base::win::OSInfo::WOW64_ENABLED);
77}
78#endif
79
[email protected]646e15552012-04-06 22:01:0480void SetCloseOnExec(nacl::Handle fd) {
81#if defined(OS_POSIX)
82 int flags = fcntl(fd, F_GETFD);
83 CHECK_NE(flags, -1);
84 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
85 CHECK_EQ(rc, 0);
86#endif
87}
88
[email protected]00d99542012-04-17 22:48:0289bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0490 base::ProcessHandle processh,
91 nacl::Handle sourceh,
92 bool close_source,
93 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
94#if defined(OS_WIN)
95 HANDLE channel;
96 int flags = DUPLICATE_SAME_ACCESS;
97 if (close_source)
98 flags |= DUPLICATE_CLOSE_SOURCE;
99 if (!DuplicateHandle(GetCurrentProcess(),
100 reinterpret_cast<HANDLE>(sourceh),
101 processh,
102 &channel,
103 0, // Unused given DUPLICATE_SAME_ACCESS.
104 FALSE,
105 flags)) {
[email protected]ec7540f2013-01-17 00:00:35106 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04107 return false;
108 }
109 handles_for_sel_ldr->push_back(
110 reinterpret_cast<nacl::FileDescriptor>(channel));
111#else
112 nacl::FileDescriptor channel;
113 channel.fd = sourceh;
114 channel.auto_close = close_source;
115 handles_for_sel_ldr->push_back(channel);
116#endif
117 return true;
118}
119
[email protected]27b04df2012-12-10 23:43:10120ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
121 // Only allow NaCl plugins to request certain permissions. We don't want
122 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
123 // permissions which may expand the surface area of the sandbox.
124 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
125 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
126}
127
[email protected]646e15552012-04-06 22:01:04128} // namespace
129
[email protected]1d8a3d1f2011-02-19 07:11:52130struct NaClProcessHost::NaClInternal {
[email protected]6294dd02013-01-09 17:27:23131 nacl::Handle socket_for_renderer;
132 nacl::Handle socket_for_sel_ldr;
133
134 NaClInternal()
135 : socket_for_renderer(nacl::kInvalidHandle),
136 socket_for_sel_ldr(nacl::kInvalidHandle) { }
[email protected]1d8a3d1f2011-02-19 07:11:52137};
138
[email protected]646e15552012-04-06 22:01:04139// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52140
[email protected]8510d282012-08-30 19:47:38141NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
142 : host_(host) {
143}
144
145bool NaClProcessHost::PluginListener::OnMessageReceived(
146 const IPC::Message& msg) {
147 return host_->OnUntrustedMessageForwarded(msg);
148}
149
[email protected]9da4fed2012-11-01 17:07:55150NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06151 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55152 uint32 permission_bits,
[email protected]2e1e6f02013-01-11 18:22:56153 bool uses_irt,
[email protected]9da4fed2012-11-01 17:07:55154 bool off_the_record)
[email protected]87f35592012-04-08 00:49:16155 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10156 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36157#if defined(OS_WIN)
158 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23159#elif defined(OS_LINUX)
160 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36161#endif
162 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38163#if defined(OS_WIN)
164 debug_exception_handler_requested_(false),
165#endif
[email protected]1d8a3d1f2011-02-19 07:11:52166 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06167 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52168 enable_exception_handling_(false),
[email protected]2c227ff2012-08-11 00:51:32169 enable_debug_stub_(false),
[email protected]2e1e6f02013-01-11 18:22:56170 uses_irt_(uses_irt),
[email protected]8510d282012-08-30 19:47:38171 off_the_record_(off_the_record),
[email protected]e02ff722012-11-06 03:53:06172 ALLOW_THIS_IN_INITIALIZER_LIST(ipc_plugin_listener_(this)),
173 render_view_id_(render_view_id) {
[email protected]4967f792012-01-20 22:14:40174 process_.reset(content::BrowserChildProcessHost::Create(
175 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16176
177 // Set the display name so the user knows what plugin the process is running.
178 // We aren't on the UI thread so getting the pref locale for language
179 // formatting isn't possible, so IDN will be lost, but this is probably OK
180 // for this use case.
181 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06182
183 // We allow untrusted hardware exception handling to be enabled via
184 // an env var for consistency with the standalone build of NaCl.
185 if (CommandLine::ForCurrentProcess()->HasSwitch(
186 switches::kEnableNaClExceptionHandling) ||
187 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
188 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06189 }
[email protected]2c227ff2012-08-11 00:51:32190 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
191 switches::kEnableNaClDebug);
[email protected]d032f492009-09-29 00:33:46192}
193
[email protected]fb1277e82009-11-21 20:32:30194NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49195 int exit_code;
[email protected]4967f792012-01-20 22:14:40196 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49197 std::string message =
198 base::StringPrintf("NaCl process exited with status %i (0x%x)",
199 exit_code, exit_code);
200 if (exit_code == 0) {
201 LOG(INFO) << message;
202 } else {
203 LOG(ERROR) << message;
204 }
205
[email protected]6294dd02013-01-09 17:27:23206 if (internal_->socket_for_renderer != nacl::kInvalidHandle) {
207 if (nacl::Close(internal_->socket_for_renderer) != 0) {
[email protected]09afc2e2012-04-10 17:29:03208 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04209 }
[email protected]c47ec402010-07-29 10:20:49210 }
[email protected]6294dd02013-01-09 17:27:23211
212 if (internal_->socket_for_sel_ldr != nacl::kInvalidHandle) {
213 if (nacl::Close(internal_->socket_for_sel_ldr) != 0) {
[email protected]09afc2e2012-04-10 17:29:03214 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04215 }
[email protected]c47ec402010-07-29 10:20:49216 }
217
[email protected]909c2402011-05-09 11:39:04218 if (reply_msg_) {
219 // The process failed to launch for some reason.
220 // Don't keep the renderer hanging.
221 reply_msg_->set_reply_error();
222 chrome_render_message_filter_->Send(reply_msg_);
223 }
[email protected]b39c6d92012-01-31 16:38:41224#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36225 if (process_launched_by_broker_) {
226 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06227 }
[email protected]b39c6d92012-01-31 16:38:41228#endif
[email protected]fb1277e82009-11-21 20:32:30229}
230
[email protected]773ebb92011-11-15 19:06:52231// This is called at browser startup.
232// static
233void NaClProcessHost::EarlyStartup() {
234#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
235 // Open the IRT file early to make sure that it isn't replaced out from
236 // under us by autoupdate.
237 NaClBrowser::GetInstance()->EnsureIrtAvailable();
238#endif
[email protected]9addd1c2012-09-15 14:28:24239 CommandLine* cmd = CommandLine::ForCurrentProcess();
240 UMA_HISTOGRAM_BOOLEAN(
241 "NaCl.nacl-gdb",
242 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
243 UMA_HISTOGRAM_BOOLEAN(
244 "NaCl.nacl-gdb-script",
245 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
246 UMA_HISTOGRAM_BOOLEAN(
247 "NaCl.enable-nacl-debug",
248 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04249 NaClBrowser::GetInstance()->SetDebugPatterns(
250 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52251}
252
[email protected]a575da52012-03-22 13:08:36253void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52254 ChromeRenderMessageFilter* chrome_render_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33255 IPC::Message* reply_msg,
256 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36257 chrome_render_message_filter_ = chrome_render_message_filter;
258 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33259 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36260
[email protected]773ebb92011-11-15 19:06:52261 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03262 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06263 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
264 nacl_browser->EnsureAllResourcesAvailable();
265 if (!nacl_browser->IsOk()) {
[email protected]ec7540f2013-01-17 00:00:35266 LOG(ERROR) << "NaCl process launch failed: could not find all the "
267 "resources needed to launch the process";
[email protected]a575da52012-03-22 13:08:36268 delete this;
269 return;
[email protected]773ebb92011-11-15 19:06:52270 }
271
[email protected]c47ec402010-07-29 10:20:49272 // Rather than creating a socket pair in the renderer, and passing
273 // one side through the browser to sel_ldr, socket pairs are created
274 // in the browser and then passed to the renderer and sel_ldr.
275 //
276 // This is mainly for the benefit of Windows, where sockets cannot
277 // be passed in messages, but are copied via DuplicateHandle().
278 // This means the sandboxed renderer cannot send handles to the
279 // browser process.
280
[email protected]6294dd02013-01-09 17:27:23281 nacl::Handle pair[2];
282 // Create a connected socket
283 if (nacl::SocketPair(pair) == -1) {
[email protected]ec7540f2013-01-17 00:00:35284 LOG(ERROR) << "NaCl process launch failed: could not create a socket pair";
[email protected]6294dd02013-01-09 17:27:23285 delete this;
286 return;
[email protected]c47ec402010-07-29 10:20:49287 }
[email protected]6294dd02013-01-09 17:27:23288 internal_->socket_for_renderer = pair[0];
289 internal_->socket_for_sel_ldr = pair[1];
290 SetCloseOnExec(pair[0]);
291 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46292
293 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30294 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36295 delete this;
[email protected]d032f492009-09-29 00:33:46296 }
[email protected]d032f492009-09-29 00:33:46297}
[email protected]646e15552012-04-06 22:01:04298
[email protected]5b974952012-04-05 18:18:23299#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04300void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
301 // Set process handle, if it was not set previously.
302 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40303 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
304 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04305 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40306 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04307 if (base::OpenProcessHandleWithAccess(
308 peer_pid,
309 base::kProcessAccessDuplicateHandle |
310 base::kProcessAccessQueryInformation |
311 base::kProcessAccessWaitForTermination,
312 &process)) {
313 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03314 if (!StartWithLaunchedProcess()) {
315 delete this;
316 return;
317 }
[email protected]646e15552012-04-06 22:01:04318 } else {
[email protected]ec7540f2013-01-17 00:00:35319 LOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04320 }
321 }
[email protected]646e15552012-04-06 22:01:04322}
323#else
324void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
325}
326#endif
327
328#if defined(OS_WIN)
329void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
330 process_launched_by_broker_ = true;
331 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03332 if (!StartWithLaunchedProcess())
333 delete this;
[email protected]646e15552012-04-06 22:01:04334}
335
[email protected]ea6588842012-05-03 05:39:38336void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
337 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
338 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
339 Send(reply);
[email protected]646e15552012-04-06 22:01:04340}
341#endif
342
343// Needed to handle sync messages in OnMessageRecieved.
344bool NaClProcessHost::Send(IPC::Message* msg) {
345 return process_->Send(msg);
346}
347
348#if defined(OS_WIN)
349scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33350 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23351 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46352 CommandLine* cmd_line = new CommandLine(nacl_gdb);
353 // We can't use PrependWrapper because our parameters contain spaces.
354 cmd_line->AppendArg("--eval-command");
355 const FilePath::StringType& irt_path =
356 NaClBrowser::GetInstance()->GetIrtFilePath().value();
357 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23358 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33359 if (!manifest_path.empty()) {
360 cmd_line->AppendArg("--eval-command");
361 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
362 manifest_path.value());
363 }
[email protected]401b90792012-05-30 11:41:13364 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
365 switches::kNaClGdbScript);
366 if (!script.empty()) {
367 cmd_line->AppendArg("--command");
368 cmd_line->AppendArgNative(script.value());
369 }
[email protected]31a665e72012-03-11 12:37:46370 cmd_line->AppendArg("--args");
371 const CommandLine::StringVector& argv = line->argv();
372 for (size_t i = 0; i < argv.size(); i++) {
373 cmd_line->AppendArgNative(argv[i]);
374 }
375 return scoped_ptr<CommandLine>(cmd_line);
376}
[email protected]5b974952012-04-05 18:18:23377#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06378class NaClProcessHost::NaClGdbWatchDelegate
379 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23380 public:
381 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
382 NaClGdbWatchDelegate(int fd_read, int fd_write,
383 const base::Closure& reply)
384 : fd_read_(fd_read),
385 fd_write_(fd_write),
386 reply_(reply) {}
387
[email protected]b94584a2013-02-07 03:02:08388 virtual ~NaClGdbWatchDelegate() {
[email protected]5b974952012-04-05 18:18:23389 if (HANDLE_EINTR(close(fd_read_)) != 0)
390 DLOG(ERROR) << "close(fd_read_) failed";
391 if (HANDLE_EINTR(close(fd_write_)) != 0)
392 DLOG(ERROR) << "close(fd_write_) failed";
393 }
394
395 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
396 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
397
398 private:
399 int fd_read_;
400 int fd_write_;
401 base::Closure reply_;
402};
403
[email protected]512d03f2012-06-26 01:06:06404void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
405 int fd) {
[email protected]5b974952012-04-05 18:18:23406 char buf;
407 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
408 LOG(ERROR) << "Failed to sync with nacl-gdb";
409 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
410}
[email protected]5b974952012-04-05 18:18:23411
412bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
413 CommandLine::StringType nacl_gdb =
414 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
415 switches::kNaClGdb);
416 CommandLine::StringVector argv;
417 // We don't support spaces inside arguments in --nacl-gdb switch.
418 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
419 CommandLine cmd_line(argv);
420 cmd_line.AppendArg("--eval-command");
421 const FilePath::StringType& irt_path =
422 NaClBrowser::GetInstance()->GetIrtFilePath().value();
423 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
424 FilePath manifest_path = GetManifestPath();
425 if (!manifest_path.empty()) {
426 cmd_line.AppendArg("--eval-command");
427 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
428 manifest_path.value());
429 }
430 cmd_line.AppendArg("--eval-command");
431 cmd_line.AppendArg("attach " + base::IntToString(pid));
432 int fds[2];
433 if (pipe(fds) != 0)
434 return false;
435 // Tell the debugger to send a byte to the writable end of the pipe.
436 // We use a file descriptor in our process because the debugger will be
437 // typically launched in a separate terminal, and a lot of terminals close all
438 // file descriptors before launching external programs.
439 cmd_line.AppendArg("--eval-command");
440 cmd_line.AppendArg("dump binary value /proc/" +
441 base::IntToString(base::GetCurrentProcId()) +
442 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]401b90792012-05-30 11:41:13443 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
444 switches::kNaClGdbScript);
445 if (!script.empty()) {
446 cmd_line.AppendArg("--command");
447 cmd_line.AppendArgNative(script.value());
448 }
[email protected]5b974952012-04-05 18:18:23449 // wait on fds[0]
450 // If the debugger crashes before attaching to the NaCl process, the user can
451 // release resources by terminating the NaCl loader in Chrome Task Manager.
452 nacl_gdb_watcher_delegate_.reset(
453 new NaClGdbWatchDelegate(
454 fds[0], fds[1],
455 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
456 weak_factory_.GetWeakPtr())));
457 MessageLoopForIO::current()->WatchFileDescriptor(
458 fds[0],
459 true,
460 MessageLoopForIO::WATCH_READ,
461 &nacl_gdb_watcher_,
462 nacl_gdb_watcher_delegate_.get());
463 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
464}
465
466void NaClProcessHost::OnNaClGdbAttached() {
467 wait_for_nacl_gdb_ = false;
468 nacl_gdb_watcher_.StopWatchingFileDescriptor();
469 nacl_gdb_watcher_delegate_.reset();
470 OnProcessLaunched();
471}
472#endif
473
474FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34475 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16476 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
[email protected]885c0e92012-11-13 20:27:42477 if (extension != NULL &&
478 manifest_url_.SchemeIs(extensions::kExtensionScheme)) {
[email protected]87f35592012-04-08 00:49:16479 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23480 TrimString(path, "/", &path); // Remove first slash
481 return extension->path().AppendASCII(path);
482 }
483 return FilePath();
484}
[email protected]31a665e72012-03-11 12:37:46485
[email protected]fb1277e82009-11-21 20:32:30486bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40487 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35488 if (channel_id.empty()) {
489 LOG(ERROR) << "NaCl process launch failed: could not create channel";
[email protected]d032f492009-09-29 00:33:46490 return false;
[email protected]ec7540f2013-01-17 00:00:35491 }
[email protected]d032f492009-09-29 00:33:46492
[email protected]e3fc75a2011-05-05 08:20:42493 CommandLine::StringType nacl_loader_prefix;
494#if defined(OS_POSIX)
495 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
496 switches::kNaClLoaderCmdPrefix);
497#endif // defined(OS_POSIX)
498
[email protected]d032f492009-09-29 00:33:46499 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36500
501#if defined(OS_MACOSX)
502 // The Native Client process needs to be able to allocate a 1GB contiguous
503 // region to use as the client environment's virtual address space. ASLR
504 // (PIE) interferes with this by making it possible that no gap large enough
505 // to accomodate this request will exist in the child process' address
506 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
507 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49508 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36509#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49510 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
511 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36512#else
[email protected]4cb43102011-12-02 20:24:49513 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36514#endif
515
[email protected]4cb43102011-12-02 20:24:49516 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30517 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46518 return false;
519
[email protected]31a665e72012-03-11 12:37:46520#if defined(OS_WIN)
521 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
522 if (RunningOnWOW64()) {
523 FilePath module_path;
[email protected]ec7540f2013-01-17 00:00:35524 if (!PathService::Get(base::FILE_MODULE, &module_path)) {
525 LOG(ERROR) << "NaCl process launch failed: could not resolve module";
[email protected]31a665e72012-03-11 12:37:46526 return false;
[email protected]ec7540f2013-01-17 00:00:35527 }
[email protected]31a665e72012-03-11 12:37:46528 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
529 }
530#endif
531
[email protected]33a05af2012-03-02 18:15:51532 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
533 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13534
[email protected]05076ba22010-07-30 05:59:57535 cmd_line->AppendSwitchASCII(switches::kProcessType,
536 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44537 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44538 if (logging::DialogsAreSuppressed())
539 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46540
[email protected]e3fc75a2011-05-05 08:20:42541 if (!nacl_loader_prefix.empty())
542 cmd_line->PrependWrapper(nacl_loader_prefix);
543
[email protected]31a665e72012-03-11 12:37:46544 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
545 switches::kNaClGdb);
546 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23547#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46548 cmd_line->AppendSwitch(switches::kNoSandbox);
549 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04550 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46551 // We can't use process_->Launch() because OnProcessLaunched will be called
552 // with process_->GetData().handle filled by handle of gdb process. This
553 // handle will be used to duplicate handles for NaCl process and as
554 // a result NaCl process will not be able to use them.
555 //
556 // So we don't fill process_->GetData().handle and wait for
557 // OnChannelConnected to get handle of NaCl process from its pid. Then we
558 // call OnProcessLaunched.
559 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23560#elif defined(OS_LINUX)
561 wait_for_nacl_gdb_ = true;
562#endif
[email protected]31a665e72012-03-11 12:37:46563 }
564
[email protected]103607e2010-02-01 18:57:09565 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46566#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52567 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35568 if (!NaClBrokerService::GetInstance()->LaunchLoader(
569 weak_factory_.GetWeakPtr(), channel_id)) {
570 LOG(ERROR) << "NaCl process launch failed: broker service did not launch "
571 "process";
572 return false;
573 }
[email protected]4bdde602010-06-16 03:17:35574 } else {
[email protected]33a05af2012-03-02 18:15:51575 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35576 }
[email protected]103607e2010-02-01 18:57:09577#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40578 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20579 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51580 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09581#endif
[email protected]d032f492009-09-29 00:33:46582
[email protected]fb1277e82009-11-21 20:32:30583 return true;
[email protected]d032f492009-09-29 00:33:46584}
585
[email protected]646e15552012-04-06 22:01:04586bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
587 bool handled = true;
588 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
589 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
590 OnQueryKnownToValidate)
591 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
592 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38593#if defined(OS_WIN)
594 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
595 OnAttachDebugExceptionHandler)
596#endif
[email protected]17b5a8172012-06-22 21:09:09597 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
598 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04599 IPC_MESSAGE_UNHANDLED(handled = false)
600 IPC_END_MESSAGE_MAP()
601 return handled;
[email protected]103607e2010-02-01 18:57:09602}
603
[email protected]773ebb92011-11-15 19:06:52604void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03605 if (!StartWithLaunchedProcess())
606 delete this;
[email protected]773ebb92011-11-15 19:06:52607}
608
[email protected]e97990f2012-05-15 22:06:06609// Called when the NaClBrowser singleton has been fully initialized.
610void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52611 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35612 if (!nacl_browser->IsReady()) {
613 LOG(ERROR) << "NaCl process launch failed: could not acquire shared "
614 "resources needed by NaCl";
615 delete this;
616 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43617 delete this;
618 }
619}
620
[email protected]17b5a8172012-06-22 21:09:09621bool NaClProcessHost::ReplyToRenderer(
622 const IPC::ChannelHandle& channel_handle) {
[email protected]6294dd02013-01-09 17:27:23623 nacl::FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49624#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23625 // Copy the handle into the renderer process.
626 HANDLE handle_in_renderer;
627 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
628 reinterpret_cast<HANDLE>(
629 internal_->socket_for_renderer),
630 chrome_render_message_filter_->peer_handle(),
631 &handle_in_renderer,
632 0, // Unused given DUPLICATE_SAME_ACCESS.
633 FALSE,
634 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]ec7540f2013-01-17 00:00:35635 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]6294dd02013-01-09 17:27:23636 return false;
[email protected]c47ec402010-07-29 10:20:49637 }
[email protected]6294dd02013-01-09 17:27:23638 handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>(
639 handle_in_renderer);
640#else
641 // No need to dup the imc_handle - we don't pass it anywhere else so
642 // it cannot be closed.
643 nacl::FileDescriptor imc_handle;
644 imc_handle.fd = internal_->socket_for_renderer;
645 imc_handle.auto_close = true;
646 handle_for_renderer = imc_handle;
647#endif
[email protected]c47ec402010-07-29 10:20:49648
649#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50650 // If we are on 64-bit Windows, the NaCl process's sandbox is
651 // managed by a different process from the renderer's sandbox. We
652 // need to inform the renderer's sandbox about the NaCl process so
653 // that the renderer can send handles to the NaCl process using
654 // BrokerDuplicateHandle().
655 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01656 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]ec7540f2013-01-17 00:00:35657 LOG(ERROR) << "Failed to add NaCl process PID";
[email protected]e4f6eb0232012-04-17 00:47:50658 return false;
659 }
660 }
[email protected]fb1277e82009-11-21 20:32:30661#endif
662
[email protected]108fd342013-01-04 20:46:54663 const ChildProcessData& data = process_->GetData();
[email protected]2ccf45c2011-08-19 23:35:50664 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]6294dd02013-01-09 17:27:23665 reply_msg_, handle_for_renderer,
[email protected]108fd342013-01-04 20:46:54666 channel_handle, base::GetProcId(data.handle), data.id);
[email protected]92d56412011-03-24 20:53:52667 chrome_render_message_filter_->Send(reply_msg_);
668 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30669 reply_msg_ = NULL;
[email protected]6294dd02013-01-09 17:27:23670 internal_->socket_for_renderer = nacl::kInvalidHandle;
[email protected]00d99542012-04-17 22:48:02671 return true;
672}
[email protected]fb1277e82009-11-21 20:32:30673
[email protected]d571c032012-09-14 12:39:18674// TCP port we chose for NaCl debug stub. It can be any other number.
675static const int kDebugStubPort = 4014;
676
677#if defined(OS_POSIX)
678SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]13cad0b2012-12-07 17:57:34679 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
680 SocketDescriptor s;
681 // We allocate currently unused TCP port for debug stub tests. The port
682 // number is passed to the test via debug stub port listener.
683 if (nacl_browser->HasGdbDebugStubPortListener()) {
684 int port;
685 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
686 if (s != net::TCPListenSocket::kInvalidSocket) {
687 nacl_browser->FireGdbDebugStubPortOpened(port);
688 }
689 } else {
690 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
691 }
692 if (s == net::TCPListenSocket::kInvalidSocket) {
693 LOG(ERROR) << "failed to open socket for debug stub";
694 return net::TCPListenSocket::kInvalidSocket;
695 }
[email protected]d571c032012-09-14 12:39:18696 if (listen(s, 1)) {
697 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34698 if (HANDLE_EINTR(close(s)) < 0)
699 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]d571c032012-09-14 12:39:18700 return net::TCPListenSocket::kInvalidSocket;
701 }
702 return s;
703}
704#endif
705
[email protected]00d99542012-04-17 22:48:02706bool NaClProcessHost::StartNaClExecution() {
707 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
708
709 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12710 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
711 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02712 params.version = chrome::VersionInfo().CreateVersionString();
713 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04714 params.enable_debug_stub = enable_debug_stub_ &&
715 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]7522077b2013-01-17 23:39:40716 // Enable PPAPI proxy channel creation only for renderer processes.
717 params.enable_ipc_proxy = enable_ppapi_proxy();
[email protected]2e1e6f02013-01-11 18:22:56718 params.uses_irt = uses_irt_;
[email protected]00d99542012-04-17 22:48:02719
720 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23721 if (!ShareHandleToSelLdr(data.handle,
722 internal_->socket_for_sel_ldr, true,
723 &params.handles)) {
724 return false;
[email protected]773ebb92011-11-15 19:06:52725 }
726
[email protected]2e1e6f02013-01-11 18:22:56727 if (params.uses_irt) {
728 base::PlatformFile irt_file = nacl_browser->IrtFile();
729 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
730
731 // Send over the IRT file handle. We don't close our own copy!
732 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
733 return false;
734 }
[email protected]c47ec402010-07-29 10:20:49735
[email protected]ab88d1542011-11-18 22:52:00736#if defined(OS_MACOSX)
737 // For dynamic loading support, NaCl requires a file descriptor that
738 // was created in /tmp, since those created with shm_open() are not
739 // mappable with PROT_EXEC. Rather than requiring an extra IPC
740 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25741 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31742 base::SharedMemoryCreateOptions options;
743 options.size = 1;
744 options.executable = true;
745 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03746 DLOG(ERROR) << "Failed to allocate memory buffer";
747 return false;
[email protected]2c68bf032010-11-11 23:16:30748 }
[email protected]cbbe7842011-11-17 22:01:25749 nacl::FileDescriptor memory_fd;
750 memory_fd.fd = dup(memory_buffer.handle().fd);
751 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03752 DLOG(ERROR) << "Failed to dup() a file descriptor";
753 return false;
[email protected]cbbe7842011-11-17 22:01:25754 }
755 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02756 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30757#endif
758
[email protected]d571c032012-09-14 12:39:18759#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05760 if (params.enable_debug_stub) {
[email protected]d571c032012-09-14 12:39:18761 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
762 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
763 params.debug_stub_server_bound_socket =
764 nacl::FileDescriptor(server_bound_socket, true);
765 }
766 }
767#endif
768
[email protected]ea6588842012-05-03 05:39:38769 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41770
[email protected]6294dd02013-01-09 17:27:23771 internal_->socket_for_sel_ldr = nacl::kInvalidHandle;
[email protected]09afc2e2012-04-10 17:29:03772 return true;
773}
774
[email protected]00d99542012-04-17 22:48:02775bool NaClProcessHost::SendStart() {
[email protected]7522077b2013-01-17 23:39:40776 if (!enable_ppapi_proxy()) {
[email protected]17b5a8172012-06-22 21:09:09777 if (!ReplyToRenderer(IPC::ChannelHandle()))
778 return false;
779 }
780 return StartNaClExecution();
781}
782
[email protected]8510d282012-08-30 19:47:38783// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
784// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
785// listener.
[email protected]17b5a8172012-06-22 21:09:09786void NaClProcessHost::OnPpapiChannelCreated(
787 const IPC::ChannelHandle& channel_handle) {
[email protected]7522077b2013-01-17 23:39:40788 // Only renderer processes should create a channel.
789 DCHECK(enable_ppapi_proxy());
[email protected]8510d282012-08-30 19:47:38790 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
791 // channel.
792 if (!ipc_proxy_channel_.get()) {
[email protected]9e141212013-01-21 15:21:56793 DCHECK_EQ(content::PROCESS_TYPE_NACL_LOADER, process_->GetData().type);
794
[email protected]8510d282012-08-30 19:47:38795 ipc_proxy_channel_.reset(
796 new IPC::ChannelProxy(channel_handle,
797 IPC::Channel::MODE_CLIENT,
798 &ipc_plugin_listener_,
799 base::MessageLoopProxy::current()));
[email protected]0c7193742012-11-07 19:05:03800 // Create the browser ppapi host and enable PPAPI message dispatching to the
801 // browser process.
[email protected]adf7cd52012-12-04 19:37:46802 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
[email protected]9e141212013-01-21 15:21:56803 ipc_proxy_channel_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03804 permissions_,
805 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55806 ipc_proxy_channel_.get(),
[email protected]e02ff722012-11-06 03:53:06807 chrome_render_message_filter_->GetHostResolver(),
808 chrome_render_message_filter_->render_process_id(),
[email protected]adf7cd52012-12-04 19:37:46809 render_view_id_));
810
[email protected]8510d282012-08-30 19:47:38811 // Send a message to create the NaCl-Renderer channel. The handle is just
812 // a place holder.
813 ipc_proxy_channel_->Send(
814 new PpapiMsg_CreateNaClChannel(
815 chrome_render_message_filter_->render_process_id(),
[email protected]195d4cde2012-10-02 18:12:41816 permissions_,
[email protected]8510d282012-08-30 19:47:38817 chrome_render_message_filter_->off_the_record(),
818 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
819 IPC::InvalidPlatformFileForTransit())));
820 } else if (reply_msg_) {
821 // Otherwise, this must be a renderer channel.
822 ReplyToRenderer(channel_handle);
823 } else {
824 // Attempt to open more than 1 renderer channel is not supported.
825 // Shut down the NaCl process.
826 process_->GetHost()->ForceShutdown();
827 }
828}
829
830bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
831 // Handle messages that have been forwarded from our PluginListener.
832 // These messages come from untrusted code so should be handled with care.
833 bool handled = true;
834 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
835 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
836 OnPpapiChannelCreated)
837 IPC_MESSAGE_UNHANDLED(handled = false)
838 IPC_END_MESSAGE_MAP()
839 return handled;
[email protected]00d99542012-04-17 22:48:02840}
841
[email protected]09afc2e2012-04-10 17:29:03842bool NaClProcessHost::StartWithLaunchedProcess() {
843#if defined(OS_LINUX)
844 if (wait_for_nacl_gdb_) {
845 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
846 // We will be called with wait_for_nacl_gdb_ = false once debugger is
847 // attached to the program.
848 return true;
849 }
850 DLOG(ERROR) << "Failed to launch debugger";
851 // Continue execution without debugger.
852 }
853#endif
854
855 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03856
[email protected]e97990f2012-05-15 22:06:06857 if (nacl_browser->IsReady()) {
858 return SendStart();
859 } else if (nacl_browser->IsOk()) {
860 nacl_browser->WaitForResources(
861 base::Bind(&NaClProcessHost::OnResourcesReady,
862 weak_factory_.GetWeakPtr()));
863 return true;
864 } else {
[email protected]ec7540f2013-01-17 00:00:35865 LOG(ERROR) << "NaCl process failed to launch: previously failed to acquire "
866 "shared resources";
[email protected]e97990f2012-05-15 22:06:06867 return false;
868 }
[email protected]d032f492009-09-29 00:33:46869}
870
[email protected]4a0141b2012-03-27 01:15:30871void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
872 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47873 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52874 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30875}
876
877void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52878 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30879}
[email protected]ea6588842012-05-03 05:39:38880
881#if defined(OS_WIN)
882void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
883 IPC::Message* reply_msg) {
884 if (!AttachDebugExceptionHandler(info, reply_msg)) {
885 // Send failure message.
886 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
887 false);
888 Send(reply_msg);
889 }
890}
891
892bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
893 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32894 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38895 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32896 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38897 return false;
898 }
899 if (debug_exception_handler_requested_) {
900 // The NaCl process should not request this multiple times.
901 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
902 return false;
903 }
904 debug_exception_handler_requested_ = true;
905
906 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
907 base::win::ScopedHandle process_handle;
908 // We cannot use process_->GetData().handle because it does not have
909 // the necessary access rights. We open the new handle here rather
910 // than in the NaCl broker process in case the NaCl loader process
911 // dies before the NaCl broker process receives the message we send.
912 // The debug exception handler uses DebugActiveProcess() to attach,
913 // but this takes a PID. We need to prevent the NaCl loader's PID
914 // from being reused before DebugActiveProcess() is called, and
915 // holding a process handle open achieves this.
916 if (!base::OpenProcessHandleWithAccess(
917 nacl_pid,
918 base::kProcessAccessQueryInformation |
919 base::kProcessAccessSuspendResume |
920 base::kProcessAccessTerminate |
921 base::kProcessAccessVMOperation |
922 base::kProcessAccessVMRead |
923 base::kProcessAccessVMWrite |
[email protected]29172322013-01-10 17:35:16924 base::kProcessAccessDuplicateHandle |
[email protected]ea6588842012-05-03 05:39:38925 base::kProcessAccessWaitForTermination,
926 process_handle.Receive())) {
927 LOG(ERROR) << "Failed to get process handle";
928 return false;
929 }
930
931 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
932 // If the NaCl loader is 64-bit, the process running its debug
933 // exception handler must be 64-bit too, so we use the 64-bit NaCl
934 // broker process for this. Otherwise, on a 32-bit system, we use
935 // the 32-bit browser process to run the debug exception handler.
936 if (RunningOnWOW64()) {
937 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48938 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38939 } else {
940 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48941 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38942 base::MessageLoopProxy::current(),
943 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
944 weak_factory_.GetWeakPtr()));
945 return true;
946 }
947}
948#endif