Skip to content

Commit 812d504

Browse files
authored
Fix NativeAOT ThunksPool thunk data block size handling. (#111149)
Updated version of #110732 fixing issues on ARM32 and other platforms where size diffrence between code and data thunk caused thunk data block size calculations to be to small. #88710 made a change in TunkPool.cs moving away from using a page size define to calling ThunkBlockSize to get to end of thunk data block where a common stub address get stored. This change is not equivalent on platforms where the thunk blocks are laid out in pair where a stub thunk blocks are followed by a data thunk block and all gets mapped from file. This is the schema used on Windows platforms. In that layout schema the ThunkBlockSize is 2 * page size meaning that the calculation getting to the end of the thunk data block will move to the end of next thunk stub that is RX memory and storing the common stub address at that location will trigger an AV. This works on iOS since it reports its ThunkBlockSize as one page but that is not totally correct since it uses 2 pages, just that they are allocated in the same way as FEATURE_RX_THUNKS, all thunk stubs blocks followed by all thunk data blocks. The reason why this works is because it only maps the thunk stubs from file, reporting a ThunkBlockSize that is inline with what gets map:ed from file, but then there is a special handling in PalAllocateThunksFromTemplate on iOS that virutal alloc template size * 2, mapping the first template size bytes from the file and the rest are kept as its thunk data blocks. This commit calculates the page size of code/data block based on max of code/data thunk size * number of thunks per block and since this is guaranteed to fit into one block and that the block size needs to be a power of 2, the correct full block size used in arch specific implementation when laying out the stub and data blocks can be calculated directly in managed code. Review feedback. Switch to ThunkDataBlockSizeMask in one place. Fix build error. Include pointer size slot in data block size calculation. Calculate page size using NumThunksPerBlock.
1 parent c9f0d04 commit 812d504

File tree

1 file changed

+11
-10
lines changed
  • src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime

1 file changed

+11
-10
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
//
3636

3737
using System.Diagnostics;
38+
using System.Numerics;
3839

3940
namespace System.Runtime
4041
{
@@ -44,8 +45,8 @@ internal static class Constants
4445
public static readonly int ThunkCodeSize = RuntimeImports.RhpGetThunkSize();
4546
public static readonly int NumThunksPerBlock = RuntimeImports.RhpGetNumThunksPerBlock();
4647
public static readonly int NumThunkBlocksPerMapping = RuntimeImports.RhpGetNumThunkBlocksPerMapping();
47-
public static readonly uint ThunkBlockSize = (uint)RuntimeImports.RhpGetThunkBlockSize();
48-
public static readonly nuint ThunkBlockSizeMask = ThunkBlockSize - 1;
48+
public static readonly uint PageSize = BitOperations.RoundUpToPowerOf2((uint)Math.Max(ThunkCodeSize * NumThunksPerBlock, ThunkDataSize * NumThunksPerBlock + IntPtr.Size));
49+
public static readonly nuint PageSizeMask = PageSize - 1;
4950
}
5051

5152
internal class ThunksHeap
@@ -97,11 +98,11 @@ private unsafe ThunksHeap(IntPtr commonStubAddress)
9798
IntPtr thunkDataBlock = RuntimeImports.RhpGetThunkDataBlockAddress(thunkStubsBlock);
9899

99100
// Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
100-
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.ThunkBlockSize) == 0);
101+
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.PageSize) == 0);
101102

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

106107
// Set the head and end of the linked list
107108
_nextAvailableThunkPtr = thunkDataBlock;
@@ -153,11 +154,11 @@ private unsafe bool ExpandHeap()
153154
IntPtr thunkDataBlock = RuntimeImports.RhpGetThunkDataBlockAddress(thunkStubsBlock);
154155

155156
// Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
156-
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.ThunkBlockSize) == 0);
157+
Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.PageSize) == 0);
157158

158159
// Update the last pointer value in the thunks data section with the value of the common stub address
159-
*(IntPtr*)(thunkDataBlock + (int)(Constants.ThunkBlockSize - IntPtr.Size)) = _commonStubAddress;
160-
Debug.Assert(*(IntPtr*)(thunkDataBlock + (int)(Constants.ThunkBlockSize - IntPtr.Size)) == _commonStubAddress);
160+
*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) = _commonStubAddress;
161+
Debug.Assert(*(IntPtr*)(thunkDataBlock + (int)(Constants.PageSize - IntPtr.Size)) == _commonStubAddress);
161162

162163
// Link the last entry in the old list to the first entry in the new list
163164
*((IntPtr*)_lastThunkPtr) = thunkDataBlock;
@@ -210,7 +211,7 @@ public unsafe IntPtr AllocateThunk()
210211
*((IntPtr*)(nextAvailableThunkPtr + IntPtr.Size)) = IntPtr.Zero;
211212
#endif
212213

213-
int thunkIndex = (int)(((nuint)(nint)nextAvailableThunkPtr) - ((nuint)(nint)nextAvailableThunkPtr & ~Constants.ThunkBlockSizeMask));
214+
int thunkIndex = (int)(((nuint)(nint)nextAvailableThunkPtr) - ((nuint)(nint)nextAvailableThunkPtr & ~Constants.PageSizeMask));
214215
Debug.Assert((thunkIndex % Constants.ThunkDataSize) == 0);
215216
thunkIndex /= Constants.ThunkDataSize;
216217

@@ -266,7 +267,7 @@ private static IntPtr TryGetThunkDataAddress(IntPtr thunkAddress)
266267
nuint thunkAddressValue = (nuint)(nint)ClearThumbBit(thunkAddress);
267268

268269
// Compute the base address of the thunk's mapping
269-
nuint currentThunksBlockAddress = thunkAddressValue & ~Constants.ThunkBlockSizeMask;
270+
nuint currentThunksBlockAddress = thunkAddressValue & ~Constants.PageSizeMask;
270271

271272
// Make sure the thunk address is valid by checking alignment
272273
if ((thunkAddressValue - currentThunksBlockAddress) % (nuint)Constants.ThunkCodeSize != 0)

0 commit comments

Comments
 (0)