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