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
|
/* -*- 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 "mozilla/ipc/SharedMemoryHandle.h"
#include "mozilla/ipc/SharedMemoryMapping.h"
#include "SharedMemoryPlatform.h"
#include "mozilla/Atomics.h"
#include "mozilla/CheckedInt.h"
#include "nsIMemoryReporter.h"
#ifdef FUZZING
# include "mozilla/ipc/SharedMemoryFuzzer.h"
#endif
namespace mozilla::ipc::shared_memory {
class MappingReporter final : public nsIMemoryReporter {
~MappingReporter() = default;
public:
static Atomic<size_t> mapped;
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
bool aAnonymize) override {
MOZ_COLLECT_REPORT(
"shmem-mapped", KIND_OTHER, UNITS_BYTES, mapped,
"Memory shared with other processes that is mapped into the address "
"space.");
return NS_OK;
}
};
Atomic<size_t> MappingReporter::mapped;
NS_IMPL_ISUPPORTS(MappingReporter, nsIMemoryReporter)
static void RegisterMappingMemoryReporter() {
static Atomic<bool> registered;
if (registered.compareExchange(false, true)) {
RegisterStrongMemoryReporter(new MappingReporter());
}
}
MappingBase::MappingBase() = default;
MappingBase& MappingBase::operator=(MappingBase&& aOther) {
// Swap members with `aOther`, and unmap that mapping.
std::swap(aOther.mMemory, mMemory);
std::swap(aOther.mSize, mSize);
aOther.Unmap();
return *this;
}
void* MappingBase::Address() const {
#ifdef FUZZING
return SharedMemoryFuzzer::MutateSharedMemory(mMemory, mSize);
#else
return mMemory;
#endif
}
bool MappingBase::Map(const HandleBase& aHandle, void* aFixedAddress,
bool aReadOnly) {
// Invalid handles will fail and result in an invalid mapping.
if (!aHandle) {
return false;
}
// Verify that the handle size can be stored as a mapping size first
// (otherwise it won't be possible to map in the address space and the Map
// call will fail).
CheckedInt<size_t> checkedSize(aHandle.Size());
if (!checkedSize.isValid()) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Error,
"handle size to map exceeds address space size");
return false;
}
return MapSubregion(aHandle, /* aOffset */ 0, checkedSize.value(),
aFixedAddress, aReadOnly);
}
bool MappingBase::MapSubregion(const HandleBase& aHandle, uint64_t aOffset,
size_t aSize, void* aFixedAddress,
bool aReadOnly) {
CheckedInt<uint64_t> endOffset(aOffset);
endOffset += aSize;
if (!endOffset.isValid() || endOffset.value() > aHandle.Size()) {
MOZ_LOG_FMT(gSharedMemoryLog, LogLevel::Error,
"cannot map region exceeding aHandle.Size()");
return false;
}
RegisterMappingMemoryReporter();
if (auto mem =
Platform::Map(aHandle, aOffset, aSize, aFixedAddress, aReadOnly)) {
mMemory = *mem;
mSize = aSize;
MappingReporter::mapped += mSize;
return true;
}
return false;
}
void MappingBase::Unmap() {
if (IsValid()) {
Platform::Unmap(mMemory, mSize);
MOZ_ASSERT(MappingReporter::mapped >= mSize,
"Can't unmap more than mapped");
MappingReporter::mapped -= mSize;
}
mMemory = nullptr;
mSize = 0;
}
std::tuple<void*, size_t> MappingBase::Release() && {
// NOTE: this doesn't reduce gShmemMapped since it _is_ still mapped memory
// (and will be until the process terminates).
return std::make_tuple(std::exchange(mMemory, nullptr),
std::exchange(mSize, 0));
}
MutableOrReadOnlyMapping::Mapping(const MutableHandle& aHandle,
void* aFixedAddress)
: mReadOnly(false) {
Map(aHandle, aFixedAddress, false);
}
MutableOrReadOnlyMapping::Mapping(const ReadOnlyHandle& aHandle,
void* aFixedAddress)
: mReadOnly(true) {
Map(aHandle, aFixedAddress, true);
}
MutableOrReadOnlyMapping::Mapping(MutableMapping&& aMapping)
: MappingData(std::move(aMapping)), mReadOnly(false) {}
MutableOrReadOnlyMapping::Mapping(ReadOnlyMapping&& aMapping)
: MappingData(std::move(aMapping)), mReadOnly(true) {}
// We still store the handle if `Map` fails: the user may want to get it back
// (for instance, if fixed-address mapping doesn't work they may try mapping
// without one).
FreezableMapping::Mapping(FreezableHandle&& aHandle, void* aFixedAddress)
: mHandle(std::move(aHandle)) {
Map(mHandle, aFixedAddress, false);
}
FreezableMapping::Mapping(FreezableHandle&& aHandle, uint64_t aOffset,
size_t aSize, void* aFixedAddress)
: mHandle(std::move(aHandle)) {
MapSubregion(mHandle, aOffset, aSize, aFixedAddress, false);
}
ReadOnlyHandle FreezableMapping::Freeze() && {
return std::move(*this).Unmap().Freeze();
}
std::tuple<ReadOnlyHandle, MutableMapping>
FreezableMapping::FreezeWithMutableMapping() && {
auto handle = std::move(mHandle);
return std::make_tuple(std::move(handle).Freeze(),
ConvertMappingTo<Type::Mutable>(std::move(*this)));
}
FreezableHandle FreezableMapping::Unmap() && {
auto handle = std::move(mHandle);
*this = nullptr;
return handle;
}
bool LocalProtect(char* aAddr, size_t aSize, Access aAccess) {
return Platform::Protect(aAddr, aSize, aAccess);
}
void* FindFreeAddressSpace(size_t aSize) {
return Platform::FindFreeAddressSpace(aSize);
}
size_t SystemPageSize() { return Platform::PageSize(); }
size_t SystemAllocationGranularity() {
return Platform::AllocationGranularity();
}
size_t PageAlignedSize(size_t aMinimum) {
const size_t pageSize = Platform::PageSize();
size_t nPagesNeeded = size_t(ceil(double(aMinimum) / double(pageSize)));
return pageSize * nPagesNeeded;
}
} // namespace mozilla::ipc::shared_memory
|