@@ -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 GetVirtualPageSize () 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, GetVirtualPageSize ());
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, GetVirtualPageSize () );
2023
2030
2024
2031
lpBuffer->BaseAddress = lpAddress;
2025
2032
lpBuffer->AllocationProtect = 0 ;
@@ -2241,7 +2248,9 @@ MAPmmapAndRecord(
2241
2248
PAL_ERROR palError = NO_ERROR;
2242
2249
LPVOID pvBaseAddress = NULL ;
2243
2250
2244
- pvBaseAddress = mmap (addr, len, prot, flags, fd, offset);
2251
+ off_t adjust = offset & (GetVirtualPageSize () - 1 );
2252
+
2253
+ pvBaseAddress = mmap (static_cast <char *>(addr) - adjust, len + adjust, prot, flags, fd, offset - adjust);
2245
2254
if (MAP_FAILED == pvBaseAddress)
2246
2255
{
2247
2256
ERROR_ (LOADER)( " mmap failed with code %d: %s.\n " , errno, strerror ( errno ) );
@@ -2368,14 +2377,6 @@ void * MAPMapPEFile(HANDLE hFile)
2368
2377
goto done;
2369
2378
}
2370
2379
2371
- // this code requires that the file alignment be the same as the page alignment
2372
- if (ntHeader.OptionalHeader .FileAlignment < VIRTUAL_PAGE_SIZE)
2373
- {
2374
- ERROR_ (LOADER)( " Optional header file alignment is bad\n " );
2375
- palError = ERROR_INVALID_PARAMETER;
2376
- goto done;
2377
- }
2378
-
2379
2380
// This doesn't read the entire NT header (the optional header technically has a variable length. But I
2380
2381
// don't need more directories.
2381
2382
@@ -2416,7 +2417,7 @@ void * MAPMapPEFile(HANDLE hFile)
2416
2417
{
2417
2418
// if we're forcing relocs, create an anonymous mapping at the preferred base. Only create the
2418
2419
// mapping if we can create it at the specified address.
2419
- pForceRelocBase = mmap ( (void *)preferredBase, VIRTUAL_PAGE_SIZE , PROT_NONE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1 , 0 );
2420
+ pForceRelocBase = mmap ( (void *)preferredBase, GetVirtualPageSize () , PROT_NONE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1 , 0 );
2420
2421
if (pForceRelocBase == MAP_FAILED)
2421
2422
{
2422
2423
TRACE_ (LOADER)(" Attempt to take preferred base of %p to force relocation failed\n " , (void *)preferredBase);
@@ -2445,7 +2446,7 @@ void * MAPMapPEFile(HANDLE hFile)
2445
2446
// First try to reserve virtual memory using ExecutableAllcator. This allows all PE images to be
2446
2447
// near each other and close to the coreclr library which also allows the runtime to generate
2447
2448
// more efficient code (by avoiding usage of jump stubs).
2448
- loadedBase = ReserveMemoryFromExecutableAllocator (pThread, virtualSize);
2449
+ loadedBase = ReserveMemoryFromExecutableAllocator (pThread, ALIGN_UP ( virtualSize, GetVirtualPageSize ()) );
2449
2450
if (loadedBase == NULL )
2450
2451
{
2451
2452
// MAC64 requires we pass MAP_SHARED (or MAP_PRIVATE) flags - otherwise, the call is failed.
@@ -2468,7 +2469,7 @@ void * MAPMapPEFile(HANDLE hFile)
2468
2469
if (forceRelocs)
2469
2470
{
2470
2471
_ASSERTE (((SIZE_T)loadedBase) != preferredBase);
2471
- munmap (pForceRelocBase, VIRTUAL_PAGE_SIZE ); // now that we've forced relocation, let the original address mapping go
2472
+ munmap (pForceRelocBase, GetVirtualPageSize () ); // now that we've forced relocation, let the original address mapping go
2472
2473
}
2473
2474
if (((SIZE_T)loadedBase) != preferredBase)
2474
2475
{
@@ -2484,7 +2485,7 @@ void * MAPMapPEFile(HANDLE hFile)
2484
2485
// separately.
2485
2486
2486
2487
size_t headerSize;
2487
- headerSize = VIRTUAL_PAGE_SIZE ; // if there are lots of sections, this could be wrong
2488
+ headerSize = GetVirtualPageSize () ; // if there are lots of sections, this could be wrong
2488
2489
2489
2490
// first, map the PE header to the first page in the image. Get pointers to the section headers
2490
2491
palError = MAPmmapAndRecord (pFileObject, loadedBase,
@@ -2519,10 +2520,8 @@ void * MAPMapPEFile(HANDLE hFile)
2519
2520
goto doneReleaseMappingCriticalSection;
2520
2521
}
2521
2522
2522
- void * prevSectionBase;
2523
- prevSectionBase = loadedBase; // the first "section" for our purposes is the header
2524
- size_t prevSectionSizeInMemory;
2525
- prevSectionSizeInMemory = headerSize;
2523
+ void * prevSectionEnd;
2524
+ prevSectionEnd = (char *)loadedBase + headerSize; // the first "section" for our purposes is the header
2526
2525
for (unsigned i = 0 ; i < numSections; ++i)
2527
2526
{
2528
2527
// for each section, map the section of the file to the correct virtual offset. Gather the
@@ -2532,12 +2531,13 @@ void * MAPMapPEFile(HANDLE hFile)
2532
2531
IMAGE_SECTION_HEADER ¤tHeader = firstSection[i];
2533
2532
2534
2533
void * sectionBase = (char *)loadedBase + currentHeader.VirtualAddress ;
2534
+ void * sectionBaseAligned = ALIGN_DOWN (sectionBase, GetVirtualPageSize ());
2535
2535
2536
2536
// Validate the section header
2537
2537
if ( (sectionBase < loadedBase) // Did computing the section base overflow?
2538
2538
|| ((char *)sectionBase + currentHeader.SizeOfRawData < (char *)sectionBase) // Does the section overflow?
2539
2539
|| ((char *)sectionBase + currentHeader.SizeOfRawData > (char *)loadedBase + virtualSize) // Does the section extend past the end of the image as the header stated?
2540
- || (( char *)prevSectionBase + prevSectionSizeInMemory > sectionBase) // Does this section overlap the previous one?
2540
+ || (prevSectionEnd > sectionBase) // Does this section overlap the previous one?
2541
2541
)
2542
2542
{
2543
2543
ERROR_ (LOADER)( " section %d is corrupt\n " , i );
@@ -2552,13 +2552,12 @@ void * MAPMapPEFile(HANDLE hFile)
2552
2552
}
2553
2553
2554
2554
// Is there space between the previous section and this one? If so, add a PROT_NONE mapping to cover it.
2555
- if (( char *)prevSectionBase + prevSectionSizeInMemory < sectionBase )
2555
+ if (prevSectionEnd < sectionBaseAligned )
2556
2556
{
2557
- char * gapBase = (char *)prevSectionBase + prevSectionSizeInMemory;
2558
2557
palError = MAPRecordMapping (pFileObject,
2559
2558
loadedBase,
2560
- ( void *)gapBase ,
2561
- (char *)sectionBase - gapBase ,
2559
+ prevSectionEnd ,
2560
+ (char *)sectionBaseAligned - ( char *)prevSectionEnd ,
2562
2561
PROT_NONE);
2563
2562
if (NO_ERROR != palError)
2564
2563
{
@@ -2602,20 +2601,18 @@ void * MAPMapPEFile(HANDLE hFile)
2602
2601
}
2603
2602
#endif // _DEBUG
2604
2603
2605
- prevSectionBase = sectionBase;
2606
- prevSectionSizeInMemory = (currentHeader.SizeOfRawData + VIRTUAL_PAGE_MASK) & ~VIRTUAL_PAGE_MASK; // round up to page boundary
2604
+ prevSectionEnd = ALIGN_UP ((char *)sectionBase + currentHeader.SizeOfRawData , GetVirtualPageSize ()); // round up to page boundary
2607
2605
}
2608
2606
2609
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.
2610
2608
char * imageEnd;
2611
2609
imageEnd = (char *)loadedBase + virtualSize; // actually, points just after the mapped end
2612
- if (( char *)prevSectionBase + prevSectionSizeInMemory < imageEnd)
2610
+ if (prevSectionEnd < imageEnd)
2613
2611
{
2614
- char * gapBase = (char *)prevSectionBase + prevSectionSizeInMemory;
2615
2612
palError = MAPRecordMapping (pFileObject,
2616
2613
loadedBase,
2617
- ( void *)gapBase ,
2618
- imageEnd - gapBase ,
2614
+ prevSectionEnd ,
2615
+ ( char *) imageEnd - ( char *)prevSectionEnd ,
2619
2616
PROT_NONE);
2620
2617
if (NO_ERROR != palError)
2621
2618
{
0 commit comments