Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
[Merge chakra-core/ChakraCore@1e02d86605] [MERGE #3534 @MikeHolman] c…
Browse files Browse the repository at this point in the history
…hange how we shift addresses from JIT proc to be more futureproof

Merge pull request #3534 from MikeHolman:safeshift

Previously we were statically mandating what dll is used for fixing up JIT helper calls, but it is more resilient if we decide at runtime based on which dll the address falls into.
  • Loading branch information
chakrabot committed Aug 17, 2017
1 parent 4bb7a0e commit 1b94b02
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 146 deletions.
4 changes: 2 additions & 2 deletions deps/chakrashim/core/lib/Backend/InterpreterThunkEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,12 @@ void InterpreterThunkEmitter::FillBuffer(
#ifdef ASMJS_PLAT
if (asmJsThunk)
{
interpreterThunk = SHIFT_ADDR(threadContext, &Js::InterpreterStackFrame::InterpreterAsmThunk);
interpreterThunk = ShiftAddr(threadContext, &Js::InterpreterStackFrame::InterpreterAsmThunk);
}
else
#endif
{
interpreterThunk = SHIFT_ADDR(threadContext, &Js::InterpreterStackFrame::InterpreterThunk);
interpreterThunk = ShiftAddr(threadContext, &Js::InterpreterStackFrame::InterpreterThunk);
}


Expand Down
84 changes: 42 additions & 42 deletions deps/chakrashim/core/lib/Backend/JnHelperMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ GetMethodAddress(ThreadContextInfo * context, IR::HelperCallOpnd* opnd)

if (0 <= diagOpnd->m_argCount && diagOpnd->m_argCount <= LowererMDFinal::MaxArgumentsToHelper)
{
return SHIFT_ADDR(context, helperMethodWrappers[diagOpnd->m_argCount]);
return ShiftAddr(context, helperMethodWrappers[diagOpnd->m_argCount]);
}
else
{
Expand Down Expand Up @@ -175,109 +175,109 @@ DECLSPEC_GUARDIGNORE _NOINLINE intptr_t GetNonTableMethodAddress(ThreadContextI
#if defined(_M_IX86)
// TODO: OOP JIT, have some way to validate that these are all loaded from CRT
case HelperDirectMath_Acos:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_acos);
return ShiftAddr(context, (double(*)(double))__libm_sse2_acos);

case HelperDirectMath_Asin:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_asin);
return ShiftAddr(context, (double(*)(double))__libm_sse2_asin);

case HelperDirectMath_Atan:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_atan);
return ShiftAddr(context, (double(*)(double))__libm_sse2_atan);

case HelperDirectMath_Atan2:
return SHIFT_CRT_ADDR(context, (double(*)(double, double))__libm_sse2_atan2);
return ShiftAddr(context, (double(*)(double, double))__libm_sse2_atan2);

case HelperDirectMath_Cos:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_cos);
return ShiftAddr(context, (double(*)(double))__libm_sse2_cos);

case HelperDirectMath_Exp:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_exp);
return ShiftAddr(context, (double(*)(double))__libm_sse2_exp);

case HelperDirectMath_Log:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_log);
return ShiftAddr(context, (double(*)(double))__libm_sse2_log);

case HelperDirectMath_Sin:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_sin);
return ShiftAddr(context, (double(*)(double))__libm_sse2_sin);

case HelperDirectMath_Tan:
return SHIFT_CRT_ADDR(context, (double(*)(double))__libm_sse2_tan);
return ShiftAddr(context, (double(*)(double))__libm_sse2_tan);
#endif

case HelperDirectMath_FloorDb:
return SHIFT_CRT_ADDR(context, (double(*)(double))floor);
return ShiftAddr(context, (double(*)(double))floor);

case HelperDirectMath_CeilDb:
return SHIFT_CRT_ADDR(context, (double(*)(double))ceil);
return ShiftAddr(context, (double(*)(double))ceil);

