#pragma once
#include "CallFrame.h"
#include "JSCJSValue.h"
#include <wtf/FastMalloc.h>
#include <wtf/Noncopyable.h>
#include <wtf/PrintStream.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
namespace JSC {
class CodeBlock;
class ExecState;
class JSArray;
class JSObject;
class JSScope;
class LLIntOffsetsExtractor;
class SlotVisitor;
class VM;
typedef ExecState CallFrame;
class ShadowChicken {
WTF_MAKE_NONCOPYABLE(ShadowChicken);
WTF_MAKE_FAST_ALLOCATED;
public:
struct Packet {
Packet()
{
}
static const unsigned unlikelyValue = 0x7a11;
static JSObject* tailMarker()
{
return bitwise_cast<JSObject*>(static_cast<intptr_t>(unlikelyValue));
}
static JSObject* throwMarker()
{
return bitwise_cast<JSObject*>(static_cast<intptr_t>(unlikelyValue + 1));
}
static Packet prologue(JSObject* callee, CallFrame* frame, CallFrame* callerFrame, JSScope* scope)
{
Packet result;
result.callee = callee;
result.frame = frame;
result.callerFrame = callerFrame;
result.scope = scope;
return result;
}
static Packet tail(CallFrame* frame, JSValue thisValue, JSScope* scope, CodeBlock* codeBlock, CallSiteIndex callSiteIndex)
{
Packet result;
result.callee = tailMarker();
result.frame = frame;
result.thisValue = thisValue;
result.scope = scope;
result.codeBlock = codeBlock;
result.callSiteIndex = callSiteIndex;
return result;
}
static Packet throwPacket()
{
Packet result;
result.callee = throwMarker();
return result;
}
explicit operator bool() const { return !!callee; }
bool isPrologue() const { return *this && callee != tailMarker() && callee != throwMarker(); }
bool isTail() const { return *this && callee == tailMarker(); }
bool isThrow() const { return *this && callee == throwMarker(); }
void dump(PrintStream&) const;
JSValue thisValue { JSValue() };
JSObject* callee { nullptr };
CallFrame* frame { nullptr };
CallFrame* callerFrame { nullptr };
JSScope* scope { nullptr };
CodeBlock* codeBlock { nullptr };
CallSiteIndex callSiteIndex;
};
struct Frame {
Frame()
{
}
Frame(JSObject* callee, CallFrame* frame, bool isTailDeleted, JSValue thisValue = JSValue(), JSScope* scope = nullptr, CodeBlock* codeBlock = nullptr, CallSiteIndex callSiteIndex = CallSiteIndex())
: callee(callee)
, frame(frame)
, thisValue(thisValue)
, scope(scope)
, codeBlock(codeBlock)
, callSiteIndex(callSiteIndex)
, isTailDeleted(isTailDeleted)
{
}
bool operator==(const Frame& other) const
{
return callee == other.callee
&& frame == other.frame
&& thisValue == other.thisValue
&& scope == other.scope
&& codeBlock == other.codeBlock
&& callSiteIndex.bits() == other.callSiteIndex.bits()
&& isTailDeleted == other.isTailDeleted;
}
bool operator!=(const Frame& other) const
{
return !(*this == other);
}
void dump(PrintStream&) const;
JSObject* callee { nullptr };
CallFrame* frame { nullptr };
JSValue thisValue { JSValue() };
JSScope* scope { nullptr };
CodeBlock* codeBlock { nullptr };
CallSiteIndex callSiteIndex;
bool isTailDeleted { false };
};
ShadowChicken();
~ShadowChicken();
void log(VM& vm, ExecState* exec, const Packet&);
void update(VM&, ExecState*);
template<typename Functor>
void iterate(VM&, ExecState*, const Functor&);
void visitChildren(SlotVisitor&);
void reset();
Packet* log() const { return m_log; }
unsigned logSize() const { return m_logSize; }
Packet** addressOfLogCursor() { return &m_logCursor; }
Packet* logEnd() { return m_logEnd; }
void dump(PrintStream&) const;
JS_EXPORT_PRIVATE JSArray* functionsOnStack(ExecState*);
private:
friend class LLIntOffsetsExtractor;
Packet* m_log { nullptr };
unsigned m_logSize { 0 };
Packet* m_logCursor { nullptr };
Packet* m_logEnd { nullptr };
Vector<Frame> m_stack;
};
}