]> git.saurik.com Git - apple/javascriptcore.git/blob - jit/ExecutableAllocatorFixedVMPool.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / jit / ExecutableAllocatorFixedVMPool.cpp
1 /*
2 * Copyright (C) 2009, 2015 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 "ExecutableAllocator.h"
28
29 #include "JSCInlines.h"
30
31 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
32
33 #include "CodeProfiling.h"
34 #include "ExecutableAllocationFuzz.h"
35 #include <errno.h>
36 #if !PLATFORM(WIN)
37 #include <unistd.h>
38 #endif
39 #include <wtf/MetaAllocator.h>
40 #include <wtf/PageReservation.h>
41 #include <wtf/VMTags.h>
42
43 #if OS(DARWIN)
44 #include <sys/mman.h>
45 #endif
46
47 #if OS(LINUX)
48 #include <stdio.h>
49 #endif
50
51 using namespace WTF;
52
53 namespace JSC {
54
55 uintptr_t startOfFixedExecutableMemoryPool;
56
57 class FixedVMPoolExecutableAllocator : public MetaAllocator {
58 WTF_MAKE_FAST_ALLOCATED;
59 public:
60 FixedVMPoolExecutableAllocator()
61 : MetaAllocator(jitAllocationGranule) // round up all allocations to 32 bytes
62 {
63 size_t reservationSize;
64 if (Options::jitMemoryReservationSize())
65 reservationSize = Options::jitMemoryReservationSize();
66 else
67 reservationSize = fixedExecutableMemoryPoolSize;
68 m_reservation = PageReservation::reserveWithGuardPages(reservationSize, OSAllocator::JSJITCodePages, EXECUTABLE_POOL_WRITABLE, true);
69 if (m_reservation) {
70 ASSERT(m_reservation.size() == reservationSize);
71 addFreshFreeSpace(m_reservation.base(), m_reservation.size());
72
73 startOfFixedExecutableMemoryPool = reinterpret_cast<uintptr_t>(m_reservation.base());
74 }
75 }
76
77 virtual ~FixedVMPoolExecutableAllocator();
78
79 protected:
80 virtual void* allocateNewSpace(size_t&) override
81 {
82 // We're operating in a fixed pool, so new allocation is always prohibited.
83 return 0;
84 }
85
86 virtual void notifyNeedPage(void* page) override
87 {
88 #if USE(MADV_FREE_FOR_JIT_MEMORY)
89 UNUSED_PARAM(page);
90 #else
91 m_reservation.commit(page, pageSize());
92 #endif
93 }
94
95 virtual void notifyPageIsFree(void* page) override
96 {
97 #if USE(MADV_FREE_FOR_JIT_MEMORY)
98 for (;;) {
99 int result = madvise(page, pageSize(), MADV_FREE);
100 if (!result)
101 return;
102 ASSERT(result == -1);
103 if (errno != EAGAIN) {
104 RELEASE_ASSERT_NOT_REACHED(); // In debug mode, this should be a hard failure.
105 break; // In release mode, we should just ignore the error - not returning memory to the OS is better than crashing, especially since we _will_ be able to reuse the memory internally anyway.
106 }
107 }
108 #else
109 m_reservation.decommit(page, pageSize());
110 #endif
111 }
112
113 private:
114 PageReservation m_reservation;
115 };
116
117 static FixedVMPoolExecutableAllocator* allocator;
118
119 void ExecutableAllocator::initializeAllocator()
120 {
121 ASSERT(!allocator);
122 allocator = new FixedVMPoolExecutableAllocator();
123 CodeProfiling::notifyAllocator(allocator);
124 }
125
126 ExecutableAllocator::ExecutableAllocator(VM&)
127 {
128 ASSERT(allocator);
129 }
130
131 ExecutableAllocator::~ExecutableAllocator()
132 {
133 }
134
135 FixedVMPoolExecutableAllocator::~FixedVMPoolExecutableAllocator()
136 {
137 m_reservation.deallocate();
138 }
139
140 bool ExecutableAllocator::isValid() const
141 {
142 return !!allocator->bytesReserved();
143 }
144
145 bool ExecutableAllocator::underMemoryPressure()
146 {
147 MetaAllocator::Statistics statistics = allocator->currentStatistics();
148 return statistics.bytesAllocated > statistics.bytesReserved / 2;
149 }
150
151 double ExecutableAllocator::memoryPressureMultiplier(size_t addedMemoryUsage)
152 {
153 MetaAllocator::Statistics statistics = allocator->currentStatistics();
154 ASSERT(statistics.bytesAllocated <= statistics.bytesReserved);
155 size_t bytesAllocated = statistics.bytesAllocated + addedMemoryUsage;
156 size_t bytesAvailable = static_cast<size_t>(
157 statistics.bytesReserved * (1 - executablePoolReservationFraction));
158 if (bytesAllocated >= bytesAvailable)
159 bytesAllocated = bytesAvailable;
160 double result = 1.0;
161 size_t divisor = bytesAvailable - bytesAllocated;
162 if (divisor)
163 result = static_cast<double>(bytesAvailable) / divisor;
164 if (result < 1.0)
165 result = 1.0;
166 return result;
167 }
168
169 RefPtr<ExecutableMemoryHandle> ExecutableAllocator::allocate(VM&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort effort)
170 {
171 if (effort != JITCompilationCanFail && Options::reportMustSucceedExecutableAllocations()) {
172 dataLog("Allocating ", sizeInBytes, " bytes of executable memory with JITCompilationMustSucceed.\n");
173 WTFReportBacktrace();
174 }
175
176 if (effort == JITCompilationCanFail
177 && doExecutableAllocationFuzzingIfEnabled() == PretendToFailExecutableAllocation)
178 return nullptr;
179
180 if (effort == JITCompilationCanFail) {
181 // Don't allow allocations if we are down to reserve.
182 MetaAllocator::Statistics statistics = allocator->currentStatistics();
183 size_t bytesAllocated = statistics.bytesAllocated + sizeInBytes;
184 size_t bytesAvailable = static_cast<size_t>(
185 statistics.bytesReserved * (1 - executablePoolReservationFraction));
186 if (bytesAllocated > bytesAvailable)
187 return nullptr;
188 }
189
190 RefPtr<ExecutableMemoryHandle> result = allocator->allocate(sizeInBytes, ownerUID);
191 if (!result) {
192 if (effort != JITCompilationCanFail) {
193 dataLog("Ran out of executable memory while allocating ", sizeInBytes, " bytes.\n");
194 CRASH();
195 }
196 return nullptr;
197 }
198 return result;
199 }
200
201 size_t ExecutableAllocator::committedByteCount()
202 {
203 return allocator->bytesCommitted();
204 }
205
206 #if ENABLE(META_ALLOCATOR_PROFILE)
207 void ExecutableAllocator::dumpProfile()
208 {
209 allocator->dumpProfile();
210 }
211 #endif
212
213 }
214
215
216 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)