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