#include "config.h"
#include "CLoopStack.h"
#if !ENABLE(JIT)
#include "CLoopStackInlines.h"
#include "ConservativeRoots.h"
#include "Interpreter.h"
#include "JSCInlines.h"
#include "Options.h"
#include <wtf/Lock.h>
namespace JSC {
static size_t committedBytesCount = 0;
static size_t commitSize()
{
static size_t size = std::max<size_t>(16 * 1024, pageSize());
return size;
}
static StaticLock stackStatisticsMutex;
CLoopStack::CLoopStack(VM& vm)
: m_vm(vm)
, m_topCallFrame(vm.topCallFrame)
, m_end(0)
, m_softReservedZoneSizeInRegisters(0)
{
size_t capacity = Options::maxPerThreadStackUsage();
ASSERT(capacity && isPageAligned(capacity));
m_reservation = PageReservation::reserve(WTF::roundUpToMultipleOf(commitSize(), capacity), OSAllocator::JSVMStackPages);
setCLoopStackLimit(highAddress());
m_commitTop = highAddress();
m_lastStackTop = baseOfStack();
m_topCallFrame = 0;
}
CLoopStack::~CLoopStack()
{
ptrdiff_t sizeToDecommit = reinterpret_cast<char*>(highAddress()) - reinterpret_cast<char*>(m_commitTop);
m_reservation.decommit(reinterpret_cast<void*>(m_commitTop), sizeToDecommit);
addToCommittedByteCount(-sizeToDecommit);
m_reservation.deallocate();
}
bool CLoopStack::grow(Register* newTopOfStack)
{
Register* newTopOfStackWithReservedZone = newTopOfStack - m_softReservedZoneSizeInRegisters;
if (newTopOfStackWithReservedZone >= m_commitTop) {
setCLoopStackLimit(newTopOfStack);
return true;
}
ptrdiff_t delta = reinterpret_cast<char*>(m_commitTop) - reinterpret_cast<char*>(newTopOfStackWithReservedZone);
delta = WTF::roundUpToMultipleOf(commitSize(), delta);
Register* newCommitTop = m_commitTop - (delta / sizeof(Register));
if (newCommitTop < reservationTop())
return false;
m_reservation.commit(newCommitTop, delta);
addToCommittedByteCount(delta);
m_commitTop = newCommitTop;
setCLoopStackLimit(newTopOfStack);
return true;
}
void CLoopStack::gatherConservativeRoots(ConservativeRoots& conservativeRoots, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks)
{
conservativeRoots.add(topOfStack() + 1, highAddress(), jitStubRoutines, codeBlocks);
}
void CLoopStack::sanitizeStack()
{
#if !ASAN_ENABLED
ASSERT(topOfStack() <= baseOfStack());
if (m_lastStackTop < topOfStack()) {
char* begin = reinterpret_cast<char*>(m_lastStackTop + 1);
char* end = reinterpret_cast<char*>(topOfStack() + 1);
memset(begin, 0, end - begin);
}
m_lastStackTop = topOfStack();
#endif
}
void CLoopStack::releaseExcessCapacity()
{
Register* highAddressWithReservedZone = highAddress() - m_softReservedZoneSizeInRegisters;
ptrdiff_t delta = reinterpret_cast<char*>(highAddressWithReservedZone) - reinterpret_cast<char*>(m_commitTop);
m_reservation.decommit(m_commitTop, delta);
addToCommittedByteCount(-delta);
m_commitTop = highAddressWithReservedZone;
}
void CLoopStack::addToCommittedByteCount(long byteCount)
{
LockHolder locker(stackStatisticsMutex);
ASSERT(static_cast<long>(committedBytesCount) + byteCount > -1);
committedBytesCount += byteCount;
}
void CLoopStack::setSoftReservedZoneSize(size_t reservedZoneSize)
{
m_softReservedZoneSizeInRegisters = reservedZoneSize / sizeof(Register);
if (m_commitTop >= (m_end + 1) - m_softReservedZoneSizeInRegisters)
grow(m_end + 1);
}
bool CLoopStack::isSafeToRecurse() const
{
void* reservationLimit = reinterpret_cast<int8_t*>(reservationTop() + m_softReservedZoneSizeInRegisters);
return !m_topCallFrame || (m_topCallFrame->topOfFrame() > reservationLimit);
}
size_t CLoopStack::committedByteCount()
{
LockHolder locker(stackStatisticsMutex);
return committedBytesCount;
}
}
#endif // !ENABLE(JIT)