blob: 4b529ac461822837acc1c5ce4e96647dfc3e5129 [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_number_conversions.h"
17#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3318#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2819#include "base/stringprintf.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]885c0e92012-11-13 20:27:4241#include "extensions/common/constants.h"
42#include "extensions/common/url_pattern.h"
[email protected]8510d282012-08-30 19:47:3843#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4644#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5245#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1646#include "net/base/net_util.h"
[email protected]d571c032012-09-14 12:39:1847#include "net/base/tcp_listen_socket.h"
[email protected]8510d282012-08-30 19:47:3848#include "ppapi/proxy/ppapi_messages.h"
[email protected]d032f492009-09-29 00:33:4649
[email protected]d032f492009-09-29 00:33:4650#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2051#include <fcntl.h>
52
[email protected]d032f492009-09-29 00:33:4653#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3554#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2055#include <windows.h>
56
[email protected]b39c6d92012-01-31 16:38:4157#include "base/process_util.h"
[email protected]885c0e92012-11-13 20:27:4258#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2559#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3560#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3861#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5062#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4663#endif
64
[email protected]631bb742011-11-02 11:29:3965using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4066using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4467using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3868using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3969
[email protected]646e15552012-04-06 22:01:0470namespace {
71
72#if defined(OS_WIN)
73bool RunningOnWOW64() {
74 return (base::win::OSInfo::GetInstance()->wow64_status() ==
75 base::win::OSInfo::WOW64_ENABLED);
76}
77#endif
78
[email protected]646e15552012-04-06 22:01:0479void SetCloseOnExec(nacl::Handle fd) {
80#if defined(OS_POSIX)
81 int flags = fcntl(fd, F_GETFD);
82 CHECK_NE(flags, -1);
83 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
84 CHECK_EQ(rc, 0);
85#endif
86}
87
[email protected]00d99542012-04-17 22:48:0288bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0489 base::ProcessHandle processh,
90 nacl::Handle sourceh,
91 bool close_source,
92 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
93#if defined(OS_WIN)
94 HANDLE channel;
95 int flags = DUPLICATE_SAME_ACCESS;
96 if (close_source)
97 flags |= DUPLICATE_CLOSE_SOURCE;
98 if (!DuplicateHandle(GetCurrentProcess(),
99 reinterpret_cast<HANDLE>(sourceh),
100 processh,
101 &channel,
102 0, // Unused given DUPLICATE_SAME_ACCESS.
103 FALSE,
104 flags)) {
[email protected]ec7540f2013-01-17 00:00:35105 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04106 return false;
107 }
108 handles_for_sel_ldr->push_back(
109 reinterpret_cast<nacl::FileDescriptor>(channel));
110#else
111 nacl::FileDescriptor channel;
112 channel.fd = sourceh;
113 channel.auto_close = close_source;
114 handles_for_sel_ldr->push_back(channel);
115#endif
116 return true;
117}
118
[email protected]27b04df2012-12-10 23:43:10119ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
120 // Only allow NaCl plugins to request certain permissions. We don't want
121 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
122 // permissions which may expand the surface area of the sandbox.
123 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
124 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
125}
126
[email protected]646e15552012-04-06 22:01:04127} // namespace
128
[email protected]1d8a3d1f2011-02-19 07:11:52129struct NaClProcessHost::NaClInternal {
[email protected]6294dd02013-01-09 17:27:23130 nacl::Handle socket_for_renderer;
131 nacl::Handle socket_for_sel_ldr;
132
133 NaClInternal()
134 : socket_for_renderer(nacl::kInvalidHandle),
135 socket_for_sel_ldr(nacl::kInvalidHandle) { }
[email protected]1d8a3d1f2011-02-19 07:11:52136};
137
[email protected]646e15552012-04-06 22:01:04138// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52139
[email protected]8510d282012-08-30 19:47:38140NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
141 : host_(host) {
142}
143
144bool NaClProcessHost::PluginListener::OnMessageReceived(
145 const IPC::Message& msg) {
146 return host_->OnUntrustedMessageForwarded(msg);
147}
148
[email protected]9da4fed2012-11-01 17:07:55149NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06150 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55151 uint32 permission_bits,
[email protected]2e1e6f02013-01-11 18:22:56152 bool uses_irt,
[email protected]9da4fed2012-11-01 17:07:55153 bool off_the_record)
[email protected]87f35592012-04-08 00:49:16154 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10155 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36156#if defined(OS_WIN)
157 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23158#elif defined(OS_LINUX)
159 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36160#endif
161 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38162#if defined(OS_WIN)
163 debug_exception_handler_requested_(false),
164#endif
[email protected]1d8a3d1f2011-02-19 07:11:52165 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06166 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52167 enable_exception_handling_(false),
[email protected]2c227ff2012-08-11 00:51:32168 enable_debug_stub_(false),
[email protected]2e1e6f02013-01-11 18:22:56169 uses_irt_(uses_irt),
[email protected]8510d282012-08-30 19:47:38170 off_the_record_(off_the_record),
171 enable_ipc_proxy_(false),
[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]17b5a8172012-06-22 21:09:09192
[email protected]45a9c0a2012-11-08 21:00:29193 enable_ipc_proxy_ = !CommandLine::ForCurrentProcess()->HasSwitch(
194 switches::kEnableNaClSRPCProxy);
[email protected]778a40252012-12-04 19:20:44195 // If render_view_id == 0 we do not need PPAPI, so we can skip
196 // PPAPI IPC proxy channel creation, etc.
197 if (!render_view_id_)
198 enable_ipc_proxy_ = false;
[email protected]d032f492009-09-29 00:33:46199}
200
[email protected]fb1277e82009-11-21 20:32:30201NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49202 int exit_code;
[email protected]4967f792012-01-20 22:14:40203 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49204 std::string message =
205 base::StringPrintf("NaCl process exited with status %i (0x%x)",
206 exit_code, exit_code);
207 if (exit_code == 0) {
208 LOG(INFO) << message;
209 } else {
210 LOG(ERROR) << message;
211 }
212
[email protected]6294dd02013-01-09 17:27:23213 if (internal_->socket_for_renderer != nacl::kInvalidHandle) {
214 if (nacl::Close(internal_->socket_for_renderer) != 0) {
[email protected]09afc2e2012-04-10 17:29:03215 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04216 }
[email protected]c47ec402010-07-29 10:20:49217 }
[email protected]6294dd02013-01-09 17:27:23218
219 if (internal_->socket_for_sel_ldr != nacl::kInvalidHandle) {
220 if (nacl::Close(internal_->socket_for_sel_ldr) != 0) {
[email protected]09afc2e2012-04-10 17:29:03221 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04222 }
[email protected]c47ec402010-07-29 10:20:49223 }
224
[email protected]909c2402011-05-09 11:39:04225 if (reply_msg_) {
226 // The process failed to launch for some reason.
227 // Don't keep the renderer hanging.
228 reply_msg_->set_reply_error();
229 chrome_render_message_filter_->Send(reply_msg_);
230 }
[email protected]b39c6d92012-01-31 16:38:41231#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36232 if (process_launched_by_broker_) {
233 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06234 }
[email protected]b39c6d92012-01-31 16:38:41235#endif
[email protected]fb1277e82009-11-21 20:32:30236}
237
[email protected]773ebb92011-11-15 19:06:52238// This is called at browser startup.
239// static
240void NaClProcessHost::EarlyStartup() {
241#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
242 // Open the IRT file early to make sure that it isn't replaced out from
243 // under us by autoupdate.
244 NaClBrowser::GetInstance()->EnsureIrtAvailable();
245#endif
[email protected]9addd1c2012-09-15 14:28:24246 CommandLine* cmd = CommandLine::ForCurrentProcess();
247 UMA_HISTOGRAM_BOOLEAN(
248 "NaCl.nacl-gdb",
249 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
250 UMA_HISTOGRAM_BOOLEAN(
251 "NaCl.nacl-gdb-script",
252 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
253 UMA_HISTOGRAM_BOOLEAN(
254 "NaCl.enable-nacl-debug",
255 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04256 NaClBrowser::GetInstance()->SetDebugPatterns(
257 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52258}
259
[email protected]a575da52012-03-22 13:08:36260void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52261 ChromeRenderMessageFilter* chrome_render_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33262 IPC::Message* reply_msg,
263 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36264 chrome_render_message_filter_ = chrome_render_message_filter;
265 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33266 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36267
[email protected]773ebb92011-11-15 19:06:52268 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03269 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06270 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
271 nacl_browser->EnsureAllResourcesAvailable();
272 if (!nacl_browser->IsOk()) {
[email protected]ec7540f2013-01-17 00:00:35273 LOG(ERROR) << "NaCl process launch failed: could not find all the "
274 "resources needed to launch the process";
[email protected]a575da52012-03-22 13:08:36275 delete this;
276 return;
[email protected]773ebb92011-11-15 19:06:52277 }
278
[email protected]c47ec402010-07-29 10:20:49279 // Rather than creating a socket pair in the renderer, and passing
280 // one side through the browser to sel_ldr, socket pairs are created
281 // in the browser and then passed to the renderer and sel_ldr.
282 //
283 // This is mainly for the benefit of Windows, where sockets cannot
284 // be passed in messages, but are copied via DuplicateHandle().
285 // This means the sandboxed renderer cannot send handles to the
286 // browser process.
287
[email protected]6294dd02013-01-09 17:27:23288 nacl::Handle pair[2];
289 // Create a connected socket
290 if (nacl::SocketPair(pair) == -1) {
[email protected]ec7540f2013-01-17 00:00:35291 LOG(ERROR) << "NaCl process launch failed: could not create a socket pair";
[email protected]6294dd02013-01-09 17:27:23292 delete this;
293 return;
[email protected]c47ec402010-07-29 10:20:49294 }
[email protected]6294dd02013-01-09 17:27:23295 internal_->socket_for_renderer = pair[0];
296 internal_->socket_for_sel_ldr = pair[1];
297 SetCloseOnExec(pair[0]);
298 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46299
300 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30301 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36302 delete this;
[email protected]d032f492009-09-29 00:33:46303 }
[email protected]d032f492009-09-29 00:33:46304}
[email protected]646e15552012-04-06 22:01:04305
[email protected]5b974952012-04-05 18:18:23306#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04307void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
308 // Set process handle, if it was not set previously.
309 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40310 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
311 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04312 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40313 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04314 if (base::OpenProcessHandleWithAccess(
315 peer_pid,
316 base::kProcessAccessDuplicateHandle |
317 base::kProcessAccessQueryInformation |
318 base::kProcessAccessWaitForTermination,
319 &process)) {
320 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03321 if (!StartWithLaunchedProcess()) {
322 delete this;
323 return;
324 }
[email protected]646e15552012-04-06 22:01:04325 } else {
[email protected]ec7540f2013-01-17 00:00:35326 LOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04327 }
328 }
[email protected]646e15552012-04-06 22:01:04329}
330#else
331void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
332}
333#endif
334
335#if defined(OS_WIN)
336void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
337 process_launched_by_broker_ = true;
338 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03339 if (!StartWithLaunchedProcess())
340 delete this;
[email protected]646e15552012-04-06 22:01:04341}
342
[email protected]ea6588842012-05-03 05:39:38343void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
344 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
345 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
346 Send(reply);
[email protected]646e15552012-04-06 22:01:04347}
348#endif
349
350// Needed to handle sync messages in OnMessageRecieved.
351bool NaClProcessHost::Send(IPC::Message* msg) {
352 return process_->Send(msg);
353}
354
355#if defined(OS_WIN)
356scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33357 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23358 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46359 CommandLine* cmd_line = new CommandLine(nacl_gdb);
360 // We can't use PrependWrapper because our parameters contain spaces.
361 cmd_line->AppendArg("--eval-command");
362 const FilePath::StringType& irt_path =
363 NaClBrowser::GetInstance()->GetIrtFilePath().value();
364 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23365 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33366 if (!manifest_path.empty()) {
367 cmd_line->AppendArg("--eval-command");
368 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
369 manifest_path.value());
370 }
[email protected]401b90792012-05-30 11:41:13371 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
372 switches::kNaClGdbScript);
373 if (!script.empty()) {
374 cmd_line->AppendArg("--command");
375 cmd_line->AppendArgNative(script.value());
376 }
[email protected]31a665e72012-03-11 12:37:46377 cmd_line->AppendArg("--args");
378 const CommandLine::StringVector& argv = line->argv();
379 for (size_t i = 0; i < argv.size(); i++) {
380 cmd_line->AppendArgNative(argv[i]);
381 }
382 return scoped_ptr<CommandLine>(cmd_line);
383}
[email protected]5b974952012-04-05 18:18:23384#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06385class NaClProcessHost::NaClGdbWatchDelegate
386 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23387 public:
388 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
389 NaClGdbWatchDelegate(int fd_read, int fd_write,
390 const base::Closure& reply)
391 : fd_read_(fd_read),
392 fd_write_(fd_write),
393 reply_(reply) {}
394
395 ~NaClGdbWatchDelegate() {
396 if (HANDLE_EINTR(close(fd_read_)) != 0)
397 DLOG(ERROR) << "close(fd_read_) failed";
398 if (HANDLE_EINTR(close(fd_write_)) != 0)
399 DLOG(ERROR) << "close(fd_write_) failed";
400 }
401
402 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
403 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
404
405 private:
406 int fd_read_;
407 int fd_write_;
408 base::Closure reply_;
409};
410
[email protected]512d03f2012-06-26 01:06:06411void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
412 int fd) {
[email protected]5b974952012-04-05 18:18:23413 char buf;
414 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
415 LOG(ERROR) << "Failed to sync with nacl-gdb";
416 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
417}
[email protected]5b974952012-04-05 18:18:23418
419bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
420 CommandLine::StringType nacl_gdb =
421 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
422 switches::kNaClGdb);
423 CommandLine::StringVector argv;
424 // We don't support spaces inside arguments in --nacl-gdb switch.
425 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
426 CommandLine cmd_line(argv);
427 cmd_line.AppendArg("--eval-command");
428 const FilePath::StringType& irt_path =
429 NaClBrowser::GetInstance()->GetIrtFilePath().value();
430 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
431 FilePath manifest_path = GetManifestPath();
432 if (!manifest_path.empty()) {
433 cmd_line.AppendArg("--eval-command");
434 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
435 manifest_path.value());
436 }
437 cmd_line.AppendArg("--eval-command");
438 cmd_line.AppendArg("attach " + base::IntToString(pid));
439 int fds[2];
440 if (pipe(fds) != 0)
441 return false;
442 // Tell the debugger to send a byte to the writable end of the pipe.
443 // We use a file descriptor in our process because the debugger will be
444 // typically launched in a separate terminal, and a lot of terminals close all
445 // file descriptors before launching external programs.
446 cmd_line.AppendArg("--eval-command");
447 cmd_line.AppendArg("dump binary value /proc/" +
448 base::IntToString(base::GetCurrentProcId()) +
449 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]401b90792012-05-30 11:41:13450 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
451 switches::kNaClGdbScript);
452 if (!script.empty()) {
453 cmd_line.AppendArg("--command");
454 cmd_line.AppendArgNative(script.value());
455 }
[email protected]5b974952012-04-05 18:18:23456 // wait on fds[0]
457 // If the debugger crashes before attaching to the NaCl process, the user can
458 // release resources by terminating the NaCl loader in Chrome Task Manager.
459 nacl_gdb_watcher_delegate_.reset(
460 new NaClGdbWatchDelegate(
461 fds[0], fds[1],
462 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
463 weak_factory_.GetWeakPtr())));
464 MessageLoopForIO::current()->WatchFileDescriptor(
465 fds[0],
466 true,
467 MessageLoopForIO::WATCH_READ,
468 &nacl_gdb_watcher_,
469 nacl_gdb_watcher_delegate_.get());
470 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
471}
472
473void NaClProcessHost::OnNaClGdbAttached() {
474 wait_for_nacl_gdb_ = false;
475 nacl_gdb_watcher_.StopWatchingFileDescriptor();
476 nacl_gdb_watcher_delegate_.reset();
477 OnProcessLaunched();
478}
479#endif
480
481FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34482 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16483 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
[email protected]885c0e92012-11-13 20:27:42484 if (extension != NULL &&
485 manifest_url_.SchemeIs(extensions::kExtensionScheme)) {
[email protected]87f35592012-04-08 00:49:16486 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23487 TrimString(path, "/", &path); // Remove first slash
488 return extension->path().AppendASCII(path);
489 }
490 return FilePath();
491}
[email protected]31a665e72012-03-11 12:37:46492
[email protected]fb1277e82009-11-21 20:32:30493bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40494 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35495 if (channel_id.empty()) {
496 LOG(ERROR) << "NaCl process launch failed: could not create channel";
[email protected]d032f492009-09-29 00:33:46497 return false;
[email protected]ec7540f2013-01-17 00:00:35498 }
[email protected]d032f492009-09-29 00:33:46499
[email protected]e3fc75a2011-05-05 08:20:42500 CommandLine::StringType nacl_loader_prefix;
501#if defined(OS_POSIX)
502 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
503 switches::kNaClLoaderCmdPrefix);
504#endif // defined(OS_POSIX)
505
[email protected]d032f492009-09-29 00:33:46506 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36507
508#if defined(OS_MACOSX)
509 // The Native Client process needs to be able to allocate a 1GB contiguous
510 // region to use as the client environment's virtual address space. ASLR
511 // (PIE) interferes with this by making it possible that no gap large enough
512 // to accomodate this request will exist in the child process' address
513 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
514 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49515 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36516#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49517 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
518 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36519#else
[email protected]4cb43102011-12-02 20:24:49520 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36521#endif
522
[email protected]4cb43102011-12-02 20:24:49523 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30524 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46525 return false;
526
[email protected]31a665e72012-03-11 12:37:46527#if defined(OS_WIN)
528 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
529 if (RunningOnWOW64()) {
530 FilePath module_path;
[email protected]ec7540f2013-01-17 00:00:35531 if (!PathService::Get(base::FILE_MODULE, &module_path)) {
532 LOG(ERROR) << "NaCl process launch failed: could not resolve module";
[email protected]31a665e72012-03-11 12:37:46533 return false;
[email protected]ec7540f2013-01-17 00:00:35534 }
[email protected]31a665e72012-03-11 12:37:46535 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
536 }
537#endif
538
[email protected]33a05af2012-03-02 18:15:51539 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
540 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13541
[email protected]05076ba22010-07-30 05:59:57542 cmd_line->AppendSwitchASCII(switches::kProcessType,
543 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44544 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44545 if (logging::DialogsAreSuppressed())
546 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46547
[email protected]e3fc75a2011-05-05 08:20:42548 if (!nacl_loader_prefix.empty())
549 cmd_line->PrependWrapper(nacl_loader_prefix);
550
[email protected]31a665e72012-03-11 12:37:46551 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
552 switches::kNaClGdb);
553 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23554#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46555 cmd_line->AppendSwitch(switches::kNoSandbox);
556 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04557 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46558 // We can't use process_->Launch() because OnProcessLaunched will be called
559 // with process_->GetData().handle filled by handle of gdb process. This
560 // handle will be used to duplicate handles for NaCl process and as
561 // a result NaCl process will not be able to use them.
562 //
563 // So we don't fill process_->GetData().handle and wait for
564 // OnChannelConnected to get handle of NaCl process from its pid. Then we
565 // call OnProcessLaunched.
566 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23567#elif defined(OS_LINUX)
568 wait_for_nacl_gdb_ = true;
569#endif
[email protected]31a665e72012-03-11 12:37:46570 }
571
[email protected]103607e2010-02-01 18:57:09572 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46573#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52574 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35575 if (!NaClBrokerService::GetInstance()->LaunchLoader(
576 weak_factory_.GetWeakPtr(), channel_id)) {
577 LOG(ERROR) << "NaCl process launch failed: broker service did not launch "
578 "process";
579 return false;
580 }
[email protected]4bdde602010-06-16 03:17:35581 } else {
[email protected]33a05af2012-03-02 18:15:51582 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35583 }
[email protected]103607e2010-02-01 18:57:09584#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40585 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20586 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51587 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09588#endif
[email protected]d032f492009-09-29 00:33:46589
[email protected]fb1277e82009-11-21 20:32:30590 return true;
[email protected]d032f492009-09-29 00:33:46591}
592
[email protected]646e15552012-04-06 22:01:04593bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
594 bool handled = true;
595 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
596 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
597 OnQueryKnownToValidate)
598 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
599 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38600#if defined(OS_WIN)
601 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
602 OnAttachDebugExceptionHandler)
603#endif
[email protected]17b5a8172012-06-22 21:09:09604 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
605 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04606 IPC_MESSAGE_UNHANDLED(handled = false)
607 IPC_END_MESSAGE_MAP()
608 return handled;
[email protected]103607e2010-02-01 18:57:09609}
610
[email protected]773ebb92011-11-15 19:06:52611void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03612 if (!StartWithLaunchedProcess())
613 delete this;
[email protected]773ebb92011-11-15 19:06:52614}
615
[email protected]e97990f2012-05-15 22:06:06616// Called when the NaClBrowser singleton has been fully initialized.
617void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52618 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35619 if (!nacl_browser->IsReady()) {
620 LOG(ERROR) << "NaCl process launch failed: could not acquire shared "
621 "resources needed by NaCl";
622 delete this;
623 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43624 delete this;
625 }
626}
627
[email protected]17b5a8172012-06-22 21:09:09628bool NaClProcessHost::ReplyToRenderer(
629 const IPC::ChannelHandle& channel_handle) {
[email protected]6294dd02013-01-09 17:27:23630 nacl::FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49631#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23632 // Copy the handle into the renderer process.
633 HANDLE handle_in_renderer;
634 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
635 reinterpret_cast<HANDLE>(
636 internal_->socket_for_renderer),
637 chrome_render_message_filter_->peer_handle(),
638 &handle_in_renderer,
639 0, // Unused given DUPLICATE_SAME_ACCESS.
640 FALSE,
641 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]ec7540f2013-01-17 00:00:35642 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]6294dd02013-01-09 17:27:23643 return false;
[email protected]c47ec402010-07-29 10:20:49644 }
[email protected]6294dd02013-01-09 17:27:23645 handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>(
646 handle_in_renderer);
647#else
648 // No need to dup the imc_handle - we don't pass it anywhere else so
649 // it cannot be closed.
650 nacl::FileDescriptor imc_handle;
651 imc_handle.fd = internal_->socket_for_renderer;
652 imc_handle.auto_close = true;
653 handle_for_renderer = imc_handle;
654#endif
[email protected]c47ec402010-07-29 10:20:49655
656#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50657 // If we are on 64-bit Windows, the NaCl process's sandbox is
658 // managed by a different process from the renderer's sandbox. We
659 // need to inform the renderer's sandbox about the NaCl process so
660 // that the renderer can send handles to the NaCl process using
661 // BrokerDuplicateHandle().
662 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01663 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]ec7540f2013-01-17 00:00:35664 LOG(ERROR) << "Failed to add NaCl process PID";
[email protected]e4f6eb0232012-04-17 00:47:50665 return false;
666 }
667 }
[email protected]fb1277e82009-11-21 20:32:30668#endif
669
[email protected]108fd342013-01-04 20:46:54670 const ChildProcessData& data = process_->GetData();
[email protected]2ccf45c2011-08-19 23:35:50671 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]6294dd02013-01-09 17:27:23672 reply_msg_, handle_for_renderer,
[email protected]108fd342013-01-04 20:46:54673 channel_handle, base::GetProcId(data.handle), data.id);
[email protected]92d56412011-03-24 20:53:52674 chrome_render_message_filter_->Send(reply_msg_);
675 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30676 reply_msg_ = NULL;
[email protected]6294dd02013-01-09 17:27:23677 internal_->socket_for_renderer = nacl::kInvalidHandle;
[email protected]00d99542012-04-17 22:48:02678 return true;
679}
[email protected]fb1277e82009-11-21 20:32:30680
[email protected]d571c032012-09-14 12:39:18681// TCP port we chose for NaCl debug stub. It can be any other number.
682static const int kDebugStubPort = 4014;
683
684#if defined(OS_POSIX)
685SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]13cad0b2012-12-07 17:57:34686 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
687 SocketDescriptor s;
688 // We allocate currently unused TCP port for debug stub tests. The port
689 // number is passed to the test via debug stub port listener.
690 if (nacl_browser->HasGdbDebugStubPortListener()) {
691 int port;
692 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
693 if (s != net::TCPListenSocket::kInvalidSocket) {
694 nacl_browser->FireGdbDebugStubPortOpened(port);
695 }
696 } else {
697 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
698 }
699 if (s == net::TCPListenSocket::kInvalidSocket) {
700 LOG(ERROR) << "failed to open socket for debug stub";
701 return net::TCPListenSocket::kInvalidSocket;
702 }
[email protected]d571c032012-09-14 12:39:18703 if (listen(s, 1)) {
704 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34705 if (HANDLE_EINTR(close(s)) < 0)
706 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]d571c032012-09-14 12:39:18707 return net::TCPListenSocket::kInvalidSocket;
708 }
709 return s;
710}
711#endif
712
[email protected]00d99542012-04-17 22:48:02713bool NaClProcessHost::StartNaClExecution() {
714 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
715
716 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12717 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
718 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02719 params.version = chrome::VersionInfo().CreateVersionString();
720 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04721 params.enable_debug_stub = enable_debug_stub_ &&
722 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]17b5a8172012-06-22 21:09:09723 params.enable_ipc_proxy = enable_ipc_proxy_;
[email protected]2e1e6f02013-01-11 18:22:56724 params.uses_irt = uses_irt_;
[email protected]00d99542012-04-17 22:48:02725
726 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23727 if (!ShareHandleToSelLdr(data.handle,
728 internal_->socket_for_sel_ldr, true,
729 &params.handles)) {
730 return false;
[email protected]773ebb92011-11-15 19:06:52731 }
732
[email protected]2e1e6f02013-01-11 18:22:56733 if (params.uses_irt) {
734 base::PlatformFile irt_file = nacl_browser->IrtFile();
735 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
736
737 // Send over the IRT file handle. We don't close our own copy!
738 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
739 return false;
740 }
[email protected]c47ec402010-07-29 10:20:49741
[email protected]ab88d1542011-11-18 22:52:00742#if defined(OS_MACOSX)
743 // For dynamic loading support, NaCl requires a file descriptor that
744 // was created in /tmp, since those created with shm_open() are not
745 // mappable with PROT_EXEC. Rather than requiring an extra IPC
746 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25747 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31748 base::SharedMemoryCreateOptions options;
749 options.size = 1;
750 options.executable = true;
751 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03752 DLOG(ERROR) << "Failed to allocate memory buffer";
753 return false;
[email protected]2c68bf032010-11-11 23:16:30754 }
[email protected]cbbe7842011-11-17 22:01:25755 nacl::FileDescriptor memory_fd;
756 memory_fd.fd = dup(memory_buffer.handle().fd);
757 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03758 DLOG(ERROR) << "Failed to dup() a file descriptor";
759 return false;
[email protected]cbbe7842011-11-17 22:01:25760 }
761 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02762 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30763#endif
764
[email protected]d571c032012-09-14 12:39:18765#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05766 if (params.enable_debug_stub) {
[email protected]d571c032012-09-14 12:39:18767 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
768 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
769 params.debug_stub_server_bound_socket =
770 nacl::FileDescriptor(server_bound_socket, true);
771 }
772 }
773#endif
774
[email protected]ea6588842012-05-03 05:39:38775 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41776
[email protected]6294dd02013-01-09 17:27:23777 internal_->socket_for_sel_ldr = nacl::kInvalidHandle;
[email protected]09afc2e2012-04-10 17:29:03778 return true;
779}
780
[email protected]00d99542012-04-17 22:48:02781bool NaClProcessHost::SendStart() {
[email protected]17b5a8172012-06-22 21:09:09782 if (!enable_ipc_proxy_) {
783 if (!ReplyToRenderer(IPC::ChannelHandle()))
784 return false;
785 }
786 return StartNaClExecution();
787}
788
[email protected]8510d282012-08-30 19:47:38789// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
790// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
791// listener.
[email protected]17b5a8172012-06-22 21:09:09792void NaClProcessHost::OnPpapiChannelCreated(
793 const IPC::ChannelHandle& channel_handle) {
794 DCHECK(enable_ipc_proxy_);
[email protected]8510d282012-08-30 19:47:38795 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
796 // channel.
797 if (!ipc_proxy_channel_.get()) {
798 ipc_proxy_channel_.reset(
799 new IPC::ChannelProxy(channel_handle,
800 IPC::Channel::MODE_CLIENT,
801 &ipc_plugin_listener_,
802 base::MessageLoopProxy::current()));
[email protected]0c7193742012-11-07 19:05:03803 // Create the browser ppapi host and enable PPAPI message dispatching to the
804 // browser process.
[email protected]adf7cd52012-12-04 19:37:46805 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
806 ipc_proxy_channel_.get(), //process_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03807 permissions_,
808 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55809 ipc_proxy_channel_.get(),
[email protected]e02ff722012-11-06 03:53:06810 chrome_render_message_filter_->GetHostResolver(),
811 chrome_render_message_filter_->render_process_id(),
[email protected]adf7cd52012-12-04 19:37:46812 render_view_id_));
813
[email protected]8510d282012-08-30 19:47:38814 // Send a message to create the NaCl-Renderer channel. The handle is just
815 // a place holder.
816 ipc_proxy_channel_->Send(
817 new PpapiMsg_CreateNaClChannel(
818 chrome_render_message_filter_->render_process_id(),
[email protected]195d4cde2012-10-02 18:12:41819 permissions_,
[email protected]8510d282012-08-30 19:47:38820 chrome_render_message_filter_->off_the_record(),
821 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
822 IPC::InvalidPlatformFileForTransit())));
823 } else if (reply_msg_) {
824 // Otherwise, this must be a renderer channel.
825 ReplyToRenderer(channel_handle);
826 } else {
827 // Attempt to open more than 1 renderer channel is not supported.
828 // Shut down the NaCl process.
829 process_->GetHost()->ForceShutdown();
830 }
831}
832
833bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
834 // Handle messages that have been forwarded from our PluginListener.
835 // These messages come from untrusted code so should be handled with care.
836 bool handled = true;
837 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
838 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
839 OnPpapiChannelCreated)
840 IPC_MESSAGE_UNHANDLED(handled = false)
841 IPC_END_MESSAGE_MAP()
842 return handled;
[email protected]00d99542012-04-17 22:48:02843}
844
[email protected]09afc2e2012-04-10 17:29:03845bool NaClProcessHost::StartWithLaunchedProcess() {
846#if defined(OS_LINUX)
847 if (wait_for_nacl_gdb_) {
848 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
849 // We will be called with wait_for_nacl_gdb_ = false once debugger is
850 // attached to the program.
851 return true;
852 }
853 DLOG(ERROR) << "Failed to launch debugger";
854 // Continue execution without debugger.
855 }
856#endif
857
858 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03859
[email protected]e97990f2012-05-15 22:06:06860 if (nacl_browser->IsReady()) {
861 return SendStart();
862 } else if (nacl_browser->IsOk()) {
863 nacl_browser->WaitForResources(
864 base::Bind(&NaClProcessHost::OnResourcesReady,
865 weak_factory_.GetWeakPtr()));
866 return true;
867 } else {
[email protected]ec7540f2013-01-17 00:00:35868 LOG(ERROR) << "NaCl process failed to launch: previously failed to acquire "
869 "shared resources";
[email protected]e97990f2012-05-15 22:06:06870 return false;
871 }
[email protected]d032f492009-09-29 00:33:46872}
873
[email protected]4a0141b2012-03-27 01:15:30874void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
875 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47876 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52877 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30878}
879
880void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52881 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30882}
[email protected]ea6588842012-05-03 05:39:38883
884#if defined(OS_WIN)
885void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
886 IPC::Message* reply_msg) {
887 if (!AttachDebugExceptionHandler(info, reply_msg)) {
888 // Send failure message.
889 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
890 false);
891 Send(reply_msg);
892 }
893}
894
895bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
896 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32897 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38898 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32899 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38900 return false;
901 }
902 if (debug_exception_handler_requested_) {
903 // The NaCl process should not request this multiple times.
904 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
905 return false;
906 }
907 debug_exception_handler_requested_ = true;
908
909 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
910 base::win::ScopedHandle process_handle;
911 // We cannot use process_->GetData().handle because it does not have
912 // the necessary access rights. We open the new handle here rather
913 // than in the NaCl broker process in case the NaCl loader process
914 // dies before the NaCl broker process receives the message we send.
915 // The debug exception handler uses DebugActiveProcess() to attach,
916 // but this takes a PID. We need to prevent the NaCl loader's PID
917 // from being reused before DebugActiveProcess() is called, and
918 // holding a process handle open achieves this.
919 if (!base::OpenProcessHandleWithAccess(
920 nacl_pid,
921 base::kProcessAccessQueryInformation |
922 base::kProcessAccessSuspendResume |
923 base::kProcessAccessTerminate |
924 base::kProcessAccessVMOperation |
925 base::kProcessAccessVMRead |
926 base::kProcessAccessVMWrite |
[email protected]29172322013-01-10 17:35:16927 base::kProcessAccessDuplicateHandle |
[email protected]ea6588842012-05-03 05:39:38928 base::kProcessAccessWaitForTermination,
929 process_handle.Receive())) {
930 LOG(ERROR) << "Failed to get process handle";
931 return false;
932 }
933
934 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
935 // If the NaCl loader is 64-bit, the process running its debug
936 // exception handler must be 64-bit too, so we use the 64-bit NaCl
937 // broker process for this. Otherwise, on a 32-bit system, we use
938 // the 32-bit browser process to run the debug exception handler.
939 if (RunningOnWOW64()) {
940 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48941 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38942 } else {
943 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48944 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38945 base::MessageLoopProxy::current(),
946 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
947 weak_factory_.GetWeakPtr()));
948 return true;
949 }
950}
951#endif