//
// These are statically initialized to an import thunk, but let's keep them out of the table in case a new CRT changes this
//
case HelperWMemCmp:
return SHIFT_CRT_ADDR(context, (int(*)(const char16 *, const char16 *, size_t))wmemcmp);
return ShiftAddr(context, (int(*)(const char16 *, const char16 *, size_t))wmemcmp);

case HelperMemCpy:
return SHIFT_CRT_ADDR(context, (void*(*)(void *, void const*, size_t))memcpy);
return ShiftAddr(context, (void*(*)(void *, void const*, size_t))memcpy);

case HelperDirectMath_FloorFlt:
return SHIFT_CRT_ADDR(context, (float(*)(float))floor);
return ShiftAddr(context, (float(*)(float))floor);

case HelperDirectMath_CeilFlt:
return SHIFT_CRT_ADDR(context, (float(*)(float))ceil);
return ShiftAddr(context, (float(*)(float))ceil);

#if defined(_M_X64)
case HelperDirectMath_Acos:
return SHIFT_CRT_ADDR(context, (double(*)(double))acos);
return ShiftAddr(context, (double(*)(double))acos);

case HelperDirectMath_Asin:
return SHIFT_CRT_ADDR(context, (double(*)(double))asin);
return ShiftAddr(context, (double(*)(double))asin);

case HelperDirectMath_Atan:
return SHIFT_CRT_ADDR(context, (double(*)(double))atan);
return ShiftAddr(context, (double(*)(double))atan);

case HelperDirectMath_Atan2:
return SHIFT_CRT_ADDR(context, (double(*)(double, double))atan2);
return ShiftAddr(context, (double(*)(double, double))atan2);

case HelperDirectMath_Cos:
return SHIFT_CRT_ADDR(context, (double(*)(double))cos);
return ShiftAddr(context, (double(*)(double))cos);

case HelperDirectMath_Exp:
return SHIFT_CRT_ADDR(context, (double(*)(double))exp);
return ShiftAddr(context, (double(*)(double))exp);

case HelperDirectMath_Log:
return SHIFT_CRT_ADDR(context, (double(*)(double))log);
return ShiftAddr(context, (double(*)(double))log);

case HelperDirectMath_Sin:
return SHIFT_CRT_ADDR(context, (double(*)(double))sin);
return ShiftAddr(context, (double(*)(double))sin);

case HelperDirectMath_Tan:
return SHIFT_CRT_ADDR(context, (double(*)(double))tan);
return ShiftAddr(context, (double(*)(double))tan);

#elif defined(_M_ARM32_OR_ARM64)
case HelperDirectMath_Acos:
return SHIFT_CRT_ADDR(context, (double(*)(double))acos);
return ShiftAddr(context, (double(*)(double))acos);

case HelperDirectMath_Asin:
return SHIFT_CRT_ADDR(context, (double(*)(double))asin);
return ShiftAddr(context, (double(*)(double))asin);

case HelperDirectMath_Atan:
return SHIFT_CRT_ADDR(context, (double(*)(double))atan);
return ShiftAddr(context, (double(*)(double))atan);

case HelperDirectMath_Atan2:
return SHIFT_CRT_ADDR(context, (double(*)(double, double))atan2);
return ShiftAddr(context, (double(*)(double, double))atan2);

case HelperDirectMath_Cos:
return SHIFT_CRT_ADDR(context, (double(*)(double))cos);
return ShiftAddr(context, (double(*)(double))cos);

case HelperDirectMath_Exp:
return SHIFT_CRT_ADDR(context, (double(*)(double))exp);
return ShiftAddr(context, (double(*)(double))exp);

case HelperDirectMath_Log:
return SHIFT_CRT_ADDR(context, (double(*)(double))log);
return ShiftAddr(context, (double(*)(double))log);

case HelperDirectMath_Sin:
return SHIFT_CRT_ADDR(context, (double(*)(double))sin);
return ShiftAddr(context, (double(*)(double))sin);

case HelperDirectMath_Tan:
return SHIFT_CRT_ADDR(context, (double(*)(double))tan);
return ShiftAddr(context, (double(*)(double))tan);
#endif

