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