Changeset 22029


Ignore:
Timestamp:
Oct 22, 2012, 1:50:20 PM (8 years ago)
Author:
dmik
Message:

pe: Fix memory allocation error for some Win32 EXEs.

This error could be displayed if the size of the EXE was too small.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/peldr/pe.c

    r22028 r22029  
    405405    ULONG  ulSysinfo, flAllocMem = 0;
    406406    BOOL   ret = FALSE;
    407 
    408     //Reserve enough space to store 4096 pointers to 1MB memory chunks
    409     static ULONG memallocs[4096];
     407    ULONG  allocSize = FALLOC_SIZE;
     408
     409    // Reserve enough space to store 4096 pointers to 1MB memory chunks +
     410    // 16 pointers to 64K memory chunks (1MB) for the extra loop (see below)
     411    static ULONG memallocs[4096 + 16];
    410412
    411413    *fNEExe = FALSE;
     
    445447    DBG(("PE: AllocateExeMem: oh.Subsystem: %d\n", oh.Subsystem));
    446448    DBG(("PE: AllocateExeMem: oh.ImageBase: 0x%08X\n", oh.ImageBase));
     449    DBG(("PE: AllocateExeMem: oh.SizeOfImage: 0x%08X\n", oh.SizeOfImage));
    447450
    448451    // check for high memory support
     
    464467    }
    465468    while(TRUE) {
    466         rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
     469        rc = DosAllocMem((PPVOID)&address, allocSize, PAG_READ | flAllocMem);
    467470        if(rc) break;
    468471
    469         if(address + FALLOC_SIZE >= oh.ImageBase) {
     472        if(address + allocSize >= oh.ImageBase) {
    470473            if(address > oh.ImageBase) {//we've passed it!
    471474                DosFreeMem((PVOID)address);
     
    483486            if(rc) break;
    484487
     488            // Sometimes it's possible that a smaller block of memory enough to
     489            // fit SizeOfImage is available below the target base address which
     490            // will be skipped by the loop allocating memory in FALLOC_SIZE
     491            // chunks when FALLOC_SIZE is greater than SizeOfImage. Continue
     492            // allocation in smaller chunks in this case to get a perfect match.
     493            if (baseAddress != oh.ImageBase) {
     494                // save already allocated blocks for further release
     495                memallocs[alloccnt++] = diff;
     496                memallocs[alloccnt++] = baseAddress;
     497                // set the exact chunk size
     498                allocSize = oh.SizeOfImage;
     499                continue;
     500            }
     501
    485502            if(diff) DosFreeMem((PVOID)address);
    486503
     
    493510        DosFreeMem((PVOID)memallocs[i]);
    494511    }
    495     DBG(("PE: AllocateExeMem: reservedMemory: 0x%08X\n", reservedMemory));
    496     ret = TRUE;
     512    ret = rc == 0;
    497513end:
    498514    if(dllfile) DosClose(dllfile);
     515    DBG(("PE: AllocateExeMem: reservedMemory: 0x%08X\n", reservedMemory));
    499516    DBG(("PE: AllocateExeMem: returning %d\n", ret));
    500517    return ret;
Note: See TracChangeset for help on using the changeset viewer.