VirtualBox

Changeset 78334 in vbox for trunk/src


Ignore:
Timestamp:
Apr 26, 2019 7:53:32 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/mem: Added RTMemPageAllocEx so we can try lock memory and try prevent it from being part of dumps/cores.

Location:
trunk/src/VBox/Runtime/r3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-heap-posix.cpp

    r76553 r78334  
    130130    /** Non-null on success.  */
    131131    void           *pvAlloc;
    132     /** Whether the pages should be zeroed or not. */
    133     bool            fZero;
     132    /** RTMEMPAGEALLOC_F_XXX. */
     133    uint32_t        fFlags;
    134134} RTHEAPPAGEALLOCARGS;
    135135
     
    234234
    235235/**
     236 * Applies flags to an allocation.
     237 *
     238 * @param   pv              The allocation.
     239 * @param   cb              The size of the allocation (page aligned).
     240 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
     241 */
     242DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)
     243{
     244#ifndef RT_OS_OS2
     245    if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
     246    {
     247        int rc = mlock(pv, cb);
     248        AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
     249        NOREF(rc);
     250    }
     251
     252# ifdef MADV_DONTDUMP
     253    if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
     254    {
     255        int rc = madvise(pv, cb, MADV_DONTDUMP);
     256        AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
     257        NOREF(rc);
     258    }
     259# endif
     260#endif
     261
     262    if (fFlags & RTMEMPAGEALLOC_F_ZERO)
     263        RT_BZERO(pv, cb);
     264}
     265
     266
     267/**
    236268 * Avoids some gotos in rtHeapPageAllocFromBlock.
    237269 *
     
    240272 * @param   iPage           The page to start allocating at.
    241273 * @param   cPages          The number of pages.
    242  * @param   fZero           Whether to clear them.
     274 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
    243275 * @param   ppv             Where to return the allocation address.
    244276 */
    245 DECLINLINE(int) rtHeapPageAllocFromBlockSuccess(PRTHEAPPAGEBLOCK pBlock, uint32_t iPage,  size_t cPages, bool fZero, void **ppv)
     277DECLINLINE(int) rtHeapPageAllocFromBlockSuccess(PRTHEAPPAGEBLOCK pBlock, uint32_t iPage, size_t cPages, uint32_t fFlags, void **ppv)
    246278{
    247279    PRTHEAPPAGE pHeap = pBlock->pHeap;
     
    256288    void *pv = (uint8_t *)pBlock->Core.Key + (iPage << PAGE_SHIFT);
    257289    *ppv = pv;
    258     if (fZero)
    259         RT_BZERO(pv, cPages << PAGE_SHIFT);
     290
     291    if (fFlags)
     292        rtMemPagePosixApplyFlags(pv, cPages << PAGE_SHIFT, fFlags);
    260293
    261294    return VINF_SUCCESS;
     
    291324 * @param   pBlock          The block to allocate from.
    292325 * @param   cPages          The size of the allocation.
    293  * @param   fZero           Whether it should be zeroed or not.
     326 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
    294327 * @param   ppv             Where to return the allocation address on success.
    295328 */
    296 DECLINLINE(int) rtHeapPageAllocFromBlock(PRTHEAPPAGEBLOCK pBlock, size_t cPages, bool fZero, void **ppv)
     329DECLINLINE(int) rtHeapPageAllocFromBlock(PRTHEAPPAGEBLOCK pBlock, size_t cPages, uint32_t fFlags, void **ppv)
    297330{
    298331    if (pBlock->cFreePages >= cPages)
     
    305338        {
    306339            ASMBitSet(&pBlock->bmAlloc[0], iPage);
    307             return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fZero, ppv);
     340            return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fFlags, ppv);
    308341        }
    309342
     
    314347            {
    315348                ASMBitSetRange(&pBlock->bmAlloc[0], iPage, iPage + cPages);
    316                 return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fZero, ppv);
     349                return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fFlags, ppv);
    317350            }
    318351
     
    340373    PRTHEAPPAGEBLOCK        pBlock = RT_FROM_MEMBER(pNode,  RTHEAPPAGEBLOCK, Core);
    341374    RTHEAPPAGEALLOCARGS    *pArgs  = (RTHEAPPAGEALLOCARGS *)pvUser;
    342     int rc = rtHeapPageAllocFromBlock(pBlock, pArgs->cPages, pArgs->fZero, &pArgs->pvAlloc);
     375    int rc = rtHeapPageAllocFromBlock(pBlock, pArgs->cPages, pArgs->fFlags, &pArgs->pvAlloc);
    343376    return RT_SUCCESS(rc) ? 1 : 0;
    344377}
     
    349382 *
    350383 * @returns IPRT status code
    351  * @param   pHeap               The heap - locked.
    352  * @param   cPages              The page count.
    353  * @param   pszTag              The tag.
    354  * @param   fZero               Whether to zero the memory.
    355  * @param   ppv                 Where to return the address of the allocation
    356  *                              on success.
    357  */
    358 static int rtHeapPageAllocLocked(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, bool fZero, void **ppv)
     384 * @param   pHeap           The heap - locked.
     385 * @param   cPages          The page count.
     386 * @param   pszTag          The tag.
     387 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
     388 * @param   ppv             Where to return the address of the allocation
     389 *                          on success.
     390 */
     391static int rtHeapPageAllocLocked(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, uint32_t fFlags, void **ppv)
    359392{
    360393    int rc;
     
    366399    if (pHeap->pHint1)
    367400    {
    368         rc = rtHeapPageAllocFromBlock(pHeap->pHint1, cPages, fZero, ppv);
     401        rc = rtHeapPageAllocFromBlock(pHeap->pHint1, cPages, fFlags, ppv);
    369402        if (rc != VERR_NO_MEMORY)
    370403            return rc;
     
    372405    if (pHeap->pHint2)
    373406    {
    374         rc = rtHeapPageAllocFromBlock(pHeap->pHint2, cPages, fZero, ppv);
     407        rc = rtHeapPageAllocFromBlock(pHeap->pHint2, cPages, fFlags, ppv);
    375408        if (rc != VERR_NO_MEMORY)
    376409            return rc;
     
    388421        Args.cPages  = cPages;
    389422        Args.pvAlloc = NULL;
    390         Args.fZero   = fZero;
     423        Args.fFlags  = fFlags;
    391424        RTAvlrPVDoWithAll(&pHeap->BlockTree, true /*fFromLeft*/, rtHeapPageAllocCallback, &Args);
    392425        if (Args.pvAlloc)
     
    442475     * Grab memory from the new block (cannot fail).
    443476     */
    444     rc = rtHeapPageAllocFromBlock(pBlock, cPages, fZero, ppv);
     477    rc = rtHeapPageAllocFromBlock(pBlock, cPages, fFlags, ppv);
    445478    Assert(rc == VINF_SUCCESS);
    446479
     
    456489 * @param   cPages          The number of pages to allocate.
    457490 * @param   pszTag          The allocation tag.
    458  * @param   fZero           Set if the pages should be zeroed or not.
     491 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
    459492 * @param   ppv             Where to return the pointer to the pages.
    460493 */
    461 int RTHeapPageAlloc(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, bool fZero, void **ppv)
     494int RTHeapPageAlloc(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, uint32_t fFlags, void **ppv)
    462495{
    463496    /*
     
    476509    if (RT_SUCCESS(rc))
    477510    {
    478         rc = rtHeapPageAllocLocked(pHeap, cPages, pszTag, fZero, ppv);
     511        rc = rtHeapPageAllocLocked(pHeap, cPages, pszTag, fFlags, ppv);
    479512        RTCritSectLeave(&pHeap->CritSect);
    480513    }
     
    561594                    pHeap->pHint1 = pBlock;
    562595
     596                /** @todo Add bitmaps for tracking madvice and mlock so we can undo those. */
     597
    563598                /*
    564599                 * Shrink the heap. Not very efficient because of the AVL tree.
     
    641676 * @param   cb                  The number of bytes to allocate.
    642677 * @param   pszTag              The tag.
    643  * @param   fZero               Whether to zero the memory or not.
     678 * @param   fFlags              RTMEMPAGEALLOC_F_XXX.
    644679 * @param   pHeap               The heap to use.
    645680 */
    646 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, bool fZero, PRTHEAPPAGE pHeap)
     681static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, PRTHEAPPAGE pHeap)
    647682{
    648683    /*
     
    667702        {
    668703            AssertPtr(pv);
    669             if (fZero)
    670                 RT_BZERO(pv, cb);
     704
     705            if (fFlags)
     706                rtMemPagePosixApplyFlags(pv, cb, fFlags);
    671707        }
    672708        else
     
    677713        int rc = RTOnce(&g_MemPagePosixInitOnce, rtMemPagePosixInitOnce, NULL);
    678714        if (RT_SUCCESS(rc))
    679             rc = RTHeapPageAlloc(pHeap, cb >> PAGE_SHIFT, pszTag, fZero, &pv);
     715            rc = RTHeapPageAlloc(pHeap, cb >> PAGE_SHIFT, pszTag, fFlags, &pv);
    680716        if (RT_FAILURE(rc))
    681717            pv = NULL;
     
    726762RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    727763{
    728     return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, &g_MemPagePosixHeap);
     764    return rtMemPagePosixAlloc(cb, pszTag, 0, &g_MemPagePosixHeap);
    729765}
    730766
     
    732768RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    733769{
    734     return rtMemPagePosixAlloc(cb, pszTag, true /*fZero*/, &g_MemPagePosixHeap);
     770    return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, &g_MemPagePosixHeap);
     771}
     772
     773
     774RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
     775{
     776    AssertReturn(!(fFlags & RTMEMPAGEALLOC_F_VALID_MASK), NULL);
     777    return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0);
    735778}
    736779
     
    747790RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    748791{
    749     return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, &g_MemExecPosixHeap);
     792    return rtMemPagePosixAlloc(cb, pszTag, 0, &g_MemExecPosixHeap);
    750793}
    751794
  • trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp

    r76553 r78334  
    4747
    4848/**
     49 * Applies flags to an allocation.
     50 *
     51 * @param   pv              The allocation.
     52 * @param   cb              The size of the allocation (page aligned).
     53 * @param   fFlags          RTMEMPAGEALLOC_F_XXX.
     54 */
     55DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)
     56{
     57#ifndef RT_OS_OS2
     58    if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
     59    {
     60        int rc = mlock(pv, cb);
     61        AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
     62        NOREF(rc);
     63    }
     64
     65# ifdef MADV_DONTDUMP
     66    if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
     67    {
     68        int rc = madvise(pv, cb, MADV_DONTDUMP);
     69        AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
     70        NOREF(rc);
     71    }
     72# endif
     73#endif
     74
     75    if (fFlags & RTMEMPAGEALLOC_F_ZERO)
     76        RT_BZERO(pv, cb);
     77}
     78
     79
     80/**
    4981 * Allocates memory from the specified heap.
    5082 *
     
    5284 * @param   cb                  The number of bytes to allocate.
    5385 * @param   pszTag              The tag.
    54  * @param   fZero               Whether to zero the memory or not.
     86 * @param   fFlags              RTMEMPAGEALLOC_F_XXX.
    5587 * @param   fProtExec           PROT_EXEC or 0.
    5688 */
    57 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, bool fZero, int fProtExec)
     89static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, int fProtExec)
    5890{
    5991    /*
     
    73105    {
    74106        AssertPtr(pv);
    75         if (fZero)
    76             RT_BZERO(pv, cb);
     107
     108        if (fFlags)
     109            rtMemPagePosixApplyFlags(pv, cb, fFlags);
    77110    }
    78111    else
     
    114147RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    115148{
    116     return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, 0);
     149    return rtMemPagePosixAlloc(cb, pszTag, 0, 0);
    117150}
    118151
     
    120153RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    121154{
    122     return rtMemPagePosixAlloc(cb, pszTag, true /*fZero*/, 0);
     155    return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, 0);
     156}
     157
     158
     159RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
     160{
     161    AssertReturn(!(fFlags & RTMEMPAGEALLOC_F_VALID_MASK), NULL);
     162    return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0);
    123163}
    124164
     
    135175RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    136176{
    137     return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, PROT_EXEC);
     177    return rtMemPagePosixAlloc(cb, pszTag, 0, PROT_EXEC);
    138178}
    139179
  • trunk/src/VBox/Runtime/r3/win/alloc-win.cpp

    r76553 r78334  
    9696
    9797
     98RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
     99{
     100    size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
     101    RT_NOREF_PV(pszTag);
     102    AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
     103
     104#ifdef USE_VIRTUAL_ALLOC
     105    void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE);
     106#else
     107    void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
     108#endif
     109    AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
     110
     111    if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
     112    {
     113        BOOL const fOkay = VirtualLock(pv, cbAligned);
     114        AssertMsg(fOkay, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
     115        NOREF(fOkay);
     116    }
     117
     118    if (fFlags & RTMEMPAGEALLOC_F_ZERO)
     119        RT_BZERO(pv, cbAligned);
     120
     121    return pv;
     122}
     123
     124
    98125RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
    99126{
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