/ Check-in [4aeb3ae4]
Login

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

Overview
Comment:Add extra assert() statements trying to catch a Mem object in an inconsistent state.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | enhanced-mem-check
Files: files | file ages | folders
SHA1: 4aeb3ae435c78070232fef21a147fde4e1c5cd31
User & Date: drh 2014-03-01 14:45:18
Context
2014-03-01
16:24
Factor the Mem invariant checker into a separate procedure (rather than a macro) so that it can be more easily extended. check-in: 354699d5 user: drh tags: enhanced-mem-check
14:45
Add extra assert() statements trying to catch a Mem object in an inconsistent state. check-in: 4aeb3ae4 user: drh tags: enhanced-mem-check
2014-02-28
20:47
In the command-line shell add the (undocumented and unsupported) ".eqp" command and -eqp command-line option, to cause EXPLAIN QUERY PLAN to be run on each SQL statement as it is evaluated. Intended use is for analysis of the query planner. check-in: e6ecf733 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbe.c.

   596    596   
   597    597       /* Sanity checking on other operands */
   598    598   #ifdef SQLITE_DEBUG
   599    599       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   600    600         assert( pOp->p1>0 );
   601    601         assert( pOp->p1<=(p->nMem-p->nCursor) );
   602    602         assert( memIsValid(&aMem[pOp->p1]) );
          603  +      assert( memSanity1(&aMem[pOp->p1]) );
   603    604         REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
   604    605       }
   605    606       if( (pOp->opflags & OPFLG_IN2)!=0 ){
   606    607         assert( pOp->p2>0 );
   607    608         assert( pOp->p2<=(p->nMem-p->nCursor) );
   608    609         assert( memIsValid(&aMem[pOp->p2]) );
          610  +      assert( memSanity1(&aMem[pOp->p2]) );
   609    611         REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
   610    612       }
   611    613       if( (pOp->opflags & OPFLG_IN3)!=0 ){
   612    614         assert( pOp->p3>0 );
   613    615         assert( pOp->p3<=(p->nMem-p->nCursor) );
   614    616         assert( memIsValid(&aMem[pOp->p3]) );
          617  +      assert( memSanity1(&aMem[pOp->p3]) );
   615    618         REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
   616    619       }
   617    620       if( (pOp->opflags & OPFLG_OUT2)!=0 ){
   618    621         assert( pOp->p2>0 );
   619    622         assert( pOp->p2<=(p->nMem-p->nCursor) );
   620    623         memAboutToChange(p, &aMem[pOp->p2]);
   621    624       }
................................................................................
  2456   2459   
  2457   2460     /* Extract the content for the p2+1-th column.  Control can only
  2458   2461     ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
  2459   2462     ** all valid.
  2460   2463     */
  2461   2464     assert( p2<pC->nHdrParsed );
  2462   2465     assert( rc==SQLITE_OK );
         2466  +  assert( memSanity1(pDest) );
  2463   2467     if( pC->szRow>=aOffset[p2+1] ){
  2464   2468       /* This is the common case where the desired content fits on the original
  2465   2469       ** page - where the content is not on an overflow page */
  2466   2470       VdbeMemRelease(pDest);
  2467   2471       sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
  2468   2472     }else{
  2469   2473       /* This branch happens only when content is on overflow pages */

Changes to src/vdbeInt.h.

   228    228   ** Return true if a memory cell is not marked as invalid.  This macro
   229    229   ** is for use inside assert() statements only.
   230    230   */
   231    231   #ifdef SQLITE_DEBUG
   232    232   #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
   233    233   #endif
   234    234   
          235  +/*
          236  +** A sanity check on a Mem object and especially the Mem.z field.
          237  +** This check says that no more than one of the following may be true:
          238  +**   (1) Mem.z comes from Mem.zMalloc
          239  +**   (2) Mem.z has a destructor Mem.xDel
          240  +**   (3) Mem.z is an ephemeral string
          241  +**   (4) Mem.z is a static string
          242  +**
          243  +** Use only inside of an assert() as follows:  assert( memSanify(pMem) );
          244  +*/
          245  +#ifdef SQLITE_DEBUG
          246  +#define memSanity1(p)                                  \
          247  +   ((((p)->zMalloc && (p)->zMalloc==(p)->z) ? 1 : 0) + \
          248  +    ((((p)->flags&MEM_Dyn)&&(p)->xDel) ? 1 : 0) +      \
          249  +    (((p)->flags&MEM_Ephem) ? 1 : 0) +                 \
          250  +    (((p)->flags&MEM_Static) ? 1 : 0) <= 1 )
          251  +#endif
          252  +
   235    253   /*
   236    254   ** Each auxilliary data pointer stored by a user defined function 
   237    255   ** implementation calling sqlite3_set_auxdata() is stored in an instance
   238    256   ** of this structure. All such structures associated with a single VM
   239    257   ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
   240    258   ** when the VM is halted (if not before).
   241    259   */

Changes to src/vdbeaux.c.

  1216   1216         for(pEnd=&p[N]; p<pEnd; p++){
  1217   1217           sqlite3DbFree(db, p->zMalloc);
  1218   1218         }
  1219   1219         return;
  1220   1220       }
  1221   1221       for(pEnd=&p[N]; p<pEnd; p++){
  1222   1222         assert( (&p[1])==pEnd || p[0].db==p[1].db );
         1223  +      assert( memSanity1(p) );
  1223   1224   
  1224   1225         /* This block is really an inlined version of sqlite3VdbeMemRelease()
  1225   1226         ** that takes advantage of the fact that the memory cell value is 
  1226   1227         ** being set to NULL after releasing any dynamic resources.
  1227   1228         **
  1228   1229         ** The justification for duplicating code is that according to 
  1229   1230         ** callgrind, this causes a certain test case to hit the CPU 4.7 

Changes to src/vdbemem.c.

    63     63   **
    64     64   ** If the bPreserve argument is true, then copy of the content of
    65     65   ** pMem->z into the new allocation.  pMem must be either a string or
    66     66   ** blob if bPreserve is true.  If bPreserve is false, any prior content
    67     67   ** in pMem->z is discarded.
    68     68   */
    69     69   int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
    70         -  assert( 1 >=
    71         -    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
    72         -    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
    73         -    ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
    74         -    ((pMem->flags&MEM_Static) ? 1 : 0)
    75         -  );
           70  +  assert( memSanity1(pMem) );
    76     71     assert( (pMem->flags&MEM_RowSet)==0 );
    77     72   
    78     73     /* If the bPreserve flag is set to true, then the memory cell must already
    79     74     ** contain a valid string or blob value.  */
    80     75     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
    81     76     testcase( bPreserve && pMem->z==0 );
    82     77   
................................................................................
   288    283   
   289    284   /*
   290    285   ** Release any memory held by the Mem. This may leave the Mem in an
   291    286   ** inconsistent state, for example with (Mem.z==0) and
   292    287   ** (Mem.memType==MEM_Str).
   293    288   */
   294    289   void sqlite3VdbeMemRelease(Mem *p){
          290  +  assert( memSanity1(p) );
   295    291     VdbeMemRelease(p);
   296    292     if( p->zMalloc ){
   297    293       sqlite3DbFree(p->db, p->zMalloc);
   298    294       p->zMalloc = 0;
   299    295     }
   300    296     p->z = 0;
   301    297     assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */