1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* Copyright (C) 2012 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JSStackInlines_h
#define JSStackInlines_h
#include "CallFrame.h"
#include "CodeBlock.h"
#include "JSStack.h"
namespace JSC {
inline Register* JSStack::getTopOfFrame(CallFrame* frame)
{
if (UNLIKELY(!frame))
return begin();
return frame->frameExtent();
}
inline Register* JSStack::getTopOfStack()
{
return getTopOfFrame(m_topCallFrame);
}
inline Register* JSStack::getStartOfFrame(CallFrame* frame)
{
CallFrame* callerFrame = frame->callerFrameNoFlags();
return getTopOfFrame(callerFrame);
}
inline CallFrame* JSStack::pushFrame(CallFrame* callerFrame,
class CodeBlock* codeBlock, JSScope* scope, int argsCount, JSObject* callee)
{
ASSERT(!!scope);
Register* oldEnd = getTopOfStack();
// Ensure that we have enough space for the parameters:
size_t paddedArgsCount = argsCount;
if (codeBlock) {
size_t numParameters = codeBlock->numParameters();
if (paddedArgsCount < numParameters)
paddedArgsCount = numParameters;
}
Register* newCallFrameSlot = oldEnd + paddedArgsCount + JSStack::CallFrameHeaderSize;
#if ENABLE(DEBUG_JSSTACK)
newCallFrameSlot += JSStack::FenceSize;
#endif
Register* newEnd = newCallFrameSlot;
if (!!codeBlock)
newEnd += codeBlock->m_numCalleeRegisters;
// Ensure that we have the needed stack capacity to push the new frame:
if (!grow(newEnd))
return 0;
// Compute the address of the new frame for this invocation:
CallFrame* newCallFrame = CallFrame::create(newCallFrameSlot);
ASSERT(!!newCallFrame);
// The caller frame should always be the real previous frame on the stack,
// and not a potential GlobalExec that was passed in. Point callerFrame to
// the top frame on the stack.
callerFrame = m_topCallFrame;
// Initialize the frame header:
newCallFrame->init(codeBlock, 0, scope,
callerFrame->addHostCallFrameFlag(), argsCount, callee);
ASSERT(!!newCallFrame->scope());
// Pad additional args if needed:
// Note: we need to subtract 1 from argsCount and paddedArgsCount to
// exclude the this pointer.
for (size_t i = argsCount-1; i < paddedArgsCount-1; ++i)
newCallFrame->setArgument(i, jsUndefined());
installFence(newCallFrame, __FUNCTION__, __LINE__);
validateFence(newCallFrame, __FUNCTION__, __LINE__);
installTrapsAfterFrame(newCallFrame);
// Push the new frame:
m_topCallFrame = newCallFrame;
return newCallFrame;
}
inline void JSStack::popFrame(CallFrame* frame)
{
validateFence(frame, __FUNCTION__, __LINE__);
CallFrame* callerFrame = frame->callerFrameNoFlags();
// Pop to the caller:
m_topCallFrame = callerFrame;
// If we are popping the very first frame from the stack i.e. no more
// frames before this, then we can now safely shrink the stack. In
// this case, we're shrinking all the way to the beginning since there
// are no more frames on the stack.
if (!callerFrame)
shrink(begin());
installTrapsAfterFrame(callerFrame);
}
#if ENABLE(DEBUG_JSSTACK)
inline JSValue JSStack::generateFenceValue(size_t argIndex)
{
unsigned fenceBits = 0xfacebad0 | ((argIndex+1) & 0xf);
JSValue fenceValue = JSValue(fenceBits);
return fenceValue;
}
// The JSStack fences mechanism works as follows:
// 1. A fence is a number (JSStack::FenceSize) of JSValues that are initialized
// with values generated by JSStack::generateFenceValue().
// 2. When pushFrame() is called, the fence is installed after the max extent
// of the previous topCallFrame and the last arg of the new frame:
//
// | ... |
// |--------------------------------------|
// | Frame Header of previous frame |
// |--------------------------------------|
// topCallFrame --> | |
// | Locals of previous frame |
// |--------------------------------------|
// | *** the Fence *** |
// |--------------------------------------|
// | Args of new frame |
// |--------------------------------------|
// | Frame Header of new frame |
// |--------------------------------------|
// frame --> | Locals of new frame |
// | |
//
// 3. In popFrame() and elsewhere, we can call JSStack::validateFence() to
// assert that the fence contains the values we expect.
inline void JSStack::installFence(CallFrame* frame, const char *function, int lineNo)
{
UNUSED_PARAM(function);
UNUSED_PARAM(lineNo);
Register* startOfFrame = getStartOfFrame(frame);
// The last argIndex is at:
size_t maxIndex = frame->argIndexForRegister(startOfFrame) + 1;
size_t startIndex = maxIndex - FenceSize;
for (size_t i = startIndex; i < maxIndex; ++i) {
JSValue fenceValue = generateFenceValue(i);
frame->setArgument(i, fenceValue);
}
}
inline void JSStack::validateFence(CallFrame* frame, const char *function, int lineNo)
{
UNUSED_PARAM(function);
UNUSED_PARAM(lineNo);
ASSERT(!!frame->scope());
Register* startOfFrame = getStartOfFrame(frame);
size_t maxIndex = frame->argIndexForRegister(startOfFrame) + 1;
size_t startIndex = maxIndex - FenceSize;
for (size_t i = startIndex; i < maxIndex; ++i) {
JSValue fenceValue = generateFenceValue(i);
JSValue actualValue = frame->getArgumentUnsafe(i);
ASSERT(fenceValue == actualValue);
}
}
// When debugging the JSStack, we install bad values after the extent of the
// topCallFrame at the end of pushFrame() and popFrame(). The intention is
// to trigger crashes in the event that memory in this supposedly unused
// region is read and consumed without proper initialization. After the trap
// words are installed, the stack looks like this:
//
// | ... |
// |-----------------------------|
// | Frame Header of frame |
// |-----------------------------|
// topCallFrame --> | |
// | Locals of frame |
// |-----------------------------|
// | *** Trap words *** |
// |-----------------------------|
// | Unused space ... |
// | ... |
inline void JSStack::installTrapsAfterFrame(CallFrame* frame)
{
Register* topOfFrame = getTopOfFrame(frame);
const int sizeOfTrap = 64;
int32_t* startOfTrap = reinterpret_cast<int32_t*>(topOfFrame);
int32_t* endOfTrap = startOfTrap + sizeOfTrap;
int32_t* endOfCommitedMemory = reinterpret_cast<int32_t*>(m_commitEnd);
// Make sure we're not exceeding the amount of available memory to write to:
if (endOfTrap > endOfCommitedMemory)
endOfTrap = endOfCommitedMemory;
// Lay the traps:
int32_t* p = startOfTrap;
while (p < endOfTrap)
*p++ = 0xabadcafe; // A bad word to trigger a crash if deref'ed.
}
#endif // ENABLE(DEBUG_JSSTACK)
} // namespace JSC
#endif // JSStackInlines_h
|