/ Check-in [7e7b2406]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge recent trunk enhancements, and in particular the snapshot interface.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 7e7b24064510001ca4a0c8295fa7d0138bde8dae
User & Date: drh 2015-12-11 12:53:26
Context
2016-01-01
13:31
Merge all recent trunk enhancements. check-in: 5b700f31 user: drh tags: apple-osx
2015-12-11
12:53
Merge recent trunk enhancements, and in particular the snapshot interface. check-in: 7e7b2406 user: drh tags: apple-osx
12:44
Add the experimental snapshot interface. Because it is experimental, it is subject to change or removal at a later date. check-in: 9b124a5a user: drh tags: trunk
2015-12-02
20:22
Merge all recent trunk enhancements, especially the unix VFS changes. check-in: e1fb33c7 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

  1164   1164                     $(TOP)/test/tt3_checkpoint.c \
  1165   1165                     $(TOP)/test/tt3_index.c      \
  1166   1166                     $(TOP)/test/tt3_vacuum.c      \
  1167   1167                     $(TOP)/test/tt3_stress.c      \
  1168   1168                     $(TOP)/test/tt3_lookaside1.c
  1169   1169   
  1170   1170   threadtest3$(TEXE): sqlite3.lo $(THREADTEST3_SRC)
  1171         -	$(LTLINK) $(TOP)/test/threadtest3.c sqlite3.lo -o $@ $(TLIBS)
         1171  +	$(LTLINK) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.lo -o $@ $(TLIBS)
  1172   1172   
  1173   1173   threadtest: threadtest3$(TEXE)
  1174   1174   	./threadtest3$(TEXE)
  1175   1175   
  1176   1176   releasetest:	
  1177   1177   	$(TCLSH_CMD) $(TOP)/test/releasetest.tcl
  1178   1178   

Changes to main.mk.

   778    778   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
   779    779                     $(TOP)/test/tt3_checkpoint.c \
   780    780                     $(TOP)/test/tt3_index.c      \
   781    781                     $(TOP)/test/tt3_vacuum.c      \
   782    782                     $(TOP)/test/tt3_stress.c      \
   783    783                     $(TOP)/test/tt3_lookaside1.c
   784    784   
   785         -threadtest3$(EXE): sqlite3.o $(THREADTEST3_SRC)
   786         -	$(TCCX) $(TOP)/test/threadtest3.c sqlite3.o -o $@ $(THREADLIB)
          785  +threadtest3$(EXE): sqlite3.o $(THREADTEST3_SRC) $(TOP)/src/test_multiplex.c
          786  +	$(TCCX) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.o -o $@ $(THREADLIB)
   787    787   
   788    788   threadtest: threadtest3$(EXE)
   789    789   	./threadtest3$(EXE)
   790    790   
   791    791   TEST_EXTENSION = $(SHPREFIX)testloadext.$(SO)
   792    792   $(TEST_EXTENSION): $(TOP)/src/test_loadext.c
   793    793   	$(MKSHLIB) $(TOP)/src/test_loadext.c -o $(TEST_EXTENSION)

Changes to src/btree.c.

  7544   7544         ** was either part of sibling page iOld (possibly an overflow cell), 
  7545   7545         ** or else the divider cell to the left of sibling page iOld. So,
  7546   7546         ** if sibling page iOld had the same page number as pNew, and if
  7547   7547         ** pCell really was a part of sibling page iOld (not a divider or
  7548   7548         ** overflow cell), we can skip updating the pointer map entries.  */
  7549   7549         if( iOld>=nNew
  7550   7550          || pNew->pgno!=aPgno[iOld]
  7551         -       || pCell<aOld
  7552         -       || pCell>=&aOld[usableSize]
         7551  +       || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
  7553   7552         ){
  7554   7553           if( !leafCorrection ){
  7555   7554             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
  7556   7555           }
  7557   7556           if( cachedCellSize(&b,i)>pNew->minLocal ){
  7558   7557             ptrmapPutOvflPtr(pNew, pCell, &rc);
  7559   7558           }

Changes to src/ctime.c.

    58     58     "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
    59     59   #endif
    60     60   #if SQLITE_DISABLE_DIRSYNC
    61     61     "DISABLE_DIRSYNC",
    62     62   #endif
    63     63   #if SQLITE_DISABLE_LFS
    64     64     "DISABLE_LFS",
           65  +#endif
           66  +#if SQLITE_ENABLE_8_3_NAMES
           67  +  "ENABLE_8_3_NAMES",
    65     68   #endif
    66     69   #if SQLITE_ENABLE_API_ARMOR
    67     70     "ENABLE_API_ARMOR",
    68     71   #endif
    69     72   #if SQLITE_ENABLE_ATOMIC_WRITE
    70     73     "ENABLE_ATOMIC_WRITE",
    71     74   #endif

Changes to src/expr.c.

   849    849   ** to store the copy of expression p, the copies of p->u.zToken
   850    850   ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   851    851   ** if any. Before returning, *pzBuffer is set to the first byte past the
   852    852   ** portion of the buffer copied into by this function.
   853    853   */
   854    854   static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   855    855     Expr *pNew = 0;                      /* Value to return */
          856  +  assert( flags==0 || flags==EXPRDUP_REDUCE );
   856    857     if( p ){
   857    858       const int isReduced = (flags&EXPRDUP_REDUCE);
   858    859       u8 *zAlloc;
   859    860       u32 staticFlag = 0;
   860    861   
   861    862       assert( pzBuffer==0 || isReduced );
   862    863   
................................................................................
   885    886         }
   886    887         if( isReduced ){
   887    888           assert( ExprHasProperty(p, EP_Reduced)==0 );
   888    889           memcpy(zAlloc, p, nNewSize);
   889    890         }else{
   890    891           int nSize = exprStructSize(p);
   891    892           memcpy(zAlloc, p, nSize);
   892         -        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
          893  +        if( nSize<EXPR_FULLSIZE ){ 
          894  +          memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
          895  +        }
   893    896         }
   894    897   
   895    898         /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
   896    899         pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
   897    900         pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
   898    901         pNew->flags |= staticFlag;
   899    902   
................................................................................
   975    978   **
   976    979   ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
   977    980   ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
   978    981   ** truncated version of the usual Expr structure that will be stored as
   979    982   ** part of the in-memory representation of the database schema.
   980    983   */
   981    984   Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
          985  +  assert( flags==0 || flags==EXPRDUP_REDUCE );
   982    986     return exprDup(db, p, flags, 0);
   983    987   }
   984    988   ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
   985    989     ExprList *pNew;
   986    990     struct ExprList_item *pItem, *pOldItem;
   987    991     int i;
   988    992     if( p==0 ) return 0;

Changes to src/loadext.c.

   406    406     /* Version 3.8.11 and later */
   407    407     (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
   408    408     sqlite3_value_free,
   409    409     sqlite3_result_zeroblob64,
   410    410     sqlite3_bind_zeroblob64,
   411    411     /* Version 3.9.0 and later */
   412    412     sqlite3_value_subtype,
   413         -  sqlite3_result_subtype
          413  +  sqlite3_result_subtype,
          414  +  /* Version 3.10.0 and later */
          415  +  sqlite3_status64,
          416  +  sqlite3_strlike,
          417  +  sqlite3_db_cacheflush
   414    418   };
   415    419   
   416    420   /*
   417    421   ** Attempt to load an SQLite extension library contained in the file
   418    422   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   419    423   ** default entry point name (sqlite3_extension_init) is used.  Use
   420    424   ** of the default name is recommended.

Changes to src/main.c.

   444    444         ** allocation (sz), and the maximum number of scratch allocations (N). */
   445    445         sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
   446    446         sqlite3GlobalConfig.szScratch = va_arg(ap, int);
   447    447         sqlite3GlobalConfig.nScratch = va_arg(ap, int);
   448    448         break;
   449    449       }
   450    450       case SQLITE_CONFIG_PAGECACHE: {
   451         -      /* EVIDENCE-OF: R-31408-40510 There are three arguments to
   452         -      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
   453         -      ** of each page buffer (sz), and the number of pages (N). */
          451  +      /* EVIDENCE-OF: R-18761-36601 There are three arguments to
          452  +      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
          453  +      ** the size of each page cache line (sz), and the number of cache lines
          454  +      ** (N). */
   454    455         sqlite3GlobalConfig.pPage = va_arg(ap, void*);
   455    456         sqlite3GlobalConfig.szPage = va_arg(ap, int);
   456    457         sqlite3GlobalConfig.nPage = va_arg(ap, int);
   457    458         break;
   458    459       }
   459    460       case SQLITE_CONFIG_PCACHE_HDRSZ: {
   460    461         /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
................................................................................
  3989   3990       (void)SQLITE_MISUSE_BKPT;
  3990   3991       return -1;
  3991   3992     }
  3992   3993   #endif
  3993   3994     pBt = sqlite3DbNameToBtree(db, zDbName);
  3994   3995     return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
  3995   3996   }
  3996         -
  3997   3997   #if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
  3998   3998   
  3999   3999   #include "sqlite3_private.h"
  4000   4000   
  4001   4001   /* 
  4002   4002   ** Testing a file path for sqlite locks held by a process ID. 
  4003   4003   ** Returns SQLITE_LOCKSTATE_ON if locks are present on path
................................................................................
  4016   4016     if( NULL!=db ){ 
  4017   4017       sqlite3_close(db); /* need to close even if open returns an error */
  4018   4018     }
  4019   4019     return SQLITE_LOCKSTATE_ERROR;
  4020   4020   }
  4021   4021   
  4022   4022   #endif /* SQLITE_ENABLE_APPLE_SPI */
         4023  +
         4024  +#ifdef SQLITE_ENABLE_SNAPSHOT
         4025  +/*
         4026  +** Obtain a snapshot handle for the snapshot of database zDb currently 
         4027  +** being read by handle db.
         4028  +*/
         4029  +int sqlite3_snapshot_get(
         4030  +  sqlite3 *db, 
         4031  +  const char *zDb,
         4032  +  sqlite3_snapshot **ppSnapshot
         4033  +){
         4034  +  int rc = SQLITE_ERROR;
         4035  +#ifndef SQLITE_OMIT_WAL
         4036  +  int iDb;
         4037  +
         4038  +#ifdef SQLITE_ENABLE_API_ARMOR
         4039  +  if( !sqlite3SafetyCheckOk(db) ){
         4040  +    return SQLITE_MISUSE_BKPT;
         4041  +  }
         4042  +#endif
         4043  +  sqlite3_mutex_enter(db->mutex);
         4044  +
         4045  +  iDb = sqlite3FindDbName(db, zDb);
         4046  +  if( iDb==0 || iDb>1 ){
         4047  +    Btree *pBt = db->aDb[iDb].pBt;
         4048  +    if( 0==sqlite3BtreeIsInTrans(pBt) ){
         4049  +      rc = sqlite3BtreeBeginTrans(pBt, 0);
         4050  +      if( rc==SQLITE_OK ){
         4051  +        rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
         4052  +      }
         4053  +    }
         4054  +  }
         4055  +
         4056  +  sqlite3_mutex_leave(db->mutex);
         4057  +#endif   /* SQLITE_OMIT_WAL */
         4058  +  return rc;
         4059  +}
         4060  +
         4061  +/*
         4062  +** Open a read-transaction on the snapshot idendified by pSnapshot.
         4063  +*/
         4064  +int sqlite3_snapshot_open(
         4065  +  sqlite3 *db, 
         4066  +  const char *zDb, 
         4067  +  sqlite3_snapshot *pSnapshot
         4068  +){
         4069  +  int rc = SQLITE_ERROR;
         4070  +#ifndef SQLITE_OMIT_WAL
         4071  +
         4072  +#ifdef SQLITE_ENABLE_API_ARMOR
         4073  +  if( !sqlite3SafetyCheckOk(db) ){
         4074  +    return SQLITE_MISUSE_BKPT;
         4075  +  }
         4076  +#endif
         4077  +  sqlite3_mutex_enter(db->mutex);
         4078  +  if( db->autoCommit==0 ){
         4079  +    int iDb;
         4080  +    iDb = sqlite3FindDbName(db, zDb);
         4081  +    if( iDb==0 || iDb>1 ){
         4082  +      Btree *pBt = db->aDb[iDb].pBt;
         4083  +      if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
         4084  +        rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot);
         4085  +        if( rc==SQLITE_OK ){
         4086  +          rc = sqlite3BtreeBeginTrans(pBt, 0);
         4087  +          sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0);
         4088  +        }
         4089  +      }
         4090  +    }
         4091  +  }
         4092  +
         4093  +  sqlite3_mutex_leave(db->mutex);
         4094  +#endif   /* SQLITE_OMIT_WAL */
         4095  +  return rc;
         4096  +}
         4097  +
         4098  +/*
         4099  +** Free a snapshot handle obtained from sqlite3_snapshot_get().
         4100  +*/
         4101  +void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
         4102  +  sqlite3_free(pSnapshot);
         4103  +}
         4104  +#endif /* SQLITE_ENABLE_SNAPSHOT */

Changes to src/malloc.c.

   354    354       ** is outstanding at one time.  (This is only checked in the
   355    355       ** single-threaded case since checking in the multi-threaded case
   356    356       ** would be much more complicated.) */
   357    357       assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
   358    358       scratchAllocOut--;
   359    359   #endif
   360    360   
   361         -    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
          361  +    if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
   362    362         /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
   363    363         ScratchFreeslot *pSlot;
   364    364         pSlot = (ScratchFreeslot*)p;
   365    365         sqlite3_mutex_enter(mem0.mutex);
   366    366         pSlot->pNext = mem0.pScratchFree;
   367    367         mem0.pScratchFree = pSlot;
   368    368         mem0.nScratchFree++;
