VirtualBox

Changeset 100315 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jun 28, 2023 10:31:50 AM (19 months ago)
Author:
vboxsync
Message:

Runtime/r3/memsafer-r3.cpp: Replace occurences of PAGE_SIZE/PAGE_OFFSET_MASK with RTSystemGetPageSize()/RTSystemGetPageOffsetMask() in code which is used by the linux.arm64 guest additions / validationkit, bugref:10476

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/memsafer-r3.cpp

    r98103 r100315  
    5252#include <iprt/param.h>
    5353#include <iprt/string.h>
     54#include <iprt/system.h>
    5455#ifdef IN_SUP_R3
    5556# include <VBox/sup.h>
     
    270271static void rtMemSaferInitializePages(PRTMEMSAFERNODE pThis, void *pvPages)
    271272{
    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;
    275277    pThis->Core.Key = pbUser;
    276278    RT_BZERO(pbUser, pThis->cbUser); /* paranoia */
    277279
    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);
    279281}
    280282
     
    312314         * SUPR3PageProtect isn't supported on all hosts, we ignore that.
    313315         */
    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);
    315318        if (RT_SUCCESS(rc))
    316319        {
    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);
    318321            if (RT_SUCCESS(rc))
    319322                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);
    321324        }
    322325        else if (rc == VERR_NOT_SUPPORTED)
     
    348351     * Try allocate the memory.
    349352     */
     353    uint32_t const cbPage = RTSystemGetPageSize();
    350354    int rc = VINF_SUCCESS;
    351     void *pvPages = RTMemPageAllocEx((size_t)pThis->cPages * PAGE_SIZE,
     355    void *pvPages = RTMemPageAllocEx((size_t)pThis->cPages * cbPage,
    352356                                     RTMEMPAGEALLOC_F_ADVISE_LOCKED | RTMEMPAGEALLOC_F_ADVISE_NO_DUMP | RTMEMPAGEALLOC_F_ZERO);
    353357    if (pvPages)
     
    358362         * Configure the guard pages.
    359363         */
    360         rc = RTMemProtect(pvPages, PAGE_SIZE, RTMEM_PROT_NONE);
     364        rc = RTMemProtect(pvPages, cbPage, RTMEM_PROT_NONE);
    361365        if (RT_SUCCESS(rc))
    362366        {
    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);
    364368            if (RT_SUCCESS(rc))
    365369                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);
    367371        }
    368372
    369373        /* failed. */
    370         RTMemPageFree(pvPages, (size_t)pThis->cPages * PAGE_SIZE);
     374        RTMemPageFree(pvPages, (size_t)pThis->cPages * cbPage);
    371375    }
    372376    else
     
    387391    *ppvNew = NULL;
    388392    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. */
    390395    AssertReturn(!(fFlags & ~RTMEMSAFER_F_VALID_MASK), VERR_INVALID_FLAGS);
    391396
     
    406411             */
    407412            pThis->cbUser  = cb;
    408             pThis->offUser = (RTRandU32Ex(0, 128) * RTMEMSAFER_ALIGN) & PAGE_OFFSET_MASK;
     413            pThis->offUser = (RTRandU32Ex(0, 128) * RTMEMSAFER_ALIGN) & RTSystemGetPageOffsetMask();
    409414
    410415            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 */
    414419
    415420            /*
     
    465470         * Free the pages.
    466471         */
    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;
    469475        switch (pThis->enmAllocator)
    470476        {
    471477#ifdef IN_SUP_R3
    472478            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);
    475481                SUPR3PageFreeEx(pbPages, pThis->cPages);
    476482                break;
    477483#endif
    478484            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);
    481487                RTMemPageFree(pbPages, cbPages);
    482488                break;
     
    552558                 * Is the enough room for us to grow?
    553559                 */
    554                 size_t cbMax = (size_t)(pThis->cPages - 2) * PAGE_SIZE;
     560                size_t cbMax = (size_t)(pThis->cPages - 2) * RTSystemGetPageSize();
    555561                if (cbNew <= cbMax)
    556562                {
     
    595601                        rtMemSaferNodeInsert(pThis);
    596602                    }
    597                     Assert(((uintptr_t)*ppvNew & PAGE_OFFSET_MASK) == pThis->offUser);
     603                    Assert(((uintptr_t)*ppvNew & RTSystemGetPageOffsetMask()) == pThis->offUser);
    598604                    pThis->cbUser = cbNew;
    599605                    rc = VINF_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette