Changeset 100315 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Jun 28, 2023 10:31:50 AM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/memsafer-r3.cpp
r98103 r100315 52 52 #include <iprt/param.h> 53 53 #include <iprt/string.h> 54 #include <iprt/system.h> 54 55 #ifdef IN_SUP_R3 55 56 # include <VBox/sup.h> … … 270 271 static void rtMemSaferInitializePages(PRTMEMSAFERNODE pThis, void *pvPages) 271 272 { 272 RTRandBytes(pvPages, PAGE_SIZE + pThis->offUser); 273 274 uint8_t *pbUser = (uint8_t *)pvPages + PAGE_SIZE + pThis->offUser; 273 uint32_t const cbPage = RTSystemGetPageSize(); 274 RTRandBytes(pvPages, cbPage + pThis->offUser); 275 276 uint8_t *pbUser = (uint8_t *)pvPages + cbPage + pThis->offUser; 275 277 pThis->Core.Key = pbUser; 276 278 RT_BZERO(pbUser, pThis->cbUser); /* paranoia */ 277 279 278 RTRandBytes(pbUser + pThis->cbUser, (size_t)pThis->cPages * PAGE_SIZE - PAGE_SIZE- pThis->offUser - pThis->cbUser);280 RTRandBytes(pbUser + pThis->cbUser, (size_t)pThis->cPages * cbPage - cbPage - pThis->offUser - pThis->cbUser); 279 281 } 280 282 … … 312 314 * SUPR3PageProtect isn't supported on all hosts, we ignore that. 313 315 */ 314 rc = SUPR3PageProtect(pvPages, NIL_RTR0PTR, 0, PAGE_SIZE, RTMEM_PROT_NONE); 316 uint32_t const cbPage = RTSystemGetPageSize(); 317 rc = SUPR3PageProtect(pvPages, NIL_RTR0PTR, 0, cbPage, RTMEM_PROT_NONE); 315 318 if (RT_SUCCESS(rc)) 316 319 { 317 rc = SUPR3PageProtect(pvPages, NIL_RTR0PTR, (pThis->cPages - 1) * PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE);320 rc = SUPR3PageProtect(pvPages, NIL_RTR0PTR, (pThis->cPages - 1) * cbPage, cbPage, RTMEM_PROT_NONE); 318 321 if (RT_SUCCESS(rc)) 319 322 return VINF_SUCCESS; 320 SUPR3PageProtect(pvPages, NIL_RTR0PTR, 0, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);323 SUPR3PageProtect(pvPages, NIL_RTR0PTR, 0, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 321 324 } 322 325 else if (rc == VERR_NOT_SUPPORTED) … … 348 351 * Try allocate the memory. 349 352 */ 353 uint32_t const cbPage = RTSystemGetPageSize(); 350 354 int rc = VINF_SUCCESS; 351 void *pvPages = RTMemPageAllocEx((size_t)pThis->cPages * PAGE_SIZE,355 void *pvPages = RTMemPageAllocEx((size_t)pThis->cPages * cbPage, 352 356 RTMEMPAGEALLOC_F_ADVISE_LOCKED | RTMEMPAGEALLOC_F_ADVISE_NO_DUMP | RTMEMPAGEALLOC_F_ZERO); 353 357 if (pvPages) … … 358 362 * Configure the guard pages. 359 363 */ 360 rc = RTMemProtect(pvPages, PAGE_SIZE, RTMEM_PROT_NONE);364 rc = RTMemProtect(pvPages, cbPage, RTMEM_PROT_NONE); 361 365 if (RT_SUCCESS(rc)) 362 366 { 363 rc = RTMemProtect((uint8_t *)pvPages + (size_t)(pThis->cPages - 1U) * PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE);367 rc = RTMemProtect((uint8_t *)pvPages + (size_t)(pThis->cPages - 1U) * cbPage, cbPage, RTMEM_PROT_NONE); 364 368 if (RT_SUCCESS(rc)) 365 369 return VINF_SUCCESS; 366 rc = RTMemProtect(pvPages, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);370 rc = RTMemProtect(pvPages, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 367 371 } 368 372 369 373 /* failed. */ 370 RTMemPageFree(pvPages, (size_t)pThis->cPages * PAGE_SIZE);374 RTMemPageFree(pvPages, (size_t)pThis->cPages * cbPage); 371 375 } 372 376 else … … 387 391 *ppvNew = NULL; 388 392 AssertReturn(cb, VERR_INVALID_PARAMETER); 389 AssertReturn(cb <= 32U*_1M - PAGE_SIZE * 3U, VERR_ALLOCATION_TOO_BIG); /* Max 32 MB minus padding and guard pages. */ 393 uint32_t const cbPage = RTSystemGetPageSize(); 394 AssertReturn(cb <= 32U*_1M - cbPage * 3U, VERR_ALLOCATION_TOO_BIG); /* Max 32 MB minus padding and guard pages. */ 390 395 AssertReturn(!(fFlags & ~RTMEMSAFER_F_VALID_MASK), VERR_INVALID_FLAGS); 391 396 … … 406 411 */ 407 412 pThis->cbUser = cb; 408 pThis->offUser = (RTRandU32Ex(0, 128) * RTMEMSAFER_ALIGN) & PAGE_OFFSET_MASK;413 pThis->offUser = (RTRandU32Ex(0, 128) * RTMEMSAFER_ALIGN) & RTSystemGetPageOffsetMask(); 409 414 410 415 size_t cbNeeded = pThis->offUser + pThis->cbUser; 411 cbNeeded = RT_ALIGN_Z(cbNeeded, PAGE_SIZE);412 413 pThis->cPages = (uint32_t)(cbNeeded / PAGE_SIZE) + 2; /* +2 for guard pages */416 cbNeeded = RT_ALIGN_Z(cbNeeded, cbPage); 417 418 pThis->cPages = (uint32_t)(cbNeeded / cbPage) + 2; /* +2 for guard pages */ 414 419 415 420 /* … … 465 470 * Free the pages. 466 471 */ 467 uint8_t *pbPages = (uint8_t *)pv - pThis->offUser - PAGE_SIZE; 468 size_t cbPages = (size_t)pThis->cPages * PAGE_SIZE; 472 uint32_t const cbPage = RTSystemGetPageSize(); 473 uint8_t *pbPages = (uint8_t *)pv - pThis->offUser - cbPage; 474 size_t cbPages = (size_t)pThis->cPages * cbPage; 469 475 switch (pThis->enmAllocator) 470 476 { 471 477 #ifdef IN_SUP_R3 472 478 case RTMEMSAFERALLOCATOR_SUPR3: 473 SUPR3PageProtect(pbPages, NIL_RTR0PTR, 0, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);474 SUPR3PageProtect(pbPages, NIL_RTR0PTR, (uint32_t)(cbPages - PAGE_SIZE), PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);479 SUPR3PageProtect(pbPages, NIL_RTR0PTR, 0, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 480 SUPR3PageProtect(pbPages, NIL_RTR0PTR, (uint32_t)(cbPages - cbPage), cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 475 481 SUPR3PageFreeEx(pbPages, pThis->cPages); 476 482 break; 477 483 #endif 478 484 case RTMEMSAFERALLOCATOR_RTMEMPAGE: 479 RTMemProtect(pbPages, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);480 RTMemProtect(pbPages + cbPages - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);485 RTMemProtect(pbPages, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 486 RTMemProtect(pbPages + cbPages - cbPage, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 481 487 RTMemPageFree(pbPages, cbPages); 482 488 break; … … 552 558 * Is the enough room for us to grow? 553 559 */ 554 size_t cbMax = (size_t)(pThis->cPages - 2) * PAGE_SIZE;560 size_t cbMax = (size_t)(pThis->cPages - 2) * RTSystemGetPageSize(); 555 561 if (cbNew <= cbMax) 556 562 { … … 595 601 rtMemSaferNodeInsert(pThis); 596 602 } 597 Assert(((uintptr_t)*ppvNew & PAGE_OFFSET_MASK) == pThis->offUser);603 Assert(((uintptr_t)*ppvNew & RTSystemGetPageOffsetMask()) == pThis->offUser); 598 604 pThis->cbUser = cbNew; 599 605 rc = VINF_SUCCESS;
Note:
See TracChangeset
for help on using the changeset viewer.