Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ca95d4b
Enable NativeAOT osx-arm64 builds
filipnavara Aug 31, 2022
35a40f0
Restrict alignment in VirtualReserveInner
filipnavara Aug 31, 2022
387b31b
Fix generation and handling of compact unwinding info on osx-arm64
filipnavara Aug 31, 2022
50eb6f6
Fix PC check in findFDE in DWARF CFI unwinding
filipnavara Sep 6, 2022
cb81686
Handle MAP_JIT for P/Invoke on osx-arm64
filipnavara Sep 7, 2022
69fd921
Handle P/Invoke with MAP_JIT on osx-arm64
filipnavara Sep 7, 2022
b05ba08
Fix incorrect OS_PAGE_SIZE definition
filipnavara Sep 7, 2022
8742268
Fix TLS register trashing
filipnavara Sep 8, 2022
92a7164
Fix memory trashing caused by incorrect PREPARE_EXTERNAL_VAR_INDIRECT…
filipnavara Sep 8, 2022
6325ba7
Ignore ESRCH in PalHijack (thread is already gone)
filipnavara Sep 8, 2022
125bed7
Make CompactUnwinder_* parametrized with registry class
filipnavara Sep 8, 2022
817b10c
Remove custom CompactUnwinder
filipnavara Sep 8, 2022
f44843f
Update src/coreclr/nativeaot/Runtime/CommonMacros.h
filipnavara Sep 8, 2022
3308493
Update llvm-libunwind-version.txt
filipnavara Sep 8, 2022
8b9d0dc
Fix initial alignment for __module_initializer. ARM64 requires absolu…
filipnavara Sep 9, 2022
9d73bfa
Allow publishing with the osx-arm64 RID
filipnavara Sep 9, 2022
5244049
Use pointer sized alignment for fat function pointers since they cont…
filipnavara Sep 9, 2022
59b79b3
Remove __builtin___clear_cache, it was already fixed
filipnavara Sep 11, 2022
d8c7ee0
Update src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
jkotas Sep 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle P/Invoke with MAP_JIT on osx-arm64
  • Loading branch information
filipnavara committed Sep 11, 2022
commit 69fd921dbbf1ecb45b30b763e92aa1ff089decf9
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ internal static extern unsafe bool RhpCallFilterFunclet(
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static extern IntPtr RhAllocateThunksMapping();

[DllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static extern void RhJitWriteProtect(int writeEnable);

// Enters a no GC region, possibly doing a blocking GC if there is not enough
// memory available to satisfy the caller's request.
[DllImport(Redhawk.BaseName)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,10 @@ private unsafe ThunksHeap(IntPtr commonStubAddress)
// Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.PageSize) == 0);

InternalCalls.RhJitWriteProtect(0);

// Update the last pointer value in the thunks data section with the value of the common stub address
*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) = commonStubAddress;
Debug.Assert(*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) == commonStubAddress);

InternalCalls.RhJitWriteProtect(1);

// Set the head and end of the linked list
_nextAvailableThunkPtr = thunkDataBlock;
_lastThunkPtr = _nextAvailableThunkPtr + Constants.ThunkDataSize * (Constants.NumThunksPerBlock - 1);
Expand Down Expand Up @@ -167,8 +163,6 @@ private unsafe bool ExpandHeap()
// Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.PageSize) == 0);

InternalCalls.RhJitWriteProtect(0);

// Update the last pointer value in the thunks data section with the value of the common stub address
*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) = _commonStubAddress;
Debug.Assert(*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) == _commonStubAddress);
Expand All @@ -179,8 +173,6 @@ private unsafe bool ExpandHeap()
// Update the pointer to the last entry in the list
_lastThunkPtr = *((IntPtr*)_lastThunkPtr) + Constants.ThunkDataSize * (Constants.NumThunksPerBlock - 1);

InternalCalls.RhJitWriteProtect(1);

newBlockInfo._blockBaseAddress = thunkStubsBlock;
newBlockInfo._nextBlock = _allocatedBlocks;

Expand Down Expand Up @@ -224,10 +216,8 @@ public unsafe IntPtr AllocateThunk()
Debug.Assert(nextAvailableThunkPtr != IntPtr.Zero);

#if DEBUG
InternalCalls.RhJitWriteProtect(0);
// Reset debug flag indicating the thunk is now in use
*((IntPtr*)(nextAvailableThunkPtr + IntPtr.Size)) = IntPtr.Zero;
InternalCalls.RhJitWriteProtect(1);
#endif

