| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/nacl/nacl_launcher_thread.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/atomicops.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "chrome/common/nacl_messages.h" | |
| 12 #include "native_client/src/shared/imc/nacl_imc.h" | |
| 13 | |
| 14 #if defined(OS_LINUX) | |
| 15 #include "content/common/child_process_sandbox_support_linux.h" | |
| 16 #endif | |
| 17 | |
| 18 #if defined(OS_WIN) | |
| 19 #include <fcntl.h> | |
| 20 #include <io.h> | |
| 21 #endif | |
| 22 | |
| 23 #if defined(OS_MACOSX) | |
| 24 namespace { | |
| 25 | |
| 26 // On Mac OS X, shm_open() works in the sandbox but does not give us | |
| 27 // an FD that we can map as PROT_EXEC. Rather than doing an IPC to | |
| 28 // get an executable SHM region when CreateMemoryObject() is called, | |
| 29 // we preallocate one on startup, since NaCl's sel_ldr only needs one | |
| 30 // of them. This saves a round trip. | |
| 31 | |
| 32 base::subtle::Atomic32 g_shm_fd = -1; | |
| 33 | |
| 34 int CreateMemoryObject(size_t size, bool executable) { | |
| 35 if (executable && size > 0) { | |
| 36 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); | |
| 37 if (result_fd != -1) { | |
| 38 // ftruncate() is disallowed by the Mac OS X sandbox and | |
| 39 // returns EPERM. Luckily, we can get the same effect with | |
| 40 // lseek() + write(). | |
| 41 if (lseek(result_fd, size - 1, SEEK_SET) == -1) { | |
| 42 LOG(ERROR) << "lseek() failed: " << errno; | |
| 43 return -1; | |
| 44 } | |
| 45 if (write(result_fd, "", 1) != 1) { | |
| 46 LOG(ERROR) << "write() failed: " << errno; | |
| 47 return -1; | |
| 48 } | |
| 49 return result_fd; | |
| 50 } | |
| 51 } | |
| 52 // Fall back to NaCl's default implementation. | |
| 53 return -1; | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 #endif // defined(OS_MACOSX) | |
| 58 | |
| 59 // This is ugly. We need an interface header file for the exported | |
| 60 // sel_ldr interfaces. | |
| 61 // TODO(gregoryd,sehr): Add an interface header. | |
| 62 #if defined(OS_WIN) | |
| 63 typedef HANDLE NaClHandle; | |
| 64 #else | |
| 65 typedef int NaClHandle; | |
| 66 #endif // NaClHandle | |
| 67 | |
| 68 // This is currently necessary because we have a conflict between | |
| 69 // NaCl's LOG_FATAL (from platform/nacl_log.h) and Chromium's | |
| 70 // LOG_FATAL (from base/logging.h). | |
| 71 extern "C" int NaClMainForChromium(int handle_count, const NaClHandle* handles, | |
| 72 int debug); | |
| 73 extern "C" void NaClSetIrtFileDesc(int fd); | |
| 74 | |
| 75 NaClLauncherThread::NaClLauncherThread(bool debug) { | |
| 76 debug_enabled_ = debug ? 1 : 0; | |
| 77 } | |
| 78 | |
| 79 NaClLauncherThread::~NaClLauncherThread() { | |
| 80 } | |
| 81 | |
| 82 NaClLauncherThread* NaClLauncherThread::current() { | |
| 83 return static_cast<NaClLauncherThread*>(ChildThread::current()); | |
| 84 } | |
| 85 | |
| 86 bool NaClLauncherThread::OnControlMessageReceived(const IPC::Message& msg) { | |
| 87 bool handled = true; | |
| 88 IPC_BEGIN_MESSAGE_MAP(NaClLauncherThread, msg) | |
| 89 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) | |
| 90 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 91 IPC_END_MESSAGE_MAP() | |
| 92 return handled; | |
| 93 } | |
| 94 | |
| 95 void NaClLauncherThread::OnStartSelLdr( | |
| 96 std::vector<nacl::FileDescriptor> handles, | |
| 97 bool have_irt_file) { | |
| 98 #if defined(OS_LINUX) | |
| 99 nacl::SetCreateMemoryObjectFunc( | |
| 100 child_process_sandbox_support::MakeSharedMemorySegmentViaIPC); | |
| 101 #elif defined(OS_MACOSX) | |
| 102 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); | |
| 103 CHECK(handles.size() >= 1); | |
| 104 g_shm_fd = nacl::ToNativeHandle(handles[handles.size() - 1]); | |
| 105 handles.pop_back(); | |
| 106 #endif | |
| 107 | |
| 108 if (have_irt_file) { | |
| 109 CHECK(handles.size() >= 1); | |
| 110 NaClHandle irt_handle = nacl::ToNativeHandle(handles[handles.size() - 1]); | |
| 111 handles.pop_back(); | |
| 112 #if defined(OS_WIN) | |
| 113 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), | |
| 114 _O_RDWR | _O_BINARY); | |
| 115 if (irt_desc < 0) { | |
| 116 LOG(ERROR) << "_open_osfhandle() failed"; | |
| 117 return; | |
| 118 } | |
| 119 #else | |
| 120 int irt_desc = irt_handle; | |
| 121 #endif | |
| 122 NaClSetIrtFileDesc(irt_desc); | |
| 123 } | |
| 124 | |
| 125 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); | |
| 126 for (size_t i = 0; i < handles.size(); i++) { | |
| 127 array[i] = nacl::ToNativeHandle(handles[i]); | |
| 128 } | |
| 129 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), | |
| 130 debug_enabled_); | |
| 131 } | |
| OLD | NEW |