2 * Copyright (C) 2009, 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "ExecutableAllocator.h"
29 #include "JSCInlines.h"
31 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
33 #include "CodeProfiling.h"
34 #include "ExecutableAllocationFuzz.h"
39 #include <wtf/MetaAllocator.h>
40 #include <wtf/PageReservation.h>
41 #include <wtf/VMTags.h>
55 uintptr_t startOfFixedExecutableMemoryPool
;
57 class FixedVMPoolExecutableAllocator
: public MetaAllocator
{
58 WTF_MAKE_FAST_ALLOCATED
;
60 FixedVMPoolExecutableAllocator()
61 : MetaAllocator(jitAllocationGranule
) // round up all allocations to 32 bytes
63 size_t reservationSize
;
64 if (Options::jitMemoryReservationSize())
65 reservationSize
= Options::jitMemoryReservationSize();
67 reservationSize
= fixedExecutableMemoryPoolSize
;
68 m_reservation
= PageReservation::reserveWithGuardPages(reservationSize
, OSAllocator::JSJITCodePages
, EXECUTABLE_POOL_WRITABLE
, true);
70 ASSERT(m_reservation
.size() == reservationSize
);
71 addFreshFreeSpace(m_reservation
.base(), m_reservation
.size());
73 startOfFixedExecutableMemoryPool
= reinterpret_cast<uintptr_t>(m_reservation
.base());
77 virtual ~FixedVMPoolExecutableAllocator();
80 virtual void* allocateNewSpace(size_t&) override
82 // We're operating in a fixed pool, so new allocation is always prohibited.
86 virtual void notifyNeedPage(void* page
) override
88 #if USE(MADV_FREE_FOR_JIT_MEMORY)
91 m_reservation
.commit(page
, pageSize());
95 virtual void notifyPageIsFree(void* page
) override
97 #if USE(MADV_FREE_FOR_JIT_MEMORY)
99 int result
= madvise(page
, pageSize(), MADV_FREE
);
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.
109 m_reservation
.decommit(page
, pageSize());
114 PageReservation m_reservation
;
117 static FixedVMPoolExecutableAllocator
* allocator
;
119 void ExecutableAllocator::initializeAllocator()
122 allocator
= new FixedVMPoolExecutableAllocator();
123 CodeProfiling::notifyAllocator(allocator
);
126 ExecutableAllocator::ExecutableAllocator(VM
&)
131 ExecutableAllocator::~ExecutableAllocator()
135 FixedVMPoolExecutableAllocator::~FixedVMPoolExecutableAllocator()
137 m_reservation
.deallocate();
140 bool ExecutableAllocator::isValid() const
142 return !!allocator
->bytesReserved();
145 bool ExecutableAllocator::underMemoryPressure()
147 MetaAllocator::Statistics statistics
= allocator
->currentStatistics();
148 return statistics
.bytesAllocated
> statistics
.bytesReserved
/ 2;
151 double ExecutableAllocator::memoryPressureMultiplier(size_t addedMemoryUsage
)
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
;
161 size_t divisor
= bytesAvailable
- bytesAllocated
;
163 result
= static_cast<double>(bytesAvailable
) / divisor
;
169 RefPtr
<ExecutableMemoryHandle
> ExecutableAllocator::allocate(VM
&, size_t sizeInBytes
, void* ownerUID
, JITCompilationEffort effort
)
171 if (effort
!= JITCompilationCanFail
&& Options::reportMustSucceedExecutableAllocations()) {
172 dataLog("Allocating ", sizeInBytes
, " bytes of executable memory with JITCompilationMustSucceed.\n");
173 WTFReportBacktrace();
176 if (effort
== JITCompilationCanFail
177 && doExecutableAllocationFuzzingIfEnabled() == PretendToFailExecutableAllocation
)
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
)
190 RefPtr
<ExecutableMemoryHandle
> result
= allocator
->allocate(sizeInBytes
, ownerUID
);
192 if (effort
!= JITCompilationCanFail
) {
193 dataLog("Ran out of executable memory while allocating ", sizeInBytes
, " bytes.\n");
201 size_t ExecutableAllocator::committedByteCount()
203 return allocator
->bytesCommitted();
206 #if ENABLE(META_ALLOCATOR_PROFILE)
207 void ExecutableAllocator::dumpProfile()
209 allocator
->dumpProfile();
216 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)