Enhance mutex initialization to prevent possible race conditions between sqlite3_initialize() and sqlite3_config(). Also, re-check sqlite3GlobalConfig.isInit after the mutex subsystem has been initialized.

FossilOrigin-Name: f6a8f577957769171acd72df3cc9aa5ad474d84b
This commit is contained in:
mistachkin
2015-09-11 05:06:15 +00:00
parent 12b3b89542
commit 8bdc914af3
9 changed files with 143 additions and 32 deletions
+1
View File
@@ -174,6 +174,7 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = {
128, /* szLookaside */
500, /* nLookaside */
{0,0,0,0,0,0,0,0}, /* m */
(void*)0, /* pMutex */
{0,0,0,0,0,0,0,0,0}, /* mutex */
{0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
(void*)0, /* pHeap */
+16 -3
View File
@@ -171,7 +171,13 @@ int sqlite3_initialize(void){
*/
MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
sqlite3_mutex_enter(pMaster);
sqlite3GlobalConfig.isMutexInit = 1;
if( sqlite3GlobalConfig.isInit ){
assert( sqlite3GlobalConfig.isMutexInit );
assert( sqlite3GlobalConfig.isMallocInit );
sqlite3_mutex_leave(pMaster);
return SQLITE_OK;
}
sqlite3GlobalConfig.isMutexInit = 1; /* possibly redundant */
if( !sqlite3GlobalConfig.isMallocInit ){
rc = sqlite3MallocInit();
}
@@ -384,8 +390,15 @@ int sqlite3_config(int op, ...){
#endif
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
case SQLITE_CONFIG_MUTEX: {
/* Specify an alternative mutex implementation */
sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
/* Atomically compare-and-swap the mutex implementation pointer to
* help prevent a race condition with sqlite3MutexInit(). */
if( sqlite3CompareAndSwap((void * volatile *)&sqlite3GlobalConfig.pMutex,
0, &sqlite3GlobalConfig.mutex)==0 ){
/* Specify an alternative mutex implementation */
sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
}else{
rc = SQLITE_ERROR;
}
break;
}
#endif
+24 -13
View File
@@ -22,39 +22,50 @@
** allocate a mutex while the system is uninitialized.
*/
static SQLITE_WSD int mutexIsInit = 0;
#endif /* SQLITE_DEBUG */
#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
#ifndef SQLITE_MUTEX_OMIT
/*
** Copies a mutex implementation. Both arguments must point to valid
** memory.
*/
static void mutexCopy(
sqlite3_mutex_methods *pTo,
sqlite3_mutex_methods const *pFrom
){
pTo->xMutexInit = pFrom->xMutexInit;
pTo->xMutexEnd = pFrom->xMutexEnd;
pTo->xMutexFree = pFrom->xMutexFree;
pTo->xMutexEnter = pFrom->xMutexEnter;
pTo->xMutexTry = pFrom->xMutexTry;
pTo->xMutexLeave = pFrom->xMutexLeave;
pTo->xMutexHeld = pFrom->xMutexHeld;
pTo->xMutexNotheld = pFrom->xMutexNotheld;
pTo->xMutexAlloc = pFrom->xMutexAlloc;
}
/*
** Initialize the mutex system.
*/
int sqlite3MutexInit(void){
int rc = SQLITE_OK;
if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
int rc;
if( sqlite3CompareAndSwap((void * volatile *)&sqlite3GlobalConfig.pMutex,
0, &sqlite3GlobalConfig.mutex)==0 ){
/* If the xMutexAlloc method has not been set, then the user did not
** install a mutex implementation via sqlite3_config() prior to
** sqlite3_initialize() being called. This block copies pointers to
** the default implementation into the sqlite3GlobalConfig structure.
*/
sqlite3_mutex_methods const *pFrom;
sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
if( sqlite3GlobalConfig.bCoreMutex ){
pFrom = sqlite3DefaultMutex();
}else{
pFrom = sqlite3NoopMutex();
}
pTo->xMutexInit = pFrom->xMutexInit;
pTo->xMutexEnd = pFrom->xMutexEnd;
pTo->xMutexFree = pFrom->xMutexFree;
pTo->xMutexEnter = pFrom->xMutexEnter;
pTo->xMutexTry = pFrom->xMutexTry;
pTo->xMutexLeave = pFrom->xMutexLeave;
pTo->xMutexHeld = pFrom->xMutexHeld;
pTo->xMutexNotheld = pFrom->xMutexNotheld;
mutexCopy(&sqlite3GlobalConfig.mutex, pFrom);
sqlite3MemoryBarrier();
pTo->xMutexAlloc = pFrom->xMutexAlloc;
}
rc = sqlite3GlobalConfig.mutex.xMutexInit();
+22
View File
@@ -29,6 +29,28 @@
#ifndef SQLITE_MUTEX_OMIT
/*
** Try to provide an atomic compare-and-swap operation on a void pointer,
** needed for initialization only.
*/
void *sqlite3NoopCompareAndSwap(
void *volatile *pCurVal,
void *cmpVal,
void *swapVal
){
/*
** This platform may not have a way to perform an atomic compare-and-swap
** operation; therefore, use the fallback algorithm.
**
** WARNING: This code is almost certainly not thread-safe.
*/
void *oldVal = *pCurVal;
if( oldVal==cmpVal ){
*pCurVal = swapVal;
}
return oldVal;
}
#ifndef SQLITE_DEBUG
/*
** Stub routines for all mutex methods.
+29 -1
View File
@@ -86,11 +86,39 @@ static int pthreadMutexNotheld(sqlite3_mutex *p){
void sqlite3MemoryBarrier(void){
#if defined(SQLITE_MEMORY_BARRIER)
SQLITE_MEMORY_BARRIER;
#elif defined(__GNUC__)
#elif defined(__GNUC__) && GCC_VERSION>=4001000
__sync_synchronize();
#endif
}
/*
** Try to provide an atomic compare-and-swap operation on a void pointer,
** needed for initialization only.
*/
void *sqlite3CompareAndSwap(
void *volatile *pCurVal,
void *cmpVal,
void *swapVal
){
#if defined(SQLITE_COMPARE_AND_SWAP)
return SQLITE_COMPARE_AND_SWAP(pCurVal, cmpVal, swapVal);
#elif defined(__GNUC__) && GCC_VERSION>=4001000 && SQLITE_PTRSIZE>4
return (void *)__sync_val_compare_and_swap_8(
(u64 volatile *)pCurVal, (u64)cmpVal, (u64)swapVal);
#elif defined(__GNUC__) && GCC_VERSION>=4001000
return (void *)__sync_val_compare_and_swap_4(
(u32 volatile *)pCurVal, (u32)cmpVal, (u32)swapVal);
#else
/*
** This platform may not have a way to perform an atomic compare-and-swap
** operation; therefore, use the fallback algorithm.
**
** WARNING: This code is almost certainly not thread-safe.
*/
return sqlite3NoopCompareAndSwap(pCurVal, cmpVal, swapVal);
#endif
}
/*
** Initialize and deinitialize the mutex subsystem.
*/
+22
View File
@@ -90,6 +90,28 @@ void sqlite3MemoryBarrier(void){
#endif
}
/*
** Try to provide an atomic compare-and-swap operation on a void pointer,
** needed for initialization only.
*/
void *sqlite3CompareAndSwap(
void * volatile *pCurVal,
void *cmpVal,
void *swapVal
){
#if defined(SQLITE_COMPARE_AND_SWAP)
return SQLITE_COMPARE_AND_SWAP(pCurVal, cmpVal, swapVal);
#elif SQLITE_PTRSIZE>4
return (void *)InterlockedCompareExchange64(
(LONGLONG SQLITE_WIN32_VOLATILE *)pCurVal, (LONGLONG)cmpVal,
(LONGLONG)swapVal);
#else
return (void *)InterlockedCompareExchange(
(LONG SQLITE_WIN32_VOLATILE *)pCurVal, (LONG)cmpVal,
(LONG)swapVal);
#endif
}
/*
** Initialize and deinitialize the mutex subsystem.
*/
+12 -1
View File
@@ -2937,6 +2937,7 @@ struct Sqlite3Config {
int szLookaside; /* Default lookaside buffer size */
int nLookaside; /* Default lookaside buffer count */
sqlite3_mem_methods m; /* Low-level memory allocation interface */
sqlite3_mutex_methods *pMutex; /* Address of mutex member or zero. */
sqlite3_mutex_methods mutex; /* Low-level mutex interface */
sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
void *pHeap; /* Heap storage space */
@@ -3188,6 +3189,7 @@ const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
#ifndef SQLITE_MUTEX_OMIT
sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
sqlite3_mutex_methods const *sqlite3NoopMutex(void);
void *sqlite3NoopCompareAndSwap(void * volatile *, void *, void *);
sqlite3_mutex *sqlite3MutexAlloc(int);
int sqlite3MutexInit(void);
int sqlite3MutexEnd(void);
@@ -3195,7 +3197,16 @@ const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
void sqlite3MemoryBarrier(void);
#else
# define sqlite3MemoryBarrier();
# define sqlite3MemoryBarrier()
#endif
#if !defined(SQLITE_MUTEX_OMIT)
# if !defined(SQLITE_MUTEX_NOOP)
void *sqlite3CompareAndSwap(void * volatile *, void *, void *);
# else
# define sqlite3CompareAndSwap sqlite3NoopCompareAndSwap
# endif
#else
# define sqlite3CompareAndSwap(x,y,z)
#endif
sqlite3_int64 sqlite3StatusValue(int);