Compare commits

...

12 Commits

Author SHA1 Message Date
mistachkin 4221e628c1 Merge updates from trunk.
FossilOrigin-Name: 6b85f8cd4d87d616a3cb362647ae3869c45c2801
2015-09-23 15:54:36 +00:00
mistachkin 5c870cb03c Experimental changes to avoid recusrively calling xMutexInit.
FossilOrigin-Name: c9400ff167d9520119081c9b92436794dcd53d22
2015-09-13 18:45:50 +00:00
mistachkin b25754a62d Merge updates from trunk.
FossilOrigin-Name: b42c2e2076da1a61f667876ff2c18e4ebed50a7f
2015-09-13 18:43:53 +00:00
mistachkin e7c4184348 Merge updates from trunk.
FossilOrigin-Name: d8051f611acabc2aaf8d23e8707c2a45961be416
2015-09-12 18:58:32 +00:00
mistachkin aa985b3208 Merge updates from trunk.
FossilOrigin-Name: 4859778900a99e7e50ce3d56910ad4c5469f2cdb
2015-09-12 16:59:38 +00:00
mistachkin 3f44c65513 Fix comments.
FossilOrigin-Name: de3fa51e931d34f341fc3432bfdf97390bb68d4e
2015-09-12 04:22:01 +00:00
mistachkin acc8f7b24c Specifying an invalid mutex implementation (via SQLITE_CONFIG_MUTEX) should cause the default one to be used instead.
FossilOrigin-Name: 1a97bc81ccfc88f2dd527dff16e9e395718675bc
2015-09-12 04:19:51 +00:00
mistachkin a4da3c17c3 Clarify the new mutex implementation reset logic in sqlite3_shutdown().
FossilOrigin-Name: f9a034834e6c6e32ac350cf48bec981490e533ad
2015-09-12 03:40:13 +00:00
mistachkin 89e57dd671 Add more asserts to the mutex subsystem.
FossilOrigin-Name: 7562f1fbede70b19bbbb928e0a534203528b5b65
2015-09-12 03:35:55 +00:00
mistachkin db17a83295 Fix typo in the Win32 implementation of sqlite3CompareAndSwap.
FossilOrigin-Name: 31a26a1dd796e073e847e1e9c636f1976ffbb085
2015-09-12 01:17:20 +00:00
mistachkin 4b3f0d6281 Make sure that the mutex implementation can be altered after calling sqlite3_shutdown().
FossilOrigin-Name: dc2cf8974337ca1ef705aee4efd4a96b91ca3edd
2015-09-11 23:24:50 +00:00
mistachkin 8bdc914af3 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
2015-09-11 05:06:15 +00:00
9 changed files with 185 additions and 35 deletions
+13 -13
View File
@@ -1,5 +1,5 @@
C Capture\sAFL-generated\sfuzz\stests\sfor\sjson1.c\sinto\sthe\stest/fuzzdata4.db\sfile.
D 2015-09-23T11:59:50.725
C Merge\supdates\sfrom\strunk.
D 2015-09-23T15:54:36.389
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in e1afa6fb2de2bddd50e0ddae8166c2ee9d69b301
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -296,7 +296,7 @@ F src/expr.c 3a76afcdac925294c39903b7002ddb9e5fd29863
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
F src/fkey.c 83e1baba999bed3144ea5a2143fc922edf51135f
F src/func.c ecdd69ec6a1e406f04cc73324be2ebbf6354197f
F src/global.c 508e4087f7b41d688e4762dcf4d4fe28cfbc87f9
F src/global.c d824163680839be79b53204105a78e5be5d4ac4a
F src/hash.c 4263fbc955f26c2e8cdc0cf214bc42435aa4e4f5
F src/hash.h c8f3c31722cf3277d03713909761e152a5b81094
F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08
@@ -305,7 +305,7 @@ F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
F src/legacy.c ba1863ea58c4c840335a84ec276fc2b25e22bc4e
F src/lempar.c d344a95d60c24e2f490ee59db9784b1b17439012
F src/loadext.c f0b66d28e377fd6c6d36cc9d92df1ff251ebee44
F src/main.c e17fcffae4306a9b8334faf3bac80d7396850b54
F src/main.c 9f196a4364270af28ab97c1d62046e56e32dd11d
F src/malloc.c 3a37ce6979a40f499d8cea9e9ab4e8517854d35d
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c abe6ee469b6c5a35c7f22bfeb9c9bac664a1c987
@@ -314,11 +314,11 @@ F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534
F src/mem5.c 61eeb90134f9a5be6c2e68d8daae7628b25953fb
F src/memjournal.c 3eb2c0b51adbd869cb6a44780323f05fa904dc85
F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495
F src/mutex.c 8e45800ee78e0cd1f1f3fe8e398853307f4a085c
F src/mutex.c b0fbea2cda9f8e81444f5f1b4b9f07c1a904269e
F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85
F src/mutex_noop.c 9d4309c075ba9cc7249e19412d3d62f7f94839c4
F src/mutex_unix.c 8cfa6e83c618d2fcae0fe63f4d2b5bb16b11a97a
F src/mutex_w32.c 2e025e6642eaf27597403690980f560d1a91f62c
F src/mutex_noop.c f03e26ba8258399da23b51234f6b6a97197c1900
F src/mutex_unix.c a72043f2560147d8e85fe48a6aef682896deb3a0
F src/mutex_w32.c 928c889408e3196f325d23fa4d115b3bc59d8eb6
F src/notify.c 9711a7575036f0d3040ba61bc6e217f13a9888e7
F src/os.c 8fd25588eeba74068d41102d26810e216999b6c8
F src/os.h 3e57a24e2794a94d3cf2342c6d9a884888cd96bf
@@ -345,7 +345,7 @@ F src/shell.c 6332ef06db1390ef812cfdff1fc97b4fd76cdd42
F src/sqlite.h.in 4e06cb5fed36736242c66237991db6f3e2395eb9
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 64350bf36833a56ad675e27392a913f417c5c308
F src/sqliteInt.h 5afc6e50402be1e0a870f28e1cd8b32eb9db590f
F src/sqliteInt.h ed65bcb0a6a7c11978c8f75d5476d6edaa024e8d
F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46
F src/status.c f266ad8a2892d659b74f0f50cb6a88b6e7c12179
F src/table.c 51b46b2a62d1b3a959633d593b89bab5e2c9155e
@@ -1388,7 +1388,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 7c7a3f3e9bc2f7a7334d8d6eae183d83f22097be
R 64646de8f025fcbfbac14050e58f6d8a
U drh
Z 8d3625ca1ecc6df3eff60cf48fea0eb8
P c9400ff167d9520119081c9b92436794dcd53d22 10a214fdb3c13b25e0bdd15c975c21c8890c47ee
R b42897c3c8ae6a403b71a2f426cb59b5
U mistachkin
Z 6c86ab7133863d50d8bbe949c96c8dea
+1 -1
View File
@@ -1 +1 @@
10a214fdb3c13b25e0bdd15c975c21c8890c47ee
6b85f8cd4d87d616a3cb362647ae3869c45c2801
+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 */
+27 -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();
}
@@ -329,6 +335,17 @@ int sqlite3_shutdown(void){
sqlite3GlobalConfig.isMutexInit = 0;
}
/*
** Force the state of the mutex subsystem to be completely reset now, even
** if the configured xMutexEnd(), if any, failed. This is not thread-safe.
** This is necessary even if the xMutexInit() was never called, due to the
** possiblity of this state being changed via SQLITE_CONFIG_MUTEX. After
** this point, the application must enable any custom mutex implementation
** again via SQLITE_CONFIG_MUTEX, if necessary.
*/
sqlite3GlobalConfig.pMutex = 0;
memset(&sqlite3GlobalConfig.mutex, 0, sizeof(sqlite3_mutex_methods));
return SQLITE_OK;
}
@@ -384,8 +401,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
+60 -18
View File
@@ -26,38 +26,80 @@ static SQLITE_WSD int mutexIsInit = 0;
#ifndef SQLITE_MUTEX_OMIT
/*
** This structure is for use by mutexIsInvalid() only. It represents an
** invalid mutex implementation (i.e. one where all the function pointers
** are null).
*/
static const sqlite3_mutex_methods mutexNullMethods = {
0, /* xMutexInit */
0, /* xMutexEnd */
0, /* xMutexAlloc */
0, /* xMutexFree */
0, /* xMutexEnter */
0, /* xMutexTry */
0, /* xMutexLeave */
0, /* xMutexHeld */
0 /* xMutexNotheld */
};
/*
** Returns non-zero if the currently configured mutex implemention is
** invalid (i.e. all of its function pointers are null).
*/
static int mutexIsInvalid(void){
return memcmp(&sqlite3GlobalConfig.mutex, &mutexNullMethods,
sizeof(sqlite3_mutex_methods))==0;
}
/*
** 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 ){
/* 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.
static int initPending = 0;
int rc;
if( sqlite3CompareAndSwap((void * volatile *)&sqlite3GlobalConfig.pMutex,
0, &sqlite3GlobalConfig.mutex)==0 || mutexIsInvalid() ){
/* If the mutex implementation pointer 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 the pointers
** for 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;
}
assert( sqlite3GlobalConfig.mutex.xMutexInit );
rc = sqlite3GlobalConfig.mutex.xMutexInit();
if( !initPending ){
assert( sqlite3GlobalConfig.mutex.xMutexInit );
initPending = 1;
rc = sqlite3GlobalConfig.mutex.xMutexInit();
initPending = 0;
}
#ifdef SQLITE_DEBUG
GLOBAL(int, mutexIsInit) = 1;
+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.
+28
View File
@@ -91,6 +91,34 @@ 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 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)swapVal,
(LONGLONG)cmpVal);
#else
return (void *)InterlockedCompareExchange(
(LONG SQLITE_WIN32_VOLATILE *)pCurVal, (LONG)swapVal,
(LONG)cmpVal);
#endif
}
/*
** Initialize and deinitialize the mutex subsystem.
*/
+11
View File
@@ -2938,6 +2938,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 */
@@ -3189,6 +3190,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);
@@ -3198,6 +3200,15 @@ const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
#else
# 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);
void sqlite3StatusUp(int, int);