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