//
Expand All @@ -290,28 +290,28 @@ DECLSPEC_GUARDIGNORE _NOINLINE intptr_t GetNonTableMethodAddress(ThreadContextI
#endif

case HelperOp_TryCatch:
return SHIFT_ADDR(context, Js::JavascriptExceptionOperators::OP_TryCatch);
return ShiftAddr(context, Js::JavascriptExceptionOperators::OP_TryCatch);

case HelperOp_TryFinally:
return SHIFT_ADDR(context, Js::JavascriptExceptionOperators::OP_TryFinally);
return ShiftAddr(context, Js::JavascriptExceptionOperators::OP_TryFinally);


case HelperOp_TryFinallySimpleJit:
return SHIFT_ADDR(context, Js::JavascriptExceptionOperators::OP_TryFinallySimpleJit);
return ShiftAddr(context, Js::JavascriptExceptionOperators::OP_TryFinallySimpleJit);

//
// Methods that we don't want to get marked as CFG targets as they dump all registers to a controlled address
//
case HelperSaveAllRegistersAndBailOut:
return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersAndBailOut);
return ShiftAddr(context, LinearScanMD::SaveAllRegistersAndBailOut);
case HelperSaveAllRegistersAndBranchBailOut:
return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersAndBranchBailOut);
return ShiftAddr(context, LinearScanMD::SaveAllRegistersAndBranchBailOut);

#ifdef _M_IX86
case HelperSaveAllRegistersNoSse2AndBailOut:
return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersNoSse2AndBailOut);
return ShiftAddr(context, LinearScanMD::SaveAllRegistersNoSse2AndBailOut);
case HelperSaveAllRegistersNoSse2AndBranchBailOut:
return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut);
return ShiftAddr(context, LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut);
#endif

}
Expand All @@ -337,7 +337,7 @@ intptr_t GetMethodOriginalAddress(ThreadContextInfo * context, JnHelperMethod he
return GetNonTableMethodAddress(context, helperMethod);
}

return SHIFT_ADDR(context, address);
return ShiftAddr(context, address);
}

#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
Expand Down
2 changes: 1 addition & 1 deletion deps/chakrashim/core/lib/Backend/ServerThreadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ServerThreadContext::GetRuntimeCRTBaseAddress() const
intptr_t
ServerThreadContext::GetJITCRTBaseAddress()
{
return (intptr_t)AutoSystemInfo::GetCRTHandle();
return (intptr_t)AutoSystemInfo::Data.GetCRTHandle();
}

PageAllocator *
Expand Down
4 changes: 2 additions & 2 deletions deps/chakrashim/core/lib/Backend/ServerThreadContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ServerThreadContext : public ThreadContextInfo

virtual bool IsNumericProperty(Js::PropertyId propId) override;

ptrdiff_t GetChakraBaseAddressDifference() const;
ptrdiff_t GetCRTBaseAddressDifference() const;
virtual ptrdiff_t GetChakraBaseAddressDifference() const override;
virtual ptrdiff_t GetCRTBaseAddressDifference() const override;

OOPCodeGenAllocators * GetCodeGenAllocators();
#if defined(_CONTROL_FLOW_GUARD) && (_M_IX86 || _M_X64)
Expand Down
20 changes: 20 additions & 0 deletions deps/chakrashim/core/lib/Common/Core/SysInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,31 @@ LPCWSTR AutoSystemInfo::GetJscriptDllFileName()
return (LPCWSTR)Data.binaryName;
}

#ifdef _WIN32
/* static */
HMODULE AutoSystemInfo::GetCRTHandle()
{
return GetModuleHandle(_u("msvcrt.dll"));
}

bool
AutoSystemInfo::IsCRTModulePointer(uintptr_t ptr)
{
HMODULE base = GetCRTHandle();
if (Data.crtSize == 0)
{
MODULEINFO info;
if (!GetModuleInformation(GetCurrentProcess(), base, &info, sizeof(MODULEINFO)))
{
AssertOrFailFast(UNREACHED);
}
Data.crtSize = info.SizeOfImage;
Assert(base == info.lpBaseOfDll);
}
return (ptr >= (uintptr_t)base && ptr < (uintptr_t)base + Data.crtSize);
}
#endif