................................................................................
   390    390   }
   391    391   
   392    392   /*
   393    393   ** TRUE if p is a lookaside memory allocation from db
   394    394   */
   395    395   #ifndef SQLITE_OMIT_LOOKASIDE
   396    396   static int isLookaside(sqlite3 *db, void *p){
   397         -  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
          397  +  return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
   398    398   }
   399    399   #else
   400    400   #define isLookaside(A,B) 0
   401    401   #endif
   402    402   
   403    403   /*
   404    404   ** Return the size of a memory allocation previously obtained from

Changes to src/os_unix.c.

  3861   3861   */
  3862   3862   static int openDirectory(const char *zFilename, int *pFd){
  3863   3863     int ii;
  3864   3864     int fd = -1;
  3865   3865     char zDirname[MAX_PATHNAME+1];
  3866   3866   
  3867   3867     sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
  3868         -  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
  3869         -  if( ii>1 ){
         3868  +  for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
         3869  +  if( ii>0 ){
  3870   3870       zDirname[ii] = '\0';
  3871         -    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
  3872         -    if( fd>=0 ){
  3873         -      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
  3874         -    }
         3871  +  }else{
         3872  +    if( zDirname[0]!='/' ) zDirname[0] = '.';
         3873  +    zDirname[1] = 0;
         3874  +  }
         3875  +  fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
         3876  +  if( fd>=0 ){
         3877  +    OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
  3875   3878     }
  3876   3879     *pFd = fd;
  3877   3880     if( fd>=0 ) return SQLITE_OK;
  3878   3881     return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
  3879   3882   }
  3880   3883   
  3881   3884   /*
................................................................................
  6695   6698       **   "<path to db>-journalNN"
  6696   6699       **   "<path to db>-walNN"
  6697   6700       **
  6698   6701       ** where NN is a decimal number. The NN naming schemes are 
  6699   6702       ** used by the test_multiplex.c module.
  6700   6703       */
  6701   6704       nDb = sqlite3Strlen30(zPath) - 1; 
  6702         -#ifdef SQLITE_ENABLE_8_3_NAMES
  6703         -    while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
  6704         -    if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
  6705         -#else
  6706   6705       while( zPath[nDb]!='-' ){
         6706  +#ifndef SQLITE_ENABLE_8_3_NAMES
         6707  +      /* In the normal case (8+3 filenames disabled) the journal filename
         6708  +      ** is guaranteed to contain a '-' character. */
  6707   6709         assert( nDb>0 );
  6708         -      assert( zPath[nDb]!='\n' );
         6710  +      assert( sqlite3Isalnum(zPath[nDb]) );
         6711  +#else
         6712  +      /* If 8+3 names are possible, then the journal file might not contain
         6713  +      ** a '-' character.  So check for that case and return early. */
         6714  +      if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
         6715  +#endif
  6709   6716         nDb--;
  6710   6717       }
  6711         -#endif
  6712   6718       memcpy(zDb, zPath, nDb);
  6713   6719       zDb[nDb] = '\0';
  6714   6720   
  6715   6721       if( 0==osStat(zDb, &sStat) ){
  6716   6722         *pMode = sStat.st_mode & 0777;
  6717   6723         *pUid = sStat.st_uid;
  6718   6724         *pGid = sStat.st_gid;

Changes to src/pager.c.

  7343   7343           if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
  7344   7344         }
  7345   7345       }
  7346   7346     }
  7347   7347     return rc;
  7348   7348   }
  7349   7349   
         7350  +#ifdef SQLITE_ENABLE_SNAPSHOT
         7351  +/*
         7352  +** If this is a WAL database, obtain a snapshot handle for the snapshot
         7353  +** currently open. Otherwise, return an error.
         7354  +*/
         7355  +int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
         7356  +  int rc = SQLITE_ERROR;
         7357  +  if( pPager->pWal ){
         7358  +    rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
         7359  +  }
         7360  +  return rc;
         7361  +}
         7362  +
         7363  +/*
         7364  +** If this is a WAL database, store a pointer to pSnapshot. Next time a
         7365  +** read transaction is opened, attempt to read from the snapshot it 
         7366  +** identifies. If this is not a WAL database, return an error.
         7367  +*/
         7368  +int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){
         7369  +  int rc = SQLITE_OK;
         7370  +  if( pPager->pWal ){
         7371  +    sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
         7372  +  }else{
         7373  +    rc = SQLITE_ERROR;
         7374  +  }
         7375  +  return rc;
         7376  +}
         7377  +#endif /* SQLITE_ENABLE_SNAPSHOT */
  7350   7378   #endif /* !SQLITE_OMIT_WAL */
  7351   7379   
  7352   7380   #ifdef SQLITE_ENABLE_ZIPVFS
  7353   7381   /*
  7354   7382   ** A read-lock must be held on the pager when this function is called. If
  7355   7383   ** the pager is in WAL mode and the WAL file currently contains one or more
  7356   7384   ** frames, return the size in bytes of the page images stored within the

Changes to src/pager.h.

   164    164   
   165    165   #ifndef SQLITE_OMIT_WAL
   166    166     int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
   167    167     int sqlite3PagerWalSupported(Pager *pPager);
   168    168     int sqlite3PagerWalCallback(Pager *pPager);
   169    169     int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
   170    170     int sqlite3PagerCloseWal(Pager *pPager);
          171  +# ifdef SQLITE_ENABLE_SNAPSHOT
          172  +  int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
          173  +  int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
          174  +# endif
   171    175   #endif
   172    176   
   173    177   #ifdef SQLITE_ENABLE_ZIPVFS
   174    178     int sqlite3PagerWalFramesize(Pager *pPager);
   175    179   #endif
   176    180   
   177    181   /* Functions used to query pager state and configuration. */

Changes to src/printf.c.

   762    762       return 0;
   763    763     }
   764    764     if( p->mxAlloc==0 ){
   765    765       N = p->nAlloc - p->nChar - 1;
   766    766       setStrAccumError(p, STRACCUM_TOOBIG);
   767    767       return N;
   768    768     }else{
   769         -    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
          769  +    char *zOld = p->bMalloced ? p->zText : 0;
   770    770       i64 szNew = p->nChar;
          771  +    assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
   771    772       szNew += N + 1;
   772    773       if( szNew+p->nChar<=p->mxAlloc ){
   773    774         /* Force exponential buffer size growth as long as it does not overflow,
   774    775         ** to avoid having to call this routine too often */
   775    776         szNew += p->nChar;
   776    777       }
   777    778       if( szNew > p->mxAlloc ){
................................................................................
   784    785       if( p->db ){
   785    786         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   786    787       }else{
   787    788         zNew = sqlite3_realloc64(zOld, p->nAlloc);
   788    789       }
   789    790       if( zNew ){
   790    791         assert( p->zText!=0 || p->nChar==0 );
   791         -      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
          792  +      if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   792    793         p->zText = zNew;
   793    794         p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
          795  +      p->bMalloced = 1;
   794    796       }else{
   795    797         sqlite3StrAccumReset(p);
   796    798         setStrAccumError(p, STRACCUM_NOMEM);
   797    799         return 0;
   798    800       }
   799    801     }
   800    802     return N;
................................................................................
   804    806   ** Append N copies of character c to the given string buffer.
   805    807   */
   806    808   void sqlite3AppendChar(StrAccum *p, int N, char c){
   807    809     testcase( p->nChar + (i64)N > 0x7fffffff );
   808    810     if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
   809    811       return;
   810    812     }
          813  +  assert( (p->zText==p->zBase)==(p->bMalloced==0) );
   811    814     while( (N--)>0 ) p->zText[p->nChar++] = c;
   812    815   }
   813    816   
   814    817   /*
   815    818   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
   816    819   ** So enlarge if first, then do the append.
   817    820   **
................................................................................
   821    824   */
   822    825   static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
   823    826     N = sqlite3StrAccumEnlarge(p, N);
   824    827     if( N>0 ){
   825    828       memcpy(&p->zText[p->nChar], z, N);
   826    829       p->nChar += N;
   827    830     }
          831  +  assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
   828    832   }
   829    833   
   830    834   /*
   831    835   ** Append N bytes of text from z to the StrAccum object.  Increase the
   832    836   ** size of the memory allocation for StrAccum if necessary.
   833    837   */
   834    838   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
................................................................................
   856    860   /*
   857    861   ** Finish off a string by making sure it is zero-terminated.
   858    862   ** Return a pointer to the resulting string.  Return a NULL
   859    863   ** pointer if any kind of error was encountered.
   860    864   */
   861    865   char *sqlite3StrAccumFinish(StrAccum *p){
   862    866     if( p->zText ){
          867  +    assert( (p->zText==p->zBase)==(p->bMalloced==0) );
   863    868       p->zText[p->nChar] = 0;
   864         -    if( p->mxAlloc>0 && p->zText==p->zBase ){
          869  +    if( p->mxAlloc>0 && p->bMalloced==0 ){
   865    870         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   866    871         if( p->zText ){
   867    872           memcpy(p->zText, p->zBase, p->nChar+1);
          873  +        p->bMalloced = 1;
   868    874         }else{
   869    875           setStrAccumError(p, STRACCUM_NOMEM);
   870    876         }
   871    877       }
   872    878     }
   873    879     return p->zText;
   874    880   }
   875    881   
   876    882   /*
   877    883   ** Reset an StrAccum string.  Reclaim all malloced memory.
   878    884   */
   879    885   void sqlite3StrAccumReset(StrAccum *p){
   880         -  if( p->zText!=p->zBase ){
          886  +  assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
          887  +  if( p->bMalloced ){
   881    888       sqlite3DbFree(p->db, p->zText);
          889  +    p->bMalloced = 0;
   882    890     }
   883    891     p->zText = 0;
   884    892   }
   885    893   
   886    894   /*
   887    895   ** Initialize a string accumulator.
   888    896   **
................................................................................
   900    908   void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
   901    909     p->zText = p->zBase = zBase;
   902    910     p->db = db;
   903    911     p->nChar = 0;
   904    912     p->nAlloc = n;
   905    913     p->mxAlloc = mx;
   906    914     p->accError = 0;
          915  +  p->bMalloced = 0;
   907    916   }
   908    917   
   909    918   /*
   910    919   ** Print into memory obtained from sqliteMalloc().  Use the internal
   911    920   ** %-conversion extensions.
   912    921   */
   913    922   char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){

Changes to src/resolve.c.

   324    324                 iCol = -1;
   325    325               }
   326    326               break;
   327    327             }
   328    328           }
   329    329           if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
   330    330             /* IMP: R-51414-32910 */
   331         -          /* IMP: R-44911-55124 */
   332    331             iCol = -1;
   333    332           }
   334    333           if( iCol<pTab->nCol ){
   335    334             cnt++;
   336    335             if( iCol<0 ){
   337    336               pExpr->affinity = SQLITE_AFF_INTEGER;
   338    337             }else if( pExpr->iTable==0 ){
................................................................................
   359    358        && cntTab==1
   360    359        && pMatch
   361    360        && (pNC->ncFlags & NC_IdxExpr)==0
   362    361        && sqlite3IsRowid(zCol)
   363    362        && VisibleRowid(pMatch->pTab)
   364    363       ){
   365    364         cnt = 1;
   366         -      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
          365  +      pExpr->iColumn = -1;
   367    366         pExpr->affinity = SQLITE_AFF_INTEGER;
   368    367       }
   369    368   
   370    369       /*
   371    370       ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
   372    371       ** might refer to an result-set alias.  This happens, for example, when
   373    372       ** we are resolving names in the WHERE clause of the following command:

Changes to src/select.c.

  1525   1525   #ifndef SQLITE_OMIT_EXPLAIN
  1526   1526     /* If this is an EXPLAIN, skip this step */
  1527   1527     if( pParse->explain ){
  1528   1528       return;
  1529   1529     }
  1530   1530   #endif
  1531   1531   
         1532  +  if( pParse->colNamesSet || db->mallocFailed ) return;
         1533  +  assert( v!=0 );
  1532   1534     assert( pTabList!=0 );
  1533         -  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
  1534   1535     pParse->colNamesSet = 1;
  1535   1536     fullNames = (db->flags & SQLITE_FullColNames)!=0;
  1536   1537     shortNames = (db->flags & SQLITE_ShortColNames)!=0;
  1537   1538     sqlite3VdbeSetNumCols(v, pEList->nExpr);
  1538   1539     for(i=0; i<pEList->nExpr; i++){
  1539   1540       Expr *p;
  1540   1541       p = pEList->a[i].pExpr;

Changes to src/shell.c.

   521    521   typedef struct ShellState ShellState;
   522    522   struct ShellState {
   523    523     sqlite3 *db;           /* The database */
   524    524     int echoOn;            /* True to echo input commands */
   525    525     int autoEQP;           /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
   526    526     int statsOn;           /* True to display memory stats before each finalize */
   527    527     int scanstatsOn;       /* True to display scan stats before each finalize */
          528  +  int countChanges;      /* True to display change counts */
   528    529     int backslashOn;       /* Resolve C-style \x escapes in SQL input text */
   529    530     int outCount;          /* Revert to stdout when reaching zero */
   530    531     int cnt;               /* Number of records displayed so far */
   531    532     FILE *out;             /* Write results here */
   532    533     FILE *traceOut;        /* Output for sqlite3_trace() */
   533    534     int nErr;              /* Number of errors seen */
   534    535     int mode;              /* An output mode setting */
................................................................................
  1781   1782   /*
  1782   1783   ** Text of a help message
  1783   1784   */
  1784   1785   static char zHelp[] =
  1785   1786     ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
  1786   1787     ".bail on|off           Stop after hitting an error.  Default OFF\n"
  1787   1788     ".binary on|off         Turn binary output on or off.  Default OFF\n"
         1789  +  ".changes on|off        Show number of rows changed by SQL\n"
  1788   1790     ".clone NEWDB           Clone data into NEWDB from the existing database\n"
  1789   1791     ".databases             List names and files of attached databases\n"
  1790   1792     ".dbinfo ?DB?           Show status information about the database\n"
  1791   1793     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  1792   1794     "                         If TABLE specified, only dump tables matching\n"
  1793   1795     "                         LIKE pattern TABLE.\n"
  1794   1796     ".echo on|off           Turn command echo on or off\n"
................................................................................
  2752   2754   
  2753   2755     /* The undocumented ".breakpoint" command causes a call to the no-op
  2754   2756     ** routine named test_breakpoint().
  2755   2757     */
  2756   2758     if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
  2757   2759       test_breakpoint();
  2758   2760     }else
         2761  +
         2762  +  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
         2763  +    if( nArg==2 ){
         2764  +      p->countChanges = booleanValue(azArg[1]);
         2765  +    }else{
         2766  +      fprintf(stderr, "Usage: .changes on|off\n");
         2767  +      rc = 1;
         2768  +    }
         2769  +  }else
  2759   2770   
  2760   2771     if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
  2761   2772       if( nArg==2 ){
  2762   2773         tryToClone(p, azArg[1]);
  2763   2774       }else{
  2764   2775         fprintf(stderr, "Usage: .clone FILENAME\n");
  2765   2776         rc = 1;
................................................................................
  4282   4293             fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
  4283   4294             sqlite3_free(zErrMsg);
  4284   4295             zErrMsg = 0;
  4285   4296           }else{
  4286   4297             fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
  4287   4298           }
  4288   4299           errCnt++;
         4300  +      }else if( p->countChanges ){
         4301  +        fprintf(p->out, "changes: %3d   total_changes: %d\n",
         4302  +                sqlite3_changes(p->db), sqlite3_total_changes(p->db));
  4289   4303         }
  4290   4304         nSql = 0;
  4291   4305         if( p->outCount ){
  4292   4306           output_reset(p);
  4293   4307           p->outCount = 0;
  4294   4308         }
  4295   4309       }else if( nSql && _all_whitespace(zSql) ){

Changes to src/sqlite.h.in.

   884    884   ** is intended for diagnostic use only.
   885    885   **
   886    886   ** <li>[[SQLITE_FCNTL_VFS_POINTER]]
   887    887   ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
   888    888   ** [VFSes] currently in use.  ^(The argument X in
   889    889   ** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
   890    890   ** of type "[sqlite3_vfs] **".  This opcodes will set *X
   891         -** to a pointer to the top-level VFS.^)
          891  +** to a pointer to the top-level VFS.)^
   892    892   ** ^When there are multiple VFS shims in the stack, this opcode finds the
   893    893   ** upper-most shim only.
   894    894   **
   895    895   ** <li>[[SQLITE_FCNTL_PRAGMA]]
   896    896   ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
   897    897   ** file control is sent to the open [sqlite3_file] object corresponding
   898    898   ** to the database file to which the pragma statement refers. ^The argument
