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