VirtualBox

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

Runtime/r3/alloc-ef.{cpp,h}: 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/alloc-ef.cpp

    r98103 r100313  
    5656#include <iprt/param.h>
    5757#include <iprt/string.h>
     58#include <iprt/system.h>
    5859
    5960#ifdef RTALLOC_REPLACE_MALLOC
     
    279280DECLINLINE(void) rtmemBlockDelayInsert(PRTMEMBLOCK pBlock)
    280281{
    281     size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     282    size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
     283    size_t const cbBlock = RTSystemPageAlignSize(pBlock->cbAligned) + cbFence;
    282284    pBlock->Core.pRight = NULL;
    283285    pBlock->Core.pLeft = NULL;
     
    315317            else
    316318                g_pBlocksDelayHead = NULL;
    317             g_cbBlocksDelay -= RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     319
     320            size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
     321            g_cbBlocksDelay -= RTSystemPageAlignSize(pBlock->cbAligned) + cbFence;
    318322        }
    319323    }
     
    455459     * Allocate a page for jump back code (we leak it).
    456460     */
    457     uint8_t *pbExecPage = (uint8_t *)RTMemPageAlloc(PAGE_SIZE); AssertFatal(pbExecPage);
    458     int rc = RTMemProtect(pbExecPage, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc);
     461    size_t const cbPage = RTSystemGetPageSize();
     462    uint8_t *pbExecPage = (uint8_t *)RTMemPageAlloc(cbPage); AssertFatal(pbExecPage);
     463    int rc = RTMemProtect(pbExecPage, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc);
    459464
    460465    /*
     
    578583     * Sanity.
    579584     */
    580     if (    RT_ALIGN_Z(RTALLOC_EFENCE_SIZE, PAGE_SIZE) != RTALLOC_EFENCE_SIZE
    581         &&  RTALLOC_EFENCE_SIZE <= 0)
    582     {
    583         rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE);
     585    size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
     586    size_t const cbPage = RTSystemGetPageSize();
     587    if (RTALLOC_EFENCE_SIZE_FACTOR <= 0)
     588    {
     589        rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE_FACTOR);
    584590        return NULL;
    585591    }
     
    616622     * Allocate a block with page alignment space + the size of the E-fence.
    617623     */
    618     size_t  cbBlock = RT_ALIGN_Z(cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     624    size_t  cbBlock = RT_ALIGN_Z(cbAligned, cbPage) + cbFence;
    619625    void   *pvBlock = RTMemPageAlloc(cbBlock);
    620626    if (pvBlock)
     
    626632#ifdef RTALLOC_EFENCE_IN_FRONT
    627633        void *pvEFence = pvBlock;
    628         void *pv       = (char *)pvEFence + RTALLOC_EFENCE_SIZE;
     634        void *pv       = (char *)pvEFence + cbFence;
    629635# ifdef RTALLOC_EFENCE_NOMAN_FILLER
    630         memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE - cbUnaligned);
     636        memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbUnaligned);
    631637# endif
    632638#else
    633         void *pvEFence = (char *)pvBlock + (cbBlock - RTALLOC_EFENCE_SIZE);
     639        void *pvEFence = (char *)pvBlock + (cbBlock - cbFence);
    634640        void *pv       = (char *)pvEFence - cbAligned;
    635641# ifdef RTALLOC_EFENCE_NOMAN_FILLER
    636         memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE - cbAligned);
     642        memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbAligned);
    637643        memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbAligned - cbUnaligned);
    638644# endif
     
    640646
    641647#ifdef RTALLOC_EFENCE_FENCE_FILLER
    642         memset(pvEFence, RTALLOC_EFENCE_FENCE_FILLER, RTALLOC_EFENCE_SIZE);
    643 #endif
    644         int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_NONE);
     648        memset(pvEFence, RTALLOC_EFENCE_FENCE_FILLER, cbFence);
     649#endif
     650        int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_NONE);
    645651        if (!rc)
    646652        {
     
    658664            return pv;
    659665        }
    660         rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
     666        rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, cbFence, rc);
    661667        RTMemPageFree(pvBlock, cbBlock);
    662668    }
     
    691697            RTAssertDoPanic();
    692698
     699    size_t cbPage = RTSystemGetPageSize();
    693700#ifdef RTALLOC_EFENCE_TRACE
    694701    /*
     
    707714#  ifdef RTALLOC_EFENCE_IN_FRONT
    708715        void *pvWrong = ASMMemFirstMismatchingU8((char *)pv + pBlock->cbUnaligned,
    709                                                  RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) - pBlock->cbUnaligned,
     716                                                 RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbUnaligned,
    710717                                                 RTALLOC_EFENCE_NOMAN_FILLER);
    711718#  else
     
    716723        if (pvWrong)
    717724            RTAssertDoPanic();
    718         pvWrong = ASMMemFirstMismatchingU8((void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK),
    719                                            RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) - pBlock->cbAligned,
     725        pvWrong = ASMMemFirstMismatchingU8((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()),
     726                                           RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbAligned,
    720727                                           RTALLOC_EFENCE_NOMAN_FILLER);
    721728#  endif
     
    737744# endif
    738745
     746        size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
    739747# if defined(RTALLOC_EFENCE_FREE_DELAYED) && RTALLOC_EFENCE_FREE_DELAYED > 0
    740748        /*
     
    753761                pv = pBlock->Core.Key;
    754762#  ifdef RTALLOC_EFENCE_IN_FRONT
    755                 void  *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
     763                void  *pvBlock = (char *)pv - cbFence;
    756764#  else
    757                 void  *pvBlock = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
     765                void  *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask());
    758766#  endif
    759                 size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     767                size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence;
    760768                rc = RTMemProtect(pvBlock, cbBlock, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
    761769                if (RT_SUCCESS(rc))
    762                     RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE);
     770                    RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence);
    763771                else
    764772                    rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvBlock, cbBlock, rc);
     
    775783         */
    776784#  ifdef RTALLOC_EFENCE_IN_FRONT
    777         void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
     785        void *pvBlock = (char *)pv - cbFence;
    778786        void *pvEFence = pvBlock;
    779787#  else
    780         void *pvBlock = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
     788        void *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask());
    781789        void *pvEFence = (char *)pv + pBlock->cb;
    782790#  endif
    783         int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
     791        int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
    784792        if (RT_SUCCESS(rc))
    785             RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE);
     793            RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence);
    786794        else
    787             rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
     795            rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, cbFence, rc);
    788796        rtmemBlockFree(pBlock);
    789797
     
    803811    if (enmType == RTMEMTYPE_RTMEMFREEZ)
    804812        RT_BZERO(pv, cbUser);
    805     int rc = RTMemProtect((void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK), PAGE_SIZE, RTMEM_PROT_NONE);
     813    int rc = RTMemProtect((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), cbPage, RTMEM_PROT_NONE);
    806814    if (RT_FAILURE(rc))
    807         rtmemComplain(pszOp, "RTMemProtect(%p, PAGE_SIZE, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK), rc);
     815        rtmemComplain(pszOp, "RTMemProtect(%p, cbPage, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), rc);
    808816#endif /* !RTALLOC_EFENCE_TRACE */
    809817}
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