blob: 04f8e4e94ef044c9dd01c16dd46ad5744b884444 [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]66f409c2012-10-04 20:59:0431#include "chrome/common/extensions/url_pattern.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"
38#include "content/public/browser/child_process_data.h"
[email protected]b4b53d182012-09-12 02:56:5539#include "content/public/browser/pepper_helper.h"
[email protected]4734d0b2011-12-03 07:10:4440#include "content/public/common/child_process_host.h"
[email protected]8510d282012-08-30 19:47:3841#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4642#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5243#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1644#include "net/base/net_util.h"
[email protected]d571c032012-09-14 12:39:1845#include "net/base/tcp_listen_socket.h"
[email protected]8510d282012-08-30 19:47:3846#include "ppapi/proxy/ppapi_messages.h"
[email protected]d032f492009-09-29 00:33:4647
[email protected]d032f492009-09-29 00:33:4648#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2049#include <fcntl.h>
50
[email protected]d032f492009-09-29 00:33:4651#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3552#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2053#include <windows.h>
54
[email protected]b39c6d92012-01-31 16:38:4155#include "base/threading/thread.h"
56#include "base/process_util.h"
[email protected]4c65fb632012-04-27 00:42:2557#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3558#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3859#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5060#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4661#endif
62
[email protected]631bb742011-11-02 11:29:3963using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4064using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4465using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3866using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3967
[email protected]646e15552012-04-06 22:01:0468namespace {
69
70#if defined(OS_WIN)
71bool RunningOnWOW64() {
72 return (base::win::OSInfo::GetInstance()->wow64_status() ==
73 base::win::OSInfo::WOW64_ENABLED);
74}
75#endif
76
[email protected]646e15552012-04-06 22:01:0477void SetCloseOnExec(nacl::Handle fd) {
78#if defined(OS_POSIX)
79 int flags = fcntl(fd, F_GETFD);
80 CHECK_NE(flags, -1);
81 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
82 CHECK_EQ(rc, 0);
83#endif
84}
85
[email protected]00d99542012-04-17 22:48:0286bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0487 base::ProcessHandle processh,
88 nacl::Handle sourceh,
89 bool close_source,
90 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
91#if defined(OS_WIN)
92 HANDLE channel;
93 int flags = DUPLICATE_SAME_ACCESS;
94 if (close_source)
95 flags |= DUPLICATE_CLOSE_SOURCE;
96 if (!DuplicateHandle(GetCurrentProcess(),
97 reinterpret_cast<HANDLE>(sourceh),
98 processh,
99 &channel,
100 0, // Unused given DUPLICATE_SAME_ACCESS.
101 FALSE,
102 flags)) {
[email protected]09afc2e2012-04-10 17:29:03103 DLOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04104 return false;
105 }
106 handles_for_sel_ldr->push_back(
107 reinterpret_cast<nacl::FileDescriptor>(channel));
108#else
109 nacl::FileDescriptor channel;
110 channel.fd = sourceh;
111 channel.auto_close = close_source;
112 handles_for_sel_ldr->push_back(channel);
113#endif
114 return true;
115}
116
[email protected]646e15552012-04-06 22:01:04117} // namespace
118
[email protected]1d8a3d1f2011-02-19 07:11:52119struct NaClProcessHost::NaClInternal {
120 std::vector<nacl::Handle> sockets_for_renderer;
121 std::vector<nacl::Handle> sockets_for_sel_ldr;
122};
123
[email protected]646e15552012-04-06 22:01:04124// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52125
[email protected]8510d282012-08-30 19:47:38126NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
127 : host_(host) {
128}
129
130bool NaClProcessHost::PluginListener::OnMessageReceived(
131 const IPC::Message& msg) {
132 return host_->OnUntrustedMessageForwarded(msg);
133}
134
[email protected]195d4cde2012-10-02 18:12:41135// TODO(brettw) bug 153036 set the pepper permissions up for dev interfaces.
[email protected]57473ebc2012-05-30 07:44:52136NaClProcessHost::NaClProcessHost(const GURL& manifest_url, bool off_the_record)
[email protected]87f35592012-04-08 00:49:16137 : manifest_url_(manifest_url),
[email protected]195d4cde2012-10-02 18:12:41138 permissions_(ppapi::PpapiPermissions::GetForCommandLine(0)),
[email protected]a575da52012-03-22 13:08:36139#if defined(OS_WIN)
140 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23141#elif defined(OS_LINUX)
142 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36143#endif
144 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38145#if defined(OS_WIN)
146 debug_exception_handler_requested_(false),
147#endif
[email protected]1d8a3d1f2011-02-19 07:11:52148 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06149 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52150 enable_exception_handling_(false),
[email protected]2c227ff2012-08-11 00:51:32151 enable_debug_stub_(false),
[email protected]8510d282012-08-30 19:47:38152 off_the_record_(off_the_record),
153 enable_ipc_proxy_(false),
154 ALLOW_THIS_IN_INITIALIZER_LIST(ipc_plugin_listener_(this)) {
[email protected]4967f792012-01-20 22:14:40155 process_.reset(content::BrowserChildProcessHost::Create(
156 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16157
158 // Set the display name so the user knows what plugin the process is running.
159 // We aren't on the UI thread so getting the pref locale for language
160 // formatting isn't possible, so IDN will be lost, but this is probably OK
161 // for this use case.
162 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06163
164 // We allow untrusted hardware exception handling to be enabled via
165 // an env var for consistency with the standalone build of NaCl.
166 if (CommandLine::ForCurrentProcess()->HasSwitch(
167 switches::kEnableNaClExceptionHandling) ||
168 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
169 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06170 }
[email protected]2c227ff2012-08-11 00:51:32171 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
172 switches::kEnableNaClDebug);
[email protected]17b5a8172012-06-22 21:09:09173
174 enable_ipc_proxy_ = CommandLine::ForCurrentProcess()->HasSwitch(
175 switches::kEnableNaClIPCProxy);
[email protected]d032f492009-09-29 00:33:46176}
177
[email protected]fb1277e82009-11-21 20:32:30178NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49179 int exit_code;
[email protected]4967f792012-01-20 22:14:40180 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49181 std::string message =
182 base::StringPrintf("NaCl process exited with status %i (0x%x)",
183 exit_code, exit_code);
184 if (exit_code == 0) {
185 LOG(INFO) << message;
186 } else {
187 LOG(ERROR) << message;
188 }
189
[email protected]1d8a3d1f2011-02-19 07:11:52190 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04191 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03192 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04193 }
[email protected]c47ec402010-07-29 10:20:49194 }
[email protected]1d8a3d1f2011-02-19 07:11:52195 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04196 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03197 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04198 }
[email protected]c47ec402010-07-29 10:20:49199 }
200
[email protected]909c2402011-05-09 11:39:04201 if (reply_msg_) {
202 // The process failed to launch for some reason.
203 // Don't keep the renderer hanging.
204 reply_msg_->set_reply_error();
205 chrome_render_message_filter_->Send(reply_msg_);
206 }
[email protected]b39c6d92012-01-31 16:38:41207#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36208 if (process_launched_by_broker_) {
209 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06210 }
[email protected]b39c6d92012-01-31 16:38:41211#endif
[email protected]fb1277e82009-11-21 20:32:30212}
213
[email protected]773ebb92011-11-15 19:06:52214// This is called at browser startup.
215// static
216void NaClProcessHost::EarlyStartup() {
217#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
218 // Open the IRT file early to make sure that it isn't replaced out from
219 // under us by autoupdate.
220 NaClBrowser::GetInstance()->EnsureIrtAvailable();
221#endif
[email protected]9addd1c2012-09-15 14:28:24222 CommandLine* cmd = CommandLine::ForCurrentProcess();
223 UMA_HISTOGRAM_BOOLEAN(
224 "NaCl.nacl-gdb",
225 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
226 UMA_HISTOGRAM_BOOLEAN(
227 "NaCl.nacl-gdb-script",
228 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
229 UMA_HISTOGRAM_BOOLEAN(
230 "NaCl.enable-nacl-debug",
231 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04232 NaClBrowser::GetInstance()->SetDebugPatterns(
233 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52234}
235
[email protected]a575da52012-03-22 13:08:36236void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52237 ChromeRenderMessageFilter* chrome_render_message_filter,
238 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33239 IPC::Message* reply_msg,
240 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36241 chrome_render_message_filter_ = chrome_render_message_filter;
242 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33243 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36244
[email protected]c47ec402010-07-29 10:20:49245 // Place an arbitrary limit on the number of sockets to limit
246 // exposure in case the renderer is compromised. We can increase
247 // this if necessary.
248 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36249 delete this;
250 return;
[email protected]c47ec402010-07-29 10:20:49251 }
252
[email protected]773ebb92011-11-15 19:06:52253 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03254 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06255 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
256 nacl_browser->EnsureAllResourcesAvailable();
257 if (!nacl_browser->IsOk()) {
258 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]a575da52012-03-22 13:08:36259 delete this;
260 return;
[email protected]773ebb92011-11-15 19:06:52261 }
262
[email protected]c47ec402010-07-29 10:20:49263 // Rather than creating a socket pair in the renderer, and passing
264 // one side through the browser to sel_ldr, socket pairs are created
265 // in the browser and then passed to the renderer and sel_ldr.
266 //
267 // This is mainly for the benefit of Windows, where sockets cannot
268 // be passed in messages, but are copied via DuplicateHandle().
269 // This means the sandboxed renderer cannot send handles to the
270 // browser process.
271
272 for (int i = 0; i < socket_count; i++) {
273 nacl::Handle pair[2];
274 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36275 if (nacl::SocketPair(pair) == -1) {
276 delete this;
277 return;
278 }
[email protected]1d8a3d1f2011-02-19 07:11:52279 internal_->sockets_for_renderer.push_back(pair[0]);
280 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49281 SetCloseOnExec(pair[0]);
282 SetCloseOnExec(pair[1]);
283 }
[email protected]d032f492009-09-29 00:33:46284
285 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30286 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36287 delete this;
[email protected]d032f492009-09-29 00:33:46288 }
[email protected]d032f492009-09-29 00:33:46289}
[email protected]646e15552012-04-06 22:01:04290
[email protected]5b974952012-04-05 18:18:23291#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04292void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
293 // Set process handle, if it was not set previously.
294 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40295 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
296 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04297 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40298 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04299 if (base::OpenProcessHandleWithAccess(
300 peer_pid,
301 base::kProcessAccessDuplicateHandle |
302 base::kProcessAccessQueryInformation |
303 base::kProcessAccessWaitForTermination,
304 &process)) {
305 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03306 if (!StartWithLaunchedProcess()) {
307 delete this;
308 return;
309 }
[email protected]646e15552012-04-06 22:01:04310 } else {
[email protected]09afc2e2012-04-10 17:29:03311 DLOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04312 }
313 }
[email protected]646e15552012-04-06 22:01:04314}
315#else
316void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
317}
318#endif
319
320#if defined(OS_WIN)
321void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
322 process_launched_by_broker_ = true;
323 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03324 if (!StartWithLaunchedProcess())
325 delete this;
[email protected]646e15552012-04-06 22:01:04326}
327
[email protected]ea6588842012-05-03 05:39:38328void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
329 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
330 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
331 Send(reply);
[email protected]646e15552012-04-06 22:01:04332}
333#endif
334
335// Needed to handle sync messages in OnMessageRecieved.
336bool NaClProcessHost::Send(IPC::Message* msg) {
337 return process_->Send(msg);
338}
339
340#if defined(OS_WIN)
341scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33342 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23343 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46344 CommandLine* cmd_line = new CommandLine(nacl_gdb);
345 // We can't use PrependWrapper because our parameters contain spaces.
346 cmd_line->AppendArg("--eval-command");
347 const FilePath::StringType& irt_path =
348 NaClBrowser::GetInstance()->GetIrtFilePath().value();
349 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23350 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33351 if (!manifest_path.empty()) {
352 cmd_line->AppendArg("--eval-command");
353 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
354 manifest_path.value());
355 }
[email protected]401b90792012-05-30 11:41:13356 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
357 switches::kNaClGdbScript);
358 if (!script.empty()) {
359 cmd_line->AppendArg("--command");
360 cmd_line->AppendArgNative(script.value());
361 }
[email protected]31a665e72012-03-11 12:37:46362 cmd_line->AppendArg("--args");
363 const CommandLine::StringVector& argv = line->argv();
364 for (size_t i = 0; i < argv.size(); i++) {
365 cmd_line->AppendArgNative(argv[i]);
366 }
367 return scoped_ptr<CommandLine>(cmd_line);
368}
[email protected]5b974952012-04-05 18:18:23369#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06370class NaClProcessHost::NaClGdbWatchDelegate
371 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23372 public:
373 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
374 NaClGdbWatchDelegate(int fd_read, int fd_write,
375 const base::Closure& reply)
376 : fd_read_(fd_read),
377 fd_write_(fd_write),
378 reply_(reply) {}
379
380 ~NaClGdbWatchDelegate() {
381 if (HANDLE_EINTR(close(fd_read_)) != 0)
382 DLOG(ERROR) << "close(fd_read_) failed";
383 if (HANDLE_EINTR(close(fd_write_)) != 0)
384 DLOG(ERROR) << "close(fd_write_) failed";
385 }
386
387 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
388 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
389
390 private:
391 int fd_read_;
392 int fd_write_;
393 base::Closure reply_;
394};
395
[email protected]512d03f2012-06-26 01:06:06396void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
397 int fd) {
[email protected]5b974952012-04-05 18:18:23398 char buf;
399 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
400 LOG(ERROR) << "Failed to sync with nacl-gdb";
401 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
402}
[email protected]5b974952012-04-05 18:18:23403
404bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
405 CommandLine::StringType nacl_gdb =
406 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
407 switches::kNaClGdb);
408 CommandLine::StringVector argv;
409 // We don't support spaces inside arguments in --nacl-gdb switch.
410 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
411 CommandLine cmd_line(argv);
412 cmd_line.AppendArg("--eval-command");
413 const FilePath::StringType& irt_path =
414 NaClBrowser::GetInstance()->GetIrtFilePath().value();
415 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
416 FilePath manifest_path = GetManifestPath();
417 if (!manifest_path.empty()) {
418 cmd_line.AppendArg("--eval-command");
419 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
420 manifest_path.value());
421 }
422 cmd_line.AppendArg("--eval-command");
423 cmd_line.AppendArg("attach " + base::IntToString(pid));
424 int fds[2];
425 if (pipe(fds) != 0)
426 return false;
427 // Tell the debugger to send a byte to the writable end of the pipe.
428 // We use a file descriptor in our process because the debugger will be
429 // typically launched in a separate terminal, and a lot of terminals close all
430 // file descriptors before launching external programs.
431 cmd_line.AppendArg("--eval-command");
432 cmd_line.AppendArg("dump binary value /proc/" +
433 base::IntToString(base::GetCurrentProcId()) +
434 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]401b90792012-05-30 11:41:13435 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
436 switches::kNaClGdbScript);
437 if (!script.empty()) {
438 cmd_line.AppendArg("--command");
439 cmd_line.AppendArgNative(script.value());
440 }
[email protected]5b974952012-04-05 18:18:23441 // wait on fds[0]
442 // If the debugger crashes before attaching to the NaCl process, the user can
443 // release resources by terminating the NaCl loader in Chrome Task Manager.
444 nacl_gdb_watcher_delegate_.reset(
445 new NaClGdbWatchDelegate(
446 fds[0], fds[1],
447 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
448 weak_factory_.GetWeakPtr())));
449 MessageLoopForIO::current()->WatchFileDescriptor(
450 fds[0],
451 true,
452 MessageLoopForIO::WATCH_READ,
453 &nacl_gdb_watcher_,
454 nacl_gdb_watcher_delegate_.get());
455 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
456}
457
458void NaClProcessHost::OnNaClGdbAttached() {
459 wait_for_nacl_gdb_ = false;
460 nacl_gdb_watcher_.StopWatchingFileDescriptor();
461 nacl_gdb_watcher_delegate_.reset();
462 OnProcessLaunched();
463}
464#endif
465
466FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34467 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16468 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
469 if (extension != NULL && manifest_url_.SchemeIs(chrome::kExtensionScheme)) {
470 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23471 TrimString(path, "/", &path); // Remove first slash
472 return extension->path().AppendASCII(path);
473 }
474 return FilePath();
475}
[email protected]31a665e72012-03-11 12:37:46476
[email protected]fb1277e82009-11-21 20:32:30477bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40478 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44479 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46480 return false;
481
[email protected]e3fc75a2011-05-05 08:20:42482 CommandLine::StringType nacl_loader_prefix;
483#if defined(OS_POSIX)
484 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
485 switches::kNaClLoaderCmdPrefix);
486#endif // defined(OS_POSIX)
487
[email protected]d032f492009-09-29 00:33:46488 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36489
490#if defined(OS_MACOSX)
491 // The Native Client process needs to be able to allocate a 1GB contiguous
492 // region to use as the client environment's virtual address space. ASLR
493 // (PIE) interferes with this by making it possible that no gap large enough
494 // to accomodate this request will exist in the child process' address
495 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
496 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49497 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36498#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49499 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
500 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36501#else
[email protected]4cb43102011-12-02 20:24:49502 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36503#endif
504
[email protected]4cb43102011-12-02 20:24:49505 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30506 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46507 return false;
508
[email protected]31a665e72012-03-11 12:37:46509#if defined(OS_WIN)
510 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
511 if (RunningOnWOW64()) {
512 FilePath module_path;
513 if (!PathService::Get(base::FILE_MODULE, &module_path))
514 return false;
515 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
516 }
517#endif
518
[email protected]33a05af2012-03-02 18:15:51519 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
520 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13521
[email protected]05076ba22010-07-30 05:59:57522 cmd_line->AppendSwitchASCII(switches::kProcessType,
523 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44524 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44525 if (logging::DialogsAreSuppressed())
526 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46527
[email protected]e3fc75a2011-05-05 08:20:42528 if (!nacl_loader_prefix.empty())
529 cmd_line->PrependWrapper(nacl_loader_prefix);
530
[email protected]31a665e72012-03-11 12:37:46531 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
532 switches::kNaClGdb);
533 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23534#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46535 cmd_line->AppendSwitch(switches::kNoSandbox);
536 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04537 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46538 // We can't use process_->Launch() because OnProcessLaunched will be called
539 // with process_->GetData().handle filled by handle of gdb process. This
540 // handle will be used to duplicate handles for NaCl process and as
541 // a result NaCl process will not be able to use them.
542 //
543 // So we don't fill process_->GetData().handle and wait for
544 // OnChannelConnected to get handle of NaCl process from its pid. Then we
545 // call OnProcessLaunched.
546 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23547#elif defined(OS_LINUX)
548 wait_for_nacl_gdb_ = true;
549#endif
[email protected]31a665e72012-03-11 12:37:46550 }
551
[email protected]103607e2010-02-01 18:57:09552 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46553#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52554 if (RunningOnWOW64()) {
[email protected]26b3c002012-04-26 19:38:34555 return NaClBrokerService::GetInstance()->LaunchLoader(
556 weak_factory_.GetWeakPtr(), channel_id);
[email protected]4bdde602010-06-16 03:17:35557 } else {
[email protected]33a05af2012-03-02 18:15:51558 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35559 }
[email protected]103607e2010-02-01 18:57:09560#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40561 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20562 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51563 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09564#endif
[email protected]d032f492009-09-29 00:33:46565
[email protected]fb1277e82009-11-21 20:32:30566 return true;
[email protected]d032f492009-09-29 00:33:46567}
568
[email protected]646e15552012-04-06 22:01:04569bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
570 bool handled = true;
571 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
572 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
573 OnQueryKnownToValidate)
574 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
575 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38576#if defined(OS_WIN)
577 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
578 OnAttachDebugExceptionHandler)
579#endif
[email protected]17b5a8172012-06-22 21:09:09580 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
581 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04582 IPC_MESSAGE_UNHANDLED(handled = false)
583 IPC_END_MESSAGE_MAP()
584 return handled;
[email protected]103607e2010-02-01 18:57:09585}
586
[email protected]773ebb92011-11-15 19:06:52587void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03588 if (!StartWithLaunchedProcess())
589 delete this;
[email protected]773ebb92011-11-15 19:06:52590}
591
[email protected]e97990f2012-05-15 22:06:06592// Called when the NaClBrowser singleton has been fully initialized.
593void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52594 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06595 if (!nacl_browser->IsReady() || !SendStart()) {
[email protected]09afc2e2012-04-10 17:29:03596 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]338466a82011-05-03 04:27:43597 delete this;
598 }
599}
600
[email protected]17b5a8172012-06-22 21:09:09601bool NaClProcessHost::ReplyToRenderer(
602 const IPC::ChannelHandle& channel_handle) {
[email protected]c47ec402010-07-29 10:20:49603 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]1d8a3d1f2011-02-19 07:11:52604 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49605#if defined(OS_WIN)
606 // Copy the handle into the renderer process.
607 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04608 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
609 reinterpret_cast<HANDLE>(
610 internal_->sockets_for_renderer[i]),
611 chrome_render_message_filter_->peer_handle(),
612 &handle_in_renderer,
613 0, // Unused given DUPLICATE_SAME_ACCESS.
614 FALSE,
615 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]09afc2e2012-04-10 17:29:03616 DLOG(ERROR) << "DuplicateHandle() failed";
617 return false;
[email protected]909c2402011-05-09 11:39:04618 }
[email protected]c47ec402010-07-29 10:20:49619 handles_for_renderer.push_back(
620 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
621#else
622 // No need to dup the imc_handle - we don't pass it anywhere else so
623 // it cannot be closed.
624 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52625 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49626 imc_handle.auto_close = true;
627 handles_for_renderer.push_back(imc_handle);
628#endif
629 }
630
631#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50632 // If we are on 64-bit Windows, the NaCl process's sandbox is
633 // managed by a different process from the renderer's sandbox. We
634 // need to inform the renderer's sandbox about the NaCl process so
635 // that the renderer can send handles to the NaCl process using
636 // BrokerDuplicateHandle().
637 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01638 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]e4f6eb0232012-04-17 00:47:50639 DLOG(ERROR) << "Failed to add NaCl process PID";
640 return false;
641 }
642 }
[email protected]fb1277e82009-11-21 20:32:30643#endif
644
[email protected]2ccf45c2011-08-19 23:35:50645 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]17b5a8172012-06-22 21:09:09646 reply_msg_, handles_for_renderer, channel_handle);
[email protected]92d56412011-03-24 20:53:52647 chrome_render_message_filter_->Send(reply_msg_);
648 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30649 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52650 internal_->sockets_for_renderer.clear();
[email protected]00d99542012-04-17 22:48:02651 return true;
652}
[email protected]fb1277e82009-11-21 20:32:30653
[email protected]d571c032012-09-14 12:39:18654// TCP port we chose for NaCl debug stub. It can be any other number.
655static const int kDebugStubPort = 4014;
656
657#if defined(OS_POSIX)
658SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
659 SocketDescriptor s = net::TCPListenSocket::CreateAndBind("127.0.0.1",
660 kDebugStubPort);
661 if (listen(s, 1)) {
662 LOG(ERROR) << "listen() failed on debug stub socket";
663 return net::TCPListenSocket::kInvalidSocket;
664 }
665 return s;
666}
667#endif
668
[email protected]00d99542012-04-17 22:48:02669bool NaClProcessHost::StartNaClExecution() {
670 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
671
672 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12673 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
674 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02675 params.version = chrome::VersionInfo().CreateVersionString();
676 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04677 params.enable_debug_stub = enable_debug_stub_ &&
678 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]17b5a8172012-06-22 21:09:09679 params.enable_ipc_proxy = enable_ipc_proxy_;
[email protected]00d99542012-04-17 22:48:02680
681 base::PlatformFile irt_file = nacl_browser->IrtFile();
682 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
683
684 const ChildProcessData& data = process_->GetData();
[email protected]1d8a3d1f2011-02-19 07:11:52685 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]00d99542012-04-17 22:48:02686 if (!ShareHandleToSelLdr(data.handle,
687 internal_->sockets_for_sel_ldr[i], true,
688 &params.handles)) {
[email protected]09afc2e2012-04-10 17:29:03689 return false;
[email protected]c47ec402010-07-29 10:20:49690 }
[email protected]773ebb92011-11-15 19:06:52691 }
692
693 // Send over the IRT file handle. We don't close our own copy!
[email protected]00d99542012-04-17 22:48:02694 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
[email protected]09afc2e2012-04-10 17:29:03695 return false;
[email protected]c47ec402010-07-29 10:20:49696
[email protected]ab88d1542011-11-18 22:52:00697#if defined(OS_MACOSX)
698 // For dynamic loading support, NaCl requires a file descriptor that
699 // was created in /tmp, since those created with shm_open() are not
700 // mappable with PROT_EXEC. Rather than requiring an extra IPC
701 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25702 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31703 base::SharedMemoryCreateOptions options;
704 options.size = 1;
705 options.executable = true;
706 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03707 DLOG(ERROR) << "Failed to allocate memory buffer";
708 return false;
[email protected]2c68bf032010-11-11 23:16:30709 }
[email protected]cbbe7842011-11-17 22:01:25710 nacl::FileDescriptor memory_fd;
711 memory_fd.fd = dup(memory_buffer.handle().fd);
712 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03713 DLOG(ERROR) << "Failed to dup() a file descriptor";
714 return false;
[email protected]cbbe7842011-11-17 22:01:25715 }
716 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02717 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30718#endif
719
[email protected]d571c032012-09-14 12:39:18720#if defined(OS_POSIX)
721 if (enable_debug_stub_) {
722 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
723 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
724 params.debug_stub_server_bound_socket =
725 nacl::FileDescriptor(server_bound_socket, true);
726 }
727 }
728#endif
729
[email protected]ea6588842012-05-03 05:39:38730 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41731
[email protected]1d8a3d1f2011-02-19 07:11:52732 internal_->sockets_for_sel_ldr.clear();
[email protected]09afc2e2012-04-10 17:29:03733 return true;
734}
735
[email protected]00d99542012-04-17 22:48:02736bool NaClProcessHost::SendStart() {
[email protected]17b5a8172012-06-22 21:09:09737 if (!enable_ipc_proxy_) {
738 if (!ReplyToRenderer(IPC::ChannelHandle()))
739 return false;
740 }
741 return StartNaClExecution();
742}
743
[email protected]8510d282012-08-30 19:47:38744// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
745// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
746// listener.
[email protected]17b5a8172012-06-22 21:09:09747void NaClProcessHost::OnPpapiChannelCreated(
748 const IPC::ChannelHandle& channel_handle) {
749 DCHECK(enable_ipc_proxy_);
[email protected]8510d282012-08-30 19:47:38750 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
751 // channel.
752 if (!ipc_proxy_channel_.get()) {
753 ipc_proxy_channel_.reset(
754 new IPC::ChannelProxy(channel_handle,
755 IPC::Channel::MODE_CLIENT,
756 &ipc_plugin_listener_,
757 base::MessageLoopProxy::current()));
[email protected]b4b53d182012-09-12 02:56:55758 // Enable PPAPI message dispatching to the browser process.
759 content::EnablePepperSupportForChannel(
760 ipc_proxy_channel_.get(),
761 chrome_render_message_filter_->GetHostResolver());
[email protected]8510d282012-08-30 19:47:38762 // Send a message to create the NaCl-Renderer channel. The handle is just
763 // a place holder.
764 ipc_proxy_channel_->Send(
765 new PpapiMsg_CreateNaClChannel(
766 chrome_render_message_filter_->render_process_id(),
[email protected]195d4cde2012-10-02 18:12:41767 permissions_,
[email protected]8510d282012-08-30 19:47:38768 chrome_render_message_filter_->off_the_record(),
769 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
770 IPC::InvalidPlatformFileForTransit())));
771 } else if (reply_msg_) {
772 // Otherwise, this must be a renderer channel.
773 ReplyToRenderer(channel_handle);
774 } else {
775 // Attempt to open more than 1 renderer channel is not supported.
776 // Shut down the NaCl process.
777 process_->GetHost()->ForceShutdown();
778 }
779}
780
781bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
782 // Handle messages that have been forwarded from our PluginListener.
783 // These messages come from untrusted code so should be handled with care.
784 bool handled = true;
785 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
786 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
787 OnPpapiChannelCreated)
788 IPC_MESSAGE_UNHANDLED(handled = false)
789 IPC_END_MESSAGE_MAP()
790 return handled;
[email protected]00d99542012-04-17 22:48:02791}
792
[email protected]09afc2e2012-04-10 17:29:03793bool NaClProcessHost::StartWithLaunchedProcess() {
794#if defined(OS_LINUX)
795 if (wait_for_nacl_gdb_) {
796 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
797 // We will be called with wait_for_nacl_gdb_ = false once debugger is
798 // attached to the program.
799 return true;
800 }
801 DLOG(ERROR) << "Failed to launch debugger";
802 // Continue execution without debugger.
803 }
804#endif
805
806 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03807
[email protected]e97990f2012-05-15 22:06:06808 if (nacl_browser->IsReady()) {
809 return SendStart();
810 } else if (nacl_browser->IsOk()) {
811 nacl_browser->WaitForResources(
812 base::Bind(&NaClProcessHost::OnResourcesReady,
813 weak_factory_.GetWeakPtr()));
814 return true;
815 } else {
816 return false;
817 }
[email protected]d032f492009-09-29 00:33:46818}
819
[email protected]4a0141b2012-03-27 01:15:30820void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
821 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47822 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52823 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30824}
825
826void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52827 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30828}
[email protected]ea6588842012-05-03 05:39:38829
830#if defined(OS_WIN)
831void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
832 IPC::Message* reply_msg) {
833 if (!AttachDebugExceptionHandler(info, reply_msg)) {
834 // Send failure message.
835 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
836 false);
837 Send(reply_msg);
838 }
839}
840
841bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
842 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32843 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38844 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32845 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38846 return false;
847 }
848 if (debug_exception_handler_requested_) {
849 // The NaCl process should not request this multiple times.
850 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
851 return false;
852 }
853 debug_exception_handler_requested_ = true;
854
855 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
856 base::win::ScopedHandle process_handle;
857 // We cannot use process_->GetData().handle because it does not have
858 // the necessary access rights. We open the new handle here rather
859 // than in the NaCl broker process in case the NaCl loader process
860 // dies before the NaCl broker process receives the message we send.
861 // The debug exception handler uses DebugActiveProcess() to attach,
862 // but this takes a PID. We need to prevent the NaCl loader's PID
863 // from being reused before DebugActiveProcess() is called, and
864 // holding a process handle open achieves this.
865 if (!base::OpenProcessHandleWithAccess(
866 nacl_pid,
867 base::kProcessAccessQueryInformation |
868 base::kProcessAccessSuspendResume |
869 base::kProcessAccessTerminate |
870 base::kProcessAccessVMOperation |
871 base::kProcessAccessVMRead |
872 base::kProcessAccessVMWrite |
873 base::kProcessAccessWaitForTermination,
874 process_handle.Receive())) {
875 LOG(ERROR) << "Failed to get process handle";
876 return false;
877 }
878
879 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
880 // If the NaCl loader is 64-bit, the process running its debug
881 // exception handler must be 64-bit too, so we use the 64-bit NaCl
882 // broker process for this. Otherwise, on a 32-bit system, we use
883 // the 32-bit browser process to run the debug exception handler.
884 if (RunningOnWOW64()) {
885 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48886 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38887 } else {
888 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48889 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38890 base::MessageLoopProxy::current(),
891 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
892 weak_factory_.GetWeakPtr()));
893 return true;
894 }
895}
896#endif