[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e15a4fa | 2010-02-11 23:09:29 | [diff] [blame] | 5 | #include "chrome/browser/nacl_host/nacl_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 6 | |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 10 | |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 11 | #include "base/base_switches.h" |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 6d7f55f | 2013-05-14 10:12:56 | [diff] [blame] | 14 | #include "base/file_util.h" |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 15 | #include "base/message_loop.h" |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 18 | #include "base/process_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 20 | #include "base/strings/string_split.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 24 | #include "base/win/windows_version.h" |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_info_map.h" |
[email protected] | 2e5f2ea4 | 2012-05-09 21:39:21 | [diff] [blame] | 27 | #include "chrome/browser/nacl_host/nacl_browser.h" |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 28 | #include "chrome/browser/nacl_host/nacl_host_message_filter.h" |
[email protected] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 29 | #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h" |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 30 | #include "chrome/common/chrome_constants.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 31 | #include "chrome/common/chrome_paths.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 32 | #include "chrome/common/chrome_process_type.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 33 | #include "chrome/common/chrome_switches.h" |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame] | 34 | #include "chrome/common/chrome_version_info.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 35 | #include "chrome/common/logging_chrome.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 36 | #include "chrome/common/nacl_cmd_line.h" |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 37 | #include "chrome/common/nacl_host_messages.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 38 | #include "chrome/common/nacl_messages.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 39 | #include "chrome/common/render_messages.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 40 | #include "content/public/browser/browser_child_process_host.h" |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 41 | #include "content/public/browser/browser_ppapi_host.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 42 | #include "content/public/browser/child_process_data.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 43 | #include "content/public/common/child_process_host.h" |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 44 | #include "content/public/common/process_type.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 45 | #include "extensions/common/constants.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 46 | #include "ipc/ipc_channel.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 47 | #include "ipc/ipc_switches.h" |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 48 | #include "native_client/src/shared/imc/nacl_imc_c.h" |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 49 | #include "net/base/net_util.h" |
[email protected] | 1946d9e | 2013-04-09 01:43:37 | [diff] [blame] | 50 | #include "net/socket/tcp_listen_socket.h" |
[email protected] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 51 | #include "ppapi/host/ppapi_host.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 52 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 53 | #include "ppapi/shared_impl/ppapi_nacl_channel_args.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 54 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 55 | #if defined(OS_POSIX) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 56 | #include <fcntl.h> |
| 57 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 58 | #include "ipc/ipc_channel_posix.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 59 | #elif defined(OS_WIN) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 60 | #include <windows.h> |
| 61 | |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 62 | #include "base/threading/thread.h" |
[email protected] | 4c65fb63 | 2012-04-27 00:42:25 | [diff] [blame] | 63 | #include "base/win/scoped_handle.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 64 | #include "chrome/browser/nacl_host/nacl_broker_service_win.h" |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 65 | #include "chrome/common/nacl_debug_exception_handler_win.h" |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 66 | #include "content/public/common/sandbox_init.h" |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 67 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 68 | #endif |
| 69 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 70 | using content::BrowserThread; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 71 | using content::ChildProcessData; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 72 | using content::ChildProcessHost; |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 73 | using ppapi::proxy::SerializedHandle; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 74 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 75 | namespace { |
| 76 | |
| 77 | #if defined(OS_WIN) |
| 78 | bool RunningOnWOW64() { |
| 79 | return (base::win::OSInfo::GetInstance()->wow64_status() == |
| 80 | base::win::OSInfo::WOW64_ENABLED); |
| 81 | } |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 82 | |
| 83 | // NOTE: changes to this class need to be reviewed by the security team. |
| 84 | class NaClSandboxedProcessLauncherDelegate |
| 85 | : public content::SandboxedProcessLauncherDelegate { |
| 86 | public: |
| 87 | NaClSandboxedProcessLauncherDelegate() {} |
| 88 | virtual ~NaClSandboxedProcessLauncherDelegate() {} |
| 89 | |
| 90 | virtual void PostSpawnTarget(base::ProcessHandle process) { |
| 91 | #if !defined(NACL_WIN64) |
| 92 | // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of |
| 93 | // address space to prevent later failure due to address space fragmentation |
| 94 | // from .dll loading. The NaCl process will attempt to locate this space by |
| 95 | // scanning the address space using VirtualQuery. |
| 96 | // TODO(bbudge) Handle the --no-sandbox case. |
| 97 | // http://code.google.com/p/nativeclient/issues/detail?id=2131 |
| 98 | const SIZE_T kOneGigabyte = 1 << 30; |
| 99 | void* nacl_mem = VirtualAllocEx(process, |
| 100 | NULL, |
| 101 | kOneGigabyte, |
| 102 | MEM_RESERVE, |
| 103 | PAGE_NOACCESS); |
| 104 | if (!nacl_mem) { |
| 105 | DLOG(WARNING) << "Failed to reserve address space for Native Client"; |
| 106 | } |
| 107 | #endif // !defined(NACL_WIN64) |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | #endif // OS_WIN |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 112 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 113 | void SetCloseOnExec(NaClHandle fd) { |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 114 | #if defined(OS_POSIX) |
| 115 | int flags = fcntl(fd, F_GETFD); |
| 116 | CHECK_NE(flags, -1); |
| 117 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 118 | CHECK_EQ(rc, 0); |
| 119 | #endif |
| 120 | } |
| 121 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 122 | bool ShareHandleToSelLdr( |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 123 | base::ProcessHandle processh, |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 124 | NaClHandle sourceh, |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 125 | bool close_source, |
| 126 | std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) { |
| 127 | #if defined(OS_WIN) |
| 128 | HANDLE channel; |
| 129 | int flags = DUPLICATE_SAME_ACCESS; |
| 130 | if (close_source) |
| 131 | flags |= DUPLICATE_CLOSE_SOURCE; |
| 132 | if (!DuplicateHandle(GetCurrentProcess(), |
| 133 | reinterpret_cast<HANDLE>(sourceh), |
| 134 | processh, |
| 135 | &channel, |
| 136 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 137 | FALSE, |
| 138 | flags)) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 139 | LOG(ERROR) << "DuplicateHandle() failed"; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | handles_for_sel_ldr->push_back( |
| 143 | reinterpret_cast<nacl::FileDescriptor>(channel)); |
| 144 | #else |
| 145 | nacl::FileDescriptor channel; |
| 146 | channel.fd = sourceh; |
| 147 | channel.auto_close = close_source; |
| 148 | handles_for_sel_ldr->push_back(channel); |
| 149 | #endif |
| 150 | return true; |
| 151 | } |
| 152 | |
[email protected] | 27b04df | 2012-12-10 23:43:10 | [diff] [blame] | 153 | ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) { |
| 154 | // Only allow NaCl plugins to request certain permissions. We don't want |
| 155 | // a compromised renderer to be able to start a nacl plugin with e.g. Flash |
| 156 | // permissions which may expand the surface area of the sandbox. |
| 157 | uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV; |
| 158 | return ppapi::PpapiPermissions::GetForCommandLine(masked_bits); |
| 159 | } |
| 160 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 161 | } // namespace |
| 162 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 163 | struct NaClProcessHost::NaClInternal { |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 164 | NaClHandle socket_for_renderer; |
| 165 | NaClHandle socket_for_sel_ldr; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 166 | |
| 167 | NaClInternal() |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 168 | : socket_for_renderer(NACL_INVALID_HANDLE), |
| 169 | socket_for_sel_ldr(NACL_INVALID_HANDLE) { } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 170 | }; |
| 171 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 172 | // ----------------------------------------------------------------------------- |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 173 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 174 | NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host) |
| 175 | : host_(host) { |
| 176 | } |
| 177 | |
| 178 | bool NaClProcessHost::PluginListener::OnMessageReceived( |
| 179 | const IPC::Message& msg) { |
| 180 | return host_->OnUntrustedMessageForwarded(msg); |
| 181 | } |
| 182 | |
[email protected] | 9da4fed | 2012-11-01 17:07:55 | [diff] [blame] | 183 | NaClProcessHost::NaClProcessHost(const GURL& manifest_url, |
[email protected] | e02ff72 | 2012-11-06 03:53:06 | [diff] [blame] | 184 | int render_view_id, |
[email protected] | 9da4fed | 2012-11-01 17:07:55 | [diff] [blame] | 185 | uint32 permission_bits, |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 186 | bool uses_irt, |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 187 | bool enable_dyncode_syscalls, |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 188 | bool enable_exception_handling, |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 189 | bool off_the_record, |
| 190 | const base::FilePath& profile_directory) |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 191 | : manifest_url_(manifest_url), |
[email protected] | 27b04df | 2012-12-10 23:43:10 | [diff] [blame] | 192 | permissions_(GetNaClPermissions(permission_bits)), |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 193 | #if defined(OS_WIN) |
| 194 | process_launched_by_broker_(false), |
| 195 | #endif |
| 196 | reply_msg_(NULL), |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 197 | #if defined(OS_WIN) |
| 198 | debug_exception_handler_requested_(false), |
| 199 | #endif |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 200 | internal_(new NaClInternal()), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 201 | weak_factory_(this), |
[email protected] | 84b8e896 | 2013-06-12 22:24:06 | [diff] [blame] | 202 | uses_irt_(uses_irt), |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 203 | enable_debug_stub_(false), |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 204 | enable_dyncode_syscalls_(enable_dyncode_syscalls), |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 205 | enable_exception_handling_(enable_exception_handling), |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 206 | off_the_record_(off_the_record), |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 207 | profile_directory_(profile_directory), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 208 | ipc_plugin_listener_(this), |
[email protected] | e02ff72 | 2012-11-06 03:53:06 | [diff] [blame] | 209 | render_view_id_(render_view_id) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 210 | process_.reset(content::BrowserChildProcessHost::Create( |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 211 | PROCESS_TYPE_NACL_LOADER, this)); |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 212 | |
| 213 | // Set the display name so the user knows what plugin the process is running. |
| 214 | // We aren't on the UI thread so getting the pref locale for language |
| 215 | // formatting isn't possible, so IDN will be lost, but this is probably OK |
| 216 | // for this use case. |
| 217 | process_->SetName(net::FormatUrl(manifest_url_, std::string())); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 218 | |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 219 | enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 220 | switches::kEnableNaClDebug); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 223 | NaClProcessHost::~NaClProcessHost() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 224 | int exit_code; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 225 | process_->GetTerminationStatus(&exit_code); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 226 | std::string message = |
| 227 | base::StringPrintf("NaCl process exited with status %i (0x%x)", |
| 228 | exit_code, exit_code); |
| 229 | if (exit_code == 0) { |
| 230 | LOG(INFO) << message; |
| 231 | } else { |
| 232 | LOG(ERROR) << message; |
| 233 | } |
| 234 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 235 | if (internal_->socket_for_renderer != NACL_INVALID_HANDLE) { |
| 236 | if (NaClClose(internal_->socket_for_renderer) != 0) { |
| 237 | NOTREACHED() << "NaClClose() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 238 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 239 | } |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 240 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 241 | if (internal_->socket_for_sel_ldr != NACL_INVALID_HANDLE) { |
| 242 | if (NaClClose(internal_->socket_for_sel_ldr) != 0) { |
| 243 | NOTREACHED() << "NaClClose() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 244 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 247 | if (reply_msg_) { |
| 248 | // The process failed to launch for some reason. |
| 249 | // Don't keep the renderer hanging. |
| 250 | reply_msg_->set_reply_error(); |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 251 | nacl_host_message_filter_->Send(reply_msg_); |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 252 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 253 | #if defined(OS_WIN) |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 254 | if (process_launched_by_broker_) { |
| 255 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 256 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 257 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 260 | // This is called at browser startup. |
| 261 | // static |
| 262 | void NaClProcessHost::EarlyStartup() { |
| 263 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 264 | // Open the IRT file early to make sure that it isn't replaced out from |
| 265 | // under us by autoupdate. |
| 266 | NaClBrowser::GetInstance()->EnsureIrtAvailable(); |
| 267 | #endif |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 268 | CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 269 | UMA_HISTOGRAM_BOOLEAN( |
| 270 | "NaCl.nacl-gdb", |
| 271 | !cmd->GetSwitchValuePath(switches::kNaClGdb).empty()); |
| 272 | UMA_HISTOGRAM_BOOLEAN( |
| 273 | "NaCl.nacl-gdb-script", |
| 274 | !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty()); |
| 275 | UMA_HISTOGRAM_BOOLEAN( |
| 276 | "NaCl.enable-nacl-debug", |
| 277 | cmd->HasSwitch(switches::kEnableNaClDebug)); |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame] | 278 | NaClBrowser::GetInstance()->SetDebugPatterns( |
| 279 | cmd->GetSwitchValueASCII(switches::kNaClDebugMask)); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 280 | } |
| 281 | |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 282 | void NaClProcessHost::Launch( |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 283 | NaClHostMessageFilter* nacl_host_message_filter, |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 284 | IPC::Message* reply_msg, |
| 285 | scoped_refptr<ExtensionInfoMap> extension_info_map) { |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 286 | nacl_host_message_filter_ = nacl_host_message_filter; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 287 | reply_msg_ = reply_msg; |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 288 | extension_info_map_ = extension_info_map; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 289 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 290 | // Start getting the IRT open asynchronously while we launch the NaCl process. |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 291 | // We'll make sure this actually finished in StartWithLaunchedProcess, below. |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 292 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 293 | nacl_browser->EnsureAllResourcesAvailable(); |
| 294 | if (!nacl_browser->IsOk()) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 295 | LOG(ERROR) << "NaCl process launch failed: could not find all the " |
| 296 | "resources needed to launch the process"; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 297 | delete this; |
| 298 | return; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 301 | // Rather than creating a socket pair in the renderer, and passing |
| 302 | // one side through the browser to sel_ldr, socket pairs are created |
| 303 | // in the browser and then passed to the renderer and sel_ldr. |
| 304 | // |
| 305 | // This is mainly for the benefit of Windows, where sockets cannot |
| 306 | // be passed in messages, but are copied via DuplicateHandle(). |
| 307 | // This means the sandboxed renderer cannot send handles to the |
| 308 | // browser process. |
| 309 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 310 | NaClHandle pair[2]; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 311 | // Create a connected socket |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 312 | if (NaClSocketPair(pair) == -1) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 313 | LOG(ERROR) << "NaCl process launch failed: could not create a socket pair"; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 314 | delete this; |
| 315 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 316 | } |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 317 | internal_->socket_for_renderer = pair[0]; |
| 318 | internal_->socket_for_sel_ldr = pair[1]; |
| 319 | SetCloseOnExec(pair[0]); |
| 320 | SetCloseOnExec(pair[1]); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 321 | |
| 322 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 323 | if (!LaunchSelLdr()) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 324 | delete this; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 325 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 326 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 327 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 328 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
[email protected] | 82d1750 | 2012-05-23 19:12:40 | [diff] [blame] | 329 | if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 330 | switches::kNaClGdb).empty()) { |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 331 | LaunchNaClGdb(); |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 332 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 333 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 334 | |
| 335 | #if defined(OS_WIN) |
| 336 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
| 337 | process_launched_by_broker_ = true; |
| 338 | process_->SetHandle(handle); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 339 | if (!StartWithLaunchedProcess()) |
| 340 | delete this; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 343 | void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) { |
| 344 | IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release(); |
| 345 | NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success); |
| 346 | Send(reply); |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 347 | } |
| 348 | #endif |
| 349 | |
| 350 | // Needed to handle sync messages in OnMessageRecieved. |
| 351 | bool NaClProcessHost::Send(IPC::Message* msg) { |
| 352 | return process_->Send(msg); |
| 353 | } |
| 354 | |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 355 | bool NaClProcessHost::LaunchNaClGdb() { |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 356 | #if defined(OS_WIN) |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 357 | base::FilePath nacl_gdb = |
| 358 | CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb); |
| 359 | CommandLine cmd_line(nacl_gdb); |
| 360 | #else |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 361 | CommandLine::StringType nacl_gdb = |
| 362 | CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 363 | switches::kNaClGdb); |
| 364 | CommandLine::StringVector argv; |
| 365 | // We don't support spaces inside arguments in --nacl-gdb switch. |
| 366 | base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv); |
| 367 | CommandLine cmd_line(argv); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 368 | #endif |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 369 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 370 | base::FilePath::StringType irt_path( |
| 371 | NaClBrowser::GetInstance()->GetIrtFilePath().value()); |
| 372 | // Avoid back slashes because nacl-gdb uses posix escaping rules on Windows. |
| 373 | // See issue https://code.google.com/p/nativeclient/issues/detail?id=3482. |
| 374 | std::replace(irt_path.begin(), irt_path.end(), '\\', '/'); |
| 375 | cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt \"") + irt_path + |
| 376 | FILE_PATH_LITERAL("\"")); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 377 | base::FilePath manifest_path = GetManifestPath(); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 378 | if (!manifest_path.empty()) { |
| 379 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 380 | base::FilePath::StringType manifest_path_value(manifest_path.value()); |
| 381 | std::replace(manifest_path_value.begin(), manifest_path_value.end(), |
| 382 | '\\', '/'); |
| 383 | cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest \"") + |
| 384 | manifest_path_value + FILE_PATH_LITERAL("\"")); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 385 | } |
| 386 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 387 | cmd_line.AppendArg("target remote :4014"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 388 | base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
[email protected] | 401b9079 | 2012-05-30 11:41:13 | [diff] [blame] | 389 | switches::kNaClGdbScript); |
| 390 | if (!script.empty()) { |
| 391 | cmd_line.AppendArg("--command"); |
| 392 | cmd_line.AppendArgNative(script.value()); |
| 393 | } |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 394 | return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL); |
| 395 | } |
| 396 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 397 | base::FilePath NaClProcessHost::GetManifestPath() { |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 398 | const extensions::Extension* extension = extension_info_map_->extensions() |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 399 | .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_)); |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 400 | if (extension != NULL && |
| 401 | manifest_url_.SchemeIs(extensions::kExtensionScheme)) { |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 402 | std::string path = manifest_url_.path(); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 403 | TrimString(path, "/", &path); // Remove first slash |
| 404 | return extension->path().AppendASCII(path); |
| 405 | } |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 406 | return base::FilePath(); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 407 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 408 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 409 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 410 | std::string channel_id = process_->GetHost()->CreateChannel(); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 411 | if (channel_id.empty()) { |
| 412 | LOG(ERROR) << "NaCl process launch failed: could not create channel"; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 413 | return false; |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 414 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 415 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 416 | CommandLine::StringType nacl_loader_prefix; |
| 417 | #if defined(OS_POSIX) |
| 418 | nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 419 | switches::kNaClLoaderCmdPrefix); |
| 420 | #endif // defined(OS_POSIX) |
| 421 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 422 | // Build command line for nacl. |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 423 | |
| 424 | #if defined(OS_MACOSX) |
| 425 | // The Native Client process needs to be able to allocate a 1GB contiguous |
| 426 | // region to use as the client environment's virtual address space. ASLR |
| 427 | // (PIE) interferes with this by making it possible that no gap large enough |
| 428 | // to accomodate this request will exist in the child process' address |
| 429 | // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and |
| 430 | // http://code.google.com/p/nativeclient/issues/detail?id=2043. |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 431 | int flags = ChildProcessHost::CHILD_NO_PIE; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 432 | #elif defined(OS_LINUX) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 433 | int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF : |
| 434 | ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 435 | #else |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 436 | int flags = ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 437 | #endif |
| 438 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 439 | base::FilePath exe_path = ChildProcessHost::GetChildPath(flags); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 440 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 441 | return false; |
| 442 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 443 | #if defined(OS_WIN) |
| 444 | // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe |
| 445 | if (RunningOnWOW64()) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 446 | base::FilePath module_path; |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 447 | if (!PathService::Get(base::FILE_MODULE, &module_path)) { |
| 448 | LOG(ERROR) << "NaCl process launch failed: could not resolve module"; |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 449 | return false; |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 450 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 451 | exe_path = module_path.DirName().Append(chrome::kNaClAppName); |
| 452 | } |
| 453 | #endif |
| 454 | |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 455 | scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path)); |
| 456 | nacl::CopyNaClCommandLineArguments(cmd_line.get()); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 457 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 458 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 459 | switches::kNaClLoaderProcess); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 460 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
[email protected] | 93156cec | 2011-09-12 21:14:44 | [diff] [blame] | 461 | if (logging::DialogsAreSuppressed()) |
| 462 | cmd_line->AppendSwitch(switches::kNoErrorDialogs); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 463 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 464 | if (!nacl_loader_prefix.empty()) |
| 465 | cmd_line->PrependWrapper(nacl_loader_prefix); |
| 466 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 467 | // On Windows we might need to start the broker process to launch a new loader |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 468 | #if defined(OS_WIN) |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 469 | if (RunningOnWOW64()) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 470 | if (!NaClBrokerService::GetInstance()->LaunchLoader( |
| 471 | weak_factory_.GetWeakPtr(), channel_id)) { |
| 472 | LOG(ERROR) << "NaCl process launch failed: broker service did not launch " |
| 473 | "process"; |
| 474 | return false; |
| 475 | } |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 476 | } else { |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 477 | process_->Launch(new NaClSandboxedProcessLauncherDelegate, |
| 478 | cmd_line.release()); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 479 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 480 | #elif defined(OS_POSIX) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 481 | process_->Launch(nacl_loader_prefix.empty(), // use_zygote |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 482 | base::EnvironmentVector(), |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 483 | cmd_line.release()); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 484 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 485 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 486 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 489 | bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 490 | bool handled = true; |
| 491 | IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg) |
| 492 | IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate, |
| 493 | OnQueryKnownToValidate) |
| 494 | IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate, |
| 495 | OnSetKnownToValidate) |
[email protected] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 496 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken, |
| 497 | OnResolveFileToken) |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 498 | #if defined(OS_WIN) |
| 499 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler, |
| 500 | OnAttachDebugExceptionHandler) |
| 501 | #endif |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 502 | IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated, |
| 503 | OnPpapiChannelCreated) |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 504 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 505 | IPC_END_MESSAGE_MAP() |
| 506 | return handled; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 507 | } |
| 508 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 509 | void NaClProcessHost::OnProcessLaunched() { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 510 | if (!StartWithLaunchedProcess()) |
| 511 | delete this; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 512 | } |
| 513 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 514 | // Called when the NaClBrowser singleton has been fully initialized. |
| 515 | void NaClProcessHost::OnResourcesReady() { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 516 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 517 | if (!nacl_browser->IsReady()) { |
| 518 | LOG(ERROR) << "NaCl process launch failed: could not acquire shared " |
| 519 | "resources needed by NaCl"; |
| 520 | delete this; |
| 521 | } else if (!SendStart()) { |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 522 | delete this; |
| 523 | } |
| 524 | } |
| 525 | |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 526 | bool NaClProcessHost::ReplyToRenderer( |
| 527 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 528 | nacl::FileDescriptor handle_for_renderer; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 529 | #if defined(OS_WIN) |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 530 | // Copy the handle into the renderer process. |
| 531 | HANDLE handle_in_renderer; |
| 532 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
| 533 | reinterpret_cast<HANDLE>( |
| 534 | internal_->socket_for_renderer), |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 535 | nacl_host_message_filter_->peer_handle(), |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 536 | &handle_in_renderer, |
| 537 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 538 | FALSE, |
| 539 | DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 540 | LOG(ERROR) << "DuplicateHandle() failed"; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 541 | return false; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 542 | } |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 543 | handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>( |
| 544 | handle_in_renderer); |
| 545 | #else |
| 546 | // No need to dup the imc_handle - we don't pass it anywhere else so |
| 547 | // it cannot be closed. |
| 548 | nacl::FileDescriptor imc_handle; |
| 549 | imc_handle.fd = internal_->socket_for_renderer; |
| 550 | imc_handle.auto_close = true; |
| 551 | handle_for_renderer = imc_handle; |
| 552 | #endif |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 553 | |
| 554 | #if defined(OS_WIN) |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 555 | // If we are on 64-bit Windows, the NaCl process's sandbox is |
| 556 | // managed by a different process from the renderer's sandbox. We |
| 557 | // need to inform the renderer's sandbox about the NaCl process so |
| 558 | // that the renderer can send handles to the NaCl process using |
| 559 | // BrokerDuplicateHandle(). |
| 560 | if (RunningOnWOW64()) { |
[email protected] | 171fa98d | 2012-04-23 21:34:01 | [diff] [blame] | 561 | if (!content::BrokerAddTargetPeer(process_->GetData().handle)) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 562 | LOG(ERROR) << "Failed to add NaCl process PID"; |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 566 | #endif |
| 567 | |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 568 | const ChildProcessData& data = process_->GetData(); |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 569 | NaClHostMsg_LaunchNaCl::WriteReplyParams( |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 570 | reply_msg_, handle_for_renderer, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 571 | channel_handle, base::GetProcId(data.handle), data.id); |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 572 | nacl_host_message_filter_->Send(reply_msg_); |
| 573 | nacl_host_message_filter_ = NULL; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 574 | reply_msg_ = NULL; |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 575 | internal_->socket_for_renderer = NACL_INVALID_HANDLE; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 576 | return true; |
| 577 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 578 | |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 579 | // TCP port we chose for NaCl debug stub. It can be any other number. |
| 580 | static const int kDebugStubPort = 4014; |
| 581 | |
| 582 | #if defined(OS_POSIX) |
| 583 | SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() { |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 584 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 585 | SocketDescriptor s; |
| 586 | // We allocate currently unused TCP port for debug stub tests. The port |
| 587 | // number is passed to the test via debug stub port listener. |
| 588 | if (nacl_browser->HasGdbDebugStubPortListener()) { |
| 589 | int port; |
| 590 | s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port); |
| 591 | if (s != net::TCPListenSocket::kInvalidSocket) { |
| 592 | nacl_browser->FireGdbDebugStubPortOpened(port); |
| 593 | } |
| 594 | } else { |
| 595 | s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort); |
| 596 | } |
| 597 | if (s == net::TCPListenSocket::kInvalidSocket) { |
| 598 | LOG(ERROR) << "failed to open socket for debug stub"; |
| 599 | return net::TCPListenSocket::kInvalidSocket; |
| 600 | } |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 601 | if (listen(s, 1)) { |
| 602 | LOG(ERROR) << "listen() failed on debug stub socket"; |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 603 | if (HANDLE_EINTR(close(s)) < 0) |
| 604 | PLOG(ERROR) << "failed to close debug stub socket"; |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 605 | return net::TCPListenSocket::kInvalidSocket; |
| 606 | } |
| 607 | return s; |
| 608 | } |
| 609 | #endif |
| 610 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 611 | bool NaClProcessHost::StartNaClExecution() { |
| 612 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 613 | |
| 614 | nacl::NaClStartParams params; |
[email protected] | 8c67083 | 2012-05-26 04:30:12 | [diff] [blame] | 615 | params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled(); |
| 616 | params.validation_cache_key = nacl_browser->GetValidationCacheKey(); |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 617 | params.version = chrome::VersionInfo().CreateVersionString(); |
| 618 | params.enable_exception_handling = enable_exception_handling_; |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame] | 619 | params.enable_debug_stub = enable_debug_stub_ && |
| 620 | NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_); |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 621 | // Enable PPAPI proxy channel creation only for renderer processes. |
| 622 | params.enable_ipc_proxy = enable_ppapi_proxy(); |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 623 | params.uses_irt = uses_irt_; |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 624 | params.enable_dyncode_syscalls = enable_dyncode_syscalls_; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 625 | |
| 626 | const ChildProcessData& data = process_->GetData(); |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 627 | if (!ShareHandleToSelLdr(data.handle, |
| 628 | internal_->socket_for_sel_ldr, true, |
| 629 | ¶ms.handles)) { |
| 630 | return false; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 633 | if (params.uses_irt) { |
| 634 | base::PlatformFile irt_file = nacl_browser->IrtFile(); |
| 635 | CHECK_NE(irt_file, base::kInvalidPlatformFileValue); |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 636 | // Send over the IRT file handle. We don't close our own copy! |
| 637 | if (!ShareHandleToSelLdr(data.handle, irt_file, false, ¶ms.handles)) |
| 638 | return false; |
| 639 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 640 | |
[email protected] | ab88d154 | 2011-11-18 22:52:00 | [diff] [blame] | 641 | #if defined(OS_MACOSX) |
| 642 | // For dynamic loading support, NaCl requires a file descriptor that |
| 643 | // was created in /tmp, since those created with shm_open() are not |
| 644 | // mappable with PROT_EXEC. Rather than requiring an extra IPC |
| 645 | // round trip out of the sandbox, we create an FD here. |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 646 | base::SharedMemory memory_buffer; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 647 | base::SharedMemoryCreateOptions options; |
| 648 | options.size = 1; |
| 649 | options.executable = true; |
| 650 | if (!memory_buffer.Create(options)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 651 | DLOG(ERROR) << "Failed to allocate memory buffer"; |
| 652 | return false; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 653 | } |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 654 | nacl::FileDescriptor memory_fd; |
| 655 | memory_fd.fd = dup(memory_buffer.handle().fd); |
| 656 | if (memory_fd.fd < 0) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 657 | DLOG(ERROR) << "Failed to dup() a file descriptor"; |
| 658 | return false; |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 659 | } |
| 660 | memory_fd.auto_close = true; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 661 | params.handles.push_back(memory_fd); |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 662 | #endif |
| 663 | |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 664 | #if defined(OS_POSIX) |
[email protected] | ed637bb | 2012-10-15 17:36:05 | [diff] [blame] | 665 | if (params.enable_debug_stub) { |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 666 | SocketDescriptor server_bound_socket = GetDebugStubSocketHandle(); |
| 667 | if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) { |
| 668 | params.debug_stub_server_bound_socket = |
| 669 | nacl::FileDescriptor(server_bound_socket, true); |
| 670 | } |
| 671 | } |
| 672 | #endif |
| 673 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 674 | process_->Send(new NaClProcessMsg_Start(params)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 675 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 676 | internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE; |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 680 | bool NaClProcessHost::SendStart() { |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 681 | if (!enable_ppapi_proxy()) { |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 682 | if (!ReplyToRenderer(IPC::ChannelHandle())) |
| 683 | return false; |
| 684 | } |
| 685 | return StartNaClExecution(); |
| 686 | } |
| 687 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 688 | // This method is called when NaClProcessHostMsg_PpapiChannelCreated is |
| 689 | // received or PpapiHostMsg_ChannelCreated is forwarded by our plugin |
| 690 | // listener. |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 691 | void NaClProcessHost::OnPpapiChannelCreated( |
| 692 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 693 | // Only renderer processes should create a channel. |
| 694 | DCHECK(enable_ppapi_proxy()); |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 695 | // If the proxy channel is null, this must be the initial NaCl-Browser IPC |
| 696 | // channel. |
| 697 | if (!ipc_proxy_channel_.get()) { |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 698 | DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type); |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 699 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 700 | ipc_proxy_channel_.reset( |
| 701 | new IPC::ChannelProxy(channel_handle, |
| 702 | IPC::Channel::MODE_CLIENT, |
| 703 | &ipc_plugin_listener_, |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 704 | base::MessageLoopProxy::current().get())); |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 705 | // Create the browser ppapi host and enable PPAPI message dispatching to the |
| 706 | // browser process. |
[email protected] | adf7cd5 | 2012-12-04 19:37:46 | [diff] [blame] | 707 | ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess( |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 708 | ipc_proxy_channel_.get(), // sender |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 709 | permissions_, |
| 710 | process_->GetData().handle, |
[email protected] | b4b53d18 | 2012-09-12 02:56:55 | [diff] [blame] | 711 | ipc_proxy_channel_.get(), |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 712 | nacl_host_message_filter_->GetHostResolver(), |
| 713 | nacl_host_message_filter_->render_process_id(), |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 714 | render_view_id_, |
| 715 | profile_directory_)); |
[email protected] | adf7cd5 | 2012-12-04 19:37:46 | [diff] [blame] | 716 | |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 717 | ppapi::PpapiNaClChannelArgs args; |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 718 | args.off_the_record = nacl_host_message_filter_->off_the_record(); |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 719 | args.permissions = permissions_; |
| 720 | CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 721 | DCHECK(cmdline); |
| 722 | std::string flag_whitelist[] = {switches::kV, switches::kVModule}; |
| 723 | for (size_t i = 0; i < arraysize(flag_whitelist); ++i) { |
| 724 | std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]); |
| 725 | if (!value.empty()) { |
| 726 | args.switch_names.push_back(flag_whitelist[i]); |
| 727 | args.switch_values.push_back(value); |
| 728 | } |
| 729 | } |
| 730 | |
[email protected] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 731 | ppapi_host_->GetPpapiHost()->AddHostFactoryFilter( |
| 732 | scoped_ptr<ppapi::host::HostFactory>( |
| 733 | new chrome::ChromeBrowserPepperHostFactory(ppapi_host_.get()))); |
| 734 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 735 | // Send a message to create the NaCl-Renderer channel. The handle is just |
| 736 | // a place holder. |
| 737 | ipc_proxy_channel_->Send( |
| 738 | new PpapiMsg_CreateNaClChannel( |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame^] | 739 | nacl_host_message_filter_->render_process_id(), |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 740 | args, |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 741 | SerializedHandle(SerializedHandle::CHANNEL_HANDLE, |
| 742 | IPC::InvalidPlatformFileForTransit()))); |
| 743 | } else if (reply_msg_) { |
| 744 | // Otherwise, this must be a renderer channel. |
| 745 | ReplyToRenderer(channel_handle); |
| 746 | } else { |
| 747 | // Attempt to open more than 1 renderer channel is not supported. |
| 748 | // Shut down the NaCl process. |
| 749 | process_->GetHost()->ForceShutdown(); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) { |
| 754 | // Handle messages that have been forwarded from our PluginListener. |
| 755 | // These messages come from untrusted code so should be handled with care. |
| 756 | bool handled = true; |
| 757 | IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg) |
| 758 | IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated, |
| 759 | OnPpapiChannelCreated) |
| 760 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 761 | IPC_END_MESSAGE_MAP() |
| 762 | return handled; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 763 | } |
| 764 | |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 765 | bool NaClProcessHost::StartWithLaunchedProcess() { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 766 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 767 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 768 | if (nacl_browser->IsReady()) { |
| 769 | return SendStart(); |
| 770 | } else if (nacl_browser->IsOk()) { |
| 771 | nacl_browser->WaitForResources( |
| 772 | base::Bind(&NaClProcessHost::OnResourcesReady, |
| 773 | weak_factory_.GetWeakPtr())); |
| 774 | return true; |
| 775 | } else { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 776 | LOG(ERROR) << "NaCl process failed to launch: previously failed to acquire " |
| 777 | "shared resources"; |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 778 | return false; |
| 779 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 780 | } |
| 781 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 782 | void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature, |
| 783 | bool* result) { |
[email protected] | 7cfaf98 | 2012-05-09 18:37:47 | [diff] [blame] | 784 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 785 | *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) { |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 789 | NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 790 | } |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 791 | |
[email protected] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 792 | void NaClProcessHost::FileResolved( |
| 793 | base::PlatformFile* file, |
| 794 | const base::FilePath& file_path, |
| 795 | IPC::Message* reply_msg) { |
| 796 | if (*file != base::kInvalidPlatformFileValue) { |
| 797 | IPC::PlatformFileForTransit handle = IPC::GetFileHandleForProcess( |
| 798 | *file, |
| 799 | process_->GetData().handle, |
| 800 | true /* close_source */); |
| 801 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 802 | reply_msg, |
| 803 | handle, |
| 804 | file_path); |
| 805 | } else { |
| 806 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 807 | reply_msg, |
| 808 | IPC::InvalidPlatformFileForTransit(), |
| 809 | base::FilePath(FILE_PATH_LITERAL(""))); |
| 810 | } |
| 811 | Send(reply_msg); |
| 812 | } |
| 813 | |
| 814 | void NaClProcessHost::OnResolveFileToken(uint64 file_token_lo, |
| 815 | uint64 file_token_hi, |
| 816 | IPC::Message* reply_msg) { |
| 817 | // Was the file registered? |
| 818 | // |
| 819 | // Note that the file path cache is of bounded size, and old entries can get |
| 820 | // evicted. If a large number of NaCl modules are being launched at once, |
| 821 | // resolving the file_token may fail because the path cache was thrashed |
| 822 | // while the file_token was in flight. In this case the query fails, and we |
| 823 | // need to fall back to the slower path. |
| 824 | // |
| 825 | // However: each NaCl process will consume 2-3 entries as it starts up, this |
| 826 | // means that eviction will not happen unless you start up 33+ NaCl processes |
| 827 | // at the same time, and this still requires worst-case timing. As a |
| 828 | // practical matter, no entries should be evicted prematurely. |
| 829 | // The cache itself should take ~ (150 characters * 2 bytes/char + ~60 bytes |
| 830 | // data structure overhead) * 100 = 35k when full, so making it bigger should |
| 831 | // not be a problem, if needed. |
| 832 | // |
| 833 | // Each NaCl process will consume 2-3 entries because the manifest and main |
| 834 | // nexe are currently not resolved. Shared libraries will be resolved. They |
| 835 | // will be loaded sequentially, so they will only consume a single entry |
| 836 | // while the load is in flight. |
| 837 | // |
| 838 | // TODO(ncbray): track behavior with UMA. If entries are getting evicted or |
| 839 | // bogus keys are getting queried, this would be good to know. |
| 840 | base::FilePath file_path; |
| 841 | if (!NaClBrowser::GetInstance()->GetFilePath(file_token_lo, file_token_hi, |
| 842 | &file_path)) { |
| 843 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 844 | reply_msg, |
| 845 | IPC::InvalidPlatformFileForTransit(), |
| 846 | base::FilePath(FILE_PATH_LITERAL(""))); |
| 847 | Send(reply_msg); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | // Scratch space to share between the callbacks. |
| 852 | base::PlatformFile* data = new base::PlatformFile(); |
| 853 | |
| 854 | // Open the file. |
| 855 | if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 856 | FROM_HERE, |
| 857 | base::Bind(nacl::OpenNaClExecutableImpl, |
| 858 | file_path, data), |
| 859 | base::Bind(&NaClProcessHost::FileResolved, |
| 860 | weak_factory_.GetWeakPtr(), |
| 861 | base::Owned(data), |
| 862 | file_path, |
| 863 | reply_msg))) { |
| 864 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 865 | reply_msg, |
| 866 | IPC::InvalidPlatformFileForTransit(), |
| 867 | base::FilePath(FILE_PATH_LITERAL(""))); |
| 868 | Send(reply_msg); |
| 869 | } |
| 870 | } |
| 871 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 872 | #if defined(OS_WIN) |
| 873 | void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info, |
| 874 | IPC::Message* reply_msg) { |
| 875 | if (!AttachDebugExceptionHandler(info, reply_msg)) { |
| 876 | // Send failure message. |
| 877 | NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg, |
| 878 | false); |
| 879 | Send(reply_msg); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info, |
| 884 | IPC::Message* reply_msg) { |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 885 | if (!enable_exception_handling_ && !enable_debug_stub_) { |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 886 | DLOG(ERROR) << |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 887 | "Debug exception handler requested by NaCl process when not enabled"; |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 888 | return false; |
| 889 | } |
| 890 | if (debug_exception_handler_requested_) { |
| 891 | // The NaCl process should not request this multiple times. |
| 892 | DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received"; |
| 893 | return false; |
| 894 | } |
| 895 | debug_exception_handler_requested_ = true; |
| 896 | |
| 897 | base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle); |
| 898 | base::win::ScopedHandle process_handle; |
| 899 | // We cannot use process_->GetData().handle because it does not have |
| 900 | // the necessary access rights. We open the new handle here rather |
| 901 | // than in the NaCl broker process in case the NaCl loader process |
| 902 | // dies before the NaCl broker process receives the message we send. |
| 903 | // The debug exception handler uses DebugActiveProcess() to attach, |
| 904 | // but this takes a PID. We need to prevent the NaCl loader's PID |
| 905 | // from being reused before DebugActiveProcess() is called, and |
| 906 | // holding a process handle open achieves this. |
| 907 | if (!base::OpenProcessHandleWithAccess( |
| 908 | nacl_pid, |
| 909 | base::kProcessAccessQueryInformation | |
| 910 | base::kProcessAccessSuspendResume | |
| 911 | base::kProcessAccessTerminate | |
| 912 | base::kProcessAccessVMOperation | |
| 913 | base::kProcessAccessVMRead | |
| 914 | base::kProcessAccessVMWrite | |
[email protected] | 2917232 | 2013-01-10 17:35:16 | [diff] [blame] | 915 | base::kProcessAccessDuplicateHandle | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 916 | base::kProcessAccessWaitForTermination, |
| 917 | process_handle.Receive())) { |
| 918 | LOG(ERROR) << "Failed to get process handle"; |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | attach_debug_exception_handler_reply_msg_.reset(reply_msg); |
| 923 | // If the NaCl loader is 64-bit, the process running its debug |
| 924 | // exception handler must be 64-bit too, so we use the 64-bit NaCl |
| 925 | // broker process for this. Otherwise, on a 32-bit system, we use |
| 926 | // the 32-bit browser process to run the debug exception handler. |
| 927 | if (RunningOnWOW64()) { |
| 928 | return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler( |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 929 | weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info); |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 930 | } else { |
| 931 | NaClStartDebugExceptionHandlerThread( |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 932 | process_handle.Take(), info, |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 933 | base::MessageLoopProxy::current(), |
| 934 | base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker, |
| 935 | weak_factory_.GetWeakPtr())); |
| 936 | return true; |
| 937 | } |
| 938 | } |
| 939 | #endif |