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