| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 | * Copyright (c) 2010 University of Szeged |
| 4 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #if USE(UNIX_DOMAIN_SOCKETS) |
| 30 | #include "SharedMemory.h" |
| 31 | |
| 32 | #include "Decoder.h" |
| 33 | #include "Encoder.h" |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | #include <wtf/Assertions.h> |
| 42 | #include <wtf/RandomNumber.h> |
| 43 | #include <wtf/UniStdExtras.h> |
| 44 | #include <wtf/text/CString.h> |
| 45 | #include <wtf/text/WTFString.h> |
| 46 | |
| 47 | #if HAVE(LINUX_MEMFD_H) |
| 48 | #include <linux/memfd.h> |
| 49 | #include <sys/syscall.h> |
| 50 | #endif |
| 51 | |
| 52 | namespace WebKit { |
| 53 | |
| 54 | SharedMemory::Handle::Handle() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | SharedMemory::Handle::~Handle() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | SharedMemory::Handle::Handle(Handle&&) = default; |
| 63 | SharedMemory::Handle& SharedMemory::Handle::operator=(Handle&& other) = default; |
| 64 | |
| 65 | void SharedMemory::Handle::clear() |
| 66 | { |
| 67 | m_attachment = IPC::Attachment(); |
| 68 | } |
| 69 | |
| 70 | bool SharedMemory::Handle::isNull() const |
| 71 | { |
| 72 | return m_attachment.fileDescriptor() == -1; |
| 73 | } |
| 74 | |
| 75 | void SharedMemory::Handle::encode(IPC::Encoder& encoder) const |
| 76 | { |
| 77 | encoder << releaseAttachment(); |
| 78 | } |
| 79 | |
| 80 | bool SharedMemory::Handle::decode(IPC::Decoder& decoder, Handle& handle) |
| 81 | { |
| 82 | ASSERT_ARG(handle, handle.isNull()); |
| 83 | |
| 84 | IPC::Attachment attachment; |
| 85 | if (!decoder.decode(attachment)) |
| 86 | return false; |
| 87 | |
| 88 | handle.adoptAttachment(WTFMove(attachment)); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | IPC::Attachment SharedMemory::Handle::releaseAttachment() const |
| 93 | { |
| 94 | return WTFMove(m_attachment); |
| 95 | } |
| 96 | |
| 97 | void SharedMemory::Handle::adoptAttachment(IPC::Attachment&& attachment) |
| 98 | { |
| 99 | ASSERT(isNull()); |
| 100 | |
| 101 | m_attachment = WTFMove(attachment); |
| 102 | } |
| 103 | |
| 104 | static inline int accessModeMMap(SharedMemory::Protection protection) |
| 105 | { |
| 106 | switch (protection) { |
| 107 | case SharedMemory::Protection::ReadOnly: |
| 108 | return PROT_READ; |
| 109 | case SharedMemory::Protection::ReadWrite: |
| 110 | return PROT_READ | PROT_WRITE; |
| 111 | } |
| 112 | |
| 113 | ASSERT_NOT_REACHED(); |
| 114 | return PROT_READ | PROT_WRITE; |
| 115 | } |
| 116 | |
| 117 | static int createSharedMemory() |
| 118 | { |
| 119 | int fileDescriptor = -1; |
| 120 | |
| 121 | #if HAVE(LINUX_MEMFD_H) |
| 122 | static bool isMemFdAvailable = true; |
| 123 | if (isMemFdAvailable) { |
| 124 | do { |
| 125 | fileDescriptor = syscall(__NR_memfd_create, "WebKitSharedMemory" , MFD_CLOEXEC); |
| 126 | } while (fileDescriptor == -1 && errno == EINTR); |
| 127 | |
| 128 | if (fileDescriptor != -1) |
| 129 | return fileDescriptor; |
| 130 | |
| 131 | if (errno != ENOSYS) |
| 132 | return fileDescriptor; |
| 133 | |
| 134 | isMemFdAvailable = false; |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | CString tempName; |
| 139 | for (int tries = 0; fileDescriptor == -1 && tries < 10; ++tries) { |
| 140 | String name = String("/WK2SharedMemory." ) + String::number(static_cast<unsigned>(WTF::randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0))); |
| 141 | tempName = name.utf8(); |
| 142 | |
| 143 | do { |
| 144 | fileDescriptor = shm_open(tempName.data(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| 145 | } while (fileDescriptor == -1 && errno == EINTR); |
| 146 | } |
| 147 | |
| 148 | if (fileDescriptor != -1) |
| 149 | shm_unlink(tempName.data()); |
| 150 | |
| 151 | return fileDescriptor; |
| 152 | } |
| 153 | |
| 154 | RefPtr<SharedMemory> SharedMemory::allocate(size_t size) |
| 155 | { |
| 156 | int fileDescriptor = createSharedMemory(); |
| 157 | if (fileDescriptor == -1) { |
| 158 | WTFLogAlways("Failed to create shared memory: %s" , strerror(errno)); |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | while (ftruncate(fileDescriptor, size) == -1) { |
| 163 | if (errno != EINTR) { |
| 164 | closeWithRetry(fileDescriptor); |
| 165 | return nullptr; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void* data = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileDescriptor, 0); |
| 170 | if (data == MAP_FAILED) { |
| 171 | closeWithRetry(fileDescriptor); |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | RefPtr<SharedMemory> instance = adoptRef(new SharedMemory()); |
| 176 | instance->m_data = data; |
| 177 | instance->m_fileDescriptor = fileDescriptor; |
| 178 | instance->m_size = size; |
| 179 | return instance; |
| 180 | } |
| 181 | |
| 182 | RefPtr<SharedMemory> SharedMemory::map(const Handle& handle, Protection protection) |
| 183 | { |
| 184 | ASSERT(!handle.isNull()); |
| 185 | |
| 186 | int fd = handle.m_attachment.releaseFileDescriptor(); |
| 187 | void* data = mmap(0, handle.m_attachment.size(), accessModeMMap(protection), MAP_SHARED, fd, 0); |
| 188 | closeWithRetry(fd); |
| 189 | if (data == MAP_FAILED) |
| 190 | return nullptr; |
| 191 | |
| 192 | RefPtr<SharedMemory> instance = wrapMap(data, handle.m_attachment.size(), -1); |
| 193 | instance->m_fileDescriptor = WTF::nullopt; |
| 194 | instance->m_isWrappingMap = false; |
| 195 | return instance; |
| 196 | } |
| 197 | |
| 198 | RefPtr<SharedMemory> SharedMemory::wrapMap(void* data, size_t size, int fileDescriptor) |
| 199 | { |
| 200 | RefPtr<SharedMemory> instance = adoptRef(new SharedMemory()); |
| 201 | instance->m_data = data; |
| 202 | instance->m_size = size; |
| 203 | instance->m_fileDescriptor = fileDescriptor; |
| 204 | instance->m_isWrappingMap = true; |
| 205 | return instance; |
| 206 | } |
| 207 | |
| 208 | SharedMemory::~SharedMemory() |
| 209 | { |
| 210 | if (m_isWrappingMap) |
| 211 | return; |
| 212 | |
| 213 | munmap(m_data, m_size); |
| 214 | if (m_fileDescriptor) |
| 215 | closeWithRetry(m_fileDescriptor.value()); |
| 216 | } |
| 217 | |
| 218 | bool SharedMemory::createHandle(Handle& handle, Protection) |
| 219 | { |
| 220 | ASSERT_ARG(handle, handle.isNull()); |
| 221 | ASSERT(m_fileDescriptor); |
| 222 | |
| 223 | // FIXME: Handle the case where the passed Protection is ReadOnly. |
| 224 | // See https://bugs.webkit.org/show_bug.cgi?id=131542. |
| 225 | |
| 226 | int duplicatedHandle = dupCloseOnExec(m_fileDescriptor.value()); |
| 227 | if (duplicatedHandle == -1) { |
| 228 | ASSERT_NOT_REACHED(); |
| 229 | return false; |
| 230 | } |
| 231 | handle.m_attachment = IPC::Attachment(duplicatedHandle, m_size); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | unsigned SharedMemory::systemPageSize() |
| 236 | { |
| 237 | static unsigned pageSize = 0; |
| 238 | |
| 239 | if (!pageSize) |
| 240 | pageSize = getpagesize(); |
| 241 | |
| 242 | return pageSize; |
| 243 | } |
| 244 | |
| 245 | } // namespace WebKit |
| 246 | |
| 247 | #endif |
| 248 | |
| 249 | |