................................................................................
  4402   4402   ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
  4403   4403   ** memory allocation fails.
  4404   4404   **
  4405   4405   ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
  4406   4406   ** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
  4407   4407   ** then sqlite3_value_free(V) is a harmless no-op.
  4408   4408   */
  4409         -SQLITE_EXPERIMENTAL sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
  4410         -SQLITE_EXPERIMENTAL void sqlite3_value_free(sqlite3_value*);
         4409  +sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
         4410  +void sqlite3_value_free(sqlite3_value*);
  4411   4411   
  4412   4412   /*
  4413   4413   ** CAPI3REF: Obtain Aggregate Function Context
  4414   4414   ** METHOD: sqlite3_context
  4415   4415   **
  4416   4416   ** Implementations of aggregate SQL functions use this
  4417   4417   ** routine to allocate memory for storing their state.
................................................................................
  7848   7848   ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
  7849   7849   */
  7850   7850   void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
  7851   7851   
  7852   7852   /*
  7853   7853   ** CAPI3REF: Flush caches to disk mid-transaction
  7854   7854   **
  7855         -** If a write-transaction is open when this function is called, any dirty
         7855  +** ^If a write-transaction is open on [database connection] D when the
         7856  +** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
  7856   7857   ** pages in the pager-cache that are not currently in use are written out 
  7857   7858   ** to disk. A dirty page may be in use if a database cursor created by an
  7858   7859   ** active SQL statement is reading from it, or if it is page 1 of a database
  7859         -** file (page 1 is always "in use"). Dirty pages are flushed for all
  7860         -** databases - "main", "temp" and any attached databases.
         7860  +** file (page 1 is always "in use").  ^The [sqlite3_db_cacheflush(D)]
         7861  +** interface flushes caches for all schemas - "main", "temp", and
         7862  +** any [attached] databases.
  7861   7863   **
  7862         -** If this function needs to obtain extra database locks before dirty pages 
  7863         -** can be flushed to disk, it does so. If said locks cannot be obtained 
         7864  +** ^If this function needs to obtain extra database locks before dirty pages 
         7865  +** can be flushed to disk, it does so. ^If those locks cannot be obtained 
  7864   7866   ** immediately and there is a busy-handler callback configured, it is invoked
  7865         -** in the usual manner. If the required lock still cannot be obtained, then
         7867  +** in the usual manner. ^If the required lock still cannot be obtained, then
  7866   7868   ** the database is skipped and an attempt made to flush any dirty pages
  7867         -** belonging to the next (if any) database. If any databases are skipped
         7869  +** belonging to the next (if any) database. ^If any databases are skipped
  7868   7870   ** because locks cannot be obtained, but no other error occurs, this
  7869   7871   ** function returns SQLITE_BUSY.
  7870   7872   **
  7871         -** If any other error occurs while flushing dirty pages to disk (for
         7873  +** ^If any other error occurs while flushing dirty pages to disk (for
  7872   7874   ** example an IO error or out-of-memory condition), then processing is
  7873         -** abandoned and an SQLite error code returned to the caller immediately.
         7875  +** abandoned and an SQLite [error code] is returned to the caller immediately.
  7874   7876   **
  7875         -** Otherwise, if no error occurs, SQLITE_OK is returned.
         7877  +** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
  7876   7878   **
  7877         -** This function does not set the database handle error code or message
  7878         -** returned by the sqlite3_errcode() and sqlite3_errmsg() functions.
         7879  +** ^This function does not set the database handle error code or message
         7880  +** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
  7879   7881   */
  7880   7882   int sqlite3_db_cacheflush(sqlite3*);
  7881   7883   
         7884  +/*
         7885  +** CAPI3REF: Database Snapshot
         7886  +** KEYWORDS: {snapshot}
         7887  +** EXPERIMENTAL
         7888  +**
         7889  +** An instance of the snapshot object records the state of a [WAL mode]
         7890  +** database for some specific point in history.
         7891  +**
         7892  +** In [WAL mode], multiple [database connections] that are open on the
         7893  +** same database file can each be reading a different historical version
         7894  +** of the database file.  When a [database connection] begins a read
         7895  +** transaction, that connection sees an unchanging copy of the database
         7896  +** as it existed for the point in time when the transaction first started.
         7897  +** Subsequent changes to the database from other connections are not seen
         7898  +** by the reader until a new read transaction is started.
         7899  +**
         7900  +** The sqlite3_snapshot object records state information about an historical
         7901  +** version of the database file so that it is possible to later open a new read
         7902  +** transaction that sees that historical version of the database rather than
         7903  +** the most recent version.
         7904  +**
         7905  +** The constructor for this object is [sqlite3_snapshot_get()].  The
         7906  +** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer
         7907  +** to an historical snapshot (if possible).  The destructor for 
         7908  +** sqlite3_snapshot objects is [sqlite3_snapshot_free()].
         7909  +*/
         7910  +typedef struct sqlite3_snapshot sqlite3_snapshot;
         7911  +
         7912  +/*
         7913  +** CAPI3REF: Record A Database Snapshot
         7914  +** EXPERIMENTAL
         7915  +**
         7916  +** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
         7917  +** new [sqlite3_snapshot] object that records the current state of
         7918  +** schema S in database connection D.  ^On success, the
         7919  +** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
         7920  +** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
         7921  +** ^If schema S of [database connection] D is not a [WAL mode] database
         7922  +** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)]
         7923  +** leaves the *P value unchanged and returns an appropriate [error code].
         7924  +**
         7925  +** The [sqlite3_snapshot] object returned from a successful call to
         7926  +** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
         7927  +** to avoid a memory leak.
         7928  +**
         7929  +** The [sqlite3_snapshot_get()] interface is only available when the
         7930  +** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
         7931  +*/
         7932  +SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
         7933  +  sqlite3 *db,
         7934  +  const char *zSchema,
         7935  +  sqlite3_snapshot **ppSnapshot
         7936  +);
         7937  +
         7938  +/*
         7939  +** CAPI3REF: Start a read transaction on an historical snapshot
         7940  +** EXPERIMENTAL
         7941  +**
         7942  +** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the
         7943  +** read transaction that is currently open on schema S of
         7944  +** [database connection] D so that it refers to historical [snapshot] P.
         7945  +** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success
         7946  +** or an appropriate [error code] if it fails.
         7947  +**
         7948  +** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
         7949  +** the first operation, apart from other sqlite3_snapshot_open() calls,
         7950  +** following the [BEGIN] that starts a new read transaction.
         7951  +** ^A [snapshot] will fail to open if it has been overwritten by a 
         7952  +** [checkpoint].  
         7953  +**
         7954  +** The [sqlite3_snapshot_open()] interface is only available when the
         7955  +** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
         7956  +*/
         7957  +SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
         7958  +  sqlite3 *db,
         7959  +  const char *zSchema,
         7960  +  sqlite3_snapshot *pSnapshot
         7961  +);
         7962  +
         7963  +/*
         7964  +** CAPI3REF: Destroy a snapshot
         7965  +** EXPERIMENTAL
         7966  +**
         7967  +** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
         7968  +** The application must eventually free every [sqlite3_snapshot] object
         7969  +** using this routine to avoid a memory leak.
         7970  +**
         7971  +** The [sqlite3_snapshot_free()] interface is only available when the
         7972  +** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
         7973  +*/
         7974  +SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
         7975  +
  7882   7976   /*
  7883   7977   ** Undo the hack that converts floating point types to integer for
  7884   7978   ** builds on processors without floating point support.
  7885   7979   */
  7886   7980   #ifdef SQLITE_OMIT_FLOATING_POINT
  7887   7981   # undef double
  7888   7982   #endif
  7889   7983   
  7890   7984   #ifdef __cplusplus
  7891   7985   }  /* End of the 'extern "C"' block */
  7892   7986   #endif
  7893   7987   #endif /* _SQLITE3_H_ */

Changes to src/sqlite3ext.h.

   272    272     void (*value_free)(sqlite3_value*);
   273    273     int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
   274    274     int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
   275    275     /* Version 3.9.0 and later */
   276    276     unsigned int (*value_subtype)(sqlite3_value*);
   277    277     void (*result_subtype)(sqlite3_context*,unsigned int);
   278    278     /* Version 3.10.0 and later */
          279  +  int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
   279    280     int (*strlike)(const char*,const char*,unsigned int);
          281  +  int (*db_cacheflush)(sqlite3*);
   280    282   };
   281    283   
   282    284   /*
   283    285   ** The following macros redefine the API routines so that they are
   284    286   ** redirected through the global sqlite3_api structure.
   285    287   **
   286    288   ** This header file is also used by the loadext.c source file
................................................................................
   513    515   #define sqlite3_value_free             sqlite3_api->value_free
   514    516   #define sqlite3_result_zeroblob64      sqlite3_api->result_zeroblob64
   515    517   #define sqlite3_bind_zeroblob64        sqlite3_api->bind_zeroblob64
   516    518   /* Version 3.9.0 and later */
   517    519   #define sqlite3_value_subtype          sqlite3_api->value_subtype
   518    520   #define sqlite3_result_subtype         sqlite3_api->result_subtype
   519    521   /* Version 3.10.0 and later */
          522  +#define sqlite3_status64               sqlite3_api->status64
   520    523   #define sqlite3_strlike                sqlite3_api->strlike
          524  +#define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
   521    525   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   522    526   
   523    527   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   524    528     /* This case when the file really is being compiled as a loadable 
   525    529     ** extension */
   526    530   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   527    531   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

   169    169   # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   170    170   # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   171    171   #else                          /* Generates a warning - but it always works */
   172    172   # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   173    173   # define SQLITE_PTR_TO_INT(X)  ((int)(X))
   174    174   #endif
   175    175   
          176  +/*
          177  +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
          178  +** something between S (inclusive) and E (exclusive).
          179  +**
          180  +** In other words, S is a buffer and E is a pointer to the first byte after
          181  +** the end of buffer S.  This macro returns true if P points to something
          182  +** contained within the buffer S.
          183  +*/
          184  +#if defined(HAVE_STDINT_H)
          185  +# define SQLITE_WITHIN(P,S,E) \
          186  +    ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E))
          187  +#else
          188  +# define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E))
          189  +#endif
          190  +
   176    191   /*
   177    192   ** A macro to hint to the compiler that a function should not be
   178    193   ** inlined.
   179    194   */
   180    195   #if defined(__GNUC__)
   181    196   #  define SQLITE_NOINLINE  __attribute__((noinline))
   182    197   #elif defined(_MSC_VER) && _MSC_VER>=1310
