1 | /* |
2 | * Copyright (C) 2017 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 | #include "config.h" |
27 | #include <wtf/Gigacage.h> |
28 | |
29 | #include <wtf/Atomics.h> |
30 | #include <wtf/PageBlock.h> |
31 | #include <wtf/OSAllocator.h> |
32 | |
33 | #if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC |
34 | |
35 | namespace Gigacage { |
36 | |
37 | alignas(void*) char g_gigacageBasePtrs[gigacageBasePtrsSize]; |
38 | |
39 | void* tryMalloc(Kind, size_t size) |
40 | { |
41 | return FastMalloc::tryMalloc(size); |
42 | } |
43 | |
44 | void* tryRealloc(Kind, void* pointer, size_t size) |
45 | { |
46 | return FastMalloc::tryRealloc(pointer, size); |
47 | } |
48 | |
49 | void* tryAllocateZeroedVirtualPages(Kind, size_t requestedSize) |
50 | { |
51 | size_t size = roundUpToMultipleOf(WTF::pageSize(), requestedSize); |
52 | RELEASE_ASSERT(size >= requestedSize); |
53 | void* result = OSAllocator::reserveAndCommit(size); |
54 | #if !ASSERT_DISABLED |
55 | if (result) { |
56 | for (size_t i = 0; i < size / sizeof(uintptr_t); ++i) |
57 | ASSERT(static_cast<uintptr_t*>(result)[i] == 0); |
58 | } |
59 | #endif |
60 | return result; |
61 | } |
62 | |
63 | void freeVirtualPages(Kind, void* basePtr, size_t size) |
64 | { |
65 | OSAllocator::decommitAndRelease(basePtr, size); |
66 | } |
67 | |
68 | } // namespace Gigacage |
69 | #else // defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC |
70 | #include <bmalloc/bmalloc.h> |
71 | |
72 | namespace Gigacage { |
73 | |
74 | // FIXME: Pointers into the primitive gigacage must be scrambled right after being returned from malloc, |
75 | // and stay scrambled except just before use. |
76 | // https://bugs.webkit.org/show_bug.cgi?id=175035 |
77 | |
78 | void* tryAlignedMalloc(Kind kind, size_t alignment, size_t size) |
79 | { |
80 | void* result = bmalloc::api::tryMemalign(alignment, size, bmalloc::heapKind(kind)); |
81 | WTF::compilerFence(); |
82 | return result; |
83 | } |
84 | |
85 | void alignedFree(Kind kind, void* p) |
86 | { |
87 | if (!p) |
88 | return; |
89 | RELEASE_ASSERT(isCaged(kind, p)); |
90 | bmalloc::api::free(p, bmalloc::heapKind(kind)); |
91 | WTF::compilerFence(); |
92 | } |
93 | |
94 | void* tryMalloc(Kind kind, size_t size) |
95 | { |
96 | void* result = bmalloc::api::tryMalloc(size, bmalloc::heapKind(kind)); |
97 | WTF::compilerFence(); |
98 | return result; |
99 | } |
100 | |
101 | void* tryRealloc(Kind kind, void* pointer, size_t size) |
102 | { |
103 | void* result = bmalloc::api::tryRealloc(pointer, size, bmalloc::heapKind(kind)); |
104 | WTF::compilerFence(); |
105 | return result; |
106 | } |
107 | |
108 | void free(Kind kind, void* p) |
109 | { |
110 | if (!p) |
111 | return; |
112 | RELEASE_ASSERT(isCaged(kind, p)); |
113 | bmalloc::api::free(p, bmalloc::heapKind(kind)); |
114 | WTF::compilerFence(); |
115 | } |
116 | |
117 | void* tryAllocateZeroedVirtualPages(Kind kind, size_t size) |
118 | { |
119 | void* result = bmalloc::api::tryLargeZeroedMemalignVirtual(WTF::pageSize(), size, bmalloc::heapKind(kind)); |
120 | WTF::compilerFence(); |
121 | return result; |
122 | } |
123 | |
124 | void freeVirtualPages(Kind kind, void* basePtr, size_t size) |
125 | { |
126 | if (!basePtr) |
127 | return; |
128 | RELEASE_ASSERT(isCaged(kind, basePtr)); |
129 | bmalloc::api::freeLargeVirtual(basePtr, size, bmalloc::heapKind(kind)); |
130 | WTF::compilerFence(); |
131 | } |
132 | |
133 | } // namespace Gigacage |
134 | #endif |
135 | |
136 | namespace Gigacage { |
137 | |
138 | void* tryMallocArray(Kind kind, size_t numElements, size_t elementSize) |
139 | { |
140 | Checked<size_t, RecordOverflow> checkedSize = elementSize; |
141 | checkedSize *= numElements; |
142 | if (checkedSize.hasOverflowed()) |
143 | return nullptr; |
144 | return tryMalloc(kind, checkedSize.unsafeGet()); |
145 | } |
146 | |
147 | void* malloc(Kind kind, size_t size) |
148 | { |
149 | void* result = tryMalloc(kind, size); |
150 | RELEASE_ASSERT(result); |
151 | return result; |
152 | } |
153 | |
154 | void* mallocArray(Kind kind, size_t numElements, size_t elementSize) |
155 | { |
156 | void* result = tryMallocArray(kind, numElements, elementSize); |
157 | RELEASE_ASSERT(result); |
158 | return result; |
159 | } |
160 | |
161 | } // namespace Gigacage |
162 | |
163 | |