Ignore:
Timestamp:
Oct 14, 1999, 7:15:50 PM (26 years ago)
Author:
phaller
Message:

Fix: a little cleanup

File:
1 edited

Legend:

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

    r1237 r1293  
    1 /* $Id: virtual.cpp,v 1.16 1999-10-09 22:28:59 phaller Exp $ */
     1/* $Id: virtual.cpp,v 1.17 1999-10-14 17:15:50 phaller Exp $ */
    22
    33/*
     
    280280  }
    281281
    282   if(fdwAllocationType & MEM_COMMIT) {
     282  if(fdwAllocationType & MEM_COMMIT)
     283  {
    283284        dprintf(("VirtualAlloc: commit\n"));
    284285        flag = PAG_COMMIT;
    285286  }
    286   if(fdwProtect & PAGE_READONLY)          flag |= PAG_READ;
    287   if(fdwProtect & PAGE_READWRITE)         flag |= (PAG_READ | PAG_WRITE);
    288   if(fdwProtect & PAGE_WRITECOPY)         flag |= (PAG_READ | PAG_WRITE);
     287
     288  if(fdwProtect & PAGE_READONLY)     flag |= PAG_READ;
     289  if(fdwProtect & PAGE_READWRITE)    flag |= (PAG_READ | PAG_WRITE);
     290  if(fdwProtect & PAGE_WRITECOPY)    flag |= (PAG_READ | PAG_WRITE);
    289291
    290292  if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
    291   if(fdwProtect & PAGE_EXECUTE_READ)      flag |= (PAG_EXECUTE | PAG_READ);
    292   if(fdwProtect & PAGE_EXECUTE)           flag |= PAG_EXECUTE;
    293 
    294   if(fdwProtect & PAGE_GUARD)             flag |= PAG_GUARD;
     293  if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
     294  if(fdwProtect & PAGE_EXECUTE)      flag |= PAG_EXECUTE;
     295
     296  if(fdwProtect & PAGE_GUARD)        flag |= PAG_GUARD;
    295297
    296298  //just do this if other options are used
    297299  if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
    298300  {
    299         dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
    300         flag |= PAG_READ | PAG_WRITE;
    301   }
    302 
    303   if(fdwAllocationType & MEM_COMMIT && lpvAddress != NULL)
     301    dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
     302    flag |= PAG_READ | PAG_WRITE;
     303  }
     304
     305  // commit memory
     306  if(fdwAllocationType & MEM_COMMIT)
    304307  {
    305308    Address = lpvAddress;
     
    372375
    373376  // verify parameters
    374   if ( (lpvAddress == NULL) ||
    375        ( (FreeType & MEM_RELEASE) &&
    376          (cbSize   != 0) )
    377      )
     377  if ( (FreeType & MEM_RELEASE) && (cbSize   != 0) )
    378378  {
    379379    SetLastError(ERROR_INVALID_PARAMETER);
     
    381381  }
    382382
    383   if(FreeType & MEM_DECOMMIT)
     383  if ( (FreeType & MEM_DECOMMIT) &&
     384       (FreeType & MEM_RELEASE) )
     385  {
     386    SetLastError(ERROR_INVALID_PARAMETER);
     387    return(FALSE);
     388  }
     389
     390  // decommit memory
     391  if (FreeType &  MEM_DECOMMIT)
     392  {
     393    // decommit memory block
    384394    rc = OSLibDosSetMem(lpvAddress, cbSize, PAG_DECOMMIT);
    385   else
    386     rc = OSLibDosFreeMem(lpvAddress);    //MEM_RELEASE, cbSize == 0 (or should be)
    387 
    388   if(rc)
    389   {
    390     dprintf(("KERNEL32:VirtualFree rc = #%d\n",
    391              rc));
    392     SetLastError(ERROR_GEN_FAILURE);
    393     return(FALSE);
     395    if(rc)
     396    {
     397      dprintf(("KERNEL32:VirtualFree:OsLibSetMem rc = #%d\n",
     398               rc));
     399      SetLastError(ERROR_INVALID_ADDRESS);
     400      return(FALSE);
     401    }
     402  }
     403
     404  // release memory
     405  if (FreeType &  MEM_RELEASE)
     406  {
     407    rc = OSLibDosFreeMem(lpvAddress); // free the memory block
     408    if(rc)
     409    {
     410      dprintf(("KERNEL32:VirtualFree:OsLibFreeMem rc = #%d\n",
     411               rc));
     412      SetLastError(ERROR_INVALID_ADDRESS);
     413      return(FALSE);
     414    }
    394415  }
    395416
     
    410431                                    DWORD*, pfdwOldProtect)
    411432{
    412  DWORD rc;
    413  ULONG  pageFlags = 0;
    414  int npages;
    415 
    416   dprintf(("VirtualProtect %X; %d bytes, new flags %X (%X)\n", (int)lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect));
     433  DWORD rc;
     434  ULONG  pageFlags = 0;
     435  int npages;
     436
    417437  if(pfdwOldProtect == NULL)
    418438        return(FALSE);
     
    480500                                   DWORD,   cbLength)
    481501{
    482   ULONG  cbRangeSize, dAttr;
    483   DWORD rc;
    484 
    485   if(lpvAddress == NULL || pmbiBuffer == NULL || cbLength == 0) {
    486         return 0;
    487   }
    488 
    489   cbRangeSize = cbLength & ~0xFFF;
    490   if(cbLength & 0xFFF) {
    491         cbRangeSize += PAGE_SIZE;
    492   }
    493   rc = OSLibDosQueryMem((LPVOID)lpvAddress, &cbRangeSize, &dAttr);
    494   if(rc) {
    495         dprintf(("VirtualQuery - DosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
    496         return 0;
    497   }
    498   memset(pmbiBuffer, 0, sizeof(MEMORY_BASIC_INFORMATION));
    499   pmbiBuffer->BaseAddress = (LPVOID)lpvAddress;
     502  ULONG  cbRangeSize,
     503         dAttr;
     504  DWORD  rc;
     505  LPVOID lpBase;
     506
     507  if(pmbiBuffer == NULL || cbLength == 0) // check parameters
     508  {
     509    return 0;                             // nothing to return
     510  }
     511
     512  // determine exact page range
     513  lpBase = (LPVOID)((ULONG)lpvAddress & 0xFFFFF000);
     514  cbRangeSize = cbLength & ~0x00000FFF;   // assuming intel page sizes :)
     515  if(cbLength & 0x00000FFF)
     516    cbRangeSize += PAGE_SIZE;
     517
     518  rc = OSLibDosQueryMem(lpBase,
     519                        &cbRangeSize,
     520                        &dAttr);
     521  if(rc)
     522  {
     523    dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
     524             lpBase,
     525             cbLength,
     526             rc));
     527    return 0;
     528  }
     529
     530  memset(pmbiBuffer,
     531         0,
     532         sizeof(MEMORY_BASIC_INFORMATION));
     533
     534  pmbiBuffer->BaseAddress = lpBase;
    500535  pmbiBuffer->RegionSize  = cbRangeSize;
     536
    501537  if(dAttr & PAG_READ && !(dAttr & PAG_WRITE))
    502         pmbiBuffer->Protect |= PAGE_READONLY;
     538    pmbiBuffer->Protect |= PAGE_READONLY;
     539
    503540  if(dAttr & PAG_WRITE)
    504         pmbiBuffer->Protect |= PAGE_READWRITE;
     541    pmbiBuffer->Protect |= PAGE_READWRITE;
    505542
    506543  if((dAttr & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
    507         pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE;
     544    pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE;
    508545  else
    509   if(dAttr & PAG_EXECUTE)
    510         pmbiBuffer->Protect |= PAGE_EXECUTE_READ;
     546    if(dAttr & PAG_EXECUTE)
     547      pmbiBuffer->Protect |= PAGE_EXECUTE_READ;
    511548
    512549  if(dAttr & PAG_GUARD)
    513         pmbiBuffer->Protect |= PAGE_GUARD;
     550    pmbiBuffer->Protect |= PAGE_GUARD;
    514551
    515552  if(dAttr & PAG_FREE)
    516         pmbiBuffer->State = MEM_FREE;
     553    pmbiBuffer->State = MEM_FREE;
    517554  else
    518   if(dAttr & PAG_COMMIT)
    519         pmbiBuffer->State = MEM_COMMIT;
    520   else  pmbiBuffer->State = MEM_RESERVE;
     555    if(dAttr & PAG_COMMIT)
     556      pmbiBuffer->State = MEM_COMMIT;
     557    else
     558      pmbiBuffer->State = MEM_RESERVE;
    521559
    522560  if(!(dAttr & PAG_SHARED))
    523         pmbiBuffer->Type = MEM_PRIVATE;
     561    pmbiBuffer->Type = MEM_PRIVATE;
    524562
    525563  //TODO: This is not correct: AllocationProtect should contain the protection
    526564  //      flags used in the initial call to VirtualAlloc
    527565  pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
    528   if(dAttr & PAG_BASE) {
    529         pmbiBuffer->AllocationBase = (LPVOID)lpvAddress;
    530   }
    531   else {
    532    while(lpvAddress > 0) {
    533       rc = OSLibDosQueryMem((LPVOID)lpvAddress, &cbRangeSize, &dAttr);
    534       if(rc) {
    535          dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
     566  if(dAttr & PAG_BASE)
     567    pmbiBuffer->AllocationBase = lpBase;
     568  else
     569  {
     570    while(lpvAddress > 0)
     571    {
     572      rc = OSLibDosQueryMem(lpBase, &cbRangeSize, &dAttr);
     573      if(rc)
     574      {
     575         dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
     576                  lpvAddress,
     577                  cbLength,
     578                  rc));
    536579         break;
    537580      }
    538       if(dAttr & PAG_BASE) {
    539          pmbiBuffer->AllocationBase = (LPVOID)lpvAddress;
     581      if(dAttr & PAG_BASE)
     582      {
     583         pmbiBuffer->AllocationBase = lpBase;
    540584         break;
    541585      }
    542       lpvAddress = (LPVOID)((ULONG)lpvAddress - PAGE_SIZE);
     586      lpvAddress = (LPVOID)((ULONG)lpBase - PAGE_SIZE);
    543587   }
    544588  }
     
    587631                                      LPDWORD, pfdwOldProtect)
    588632{
    589   return VirtualProtect(lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
     633  // only execute API, if this is the current process !
     634  if (GetCurrentProcess() == hProcess)
     635    return VirtualProtect(lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
     636  else
     637  {
     638    SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
     639    return FALSE;
     640  }
    590641}
    591642
     
    612663                                     DWORD,   cbLength)
    613664{
    614   return VirtualQuery(lpvAddress, pmbiBuffer, cbLength);
    615 }
     665  // only execute API, if this is the current process !
     666  if (GetCurrentProcess() == hProcess)
     667    return VirtualQuery(lpvAddress, pmbiBuffer, cbLength);
     668  else
     669  {
     670    SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
     671    return FALSE;
     672  }
     673}
Note: See TracChangeset for help on using the changeset viewer.