................................................................................
  2958   2973     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
  2959   2974     char *zBase;         /* A base allocation.  Not from malloc. */
  2960   2975     char *zText;         /* The string collected so far */
  2961   2976     int  nChar;          /* Length of the string so far */
  2962   2977     int  nAlloc;         /* Amount of space allocated in zText */
  2963   2978     int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
  2964   2979     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
         2980  +  u8   bMalloced;      /* zText points to allocated space */
  2965   2981   };
  2966   2982   #define STRACCUM_NOMEM   1
  2967   2983   #define STRACCUM_TOOBIG  2
  2968   2984   
  2969   2985   /*
  2970   2986   ** A pointer to this structure is used to communicate information
  2971   2987   ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.

Changes to src/test1.c.

  2265   2265       return TCL_ERROR;
  2266   2266     }
  2267   2267     pVfs->xCurrentTimeInt64(pVfs, &t);
  2268   2268     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
  2269   2269     return TCL_OK;
  2270   2270   }
  2271   2271   
         2272  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2273  +/*
         2274  +** Usage: sqlite3_snapshot_get DB DBNAME
         2275  +*/
         2276  +static int test_snapshot_get(
         2277  +  void * clientData,
         2278  +  Tcl_Interp *interp,
         2279  +  int objc,
         2280  +  Tcl_Obj *CONST objv[]
         2281  +){
         2282  +  int rc;
         2283  +  sqlite3 *db;
         2284  +  char *zName;
         2285  +  sqlite3_snapshot *pSnapshot = 0;
         2286  +
         2287  +  if( objc!=3 ){
         2288  +    Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
         2289  +    return TCL_ERROR;
         2290  +  }
         2291  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         2292  +  zName = Tcl_GetString(objv[2]);
         2293  +
         2294  +  rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
         2295  +  if( rc!=SQLITE_OK ){
         2296  +    Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
         2297  +    return TCL_ERROR;
         2298  +  }else{
         2299  +    char zBuf[100];
         2300  +    if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
         2301  +    Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
         2302  +  }
         2303  +  return TCL_OK;
         2304  +}
         2305  +#endif /* SQLITE_ENABLE_SNAPSHOT */
         2306  +
         2307  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2308  +/*
         2309  +** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
         2310  +*/
         2311  +static int test_snapshot_open(
         2312  +  void * clientData,
         2313  +  Tcl_Interp *interp,
         2314  +  int objc,
         2315  +  Tcl_Obj *CONST objv[]
         2316  +){
         2317  +  int rc;
         2318  +  sqlite3 *db;
         2319  +  char *zName;
         2320  +  sqlite3_snapshot *pSnapshot;
         2321  +
         2322  +  if( objc!=4 ){
         2323  +    Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
         2324  +    return TCL_ERROR;
         2325  +  }
         2326  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         2327  +  zName = Tcl_GetString(objv[2]);
         2328  +  pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
         2329  +
         2330  +  rc = sqlite3_snapshot_open(db, zName, pSnapshot);
         2331  +  if( rc!=SQLITE_OK ){
         2332  +    Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
         2333  +    return TCL_ERROR;
         2334  +  }
         2335  +  return TCL_OK;
         2336  +}
         2337  +#endif /* SQLITE_ENABLE_SNAPSHOT */
         2338  +
         2339  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2340  +/*
         2341  +** Usage: sqlite3_snapshot_free SNAPSHOT
         2342  +*/
         2343  +static int test_snapshot_free(
         2344  +  void * clientData,
         2345  +  Tcl_Interp *interp,
         2346  +  int objc,
         2347  +  Tcl_Obj *CONST objv[]
         2348  +){
         2349  +  sqlite3_snapshot *pSnapshot;
         2350  +  if( objc!=2 ){
         2351  +    Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
         2352  +    return TCL_ERROR;
         2353  +  }
         2354  +  pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
         2355  +  sqlite3_snapshot_free(pSnapshot);
         2356  +  return TCL_OK;
         2357  +}
         2358  +#endif /* SQLITE_ENABLE_SNAPSHOT */
         2359  +
  2272   2360   /*
  2273   2361   ** Usage:  sqlite3_next_stmt  DB  STMT
  2274   2362   **
  2275   2363   ** Return the next statment in sequence after STMT.
  2276   2364   */
  2277   2365   static int test_next_stmt(
  2278   2366     void * clientData,
................................................................................
  6094   6182       Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
  6095   6183       return TCL_ERROR;
  6096   6184     }
  6097   6185     if( logcallback.pObj ){
  6098   6186       Tcl_DecrRefCount(logcallback.pObj);
  6099   6187       logcallback.pObj = 0;
  6100   6188       logcallback.pInterp = 0;
  6101         -    sqlite3_config(SQLITE_CONFIG_LOG, 0, 0);
         6189  +    sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
  6102   6190     }
  6103   6191     if( objc>1 ){
  6104   6192       logcallback.pObj = objv[1];
  6105   6193       Tcl_IncrRefCount(logcallback.pObj);
  6106   6194       logcallback.pInterp = interp;
  6107         -    sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0);
         6195  +    sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
  6108   6196     }
  6109   6197     return TCL_OK;
  6110   6198   }
  6111   6199   
  6112   6200   /*
  6113   6201   **     tcl_objproc COMMANDNAME ARGS...
  6114   6202   **
................................................................................
  7278   7366        { "sqlite3_stmt_scanstatus",       test_stmt_scanstatus,   0 },
  7279   7367        { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset,   0 },
  7280   7368   #endif
  7281   7369   #ifdef SQLITE_ENABLE_SQLLOG
  7282   7370        { "sqlite3_config_sqllog",         test_config_sqllog,   0 },
  7283   7371   #endif
  7284   7372        { "vfs_current_time_int64",           vfsCurrentTimeInt64,   0 },
         7373  +#ifdef SQLITE_ENABLE_SNAPSHOT
         7374  +     { "sqlite3_snapshot_get", test_snapshot_get, 0 },
         7375  +     { "sqlite3_snapshot_open", test_snapshot_open, 0 },
         7376  +     { "sqlite3_snapshot_free", test_snapshot_free, 0 },
         7377  +#endif
  7285   7378     };
  7286   7379     static int bitmask_size = sizeof(Bitmask)*8;
  7287   7380     static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
  7288   7381     int i;
  7289   7382     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  7290   7383     extern int sqlite3_opentemp_count;
  7291   7384     extern int sqlite3_like_count;

Changes to src/test_config.c.

   138    138   #endif
   139    139   
   140    140   #ifdef SQLITE_ENABLE_MEMSYS5
   141    141     Tcl_SetVar2(interp, "sqlite_options", "mem5", "1", TCL_GLOBAL_ONLY);
   142    142   #else
   143    143     Tcl_SetVar2(interp, "sqlite_options", "mem5", "0", TCL_GLOBAL_ONLY);
   144    144   #endif
          145  +
          146  +#ifdef SQLITE_ENABLE_SNAPSHOT
          147  +  Tcl_SetVar2(interp, "sqlite_options", "snapshot", "1", TCL_GLOBAL_ONLY);
          148  +#else
          149  +  Tcl_SetVar2(interp, "sqlite_options", "snapshot", "0", TCL_GLOBAL_ONLY);
          150  +#endif
   145    151   
   146    152   #ifdef SQLITE_MUTEX_OMIT
   147    153     Tcl_SetVar2(interp, "sqlite_options", "mutex", "0", TCL_GLOBAL_ONLY);
   148    154   #else
   149    155     Tcl_SetVar2(interp, "sqlite_options", "mutex", "1", TCL_GLOBAL_ONLY);
   150    156   #endif
   151    157   

Changes to src/test_fs.c.

   198    198   /*
   199    199   ** xOpen implementation.
   200    200   **
   201    201   ** Open a new fsdir cursor.
   202    202   */
   203    203   static int fsdirOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   204    204     FsdirCsr *pCur;
   205         -  pCur = (FsdirCsr*)sqlite3_malloc(sizeof(FsdirCsr));
          205  +  /* Allocate an extra 256 bytes because it is undefined how big dirent.d_name
          206  +  ** is and we need enough space.  Linux provides plenty already, but
          207  +  ** Solaris only provides one byte. */
          208  +  pCur = (FsdirCsr*)sqlite3_malloc(sizeof(FsdirCsr)+256);
   206    209     if( pCur==0 ) return SQLITE_NOMEM;
   207    210     memset(pCur, 0, sizeof(FsdirCsr));
   208    211     *ppCursor = &pCur->base;
   209    212     return SQLITE_OK;
   210    213   }
   211    214   
   212    215   /*

Changes to src/test_malloc.c.

   218    218       ** a zeroed allocator then calling GETMALLOC. */
   219    219       memset(&m2, 0, sizeof(m2));
   220    220       sqlite3_config(SQLITE_CONFIG_MALLOC, &m2);
   221    221       sqlite3_config(SQLITE_CONFIG_GETMALLOC, &m2);
   222    222       assert( memcmp(&m2, &memfault.m, sizeof(m2))==0 );
   223    223   
   224    224       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memfault.m);
   225         -    sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, 0, 0);
          225  +    sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,
          226  +        (void*)0, (void*)0);
   226    227     }
   227    228   
   228    229     if( rc==SQLITE_OK ){
   229    230       memfault.isInstalled = 1;
   230    231     }
   231    232     return rc;
   232    233   }

Changes to src/test_multiplex.c.

   185    185     sqlite3_io_methods sIoMethodsV2;
   186    186   
   187    187     /* True when this shim has been initialized.
   188    188     */
   189    189     int isInitialized;
   190    190   
   191    191     /* For run-time access any of the other global data structures in this
   192         -  ** shim, the following mutex must be held.
   193         -  */
          192  +  ** shim, the following mutex must be held. In practice, all this mutex
          193  +  ** protects is add/remove operations to/from the linked list of group objects
          194  +  ** starting at pGroups below. More specifically, it protects the value of
          195  +  ** pGroups itself, and the pNext/pPrev fields of each multiplexGroup
          196  +  ** structure.  */
   194    197     sqlite3_mutex *pMutex;
   195    198   
   196    199     /* List of multiplexGroup objects.
   197    200     */
   198    201     multiplexGroup *pGroups;
   199    202   } gMultiplex;
   200    203   
................................................................................
   754    757     void *pBuf,
   755    758     int iAmt,
   756    759     sqlite3_int64 iOfst
   757    760   ){
   758    761     multiplexConn *p = (multiplexConn*)pConn;
   759    762     multiplexGroup *pGroup = p->pGroup;
   760    763     int rc = SQLITE_OK;
   761         -  int nMutex = 0;
   762         -  multiplexEnter(); nMutex++;
   763    764     if( !pGroup->bEnabled ){
   764    765       sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
   765         -    multiplexLeave(); nMutex--;
   766    766       if( pSubOpen==0 ){
   767    767         rc = SQLITE_IOERR_READ;
   768    768       }else{
   769    769         rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
   770    770       }
   771    771     }else{
   772    772       while( iAmt > 0 ){
   773    773         int i = (int)(iOfst / pGroup->szChunk);
   774    774         sqlite3_file *pSubOpen;
   775         -      if( nMutex==0 ){ multiplexEnter(); nMutex++; }
   776    775         pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1);
   777         -      multiplexLeave(); nMutex--;
   778    776         if( pSubOpen ){
   779    777           int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) - pGroup->szChunk;
   780    778           if( extra<0 ) extra = 0;
   781    779           iAmt -= extra;
   782    780           rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt,
   783    781                                          iOfst % pGroup->szChunk);
   784    782           if( rc!=SQLITE_OK ) break;
................................................................................
   787    785           iAmt = extra;
   788    786         }else{
   789    787           rc = SQLITE_IOERR_READ;
   790    788           break;
   791    789         }
   792    790       }
   793    791     }
   794         -  assert( nMutex==0 || nMutex==1 );
   795         -  if( nMutex ) multiplexLeave();
          792  +
   796    793     return rc;
   797    794   }
   798    795   
   799    796   /* Pass xWrite requests thru to the original VFS after
   800    797   ** determining the correct chunk to operate on.
   801    798   ** Break up writes across chunk boundaries.
   802    799   */
................................................................................
   805    802     const void *pBuf,
   806    803     int iAmt,
   807    804     sqlite3_int64 iOfst
   808    805   ){
   809    806     multiplexConn *p = (multiplexConn*)pConn;
   810    807     multiplexGroup *pGroup = p->pGroup;
   811    808     int rc = SQLITE_OK;
   812         -  multiplexEnter();
   813    809     if( !pGroup->bEnabled ){
   814    810       sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
   815    811       if( pSubOpen==0 ){
   816    812         rc = SQLITE_IOERR_WRITE;
   817    813       }else{
   818    814         rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
   819    815       }
................................................................................
   830    826                                           iOfst % pGroup->szChunk);
   831    827           pBuf = (char *)pBuf + iAmt;
   832    828           iOfst += iAmt;
   833    829           iAmt = extra;
   834    830         }
   835    831       }
   836    832     }
   837         -  multiplexLeave();
   838    833     return rc;
   839    834   }
   840    835   
   841    836   /* Pass xTruncate requests thru to the original VFS after
   842    837   ** determining the correct chunk to operate on.  Delete any
   843    838   ** chunks above the truncate mark.
   844    839   */

Changes to src/trigger.c.

   555    555     }
   556    556   #endif
   557    557   
   558    558     /* Generate code to destroy the database record of the trigger.
   559    559     */
   560    560     assert( pTable!=0 );
   561    561     if( (v = sqlite3GetVdbe(pParse))!=0 ){
   562         -    int base;
   563         -    static const int iLn = VDBE_OFFSET_LINENO(2);
   564         -    static const VdbeOpList dropTrigger[] = {
   565         -      { OP_Rewind,     0, ADDR(9),  0},
   566         -      { OP_String8,    0, 1,        0}, /* 1 */
   567         -      { OP_Column,     0, 1,        2},
   568         -      { OP_Ne,         2, ADDR(8),  1},
   569         -      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
   570         -      { OP_Column,     0, 0,        2},
   571         -      { OP_Ne,         2, ADDR(8),  1},
   572         -      { OP_Delete,     0, 0,        0},
   573         -      { OP_Next,       0, ADDR(1),  0}, /* 8 */
   574         -    };
   575         -
   576         -    sqlite3BeginWriteOperation(pParse, 0, iDb);
   577         -    sqlite3OpenMasterTable(pParse, iDb);
   578         -    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
   579         -    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
   580         -    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
          562  +    sqlite3NestedParse(pParse,
          563  +       "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
          564  +       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName
          565  +    );
   581    566       sqlite3ChangeCookie(pParse, iDb);
   582         -    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
   583    567       sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
   584         -    if( pParse->nMem<3 ){
   585         -      pParse->nMem = 3;
   586         -    }
   587    568     }
   588    569   }
   589    570   
   590    571   /*
   591    572   ** Remove a trigger from the hash tables of the sqlite* pointer.
   592    573   */
   593    574   void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){

Changes to src/vdbeaux.c.

   345    345     if( (i & (i-1))==0 ){
   346    346       p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
   347    347                                          (i*2+1)*sizeof(p->aLabel[0]));
   348    348     }
   349    349     if( p->aLabel ){
   350    350       p->aLabel[i] = -1;
   351    351     }
   352         -  return -1-i;
          352  +  return ADDR(i);
   353    353   }
   354    354   
   355    355   /*
   356    356   ** Resolve label "x" to be the address of the next instruction to
   357    357   ** be inserted.  The parameter "x" must have been obtained from
   358    358   ** a prior call to sqlite3VdbeMakeLabel().
   359    359   */
   360    360   void sqlite3VdbeResolveLabel(Vdbe *v, int x){
   361    361     Parse *p = v->pParse;
   362         -  int j = -1-x;
          362  +  int j = ADDR(x);
   363    363     assert( v->magic==VDBE_MAGIC_INIT );
   364    364     assert( j<p->nLabel );
   365    365     assert( j>=0 );
   366    366     if( p->aLabel ){
   367    367       p->aLabel[j] = v->nOp;
   368    368     }
   369    369     p->iFixedOp = v->nOp - 1;
................................................................................
   582    582           pOp->p4type = P4_ADVANCE;
   583    583           break;
   584    584         }
   585    585       }
   586    586   
   587    587       pOp->opflags = sqlite3OpcodeProperty[opcode];
   588    588       if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
   589         -      assert( -1-pOp->p2<pParse->nLabel );
   590         -      pOp->p2 = aLabel[-1-pOp->p2];
          589  +      assert( ADDR(pOp->p2)<pParse->nLabel );
          590  +      pOp->p2 = aLabel[ADDR(pOp->p2)];
   591    591       }
   592    592     }
   593    593     sqlite3DbFree(p->db, pParse->aLabel);
   594    594     pParse->aLabel = 0;
   595    595     pParse->nLabel = 0;
   596    596     *pMaxFuncArgs = nMaxArgs;
   597    597     assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
