blob: 65ce10745d76b4bcde39c1210145843497641927 [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]fb335fe2012-03-24 18:11:38261 debug_context_ = new DebugContext();
[email protected]b39c6d92012-01-31 16:38:41262#endif
[email protected]5ca93be2012-03-21 20:04:06263 }
[email protected]d032f492009-09-29 00:33:46264}
265
[email protected]fb1277e82009-11-21 20:32:30266NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49267 int exit_code;
[email protected]4967f792012-01-20 22:14:40268 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49269 std::string message =
270 base::StringPrintf("NaCl process exited with status %i (0x%x)",
271 exit_code, exit_code);
272 if (exit_code == 0) {
273 LOG(INFO) << message;
274 } else {
275 LOG(ERROR) << message;
276 }
277
[email protected]1d8a3d1f2011-02-19 07:11:52278 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04279 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
280 LOG(ERROR) << "nacl::Close() failed";
281 }
[email protected]c47ec402010-07-29 10:20:49282 }
[email protected]1d8a3d1f2011-02-19 07:11:52283 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04284 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
285 LOG(ERROR) << "nacl::Close() failed";
286 }
[email protected]c47ec402010-07-29 10:20:49287 }
288
[email protected]909c2402011-05-09 11:39:04289 if (reply_msg_) {
290 // The process failed to launch for some reason.
291 // Don't keep the renderer hanging.
292 reply_msg_->set_reply_error();
293 chrome_render_message_filter_->Send(reply_msg_);
294 }
[email protected]b39c6d92012-01-31 16:38:41295#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36296 if (process_launched_by_broker_) {
297 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06298 }
299 if (debug_context_ != NULL) {
[email protected]b39c6d92012-01-31 16:38:41300 debug_context_->SetChildProcessHost(NULL);
301 }
302#endif
[email protected]fb1277e82009-11-21 20:32:30303}
304
[email protected]773ebb92011-11-15 19:06:52305// Attempt to ensure the IRT will be available when we need it, but don't wait.
306bool NaClBrowser::EnsureIrtAvailable() {
307 if (IrtAvailable())
308 return true;
309
310 return BrowserThread::PostTask(
311 BrowserThread::FILE, FROM_HERE,
312 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
313}
314
315// We really need the IRT to be available now, so make sure that it is.
316// When it's ready, we'll run the reply closure.
317bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
318 return BrowserThread::PostTaskAndReply(
319 BrowserThread::FILE, FROM_HERE,
320 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
321}
322
323// This is called at browser startup.
324// static
325void NaClProcessHost::EarlyStartup() {
326#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
327 // Open the IRT file early to make sure that it isn't replaced out from
328 // under us by autoupdate.
329 NaClBrowser::GetInstance()->EnsureIrtAvailable();
330#endif
331}
332
[email protected]a575da52012-03-22 13:08:36333void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52334 ChromeRenderMessageFilter* chrome_render_message_filter,
335 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33336 IPC::Message* reply_msg,
337 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36338 chrome_render_message_filter_ = chrome_render_message_filter;
339 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33340 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36341
[email protected]c47ec402010-07-29 10:20:49342 // Place an arbitrary limit on the number of sockets to limit
343 // exposure in case the renderer is compromised. We can increase
344 // this if necessary.
345 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36346 delete this;
347 return;
[email protected]c47ec402010-07-29 10:20:49348 }
349
[email protected]773ebb92011-11-15 19:06:52350 // Start getting the IRT open asynchronously while we launch the NaCl process.
351 // We'll make sure this actually finished in OnProcessLaunched, below.
352 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
353 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36354 delete this;
355 return;
[email protected]773ebb92011-11-15 19:06:52356 }
357
[email protected]c47ec402010-07-29 10:20:49358 // Rather than creating a socket pair in the renderer, and passing
359 // one side through the browser to sel_ldr, socket pairs are created
360 // in the browser and then passed to the renderer and sel_ldr.
361 //
362 // This is mainly for the benefit of Windows, where sockets cannot
363 // be passed in messages, but are copied via DuplicateHandle().
364 // This means the sandboxed renderer cannot send handles to the
365 // browser process.
366
367 for (int i = 0; i < socket_count; i++) {
368 nacl::Handle pair[2];
369 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36370 if (nacl::SocketPair(pair) == -1) {
371 delete this;
372 return;
373 }
[email protected]1d8a3d1f2011-02-19 07:11:52374 internal_->sockets_for_renderer.push_back(pair[0]);
375 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49376 SetCloseOnExec(pair[0]);
377 SetCloseOnExec(pair[1]);
378 }
[email protected]d032f492009-09-29 00:33:46379
380 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30381 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36382 delete this;
[email protected]d032f492009-09-29 00:33:46383 }
[email protected]d032f492009-09-29 00:33:46384}
385
[email protected]8ae7bd6f2012-03-14 03:23:02386scoped_ptr<CommandLine> NaClProcessHost::LaunchWithNaClGdb(
[email protected]8f42b4d2012-03-24 14:12:33387 const FilePath& nacl_gdb,
388 CommandLine* line,
389 const FilePath& manifest_path) {
[email protected]31a665e72012-03-11 12:37:46390 CommandLine* cmd_line = new CommandLine(nacl_gdb);
391 // We can't use PrependWrapper because our parameters contain spaces.
392 cmd_line->AppendArg("--eval-command");
393 const FilePath::StringType& irt_path =
394 NaClBrowser::GetInstance()->GetIrtFilePath().value();
395 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]8f42b4d2012-03-24 14:12:33396 if (!manifest_path.empty()) {
397 cmd_line->AppendArg("--eval-command");
398 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
399 manifest_path.value());
400 }
[email protected]31a665e72012-03-11 12:37:46401 cmd_line->AppendArg("--args");
402 const CommandLine::StringVector& argv = line->argv();
403 for (size_t i = 0; i < argv.size(); i++) {
404 cmd_line->AppendArgNative(argv[i]);
405 }
406 return scoped_ptr<CommandLine>(cmd_line);
407}
408
[email protected]fb1277e82009-11-21 20:32:30409bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40410 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44411 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46412 return false;
413
[email protected]e3fc75a2011-05-05 08:20:42414 CommandLine::StringType nacl_loader_prefix;
415#if defined(OS_POSIX)
416 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
417 switches::kNaClLoaderCmdPrefix);
418#endif // defined(OS_POSIX)
419
[email protected]d032f492009-09-29 00:33:46420 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36421
422#if defined(OS_MACOSX)
423 // The Native Client process needs to be able to allocate a 1GB contiguous
424 // region to use as the client environment's virtual address space. ASLR
425 // (PIE) interferes with this by making it possible that no gap large enough
426 // to accomodate this request will exist in the child process' address
427 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
428 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49429 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36430#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49431 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
432 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36433#else
[email protected]4cb43102011-12-02 20:24:49434 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36435#endif
436
[email protected]4cb43102011-12-02 20:24:49437 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30438 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46439 return false;
440
[email protected]31a665e72012-03-11 12:37:46441#if defined(OS_WIN)
442 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
443 if (RunningOnWOW64()) {
444 FilePath module_path;
445 if (!PathService::Get(base::FILE_MODULE, &module_path))
446 return false;
447 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
448 }
449#endif
450
[email protected]33a05af2012-03-02 18:15:51451 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
452 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13453
[email protected]05076ba22010-07-30 05:59:57454 cmd_line->AppendSwitchASCII(switches::kProcessType,
455 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44456 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44457 if (logging::DialogsAreSuppressed())
458 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46459
[email protected]e3fc75a2011-05-05 08:20:42460 if (!nacl_loader_prefix.empty())
461 cmd_line->PrependWrapper(nacl_loader_prefix);
462
[email protected]31a665e72012-03-11 12:37:46463 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
464 switches::kNaClGdb);
465 if (!nacl_gdb.empty()) {
[email protected]8f42b4d2012-03-24 14:12:33466 GURL manifest_url = GURL(process_->GetData().name);
467 FilePath manifest_path;
468 const Extension* extension = extension_info_map_->extensions().
469 GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url));
470 if (extension != NULL && manifest_url.SchemeIs(chrome::kExtensionScheme)) {
471 std::string path = manifest_url.path();
472 TrimString(path, "/", &path); // Remove first slash
473 manifest_path = extension->path().AppendASCII(path);
474 }
[email protected]31a665e72012-03-11 12:37:46475 cmd_line->AppendSwitch(switches::kNoSandbox);
476 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]8f42b4d2012-03-24 14:12:33477 LaunchWithNaClGdb(nacl_gdb, cmd_line.get(), manifest_path));
[email protected]31a665e72012-03-11 12:37:46478 // We can't use process_->Launch() because OnProcessLaunched will be called
479 // with process_->GetData().handle filled by handle of gdb process. This
480 // handle will be used to duplicate handles for NaCl process and as
481 // a result NaCl process will not be able to use them.
482 //
483 // So we don't fill process_->GetData().handle and wait for
484 // OnChannelConnected to get handle of NaCl process from its pid. Then we
485 // call OnProcessLaunched.
486 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
487 }
488
[email protected]103607e2010-02-01 18:57:09489 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46490#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52491 if (RunningOnWOW64()) {
[email protected]1dbaaa702011-04-06 17:43:42492 return NaClBrokerService::GetInstance()->LaunchLoader(
[email protected]4734d0b2011-12-03 07:10:44493 this, ASCIIToWide(channel_id));
[email protected]4bdde602010-06-16 03:17:35494 } else {
[email protected]33a05af2012-03-02 18:15:51495 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35496 }
[email protected]103607e2010-02-01 18:57:09497#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40498 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20499 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51500 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09501#endif
[email protected]d032f492009-09-29 00:33:46502
[email protected]fb1277e82009-11-21 20:32:30503 return true;
[email protected]d032f492009-09-29 00:33:46504}
505
[email protected]a575da52012-03-22 13:08:36506#if defined(OS_WIN)
[email protected]fb335fe2012-03-24 18:11:38507void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
[email protected]a575da52012-03-22 13:08:36508 process_launched_by_broker_ = true;
[email protected]4967f792012-01-20 22:14:40509 process_->SetHandle(handle);
[email protected]103607e2010-02-01 18:57:09510 OnProcessLaunched();
511}
512
[email protected]fb335fe2012-03-24 18:11:38513void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker() {
514 debug_context_->AllowAndSendStartMessage();
515}
516#endif
517
[email protected]463ea5f2011-12-03 06:57:47518void NaClProcessHost::OnProcessCrashed(int exit_code) {
519 std::string message = base::StringPrintf(
520 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
521 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09522}
523
[email protected]75e0c502011-12-16 21:53:01524namespace {
525
526// Determine the name of the IRT file based on the architecture.
527
528#define NACL_IRT_FILE_NAME(arch_string) \
529 (FILE_PATH_LITERAL("nacl_irt_") \
530 FILE_PATH_LITERAL(arch_string) \
531 FILE_PATH_LITERAL(".nexe"))
532
533const FilePath::StringType NaClIrtName() {
534#if defined(ARCH_CPU_X86_FAMILY)
[email protected]75e0c502011-12-16 21:53:01535#if defined(ARCH_CPU_X86_64)
[email protected]5804cb5d2011-12-19 21:55:35536 bool is64 = true;
537#elif defined(OS_WIN)
538 bool is64 = RunningOnWOW64();
539#else
540 bool is64 = false;
[email protected]75e0c502011-12-16 21:53:01541#endif
542 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
543#elif defined(ARCH_CPU_ARMEL)
544 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
545 // That may need to be based on the actual nexe rather than a static
546 // choice, which would require substantial refactoring.
547 return NACL_IRT_FILE_NAME("arm");
548#else
549#error Add support for your architecture to NaCl IRT file selection
550#endif
551}
552
553} // namespace
554
[email protected]31a665e72012-03-11 12:37:46555void NaClBrowser::InitIrtFilePath() {
[email protected]88e15832011-07-19 01:18:24556 // Allow the IRT library to be overridden via an environment
557 // variable. This allows the NaCl/Chromium integration bot to
558 // specify a newly-built IRT rather than using a prebuilt one
559 // downloaded via Chromium's DEPS file. We use the same environment
560 // variable that the standalone NaCl PPAPI plugin accepts.
561 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
562 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52563 FilePath::StringType path_string(
564 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
[email protected]31a665e72012-03-11 12:37:46565 irt_filepath_ = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24566 } else {
567 FilePath plugin_dir;
568 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
569 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24570 return;
571 }
[email protected]773ebb92011-11-15 19:06:52572
[email protected]31a665e72012-03-11 12:37:46573 irt_filepath_ = plugin_dir.Append(NaClIrtName());
[email protected]338466a82011-05-03 04:27:43574 }
[email protected]31a665e72012-03-11 12:37:46575}
576
577const FilePath& NaClBrowser::GetIrtFilePath() {
578 return irt_filepath_;
579}
580
581// This only ever runs on the BrowserThread::FILE thread.
582// If multiple tasks are posted, the later ones are no-ops.
583void NaClBrowser::OpenIrtLibraryFile() {
584 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
585 // We've already run.
586 return;
[email protected]88e15832011-07-19 01:18:24587
[email protected]773ebb92011-11-15 19:06:52588 base::PlatformFileError error_code;
[email protected]31a665e72012-03-11 12:37:46589 irt_platform_file_ = base::CreatePlatformFile(irt_filepath_,
[email protected]773ebb92011-11-15 19:06:52590 base::PLATFORM_FILE_OPEN |
591 base::PLATFORM_FILE_READ,
592 NULL,
593 &error_code);
594 if (error_code != base::PLATFORM_FILE_OK) {
595 LOG(ERROR) << "Failed to open NaCl IRT file \""
[email protected]31a665e72012-03-11 12:37:46596 << irt_filepath_.LossyDisplayName()
[email protected]773ebb92011-11-15 19:06:52597 << "\": " << error_code;
598 }
599}
600
601void NaClProcessHost::OnProcessLaunched() {
602 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
603
604 if (nacl_browser->IrtAvailable()) {
605 // The IRT is already open. Away we go.
606 SendStart(nacl_browser->IrtFile());
607 } else {
608 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43609 if (!nacl_browser->MakeIrtAvailable(
610 base::Bind(&NaClProcessHost::IrtReady,
611 weak_factory_.GetWeakPtr())))
612 delete this;
[email protected]773ebb92011-11-15 19:06:52613 }
614}
615
616// The asynchronous attempt to get the IRT file open has completed.
617void NaClProcessHost::IrtReady() {
618 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
619
620 if (nacl_browser->IrtAvailable()) {
621 SendStart(nacl_browser->IrtFile());
622 } else {
623 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43624 delete this;
625 }
626}
627
[email protected]b39c6d92012-01-31 16:38:41628#if defined(OS_WIN)
[email protected]b39c6d92012-01-31 16:38:41629void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]31a665e72012-03-11 12:37:46630 // Set process handle, if it was not set previously.
631 // This is needed when NaCl process is launched with nacl-gdb.
632 if (process_->GetData().handle == base::kNullProcessHandle) {
633 base::ProcessHandle process;
634 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
635 switches::kNaClGdb).empty());
636 if (base::OpenProcessHandleWithAccess(
637 peer_pid,
638 base::kProcessAccessDuplicateHandle |
[email protected]59261242012-03-19 13:08:59639 base::kProcessAccessQueryInformation |
[email protected]31a665e72012-03-11 12:37:46640 base::kProcessAccessWaitForTermination,
641 &process)) {
642 process_->SetHandle(process);
643 OnProcessLaunched();
644 } else {
645 LOG(ERROR) << "Failed to get process handle";
646 }
647 }
[email protected]5ca93be2012-03-21 20:04:06648 if (debug_context_ == NULL) {
[email protected]b39c6d92012-01-31 16:38:41649 return;
650 }
[email protected]b39c6d92012-01-31 16:38:41651 debug_context_->SetChildProcessHost(process_->GetHost());
[email protected]fb335fe2012-03-24 18:11:38652 if (RunningOnWOW64()) {
653 if (!NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
654 this, peer_pid)) {
655 debug_context_->AllowAndSendStartMessage();
656 }
657 } else {
658 // Start new thread for debug loop
659 // We can't use process_->GetData().handle because it doesn't have necessary
660 // access rights.
661 base::ProcessHandle process;
662 if (!base::OpenProcessHandleWithAccess(
663 peer_pid,
664 base::kProcessAccessQueryInformation |
665 base::kProcessAccessSuspendResume |
666 base::kProcessAccessTerminate |
667 base::kProcessAccessVMOperation |
668 base::kProcessAccessVMRead |
669 base::kProcessAccessVMWrite |
670 base::kProcessAccessWaitForTermination,
671 &process)) {
672 LOG(ERROR) << "Failed to open the process";
673 debug_context_->AllowAndSendStartMessage();
674 return;
675 }
676 base::Thread* dbg_thread = new base::Thread("Debug thread");
677 if (!dbg_thread->Start()) {
678 LOG(ERROR) << "Debug thread not started";
679 debug_context_->AllowAndSendStartMessage();
680 base::CloseProcessHandle(process);
681 return;
682 }
683 debug_context_->SetDebugThread(dbg_thread);
684 // System can not reallocate pid until we close process handle. So using
685 // pid in different thread is fine.
686 dbg_thread->message_loop()->PostTask(FROM_HERE,
687 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
688 debug_context_, peer_pid, process));
[email protected]b39c6d92012-01-31 16:38:41689 }
[email protected]b39c6d92012-01-31 16:38:41690}
691#else
692void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
693}
694#endif
695
696
[email protected]773ebb92011-11-15 19:06:52697static bool SendHandleToSelLdr(
698 base::ProcessHandle processh,
699 nacl::Handle sourceh, bool close_source,
700 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
701#if defined(OS_WIN)
702 HANDLE channel;
703 int flags = DUPLICATE_SAME_ACCESS;
704 if (close_source)
705 flags |= DUPLICATE_CLOSE_SOURCE;
706 if (!DuplicateHandle(GetCurrentProcess(),
707 reinterpret_cast<HANDLE>(sourceh),
708 processh,
709 &channel,
710 0, // Unused given DUPLICATE_SAME_ACCESS.
711 FALSE,
712 flags)) {
713 LOG(ERROR) << "DuplicateHandle() failed";
714 return false;
715 }
716 handles_for_sel_ldr->push_back(
717 reinterpret_cast<nacl::FileDescriptor>(channel));
718#else
719 nacl::FileDescriptor channel;
720 channel.fd = sourceh;
721 channel.auto_close = close_source;
722 handles_for_sel_ldr->push_back(channel);
723#endif
724 return true;
725}
726
727void NaClProcessHost::SendStart(base::PlatformFile irt_file) {
728 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
729
[email protected]c47ec402010-07-29 10:20:49730 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30731 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30732
[email protected]1d8a3d1f2011-02-19 07:11:52733 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49734#if defined(OS_WIN)
735 // Copy the handle into the renderer process.
736 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04737 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
738 reinterpret_cast<HANDLE>(
739 internal_->sockets_for_renderer[i]),
740 chrome_render_message_filter_->peer_handle(),
741 &handle_in_renderer,
742 0, // Unused given DUPLICATE_SAME_ACCESS.
743 FALSE,
744 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
745 LOG(ERROR) << "DuplicateHandle() failed";
746 delete this;
747 return;
748 }
[email protected]c47ec402010-07-29 10:20:49749 handles_for_renderer.push_back(
750 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
751#else
752 // No need to dup the imc_handle - we don't pass it anywhere else so
753 // it cannot be closed.
754 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52755 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49756 imc_handle.auto_close = true;
757 handles_for_renderer.push_back(imc_handle);
758#endif
759 }
760
[email protected]4967f792012-01-20 22:14:40761 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49762#if defined(OS_WIN)
763 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04764 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40765 data.handle,
[email protected]909c2402011-05-09 11:39:04766 chrome_render_message_filter_->peer_handle(),
767 &nacl_process_handle,
768 PROCESS_DUP_HANDLE,
769 FALSE,
770 0)) {
771 LOG(ERROR) << "DuplicateHandle() failed";
772 delete this;
773 return;
774 }
[email protected]fb1277e82009-11-21 20:32:30775#else
[email protected]fb1277e82009-11-21 20:32:30776 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40777 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30778#endif
779
780 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40781 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30782
[email protected]2ccf45c2011-08-19 23:35:50783 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49784 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52785 chrome_render_message_filter_->Send(reply_msg_);
786 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30787 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52788 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30789
[email protected]c47ec402010-07-29 10:20:49790 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52791 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40792 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52793 internal_->sockets_for_sel_ldr[i], true,
794 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04795 delete this;
[email protected]c47ec402010-07-29 10:20:49796 return;
797 }
[email protected]773ebb92011-11-15 19:06:52798 }
799
800 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40801 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52802 delete this;
803 return;
[email protected]c47ec402010-07-29 10:20:49804 }
805
[email protected]ab88d1542011-11-18 22:52:00806#if defined(OS_MACOSX)
807 // For dynamic loading support, NaCl requires a file descriptor that
808 // was created in /tmp, since those created with shm_open() are not
809 // mappable with PROT_EXEC. Rather than requiring an extra IPC
810 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25811 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31812 base::SharedMemoryCreateOptions options;
813 options.size = 1;
814 options.executable = true;
815 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30816 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04817 delete this;
[email protected]2c68bf032010-11-11 23:16:30818 return;
819 }
[email protected]cbbe7842011-11-17 22:01:25820 nacl::FileDescriptor memory_fd;
821 memory_fd.fd = dup(memory_buffer.handle().fd);
822 if (memory_fd.fd < 0) {
823 LOG(ERROR) << "Failed to dup() a file descriptor";
824 delete this;
825 return;
826 }
827 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30828 handles_for_sel_ldr.push_back(memory_fd);
829#endif
830
[email protected]5ca93be2012-03-21 20:04:06831 IPC::Message* start_message =
832 new NaClProcessMsg_Start(handles_for_sel_ldr, enable_exception_handling_);
[email protected]b39c6d92012-01-31 16:38:41833#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06834 if (debug_context_ != NULL) {
835 debug_context_->SetStartMessage(start_message);
[email protected]b39c6d92012-01-31 16:38:41836 debug_context_->SendStartMessage();
837 } else {
[email protected]5ca93be2012-03-21 20:04:06838 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41839 }
840#else
[email protected]5ca93be2012-03-21 20:04:06841 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41842#endif
843
[email protected]1d8a3d1f2011-02-19 07:11:52844 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46845}
846
[email protected]a95986a82010-12-24 06:19:28847bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]d032f492009-09-29 00:33:46848 NOTREACHED() << "Invalid message with type = " << msg.type();
[email protected]a95986a82010-12-24 06:19:28849 return false;
[email protected]d032f492009-09-29 00:33:46850}