bool AutoSystemInfo::IsLowMemoryProcess()
{
ULONG64 commit = ULONG64(-1);
Expand Down
11 changes: 9 additions & 2 deletions deps/chakrashim/core/lib/Common/Core/SysInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class AutoSystemInfo : public SYSTEM_INFO
DWORD GetNumberOfLogicalProcessors() const { return this->dwNumberOfProcessors; }
DWORD GetNumberOfPhysicalProcessors() const { return this->dwNumberOfPhysicalProcessors; }

#ifdef _WIN32
bool IsCRTModulePointer(uintptr_t ptr);
#endif

#if SYSINFO_IMAGE_BASE_AVAILABLE
UINT_PTR GetChakraBaseAddr() const;
#endif
Expand All @@ -49,12 +53,14 @@ class AutoSystemInfo : public SYSTEM_INFO
static DWORD SaveModuleFileName(HANDLE hMod);
static LPCWSTR GetJscriptDllFileName();
static HRESULT GetJscriptFileVersion(DWORD* majorVersion, DWORD* minorVersion, DWORD *buildDateHash = nullptr, DWORD *buildTimeHash = nullptr);
static HMODULE GetCRTHandle();
#if DBG
static bool IsInitialized();
#endif
#if SYSINFO_IMAGE_BASE_AVAILABLE
static bool IsJscriptModulePointer(void * ptr);
#endif
#ifdef _WIN32
static HMODULE GetCRTHandle();
#endif
static DWORD const PageSize = 4096;

Expand All @@ -78,11 +84,12 @@ class AutoSystemInfo : public SYSTEM_INFO
#endif

private:
AutoSystemInfo() : majorVersion(0), minorVersion(0), buildDateHash(0), buildTimeHash(0) { Initialize(); }
AutoSystemInfo() : majorVersion(0), minorVersion(0), buildDateHash(0), buildTimeHash(0), crtSize(0) { Initialize(); }
void Initialize();
bool isWindows8OrGreater;
uint allocationGranularityPageCount;
HANDLE processHandle;
DWORD crtSize;
#if defined(_M_IX86) || defined(_M_X64)
int CPUInfo[4];
#endif
Expand Down
2 changes: 1 addition & 1 deletion deps/chakrashim/core/lib/Runtime/Base/ThreadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ ThreadContext::EnsureJITThreadContext(bool allowPrereserveAlloc)

ThreadContextDataIDL contextData;
contextData.chakraBaseAddress = (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
contextData.crtBaseAddress = (intptr_t)AutoSystemInfo::GetCRTHandle();
contextData.crtBaseAddress = (intptr_t)AutoSystemInfo::Data.GetCRTHandle();
contextData.threadStackLimitAddr = reinterpret_cast<intptr_t>(GetAddressOfStackLimitForCurrentThread());
contextData.bailOutRegisterSaveSpaceAddr = (intptr_t)bailOutRegisterSaveSpace;
contextData.disableImplicitFlagsAddr = (intptr_t)GetAddressOfDisableImplicitFlags();
Expand Down
4 changes: 2 additions & 2 deletions deps/chakrashim/core/lib/Runtime/Base/ThreadContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1299,8 +1299,8 @@ class ThreadContext sealed :
virtual intptr_t GetDisableImplicitFlagsAddr() const override;
virtual intptr_t GetImplicitCallFlagsAddr() const override;

ptrdiff_t GetChakraBaseAddressDifference() const;
ptrdiff_t GetCRTBaseAddressDifference() const;
virtual ptrdiff_t GetChakraBaseAddressDifference() const override;
virtual ptrdiff_t GetCRTBaseAddressDifference() const override;

private:
void RegisterInlineCache(InlineCacheListMapByPropertyId& inlineCacheMap, Js::InlineCache* inlineCache, Js::PropertyId propertyId);
Expand Down
Loading

0 comments on commit 1b94b02

Please sign in to comment.