................................................................................
   640    640     assert( p->magic==VDBE_MAGIC_INIT );
   641    641     if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
   642    642       return 0;
   643    643     }
   644    644     addr = p->nOp;
   645    645     pOut = &p->aOp[addr];
   646    646     for(i=0; i<nOp; i++, aOp++, pOut++){
   647         -    int p2 = aOp->p2;
   648    647       pOut->opcode = aOp->opcode;
   649    648       pOut->p1 = aOp->p1;
   650         -    if( p2<0 ){
   651         -      assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
   652         -      pOut->p2 = addr + ADDR(p2);
   653         -    }else{
   654         -      pOut->p2 = p2;
   655         -    }
          649  +    pOut->p2 = aOp->p2;
          650  +    assert( aOp->p2>=0 );
   656    651       pOut->p3 = aOp->p3;
   657    652       pOut->p4type = P4_NOTUSED;
   658    653       pOut->p4.p = 0;
   659    654       pOut->p5 = 0;
   660    655   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   661    656       pOut->zComment = 0;
   662    657   #endif
................................................................................
  1721   1716   ** NULL, it means that memory space has already been allocated and that
  1722   1717   ** this routine should not allocate any new memory.  When pBuf is not
  1723   1718   ** NULL simply return pBuf.  Only allocate new memory space when pBuf
  1724   1719   ** is NULL.
  1725   1720   **
  1726   1721   ** nByte is the number of bytes of space needed.
  1727   1722   **
  1728         -** *ppFrom points to available space and pEnd points to the end of the
  1729         -** available space.  When space is allocated, *ppFrom is advanced past
  1730         -** the end of the allocated space.
         1723  +** pFrom points to *pnFrom bytes of available space.  New space is allocated
         1724  +** from the end of the pFrom buffer and *pnFrom is decremented.
  1731   1725   **
  1732         -** *pnByte is a counter of the number of bytes of space that have failed
  1733         -** to allocate.  If there is insufficient space in *ppFrom to satisfy the
  1734         -** request, then increment *pnByte by the amount of the request.
         1726  +** *pnNeeded is a counter of the number of bytes of space that have failed
         1727  +** to allocate.  If there is insufficient space in pFrom to satisfy the
         1728  +** request, then increment *pnNeeded by the amount of the request.
  1735   1729   */
  1736   1730   static void *allocSpace(
  1737   1731     void *pBuf,          /* Where return pointer will be stored */
  1738   1732     int nByte,           /* Number of bytes to allocate */
  1739         -  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
  1740         -  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
  1741         -  int *pnByte          /* If allocation cannot be made, increment *pnByte */
         1733  +  u8 *pFrom,           /* Memory available for allocation */
         1734  +  int *pnFrom,         /* IN/OUT: Space available at pFrom */
         1735  +  int *pnNeeded        /* If allocation cannot be made, increment *pnByte */
  1742   1736   ){
  1743         -  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
  1744         -  if( pBuf ) return pBuf;
  1745         -  nByte = ROUND8(nByte);
  1746         -  if( &(*ppFrom)[nByte] <= pEnd ){
  1747         -    pBuf = (void*)*ppFrom;
  1748         -    *ppFrom += nByte;
  1749         -  }else{
  1750         -    *pnByte += nByte;
         1737  +  assert( EIGHT_BYTE_ALIGNMENT(pFrom) );
         1738  +  if( pBuf==0 ){
         1739  +    nByte = ROUND8(nByte);
         1740  +    if( nByte <= *pnFrom ){
         1741  +      *pnFrom -= nByte;
         1742  +      pBuf = &pFrom[*pnFrom];
         1743  +    }else{
         1744  +      *pnNeeded += nByte;
         1745  +    }
  1751   1746     }
         1747  +  assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
  1752   1748     return pBuf;
  1753   1749   }
  1754   1750   
  1755   1751   /*
  1756   1752   ** Rewind the VDBE back to the beginning in preparation for
  1757   1753   ** running it.
  1758   1754   */
................................................................................
  1817   1813     sqlite3 *db;                   /* The database connection */
  1818   1814     int nVar;                      /* Number of parameters */
  1819   1815     int nMem;                      /* Number of VM memory registers */
  1820   1816     int nCursor;                   /* Number of cursors required */
  1821   1817     int nArg;                      /* Number of arguments in subprograms */
  1822   1818     int nOnce;                     /* Number of OP_Once instructions */
  1823   1819     int n;                         /* Loop counter */
         1820  +  int nFree;                     /* Available free space */
  1824   1821     u8 *zCsr;                      /* Memory available for allocation */
  1825         -  u8 *zEnd;                      /* First byte past allocated memory */
  1826   1822     int nByte;                     /* How much extra memory is needed */
  1827   1823   
  1828   1824     assert( p!=0 );
  1829   1825     assert( p->nOp>0 );
  1830   1826     assert( pParse!=0 );
  1831   1827     assert( p->magic==VDBE_MAGIC_INIT );
  1832   1828     assert( pParse==p->pParse );
................................................................................
  1850   1846     */
  1851   1847     nMem += nCursor;
  1852   1848   
  1853   1849     /* Allocate space for memory registers, SQL variables, VDBE cursors and 
  1854   1850     ** an array to marshal SQL function arguments in.
  1855   1851     */
  1856   1852     zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
  1857         -  zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
         1853  +  assert( pParse->nOpAlloc*sizeof(Op) <= 0x7fffff00 );
         1854  +  nFree = (pParse->nOpAlloc - p->nOp)*sizeof(p->aOp[0]); /* Available space */
  1858   1855   
  1859   1856     resolveP2Values(p, &nArg);
  1860   1857     p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
  1861   1858     if( pParse->explain && nMem<10 ){
  1862   1859       nMem = 10;
  1863   1860     }
  1864         -  memset(zCsr, 0, zEnd-zCsr);
         1861  +  memset(zCsr, 0, nFree);
         1862  +  nFree -= (zCsr - (u8*)0)&7;
  1865   1863     zCsr += (zCsr - (u8*)0)&7;
  1866   1864     assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
  1867   1865     p->expired = 0;
  1868   1866   
  1869   1867     p->expired = 0;
  1870   1868     
  1871   1869     /* Memory for registers, parameters, cursor, etc, is allocated in two
................................................................................
  1876   1874     **
  1877   1875     ** This two-pass approach that reuses as much memory as possible from
  1878   1876     ** the leftover space at the end of the opcode array can significantly
  1879   1877     ** reduce the amount of memory held by a prepared statement.
  1880   1878     */
  1881   1879     do {
  1882   1880       nByte = 0;
  1883         -    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
  1884         -    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
  1885         -    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
  1886         -    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
         1881  +    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte);
         1882  +    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte);
         1883  +    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte);
         1884  +    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), zCsr, &nFree, &nByte);
  1887   1885       p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
  1888         -                          &zCsr, zEnd, &nByte);
  1889         -    p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
         1886  +                          zCsr, &nFree, &nByte);
         1887  +    p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte);
  1890   1888   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  1891         -    p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
         1889  +    p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte);
  1892   1890   #endif
  1893   1891       if( nByte ){
  1894   1892         p->pFree = sqlite3DbMallocZero(db, nByte);
  1895   1893       }
  1896   1894       zCsr = p->pFree;
  1897         -    zEnd = &zCsr[nByte];
         1895  +    nFree = nByte;
  1898   1896     }while( nByte && !db->mallocFailed );
  1899   1897   
  1900   1898     p->nCursor = nCursor;
  1901   1899     p->nOnceFlag = nOnce;
  1902   1900     if( p->aVar ){
  1903   1901       p->nVar = (ynVar)nVar;
  1904   1902       for(n=0; n<nVar; n++){
................................................................................
  3233   3231     }
  3234   3232   
  3235   3233     /* String or blob */
  3236   3234     if( serial_type>=12 ){
  3237   3235       assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
  3238   3236                == (int)sqlite3VdbeSerialTypeLen(serial_type) );
  3239   3237       len = pMem->n;
  3240         -    memcpy(buf, pMem->z, len);
         3238  +    if( len>0 ) memcpy(buf, pMem->z, len);
  3241   3239       return len;
  3242   3240     }
  3243   3241   
  3244   3242     /* NULL or constants 0 or 1 */
  3245   3243     return 0;
  3246   3244   }
  3247   3245   

Changes to src/wal.c.

   268    268   ** returns SQLITE_CANTOPEN.
   269    269   */
   270    270   #define WAL_MAX_VERSION      3007000
   271    271   #define WALINDEX_MAX_VERSION 3007000
   272    272   
   273    273   /*
   274    274   ** Indices of various locking bytes.   WAL_NREADER is the number
   275         -** of available reader locks and should be at least 3.
          275  +** of available reader locks and should be at least 3.  The default
          276  +** is SQLITE_SHM_NLOCK==8 and  WAL_NREADER==5.
   276    277   */
   277    278   #define WAL_WRITE_LOCK         0
   278    279   #define WAL_ALL_BUT_WRITE      1
   279    280   #define WAL_CKPT_LOCK          1
   280    281   #define WAL_RECOVER_LOCK       2
   281    282   #define WAL_READ_LOCK(I)       (3+(I))
   282    283   #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
................................................................................
   288    289   typedef struct WalCkptInfo WalCkptInfo;
   289    290   
   290    291   
   291    292   /*
   292    293   ** The following object holds a copy of the wal-index header content.
   293    294   **
   294    295   ** The actual header in the wal-index consists of two copies of this
   295         -** object.
          296  +** object followed by one instance of the WalCkptInfo object.
          297  +** For all versions of SQLite through 3.10.0 and probably beyond,
          298  +** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
          299  +** the total header size is 136 bytes.
   296    300   **
   297    301   ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
   298    302   ** Or it can be 1 to represent a 65536-byte page.  The latter case was
   299    303   ** added in 3.7.1 when support for 64K pages was added.  
   300    304   */
   301    305   struct WalIndexHdr {
   302    306     u32 iVersion;                   /* Wal-index version */
................................................................................
   320    324   ** nBackfill is the number of frames in the WAL that have been written
   321    325   ** back into the database. (We call the act of moving content from WAL to
   322    326   ** database "backfilling".)  The nBackfill number is never greater than
   323    327   ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
   324    328   ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
   325    329   ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
   326    330   ** mxFrame back to zero when the WAL is reset.
          331  +**
          332  +** nBackfillAttempted is the largest value of nBackfill that a checkpoint
          333  +** has attempted to achieve.  Normally nBackfill==nBackfillAtempted, however
          334  +** the nBackfillAttempted is set before any backfilling is done and the
          335  +** nBackfill is only set after all backfilling completes.  So if a checkpoint
          336  +** crashes, nBackfillAttempted might be larger than nBackfill.  The
          337  +** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
          338  +**
          339  +** The aLock[] field is a set of bytes used for locking.  These bytes should
          340  +** never be read or written.
   327    341   **
   328    342   ** There is one entry in aReadMark[] for each reader lock.  If a reader
   329    343   ** holds read-lock K, then the value in aReadMark[K] is no greater than
   330    344   ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
   331    345   ** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
   332    346   ** a special case; its value is never used and it exists as a place-holder
   333    347   ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
................................................................................
   360    374   **
   361    375   ** We assume that 32-bit loads are atomic and so no locks are needed in
   362    376   ** order to read from any aReadMark[] entries.
   363    377   */
   364    378   struct WalCkptInfo {
   365    379     u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
   366    380     u32 aReadMark[WAL_NREADER];     /* Reader marks */
          381  +  u8 aLock[SQLITE_SHM_NLOCK];     /* Reserved space for locks */
          382  +  u32 nBackfillAttempted;         /* WAL frames perhaps written, or maybe not */
          383  +  u32 notUsed0;                   /* Available for future enhancements */
   367    384   };
   368    385   #define READMARK_NOT_USED  0xffffffff
   369    386   
   370    387   
   371    388   /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
   372    389   ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
   373    390   ** only support mandatory file-locks, we do not read or write data
   374    391   ** from the region of the file on which locks are applied.
   375    392   */
   376         -#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
   377         -#define WALINDEX_LOCK_RESERVED 16
   378         -#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
          393  +#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
          394  +#define WALINDEX_HDR_SIZE    (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
   379    395   
   380    396   /* Size of header before each frame in wal */
   381    397   #define WAL_FRAME_HDRSIZE 24
   382    398   
   383    399   /* Size of write ahead log header, including checksum. */
   384    400   /* #define WAL_HDRSIZE 24 */
   385    401   #define WAL_HDRSIZE 32
................................................................................
   430    446     WalIndexHdr hdr;           /* Wal-index header for current transaction */
   431    447     u32 minFrame;              /* Ignore wal frames before this one */
   432    448     const char *zWalName;      /* Name of WAL file */
   433    449     u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
   434    450   #ifdef SQLITE_DEBUG
   435    451     u8 lockError;              /* True if a locking error has occurred */
   436    452   #endif
          453  +#ifdef SQLITE_ENABLE_SNAPSHOT
          454  +  WalIndexHdr *pSnapshot;    /* Start transaction here if not NULL */
          455  +#endif
   437    456   };
   438    457   
   439    458   /*
   440    459   ** Candidate values for Wal.exclusiveMode.
   441    460   */
   442    461   #define WAL_NORMAL_MODE     0
   443    462   #define WAL_EXCLUSIVE_MODE  1     
................................................................................
  1194   1213   
  1195   1214       /* Reset the checkpoint-header. This is safe because this thread is 
  1196   1215       ** currently holding locks that exclude all other readers, writers and
  1197   1216       ** checkpointers.
  1198   1217       */
  1199   1218       pInfo = walCkptInfo(pWal);
  1200   1219       pInfo->nBackfill = 0;
         1220  +    pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
  1201   1221       pInfo->aReadMark[0] = 0;
  1202   1222       for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  1203   1223       if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
  1204   1224   
  1205   1225       /* If more than one frame was recovered from the log file, report an
  1206   1226       ** event via sqlite3_log(). This is to help with identifying performance
  1207   1227       ** problems caused by applications routinely shutting down without
................................................................................
  1266   1286   
  1267   1287     assert( zWalName && zWalName[0] );
  1268   1288     assert( pDbFd );
  1269   1289   
  1270   1290     /* In the amalgamation, the os_unix.c and os_win.c source files come before
  1271   1291     ** this source file.  Verify that the #defines of the locking byte offsets
  1272   1292     ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
         1293  +  ** For that matter, if the lock offset ever changes from its initial design
         1294  +  ** value of 120, we need to know that so there is an assert() to check it.
  1273   1295     */
         1296  +  assert( 120==WALINDEX_LOCK_OFFSET );
         1297  +  assert( 136==WALINDEX_HDR_SIZE );
  1274   1298   #ifdef WIN_SHM_BASE
  1275   1299     assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
  1276   1300   #endif
  1277   1301   #ifdef UNIX_SHM_BASE
  1278   1302     assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
  1279   1303   #endif
  1280   1304   
