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