blob: 3119c1b1dd9b1bce1b6c89aeabf41b6821d4f005 [file] [log] [blame]
[email protected]b39c6d92012-01-31 16:38:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d032f492009-09-29 00:33:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e15a4fa2010-02-11 23:09:295#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]d032f492009-09-29 00:33:466
[email protected]a82af392012-02-24 04:40:207#include <string>
8#include <vector>
[email protected]d032f492009-09-29 00:33:469
[email protected]30c1eea2011-10-17 18:40:3010#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0911#include "base/command_line.h"
[email protected]5b974952012-04-05 18:18:2312#include "base/message_loop.h"
[email protected]9addd1c2012-09-15 14:28:2413#include "base/metrics/histogram.h"
[email protected]338466a82011-05-03 04:27:4314#include "base/path_service.h"
[email protected]5b974952012-04-05 18:18:2315#include "base/string_number_conversions.h"
16#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3317#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2818#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2219#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3720#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2021#include "build/build_config.h"
[email protected]8510d282012-08-30 19:47:3822#include "chrome/browser/browser_process.h"
[email protected]8f42b4d2012-03-24 14:12:3323#include "chrome/browser/extensions/extension_info_map.h"
[email protected]8510d282012-08-30 19:47:3824#include "chrome/browser/io_thread.h"
[email protected]2e5f2ea42012-05-09 21:39:2125#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3326#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4627#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4328#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4629#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0030#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4631#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0932#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4633#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3034#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3335#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4036#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0337#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4038#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4439#include "content/public/common/child_process_host.h"
[email protected]885c0e92012-11-13 20:27:4240#include "extensions/common/constants.h"
41#include "extensions/common/url_pattern.h"
[email protected]8510d282012-08-30 19:47:3842#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4643#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5244#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1645#include "net/base/net_util.h"
[email protected]d571c032012-09-14 12:39:1846#include "net/base/tcp_listen_socket.h"
[email protected]8510d282012-08-30 19:47:3847#include "ppapi/proxy/ppapi_messages.h"
[email protected]d032f492009-09-29 00:33:4648
[email protected]d032f492009-09-29 00:33:4649#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2050#include <fcntl.h>
51
[email protected]d032f492009-09-29 00:33:4652#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3553#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2054#include <windows.h>
55
[email protected]b39c6d92012-01-31 16:38:4156#include "base/process_util.h"
[email protected]885c0e92012-11-13 20:27:4257#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2558#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3559#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3860#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5061#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4662#endif
63
[email protected]631bb742011-11-02 11:29:3964using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4065using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4466using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3867using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3968
[email protected]646e15552012-04-06 22:01:0469namespace {
70
71#if defined(OS_WIN)
72bool RunningOnWOW64() {
73 return (base::win::OSInfo::GetInstance()->wow64_status() ==
74 base::win::OSInfo::WOW64_ENABLED);
75}
76#endif
77
[email protected]646e15552012-04-06 22:01:0478void SetCloseOnExec(nacl::Handle fd) {
79#if defined(OS_POSIX)
80 int flags = fcntl(fd, F_GETFD);
81 CHECK_NE(flags, -1);
82 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
83 CHECK_EQ(rc, 0);
84#endif
85}
86
[email protected]00d99542012-04-17 22:48:0287bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0488 base::ProcessHandle processh,
89 nacl::Handle sourceh,
90 bool close_source,
91 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
92#if defined(OS_WIN)
93 HANDLE channel;
94 int flags = DUPLICATE_SAME_ACCESS;
95 if (close_source)
96 flags |= DUPLICATE_CLOSE_SOURCE;
97 if (!DuplicateHandle(GetCurrentProcess(),
98 reinterpret_cast<HANDLE>(sourceh),
99 processh,
100 &channel,
101 0, // Unused given DUPLICATE_SAME_ACCESS.
102 FALSE,
103 flags)) {
[email protected]09afc2e2012-04-10 17:29:03104 DLOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04105 return false;
106 }
107 handles_for_sel_ldr->push_back(
108 reinterpret_cast<nacl::FileDescriptor>(channel));
109#else
110 nacl::FileDescriptor channel;
111 channel.fd = sourceh;
112 channel.auto_close = close_source;
113 handles_for_sel_ldr->push_back(channel);
114#endif
115 return true;
116}
117
[email protected]27b04df2012-12-10 23:43:10118ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
119 // Only allow NaCl plugins to request certain permissions. We don't want
120 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
121 // permissions which may expand the surface area of the sandbox.
122 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
123 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
124}
125
[email protected]646e15552012-04-06 22:01:04126} // namespace
127
[email protected]1d8a3d1f2011-02-19 07:11:52128struct NaClProcessHost::NaClInternal {
129 std::vector<nacl::Handle> sockets_for_renderer;
130 std::vector<nacl::Handle> sockets_for_sel_ldr;
131};
132
[email protected]646e15552012-04-06 22:01:04133// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52134
[email protected]8510d282012-08-30 19:47:38135NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
136 : host_(host) {
137}
138
139bool NaClProcessHost::PluginListener::OnMessageReceived(
140 const IPC::Message& msg) {
141 return host_->OnUntrustedMessageForwarded(msg);
142}
143
[email protected]9da4fed2012-11-01 17:07:55144NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06145 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55146 uint32 permission_bits,
147 bool off_the_record)
[email protected]87f35592012-04-08 00:49:16148 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10149 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36150#if defined(OS_WIN)
151 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23152#elif defined(OS_LINUX)
153 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36154#endif
155 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38156#if defined(OS_WIN)
157 debug_exception_handler_requested_(false),
158#endif
[email protected]1d8a3d1f2011-02-19 07:11:52159 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06160 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]57473ebc2012-05-30 07:44:52161 enable_exception_handling_(false),
[email protected]2c227ff2012-08-11 00:51:32162 enable_debug_stub_(false),
[email protected]8510d282012-08-30 19:47:38163 off_the_record_(off_the_record),
164 enable_ipc_proxy_(false),
[email protected]e02ff722012-11-06 03:53:06165 ALLOW_THIS_IN_INITIALIZER_LIST(ipc_plugin_listener_(this)),
166 render_view_id_(render_view_id) {
[email protected]4967f792012-01-20 22:14:40167 process_.reset(content::BrowserChildProcessHost::Create(
168 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16169
170 // Set the display name so the user knows what plugin the process is running.
171 // We aren't on the UI thread so getting the pref locale for language
172 // formatting isn't possible, so IDN will be lost, but this is probably OK
173 // for this use case.
174 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06175
176 // We allow untrusted hardware exception handling to be enabled via
177 // an env var for consistency with the standalone build of NaCl.
178 if (CommandLine::ForCurrentProcess()->HasSwitch(
179 switches::kEnableNaClExceptionHandling) ||
180 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
181 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06182 }
[email protected]2c227ff2012-08-11 00:51:32183 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
184 switches::kEnableNaClDebug);
[email protected]17b5a8172012-06-22 21:09:09185
[email protected]45a9c0a2012-11-08 21:00:29186 enable_ipc_proxy_ = !CommandLine::ForCurrentProcess()->HasSwitch(
187 switches::kEnableNaClSRPCProxy);
[email protected]778a40252012-12-04 19:20:44188 // If render_view_id == 0 we do not need PPAPI, so we can skip
189 // PPAPI IPC proxy channel creation, etc.
190 if (!render_view_id_)
191 enable_ipc_proxy_ = false;
[email protected]d032f492009-09-29 00:33:46192}
193
[email protected]fb1277e82009-11-21 20:32:30194NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49195 int exit_code;
[email protected]4967f792012-01-20 22:14:40196 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49197 std::string message =
198 base::StringPrintf("NaCl process exited with status %i (0x%x)",
199 exit_code, exit_code);
200 if (exit_code == 0) {
201 LOG(INFO) << message;
202 } else {
203 LOG(ERROR) << message;
204 }
205
[email protected]1d8a3d1f2011-02-19 07:11:52206 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04207 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03208 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04209 }
[email protected]c47ec402010-07-29 10:20:49210 }
[email protected]1d8a3d1f2011-02-19 07:11:52211 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04212 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03213 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04214 }
[email protected]c47ec402010-07-29 10:20:49215 }
216
[email protected]909c2402011-05-09 11:39:04217 if (reply_msg_) {
218 // The process failed to launch for some reason.
219 // Don't keep the renderer hanging.
220 reply_msg_->set_reply_error();
221 chrome_render_message_filter_->Send(reply_msg_);
222 }
[email protected]b39c6d92012-01-31 16:38:41223#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36224 if (process_launched_by_broker_) {
225 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06226 }
[email protected]b39c6d92012-01-31 16:38:41227#endif
[email protected]fb1277e82009-11-21 20:32:30228}
229
[email protected]773ebb92011-11-15 19:06:52230// This is called at browser startup.
231// static
232void NaClProcessHost::EarlyStartup() {
233#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
234 // Open the IRT file early to make sure that it isn't replaced out from
235 // under us by autoupdate.
236 NaClBrowser::GetInstance()->EnsureIrtAvailable();
237#endif
[email protected]9addd1c2012-09-15 14:28:24238 CommandLine* cmd = CommandLine::ForCurrentProcess();
239 UMA_HISTOGRAM_BOOLEAN(
240 "NaCl.nacl-gdb",
241 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
242 UMA_HISTOGRAM_BOOLEAN(
243 "NaCl.nacl-gdb-script",
244 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
245 UMA_HISTOGRAM_BOOLEAN(
246 "NaCl.enable-nacl-debug",
247 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04248 NaClBrowser::GetInstance()->SetDebugPatterns(
249 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52250}
251
[email protected]a575da52012-03-22 13:08:36252void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52253 ChromeRenderMessageFilter* chrome_render_message_filter,
254 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33255 IPC::Message* reply_msg,
256 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36257 chrome_render_message_filter_ = chrome_render_message_filter;
258 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33259 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36260
[email protected]c47ec402010-07-29 10:20:49261 // Place an arbitrary limit on the number of sockets to limit
262 // exposure in case the renderer is compromised. We can increase
263 // this if necessary.
264 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36265 delete this;
266 return;
[email protected]c47ec402010-07-29 10:20:49267 }
268
[email protected]773ebb92011-11-15 19:06:52269 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03270 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06271 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
272 nacl_browser->EnsureAllResourcesAvailable();
273 if (!nacl_browser->IsOk()) {
274 DLOG(ERROR) << "Cannot launch NaCl 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
288 for (int i = 0; i < socket_count; i++) {
289 nacl::Handle pair[2];
290 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36291 if (nacl::SocketPair(pair) == -1) {
292 delete this;
293 return;
294 }
[email protected]1d8a3d1f2011-02-19 07:11:52295 internal_->sockets_for_renderer.push_back(pair[0]);
296 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49297 SetCloseOnExec(pair[0]);
298 SetCloseOnExec(pair[1]);
299 }
[email protected]d032f492009-09-29 00:33:46300
301 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30302 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36303 delete this;
[email protected]d032f492009-09-29 00:33:46304 }
[email protected]d032f492009-09-29 00:33:46305}
[email protected]646e15552012-04-06 22:01:04306
[email protected]5b974952012-04-05 18:18:23307#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04308void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
309 // Set process handle, if it was not set previously.
310 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40311 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
312 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04313 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40314 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04315 if (base::OpenProcessHandleWithAccess(
316 peer_pid,
317 base::kProcessAccessDuplicateHandle |
318 base::kProcessAccessQueryInformation |
319 base::kProcessAccessWaitForTermination,
320 &process)) {
321 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03322 if (!StartWithLaunchedProcess()) {
323 delete this;
324 return;
325 }
[email protected]646e15552012-04-06 22:01:04326 } else {
[email protected]09afc2e2012-04-10 17:29:03327 DLOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04328 }
329 }
[email protected]646e15552012-04-06 22:01:04330}
331#else
332void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
333}
334#endif
335
336#if defined(OS_WIN)
337void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
338 process_launched_by_broker_ = true;
339 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03340 if (!StartWithLaunchedProcess())
341 delete this;
[email protected]646e15552012-04-06 22:01:04342}
343
[email protected]ea6588842012-05-03 05:39:38344void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
345 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
346 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
347 Send(reply);
[email protected]646e15552012-04-06 22:01:04348}
349#endif
350
351// Needed to handle sync messages in OnMessageRecieved.
352bool NaClProcessHost::Send(IPC::Message* msg) {
353 return process_->Send(msg);
354}
355
356#if defined(OS_WIN)
357scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33358 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23359 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46360 CommandLine* cmd_line = new CommandLine(nacl_gdb);
361 // We can't use PrependWrapper because our parameters contain spaces.
362 cmd_line->AppendArg("--eval-command");
363 const FilePath::StringType& irt_path =
364 NaClBrowser::GetInstance()->GetIrtFilePath().value();
365 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23366 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33367 if (!manifest_path.empty()) {
368 cmd_line->AppendArg("--eval-command");
369 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
370 manifest_path.value());
371 }
[email protected]401b90792012-05-30 11:41:13372 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
373 switches::kNaClGdbScript);
374 if (!script.empty()) {
375 cmd_line->AppendArg("--command");
376 cmd_line->AppendArgNative(script.value());
377 }
[email protected]31a665e72012-03-11 12:37:46378 cmd_line->AppendArg("--args");
379 const CommandLine::StringVector& argv = line->argv();
380 for (size_t i = 0; i < argv.size(); i++) {
381 cmd_line->AppendArgNative(argv[i]);
382 }
383 return scoped_ptr<CommandLine>(cmd_line);
384}
[email protected]5b974952012-04-05 18:18:23385#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06386class NaClProcessHost::NaClGdbWatchDelegate
387 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23388 public:
389 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
390 NaClGdbWatchDelegate(int fd_read, int fd_write,
391 const base::Closure& reply)
392 : fd_read_(fd_read),
393 fd_write_(fd_write),
394 reply_(reply) {}
395
396 ~NaClGdbWatchDelegate() {
397 if (HANDLE_EINTR(close(fd_read_)) != 0)
398 DLOG(ERROR) << "close(fd_read_) failed";
399 if (HANDLE_EINTR(close(fd_write_)) != 0)
400 DLOG(ERROR) << "close(fd_write_) failed";
401 }
402
403 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
404 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
405
406 private:
407 int fd_read_;
408 int fd_write_;
409 base::Closure reply_;
410};
411
[email protected]512d03f2012-06-26 01:06:06412void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
413 int fd) {
[email protected]5b974952012-04-05 18:18:23414 char buf;
415 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
416 LOG(ERROR) << "Failed to sync with nacl-gdb";
417 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
418}
[email protected]5b974952012-04-05 18:18:23419
420bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
421 CommandLine::StringType nacl_gdb =
422 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
423 switches::kNaClGdb);
424 CommandLine::StringVector argv;
425 // We don't support spaces inside arguments in --nacl-gdb switch.
426 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
427 CommandLine cmd_line(argv);
428 cmd_line.AppendArg("--eval-command");
429 const FilePath::StringType& irt_path =
430 NaClBrowser::GetInstance()->GetIrtFilePath().value();
431 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
432 FilePath manifest_path = GetManifestPath();
433 if (!manifest_path.empty()) {
434 cmd_line.AppendArg("--eval-command");
435 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
436 manifest_path.value());
437 }
438 cmd_line.AppendArg("--eval-command");
439 cmd_line.AppendArg("attach " + base::IntToString(pid));
440 int fds[2];
441 if (pipe(fds) != 0)
442 return false;
443 // Tell the debugger to send a byte to the writable end of the pipe.
444 // We use a file descriptor in our process because the debugger will be
445 // typically launched in a separate terminal, and a lot of terminals close all
446 // file descriptors before launching external programs.
447 cmd_line.AppendArg("--eval-command");
448 cmd_line.AppendArg("dump binary value /proc/" +
449 base::IntToString(base::GetCurrentProcId()) +
450 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]401b90792012-05-30 11:41:13451 FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
452 switches::kNaClGdbScript);
453 if (!script.empty()) {
454 cmd_line.AppendArg("--command");
455 cmd_line.AppendArgNative(script.value());
456 }
[email protected]5b974952012-04-05 18:18:23457 // wait on fds[0]
458 // If the debugger crashes before attaching to the NaCl process, the user can
459 // release resources by terminating the NaCl loader in Chrome Task Manager.
460 nacl_gdb_watcher_delegate_.reset(
461 new NaClGdbWatchDelegate(
462 fds[0], fds[1],
463 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
464 weak_factory_.GetWeakPtr())));
465 MessageLoopForIO::current()->WatchFileDescriptor(
466 fds[0],
467 true,
468 MessageLoopForIO::WATCH_READ,
469 &nacl_gdb_watcher_,
470 nacl_gdb_watcher_delegate_.get());
471 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
472}
473
474void NaClProcessHost::OnNaClGdbAttached() {
475 wait_for_nacl_gdb_ = false;
476 nacl_gdb_watcher_.StopWatchingFileDescriptor();
477 nacl_gdb_watcher_delegate_.reset();
478 OnProcessLaunched();
479}
480#endif
481
482FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34483 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16484 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
[email protected]885c0e92012-11-13 20:27:42485 if (extension != NULL &&
486 manifest_url_.SchemeIs(extensions::kExtensionScheme)) {
[email protected]87f35592012-04-08 00:49:16487 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23488 TrimString(path, "/", &path); // Remove first slash
489 return extension->path().AppendASCII(path);
490 }
491 return FilePath();
492}
[email protected]31a665e72012-03-11 12:37:46493
[email protected]fb1277e82009-11-21 20:32:30494bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40495 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44496 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46497 return false;
498
[email protected]e3fc75a2011-05-05 08:20:42499 CommandLine::StringType nacl_loader_prefix;
500#if defined(OS_POSIX)
501 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
502 switches::kNaClLoaderCmdPrefix);
503#endif // defined(OS_POSIX)
504
[email protected]d032f492009-09-29 00:33:46505 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36506
507#if defined(OS_MACOSX)
508 // The Native Client process needs to be able to allocate a 1GB contiguous
509 // region to use as the client environment's virtual address space. ASLR
510 // (PIE) interferes with this by making it possible that no gap large enough
511 // to accomodate this request will exist in the child process' address
512 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
513 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49514 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36515#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49516 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
517 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36518#else
[email protected]4cb43102011-12-02 20:24:49519 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36520#endif
521
[email protected]4cb43102011-12-02 20:24:49522 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30523 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46524 return false;
525
[email protected]31a665e72012-03-11 12:37:46526#if defined(OS_WIN)
527 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
528 if (RunningOnWOW64()) {
529 FilePath module_path;
530 if (!PathService::Get(base::FILE_MODULE, &module_path))
531 return false;
532 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
533 }
534#endif
535
[email protected]33a05af2012-03-02 18:15:51536 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
537 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13538
[email protected]05076ba22010-07-30 05:59:57539 cmd_line->AppendSwitchASCII(switches::kProcessType,
540 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44541 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44542 if (logging::DialogsAreSuppressed())
543 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46544
[email protected]e3fc75a2011-05-05 08:20:42545 if (!nacl_loader_prefix.empty())
546 cmd_line->PrependWrapper(nacl_loader_prefix);
547
[email protected]31a665e72012-03-11 12:37:46548 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
549 switches::kNaClGdb);
550 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23551#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46552 cmd_line->AppendSwitch(switches::kNoSandbox);
553 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04554 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46555 // We can't use process_->Launch() because OnProcessLaunched will be called
556 // with process_->GetData().handle filled by handle of gdb process. This
557 // handle will be used to duplicate handles for NaCl process and as
558 // a result NaCl process will not be able to use them.
559 //
560 // So we don't fill process_->GetData().handle and wait for
561 // OnChannelConnected to get handle of NaCl process from its pid. Then we
562 // call OnProcessLaunched.
563 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23564#elif defined(OS_LINUX)
565 wait_for_nacl_gdb_ = true;
566#endif
[email protected]31a665e72012-03-11 12:37:46567 }
568
[email protected]103607e2010-02-01 18:57:09569 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46570#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52571 if (RunningOnWOW64()) {
[email protected]26b3c002012-04-26 19:38:34572 return NaClBrokerService::GetInstance()->LaunchLoader(
573 weak_factory_.GetWeakPtr(), channel_id);
[email protected]4bdde602010-06-16 03:17:35574 } else {
[email protected]33a05af2012-03-02 18:15:51575 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35576 }
[email protected]103607e2010-02-01 18:57:09577#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40578 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20579 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51580 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09581#endif
[email protected]d032f492009-09-29 00:33:46582
[email protected]fb1277e82009-11-21 20:32:30583 return true;
[email protected]d032f492009-09-29 00:33:46584}
585
[email protected]646e15552012-04-06 22:01:04586bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
587 bool handled = true;
588 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
589 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
590 OnQueryKnownToValidate)
591 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
592 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38593#if defined(OS_WIN)
594 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
595 OnAttachDebugExceptionHandler)
596#endif
[email protected]17b5a8172012-06-22 21:09:09597 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
598 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04599 IPC_MESSAGE_UNHANDLED(handled = false)
600 IPC_END_MESSAGE_MAP()
601 return handled;
[email protected]103607e2010-02-01 18:57:09602}
603
[email protected]773ebb92011-11-15 19:06:52604void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03605 if (!StartWithLaunchedProcess())
606 delete this;
[email protected]773ebb92011-11-15 19:06:52607}
608
[email protected]e97990f2012-05-15 22:06:06609// Called when the NaClBrowser singleton has been fully initialized.
610void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52611 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06612 if (!nacl_browser->IsReady() || !SendStart()) {
[email protected]09afc2e2012-04-10 17:29:03613 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]338466a82011-05-03 04:27:43614 delete this;
615 }
616}
617
[email protected]17b5a8172012-06-22 21:09:09618bool NaClProcessHost::ReplyToRenderer(
619 const IPC::ChannelHandle& channel_handle) {
[email protected]c47ec402010-07-29 10:20:49620 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]1d8a3d1f2011-02-19 07:11:52621 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49622#if defined(OS_WIN)
623 // Copy the handle into the renderer process.
624 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04625 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
626 reinterpret_cast<HANDLE>(
627 internal_->sockets_for_renderer[i]),
628 chrome_render_message_filter_->peer_handle(),
629 &handle_in_renderer,
630 0, // Unused given DUPLICATE_SAME_ACCESS.
631 FALSE,
632 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]09afc2e2012-04-10 17:29:03633 DLOG(ERROR) << "DuplicateHandle() failed";
634 return false;
[email protected]909c2402011-05-09 11:39:04635 }
[email protected]c47ec402010-07-29 10:20:49636 handles_for_renderer.push_back(
637 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
638#else
639 // No need to dup the imc_handle - we don't pass it anywhere else so
640 // it cannot be closed.
641 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52642 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49643 imc_handle.auto_close = true;
644 handles_for_renderer.push_back(imc_handle);
645#endif
646 }
647
648#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50649 // If we are on 64-bit Windows, the NaCl process's sandbox is
650 // managed by a different process from the renderer's sandbox. We
651 // need to inform the renderer's sandbox about the NaCl process so
652 // that the renderer can send handles to the NaCl process using
653 // BrokerDuplicateHandle().
654 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01655 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]e4f6eb0232012-04-17 00:47:50656 DLOG(ERROR) << "Failed to add NaCl process PID";
657 return false;
658 }
659 }
[email protected]fb1277e82009-11-21 20:32:30660#endif
661
[email protected]2ccf45c2011-08-19 23:35:50662 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]2b7197b2012-10-23 21:54:42663 reply_msg_, handles_for_renderer,
664 channel_handle, process_->GetData().id);
[email protected]92d56412011-03-24 20:53:52665 chrome_render_message_filter_->Send(reply_msg_);
666 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30667 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52668 internal_->sockets_for_renderer.clear();
[email protected]00d99542012-04-17 22:48:02669 return true;
670}
[email protected]fb1277e82009-11-21 20:32:30671
[email protected]d571c032012-09-14 12:39:18672// TCP port we chose for NaCl debug stub. It can be any other number.
673static const int kDebugStubPort = 4014;
674
675#if defined(OS_POSIX)
676SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]13cad0b2012-12-07 17:57:34677 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
678 SocketDescriptor s;
679 // We allocate currently unused TCP port for debug stub tests. The port
680 // number is passed to the test via debug stub port listener.
681 if (nacl_browser->HasGdbDebugStubPortListener()) {
682 int port;
683 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
684 if (s != net::TCPListenSocket::kInvalidSocket) {
685 nacl_browser->FireGdbDebugStubPortOpened(port);
686 }
687 } else {
688 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
689 }
690 if (s == net::TCPListenSocket::kInvalidSocket) {
691 LOG(ERROR) << "failed to open socket for debug stub";
692 return net::TCPListenSocket::kInvalidSocket;
693 }
[email protected]d571c032012-09-14 12:39:18694 if (listen(s, 1)) {
695 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34696 if (HANDLE_EINTR(close(s)) < 0)
697 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]d571c032012-09-14 12:39:18698 return net::TCPListenSocket::kInvalidSocket;
699 }
700 return s;
701}
702#endif
703
[email protected]00d99542012-04-17 22:48:02704bool NaClProcessHost::StartNaClExecution() {
705 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
706
707 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12708 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
709 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02710 params.version = chrome::VersionInfo().CreateVersionString();
711 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04712 params.enable_debug_stub = enable_debug_stub_ &&
713 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]17b5a8172012-06-22 21:09:09714 params.enable_ipc_proxy = enable_ipc_proxy_;
[email protected]00d99542012-04-17 22:48:02715
716 base::PlatformFile irt_file = nacl_browser->IrtFile();
717 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
718
719 const ChildProcessData& data = process_->GetData();
[email protected]1d8a3d1f2011-02-19 07:11:52720 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]00d99542012-04-17 22:48:02721 if (!ShareHandleToSelLdr(data.handle,
722 internal_->sockets_for_sel_ldr[i], true,
723 &params.handles)) {
[email protected]09afc2e2012-04-10 17:29:03724 return false;
[email protected]c47ec402010-07-29 10:20:49725 }
[email protected]773ebb92011-11-15 19:06:52726 }
727
728 // Send over the IRT file handle. We don't close our own copy!
[email protected]00d99542012-04-17 22:48:02729 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
[email protected]09afc2e2012-04-10 17:29:03730 return false;
[email protected]c47ec402010-07-29 10:20:49731
[email protected]ab88d1542011-11-18 22:52:00732#if defined(OS_MACOSX)
733 // For dynamic loading support, NaCl requires a file descriptor that
734 // was created in /tmp, since those created with shm_open() are not
735 // mappable with PROT_EXEC. Rather than requiring an extra IPC
736 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25737 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31738 base::SharedMemoryCreateOptions options;
739 options.size = 1;
740 options.executable = true;
741 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03742 DLOG(ERROR) << "Failed to allocate memory buffer";
743 return false;
[email protected]2c68bf032010-11-11 23:16:30744 }
[email protected]cbbe7842011-11-17 22:01:25745 nacl::FileDescriptor memory_fd;
746 memory_fd.fd = dup(memory_buffer.handle().fd);
747 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03748 DLOG(ERROR) << "Failed to dup() a file descriptor";
749 return false;
[email protected]cbbe7842011-11-17 22:01:25750 }
751 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02752 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30753#endif
754
[email protected]d571c032012-09-14 12:39:18755#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05756 if (params.enable_debug_stub) {
[email protected]d571c032012-09-14 12:39:18757 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
758 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
759 params.debug_stub_server_bound_socket =
760 nacl::FileDescriptor(server_bound_socket, true);
761 }
762 }
763#endif
764
[email protected]ea6588842012-05-03 05:39:38765 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41766
[email protected]1d8a3d1f2011-02-19 07:11:52767 internal_->sockets_for_sel_ldr.clear();
[email protected]09afc2e2012-04-10 17:29:03768 return true;
769}
770
[email protected]00d99542012-04-17 22:48:02771bool NaClProcessHost::SendStart() {
[email protected]17b5a8172012-06-22 21:09:09772 if (!enable_ipc_proxy_) {
773 if (!ReplyToRenderer(IPC::ChannelHandle()))
774 return false;
775 }
776 return StartNaClExecution();
777}
778
[email protected]8510d282012-08-30 19:47:38779// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
780// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
781// listener.
[email protected]17b5a8172012-06-22 21:09:09782void NaClProcessHost::OnPpapiChannelCreated(
783 const IPC::ChannelHandle& channel_handle) {
784 DCHECK(enable_ipc_proxy_);
[email protected]8510d282012-08-30 19:47:38785 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
786 // channel.
787 if (!ipc_proxy_channel_.get()) {
788 ipc_proxy_channel_.reset(
789 new IPC::ChannelProxy(channel_handle,
790 IPC::Channel::MODE_CLIENT,
791 &ipc_plugin_listener_,
792 base::MessageLoopProxy::current()));
[email protected]0c7193742012-11-07 19:05:03793 // Create the browser ppapi host and enable PPAPI message dispatching to the
794 // browser process.
[email protected]adf7cd52012-12-04 19:37:46795 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
796 ipc_proxy_channel_.get(), //process_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03797 permissions_,
798 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55799 ipc_proxy_channel_.get(),
[email protected]e02ff722012-11-06 03:53:06800 chrome_render_message_filter_->GetHostResolver(),
801 chrome_render_message_filter_->render_process_id(),
[email protected]adf7cd52012-12-04 19:37:46802 render_view_id_));
803
[email protected]8510d282012-08-30 19:47:38804 // Send a message to create the NaCl-Renderer channel. The handle is just
805 // a place holder.
806 ipc_proxy_channel_->Send(
807 new PpapiMsg_CreateNaClChannel(
808 chrome_render_message_filter_->render_process_id(),
[email protected]195d4cde2012-10-02 18:12:41809 permissions_,
[email protected]8510d282012-08-30 19:47:38810 chrome_render_message_filter_->off_the_record(),
811 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
812 IPC::InvalidPlatformFileForTransit())));
813 } else if (reply_msg_) {
814 // Otherwise, this must be a renderer channel.
815 ReplyToRenderer(channel_handle);
816 } else {
817 // Attempt to open more than 1 renderer channel is not supported.
818 // Shut down the NaCl process.
819 process_->GetHost()->ForceShutdown();
820 }
821}
822
823bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
824 // Handle messages that have been forwarded from our PluginListener.
825 // These messages come from untrusted code so should be handled with care.
826 bool handled = true;
827 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
828 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
829 OnPpapiChannelCreated)
830 IPC_MESSAGE_UNHANDLED(handled = false)
831 IPC_END_MESSAGE_MAP()
832 return handled;
[email protected]00d99542012-04-17 22:48:02833}
834
[email protected]09afc2e2012-04-10 17:29:03835bool NaClProcessHost::StartWithLaunchedProcess() {
836#if defined(OS_LINUX)
837 if (wait_for_nacl_gdb_) {
838 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
839 // We will be called with wait_for_nacl_gdb_ = false once debugger is
840 // attached to the program.
841 return true;
842 }
843 DLOG(ERROR) << "Failed to launch debugger";
844 // Continue execution without debugger.
845 }
846#endif
847
848 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03849
[email protected]e97990f2012-05-15 22:06:06850 if (nacl_browser->IsReady()) {
851 return SendStart();
852 } else if (nacl_browser->IsOk()) {
853 nacl_browser->WaitForResources(
854 base::Bind(&NaClProcessHost::OnResourcesReady,
855 weak_factory_.GetWeakPtr()));
856 return true;
857 } else {
858 return false;
859 }
[email protected]d032f492009-09-29 00:33:46860}
861
[email protected]4a0141b2012-03-27 01:15:30862void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
863 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47864 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52865 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30866}
867
868void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52869 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30870}
[email protected]ea6588842012-05-03 05:39:38871
872#if defined(OS_WIN)
873void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
874 IPC::Message* reply_msg) {
875 if (!AttachDebugExceptionHandler(info, reply_msg)) {
876 // Send failure message.
877 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
878 false);
879 Send(reply_msg);
880 }
881}
882
883bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
884 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32885 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38886 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32887 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38888 return false;
889 }
890 if (debug_exception_handler_requested_) {
891 // The NaCl process should not request this multiple times.
892 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
893 return false;
894 }
895 debug_exception_handler_requested_ = true;
896
897 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
898 base::win::ScopedHandle process_handle;
899 // We cannot use process_->GetData().handle because it does not have
900 // the necessary access rights. We open the new handle here rather
901 // than in the NaCl broker process in case the NaCl loader process
902 // dies before the NaCl broker process receives the message we send.
903 // The debug exception handler uses DebugActiveProcess() to attach,
904 // but this takes a PID. We need to prevent the NaCl loader's PID
905 // from being reused before DebugActiveProcess() is called, and
906 // holding a process handle open achieves this.
907 if (!base::OpenProcessHandleWithAccess(
908 nacl_pid,
909 base::kProcessAccessQueryInformation |
910 base::kProcessAccessSuspendResume |
911 base::kProcessAccessTerminate |
912 base::kProcessAccessVMOperation |
913 base::kProcessAccessVMRead |
914 base::kProcessAccessVMWrite |
915 base::kProcessAccessWaitForTermination,
916 process_handle.Receive())) {
917 LOG(ERROR) << "Failed to get process handle";
918 return false;
919 }
920
921 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
922 // If the NaCl loader is 64-bit, the process running its debug
923 // exception handler must be 64-bit too, so we use the 64-bit NaCl
924 // broker process for this. Otherwise, on a 32-bit system, we use
925 // the 32-bit browser process to run the debug exception handler.
926 if (RunningOnWOW64()) {
927 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48928 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38929 } else {
930 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48931 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38932 base::MessageLoopProxy::current(),
933 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
934 weak_factory_.GetWeakPtr()));
935 return true;
936 }
937}
938#endif