| Index: third_party/sqlite/src/src/malloc.c
|
| diff --git a/third_party/sqlite/src/src/malloc.c b/third_party/sqlite/src/src/malloc.c
|
| index 50fdf524c5242f728c37cf782975204e3a881089..6fb9d53d1b6c054ca04af84a9d440c0100650aeb 100644
|
| --- a/third_party/sqlite/src/src/malloc.c
|
| +++ b/third_party/sqlite/src/src/malloc.c
|
| @@ -130,7 +130,8 @@ sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
|
| sqlite3_int64 priorLimit;
|
| sqlite3_int64 excess;
|
| #ifndef SQLITE_OMIT_AUTOINIT
|
| - sqlite3_initialize();
|
| + int rc = sqlite3_initialize();
|
| + if( rc ) return -1;
|
| #endif
|
| sqlite3_mutex_enter(mem0.mutex);
|
| priorLimit = mem0.alarmThreshold;
|
| @@ -266,7 +267,7 @@ static int mallocWithAlarm(int n, void **pp){
|
| sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
|
| if( mem0.alarmCallback!=0 ){
|
| int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
|
| - if( nUsed+nFull >= mem0.alarmThreshold ){
|
| + if( nUsed >= mem0.alarmThreshold - nFull ){
|
| mem0.nearlyFull = 1;
|
| sqlite3MallocAlarm(nFull);
|
| }else{
|
| @@ -293,11 +294,9 @@ static int mallocWithAlarm(int n, void **pp){
|
| ** Allocate memory. This routine is like sqlite3_malloc() except that it
|
| ** assumes the memory subsystem has already been initialized.
|
| */
|
| -void *sqlite3Malloc(int n){
|
| +void *sqlite3Malloc(u64 n){
|
| void *p;
|
| - if( n<=0 /* IMP: R-65312-04917 */
|
| - || n>=0x7fffff00
|
| - ){
|
| + if( n==0 || n>=0x7fffff00 ){
|
| /* A memory allocation of a number of bytes which is near the maximum
|
| ** signed integer value might cause an integer overflow inside of the
|
| ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
|
| @@ -306,12 +305,12 @@ void *sqlite3Malloc(int n){
|
| p = 0;
|
| }else if( sqlite3GlobalConfig.bMemstat ){
|
| sqlite3_mutex_enter(mem0.mutex);
|
| - mallocWithAlarm(n, &p);
|
| + mallocWithAlarm((int)n, &p);
|
| sqlite3_mutex_leave(mem0.mutex);
|
| }else{
|
| - p = sqlite3GlobalConfig.m.xMalloc(n);
|
| + p = sqlite3GlobalConfig.m.xMalloc((int)n);
|
| }
|
| - assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
|
| + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
|
| return p;
|
| }
|
|
|
| @@ -324,6 +323,12 @@ void *sqlite3_malloc(int n){
|
| #ifndef SQLITE_OMIT_AUTOINIT
|
| if( sqlite3_initialize() ) return 0;
|
| #endif
|
| + return n<=0 ? 0 : sqlite3Malloc(n);
|
| +}
|
| +void *sqlite3_malloc64(sqlite3_uint64 n){
|
| +#ifndef SQLITE_OMIT_AUTOINIT
|
| + if( sqlite3_initialize() ) return 0;
|
| +#endif
|
| return sqlite3Malloc(n);
|
| }
|
|
|
| @@ -351,22 +356,20 @@ void *sqlite3ScratchMalloc(int n){
|
| assert( n>0 );
|
|
|
| sqlite3_mutex_enter(mem0.mutex);
|
| + sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
|
| if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
|
| p = mem0.pScratchFree;
|
| mem0.pScratchFree = mem0.pScratchFree->pNext;
|
| mem0.nScratchFree--;
|
| sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
|
| - sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
|
| sqlite3_mutex_leave(mem0.mutex);
|
| }else{
|
| - if( sqlite3GlobalConfig.bMemstat ){
|
| - sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
|
| - n = mallocWithAlarm(n, &p);
|
| - if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
|
| - sqlite3_mutex_leave(mem0.mutex);
|
| - }else{
|
| + sqlite3_mutex_leave(mem0.mutex);
|
| + p = sqlite3Malloc(n);
|
| + if( sqlite3GlobalConfig.bMemstat && p ){
|
| + sqlite3_mutex_enter(mem0.mutex);
|
| + sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
|
| sqlite3_mutex_leave(mem0.mutex);
|
| - p = sqlite3GlobalConfig.m.xMalloc(n);
|
| }
|
| sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
|
| }
|
| @@ -432,7 +435,7 @@ void sqlite3ScratchFree(void *p){
|
| */
|
| #ifndef SQLITE_OMIT_LOOKASIDE
|
| static int isLookaside(sqlite3 *db, void *p){
|
| - return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
|
| + return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
|
| }
|
| #else
|
| #define isLookaside(A,B) 0
|
| @@ -444,28 +447,37 @@ static int isLookaside(sqlite3 *db, void *p){
|
| */
|
| int sqlite3MallocSize(void *p){
|
| assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
| - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
|
| return sqlite3GlobalConfig.m.xSize(p);
|
| }
|
| int sqlite3DbMallocSize(sqlite3 *db, void *p){
|
| - assert( db==0 || sqlite3_mutex_held(db->mutex) );
|
| - if( db && isLookaside(db, p) ){
|
| - return db->lookaside.sz;
|
| + if( db==0 ){
|
| + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
| + return sqlite3MallocSize(p);
|
| }else{
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
|
| - assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
|
| - return sqlite3GlobalConfig.m.xSize(p);
|
| + assert( sqlite3_mutex_held(db->mutex) );
|
| + if( isLookaside(db, p) ){
|
| + return db->lookaside.sz;
|
| + }else{
|
| + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| + return sqlite3GlobalConfig.m.xSize(p);
|
| + }
|
| }
|
| }
|
| +sqlite3_uint64 sqlite3_msize(void *p){
|
| + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
| + return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
|
| +}
|
|
|
| /*
|
| ** Free memory previously obtained from sqlite3Malloc().
|
| */
|
| void sqlite3_free(void *p){
|
| if( p==0 ) return; /* IMP: R-49053-54554 */
|
| - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
|
| assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
|
| if( sqlite3GlobalConfig.bMemstat ){
|
| sqlite3_mutex_enter(mem0.mutex);
|
| sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
|
| @@ -478,26 +490,39 @@ void sqlite3_free(void *p){
|
| }
|
|
|
| /*
|
| +** Add the size of memory allocation "p" to the count in
|
| +** *db->pnBytesFreed.
|
| +*/
|
| +static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
|
| + *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
|
| +}
|
| +
|
| +/*
|
| ** Free memory that might be associated with a particular database
|
| ** connection.
|
| */
|
| void sqlite3DbFree(sqlite3 *db, void *p){
|
| assert( db==0 || sqlite3_mutex_held(db->mutex) );
|
| + if( p==0 ) return;
|
| if( db ){
|
| if( db->pnBytesFreed ){
|
| - *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
|
| + measureAllocationSize(db, p);
|
| return;
|
| }
|
| if( isLookaside(db, p) ){
|
| LookasideSlot *pBuf = (LookasideSlot*)p;
|
| +#if SQLITE_DEBUG
|
| + /* Trash all content in the buffer being freed */
|
| + memset(p, 0xaa, db->lookaside.sz);
|
| +#endif
|
| pBuf->pNext = db->lookaside.pFree;
|
| db->lookaside.pFree = pBuf;
|
| db->lookaside.nOut--;
|
| return;
|
| }
|
| }
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
|
| sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
| sqlite3_free(p);
|
| @@ -506,14 +531,16 @@ void sqlite3DbFree(sqlite3 *db, void *p){
|
| /*
|
| ** Change the size of an existing memory allocation
|
| */
|
| -void *sqlite3Realloc(void *pOld, int nBytes){
|
| - int nOld, nNew;
|
| +void *sqlite3Realloc(void *pOld, u64 nBytes){
|
| + int nOld, nNew, nDiff;
|
| void *pNew;
|
| + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
|
| if( pOld==0 ){
|
| - return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
|
| + return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
|
| }
|
| - if( nBytes<=0 ){
|
| - sqlite3_free(pOld); /* IMP: R-31593-10574 */
|
| + if( nBytes==0 ){
|
| + sqlite3_free(pOld); /* IMP: R-26507-47431 */
|
| return 0;
|
| }
|
| if( nBytes>=0x7fffff00 ){
|
| @@ -524,21 +551,20 @@ void *sqlite3Realloc(void *pOld, int nBytes){
|
| /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
|
| ** argument to xRealloc is always a value returned by a prior call to
|
| ** xRoundup. */
|
| - nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
|
| + nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
|
| if( nOld==nNew ){
|
| pNew = pOld;
|
| }else if( sqlite3GlobalConfig.bMemstat ){
|
| sqlite3_mutex_enter(mem0.mutex);
|
| - sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
|
| - if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
|
| - mem0.alarmThreshold ){
|
| - sqlite3MallocAlarm(nNew-nOld);
|
| + sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
|
| + nDiff = nNew - nOld;
|
| + if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
|
| + mem0.alarmThreshold-nDiff ){
|
| + sqlite3MallocAlarm(nDiff);
|
| }
|
| - assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
|
| - assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
|
| pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
| if( pNew==0 && mem0.alarmCallback ){
|
| - sqlite3MallocAlarm(nBytes);
|
| + sqlite3MallocAlarm((int)nBytes);
|
| pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
| }
|
| if( pNew ){
|
| @@ -549,7 +575,7 @@ void *sqlite3Realloc(void *pOld, int nBytes){
|
| }else{
|
| pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
| }
|
| - assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
|
| + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
|
| return pNew;
|
| }
|
|
|
| @@ -561,6 +587,13 @@ void *sqlite3_realloc(void *pOld, int n){
|
| #ifndef SQLITE_OMIT_AUTOINIT
|
| if( sqlite3_initialize() ) return 0;
|
| #endif
|
| + if( n<0 ) n = 0; /* IMP: R-26507-47431 */
|
| + return sqlite3Realloc(pOld, n);
|
| +}
|
| +void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
|
| +#ifndef SQLITE_OMIT_AUTOINIT
|
| + if( sqlite3_initialize() ) return 0;
|
| +#endif
|
| return sqlite3Realloc(pOld, n);
|
| }
|
|
|
| @@ -568,10 +601,10 @@ void *sqlite3_realloc(void *pOld, int n){
|
| /*
|
| ** Allocate and zero memory.
|
| */
|
| -void *sqlite3MallocZero(int n){
|
| +void *sqlite3MallocZero(u64 n){
|
| void *p = sqlite3Malloc(n);
|
| if( p ){
|
| - memset(p, 0, n);
|
| + memset(p, 0, (size_t)n);
|
| }
|
| return p;
|
| }
|
| @@ -580,10 +613,10 @@ void *sqlite3MallocZero(int n){
|
| ** Allocate and zero memory. If the allocation fails, make
|
| ** the mallocFailed flag in the connection pointer.
|
| */
|
| -void *sqlite3DbMallocZero(sqlite3 *db, int n){
|
| +void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
|
| void *p = sqlite3DbMallocRaw(db, n);
|
| if( p ){
|
| - memset(p, 0, n);
|
| + memset(p, 0, (size_t)n);
|
| }
|
| return p;
|
| }
|
| @@ -606,7 +639,7 @@ void *sqlite3DbMallocZero(sqlite3 *db, int n){
|
| ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
|
| ** that all prior mallocs (ex: "a") worked too.
|
| */
|
| -void *sqlite3DbMallocRaw(sqlite3 *db, int n){
|
| +void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
|
| void *p;
|
| assert( db==0 || sqlite3_mutex_held(db->mutex) );
|
| assert( db==0 || db->pnBytesFreed==0 );
|
| @@ -641,8 +674,8 @@ void *sqlite3DbMallocRaw(sqlite3 *db, int n){
|
| if( !p && db ){
|
| db->mallocFailed = 1;
|
| }
|
| - sqlite3MemdebugSetType(p, MEMTYPE_DB |
|
| - ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
|
| + sqlite3MemdebugSetType(p,
|
| + (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
|
| return p;
|
| }
|
|
|
| @@ -650,7 +683,7 @@ void *sqlite3DbMallocRaw(sqlite3 *db, int n){
|
| ** Resize the block of memory pointed to by p to n bytes. If the
|
| ** resize fails, set the mallocFailed flag in the connection object.
|
| */
|
| -void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
|
| +void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
|
| void *pNew = 0;
|
| assert( db!=0 );
|
| assert( sqlite3_mutex_held(db->mutex) );
|
| @@ -668,15 +701,14 @@ void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
|
| sqlite3DbFree(db, p);
|
| }
|
| }else{
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
|
| - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
|
| + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
| sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
| - pNew = sqlite3_realloc(p, n);
|
| + pNew = sqlite3_realloc64(p, n);
|
| if( !pNew ){
|
| - sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
|
| db->mallocFailed = 1;
|
| }
|
| - sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
|
| + sqlite3MemdebugSetType(pNew,
|
| (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
|
| }
|
| }
|
| @@ -687,7 +719,7 @@ void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
|
| ** Attempt to reallocate p. If the reallocation fails, then free p
|
| ** and set the mallocFailed flag in the database connection.
|
| */
|
| -void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
|
| +void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
|
| void *pNew;
|
| pNew = sqlite3DbRealloc(db, p, n);
|
| if( !pNew ){
|
| @@ -717,7 +749,7 @@ char *sqlite3DbStrDup(sqlite3 *db, const char *z){
|
| }
|
| return zNew;
|
| }
|
| -char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
|
| +char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
|
| char *zNew;
|
| if( z==0 ){
|
| return 0;
|
| @@ -725,7 +757,7 @@ char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
|
| assert( (n&0x7fffffff)==n );
|
| zNew = sqlite3DbMallocRaw(db, n+1);
|
| if( zNew ){
|
| - memcpy(zNew, z, n);
|
| + memcpy(zNew, z, (size_t)n);
|
| zNew[n] = 0;
|
| }
|
| return zNew;
|
| @@ -747,6 +779,14 @@ void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
|
| *pz = z;
|
| }
|
|
|
| +/*
|
| +** Take actions at the end of an API call to indicate an OOM error
|
| +*/
|
| +static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
|
| + db->mallocFailed = 0;
|
| + sqlite3Error(db, SQLITE_NOMEM);
|
| + return SQLITE_NOMEM;
|
| +}
|
|
|
| /*
|
| ** This function must be called before exiting any API function (i.e.
|
| @@ -767,10 +807,9 @@ int sqlite3ApiExit(sqlite3* db, int rc){
|
| ** is unsafe, as is the call to sqlite3Error().
|
| */
|
| assert( !db || sqlite3_mutex_held(db->mutex) );
|
| - if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
|
| - sqlite3Error(db, SQLITE_NOMEM, 0);
|
| - db->mallocFailed = 0;
|
| - rc = SQLITE_NOMEM;
|
| + if( db==0 ) return rc & 0xff;
|
| + if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
|
| + return apiOomError(db);
|
| }
|
| - return rc & (db ? db->errMask : 0xff);
|
| + return rc & db->errMask;
|
| }
|
|
|