| 1 | /* |
| 2 | * Copyright (C) 2017-2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "Algorithm.h" |
| 29 | #include "BAssert.h" |
| 30 | #include "BExport.h" |
| 31 | #include "BInline.h" |
| 32 | #include "BPlatform.h" |
| 33 | #include "Sizes.h" |
| 34 | #include <cstddef> |
| 35 | #include <inttypes.h> |
| 36 | |
| 37 | #if ((BOS(DARWIN) || BOS(LINUX)) && \ |
| 38 | (BCPU(X86_64) || (BCPU(ARM64) && !defined(__ILP32__) && (!BPLATFORM(IOS_FAMILY) || BPLATFORM(IOS))))) |
| 39 | #define GIGACAGE_ENABLED 1 |
| 40 | #else |
| 41 | #define GIGACAGE_ENABLED 0 |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | namespace Gigacage { |
| 46 | |
| 47 | enum Kind { |
| 48 | ReservedForFlagsAndNotABasePtr = 0, |
| 49 | Primitive, |
| 50 | JSValue, |
| 51 | }; |
| 52 | |
| 53 | BINLINE const char* name(Kind kind) |
| 54 | { |
| 55 | switch (kind) { |
| 56 | case ReservedForFlagsAndNotABasePtr: |
| 57 | RELEASE_BASSERT_NOT_REACHED(); |
| 58 | case Primitive: |
| 59 | return "Primitive" ; |
| 60 | case JSValue: |
| 61 | return "JSValue" ; |
| 62 | } |
| 63 | BCRASH(); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | #if GIGACAGE_ENABLED |
| 68 | |
| 69 | #if BCPU(ARM64) |
| 70 | constexpr size_t primitiveGigacageSize = 2 * bmalloc::Sizes::GB; |
| 71 | constexpr size_t jsValueGigacageSize = 1 * bmalloc::Sizes::GB; |
| 72 | constexpr size_t gigacageBasePtrsSize = 16 * bmalloc::Sizes::kB; |
| 73 | constexpr size_t maximumCageSizeReductionForSlide = bmalloc::Sizes::GB / 2; |
| 74 | #define GIGACAGE_ALLOCATION_CAN_FAIL 1 |
| 75 | #else |
| 76 | constexpr size_t primitiveGigacageSize = 32 * bmalloc::Sizes::GB; |
| 77 | constexpr size_t jsValueGigacageSize = 16 * bmalloc::Sizes::GB; |
| 78 | constexpr size_t = 4 * bmalloc::Sizes::kB; |
| 79 | constexpr size_t maximumCageSizeReductionForSlide = 4 * bmalloc::Sizes::GB; |
| 80 | #define GIGACAGE_ALLOCATION_CAN_FAIL 0 |
| 81 | #endif |
| 82 | |
| 83 | // In Linux, if `vm.overcommit_memory = 2` is specified, mmap with large size can fail if it exceeds the size of RAM. |
| 84 | // So we specify GIGACAGE_ALLOCATION_CAN_FAIL = 1. |
| 85 | #if BOS(LINUX) |
| 86 | #undef GIGACAGE_ALLOCATION_CAN_FAIL |
| 87 | #define GIGACAGE_ALLOCATION_CAN_FAIL 1 |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | static_assert(bmalloc::isPowerOfTwo(primitiveGigacageSize), "" ); |
| 92 | static_assert(bmalloc::isPowerOfTwo(jsValueGigacageSize), "" ); |
| 93 | static_assert(primitiveGigacageSize > maximumCageSizeReductionForSlide, "" ); |
| 94 | static_assert(jsValueGigacageSize > maximumCageSizeReductionForSlide, "" ); |
| 95 | |
| 96 | constexpr size_t gigacageSizeToMask(size_t size) { return size - 1; } |
| 97 | |
| 98 | constexpr size_t primitiveGigacageMask = gigacageSizeToMask(primitiveGigacageSize); |
| 99 | constexpr size_t jsValueGigacageMask = gigacageSizeToMask(jsValueGigacageSize); |
| 100 | |
| 101 | extern "C" alignas(gigacageBasePtrsSize) BEXPORT char g_gigacageBasePtrs[gigacageBasePtrsSize]; |
| 102 | |
| 103 | BINLINE bool wasEnabled() { return g_gigacageBasePtrs[0]; } |
| 104 | BINLINE void setWasEnabled() { g_gigacageBasePtrs[0] = true; } |
| 105 | |
| 106 | struct BasePtrs { |
| 107 | uintptr_t reservedForFlags; |
| 108 | void* primitive; |
| 109 | void* jsValue; |
| 110 | }; |
| 111 | |
| 112 | static_assert(offsetof(BasePtrs, primitive) == Kind::Primitive * sizeof(void*), "" ); |
| 113 | static_assert(offsetof(BasePtrs, jsValue) == Kind::JSValue * sizeof(void*), "" ); |
| 114 | |
| 115 | constexpr unsigned numKinds = 2; |
| 116 | |
| 117 | BEXPORT void ensureGigacage(); |
| 118 | |
| 119 | BEXPORT void disablePrimitiveGigacage(); |
| 120 | |
| 121 | // This will call the disable callback immediately if the Primitive Gigacage is currently disabled. |
| 122 | BEXPORT void addPrimitiveDisableCallback(void (*)(void*), void*); |
| 123 | BEXPORT void removePrimitiveDisableCallback(void (*)(void*), void*); |
| 124 | |
| 125 | BEXPORT void disableDisablingPrimitiveGigacageIfShouldBeEnabled(); |
| 126 | |
| 127 | BEXPORT bool isDisablingPrimitiveGigacageDisabled(); |
| 128 | inline bool isPrimitiveGigacagePermanentlyEnabled() { return isDisablingPrimitiveGigacageDisabled(); } |
| 129 | inline bool canPrimitiveGigacageBeDisabled() { return !isDisablingPrimitiveGigacageDisabled(); } |
| 130 | |
| 131 | BINLINE void*& basePtr(BasePtrs& basePtrs, Kind kind) |
| 132 | { |
| 133 | switch (kind) { |
| 134 | case ReservedForFlagsAndNotABasePtr: |
| 135 | RELEASE_BASSERT_NOT_REACHED(); |
| 136 | case Primitive: |
| 137 | return basePtrs.primitive; |
| 138 | case JSValue: |
| 139 | return basePtrs.jsValue; |
| 140 | } |
| 141 | BCRASH(); |
| 142 | return basePtrs.primitive; |
| 143 | } |
| 144 | |
| 145 | BINLINE BasePtrs& basePtrs() |
| 146 | { |
| 147 | return *reinterpret_cast<BasePtrs*>(reinterpret_cast<void*>(g_gigacageBasePtrs)); |
| 148 | } |
| 149 | |
| 150 | BINLINE void*& basePtr(Kind kind) |
| 151 | { |
| 152 | return basePtr(basePtrs(), kind); |
| 153 | } |
| 154 | |
| 155 | BINLINE bool isEnabled(Kind kind) |
| 156 | { |
| 157 | return !!basePtr(kind); |
| 158 | } |
| 159 | |
| 160 | BINLINE size_t size(Kind kind) |
| 161 | { |
| 162 | switch (kind) { |
| 163 | case ReservedForFlagsAndNotABasePtr: |
| 164 | RELEASE_BASSERT_NOT_REACHED(); |
| 165 | case Primitive: |
| 166 | return static_cast<size_t>(primitiveGigacageSize); |
| 167 | case JSValue: |
| 168 | return static_cast<size_t>(jsValueGigacageSize); |
| 169 | } |
| 170 | BCRASH(); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | BINLINE size_t alignment(Kind kind) |
| 175 | { |
| 176 | return size(kind); |
| 177 | } |
| 178 | |
| 179 | BINLINE size_t mask(Kind kind) |
| 180 | { |
| 181 | return gigacageSizeToMask(size(kind)); |
| 182 | } |
| 183 | |
| 184 | template<typename Func> |
| 185 | void forEachKind(const Func& func) |
| 186 | { |
| 187 | func(Primitive); |
| 188 | func(JSValue); |
| 189 | } |
| 190 | |
| 191 | template<typename T> |
| 192 | BINLINE T* caged(Kind kind, T* ptr) |
| 193 | { |
| 194 | BASSERT(ptr); |
| 195 | void* gigacageBasePtr = basePtr(kind); |
| 196 | if (!gigacageBasePtr) |
| 197 | return ptr; |
| 198 | return reinterpret_cast<T*>( |
| 199 | reinterpret_cast<uintptr_t>(gigacageBasePtr) + ( |
| 200 | reinterpret_cast<uintptr_t>(ptr) & mask(kind))); |
| 201 | } |
| 202 | |
| 203 | template<typename T> |
| 204 | BINLINE T* cagedMayBeNull(Kind kind, T* ptr) |
| 205 | { |
| 206 | if (!ptr) |
| 207 | return ptr; |
| 208 | return caged(kind, ptr); |
| 209 | } |
| 210 | |
| 211 | BINLINE bool isCaged(Kind kind, const void* ptr) |
| 212 | { |
| 213 | return caged(kind, ptr) == ptr; |
| 214 | } |
| 215 | |
| 216 | BEXPORT bool shouldBeEnabled(); |
| 217 | |
| 218 | #else // GIGACAGE_ENABLED |
| 219 | |
| 220 | BINLINE void*& basePtr(Kind) |
| 221 | { |
| 222 | BCRASH(); |
| 223 | static void* unreachable; |
| 224 | return unreachable; |
| 225 | } |
| 226 | BINLINE size_t size(Kind) { BCRASH(); return 0; } |
| 227 | BINLINE void ensureGigacage() { } |
| 228 | BINLINE bool wasEnabled() { return false; } |
| 229 | BINLINE bool isCaged(Kind, const void*) { return true; } |
| 230 | BINLINE bool isEnabled(Kind) { return false; } |
| 231 | template<typename T> BINLINE T* caged(Kind, T* ptr) { return ptr; } |
| 232 | template<typename T> BINLINE T* cagedMayBeNull(Kind, T* ptr) { return ptr; } |
| 233 | BINLINE void disableDisablingPrimitiveGigacageIfShouldBeEnabled() { } |
| 234 | BINLINE bool canPrimitiveGigacageBeDisabled() { return false; } |
| 235 | BINLINE void disablePrimitiveGigacage() { } |
| 236 | BINLINE void addPrimitiveDisableCallback(void (*)(void*), void*) { } |
| 237 | BINLINE void removePrimitiveDisableCallback(void (*)(void*), void*) { } |
| 238 | |
| 239 | #endif // GIGACAGE_ENABLED |
| 240 | |
| 241 | } // namespace Gigacage |
| 242 | |
| 243 | |
| 244 | |
| 245 | |