2525//
2626// With FEATURE_RX_THUNKS, thunks are created by allocating new virtual memory space, where the first half of
2727// that space is filled with thunk stubs, and gets RX permissions, and the second half is for the thunks data,
28- // and gets RW permissions. The thunk stubs and data blocks are not in groupped in pairs:
28+ // and gets RW permissions. The thunk stubs and data blocks are not grouped in pairs:
2929// all the thunk stubs blocks are groupped at the beginning of the allocated virtual memory space, and all the
3030// thunk data blocks are groupped in the second half of the virtual space.
3131//
@@ -40,20 +40,12 @@ namespace System.Runtime
4040{
4141 internal static class Constants
4242 {
43- #if TARGET_ARM64 && ( TARGET_OSX || TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS )
44- public const uint PageSize = 0x4000 ; // 16k
45- public const nuint PageSizeMask = 0x3FFF ;
46- #else
47- public const uint PageSize = 0x1000 ; // 4k
48- public const nuint PageSizeMask = 0xFFF ;
49- #endif
50- public const uint AllocationGranularity = 0x10000 ; // 64k
51- public const nuint AllocationGranularityMask = 0xFFFF ;
52-
5343 public static readonly int ThunkDataSize = 2 * IntPtr . Size ;
5444 public static readonly int ThunkCodeSize = InternalCalls . RhpGetThunkSize ( ) ;
5545 public static readonly int NumThunksPerBlock = InternalCalls . RhpGetNumThunksPerBlock ( ) ;
5646 public static readonly int NumThunkBlocksPerMapping = InternalCalls . RhpGetNumThunkBlocksPerMapping ( ) ;
47+ public static readonly uint ThunkBlockSize = ( uint ) InternalCalls . RhpGetThunkBlockSize ( ) ;
48+ public static readonly nuint ThunkBlockSizeMask = ThunkBlockSize - 1 ;
5749 }
5850
5951 internal class ThunksHeap
@@ -105,11 +97,11 @@ private unsafe ThunksHeap(IntPtr commonStubAddress)
10597 IntPtr thunkDataBlock = InternalCalls . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
10698
10799 // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
108- Debug . Assert ( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
100+ Debug . Assert ( ( ( nuint ) ( nint ) thunkDataBlock % Constants . ThunkBlockSize ) == 0 ) ;
109101
110102 // Update the last pointer value in the thunks data section with the value of the common stub address
111- * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = commonStubAddress;
112- Debug. Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == commonStubAddress ) ;
103+ * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . ThunkBlockSize - IntPtr . Size ) ) = commonStubAddress;
104+ Debug. Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . ThunkBlockSize - IntPtr . Size ) ) == commonStubAddress ) ;
113105
114106 // Set the head and end of the linked list
115107 _nextAvailableThunkPtr = thunkDataBlock ;
@@ -161,11 +153,11 @@ private unsafe bool ExpandHeap()
161153 IntPtr thunkDataBlock = InternalCalls . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
162154
163155 // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
164- Debug . Assert( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
156+ Debug . Assert( ( ( nuint ) ( nint ) thunkDataBlock % Constants . ThunkBlockSize ) == 0 ) ;
165157
166158 // Update the last pointer value in the thunks data section with the value of the common stub address
167- * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = _commonStubAddress;
168- Debug . Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == _commonStubAddress) ;
159+ * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . ThunkBlockSize - IntPtr . Size ) ) = _commonStubAddress;
160+ Debug . Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . ThunkBlockSize - IntPtr . Size ) ) == _commonStubAddress) ;
169161
170162 // Link the last entry in the old list to the first entry in the new list
171163 * ( ( IntPtr * ) _lastThunkPtr) = thunkDataBlock;
@@ -220,7 +212,7 @@ public unsafe IntPtr AllocateThunk()
220212 * ( ( IntPtr* ) ( nextAvailableThunkPtr + IntPtr. Size) ) = IntPtr. Zero;
221213#endif
222214
223- int thunkIndex = ( int ) ( ( ( nuint ) ( nint ) nextAvailableThunkPtr) - ( ( nuint ) ( nint ) nextAvailableThunkPtr & ~ Constants. PageSizeMask ) ) ;
215+ int thunkIndex = ( int ) ( ( ( nuint ) ( nint ) nextAvailableThunkPtr) - ( ( nuint ) ( nint ) nextAvailableThunkPtr & ~ Constants. ThunkBlockSizeMask ) ) ;
224216 Debug. Assert( ( thunkIndex % Constants. ThunkDataSize) == 0 ) ;
225217 thunkIndex /= Constants. ThunkDataSize;
226218
@@ -279,7 +271,7 @@ private static IntPtr TryGetThunkDataAddress(IntPtr thunkAddress)
279271 nuint thunkAddressValue = ( nuint ) ( nint ) ClearThumbBit( thunkAddress) ;
280272
281273 // Compute the base address of the thunk's mapping
282- nuint currentThunksBlockAddress = thunkAddressValue & ~ Constants. PageSizeMask ;
274+ nuint currentThunksBlockAddress = thunkAddressValue & ~ Constants. ThunkBlockSizeMask ;
283275
284276 // Make sure the thunk address is valid by checking alignment
285277 if ( ( thunkAddressValue - currentThunksBlockAddress) % ( nuint ) Constants. ThunkCodeSize != 0 )
0 commit comments