blob: 1cbab6fc5131b5afe9c090f75902381dd0c9ec62 [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]4734d0b2011-12-03 07:10:4412#include "base/memory/singleton.h"
[email protected]338466a82011-05-03 04:27:4313#include "base/path_service.h"
[email protected]8f42b4d2012-03-24 14:12:3314#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2815#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2216#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3717#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2018#include "build/build_config.h"
[email protected]8f42b4d2012-03-24 14:12:3319#include "chrome/browser/extensions/extension_info_map.h"
20#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4621#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4322#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4623#include "chrome/common/chrome_switches.h"
24#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0925#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4626#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3027#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3328#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4029#include "content/public/browser/browser_child_process_host.h"
30#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4431#include "content/public/common/child_process_host.h"
[email protected]8f42b4d2012-03-24 14:12:3332#include "googleurl/src/gurl.h"
[email protected]d032f492009-09-29 00:33:4633#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5234#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]d032f492009-09-29 00:33:4635
[email protected]d032f492009-09-29 00:33:4636#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2037#include <fcntl.h>
38
[email protected]d032f492009-09-29 00:33:4639#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3540#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2041#include <windows.h>
42
[email protected]b39c6d92012-01-31 16:38:4143#include "base/threading/thread.h"
44#include "base/process_util.h"
[email protected]4bdde602010-06-16 03:17:3545#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]b39c6d92012-01-31 16:38:4146#include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h"
[email protected]d032f492009-09-29 00:33:4647#endif
48
[email protected]631bb742011-11-02 11:29:3949using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4050using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4451using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3952
[email protected]b39c6d92012-01-31 16:38:4153#if defined(OS_WIN)
54class NaClProcessHost::DebugContext
55 : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> {
56 public:
57 DebugContext()
58 : can_send_start_msg_(false),
59 child_process_host_(NULL) {
60 }
61
62 ~DebugContext() {
63 }
64
65 void AttachDebugger(int pid, base::ProcessHandle process);
66
67 // 6 methods below must be called on Browser::IO thread.
68 void SetStartMessage(IPC::Message* start_msg);
69 void SetChildProcessHost(content::ChildProcessHost* child_process_host);
70 void SetDebugThread(base::Thread* thread_);
71
72 // Start message is sent from 2 flows of execution. The first flow is
73 // NaClProcessHost::SendStart. The second flow is
74 // NaClProcessHost::OnChannelConnected and
75 // NaClProcessHost::DebugContext::AttachThread. The message itself is created
76 // by first flow. But the moment it can be sent is determined by second flow.
77 // So first flow executes SetStartMessage and SendStartMessage while second
78 // flow uses AllowAndSendStartMessage to either send potentially pending
79 // start message or set the flag that allows the first flow to do this.
80
81 // Clears the flag that prevents sending start message.
82 void AllowToSendStartMsg();
83 // Send start message to the NaCl process or do nothing if message is not
84 // set or not allowed to be send. If message is sent, it is cleared and
85 // repeated calls do nothing.
86 void SendStartMessage();
87 // Clear the flag that prevents further sending start message and send start
88 // message if it is set.
89 void AllowAndSendStartMessage();
90 private:
91 void StopThread();
92 // These 4 fields are accessed only from Browser::IO thread.
93 scoped_ptr<base::Thread> thread_;
94 scoped_ptr<IPC::Message> start_msg_;
95 // Debugger is attached or exception handling is not switched on.
96 // This means that start message can be sent to the NaCl process.
97 bool can_send_start_msg_;
98 content::ChildProcessHost* child_process_host_;
99};
100
101void NaClProcessHost::DebugContext::AttachDebugger(
102 int pid, base::ProcessHandle process) {
103 BOOL attached;
104 DWORD exit_code;
105 attached = DebugActiveProcess(pid);
106 if (!attached) {
107 LOG(ERROR) << "Failed to connect to the process";
108 }
109 BrowserThread::PostTask(
110 BrowserThread::IO, FROM_HERE,
111 base::Bind(
112 &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this));
113 if (attached) {
114 // debug the process
115 NaClDebugLoop(process, &exit_code);
116 base::CloseProcessHandle(process);
117 }
118 BrowserThread::PostTask(
119 BrowserThread::IO, FROM_HERE,
120 base::Bind(&NaClProcessHost::DebugContext::StopThread, this));
121}
122
123void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) {
124 start_msg_.reset(start_msg);
125}
126
127void NaClProcessHost::DebugContext::SetChildProcessHost(
128 content::ChildProcessHost* child_process_host) {
129 child_process_host_ = child_process_host;
130}
131
132void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) {
133 thread_.reset(thread);
134}
135
136void NaClProcessHost::DebugContext::AllowToSendStartMsg() {
137 can_send_start_msg_ = true;
138}
139
140void NaClProcessHost::DebugContext::SendStartMessage() {
141 if (start_msg_.get() && can_send_start_msg_) {
142 if (child_process_host_) {
143 if (!child_process_host_->Send(start_msg_.release())) {
144 LOG(ERROR) << "Failed to send start message";
145 }
146 }
147 }
148}
149
150void NaClProcessHost::DebugContext::AllowAndSendStartMessage() {
151 AllowToSendStartMsg();
152 SendStartMessage();
153}
154
155void NaClProcessHost::DebugContext::StopThread() {
156 thread_->Stop();
157 thread_.reset();
158}
159#endif
160
[email protected]c47ec402010-07-29 10:20:49161namespace {
162
163void SetCloseOnExec(nacl::Handle fd) {
164#if defined(OS_POSIX)
165 int flags = fcntl(fd, F_GETFD);
[email protected]773ebb92011-11-15 19:06:52166 CHECK_NE(flags, -1);
[email protected]c47ec402010-07-29 10:20:49167 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
[email protected]773ebb92011-11-15 19:06:52168 CHECK_EQ(rc, 0);
[email protected]c47ec402010-07-29 10:20:49169#endif
170}
171
[email protected]773ebb92011-11-15 19:06:52172// Represents shared state for all NaClProcessHost objects in the browser.
173// Currently this just handles holding onto the file descriptor for the IRT.
174class NaClBrowser {
175 public:
176 static NaClBrowser* GetInstance() {
177 return Singleton<NaClBrowser>::get();
178 }
179
180 bool IrtAvailable() const {
181 return irt_platform_file_ != base::kInvalidPlatformFileValue;
182 }
183
184 base::PlatformFile IrtFile() const {
185 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
186 return irt_platform_file_;
187 }
188
189 // Asynchronously attempt to get the IRT open.
190 bool EnsureIrtAvailable();
191
192 // Make sure the IRT gets opened and follow up with the reply when it's ready.
193 bool MakeIrtAvailable(const base::Closure& reply);
194
[email protected]31a665e72012-03-11 12:37:46195 // Path to IRT. Available even before IRT is loaded.
196 const FilePath& GetIrtFilePath();
197
[email protected]773ebb92011-11-15 19:06:52198 private:
199 base::PlatformFile irt_platform_file_;
200
[email protected]31a665e72012-03-11 12:37:46201 FilePath irt_filepath_;
202
[email protected]773ebb92011-11-15 19:06:52203 friend struct DefaultSingletonTraits<NaClBrowser>;
204
205 NaClBrowser()
[email protected]31a665e72012-03-11 12:37:46206 : irt_platform_file_(base::kInvalidPlatformFileValue),
207 irt_filepath_() {
208 InitIrtFilePath();
209 }
[email protected]773ebb92011-11-15 19:06:52210
211 ~NaClBrowser() {
212 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
213 base::ClosePlatformFile(irt_platform_file_);
214 }
215
[email protected]31a665e72012-03-11 12:37:46216 void InitIrtFilePath();
217
[email protected]773ebb92011-11-15 19:06:52218 void OpenIrtLibraryFile();
219
220 static void DoOpenIrtLibraryFile() {
221 GetInstance()->OpenIrtLibraryFile();
222 }
223
224 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
225};
226
[email protected]c47ec402010-07-29 10:20:49227} // namespace
228
[email protected]1d8a3d1f2011-02-19 07:11:52229struct NaClProcessHost::NaClInternal {
230 std::vector<nacl::Handle> sockets_for_renderer;
231 std::vector<nacl::Handle> sockets_for_sel_ldr;
232};
233
[email protected]773ebb92011-11-15 19:06:52234#if defined(OS_WIN)
[email protected]5804cb5d2011-12-19 21:55:35235static bool RunningOnWOW64() {
[email protected]773ebb92011-11-15 19:06:52236 return (base::win::OSInfo::GetInstance()->wow64_status() ==
237 base::win::OSInfo::WOW64_ENABLED);
[email protected]773ebb92011-11-15 19:06:52238}
[email protected]5804cb5d2011-12-19 21:55:35239#endif
[email protected]773ebb92011-11-15 19:06:52240
[email protected]1dbaaa702011-04-06 17:43:42241NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]a575da52012-03-22 13:08:36242 :
243#if defined(OS_WIN)
244 process_launched_by_broker_(false),
245#endif
246 reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52247 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06248 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
249 enable_exception_handling_(false) {
[email protected]4967f792012-01-20 22:14:40250 process_.reset(content::BrowserChildProcessHost::Create(
251 content::PROCESS_TYPE_NACL_LOADER, this));
252 process_->SetName(WideToUTF16Hack(url));
[email protected]5ca93be2012-03-21 20:04:06253
254 // We allow untrusted hardware exception handling to be enabled via
255 // an env var for consistency with the standalone build of NaCl.
256 if (CommandLine::ForCurrentProcess()->HasSwitch(
257 switches::kEnableNaClExceptionHandling) ||
258 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
259 enable_exception_handling_ = true;
[email protected]b39c6d92012-01-31 16:38:41260#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06261 if (!RunningOnWOW64()) {
262 debug_context_ = new DebugContext();
263 }
[email protected]b39c6d92012-01-31 16:38:41264#endif
[email protected]5ca93be2012-03-21 20:04:06265 }
[email protected]d032f492009-09-29 00:33:46266}
267
[email protected]fb1277e82009-11-21 20:32:30268NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49269 int exit_code;
[email protected]4967f792012-01-20 22:14:40270 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49271 std::string message =
272 base::StringPrintf("NaCl process exited with status %i (0x%x)",
273 exit_code, exit_code);
274 if (exit_code == 0) {
275 LOG(INFO) << message;
276 } else {
277 LOG(ERROR) << message;
278 }
279
[email protected]1d8a3d1f2011-02-19 07:11:52280 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04281 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
282 LOG(ERROR) << "nacl::Close() failed";
283 }
[email protected]c47ec402010-07-29 10:20:49284 }
[email protected]1d8a3d1f2011-02-19 07:11:52285 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04286 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
287 LOG(ERROR) << "nacl::Close() failed";
288 }
[email protected]c47ec402010-07-29 10:20:49289 }
290
[email protected]909c2402011-05-09 11:39:04291 if (reply_msg_) {
292 // The process failed to launch for some reason.
293 // Don't keep the renderer hanging.
294 reply_msg_->set_reply_error();
295 chrome_render_message_filter_->Send(reply_msg_);
296 }
[email protected]b39c6d92012-01-31 16:38:41297#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36298 if (process_launched_by_broker_) {
299 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06300 }
301 if (debug_context_ != NULL) {
[email protected]b39c6d92012-01-31 16:38:41302 debug_context_->SetChildProcessHost(NULL);
303 }
304#endif
[email protected]fb1277e82009-11-21 20:32:30305}
306
[email protected]773ebb92011-11-15 19:06:52307// Attempt to ensure the IRT will be available when we need it, but don't wait.
308bool NaClBrowser::EnsureIrtAvailable() {
309 if (IrtAvailable())
310 return true;
311
312 return BrowserThread::PostTask(
313 BrowserThread::FILE, FROM_HERE,
314 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
315}
316
317// We really need the IRT to be available now, so make sure that it is.
318// When it's ready, we'll run the reply closure.
319bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
320 return BrowserThread::PostTaskAndReply(
321 BrowserThread::FILE, FROM_HERE,
322 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
323}
324
325// This is called at browser startup.
326// static
327void NaClProcessHost::EarlyStartup() {
328#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
329 // Open the IRT file early to make sure that it isn't replaced out from
330 // under us by autoupdate.
331 NaClBrowser::GetInstance()->EnsureIrtAvailable();
332#endif
333}
334
[email protected]a575da52012-03-22 13:08:36335void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52336 ChromeRenderMessageFilter* chrome_render_message_filter,
337 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33338 IPC::Message* reply_msg,
339 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36340 chrome_render_message_filter_ = chrome_render_message_filter;
341 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33342 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36343
[email protected]c47ec402010-07-29 10:20:49344 // Place an arbitrary limit on the number of sockets to limit
345 // exposure in case the renderer is compromised. We can increase
346 // this if necessary.
347 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36348 delete this;
349 return;
[email protected]c47ec402010-07-29 10:20:49350 }
351
[email protected]773ebb92011-11-15 19:06:52352 // Start getting the IRT open asynchronously while we launch the NaCl process.
353 // We'll make sure this actually finished in OnProcessLaunched, below.
354 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
355 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36356 delete this;
357 return;
[email protected]773ebb92011-11-15 19:06:52358 }
359
[email protected]c47ec402010-07-29 10:20:49360 // Rather than creating a socket pair in the renderer, and passing
361 // one side through the browser to sel_ldr, socket pairs are created
362 // in the browser and then passed to the renderer and sel_ldr.
363 //
364 // This is mainly for the benefit of Windows, where sockets cannot
365 // be passed in messages, but are copied via DuplicateHandle().
366 // This means the sandboxed renderer cannot send handles to the
367 // browser process.
368
369 for (int i = 0; i < socket_count; i++) {
370 nacl::Handle pair[2];
371 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36372 if (nacl::SocketPair(pair) == -1) {
373 delete this;
374 return;
375 }
[email protected]1d8a3d1f2011-02-19 07:11:52376 internal_->sockets_for_renderer.push_back(pair[0]);
377 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49378 SetCloseOnExec(pair[0]);
379 SetCloseOnExec(pair[1]);
380 }
[email protected]d032f492009-09-29 00:33:46381
382 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30383 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36384 delete this;
[email protected]d032f492009-09-29 00:33:46385 }
[email protected]d032f492009-09-29 00:33:46386}
387
[email protected]8ae7bd6f2012-03-14 03:23:02388scoped_ptr<CommandLine> NaClProcessHost::LaunchWithNaClGdb(
[email protected]8f42b4d2012-03-24 14:12:33389 const FilePath& nacl_gdb,
390 CommandLine* line,
391 const FilePath& manifest_path) {
[email protected]31a665e72012-03-11 12:37:46392 CommandLine* cmd_line = new CommandLine(nacl_gdb);
393 // We can't use PrependWrapper because our parameters contain spaces.
394 cmd_line->AppendArg("--eval-command");
395 const FilePath::StringType& irt_path =
396 NaClBrowser::GetInstance()->GetIrtFilePath().value();
397 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]8f42b4d2012-03-24 14:12:33398 if (!manifest_path.empty()) {
399 cmd_line->AppendArg("--eval-command");
400 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
401 manifest_path.value());
402 }
[email protected]31a665e72012-03-11 12:37:46403 cmd_line->AppendArg("--args");
404 const CommandLine::StringVector& argv = line->argv();
405 for (size_t i = 0; i < argv.size(); i++) {
406 cmd_line->AppendArgNative(argv[i]);
407 }
408 return scoped_ptr<CommandLine>(cmd_line);
409}
410
[email protected]fb1277e82009-11-21 20:32:30411bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40412 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44413 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46414 return false;
415
[email protected]e3fc75a2011-05-05 08:20:42416 CommandLine::StringType nacl_loader_prefix;
417#if defined(OS_POSIX)
418 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
419 switches::kNaClLoaderCmdPrefix);
420#endif // defined(OS_POSIX)
421
[email protected]d032f492009-09-29 00:33:46422 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36423
424#if defined(OS_MACOSX)
425 // The Native Client process needs to be able to allocate a 1GB contiguous
426 // region to use as the client environment's virtual address space. ASLR
427 // (PIE) interferes with this by making it possible that no gap large enough
428 // to accomodate this request will exist in the child process' address
429 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
430 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49431 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36432#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49433 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
434 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36435#else
[email protected]4cb43102011-12-02 20:24:49436 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36437#endif
438
[email protected]4cb43102011-12-02 20:24:49439 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30440 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46441 return false;
442
[email protected]31a665e72012-03-11 12:37:46443#if defined(OS_WIN)
444 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
445 if (RunningOnWOW64()) {
446 FilePath module_path;
447 if (!PathService::Get(base::FILE_MODULE, &module_path))
448 return false;
449 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
450 }
451#endif
452
[email protected]33a05af2012-03-02 18:15:51453 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
454 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13455
[email protected]05076ba22010-07-30 05:59:57456 cmd_line->AppendSwitchASCII(switches::kProcessType,
457 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44458 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44459 if (logging::DialogsAreSuppressed())
460 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46461
[email protected]e3fc75a2011-05-05 08:20:42462 if (!nacl_loader_prefix.empty())
463 cmd_line->PrependWrapper(nacl_loader_prefix);
464
[email protected]31a665e72012-03-11 12:37:46465 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
466 switches::kNaClGdb);
467 if (!nacl_gdb.empty()) {
[email protected]8f42b4d2012-03-24 14:12:33468 GURL manifest_url = GURL(process_->GetData().name);
469 FilePath manifest_path;
470 const Extension* extension = extension_info_map_->extensions().
471 GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url));
472 if (extension != NULL && manifest_url.SchemeIs(chrome::kExtensionScheme)) {
473 std::string path = manifest_url.path();
474 TrimString(path, "/", &path); // Remove first slash
475 manifest_path = extension->path().AppendASCII(path);
476 }
[email protected]31a665e72012-03-11 12:37:46477 cmd_line->AppendSwitch(switches::kNoSandbox);
478 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]8f42b4d2012-03-24 14:12:33479 LaunchWithNaClGdb(nacl_gdb, cmd_line.get(), manifest_path));
[email protected]31a665e72012-03-11 12:37:46480 // We can't use process_->Launch() because OnProcessLaunched will be called
481 // with process_->GetData().handle filled by handle of gdb process. This
482 // handle will be used to duplicate handles for NaCl process and as
483 // a result NaCl process will not be able to use them.
484 //
485 // So we don't fill process_->GetData().handle and wait for
486 // OnChannelConnected to get handle of NaCl process from its pid. Then we
487 // call OnProcessLaunched.
488 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
489 }
490
[email protected]103607e2010-02-01 18:57:09491 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46492#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52493 if (RunningOnWOW64()) {
[email protected]1dbaaa702011-04-06 17:43:42494 return NaClBrokerService::GetInstance()->LaunchLoader(
[email protected]4734d0b2011-12-03 07:10:44495 this, ASCIIToWide(channel_id));
[email protected]4bdde602010-06-16 03:17:35496 } else {
[email protected]33a05af2012-03-02 18:15:51497 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35498 }
[email protected]103607e2010-02-01 18:57:09499#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40500 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20501 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51502 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09503#endif
[email protected]d032f492009-09-29 00:33:46504
[email protected]fb1277e82009-11-21 20:32:30505 return true;
[email protected]d032f492009-09-29 00:33:46506}
507
[email protected]103607e2010-02-01 18:57:09508void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
[email protected]a575da52012-03-22 13:08:36509#if defined(OS_WIN)
510 process_launched_by_broker_ = true;
511#endif
[email protected]4967f792012-01-20 22:14:40512 process_->SetHandle(handle);
[email protected]103607e2010-02-01 18:57:09513 OnProcessLaunched();
514}
515
[email protected]463ea5f2011-12-03 06:57:47516void NaClProcessHost::OnProcessCrashed(int exit_code) {
517 std::string message = base::StringPrintf(
518 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
519 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09520}
521
[email protected]75e0c502011-12-16 21:53:01522namespace {
523
524// Determine the name of the IRT file based on the architecture.
525
526#define NACL_IRT_FILE_NAME(arch_string) \
527 (FILE_PATH_LITERAL("nacl_irt_") \
528 FILE_PATH_LITERAL(arch_string) \
529 FILE_PATH_LITERAL(".nexe"))
530
531const FilePath::StringType NaClIrtName() {
532#if defined(ARCH_CPU_X86_FAMILY)
[email protected]75e0c502011-12-16 21:53:01533#if defined(ARCH_CPU_X86_64)
[email protected]5804cb5d2011-12-19 21:55:35534 bool is64 = true;
535#elif defined(OS_WIN)
536 bool is64 = RunningOnWOW64();
537#else
538 bool is64 = false;
[email protected]75e0c502011-12-16 21:53:01539#endif
540 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
541#elif defined(ARCH_CPU_ARMEL)
542 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
543 // That may need to be based on the actual nexe rather than a static
544 // choice, which would require substantial refactoring.
545 return NACL_IRT_FILE_NAME("arm");
546#else
547#error Add support for your architecture to NaCl IRT file selection
548#endif
549}
550
551} // namespace
552
[email protected]31a665e72012-03-11 12:37:46553void NaClBrowser::InitIrtFilePath() {
[email protected]88e15832011-07-19 01:18:24554 // Allow the IRT library to be overridden via an environment
555 // variable. This allows the NaCl/Chromium integration bot to
556 // specify a newly-built IRT rather than using a prebuilt one
557 // downloaded via Chromium's DEPS file. We use the same environment
558 // variable that the standalone NaCl PPAPI plugin accepts.
559 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
560 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52561 FilePath::StringType path_string(
562 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
[email protected]31a665e72012-03-11 12:37:46563 irt_filepath_ = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24564 } else {
565 FilePath plugin_dir;
566 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
567 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24568 return;
569 }
[email protected]773ebb92011-11-15 19:06:52570
[email protected]31a665e72012-03-11 12:37:46571 irt_filepath_ = plugin_dir.Append(NaClIrtName());
[email protected]338466a82011-05-03 04:27:43572 }
[email protected]31a665e72012-03-11 12:37:46573}
574
575const FilePath& NaClBrowser::GetIrtFilePath() {
576 return irt_filepath_;
577}
578
579// This only ever runs on the BrowserThread::FILE thread.
580// If multiple tasks are posted, the later ones are no-ops.
581void NaClBrowser::OpenIrtLibraryFile() {
582 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
583 // We've already run.
584 return;
[email protected]88e15832011-07-19 01:18:24585
[email protected]773ebb92011-11-15 19:06:52586 base::PlatformFileError error_code;
[email protected]31a665e72012-03-11 12:37:46587 irt_platform_file_ = base::CreatePlatformFile(irt_filepath_,
[email protected]773ebb92011-11-15 19:06:52588 base::PLATFORM_FILE_OPEN |
589 base::PLATFORM_FILE_READ,
590 NULL,
591 &error_code);
592 if (error_code != base::PLATFORM_FILE_OK) {
593 LOG(ERROR) << "Failed to open NaCl IRT file \""
[email protected]31a665e72012-03-11 12:37:46594 << irt_filepath_.LossyDisplayName()
[email protected]773ebb92011-11-15 19:06:52595 << "\": " << error_code;
596 }
597}
598
599void NaClProcessHost::OnProcessLaunched() {
600 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
601
602 if (nacl_browser->IrtAvailable()) {
603 // The IRT is already open. Away we go.
604 SendStart(nacl_browser->IrtFile());
605 } else {
606 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43607 if (!nacl_browser->MakeIrtAvailable(
608 base::Bind(&NaClProcessHost::IrtReady,
609 weak_factory_.GetWeakPtr())))
610 delete this;
[email protected]773ebb92011-11-15 19:06:52611 }
612}
613
614// The asynchronous attempt to get the IRT file open has completed.
615void NaClProcessHost::IrtReady() {
616 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
617
618 if (nacl_browser->IrtAvailable()) {
619 SendStart(nacl_browser->IrtFile());
620 } else {
621 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43622 delete this;
623 }
624}
625
[email protected]b39c6d92012-01-31 16:38:41626#if defined(OS_WIN)
[email protected]b39c6d92012-01-31 16:38:41627void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]31a665e72012-03-11 12:37:46628 // Set process handle, if it was not set previously.
629 // This is needed when NaCl process is launched with nacl-gdb.
630 if (process_->GetData().handle == base::kNullProcessHandle) {
631 base::ProcessHandle process;
632 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
633 switches::kNaClGdb).empty());
634 if (base::OpenProcessHandleWithAccess(
635 peer_pid,
636 base::kProcessAccessDuplicateHandle |
[email protected]59261242012-03-19 13:08:59637 base::kProcessAccessQueryInformation |
[email protected]31a665e72012-03-11 12:37:46638 base::kProcessAccessWaitForTermination,
639 &process)) {
640 process_->SetHandle(process);
641 OnProcessLaunched();
642 } else {
643 LOG(ERROR) << "Failed to get process handle";
644 }
645 }
[email protected]5ca93be2012-03-21 20:04:06646 if (debug_context_ == NULL) {
[email protected]b39c6d92012-01-31 16:38:41647 return;
648 }
649 // Start new thread for debug loop
650 debug_context_->SetChildProcessHost(process_->GetHost());
651 // We can't use process_->GetData().handle because it doesn't have necessary
652 // access rights.
653 base::ProcessHandle process;
654 if (!base::OpenProcessHandleWithAccess(
655 peer_pid,
656 base::kProcessAccessQueryInformation |
657 base::kProcessAccessSuspendResume |
658 base::kProcessAccessTerminate |
659 base::kProcessAccessVMOperation |
660 base::kProcessAccessVMRead |
661 base::kProcessAccessVMWrite |
662 base::kProcessAccessWaitForTermination,
663 &process)) {
664 LOG(ERROR) << "Failed to open the process";
665 debug_context_->AllowAndSendStartMessage();
666 return;
667 }
668 base::Thread* dbg_thread = new base::Thread("Debug thread");
669 if (!dbg_thread->Start()) {
670 LOG(ERROR) << "Debug thread not started";
671 debug_context_->AllowAndSendStartMessage();
672 base::CloseProcessHandle(process);
673 return;
674 }
675 debug_context_->SetDebugThread(dbg_thread);
676 // System can not reallocate pid until we close process handle. So using
677 // pid in different thread is fine.
678 dbg_thread->message_loop()->PostTask(FROM_HERE,
679 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
[email protected]5ca93be2012-03-21 20:04:06680 debug_context_, peer_pid, process));
[email protected]b39c6d92012-01-31 16:38:41681}
682#else
683void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
684}
685#endif
686
687
[email protected]773ebb92011-11-15 19:06:52688static bool SendHandleToSelLdr(
689 base::ProcessHandle processh,
690 nacl::Handle sourceh, bool close_source,
691 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
692#if defined(OS_WIN)
693 HANDLE channel;
694 int flags = DUPLICATE_SAME_ACCESS;
695 if (close_source)
696 flags |= DUPLICATE_CLOSE_SOURCE;
697 if (!DuplicateHandle(GetCurrentProcess(),
698 reinterpret_cast<HANDLE>(sourceh),
699 processh,
700 &channel,
701 0, // Unused given DUPLICATE_SAME_ACCESS.
702 FALSE,
703 flags)) {
704 LOG(ERROR) << "DuplicateHandle() failed";
705 return false;
706 }
707 handles_for_sel_ldr->push_back(
708 reinterpret_cast<nacl::FileDescriptor>(channel));
709#else
710 nacl::FileDescriptor channel;
711 channel.fd = sourceh;
712 channel.auto_close = close_source;
713 handles_for_sel_ldr->push_back(channel);
714#endif
715 return true;
716}
717
718void NaClProcessHost::SendStart(base::PlatformFile irt_file) {
719 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
720
[email protected]c47ec402010-07-29 10:20:49721 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30722 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30723
[email protected]1d8a3d1f2011-02-19 07:11:52724 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49725#if defined(OS_WIN)
726 // Copy the handle into the renderer process.
727 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04728 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
729 reinterpret_cast<HANDLE>(
730 internal_->sockets_for_renderer[i]),
731 chrome_render_message_filter_->peer_handle(),
732 &handle_in_renderer,
733 0, // Unused given DUPLICATE_SAME_ACCESS.
734 FALSE,
735 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
736 LOG(ERROR) << "DuplicateHandle() failed";
737 delete this;
738 return;
739 }
[email protected]c47ec402010-07-29 10:20:49740 handles_for_renderer.push_back(
741 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
742#else
743 // No need to dup the imc_handle - we don't pass it anywhere else so
744 // it cannot be closed.
745 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52746 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49747 imc_handle.auto_close = true;
748 handles_for_renderer.push_back(imc_handle);
749#endif
750 }
751
[email protected]4967f792012-01-20 22:14:40752 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49753#if defined(OS_WIN)
754 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04755 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40756 data.handle,
[email protected]909c2402011-05-09 11:39:04757 chrome_render_message_filter_->peer_handle(),
758 &nacl_process_handle,
759 PROCESS_DUP_HANDLE,
760 FALSE,
761 0)) {
762 LOG(ERROR) << "DuplicateHandle() failed";
763 delete this;
764 return;
765 }
[email protected]fb1277e82009-11-21 20:32:30766#else
[email protected]fb1277e82009-11-21 20:32:30767 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40768 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30769#endif
770
771 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40772 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30773
[email protected]2ccf45c2011-08-19 23:35:50774 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49775 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52776 chrome_render_message_filter_->Send(reply_msg_);
777 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30778 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52779 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30780
[email protected]c47ec402010-07-29 10:20:49781 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52782 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40783 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52784 internal_->sockets_for_sel_ldr[i], true,
785 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04786 delete this;
[email protected]c47ec402010-07-29 10:20:49787 return;
788 }
[email protected]773ebb92011-11-15 19:06:52789 }
790
791 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40792 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52793 delete this;
794 return;
[email protected]c47ec402010-07-29 10:20:49795 }
796
[email protected]ab88d1542011-11-18 22:52:00797#if defined(OS_MACOSX)
798 // For dynamic loading support, NaCl requires a file descriptor that
799 // was created in /tmp, since those created with shm_open() are not
800 // mappable with PROT_EXEC. Rather than requiring an extra IPC
801 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25802 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31803 base::SharedMemoryCreateOptions options;
804 options.size = 1;
805 options.executable = true;
806 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30807 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04808 delete this;
[email protected]2c68bf032010-11-11 23:16:30809 return;
810 }
[email protected]cbbe7842011-11-17 22:01:25811 nacl::FileDescriptor memory_fd;
812 memory_fd.fd = dup(memory_buffer.handle().fd);
813 if (memory_fd.fd < 0) {
814 LOG(ERROR) << "Failed to dup() a file descriptor";
815 delete this;
816 return;
817 }
818 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30819 handles_for_sel_ldr.push_back(memory_fd);
820#endif
821
[email protected]5ca93be2012-03-21 20:04:06822 IPC::Message* start_message =
823 new NaClProcessMsg_Start(handles_for_sel_ldr, enable_exception_handling_);
[email protected]b39c6d92012-01-31 16:38:41824#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06825 if (debug_context_ != NULL) {
826 debug_context_->SetStartMessage(start_message);
[email protected]b39c6d92012-01-31 16:38:41827 debug_context_->SendStartMessage();
828 } else {
[email protected]5ca93be2012-03-21 20:04:06829 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41830 }
831#else
[email protected]5ca93be2012-03-21 20:04:06832 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41833#endif
834
[email protected]1d8a3d1f2011-02-19 07:11:52835 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46836}
837
[email protected]a95986a82010-12-24 06:19:28838bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]d032f492009-09-29 00:33:46839 NOTREACHED() << "Invalid message with type = " << msg.type();
[email protected]a95986a82010-12-24 06:19:28840 return false;
[email protected]d032f492009-09-29 00:33:46841}