lockvalidator.cpp revision cc8f458e924eba76c9fd928e46a46e59a41318da
/* $Id$ */
/** @file
* IPRT - Lock Validator.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/lockvalidator.h>
#include <iprt/semaphore.h>
#include "internal/lockvalidator.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Serializing object destruction and deadlock detection.
* NS: RTLOCKVALIDATORREC and RTTHREADINT destruction.
* EW: Deadlock detection.
*/
/** Whether the lock validator is enabled or disabled.
* Only applies to new locks. */
static bool volatile g_fLockValidatorEnabled = true;
/**
* Copy a source position record.
*
* @param pDst The destination.
* @param pSrc The source.
*/
DECL_FORCE_INLINE(void) rtLockValidatorCopySrcPos(PRTLOCKVALIDATORSRCPOS pDst, PCRTLOCKVALIDATORSRCPOS pSrc)
{
}
/**
* Init a source position record.
*
* @param pSrcPos The source position record.
*/
{
#if HC_ARCH_BITS == 64
pSrcPos->u32Padding = 0;
#endif
}
/**
* Serializes destruction of RTLOCKVALIDATORREC and RTTHREADINT structures.
*/
DECLHIDDEN(void) rtLockValidatorSerializeDestructEnter(void)
{
if (hXRoads != NIL_RTSEMXROADS)
}
/**
* Call after rtLockValidatorSerializeDestructEnter.
*/
DECLHIDDEN(void) rtLockValidatorSerializeDestructLeave(void)
{
if (hXRoads != NIL_RTSEMXROADS)
}
/**
* Serializes deadlock detection against destruction of the objects being
* inspected.
*/
DECLINLINE(void) rtLockValidatorSerializeDetectionEnter(void)
{
if (hXRoads != NIL_RTSEMXROADS)
}
/**
* Call after rtLockValidatorSerializeDetectionEnter.
*/
DECLHIDDEN(void) rtLockValidatorSerializeDetectionLeave(void)
{
if (hXRoads != NIL_RTSEMXROADS)
}
{
pRec->afReserved[0] = 0;
pRec->cRecursion = 0;
/* Lazily initialize the crossroads semaphore. */
static uint32_t volatile s_fInitializing = false;
&& ASMAtomicCmpXchgU32(&s_fInitializing, true, false)))
{
if (RT_SUCCESS(rc))
ASMAtomicWriteU32(&s_fInitializing, false);
}
}
{
if (!pRec)
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
{
}
{
if (pRec)
{
}
}
{
/* the table */
pRec->iLastEntry = 0;
pRec->cAllocated = 0;
pRec->fReallocating = false;
}
{
/*
* Flip it into table realloc mode and take the destruction lock.
*/
{
}
{
}
}
/**
* Locates a thread in a shared lock record.
*
* @returns Pointer to the thread record on success, NULL on failure..
* @param pShared The shared lock record.
* @param hThread The thread to find.
* @param piEntry Where to optionally return the table in index.
*/
rtLockValidatorSharedRecFindThread(PRTLOCKVALIDATORSHARED pShared, RTTHREAD hThread, uint32_t *piEntry)
{
{
{
{
if (piEntry)
return pEntry;
}
}
}
return NULL;
}
/**
* Allocates and initializes a thread entry for the shared lock record.
*
* @returns The new thread entry.
* @param pShared The shared lock record.
* @param hThread The thread handle.
* @param pSrcPos The source position.
*/
rtLockValidatorSharedRecAllocThread(PRTLOCKVALIDATORSHARED pRead, RTTHREAD hThread, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
if (pEntry)
{
#if HC_ARCH_BITS == 32
#endif
if (pSrcPos)
else
}
return pEntry;
}
/**
* Frees a thread entry allocated by rtLockValidatorSharedRecAllocThread.
*
* @param pEntry The thread entry.
*/
{
if (pEntry)
{
}
}
/**
* Make more room in the table.
*
* @retval true on success
* @retval false if we're out of memory or running into a bad race condition
* (probably a bug somewhere). No longer holding the lock.
*
* @param pShared The shared lock record.
*/
{
for (unsigned i = 0; i < 1000; i++)
{
/*
* Switch to the other data access direction.
*/
if (i >= 10)
{
RTThreadSleep(i >= 100);
}
/*
* Try grab the privilege to reallocating the table.
*/
{
{
/*
* Ok, still not enough space. Reallocate the table.
*/
#if 0 /** @todo enable this after making sure growing works flawlessly. */
#else
#endif
(cAllocated + cInc) * sizeof(void *));
if (!papOwners)
{
/* RTMemRealloc will assert */
return false;
}
while (cInc-- > 0)
{
cAllocated++;
}
}
}
break;
return true;
}
AssertFailed(); /* too many iterations or destroyed while racing. */
return false;
}
/**
* Adds a thread entry to a shared lock record.
*
* @returns true on success, false on serious race or we're if out of memory.
* @param pShared The shared lock record.
* @param pEntry The thread entry.
*/
DECLINLINE(bool) rtLockValidatorSharedRecAddThread(PRTLOCKVALIDATORSHARED pShared, PRTLOCKVALIDATORSHAREDONE pEntry)
{
{
return false; /* the worker leave the lock */
for (unsigned i = 0; i < 100; i++)
{
{
{
return true;
}
}
Assert(i != 25);
}
AssertFailed();
}
return false;
}
/**
* Remove a thread entry from a shared lock record and free it.
*
* @param pShared The shared lock record.
* @param pEntry The thread entry to remove.
* @param iEntry The last known index.
*/
DECLINLINE(void) rtLockValidatorSharedRecRemoveAndFree(PRTLOCKVALIDATORSHARED pShared, PRTLOCKVALIDATORSHAREDONE pEntry,
{
/*
* Remove it from the table.
*/
AssertReturnVoidStmt(pShared->u32Magic == RTLOCKVALIDATORSHARED_MAGIC, rtLockValidatorSerializeDetectionLeave());
{
/* this shouldn't happen yet... */
AssertFailed();
break;
}
/*
* Successfully removed, now free it.
*/
}
RTDECL(int) RTLockValidatorCheckOrder(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
return VINF_SUCCESS;
/*
* Check it locks we're currently holding.
*/
/** @todo later */
/*
* If missing order rules, add them.
*/
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
/*
* Locate the entry for this thread in the table.
*/
/*
* Check the release order.
*/
{
/** @todo order validation */
}
/*
* Release the ownership or unwind a level of recursion.
*/
pEntry->cRecursion--;
else
return VINF_SUCCESS;
}
RTDECL(int) RTLockValidatorRecordRecursion(PRTLOCKVALIDATORREC pRec, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
return VINF_SUCCESS;
pRec->cRecursion++;
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
pRec->cRecursion--;
return VINF_SUCCESS;
}
RTDECL(int) RTLockValidatorRecordReadWriteRecursion(PRTLOCKVALIDATORREC pWrite, PRTLOCKVALIDATORSHARED pRead, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
return VINF_SUCCESS;
pWrite->cRecursion++;
return VINF_SUCCESS;
}
RTDECL(int) RTLockValidatorUnwindReadWriteRecursion(PRTLOCKVALIDATORREC pWrite, PRTLOCKVALIDATORSHARED pRead)
{
return VINF_SUCCESS;
pWrite->cRecursion--;
return VINF_SUCCESS;
}
RTDECL(RTTHREAD) RTLockValidatorSetOwner(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
return VINF_SUCCESS;
if (hThread == NIL_RTTHREAD)
{
}
pRec->cRecursion++;
else
{
/*
* Update the record.
*/
/*
* Push the lock onto the lock stack.
*/
/** @todo push it onto the per-thread lock stack. */
}
return hThread;
}
{
return VINF_SUCCESS;
{
/*
* Pop (remove) the lock.
*/
/*
* Update the record.
*/
}
return pThread;
}
RTDECL(void) RTLockValidatorAddReadOwner(PRTLOCKVALIDATORSHARED pRead, RTTHREAD hThread, PCRTLOCKVALIDATORSRCPOS pSrcPos)
{
return;
/*
* Recursive?
*
* Note! This code can be optimized to try avoid scanning the table on
* insert. However, that's annoying work that makes the code big,
* so it can wait til later sometime.
*/
if (pEntry)
{
pEntry->cRecursion++;
return;
}
/*
* Allocate a new thread entry and insert it into the table.
*/
if ( pEntry
}
{
return;
/*
* Find the entry hope it's a recursive one.
*/
pEntry->cRecursion--;
else
}
{
if (Thread == NIL_RTTHREAD)
return 0;
if (!pThread)
return VERR_INVALID_HANDLE;
return cWriteLocks;
}
{
}
{
}
{
if (Thread == NIL_RTTHREAD)
return 0;
if (!pThread)
return VERR_INVALID_HANDLE;
return cReadLocks;
}
{
}
{
}
/**
* Bitch about a deadlock.
*
* @param pRec The lock validator record we're going to block on.
* @param pThread This thread.
* @param pCur The thread we're deadlocking with.
* @param enmState The sleep state.
* @param pSrcPos Where we are going to deadlock.
*/
static void rtLockValidatorComplainAboutDeadlock(PRTLOCKVALIDATORREC pRec, PRTTHREADINT pThread, RTTHREADSTATE enmState,
{
RTAssertMsg1Weak(pCur == pThread ? "!!Deadlock detected!!" : "!!Deadlock exists!!", pSrcPos->uLine, pSrcPos->pszFile, pSrcPos->pszFunction);
/*
* Print the threads and locks involved.
*/
unsigned iSeenThread = 0;
{
/*
* Print info on pCur. Determin next while doing so.
*/
RTAssertMsg2Weak(" #%u: %RTthrd/%RTnthrd %s: %s(%u) %RTptr\n",
switch (enmCurState)
{
case RTTHREADSTATE_CRITSECT:
case RTTHREADSTATE_EVENT:
case RTTHREADSTATE_FAST_MUTEX:
case RTTHREADSTATE_MUTEX:
case RTTHREADSTATE_RW_READ:
case RTTHREADSTATE_RW_WRITE:
case RTTHREADSTATE_SPIN_MUTEX:
{
if (enmCurState2 != enmCurState)
{
RTAssertMsg2Weak(" Impossible!!! enmState=%s -> %s (%d)\n",
break;
}
{
RTAssertMsg2Weak(" Waiting on %s %p [%s]: Entered %s(%u) %s %p\n",
}
RTAssertMsg2Weak(" Waiting on %s pCurRec=%p: invalid magic number: %#x\n",
else
RTAssertMsg2Weak(" Waiting on %s pCurRec=%p: invalid pointer\n",
break;
}
default:
break;
}
/*
* Check for cycle.
*/
break;
for (unsigned i = 0; i < RT_ELEMENTS(apSeenThreads); i++)
if (apSeenThreads[i] == pCur)
{
RTAssertMsg2Weak(" Cycle!\n");
break;
}
/*
* Advance to the next thread.
*/
}
}
RTDECL(int) RTLockValidatorCheckWriteOrderBlocking(PRTLOCKVALIDATORREC pWrite, PRTLOCKVALIDATORSHARED pRead,
{
/*
* Fend off wild life.
*/
return VINF_SUCCESS;
/*
* Check for attempts at doing a read upgrade.
*/
if (pEntry)
{
AssertMsgFailed(("Read lock upgrade at %s(%d) %s %p!\nRead lock take at %s(%d) %s %p!\n",
return VERR_SEM_LV_UPGRADE;
}
return VINF_SUCCESS;
}
RTDECL(int) RTLockValidatorCheckReadOrderBlocking(PRTLOCKVALIDATORSHARED pRead, PRTLOCKVALIDATORREC pWrite,
{
/*
* Fend off wild life.
*/
return VINF_SUCCESS;
return VINF_SUCCESS;
}
{
/*
* Fend off wild life.
*/
return VINF_SUCCESS;
/*
* Record the location and everything before changing the state and
* performing deadlock detection.
*/
/*
* Don't do deadlock detection if we're recursing and that's OK.
*
* On some hosts we don't do recursion accounting our selves and there
* isn't any other place to check for this. semmutex-win.cpp for instance.
*/
{
if (fRecursiveOk)
return VINF_SUCCESS;
return VERR_SEM_LV_NESTED;
}
/*
* Do deadlock detection.
*
* Since we're missing proper serialization, we don't declare it a
* deadlock until we've got three runs with the same list length.
* While this isn't perfect, it should avoid out the most obvious
* races on SMP boxes.
*/
unsigned cPrevLength = ~0U;
unsigned cEqualRuns = 0;
unsigned iParanoia = 256;
do
{
unsigned cLength = 0;
for (;;)
{
/*
* Get the next thread.
*/
for (;;)
{
switch (enmCurState)
{
case RTTHREADSTATE_CRITSECT:
case RTTHREADSTATE_EVENT:
case RTTHREADSTATE_FAST_MUTEX:
case RTTHREADSTATE_MUTEX:
case RTTHREADSTATE_RW_READ:
case RTTHREADSTATE_RW_WRITE:
case RTTHREADSTATE_SPIN_MUTEX:
{
continue;
continue;
break;
}
default:
break;
}
break;
}
/*
* If we arrive at the end of the list we're good.
*/
if (!pCur)
{
return VINF_SUCCESS;
}
/*
* If we've got back to the blocking thread id we've
* got a deadlock.
*/
break;
/*
* If we've got a chain of more than 256 items, there is some
* kind of cycle in the list, which means that there is already
* a deadlock somewhere.
*/
if (cLength >= 256)
break;
cLength++;
}
/* compare with previous list run. */
if (cLength != cPrevLength)
{
cEqualRuns = 0;
}
else
cEqualRuns++;
/*
* Ok, if we ever get here, it's most likely a genuine deadlock.
*/
return VERR_SEM_LV_DEADLOCK;
}
{
}
RTDECL(bool) RTLockValidatorIsEnabled(void)
{
return ASMAtomicUoReadBool(&g_fLockValidatorEnabled);
}