Skip to content

Commit

Permalink
Reflect PR feedback and fix old macOS x64
Browse files Browse the repository at this point in the history
  • Loading branch information
janvorli committed Jul 8, 2021
1 parent f7e7363 commit 74c15e2
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/inc/CrstTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Crst Exception
End

Crst ExecutableAllocatorLock
Unordered
AcquiredAfter LoaderHeap
End

Crst ExecuteManRangeLock
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/clrconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ RETAIL_CONFIG_DWORD_INFO(UNSUPPORTED_LTTng, W("LTTng"), 1, "If COMPlus_LTTng is
//
// Executable code
//
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_EnableWXORX, W("EnableWXORX"), 1, "Enable W^X for executable memory.");
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_EnableWriteXorExecute, W("EnableWriteXorExecute"), 1, "Enable W^X for executable memory.");

#ifdef FEATURE_GDBJIT
///
Expand Down
40 changes: 20 additions & 20 deletions src/coreclr/inc/crsttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ int g_rgCrstLevelMap[] =
0, // CrstArgBasedStubCache
0, // CrstAssemblyList
12, // CrstAssemblyLoader
3, // CrstAvailableClass
4, // CrstAvailableParamTypes
4, // CrstAvailableClass
5, // CrstAvailableParamTypes
7, // CrstBaseDomain
-1, // CrstCCompRC
13, // CrstClassFactInfoHash
Expand All @@ -161,7 +161,7 @@ int g_rgCrstLevelMap[] =
6, // CrstCodeFragmentHeap
9, // CrstCodeVersioning
0, // CrstCOMCallWrapper
4, // CrstCOMWrapperCache
5, // CrstCOMWrapperCache
3, // CrstDataTest1
0, // CrstDataTest2
0, // CrstDbgTransport
Expand All @@ -180,10 +180,10 @@ int g_rgCrstLevelMap[] =
18, // CrstEventPipe
0, // CrstEventStore
0, // CrstException
-1, // CrstExecutableAllocatorLock
0, // CrstExecutableAllocatorLock
0, // CrstExecuteManRangeLock
0, // CrstExternalObjectContextCache
3, // CrstFCall
4, // CrstFCall
7, // CrstFuncPtrStubs
10, // CrstFusionAppCtx
10, // CrstGCCover
Expand All @@ -198,48 +198,48 @@ int g_rgCrstLevelMap[] =
3, // CrstInlineTrackingMap
17, // CrstInstMethodHashTable
20, // CrstInterop
4, // CrstInteropData
5, // CrstInteropData
0, // CrstIsJMCMethod
7, // CrstISymUnmanagedReader
11, // CrstJit
0, // CrstJitGenericHandleCache
16, // CrstJitInlineTrackingMap
3, // CrstJitPatchpoint
4, // CrstJitPatchpoint
-1, // CrstJitPerf
6, // CrstJumpStubCache
0, // CrstLeafLock
-1, // CrstListLock
15, // CrstLoaderAllocator
16, // CrstLoaderAllocatorReferences
0, // CrstLoaderHeap
3, // CrstLoaderHeap
3, // CrstManagedObjectWrapperMap
14, // CrstMethodDescBackpatchInfoTracker
4, // CrstModule
5, // CrstModule
15, // CrstModuleFixup
3, // CrstModuleLookupTable
4, // CrstModuleLookupTable
0, // CrstMulticoreJitHash
13, // CrstMulticoreJitManager
0, // CrstNativeImageEagerFixups
0, // CrstNativeImageLoad
0, // CrstNls
0, // CrstNotifyGdb
2, // CrstObjectList
4, // CrstPEImage
5, // CrstPEImage
19, // CrstPendingTypeLoadEntry
3, // CrstPgoData
4, // CrstPgoData
0, // CrstPinnedByrefValidation
0, // CrstProfilerGCRefDataFreeList
0, // CrstProfilingAPIStatus
3, // CrstRCWCache
4, // CrstRCWCache
0, // CrstRCWCleanupList
10, // CrstReadyToRunEntryPointToMethodDescMap
8, // CrstReflection
17, // CrstReJITGlobalRequest
3, // CrstRetThunkCache
4, // CrstRetThunkCache
3, // CrstSavedExceptionInfo
0, // CrstSaveModuleProfileData
0, // CrstSecurityStackwalkCache
3, // CrstSigConvert
4, // CrstSigConvert
5, // CrstSingleUseLock
0, // CrstSpecialStatics
0, // CrstStackSampler
Expand All @@ -249,7 +249,7 @@ int g_rgCrstLevelMap[] =
4, // CrstStubUnwindInfoHeapSegments
3, // CrstSyncBlockCache
0, // CrstSyncHashLock
4, // CrstSystemBaseDomain
5, // CrstSystemBaseDomain
13, // CrstSystemDomain
0, // CrstSystemDomainDelayedUnloadList
0, // CrstThreadIdDispenser
Expand All @@ -258,13 +258,13 @@ int g_rgCrstLevelMap[] =
13, // CrstThreadpoolWorker
12, // CrstThreadStore
8, // CrstTieredCompilation
3, // CrstTypeEquivalenceMap
4, // CrstTypeEquivalenceMap
10, // CrstTypeIDMap
3, // CrstUMEntryThunkCache
3, // CrstUniqueStack
4, // CrstUMEntryThunkCache
4, // CrstUniqueStack
7, // CrstUnresolvedClassLock
3, // CrstUnwindInfoTableLock
3, // CrstVSDIndirectionCellLock
4, // CrstVSDIndirectionCellLock
3, // CrstWrapperTemplate
};

