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
45namespace Gigacage {
46
47enum Kind {
48 ReservedForFlagsAndNotABasePtr = 0,
49 Primitive,
50 JSValue,
51};
52
53BINLINE 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)
70constexpr size_t primitiveGigacageSize = 2 * bmalloc::Sizes::GB;
71constexpr size_t jsValueGigacageSize = 1 * bmalloc::Sizes::GB;
72constexpr size_t gigacageBasePtrsSize = 16 * bmalloc::Sizes::kB;
73constexpr size_t maximumCageSizeReductionForSlide = bmalloc::Sizes::GB / 2;
74#define GIGACAGE_ALLOCATION_CAN_FAIL 1
75#else
76constexpr size_t primitiveGigacageSize = 32 * bmalloc::Sizes::GB;
77constexpr size_t jsValueGigacageSize = 16 * bmalloc::Sizes::GB;
78constexpr size_t gigacageBasePtrsSize = 4 * bmalloc::Sizes::kB;
79constexpr 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
91static_assert(bmalloc::isPowerOfTwo(primitiveGigacageSize), "");
92static_assert(bmalloc::isPowerOfTwo(jsValueGigacageSize), "");
93static_assert(primitiveGigacageSize > maximumCageSizeReductionForSlide, "");
94static_assert(jsValueGigacageSize > maximumCageSizeReductionForSlide, "");
95
96constexpr size_t gigacageSizeToMask(size_t size) { return size - 1; }
97
98constexpr size_t primitiveGigacageMask = gigacageSizeToMask(primitiveGigacageSize);
99constexpr size_t jsValueGigacageMask = gigacageSizeToMask(jsValueGigacageSize);
100
101extern "C" alignas(gigacageBasePtrsSize) BEXPORT char g_gigacageBasePtrs[gigacageBasePtrsSize];
102
103BINLINE bool wasEnabled() { return g_gigacageBasePtrs[0]; }
104BINLINE void setWasEnabled() { g_gigacageBasePtrs[0] = true; }
105
106struct BasePtrs {
107 uintptr_t reservedForFlags;
108 void* primitive;
109 void* jsValue;
110};
111
112static_assert(offsetof(BasePtrs, primitive) == Kind::Primitive * sizeof(void*), "");
113static_assert(offsetof(BasePtrs, jsValue) == Kind::JSValue * sizeof(void*), "");
114
115constexpr unsigned numKinds = 2;
116
117BEXPORT void ensureGigacage();
118
119BEXPORT void disablePrimitiveGigacage();
120
121// This will call the disable callback immediately if the Primitive Gigacage is currently disabled.
122BEXPORT void addPrimitiveDisableCallback(void (*)(void*), void*);
123BEXPORT void removePrimitiveDisableCallback(void (*)(void*), void*);
124
125BEXPORT void disableDisablingPrimitiveGigacageIfShouldBeEnabled();
126
127BEXPORT bool isDisablingPrimitiveGigacageDisabled();
128inline bool isPrimitiveGigacagePermanentlyEnabled() { return isDisablingPrimitiveGigacageDisabled(); }
129inline bool canPrimitiveGigacageBeDisabled() { return !isDisablingPrimitiveGigacageDisabled(); }
130
131BINLINE 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
145BINLINE BasePtrs& basePtrs()
146{
147 return *reinterpret_cast<BasePtrs*>(reinterpret_cast<void*>(g_gigacageBasePtrs));
148}
149
150BINLINE void*& basePtr(Kind kind)
151{
152 return basePtr(basePtrs(), kind);
153}
154
155BINLINE bool isEnabled(Kind kind)
156{
157 return !!basePtr(kind);
158}
159
160BINLINE 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
174BINLINE size_t alignment(Kind kind)
175{
176 return size(kind);
177}
178
179BINLINE size_t mask(Kind kind)
180{
181 return gigacageSizeToMask(size(kind));
182}
183
184template<typename Func>
185void forEachKind(const Func& func)
186{
187 func(Primitive);
188 func(JSValue);
189}
190
191template<typename T>
192BINLINE 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
203template<typename T>
204BINLINE T* cagedMayBeNull(Kind kind, T* ptr)
205{
206 if (!ptr)
207 return ptr;
208 return caged(kind, ptr);
209}
210
211BINLINE bool isCaged(Kind kind, const void* ptr)
212{
213 return caged(kind, ptr) == ptr;
214}
215
216BEXPORT bool shouldBeEnabled();
217
218#else // GIGACAGE_ENABLED
219
220BINLINE void*& basePtr(Kind)
221{
222 BCRASH();
223 static void* unreachable;
224 return unreachable;
225}
226BINLINE size_t size(Kind) { BCRASH(); return 0; }
227BINLINE void ensureGigacage() { }
228BINLINE bool wasEnabled() { return false; }
229BINLINE bool isCaged(Kind, const void*) { return true; }
230BINLINE bool isEnabled(Kind) { return false; }
231template<typename T> BINLINE T* caged(Kind, T* ptr) { return ptr; }
232template<typename T> BINLINE T* cagedMayBeNull(Kind, T* ptr) { return ptr; }
233BINLINE void disableDisablingPrimitiveGigacageIfShouldBeEnabled() { }
234BINLINE bool canPrimitiveGigacageBeDisabled() { return false; }
235BINLINE void disablePrimitiveGigacage() { }
236BINLINE void addPrimitiveDisableCallback(void (*)(void*), void*) { }
237BINLINE void removePrimitiveDisableCallback(void (*)(void*), void*) { }
238
239#endif // GIGACAGE_ENABLED
240
241} // namespace Gigacage
242
243
244
245