@@ -44,6 +44,13 @@ using namespace CorUnix;
44
44
45
45
SET_DEFAULT_DEBUG_CHANNEL (VIRTUAL);
46
46
47
+ #include " pal/utils.h"
48
+
49
+ // This is temporary until #10981 merges.
50
+ // There will be an equivalent but opposite temporary fix in #10981 which
51
+ // will trigger a merge conflict to be sure both of these workarounds are removed
52
+ #define VirtualPageSize () VIRTUAL_PAGE_SIZE
53
+
47
54
//
48
55
// The mapping critical section guards access to the list
49
56
// of currently mapped views. If a thread needs to access
@@ -2012,14 +2019,14 @@ BOOL MAPGetRegionInfo(LPVOID lpAddress,
2012
2019
real_map_sz = pView->NumberOfBytesToMap ;
2013
2020
#endif
2014
2021
2015
- MappedSize = (( real_map_sz- 1 ) & ~VIRTUAL_PAGE_MASK) + VIRTUAL_PAGE_SIZE;
2022
+ MappedSize = ALIGN_UP ( real_map_sz, VirtualPageSize ());
2016
2023
if ( real_map_addr <= lpAddress &&
2017
2024
(VOID *)((UINT_PTR)real_map_addr+MappedSize) > lpAddress )
2018
2025
{
2019
2026
if (lpBuffer)
2020
2027
{
2021
- SIZE_T regionSize = MappedSize + (UINT_PTR) real_map_addr -
2022
- ((UINT_PTR) lpAddress & ~VIRTUAL_PAGE_MASK );
2028
+ SIZE_T regionSize = MappedSize + (UINT_PTR) real_map_addr -
2029
+ ALIGN_DOWN ((UINT_PTR)lpAddress, VirtualPageSize () );
2023
2030
2024
2031
lpBuffer->BaseAddress = lpAddress;
2025
2032
lpBuffer->AllocationProtect = 0 ;
@@ -2241,7 +2248,7 @@ MAPmmapAndRecord(
2241
2248
PAL_ERROR palError = NO_ERROR;
2242
2249
LPVOID pvBaseAddress = NULL ;
2243
2250
2244
- off_t adjust = offset & (VIRTUAL_PAGE_MASK );
2251
+ off_t adjust = offset & (VirtualPageSize () - 1 );
2245
2252
2246
2253
pvBaseAddress = mmap (static_cast <char *>(addr) - adjust, len + adjust, prot, flags, fd, offset - adjust);
2247
2254
if (MAP_FAILED == pvBaseAddress)
@@ -2410,7 +2417,7 @@ void * MAPMapPEFile(HANDLE hFile)
2410
2417
{
2411
2418
// if we're forcing relocs, create an anonymous mapping at the preferred base. Only create the
2412
2419
// mapping if we can create it at the specified address.
2413
- pForceRelocBase = mmap ( (void *)preferredBase, VIRTUAL_PAGE_SIZE , PROT_NONE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1 , 0 );
2420
+ pForceRelocBase = mmap ( (void *)preferredBase, VirtualPageSize () , PROT_NONE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1 , 0 );
2414
2421
if (pForceRelocBase == MAP_FAILED)
2415
2422
{
2416
2423
TRACE_ (LOADER)(" Attempt to take preferred base of %p to force relocation failed\n " , (void *)preferredBase);
@@ -2439,7 +2446,7 @@ void * MAPMapPEFile(HANDLE hFile)
2439
2446
// First try to reserve virtual memory using ExecutableAllcator. This allows all PE images to be
2440
2447
// near each other and close to the coreclr library which also allows the runtime to generate
2441
2448
// more efficient code (by avoiding usage of jump stubs).
2442
- loadedBase = ReserveMemoryFromExecutableAllocator (pThread, (( virtualSize- 1 ) & ~VIRTUAL_PAGE_MASK) + VIRTUAL_PAGE_SIZE );
2449
+ loadedBase = ReserveMemoryFromExecutableAllocator (pThread, ALIGN_UP ( virtualSize, VirtualPageSize ()) );
2443
2450
if (loadedBase == NULL )
2444
2451
{
2445
2452
// MAC64 requires we pass MAP_SHARED (or MAP_PRIVATE) flags - otherwise, the call is failed.
@@ -2462,7 +2469,7 @@ void * MAPMapPEFile(HANDLE hFile)
2462
2469
if (forceRelocs)
2463
2470
{
2464
2471
_ASSERTE (((SIZE_T)loadedBase) != preferredBase);
2465
- munmap (pForceRelocBase, VIRTUAL_PAGE_SIZE ); // now that we've forced relocation, let the original address mapping go
2472
+ munmap (pForceRelocBase, VirtualPageSize () ); // now that we've forced relocation, let the original address mapping go
2466
2473
}
2467
2474
if (((SIZE_T)loadedBase) != preferredBase)
2468
2475
{
@@ -2478,7 +2485,7 @@ void * MAPMapPEFile(HANDLE hFile)
2478
2485
// separately.
2479
2486
2480
2487
size_t headerSize;
2481
- headerSize = VIRTUAL_PAGE_SIZE ; // if there are lots of sections, this could be wrong
2488
+ headerSize = VirtualPageSize () ; // if there are lots of sections, this could be wrong
2482
2489
2483
2490
// first, map the PE header to the first page in the image. Get pointers to the section headers
2484
2491
palError = MAPmmapAndRecord (pFileObject, loadedBase,
@@ -2513,10 +2520,8 @@ void * MAPMapPEFile(HANDLE hFile)
2513
2520
goto doneReleaseMappingCriticalSection;
2514
2521
}
2515
2522
2516
- void * prevSectionBase;
2517
- prevSectionBase = loadedBase; // the first "section" for our purposes is the header
2518
- size_t prevSectionSizeInMemory;
2519
- prevSectionSizeInMemory = headerSize;
2523
+ void * prevSectionEnd;
2524
+ prevSectionEnd = (char *)loadedBase + headerSize; // the first "section" for our purposes is the header
2520
2525
for (unsigned i = 0 ; i < numSections; ++i)
2521
2526
{
2522
2527
// for each section, map the section of the file to the correct virtual offset. Gather the
@@ -2526,12 +2531,13 @@ void * MAPMapPEFile(HANDLE hFile)
2526
2531
IMAGE_SECTION_HEADER ¤tHeader = firstSection[i];
2527
2532
2528
2533
void * sectionBase = (char *)loadedBase + currentHeader.VirtualAddress ;
2534
+ void * sectionBaseAligned = ALIGN_DOWN (sectionBase, VirtualPageSize ());
2529
2535
2530
2536
// Validate the section header
2531
2537
if ( (sectionBase < loadedBase) // Did computing the section base overflow?
2532
2538
|| ((char *)sectionBase + currentHeader.SizeOfRawData < (char *)sectionBase) // Does the section overflow?
2533
2539
|| ((char *)sectionBase + currentHeader.SizeOfRawData > (char *)loadedBase + virtualSize) // Does the section extend past the end of the image as the header stated?
2534
- || (( char *)prevSectionBase + prevSectionSizeInMemory > sectionBase) // Does this section overlap the previous one?
2540
+ || (prevSectionEnd > sectionBase) // Does this section overlap the previous one?
2535
2541
)
2536
2542
{
2537
2543
ERROR_ (LOADER)( " section %d is corrupt\n " , i );
@@ -2546,13 +2552,12 @@ void * MAPMapPEFile(HANDLE hFile)
2546
2552
}
2547
2553
2548
2554
// Is there space between the previous section and this one? If so, add a PROT_NONE mapping to cover it.
2549
- if (( char *)prevSectionBase + prevSectionSizeInMemory < sectionBase )
2555
+ if (prevSectionEnd < sectionBaseAligned )
2550
2556
{
2551
- char * gapBase = (char *)prevSectionBase + prevSectionSizeInMemory;
2552
2557
palError = MAPRecordMapping (pFileObject,
2553
2558
loadedBase,
2554
- ( void *)gapBase ,
2555
- (char *)sectionBase - gapBase ,
2559
+ prevSectionEnd ,
2560
+ (char *)sectionBaseAligned - ( char *)prevSectionEnd ,
2556
2561
PROT_NONE);
2557
2562
if (NO_ERROR != palError)
2558
2563
{
@@ -2596,20 +2601,18 @@ void * MAPMapPEFile(HANDLE hFile)
2596
2601
}
2597
2602
#endif // _DEBUG
2598
2603
2599
- prevSectionBase = sectionBase;
2600
- prevSectionSizeInMemory = (currentHeader.SizeOfRawData + VIRTUAL_PAGE_MASK) & ~VIRTUAL_PAGE_MASK; // round up to page boundary
2604
+ prevSectionEnd = ALIGN_UP ((char *)sectionBase + currentHeader.SizeOfRawData , VirtualPageSize ()); // round up to page boundary
2601
2605
}
2602
2606
2603
2607
// Is there space after the last section and before the end of the mapped image? If so, add a PROT_NONE mapping to cover it.
2604
2608
char * imageEnd;
2605
2609
imageEnd = (char *)loadedBase + virtualSize; // actually, points just after the mapped end
2606
- if (( char *)prevSectionBase + prevSectionSizeInMemory < imageEnd)
2610
+ if (prevSectionEnd < imageEnd)
2607
2611
{
2608
- char * gapBase = (char *)prevSectionBase + prevSectionSizeInMemory;
2609
2612
palError = MAPRecordMapping (pFileObject,
2610
2613
loadedBase,
2611
- ( void *)gapBase ,
2612
- imageEnd - gapBase ,
2614
+ prevSectionEnd ,
2615
+ ( char *) imageEnd - ( char *)prevSectionEnd ,
2613
2616
PROT_NONE);
2614
2617
if (NO_ERROR != palError)
2615
2618
{
0 commit comments