From: danielk1977 Date: Wed, 18 Jan 2006 18:22:43 +0000 (+0000) Subject: Include the ThreadData structure in internal malloc() testing. Also fixes for reveale... X-Git-Tag: version-3.6.10~3193 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=76e8d1a29270e5edbfc14c7dfd36bdc0376cd41b;p=thirdparty%2Fsqlite.git Include the ThreadData structure in internal malloc() testing. Also fixes for revealed bugs. (CVS 2974) FossilOrigin-Name: c7b9d50d0a975912776aaa2b870308652f77e1e6 --- diff --git a/manifest b/manifest index 875eede5a4..c62c14ca82 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Modify\sthe\ssqlite3OsInMutex\sos-layer\sinterface\sto\stake\sa\ssingle\sparameter\nto\sdistinguish\sbetween\smutexes\sheld\sby\sthe\scurrent\sthread\sand\smutexes\sheld\nby\sany\sthread.\s\sPart\sof\sthe\sfix\sfor\sticket\s#1630.\s(CVS\s2973) -D 2006-01-18T17:25:46 +C Include\sthe\sThreadData\sstructure\sin\sinternal\smalloc()\stesting.\sAlso\sfixes\sfor\srevealed\sbugs.\s(CVS\s2974) +D 2006-01-18T18:22:43 F Makefile.in ab3ffd8d469cef4477257169b82810030a6bb967 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -55,25 +55,25 @@ F src/os.h e4637eadcd5a9ca079029078d31751ae295fb05e F src/os_common.h 95b29ca6f3e6636cb33c9219b3f91a96fa7224b1 F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3 -F src/os_unix.c 9d44bcfbc1e0997331acec827027637dbe9d7131 +F src/os_unix.c a242a9458b08f01fa11d42b23bcdb89a3fbf2a68 F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_win.c 98e4e38db7d4a00647b2bb1c60d28b7ca5034c03 F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b -F src/pager.c 651fda1756c2334009e14bfdc652aadbdbed4e8c +F src/pager.c ccdd092702decc02fd3a54380c6d46c546eea5a6 F src/pager.h e0acb095b3ad0bca48f2ab00c87346665643f64f F src/parse.y 83df51fea35f68f7e07384d75dce83d1ed30434c F src/pragma.c 4496cc77dc35824e1c978c3d1413b8a5a4c777d3 -F src/prepare.c 5aa661a35a18edd2e13f5c87263080781cf0fcf3 +F src/prepare.c 5d6f5b7194ee72cecd66cab49d15159e55d63f28 F src/printf.c f47a2f4b5387cd2ebb12e9117a1a5d6bd9a2b812 F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/select.c e80ac3438d64bfdb1116660309f578eb1efed6e1 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c 66b073375efbdee19045e7e0cd38b85f9aff71da F src/sqlite.h.in 492580f7e3ff71eb43193eb7bb98e2d549889ce3 -F src/sqliteInt.h ecb38c65affb3411fbd375fb14707000aab15aed +F src/sqliteInt.h 7ddd6141a57748363fe42119f165d06260996aa3 F src/table.c 486dcfce532685b53b5a2b5da8bba0ded6fb2316 F src/tclsqlite.c d650bea0248fc0a310ddc2cb94273a3a5021fddf -F src/test1.c 2ddf346a75069254ce24853fe970f8ee2812a371 +F src/test1.c 1c08d6533843687266d0d090d2c19bb0c9a9a907 F src/test2.c ca74a1d8aeb7d9606e8f6b762c5daf85c1a3f92b F src/test3.c 9742aa146eb750cab81c1d5605286c3a0eb88054 F src/test4.c 6633cb7b4a7429e938804a34f688d118c8b7f8e1 @@ -85,12 +85,12 @@ F src/tokenize.c 1ac078540c2074568e16c0e9beea836fb16b1adc F src/trigger.c 4d3644cbd16959b568c95ae73493402be8021b08 F src/update.c 14be4ba2f438919b4217085c02feff569e6cf1f2 F src/utf.c 5ab8ca05d4e9ec81174b010f01ab12a232f0087d -F src/util.c b916d21774e6259d86e875df399dbe1a28e69b8e +F src/util.c 3f09c6ffb1d0ea4efe17304c8c5c3612c5cd53fc F src/vacuum.c 3865673cc66acd0717ecd517f6b8fdb2a5e7924b -F src/vdbe.c 5af32281e96b2c3e29f50975a86b9f5eef044057 +F src/vdbe.c 2a3d5ea76ab166bd77de9e3a7dbeb4a9354a1603 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13 F src/vdbeInt.h 5451cf71f229e366ac543607c0a17f36e5737ea9 -F src/vdbeapi.c 08578a1d601bded62e829bc2f85281556f787333 +F src/vdbeapi.c b5a3eacce266a657cdc0fc740b60ba480fb88649 F src/vdbeaux.c 1b3fe25f69ec9a3616f8cd747f3b8b87a0a40f04 F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5 F src/vdbemem.c 53f25c5c537e4ded24549d6c8537e65d4efc42d1 @@ -341,7 +341,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P ac090f2ab3b5a792c2fdf897e10060f263e0d408 -R 789c880886342b50e95c468de56aedb9 -U drh -Z cf39b9ccff84563836ed01af4518e925 +P e1ad9df1cf46287f2a7789275e98d28e05880e17 +R d16f70b255799a1d6293d5f839dfd544 +U danielk1977 +Z d52ffd289093139bee40bd2d96a45c60 diff --git a/manifest.uuid b/manifest.uuid index f0fccb15ac..27c53c3816 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e1ad9df1cf46287f2a7789275e98d28e05880e17 \ No newline at end of file +c7b9d50d0a975912776aaa2b870308652f77e1e6 \ No newline at end of file diff --git a/src/os_unix.c b/src/os_unix.c index 0cee34299a..dfa33f2e2d 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -1782,7 +1782,12 @@ ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){ pTsd = pthread_getspecific(key); if( allocateFlag>0 ){ if( pTsd==0 ){ - pTsd = sqlite3OsMalloc(sizeof(zeroData)); + if( !sqlite3TestMallocFail() ){ + pTsd = sqlite3OsMalloc(sizeof(zeroData)); + } +#ifdef SQLITE_MEMDEBUG + sqlite3_isFail = 0; +#endif if( pTsd ){ *pTsd = zeroData; pthread_setspecific(key, pTsd); @@ -1801,7 +1806,12 @@ ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){ static ThreadData *pTsd = 0; if( allocateFlag>0 ){ if( pTsd==0 ){ - pTsd = sqlite3OsMalloc( sizeof(zeroData) ); + if( !sqlite3TestMallocFail() ){ + pTsd = sqlite3OsMalloc( sizeof(zeroData) ); + } +#ifdef SQLITE_MEMDEBUG + sqlite3_isFail = 0; +#endif if( pTsd ){ *pTsd = zeroData; TSD_COUNTER(+1); diff --git a/src/pager.c b/src/pager.c index 51a11064cf..27c7078b75 100644 --- a/src/pager.c +++ b/src/pager.c @@ -18,7 +18,7 @@ ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.244 2006/01/18 17:25:46 drh Exp $ +** @(#) $Id: pager.c,v 1.245 2006/01/18 18:22:43 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" @@ -1999,6 +1999,7 @@ int sqlite3pager_close(Pager *pPager){ ** so that ThreadData.nAlloc can be set. */ ThreadData *pTsd = sqlite3ThreadData(); + assert( pPager ); assert( pTsd && pTsd->nAlloc ); #endif diff --git a/src/prepare.c b/src/prepare.c index b60778365a..778169d240 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -13,7 +13,7 @@ ** interface, and routines that contribute to loading the database schema ** from disk. ** -** $Id: prepare.c,v 1.26 2006/01/18 16:51:35 danielk1977 Exp $ +** $Id: prepare.c,v 1.27 2006/01/18 18:22:43 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -520,6 +520,9 @@ int sqlite3_prepare( memset(&sParse, 0, sizeof(sParse)); sParse.db = db; sParse.pTsd = sqlite3ThreadData(); + if( !sParse.pTsd ){ + goto prepare_out; + } sParse.pTsd->nRef++; sqlite3RunParser(&sParse, zSql, &zErrMsg); @@ -554,6 +557,7 @@ int sqlite3_prepare( } #endif +prepare_out: if( sqlite3SafetyOff(db) ){ rc = SQLITE_MISUSE; } @@ -570,7 +574,9 @@ int sqlite3_prepare( sqlite3Error(db, rc, 0); } - sParse.pTsd->nRef--; + if( sParse.pTsd ){ + sParse.pTsd->nRef--; + } rc = sqlite3ApiExit(db, rc); sqlite3ReleaseThreadData(); return rc; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 45854dc54f..f62fd37098 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.474 2006/01/18 16:51:35 danielk1977 Exp $ +** @(#) $Id: sqliteInt.h,v 1.475 2006/01/18 18:22:43 danielk1977 Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -1759,7 +1759,9 @@ void sqlite3FailedMalloc(); #ifdef SQLITE_MEMDEBUG void sqlite3MallocDisallow(); void sqlite3MallocAllow(); + int sqlite3TestMallocFail(); #else + #define sqlite3TestMallocFail() 0 #define sqlite3MallocDisallow() #define sqlite3MallocAllow() #endif diff --git a/src/test1.c b/src/test1.c index 018b173913..06699009ba 100644 --- a/src/test1.c +++ b/src/test1.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.196 2006/01/18 16:51:35 danielk1977 Exp $ +** $Id: test1.c,v 1.197 2006/01/18 18:22:43 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -911,7 +911,7 @@ static int sqlite_malloc_outstanding( #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT if( objc==2 ){ - ThreadData *pTd = sqlite3ThreadData(); + ThreadData const *pTd = sqlite3ThreadDataReadOnly(); const char *zArg = Tcl_GetString(objv[1]); if( 0==strcmp(zArg, "-bytes") ){ Tcl_SetObjResult(interp, Tcl_NewIntObj(pTd->nAlloc)); diff --git a/src/util.c b/src/util.c index 53bd6001bd..3ba13913bf 100644 --- a/src/util.c +++ b/src/util.c @@ -14,7 +14,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.177 2006/01/18 17:25:46 drh Exp $ +** $Id: util.c,v 1.178 2006/01/18 18:22:43 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -72,7 +72,10 @@ ** value indicates no limit. */ void sqlite3_soft_heap_limit(int n){ - sqlite3ThreadData()->nSoftHeapLimit = n; + ThreadData *pTd = sqlite3ThreadData(); + if( pTd ){ + pTd->nSoftHeapLimit = n; + } sqlite3ReleaseThreadData(); } @@ -207,8 +210,7 @@ int sqlite3_iLine = 0; /* Line number for debug info */ ** Check for a simulated memory allocation failure. Return true if ** the failure should be simulated. Return false to proceed as normal. */ -static int failMalloc(){ - ThreadData *pTsd = sqlite3ThreadData(); +int sqlite3TestMallocFail(){ if( sqlite3_isFail ){ return 1; } @@ -316,7 +318,6 @@ static void *getOsPointer(void *p) ** of allocations. */ static void linkAlloc(void *p){ - ThreadData *pTsd = sqlite3ThreadData(); void **pp = (void **)p; pp[0] = 0; pp[1] = sqlite3_pFirst; @@ -332,7 +333,6 @@ static void linkAlloc(void *p){ */ static void unlinkAlloc(void *p) { - ThreadData *pTsd = sqlite3ThreadData(); void **pp = (void **)p; if( p==sqlite3_pFirst ){ assert(!pp[0]); @@ -365,7 +365,6 @@ static void relinkAlloc(void *p) if( pp[0] ){ ((void **)(pp[0]))[1] = p; }else{ - ThreadData *pTsd = sqlite3ThreadData(); sqlite3_pFirst = p; } if( pp[1] ){ @@ -397,7 +396,6 @@ static void relinkAlloc(void *p) #include int sqlite3OutstandingMallocs(Tcl_Interp *interp){ void *p; - ThreadData *pTsd = sqlite3ThreadData(); Tcl_Obj *pRes = Tcl_NewObj(); Tcl_IncrRefCount(pRes); @@ -445,11 +443,11 @@ int sqlite3OutstandingMallocs(Tcl_Interp *interp){ */ static void * OSMALLOC(int n){ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - ThreadData *pTsd = sqlite3ThreadData(); + ThreadData const *pTsd = sqlite3ThreadDataReadOnly(); sqlite3_nMaxAlloc = MAX(sqlite3_nMaxAlloc, pTsd->nAlloc); #endif assert( !sqlite3_mallocDisallowed ); - if( !failMalloc() ){ + if( !sqlite3TestMallocFail() ){ u32 *p; p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); assert(p); @@ -488,10 +486,13 @@ static void OSFREE(void *pFree){ static void * OSREALLOC(void *pRealloc, int n){ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT ThreadData *pTsd = sqlite3ThreadData(); + if( !pTsd ){ + return 0; + } sqlite3_nMaxAlloc = MAX(sqlite3_nMaxAlloc, pTsd->nAlloc); #endif assert( !sqlite3_mallocDisallowed ); - if( !failMalloc() ){ + if( !sqlite3TestMallocFail() ){ u32 *p = (u32 *)getOsPointer(pRealloc); checkGuards(p); p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); @@ -535,18 +536,21 @@ static void OSMALLOC_FAILED(){ ** a no-op */ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT -static void handleSoftLimit(int n){ +static int handleSoftLimit(int n){ ThreadData *pTsd = sqlite3ThreadData(); - pTsd->nAlloc += n; - assert( pTsd->nAlloc>=0 ); - if( n>0 && pTsd->nSoftHeapLimit>0 ){ - while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ); - }else if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){ - sqlite3ReleaseThreadData(); + if( pTsd ){ + pTsd->nAlloc += n; + assert( pTsd->nAlloc>=0 ); + if( n>0 && pTsd->nSoftHeapLimit>0 ){ + while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ); + }else if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){ + sqlite3ReleaseThreadData(); + } } + return (pTsd ? 0 : 1); } #else -#define handleSoftLimit(x) +#define handleSoftLimit(x) 0 #endif /* @@ -556,8 +560,7 @@ static void handleSoftLimit(int n){ */ void *sqlite3MallocRaw(int n){ void *p = 0; - if( n>0 && !sqlite3MallocFailed() ){ - handleSoftLimit(n); + if( n>0 && !sqlite3MallocFailed() && !handleSoftLimit(n) ){ while( !(p = OSMALLOC(n)) && sqlite3_release_memory(n) ); if( !p ){ /* If the allocation failed, call handleSoftLimit() again, this time @@ -587,17 +590,18 @@ void *sqlite3Realloc(void *p, int n){ return sqlite3Malloc(n); }else{ void *np = 0; - handleSoftLimit(n - OSSIZEOF(p)); - while( !(np = OSREALLOC(p, n)) && sqlite3_release_memory(n) ); - if( !np ){ - /* If the allocation failed, call handleSoftLimit() again, this time - ** with the additive inverse of the argument passed to - ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is - ** still correct after a malloc() failure. - */ - handleSoftLimit(OSSIZEOF(p) - n); - sqlite3FailedMalloc(); - OSMALLOC_FAILED(); + if( !handleSoftLimit(n - OSSIZEOF(p)) ){ + while( !(np = OSREALLOC(p, n)) && sqlite3_release_memory(n) ); + if( !np ){ + /* If the allocation failed, call handleSoftLimit() again, this time + ** with the additive inverse of the argument passed to + ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is + ** still correct after a malloc() failure. + */ + handleSoftLimit(OSSIZEOF(p) - n); + sqlite3FailedMalloc(); + OSMALLOC_FAILED(); + } } return np; } @@ -1322,7 +1326,11 @@ void *sqlite3TextToPtr(const char *z){ ** Return a pointer to the ThreadData associated with the calling thread. */ ThreadData *sqlite3ThreadData(){ - return (ThreadData*)sqlite3OsThreadSpecificData(1); + ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1); + if( !p ){ + sqlite3FailedMalloc(); + } + return p; } /* diff --git a/src/vdbe.c b/src/vdbe.c index a99f3354d6..ccc2ea1d70 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -43,7 +43,7 @@ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.532 2006/01/18 16:51:36 danielk1977 Exp $ +** $Id: vdbe.c,v 1.533 2006/01/18 18:22:43 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -408,6 +408,9 @@ int sqlite3VdbeExec( Mem *pStackLimit; #endif ThreadData *pTsd = sqlite3ThreadData(); + if( !pTsd ){ + goto no_mem; + } if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; pTsd->nRef++; @@ -4615,7 +4618,9 @@ vdbe_halt: } sqlite3VdbeHalt(p); p->pTos = pTos; - pTsd->nRef--; + if( pTsd ){ + pTsd->nRef--; + } return rc; /* Jump to here if a malloc() fails. It's hard to get a malloc() diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 19ab3474b2..4b5c13642e 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -242,7 +242,8 @@ int sqlite3_step(sqlite3_stmt *pStmt){ #endif sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); - return sqlite3ApiExit(p->db, rc); + p->rc = sqlite3ApiExit(p->db, p->rc); + return rc; } /*