Expand Down
41 changes: 40 additions & 1 deletion src/coreclr/minipal/Unix/doublemapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
#ifdef TARGET_LINUX
#include <linux/memfd.h>
#include <sys/syscall.h> // __NR_memfd_create
Expand Down Expand Up @@ -77,6 +78,39 @@ void VMToOSInterface::DestroyDoubleMemoryMapper(void *mapperHandle)

extern "C" void* PAL_VirtualReserveFromExecutableMemoryAllocatorWithinRange(const void* lpBeginAddress, const void* lpEndAddress, size_t dwSize);

#ifdef TARGET_OSX
bool IsMapJitFlagNeeded()
{
static volatile int isMapJitFlagNeeded = -1;

if (isMapJitFlagNeeded == -1)
{
int mapJitFlagCheckResult = 0;
int pageSize = sysconf(_SC_PAGE_SIZE);
// Try to map a page with read-write-execute protection. It should fail on Mojave hardened runtime and higher.
void* testPage = mmap(NULL, pageSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (testPage == MAP_FAILED && (errno == EACCES))
{
// The mapping has failed with EACCES, check if making the same mapping with MAP_JIT flag works
testPage = mmap(NULL, pageSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, -1, 0);
if (testPage != MAP_FAILED)
{
mapJitFlagCheckResult = 1;
}
}

if (testPage != MAP_FAILED)
{
munmap(testPage, pageSize);
}

isMapJitFlagNeeded = mapJitFlagCheckResult;
}

return (bool)isMapJitFlagNeeded;
}
#endif // TARGET_OSX

void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offset, size_t size, const void *rangeStart, const void* rangeEnd)
{
int fd = (int)(size_t)mapperHandle;
Expand All @@ -103,7 +137,12 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs
#ifndef TARGET_OSX
void* result = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, offset);
#else
void* result = mmap(NULL, size, PROT_NONE, MAP_JIT | MAP_ANON | MAP_PRIVATE, -1, 0);
int mmapFlags = MAP_ANON | MAP_PRIVATE;
if (IsMapJitFlagNeeded())
{
mmapFlags |= MAP_JIT;
}
void* result = mmap(NULL, size, PROT_NONE, mmapFlags, -1, 0);
#endif
if (result == MAP_FAILED)
{
Expand Down
41 changes: 0 additions & 41 deletions src/coreclr/minipal/Windows/doublemapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,6 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs
break;
}

#ifdef _DEBUG
// if (ShouldInjectFaultInRange())
// {
// // return nullptr (failure)
// faultInjected = true;
// break;
// }
#endif // _DEBUG

// On UNIX we can also fail if our request size 'dwSize' is larger than 64K and
// and our tryAddr is pointing at a small MEM_FREE region (smaller than 'dwSize')
// However we can't distinguish between this and the race case.

// We might fail in a race. So just move on to next region and continue trying
tryAddr = tryAddr + VIRTUAL_ALLOC_RESERVE_GRANULARITY;
}
Expand All @@ -187,35 +174,7 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs
}
}

// STRESS_LOG7(LF_JIT, LL_INFO100,
// "ClrVirtualAllocWithinRange request #%u for %08x bytes in [ %p .. %p ], query count was %u - returned %s: %p\n",
// countOfCalls, (DWORD)dwSize, pMinAddr, pMaxAddr,
// virtualQueryCount, (pResult != nullptr) ? "success" : "failure", pResult);

// If we failed this call the process will typically be terminated
// so we log any additional reason for failing this call.
//
if (pResult == nullptr)
{
// if ((tryAddr + dwSize) > (BYTE *)pMaxAddr)
// {
// // Our tryAddr reached pMaxAddr
// STRESS_LOG0(LF_JIT, LL_INFO100, "Additional reason: Address space exhausted.\n");
// }

// if (virtualQueryFailed)
// {
// STRESS_LOG0(LF_JIT, LL_INFO100, "Additional reason: VirtualQuery operation failed.\n");
// }

// if (faultInjected)
// {
// STRESS_LOG0(LF_JIT, LL_INFO100, "Additional reason: fault injected.\n");
// }
}

return pResult;

}