................................................................................
  1656   1680     u32 *aSalt = pWal->hdr.aSalt;   /* Big-endian salt values */
  1657   1681     pWal->nCkpt++;
  1658   1682     pWal->hdr.mxFrame = 0;
  1659   1683     sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
  1660   1684     memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
  1661   1685     walIndexWriteHdr(pWal);
  1662   1686     pInfo->nBackfill = 0;
         1687  +  pInfo->nBackfillAttempted = 0;
  1663   1688     pInfo->aReadMark[1] = 0;
  1664   1689     for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  1665   1690     assert( pInfo->aReadMark[0]==0 );
  1666   1691   }
  1667   1692   
  1668   1693   /*
  1669   1694   ** Copy as much content as we can from the WAL back into the database file
................................................................................
  1764   1789       }
  1765   1790   
  1766   1791       if( pInfo->nBackfill<mxSafeFrame
  1767   1792        && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
  1768   1793       ){
  1769   1794         i64 nSize;                    /* Current size of database file */
  1770   1795         u32 nBackfill = pInfo->nBackfill;
         1796  +
         1797  +      pInfo->nBackfillAttempted = mxSafeFrame;
  1771   1798   
  1772   1799         /* Sync the WAL to disk */
  1773   1800         if( sync_flags ){
  1774   1801           rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
  1775   1802         }
  1776   1803   
  1777   1804         /* If the database may grow as a result of this checkpoint, hint
................................................................................
  2151   2178   */
  2152   2179   static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
  2153   2180     volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
  2154   2181     u32 mxReadMark;                 /* Largest aReadMark[] value */
  2155   2182     int mxI;                        /* Index of largest aReadMark[] value */
  2156   2183     int i;                          /* Loop counter */
  2157   2184     int rc = SQLITE_OK;             /* Return code  */
         2185  +  u32 mxFrame;                    /* Wal frame to lock to */
  2158   2186   
  2159   2187     assert( pWal->readLock<0 );     /* Not currently locked */
  2160   2188   
  2161   2189     /* Take steps to avoid spinning forever if there is a protocol error.
  2162   2190     **
  2163   2191     ** Circumstances that cause a RETRY should only last for the briefest
  2164   2192     ** instances of time.  No I/O or other system calls are done while the
................................................................................
  2214   2242       }
  2215   2243       if( rc!=SQLITE_OK ){
  2216   2244         return rc;
  2217   2245       }
  2218   2246     }
  2219   2247   
  2220   2248     pInfo = walCkptInfo(pWal);
  2221         -  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
         2249  +  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame 
         2250  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2251  +   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0
         2252  +     || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr)))
         2253  +#endif
         2254  +  ){
  2222   2255       /* The WAL has been completely backfilled (or it is empty).
  2223   2256       ** and can be safely ignored.
  2224   2257       */
  2225   2258       rc = walLockShared(pWal, WAL_READ_LOCK(0));
  2226   2259       walShmBarrier(pWal);
  2227   2260       if( rc==SQLITE_OK ){
  2228   2261         if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
................................................................................
  2252   2285     /* If we get this far, it means that the reader will want to use
  2253   2286     ** the WAL to get at content from recent commits.  The job now is
  2254   2287     ** to select one of the aReadMark[] entries that is closest to
  2255   2288     ** but not exceeding pWal->hdr.mxFrame and lock that entry.
  2256   2289     */
  2257   2290     mxReadMark = 0;
  2258   2291     mxI = 0;
         2292  +  mxFrame = pWal->hdr.mxFrame;
         2293  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2294  +  if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
         2295  +    mxFrame = pWal->pSnapshot->mxFrame;
         2296  +  }
         2297  +#endif
  2259   2298     for(i=1; i<WAL_NREADER; i++){
  2260   2299       u32 thisMark = pInfo->aReadMark[i];
  2261         -    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
         2300  +    if( mxReadMark<=thisMark && thisMark<=mxFrame ){
  2262   2301         assert( thisMark!=READMARK_NOT_USED );
  2263   2302         mxReadMark = thisMark;
  2264   2303         mxI = i;
  2265   2304       }
  2266   2305     }
  2267         -  /* There was once an "if" here. The extra "{" is to preserve indentation. */
  2268         -  {
  2269         -    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
  2270         -     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
  2271         -    ){
  2272         -      for(i=1; i<WAL_NREADER; i++){
  2273         -        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
  2274         -        if( rc==SQLITE_OK ){
  2275         -          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
  2276         -          mxI = i;
  2277         -          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
  2278         -          break;
  2279         -        }else if( rc!=SQLITE_BUSY ){
  2280         -          return rc;
  2281         -        }
         2306  +  if( (pWal->readOnly & WAL_SHM_RDONLY)==0
         2307  +   && (mxReadMark<mxFrame || mxI==0)
         2308  +  ){
         2309  +    for(i=1; i<WAL_NREADER; i++){
         2310  +      rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
         2311  +      if( rc==SQLITE_OK ){
         2312  +        mxReadMark = pInfo->aReadMark[i] = mxFrame;
         2313  +        mxI = i;
         2314  +        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
         2315  +        break;
         2316  +      }else if( rc!=SQLITE_BUSY ){
         2317  +        return rc;
  2282   2318         }
  2283   2319       }
  2284         -    if( mxI==0 ){
  2285         -      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
  2286         -      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
  2287         -    }
  2288         -
  2289         -    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
  2290         -    if( rc ){
  2291         -      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
  2292         -    }
  2293         -    /* Now that the read-lock has been obtained, check that neither the
  2294         -    ** value in the aReadMark[] array or the contents of the wal-index
  2295         -    ** header have changed.
  2296         -    **
  2297         -    ** It is necessary to check that the wal-index header did not change
  2298         -    ** between the time it was read and when the shared-lock was obtained
  2299         -    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
  2300         -    ** that the log file may have been wrapped by a writer, or that frames
  2301         -    ** that occur later in the log than pWal->hdr.mxFrame may have been
  2302         -    ** copied into the database by a checkpointer. If either of these things
  2303         -    ** happened, then reading the database with the current value of
  2304         -    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
  2305         -    ** instead.
  2306         -    **
  2307         -    ** Before checking that the live wal-index header has not changed
  2308         -    ** since it was read, set Wal.minFrame to the first frame in the wal
  2309         -    ** file that has not yet been checkpointed. This client will not need
  2310         -    ** to read any frames earlier than minFrame from the wal file - they
  2311         -    ** can be safely read directly from the database file.
  2312         -    **
  2313         -    ** Because a ShmBarrier() call is made between taking the copy of 
  2314         -    ** nBackfill and checking that the wal-header in shared-memory still
  2315         -    ** matches the one cached in pWal->hdr, it is guaranteed that the 
  2316         -    ** checkpointer that set nBackfill was not working with a wal-index
  2317         -    ** header newer than that cached in pWal->hdr. If it were, that could
  2318         -    ** cause a problem. The checkpointer could omit to checkpoint
  2319         -    ** a version of page X that lies before pWal->minFrame (call that version
  2320         -    ** A) on the basis that there is a newer version (version B) of the same
  2321         -    ** page later in the wal file. But if version B happens to like past
  2322         -    ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
  2323         -    ** that it can read version A from the database file. However, since
  2324         -    ** we can guarantee that the checkpointer that set nBackfill could not
  2325         -    ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
  2326         -    */
  2327         -    pWal->minFrame = pInfo->nBackfill+1;
  2328         -    walShmBarrier(pWal);
  2329         -    if( pInfo->aReadMark[mxI]!=mxReadMark
  2330         -     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
  2331         -    ){
  2332         -      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
  2333         -      return WAL_RETRY;
  2334         -    }else{
  2335         -      assert( mxReadMark<=pWal->hdr.mxFrame );
  2336         -      pWal->readLock = (i16)mxI;
  2337         -    }
         2320  +  }
         2321  +  if( mxI==0 ){
         2322  +    assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
         2323  +    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
         2324  +  }
         2325  +
         2326  +  rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
         2327  +  if( rc ){
         2328  +    return rc==SQLITE_BUSY ? WAL_RETRY : rc;
         2329  +  }
         2330  +  /* Now that the read-lock has been obtained, check that neither the
         2331  +  ** value in the aReadMark[] array or the contents of the wal-index
         2332  +  ** header have changed.
         2333  +  **
         2334  +  ** It is necessary to check that the wal-index header did not change
         2335  +  ** between the time it was read and when the shared-lock was obtained
         2336  +  ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
         2337  +  ** that the log file may have been wrapped by a writer, or that frames
         2338  +  ** that occur later in the log than pWal->hdr.mxFrame may have been
         2339  +  ** copied into the database by a checkpointer. If either of these things
         2340  +  ** happened, then reading the database with the current value of
         2341  +  ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
         2342  +  ** instead.
         2343  +  **
         2344  +  ** Before checking that the live wal-index header has not changed
         2345  +  ** since it was read, set Wal.minFrame to the first frame in the wal
         2346  +  ** file that has not yet been checkpointed. This client will not need
         2347  +  ** to read any frames earlier than minFrame from the wal file - they
         2348  +  ** can be safely read directly from the database file.
         2349  +  **
         2350  +  ** Because a ShmBarrier() call is made between taking the copy of 
         2351  +  ** nBackfill and checking that the wal-header in shared-memory still
         2352  +  ** matches the one cached in pWal->hdr, it is guaranteed that the 
         2353  +  ** checkpointer that set nBackfill was not working with a wal-index
         2354  +  ** header newer than that cached in pWal->hdr. If it were, that could
         2355  +  ** cause a problem. The checkpointer could omit to checkpoint
         2356  +  ** a version of page X that lies before pWal->minFrame (call that version
         2357  +  ** A) on the basis that there is a newer version (version B) of the same
         2358  +  ** page later in the wal file. But if version B happens to like past
         2359  +  ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
         2360  +  ** that it can read version A from the database file. However, since
         2361  +  ** we can guarantee that the checkpointer that set nBackfill could not
         2362  +  ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
         2363  +  */
         2364  +  pWal->minFrame = pInfo->nBackfill+1;
         2365  +  walShmBarrier(pWal);
         2366  +  if( pInfo->aReadMark[mxI]!=mxReadMark
         2367  +   || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
         2368  +  ){
         2369  +    walUnlockShared(pWal, WAL_READ_LOCK(mxI));
         2370  +    return WAL_RETRY;
         2371  +  }else{
         2372  +    assert( mxReadMark<=pWal->hdr.mxFrame );
         2373  +    pWal->readLock = (i16)mxI;
  2338   2374     }
  2339   2375     return rc;
  2340   2376   }
  2341   2377   
  2342   2378   /*
  2343   2379   ** Begin a read transaction on the database.
  2344   2380   **
................................................................................
  2353   2389   ** Pager layer will use this to know that is cache is stale and
  2354   2390   ** needs to be flushed.
  2355   2391   */
  2356   2392   int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
  2357   2393     int rc;                         /* Return code */
  2358   2394     int cnt = 0;                    /* Number of TryBeginRead attempts */
  2359   2395   
         2396  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2397  +  int bChanged = 0;
         2398  +  WalIndexHdr *pSnapshot = pWal->pSnapshot;
         2399  +  if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
         2400  +    bChanged = 1;
         2401  +  }
         2402  +#endif
         2403  +
  2360   2404     do{
  2361   2405       rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
  2362   2406     }while( rc==WAL_RETRY );
  2363   2407     testcase( (rc&0xff)==SQLITE_BUSY );
  2364   2408     testcase( (rc&0xff)==SQLITE_IOERR );
  2365   2409     testcase( rc==SQLITE_PROTOCOL );
  2366   2410     testcase( rc==SQLITE_OK );
         2411  +
         2412  +#ifdef SQLITE_ENABLE_SNAPSHOT
         2413  +  if( rc==SQLITE_OK ){
         2414  +    if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
         2415  +      /* At this point the client has a lock on an aReadMark[] slot holding
         2416  +      ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
         2417  +      ** is populated with the wal-index header corresponding to the head
         2418  +      ** of the wal file. Verify that pSnapshot is still valid before
         2419  +      ** continuing.  Reasons why pSnapshot might no longer be valid:
         2420  +      **
         2421  +      **    (1)  The WAL file has been reset since the snapshot was taken.
         2422  +      **         In this case, the salt will have changed.
         2423  +      **
         2424  +      **    (2)  A checkpoint as been attempted that wrote frames past
         2425  +      **         pSnapshot->mxFrame into the database file.  Note that the
         2426  +      **         checkpoint need not have completed for this to cause problems.
         2427  +      */
         2428  +      volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
         2429  +
         2430  +      assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
         2431  +      assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
         2432  +
         2433  +      /* It is possible that there is a checkpointer thread running 
         2434  +      ** concurrent with this code. If this is the case, it may be that the
         2435  +      ** checkpointer has already determined that it will checkpoint 
         2436  +      ** snapshot X, where X is later in the wal file than pSnapshot, but 
         2437  +      ** has not yet set the pInfo->nBackfillAttempted variable to indicate 
         2438  +      ** its intent. To avoid the race condition this leads to, ensure that
         2439  +      ** there is no checkpointer process by taking a shared CKPT lock 
         2440  +      ** before checking pInfo->nBackfillAttempted.  */
         2441  +      rc = walLockShared(pWal, WAL_CKPT_LOCK);
         2442  +
         2443  +      if( rc==SQLITE_OK ){
         2444  +        /* Check that the wal file has not been wrapped. Assuming that it has
         2445  +        ** not, also check that no checkpointer has attempted to checkpoint any
         2446  +        ** frames beyond pSnapshot->mxFrame. If either of these conditions are
         2447  +        ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr
         2448  +        ** with *pSnapshot and set *pChanged as appropriate for opening the
         2449  +        ** snapshot.  */
         2450  +        if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
         2451  +         && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
         2452  +        ){
         2453  +          memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
         2454  +          *pChanged = bChanged;
         2455  +        }else{
         2456  +          rc = SQLITE_BUSY_SNAPSHOT;
         2457  +        }
         2458  +
         2459  +        /* Release the shared CKPT lock obtained above. */
         2460  +        walUnlockShared(pWal, WAL_CKPT_LOCK);
         2461  +      }
         2462  +
         2463  +
         2464  +      if( rc!=SQLITE_OK ){
         2465  +        sqlite3WalEndReadTransaction(pWal);
         2466  +      }
         2467  +    }
         2468  +  }
         2469  +#endif
  2367   2470     return rc;
  2368   2471   }
  2369   2472   
  2370   2473   /*
  2371   2474   ** Finish with a read transaction.  All this does is release the
  2372   2475   ** read-lock.
  2373   2476   */
................................................................................
  3206   3309   ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
  3207   3310   ** WAL module is using shared-memory, return false. 
  3208   3311   */
  3209   3312   int sqlite3WalHeapMemory(Wal *pWal){
  3210   3313     return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
  3211   3314   }
  3212   3315   
         3316  +#ifdef SQLITE_ENABLE_SNAPSHOT
         3317  +/* Create a snapshot object.  The content of a snapshot is opaque to
         3318  +** every other subsystem, so the WAL module can put whatever it needs
         3319  +** in the object.
         3320  +*/
         3321  +int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
         3322  +  int rc = SQLITE_OK;
         3323  +  WalIndexHdr *pRet;
         3324  +
         3325  +  assert( pWal->readLock>=0 && pWal->writeLock==0 );
         3326  +
         3327  +  pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
         3328  +  if( pRet==0 ){
         3329  +    rc = SQLITE_NOMEM;
         3330  +  }else{
         3331  +    memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
         3332  +    *ppSnapshot = (sqlite3_snapshot*)pRet;
         3333  +  }
         3334  +
         3335  +  return rc;
         3336  +}
         3337  +
         3338  +/* Try to open on pSnapshot when the next read-transaction starts
         3339  +*/
         3340  +void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
         3341  +  pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
         3342  +}
         3343  +#endif /* SQLITE_ENABLE_SNAPSHOT */
         3344  +
  3213   3345   #ifdef SQLITE_ENABLE_ZIPVFS
  3214   3346   /*
  3215   3347   ** If the argument is not NULL, it points to a Wal object that holds a
  3216   3348   ** read-lock. This function returns the database page-size if it is known,
  3217   3349   ** or zero if it is not (or if pWal is NULL).
  3218   3350   */
  3219   3351   int sqlite3WalFramesize(Wal *pWal){
  3220   3352     assert( pWal==0 || pWal->readLock>=0 );
  3221   3353     return (pWal ? pWal->szPage : 0);
  3222   3354   }
  3223   3355   #endif
  3224   3356   
  3225   3357   #endif /* #ifndef SQLITE_OMIT_WAL */

