#include "config.h"
#include "Watchdog.h"
#include "CallFrame.h"
#include <wtf/CurrentTime.h>
#include <wtf/MathExtras.h>
namespace JSC {
const std::chrono::microseconds Watchdog::noTimeLimit = std::chrono::microseconds::max();
static std::chrono::microseconds currentWallClockTime()
{
auto steadyTimeSinceEpoch = std::chrono::steady_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::microseconds>(steadyTimeSinceEpoch);
}
Watchdog::Watchdog(VM* vm)
: m_vm(vm)
, m_timeLimit(noTimeLimit)
, m_cpuDeadline(noTimeLimit)
, m_wallClockDeadline(noTimeLimit)
, m_callback(0)
, m_callbackData1(0)
, m_callbackData2(0)
, m_timerQueue(WorkQueue::create("jsc.watchdog.queue", WorkQueue::Type::Serial, WorkQueue::QOS::Utility))
{
}
void Watchdog::setTimeLimit(std::chrono::microseconds limit,
ShouldTerminateCallback callback, void* data1, void* data2)
{
ASSERT(m_vm->currentThreadIsHoldingAPILock());
m_timeLimit = limit;
m_callback = callback;
m_callbackData1 = data1;
m_callbackData2 = data2;
if (m_hasEnteredVM && hasTimeLimit())
startTimer(m_timeLimit);
}
bool Watchdog::shouldTerminate(ExecState* exec)
{
ASSERT(m_vm->currentThreadIsHoldingAPILock());
if (currentWallClockTime() < m_wallClockDeadline)
return false;
m_wallClockDeadline = noTimeLimit;
auto cpuTime = currentCPUTime();
if (cpuTime < m_cpuDeadline) {
auto remainingCPUTime = m_cpuDeadline - cpuTime;
startTimer(remainingCPUTime);
return false;
}
bool needsTermination = !m_callback
|| m_callback(exec, m_callbackData1, m_callbackData2);
if (needsTermination)
return true;
ASSERT(m_hasEnteredVM);
bool callbackAlreadyStartedTimer = (m_cpuDeadline != noTimeLimit);
if (hasTimeLimit() && !callbackAlreadyStartedTimer)
startTimer(m_timeLimit);
return false;
}
bool Watchdog::hasTimeLimit()
{
return (m_timeLimit != noTimeLimit);
}
void Watchdog::enteredVM()
{
m_hasEnteredVM = true;
if (hasTimeLimit())
startTimer(m_timeLimit);
}
void Watchdog::exitedVM()
{
ASSERT(m_hasEnteredVM);
stopTimer();
m_hasEnteredVM = false;
}
void Watchdog::startTimer(std::chrono::microseconds timeLimit)
{
ASSERT(m_hasEnteredVM);
ASSERT(m_vm->currentThreadIsHoldingAPILock());
ASSERT(hasTimeLimit());
ASSERT(timeLimit <= m_timeLimit);
m_cpuDeadline = currentCPUTime() + timeLimit;
auto wallClockTime = currentWallClockTime();
auto wallClockDeadline = wallClockTime + timeLimit;
if ((wallClockTime < m_wallClockDeadline)
&& (m_wallClockDeadline <= wallClockDeadline))
return;
m_wallClockDeadline = wallClockDeadline;
RefPtr<Watchdog> protectedThis = this;
m_timerQueue->dispatchAfter(Seconds::fromMicroseconds(timeLimit.count()), [this, protectedThis] {
LockHolder locker(m_lock);
if (m_vm)
m_vm->notifyNeedWatchdogCheck();
});
}
void Watchdog::stopTimer()
{
ASSERT(m_hasEnteredVM);
ASSERT(m_vm->currentThreadIsHoldingAPILock());
m_cpuDeadline = noTimeLimit;
}
void Watchdog::willDestroyVM(VM* vm)
{
LockHolder locker(m_lock);
ASSERT_UNUSED(vm, m_vm == vm);
m_vm = nullptr;
}
}