int thunkIndex = (int)(((nuint)(nint)nextAvailableThunkPtr) - ((nuint)(nint)nextAvailableThunkPtr & ~Constants.PageSizeMask));
Expand All @@ -252,12 +242,8 @@ public unsafe void FreeThunk(IntPtr thunkAddress)
if (!IsThunkInHeap(thunkAddress))
EH.FallbackFailFast(RhFailFastReason.InternalError, null);

InternalCalls.RhJitWriteProtect(0);

// Debug flag indicating the thunk is no longer used
*((IntPtr*)(dataAddress + IntPtr.Size)) = new IntPtr(-1);
#else
InternalCalls.RhJitWriteProtect(0);
#endif

InternalCalls.RhpAcquireThunkPoolLock();
Expand All @@ -266,8 +252,6 @@ public unsafe void FreeThunk(IntPtr thunkAddress)
_nextAvailableThunkPtr = dataAddress;

InternalCalls.RhpReleaseThunkPoolLock();

InternalCalls.RhJitWriteProtect(1);
}

private bool IsThunkInHeap(IntPtr thunkAddress)
Expand Down Expand Up @@ -329,14 +313,10 @@ public unsafe bool TryGetThunkData(IntPtr thunkAddress, out IntPtr context, out
if (!IsThunkInHeap(thunkAddress))
return false;

InternalCalls.RhJitWriteProtect(0);

// Update the data that will be used by the thunk that was allocated
context = *((IntPtr*)(dataAddress));
target = *((IntPtr*)(dataAddress + IntPtr.Size));

InternalCalls.RhJitWriteProtect(1);

return true;
}

Expand All @@ -357,13 +337,9 @@ public unsafe void SetThunkData(IntPtr thunkAddress, IntPtr context, IntPtr targ
EH.FallbackFailFast(RhFailFastReason.InternalError, null);
#endif

InternalCalls.RhJitWriteProtect(0);

// Update the data that will be used by the thunk that was allocated
*((IntPtr*)(dataAddress)) = context;
*((IntPtr*)(dataAddress + IntPtr.Size)) = target;

InternalCalls.RhJitWriteProtect(1);
}
}

Expand Down Expand Up @@ -420,8 +396,6 @@ public static unsafe IntPtr GetNewThunksBlock()
// Use the first data field of the thunk to build the linked list.
IntPtr dataAddress = InternalCalls.RhpGetThunkDataBlockAddress(nextThunksBlock);

InternalCalls.RhJitWriteProtect(0);

for (int i = 0; i < Constants.NumThunksPerBlock; i++)
{
if (i == (Constants.NumThunksPerBlock - 1))
Expand All @@ -437,8 +411,6 @@ public static unsafe IntPtr GetNewThunksBlock()
dataAddress += Constants.ThunkDataSize;
}

InternalCalls.RhJitWriteProtect(1);

return nextThunksBlock;
}

Expand Down
15 changes: 5 additions & 10 deletions src/coreclr/nativeaot/Runtime/ThunksMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ COOP_PINVOKE_HELPER(int, RhpGetThunkBlockSize, ())
return OS_PAGE_SIZE;
}

EXTERN_C NATIVEAOT_API void __cdecl RhJitWriteProtect(int writeEnable)
{
#if defined(HOST_OSX) && defined(HOST_ARM64)
pthread_jit_write_protect_np(writeEnable);
#endif
}

EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
{
#ifdef WIN32
Expand Down Expand Up @@ -128,8 +121,9 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
return NULL;
}

RhJitWriteProtect(0);

#if defined(HOST_OSX) && defined(HOST_ARM64)
pthread_jit_write_protect_np(0);
#endif
#endif

int numBlocksPerMap = RhpGetNumThunkBlocksPerMapping();
Expand Down Expand Up @@ -233,7 +227,8 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
}

#if defined(HOST_OSX) && defined(HOST_ARM64)
RhJitWriteProtect(1);
pthread_jit_write_protect_np(1);
__builtin___clear_cache((char*)pThunksSection, (char*)pThunksSection + THUNKS_MAP_SIZE);
#else
if (!PalVirtualProtect(pThunksSection, THUNKS_MAP_SIZE, PAGE_EXECUTE_READ))
{
Expand Down