Changes to src/wal.h.

   124    124   int sqlite3WalExclusiveMode(Wal *pWal, int op);
   125    125   
   126    126   /* Return true if the argument is non-NULL and the WAL module is using
   127    127   ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
   128    128   ** WAL module is using shared-memory, return false. 
   129    129   */
   130    130   int sqlite3WalHeapMemory(Wal *pWal);
          131  +
          132  +#ifdef SQLITE_ENABLE_SNAPSHOT
          133  +int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
          134  +void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
          135  +#endif
   131    136   
   132    137   #ifdef SQLITE_ENABLE_ZIPVFS
   133    138   /* If the WAL file is not empty, return the number of bytes of content
   134    139   ** stored in each frame (i.e. the db page-size when the WAL was created).
   135    140   */
   136    141   int sqlite3WalFramesize(Wal *pWal);
   137    142   #endif
   138    143   
   139    144   #endif /* ifndef SQLITE_OMIT_WAL */
   140    145   #endif /* _WAL_H_ */

Changes to test/pragma2.test.

   121    121         PRAGMA aux.freelist_count;
   122    122       }
   123    123     } {9 9}
   124    124   }
   125    125   
   126    126   # Default setting of PRAGMA cache_spill is always ON
   127    127   #
   128         -# EVIDENCE-OF: R-51036-62828 PRAGMA cache_spill; PRAGMA
   129         -# cache_spill=boolean;
          128  +# EVIDENCE-OF: R-63549-59887 PRAGMA cache_spill; PRAGMA
          129  +# cache_spill=boolean; PRAGMA schema.cache_spill=N;
   130    130   #
   131    131   # EVIDENCE-OF: R-23955-02765 Cache_spill is enabled by default
   132    132   #
   133    133   db close
   134    134   delete_file test.db test.db-journal
   135    135   delete_file test2.db test2.db-journal
   136    136   sqlite3 db test.db
................................................................................
   186    186       PRAGMA cache_spill=OFF;
   187    187       PRAGMA Cache_Spill;
   188    188       BEGIN;
   189    189       UPDATE t1 SET c=c+1;
   190    190       PRAGMA lock_status;
   191    191     }
   192    192   } {0 main reserved temp unknown}   ;# No cache spill, so no exclusive lock
          193  +
          194  +
          195  +# EVIDENCE-OF: R-34657-61226 The "PRAGMA cache_spill=N" form of this
          196  +# pragma sets a minimum cache size threshold required for spilling to
          197  +# occur.
   193    198   do_test pragma2-4.5.2 {
   194    199     db eval {
   195    200       ROLLBACK;
   196    201       PRAGMA cache_spill=100000;
   197    202       PRAGMA cache_spill;
   198    203       BEGIN;
   199    204       UPDATE t1 SET c=c+1;

Added test/snapshot.test.

            1  +# 2015 December 7
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library. The focus
           12  +# of this file is the sqlite3_snapshot_xxx() APIs.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +ifcapable !snapshot {finish_test; return}
           18  +set testprefix snapshot
           19  +
           20  +#-------------------------------------------------------------------------
           21  +# Check some error conditions in snapshot_get(). It is an error if:
           22  +#
           23  +#  1) snapshot_get() is called on a non-WAL database, or
           24  +#  2) there is an open write transaction on the database.
           25  +#
           26  +do_execsql_test 1.0 {
           27  +  CREATE TABLE t1(a, b);
           28  +  INSERT INTO t1 VALUES(1, 2);
           29  +  INSERT INTO t1 VALUES(3, 4);
           30  +}
           31  +
           32  +do_test 1.1.1 {
           33  +  execsql { BEGIN; SELECT * FROM t1; }
           34  +  list [catch { sqlite3_snapshot_get db main } msg] $msg
           35  +} {1 SQLITE_ERROR}
           36  +do_execsql_test 1.1.2 COMMIT
           37  +
           38  +do_test 1.2.1 {
           39  +  execsql {
           40  +    PRAGMA journal_mode = WAL;
           41  +    BEGIN;
           42  +      INSERT INTO t1 VALUES(5, 6);
           43  +      INSERT INTO t1 VALUES(7, 8);
           44  +  }
           45  +  list [catch { sqlite3_snapshot_get db main } msg] $msg
           46  +} {1 SQLITE_ERROR}
           47  +do_execsql_test 1.3.2 COMMIT
           48  +
           49  +#-------------------------------------------------------------------------
           50  +# Check that a simple case works. Reuse the database created by the
           51  +# block of tests above.
           52  +#
           53  +do_execsql_test 2.1.0 {
           54  +  BEGIN;
           55  +    SELECT * FROM t1;
           56  +} {1 2 3 4 5 6 7 8}
           57  +
           58  +breakpoint
           59  +do_test 2.1.1 {
           60  +  set snapshot [sqlite3_snapshot_get db main]
           61  +  execsql {
           62  +    COMMIT;
           63  +    INSERT INTO t1 VALUES(9, 10);
           64  +    SELECT * FROM t1;
           65  +  }
           66  +} {1 2 3 4 5 6 7 8 9 10}
           67  +
           68  +do_test 2.1.2 {
           69  +  execsql BEGIN
           70  +  sqlite3_snapshot_open db main $snapshot
           71  +  execsql {
           72  +    SELECT * FROM t1;
           73  +  }
           74  +} {1 2 3 4 5 6 7 8}
           75  +
           76  +do_test 2.1.3 {
           77  +  sqlite3_snapshot_free $snapshot
           78  +  execsql COMMIT
           79  +} {}
           80  +
           81  +do_test 2.2.0 {
           82  +  sqlite3 db2 test.db
           83  +  execsql {
           84  +    BEGIN;
           85  +      SELECT * FROM t1;
           86  +  } db2
           87  +} {1 2 3 4 5 6 7 8 9 10}
           88  +
           89  +do_test 2.2.1 {
           90  +  set snapshot [sqlite3_snapshot_get db2 main]
           91  +  execsql {
           92  +    INSERT INTO t1 VALUES(11, 12);
           93  +    SELECT * FROM t1;
           94  +  }
           95  +} {1 2 3 4 5 6 7 8 9 10 11 12}
           96  +
           97  +do_test 2.2.2 {
           98  +  execsql BEGIN
           99  +  sqlite3_snapshot_open db main $snapshot
          100  +  execsql {
          101  +    SELECT * FROM t1;
          102  +  }
          103  +} {1 2 3 4 5 6 7 8 9 10}
          104  +
          105  +do_test 2.2.3 {
          106  +  sqlite3_snapshot_free $snapshot
          107  +  execsql COMMIT
          108  +  execsql COMMIT db2
          109  +  db2 close
          110  +} {}
          111  +
          112  +do_test 2.3.1 {
          113  +  execsql { DELETE FROM t1 WHERE a>6 }
          114  +  set snapshot [sqlite3_snapshot_get db main]
          115  +  execsql {
          116  +    INSERT INTO t1 VALUES('a', 'b');
          117  +    INSERT INTO t1 VALUES('c', 'd');
          118  +    SELECT * FROM t1;
          119  +  }
          120  +} {1 2 3 4 5 6 a b c d}
          121  +do_test 2.3.2 {
          122  +  execsql BEGIN
          123  +  sqlite3_snapshot_open db main $snapshot
          124  +  execsql { SELECT * FROM t1 }
          125  +} {1 2 3 4 5 6}
          126  +
          127  +do_test 2.3.3 {
          128  +  catchsql {
          129  +    INSERT INTO t1 VALUES('x','y')
          130  +  }
          131  +} {1 {database is locked}}
          132  +do_test 2.3.4 {
          133  +  execsql COMMIT
          134  +  sqlite3_snapshot_free $snapshot
          135  +} {}
          136  +
          137  +#-------------------------------------------------------------------------
          138  +# Check some errors in sqlite3_snapshot_open(). It is an error if:
          139  +#
          140  +#   1) the db is in auto-commit mode,
          141  +#   2) the db has an open (read or write) transaction,
          142  +#   3) the db is not a wal database,
          143  +#
          144  +# Reuse the database created by earlier tests.
          145  +#
          146  +do_execsql_test 3.0.0 {
          147  +  CREATE TABLE t2(x, y);
          148  +  INSERT INTO t2 VALUES('a', 'b');
          149  +  INSERT INTO t2 VALUES('c', 'd');
          150  +  BEGIN;
          151  +    SELECT * FROM t2;
          152  +} {a b c d}
          153  +do_test 3.0.1 {
          154  +  set snapshot [sqlite3_snapshot_get db main]
          155  +  execsql { COMMIT }
          156  +  execsql { INSERT INTO t2 VALUES('e', 'f'); }
          157  +} {}
          158  +
          159  +do_test 3.1 {
          160  +  list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
          161  +} {1 SQLITE_ERROR}
          162  +
          163  +do_test 3.2.1 {
          164  +  execsql {
          165  +    BEGIN;
          166  +      SELECT * FROM t2;
          167  +  }
          168  +} {a b c d e f}
          169  +do_test 3.2.2 {
          170  +  list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
          171  +} {1 SQLITE_ERROR}
          172  +
          173  +do_test 3.2.3 {
          174  +  execsql {
          175  +    COMMIT;
          176  +    BEGIN;
          177  +      INSERT INTO t2 VALUES('g', 'h');
          178  +  }
          179  +  list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
          180  +} {1 SQLITE_ERROR}
          181  +do_execsql_test 3.2.4 COMMIT
          182  +
          183  +do_test 3.3.1 {
          184  +  execsql { PRAGMA journal_mode = DELETE }
          185  +  execsql { BEGIN }
          186  +  list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
          187  +} {1 SQLITE_ERROR}
          188  +
          189  +do_test 3.3.2 {
          190  +  sqlite3_snapshot_free $snapshot
          191  +  execsql COMMIT
          192  +} {}
          193  +
          194  +#-------------------------------------------------------------------------
          195  +# Check that SQLITE_BUSY_SNAPSHOT is returned if the specified snapshot
          196  +# no longer exists because the wal file has been checkpointed.
          197  +#
          198  +#   1. Reading a snapshot from the middle of a wal file is not possible
          199  +#      after the wal file has been checkpointed.
          200  +#
          201  +#   2. That a snapshot from the end of a wal file can not be read once
          202  +#      the wal file has been wrapped.
          203  +#
          204  +do_execsql_test 4.1.0 {
          205  +  PRAGMA journal_mode = wal;
          206  +  CREATE TABLE t3(i, j);
          207  +  INSERT INTO t3 VALUES('o', 't');
          208  +  INSERT INTO t3 VALUES('t', 'f');
          209  +  BEGIN;
          210  +    SELECT * FROM t3;
          211  +} {wal o t t f}
          212  +
          213  +do_test 4.1.1 {
          214  +  set snapshot [sqlite3_snapshot_get db main]
          215  +  execsql COMMIT
          216  +} {}
          217  +do_test 4.1.2 {
          218  +  execsql { 
          219  +    INSERT INTO t3 VALUES('f', 's'); 
          220  +    BEGIN;
          221  +  }
          222  +  sqlite3_snapshot_open db main $snapshot
          223  +  execsql { SELECT * FROM t3 }
          224  +} {o t t f}
          225  +
          226  +do_test 4.1.3 {
          227  +  execsql { 
          228  +    COMMIT;
          229  +    PRAGMA wal_checkpoint;
          230  +    BEGIN;
          231  +  }
          232  +  list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
          233  +} {1 SQLITE_BUSY_SNAPSHOT}
          234  +do_test 4.1.4 {
          235  +  sqlite3_snapshot_free $snapshot
          236  +  execsql COMMIT
          237  +} {}
          238  +
          239  +do_test 4.2.1 {
          240  +  execsql {
          241  +    INSERT INTO t3 VALUES('s', 'e');
          242  +    INSERT INTO t3 VALUES('n', 't');
          243  +    BEGIN;
          244  +      SELECT * FROM t3;
          245  +  }
          246  +} {o t t f f s s e n t}
          247  +do_test 4.2.2 {
          248  +  set snapshot [sqlite3_snapshot_get db main]
          249  +  execsql {
          250  +    COMMIT;
          251  +    PRAGMA wal_checkpoint;
          252  +    BEGIN;
          253  +  }
          254  +  sqlite3_snapshot_open db main $snapshot
          255  +  execsql { SELECT * FROM t3 }
          256  +} {o t t f f s s e n t}
          257  +do_test 4.2.3 {
          258  +  execsql {
          259  +    COMMIT;
          260  +    INSERT INTO t3 VALUES('e', 't');
          261  +    BEGIN;
          262  +  }
          263  +  list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
          264  +} {1 SQLITE_BUSY_SNAPSHOT}
          265  +do_test 4.2.4 {
          266  +  sqlite3_snapshot_free $snapshot
          267  +} {}
          268  +
          269  +#-------------------------------------------------------------------------
          270  +# Check that SQLITE_BUSY is returned if a checkpoint is running when
          271  +# sqlite3_snapshot_open() is called.
          272  +#
          273  +reset_db
          274  +db close
          275  +testvfs tvfs
          276  +sqlite3 db test.db -vfs tvfs
          277  +
          278  +do_execsql_test 5.1 {
          279  +  PRAGMA journal_mode = wal;
          280  +  CREATE TABLE x1(x, xx, xxx);
          281  +  INSERT INTO x1 VALUES('z', 'zz', 'zzz');
          282  +  BEGIN;
          283  +    SELECT * FROM x1;
          284  +} {wal z zz zzz}
          285  +
          286  +do_test 5.2 {
          287  +  set ::snapshot [sqlite3_snapshot_get db main]
          288  +  sqlite3 db2 test.db -vfs tvfs
          289  +  execsql {
          290  +    INSERT INTO x1 VALUES('a', 'aa', 'aaa');
          291  +    COMMIT;
          292  +  }
          293  +} {}
          294  +
          295  +set t53 0
          296  +proc write_callback {args} {
          297  +  do_test 5.3.[incr ::t53] {
          298  +    execsql BEGIN
          299  +    list [catch { sqlite3_snapshot_open db main $::snapshot } msg] $msg
          300  +  } {1 SQLITE_BUSY}
          301  +  catchsql COMMIT
          302  +}
          303  +
          304  +tvfs filter xWrite
          305  +tvfs script write_callback
          306  +db2 eval { PRAGMA wal_checkpoint }
          307  +db close
          308  +db2 close
          309  +tvfs delete
          310  +sqlite3_snapshot_free $snapshot
          311  +
          312  +#-------------------------------------------------------------------------
          313  +# Test that sqlite3_snapshot_get() may be called immediately after
          314  +# "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may
          315  +# be called after opening the db handle and running the script
          316  +# "PRAGMA user_version; BEGIN".
          317  +reset_db
          318  +do_execsql_test 6.1 {
          319  +  PRAGMA journal_mode = wal;
          320  +  CREATE TABLE x1(x, xx, xxx);
          321  +  INSERT INTO x1 VALUES('z', 'zz', 'zzz');
          322  +  BEGIN;
          323  +    PRAGMA user_version;
          324  +} {wal 0}
          325  +do_test 6.2 {
          326  +  set ::snapshot [sqlite3_snapshot_get db main]
          327  +  execsql {
          328  +    INSERT INTO x1 VALUES('a', 'aa', 'aaa');
          329  +    COMMIT;
          330  +  }
          331  +} {}
          332  +do_test 6.3 {
          333  +  sqlite3 db2 test.db 
          334  +  db2 eval "PRAGMA user_version ; BEGIN"
          335  +  sqlite3_snapshot_open db2 main $::snapshot
          336  +  db2 eval { SELECT * FROM x1 }
          337  +} {z zz zzz}
          338  +sqlite3_snapshot_free $snapshot
          339  +
          340  +finish_test

Added test/snapshot_fault.test.

            1  +# 2015 December 10
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library. The focus
           12  +# of this file is the sqlite3_snapshot_xxx() APIs.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +ifcapable !snapshot {finish_test; return}
           18  +set testprefix snapshot_fault
           19  +
           20  +#-------------------------------------------------------------------------
           21  +# Check that an sqlite3_snapshot_open() client cannot be tricked into
           22  +# reading a corrupt snapshot even if a second client fails while 
           23  +# checkpointing the db.
           24  +#
           25  +do_faultsim_test 1.0 -prep {
           26  +  faultsim_delete_and_reopen
           27  +  sqlite3 db2 test.db
           28  +  db2 eval { 
           29  +    CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
           30  +    INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
           31  +    INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
           32  +    PRAGMA journal_mode = wal;
           33  +    INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
           34  +    BEGIN;
           35  +      SELECT a FROM t1;
           36  +  }
           37  +  set ::snapshot [sqlite3_snapshot_get db2 main] 
           38  +  db2 eval COMMIT
           39  +  db2 eval {
           40  +    UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
           41  +    INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
           42  +    INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
           43  +    INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
           44  +  }
           45  +} -body {
           46  +  db eval { PRAGMA wal_checkpoint }
           47  +} -test {
           48  +  db2 eval BEGIN
           49  +  if {[catch { sqlite3_snapshot_open db2 main $::snapshot } msg]} {
           50  +    if {$msg != "SQLITE_BUSY_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
           51  +      error "error is $msg" 
           52  +    }
           53  +  } else {
           54  +    set res [db2 eval { 
           55  +      SELECT a FROM t1;
           56  +      PRAGMA integrity_check;
           57  +    }]
           58  +    if {$res != "1 2 3 ok"} { error "res is $res" }
           59  +  }
           60  +
           61  +  sqlite3_snapshot_free $::snapshot
           62  +}
           63  +
           64  +#-------------------------------------------------------------------------
           65  +# This test is similar to the previous one. Except, after the 
           66  +# "PRAGMA wal_checkpoint" command fails the db is closed and reopened
           67  +# so as to require wal file recovery. It should not be possible to open
           68  +# a snapshot that is part of the body of a recovered wal file.
           69  +#
           70  +do_faultsim_test 2.0 -prep {
           71  +  faultsim_delete_and_reopen
           72  +  db eval { 
           73  +    CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
           74  +    INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
           75  +    INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
           76  +    PRAGMA journal_mode = wal;
           77  +    INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
           78  +    BEGIN;
           79  +      SELECT a FROM t1;
           80  +  }
           81  +  set ::snapshot [sqlite3_snapshot_get db main] 
           82  +  db eval COMMIT
           83  +
           84  +  db eval {
           85  +    UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
           86  +    INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
           87  +    INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
           88  +    INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
           89  +  }
           90  +} -body {
           91  +  db eval { PRAGMA wal_checkpoint }
           92  +} -test {
           93  +
           94  +  db_save
           95  +  db close
           96  +  db_restore_and_reopen
           97  +  db eval { SELECT * FROM t1 }
           98  +  
           99  +  db eval BEGIN
          100  +  if {[catch { sqlite3_snapshot_open db main $::snapshot } msg]} {
          101  +    if {$msg != "SQLITE_BUSY_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
          102  +      error "error is $msg" 
          103  +    }
          104  +  } else {
          105  +    # This branch should actually never be taken. But it was useful in
          106  +    # determining whether or not this test was actually working (by 
          107  +    # running a modified version of SQLite that allowed snapshots to be
          108  +    # opened following a recovery).
          109  +    error "TEST HAS FAILED"
          110  +
          111  +    set res [db eval { 
          112  +      SELECT a FROM t1;
          113  +      PRAGMA integrity_check;
          114  +    }]
          115  +    if {$res != "1 2 3 ok"} { error "res is $res" }
          116  +  }
          117  +
          118  +  sqlite3_snapshot_free $::snapshot
          119  +}
          120  +
          121  +#-------------------------------------------------------------------------
          122  +# Test the handling of faults that occur within sqlite3_snapshot_open().
          123  +#
          124  +do_faultsim_test 3.0 -prep {
          125  +  faultsim_delete_and_reopen
          126  +  db eval { 
          127  +    CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
          128  +    INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
          129  +    INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
          130  +    PRAGMA journal_mode = wal;
          131  +    INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
          132  +    BEGIN;
          133  +      SELECT a FROM t1;
          134  +  }
          135  +  set ::snapshot [sqlite3_snapshot_get db main] 
          136  +  db eval COMMIT
          137  +  db eval {
          138  +    UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
          139  +    INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
          140  +    INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
          141  +    INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
          142  +    BEGIN;
          143  +  }
          144  +} -body {
          145  +  if { [catch { sqlite3_snapshot_open db main $::snapshot } msg] } {
          146  +    error $msg
          147  +  }
          148  +} -test {
          149  +  faultsim_test_result {0 {}} {1 SQLITE_IOERR} \
          150  +                              {1 SQLITE_IOERR_NOMEM} {1 SQLITE_IOERR_READ}
          151  +  if {$testrc==0} {
          152  +    set res [db eval { 
          153  +      SELECT a FROM t1;
          154  +      PRAGMA integrity_check;
          155  +    }]
          156  +    if {$res != "1 2 3 ok"} { error "res is $res" }
          157  +  }
          158  +
          159  +  sqlite3_snapshot_free $::snapshot
          160  +}
          161  +
          162  +
          163  +
          164  +finish_test

Changes to test/sysfault.test.

   222    222     test_syscall reset
   223    223     test_syscall install {fstat fallocate}
   224    224   }
   225    225   do_faultsim_test 3 -faults vfsfault-* -prep {
   226    226     faultsim_delete_and_reopen
   227    227     file_control_chunksize_test db main 8192
   228    228     execsql {
          229  +    PRAGMA synchronous=OFF;
   229    230       CREATE TABLE t1(a, b);
   230    231       BEGIN;
   231    232         SELECT * FROM t1;
   232    233     }
   233    234   } -body {
   234    235     test_syscall errno fstat     EIO
   235    236     test_syscall errno fallocate EIO

Changes to test/threadtest3.c.

    84     84   #include <assert.h>
    85     85   #include <sys/types.h> 
    86     86   #include <sys/stat.h> 
    87     87   #include <string.h>
    88     88   #include <fcntl.h>
    89     89   #include <errno.h>
    90     90   
           91  +#include "test_multiplex.h"
           92  +
           93  +/* Required to link test_multiplex.c */
           94  +#ifndef SQLITE_OMIT_WSD
           95  +int sqlite3PendingByte = 0x40000000;
           96  +#endif
           97  +
    91     98   /*
    92     99    * This code implements the MD5 message-digest algorithm.
    93    100    * The algorithm is due to Ron Rivest.  This code was
    94    101    * written by Colin Plumb in 1993, no copyright is claimed.
    95    102    * This code is in the public domain; do with it what you wish.
    96    103    *
    97    104    * Equivalent code is available from RSA Data Security, Inc.
................................................................................
  1456   1463     int nTestfound = 0;
  1457   1464   
  1458   1465     sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
  1459   1466     if( argc<2 ){
  1460   1467       argc = 2;
  1461   1468       argv = substArgv;
  1462   1469     }
         1470  +
         1471  +  /* Loop through the command-line arguments to ensure that each argument
         1472  +  ** selects at least one test. If not, assume there is a typo on the 
         1473  +  ** command-line and bail out with the usage message.  */
  1463   1474     for(iArg=1; iArg<argc; iArg++){
         1475  +    const char *zArg = argv[iArg];
         1476  +    if( zArg[0]=='-' ){
         1477  +      if( sqlite3_stricmp(zArg, "-multiplexor")==0 ){
         1478  +        /* Install the multiplexor VFS as the default */
         1479  +        int rc = sqlite3_multiplex_initialize(0, 1);
         1480  +        if( rc!=SQLITE_OK ){
         1481  +          fprintf(stderr, "Failed to install multiplexor VFS (%d)\n", rc);
         1482  +          return 253;
         1483  +        }
         1484  +      }
         1485  +      else {
         1486  +        goto usage;
         1487  +      }
         1488  +
         1489  +      continue;
         1490  +    }
         1491  +
  1464   1492       for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
  1465         -      if( sqlite3_strglob(argv[iArg],aTest[i].zTest)==0 ) break;
         1493  +      if( sqlite3_strglob(zArg, aTest[i].zTest)==0 ) break;
  1466   1494       }
  1467   1495       if( i>=sizeof(aTest)/sizeof(aTest[0]) ) goto usage;   
  1468   1496     }
         1497  +
  1469   1498     for(iArg=1; iArg<argc; iArg++){
         1499  +    if( argv[iArg][0]=='-' ) continue;
  1470   1500       for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
  1471   1501         char const *z = aTest[i].zTest;
  1472   1502         if( sqlite3_strglob(argv[iArg],z)==0 ){
  1473   1503           printf("Running %s for %d seconds...\n", z, aTest[i].nMs/1000);
  1474   1504           fflush(stdout);
  1475   1505           aTest[i].xTest(aTest[i].nMs);
  1476   1506           nTestfound++;
................................................................................
  1479   1509     }
  1480   1510     if( nTestfound==0 ) goto usage;
  1481   1511   
  1482   1512     printf("%d errors out of %d tests\n", nGlobalErr, nTestfound);
  1483   1513     return (nGlobalErr>0 ? 255 : 0);
  1484   1514   
  1485   1515    usage:
  1486         -  printf("Usage: %s [testname|testprefix*]...\n", argv[0]);
         1516  +  printf("Usage: %s [-multiplexor] [testname|testprefix*]...\n", argv[0]);
  1487   1517     printf("Available tests are:\n");
  1488   1518     for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
  1489   1519       printf("   %s\n", aTest[i].zTest);
  1490   1520     }
  1491   1521   
  1492   1522     return 254;
  1493   1523   }

