blob: 748506c30849384334c14e4fbb5b87653a44889f [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]5b974952012-04-05 18:18:2315#include "base/string_number_conversions.h"
16#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]be1ce6a72010-08-03 14:35:2219#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3720#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2021#include "build/build_config.h"
[email protected]8510d282012-08-30 19:47:3822#include "chrome/browser/browser_process.h"
[email protected]8f42b4d2012-03-24 14:12:3323#include "chrome/browser/extensions/extension_info_map.h"
[email protected]8510d282012-08-30 19:47:3824#include "chrome/browser/io_thread.h"
[email protected]2e5f2ea42012-05-09 21:39:2125#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3326#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4627#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4328#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4629#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0030#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4631#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0932#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4633#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3034#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3335#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4036#include "content/public/browser/browser_child_process_host.h"
37#include "content/public/browser/child_process_data.h"
[email protected]b4b53d182012-09-12 02:56:5538#include "content/public/browser/pepper_helper.h"
[email protected]4734d0b2011-12-03 07:10:4439#include "content/public/common/child_process_host.h"
[email protected]8510d282012-08-30 19:47:3840#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4641#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5242#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1643#include "net/base/net_util.h"
[email protected]d571c032012-09-14 12:39:1844#include "net/base/tcp_listen_socket.h"
[email protected]8510d282012-08-30 19:47:3845#include "ppapi/proxy/ppapi_messages.h"
[email protected]d032f492009-09-29 00:33:4646
[email protected]d032f492009-09-29 00:33:4647#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2048#include <fcntl.h>
49
[email protected]d032f492009-09-29 00:33:4650#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3551#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2052#include <windows.h>
53
[email protected]b39c6d92012-01-31 16:38:4154#include "base/threading/thread.h"
55#include "base/process_util.h"
[email protected]4c65fb632012-04-27 00:42:2556#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3557#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3858#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5059#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4660#endif
61
[email protected]631bb742011-11-02 11:29:3962using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4063using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4464using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3865using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3966
[email protected]646e15552012-04-06 22:01:0467namespace {
68
69#if defined(OS_WIN)
70bool RunningOnWOW64() {
71 return (base::win::OSInfo::GetInstance()->wow64_status() ==
72 base::win::OSInfo::WOW64_ENABLED);
73}
74#endif
75
[email protected]646e15552012-04-06 22:01:0476void SetCloseOnExec(nacl::Handle fd) {
77#if defined(OS_POSIX)
78 int flags = fcntl(fd, F_GETFD);
79 CHECK_NE(flags, -1);
80 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
81 CHECK_EQ(rc, 0);
82#endif
83}
84
[email protected]00d99542012-04-17 22:48:0285bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0486 base::ProcessHandle processh,
87 nacl::Handle sourceh,
88 bool close_source,
89 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
90#if defined(OS_WIN)
91 HANDLE channel;
92 int flags = DUPLICATE_SAME_ACCESS;
93 if (close_source)
94 flags |= DUPLICATE_CLOSE_SOURCE;
95 if (!DuplicateHandle(GetCurrentProcess(),
96 reinterpret_cast<HANDLE>(sourceh),
97 processh,
98 &channel,
99 0, // Unused given DUPLICATE_SAME_ACCESS.
100 FALSE,
101 flags)) {
[email protected]09afc2e2012-04-10 17:29:03102 DLOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04103 return false;
104 }
105 handles_for_sel_ldr->push_back(
106 reinterpret_cast<nacl::FileDescriptor>(channel));
107#else
108 nacl::FileDescriptor channel;
109 channel.fd = sourceh;
110 channel.auto_close = close_source;
111 handles_for_sel_ldr->push_back(channel);
112#endif
113 return true;
114}
115
[email protected]646e15552012-04-06 22:01:04116} // namespace
117
[email protected]1d8a3d1f2011-02-19 07:11:52118struct NaClProcessHost::NaClInternal {
119 std::vector<nacl::Handle> sockets_for_renderer;
120 std::vector<nacl::Handle> sockets_for_sel_ldr;
121};
122
[email protected]646e15552012-04-06 22:01:04123// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52124
[email protected]8510d282012-08-30 19:47:38125NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
126 : host_(host) {
127}
128
129bool NaClProcessHost::PluginListener::OnMessageReceived(
130 const IPC::Message& msg) {
131 return host_->OnUntrustedMessageForwarded(msg);
132}
133
[email protected]57473ebc2012-05-30 07:44:52134NaClProcessHost::NaClProcessHost(const GURL& manifest_url, bool off_the_record)
[email protected]87f35592012-04-08 00:49:16135 : manifest_url_(manifest_url),
[email protected]a575da52012-03-22 13:08:36136#if defined(OS_WIN)
137 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23138#elif defined(OS_LINUX)
139 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36140#endif
141 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38142#if defined(OS_WIN)
143 debug_exception_handler_requested_(false),
144#endif
[email protected]1d8a3d1f2011-02-19 07:11:52145 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06146 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52147 enable_exception_handling_(false),
[email protected]2c227ff2012-08-11 00:51:32148 enable_debug_stub_(false),
[email protected]8510d282012-08-30 19:47:38149 off_the_record_(off_the_record),
150 enable_ipc_proxy_(false),
151 ALLOW_THIS_IN_INITIALIZER_LIST(ipc_plugin_listener_(this)) {
[email protected]4967f792012-01-20 22:14:40152 process_.reset(content::BrowserChildProcessHost::Create(
153 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16154
155 // Set the display name so the user knows what plugin the process is running.
156 // We aren't on the UI thread so getting the pref locale for language
157 // formatting isn't possible, so IDN will be lost, but this is probably OK
158 // for this use case.
159 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06160
161 // We allow untrusted hardware exception handling to be enabled via
162 // an env var for consistency with the standalone build of NaCl.
163 if (CommandLine::ForCurrentProcess()->HasSwitch(
164 switches::kEnableNaClExceptionHandling) ||
165 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
166 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06167 }
[email protected]2c227ff2012-08-11 00:51:32168 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
169 switches::kEnableNaClDebug);
[email protected]17b5a8172012-06-22 21:09:09170
171 enable_ipc_proxy_ = CommandLine::ForCurrentProcess()->HasSwitch(
172 switches::kEnableNaClIPCProxy);
[email protected]d032f492009-09-29 00:33:46173}
174
[email protected]fb1277e82009-11-21 20:32:30175NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49176 int exit_code;
[email protected]4967f792012-01-20 22:14:40177 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49178 std::string message =
179 base::StringPrintf("NaCl process exited with status %i (0x%x)",
180 exit_code, exit_code);
181 if (exit_code == 0) {
182 LOG(INFO) << message;
183 } else {
184 LOG(ERROR) << message;
185 }
186
[email protected]1d8a3d1f2011-02-19 07:11:52187 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04188 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03189 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04190 }
[email protected]c47ec402010-07-29 10:20:49191 }
[email protected]1d8a3d1f2011-02-19 07:11:52192 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04193 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03194 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04195 }
[email protected]c47ec402010-07-29 10:20:49196 }
197
[email protected]909c2402011-05-09 11:39:04198 if (reply_msg_) {
199 // The process failed to launch for some reason.
200 // Don't keep the renderer hanging.
201 reply_msg_->set_reply_error();
202 chrome_render_message_filter_->Send(reply_msg_);
203 }
[email protected]b39c6d92012-01-31 16:38:41204#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36205 if (process_launched_by_broker_) {
206 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06207 }
[email protected]b39c6d92012-01-31 16:38:41208#endif
[email protected]fb1277e82009-11-21 20:32:30209}
210
[email protected]773ebb92011-11-15 19:06:52211// This is called at browser startup.
212// static
213void NaClProcessHost::EarlyStartup() {
214#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
215 // Open the IRT file early to make sure that it isn't replaced out from
216 // under us by autoupdate.
217 NaClBrowser::GetInstance()->EnsureIrtAvailable();
218#endif
[email protected]9addd1c2012-09-15 14:28:24219 CommandLine* cmd = CommandLine::ForCurrentProcess();
220 UMA_HISTOGRAM_BOOLEAN(
221 "NaCl.nacl-gdb",
222 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
223 UMA_HISTOGRAM_BOOLEAN(
224 "NaCl.nacl-gdb-script",
225 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
226 UMA_HISTOGRAM_BOOLEAN(
227 "NaCl.enable-nacl-debug",
228 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]773ebb92011-11-15 19:06:52229}
230
[email protected]a575da52012-03-22 13:08:36231void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52232 ChromeRenderMessageFilter* chrome_render_message_filter,
233 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33234 IPC::Message* reply_msg,
235 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36236 chrome_render_message_filter_ = chrome_render_message_filter;
237 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33238 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36239
[email protected]c47ec402010-07-29 10:20:49240 // Place an arbitrary limit on the number of sockets to limit
241 // exposure in case the renderer is compromised. We can increase
242 // this if necessary.
243 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36244 delete this;
245 return;
[email protected]c47ec402010-07-29 10:20:49246 }
247
[email protected]773ebb92011-11-15 19:06:52248 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03249 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06250 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
251 nacl_browser->EnsureAllResourcesAvailable();
252 if (!nacl_browser->IsOk()) {
253 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]a575da52012-03-22 13:08:36254 delete this;
255 return;
[email protected]773ebb92011-11-15 19:06:52256 }
257
[email protected]c47ec402010-07-29 10:20:49258 // Rather than creating a socket pair in the renderer, and passing
259 // one side through the browser to sel_ldr, socket pairs are created
260 // in the browser and then passed to the renderer and sel_ldr.
261 //
262 // This is mainly for the benefit of Windows, where sockets cannot
263 // be passed in messages, but are copied via DuplicateHandle().
264 // This means the sandboxed renderer cannot send handles to the
265 // browser process.
266
267 for (int i = 0; i < socket_count; i++) {
268 nacl::Handle pair[2];
269 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36270 if (nacl::SocketPair(pair) == -1) {
271 delete this;
272 return;
273 }
[email protected]1d8a3d1f2011-02-19 07:11:52274 internal_->sockets_for_renderer.push_back(pair[0]);
275 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49276 SetCloseOnExec(pair[0]);
277 SetCloseOnExec(pair[1]);
278 }
[email protected]d032f492009-09-29 00:33:46279
280 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30281 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36282 delete this;
[email protected]d032f492009-09-29 00:33:46283 }
[email protected]d032f492009-09-29 00:33:46284}
[email protected]646e15552012-04-06 22:01:04285
[email protected]5b974952012-04-05 18:18:23286#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04287void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
288 // Set process handle, if it was not set previously.
289 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40290 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
291 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04292 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40293 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04294 if (base::OpenProcessHandleWithAccess(
295 peer_pid,
296 base::kProcessAccessDuplicateHandle |
297 base::kProcessAccessQueryInformation |
298 base::kProcessAccessWaitForTermination,
299 &process)) {
300 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03301 if (!StartWithLaunchedProcess()) {
302 delete this;
303 return;
304 }
[email protected]646e15552012-04-06 22:01:04305 } else {
[email protected]09afc2e2012-04-10 17:29:03306 DLOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04307 }
308 }
[email protected]646e15552012-04-06 22:01:04309}
310#else
311void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
312}
313#endif
314
315#if defined(OS_WIN)
316void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
317 process_launched_by_broker_ = true;
318 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03319 if (!StartWithLaunchedProcess())
320 delete this;
[email protected]646e15552012-04-06 22:01:04321}
322
[email protected]ea6588842012-05-03 05:39:38323void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
324 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
325 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
326 Send(reply);
[email protected]646e15552012-04-06 22:01:04327}
328#endif
329
330// Needed to handle sync messages in OnMessageRecieved.
331bool NaClProcessHost::Send(IPC::Message* msg) {
332 return process_->Send(msg);
333}
334
335#if defined(OS_WIN)
336scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33337 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23338 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46339 CommandLine* cmd_line = new CommandLine(nacl_gdb);
340 // We can't use PrependWrapper because our parameters contain spaces.
341 cmd_line->AppendArg("--eval-command");
342 const FilePath::StringType& irt_path =
343 NaClBrowser::GetInstance()->GetIrtFilePath().value();
344 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23345 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33346 if (!manifest_path.empty()) {
347 cmd_line->AppendArg("--eval-command");
348 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
349 manifest_path.value());
350 }
[email protected]401b90792012-05-30 11:41:13351 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
352 switches::kNaClGdbScript);
353 if (!script.empty()) {
354 cmd_line->AppendArg("--command");
355 cmd_line->AppendArgNative(script.value());
356 }
[email protected]31a665e72012-03-11 12:37:46357 cmd_line->AppendArg("--args");
358 const CommandLine::StringVector& argv = line->argv();
359 for (size_t i = 0; i < argv.size(); i++) {
360 cmd_line->AppendArgNative(argv[i]);
361 }
362 return scoped_ptr<CommandLine>(cmd_line);
363}
[email protected]5b974952012-04-05 18:18:23364#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06365class NaClProcessHost::NaClGdbWatchDelegate
366 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23367 public:
368 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
369 NaClGdbWatchDelegate(int fd_read, int fd_write,
370 const base::Closure& reply)
371 : fd_read_(fd_read),
372 fd_write_(fd_write),
373 reply_(reply) {}
374
375 ~NaClGdbWatchDelegate() {
376 if (HANDLE_EINTR(close(fd_read_)) != 0)
377 DLOG(ERROR) << "close(fd_read_) failed";
378 if (HANDLE_EINTR(close(fd_write_)) != 0)
379 DLOG(ERROR) << "close(fd_write_) failed";
380 }
381
382 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
383 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
384
385 private:
386 int fd_read_;
387 int fd_write_;
388 base::Closure reply_;
389};
390
[email protected]512d03f2012-06-26 01:06:06391void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
392 int fd) {
[email protected]5b974952012-04-05 18:18:23393 char buf;
394 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
395 LOG(ERROR) << "Failed to sync with nacl-gdb";
396 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
397}
[email protected]5b974952012-04-05 18:18:23398
399bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
400 CommandLine::StringType nacl_gdb =
401 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
402 switches::kNaClGdb);
403 CommandLine::StringVector argv;
404 // We don't support spaces inside arguments in --nacl-gdb switch.
405 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
406 CommandLine cmd_line(argv);
407 cmd_line.AppendArg("--eval-command");
408 const FilePath::StringType& irt_path =
409 NaClBrowser::GetInstance()->GetIrtFilePath().value();
410 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
411 FilePath manifest_path = GetManifestPath();
412 if (!manifest_path.empty()) {
413 cmd_line.AppendArg("--eval-command");
414 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
415 manifest_path.value());
416 }
417 cmd_line.AppendArg("--eval-command");
418 cmd_line.AppendArg("attach " + base::IntToString(pid));
419 int fds[2];
420 if (pipe(fds) != 0)
421 return false;
422 // Tell the debugger to send a byte to the writable end of the pipe.
423 // We use a file descriptor in our process because the debugger will be
424 // typically launched in a separate terminal, and a lot of terminals close all
425 // file descriptors before launching external programs.
426 cmd_line.AppendArg("--eval-command");
427 cmd_line.AppendArg("dump binary value /proc/" +
428 base::IntToString(base::GetCurrentProcId()) +
429 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]401b90792012-05-30 11:41:13430 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
431 switches::kNaClGdbScript);
432 if (!script.empty()) {
433 cmd_line.AppendArg("--command");
434 cmd_line.AppendArgNative(script.value());
435 }
[email protected]5b974952012-04-05 18:18:23436 // wait on fds[0]
437 // If the debugger crashes before attaching to the NaCl process, the user can
438 // release resources by terminating the NaCl loader in Chrome Task Manager.
439 nacl_gdb_watcher_delegate_.reset(
440 new NaClGdbWatchDelegate(
441 fds[0], fds[1],
442 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
443 weak_factory_.GetWeakPtr())));
444 MessageLoopForIO::current()->WatchFileDescriptor(
445 fds[0],
446 true,
447 MessageLoopForIO::WATCH_READ,
448 &nacl_gdb_watcher_,
449 nacl_gdb_watcher_delegate_.get());
450 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
451}
452
453void NaClProcessHost::OnNaClGdbAttached() {
454 wait_for_nacl_gdb_ = false;
455 nacl_gdb_watcher_.StopWatchingFileDescriptor();
456 nacl_gdb_watcher_delegate_.reset();
457 OnProcessLaunched();
458}
459#endif
460
461FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34462 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16463 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
464 if (extension != NULL && manifest_url_.SchemeIs(chrome::kExtensionScheme)) {
465 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23466 TrimString(path, "/", &path); // Remove first slash
467 return extension->path().AppendASCII(path);
468 }
469 return FilePath();
470}
[email protected]31a665e72012-03-11 12:37:46471
[email protected]fb1277e82009-11-21 20:32:30472bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40473 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44474 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46475 return false;
476
[email protected]e3fc75a2011-05-05 08:20:42477 CommandLine::StringType nacl_loader_prefix;
478#if defined(OS_POSIX)
479 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
480 switches::kNaClLoaderCmdPrefix);
481#endif // defined(OS_POSIX)
482
[email protected]d032f492009-09-29 00:33:46483 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36484
485#if defined(OS_MACOSX)
486 // The Native Client process needs to be able to allocate a 1GB contiguous
487 // region to use as the client environment's virtual address space. ASLR
488 // (PIE) interferes with this by making it possible that no gap large enough
489 // to accomodate this request will exist in the child process' address
490 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
491 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49492 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36493#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49494 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
495 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36496#else
[email protected]4cb43102011-12-02 20:24:49497 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36498#endif
499
[email protected]4cb43102011-12-02 20:24:49500 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30501 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46502 return false;
503
[email protected]31a665e72012-03-11 12:37:46504#if defined(OS_WIN)
505 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
506 if (RunningOnWOW64()) {
507 FilePath module_path;
508 if (!PathService::Get(base::FILE_MODULE, &module_path))
509 return false;
510 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
511 }
512#endif
513
[email protected]33a05af2012-03-02 18:15:51514 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
515 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13516
[email protected]05076ba22010-07-30 05:59:57517 cmd_line->AppendSwitchASCII(switches::kProcessType,
518 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44519 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44520 if (logging::DialogsAreSuppressed())
521 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46522
[email protected]e3fc75a2011-05-05 08:20:42523 if (!nacl_loader_prefix.empty())
524 cmd_line->PrependWrapper(nacl_loader_prefix);
525
[email protected]31a665e72012-03-11 12:37:46526 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
527 switches::kNaClGdb);
528 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23529#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46530 cmd_line->AppendSwitch(switches::kNoSandbox);
531 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04532 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46533 // We can't use process_->Launch() because OnProcessLaunched will be called
534 // with process_->GetData().handle filled by handle of gdb process. This
535 // handle will be used to duplicate handles for NaCl process and as
536 // a result NaCl process will not be able to use them.
537 //
538 // So we don't fill process_->GetData().handle and wait for
539 // OnChannelConnected to get handle of NaCl process from its pid. Then we
540 // call OnProcessLaunched.
541 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23542#elif defined(OS_LINUX)
543 wait_for_nacl_gdb_ = true;
544#endif
[email protected]31a665e72012-03-11 12:37:46545 }
546
[email protected]103607e2010-02-01 18:57:09547 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46548#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52549 if (RunningOnWOW64()) {
[email protected]26b3c002012-04-26 19:38:34550 return NaClBrokerService::GetInstance()->LaunchLoader(
551 weak_factory_.GetWeakPtr(), channel_id);
[email protected]4bdde602010-06-16 03:17:35552 } else {
[email protected]33a05af2012-03-02 18:15:51553 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35554 }
[email protected]103607e2010-02-01 18:57:09555#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40556 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20557 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51558 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09559#endif
[email protected]d032f492009-09-29 00:33:46560
[email protected]fb1277e82009-11-21 20:32:30561 return true;
[email protected]d032f492009-09-29 00:33:46562}
563
[email protected]646e15552012-04-06 22:01:04564bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
565 bool handled = true;
566 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
567 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
568 OnQueryKnownToValidate)
569 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
570 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38571#if defined(OS_WIN)
572 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
573 OnAttachDebugExceptionHandler)
574#endif
[email protected]17b5a8172012-06-22 21:09:09575 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
576 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04577 IPC_MESSAGE_UNHANDLED(handled = false)
578 IPC_END_MESSAGE_MAP()
579 return handled;
[email protected]103607e2010-02-01 18:57:09580}
581
[email protected]773ebb92011-11-15 19:06:52582void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03583 if (!StartWithLaunchedProcess())
584 delete this;
[email protected]773ebb92011-11-15 19:06:52585}
586
[email protected]e97990f2012-05-15 22:06:06587// Called when the NaClBrowser singleton has been fully initialized.
588void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52589 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06590 if (!nacl_browser->IsReady() || !SendStart()) {
[email protected]09afc2e2012-04-10 17:29:03591 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]338466a82011-05-03 04:27:43592 delete this;
593 }
594}
595
[email protected]17b5a8172012-06-22 21:09:09596bool NaClProcessHost::ReplyToRenderer(
597 const IPC::ChannelHandle& channel_handle) {
[email protected]c47ec402010-07-29 10:20:49598 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]1d8a3d1f2011-02-19 07:11:52599 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49600#if defined(OS_WIN)
601 // Copy the handle into the renderer process.
602 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04603 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
604 reinterpret_cast<HANDLE>(
605 internal_->sockets_for_renderer[i]),
606 chrome_render_message_filter_->peer_handle(),
607 &handle_in_renderer,
608 0, // Unused given DUPLICATE_SAME_ACCESS.
609 FALSE,
610 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]09afc2e2012-04-10 17:29:03611 DLOG(ERROR) << "DuplicateHandle() failed";
612 return false;
[email protected]909c2402011-05-09 11:39:04613 }
[email protected]c47ec402010-07-29 10:20:49614 handles_for_renderer.push_back(
615 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
616#else
617 // No need to dup the imc_handle - we don't pass it anywhere else so
618 // it cannot be closed.
619 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52620 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49621 imc_handle.auto_close = true;
622 handles_for_renderer.push_back(imc_handle);
623#endif
624 }
625
626#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50627 // If we are on 64-bit Windows, the NaCl process's sandbox is
628 // managed by a different process from the renderer's sandbox. We
629 // need to inform the renderer's sandbox about the NaCl process so
630 // that the renderer can send handles to the NaCl process using
631 // BrokerDuplicateHandle().
632 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01633 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]e4f6eb0232012-04-17 00:47:50634 DLOG(ERROR) << "Failed to add NaCl process PID";
635 return false;
636 }
637 }
[email protected]fb1277e82009-11-21 20:32:30638#endif
639
[email protected]2ccf45c2011-08-19 23:35:50640 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]17b5a8172012-06-22 21:09:09641 reply_msg_, handles_for_renderer, channel_handle);
[email protected]92d56412011-03-24 20:53:52642 chrome_render_message_filter_->Send(reply_msg_);
643 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30644 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52645 internal_->sockets_for_renderer.clear();
[email protected]00d99542012-04-17 22:48:02646 return true;
647}
[email protected]fb1277e82009-11-21 20:32:30648
[email protected]d571c032012-09-14 12:39:18649// TCP port we chose for NaCl debug stub. It can be any other number.
650static const int kDebugStubPort = 4014;
651
652#if defined(OS_POSIX)
653SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
654 SocketDescriptor s = net::TCPListenSocket::CreateAndBind("127.0.0.1",
655 kDebugStubPort);
656 if (listen(s, 1)) {
657 LOG(ERROR) << "listen() failed on debug stub socket";
658 return net::TCPListenSocket::kInvalidSocket;
659 }
660 return s;
661}
662#endif
663
[email protected]00d99542012-04-17 22:48:02664bool NaClProcessHost::StartNaClExecution() {
665 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
666
667 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12668 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
669 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02670 params.version = chrome::VersionInfo().CreateVersionString();
671 params.enable_exception_handling = enable_exception_handling_;
[email protected]2c227ff2012-08-11 00:51:32672 params.enable_debug_stub = enable_debug_stub_;
[email protected]17b5a8172012-06-22 21:09:09673 params.enable_ipc_proxy = enable_ipc_proxy_;
[email protected]00d99542012-04-17 22:48:02674
675 base::PlatformFile irt_file = nacl_browser->IrtFile();
676 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
677
678 const ChildProcessData& data = process_->GetData();
[email protected]1d8a3d1f2011-02-19 07:11:52679 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]00d99542012-04-17 22:48:02680 if (!ShareHandleToSelLdr(data.handle,
681 internal_->sockets_for_sel_ldr[i], true,
682 &params.handles)) {
[email protected]09afc2e2012-04-10 17:29:03683 return false;
[email protected]c47ec402010-07-29 10:20:49684 }
[email protected]773ebb92011-11-15 19:06:52685 }
686
687 // Send over the IRT file handle. We don't close our own copy!
[email protected]00d99542012-04-17 22:48:02688 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
[email protected]09afc2e2012-04-10 17:29:03689 return false;
[email protected]c47ec402010-07-29 10:20:49690
[email protected]ab88d1542011-11-18 22:52:00691#if defined(OS_MACOSX)
692 // For dynamic loading support, NaCl requires a file descriptor that
693 // was created in /tmp, since those created with shm_open() are not
694 // mappable with PROT_EXEC. Rather than requiring an extra IPC
695 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25696 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31697 base::SharedMemoryCreateOptions options;
698 options.size = 1;
699 options.executable = true;
700 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03701 DLOG(ERROR) << "Failed to allocate memory buffer";
702 return false;
[email protected]2c68bf032010-11-11 23:16:30703 }
[email protected]cbbe7842011-11-17 22:01:25704 nacl::FileDescriptor memory_fd;
705 memory_fd.fd = dup(memory_buffer.handle().fd);
706 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03707 DLOG(ERROR) << "Failed to dup() a file descriptor";
708 return false;
[email protected]cbbe7842011-11-17 22:01:25709 }
710 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02711 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30712#endif
713
[email protected]d571c032012-09-14 12:39:18714#if defined(OS_POSIX)
715 if (enable_debug_stub_) {
716 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
717 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
718 params.debug_stub_server_bound_socket =
719 nacl::FileDescriptor(server_bound_socket, true);
720 }
721 }
722#endif
723
[email protected]ea6588842012-05-03 05:39:38724 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41725
[email protected]1d8a3d1f2011-02-19 07:11:52726 internal_->sockets_for_sel_ldr.clear();
[email protected]09afc2e2012-04-10 17:29:03727 return true;
728}
729
[email protected]00d99542012-04-17 22:48:02730bool NaClProcessHost::SendStart() {
[email protected]17b5a8172012-06-22 21:09:09731 if (!enable_ipc_proxy_) {
732 if (!ReplyToRenderer(IPC::ChannelHandle()))
733 return false;
734 }
735 return StartNaClExecution();
736}
737
[email protected]8510d282012-08-30 19:47:38738// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
739// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
740// listener.
[email protected]17b5a8172012-06-22 21:09:09741void NaClProcessHost::OnPpapiChannelCreated(
742 const IPC::ChannelHandle& channel_handle) {
743 DCHECK(enable_ipc_proxy_);
[email protected]8510d282012-08-30 19:47:38744 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
745 // channel.
746 if (!ipc_proxy_channel_.get()) {
747 ipc_proxy_channel_.reset(
748 new IPC::ChannelProxy(channel_handle,
749 IPC::Channel::MODE_CLIENT,
750 &ipc_plugin_listener_,
751 base::MessageLoopProxy::current()));
[email protected]b4b53d182012-09-12 02:56:55752 // Enable PPAPI message dispatching to the browser process.
753 content::EnablePepperSupportForChannel(
754 ipc_proxy_channel_.get(),
755 chrome_render_message_filter_->GetHostResolver());
[email protected]8510d282012-08-30 19:47:38756 // Send a message to create the NaCl-Renderer channel. The handle is just
757 // a place holder.
758 ipc_proxy_channel_->Send(
759 new PpapiMsg_CreateNaClChannel(
760 chrome_render_message_filter_->render_process_id(),
761 chrome_render_message_filter_->off_the_record(),
762 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
763 IPC::InvalidPlatformFileForTransit())));
764 } else if (reply_msg_) {
765 // Otherwise, this must be a renderer channel.
766 ReplyToRenderer(channel_handle);
767 } else {
768 // Attempt to open more than 1 renderer channel is not supported.
769 // Shut down the NaCl process.
770 process_->GetHost()->ForceShutdown();
771 }
772}
773
774bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
775 // Handle messages that have been forwarded from our PluginListener.
776 // These messages come from untrusted code so should be handled with care.
777 bool handled = true;
778 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
779 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
780 OnPpapiChannelCreated)
781 IPC_MESSAGE_UNHANDLED(handled = false)
782 IPC_END_MESSAGE_MAP()
783 return handled;
[email protected]00d99542012-04-17 22:48:02784}
785
[email protected]09afc2e2012-04-10 17:29:03786bool NaClProcessHost::StartWithLaunchedProcess() {
787#if defined(OS_LINUX)
788 if (wait_for_nacl_gdb_) {
789 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
790 // We will be called with wait_for_nacl_gdb_ = false once debugger is
791 // attached to the program.
792 return true;
793 }
794 DLOG(ERROR) << "Failed to launch debugger";
795 // Continue execution without debugger.
796 }
797#endif
798
799 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03800
[email protected]e97990f2012-05-15 22:06:06801 if (nacl_browser->IsReady()) {
802 return SendStart();
803 } else if (nacl_browser->IsOk()) {
804 nacl_browser->WaitForResources(
805 base::Bind(&NaClProcessHost::OnResourcesReady,
806 weak_factory_.GetWeakPtr()));
807 return true;
808 } else {
809 return false;
810 }
[email protected]d032f492009-09-29 00:33:46811}
812
[email protected]4a0141b2012-03-27 01:15:30813void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
814 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47815 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52816 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30817}
818
819void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52820 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30821}
[email protected]ea6588842012-05-03 05:39:38822
823#if defined(OS_WIN)
824void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
825 IPC::Message* reply_msg) {
826 if (!AttachDebugExceptionHandler(info, reply_msg)) {
827 // Send failure message.
828 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
829 false);
830 Send(reply_msg);
831 }
832}
833
834bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
835 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32836 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38837 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32838 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38839 return false;
840 }
841 if (debug_exception_handler_requested_) {
842 // The NaCl process should not request this multiple times.
843 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
844 return false;
845 }
846 debug_exception_handler_requested_ = true;
847
848 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
849 base::win::ScopedHandle process_handle;
850 // We cannot use process_->GetData().handle because it does not have
851 // the necessary access rights. We open the new handle here rather
852 // than in the NaCl broker process in case the NaCl loader process
853 // dies before the NaCl broker process receives the message we send.
854 // The debug exception handler uses DebugActiveProcess() to attach,
855 // but this takes a PID. We need to prevent the NaCl loader's PID
856 // from being reused before DebugActiveProcess() is called, and
857 // holding a process handle open achieves this.
858 if (!base::OpenProcessHandleWithAccess(
859 nacl_pid,
860 base::kProcessAccessQueryInformation |
861 base::kProcessAccessSuspendResume |
862 base::kProcessAccessTerminate |
863 base::kProcessAccessVMOperation |
864 base::kProcessAccessVMRead |
865 base::kProcessAccessVMWrite |
866 base::kProcessAccessWaitForTermination,
867 process_handle.Receive())) {
868 LOG(ERROR) << "Failed to get process handle";
869 return false;
870 }
871
872 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
873 // If the NaCl loader is 64-bit, the process running its debug
874 // exception handler must be 64-bit too, so we use the 64-bit NaCl
875 // broker process for this. Otherwise, on a 32-bit system, we use
876 // the 32-bit browser process to run the debug exception handler.
877 if (RunningOnWOW64()) {
878 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48879 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38880 } else {
881 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48882 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38883 base::MessageLoopProxy::current(),
884 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
885 weak_factory_.GetWeakPtr()));
886 return true;
887 }
888}
889#endif