[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] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 12 | #include "base/memory/mru_cache.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 0ae52b4 | 2012-03-27 02:53:59 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 16 | #include "base/string_util.h" |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 17 | #include "base/rand_util.h" |
[email protected] | a0a69bf | 2011-09-23 21:40:28 | [diff] [blame] | 18 | #include "base/stringprintf.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 19 | #include "base/utf_string_conversions.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 20 | #include "base/win/windows_version.h" |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_info_map.h" |
| 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] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 36 | #include "googleurl/src/gurl.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 37 | #include "ipc/ipc_switches.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 38 | #include "native_client/src/shared/imc/nacl_imc.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] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 49 | #include "chrome/browser/nacl_host/nacl_broker_service_win.h" |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 50 | #include "native_client/src/trusted/service_runtime/win/debug_exception_handler.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 51 | #endif |
| 52 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 53 | using content::BrowserThread; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 54 | using content::ChildProcessData; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 55 | using content::ChildProcessHost; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 56 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 57 | #if defined(OS_WIN) |
| 58 | class NaClProcessHost::DebugContext |
| 59 | : public base::RefCountedThreadSafe<NaClProcessHost::DebugContext> { |
| 60 | public: |
| 61 | DebugContext() |
| 62 | : can_send_start_msg_(false), |
| 63 | child_process_host_(NULL) { |
| 64 | } |
| 65 | |
| 66 | ~DebugContext() { |
| 67 | } |
| 68 | |
| 69 | void AttachDebugger(int pid, base::ProcessHandle process); |
| 70 | |
| 71 | // 6 methods below must be called on Browser::IO thread. |
| 72 | void SetStartMessage(IPC::Message* start_msg); |
| 73 | void SetChildProcessHost(content::ChildProcessHost* child_process_host); |
| 74 | void SetDebugThread(base::Thread* thread_); |
| 75 | |
| 76 | // Start message is sent from 2 flows of execution. The first flow is |
| 77 | // NaClProcessHost::SendStart. The second flow is |
| 78 | // NaClProcessHost::OnChannelConnected and |
| 79 | // NaClProcessHost::DebugContext::AttachThread. The message itself is created |
| 80 | // by first flow. But the moment it can be sent is determined by second flow. |
| 81 | // So first flow executes SetStartMessage and SendStartMessage while second |
| 82 | // flow uses AllowAndSendStartMessage to either send potentially pending |
| 83 | // start message or set the flag that allows the first flow to do this. |
| 84 | |
| 85 | // Clears the flag that prevents sending start message. |
| 86 | void AllowToSendStartMsg(); |
| 87 | // Send start message to the NaCl process or do nothing if message is not |
| 88 | // set or not allowed to be send. If message is sent, it is cleared and |
| 89 | // repeated calls do nothing. |
| 90 | void SendStartMessage(); |
| 91 | // Clear the flag that prevents further sending start message and send start |
| 92 | // message if it is set. |
| 93 | void AllowAndSendStartMessage(); |
| 94 | private: |
| 95 | void StopThread(); |
| 96 | // These 4 fields are accessed only from Browser::IO thread. |
| 97 | scoped_ptr<base::Thread> thread_; |
| 98 | scoped_ptr<IPC::Message> start_msg_; |
| 99 | // Debugger is attached or exception handling is not switched on. |
| 100 | // This means that start message can be sent to the NaCl process. |
| 101 | bool can_send_start_msg_; |
| 102 | content::ChildProcessHost* child_process_host_; |
| 103 | }; |
| 104 | |
| 105 | void NaClProcessHost::DebugContext::AttachDebugger( |
| 106 | int pid, base::ProcessHandle process) { |
| 107 | BOOL attached; |
| 108 | DWORD exit_code; |
| 109 | attached = DebugActiveProcess(pid); |
| 110 | if (!attached) { |
| 111 | LOG(ERROR) << "Failed to connect to the process"; |
| 112 | } |
| 113 | BrowserThread::PostTask( |
| 114 | BrowserThread::IO, FROM_HERE, |
| 115 | base::Bind( |
| 116 | &NaClProcessHost::DebugContext::AllowAndSendStartMessage, this)); |
| 117 | if (attached) { |
| 118 | // debug the process |
| 119 | NaClDebugLoop(process, &exit_code); |
| 120 | base::CloseProcessHandle(process); |
| 121 | } |
| 122 | BrowserThread::PostTask( |
| 123 | BrowserThread::IO, FROM_HERE, |
| 124 | base::Bind(&NaClProcessHost::DebugContext::StopThread, this)); |
| 125 | } |
| 126 | |
| 127 | void NaClProcessHost::DebugContext::SetStartMessage(IPC::Message* start_msg) { |
| 128 | start_msg_.reset(start_msg); |
| 129 | } |
| 130 | |
| 131 | void NaClProcessHost::DebugContext::SetChildProcessHost( |
| 132 | content::ChildProcessHost* child_process_host) { |
| 133 | child_process_host_ = child_process_host; |
| 134 | } |
| 135 | |
| 136 | void NaClProcessHost::DebugContext::SetDebugThread(base::Thread* thread) { |
| 137 | thread_.reset(thread); |
| 138 | } |
| 139 | |
| 140 | void NaClProcessHost::DebugContext::AllowToSendStartMsg() { |
| 141 | can_send_start_msg_ = true; |
| 142 | } |
| 143 | |
| 144 | void NaClProcessHost::DebugContext::SendStartMessage() { |
| 145 | if (start_msg_.get() && can_send_start_msg_) { |
| 146 | if (child_process_host_) { |
| 147 | if (!child_process_host_->Send(start_msg_.release())) { |
| 148 | LOG(ERROR) << "Failed to send start message"; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void NaClProcessHost::DebugContext::AllowAndSendStartMessage() { |
| 155 | AllowToSendStartMsg(); |
| 156 | SendStartMessage(); |
| 157 | } |
| 158 | |
| 159 | void NaClProcessHost::DebugContext::StopThread() { |
| 160 | thread_->Stop(); |
| 161 | thread_.reset(); |
| 162 | } |
| 163 | #endif |
| 164 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 165 | namespace { |
| 166 | |
| 167 | void SetCloseOnExec(nacl::Handle fd) { |
| 168 | #if defined(OS_POSIX) |
| 169 | int flags = fcntl(fd, F_GETFD); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 170 | CHECK_NE(flags, -1); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 171 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 172 | CHECK_EQ(rc, 0); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 173 | #endif |
| 174 | } |
| 175 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 176 | // Represents shared state for all NaClProcessHost objects in the browser. |
| 177 | // Currently this just handles holding onto the file descriptor for the IRT. |
| 178 | class NaClBrowser { |
| 179 | public: |
| 180 | static NaClBrowser* GetInstance() { |
| 181 | return Singleton<NaClBrowser>::get(); |
| 182 | } |
| 183 | |
| 184 | bool IrtAvailable() const { |
| 185 | return irt_platform_file_ != base::kInvalidPlatformFileValue; |
| 186 | } |
| 187 | |
| 188 | base::PlatformFile IrtFile() const { |
| 189 | CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue); |
| 190 | return irt_platform_file_; |
| 191 | } |
| 192 | |
| 193 | // Asynchronously attempt to get the IRT open. |
| 194 | bool EnsureIrtAvailable(); |
| 195 | |
| 196 | // Make sure the IRT gets opened and follow up with the reply when it's ready. |
| 197 | bool MakeIrtAvailable(const base::Closure& reply); |
| 198 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 199 | // Path to IRT. Available even before IRT is loaded. |
| 200 | const FilePath& GetIrtFilePath(); |
| 201 | |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 202 | // Get the key used for HMACing validation signatures. This should be a |
| 203 | // string of cryptographically secure random bytes. |
| 204 | const std::string& GetValidatorCacheKey() const { |
| 205 | return validator_cache_key_; |
| 206 | } |
| 207 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 208 | // Is the validation signature in the database? |
| 209 | bool QueryKnownToValidate(const std::string& signature); |
| 210 | |
| 211 | // Put the validation signature in the database. |
| 212 | void SetKnownToValidate(const std::string& signature); |
| 213 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 214 | private: |
| 215 | base::PlatformFile irt_platform_file_; |
| 216 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 217 | FilePath irt_filepath_; |
| 218 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 219 | typedef base::HashingMRUCache<std::string, bool> ValidationCacheType; |
| 220 | ValidationCacheType validation_cache_; |
| 221 | |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 222 | std::string validator_cache_key_; |
| 223 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 224 | friend struct DefaultSingletonTraits<NaClBrowser>; |
| 225 | |
| 226 | NaClBrowser() |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 227 | : irt_platform_file_(base::kInvalidPlatformFileValue), |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 228 | irt_filepath_(), |
| 229 | // For the moment, choose an arbitrary cache size. |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 230 | validation_cache_(200), |
| 231 | // TODO(ncbray) persist this key along with the cache. |
| 232 | // Key size is equal to the block size (not the digest size) of SHA256. |
| 233 | validator_cache_key_(base::RandBytesAsString(64)) { |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 234 | InitIrtFilePath(); |
| 235 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 236 | |
| 237 | ~NaClBrowser() { |
| 238 | if (irt_platform_file_ != base::kInvalidPlatformFileValue) |
| 239 | base::ClosePlatformFile(irt_platform_file_); |
| 240 | } |
| 241 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 242 | void InitIrtFilePath(); |
| 243 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 244 | void OpenIrtLibraryFile(); |
| 245 | |
| 246 | static void DoOpenIrtLibraryFile() { |
| 247 | GetInstance()->OpenIrtLibraryFile(); |
| 248 | } |
| 249 | |
| 250 | DISALLOW_COPY_AND_ASSIGN(NaClBrowser); |
| 251 | }; |
| 252 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 253 | } // namespace |
| 254 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 255 | struct NaClProcessHost::NaClInternal { |
| 256 | std::vector<nacl::Handle> sockets_for_renderer; |
| 257 | std::vector<nacl::Handle> sockets_for_sel_ldr; |
| 258 | }; |
| 259 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 260 | #if defined(OS_WIN) |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 261 | static bool RunningOnWOW64() { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 262 | return (base::win::OSInfo::GetInstance()->wow64_status() == |
| 263 | base::win::OSInfo::WOW64_ENABLED); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 264 | } |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 265 | #endif |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 266 | |
[email protected] | 1dbaaa70 | 2011-04-06 17:43:42 | [diff] [blame] | 267 | NaClProcessHost::NaClProcessHost(const std::wstring& url) |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 268 | : |
| 269 | #if defined(OS_WIN) |
| 270 | process_launched_by_broker_(false), |
| 271 | #endif |
| 272 | reply_msg_(NULL), |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 273 | internal_(new NaClInternal()), |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 274 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 275 | enable_exception_handling_(false) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 276 | process_.reset(content::BrowserChildProcessHost::Create( |
| 277 | content::PROCESS_TYPE_NACL_LOADER, this)); |
| 278 | process_->SetName(WideToUTF16Hack(url)); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 279 | |
| 280 | // We allow untrusted hardware exception handling to be enabled via |
| 281 | // an env var for consistency with the standalone build of NaCl. |
| 282 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 283 | switches::kEnableNaClExceptionHandling) || |
| 284 | getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) { |
| 285 | enable_exception_handling_ = true; |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 286 | #if defined(OS_WIN) |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 287 | debug_context_ = new DebugContext(); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 288 | #endif |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 289 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 292 | NaClProcessHost::~NaClProcessHost() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 293 | int exit_code; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 294 | process_->GetTerminationStatus(&exit_code); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 295 | std::string message = |
| 296 | base::StringPrintf("NaCl process exited with status %i (0x%x)", |
| 297 | exit_code, exit_code); |
| 298 | if (exit_code == 0) { |
| 299 | LOG(INFO) << message; |
| 300 | } else { |
| 301 | LOG(ERROR) << message; |
| 302 | } |
| 303 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 304 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 305 | if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) { |
| 306 | LOG(ERROR) << "nacl::Close() failed"; |
| 307 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 308 | } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 309 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 310 | if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) { |
| 311 | LOG(ERROR) << "nacl::Close() failed"; |
| 312 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 315 | if (reply_msg_) { |
| 316 | // The process failed to launch for some reason. |
| 317 | // Don't keep the renderer hanging. |
| 318 | reply_msg_->set_reply_error(); |
| 319 | chrome_render_message_filter_->Send(reply_msg_); |
| 320 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 321 | #if defined(OS_WIN) |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 322 | if (process_launched_by_broker_) { |
| 323 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 324 | } |
| 325 | if (debug_context_ != NULL) { |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 326 | debug_context_->SetChildProcessHost(NULL); |
| 327 | } |
| 328 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 331 | // Attempt to ensure the IRT will be available when we need it, but don't wait. |
| 332 | bool NaClBrowser::EnsureIrtAvailable() { |
| 333 | if (IrtAvailable()) |
| 334 | return true; |
| 335 | |
| 336 | return BrowserThread::PostTask( |
| 337 | BrowserThread::FILE, FROM_HERE, |
| 338 | base::Bind(&NaClBrowser::DoOpenIrtLibraryFile)); |
| 339 | } |
| 340 | |
| 341 | // We really need the IRT to be available now, so make sure that it is. |
| 342 | // When it's ready, we'll run the reply closure. |
| 343 | bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) { |
| 344 | return BrowserThread::PostTaskAndReply( |
| 345 | BrowserThread::FILE, FROM_HERE, |
| 346 | base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply); |
| 347 | } |
| 348 | |
| 349 | // This is called at browser startup. |
| 350 | // static |
| 351 | void NaClProcessHost::EarlyStartup() { |
| 352 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 353 | // Open the IRT file early to make sure that it isn't replaced out from |
| 354 | // under us by autoupdate. |
| 355 | NaClBrowser::GetInstance()->EnsureIrtAvailable(); |
| 356 | #endif |
| 357 | } |
| 358 | |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 359 | void NaClProcessHost::Launch( |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 360 | ChromeRenderMessageFilter* chrome_render_message_filter, |
| 361 | int socket_count, |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 362 | IPC::Message* reply_msg, |
| 363 | scoped_refptr<ExtensionInfoMap> extension_info_map) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 364 | chrome_render_message_filter_ = chrome_render_message_filter; |
| 365 | reply_msg_ = reply_msg; |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 366 | extension_info_map_ = extension_info_map; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 367 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 368 | // Place an arbitrary limit on the number of sockets to limit |
| 369 | // exposure in case the renderer is compromised. We can increase |
| 370 | // this if necessary. |
| 371 | if (socket_count > 8) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 372 | delete this; |
| 373 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 376 | // Start getting the IRT open asynchronously while we launch the NaCl process. |
| 377 | // We'll make sure this actually finished in OnProcessLaunched, below. |
| 378 | if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) { |
| 379 | LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed"; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 380 | delete this; |
| 381 | return; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 384 | // Rather than creating a socket pair in the renderer, and passing |
| 385 | // one side through the browser to sel_ldr, socket pairs are created |
| 386 | // in the browser and then passed to the renderer and sel_ldr. |
| 387 | // |
| 388 | // This is mainly for the benefit of Windows, where sockets cannot |
| 389 | // be passed in messages, but are copied via DuplicateHandle(). |
| 390 | // This means the sandboxed renderer cannot send handles to the |
| 391 | // browser process. |
| 392 | |
| 393 | for (int i = 0; i < socket_count; i++) { |
| 394 | nacl::Handle pair[2]; |
| 395 | // Create a connected socket |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 396 | if (nacl::SocketPair(pair) == -1) { |
| 397 | delete this; |
| 398 | return; |
| 399 | } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 400 | internal_->sockets_for_renderer.push_back(pair[0]); |
| 401 | internal_->sockets_for_sel_ldr.push_back(pair[1]); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 402 | SetCloseOnExec(pair[0]); |
| 403 | SetCloseOnExec(pair[1]); |
| 404 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 405 | |
| 406 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 407 | if (!LaunchSelLdr()) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 408 | delete this; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 409 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 410 | } |
| 411 | |
[email protected] | 8ae7bd6f | 2012-03-14 03:23:02 | [diff] [blame] | 412 | scoped_ptr<CommandLine> NaClProcessHost::LaunchWithNaClGdb( |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 413 | const FilePath& nacl_gdb, |
| 414 | CommandLine* line, |
| 415 | const FilePath& manifest_path) { |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 416 | CommandLine* cmd_line = new CommandLine(nacl_gdb); |
| 417 | // We can't use PrependWrapper because our parameters contain spaces. |
| 418 | cmd_line->AppendArg("--eval-command"); |
| 419 | const FilePath::StringType& irt_path = |
| 420 | NaClBrowser::GetInstance()->GetIrtFilePath().value(); |
| 421 | cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path); |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 422 | if (!manifest_path.empty()) { |
| 423 | cmd_line->AppendArg("--eval-command"); |
| 424 | cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") + |
| 425 | manifest_path.value()); |
| 426 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 427 | cmd_line->AppendArg("--args"); |
| 428 | const CommandLine::StringVector& argv = line->argv(); |
| 429 | for (size_t i = 0; i < argv.size(); i++) { |
| 430 | cmd_line->AppendArgNative(argv[i]); |
| 431 | } |
| 432 | return scoped_ptr<CommandLine>(cmd_line); |
| 433 | } |
| 434 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 435 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 436 | std::string channel_id = process_->GetHost()->CreateChannel(); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 437 | if (channel_id.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 438 | return false; |
| 439 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 440 | CommandLine::StringType nacl_loader_prefix; |
| 441 | #if defined(OS_POSIX) |
| 442 | nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 443 | switches::kNaClLoaderCmdPrefix); |
| 444 | #endif // defined(OS_POSIX) |
| 445 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 446 | // Build command line for nacl. |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 447 | |
| 448 | #if defined(OS_MACOSX) |
| 449 | // The Native Client process needs to be able to allocate a 1GB contiguous |
| 450 | // region to use as the client environment's virtual address space. ASLR |
| 451 | // (PIE) interferes with this by making it possible that no gap large enough |
| 452 | // to accomodate this request will exist in the child process' address |
| 453 | // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and |
| 454 | // http://code.google.com/p/nativeclient/issues/detail?id=2043. |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 455 | int flags = ChildProcessHost::CHILD_NO_PIE; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 456 | #elif defined(OS_LINUX) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 457 | int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF : |
| 458 | ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 459 | #else |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 460 | int flags = ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 461 | #endif |
| 462 | |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 463 | FilePath exe_path = ChildProcessHost::GetChildPath(flags); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 464 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 465 | return false; |
| 466 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 467 | #if defined(OS_WIN) |
| 468 | // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe |
| 469 | if (RunningOnWOW64()) { |
| 470 | FilePath module_path; |
| 471 | if (!PathService::Get(base::FILE_MODULE, &module_path)) |
| 472 | return false; |
| 473 | exe_path = module_path.DirName().Append(chrome::kNaClAppName); |
| 474 | } |
| 475 | #endif |
| 476 | |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 477 | scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path)); |
| 478 | nacl::CopyNaClCommandLineArguments(cmd_line.get()); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 479 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 480 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 481 | switches::kNaClLoaderProcess); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 482 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
[email protected] | 93156cec | 2011-09-12 21:14:44 | [diff] [blame] | 483 | if (logging::DialogsAreSuppressed()) |
| 484 | cmd_line->AppendSwitch(switches::kNoErrorDialogs); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 485 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 486 | if (!nacl_loader_prefix.empty()) |
| 487 | cmd_line->PrependWrapper(nacl_loader_prefix); |
| 488 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 489 | FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 490 | switches::kNaClGdb); |
| 491 | if (!nacl_gdb.empty()) { |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 492 | GURL manifest_url = GURL(process_->GetData().name); |
| 493 | FilePath manifest_path; |
| 494 | const Extension* extension = extension_info_map_->extensions(). |
| 495 | GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url)); |
| 496 | if (extension != NULL && manifest_url.SchemeIs(chrome::kExtensionScheme)) { |
| 497 | std::string path = manifest_url.path(); |
| 498 | TrimString(path, "/", &path); // Remove first slash |
| 499 | manifest_path = extension->path().AppendASCII(path); |
| 500 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 501 | cmd_line->AppendSwitch(switches::kNoSandbox); |
| 502 | scoped_ptr<CommandLine> gdb_cmd_line( |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 503 | LaunchWithNaClGdb(nacl_gdb, cmd_line.get(), manifest_path)); |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 504 | // We can't use process_->Launch() because OnProcessLaunched will be called |
| 505 | // with process_->GetData().handle filled by handle of gdb process. This |
| 506 | // handle will be used to duplicate handles for NaCl process and as |
| 507 | // a result NaCl process will not be able to use them. |
| 508 | // |
| 509 | // So we don't fill process_->GetData().handle and wait for |
| 510 | // OnChannelConnected to get handle of NaCl process from its pid. Then we |
| 511 | // call OnProcessLaunched. |
| 512 | return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL); |
| 513 | } |
| 514 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 515 | // 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] | 516 | #if defined(OS_WIN) |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 517 | if (RunningOnWOW64()) { |
[email protected] | 644b2e2 | 2012-03-25 01:41:07 | [diff] [blame] | 518 | return NaClBrokerService::GetInstance()->LaunchLoader(this, channel_id); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 519 | } else { |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 520 | process_->Launch(FilePath(), cmd_line.release()); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 521 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 522 | #elif defined(OS_POSIX) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 523 | process_->Launch(nacl_loader_prefix.empty(), // use_zygote |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 524 | base::EnvironmentVector(), |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 525 | cmd_line.release()); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 526 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 527 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 528 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 529 | } |
| 530 | |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 531 | #if defined(OS_WIN) |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 532 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 533 | process_launched_by_broker_ = true; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 534 | process_->SetHandle(handle); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 535 | OnProcessLaunched(); |
| 536 | } |
| 537 | |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 538 | void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker() { |
| 539 | debug_context_->AllowAndSendStartMessage(); |
| 540 | } |
| 541 | #endif |
| 542 | |
[email protected] | 463ea5f | 2011-12-03 06:57:47 | [diff] [blame] | 543 | void NaClProcessHost::OnProcessCrashed(int exit_code) { |
| 544 | std::string message = base::StringPrintf( |
| 545 | "NaCl process exited with status %i (0x%x)", exit_code, exit_code); |
| 546 | LOG(ERROR) << message; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 547 | } |
| 548 | |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 549 | namespace { |
| 550 | |
| 551 | // Determine the name of the IRT file based on the architecture. |
| 552 | |
| 553 | #define NACL_IRT_FILE_NAME(arch_string) \ |
| 554 | (FILE_PATH_LITERAL("nacl_irt_") \ |
| 555 | FILE_PATH_LITERAL(arch_string) \ |
| 556 | FILE_PATH_LITERAL(".nexe")) |
| 557 | |
| 558 | const FilePath::StringType NaClIrtName() { |
| 559 | #if defined(ARCH_CPU_X86_FAMILY) |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 560 | #if defined(ARCH_CPU_X86_64) |
[email protected] | 5804cb5d | 2011-12-19 21:55:35 | [diff] [blame] | 561 | bool is64 = true; |
| 562 | #elif defined(OS_WIN) |
| 563 | bool is64 = RunningOnWOW64(); |
| 564 | #else |
| 565 | bool is64 = false; |
[email protected] | 75e0c50 | 2011-12-16 21:53:01 | [diff] [blame] | 566 | #endif |
| 567 | return is64 ? NACL_IRT_FILE_NAME("x86_64") : NACL_IRT_FILE_NAME("x86_32"); |
| 568 | #elif defined(ARCH_CPU_ARMEL) |
| 569 | // TODO(mcgrathr): Eventually we'll need to distinguish arm32 vs thumb2. |
| 570 | // That may need to be based on the actual nexe rather than a static |
| 571 | // choice, which would require substantial refactoring. |
| 572 | return NACL_IRT_FILE_NAME("arm"); |
| 573 | #else |
| 574 | #error Add support for your architecture to NaCl IRT file selection |
| 575 | #endif |
| 576 | } |
| 577 | |
| 578 | } // namespace |
| 579 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 580 | void NaClBrowser::InitIrtFilePath() { |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 581 | // Allow the IRT library to be overridden via an environment |
| 582 | // variable. This allows the NaCl/Chromium integration bot to |
| 583 | // specify a newly-built IRT rather than using a prebuilt one |
| 584 | // downloaded via Chromium's DEPS file. We use the same environment |
| 585 | // variable that the standalone NaCl PPAPI plugin accepts. |
| 586 | const char* irt_path_var = getenv("NACL_IRT_LIBRARY"); |
| 587 | if (irt_path_var != NULL) { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 588 | FilePath::StringType path_string( |
| 589 | irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0'))); |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 590 | irt_filepath_ = FilePath(path_string); |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 591 | } else { |
| 592 | FilePath plugin_dir; |
| 593 | if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) { |
| 594 | LOG(ERROR) << "Failed to locate the plugins directory"; |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 595 | return; |
| 596 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 597 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 598 | irt_filepath_ = plugin_dir.Append(NaClIrtName()); |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 599 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | const FilePath& NaClBrowser::GetIrtFilePath() { |
| 603 | return irt_filepath_; |
| 604 | } |
| 605 | |
| 606 | // This only ever runs on the BrowserThread::FILE thread. |
| 607 | // If multiple tasks are posted, the later ones are no-ops. |
| 608 | void NaClBrowser::OpenIrtLibraryFile() { |
| 609 | if (irt_platform_file_ != base::kInvalidPlatformFileValue) |
| 610 | // We've already run. |
| 611 | return; |
[email protected] | 88e1583 | 2011-07-19 01:18:24 | [diff] [blame] | 612 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 613 | base::PlatformFileError error_code; |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 614 | irt_platform_file_ = base::CreatePlatformFile(irt_filepath_, |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 615 | base::PLATFORM_FILE_OPEN | |
| 616 | base::PLATFORM_FILE_READ, |
| 617 | NULL, |
| 618 | &error_code); |
| 619 | if (error_code != base::PLATFORM_FILE_OK) { |
| 620 | LOG(ERROR) << "Failed to open NaCl IRT file \"" |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 621 | << irt_filepath_.LossyDisplayName() |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 622 | << "\": " << error_code; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void NaClProcessHost::OnProcessLaunched() { |
| 627 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 628 | |
| 629 | if (nacl_browser->IrtAvailable()) { |
| 630 | // The IRT is already open. Away we go. |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 631 | SendStart(); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 632 | } else { |
| 633 | // We're waiting for the IRT to be open. |
[email protected] | 98999e80 | 2011-12-21 21:54:43 | [diff] [blame] | 634 | if (!nacl_browser->MakeIrtAvailable( |
| 635 | base::Bind(&NaClProcessHost::IrtReady, |
| 636 | weak_factory_.GetWeakPtr()))) |
| 637 | delete this; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | // The asynchronous attempt to get the IRT file open has completed. |
| 642 | void NaClProcessHost::IrtReady() { |
| 643 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 644 | |
| 645 | if (nacl_browser->IrtAvailable()) { |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 646 | SendStart(); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 647 | } else { |
| 648 | LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed"; |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 649 | delete this; |
| 650 | } |
| 651 | } |
| 652 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 653 | #if defined(OS_WIN) |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 654 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 655 | // Set process handle, if it was not set previously. |
| 656 | // This is needed when NaCl process is launched with nacl-gdb. |
| 657 | if (process_->GetData().handle == base::kNullProcessHandle) { |
| 658 | base::ProcessHandle process; |
| 659 | DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 660 | switches::kNaClGdb).empty()); |
| 661 | if (base::OpenProcessHandleWithAccess( |
| 662 | peer_pid, |
| 663 | base::kProcessAccessDuplicateHandle | |
[email protected] | 5926124 | 2012-03-19 13:08:59 | [diff] [blame] | 664 | base::kProcessAccessQueryInformation | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 665 | base::kProcessAccessWaitForTermination, |
| 666 | &process)) { |
| 667 | process_->SetHandle(process); |
| 668 | OnProcessLaunched(); |
| 669 | } else { |
| 670 | LOG(ERROR) << "Failed to get process handle"; |
| 671 | } |
| 672 | } |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 673 | if (debug_context_ == NULL) { |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 674 | return; |
| 675 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 676 | debug_context_->SetChildProcessHost(process_->GetHost()); |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 677 | if (RunningOnWOW64()) { |
| 678 | if (!NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler( |
| 679 | this, peer_pid)) { |
| 680 | debug_context_->AllowAndSendStartMessage(); |
| 681 | } |
| 682 | } else { |
| 683 | // Start new thread for debug loop |
| 684 | // We can't use process_->GetData().handle because it doesn't have necessary |
| 685 | // access rights. |
| 686 | base::ProcessHandle process; |
| 687 | if (!base::OpenProcessHandleWithAccess( |
| 688 | peer_pid, |
| 689 | base::kProcessAccessQueryInformation | |
| 690 | base::kProcessAccessSuspendResume | |
| 691 | base::kProcessAccessTerminate | |
| 692 | base::kProcessAccessVMOperation | |
| 693 | base::kProcessAccessVMRead | |
| 694 | base::kProcessAccessVMWrite | |
| 695 | base::kProcessAccessWaitForTermination, |
| 696 | &process)) { |
| 697 | LOG(ERROR) << "Failed to open the process"; |
| 698 | debug_context_->AllowAndSendStartMessage(); |
| 699 | return; |
| 700 | } |
| 701 | base::Thread* dbg_thread = new base::Thread("Debug thread"); |
| 702 | if (!dbg_thread->Start()) { |
| 703 | LOG(ERROR) << "Debug thread not started"; |
| 704 | debug_context_->AllowAndSendStartMessage(); |
| 705 | base::CloseProcessHandle(process); |
| 706 | return; |
| 707 | } |
| 708 | debug_context_->SetDebugThread(dbg_thread); |
| 709 | // System can not reallocate pid until we close process handle. So using |
| 710 | // pid in different thread is fine. |
| 711 | dbg_thread->message_loop()->PostTask(FROM_HERE, |
| 712 | base::Bind(&NaClProcessHost::DebugContext::AttachDebugger, |
| 713 | debug_context_, peer_pid, process)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 714 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 715 | } |
| 716 | #else |
| 717 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 722 | static bool SendHandleToSelLdr( |
| 723 | base::ProcessHandle processh, |
| 724 | nacl::Handle sourceh, bool close_source, |
| 725 | std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) { |
| 726 | #if defined(OS_WIN) |
| 727 | HANDLE channel; |
| 728 | int flags = DUPLICATE_SAME_ACCESS; |
| 729 | if (close_source) |
| 730 | flags |= DUPLICATE_CLOSE_SOURCE; |
| 731 | if (!DuplicateHandle(GetCurrentProcess(), |
| 732 | reinterpret_cast<HANDLE>(sourceh), |
| 733 | processh, |
| 734 | &channel, |
| 735 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 736 | FALSE, |
| 737 | flags)) { |
| 738 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 739 | return false; |
| 740 | } |
| 741 | handles_for_sel_ldr->push_back( |
| 742 | reinterpret_cast<nacl::FileDescriptor>(channel)); |
| 743 | #else |
| 744 | nacl::FileDescriptor channel; |
| 745 | channel.fd = sourceh; |
| 746 | channel.auto_close = close_source; |
| 747 | handles_for_sel_ldr->push_back(channel); |
| 748 | #endif |
| 749 | return true; |
| 750 | } |
| 751 | |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 752 | void NaClProcessHost::SendStart() { |
| 753 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 754 | base::PlatformFile irt_file = nacl_browser->IrtFile(); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 755 | CHECK_NE(irt_file, base::kInvalidPlatformFileValue); |
| 756 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 757 | std::vector<nacl::FileDescriptor> handles_for_renderer; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 758 | base::ProcessHandle nacl_process_handle; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 759 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 760 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 761 | #if defined(OS_WIN) |
| 762 | // Copy the handle into the renderer process. |
| 763 | HANDLE handle_in_renderer; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 764 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
| 765 | reinterpret_cast<HANDLE>( |
| 766 | internal_->sockets_for_renderer[i]), |
| 767 | chrome_render_message_filter_->peer_handle(), |
| 768 | &handle_in_renderer, |
| 769 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 770 | FALSE, |
| 771 | DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
| 772 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 773 | delete this; |
| 774 | return; |
| 775 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 776 | handles_for_renderer.push_back( |
| 777 | reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer)); |
| 778 | #else |
| 779 | // No need to dup the imc_handle - we don't pass it anywhere else so |
| 780 | // it cannot be closed. |
| 781 | nacl::FileDescriptor imc_handle; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 782 | imc_handle.fd = internal_->sockets_for_renderer[i]; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 783 | imc_handle.auto_close = true; |
| 784 | handles_for_renderer.push_back(imc_handle); |
| 785 | #endif |
| 786 | } |
| 787 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 788 | const ChildProcessData& data = process_->GetData(); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 789 | #if defined(OS_WIN) |
| 790 | // Copy the process handle into the renderer process. |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 791 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 792 | data.handle, |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 793 | chrome_render_message_filter_->peer_handle(), |
| 794 | &nacl_process_handle, |
| 795 | PROCESS_DUP_HANDLE, |
| 796 | FALSE, |
| 797 | 0)) { |
| 798 | LOG(ERROR) << "DuplicateHandle() failed"; |
| 799 | delete this; |
| 800 | return; |
| 801 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 802 | #else |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 803 | // We use pid as process handle on Posix |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 804 | nacl_process_handle = data.handle; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 805 | #endif |
| 806 | |
| 807 | // Get the pid of the NaCl process |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 808 | base::ProcessId nacl_process_id = base::GetProcId(data.handle); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 809 | |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 810 | ChromeViewHostMsg_LaunchNaCl::WriteReplyParams( |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 811 | reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id); |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 812 | chrome_render_message_filter_->Send(reply_msg_); |
| 813 | chrome_render_message_filter_ = NULL; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 814 | reply_msg_ = NULL; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 815 | internal_->sockets_for_renderer.clear(); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 816 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 817 | std::vector<nacl::FileDescriptor> handles_for_sel_ldr; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 818 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 819 | if (!SendHandleToSelLdr(data.handle, |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 820 | internal_->sockets_for_sel_ldr[i], true, |
| 821 | &handles_for_sel_ldr)) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 822 | delete this; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 823 | return; |
| 824 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Send over the IRT file handle. We don't close our own copy! |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 828 | if (!SendHandleToSelLdr(data.handle, irt_file, false, &handles_for_sel_ldr)) { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 829 | delete this; |
| 830 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 831 | } |
| 832 | |
[email protected] | ab88d154 | 2011-11-18 22:52:00 | [diff] [blame] | 833 | #if defined(OS_MACOSX) |
| 834 | // For dynamic loading support, NaCl requires a file descriptor that |
| 835 | // was created in /tmp, since those created with shm_open() are not |
| 836 | // mappable with PROT_EXEC. Rather than requiring an extra IPC |
| 837 | // round trip out of the sandbox, we create an FD here. |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 838 | base::SharedMemory memory_buffer; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 839 | base::SharedMemoryCreateOptions options; |
| 840 | options.size = 1; |
| 841 | options.executable = true; |
| 842 | if (!memory_buffer.Create(options)) { |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 843 | LOG(ERROR) << "Failed to allocate memory buffer"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 844 | delete this; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 845 | return; |
| 846 | } |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 847 | nacl::FileDescriptor memory_fd; |
| 848 | memory_fd.fd = dup(memory_buffer.handle().fd); |
| 849 | if (memory_fd.fd < 0) { |
| 850 | LOG(ERROR) << "Failed to dup() a file descriptor"; |
| 851 | delete this; |
| 852 | return; |
| 853 | } |
| 854 | memory_fd.auto_close = true; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 855 | handles_for_sel_ldr.push_back(memory_fd); |
| 856 | #endif |
| 857 | |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 858 | // Sending the version string over IPC avoids linkage issues in cases where |
| 859 | // NaCl is not compiled into the main Chromium executable or DLL. |
| 860 | chrome::VersionInfo version_info; |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 861 | IPC::Message* start_message = |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame^] | 862 | new NaClProcessMsg_Start(handles_for_sel_ldr, |
| 863 | nacl_browser->GetValidatorCacheKey(), |
| 864 | version_info.CreateVersionString(), |
| 865 | enable_exception_handling_); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 866 | #if defined(OS_WIN) |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 867 | if (debug_context_ != NULL) { |
| 868 | debug_context_->SetStartMessage(start_message); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 869 | debug_context_->SendStartMessage(); |
| 870 | } else { |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 871 | process_->Send(start_message); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 872 | } |
| 873 | #else |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 874 | process_->Send(start_message); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 875 | #endif |
| 876 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 877 | internal_->sockets_for_sel_ldr.clear(); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 878 | } |
| 879 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 880 | bool NaClBrowser::QueryKnownToValidate(const std::string& signature) { |
[email protected] | 0ae52b4 | 2012-03-27 02:53:59 | [diff] [blame] | 881 | bool result = false; |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 882 | ValidationCacheType::iterator iter = validation_cache_.Get(signature); |
[email protected] | 0ae52b4 | 2012-03-27 02:53:59 | [diff] [blame] | 883 | if (iter != validation_cache_.end()) { |
| 884 | result = iter->second; |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 885 | } |
[email protected] | 0ae52b4 | 2012-03-27 02:53:59 | [diff] [blame] | 886 | UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Query", |
| 887 | result ? 1 : 0, 2); |
| 888 | return result; |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void NaClBrowser::SetKnownToValidate(const std::string& signature) { |
| 892 | validation_cache_.Put(signature, true); |
[email protected] | 0ae52b4 | 2012-03-27 02:53:59 | [diff] [blame] | 893 | // The number of sets should be equal to the number of cache misses, minus |
| 894 | // validation failures and successful validations where stubout occurs. |
| 895 | // Bucket zero is reserved for future use. |
| 896 | UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Set", 1, 2); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature, |
| 900 | bool* result) { |
| 901 | *result = NaClBrowser::GetInstance()->QueryKnownToValidate(signature); |
| 902 | } |
| 903 | |
| 904 | void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) { |
| 905 | NaClBrowser::GetInstance()->SetKnownToValidate(signature); |
| 906 | } |
| 907 | |
| 908 | // Needed to handle sync messages in OnMessageRecieved. |
| 909 | bool NaClProcessHost::Send(IPC::Message* msg) { |
| 910 | return process_->Send(msg); |
| 911 | } |
| 912 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 913 | bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 914 | bool handled = true; |
| 915 | IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg) |
| 916 | IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate, |
| 917 | OnQueryKnownToValidate) |
| 918 | IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate, |
| 919 | OnSetKnownToValidate) |
| 920 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 921 | IPC_END_MESSAGE_MAP() |
| 922 | return handled; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 923 | } |