| 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 <wtf/FastMalloc.h> |
| 29 | #include <wtf/StdLibExtras.h> |
| 30 | |
| 31 | #if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC |
| 32 | #define GIGACAGE_ENABLED 0 |
| 33 | |
| 34 | namespace Gigacage { |
| 35 | |
| 36 | const size_t primitiveGigacageMask = 0; |
| 37 | const size_t jsValueGigacageMask = 0; |
| 38 | const size_t gigacageBasePtrsSize = 8 * KB; |
| 39 | |
| 40 | extern "C" alignas(void*) WTF_EXPORT_PRIVATE char g_gigacageBasePtrs[gigacageBasePtrsSize]; |
| 41 | |
| 42 | struct BasePtrs { |
| 43 | uintptr_t reservedForFlags; |
| 44 | void* primitive; |
| 45 | void* jsValue; |
| 46 | }; |
| 47 | |
| 48 | enum Kind { |
| 49 | ReservedForFlagsAndNotABasePtr = 0, |
| 50 | Primitive, |
| 51 | JSValue, |
| 52 | }; |
| 53 | |
| 54 | static_assert(offsetof(BasePtrs, primitive) == Kind::Primitive * sizeof(void*), "" ); |
| 55 | static_assert(offsetof(BasePtrs, jsValue) == Kind::JSValue * sizeof(void*), "" ); |
| 56 | |
| 57 | inline void ensureGigacage() { } |
| 58 | inline void disablePrimitiveGigacage() { } |
| 59 | inline bool shouldBeEnabled() { return false; } |
| 60 | |
| 61 | inline void addPrimitiveDisableCallback(void (*)(void*), void*) { } |
| 62 | inline void removePrimitiveDisableCallback(void (*)(void*), void*) { } |
| 63 | |
| 64 | inline void disableDisablingPrimitiveGigacageIfShouldBeEnabled() { } |
| 65 | |
| 66 | inline bool isDisablingPrimitiveGigacageDisabled() { return false; } |
| 67 | inline bool isPrimitiveGigacagePermanentlyEnabled() { return false; } |
| 68 | inline bool canPrimitiveGigacageBeDisabled() { return true; } |
| 69 | |
| 70 | ALWAYS_INLINE const char* name(Kind kind) |
| 71 | { |
| 72 | switch (kind) { |
| 73 | case ReservedForFlagsAndNotABasePtr: |
| 74 | RELEASE_ASSERT_NOT_REACHED(); |
| 75 | case Primitive: |
| 76 | return "Primitive" ; |
| 77 | case JSValue: |
| 78 | return "JSValue" ; |
| 79 | } |
| 80 | RELEASE_ASSERT_NOT_REACHED(); |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | ALWAYS_INLINE void*& basePtr(BasePtrs& basePtrs, Kind kind) |
| 85 | { |
| 86 | switch (kind) { |
| 87 | case ReservedForFlagsAndNotABasePtr: |
| 88 | RELEASE_ASSERT_NOT_REACHED(); |
| 89 | case Primitive: |
| 90 | return basePtrs.primitive; |
| 91 | case JSValue: |
| 92 | return basePtrs.jsValue; |
| 93 | } |
| 94 | RELEASE_ASSERT_NOT_REACHED(); |
| 95 | return basePtrs.primitive; |
| 96 | } |
| 97 | |
| 98 | ALWAYS_INLINE BasePtrs& basePtrs() |
| 99 | { |
| 100 | return *reinterpret_cast<BasePtrs*>(reinterpret_cast<void*>(g_gigacageBasePtrs)); |
| 101 | } |
| 102 | |
| 103 | ALWAYS_INLINE void*& basePtr(Kind kind) |
| 104 | { |
| 105 | return basePtr(basePtrs(), kind); |
| 106 | } |
| 107 | |
| 108 | ALWAYS_INLINE bool isEnabled(Kind kind) |
| 109 | { |
| 110 | return !!basePtr(kind); |
| 111 | } |
| 112 | |
| 113 | ALWAYS_INLINE size_t mask(Kind) { return 0; } |
| 114 | |
| 115 | template<typename T> |
| 116 | inline T* caged(Kind, T* ptr) { return ptr; } |
| 117 | template<typename T> |
| 118 | inline T* cagedMayBeNull(Kind, T* ptr) { return ptr; } |
| 119 | |
| 120 | inline bool isCaged(Kind, const void*) { return false; } |
| 121 | |
| 122 | inline void* tryAlignedMalloc(Kind, size_t alignment, size_t size) { return tryFastAlignedMalloc(alignment, size); } |
| 123 | inline void alignedFree(Kind, void* p) { fastAlignedFree(p); } |
| 124 | WTF_EXPORT_PRIVATE void* tryMalloc(Kind, size_t size); |
| 125 | WTF_EXPORT_PRIVATE void* tryRealloc(Kind, void*, size_t); |
| 126 | inline void free(Kind, void* p) { fastFree(p); } |
| 127 | |
| 128 | WTF_EXPORT_PRIVATE void* tryAllocateZeroedVirtualPages(Kind, size_t size); |
| 129 | WTF_EXPORT_PRIVATE void freeVirtualPages(Kind, void* basePtr, size_t size); |
| 130 | |
| 131 | } // namespace Gigacage |
| 132 | #else |
| 133 | #include <bmalloc/Gigacage.h> |
| 134 | |
| 135 | namespace Gigacage { |
| 136 | |
| 137 | WTF_EXPORT_PRIVATE void* tryAlignedMalloc(Kind, size_t alignment, size_t size); |
| 138 | WTF_EXPORT_PRIVATE void alignedFree(Kind, void*); |
| 139 | WTF_EXPORT_PRIVATE void* tryMalloc(Kind, size_t); |
| 140 | WTF_EXPORT_PRIVATE void* tryRealloc(Kind, void*, size_t); |
| 141 | WTF_EXPORT_PRIVATE void free(Kind, void*); |
| 142 | |
| 143 | WTF_EXPORT_PRIVATE void* tryAllocateZeroedVirtualPages(Kind, size_t size); |
| 144 | WTF_EXPORT_PRIVATE void freeVirtualPages(Kind, void* basePtr, size_t size); |
| 145 | |
| 146 | } // namespace Gigacage |
| 147 | #endif |
| 148 | |
| 149 | namespace Gigacage { |
| 150 | |
| 151 | WTF_EXPORT_PRIVATE void* tryMallocArray(Kind, size_t numElements, size_t elementSize); |
| 152 | |
| 153 | WTF_EXPORT_PRIVATE void* malloc(Kind, size_t); |
| 154 | WTF_EXPORT_PRIVATE void* mallocArray(Kind, size_t numElements, size_t elementSize); |
| 155 | |
| 156 | } // namespace Gigacage |
| 157 | |
| 158 | |
| 159 | |