VirtualBox

Changeset 69971 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Dec 7, 2017 11:16:53 AM (7 years ago)
Author:
vboxsync
Message:

iprt/memtracker: Add the caller address to the tracking information.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/alloc/memtracker.cpp

    r69111 r69971  
    505505 * @param   cbUser              The size requested by the user.
    506506 * @param   pszTag              The tag string.
     507 * @param   pvCaller            The return address.
    507508 * @param   enmMethod           The allocation method.
    508509 */
    509510static void *rtMemTrackerHdrAllocEx(PRTMEMTRACKERINT pTracker, void *pv, size_t cbUser,
    510                                     const char *pszTag, RTMEMTRACKERMETHOD enmMethod)
     511                                    const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod)
    511512{
    512513    /*
     
    528529    pHdr->pszTag            = pszTag;
    529530    pHdr->pTag              = NULL;
     531    pHdr->pvCaller          = pvCaller;
    530532    pHdr->pvUser            = pHdr + 1;
     533    pHdr->uReserved         = 0;
    531534
    532535    /*
     
    579582 * @param   cbUser              The size of the user memory or 0.
    580583 * @param   pszTag              The tag to associate the free with.
     584 * @param   pvCaller            The return address.
    581585 * @param   enmMethod           The free method.
    582586 * @param   uDeadMagic          The dead magic value to use.
    583587 */
    584588static void *rtMemTrackerHdrFreeCommon(PRTMEMTRACKERINT pTracker, void *pvUser, size_t cbUser,
    585                                        const char *pszTag, RTMEMTRACKERMETHOD enmMethod,
     589                                       const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod,
    586590                                       size_t uDeadMagic)
    587591{
     
    633637
    634638        /** @todo we're currently ignoring pszTag, consider how to correctly
    635          *        attribute the free operation if the tags differ - it
     639         *        attribute the free operation if the tags differ - if it
    636640         *        makes sense at all... */
    637641        NOREF(pszTag);
     
    656660    }
    657661
     662    NOREF(pvCaller);  /* Intended for We may later do some use-after-free tracking. */
    658663    return pHdr;
    659664}
     
    669674 * @param   cbOldUser           The size of the user memory, 0 if unknown.
    670675 * @param   pszTag              The tag string.
    671  */
    672 static void *rtMemTrackerHdrReallocPrepEx(PRTMEMTRACKERINT pTracker, void *pvOldUser, size_t cbOldUser, const char *pszTag)
     676 * @param   pvCaller            The return address.
     677 */
     678static void *rtMemTrackerHdrReallocPrepEx(PRTMEMTRACKERINT pTracker, void *pvOldUser, size_t cbOldUser,
     679                                          const char *pszTag, void *pvCaller)
    673680{
    674681    if (!pvOldUser)
    675682        return NULL;
    676     return rtMemTrackerHdrFreeCommon(pTracker, pvOldUser, cbOldUser, pszTag,
     683    return rtMemTrackerHdrFreeCommon(pTracker, pvOldUser, cbOldUser, pszTag, pvCaller,
    677684                                     RTMEMTRACKERMETHOD_REALLOC_PREP, RTMEMTRACKERHDR_MAGIC_REALLOC);
    678685}
     
    689696 * @param   pvOldUser           Pointer to the old user memory.
    690697 * @param   pszTag              The tag string.
     698 * @param   pvCaller            The return address.
    691699 */
    692700static void *rtMemTrackerHdrReallocDoneEx(PRTMEMTRACKERINT pTracker, void *pvNew, size_t cbNewUser,
    693                                           void *pvOldUser, const char *pszTag)
     701                                          void *pvOldUser, const char *pszTag, void *pvCaller)
    694702{
    695703    /* Succeeded? */
    696704    if (pvNew)
    697         return rtMemTrackerHdrAllocEx(pTracker, pvNew, cbNewUser, pszTag, RTMEMTRACKERMETHOD_REALLOC_DONE);
     705        return rtMemTrackerHdrAllocEx(pTracker, pvNew, cbNewUser, pszTag, pvCaller, RTMEMTRACKERMETHOD_REALLOC_DONE);
    698706
    699707    /* Failed or just realloc to zero? */
     
    703711        AssertReturn(pHdr->uMagic == RTMEMTRACKERHDR_MAGIC_REALLOC, NULL);
    704712
    705         return rtMemTrackerHdrAllocEx(pTracker, pHdr, pHdr->cbUser, pszTag, RTMEMTRACKERMETHOD_REALLOC_FAILED);
     713        return rtMemTrackerHdrAllocEx(pTracker, pHdr, pHdr->cbUser, pszTag, pvCaller, RTMEMTRACKERMETHOD_REALLOC_FAILED);
    706714    }
    707715
     
    719727 * @param   cbUser              The size of the user memory, 0 if unknown.
    720728 * @param   pszTag              The tag string.
     729 * @param   pvCaller            The return address.
    721730 * @param   enmMethod           The free method.
    722731 */
    723732static void *rtMemTrackerHdrFreeEx(PRTMEMTRACKERINT pTracker, void *pvUser, size_t cbUser,
    724                                    const char *pszTag, RTMEMTRACKERMETHOD enmMethod)
     733                                   const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod)
    725734{
    726735    if (!pvUser)
    727736        return NULL;
    728     return rtMemTrackerHdrFreeCommon(pTracker, pvUser, cbUser, pszTag, enmMethod, RTMEMTRACKERHDR_MAGIC_FREE);
     737    return rtMemTrackerHdrFreeCommon(pTracker, pvUser, cbUser, pszTag, pvCaller, enmMethod, RTMEMTRACKERHDR_MAGIC_FREE);
    729738}
    730739
     
    825834            if (pCurHdr->pTag)
    826835                pOutput->pfnPrintf(pOutput,
    827                                    "    %zu bytes at %p with tag %s\n"
    828                                    "    %.*Rhxd\n"
     836                                   "    %zu bytes at %p by %p with tag %s\n"
     837                                   "%.*Rhxd\n"
    829838                                   "\n",
    830                                    pCurHdr->cbUser, pCurHdr->pvUser, pCurHdr->pTag->szTag,
     839                                   pCurHdr->cbUser, pCurHdr->pvUser, pCurHdr->pvCaller, pCurHdr->pTag->szTag,
    831840                                   RT_MIN(pCurHdr->cbUser, 16*3), pCurHdr->pvUser);
    832841            else
    833842                pOutput->pfnPrintf(pOutput,
    834                                    "    %zu bytes at %p without a tag\n"
    835                                    "    %.*Rhxd\n"
     843                                   "    %zu bytes at %p by %p without a tag\n"
     844                                   "%.*Rhxd\n"
    836845                                   "\n",
    837                                    pCurHdr->cbUser, pCurHdr->pvUser,
     846                                   pCurHdr->cbUser, pCurHdr->pvUser, pCurHdr->pvCaller,
    838847                                   RT_MIN(pCurHdr->cbUser, 16*3), pCurHdr->pvUser);
    839848        }
     
    12141223
    12151224
    1216 RTDECL(void *) RTMemTrackerHdrAlloc(void *pv, size_t cb, const char *pszTag, RTMEMTRACKERMETHOD enmMethod)
     1225RTDECL(void *) RTMemTrackerHdrAlloc(void *pv, size_t cb, const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod)
    12171226{
    12181227    PRTMEMTRACKERINT pTracker = g_pDefaultTracker;
    12191228    if (RT_UNLIKELY(!pTracker))
    12201229        pTracker = rtMemTrackerLazyInitDefaultTracker();
    1221     return rtMemTrackerHdrAllocEx(pTracker, pv, cb, pszTag, enmMethod);
    1222 }
    1223 
    1224 
    1225 RTDECL(void *) RTMemTrackerHdrReallocPrep(void *pvOldUser, size_t cbOldUser, const char *pszTag)
     1230    return rtMemTrackerHdrAllocEx(pTracker, pv, cb, pszTag, pvCaller, enmMethod);
     1231}
     1232
     1233
     1234RTDECL(void *) RTMemTrackerHdrReallocPrep(void *pvOldUser, size_t cbOldUser, const char *pszTag, void *pvCaller)
    12261235{
    12271236    PRTMEMTRACKERINT pTracker = g_pDefaultTracker;
    12281237    if (RT_UNLIKELY(!pTracker))
    12291238        pTracker = rtMemTrackerLazyInitDefaultTracker();
    1230     return rtMemTrackerHdrReallocPrepEx(pTracker, pvOldUser, cbOldUser, pszTag);
    1231 }
    1232 
    1233 
    1234 RTDECL(void *) RTMemTrackerHdrReallocDone(void *pvNew, size_t cbNewUser, void *pvOld, const char *pszTag)
     1239    return rtMemTrackerHdrReallocPrepEx(pTracker, pvOldUser, cbOldUser, pszTag, pvCaller);
     1240}
     1241
     1242
     1243RTDECL(void *) RTMemTrackerHdrReallocDone(void *pvNew, size_t cbNewUser, void *pvOld, const char *pszTag, void *pvCaller)
    12351244{
    12361245    PRTMEMTRACKERINT pTracker = g_pDefaultTracker;
    12371246    if (RT_UNLIKELY(!pTracker))
    12381247        pTracker = rtMemTrackerLazyInitDefaultTracker();
    1239     return rtMemTrackerHdrReallocDoneEx(pTracker, pvNew, cbNewUser, pvOld, pszTag);
    1240 }
    1241 
    1242 
    1243 RTDECL(void *) RTMemTrackerHdrFree(void *pvUser, size_t cbUser, const char *pszTag, RTMEMTRACKERMETHOD enmMethod)
     1248    return rtMemTrackerHdrReallocDoneEx(pTracker, pvNew, cbNewUser, pvOld, pszTag, pvCaller);
     1249}
     1250
     1251
     1252RTDECL(void *) RTMemTrackerHdrFree(void *pvUser, size_t cbUser, const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod)
    12441253{
    12451254    PRTMEMTRACKERINT pTracker = g_pDefaultTracker;
    12461255    if (RT_UNLIKELY(!pTracker))
    12471256        pTracker = rtMemTrackerLazyInitDefaultTracker();
    1248     return rtMemTrackerHdrFreeEx(pTracker, pvUser, cbUser, pszTag, enmMethod);
     1257    return rtMemTrackerHdrFreeEx(pTracker, pvUser, cbUser, pszTag, pvCaller, enmMethod);
    12491258}
    12501259
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