15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/nacl/browser/nacl_process_host.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/base_switches.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
15ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/process/launch.h"
19bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch#include "base/process/process_iterator.h"
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/rand_util.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "components/nacl/browser/nacl_browser.h"
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/nacl/browser/nacl_browser_delegate.h"
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/nacl/browser/nacl_host_message_filter.h"
32ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "components/nacl/common/nacl_cmd_line.h"
33a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "components/nacl/common/nacl_host_messages.h"
34a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "components/nacl/common/nacl_messages.h"
357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "components/nacl/common/nacl_process_type.h"
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/nacl/common/nacl_switches.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_child_process_host.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/child_process_data.h"
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/plugin_service.h"
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "content/public/browser/render_process_host.h"
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "content/public/browser/web_contents.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/child_process_host.h"
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "content/public/common/content_switches.h"
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/process_type.h"
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/sandboxed_process_launcher_delegate.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_channel.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_switches.h"
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "native_client/src/public/nacl_file_info.h"
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "native_client/src/shared/imc/nacl_imc_c.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/socket/tcp_listen_socket.h"
537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "ppapi/host/host_factory.h"
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/host/ppapi_host.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ppapi/shared_impl/ppapi_constants.h"
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/shared_impl/ppapi_nacl_plugin_args.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_channel_posix.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/thread.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/scoped_handle.h"
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/nacl/browser/nacl_broker_service_win.h"
70a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "components/nacl/common/nacl_debug_exception_handler_win.h"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/sandbox_init.h"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::ChildProcessData;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::ChildProcessHost;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ppapi::proxy::SerializedHandle;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace nacl {
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_WIN)
823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace {
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Looks for the largest contiguous unallocated region of address
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// space and returns it via |*out_addr| and |*out_size|.
863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FindAddressSpace(base::ProcessHandle process,
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      char** out_addr, size_t* out_size) {
883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  *out_addr = NULL;
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  *out_size = 0;
903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  char* addr = 0;
913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  while (true) {
923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    MEMORY_BASIC_INFORMATION info;
933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    size_t result = VirtualQueryEx(process, static_cast<void*>(addr),
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                   &info, sizeof(info));
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (result < sizeof(info))
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      break;
973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (info.State == MEM_FREE && info.RegionSize > *out_size) {
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      *out_addr = addr;
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      *out_size = info.RegionSize;
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    addr += info.RegionSize;
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifdef _DLL
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool IsInPath(const std::string& path_env_var, const std::string& dir) {
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<std::string> split;
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::SplitString(path_env_var, ';', &split);
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (std::vector<std::string>::const_iterator i(split.begin());
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       i != split.end();
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       ++i) {
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (*i == dir)
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // _DLL
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Allocates |size| bytes of address space in the given process at a
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// randomised address.
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size) {
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  char* addr;
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  size_t avail_size;
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  FindAddressSpace(process, &addr, &avail_size);
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (avail_size < size)
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return NULL;
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  size_t offset = base::RandGenerator(avail_size - size);
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  const int kPageSize = 0x10000;
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void* request_addr =
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      reinterpret_cast<void*>(reinterpret_cast<uint64>(addr + offset)
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              & ~(kPageSize - 1));
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return VirtualAllocEx(process, request_addr, size,
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        MEM_RESERVE, PAGE_NOACCESS);
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RunningOnWOW64() {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (base::win::OSInfo::GetInstance()->wow64_status() ==
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::win::OSInfo::WOW64_ENABLED);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // defined(OS_WIN)
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NOTE: changes to this class need to be reviewed by the security team.
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class NaClSandboxedProcessLauncherDelegate
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : public content::SandboxedProcessLauncherDelegate {
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NaClSandboxedProcessLauncherDelegate(ChildProcessHost* host)
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_POSIX)
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      : ipc_fd_(host->TakeClientFileDescriptor())
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {}
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~NaClSandboxedProcessLauncherDelegate() {}
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_WIN)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void PostSpawnTarget(base::ProcessHandle process) {
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // address space to prevent later failure due to address space fragmentation
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // from .dll loading. The NaCl process will attempt to locate this space by
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // scanning the address space using VirtualQuery.
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(bbudge) Handle the --no-sandbox case.
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // http://code.google.com/p/nativeclient/issues/detail?id=2131
1733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const SIZE_T kNaClSandboxSize = 1 << 30;
1743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!nacl::AllocateAddressSpaceASLR(process, kNaClSandboxSize)) {
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DLOG(WARNING) << "Failed to reserve address space for Native Client";
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#elif defined(OS_POSIX)
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool ShouldUseZygote() OVERRIDE {
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return true;
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual int GetIpcFd() OVERRIDE {
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return ipc_fd_;
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // OS_WIN
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_POSIX)
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int ipc_fd_;
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // OS_POSIX
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SetCloseOnExec(NaClHandle fd) {
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int flags = fcntl(fd, F_GETFD);
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_NE(flags, -1);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_EQ(rc, 0);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShareHandleToSelLdr(
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::ProcessHandle processh,
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NaClHandle sourceh,
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool close_source,
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HANDLE channel;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int flags = DUPLICATE_SAME_ACCESS;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (close_source)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flags |= DUPLICATE_CLOSE_SOURCE;
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!DuplicateHandle(GetCurrentProcess(),
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       reinterpret_cast<HANDLE>(sourceh),
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       processh,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       &channel,
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       0,  // Unused given DUPLICATE_SAME_ACCESS.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       FALSE,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       flags)) {
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "DuplicateHandle() failed";
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  handles_for_sel_ldr->push_back(
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<nacl::FileDescriptor>(channel));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl::FileDescriptor channel;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  channel.fd = sourceh;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  channel.auto_close = close_source;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  handles_for_sel_ldr->push_back(channel);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochunsigned NaClProcessHost::keepalive_throttle_interval_milliseconds_ =
236effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ppapi::kKeepaliveThrottleIntervalDefaultMilliseconds;
237effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
239116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::File nexe_file,
2405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 const NaClFileToken& nexe_token,
2416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                                 ppapi::PpapiPermissions permissions,
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 int render_view_id,
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 uint32 permission_bits,
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 bool uses_irt,
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 bool uses_nonsfi_mode,
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                 bool enable_dyncode_syscalls,
2477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                 bool enable_exception_handling,
2483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                 bool enable_crash_throttling,
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                 bool off_the_record,
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                 const base::FilePath& profile_directory)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : manifest_url_(manifest_url),
252116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      nexe_file_(nexe_file.Pass()),
2535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      nexe_token_(nexe_token),
2546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      permissions_(permissions),
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      process_launched_by_broker_(false),
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reply_msg_(NULL),
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      debug_exception_handler_requested_(false),
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      uses_irt_(uses_irt),
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      uses_nonsfi_mode_(uses_nonsfi_mode),
2647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      enable_debug_stub_(false),
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      enable_dyncode_syscalls_(enable_dyncode_syscalls),
2667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      enable_exception_handling_(enable_exception_handling),
2673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      enable_crash_throttling_(enable_crash_throttling),
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      off_the_record_(off_the_record),
269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_directory_(profile_directory),
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      render_view_id_(render_view_id),
2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      weak_factory_(this) {
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  process_.reset(content::BrowserChildProcessHost::Create(
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PROCESS_TYPE_NACL_LOADER, this));
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the display name so the user knows what plugin the process is running.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We aren't on the UI thread so getting the pref locale for language
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // formatting isn't possible, so IDN will be lost, but this is probably OK
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for this use case.
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  process_->SetName(net::FormatUrl(manifest_url_, std::string()));
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switches::kEnableNaClDebug);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NaClProcessHost::~NaClProcessHost() {
286424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Report exit status only if the process was successfully started.
287424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (process_->GetData().handle != base::kNullProcessHandle) {
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    int exit_code = 0;
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    process_->GetTerminationStatus(false /* known_dead */, &exit_code);
290424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    std::string message =
291424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        base::StringPrintf("NaCl process exited with status %i (0x%x)",
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                           exit_code, exit_code);
293424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (exit_code == 0) {
294f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      VLOG(1) << message;
295424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    } else {
296424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      LOG(ERROR) << message;
297424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
298cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NaClBrowser::GetInstance()->OnProcessEnd(process_->GetData().id);
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reply_msg_) {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The process failed to launch for some reason.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Don't keep the renderer hanging.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reply_msg_->set_reply_error();
3057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    nacl_host_message_filter_->Send(reply_msg_);
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (process_launched_by_broker_) {
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NaClBrokerService::GetInstance()->OnLoaderDied();
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void NaClProcessHost::OnProcessCrashed(int exit_status) {
3153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (enable_crash_throttling_ &&
3163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      !CommandLine::ForCurrentProcess()->HasSwitch(
3173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          switches::kDisablePnaclCrashThrottling)) {
318f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    NaClBrowser::GetInstance()->OnProcessCrashed();
3193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
3203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
3213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is called at browser startup.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void NaClProcessHost::EarlyStartup() {
325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser::GetInstance()->EarlyStartup();
326cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Inform NaClBrowser that we exist and will have a debug port at some point.
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open the IRT file early to make sure that it isn't replaced out from
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // under us by autoupdate.
330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser::GetInstance()->EnsureIrtAvailable();
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine* cmd = CommandLine::ForCurrentProcess();
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN(
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "NaCl.nacl-gdb",
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN(
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "NaCl.nacl-gdb-script",
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN(
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "NaCl.enable-nacl-debug",
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cmd->HasSwitch(switches::kEnableNaClDebug));
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string nacl_debug_mask =
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      cmd->GetSwitchValueASCII(switches::kNaClDebugMask);
344a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // By default, exclude debugging SSH and the PNaCl translator.
345a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // about::flags only allows empty flags as the default, so replace
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the empty setting with the default. To debug all apps, use a wild-card.
347a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (nacl_debug_mask.empty()) {
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    nacl_debug_mask = "!*://*/*ssh_client.nmf,chrome://pnacl-translator/*";
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NaClBrowser::GetDelegate()->SetDebugPatterns(nacl_debug_mask);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
353effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// static
354effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid NaClProcessHost::SetPpapiKeepAliveThrottleForTesting(
355effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    unsigned milliseconds) {
356effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  keepalive_throttle_interval_milliseconds_ = milliseconds;
357effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
358effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::Launch(
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    NaClHostMessageFilter* nacl_host_message_filter,
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC::Message* reply_msg,
362eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const base::FilePath& manifest_path) {
3637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  nacl_host_message_filter_ = nacl_host_message_filter;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  reply_msg_ = reply_msg;
365eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  manifest_path_ = manifest_path;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Do not launch the requested NaCl module if NaCl is marked "unstable" due
3683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // to too many crashes within a given time period.
3693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (enable_crash_throttling_ &&
3703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      !CommandLine::ForCurrentProcess()->HasSwitch(
3713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          switches::kDisablePnaclCrashThrottling) &&
372f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      NaClBrowser::GetInstance()->IsThrottled()) {
3733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    SendErrorToRenderer("Process creation was throttled due to excessive"
3743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        " crashes");
3753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    delete this;
3763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return;
3773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
3783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
379a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const CommandLine* cmd = CommandLine::ForCurrentProcess();
380a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#if defined(OS_WIN)
381a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (cmd->HasSwitch(switches::kEnableNaClDebug) &&
382a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      !cmd->HasSwitch(switches::kNoSandbox)) {
383a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // We don't switch off sandbox automatically for security reasons.
384a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SendErrorToRenderer("NaCl's GDB debug stub requires --no-sandbox flag"
385a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                        " on Windows. See crbug.com/265624.");
386a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    delete this;
387a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
388a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
389a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#endif
390a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (cmd->HasSwitch(switches::kNaClGdb) &&
391a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      !cmd->HasSwitch(switches::kEnableNaClDebug)) {
392a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    LOG(WARNING) << "--nacl-gdb flag requires --enable-nacl-debug flag";
393a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
394a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Start getting the IRT open asynchronously while we launch the NaCl process.
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We'll make sure this actually finished in StartWithLaunchedProcess, below.
397f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl_browser->EnsureAllResourcesAvailable();
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!nacl_browser->IsOk()) {
400ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SendErrorToRenderer("could not find all the resources needed"
401ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                        " to launch the process");
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
406e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (uses_nonsfi_mode_) {
407cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool nonsfi_mode_forced_by_command_line = false;
408cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool nonsfi_mode_allowed = false;
409e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#if defined(OS_LINUX)
410cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    nonsfi_mode_forced_by_command_line =
411cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        cmd->HasSwitch(switches::kEnableNaClNonSfiMode);
412cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
413cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    nonsfi_mode_allowed = NaClBrowser::GetDelegate()->IsNonSfiModeAllowed(
414cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        nacl_host_message_filter->profile_directory(), manifest_url_);
415cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
416e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif
417cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool nonsfi_mode_enabled =
418cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        nonsfi_mode_forced_by_command_line || nonsfi_mode_allowed;
419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!nonsfi_mode_enabled) {
421cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      SendErrorToRenderer(
422cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          "NaCl non-SFI mode is not available for this platform"
423cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          " and NaCl module.");
424e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      delete this;
425e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      return;
426e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    }
427116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  } else {
428116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Rather than creating a socket pair in the renderer, and passing
429116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // one side through the browser to sel_ldr, socket pairs are created
430116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // in the browser and then passed to the renderer and sel_ldr.
431116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    //
432116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // This is mainly for the benefit of Windows, where sockets cannot
433116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // be passed in messages, but are copied via DuplicateHandle().
434116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // This means the sandboxed renderer cannot send handles to the
435116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // browser process.
436e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
437116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    NaClHandle pair[2];
438116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Create a connected socket
439116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (NaClSocketPair(pair) == -1) {
440116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      SendErrorToRenderer("NaClSocketPair() failed");
441116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      delete this;
442116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
443116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
4441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    socket_for_renderer_ = base::File(pair[0]);
4451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    socket_for_sel_ldr_ = base::File(pair[1]);
446116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    SetCloseOnExec(pair[0]);
447116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    SetCloseOnExec(pair[1]);
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Create a shared memory region that the renderer and plugin share for
45103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // reporting crash information.
45203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  crash_info_shmem_.CreateAnonymous(kNaClCrashInfoShmemSize);
45303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Launch the process
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!LaunchSelLdr()) {
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kNaClGdb).empty()) {
4637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LaunchNaClGdb();
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  process_launched_by_broker_ = true;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  process_->SetHandle(handle);
471cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetDebugStubPort(nacl::kGdbDebugStubPortUnknown);
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!StartWithLaunchedProcess())
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Send(reply);
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
483010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Needed to handle sync messages in OnMessageReceived.
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::Send(IPC::Message* msg) {
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return process_->Send(msg);
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool NaClProcessHost::LaunchNaClGdb() {
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
4907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath nacl_gdb =
4917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb);
4927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CommandLine cmd_line(nacl_gdb);
4937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#else
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine::StringType nacl_gdb =
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CommandLine::ForCurrentProcess()->GetSwitchValueNative(
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kNaClGdb);
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine::StringVector argv;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't support spaces inside arguments in --nacl-gdb switch.
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine cmd_line(argv);
5017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line.AppendArg("--eval-command");
5037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath::StringType irt_path(
504f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      NaClBrowser::GetInstance()->GetIrtFilePath().value());
5057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Avoid back slashes because nacl-gdb uses posix escaping rules on Windows.
5067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // See issue https://code.google.com/p/nativeclient/issues/detail?id=3482.
5077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::replace(irt_path.begin(), irt_path.end(), '\\', '/');
5087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt \"") + irt_path +
5097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                           FILE_PATH_LITERAL("\""));
510eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!manifest_path_.empty()) {
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line.AppendArg("--eval-command");
512eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath::StringType manifest_path_value(manifest_path_.value());
5137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::replace(manifest_path_value.begin(), manifest_path_value.end(),
5147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                 '\\', '/');
5157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest \"") +
5167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                             manifest_path_value + FILE_PATH_LITERAL("\""));
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line.AppendArg("--eval-command");
5197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  cmd_line.AppendArg("target remote :4014");
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switches::kNaClGdbScript);
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!script.empty()) {
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line.AppendArg("--command");
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line.AppendArgNative(script.value());
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::LaunchSelLdr() {
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string channel_id = process_->GetHost()->CreateChannel();
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (channel_id.empty()) {
532ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SendErrorToRenderer("CreateChannel() failed");
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Build command line for nacl.
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The Native Client process needs to be able to allocate a 1GB contiguous
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // region to use as the client environment's virtual address space. ASLR
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (PIE) interferes with this by making it possible that no gap large enough
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to accomodate this request will exist in the child process' address
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://code.google.com/p/nativeclient/issues/detail?id=2043.
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int flags = ChildProcessHost::CHILD_NO_PIE;
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_LINUX)
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int flags = ChildProcessHost::CHILD_ALLOW_SELF;
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int flags = ChildProcessHost::CHILD_NORMAL;
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (exe_path.empty())
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (RunningOnWOW64()) {
559f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!NaClBrowser::GetInstance()->GetNaCl64ExePath(&exe_path)) {
560ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      SendErrorToRenderer("could not get path to nacl64.exe");
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
564a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifdef _DLL
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // When using the DLL CRT on Windows, we need to amend the PATH to include
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // the location of the x64 CRT DLLs. This is only the case when using a
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // component=shared_library build (i.e. generally dev debug builds). The
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // x86 CRT DLLs are in e.g. out\Debug for chrome.exe etc., so the x64 ones
569a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // are put in out\Debug\x64 which we add to the PATH here so that loader
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // can find them. See http://crbug.com/346034.
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<base::Environment> env(base::Environment::Create());
572a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    static const char kPath[] = "PATH";
573a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::string old_path;
574a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::FilePath module_path;
575a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!PathService::Get(base::FILE_MODULE, &module_path)) {
576a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      SendErrorToRenderer("could not get path to current module");
577a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return false;
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::string x64_crt_path =
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::WideToUTF8(module_path.DirName().Append(L"x64").value());
581a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!env->GetVar(kPath, &old_path)) {
582a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      env->SetVar(kPath, x64_crt_path);
583a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    } else if (!IsInPath(old_path, x64_crt_path)) {
584a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::string new_path(old_path);
585a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new_path.append(";");
586a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new_path.append(x64_crt_path);
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      env->SetVar(kPath, new_path);
588a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
589a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // _DLL
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CopyNaClCommandLineArguments(cmd_line.get());
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line->AppendSwitchASCII(switches::kProcessType,
597e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                              (uses_nonsfi_mode_ ?
598e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                               switches::kNaClLoaderNonSfiProcess :
599e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                               switches::kNaClLoaderProcess));
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
601f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (NaClBrowser::GetDelegate()->DialogsAreSuppressed())
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line->AppendSwitch(switches::kNoErrorDialogs);
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Windows we might need to start the broker process to launch a new loader
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (RunningOnWOW64()) {
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!NaClBrokerService::GetInstance()->LaunchLoader(
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            weak_factory_.GetWeakPtr(), channel_id)) {
609ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      SendErrorToRenderer("broker service did not launch process");
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
612a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return true;
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
615a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  process_->Launch(
616a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new NaClSandboxedProcessLauncherDelegate(process_->GetHost()),
617a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      cmd_line.release());
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool handled = true;
623010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (uses_nonsfi_mode_) {
624010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // IPC messages relating to NaCl's validation cache must not be exposed
625010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // in Non-SFI Mode, otherwise a Non-SFI nexe could use
626010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // SetKnownToValidate to create a hole in the SFI sandbox.
627010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
628010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelsCreated,
629010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          OnPpapiChannelsCreated)
630010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_UNHANDLED(handled = false)
631010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_END_MESSAGE_MAP()
632010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  } else {
633010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
634010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
635010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          OnQueryKnownToValidate)
636010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
637010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          OnSetKnownToValidate)
638010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken,
639010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      OnResolveFileToken)
6401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      IPC_MESSAGE_HANDLER(NaClProcessMsg_ResolveFileTokenAsync,
6411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                          OnResolveFileTokenAsync)
6421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
644010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER_DELAY_REPLY(
645010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          NaClProcessMsg_AttachDebugExceptionHandler,
646010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          OnAttachDebugExceptionHandler)
647cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      IPC_MESSAGE_HANDLER(NaClProcessHostMsg_DebugStubPortSelected,
648cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                          OnDebugStubPortSelected)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
650010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelsCreated,
651010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          OnPpapiChannelsCreated)
652010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      IPC_MESSAGE_UNHANDLED(handled = false)
653010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_END_MESSAGE_MAP()
654010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return handled;
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnProcessLaunched() {
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!StartWithLaunchedProcess())
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Called when the NaClBrowser singleton has been fully initialized.
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnResourcesReady() {
665f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!nacl_browser->IsReady()) {
667ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SendErrorToRenderer("could not acquire shared resources needed by NaCl");
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
669a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (!StartNaClExecution()) {
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::ReplyToRenderer(
675a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const IPC::ChannelHandle& ppapi_channel_handle,
6760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const IPC::ChannelHandle& trusted_channel_handle,
6770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const IPC::ChannelHandle& manifest_service_channel_handle) {
6787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_WIN)
6797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If we are on 64-bit Windows, the NaCl process's sandbox is
6807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // managed by a different process from the renderer's sandbox.  We
6817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // need to inform the renderer's sandbox about the NaCl process so
6827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // that the renderer can send handles to the NaCl process using
6837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // BrokerDuplicateHandle().
6847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (RunningOnWOW64()) {
6857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
686ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      SendErrorToRenderer("BrokerAddTargetPeer() failed");
6877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return false;
6887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
6897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
6907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
6917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
692a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FileDescriptor imc_handle_for_renderer;
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Copy the handle into the renderer process.
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HANDLE handle_in_renderer;
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!DuplicateHandle(base::GetCurrentProcessHandle(),
6971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       socket_for_renderer_.TakePlatformFile(),
6987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                       nacl_host_message_filter_->PeerHandle(),
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       &handle_in_renderer,
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       0,  // Unused given DUPLICATE_SAME_ACCESS.
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       FALSE,
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
703ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SendErrorToRenderer("DuplicateHandle() failed");
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
706a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  imc_handle_for_renderer = reinterpret_cast<FileDescriptor>(
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      handle_in_renderer);
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // No need to dup the imc_handle - we don't pass it anywhere else so
7102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it cannot be closed.
711f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FileDescriptor imc_handle;
7121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  imc_handle.fd = socket_for_renderer_.TakePlatformFile();
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  imc_handle.auto_close = true;
714a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  imc_handle_for_renderer = imc_handle;
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ChildProcessData& data = process_->GetData();
71803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::SharedMemoryHandle crash_info_shmem_renderer_handle;
71903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (!crash_info_shmem_.ShareToProcess(nacl_host_message_filter_->PeerHandle(),
72003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                        &crash_info_shmem_renderer_handle)) {
72103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    SendErrorToRenderer("ShareToProcess() failed");
72203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return false;
72303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
72403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
725ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  SendMessageToRenderer(
726a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      NaClLaunchResult(imc_handle_for_renderer,
727a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       ppapi_channel_handle,
728a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       trusted_channel_handle,
7290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                       manifest_service_channel_handle,
730a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       base::GetProcId(data.handle),
73103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                       data.id,
73203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                       crash_info_shmem_renderer_handle),
733ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      std::string() /* error_message */);
73403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
73503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Now that the crash information shmem handles have been shared with the
73603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // plugin and the renderer, the browser can close its handle.
73703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  crash_info_shmem_.Close();
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
741ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid NaClProcessHost::SendErrorToRenderer(const std::string& error_message) {
742ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  LOG(ERROR) << "NaCl process launch failed: " << error_message;
743f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SendMessageToRenderer(NaClLaunchResult(), error_message);
744ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
745ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
746ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid NaClProcessHost::SendMessageToRenderer(
747f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const NaClLaunchResult& result,
748ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const std::string& error_message) {
7491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(nacl_host_message_filter_.get());
750ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  DCHECK(reply_msg_);
7511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (nacl_host_message_filter_.get() != NULL && reply_msg_ != NULL) {
752ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    NaClHostMsg_LaunchNaCl::WriteReplyParams(
753ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        reply_msg_, result, error_message);
754ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    nacl_host_message_filter_->Send(reply_msg_);
755ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    nacl_host_message_filter_ = NULL;
756ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    reply_msg_ = NULL;
757ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
758ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
759ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
760cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void NaClProcessHost::SetDebugStubPort(int port) {
761cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
762cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  nacl_browser->SetProcessGdbDebugStubPort(process_->GetData().id, port);
763cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
764cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
765cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(OS_POSIX)
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TCP port we chose for NaCl debug stub. It can be any other number.
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static const int kInitialDebugStubPort = 4014;
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
769424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)net::SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
770424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  net::SocketDescriptor s = net::kInvalidSocket;
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We always try to allocate the default port first. If this fails, we then
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // allocate any available port.
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // On success, if the test system has register a handler
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // (GdbDebugStubPortListener), we fire a notification.
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int port = kInitialDebugStubPort;
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  s = net::TCPListenSocket::CreateAndBind("127.0.0.1", port);
7775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (s == net::kInvalidSocket) {
7782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (s != net::kInvalidSocket) {
781cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SetDebugStubPort(port);
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
783424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (s == net::kInvalidSocket) {
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "failed to open socket for debug stub";
785424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return net::kInvalidSocket;
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(WARNING) << "debug stub on port " << port;
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (listen(s, 1)) {
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "listen() failed on debug stub socket";
791a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (IGNORE_EINTR(close(s)) < 0)
7922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PLOG(ERROR) << "failed to close debug stub socket";
793424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return net::kInvalidSocket;
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return s;
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
799cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(OS_WIN)
800cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void NaClProcessHost::OnDebugStubPortSelected(uint16_t debug_stub_port) {
801cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CHECK(!uses_nonsfi_mode_);
802cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetDebugStubPort(debug_stub_port);
803cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
804cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
805cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::StartNaClExecution() {
807f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
809f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClStartParams params;
810116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Enable PPAPI proxy channel creation only for renderer processes.
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  params.enable_ipc_proxy = enable_ppapi_proxy();
813116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (uses_nonsfi_mode_) {
814116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Currently, non-SFI mode is supported only on Linux.
815116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_LINUX)
816116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // In non-SFI mode, we do not use SRPC. Make sure that the socketpair is
817116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // not created.
8181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    DCHECK(!socket_for_sel_ldr_.IsValid());
819116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
820116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  } else {
821010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
822010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.validation_cache_key = nacl_browser->GetValidationCacheKey();
823010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.version = NaClBrowser::GetDelegate()->GetVersionString();
824010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.enable_exception_handling = enable_exception_handling_;
825010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.enable_debug_stub = enable_debug_stub_ &&
826010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        NaClBrowser::GetDelegate()->URLMatchesDebugPatterns(manifest_url_);
827010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.uses_irt = uses_irt_;
828010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    params.enable_dyncode_syscalls = enable_dyncode_syscalls_;
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // TODO(teravest): Resolve the file tokens right now instead of making the
8315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // loader send IPC to resolve them later.
8325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params.nexe_token_lo = nexe_token_.lo;
8335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params.nexe_token_hi = nexe_token_.hi;
8345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
835116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const ChildProcessData& data = process_->GetData();
836116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!ShareHandleToSelLdr(data.handle,
8371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             socket_for_sel_ldr_.TakePlatformFile(),
8381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             true,
839010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             &params.handles)) {
8402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
841010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
842116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
843116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (params.uses_irt) {
844116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const base::File& irt_file = nacl_browser->IrtFile();
845116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      CHECK(irt_file.IsValid());
846116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // Send over the IRT file handle.  We don't close our own copy!
847116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (!ShareHandleToSelLdr(data.handle, irt_file.GetPlatformFile(), false,
848116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                               &params.handles)) {
849116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        return false;
850116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
851116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
854116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // For dynamic loading support, NaCl requires a file descriptor that
855116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // was created in /tmp, since those created with shm_open() are not
856116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // mappable with PROT_EXEC.  Rather than requiring an extra IPC
857116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // round trip out of the sandbox, we create an FD here.
858116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::SharedMemory memory_buffer;
859116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::SharedMemoryCreateOptions options;
860116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    options.size = 1;
861116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    options.executable = true;
862116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!memory_buffer.Create(options)) {
863116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      DLOG(ERROR) << "Failed to allocate memory buffer";
864116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return false;
865116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
866116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    FileDescriptor memory_fd;
867116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    memory_fd.fd = dup(memory_buffer.handle().fd);
868116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (memory_fd.fd < 0) {
869116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      DLOG(ERROR) << "Failed to dup() a file descriptor";
870116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return false;
871116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
872116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    memory_fd.auto_close = true;
873116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    params.handles.push_back(memory_fd);
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
877116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (params.enable_debug_stub) {
878116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      net::SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
879116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (server_bound_socket != net::kInvalidSocket) {
880116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params.debug_stub_server_bound_socket =
881116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            FileDescriptor(server_bound_socket, true);
882116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
885116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  params.nexe_file = IPC::TakeFileHandleForProcess(nexe_file_.Pass(),
8885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                   process_->GetData().handle);
88903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (!crash_info_shmem_.ShareToProcess(process_->GetData().handle,
89003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                        &params.crash_info_shmem_handle)) {
89103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    DLOG(ERROR) << "Failed to ShareToProcess() a shared memory buffer";
89203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return false;
89303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
8945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
895116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  process_->Send(new NaClProcessMsg_Start(params));
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
9005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// received.
9015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void NaClProcessHost::OnPpapiChannelsCreated(
9025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const IPC::ChannelHandle& browser_channel_handle,
903a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const IPC::ChannelHandle& ppapi_renderer_channel_handle,
9040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const IPC::ChannelHandle& trusted_renderer_channel_handle,
9050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const IPC::ChannelHandle& manifest_service_channel_handle) {
906a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!enable_ppapi_proxy()) {
9070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ReplyToRenderer(IPC::ChannelHandle(),
9080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    trusted_renderer_channel_handle,
9090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    manifest_service_channel_handle);
910a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
911a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
912a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ipc_proxy_channel_.get()) {
9142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type);
9152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
91646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ipc_proxy_channel_ =
91746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        IPC::ChannelProxy::Create(browser_channel_handle,
91846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  IPC::Channel::MODE_CLIENT,
91946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  NULL,
92046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  base::MessageLoopProxy::current().get());
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Create the browser ppapi host and enable PPAPI message dispatching to the
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // browser process.
9232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
9242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ipc_proxy_channel_.get(),  // sender
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        permissions_,
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        process_->GetData().handle,
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ipc_proxy_channel_.get(),
9287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        nacl_host_message_filter_->render_process_id(),
929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        render_view_id_,
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile_directory_));
9315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ppapi_host_->SetOnKeepaliveCallback(
9325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        NaClBrowser::GetDelegate()->GetOnKeepaliveCallback());
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ppapi::PpapiNaClPluginArgs args;
9357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    args.off_the_record = nacl_host_message_filter_->off_the_record();
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    args.permissions = permissions_;
937effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    args.keepalive_throttle_interval_milliseconds =
938effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        keepalive_throttle_interval_milliseconds_;
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CommandLine* cmdline = CommandLine::ForCurrentProcess();
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(cmdline);
9415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::string flag_whitelist[] = {
9425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      switches::kV,
9435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      switches::kVModule,
9445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    };
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < arraysize(flag_whitelist); ++i) {
946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]);
947c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (!value.empty()) {
948c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        args.switch_names.push_back(flag_whitelist[i]);
949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        args.switch_values.push_back(value);
950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ppapi_host_->GetPpapiHost()->AddHostFactoryFilter(
954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        scoped_ptr<ppapi::host::HostFactory>(
955f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            NaClBrowser::GetDelegate()->CreatePpapiHostFactory(
9567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                ppapi_host_.get())));
9572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Send a message to initialize the IPC dispatchers in the NaCl plugin.
9595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ipc_proxy_channel_->Send(new PpapiMsg_InitializeNaClDispatcher(args));
9605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Let the renderer know that the IPC channels are established.
962a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ReplyToRenderer(ppapi_renderer_channel_handle,
9630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    trusted_renderer_channel_handle,
9640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    manifest_service_channel_handle);
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
9665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Attempt to open more than 1 browser channel is not supported.
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Shut down the NaCl process.
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    process_->GetHost()->ForceShutdown();
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::StartWithLaunchedProcess() {
973f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (nacl_browser->IsReady()) {
976a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return StartNaClExecution();
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (nacl_browser->IsOk()) {
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    nacl_browser->WaitForResources(
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(&NaClProcessHost::OnResourcesReady,
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   weak_factory_.GetWeakPtr()));
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
983ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SendErrorToRenderer("previously failed to acquire shared resources");
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
9895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             bool* result) {
990010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CHECK(!uses_nonsfi_mode_);
991f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
9925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
9935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
996010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CHECK(!uses_nonsfi_mode_);
997f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NaClBrowser::GetInstance()->SetKnownToValidate(
9980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      signature, off_the_record_);
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
100190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void NaClProcessHost::OnResolveFileToken(uint64 file_token_lo,
100290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                         uint64 file_token_hi,
100390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                         IPC::Message* reply_msg) {
100490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Was the file registered?
100590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
100690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Note that the file path cache is of bounded size, and old entries can get
100790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // evicted.  If a large number of NaCl modules are being launched at once,
100890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // resolving the file_token may fail because the path cache was thrashed
100990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // while the file_token was in flight.  In this case the query fails, and we
101090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // need to fall back to the slower path.
101190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
101290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // However: each NaCl process will consume 2-3 entries as it starts up, this
101390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // means that eviction will not happen unless you start up 33+ NaCl processes
101490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // at the same time, and this still requires worst-case timing.  As a
101590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // practical matter, no entries should be evicted prematurely.
101690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The cache itself should take ~ (150 characters * 2 bytes/char + ~60 bytes
101790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // data structure overhead) * 100 = 35k when full, so making it bigger should
101890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // not be a problem, if needed.
101990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
102090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Each NaCl process will consume 2-3 entries because the manifest and main
102190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // nexe are currently not resolved.  Shared libraries will be resolved.  They
102290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // will be loaded sequentially, so they will only consume a single entry
102390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // while the load is in flight.
102490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
102590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(ncbray): track behavior with UMA. If entries are getting evicted or
102690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // bogus keys are getting queried, this would be good to know.
1027010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CHECK(!uses_nonsfi_mode_);
102890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::FilePath file_path;
1029f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!NaClBrowser::GetInstance()->GetFilePath(
10300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        file_token_lo, file_token_hi, &file_path)) {
103190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    NaClProcessMsg_ResolveFileToken::WriteReplyParams(
103290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        reply_msg,
103390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        IPC::InvalidPlatformFileForTransit(),
1034f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        base::FilePath());
103590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Send(reply_msg);
103690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
103790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
103890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
103990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Open the file.
1040f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!base::PostTaskAndReplyWithResult(
1041f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          content::BrowserThread::GetBlockingPool(),
104290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FROM_HERE,
1043116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          base::Bind(OpenNaClReadExecImpl, file_path, true /* is_executable */),
104490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          base::Bind(&NaClProcessHost::FileResolved,
104590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                     weak_factory_.GetWeakPtr(),
104690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                     file_path,
104790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                     reply_msg))) {
104890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)     NaClProcessMsg_ResolveFileToken::WriteReplyParams(
104990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)         reply_msg,
105090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)         IPC::InvalidPlatformFileForTransit(),
1051f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         base::FilePath());
105290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)     Send(reply_msg);
105390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
105490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
105590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid NaClProcessHost::OnResolveFileTokenAsync(uint64 file_token_lo,
10571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                              uint64 file_token_hi) {
10581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // See the comment at OnResolveFileToken() for details of the file path cache
10591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // behavior.
10601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(!uses_nonsfi_mode_);
10611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::FilePath file_path;
10621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!NaClBrowser::GetInstance()->GetFilePath(
10631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        file_token_lo, file_token_hi, &file_path)) {
10641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Send(new NaClProcessMsg_ResolveFileTokenAsyncReply(
10651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci             file_token_lo,
10661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci             file_token_hi,
10671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci             IPC::PlatformFileForTransit(),
10681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci             base::FilePath()));
10691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
10701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
10711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
10721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Open the file.
10731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!base::PostTaskAndReplyWithResult(
10741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::GetBlockingPool(),
10751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          FROM_HERE,
10761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Bind(OpenNaClReadExecImpl, file_path, true /* is_executable */),
10771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Bind(&NaClProcessHost::FileResolvedAsync,
10781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     weak_factory_.GetWeakPtr(),
10791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     file_token_lo,
10801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     file_token_hi,
10811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     file_path))) {
10821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Send(new NaClProcessMsg_ResolveFileTokenAsyncReply(
10831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            file_token_lo,
10841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            file_token_hi,
10851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            IPC::PlatformFileForTransit(),
10861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            base::FilePath()));
10871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
10881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
10891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
10901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid NaClProcessHost::FileResolved(
10911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const base::FilePath& file_path,
10921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    IPC::Message* reply_msg,
10931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::File file) {
10941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (file.IsValid()) {
10951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    IPC::PlatformFileForTransit handle = IPC::TakeFileHandleForProcess(
10961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        file.Pass(),
10971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        process_->GetData().handle);
10981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    NaClProcessMsg_ResolveFileToken::WriteReplyParams(
10991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        reply_msg,
11001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        handle,
11011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        file_path);
11021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else {
11031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    NaClProcessMsg_ResolveFileToken::WriteReplyParams(
11041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        reply_msg,
11051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        IPC::InvalidPlatformFileForTransit(),
11061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::FilePath());
11071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
11081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Send(reply_msg);
11091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
11101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid NaClProcessHost::FileResolvedAsync(
11121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    uint64_t file_token_lo,
11131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    uint64_t file_token_hi,
11141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const base::FilePath& file_path,
11151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::File file) {
11161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::FilePath out_file_path;
11171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  IPC::PlatformFileForTransit out_handle;
11181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (file.IsValid()) {
11191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    out_file_path = file_path;
11201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    out_handle = IPC::TakeFileHandleForProcess(
11211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        file.Pass(),
11221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        process_->GetData().handle);
11231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else {
11241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    out_handle = IPC::InvalidPlatformFileForTransit();
11251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
11261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Send(new NaClProcessMsg_ResolveFileTokenAsyncReply(
11271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci           file_token_lo,
11281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci           file_token_hi,
11291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci           out_handle,
11301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci           out_file_path));
11311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
11321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
11345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
11355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                    IPC::Message* reply_msg) {
1136010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CHECK(!uses_nonsfi_mode_);
11375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!AttachDebugExceptionHandler(info, reply_msg)) {
11385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Send failure message.
11395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
11405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                                 false);
11415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Send(reply_msg);
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
11465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                  IPC::Message* reply_msg) {
11475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!enable_exception_handling_ && !enable_debug_stub_) {
11485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DLOG(ERROR) <<
11495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "Debug exception handler requested by NaCl process when not enabled";
11505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
11515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (debug_exception_handler_requested_) {
11535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The NaCl process should not request this multiple times.
11545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
11555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
11565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  debug_exception_handler_requested_ = true;
11585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
1160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::ProcessHandle temp_handle;
11615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We cannot use process_->GetData().handle because it does not have
11625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the necessary access rights.  We open the new handle here rather
11635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // than in the NaCl broker process in case the NaCl loader process
11645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dies before the NaCl broker process receives the message we send.
11655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The debug exception handler uses DebugActiveProcess() to attach,
11665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // but this takes a PID.  We need to prevent the NaCl loader's PID
11675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from being reused before DebugActiveProcess() is called, and
11685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // holding a process handle open achieves this.
11695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!base::OpenProcessHandleWithAccess(
11705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           nacl_pid,
11715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessQueryInformation |
11725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessSuspendResume |
11735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessTerminate |
11745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessVMOperation |
11755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessVMRead |
11765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessVMWrite |
11772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           base::kProcessAccessDuplicateHandle |
11785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           base::kProcessAccessWaitForTermination,
1179f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)           &temp_handle)) {
11805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to get process handle";
11815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
11825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::win::ScopedHandle process_handle(temp_handle);
11845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  attach_debug_exception_handler_reply_msg_.reset(reply_msg);
11865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the NaCl loader is 64-bit, the process running its debug
11875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exception handler must be 64-bit too, so we use the 64-bit NaCl
11885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // broker process for this.  Otherwise, on a 32-bit system, we use
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the 32-bit browser process to run the debug exception handler.
11905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (RunningOnWOW64()) {
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
11925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
11935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
11945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NaClStartDebugExceptionHandlerThread(
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        process_handle.Take(), info,
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::MessageLoopProxy::current(),
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
11985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   weak_factory_.GetWeakPtr()));
11995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
12005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
12015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace nacl
1205