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