VirtualBox

Ignore:
Timestamp:
Dec 30, 2022 10:24:20 PM (2 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
155013
Message:

Add/VBoxGuestR0LibPhysHeap.cpp: Made VbglR0PhysHeapFree and VbglR0PhysHeapGetPhysAddr more strict wrt input. Cleanups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/lib/testcase/tstVbglR0PhysHeap-1.cpp

    r97921 r97923  
    8787            g_cChunks++;
    8888            g_cbChunks += cb;
    89             *pPhys = (uint32_t)(uintptr_t)pvRet ^ UINT32_C(0xf0f0f000);
     89            *pPhys = (uint32_t)(uintptr_t)pvRet ^ (UINT32_C(0xf0f0f0f0) & ~(uint32_t)PAGE_OFFSET_MASK);
     90
     91            /* Avoid problematic values that won't happen in real life:  */
     92            if (!*pPhys)
     93                *pPhys = 4U << PAGE_SHIFT;
     94            if (UINT32_MAX - *pPhys < cb)
     95                *pPhys -= RT_ALIGN_32(cb, PAGE_SIZE);
     96
    9097            return pvRet;
    9198        }
     
    160167    if (RT_FAILURE(rc))
    161168        return RTTestSummaryAndDestroy(hTest);
     169
     170#define CHECK_PHYS_ADDR(a_pv) do { \
     171        uint32_t const uPhys = VbglR0PhysHeapGetPhysAddr(a_pv); \
     172        if (uPhys == 0 || uPhys == UINT32_MAX || (uPhys & PAGE_OFFSET_MASK) != ((uintptr_t)(a_pv) & PAGE_OFFSET_MASK)) \
     173            RTTestIFailed("line %u: %s=%p: uPhys=%#x\n", __LINE__, #a_pv, (a_pv), uPhys); \
     174    } while (0)
    162175
    163176    /*
     
    210223        RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, sizeof(void *)) == s_aOps[i].pvAlloc,
    211224                          ("VbglR0PhysHeapAlloc(%#x) -> %p\n", s_aOps[i].cb, i));
    212         if (!s_aOps[i].pvAlloc)
    213             return RTTestSummaryAndDestroy(hTest);
     225
     226        CHECK_PHYS_ADDR(s_aOps[i].pvAlloc);
    214227    }
    215228
     
    230243        if (!pv)
    231244            return RTTestSummaryAndDestroy(hTest);
     245        CHECK_PHYS_ADDR(pv);
     246
    232247        //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
    233248        //         cbBeforeSub, cbAfterSubFree, VbglR0PhysHeapGetFreeSize());
     
    283298            }
    284299            if (s_aHistory[i].pv)
     300            {
    285301                memset(s_aHistory[i].pv, 0xbb, s_aHistory[i].cb);
     302                CHECK_PHYS_ADDR(s_aHistory[i].pv);
     303            }
    286304        }
    287305        else
     
    311329                    s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
    312330                    if (s_aHistory[i].pv)
     331                    {
    313332                        memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
     333                        CHECK_PHYS_ADDR(s_aHistory[i].pv);
     334                    }
    314335                }
    315336
     
    327348                        }
    328349                        if (s_aHistory[i].pv)
     350                        {
    329351                            memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
     352                            CHECK_PHYS_ADDR(s_aHistory[i].pv);
     353                        }
    330354
    331355                        cbFree = VbglR0PhysHeapGetFreeSize();
Note: See TracChangeset for help on using the changeset viewer.

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