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
|
/* -*- 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/. */
/* This source code was derived from Chromium code, and as such is also subject
* to the [Chromium license](ipc/chromium/src/LICENSE). */
#include "SharedMemoryPlatform.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <android/sharedmem.h>
#ifdef MOZ_VALGRIND
# include <valgrind/valgrind.h>
#endif
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtrExtensions.h"
#include "prenv.h"
namespace mozilla::ipc::shared_memory {
// Right now we do nothing different for freezable shared memory on Android.
static Maybe<PlatformHandle> CreateImpl(size_t aSize, bool aFreezable) {
MOZ_ASSERT(aSize > 0);
int fd = ASharedMemory_create(nullptr, aSize);
if (fd < 0) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Warning, "failed to open shm: {}",
strerror(errno));
return Nothing();
}
return Some(fd);
}
bool AppendPosixShmPrefix(std::string* str, pid_t pid) { return false; }
bool UsingPosixShm() { return false; }
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) {
const int new_fd = dup(aHandle.get());
if (new_fd < 0) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Warning,
"failed to duplicate file descriptor: {}", strerror(errno));
return nullptr;
}
return mozilla::UniqueFileHandle(new_fd);
}
bool Platform::Freeze(FreezableHandle& aHandle) {
if (ASharedMemory_setProt(aHandle.mHandle.get(), PROT_READ) != 0) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Warning,
"failed to set sharedmem read-only: {}", strerror(errno));
return false;
}
return true;
}
Maybe<void*> Platform::Map(const HandleBase& aHandle, uint64_t aOffset,
size_t aSize, void* aFixedAddress, bool aReadOnly) {
// Don't use MAP_FIXED when a fixed_address was specified, since that can
// replace pages that are alread mapped at that address.
void* mem =
mmap(aFixedAddress, aSize, PROT_READ | (aReadOnly ? 0 : PROT_WRITE),
MAP_SHARED, aHandle.mHandle.get(), aOffset);
if (mem == MAP_FAILED) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Warning, "call to mmap failed: {}",
strerror(errno));
return Nothing();
}
if (aFixedAddress && mem != aFixedAddress) {
DebugOnly<bool> munmap_succeeded = munmap(mem, aSize) == 0;
MOZ_ASSERT(munmap_succeeded, "call to munmap failed");
return Nothing();
}
return Some(mem);
}
void Platform::Unmap(void* aMemory, size_t aSize) { munmap(aMemory, aSize); }
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) {
void* memory = mmap(nullptr, aSize, PROT_NONE,
MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE, -1, 0);
if (memory == MAP_FAILED) {
return nullptr;
}
munmap(memory, aSize);
return memory;
}
size_t Platform::PageSize() { return sysconf(_SC_PAGESIZE); }
size_t Platform::AllocationGranularity() { return PageSize(); }
bool Platform::IsSafeToMap(const PlatformHandle&) { return true; }
} // namespace mozilla::ipc::shared_memory
|