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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Copyright (C) 2024-2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/Platform.h>
#include <wtf/StdLibExtras.h>
#if USE(PROTECTED_JIT)
#include <cstddef>
#include <cstdint>
#include <mach/mach_vm.h>
#include <mach/vm_map.h>
#include <mach/vm_param.h>
#include <pthread.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wtf/Assertions.h>
#include <wtf/Atomics.h>
#include <wtf/Compiler.h>
#include <wtf/DataLog.h>
#include <wtf/DoublyLinkedList.h>
#include <wtf/Lock.h>
#include <wtf/PageBlock.h>
#include <wtf/Threading.h>
#if OS(DARWIN)
#include <pthread/tsd_private.h>
#endif
namespace WTF {
struct GranuleHeader : public DoublyLinkedListNode<GranuleHeader> {
// non-inclusive of the page this is on
// so a value of 0 encodes 1 total pages
GranuleHeader* m_prev;
GranuleHeader* m_next;
size_t additionalPageCount;
};
using GranuleList = DoublyLinkedList<GranuleHeader>;
class ConcurrentDecommitQueue {
static constexpr bool verbose { false };
public:
void concatenate(GranuleList&& granules)
{
if (granules.isEmpty())
return;
{
Locker lock(m_decommitLock);
m_granules.append(granules);
granules.clear();
}
}
void decommit();
private:
GranuleList acquireExclusiveCopyOfGranuleList()
{
GranuleList granules { };
{
Locker lock(m_decommitLock);
granules = m_granules;
m_granules.clear();
}
return granules;
}
GranuleList m_granules { };
Lock m_decommitLock { };
};
// FIXME: a lot of this, but not all, can be de-duped with SequesteredArenaAllocator::Arena
class SequesteredImmortalAllocator {
constexpr static bool verbose { false };
constexpr static size_t minGranuleSize { 16 * KB };
constexpr static size_t minHeadAlignment { alignof(std::max_align_t) };
public:
SequesteredImmortalAllocator() = default;
SequesteredImmortalAllocator(SequesteredImmortalAllocator&& other) = delete;
SequesteredImmortalAllocator& operator=(SequesteredImmortalAllocator&& other) = delete;
SequesteredImmortalAllocator(const SequesteredImmortalAllocator&) = delete;
SequesteredImmortalAllocator& operator=(const SequesteredImmortalAllocator&) = delete;
void* allocate(size_t bytes)
{
void* retval { };
void* newAllocHead { };
{
Locker lock(m_lock);
retval = allocateImpl(bytes);
newAllocHead = reinterpret_cast<void*>(m_allocHead);
}
dataLogLnIf(verbose,
"SequesteredImmortalAllocator at ", RawPointer(this),
": allocated ", bytes, "B: alloc (", RawPointer(retval),
"), allocHead (", RawPointer(newAllocHead),
")");
return retval;
}
void* alignedAllocate(size_t alignment, size_t bytes)
{
void* retval { };
void* newAllocHead { };
{
Locker lock(m_lock);
retval = alignedAllocateImpl(alignment, bytes);
newAllocHead = reinterpret_cast<void*>(m_allocHead);
}
dataLogLnIf(verbose,
"SequesteredImmortalAllocator at ", RawPointer(this),
": align-allocated ", bytes, "B: alloc (", RawPointer(retval),
"), allocHead (", RawPointer(newAllocHead),
")");
return retval;
}
private:
uintptr_t headIncrementedBy(size_t bytes) const
{
constexpr size_t alignmentMask = minHeadAlignment - 1;
return (m_allocHead + bytes + alignmentMask) & ~alignmentMask;
}
void* allocateImpl(size_t bytes)
{
uintptr_t allocation = m_allocHead;
uintptr_t newHead = headIncrementedBy(bytes);
if (newHead < m_allocBound) [[likely]] {
m_allocHead = newHead;
return reinterpret_cast<void*>(allocation);
}
return allocateImplSlowPath(bytes);
}
void* alignedAllocateImpl(size_t alignment, size_t bytes)
{
uintptr_t allocation = WTF::roundUpToMultipleOf<minHeadAlignment>(m_allocHead);
uintptr_t newHead = headIncrementedBy((allocation - m_allocHead) + bytes);
if (newHead < m_allocBound) [[likely]] {
m_allocHead = newHead;
return reinterpret_cast<void*>(allocation);
}
return alignedAllocateImplSlowPath(alignment, bytes);
}
NEVER_INLINE void* allocateImplSlowPath(size_t bytes)
{
addGranule(bytes);
uintptr_t allocation = m_allocHead;
m_allocHead = headIncrementedBy(bytes);
ASSERT(m_allocHead <= m_allocBound);
return reinterpret_cast<void*>(allocation);
}
NEVER_INLINE void* alignedAllocateImplSlowPath(size_t alignment, size_t bytes)
{
addGranule(bytes);
alignment = std::max(alignment, minHeadAlignment);
uintptr_t allocation = WTF::roundUpToMultipleOf(alignment, m_allocHead);
m_allocHead = headIncrementedBy((allocation - m_allocHead) + bytes);
ASSERT(m_allocHead <= m_allocBound);
return reinterpret_cast<void*>(allocation);
}
GranuleHeader* addGranule(size_t minSize);
GranuleList m_granules { };
uintptr_t m_allocHead { 0 };
uintptr_t m_allocBound { 0 };
Lock m_lock { };
};
class alignas(16 * KB) SequesteredImmortalHeap {
friend class WTF::LazyNeverDestroyed<SequesteredImmortalHeap>;
static constexpr bool verbose { false };
static constexpr pthread_key_t key = __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0;
static constexpr size_t sequesteredImmortalHeapSlotSize { 16 * KB };
public:
static constexpr size_t slotSize { 128 };
static constexpr size_t numSlots { 64 };
enum class AllocationFailureMode {
Assert,
ReturnNull
};
static SequesteredImmortalHeap& instance();
template <typename T> requires (sizeof(T) <= slotSize)
T* allocateAndInstall()
{
T* slot = nullptr;
{
Locker locker { m_scavengerLock };
ASSERT(!getUnchecked());
// FIXME: implement resizing to a larger capacity
RELEASE_ASSERT(m_nextFreeIndex < numSlots);
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
void* buff = &(m_allocatorSlots[m_nextFreeIndex++]);
slot = new (buff) T();
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
}
_pthread_setspecific_direct(key, reinterpret_cast<void*>(slot));
pthread_key_init_np(key, nullptr);
dataLogIf(verbose, "SequesteredImmortalHeap: thread (", Thread::currentSingleton(), ") allocated slot ", instance().m_nextFreeIndex - 1, " (", slot, ")");
return slot;
}
void* immortalMalloc(size_t bytes)
{
return m_immortalAllocator.allocate(bytes);
}
void* immortalAlignedMalloc(size_t alignment, size_t bytes)
{
return m_immortalAllocator.alignedAllocate(alignment, bytes);
}
void* getSlot()
{
return getUnchecked();
}
int computeSlotIndex(void* slotPtr)
{
auto slot = reinterpret_cast<uintptr_t>(slotPtr);
auto arrayBase = reinterpret_cast<uintptr_t>(m_allocatorSlots.begin());
auto arrayBound = reinterpret_cast<uintptr_t>(m_allocatorSlots.begin()) + sizeof(m_allocatorSlots);
ASSERT_UNUSED(arrayBound, slot >= arrayBase && slot < arrayBound);
return static_cast<int>((slot - arrayBase) / slotSize);
}
static bool scavenge(void* userdata)
{
auto& sih = instance();
return sih.scavengeImpl(userdata);
}
template<AllocationFailureMode mode>
GranuleHeader* mapGranule(size_t bytes)
{
void* p = mmap(nullptr, bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (p == MAP_FAILED) [[unlikely]] {
if constexpr (mode == AllocationFailureMode::ReturnNull)
return nullptr;
RELEASE_ASSERT_NOT_REACHED();
}
RELEASE_ASSERT_DATA_ADDRESS_IS_SANE(p);
auto* gran = reinterpret_cast<GranuleHeader*>(p);
gran->additionalPageCount = bytes / pageSize() - 1;
return gran;
}
size_t decommitGranule(GranuleHeader* gran)
{
size_t pageCount = 1 + gran->additionalPageCount;
size_t bytes = pageCount * pageSize();
// FIXME: experiment with other decommit strategies
auto success = munmap(gran, bytes);
RELEASE_ASSERT(!success);
return pageCount;
}
private:
SequesteredImmortalHeap()
{
RELEASE_ASSERT(!(reinterpret_cast<uintptr_t>(this) % sequesteredImmortalHeapSlotSize));
static_assert(sizeof(*this) <= sequesteredImmortalHeapSlotSize);
auto flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | VM_FLAGS_PERMANENT;
auto prots = VM_PROT_READ | VM_PROT_WRITE;
auto* self = reinterpret_cast<mach_vm_address_t*>(this);
mach_vm_map(mach_task_self(), self, sequesteredImmortalHeapSlotSize, sequesteredImmortalHeapSlotSize - 1, flags, MEMORY_OBJECT_NULL, 0, false, prots, prots, VM_INHERIT_DEFAULT);
installScavenger();
// Cannot use dataLog here as it takes a lock
if constexpr (verbose)
SAFE_FPRINTF(stderr, "SequesteredImmortalHeap: initialized by thread (%u)\n", Thread::currentSingleton().uid());
}
void installScavenger();
bool scavengeImpl(void* userdata);
static void* getUnchecked()
{
return _pthread_getspecific_direct(key);
}
struct alignas(slotSize) Slot {
std::array<std::byte, slotSize> data;
};
Lock m_scavengerLock { };
size_t m_nextFreeIndex { };
SequesteredImmortalAllocator m_immortalAllocator { };
std::array<Slot, numSlots> m_allocatorSlots { };
};
}
#endif // USE(PROTECTED_JIT)
|