blob: 0e294685da772871bc036dc927529110de0c9a9e [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]4a0141b2012-03-27 01:15:3012#include "base/memory/mru_cache.h"
[email protected]4734d0b2011-12-03 07:10:4413#include "base/memory/singleton.h"
[email protected]5b974952012-04-05 18:18:2314#include "base/message_loop.h"
[email protected]0ae52b42012-03-27 02:53:5915#include "base/metrics/histogram.h"
[email protected]338466a82011-05-03 04:27:4316#include "base/path_service.h"
[email protected]5b974952012-04-05 18:18:2317#include "base/string_number_conversions.h"
18#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3319#include "base/string_util.h"
[email protected]1657e6d2012-03-30 20:28:0020#include "base/rand_util.h"
[email protected]a0a69bf2011-09-23 21:40:2821#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2222#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3723#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2024#include "build/build_config.h"
[email protected]8f42b4d2012-03-24 14:12:3325#include "chrome/browser/extensions/extension_info_map.h"
26#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4627#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4328#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4629#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0030#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4631#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0932#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4633#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3034#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3335#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4036#include "content/public/browser/browser_child_process_host.h"
37#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4438#include "content/public/common/child_process_host.h"
[email protected]8f42b4d2012-03-24 14:12:3339#include "googleurl/src/gurl.h"
[email protected]d032f492009-09-29 00:33:4640#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5241#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]d032f492009-09-29 00:33:4642
[email protected]d032f492009-09-29 00:33:4643#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2044#include <fcntl.h>
45
[email protected]d032f492009-09-29 00:33:4646#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3547#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2048#include <windows.h>
49
[email protected]b39c6d92012-01-31 16:38:4150#include "base/threading/thread.h"
51#include "base/process_util.h"
[email protected]4bdde602010-06-16 03:17:3552#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]b39c6d92012-01-31 16:38:4153#include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h"
[email protected]d032f492009-09-29 00:33:4654#endif
55
[email protected]631bb742011-11-02 11:29:3956using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4057using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4458using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3959
[email protected]b39c6d92012-01-31 16:38:4160#if defined(OS_WIN)
61class NaClProcessHost::DebugContext
62 : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> {
63 public:
64 DebugContext()
[email protected]8236f852012-04-05 10:24:3165 : can_send_start_msg_(false) {
[email protected]b39c6d92012-01-31 16:38:4166 }
67
68 ~DebugContext() {
69 }
70
71 void AttachDebugger(int pid, base::ProcessHandle process);
72
73 // 6 methods below must be called on Browser::IO thread.
74 void SetStartMessage(IPC::Message* start_msg);
[email protected]8236f852012-04-05 10:24:3175 void SetNaClProcessHost(base::WeakPtr<NaClProcessHost> nacl_process_host);
[email protected]b39c6d92012-01-31 16:38:4176 void SetDebugThread(base::Thread* thread_);
77
78 // Start message is sent from 2 flows of execution. The first flow is
79 // NaClProcessHost::SendStart. The second flow is
80 // NaClProcessHost::OnChannelConnected and
81 // NaClProcessHost::DebugContext::AttachThread. The message itself is created
82 // by first flow. But the moment it can be sent is determined by second flow.
83 // So first flow executes SetStartMessage and SendStartMessage while second
84 // flow uses AllowAndSendStartMessage to either send potentially pending
85 // start message or set the flag that allows the first flow to do this.
86
87 // Clears the flag that prevents sending start message.
88 void AllowToSendStartMsg();
89 // Send start message to the NaCl process or do nothing if message is not
90 // set or not allowed to be send. If message is sent, it is cleared and
91 // repeated calls do nothing.
92 void SendStartMessage();
93 // Clear the flag that prevents further sending start message and send start
94 // message if it is set.
95 void AllowAndSendStartMessage();
96 private:
97 void StopThread();
98 // These 4 fields are accessed only from Browser::IO thread.
99 scoped_ptr<base::Thread> thread_;
100 scoped_ptr<IPC::Message> start_msg_;
101 // Debugger is attached or exception handling is not switched on.
102 // This means that start message can be sent to the NaCl process.
103 bool can_send_start_msg_;
[email protected]8236f852012-04-05 10:24:31104 base::WeakPtr<NaClProcessHost> nacl_process_host_;
[email protected]b39c6d92012-01-31 16:38:41105};
106
107void NaClProcessHost::DebugContext::AttachDebugger(
108 int pid, base::ProcessHandle process) {
109 BOOL attached;
110 DWORD exit_code;
111 attached = DebugActiveProcess(pid);
112 if (!attached) {
113 LOG(ERROR) << "Failed to connect to the process";
114 }
115 BrowserThread::PostTask(
116 BrowserThread::IO, FROM_HERE,
117 base::Bind(
118 &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this));
119 if (attached) {
120 // debug the process
121 NaClDebugLoop(process, &exit_code);
122 base::CloseProcessHandle(process);
123 }
124 BrowserThread::PostTask(
125 BrowserThread::IO, FROM_HERE,
126 base::Bind(&NaClProcessHost::DebugContext::StopThread, this));
127}
128
129void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) {
130 start_msg_.reset(start_msg);
131}
132
[email protected]8236f852012-04-05 10:24:31133void NaClProcessHost::DebugContext::SetNaClProcessHost(
134 base::WeakPtr<NaClProcessHost> nacl_process_host) {
135 nacl_process_host_ = nacl_process_host;
[email protected]b39c6d92012-01-31 16:38:41136}
137
138void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) {
139 thread_.reset(thread);
140}
141
142void NaClProcessHost::DebugContext::AllowToSendStartMsg() {
143 can_send_start_msg_ = true;
144}
145
146void NaClProcessHost::DebugContext::SendStartMessage() {
147 if (start_msg_.get() && can_send_start_msg_) {
[email protected]8236f852012-04-05 10:24:31148 if (nacl_process_host_) {
149 if (!nacl_process_host_->Send(start_msg_.release())) {
[email protected]b39c6d92012-01-31 16:38:41150 LOG(ERROR) << "Failed to send start message";
151 }
152 }
153 }
154}
155
156void NaClProcessHost::DebugContext::AllowAndSendStartMessage() {
157 AllowToSendStartMsg();
158 SendStartMessage();
159}
160
161void NaClProcessHost::DebugContext::StopThread() {
162 thread_->Stop();
163 thread_.reset();
164}
165#endif
166
[email protected]c47ec402010-07-29 10:20:49167namespace {
168
169void SetCloseOnExec(nacl::Handle fd) {
170#if defined(OS_POSIX)
171 int flags = fcntl(fd, F_GETFD);
[email protected]773ebb92011-11-15 19:06:52172 CHECK_NE(flags, -1);
[email protected]c47ec402010-07-29 10:20:49173 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
[email protected]773ebb92011-11-15 19:06:52174 CHECK_EQ(rc, 0);
[email protected]c47ec402010-07-29 10:20:49175#endif
176}
177
[email protected]773ebb92011-11-15 19:06:52178// Represents shared state for all NaClProcessHost objects in the browser.
179// Currently this just handles holding onto the file descriptor for the IRT.
180class NaClBrowser {
181 public:
182 static NaClBrowser* GetInstance() {
183 return Singleton<NaClBrowser>::get();
184 }
185
186 bool IrtAvailable() const {
187 return irt_platform_file_ != base::kInvalidPlatformFileValue;
188 }
189
190 base::PlatformFile IrtFile() const {
191 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
192 return irt_platform_file_;
193 }
194
195 // Asynchronously attempt to get the IRT open.
196 bool EnsureIrtAvailable();
197
198 // Make sure the IRT gets opened and follow up with the reply when it's ready.
199 bool MakeIrtAvailable(const base::Closure& reply);
200
[email protected]31a665e72012-03-11 12:37:46201 // Path to IRT. Available even before IRT is loaded.
202 const FilePath& GetIrtFilePath();
203
[email protected]1657e6d2012-03-30 20:28:00204 // Get the key used for HMACing validation signatures. This should be a
205 // string of cryptographically secure random bytes.
206 const std::string& GetValidatorCacheKey() const {
207 return validator_cache_key_;
208 }
209
[email protected]4a0141b2012-03-27 01:15:30210 // Is the validation signature in the database?
211 bool QueryKnownToValidate(const std::string& signature);
212
213 // Put the validation signature in the database.
214 void SetKnownToValidate(const std::string& signature);
215
[email protected]773ebb92011-11-15 19:06:52216 private:
217 base::PlatformFile irt_platform_file_;
218
[email protected]31a665e72012-03-11 12:37:46219 FilePath irt_filepath_;
220
[email protected]4a0141b2012-03-27 01:15:30221 typedef base::HashingMRUCache<std::string, bool> ValidationCacheType;
222 ValidationCacheType validation_cache_;
223
[email protected]1657e6d2012-03-30 20:28:00224 std::string validator_cache_key_;
225
[email protected]773ebb92011-11-15 19:06:52226 friend struct DefaultSingletonTraits<NaClBrowser>;
227
228 NaClBrowser()
[email protected]31a665e72012-03-11 12:37:46229 : irt_platform_file_(base::kInvalidPlatformFileValue),
[email protected]4a0141b2012-03-27 01:15:30230 irt_filepath_(),
231 // For the moment, choose an arbitrary cache size.
[email protected]1657e6d2012-03-30 20:28:00232 validation_cache_(200),
233 // TODO(ncbray) persist this key along with the cache.
234 // Key size is equal to the block size (not the digest size) of SHA256.
235 validator_cache_key_(base::RandBytesAsString(64)) {
[email protected]31a665e72012-03-11 12:37:46236 InitIrtFilePath();
237 }
[email protected]773ebb92011-11-15 19:06:52238
239 ~NaClBrowser() {
240 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
241 base::ClosePlatformFile(irt_platform_file_);
242 }
243
[email protected]31a665e72012-03-11 12:37:46244 void InitIrtFilePath();
245
[email protected]773ebb92011-11-15 19:06:52246 void OpenIrtLibraryFile();
247
248 static void DoOpenIrtLibraryFile() {
249 GetInstance()->OpenIrtLibraryFile();
250 }
251
252 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
253};
254
[email protected]c47ec402010-07-29 10:20:49255} // namespace
256
[email protected]1d8a3d1f2011-02-19 07:11:52257struct NaClProcessHost::NaClInternal {
258 std::vector<nacl::Handle> sockets_for_renderer;
259 std::vector<nacl::Handle> sockets_for_sel_ldr;
260};
261
[email protected]773ebb92011-11-15 19:06:52262#if defined(OS_WIN)
[email protected]5804cb5d2011-12-19 21:55:35263static bool RunningOnWOW64() {
[email protected]773ebb92011-11-15 19:06:52264 return (base::win::OSInfo::GetInstance()->wow64_status() ==
265 base::win::OSInfo::WOW64_ENABLED);
[email protected]773ebb92011-11-15 19:06:52266}
[email protected]5804cb5d2011-12-19 21:55:35267#endif
[email protected]773ebb92011-11-15 19:06:52268
[email protected]1dbaaa702011-04-06 17:43:42269NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]a575da52012-03-22 13:08:36270 :
271#if defined(OS_WIN)
272 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23273#elif defined(OS_LINUX)
274 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36275#endif
276 reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52277 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06278 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
279 enable_exception_handling_(false) {
[email protected]4967f792012-01-20 22:14:40280 process_.reset(content::BrowserChildProcessHost::Create(
281 content::PROCESS_TYPE_NACL_LOADER, this));
282 process_->SetName(WideToUTF16Hack(url));
[email protected]5ca93be2012-03-21 20:04:06283
284 // We allow untrusted hardware exception handling to be enabled via
285 // an env var for consistency with the standalone build of NaCl.
286 if (CommandLine::ForCurrentProcess()->HasSwitch(
287 switches::kEnableNaClExceptionHandling) ||
288 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
289 enable_exception_handling_ = true;
[email protected]b39c6d92012-01-31 16:38:41290#if defined(OS_WIN)
[email protected]fb335fe2012-03-24 18:11:38291 debug_context_ = new DebugContext();
[email protected]b39c6d92012-01-31 16:38:41292#endif
[email protected]5ca93be2012-03-21 20:04:06293 }
[email protected]d032f492009-09-29 00:33:46294}
295
[email protected]fb1277e82009-11-21 20:32:30296NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49297 int exit_code;
[email protected]4967f792012-01-20 22:14:40298 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49299 std::string message =
300 base::StringPrintf("NaCl process exited with status %i (0x%x)",
301 exit_code, exit_code);
302 if (exit_code == 0) {
303 LOG(INFO) << message;
304 } else {
305 LOG(ERROR) << message;
306 }
307
[email protected]1d8a3d1f2011-02-19 07:11:52308 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04309 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
310 LOG(ERROR) << "nacl::Close() failed";
311 }
[email protected]c47ec402010-07-29 10:20:49312 }
[email protected]1d8a3d1f2011-02-19 07:11:52313 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04314 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
315 LOG(ERROR) << "nacl::Close() failed";
316 }
[email protected]c47ec402010-07-29 10:20:49317 }
318
[email protected]909c2402011-05-09 11:39:04319 if (reply_msg_) {
320 // The process failed to launch for some reason.
321 // Don't keep the renderer hanging.
322 reply_msg_->set_reply_error();
323 chrome_render_message_filter_->Send(reply_msg_);
324 }
[email protected]b39c6d92012-01-31 16:38:41325#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36326 if (process_launched_by_broker_) {
327 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06328 }
[email protected]b39c6d92012-01-31 16:38:41329#endif
[email protected]fb1277e82009-11-21 20:32:30330}
331
[email protected]773ebb92011-11-15 19:06:52332// Attempt to ensure the IRT will be available when we need it, but don't wait.
333bool NaClBrowser::EnsureIrtAvailable() {
334 if (IrtAvailable())
335 return true;
336
337 return BrowserThread::PostTask(
338 BrowserThread::FILE, FROM_HERE,
339 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
340}
341
342// We really need the IRT to be available now, so make sure that it is.
343// When it's ready, we'll run the reply closure.
344bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
345 return BrowserThread::PostTaskAndReply(
346 BrowserThread::FILE, FROM_HERE,
347 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
348}
349
350// This is called at browser startup.
351// static
352void NaClProcessHost::EarlyStartup() {
353#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
354 // Open the IRT file early to make sure that it isn't replaced out from
355 // under us by autoupdate.
356 NaClBrowser::GetInstance()->EnsureIrtAvailable();
357#endif
358}
359
[email protected]a575da52012-03-22 13:08:36360void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52361 ChromeRenderMessageFilter* chrome_render_message_filter,
362 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33363 IPC::Message* reply_msg,
364 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36365 chrome_render_message_filter_ = chrome_render_message_filter;
366 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33367 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36368
[email protected]c47ec402010-07-29 10:20:49369 // Place an arbitrary limit on the number of sockets to limit
370 // exposure in case the renderer is compromised. We can increase
371 // this if necessary.
372 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36373 delete this;
374 return;
[email protected]c47ec402010-07-29 10:20:49375 }
376
[email protected]773ebb92011-11-15 19:06:52377 // Start getting the IRT open asynchronously while we launch the NaCl process.
378 // We'll make sure this actually finished in OnProcessLaunched, below.
379 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
380 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36381 delete this;
382 return;
[email protected]773ebb92011-11-15 19:06:52383 }
384
[email protected]c47ec402010-07-29 10:20:49385 // Rather than creating a socket pair in the renderer, and passing
386 // one side through the browser to sel_ldr, socket pairs are created
387 // in the browser and then passed to the renderer and sel_ldr.
388 //
389 // This is mainly for the benefit of Windows, where sockets cannot
390 // be passed in messages, but are copied via DuplicateHandle().
391 // This means the sandboxed renderer cannot send handles to the
392 // browser process.
393
394 for (int i = 0; i < socket_count; i++) {
395 nacl::Handle pair[2];
396 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36397 if (nacl::SocketPair(pair) == -1) {
398 delete this;
399 return;
400 }
[email protected]1d8a3d1f2011-02-19 07:11:52401 internal_->sockets_for_renderer.push_back(pair[0]);
402 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49403 SetCloseOnExec(pair[0]);
404 SetCloseOnExec(pair[1]);
405 }
[email protected]d032f492009-09-29 00:33:46406
407 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30408 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36409 delete this;
[email protected]d032f492009-09-29 00:33:46410 }
[email protected]d032f492009-09-29 00:33:46411}
[email protected]5b974952012-04-05 18:18:23412#if defined(OS_WIN)
[email protected]8ae7bd6f2012-03-14 03:23:02413scoped_ptr<CommandLine> NaClProcessHost::LaunchWithNaClGdb(
[email protected]8f42b4d2012-03-24 14:12:33414 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23415 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46416 CommandLine* cmd_line = new CommandLine(nacl_gdb);
417 // We can't use PrependWrapper because our parameters contain spaces.
418 cmd_line->AppendArg("--eval-command");
419 const FilePath::StringType& irt_path =
420 NaClBrowser::GetInstance()->GetIrtFilePath().value();
421 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23422 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33423 if (!manifest_path.empty()) {
424 cmd_line->AppendArg("--eval-command");
425 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
426 manifest_path.value());
427 }
[email protected]31a665e72012-03-11 12:37:46428 cmd_line->AppendArg("--args");
429 const CommandLine::StringVector& argv = line->argv();
430 for (size_t i = 0; i < argv.size(); i++) {
431 cmd_line->AppendArgNative(argv[i]);
432 }
433 return scoped_ptr<CommandLine>(cmd_line);
434}
[email protected]5b974952012-04-05 18:18:23435#elif defined(OS_LINUX)
436namespace {
437class NaClGdbWatchDelegate : public MessageLoopForIO::Watcher {
438 public:
439 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
440 NaClGdbWatchDelegate(int fd_read, int fd_write,
441 const base::Closure& reply)
442 : fd_read_(fd_read),
443 fd_write_(fd_write),
444 reply_(reply) {}
445
446 ~NaClGdbWatchDelegate() {
447 if (HANDLE_EINTR(close(fd_read_)) != 0)
448 DLOG(ERROR) << "close(fd_read_) failed";
449 if (HANDLE_EINTR(close(fd_write_)) != 0)
450 DLOG(ERROR) << "close(fd_write_) failed";
451 }
452
453 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
454 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
455
456 private:
457 int fd_read_;
458 int fd_write_;
459 base::Closure reply_;
460};
461
462void NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(int fd) {
463 char buf;
464 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
465 LOG(ERROR) << "Failed to sync with nacl-gdb";
466 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
467}
468} // namespace
469
470bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
471 CommandLine::StringType nacl_gdb =
472 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
473 switches::kNaClGdb);
474 CommandLine::StringVector argv;
475 // We don't support spaces inside arguments in --nacl-gdb switch.
476 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
477 CommandLine cmd_line(argv);
478 cmd_line.AppendArg("--eval-command");
479 const FilePath::StringType& irt_path =
480 NaClBrowser::GetInstance()->GetIrtFilePath().value();
481 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
482 FilePath manifest_path = GetManifestPath();
483 if (!manifest_path.empty()) {
484 cmd_line.AppendArg("--eval-command");
485 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
486 manifest_path.value());
487 }
488 cmd_line.AppendArg("--eval-command");
489 cmd_line.AppendArg("attach " + base::IntToString(pid));
490 int fds[2];
491 if (pipe(fds) != 0)
492 return false;
493 // Tell the debugger to send a byte to the writable end of the pipe.
494 // We use a file descriptor in our process because the debugger will be
495 // typically launched in a separate terminal, and a lot of terminals close all
496 // file descriptors before launching external programs.
497 cmd_line.AppendArg("--eval-command");
498 cmd_line.AppendArg("dump binary value /proc/" +
499 base::IntToString(base::GetCurrentProcId()) +
500 "/fd/" + base::IntToString(fds[1]) + " (char)0");
501 // wait on fds[0]
502 // If the debugger crashes before attaching to the NaCl process, the user can
503 // release resources by terminating the NaCl loader in Chrome Task Manager.
504 nacl_gdb_watcher_delegate_.reset(
505 new NaClGdbWatchDelegate(
506 fds[0], fds[1],
507 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
508 weak_factory_.GetWeakPtr())));
509 MessageLoopForIO::current()->WatchFileDescriptor(
510 fds[0],
511 true,
512 MessageLoopForIO::WATCH_READ,
513 &nacl_gdb_watcher_,
514 nacl_gdb_watcher_delegate_.get());
515 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
516}
517
518void NaClProcessHost::OnNaClGdbAttached() {
519 wait_for_nacl_gdb_ = false;
520 nacl_gdb_watcher_.StopWatchingFileDescriptor();
521 nacl_gdb_watcher_delegate_.reset();
522 OnProcessLaunched();
523}
524#endif
525
526FilePath NaClProcessHost::GetManifestPath() {
527 GURL manifest_url = GURL(process_->GetData().name);
528 const Extension* extension = extension_info_map_->extensions()
529 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url));
530 if (extension != NULL && manifest_url.SchemeIs(chrome::kExtensionScheme)) {
531 std::string path = manifest_url.path();
532 TrimString(path, "/", &path); // Remove first slash
533 return extension->path().AppendASCII(path);
534 }
535 return FilePath();
536}
[email protected]31a665e72012-03-11 12:37:46537
[email protected]fb1277e82009-11-21 20:32:30538bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40539 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44540 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46541 return false;
542
[email protected]e3fc75a2011-05-05 08:20:42543 CommandLine::StringType nacl_loader_prefix;
544#if defined(OS_POSIX)
545 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
546 switches::kNaClLoaderCmdPrefix);
547#endif // defined(OS_POSIX)
548
[email protected]d032f492009-09-29 00:33:46549 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36550
551#if defined(OS_MACOSX)
552 // The Native Client process needs to be able to allocate a 1GB contiguous
553 // region to use as the client environment's virtual address space. ASLR
554 // (PIE) interferes with this by making it possible that no gap large enough
555 // to accomodate this request will exist in the child process' address
556 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
557 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49558 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36559#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49560 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
561 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36562#else
[email protected]4cb43102011-12-02 20:24:49563 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36564#endif
565
[email protected]4cb43102011-12-02 20:24:49566 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30567 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46568 return false;
569
[email protected]31a665e72012-03-11 12:37:46570#if defined(OS_WIN)
571 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
572 if (RunningOnWOW64()) {
573 FilePath module_path;
574 if (!PathService::Get(base::FILE_MODULE, &module_path))
575 return false;
576 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
577 }
578#endif
579
[email protected]33a05af2012-03-02 18:15:51580 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
581 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13582
[email protected]05076ba22010-07-30 05:59:57583 cmd_line->AppendSwitchASCII(switches::kProcessType,
584 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44585 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44586 if (logging::DialogsAreSuppressed())
587 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46588
[email protected]e3fc75a2011-05-05 08:20:42589 if (!nacl_loader_prefix.empty())
590 cmd_line->PrependWrapper(nacl_loader_prefix);
591
[email protected]31a665e72012-03-11 12:37:46592 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
593 switches::kNaClGdb);
594 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23595#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46596 cmd_line->AppendSwitch(switches::kNoSandbox);
597 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]5b974952012-04-05 18:18:23598 LaunchWithNaClGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46599 // We can't use process_->Launch() because OnProcessLaunched will be called
600 // with process_->GetData().handle filled by handle of gdb process. This
601 // handle will be used to duplicate handles for NaCl process and as
602 // a result NaCl process will not be able to use them.
603 //
604 // So we don't fill process_->GetData().handle and wait for
605 // OnChannelConnected to get handle of NaCl process from its pid. Then we
606 // call OnProcessLaunched.
607 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23608#elif defined(OS_LINUX)
609 wait_for_nacl_gdb_ = true;
610#endif
[email protected]31a665e72012-03-11 12:37:46611 }
612
[email protected]103607e2010-02-01 18:57:09613 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46614#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52615 if (RunningOnWOW64()) {
[email protected]644b2e22012-03-25 01:41:07616 return NaClBrokerService::GetInstance()->LaunchLoader(this, channel_id);
[email protected]4bdde602010-06-16 03:17:35617 } else {
[email protected]33a05af2012-03-02 18:15:51618 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35619 }
[email protected]103607e2010-02-01 18:57:09620#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40621 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20622 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51623 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09624#endif
[email protected]d032f492009-09-29 00:33:46625
[email protected]fb1277e82009-11-21 20:32:30626 return true;
[email protected]d032f492009-09-29 00:33:46627}
628
[email protected]a575da52012-03-22 13:08:36629#if defined(OS_WIN)
[email protected]fb335fe2012-03-24 18:11:38630void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
[email protected]a575da52012-03-22 13:08:36631 process_launched_by_broker_ = true;
[email protected]4967f792012-01-20 22:14:40632 process_->SetHandle(handle);
[email protected]103607e2010-02-01 18:57:09633 OnProcessLaunched();
634}
635
[email protected]fb335fe2012-03-24 18:11:38636void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker() {
637 debug_context_->AllowAndSendStartMessage();
638}
639#endif
640
[email protected]463ea5f2011-12-03 06:57:47641void NaClProcessHost::OnProcessCrashed(int exit_code) {
642 std::string message = base::StringPrintf(
643 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
644 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09645}
646
[email protected]75e0c502011-12-16 21:53:01647namespace {
648
649// Determine the name of the IRT file based on the architecture.
650
651#define NACL_IRT_FILE_NAME(arch_string) \
652 (FILE_PATH_LITERAL("nacl_irt_") \
653 FILE_PATH_LITERAL(arch_string) \
654 FILE_PATH_LITERAL(".nexe"))
655
656const FilePath::StringType NaClIrtName() {
657#if defined(ARCH_CPU_X86_FAMILY)
[email protected]75e0c502011-12-16 21:53:01658#if defined(ARCH_CPU_X86_64)
[email protected]5804cb5d2011-12-19 21:55:35659 bool is64 = true;
660#elif defined(OS_WIN)
661 bool is64 = RunningOnWOW64();
662#else
663 bool is64 = false;
[email protected]75e0c502011-12-16 21:53:01664#endif
665 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
666#elif defined(ARCH_CPU_ARMEL)
667 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
668 // That may need to be based on the actual nexe rather than a static
669 // choice, which would require substantial refactoring.
670 return NACL_IRT_FILE_NAME("arm");
671#else
672#error Add support for your architecture to NaCl IRT file selection
673#endif
674}
675
676} // namespace
677
[email protected]31a665e72012-03-11 12:37:46678void NaClBrowser::InitIrtFilePath() {
[email protected]88e15832011-07-19 01:18:24679 // Allow the IRT library to be overridden via an environment
680 // variable. This allows the NaCl/Chromium integration bot to
681 // specify a newly-built IRT rather than using a prebuilt one
682 // downloaded via Chromium's DEPS file. We use the same environment
683 // variable that the standalone NaCl PPAPI plugin accepts.
684 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
685 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52686 FilePath::StringType path_string(
687 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
[email protected]31a665e72012-03-11 12:37:46688 irt_filepath_ = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24689 } else {
690 FilePath plugin_dir;
691 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
692 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24693 return;
694 }
[email protected]773ebb92011-11-15 19:06:52695
[email protected]31a665e72012-03-11 12:37:46696 irt_filepath_ = plugin_dir.Append(NaClIrtName());
[email protected]338466a82011-05-03 04:27:43697 }
[email protected]31a665e72012-03-11 12:37:46698}
699
700const FilePath& NaClBrowser::GetIrtFilePath() {
701 return irt_filepath_;
702}
703
704// This only ever runs on the BrowserThread::FILE thread.
705// If multiple tasks are posted, the later ones are no-ops.
706void NaClBrowser::OpenIrtLibraryFile() {
707 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
708 // We've already run.
709 return;
[email protected]88e15832011-07-19 01:18:24710
[email protected]773ebb92011-11-15 19:06:52711 base::PlatformFileError error_code;
[email protected]31a665e72012-03-11 12:37:46712 irt_platform_file_ = base::CreatePlatformFile(irt_filepath_,
[email protected]773ebb92011-11-15 19:06:52713 base::PLATFORM_FILE_OPEN |
714 base::PLATFORM_FILE_READ,
715 NULL,
716 &error_code);
717 if (error_code != base::PLATFORM_FILE_OK) {
718 LOG(ERROR) << "Failed to open NaCl IRT file \""
[email protected]31a665e72012-03-11 12:37:46719 << irt_filepath_.LossyDisplayName()
[email protected]773ebb92011-11-15 19:06:52720 << "\": " << error_code;
721 }
722}
723
724void NaClProcessHost::OnProcessLaunched() {
[email protected]5b974952012-04-05 18:18:23725#if defined(OS_LINUX)
726 if (wait_for_nacl_gdb_) {
727 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
728 // OnProcessLaunched will be called with wait_for_nacl_gdb_ = false once
729 // debugger is attached to the program.
730 return;
731 }
732 LOG(ERROR) << "Failed to launch debugger";
733 // Continue execution without debugger.
734 }
735#endif
[email protected]773ebb92011-11-15 19:06:52736 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
737
738 if (nacl_browser->IrtAvailable()) {
739 // The IRT is already open. Away we go.
[email protected]1657e6d2012-03-30 20:28:00740 SendStart();
[email protected]773ebb92011-11-15 19:06:52741 } else {
742 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43743 if (!nacl_browser->MakeIrtAvailable(
744 base::Bind(&NaClProcessHost::IrtReady,
745 weak_factory_.GetWeakPtr())))
746 delete this;
[email protected]773ebb92011-11-15 19:06:52747 }
748}
749
750// The asynchronous attempt to get the IRT file open has completed.
751void NaClProcessHost::IrtReady() {
752 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
753
754 if (nacl_browser->IrtAvailable()) {
[email protected]1657e6d2012-03-30 20:28:00755 SendStart();
[email protected]773ebb92011-11-15 19:06:52756 } else {
757 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43758 delete this;
759 }
760}
761
[email protected]b39c6d92012-01-31 16:38:41762#if defined(OS_WIN)
[email protected]b39c6d92012-01-31 16:38:41763void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]31a665e72012-03-11 12:37:46764 // Set process handle, if it was not set previously.
765 // This is needed when NaCl process is launched with nacl-gdb.
766 if (process_->GetData().handle == base::kNullProcessHandle) {
767 base::ProcessHandle process;
768 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
769 switches::kNaClGdb).empty());
770 if (base::OpenProcessHandleWithAccess(
771 peer_pid,
772 base::kProcessAccessDuplicateHandle |
[email protected]59261242012-03-19 13:08:59773 base::kProcessAccessQueryInformation |
[email protected]31a665e72012-03-11 12:37:46774 base::kProcessAccessWaitForTermination,
775 &process)) {
776 process_->SetHandle(process);
777 OnProcessLaunched();
778 } else {
779 LOG(ERROR) << "Failed to get process handle";
780 }
781 }
[email protected]5ca93be2012-03-21 20:04:06782 if (debug_context_ == NULL) {
[email protected]b39c6d92012-01-31 16:38:41783 return;
784 }
[email protected]8236f852012-04-05 10:24:31785 debug_context_->SetNaClProcessHost(weak_factory_.GetWeakPtr());
[email protected]fb335fe2012-03-24 18:11:38786 if (RunningOnWOW64()) {
787 if (!NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
788 this, peer_pid)) {
789 debug_context_->AllowAndSendStartMessage();
790 }
791 } else {
792 // Start new thread for debug loop
793 // We can't use process_->GetData().handle because it doesn't have necessary
794 // access rights.
795 base::ProcessHandle process;
796 if (!base::OpenProcessHandleWithAccess(
797 peer_pid,
798 base::kProcessAccessQueryInformation |
799 base::kProcessAccessSuspendResume |
800 base::kProcessAccessTerminate |
801 base::kProcessAccessVMOperation |
802 base::kProcessAccessVMRead |
803 base::kProcessAccessVMWrite |
804 base::kProcessAccessWaitForTermination,
805 &process)) {
806 LOG(ERROR) << "Failed to open the process";
807 debug_context_->AllowAndSendStartMessage();
808 return;
809 }
810 base::Thread* dbg_thread = new base::Thread("Debug thread");
811 if (!dbg_thread->Start()) {
812 LOG(ERROR) << "Debug thread not started";
813 debug_context_->AllowAndSendStartMessage();
814 base::CloseProcessHandle(process);
815 return;
816 }
817 debug_context_->SetDebugThread(dbg_thread);
818 // System can not reallocate pid until we close process handle. So using
819 // pid in different thread is fine.
820 dbg_thread->message_loop()->PostTask(FROM_HERE,
821 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
822 debug_context_, peer_pid, process));
[email protected]b39c6d92012-01-31 16:38:41823 }
[email protected]b39c6d92012-01-31 16:38:41824}
825#else
826void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
827}
828#endif
829
830
[email protected]773ebb92011-11-15 19:06:52831static bool SendHandleToSelLdr(
832 base::ProcessHandle processh,
833 nacl::Handle sourceh, bool close_source,
834 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
835#if defined(OS_WIN)
836 HANDLE channel;
837 int flags = DUPLICATE_SAME_ACCESS;
838 if (close_source)
839 flags |= DUPLICATE_CLOSE_SOURCE;
840 if (!DuplicateHandle(GetCurrentProcess(),
841 reinterpret_cast<HANDLE>(sourceh),
842 processh,
843 &channel,
844 0, // Unused given DUPLICATE_SAME_ACCESS.
845 FALSE,
846 flags)) {
847 LOG(ERROR) << "DuplicateHandle() failed";
848 return false;
849 }
850 handles_for_sel_ldr->push_back(
851 reinterpret_cast<nacl::FileDescriptor>(channel));
852#else
853 nacl::FileDescriptor channel;
854 channel.fd = sourceh;
855 channel.auto_close = close_source;
856 handles_for_sel_ldr->push_back(channel);
857#endif
858 return true;
859}
860
[email protected]1657e6d2012-03-30 20:28:00861void NaClProcessHost::SendStart() {
862 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
863 base::PlatformFile irt_file = nacl_browser->IrtFile();
[email protected]773ebb92011-11-15 19:06:52864 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
865
[email protected]c47ec402010-07-29 10:20:49866 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30867 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30868
[email protected]1d8a3d1f2011-02-19 07:11:52869 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49870#if defined(OS_WIN)
871 // Copy the handle into the renderer process.
872 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04873 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
874 reinterpret_cast<HANDLE>(
875 internal_->sockets_for_renderer[i]),
876 chrome_render_message_filter_->peer_handle(),
877 &handle_in_renderer,
878 0, // Unused given DUPLICATE_SAME_ACCESS.
879 FALSE,
880 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
881 LOG(ERROR) << "DuplicateHandle() failed";
882 delete this;
883 return;
884 }
[email protected]c47ec402010-07-29 10:20:49885 handles_for_renderer.push_back(
886 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
887#else
888 // No need to dup the imc_handle - we don't pass it anywhere else so
889 // it cannot be closed.
890 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52891 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49892 imc_handle.auto_close = true;
893 handles_for_renderer.push_back(imc_handle);
894#endif
895 }
896
[email protected]4967f792012-01-20 22:14:40897 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49898#if defined(OS_WIN)
899 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04900 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40901 data.handle,
[email protected]909c2402011-05-09 11:39:04902 chrome_render_message_filter_->peer_handle(),
903 &nacl_process_handle,
904 PROCESS_DUP_HANDLE,
905 FALSE,
906 0)) {
907 LOG(ERROR) << "DuplicateHandle() failed";
908 delete this;
909 return;
910 }
[email protected]fb1277e82009-11-21 20:32:30911#else
[email protected]fb1277e82009-11-21 20:32:30912 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40913 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30914#endif
915
916 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40917 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30918
[email protected]2ccf45c2011-08-19 23:35:50919 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49920 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52921 chrome_render_message_filter_->Send(reply_msg_);
922 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30923 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52924 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30925
[email protected]c47ec402010-07-29 10:20:49926 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52927 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40928 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52929 internal_->sockets_for_sel_ldr[i], true,
930 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04931 delete this;
[email protected]c47ec402010-07-29 10:20:49932 return;
933 }
[email protected]773ebb92011-11-15 19:06:52934 }
935
936 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40937 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52938 delete this;
939 return;
[email protected]c47ec402010-07-29 10:20:49940 }
941
[email protected]ab88d1542011-11-18 22:52:00942#if defined(OS_MACOSX)
943 // For dynamic loading support, NaCl requires a file descriptor that
944 // was created in /tmp, since those created with shm_open() are not
945 // mappable with PROT_EXEC. Rather than requiring an extra IPC
946 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25947 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31948 base::SharedMemoryCreateOptions options;
949 options.size = 1;
950 options.executable = true;
951 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30952 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04953 delete this;
[email protected]2c68bf032010-11-11 23:16:30954 return;
955 }
[email protected]cbbe7842011-11-17 22:01:25956 nacl::FileDescriptor memory_fd;
957 memory_fd.fd = dup(memory_buffer.handle().fd);
958 if (memory_fd.fd < 0) {
959 LOG(ERROR) << "Failed to dup() a file descriptor";
960 delete this;
961 return;
962 }
963 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30964 handles_for_sel_ldr.push_back(memory_fd);
965#endif
966
[email protected]1657e6d2012-03-30 20:28:00967 // Sending the version string over IPC avoids linkage issues in cases where
968 // NaCl is not compiled into the main Chromium executable or DLL.
969 chrome::VersionInfo version_info;
[email protected]5ca93be2012-03-21 20:04:06970 IPC::Message* start_message =
[email protected]1657e6d2012-03-30 20:28:00971 new NaClProcessMsg_Start(handles_for_sel_ldr,
972 nacl_browser->GetValidatorCacheKey(),
973 version_info.CreateVersionString(),
974 enable_exception_handling_);
[email protected]b39c6d92012-01-31 16:38:41975#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:06976 if (debug_context_ != NULL) {
977 debug_context_->SetStartMessage(start_message);
[email protected]b39c6d92012-01-31 16:38:41978 debug_context_->SendStartMessage();
979 } else {
[email protected]5ca93be2012-03-21 20:04:06980 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41981 }
982#else
[email protected]5ca93be2012-03-21 20:04:06983 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:41984#endif
985
[email protected]1d8a3d1f2011-02-19 07:11:52986 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46987}
988
[email protected]4a0141b2012-03-27 01:15:30989bool NaClBrowser::QueryKnownToValidate(const std::string& signature) {
[email protected]0ae52b42012-03-27 02:53:59990 bool result = false;
[email protected]4a0141b2012-03-27 01:15:30991 ValidationCacheType::iterator iter = validation_cache_.Get(signature);
[email protected]0ae52b42012-03-27 02:53:59992 if (iter != validation_cache_.end()) {
993 result = iter->second;
[email protected]4a0141b2012-03-27 01:15:30994 }
[email protected]0ae52b42012-03-27 02:53:59995 UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Query",
996 result ? 1 : 0, 2);
997 return result;
[email protected]4a0141b2012-03-27 01:15:30998}
999
1000void NaClBrowser::SetKnownToValidate(const std::string& signature) {
1001 validation_cache_.Put(signature, true);
[email protected]0ae52b42012-03-27 02:53:591002 // The number of sets should be equal to the number of cache misses, minus
1003 // validation failures and successful validations where stubout occurs.
1004 // Bucket zero is reserved for future use.
1005 UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Set", 1, 2);
[email protected]4a0141b2012-03-27 01:15:301006}
1007
1008void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
1009 bool* result) {
1010 *result = NaClBrowser::GetInstance()->QueryKnownToValidate(signature);
1011}
1012
1013void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
1014 NaClBrowser::GetInstance()->SetKnownToValidate(signature);
1015}
1016
1017// Needed to handle sync messages in OnMessageRecieved.
1018bool NaClProcessHost::Send(IPC::Message* msg) {
1019 return process_->Send(msg);
1020}
1021
[email protected]a95986a82010-12-24 06:19:281022bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]4a0141b2012-03-27 01:15:301023 bool handled = true;
1024 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
1025 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
1026 OnQueryKnownToValidate)
1027 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
1028 OnSetKnownToValidate)
1029 IPC_MESSAGE_UNHANDLED(handled = false)
1030 IPC_END_MESSAGE_MAP()
1031 return handled;
[email protected]d032f492009-09-29 00:33:461032}