1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SharedMemoryPlatform.h"
#include <utility>
#include <mach/vm_map.h>
#include <mach/mach_port.h>
#if defined(XP_IOS)
# include <mach/vm_map.h>
# define mach_vm_address_t vm_address_t
# define mach_vm_map vm_map
# define mach_vm_read vm_read
# define mach_vm_region_recurse vm_region_recurse_64
# define mach_vm_size_t vm_size_t
#else
# include <mach/mach_vm.h>
#endif
#include <pthread.h>
#include <sys/mman.h> // mprotect
#include <unistd.h>
#if defined(XP_MACOSX) && defined(__x86_64__)
# include "prenv.h"
#endif
#include <prtypes.h>
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Printf.h"
#include "nsDebug.h"
#ifdef DEBUG
# define LOG_ERROR(str, args...) \
PR_BEGIN_MACRO \
mozilla::SmprintfPointer msg = mozilla::Smprintf(str, ##args); \
NS_WARNING(msg.get()); \
PR_END_MACRO
#else
# define LOG_ERROR(str, args...) \
do { /* nothing */ \
} while (0)
#endif
namespace mozilla::ipc::shared_memory {
static inline void* toPointer(mach_vm_address_t aAddress) {
return reinterpret_cast<void*>(static_cast<uintptr_t>(aAddress));
}
static inline mach_vm_address_t toVMAddress(void* aPointer) {
return static_cast<mach_vm_address_t>(reinterpret_cast<uintptr_t>(aPointer));
}
static Maybe<PlatformHandle> CreateImpl(size_t aSize, bool aFreezable) {
memory_object_size_t memoryObjectSize = round_page(aSize);
PlatformHandle handle;
kern_return_t kr =
mach_make_memory_entry_64(mach_task_self(), &memoryObjectSize, 0,
MAP_MEM_NAMED_CREATE | VM_PROT_DEFAULT,
getter_Transfers(handle), MACH_PORT_NULL);
if (kr != KERN_SUCCESS || memoryObjectSize < round_page(aSize)) {
LOG_ERROR("Failed to make memory entry (%zu bytes). %s (%x)\n", aSize,
mach_error_string(kr), kr);
return Nothing();
}
return Some(std::move(handle));
}
bool Platform::Create(MutableHandle& aHandle, size_t aSize) {
if (auto ph = CreateImpl(aSize, false)) {
aHandle.mHandle = std::move(*ph);
aHandle.SetSize(aSize);
return true;
}
return false;
}
bool Platform::CreateFreezable(FreezableHandle& aHandle, size_t aSize) {
if (auto ph = CreateImpl(aSize, true)) {
aHandle.mHandle = std::move(*ph);
aHandle.SetSize(aSize);
return true;
}
return false;
}
PlatformHandle Platform::CloneHandle(const PlatformHandle& aHandle) {
return mozilla::RetainMachSendRight(aHandle.get());
}
static Maybe<void*> MapMemory(uint64_t aOffset, size_t aSize,
void* aFixedAddress,
const mozilla::UniqueMachSendRight& aPort,
bool aReadOnly) {
kern_return_t kr;
mach_vm_address_t address = toVMAddress(aFixedAddress);
vm_prot_t vmProtection = VM_PROT_READ;
if (!aReadOnly) {
vmProtection |= VM_PROT_WRITE;
}
kr = mach_vm_map(mach_task_self(), &address, round_page(aSize), 0,
aFixedAddress ? VM_FLAGS_FIXED : VM_FLAGS_ANYWHERE,
aPort.get(), aOffset, false, vmProtection, vmProtection,
VM_INHERIT_NONE);
if (kr != KERN_SUCCESS) {
if (!aFixedAddress) {
LOG_ERROR(
"Failed to map shared memory (%zu bytes) into %x, port %x. %s (%x)\n",
aSize, mach_task_self(), mach_port_t(aPort.get()),
mach_error_string(kr), kr);
}
return Nothing();
}
if (aFixedAddress && aFixedAddress != toPointer(address)) {
kr = vm_deallocate(mach_task_self(), address, aSize);
if (kr != KERN_SUCCESS) {
LOG_ERROR(
"Failed to unmap shared memory at unsuitable address "
"(%zu bytes) from %x, port %x. %s (%x)\n",
aSize, mach_task_self(), mach_port_t(aPort.get()),
mach_error_string(kr), kr);
}
return Nothing();
}
return Some(toPointer(address));
}
bool Platform::Freeze(FreezableHandle& aHandle) {
memory_object_size_t memoryObjectSize = round_page(aHandle.Size());
mozilla::UniqueMachSendRight port;
// Temporarily map memory (as readonly) to get an address.
auto memory = MapMemory(0, memoryObjectSize, nullptr, aHandle.mHandle, true);
if (!memory) {
return false;
}
kern_return_t kr = mach_make_memory_entry_64(
mach_task_self(), &memoryObjectSize,
static_cast<memory_object_offset_t>(reinterpret_cast<uintptr_t>(*memory)),
VM_PROT_READ, getter_Transfers(port), MACH_PORT_NULL);
// Immediately try to deallocate, regardless of success.
{
kern_return_t kr =
vm_deallocate(mach_task_self(), toVMAddress(*memory), memoryObjectSize);
if (kr != KERN_SUCCESS) {
LOG_ERROR("Failed to deallocate shared memory. %s (%x)\n",
mach_error_string(kr), kr);
}
}
if (kr != KERN_SUCCESS || memoryObjectSize < round_page(aHandle.Size())) {
LOG_ERROR("Failed to make memory entry (%llu bytes). %s (%x)\n",
aHandle.Size(), mach_error_string(kr), kr);
return false;
}
aHandle.mHandle = std::move(port);
return true;
}
Maybe<void*> Platform::Map(const HandleBase& aHandle, uint64_t aOffset,
size_t aSize, void* aFixedAddress, bool aReadOnly) {
return MapMemory(aOffset, aSize, aFixedAddress, aHandle.mHandle, aReadOnly);
}
void Platform::Unmap(void* aMemory, size_t aSize) {
vm_address_t vm_address = toVMAddress(aMemory);
kern_return_t kr =
vm_deallocate(mach_task_self(), vm_address, round_page(aSize));
if (kr != KERN_SUCCESS) {
LOG_ERROR("Failed to deallocate shared memory. %s (%x)\n",
mach_error_string(kr), kr);
}
}
bool Platform::Protect(char* aAddr, size_t aSize, Access aAccess) {
int flags = PROT_NONE;
if (aAccess & AccessRead) flags |= PROT_READ;
if (aAccess & AccessWrite) flags |= PROT_WRITE;
return 0 == mprotect(aAddr, aSize, flags);
}
void* Platform::FindFreeAddressSpace(size_t aSize) {
mach_vm_address_t address = 0;
aSize = round_page(aSize);
if (mach_vm_map(mach_task_self(), &address, aSize, 0, VM_FLAGS_ANYWHERE,
MEMORY_OBJECT_NULL, 0, false, VM_PROT_NONE, VM_PROT_NONE,
VM_INHERIT_NONE) != KERN_SUCCESS ||
vm_deallocate(mach_task_self(), address, aSize) != KERN_SUCCESS) {
return nullptr;
}
return toPointer(address);
}
size_t Platform::PageSize() {
#if defined(XP_MACOSX) && defined(__x86_64__)
static std::atomic<size_t> sPageSizeOverride = 0;
if (sPageSizeOverride == 0) {
if (PR_GetEnv("MOZ_SHMEM_PAGESIZE_16K")) {
sPageSizeOverride = 16 * 1024;
} else {
sPageSizeOverride = sysconf(_SC_PAGESIZE);
}
}
return sPageSizeOverride;
#else
return sysconf(_SC_PAGESIZE);
#endif
}
size_t Platform::AllocationGranularity() { return PageSize(); }
bool Platform::IsSafeToMap(const PlatformHandle&) { return true; }
} // namespace mozilla::ipc::shared_memory
|