blob: bb535332f64b61463e4c328a3bc7e47eee8cad53 [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]646e15552012-04-06 22:01:0460namespace {
61
62#if defined(OS_WIN)
63bool RunningOnWOW64() {
64 return (base::win::OSInfo::GetInstance()->wow64_status() ==
65 base::win::OSInfo::WOW64_ENABLED);
66}
67#endif
68
69// Determine the name of the IRT file based on the architecture.
70#define NACL_IRT_FILE_NAME(arch_string) \
71 (FILE_PATH_LITERAL("nacl_irt_") \
72 FILE_PATH_LITERAL(arch_string) \
73 FILE_PATH_LITERAL(".nexe"))
74
75const FilePath::StringType NaClIrtName() {
76#if defined(ARCH_CPU_X86_FAMILY)
77#if defined(ARCH_CPU_X86_64)
78 bool is64 = true;
79#elif defined(OS_WIN)
80 bool is64 = RunningOnWOW64();
81#else
82 bool is64 = false;
83#endif
84 return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32");
85#elif defined(ARCH_CPU_ARMEL)
86 // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2.
87 // That may need to be based on the actual nexe rather than a static
88 // choice, which would require substantial refactoring.
89 return NACL_IRT_FILE_NAME("arm");
90#else
91#error Add support for your architecture to NaCl IRT file selection
92#endif
93}
94
95void SetCloseOnExec(nacl::Handle fd) {
96#if defined(OS_POSIX)
97 int flags = fcntl(fd, F_GETFD);
98 CHECK_NE(flags, -1);
99 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
100 CHECK_EQ(rc, 0);
101#endif
102}
103
104bool SendHandleToSelLdr(
105 base::ProcessHandle processh,
106 nacl::Handle sourceh,
107 bool close_source,
108 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
109#if defined(OS_WIN)
110 HANDLE channel;
111 int flags = DUPLICATE_SAME_ACCESS;
112 if (close_source)
113 flags |= DUPLICATE_CLOSE_SOURCE;
114 if (!DuplicateHandle(GetCurrentProcess(),
115 reinterpret_cast<HANDLE>(sourceh),
116 processh,
117 &channel,
118 0, // Unused given DUPLICATE_SAME_ACCESS.
119 FALSE,
120 flags)) {
121 LOG(ERROR) << "DuplicateHandle() failed";
122 return false;
123 }
124 handles_for_sel_ldr->push_back(
125 reinterpret_cast<nacl::FileDescriptor>(channel));
126#else
127 nacl::FileDescriptor channel;
128 channel.fd = sourceh;
129 channel.auto_close = close_source;
130 handles_for_sel_ldr->push_back(channel);
131#endif
132 return true;
133}
134
135// NaClBrowser -----------------------------------------------------------------
136
137// Represents shared state for all NaClProcessHost objects in the browser.
138// Currently this just handles holding onto the file descriptor for the IRT.
139class NaClBrowser {
140 public:
141 static NaClBrowser* GetInstance() {
142 return Singleton<NaClBrowser>::get();
143 }
144
145 bool IrtAvailable() const {
146 return irt_platform_file_ != base::kInvalidPlatformFileValue;
147 }
148
149 base::PlatformFile IrtFile() const {
150 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
151 return irt_platform_file_;
152 }
153
154 // Asynchronously attempt to get the IRT open.
155 bool EnsureIrtAvailable();
156
157 // Make sure the IRT gets opened and follow up with the reply when it's ready.
158 bool MakeIrtAvailable(const base::Closure& reply);
159
160 // Path to IRT. Available even before IRT is loaded.
161 const FilePath& GetIrtFilePath();
162
163 // Get the key used for HMACing validation signatures. This should be a
164 // string of cryptographically secure random bytes.
165 const std::string& GetValidatorCacheKey() const {
166 return validator_cache_key_;
167 }
168
169 // Is the validation signature in the database?
170 bool QueryKnownToValidate(const std::string& signature);
171
172 // Put the validation signature in the database.
173 void SetKnownToValidate(const std::string& signature);
174
175 private:
176 friend struct DefaultSingletonTraits<NaClBrowser>;
177
178 NaClBrowser()
179 : irt_platform_file_(base::kInvalidPlatformFileValue),
180 irt_filepath_(),
181 // For the moment, choose an arbitrary cache size.
182 validation_cache_(200),
183 // TODO(ncbray) persist this key along with the cache.
184 // Key size is equal to the block size (not the digest size) of SHA256.
185 validator_cache_key_(base::RandBytesAsString(64)) {
186 InitIrtFilePath();
187 }
188
189 ~NaClBrowser() {
190 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
191 base::ClosePlatformFile(irt_platform_file_);
192 }
193
194 void InitIrtFilePath();
195
196 void OpenIrtLibraryFile();
197
198 static void DoOpenIrtLibraryFile() {
199 GetInstance()->OpenIrtLibraryFile();
200 }
201
202 base::PlatformFile irt_platform_file_;
203
204 FilePath irt_filepath_;
205
206 typedef base::HashingMRUCache<std::string, bool> ValidationCacheType;
207 ValidationCacheType validation_cache_;
208
209 std::string validator_cache_key_;
210
211 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
212};
213
214// Attempt to ensure the IRT will be available when we need it, but don't wait.
215bool NaClBrowser::EnsureIrtAvailable() {
216 if (IrtAvailable())
217 return true;
218
219 return BrowserThread::PostTask(
220 BrowserThread::FILE, FROM_HERE,
221 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
222}
223
224// We really need the IRT to be available now, so make sure that it is.
225// When it's ready, we'll run the reply closure.
226bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
227 return BrowserThread::PostTaskAndReply(
228 BrowserThread::FILE, FROM_HERE,
229 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
230}
231
232const FilePath& NaClBrowser::GetIrtFilePath() {
233 return irt_filepath_;
234}
235
236bool NaClBrowser::QueryKnownToValidate(const std::string& signature) {
237 bool result = false;
238 ValidationCacheType::iterator iter = validation_cache_.Get(signature);
239 if (iter != validation_cache_.end()) {
240 result = iter->second;
241 }
242 UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Query",
243 result ? 1 : 0, 2);
244 return result;
245}
246
247void NaClBrowser::SetKnownToValidate(const std::string& signature) {
248 validation_cache_.Put(signature, true);
249 // The number of sets should be equal to the number of cache misses, minus
250 // validation failures and successful validations where stubout occurs.
251 // Bucket zero is reserved for future use.
252 UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Set", 1, 2);
253}
254
255void NaClBrowser::InitIrtFilePath() {
256 // Allow the IRT library to be overridden via an environment
257 // variable. This allows the NaCl/Chromium integration bot to
258 // specify a newly-built IRT rather than using a prebuilt one
259 // downloaded via Chromium's DEPS file. We use the same environment
260 // variable that the standalone NaCl PPAPI plugin accepts.
261 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
262 if (irt_path_var != NULL) {
263 FilePath::StringType path_string(
264 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
265 irt_filepath_ = FilePath(path_string);
266 } else {
267 FilePath plugin_dir;
268 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
269 LOG(ERROR) << "Failed to locate the plugins directory";
270 return;
271 }
272
273 irt_filepath_ = plugin_dir.Append(NaClIrtName());
274 }
275}
276
277// This only ever runs on the BrowserThread::FILE thread.
278// If multiple tasks are posted, the later ones are no-ops.
279void NaClBrowser::OpenIrtLibraryFile() {
280 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
281 // We've already run.
282 return;
283
284 base::PlatformFileError error_code;
285 irt_platform_file_ = base::CreatePlatformFile(irt_filepath_,
286 base::PLATFORM_FILE_OPEN |
287 base::PLATFORM_FILE_READ,
288 NULL,
289 &error_code);
290 if (error_code != base::PLATFORM_FILE_OK) {
291 LOG(ERROR) << "Failed to open NaCl IRT file \""
292 << irt_filepath_.LossyDisplayName()
293 << "\": " << error_code;
294 }
295}
296
297} // namespace
298
299// DebugContext ----------------------------------------------------------------
300
[email protected]b39c6d92012-01-31 16:38:41301#if defined(OS_WIN)
302class NaClProcessHost::DebugContext
303 : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> {
304 public:
305 DebugContext()
[email protected]8236f852012-04-05 10:24:31306 : can_send_start_msg_(false) {
[email protected]b39c6d92012-01-31 16:38:41307 }
308
309 ~DebugContext() {
310 }
311
312 void AttachDebugger(int pid, base::ProcessHandle process);
313
314 // 6 methods below must be called on Browser::IO thread.
315 void SetStartMessage(IPC::Message* start_msg);
[email protected]8236f852012-04-05 10:24:31316 void SetNaClProcessHost(base::WeakPtr<NaClProcessHost> nacl_process_host);
[email protected]b39c6d92012-01-31 16:38:41317 void SetDebugThread(base::Thread* thread_);
318
319 // Start message is sent from 2 flows of execution. The first flow is
320 // NaClProcessHost::SendStart. The second flow is
321 // NaClProcessHost::OnChannelConnected and
322 // NaClProcessHost::DebugContext::AttachThread. The message itself is created
323 // by first flow. But the moment it can be sent is determined by second flow.
324 // So first flow executes SetStartMessage and SendStartMessage while second
325 // flow uses AllowAndSendStartMessage to either send potentially pending
326 // start message or set the flag that allows the first flow to do this.
327
328 // Clears the flag that prevents sending start message.
329 void AllowToSendStartMsg();
330 // Send start message to the NaCl process or do nothing if message is not
331 // set or not allowed to be send. If message is sent, it is cleared and
332 // repeated calls do nothing.
333 void SendStartMessage();
334 // Clear the flag that prevents further sending start message and send start
335 // message if it is set.
336 void AllowAndSendStartMessage();
337 private:
338 void StopThread();
339 // These 4 fields are accessed only from Browser::IO thread.
340 scoped_ptr<base::Thread> thread_;
341 scoped_ptr<IPC::Message> start_msg_;
342 // Debugger is attached or exception handling is not switched on.
343 // This means that start message can be sent to the NaCl process.
344 bool can_send_start_msg_;
[email protected]8236f852012-04-05 10:24:31345 base::WeakPtr<NaClProcessHost> nacl_process_host_;
[email protected]b39c6d92012-01-31 16:38:41346};
347
348void NaClProcessHost::DebugContext::AttachDebugger(
349 int pid, base::ProcessHandle process) {
350 BOOL attached;
351 DWORD exit_code;
352 attached = DebugActiveProcess(pid);
353 if (!attached) {
354 LOG(ERROR) << "Failed to connect to the process";
355 }
356 BrowserThread::PostTask(
357 BrowserThread::IO, FROM_HERE,
358 base::Bind(
359 &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this));
360 if (attached) {
361 // debug the process
362 NaClDebugLoop(process, &exit_code);
363 base::CloseProcessHandle(process);
364 }
365 BrowserThread::PostTask(
366 BrowserThread::IO, FROM_HERE,
367 base::Bind(&NaClProcessHost::DebugContext::StopThread, this));
368}
369
370void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) {
371 start_msg_.reset(start_msg);
372}
373
[email protected]8236f852012-04-05 10:24:31374void NaClProcessHost::DebugContext::SetNaClProcessHost(
375 base::WeakPtr<NaClProcessHost> nacl_process_host) {
376 nacl_process_host_ = nacl_process_host;
[email protected]b39c6d92012-01-31 16:38:41377}
378
379void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) {
380 thread_.reset(thread);
381}
382
383void NaClProcessHost::DebugContext::AllowToSendStartMsg() {
384 can_send_start_msg_ = true;
385}
386
387void NaClProcessHost::DebugContext::SendStartMessage() {
388 if (start_msg_.get() && can_send_start_msg_) {
[email protected]8236f852012-04-05 10:24:31389 if (nacl_process_host_) {
390 if (!nacl_process_host_->Send(start_msg_.release())) {
[email protected]b39c6d92012-01-31 16:38:41391 LOG(ERROR) << "Failed to send start message";
392 }
393 }
394 }
395}
396
397void NaClProcessHost::DebugContext::AllowAndSendStartMessage() {
398 AllowToSendStartMsg();
399 SendStartMessage();
400}
401
402void NaClProcessHost::DebugContext::StopThread() {
403 thread_->Stop();
404 thread_.reset();
405}
406#endif
407
[email protected]1d8a3d1f2011-02-19 07:11:52408struct NaClProcessHost::NaClInternal {
409 std::vector<nacl::Handle> sockets_for_renderer;
410 std::vector<nacl::Handle> sockets_for_sel_ldr;
411};
412
[email protected]646e15552012-04-06 22:01:04413// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52414
[email protected]1dbaaa702011-04-06 17:43:42415NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]a575da52012-03-22 13:08:36416 :
417#if defined(OS_WIN)
418 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23419#elif defined(OS_LINUX)
420 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36421#endif
422 reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52423 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06424 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
425 enable_exception_handling_(false) {
[email protected]4967f792012-01-20 22:14:40426 process_.reset(content::BrowserChildProcessHost::Create(
427 content::PROCESS_TYPE_NACL_LOADER, this));
428 process_->SetName(WideToUTF16Hack(url));
[email protected]5ca93be2012-03-21 20:04:06429
430 // We allow untrusted hardware exception handling to be enabled via
431 // an env var for consistency with the standalone build of NaCl.
432 if (CommandLine::ForCurrentProcess()->HasSwitch(
433 switches::kEnableNaClExceptionHandling) ||
434 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
435 enable_exception_handling_ = true;
[email protected]b39c6d92012-01-31 16:38:41436#if defined(OS_WIN)
[email protected]fb335fe2012-03-24 18:11:38437 debug_context_ = new DebugContext();
[email protected]b39c6d92012-01-31 16:38:41438#endif
[email protected]5ca93be2012-03-21 20:04:06439 }
[email protected]d032f492009-09-29 00:33:46440}
441
[email protected]fb1277e82009-11-21 20:32:30442NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49443 int exit_code;
[email protected]4967f792012-01-20 22:14:40444 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49445 std::string message =
446 base::StringPrintf("NaCl process exited with status %i (0x%x)",
447 exit_code, exit_code);
448 if (exit_code == 0) {
449 LOG(INFO) << message;
450 } else {
451 LOG(ERROR) << message;
452 }
453
[email protected]1d8a3d1f2011-02-19 07:11:52454 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04455 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
456 LOG(ERROR) << "nacl::Close() failed";
457 }
[email protected]c47ec402010-07-29 10:20:49458 }
[email protected]1d8a3d1f2011-02-19 07:11:52459 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04460 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
461 LOG(ERROR) << "nacl::Close() failed";
462 }
[email protected]c47ec402010-07-29 10:20:49463 }
464
[email protected]909c2402011-05-09 11:39:04465 if (reply_msg_) {
466 // The process failed to launch for some reason.
467 // Don't keep the renderer hanging.
468 reply_msg_->set_reply_error();
469 chrome_render_message_filter_->Send(reply_msg_);
470 }
[email protected]b39c6d92012-01-31 16:38:41471#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36472 if (process_launched_by_broker_) {
473 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06474 }
[email protected]b39c6d92012-01-31 16:38:41475#endif
[email protected]fb1277e82009-11-21 20:32:30476}
477
[email protected]773ebb92011-11-15 19:06:52478// This is called at browser startup.
479// static
480void NaClProcessHost::EarlyStartup() {
481#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
482 // Open the IRT file early to make sure that it isn't replaced out from
483 // under us by autoupdate.
484 NaClBrowser::GetInstance()->EnsureIrtAvailable();
485#endif
486}
487
[email protected]a575da52012-03-22 13:08:36488void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52489 ChromeRenderMessageFilter* chrome_render_message_filter,
490 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33491 IPC::Message* reply_msg,
492 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36493 chrome_render_message_filter_ = chrome_render_message_filter;
494 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33495 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36496
[email protected]c47ec402010-07-29 10:20:49497 // Place an arbitrary limit on the number of sockets to limit
498 // exposure in case the renderer is compromised. We can increase
499 // this if necessary.
500 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36501 delete this;
502 return;
[email protected]c47ec402010-07-29 10:20:49503 }
504
[email protected]773ebb92011-11-15 19:06:52505 // Start getting the IRT open asynchronously while we launch the NaCl process.
506 // We'll make sure this actually finished in OnProcessLaunched, below.
507 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
508 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36509 delete this;
510 return;
[email protected]773ebb92011-11-15 19:06:52511 }
512
[email protected]c47ec402010-07-29 10:20:49513 // Rather than creating a socket pair in the renderer, and passing
514 // one side through the browser to sel_ldr, socket pairs are created
515 // in the browser and then passed to the renderer and sel_ldr.
516 //
517 // This is mainly for the benefit of Windows, where sockets cannot
518 // be passed in messages, but are copied via DuplicateHandle().
519 // This means the sandboxed renderer cannot send handles to the
520 // browser process.
521
522 for (int i = 0; i < socket_count; i++) {
523 nacl::Handle pair[2];
524 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36525 if (nacl::SocketPair(pair) == -1) {
526 delete this;
527 return;
528 }
[email protected]1d8a3d1f2011-02-19 07:11:52529 internal_->sockets_for_renderer.push_back(pair[0]);
530 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49531 SetCloseOnExec(pair[0]);
532 SetCloseOnExec(pair[1]);
533 }
[email protected]d032f492009-09-29 00:33:46534
535 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30536 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36537 delete this;
[email protected]d032f492009-09-29 00:33:46538 }
[email protected]d032f492009-09-29 00:33:46539}
[email protected]646e15552012-04-06 22:01:04540
[email protected]5b974952012-04-05 18:18:23541#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04542void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
543 // Set process handle, if it was not set previously.
544 // This is needed when NaCl process is launched with nacl-gdb.
545 if (process_->GetData().handle == base::kNullProcessHandle) {
546 base::ProcessHandle process;
547 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
548 switches::kNaClGdb).empty());
549 if (base::OpenProcessHandleWithAccess(
550 peer_pid,
551 base::kProcessAccessDuplicateHandle |
552 base::kProcessAccessQueryInformation |
553 base::kProcessAccessWaitForTermination,
554 &process)) {
555 process_->SetHandle(process);
556 OnProcessLaunched();
557 } else {
558 LOG(ERROR) << "Failed to get process handle";
559 }
560 }
561 if (debug_context_ == NULL) {
562 return;
563 }
564 debug_context_->SetNaClProcessHost(weak_factory_.GetWeakPtr());
565 if (RunningOnWOW64()) {
566 if (!NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
567 this, peer_pid)) {
568 debug_context_->AllowAndSendStartMessage();
569 }
570 } else {
571 // Start new thread for debug loop
572 // We can't use process_->GetData().handle because it doesn't have necessary
573 // access rights.
574 base::ProcessHandle process;
575 if (!base::OpenProcessHandleWithAccess(
576 peer_pid,
577 base::kProcessAccessQueryInformation |
578 base::kProcessAccessSuspendResume |
579 base::kProcessAccessTerminate |
580 base::kProcessAccessVMOperation |
581 base::kProcessAccessVMRead |
582 base::kProcessAccessVMWrite |
583 base::kProcessAccessWaitForTermination,
584 &process)) {
585 LOG(ERROR) << "Failed to open the process";
586 debug_context_->AllowAndSendStartMessage();
587 return;
588 }
589 base::Thread* dbg_thread = new base::Thread("Debug thread");
590 if (!dbg_thread->Start()) {
591 LOG(ERROR) << "Debug thread not started";
592 debug_context_->AllowAndSendStartMessage();
593 base::CloseProcessHandle(process);
594 return;
595 }
596 debug_context_->SetDebugThread(dbg_thread);
597 // System can not reallocate pid until we close process handle. So using
598 // pid in different thread is fine.
599 dbg_thread->message_loop()->PostTask(FROM_HERE,
600 base::Bind(&NaClProcessHost::DebugContext::AttachDebugger,
601 debug_context_, peer_pid, process));
602 }
603}
604#else
605void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
606}
607#endif
608
609#if defined(OS_WIN)
610void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
611 process_launched_by_broker_ = true;
612 process_->SetHandle(handle);
613 OnProcessLaunched();
614}
615
616void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker() {
617 debug_context_->AllowAndSendStartMessage();
618}
619#endif
620
621// Needed to handle sync messages in OnMessageRecieved.
622bool NaClProcessHost::Send(IPC::Message* msg) {
623 return process_->Send(msg);
624}
625
626#if defined(OS_WIN)
627scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33628 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23629 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46630 CommandLine* cmd_line = new CommandLine(nacl_gdb);
631 // We can't use PrependWrapper because our parameters contain spaces.
632 cmd_line->AppendArg("--eval-command");
633 const FilePath::StringType& irt_path =
634 NaClBrowser::GetInstance()->GetIrtFilePath().value();
635 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23636 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33637 if (!manifest_path.empty()) {
638 cmd_line->AppendArg("--eval-command");
639 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
640 manifest_path.value());
641 }
[email protected]31a665e72012-03-11 12:37:46642 cmd_line->AppendArg("--args");
643 const CommandLine::StringVector& argv = line->argv();
644 for (size_t i = 0; i < argv.size(); i++) {
645 cmd_line->AppendArgNative(argv[i]);
646 }
647 return scoped_ptr<CommandLine>(cmd_line);
648}
[email protected]5b974952012-04-05 18:18:23649#elif defined(OS_LINUX)
650namespace {
651class NaClGdbWatchDelegate : public MessageLoopForIO::Watcher {
652 public:
653 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
654 NaClGdbWatchDelegate(int fd_read, int fd_write,
655 const base::Closure& reply)
656 : fd_read_(fd_read),
657 fd_write_(fd_write),
658 reply_(reply) {}
659
660 ~NaClGdbWatchDelegate() {
661 if (HANDLE_EINTR(close(fd_read_)) != 0)
662 DLOG(ERROR) << "close(fd_read_) failed";
663 if (HANDLE_EINTR(close(fd_write_)) != 0)
664 DLOG(ERROR) << "close(fd_write_) failed";
665 }
666
667 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
668 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
669
670 private:
671 int fd_read_;
672 int fd_write_;
673 base::Closure reply_;
674};
675
676void NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(int fd) {
677 char buf;
678 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
679 LOG(ERROR) << "Failed to sync with nacl-gdb";
680 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
681}
682} // namespace
683
684bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
685 CommandLine::StringType nacl_gdb =
686 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
687 switches::kNaClGdb);
688 CommandLine::StringVector argv;
689 // We don't support spaces inside arguments in --nacl-gdb switch.
690 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
691 CommandLine cmd_line(argv);
692 cmd_line.AppendArg("--eval-command");
693 const FilePath::StringType& irt_path =
694 NaClBrowser::GetInstance()->GetIrtFilePath().value();
695 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
696 FilePath manifest_path = GetManifestPath();
697 if (!manifest_path.empty()) {
698 cmd_line.AppendArg("--eval-command");
699 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
700 manifest_path.value());
701 }
702 cmd_line.AppendArg("--eval-command");
703 cmd_line.AppendArg("attach " + base::IntToString(pid));
704 int fds[2];
705 if (pipe(fds) != 0)
706 return false;
707 // Tell the debugger to send a byte to the writable end of the pipe.
708 // We use a file descriptor in our process because the debugger will be
709 // typically launched in a separate terminal, and a lot of terminals close all
710 // file descriptors before launching external programs.
711 cmd_line.AppendArg("--eval-command");
712 cmd_line.AppendArg("dump binary value /proc/" +
713 base::IntToString(base::GetCurrentProcId()) +
714 "/fd/" + base::IntToString(fds[1]) + " (char)0");
715 // wait on fds[0]
716 // If the debugger crashes before attaching to the NaCl process, the user can
717 // release resources by terminating the NaCl loader in Chrome Task Manager.
718 nacl_gdb_watcher_delegate_.reset(
719 new NaClGdbWatchDelegate(
720 fds[0], fds[1],
721 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
722 weak_factory_.GetWeakPtr())));
723 MessageLoopForIO::current()->WatchFileDescriptor(
724 fds[0],
725 true,
726 MessageLoopForIO::WATCH_READ,
727 &nacl_gdb_watcher_,
728 nacl_gdb_watcher_delegate_.get());
729 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
730}
731
732void NaClProcessHost::OnNaClGdbAttached() {
733 wait_for_nacl_gdb_ = false;
734 nacl_gdb_watcher_.StopWatchingFileDescriptor();
735 nacl_gdb_watcher_delegate_.reset();
736 OnProcessLaunched();
737}
738#endif
739
740FilePath NaClProcessHost::GetManifestPath() {
741 GURL manifest_url = GURL(process_->GetData().name);
742 const Extension* extension = extension_info_map_->extensions()
743 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url));
744 if (extension != NULL && manifest_url.SchemeIs(chrome::kExtensionScheme)) {
745 std::string path = manifest_url.path();
746 TrimString(path, "/", &path); // Remove first slash
747 return extension->path().AppendASCII(path);
748 }
749 return FilePath();
750}
[email protected]31a665e72012-03-11 12:37:46751
[email protected]fb1277e82009-11-21 20:32:30752bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40753 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44754 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46755 return false;
756
[email protected]e3fc75a2011-05-05 08:20:42757 CommandLine::StringType nacl_loader_prefix;
758#if defined(OS_POSIX)
759 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
760 switches::kNaClLoaderCmdPrefix);
761#endif // defined(OS_POSIX)
762
[email protected]d032f492009-09-29 00:33:46763 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36764
765#if defined(OS_MACOSX)
766 // The Native Client process needs to be able to allocate a 1GB contiguous
767 // region to use as the client environment's virtual address space. ASLR
768 // (PIE) interferes with this by making it possible that no gap large enough
769 // to accomodate this request will exist in the child process' address
770 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
771 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49772 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36773#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49774 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
775 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36776#else
[email protected]4cb43102011-12-02 20:24:49777 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36778#endif
779
[email protected]4cb43102011-12-02 20:24:49780 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30781 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46782 return false;
783
[email protected]31a665e72012-03-11 12:37:46784#if defined(OS_WIN)
785 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
786 if (RunningOnWOW64()) {
787 FilePath module_path;
788 if (!PathService::Get(base::FILE_MODULE, &module_path))
789 return false;
790 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
791 }
792#endif
793
[email protected]33a05af2012-03-02 18:15:51794 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
795 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13796
[email protected]05076ba22010-07-30 05:59:57797 cmd_line->AppendSwitchASCII(switches::kProcessType,
798 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44799 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44800 if (logging::DialogsAreSuppressed())
801 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46802
[email protected]e3fc75a2011-05-05 08:20:42803 if (!nacl_loader_prefix.empty())
804 cmd_line->PrependWrapper(nacl_loader_prefix);
805
[email protected]31a665e72012-03-11 12:37:46806 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
807 switches::kNaClGdb);
808 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23809#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46810 cmd_line->AppendSwitch(switches::kNoSandbox);
811 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04812 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46813 // We can't use process_->Launch() because OnProcessLaunched will be called
814 // with process_->GetData().handle filled by handle of gdb process. This
815 // handle will be used to duplicate handles for NaCl process and as
816 // a result NaCl process will not be able to use them.
817 //
818 // So we don't fill process_->GetData().handle and wait for
819 // OnChannelConnected to get handle of NaCl process from its pid. Then we
820 // call OnProcessLaunched.
821 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23822#elif defined(OS_LINUX)
823 wait_for_nacl_gdb_ = true;
824#endif
[email protected]31a665e72012-03-11 12:37:46825 }
826
[email protected]103607e2010-02-01 18:57:09827 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46828#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52829 if (RunningOnWOW64()) {
[email protected]644b2e22012-03-25 01:41:07830 return NaClBrokerService::GetInstance()->LaunchLoader(this, channel_id);
[email protected]4bdde602010-06-16 03:17:35831 } else {
[email protected]33a05af2012-03-02 18:15:51832 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35833 }
[email protected]103607e2010-02-01 18:57:09834#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40835 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20836 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51837 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09838#endif
[email protected]d032f492009-09-29 00:33:46839
[email protected]fb1277e82009-11-21 20:32:30840 return true;
[email protected]d032f492009-09-29 00:33:46841}
842
[email protected]646e15552012-04-06 22:01:04843bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
844 bool handled = true;
845 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
846 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
847 OnQueryKnownToValidate)
848 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
849 OnSetKnownToValidate)
850 IPC_MESSAGE_UNHANDLED(handled = false)
851 IPC_END_MESSAGE_MAP()
852 return handled;
[email protected]103607e2010-02-01 18:57:09853}
854
[email protected]463ea5f2011-12-03 06:57:47855void NaClProcessHost::OnProcessCrashed(int exit_code) {
856 std::string message = base::StringPrintf(
857 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
858 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09859}
860
[email protected]773ebb92011-11-15 19:06:52861void NaClProcessHost::OnProcessLaunched() {
[email protected]5b974952012-04-05 18:18:23862#if defined(OS_LINUX)
863 if (wait_for_nacl_gdb_) {
864 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
865 // OnProcessLaunched will be called with wait_for_nacl_gdb_ = false once
866 // debugger is attached to the program.
867 return;
868 }
869 LOG(ERROR) << "Failed to launch debugger";
870 // Continue execution without debugger.
871 }
872#endif
[email protected]773ebb92011-11-15 19:06:52873 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
874
875 if (nacl_browser->IrtAvailable()) {
876 // The IRT is already open. Away we go.
[email protected]1657e6d2012-03-30 20:28:00877 SendStart();
[email protected]773ebb92011-11-15 19:06:52878 } else {
879 // We're waiting for the IRT to be open.
[email protected]98999e802011-12-21 21:54:43880 if (!nacl_browser->MakeIrtAvailable(
881 base::Bind(&NaClProcessHost::IrtReady,
882 weak_factory_.GetWeakPtr())))
883 delete this;
[email protected]773ebb92011-11-15 19:06:52884 }
885}
886
887// The asynchronous attempt to get the IRT file open has completed.
888void NaClProcessHost::IrtReady() {
889 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
890
891 if (nacl_browser->IrtAvailable()) {
[email protected]1657e6d2012-03-30 20:28:00892 SendStart();
[email protected]773ebb92011-11-15 19:06:52893 } else {
894 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43895 delete this;
896 }
897}
898
[email protected]1657e6d2012-03-30 20:28:00899void NaClProcessHost::SendStart() {
900 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
901 base::PlatformFile irt_file = nacl_browser->IrtFile();
[email protected]773ebb92011-11-15 19:06:52902 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
903
[email protected]c47ec402010-07-29 10:20:49904 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30905 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30906
[email protected]1d8a3d1f2011-02-19 07:11:52907 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49908#if defined(OS_WIN)
909 // Copy the handle into the renderer process.
910 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04911 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
912 reinterpret_cast<HANDLE>(
913 internal_->sockets_for_renderer[i]),
914 chrome_render_message_filter_->peer_handle(),
915 &handle_in_renderer,
916 0, // Unused given DUPLICATE_SAME_ACCESS.
917 FALSE,
918 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
919 LOG(ERROR) << "DuplicateHandle() failed";
920 delete this;
921 return;
922 }
[email protected]c47ec402010-07-29 10:20:49923 handles_for_renderer.push_back(
924 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
925#else
926 // No need to dup the imc_handle - we don't pass it anywhere else so
927 // it cannot be closed.
928 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52929 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49930 imc_handle.auto_close = true;
931 handles_for_renderer.push_back(imc_handle);
932#endif
933 }
934
[email protected]4967f792012-01-20 22:14:40935 const ChildProcessData& data = process_->GetData();
[email protected]c47ec402010-07-29 10:20:49936#if defined(OS_WIN)
937 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04938 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
[email protected]4967f792012-01-20 22:14:40939 data.handle,
[email protected]909c2402011-05-09 11:39:04940 chrome_render_message_filter_->peer_handle(),
941 &nacl_process_handle,
942 PROCESS_DUP_HANDLE,
943 FALSE,
944 0)) {
945 LOG(ERROR) << "DuplicateHandle() failed";
946 delete this;
947 return;
948 }
[email protected]fb1277e82009-11-21 20:32:30949#else
[email protected]fb1277e82009-11-21 20:32:30950 // We use pid as process handle on Posix
[email protected]4967f792012-01-20 22:14:40951 nacl_process_handle = data.handle;
[email protected]fb1277e82009-11-21 20:32:30952#endif
953
954 // Get the pid of the NaCl process
[email protected]4967f792012-01-20 22:14:40955 base::ProcessId nacl_process_id = base::GetProcId(data.handle);
[email protected]fb1277e82009-11-21 20:32:30956
[email protected]2ccf45c2011-08-19 23:35:50957 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49958 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52959 chrome_render_message_filter_->Send(reply_msg_);
960 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30961 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52962 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30963
[email protected]c47ec402010-07-29 10:20:49964 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52965 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]4967f792012-01-20 22:14:40966 if (!SendHandleToSelLdr(data.handle,
[email protected]773ebb92011-11-15 19:06:52967 internal_->sockets_for_sel_ldr[i], true,
968 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04969 delete this;
[email protected]c47ec402010-07-29 10:20:49970 return;
971 }
[email protected]773ebb92011-11-15 19:06:52972 }
973
974 // Send over the IRT file handle. We don't close our own copy!
[email protected]4967f792012-01-20 22:14:40975 if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) {
[email protected]773ebb92011-11-15 19:06:52976 delete this;
977 return;
[email protected]c47ec402010-07-29 10:20:49978 }
979
[email protected]ab88d1542011-11-18 22:52:00980#if defined(OS_MACOSX)
981 // For dynamic loading support, NaCl requires a file descriptor that
982 // was created in /tmp, since those created with shm_open() are not
983 // mappable with PROT_EXEC. Rather than requiring an extra IPC
984 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25985 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31986 base::SharedMemoryCreateOptions options;
987 options.size = 1;
988 options.executable = true;
989 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30990 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04991 delete this;
[email protected]2c68bf032010-11-11 23:16:30992 return;
993 }
[email protected]cbbe7842011-11-17 22:01:25994 nacl::FileDescriptor memory_fd;
995 memory_fd.fd = dup(memory_buffer.handle().fd);
996 if (memory_fd.fd < 0) {
997 LOG(ERROR) << "Failed to dup() a file descriptor";
998 delete this;
999 return;
1000 }
1001 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:301002 handles_for_sel_ldr.push_back(memory_fd);
1003#endif
1004
[email protected]1657e6d2012-03-30 20:28:001005 // Sending the version string over IPC avoids linkage issues in cases where
1006 // NaCl is not compiled into the main Chromium executable or DLL.
1007 chrome::VersionInfo version_info;
[email protected]5ca93be2012-03-21 20:04:061008 IPC::Message* start_message =
[email protected]1657e6d2012-03-30 20:28:001009 new NaClProcessMsg_Start(handles_for_sel_ldr,
1010 nacl_browser->GetValidatorCacheKey(),
1011 version_info.CreateVersionString(),
1012 enable_exception_handling_);
[email protected]b39c6d92012-01-31 16:38:411013#if defined(OS_WIN)
[email protected]5ca93be2012-03-21 20:04:061014 if (debug_context_ != NULL) {
1015 debug_context_->SetStartMessage(start_message);
[email protected]b39c6d92012-01-31 16:38:411016 debug_context_->SendStartMessage();
1017 } else {
[email protected]5ca93be2012-03-21 20:04:061018 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:411019 }
1020#else
[email protected]5ca93be2012-03-21 20:04:061021 process_->Send(start_message);
[email protected]b39c6d92012-01-31 16:38:411022#endif
1023
[email protected]1d8a3d1f2011-02-19 07:11:521024 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:461025}
1026
[email protected]4a0141b2012-03-27 01:15:301027void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
1028 bool* result) {
1029 *result = NaClBrowser::GetInstance()->QueryKnownToValidate(signature);
1030}
1031
1032void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
1033 NaClBrowser::GetInstance()->SetKnownToValidate(signature);
1034}