Ignore:
Timestamp:
Nov 22, 1999, 9:35:52 PM (26 years ago)
Author:
sandervl
Message:

Rewrite of PE loader code, EB's fixes + VirtualProtect bugfix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/mmap.cpp

    r1687 r1811  
    1 /* $Id: mmap.cpp,v 1.22 1999-11-10 14:16:01 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.23 1999-11-22 20:35:50 sandervl Exp $ */
    22
    33/*
     
    2929#include "mmap.h"
    3030#include "oslibdos.h"
     31#include <winimagepeldr.h>
    3132
    3233//Global DLL Data
     
    4243//******************************************************************************
    4344Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
    44                : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0)
     45               : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), image(0)
    4546{
    4647  globalmapMutex.enter();
     
    6061  }
    6162  else  lpszMapName = NULL;
     63}
     64//******************************************************************************
     65//Map constructor used for executable image maps (only used internally)
     66//******************************************************************************
     67Win32MemMap::Win32MemMap(Win32PeLdrImage *pImage, ULONG baseAddress, ULONG size)
     68               : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0)
     69{
     70  globalmapMutex.enter();
     71  next    = memmaps;
     72  memmaps = this;
     73  globalmapMutex.leave();
     74
     75  hMemFile   = -1;
     76
     77  mSize      = size;
     78  mProtFlags = PAGE_READWRITE;
     79  mProcessId = GetCurrentProcess();
     80
     81  pMapping   = (LPVOID)baseAddress;
     82
     83  image      = pImage;
     84  lpszMapName= NULL;
    6285}
    6386//******************************************************************************
     
    107130        free(lpszMapName);
    108131  }
    109   if(pMapping) {
     132  if(pMapping && !image) {
    110133        if(lpszMapName) {
    111134                OSLibDosFreeMem(pMapping);
     
    154177//  mapMutex.enter();
    155178
     179  if(image) {
     180        return image->commitPage(pageAddr, fWriteAccess);
     181  }
    156182  newProt  = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
    157183
     
    199225                        goto fail;
    200226                }
    201                 if(mProtFlags & PAGE_READONLY) {
     227                if(mProtFlags != PAGE_READWRITE) {
    202228                        if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
    203229                                goto fail;
     
    211237                goto fail;
    212238        }
    213         if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE) {
    214                 goto fail;
     239        if(!(memInfo.State & MEM_COMMIT))
     240        {//if it's already committed, then the app tried to write to it
     241                if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
     242                        goto fail;
    215243        }
    216244  }
     
    282310#endif
    283311
    284   if(nrMappings == 0) {//if not mapped, reserve/commit entire view
     312  //Memory has already been allocated for executable image maps (only used internally)
     313  if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
    285314        //SvL: Always read/write access or else ReadFile will crash once we
    286315        //     start committing pages.
     
    332361 ULONG nrBytesWritten, size;
    333362 int   i;
     363
     364  if(image) //no flushing for image maps
     365        return TRUE;
    334366
    335367  dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
     
    447479        nextmap = map->next;
    448480        if(map->getProcessId() == processId) {
    449                 CloseHandle(memmaps->hMemMap);
     481                //Delete map directly for executable images (only used internally)
     482                if(map->getImage()) {
     483                        delete map;
     484                }
     485                else    CloseHandle(memmaps->hMemMap);
    450486        }
    451487        else {
     
    492528  }
    493529 
    494   if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
    495         dprintf(("new OSLibDosAliasMem FAILED"));
    496         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
    497         errorState = 1;
    498         return;
     530  //view == memory mapping for executable images (only used internally)
     531  if(map->getImage()) {
     532        pMapView = map->getMappingAddr();
     533  }
     534  else {
     535        if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
     536                dprintf(("new OSLibDosAliasMem FAILED"));
     537                SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     538                errorState = 1;
     539                return;
     540        }
    499541  }
    500542
     
    530572        mParentMap->flushView(mOffset, mSize);
    531573
    532   OSLibDosFreeMem(pMapView);
     574  //Don't free memory for executable image map views (only used internally)
     575  if(!mParentMap->getImage())
     576        OSLibDosFreeMem(pMapView);
    533577
    534578  globalviewMutex.enter();
Note: See TracChangeset for help on using the changeset viewer.