blob: baa690458271f2dd9e78db4c694332b8283dba33 [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]a0a69bf2011-09-23 21:40:2814#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2215#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3716#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2017#include "build/build_config.h"
[email protected]338466a82011-05-03 04:27:4318#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4619#include "chrome/common/chrome_switches.h"
20#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0921#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4622#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3023#include "chrome/common/render_messages.h"
[email protected]92d56412011-03-24 20:53:5224#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]4967f792012-01-20 22:14:4025#include "content/public/browser/browser_child_process_host.h"
26#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4427#include "content/public/common/child_process_host.h"
[email protected]d032f492009-09-29 00:33:4628#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5229#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]d032f492009-09-29 00:33:4630
[email protected]d032f492009-09-29 00:33:4631#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2032#include <fcntl.h>
33
[email protected]d032f492009-09-29 00:33:4634#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3535#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2036#include <windows.h>
37
[email protected]b39c6d92012-01-31 16:38:4138#include "base/threading/thread.h"
39#include "base/process_util.h"
[email protected]4bdde602010-06-16 03:17:3540#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]b39c6d92012-01-31 16:38:4141#include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h"
[email protected]d032f492009-09-29 00:33:4642#endif
43
[email protected]631bb742011-11-02 11:29:3944using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4045using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4446using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3947
[email protected]b39c6d92012-01-31 16:38:4148#if defined(OS_WIN)
49class NaClProcessHost::DebugContext
50 : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> {
51 public:
52 DebugContext()
53 : can_send_start_msg_(false),
54 child_process_host_(NULL) {
55 }
56
57 ~DebugContext() {
58 }
59
60 void AttachDebugger(int pid, base::ProcessHandle process);
61
62 // 6 methods below must be called on Browser::IO thread.
63 void SetStartMessage(IPC::Message* start_msg);
64 void SetChildProcessHost(content::ChildProcessHost* child_process_host);
65 void SetDebugThread(base::Thread* thread_);
66
67 // Start message is sent from 2 flows of execution. The first flow is
68 // NaClProcessHost::SendStart. The second flow is
69 // NaClProcessHost::OnChannelConnected and
70 // NaClProcessHost::DebugContext::AttachThread. The message itself is created
71 // by first flow. But the moment it can be sent is determined by second flow.
72 // So first flow executes SetStartMessage and SendStartMessage while second
73 // flow uses AllowAndSendStartMessage to either send potentially pending
74 // start message or set the flag that allows the first flow to do this.
75
76 // Clears the flag that prevents sending start message.
77 void AllowToSendStartMsg();
78 // Send start message to the NaCl process or do nothing if message is not
79 // set or not allowed to be send. If message is sent, it is cleared and
80 // repeated calls do nothing.
81 void SendStartMessage();
82 // Clear the flag that prevents further sending start message and send start
83 // message if it is set.
84 void AllowAndSendStartMessage();
85 private:
86 void StopThread();
87 // These 4 fields are accessed only from Browser::IO thread.
88 scoped_ptr<base::Thread> thread_;
89 scoped_ptr<IPC::Message> start_msg_;
90 // Debugger is attached or exception handling is not switched on.
91 // This means that start message can be sent to the NaCl process.
92 bool can_send_start_msg_;
93 content::ChildProcessHost* child_process_host_;
94};
95
96void NaClProcessHost::DebugContext::AttachDebugger(
97 int pid, base::ProcessHandle process) {
98 BOOL attached;
99 DWORD exit_code;
100 attached = DebugActiveProcess(pid);
101 if (!attached) {
102 LOG(ERROR) << "Failed to connect to the process";
103 }
104 BrowserThread::PostTask(
105 BrowserThread::IO, FROM_HERE,
106 base::Bind(
107 &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this));
108 if (attached) {
109 // debug the process
110 NaClDebugLoop(process, &exit_code);
111 base::CloseProcessHandle(process);
112 }
113 BrowserThread::PostTask(
114 BrowserThread::IO, FROM_HERE,
115 base::Bind(&NaClProcessHost::DebugContext::StopThread, this));
116}
117
118void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) {
119 start_msg_.reset(start_msg);
120}
121
122void NaClProcessHost::DebugContext::SetChildProcessHost(
123 content::ChildProcessHost* child_process_host) {
124 child_process_host_ = child_process_host;
125}
126
127void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) {
128 thread_.reset(thread);
129}
130
131void NaClProcessHost::DebugContext::AllowToSendStartMsg() {
132 can_send_start_msg_ = true;
133}
134
135void NaClProcessHost::DebugContext::SendStartMessage() {
136 if (start_msg_.get() && can_send_start_msg_) {
137 if (child_process_host_) {
138 if (!child_process_host_->Send(start_msg_.release())) {
139 LOG(ERROR) << "Failed to send start message";
140 }
141 }
142 }
143}
144
145void NaClProcessHost::DebugContext::AllowAndSendStartMessage() {
146 AllowToSendStartMsg();
147 SendStartMessage();
148}
149
150void NaClProcessHost::DebugContext::StopThread() {
151 thread_->Stop();
152 thread_.reset();
153}
154#endif
155
[email protected]c47ec402010-07-29 10:20:49156namespace {
157
158void SetCloseOnExec(nacl::Handle fd) {
159#if defined(OS_POSIX)
160 int flags = fcntl(fd, F_GETFD);
[email protected]773ebb92011-11-15 19:06:52161 CHECK_NE(flags, -1);
[email protected]c47ec402010-07-29 10:20:49162 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
[email protected]773ebb92011-11-15 19:06:52163 CHECK_EQ(rc, 0);
[email protected]c47ec402010-07-29 10:20:49164#endif
165}
166
[email protected]773ebb92011-11-15 19:06:52167// Represents shared state for all NaClProcessHost objects in the browser.
168// Currently this just handles holding onto the file descriptor for the IRT.
169class NaClBrowser {
170 public:
171 static NaClBrowser* GetInstance() {
172 return Singleton<NaClBrowser>::get();
173 }
174
175 bool IrtAvailable() const {
176 return irt_platform_file_ != base::kInvalidPlatformFileValue;
177 }
178
179 base::PlatformFile IrtFile() const {
180 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
181 return irt_platform_file_;
182 }
183
184 // Asynchronously attempt to get the IRT open.
185 bool EnsureIrtAvailable();
186
187 // Make sure the IRT gets opened and follow up with the reply when it's ready.
188 bool MakeIrtAvailable(const base::Closure& reply);
189
190 private:
191 base::PlatformFile irt_platform_file_;
192
193 friend struct DefaultSingletonTraits<NaClBrowser>;
194
195 NaClBrowser()
196 : irt_platform_file_(base::kInvalidPlatformFileValue)
197 {}
198
199 ~NaClBrowser() {
200 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
201 base::ClosePlatformFile(irt_platform_file_);
202 }
203
204 void OpenIrtLibraryFile();
205
206 static void DoOpenIrtLibraryFile() {
207 GetInstance()->OpenIrtLibraryFile();
208 }
209
210 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
211};
212
[email protected]c47ec402010-07-29 10:20:49213} // namespace
214
[email protected]1d8a3d1f2011-02-19 07:11:52215struct NaClProcessHost::NaClInternal {
216 std::vector<nacl::Handle> sockets_for_renderer;
217 std::vector<nacl::Handle> sockets_for_sel_ldr;
218};
219
[email protected]773ebb92011-11-15 19:06:52220#if defined(OS_WIN)
[email protected]5804cb5d2011-12-19 21:55:35221static bool RunningOnWOW64() {
[email protected]773ebb92011-11-15 19:06:52222 return (base::win::OSInfo::GetInstance()->wow64_status() ==
223 base::win::OSInfo::WOW64_ENABLED);
[email protected]773ebb92011-11-15 19:06:52224}
[email protected]5804cb5d2011-12-19 21:55:35225#endif
[email protected]773ebb92011-11-15 19:06:52226
[email protected]1dbaaa702011-04-06 17:43:42227NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]4967f792012-01-20 22:14:40228 : reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52229 internal_(new NaClInternal()),
[email protected]30c1eea2011-10-17 18:40:30230 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]4967f792012-01-20 22:14:40231 process_.reset(content::BrowserChildProcessHost::Create(
232 content::PROCESS_TYPE_NACL_LOADER, this));
233 process_->SetName(WideToUTF16Hack(url));
[email protected]b39c6d92012-01-31 16:38:41234#if defined(OS_WIN)
235 if (!RunningOnWOW64()) {
236 debug_context_ = new DebugContext();
237 }
238#endif
[email protected]d032f492009-09-29 00:33:46239}
240
[email protected]fb1277e82009-11-21 20:32:30241NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49242 int exit_code;
[email protected]4967f792012-01-20 22:14:40243 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49244 std::string message =
245 base::StringPrintf("NaCl process exited with status %i (0x%x)",
246 exit_code, exit_code);
247 if (exit_code == 0) {
248 LOG(INFO) << message;
249 } else {
250 LOG(ERROR) << message;
251 }
252
253#if defined(OS_WIN)
254 NaClBrokerService::GetInstance()->OnLoaderDied();
255#endif
256
[email protected]1d8a3d1f2011-02-19 07:11:52257 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04258 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
259 LOG(ERROR) << "nacl::Close() failed";
260 }
[email protected]c47ec402010-07-29 10:20:49261 }
[email protected]1d8a3d1f2011-02-19 07:11:52262 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04263 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
264 LOG(ERROR) << "nacl::Close() failed";
265 }
[email protected]c47ec402010-07-29 10:20:49266 }
267
[email protected]909c2402011-05-09 11:39:04268 if (reply_msg_) {
269 // The process failed to launch for some reason.
270 // Don't keep the renderer hanging.
271 reply_msg_->set_reply_error();
272 chrome_render_message_filter_->Send(reply_msg_);
273 }
[email protected]b39c6d92012-01-31 16:38:41274#if defined(OS_WIN)
275 if (!RunningOnWOW64()) {
276 debug_context_->SetChildProcessHost(NULL);
277 }
278#endif
[email protected]463ea5f2011-12-03 06:57:47279
280#if defined(OS_WIN)
281 NaClBrokerService::GetInstance()->OnLoaderDied();
282#endif
[email protected]fb1277e82009-11-21 20:32:30283}
284
[email protected]773ebb92011-11-15 19:06:52285// Attempt to ensure the IRT will be available when we need it, but don't wait.
286bool NaClBrowser::EnsureIrtAvailable() {
287 if (IrtAvailable())
288 return true;
289
290 return BrowserThread::PostTask(
291 BrowserThread::FILE, FROM_HERE,
292 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
293}
294
295// We really need the IRT to be available now, so make sure that it is.
296// When it's ready, we'll run the reply closure.
297bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
298 return BrowserThread::PostTaskAndReply(
299 BrowserThread::FILE, FROM_HERE,
300 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
301}
302
303// This is called at browser startup.
304// static
305void NaClProcessHost::EarlyStartup() {
306#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
307 // Open the IRT file early to make sure that it isn't replaced out from
308 // under us by autoupdate.
309 NaClBrowser::GetInstance()->EnsureIrtAvailable();
310#endif
311}
312
[email protected]92d56412011-03-24 20:53:52313bool NaClProcessHost::Launch(
314 ChromeRenderMessageFilter* chrome_render_message_filter,
315 int socket_count,
316 IPC::Message* reply_msg) {
[email protected]c47ec402010-07-29 10:20:49317 // Place an arbitrary limit on the number of sockets to limit
318 // exposure in case the renderer is compromised. We can increase
319 // this if necessary.
320 if (socket_count > 8) {
[email protected]d032f492009-09-29 00:33:46321 return false;
[email protected]c47ec402010-07-29 10:20:49322 }
323
[email protected]773ebb92011-11-15 19:06:52324 // Start getting the IRT open asynchronously while we launch the NaCl process.
325 // We'll make sure this actually finished in OnProcessLaunched, below.
326 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
327 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
328 return false;
329 }
330
[email protected]c47ec402010-07-29 10:20:49331 // Rather than creating a socket pair in the renderer, and passing
332 // one side through the browser to sel_ldr, socket pairs are created
333 // in the browser and then passed to the renderer and sel_ldr.
334 //
335 // This is mainly for the benefit of Windows, where sockets cannot
336 // be passed in messages, but are copied via DuplicateHandle().
337 // This means the sandboxed renderer cannot send handles to the
338 // browser process.
339
340 for (int i = 0; i < socket_count; i++) {
341 nacl::Handle pair[2];
342 // Create a connected socket
343 if (nacl::SocketPair(pair) == -1)
344 return false;
[email protected]1d8a3d1f2011-02-19 07:11:52345 internal_->sockets_for_renderer.push_back(pair[0]);
346 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49347 SetCloseOnExec(pair[0]);
348 SetCloseOnExec(pair[1]);
349 }
[email protected]d032f492009-09-29 00:33:46350
351 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30352 if (!LaunchSelLdr()) {
[email protected]d032f492009-09-29 00:33:46353 return false;
354 }
[email protected]f08e47d2009-10-15 21:46:15355
[email protected]98999e802011-12-21 21:54:43356 chrome_render_message_filter_ = chrome_render_message_filter;
357
358 // On success, we take responsibility for sending the reply.
359 reply_msg_ = reply_msg;
[email protected]d032f492009-09-29 00:33:46360 return true;
361}
362
[email protected]fb1277e82009-11-21 20:32:30363bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40364 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44365 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46366 return false;
367
[email protected]e3fc75a2011-05-05 08:20:42368 CommandLine::StringType nacl_loader_prefix;
369#if defined(OS_POSIX)
370 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
371 switches::kNaClLoaderCmdPrefix);
372#endif // defined(OS_POSIX)
373
[email protected]d032f492009-09-29 00:33:46374 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36375
376#if defined(OS_MACOSX)
377 // The Native Client process needs to be able to allocate a 1GB contiguous
378 // region to use as the client environment's virtual address space. ASLR
379 // (PIE) interferes with this by making it possible that no gap large enough
380 // to accomodate this request will exist in the child process' address
381 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
382 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49383 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36384#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49385 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
386 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36387#else
[email protected]4cb43102011-12-02 20:24:49388 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36389#endif
390
[email protected]4cb43102011-12-02 20:24:49391 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30392 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46393 return false;
394
[email protected]662a0172011-12-21 04:18:19395 CommandLine* cmd_line = new CommandLine(exe_path);
396 nacl::CopyNaClCommandLineArguments(cmd_line);
[email protected]599e6642010-01-27 18:52:13397
[email protected]05076ba22010-07-30 05:59:57398 cmd_line->AppendSwitchASCII(switches::kProcessType,
399 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44400 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44401 if (logging::DialogsAreSuppressed())
402 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46403
[email protected]e3fc75a2011-05-05 08:20:42404 if (!nacl_loader_prefix.empty())
405 cmd_line->PrependWrapper(nacl_loader_prefix);
406
[email protected]103607e2010-02-01 18:57:09407 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46408#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52409 if (RunningOnWOW64()) {
[email protected]1dbaaa702011-04-06 17:43:42410 return NaClBrokerService::GetInstance()->LaunchLoader(
[email protected]4734d0b2011-12-03 07:10:44411 this, ASCIIToWide(channel_id));
[email protected]4bdde602010-06-16 03:17:35412 } else {
[email protected]4967f792012-01-20 22:14:40413 process_->Launch(FilePath(), cmd_line);
[email protected]4bdde602010-06-16 03:17:35414 }
[email protected]103607e2010-02-01 18:57:09415#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40416 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20417 base::EnvironmentVector(),
[email protected]4967f792012-01-20 22:14:40418 cmd_line);
[email protected]103607e2010-02-01 18:57:09419#endif
[email protected]d032f492009-09-29 00:33:46420
[email protected]fb1277e82009-11-21 20:32:30421 return true;
[email protected]d032f492009-09-29 00:33:46422}
423
[email protected]103607e2010-02-01 18:57:09424void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
[email protected]4967f792012-01-20 22:14:40425 process_->SetHandle(handle);
[email protected]103607e2010-02-01 18:57:09426 OnProcessLaunched();
427}
428
[email protected]463ea5f2011-12-03 06:57:47429void NaClProcessHost::OnProcessCrashed(int exit_code) {
430 std::string message = base::StringPrintf(
431 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
432 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09433}
434
[email protected]75e0c502011-12-16 21:53:01435namespace {
436
437// Determine the name of the IRT file based on the architecture.
438
439#define NACL_IRT_FILE_NAME(arch_string) \
440 (FILE_PATH_LITERAL("nacl_irt_") \
441 FILE_PATH_LITERAL(arch_string) \
442 FILE_PATH_LITERAL(".nexe"))
443
444const FilePath::StringType NaClIrtName() {
445#if defined(ARCH_CPU_X86_FAMILY)
[email protected]75e0c502011-12-16 21:53:01446#if defined(ARCH_CPU_X86_64)
[email protected]5804cb5d2011-12-19 21:55:35447 bool is64 = true;
448#elif defined(OS_WIN)
449 bool is64 = RunningOnWOW64();
450#else
451 bool is64 = false;
[email protected]75e0c502011-12-16 21:53:01452#endif
453 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
454#elif defined(ARCH_CPU_ARMEL)
455 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
456 // That may need to be based on the actual nexe rather than a static
457 // choice, which would require substantial refactoring.
458 return NACL_IRT_FILE_NAME("arm");
459#else
460#error Add support for your architecture to NaCl IRT file selection
461#endif
462}
463
464} // namespace
465
[email protected]773ebb92011-11-15 19:06:52466// This only ever runs on the BrowserThread::FILE thread.
467// If multiple tasks are posted, the later ones are no-ops.
468void NaClBrowser::OpenIrtLibraryFile() {
469 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
470 // We've already run.
471 return;
[email protected]338466a82011-05-03 04:27:43472
[email protected]773ebb92011-11-15 19:06:52473 FilePath irt_filepath;
474
[email protected]88e15832011-07-19 01:18:24475 // Allow the IRT library to be overridden via an environment
476 // variable. This allows the NaCl/Chromium integration bot to
477 // specify a newly-built IRT rather than using a prebuilt one
478 // downloaded via Chromium's DEPS file. We use the same environment
479 // variable that the standalone NaCl PPAPI plugin accepts.
480 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
481 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52482 FilePath::StringType path_string(
483 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
484 irt_filepath = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24485 } else {
486 FilePath plugin_dir;
487 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
488 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24489 return;
490 }
[email protected]773ebb92011-11-15 19:06:52491
[email protected]75e0c502011-12-16 21:53:01492 irt_filepath = plugin_dir.Append(NaClIrtName());
[email protected]338466a82011-05-03 04:27:43493 }
[email protected]88e15832011-07-19 01:18:24494
[email protected]773ebb92011-11-15 19:06:52495 base::PlatformFileError error_code;
496 irt_platform_file_ = base::CreatePlatformFile(irt_filepath,
497 base::PLATFORM_FILE_OPEN |
498 base::PLATFORM_FILE_READ,
499 NULL,
500 &error_code);
501 if (error_code != base::PLATFORM_FILE_OK) {
502 LOG(ERROR) << "Failed to open NaCl IRT file \""
503 << irt_filepath.LossyDisplayName()
504 << "\": " << error_code;
505 }
506}
507
508void NaClProcessHost::OnProcessLaunched() {
509 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
510
511 if (nacl_browser->IrtAvailable()) {
512 // The IRT is already open. Away we go.
513 SendStart(nacl_browser->IrtFile());
514 } else {
515 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43516 if (!nacl_browser->MakeIrtAvailable(
517 base::Bind(&NaClProcessHost::IrtReady,
518 weak_factory_.GetWeakPtr())))
519 delete this;
[email protected]773ebb92011-11-15 19:06:52520 }
521}
522
523// The asynchronous attempt to get the IRT file open has completed.
524void NaClProcessHost::IrtReady() {
525 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
526
527 if (nacl_browser->IrtAvailable()) {
528 SendStart(nacl_browser->IrtFile());
529 } else {
530 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43531 delete this;
532 }
533}
534
[email protected]b39c6d92012-01-31 16:38:41535#if defined(OS_WIN)
536bool NaClProcessHost::IsHardwareExceptionHandlingEnabled() {
537 return CommandLine::ForCurrentProcess()->HasSwitch(
538 switches::kEnableNaClExceptionHandling) && !RunningOnWOW64();
539}
540
541void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
542 if (!IsHardwareExceptionHandlingEnabled()) {
543 return;
544 }
545 // Start new thread for debug loop
546 debug_context_->SetChildProcessHost(process_->GetHost());
547 // We can't use process_->GetData().handle because it doesn't have necessary
548 // access rights.
549 base::ProcessHandle process;
550 if (!base::OpenProcessHandleWithAccess(
551 peer_pid,
552 base::kProcessAccessQueryInformation |
553 base::kProcessAccessSuspendResume |
554 base::kProcessAccessTerminate |
555 base::kProcessAccessVMOperation |
556 base::kProcessAccessVMRead |
557 base::kProcessAccessVMWrite |
558 base::kProcessAccessWaitForTermination,
559 &process)) {
560 LOG(ERROR) << "Failed to open the process";
561 debug_context_->AllowAndSendStartMessage();
562 return;
563 }
564 base::Thread* dbg_thread = new base::Thread("Debug thread");
565 if (!dbg_thread->Start()) {
566 LOG(ERROR) << "Debug thread not started";
567 debug_context_->AllowAndSendStartMessage();
568 base::CloseProcessHandle(process);
569 return;
570 }
571 debug_context_->SetDebugThread(dbg_thread);
572 // System can not reallocate pid until we close process handle. So using
573 // pid in different thread is fine.
574 dbg_thread->message_loop()->PostTask(FROM_HERE,
575 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
576 debug_context_, peer_pid, process));
577}
578#else
579void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
580}
581#endif
582
583
[email protected]773ebb92011-11-15 19:06:52584static bool SendHandleToSelLdr(
585 base::ProcessHandle processh,
586 nacl::Handle sourceh, bool close_source,
587 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
588#if defined(OS_WIN)
589 HANDLE channel;
590 int flags = DUPLICATE_SAME_ACCESS;
591 if (close_source)
592 flags |= DUPLICATE_CLOSE_SOURCE;
593 if (!DuplicateHandle(GetCurrentProcess(),
594 reinterpret_cast<HANDLE>(sourceh),
595 processh,
596 &channel,
597 0, // Unused given DUPLICATE_SAME_ACCESS.
598 FALSE,
599 flags)) {
600 LOG(ERROR) << "DuplicateHandle() failed";
601 return false;
602 }
603 handles_for_sel_ldr->push_back(
604 reinterpret_cast<nacl::FileDescriptor>(channel));
605#else
606 nacl::FileDescriptor channel;
607 channel.fd = sourceh;
608 channel.auto_close = close_source;
609 handles_for_sel_ldr->push_back(channel);
610#endif
611 return true;
612}
613
614void NaClProcessHost::SendStart(base::PlatformFile irt_file) {
615 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
616
[email protected]c47ec402010-07-29 10:20:49617 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30618 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30619
[email protected]1d8a3d1f2011-02-19 07:11:52620 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49621#if defined(OS_WIN)
622 // Copy the handle into the renderer process.
623 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04624 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
625 reinterpret_cast<HANDLE>(
626 internal_->sockets_for_renderer[i]),
627 chrome_render_message_filter_->peer_handle(),
628 &handle_in_renderer,
629 0, // Unused given DUPLICATE_SAME_ACCESS.
630 FALSE,
631 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
632 LOG(ERROR) << "DuplicateHandle() failed";
633 delete this;
634 return;
635 }
[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
[email protected]4967f792012-01-20 22:14:40648 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49649#if defined(OS_WIN)
650 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04651 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40652 data.handle,
[email protected]909c2402011-05-09 11:39:04653 chrome_render_message_filter_->peer_handle(),
654 &nacl_process_handle,
655 PROCESS_DUP_HANDLE,
656 FALSE,
657 0)) {
658 LOG(ERROR) << "DuplicateHandle() failed";
659 delete this;
660 return;
661 }
[email protected]fb1277e82009-11-21 20:32:30662#else
[email protected]fb1277e82009-11-21 20:32:30663 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40664 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30665#endif
666
667 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40668 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30669
[email protected]2ccf45c2011-08-19 23:35:50670 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49671 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52672 chrome_render_message_filter_->Send(reply_msg_);
673 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30674 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52675 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30676
[email protected]c47ec402010-07-29 10:20:49677 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52678 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40679 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52680 internal_->sockets_for_sel_ldr[i], true,
681 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04682 delete this;
[email protected]c47ec402010-07-29 10:20:49683 return;
684 }
[email protected]773ebb92011-11-15 19:06:52685 }
686
687 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40688 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52689 delete this;
690 return;
[email protected]c47ec402010-07-29 10:20:49691 }
692
[email protected]ab88d1542011-11-18 22:52:00693#if defined(OS_MACOSX)
694 // For dynamic loading support, NaCl requires a file descriptor that
695 // was created in /tmp, since those created with shm_open() are not
696 // mappable with PROT_EXEC. Rather than requiring an extra IPC
697 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25698 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31699 base::SharedMemoryCreateOptions options;
700 options.size = 1;
701 options.executable = true;
702 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30703 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04704 delete this;
[email protected]2c68bf032010-11-11 23:16:30705 return;
706 }
[email protected]cbbe7842011-11-17 22:01:25707 nacl::FileDescriptor memory_fd;
708 memory_fd.fd = dup(memory_buffer.handle().fd);
709 if (memory_fd.fd < 0) {
710 LOG(ERROR) << "Failed to dup() a file descriptor";
711 delete this;
712 return;
713 }
714 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30715 handles_for_sel_ldr.push_back(memory_fd);
716#endif
717
[email protected]b39c6d92012-01-31 16:38:41718#if defined(OS_WIN)
719 if (IsHardwareExceptionHandlingEnabled()) {
720 debug_context_->SetStartMessage(
721 new NaClProcessMsg_Start(handles_for_sel_ldr));
722 debug_context_->SendStartMessage();
723 } else {
724 process_->Send(new NaClProcessMsg_Start(handles_for_sel_ldr));
725 }
726#else
[email protected]4967f792012-01-20 22:14:40727 process_->Send(new NaClProcessMsg_Start(handles_for_sel_ldr));
[email protected]b39c6d92012-01-31 16:38:41728#endif
729
[email protected]1d8a3d1f2011-02-19 07:11:52730 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46731}
732
[email protected]a95986a82010-12-24 06:19:28733bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]d032f492009-09-29 00:33:46734 NOTREACHED() << "Invalid message with type = " << msg.type();
[email protected]a95986a82010-12-24 06:19:28735 return false;
[email protected]d032f492009-09-29 00:33:46736}