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