1/*
2 * Copyright (C) 2008, 2013, 2014, 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "JSStackInlines.h"
31
32#include "ConservativeRoots.h"
33#include "Interpreter.h"
34#include "JSCInlines.h"
35#include "Options.h"
36#include <wtf/Lock.h>
37
38namespace JSC {
39
40#if !ENABLE(JIT)
41static size_t committedBytesCount = 0;
42
43static size_t commitSize()
44{
45 static size_t size = std::max<size_t>(16 * 1024, pageSize());
46 return size;
47}
48
49static StaticLock stackStatisticsMutex;
50#endif // !ENABLE(JIT)
51
52JSStack::JSStack(VM& vm)
53 : m_vm(vm)
54 , m_topCallFrame(vm.topCallFrame)
55#if !ENABLE(JIT)
56 , m_end(0)
57 , m_reservedZoneSizeInRegisters(0)
58#endif
59{
60#if !ENABLE(JIT)
61 size_t capacity = Options::maxPerThreadStackUsage();
62 ASSERT(capacity && isPageAligned(capacity));
63
64 m_reservation = PageReservation::reserve(WTF::roundUpToMultipleOf(commitSize(), capacity), OSAllocator::JSVMStackPages);
65 setStackLimit(highAddress());
66 m_commitTop = highAddress();
67
68 m_lastStackTop = baseOfStack();
69#endif // !ENABLE(JIT)
70
71 m_topCallFrame = 0;
72}
73
74#if !ENABLE(JIT)
75JSStack::~JSStack()
76{
77 ptrdiff_t sizeToDecommit = reinterpret_cast<char*>(highAddress()) - reinterpret_cast<char*>(m_commitTop);
78 m_reservation.decommit(reinterpret_cast<void*>(m_commitTop), sizeToDecommit);
79 addToCommittedByteCount(-sizeToDecommit);
80 m_reservation.deallocate();
81}
82
83bool JSStack::growSlowCase(Register* newTopOfStack)
84{
85 Register* newTopOfStackWithReservedZone = newTopOfStack - m_reservedZoneSizeInRegisters;
86
87 // If we have already committed enough memory to satisfy this request,
88 // just update the end pointer and return.
89 if (newTopOfStackWithReservedZone >= m_commitTop) {
90 setStackLimit(newTopOfStack);
91 return true;
92 }
93
94 // Compute the chunk size of additional memory to commit, and see if we
95 // have it is still within our budget. If not, we'll fail to grow and
96 // return false.
97 ptrdiff_t delta = reinterpret_cast<char*>(m_commitTop) - reinterpret_cast<char*>(newTopOfStackWithReservedZone);
98 delta = WTF::roundUpToMultipleOf(commitSize(), delta);
99 Register* newCommitTop = m_commitTop - (delta / sizeof(Register));
100 if (newCommitTop < reservationTop())
101 return false;
102
103 // Otherwise, the growth is still within our budget. Commit it and return true.
104 m_reservation.commit(newCommitTop, delta);
105 addToCommittedByteCount(delta);
106 m_commitTop = newCommitTop;
107 setStackLimit(newTopOfStack);
108 return true;
109}
110
111void JSStack::gatherConservativeRoots(ConservativeRoots& conservativeRoots, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks)
112{
113 conservativeRoots.add(topOfStack() + 1, highAddress(), jitStubRoutines, codeBlocks);
114}
115
116void JSStack::sanitizeStack()
117{
118#if !ASAN_ENABLED
119 ASSERT(topOfStack() <= baseOfStack());
120
121 if (m_lastStackTop < topOfStack()) {
122 char* begin = reinterpret_cast<char*>(m_lastStackTop + 1);
123 char* end = reinterpret_cast<char*>(topOfStack() + 1);
124 memset(begin, 0, end - begin);
125 }
126
127 m_lastStackTop = topOfStack();
128#endif
129}
130
131void JSStack::releaseExcessCapacity()
132{
133 Register* highAddressWithReservedZone = highAddress() - m_reservedZoneSizeInRegisters;
134 ptrdiff_t delta = reinterpret_cast<char*>(highAddressWithReservedZone) - reinterpret_cast<char*>(m_commitTop);
135 m_reservation.decommit(m_commitTop, delta);
136 addToCommittedByteCount(-delta);
137 m_commitTop = highAddressWithReservedZone;
138}
139
140void JSStack::addToCommittedByteCount(long byteCount)
141{
142 LockHolder locker(stackStatisticsMutex);
143 ASSERT(static_cast<long>(committedBytesCount) + byteCount > -1);
144 committedBytesCount += byteCount;
145}
146
147void JSStack::setReservedZoneSize(size_t reservedZoneSize)
148{
149 m_reservedZoneSizeInRegisters = reservedZoneSize / sizeof(Register);
150 if (m_commitTop >= (m_end + 1) - m_reservedZoneSizeInRegisters)
151 growSlowCase(m_end + 1);
152}
153#endif // !ENABLE(JIT)
154
155#if ENABLE(JIT)
156Register* JSStack::lowAddress() const
157{
158 ASSERT(wtfThreadData().stack().isGrowingDownward());
159 return reinterpret_cast<Register*>(m_vm.stackLimit());
160}
161
162Register* JSStack::highAddress() const
163{
164 ASSERT(wtfThreadData().stack().isGrowingDownward());
165 return reinterpret_cast<Register*>(wtfThreadData().stack().origin());
166}
167#endif // ENABLE(JIT)
168
169size_t JSStack::committedByteCount()
170{
171#if !ENABLE(JIT)
172 LockHolder locker(stackStatisticsMutex);
173 return committedBytesCount;
174#else
175 // When using the C stack, we don't know how many stack pages are actually
176 // committed. So, we use the current stack usage as an estimate.
177 ASSERT(wtfThreadData().stack().isGrowingDownward());
178 int8_t* current = reinterpret_cast<int8_t*>(&current);
179 int8_t* high = reinterpret_cast<int8_t*>(wtfThreadData().stack().origin());
180 return high - current;
181#endif
182}
183
184} // namespace JSC
185