MacroAssemblerCodeRef.h [plain text]
#pragma once
#include "ExecutableAllocator.h"
#include "JSCPoison.h"
#include "JSCPtrTag.h"
#include <wtf/DataLog.h>
#include <wtf/PrintStream.h>
#include <wtf/RefPtr.h>
#include <wtf/text/CString.h>
#if CPU(ARM_THUMB2) && ENABLE(JIT)
#define ASSERT_NULL_OR_VALID_CODE_POINTER(ptr) \
ASSERT(!ptr || reinterpret_cast<intptr_t>(ptr) & ~1)
#define ASSERT_VALID_CODE_POINTER(ptr) \
ASSERT(reinterpret_cast<intptr_t>(ptr) & ~1)
#define ASSERT_VALID_CODE_OFFSET(offset) \
ASSERT(!(offset & 1)) #else
#define ASSERT_NULL_OR_VALID_CODE_POINTER(ptr) // Anything goes!
#define ASSERT_VALID_CODE_POINTER(ptr) \
ASSERT(ptr)
#define ASSERT_VALID_CODE_OFFSET(offset) // Anything goes!
#endif
namespace JSC {
template<PtrTag> class MacroAssemblerCodePtr;
enum OpcodeID : unsigned;
template<PtrTag tag = CFunctionPtrTag>
class FunctionPtr {
public:
FunctionPtr() { }
FunctionPtr(std::nullptr_t) { }
template<typename ReturnType, typename... Arguments>
FunctionPtr(ReturnType(*value)(Arguments...))
: m_value(tagCFunctionPtr<void*, tag>(value))
{
assertIsNullOrCFunctionPtr(value);
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_NULL_OR_VALID_CODE_POINTER(m_value);
}
#if CALLING_CONVENTION_IS_STDCALL && !OS(WINDOWS)
template<typename ReturnType, typename... Arguments>
FunctionPtr(ReturnType(CDECL *value)(Arguments...))
: m_value(tagCFunctionPtr<void*, tag>(value))
{
assertIsNullOrCFunctionPtr(value);
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_NULL_OR_VALID_CODE_POINTER(m_value);
}
#endif // CALLING_CONVENTION_IS_STDCALL && !OS(WINDOWS)
#if COMPILER_SUPPORTS(FASTCALL_CALLING_CONVENTION)
template<typename ReturnType, typename... Arguments>
FunctionPtr(ReturnType(FASTCALL *value)(Arguments...))
: m_value(tagCFunctionPtr<void*, tag>(value))
{
assertIsNullOrCFunctionPtr(value);
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_NULL_OR_VALID_CODE_POINTER(m_value);
}
#endif // COMPILER_SUPPORTS(FASTCALL_CALLING_CONVENTION)
template<typename PtrType, typename = std::enable_if_t<std::is_pointer<PtrType>::value && !std::is_function<typename std::remove_pointer<PtrType>::type>::value>>
explicit FunctionPtr(PtrType value)
: m_value(tagCFunctionPtr<void*, tag>(value))
{
assertIsNullOrCFunctionPtr(value);
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_NULL_OR_VALID_CODE_POINTER(m_value);
}
explicit FunctionPtr(MacroAssemblerCodePtr<tag>);
template<PtrTag otherTag>
FunctionPtr<otherTag> retagged() const
{
if (!m_value)
return FunctionPtr<otherTag>();
return FunctionPtr<otherTag>(*this);
}
void* executableAddress() const
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
return m_value;
}
template<PtrTag newTag>
void* retaggedExecutableAddress() const
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
return retagCodePtr<tag, newTag>(m_value);
}
explicit operator bool() const { return !!m_value; }
bool operator!() const { return !m_value; }
bool operator==(const FunctionPtr& other) const { return m_value == other.m_value; }
bool operator!=(const FunctionPtr& other) const { return m_value != other.m_value; }
private:
template<PtrTag otherTag>
explicit FunctionPtr(const FunctionPtr<otherTag>& other)
: m_value(retagCodePtr<otherTag, tag>(other.executableAddress()))
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_NULL_OR_VALID_CODE_POINTER(m_value);
}
void* m_value { nullptr };
template<PtrTag> friend class FunctionPtr;
};
static_assert(sizeof(FunctionPtr<CFunctionPtrTag>) == sizeof(void*), "");
#if COMPILER_SUPPORTS(BUILTIN_IS_TRIVIALLY_COPYABLE)
static_assert(__is_trivially_copyable(FunctionPtr<CFunctionPtrTag>), "");
#endif
class ReturnAddressPtr {
public:
ReturnAddressPtr() { }
explicit ReturnAddressPtr(void* value)
: m_value(value)
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_VALID_CODE_POINTER(m_value);
}
template<PtrTag tag>
explicit ReturnAddressPtr(FunctionPtr<tag> function)
: m_value(untagCodePtr<tag>(function.executableAddress()))
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
ASSERT_VALID_CODE_POINTER(m_value);
}
void* value() const
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
return m_value;
}
void dump(PrintStream& out) const
{
out.print(RawPointer(m_value));
}
private:
void* m_value { nullptr };
};
class MacroAssemblerCodePtrBase {
protected:
static void dumpWithName(void* executableAddress, void* dataLocation, const char* name, PrintStream& out);
};
template<PtrTag tag>
class MacroAssemblerCodePtr : private MacroAssemblerCodePtrBase {
public:
MacroAssemblerCodePtr() = default;
MacroAssemblerCodePtr(std::nullptr_t) : m_value(nullptr) { }
explicit MacroAssemblerCodePtr(void* value)
#if CPU(ARM_THUMB2)
: m_value(reinterpret_cast<char*>(value) + 1)
#else
: m_value(value)
#endif
{
assertIsTaggedWith(value, tag);
m_value.assertIsPoisoned();
ASSERT(value);
#if CPU(ARM_THUMB2)
ASSERT(!(reinterpret_cast<uintptr_t>(value) & 1));
#endif
ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
}
static MacroAssemblerCodePtr createFromExecutableAddress(void* value)
{
ASSERT(value);
ASSERT_VALID_CODE_POINTER(value);
assertIsTaggedWith(value, tag);
MacroAssemblerCodePtr result;
result.m_value = PoisonedMasmPtr(value);
result.m_value.assertIsPoisoned();
return result;
}
explicit MacroAssemblerCodePtr(ReturnAddressPtr ra)
: m_value(tagCodePtr<tag>(ra.value()))
{
assertIsNotTagged(ra.value());
ASSERT(ra.value());
m_value.assertIsPoisoned();
ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
}
PoisonedMasmPtr poisonedPtr() const { return m_value; }
template<PtrTag newTag>
MacroAssemblerCodePtr<newTag> retagged() const
{
if (!m_value)
return MacroAssemblerCodePtr<newTag>();
return MacroAssemblerCodePtr<newTag>::createFromExecutableAddress(retaggedExecutableAddress<newTag>());
}
template<typename T = void*>
T executableAddress() const
{
m_value.assertIsPoisoned();
return m_value.unpoisoned<T>();
}
template<typename T = void*>
T untaggedExecutableAddress() const
{
m_value.assertIsPoisoned();
return untagCodePtr<T, tag>(m_value.unpoisoned());
}
template<PtrTag newTag, typename T = void*>
T retaggedExecutableAddress() const
{
m_value.assertIsPoisoned();
return retagCodePtr<T, tag, newTag>(m_value.unpoisoned());
}
#if CPU(ARM_THUMB2)
template<typename T = void*>
T dataLocation() const
{
m_value.assertIsPoisoned();
ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
return bitwise_cast<T>(m_value ? m_value.unpoisoned<char*>() - 1 : nullptr);
}
#else
template<typename T = void*>
T dataLocation() const
{
m_value.assertIsPoisoned();
ASSERT_VALID_CODE_POINTER(m_value);
return untagCodePtr<T, tag>(m_value.unpoisoned());
}
#endif
bool operator!() const
{
#if ENABLE(POISON_ASSERTS)
if (!isEmptyValue() && !isDeletedValue())
m_value.assertIsPoisoned();
#endif
return !m_value;
}
explicit operator bool() const { return !(!*this); }
bool operator==(const MacroAssemblerCodePtr& other) const
{
#if ENABLE(POISON_ASSERTS)
if (!isEmptyValue() && !isDeletedValue())
m_value.assertIsPoisoned();
if (!other.isEmptyValue() && !other.isDeletedValue())
other.m_value.assertIsPoisoned();
#endif
return m_value == other.m_value;
}
template<typename T, typename = std::enable_if_t<!std::is_same<T, bool>::value>>
operator T() = delete;
void dumpWithName(const char* name, PrintStream& out) const
{
MacroAssemblerCodePtrBase::dumpWithName(executableAddress(), dataLocation(), name, out);
}
void dump(PrintStream& out) const { dumpWithName("CodePtr", out); }
enum EmptyValueTag { EmptyValue };
enum DeletedValueTag { DeletedValue };
MacroAssemblerCodePtr(EmptyValueTag)
: m_value(emptyValue())
{ }
MacroAssemblerCodePtr(DeletedValueTag)
: m_value(deletedValue())
{ }
bool isEmptyValue() const { return m_value == emptyValue(); }
bool isDeletedValue() const { return m_value == deletedValue(); }
unsigned hash() const { return IntHash<uintptr_t>::hash(m_value.bits()); }
static void initialize();
private:
static PoisonedMasmPtr emptyValue() { return PoisonedMasmPtr(AlreadyPoisoned, 1); }
static PoisonedMasmPtr deletedValue() { return PoisonedMasmPtr(AlreadyPoisoned, 2); }
PoisonedMasmPtr m_value;
};
template<PtrTag tag>
struct MacroAssemblerCodePtrHash {
static unsigned hash(const MacroAssemblerCodePtr<tag>& ptr) { return ptr.hash(); }
static bool equal(const MacroAssemblerCodePtr<tag>& a, const MacroAssemblerCodePtr<tag>& b)
{
return a == b;
}
static const bool safeToCompareToEmptyOrDeleted = true;
};
class MacroAssemblerCodeRefBase {
protected:
static bool tryToDisassemble(MacroAssemblerCodePtr<DisassemblyPtrTag>, size_t, const char* prefix, PrintStream& out);
static bool tryToDisassemble(MacroAssemblerCodePtr<DisassemblyPtrTag>, size_t, const char* prefix);
JS_EXPORT_PRIVATE static CString disassembly(MacroAssemblerCodePtr<DisassemblyPtrTag>, size_t);
};
template<PtrTag tag>
class MacroAssemblerCodeRef : private MacroAssemblerCodeRefBase {
private:
explicit MacroAssemblerCodeRef(MacroAssemblerCodePtr<tag> codePtr)
: m_codePtr(codePtr)
{
ASSERT(m_codePtr);
}
public:
MacroAssemblerCodeRef() = default;
MacroAssemblerCodeRef(Ref<ExecutableMemoryHandle>&& executableMemory)
: m_codePtr(executableMemory->start().retaggedPtr<tag>())
, m_executableMemory(WTFMove(executableMemory))
{
ASSERT(m_executableMemory->isManaged());
ASSERT(m_executableMemory->start());
ASSERT(m_codePtr);
}
static MacroAssemblerCodeRef createSelfManagedCodeRef(MacroAssemblerCodePtr<tag> codePtr)
{
return MacroAssemblerCodeRef(codePtr);
}
ExecutableMemoryHandle* executableMemory() const
{
return m_executableMemory.get();
}
MacroAssemblerCodePtr<tag> code() const
{
return m_codePtr;
}
template<PtrTag newTag>
MacroAssemblerCodePtr<newTag> retaggedCode() const
{
return m_codePtr.template retagged<newTag>();
}
template<PtrTag newTag>
MacroAssemblerCodeRef<newTag> retagged() const
{
return MacroAssemblerCodeRef<newTag>(*this);
}
size_t size() const
{
if (!m_executableMemory)
return 0;
return m_executableMemory->sizeInBytes();
}
bool tryToDisassemble(PrintStream& out, const char* prefix = "") const
{
return tryToDisassemble(retaggedCode<DisassemblyPtrTag>(), size(), prefix, out);
}
bool tryToDisassemble(const char* prefix = "") const
{
return tryToDisassemble(retaggedCode<DisassemblyPtrTag>(), size(), prefix);
}
CString disassembly() const
{
return MacroAssemblerCodeRefBase::disassembly(retaggedCode<DisassemblyPtrTag>(), size());
}
explicit operator bool() const { return !!m_codePtr; }
void dump(PrintStream& out) const
{
m_codePtr.dumpWithName("CodeRef", out);
}
private:
template<PtrTag otherTag>
MacroAssemblerCodeRef(const MacroAssemblerCodeRef<otherTag>& otherCodeRef)
: m_codePtr(MacroAssemblerCodePtr<tag>::createFromExecutableAddress(otherCodeRef.code().template retaggedExecutableAddress<tag>()))
, m_executableMemory(otherCodeRef.m_executableMemory)
{ }
MacroAssemblerCodePtr<tag> m_codePtr;
RefPtr<ExecutableMemoryHandle> m_executableMemory;
template<PtrTag> friend class MacroAssemblerCodeRef;
};
template<PtrTag tag>
inline FunctionPtr<tag>::FunctionPtr(MacroAssemblerCodePtr<tag> ptr)
: m_value(ptr.executableAddress())
{
PoisonedMasmPtr::assertIsNotPoisoned(m_value);
}
}
namespace WTF {
template<typename T> struct DefaultHash;
template<JSC::PtrTag tag> struct DefaultHash<JSC::MacroAssemblerCodePtr<tag>> {
typedef JSC::MacroAssemblerCodePtrHash<tag> Hash;
};
template<typename T> struct HashTraits;
template<JSC::PtrTag tag> struct HashTraits<JSC::MacroAssemblerCodePtr<tag>> : public CustomHashTraits<JSC::MacroAssemblerCodePtr<tag>> { };
}