Changeset 1293 for trunk/src/kernel32/virtual.cpp
- Timestamp:
- Oct 14, 1999, 7:15:50 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/virtual.cpp
r1237 r1293 1 /* $Id: virtual.cpp,v 1.1 6 1999-10-09 22:28:59phaller Exp $ */1 /* $Id: virtual.cpp,v 1.17 1999-10-14 17:15:50 phaller Exp $ */ 2 2 3 3 /* … … 280 280 } 281 281 282 if(fdwAllocationType & MEM_COMMIT) { 282 if(fdwAllocationType & MEM_COMMIT) 283 { 283 284 dprintf(("VirtualAlloc: commit\n")); 284 285 flag = PAG_COMMIT; 285 286 } 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); 289 291 290 292 if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ); 291 if(fdwProtect & PAGE_EXECUTE_READ) 292 if(fdwProtect & PAGE_EXECUTE) 293 294 if(fdwProtect & PAGE_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; 295 297 296 298 //just do this if other options are used 297 299 if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0) 298 300 { 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) 304 307 { 305 308 Address = lpvAddress; … … 372 375 373 376 // verify parameters 374 if ( (lpvAddress == NULL) || 375 ( (FreeType & MEM_RELEASE) && 376 (cbSize != 0) ) 377 ) 377 if ( (FreeType & MEM_RELEASE) && (cbSize != 0) ) 378 378 { 379 379 SetLastError(ERROR_INVALID_PARAMETER); … … 381 381 } 382 382 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 384 394 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 } 394 415 } 395 416 … … 410 431 DWORD*, pfdwOldProtect) 411 432 { 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 417 437 if(pfdwOldProtect == NULL) 418 438 return(FALSE); … … 480 500 DWORD, cbLength) 481 501 { 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; 500 535 pmbiBuffer->RegionSize = cbRangeSize; 536 501 537 if(dAttr & PAG_READ && !(dAttr & PAG_WRITE)) 502 pmbiBuffer->Protect |= PAGE_READONLY; 538 pmbiBuffer->Protect |= PAGE_READONLY; 539 503 540 if(dAttr & PAG_WRITE) 504 541 pmbiBuffer->Protect |= PAGE_READWRITE; 505 542 506 543 if((dAttr & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE)) 507 544 pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE; 508 545 else 509 if(dAttr & PAG_EXECUTE)510 546 if(dAttr & PAG_EXECUTE) 547 pmbiBuffer->Protect |= PAGE_EXECUTE_READ; 511 548 512 549 if(dAttr & PAG_GUARD) 513 550 pmbiBuffer->Protect |= PAGE_GUARD; 514 551 515 552 if(dAttr & PAG_FREE) 516 553 pmbiBuffer->State = MEM_FREE; 517 554 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; 521 559 522 560 if(!(dAttr & PAG_SHARED)) 523 561 pmbiBuffer->Type = MEM_PRIVATE; 524 562 525 563 //TODO: This is not correct: AllocationProtect should contain the protection 526 564 // flags used in the initial call to VirtualAlloc 527 565 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)); 536 579 break; 537 580 } 538 if(dAttr & PAG_BASE) { 539 pmbiBuffer->AllocationBase = (LPVOID)lpvAddress; 581 if(dAttr & PAG_BASE) 582 { 583 pmbiBuffer->AllocationBase = lpBase; 540 584 break; 541 585 } 542 lpvAddress = (LPVOID)((ULONG)lp vAddress- PAGE_SIZE);586 lpvAddress = (LPVOID)((ULONG)lpBase - PAGE_SIZE); 543 587 } 544 588 } … … 587 631 LPDWORD, pfdwOldProtect) 588 632 { 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 } 590 641 } 591 642 … … 612 663 DWORD, cbLength) 613 664 { 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.