void *VMToOSInterface::CommitDoubleMappedMemory(void* pStart, size_t size, bool isExecutable)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/utilcode/executableallocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ HRESULT ExecutableAllocator::StaticInitialize(FatalErrorHandler fatalErrorHandle
LIMITED_METHOD_CONTRACT;

g_fatalErrorHandler = fatalErrorHandler;
g_isWXorXEnabled = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_EnableWXORX) != 0;
g_isWXorXEnabled = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_EnableWriteXorExecute) != 0;
g_instance = new (nothrow) ExecutableAllocator();
if (g_instance == NULL)
{
Expand Down
37 changes: 17 additions & 20 deletions src/coreclr/vm/jitinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,28 +339,25 @@ EXTERN_C FCDECL2_VV(UINT64, JIT_LRsz, UINT64 num, int shift);

#ifdef TARGET_X86

#define ENUM_X86_WRITE_BARRIER_REGISTERS() \
X86_WRITE_BARRIER_REGISTER(EAX) \
X86_WRITE_BARRIER_REGISTER(ECX) \
X86_WRITE_BARRIER_REGISTER(EBX) \
X86_WRITE_BARRIER_REGISTER(ESI) \
X86_WRITE_BARRIER_REGISTER(EDI) \
X86_WRITE_BARRIER_REGISTER(EBP)

extern "C"
{
void STDCALL JIT_CheckedWriteBarrierEAX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_CheckedWriteBarrierEBX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_CheckedWriteBarrierECX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_CheckedWriteBarrierESI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_CheckedWriteBarrierEDI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_CheckedWriteBarrierEBP(); // JIThelp.asm/JIThelp.s

void STDCALL JIT_DebugWriteBarrierEAX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_DebugWriteBarrierEBX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_DebugWriteBarrierECX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_DebugWriteBarrierESI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_DebugWriteBarrierEDI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_DebugWriteBarrierEBP(); // JIThelp.asm/JIThelp.s

void STDCALL JIT_WriteBarrierEAX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_WriteBarrierEBX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_WriteBarrierECX(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_WriteBarrierESI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_WriteBarrierEDI(); // JIThelp.asm/JIThelp.s
void STDCALL JIT_WriteBarrierEBP(); // JIThelp.asm/JIThelp.s

// JIThelp.asm/JIThelp.s
#define X86_WRITE_BARRIER_REGISTER(reg) \
void STDCALL JIT_CheckedWriteBarrier##reg(); \
void STDCALL JIT_DebugWriteBarrier##reg(); \
void STDCALL JIT_WriteBarrier##reg();

ENUM_X86_WRITE_BARRIER_REGISTERS()
#undef X86_WRITE_BARRIER_REGISTER

void STDCALL JIT_WriteBarrierGroup();
void STDCALL JIT_WriteBarrierGroup_End();
Expand Down
18 changes: 12 additions & 6 deletions src/coreclr/vm/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,16 +1177,20 @@ void InitThreadManager()
// can jump to it.
#ifdef TARGET_X86
JIT_WriteBarrierEAX_Loc = GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierEAX);
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_EAX, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierEAX));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_ECX, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierECX));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_EBX, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierEBX));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_ESI, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierESI));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_EDI, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierEDI));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_EBP, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrierEBP));

#define X86_WRITE_BARRIER_REGISTER(reg) \
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF_##reg, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrier##reg)); \
ETW::MethodLog::StubInitialized((ULONGLONG)GetWriteBarrierCodeLocation((void*)JIT_WriteBarrier##reg), W("@WriteBarrier" #reg));

ENUM_X86_WRITE_BARRIER_REGISTERS()

#undef X86_WRITE_BARRIER_REGISTER

#else // TARGET_X86
JIT_WriteBarrier_Loc = GetWriteBarrierCodeLocation((void*)JIT_WriteBarrier);
#endif // TARGET_X86
SetJitHelperFunction(CORINFO_HELP_ASSIGN_REF, GetWriteBarrierCodeLocation((void*)JIT_WriteBarrier));
ETW::MethodLog::StubInitialized((ULONGLONG)GetWriteBarrierCodeLocation((void*)JIT_WriteBarrier), W("@WriteBarrier"));

#ifdef TARGET_ARM64
// Store the JIT_WriteBarrier_Table copy location to a global variable so that it can be updated.
Expand All @@ -1195,7 +1199,9 @@ void InitThreadManager()

#if defined(TARGET_ARM64) || defined(TARGET_ARM)
SetJitHelperFunction(CORINFO_HELP_CHECKED_ASSIGN_REF, GetWriteBarrierCodeLocation((void*)JIT_CheckedWriteBarrier));
ETW::MethodLog::StubInitialized((ULONGLONG)GetWriteBarrierCodeLocation((void*)JIT_CheckedWriteBarrier), W("@CheckedWriteBarrier"));
SetJitHelperFunction(CORINFO_HELP_ASSIGN_BYREF, GetWriteBarrierCodeLocation((void*)JIT_ByRefWriteBarrier));
ETW::MethodLog::StubInitialized((ULONGLONG)GetWriteBarrierCodeLocation((void*)JIT_ByRefWriteBarrier), W("@ByRefWriteBarrier"));
#endif // TARGET_ARM64 || TARGET_ARM

}
Expand Down

0 comments on commit 74c15e2

Please sign in to comment.