Changes to test/vtabH.test.

   121    121         set res [list]
   122    122         foreach name [glob -directory $::env(SystemDrive)/ -- *] {
   123    123           if {[string index [file tail $name] 0] eq "."} continue
   124    124           lappend res $name
   125    125         }
   126    126         return $res
   127    127       } else {
   128         -      return [exec ls -U /]
          128  +      return [string map {/ {}} [glob /*]]
   129    129       }
   130    130     }
   131    131   
   132    132     proc list_files { pattern } {
   133    133       if {$::tcl_platform(platform) eq "windows"} {
   134    134         set res [list]
   135    135         foreach name [glob -nocomplain $pattern] {
................................................................................
   210    210       SELECT size FROM fstree WHERE path = $pwd || '/subdir/x1.txt'
   211    211     } 143
   212    212   
   213    213   }
   214    214   
   215    215   
   216    216   finish_test
   217         -
   218         -

Changes to test/where7.test.

    43     43       INSERT INTO t1 VALUES(4,5,10,15);
    44     44       INSERT INTO t1 VALUES(5,10,100,1000);
    45     45       CREATE INDEX t1b ON t1(b);
    46     46       CREATE INDEX t1c ON t1(c);
    47     47       SELECT * FROM t1;
    48     48     }
    49     49   } {1 2 3 4 2 3 4 5 3 4 6 8 4 5 10 15 5 10 100 1000}
           50  +do_execsql_test where7-1.1.1 {
           51  +  CREATE TABLE t(a);
           52  +  CREATE INDEX ta ON t(a);
           53  +  INSERT INTO t(a) VALUES(1),(2);
           54  +  SELECT * FROM t ORDER BY a;
           55  +  SELECT * FROM t WHERE a<2 OR a<3 ORDER BY a;
           56  +  PRAGMA count_changes=ON;
           57  +  DELETE FROM t WHERE a<2 OR a<3;
           58  +  SELECT * FROM t;
           59  +  PRAGMA count_changes=OFF;
           60  +  DROP TABLE t;
           61  +} {1 2 1 2 2}
    50     62   do_test where7-1.2 {
    51     63     count_steps {
    52     64       SELECT a FROM t1 WHERE b=3 OR c=6 ORDER BY a
    53     65     }
    54     66   } {2 3 scan 0 sort 1}
    55     67   do_test where7-1.3 {
    56     68     count_steps {

Changes to tool/GetTclKit.bat.

    58     58     ECHO The TEMP environment variable must be set first.
    59     59     GOTO errors
    60     60   )
    61     61   
    62     62   %_VECHO% Temp = '%TEMP%'
    63     63   
    64     64   IF NOT DEFINED TCLKIT_URI (
    65         -  SET TCLKIT_URI=http://tclsh.com/
           65  +  SET TCLKIT_URI=https://tclsh.com/
    66     66   )
    67     67   
    68     68   %_VECHO% TclKitUri = '%TCLKIT_URI%'
    69     69   
    70     70   IF /I "%PROCESSOR%" == "x86" (
    71     71     CALL :fn_TclKitX86Variables
    72     72   ) ELSE IF /I "%PROCESSOR%" == "x64" (

Changes to tool/build-all-msvc.bat.

   316    316   REM
   317    317   REM NOTE: This is the name of the sub-directory where the UCRT libraries may
   318    318   REM       be found.  It is only used when compiling against the UCRT.
   319    319   REM
   320    320   IF DEFINED UCRTVersion (
   321    321     SET NUCRTVER=%UCRTVersion%
   322    322   ) ELSE (
   323         -  SET NUCRTVER=10.0.10240.0
          323  +  SET NUCRTVER=10.0.10586.0
   324    324   )
   325    325   
   326    326   REM
   327    327   REM NOTE: This is the name of the sub-directory where the Windows 10.0 SDK
   328    328   REM       libraries may be found.  It is only used when compiling with the
   329    329   REM       Windows 10.0 SDK.
   330    330   REM