xref: /sqlite-3.40.0/src/vdbeaux.c (revision 735ff4a8)
1 /*
2 ** 2003 September 6
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains code used for creating, destroying, and populating
13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
14 */
15 #include "sqliteInt.h"
16 #include "vdbeInt.h"
17 
18 /*
19 ** Create a new virtual database engine.
20 */
21 Vdbe *sqlite3VdbeCreate(Parse *pParse){
22   sqlite3 *db = pParse->db;
23   Vdbe *p;
24   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
25   if( p==0 ) return 0;
26   p->db = db;
27   if( db->pVdbe ){
28     db->pVdbe->pPrev = p;
29   }
30   p->pNext = db->pVdbe;
31   p->pPrev = 0;
32   db->pVdbe = p;
33   p->magic = VDBE_MAGIC_INIT;
34   p->pParse = pParse;
35   assert( pParse->aLabel==0 );
36   assert( pParse->nLabel==0 );
37   assert( pParse->nOpAlloc==0 );
38   assert( pParse->szOpAlloc==0 );
39   return p;
40 }
41 
42 /*
43 ** Change the error string stored in Vdbe.zErrMsg
44 */
45 void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
46   va_list ap;
47   sqlite3DbFree(p->db, p->zErrMsg);
48   va_start(ap, zFormat);
49   p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
50   va_end(ap);
51 }
52 
53 /*
54 ** Remember the SQL string for a prepared statement.
55 */
56 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
57   assert( isPrepareV2==1 || isPrepareV2==0 );
58   if( p==0 ) return;
59 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
60   if( !isPrepareV2 ) return;
61 #endif
62   assert( p->zSql==0 );
63   p->zSql = sqlite3DbStrNDup(p->db, z, n);
64   p->isPrepareV2 = (u8)isPrepareV2;
65 }
66 
67 /*
68 ** Return the SQL associated with a prepared statement
69 */
70 const char *sqlite3_sql(sqlite3_stmt *pStmt){
71   Vdbe *p = (Vdbe *)pStmt;
72   return p ? p->zSql : 0;
73 }
74 
75 /*
76 ** Swap all content between two VDBE structures.
77 */
78 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
79   Vdbe tmp, *pTmp;
80   char *zTmp;
81   tmp = *pA;
82   *pA = *pB;
83   *pB = tmp;
84   pTmp = pA->pNext;
85   pA->pNext = pB->pNext;
86   pB->pNext = pTmp;
87   pTmp = pA->pPrev;
88   pA->pPrev = pB->pPrev;
89   pB->pPrev = pTmp;
90   zTmp = pA->zSql;
91   pA->zSql = pB->zSql;
92   pB->zSql = zTmp;
93   pB->isPrepareV2 = pA->isPrepareV2;
94 }
95 
96 /*
97 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
98 ** than its current size. nOp is guaranteed to be less than or equal
99 ** to 1024/sizeof(Op).
100 **
101 ** If an out-of-memory error occurs while resizing the array, return
102 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
103 ** unchanged (this is so that any opcodes already allocated can be
104 ** correctly deallocated along with the rest of the Vdbe).
105 */
106 static int growOpArray(Vdbe *v, int nOp){
107   VdbeOp *pNew;
108   Parse *p = v->pParse;
109 
110   /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
111   ** more frequent reallocs and hence provide more opportunities for
112   ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
113   ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
114   ** by the minimum* amount required until the size reaches 512.  Normal
115   ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
116   ** size of the op array or add 1KB of space, whichever is smaller. */
117 #ifdef SQLITE_TEST_REALLOC_STRESS
118   int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
119 #else
120   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
121   UNUSED_PARAMETER(nOp);
122 #endif
123 
124   assert( nOp<=(1024/sizeof(Op)) );
125   assert( nNew>=(p->nOpAlloc+nOp) );
126   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
127   if( pNew ){
128     p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
129     p->nOpAlloc = p->szOpAlloc/sizeof(Op);
130     v->aOp = pNew;
131   }
132   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
133 }
134 
135 #ifdef SQLITE_DEBUG
136 /* This routine is just a convenient place to set a breakpoint that will
137 ** fire after each opcode is inserted and displayed using
138 ** "PRAGMA vdbe_addoptrace=on".
139 */
140 static void test_addop_breakpoint(void){
141   static int n = 0;
142   n++;
143 }
144 #endif
145 
146 /*
147 ** Add a new instruction to the list of instructions current in the
148 ** VDBE.  Return the address of the new instruction.
149 **
150 ** Parameters:
151 **
152 **    p               Pointer to the VDBE
153 **
154 **    op              The opcode for this instruction
155 **
156 **    p1, p2, p3      Operands
157 **
158 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
159 ** the sqlite3VdbeChangeP4() function to change the value of the P4
160 ** operand.
161 */
162 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
163   assert( p->pParse->nOpAlloc<=p->nOp );
164   if( growOpArray(p, 1) ) return 1;
165   assert( p->pParse->nOpAlloc>p->nOp );
166   return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
167 }
168 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
169   int i;
170   VdbeOp *pOp;
171 
172   i = p->nOp;
173   assert( p->magic==VDBE_MAGIC_INIT );
174   assert( op>=0 && op<0xff );
175   if( p->pParse->nOpAlloc<=i ){
176     return growOp3(p, op, p1, p2, p3);
177   }
178   p->nOp++;
179   pOp = &p->aOp[i];
180   pOp->opcode = (u8)op;
181   pOp->p5 = 0;
182   pOp->p1 = p1;
183   pOp->p2 = p2;
184   pOp->p3 = p3;
185   pOp->p4.p = 0;
186   pOp->p4type = P4_NOTUSED;
187 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
188   pOp->zComment = 0;
189 #endif
190 #ifdef SQLITE_DEBUG
191   if( p->db->flags & SQLITE_VdbeAddopTrace ){
192     int jj, kk;
193     Parse *pParse = p->pParse;
194     for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
195       struct yColCache *x = pParse->aColCache + jj;
196       if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
197       printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
198       kk++;
199     }
200     if( kk ) printf("\n");
201     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
202     test_addop_breakpoint();
203   }
204 #endif
205 #ifdef VDBE_PROFILE
206   pOp->cycles = 0;
207   pOp->cnt = 0;
208 #endif
209 #ifdef SQLITE_VDBE_COVERAGE
210   pOp->iSrcLine = 0;
211 #endif
212   return i;
213 }
214 int sqlite3VdbeAddOp0(Vdbe *p, int op){
215   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
216 }
217 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
218   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
219 }
220 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
221   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
222 }
223 
224 /* Generate code for an unconditional jump to instruction iDest
225 */
226 int sqlite3VdbeGoto(Vdbe *p, int iDest){
227   return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
228 }
229 
230 /* Generate code to cause the string zStr to be loaded into
231 ** register iDest
232 */
233 int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
234   return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
235 }
236 
237 /*
238 ** Generate code that initializes multiple registers to string or integer
239 ** constants.  The registers begin with iDest and increase consecutively.
240 ** One register is initialized for each characgter in zTypes[].  For each
241 ** "s" character in zTypes[], the register is a string if the argument is
242 ** not NULL, or OP_Null if the value is a null pointer.  For each "i" character
243 ** in zTypes[], the register is initialized to an integer.
244 */
245 void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
246   va_list ap;
247   int i;
248   char c;
249   va_start(ap, zTypes);
250   for(i=0; (c = zTypes[i])!=0; i++){
251     if( c=='s' ){
252       const char *z = va_arg(ap, const char*);
253       sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0);
254     }else{
255       assert( c=='i' );
256       sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++);
257     }
258   }
259   va_end(ap);
260 }
261 
262 /*
263 ** Add an opcode that includes the p4 value as a pointer.
264 */
265 int sqlite3VdbeAddOp4(
266   Vdbe *p,            /* Add the opcode to this VM */
267   int op,             /* The new opcode */
268   int p1,             /* The P1 operand */
269   int p2,             /* The P2 operand */
270   int p3,             /* The P3 operand */
271   const char *zP4,    /* The P4 operand */
272   int p4type          /* P4 operand type */
273 ){
274   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
275   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
276   return addr;
277 }
278 
279 /*
280 ** Add an opcode that includes the p4 value with a P4_INT64 or
281 ** P4_REAL type.
282 */
283 int sqlite3VdbeAddOp4Dup8(
284   Vdbe *p,            /* Add the opcode to this VM */
285   int op,             /* The new opcode */
286   int p1,             /* The P1 operand */
287   int p2,             /* The P2 operand */
288   int p3,             /* The P3 operand */
289   const u8 *zP4,      /* The P4 operand */
290   int p4type          /* P4 operand type */
291 ){
292   char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
293   if( p4copy ) memcpy(p4copy, zP4, 8);
294   return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
295 }
296 
297 /*
298 ** Add an OP_ParseSchema opcode.  This routine is broken out from
299 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
300 ** as having been used.
301 **
302 ** The zWhere string must have been obtained from sqlite3_malloc().
303 ** This routine will take ownership of the allocated memory.
304 */
305 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
306   int j;
307   sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
308   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
309 }
310 
311 /*
312 ** Add an opcode that includes the p4 value as an integer.
313 */
314 int sqlite3VdbeAddOp4Int(
315   Vdbe *p,            /* Add the opcode to this VM */
316   int op,             /* The new opcode */
317   int p1,             /* The P1 operand */
318   int p2,             /* The P2 operand */
319   int p3,             /* The P3 operand */
320   int p4              /* The P4 operand as an integer */
321 ){
322   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
323   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
324   return addr;
325 }
326 
327 /*
328 ** Create a new symbolic label for an instruction that has yet to be
329 ** coded.  The symbolic label is really just a negative number.  The
330 ** label can be used as the P2 value of an operation.  Later, when
331 ** the label is resolved to a specific address, the VDBE will scan
332 ** through its operation list and change all values of P2 which match
333 ** the label into the resolved address.
334 **
335 ** The VDBE knows that a P2 value is a label because labels are
336 ** always negative and P2 values are suppose to be non-negative.
337 ** Hence, a negative P2 value is a label that has yet to be resolved.
338 **
339 ** Zero is returned if a malloc() fails.
340 */
341 int sqlite3VdbeMakeLabel(Vdbe *v){
342   Parse *p = v->pParse;
343   int i = p->nLabel++;
344   assert( v->magic==VDBE_MAGIC_INIT );
345   if( (i & (i-1))==0 ){
346     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
347                                        (i*2+1)*sizeof(p->aLabel[0]));
348   }
349   if( p->aLabel ){
350     p->aLabel[i] = -1;
351   }
352   return ADDR(i);
353 }
354 
355 /*
356 ** Resolve label "x" to be the address of the next instruction to
357 ** be inserted.  The parameter "x" must have been obtained from
358 ** a prior call to sqlite3VdbeMakeLabel().
359 */
360 void sqlite3VdbeResolveLabel(Vdbe *v, int x){
361   Parse *p = v->pParse;
362   int j = ADDR(x);
363   assert( v->magic==VDBE_MAGIC_INIT );
364   assert( j<p->nLabel );
365   assert( j>=0 );
366   if( p->aLabel ){
367     p->aLabel[j] = v->nOp;
368   }
369   p->iFixedOp = v->nOp - 1;
370 }
371 
372 /*
373 ** Mark the VDBE as one that can only be run one time.
374 */
375 void sqlite3VdbeRunOnlyOnce(Vdbe *p){
376   p->runOnlyOnce = 1;
377 }
378 
379 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
380 
381 /*
382 ** The following type and function are used to iterate through all opcodes
383 ** in a Vdbe main program and each of the sub-programs (triggers) it may
384 ** invoke directly or indirectly. It should be used as follows:
385 **
386 **   Op *pOp;
387 **   VdbeOpIter sIter;
388 **
389 **   memset(&sIter, 0, sizeof(sIter));
390 **   sIter.v = v;                            // v is of type Vdbe*
391 **   while( (pOp = opIterNext(&sIter)) ){
392 **     // Do something with pOp
393 **   }
394 **   sqlite3DbFree(v->db, sIter.apSub);
395 **
396 */
397 typedef struct VdbeOpIter VdbeOpIter;
398 struct VdbeOpIter {
399   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
400   SubProgram **apSub;        /* Array of subprograms */
401   int nSub;                  /* Number of entries in apSub */
402   int iAddr;                 /* Address of next instruction to return */
403   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
404 };
405 static Op *opIterNext(VdbeOpIter *p){
406   Vdbe *v = p->v;
407   Op *pRet = 0;
408   Op *aOp;
409   int nOp;
410 
411   if( p->iSub<=p->nSub ){
412 
413     if( p->iSub==0 ){
414       aOp = v->aOp;
415       nOp = v->nOp;
416     }else{
417       aOp = p->apSub[p->iSub-1]->aOp;
418       nOp = p->apSub[p->iSub-1]->nOp;
419     }
420     assert( p->iAddr<nOp );
421 
422     pRet = &aOp[p->iAddr];
423     p->iAddr++;
424     if( p->iAddr==nOp ){
425       p->iSub++;
426       p->iAddr = 0;
427     }
428 
429     if( pRet->p4type==P4_SUBPROGRAM ){
430       int nByte = (p->nSub+1)*sizeof(SubProgram*);
431       int j;
432       for(j=0; j<p->nSub; j++){
433         if( p->apSub[j]==pRet->p4.pProgram ) break;
434       }
435       if( j==p->nSub ){
436         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
437         if( !p->apSub ){
438           pRet = 0;
439         }else{
440           p->apSub[p->nSub++] = pRet->p4.pProgram;
441         }
442       }
443     }
444   }
445 
446   return pRet;
447 }
448 
449 /*
450 ** Check if the program stored in the VM associated with pParse may
451 ** throw an ABORT exception (causing the statement, but not entire transaction
452 ** to be rolled back). This condition is true if the main program or any
453 ** sub-programs contains any of the following:
454 **
455 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
456 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
457 **   *  OP_Destroy
458 **   *  OP_VUpdate
459 **   *  OP_VRename
460 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
461 **   *  OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
462 **
463 ** Then check that the value of Parse.mayAbort is true if an
464 ** ABORT may be thrown, or false otherwise. Return true if it does
465 ** match, or false otherwise. This function is intended to be used as
466 ** part of an assert statement in the compiler. Similar to:
467 **
468 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
469 */
470 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
471   int hasAbort = 0;
472   int hasFkCounter = 0;
473   int hasCreateTable = 0;
474   int hasInitCoroutine = 0;
475   Op *pOp;
476   VdbeOpIter sIter;
477   memset(&sIter, 0, sizeof(sIter));
478   sIter.v = v;
479 
480   while( (pOp = opIterNext(&sIter))!=0 ){
481     int opcode = pOp->opcode;
482     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
483      || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
484       && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
485     ){
486       hasAbort = 1;
487       break;
488     }
489     if( opcode==OP_CreateTable ) hasCreateTable = 1;
490     if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
491 #ifndef SQLITE_OMIT_FOREIGN_KEY
492     if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
493       hasFkCounter = 1;
494     }
495 #endif
496   }
497   sqlite3DbFree(v->db, sIter.apSub);
498 
499   /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
500   ** If malloc failed, then the while() loop above may not have iterated
501   ** through all opcodes and hasAbort may be set incorrectly. Return
502   ** true for this case to prevent the assert() in the callers frame
503   ** from failing.  */
504   return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
505               || (hasCreateTable && hasInitCoroutine) );
506 }
507 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
508 
509 /*
510 ** This routine is called after all opcodes have been inserted.  It loops
511 ** through all the opcodes and fixes up some details.
512 **
513 ** (1) For each jump instruction with a negative P2 value (a label)
514 **     resolve the P2 value to an actual address.
515 **
516 ** (2) Compute the maximum number of arguments used by any SQL function
517 **     and store that value in *pMaxFuncArgs.
518 **
519 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
520 **     indicate what the prepared statement actually does.
521 **
522 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
523 **
524 ** (5) Reclaim the memory allocated for storing labels.
525 */
526 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
527   int i;
528   int nMaxArgs = *pMaxFuncArgs;
529   Op *pOp;
530   Parse *pParse = p->pParse;
531   int *aLabel = pParse->aLabel;
532   p->readOnly = 1;
533   p->bIsReader = 0;
534   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
535     u8 opcode = pOp->opcode;
536 
537     /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
538     ** cases from this switch! */
539     switch( opcode ){
540       case OP_Transaction: {
541         if( pOp->p2!=0 ) p->readOnly = 0;
542         /* fall thru */
543       }
544       case OP_AutoCommit:
545       case OP_Savepoint: {
546         p->bIsReader = 1;
547         break;
548       }
549 #ifndef SQLITE_OMIT_WAL
550       case OP_Checkpoint:
551 #endif
552       case OP_Vacuum:
553       case OP_JournalMode: {
554         p->readOnly = 0;
555         p->bIsReader = 1;
556         break;
557       }
558 #ifndef SQLITE_OMIT_VIRTUALTABLE
559       case OP_VUpdate: {
560         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
561         break;
562       }
563       case OP_VFilter: {
564         int n;
565         assert( p->nOp - i >= 3 );
566         assert( pOp[-1].opcode==OP_Integer );
567         n = pOp[-1].p1;
568         if( n>nMaxArgs ) nMaxArgs = n;
569         break;
570       }
571 #endif
572       case OP_Next:
573       case OP_NextIfOpen:
574       case OP_SorterNext: {
575         pOp->p4.xAdvance = sqlite3BtreeNext;
576         pOp->p4type = P4_ADVANCE;
577         break;
578       }
579       case OP_Prev:
580       case OP_PrevIfOpen: {
581         pOp->p4.xAdvance = sqlite3BtreePrevious;
582         pOp->p4type = P4_ADVANCE;
583         break;
584       }
585     }
586 
587     pOp->opflags = sqlite3OpcodeProperty[opcode];
588     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
589       assert( ADDR(pOp->p2)<pParse->nLabel );
590       pOp->p2 = aLabel[ADDR(pOp->p2)];
591     }
592   }
593   sqlite3DbFree(p->db, pParse->aLabel);
594   pParse->aLabel = 0;
595   pParse->nLabel = 0;
596   *pMaxFuncArgs = nMaxArgs;
597   assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
598 }
599 
600 /*
601 ** Return the address of the next instruction to be inserted.
602 */
603 int sqlite3VdbeCurrentAddr(Vdbe *p){
604   assert( p->magic==VDBE_MAGIC_INIT );
605   return p->nOp;
606 }
607 
608 /*
609 ** Verify that at least N opcode slots are available in p without
610 ** having to malloc for more space (except when compiled using
611 ** SQLITE_TEST_REALLOC_STRESS).  This interface is used during testing
612 ** to verify that certain calls to sqlite3VdbeAddOpList() can never
613 ** fail due to a OOM fault and hence that the return value from
614 ** sqlite3VdbeAddOpList() will always be non-NULL.
615 */
616 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
617 void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
618   assert( p->nOp + N <= p->pParse->nOpAlloc );
619 }
620 #endif
621 
622 /*
623 ** This function returns a pointer to the array of opcodes associated with
624 ** the Vdbe passed as the first argument. It is the callers responsibility
625 ** to arrange for the returned array to be eventually freed using the
626 ** vdbeFreeOpArray() function.
627 **
628 ** Before returning, *pnOp is set to the number of entries in the returned
629 ** array. Also, *pnMaxArg is set to the larger of its current value and
630 ** the number of entries in the Vdbe.apArg[] array required to execute the
631 ** returned program.
632 */
633 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
634   VdbeOp *aOp = p->aOp;
635   assert( aOp && !p->db->mallocFailed );
636 
637   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
638   assert( DbMaskAllZero(p->btreeMask) );
639 
640   resolveP2Values(p, pnMaxArg);
641   *pnOp = p->nOp;
642   p->aOp = 0;
643   return aOp;
644 }
645 
646 /*
647 ** Add a whole list of operations to the operation stack.  Return a
648 ** pointer to the first operation inserted.
649 */
650 VdbeOp *sqlite3VdbeAddOpList(
651   Vdbe *p,                     /* Add opcodes to the prepared statement */
652   int nOp,                     /* Number of opcodes to add */
653   VdbeOpList const *aOp,       /* The opcodes to be added */
654   int iLineno                  /* Source-file line number of first opcode */
655 ){
656   int i;
657   VdbeOp *pOut, *pFirst;
658   assert( nOp>0 );
659   assert( p->magic==VDBE_MAGIC_INIT );
660   if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
661     return 0;
662   }
663   pFirst = pOut = &p->aOp[p->nOp];
664   for(i=0; i<nOp; i++, aOp++, pOut++){
665     pOut->opcode = aOp->opcode;
666     pOut->p1 = aOp->p1;
667     pOut->p2 = aOp->p2;
668     assert( aOp->p2>=0 );
669     pOut->p3 = aOp->p3;
670     pOut->p4type = P4_NOTUSED;
671     pOut->p4.p = 0;
672     pOut->p5 = 0;
673 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
674     pOut->zComment = 0;
675 #endif
676 #ifdef SQLITE_VDBE_COVERAGE
677     pOut->iSrcLine = iLineno+i;
678 #else
679     (void)iLineno;
680 #endif
681 #ifdef SQLITE_DEBUG
682     if( p->db->flags & SQLITE_VdbeAddopTrace ){
683       sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
684     }
685 #endif
686   }
687   p->nOp += nOp;
688   return pFirst;
689 }
690 
691 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
692 /*
693 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
694 */
695 void sqlite3VdbeScanStatus(
696   Vdbe *p,                        /* VM to add scanstatus() to */
697   int addrExplain,                /* Address of OP_Explain (or 0) */
698   int addrLoop,                   /* Address of loop counter */
699   int addrVisit,                  /* Address of rows visited counter */
700   LogEst nEst,                    /* Estimated number of output rows */
701   const char *zName               /* Name of table or index being scanned */
702 ){
703   int nByte = (p->nScan+1) * sizeof(ScanStatus);
704   ScanStatus *aNew;
705   aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
706   if( aNew ){
707     ScanStatus *pNew = &aNew[p->nScan++];
708     pNew->addrExplain = addrExplain;
709     pNew->addrLoop = addrLoop;
710     pNew->addrVisit = addrVisit;
711     pNew->nEst = nEst;
712     pNew->zName = sqlite3DbStrDup(p->db, zName);
713     p->aScan = aNew;
714   }
715 }
716 #endif
717 
718 
719 /*
720 ** Change the value of the opcode, or P1, P2, P3, or P5 operands
721 ** for a specific instruction.
722 */
723 void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
724   sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
725 }
726 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
727   sqlite3VdbeGetOp(p,addr)->p1 = val;
728 }
729 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
730   sqlite3VdbeGetOp(p,addr)->p2 = val;
731 }
732 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
733   sqlite3VdbeGetOp(p,addr)->p3 = val;
734 }
735 void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
736   if( !p->db->mallocFailed ) p->aOp[p->nOp-1].p5 = p5;
737 }
738 
739 /*
740 ** Change the P2 operand of instruction addr so that it points to
741 ** the address of the next instruction to be coded.
742 */
743 void sqlite3VdbeJumpHere(Vdbe *p, int addr){
744   p->pParse->iFixedOp = p->nOp - 1;
745   sqlite3VdbeChangeP2(p, addr, p->nOp);
746 }
747 
748 
749 /*
750 ** If the input FuncDef structure is ephemeral, then free it.  If
751 ** the FuncDef is not ephermal, then do nothing.
752 */
753 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
754   if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
755     sqlite3DbFree(db, pDef);
756   }
757 }
758 
759 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
760 
761 /*
762 ** Delete a P4 value if necessary.
763 */
764 static void freeP4(sqlite3 *db, int p4type, void *p4){
765   if( p4 ){
766     assert( db );
767     switch( p4type ){
768       case P4_FUNCCTX: {
769         freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
770         /* Fall through into the next case */
771       }
772       case P4_REAL:
773       case P4_INT64:
774       case P4_DYNAMIC:
775       case P4_INTARRAY: {
776         sqlite3DbFree(db, p4);
777         break;
778       }
779       case P4_KEYINFO: {
780         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
781         break;
782       }
783 #ifdef SQLITE_ENABLE_CURSOR_HINTS
784       case P4_EXPR: {
785         sqlite3ExprDelete(db, (Expr*)p4);
786         break;
787       }
788 #endif
789       case P4_MPRINTF: {
790         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
791         break;
792       }
793       case P4_FUNCDEF: {
794         freeEphemeralFunction(db, (FuncDef*)p4);
795         break;
796       }
797       case P4_MEM: {
798         if( db->pnBytesFreed==0 ){
799           sqlite3ValueFree((sqlite3_value*)p4);
800         }else{
801           Mem *p = (Mem*)p4;
802           if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
803           sqlite3DbFree(db, p);
804         }
805         break;
806       }
807       case P4_VTAB : {
808         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
809         break;
810       }
811     }
812   }
813 }
814 
815 /*
816 ** Free the space allocated for aOp and any p4 values allocated for the
817 ** opcodes contained within. If aOp is not NULL it is assumed to contain
818 ** nOp entries.
819 */
820 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
821   if( aOp ){
822     Op *pOp;
823     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
824       if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p);
825 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
826       sqlite3DbFree(db, pOp->zComment);
827 #endif
828     }
829   }
830   sqlite3DbFree(db, aOp);
831 }
832 
833 /*
834 ** Link the SubProgram object passed as the second argument into the linked
835 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
836 ** objects when the VM is no longer required.
837 */
838 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
839   p->pNext = pVdbe->pProgram;
840   pVdbe->pProgram = p;
841 }
842 
843 /*
844 ** Change the opcode at addr into OP_Noop
845 */
846 int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
847   VdbeOp *pOp;
848   if( p->db->mallocFailed ) return 0;
849   assert( addr>=0 && addr<p->nOp );
850   pOp = &p->aOp[addr];
851   freeP4(p->db, pOp->p4type, pOp->p4.p);
852   pOp->p4type = P4_NOTUSED;
853   pOp->p4.z = 0;
854   pOp->opcode = OP_Noop;
855   return 1;
856 }
857 
858 /*
859 ** If the last opcode is "op" and it is not a jump destination,
860 ** then remove it.  Return true if and only if an opcode was removed.
861 */
862 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
863   if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
864     return sqlite3VdbeChangeToNoop(p, p->nOp-1);
865   }else{
866     return 0;
867   }
868 }
869 
870 /*
871 ** Change the value of the P4 operand for a specific instruction.
872 ** This routine is useful when a large program is loaded from a
873 ** static array using sqlite3VdbeAddOpList but we want to make a
874 ** few minor changes to the program.
875 **
876 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
877 ** the string is made into memory obtained from sqlite3_malloc().
878 ** A value of n==0 means copy bytes of zP4 up to and including the
879 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
880 **
881 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
882 ** to a string or structure that is guaranteed to exist for the lifetime of
883 ** the Vdbe. In these cases we can just copy the pointer.
884 **
885 ** If addr<0 then change P4 on the most recently inserted instruction.
886 */
887 static void SQLITE_NOINLINE vdbeChangeP4Full(
888   Vdbe *p,
889   Op *pOp,
890   const char *zP4,
891   int n
892 ){
893   if( pOp->p4type ){
894     freeP4(p->db, pOp->p4type, pOp->p4.p);
895     pOp->p4type = 0;
896     pOp->p4.p = 0;
897   }
898   if( n<0 ){
899     sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
900   }else{
901     if( n==0 ) n = sqlite3Strlen30(zP4);
902     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
903     pOp->p4type = P4_DYNAMIC;
904   }
905 }
906 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
907   Op *pOp;
908   sqlite3 *db;
909   assert( p!=0 );
910   db = p->db;
911   assert( p->magic==VDBE_MAGIC_INIT );
912   assert( p->aOp!=0 || db->mallocFailed );
913   if( db->mallocFailed ){
914     if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
915     return;
916   }
917   assert( p->nOp>0 );
918   assert( addr<p->nOp );
919   if( addr<0 ){
920     addr = p->nOp - 1;
921   }
922   pOp = &p->aOp[addr];
923   if( n>=0 || pOp->p4type ){
924     vdbeChangeP4Full(p, pOp, zP4, n);
925     return;
926   }
927   if( n==P4_INT32 ){
928     /* Note: this cast is safe, because the origin data point was an int
929     ** that was cast to a (const char *). */
930     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
931     pOp->p4type = P4_INT32;
932   }else if( zP4!=0 ){
933     assert( n<0 );
934     pOp->p4.p = (void*)zP4;
935     pOp->p4type = (signed char)n;
936     if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
937   }
938 }
939 
940 /*
941 ** Set the P4 on the most recently added opcode to the KeyInfo for the
942 ** index given.
943 */
944 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
945   Vdbe *v = pParse->pVdbe;
946   assert( v!=0 );
947   assert( pIdx!=0 );
948   sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
949                       P4_KEYINFO);
950 }
951 
952 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
953 /*
954 ** Change the comment on the most recently coded instruction.  Or
955 ** insert a No-op and add the comment to that new instruction.  This
956 ** makes the code easier to read during debugging.  None of this happens
957 ** in a production build.
958 */
959 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
960   assert( p->nOp>0 || p->aOp==0 );
961   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
962   if( p->nOp ){
963     assert( p->aOp );
964     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
965     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
966   }
967 }
968 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
969   va_list ap;
970   if( p ){
971     va_start(ap, zFormat);
972     vdbeVComment(p, zFormat, ap);
973     va_end(ap);
974   }
975 }
976 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
977   va_list ap;
978   if( p ){
979     sqlite3VdbeAddOp0(p, OP_Noop);
980     va_start(ap, zFormat);
981     vdbeVComment(p, zFormat, ap);
982     va_end(ap);
983   }
984 }
985 #endif  /* NDEBUG */
986 
987 #ifdef SQLITE_VDBE_COVERAGE
988 /*
989 ** Set the value if the iSrcLine field for the previously coded instruction.
990 */
991 void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
992   sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
993 }
994 #endif /* SQLITE_VDBE_COVERAGE */
995 
996 /*
997 ** Return the opcode for a given address.  If the address is -1, then
998 ** return the most recently inserted opcode.
999 **
1000 ** If a memory allocation error has occurred prior to the calling of this
1001 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
1002 ** is readable but not writable, though it is cast to a writable value.
1003 ** The return of a dummy opcode allows the call to continue functioning
1004 ** after an OOM fault without having to check to see if the return from
1005 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
1006 ** dummy will never be written to.  This is verified by code inspection and
1007 ** by running with Valgrind.
1008 */
1009 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
1010   /* C89 specifies that the constant "dummy" will be initialized to all
1011   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
1012   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
1013   assert( p->magic==VDBE_MAGIC_INIT );
1014   if( addr<0 ){
1015     addr = p->nOp - 1;
1016   }
1017   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
1018   if( p->db->mallocFailed ){
1019     return (VdbeOp*)&dummy;
1020   }else{
1021     return &p->aOp[addr];
1022   }
1023 }
1024 
1025 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
1026 /*
1027 ** Return an integer value for one of the parameters to the opcode pOp
1028 ** determined by character c.
1029 */
1030 static int translateP(char c, const Op *pOp){
1031   if( c=='1' ) return pOp->p1;
1032   if( c=='2' ) return pOp->p2;
1033   if( c=='3' ) return pOp->p3;
1034   if( c=='4' ) return pOp->p4.i;
1035   return pOp->p5;
1036 }
1037 
1038 /*
1039 ** Compute a string for the "comment" field of a VDBE opcode listing.
1040 **
1041 ** The Synopsis: field in comments in the vdbe.c source file gets converted
1042 ** to an extra string that is appended to the sqlite3OpcodeName().  In the
1043 ** absence of other comments, this synopsis becomes the comment on the opcode.
1044 ** Some translation occurs:
1045 **
1046 **       "PX"      ->  "r[X]"
1047 **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
1048 **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
1049 **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
1050 */
1051 static int displayComment(
1052   const Op *pOp,     /* The opcode to be commented */
1053   const char *zP4,   /* Previously obtained value for P4 */
1054   char *zTemp,       /* Write result here */
1055   int nTemp          /* Space available in zTemp[] */
1056 ){
1057   const char *zOpName;
1058   const char *zSynopsis;
1059   int nOpName;
1060   int ii, jj;
1061   zOpName = sqlite3OpcodeName(pOp->opcode);
1062   nOpName = sqlite3Strlen30(zOpName);
1063   if( zOpName[nOpName+1] ){
1064     int seenCom = 0;
1065     char c;
1066     zSynopsis = zOpName += nOpName + 1;
1067     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
1068       if( c=='P' ){
1069         c = zSynopsis[++ii];
1070         if( c=='4' ){
1071           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
1072         }else if( c=='X' ){
1073           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
1074           seenCom = 1;
1075         }else{
1076           int v1 = translateP(c, pOp);
1077           int v2;
1078           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
1079           if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
1080             ii += 3;
1081             jj += sqlite3Strlen30(zTemp+jj);
1082             v2 = translateP(zSynopsis[ii], pOp);
1083             if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
1084               ii += 2;
1085               v2++;
1086             }
1087             if( v2>1 ){
1088               sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
1089             }
1090           }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
1091             ii += 4;
1092           }
1093         }
1094         jj += sqlite3Strlen30(zTemp+jj);
1095       }else{
1096         zTemp[jj++] = c;
1097       }
1098     }
1099     if( !seenCom && jj<nTemp-5 && pOp->zComment ){
1100       sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
1101       jj += sqlite3Strlen30(zTemp+jj);
1102     }
1103     if( jj<nTemp ) zTemp[jj] = 0;
1104   }else if( pOp->zComment ){
1105     sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
1106     jj = sqlite3Strlen30(zTemp);
1107   }else{
1108     zTemp[0] = 0;
1109     jj = 0;
1110   }
1111   return jj;
1112 }
1113 #endif /* SQLITE_DEBUG */
1114 
1115 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
1116 /*
1117 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
1118 ** that can be displayed in the P4 column of EXPLAIN output.
1119 */
1120 static void displayP4Expr(StrAccum *p, Expr *pExpr){
1121   const char *zOp = 0;
1122   switch( pExpr->op ){
1123     case TK_STRING:
1124       sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
1125       break;
1126     case TK_INTEGER:
1127       sqlite3XPrintf(p, "%d", pExpr->u.iValue);
1128       break;
1129     case TK_NULL:
1130       sqlite3XPrintf(p, "NULL");
1131       break;
1132     case TK_REGISTER: {
1133       sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
1134       break;
1135     }
1136     case TK_COLUMN: {
1137       if( pExpr->iColumn<0 ){
1138         sqlite3XPrintf(p, "rowid");
1139       }else{
1140         sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
1141       }
1142       break;
1143     }
1144     case TK_LT:      zOp = "LT";      break;
1145     case TK_LE:      zOp = "LE";      break;
1146     case TK_GT:      zOp = "GT";      break;
1147     case TK_GE:      zOp = "GE";      break;
1148     case TK_NE:      zOp = "NE";      break;
1149     case TK_EQ:      zOp = "EQ";      break;
1150     case TK_IS:      zOp = "IS";      break;
1151     case TK_ISNOT:   zOp = "ISNOT";   break;
1152     case TK_AND:     zOp = "AND";     break;
1153     case TK_OR:      zOp = "OR";      break;
1154     case TK_PLUS:    zOp = "ADD";     break;
1155     case TK_STAR:    zOp = "MUL";     break;
1156     case TK_MINUS:   zOp = "SUB";     break;
1157     case TK_REM:     zOp = "REM";     break;
1158     case TK_BITAND:  zOp = "BITAND";  break;
1159     case TK_BITOR:   zOp = "BITOR";   break;
1160     case TK_SLASH:   zOp = "DIV";     break;
1161     case TK_LSHIFT:  zOp = "LSHIFT";  break;
1162     case TK_RSHIFT:  zOp = "RSHIFT";  break;
1163     case TK_CONCAT:  zOp = "CONCAT";  break;
1164     case TK_UMINUS:  zOp = "MINUS";   break;
1165     case TK_UPLUS:   zOp = "PLUS";    break;
1166     case TK_BITNOT:  zOp = "BITNOT";  break;
1167     case TK_NOT:     zOp = "NOT";     break;
1168     case TK_ISNULL:  zOp = "ISNULL";  break;
1169     case TK_NOTNULL: zOp = "NOTNULL"; break;
1170 
1171     default:
1172       sqlite3XPrintf(p, "%s", "expr");
1173       break;
1174   }
1175 
1176   if( zOp ){
1177     sqlite3XPrintf(p, "%s(", zOp);
1178     displayP4Expr(p, pExpr->pLeft);
1179     if( pExpr->pRight ){
1180       sqlite3StrAccumAppend(p, ",", 1);
1181       displayP4Expr(p, pExpr->pRight);
1182     }
1183     sqlite3StrAccumAppend(p, ")", 1);
1184   }
1185 }
1186 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
1187 
1188 
1189 #if VDBE_DISPLAY_P4
1190 /*
1191 ** Compute a string that describes the P4 parameter for an opcode.
1192 ** Use zTemp for any required temporary buffer space.
1193 */
1194 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
1195   char *zP4 = zTemp;
1196   StrAccum x;
1197   assert( nTemp>=20 );
1198   sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
1199   switch( pOp->p4type ){
1200     case P4_KEYINFO: {
1201       int j;
1202       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1203       assert( pKeyInfo->aSortOrder!=0 );
1204       sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
1205       for(j=0; j<pKeyInfo->nField; j++){
1206         CollSeq *pColl = pKeyInfo->aColl[j];
1207         const char *zColl = pColl ? pColl->zName : "";
1208         if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
1209         sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
1210       }
1211       sqlite3StrAccumAppend(&x, ")", 1);
1212       break;
1213     }
1214 #ifdef SQLITE_ENABLE_CURSOR_HINTS
1215     case P4_EXPR: {
1216       displayP4Expr(&x, pOp->p4.pExpr);
1217       break;
1218     }
1219 #endif
1220     case P4_COLLSEQ: {
1221       CollSeq *pColl = pOp->p4.pColl;
1222       sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
1223       break;
1224     }
1225     case P4_FUNCDEF: {
1226       FuncDef *pDef = pOp->p4.pFunc;
1227       sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
1228       break;
1229     }
1230 #ifdef SQLITE_DEBUG
1231     case P4_FUNCCTX: {
1232       FuncDef *pDef = pOp->p4.pCtx->pFunc;
1233       sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
1234       break;
1235     }
1236 #endif
1237     case P4_INT64: {
1238       sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
1239       break;
1240     }
1241     case P4_INT32: {
1242       sqlite3XPrintf(&x, "%d", pOp->p4.i);
1243       break;
1244     }
1245     case P4_REAL: {
1246       sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
1247       break;
1248     }
1249     case P4_MEM: {
1250       Mem *pMem = pOp->p4.pMem;
1251       if( pMem->flags & MEM_Str ){
1252         zP4 = pMem->z;
1253       }else if( pMem->flags & MEM_Int ){
1254         sqlite3XPrintf(&x, "%lld", pMem->u.i);
1255       }else if( pMem->flags & MEM_Real ){
1256         sqlite3XPrintf(&x, "%.16g", pMem->u.r);
1257       }else if( pMem->flags & MEM_Null ){
1258         zP4 = "NULL";
1259       }else{
1260         assert( pMem->flags & MEM_Blob );
1261         zP4 = "(blob)";
1262       }
1263       break;
1264     }
1265 #ifndef SQLITE_OMIT_VIRTUALTABLE
1266     case P4_VTAB: {
1267       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
1268       sqlite3XPrintf(&x, "vtab:%p", pVtab);
1269       break;
1270     }
1271 #endif
1272     case P4_INTARRAY: {
1273       int i;
1274       int *ai = pOp->p4.ai;
1275       int n = ai[0];   /* The first element of an INTARRAY is always the
1276                        ** count of the number of elements to follow */
1277       for(i=1; i<n; i++){
1278         sqlite3XPrintf(&x, ",%d", ai[i]);
1279       }
1280       zTemp[0] = '[';
1281       sqlite3StrAccumAppend(&x, "]", 1);
1282       break;
1283     }
1284     case P4_SUBPROGRAM: {
1285       sqlite3XPrintf(&x, "program");
1286       break;
1287     }
1288     case P4_ADVANCE: {
1289       zTemp[0] = 0;
1290       break;
1291     }
1292     default: {
1293       zP4 = pOp->p4.z;
1294       if( zP4==0 ){
1295         zP4 = zTemp;
1296         zTemp[0] = 0;
1297       }
1298     }
1299   }
1300   sqlite3StrAccumFinish(&x);
1301   assert( zP4!=0 );
1302   return zP4;
1303 }
1304 #endif /* VDBE_DISPLAY_P4 */
1305 
1306 /*
1307 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
1308 **
1309 ** The prepared statements need to know in advance the complete set of
1310 ** attached databases that will be use.  A mask of these databases
1311 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
1312 ** p->btreeMask of databases that will require a lock.
1313 */
1314 void sqlite3VdbeUsesBtree(Vdbe *p, int i){
1315   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
1316   assert( i<(int)sizeof(p->btreeMask)*8 );
1317   DbMaskSet(p->btreeMask, i);
1318   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
1319     DbMaskSet(p->lockMask, i);
1320   }
1321 }
1322 
1323 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1324 /*
1325 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1326 ** this routine obtains the mutex associated with each BtShared structure
1327 ** that may be accessed by the VM passed as an argument. In doing so it also
1328 ** sets the BtShared.db member of each of the BtShared structures, ensuring
1329 ** that the correct busy-handler callback is invoked if required.
1330 **
1331 ** If SQLite is not threadsafe but does support shared-cache mode, then
1332 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1333 ** of all of BtShared structures accessible via the database handle
1334 ** associated with the VM.
1335 **
1336 ** If SQLite is not threadsafe and does not support shared-cache mode, this
1337 ** function is a no-op.
1338 **
1339 ** The p->btreeMask field is a bitmask of all btrees that the prepared
1340 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
1341 ** corresponding to btrees that use shared cache.  Then the runtime of
1342 ** this routine is N*N.  But as N is rarely more than 1, this should not
1343 ** be a problem.
1344 */
1345 void sqlite3VdbeEnter(Vdbe *p){
1346   int i;
1347   sqlite3 *db;
1348   Db *aDb;
1349   int nDb;
1350   if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
1351   db = p->db;
1352   aDb = db->aDb;
1353   nDb = db->nDb;
1354   for(i=0; i<nDb; i++){
1355     if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
1356       sqlite3BtreeEnter(aDb[i].pBt);
1357     }
1358   }
1359 }
1360 #endif
1361 
1362 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1363 /*
1364 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1365 */
1366 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
1367   int i;
1368   sqlite3 *db;
1369   Db *aDb;
1370   int nDb;
1371   db = p->db;
1372   aDb = db->aDb;
1373   nDb = db->nDb;
1374   for(i=0; i<nDb; i++){
1375     if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
1376       sqlite3BtreeLeave(aDb[i].pBt);
1377     }
1378   }
1379 }
1380 void sqlite3VdbeLeave(Vdbe *p){
1381   if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
1382   vdbeLeave(p);
1383 }
1384 #endif
1385 
1386 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1387 /*
1388 ** Print a single opcode.  This routine is used for debugging only.
1389 */
1390 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
1391   char *zP4;
1392   char zPtr[50];
1393   char zCom[100];
1394   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
1395   if( pOut==0 ) pOut = stdout;
1396   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
1397 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1398   displayComment(pOp, zP4, zCom, sizeof(zCom));
1399 #else
1400   zCom[0] = 0;
1401 #endif
1402   /* NB:  The sqlite3OpcodeName() function is implemented by code created
1403   ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1404   ** information from the vdbe.c source text */
1405   fprintf(pOut, zFormat1, pc,
1406       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
1407       zCom
1408   );
1409   fflush(pOut);
1410 }
1411 #endif
1412 
1413 /*
1414 ** Release an array of N Mem elements
1415 */
1416 static void releaseMemArray(Mem *p, int N){
1417   if( p && N ){
1418     Mem *pEnd = &p[N];
1419     sqlite3 *db = p->db;
1420     u8 malloc_failed = db->mallocFailed;
1421     if( db->pnBytesFreed ){
1422       do{
1423         if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
1424       }while( (++p)<pEnd );
1425       return;
1426     }
1427     do{
1428       assert( (&p[1])==pEnd || p[0].db==p[1].db );
1429       assert( sqlite3VdbeCheckMemInvariants(p) );
1430 
1431       /* This block is really an inlined version of sqlite3VdbeMemRelease()
1432       ** that takes advantage of the fact that the memory cell value is
1433       ** being set to NULL after releasing any dynamic resources.
1434       **
1435       ** The justification for duplicating code is that according to
1436       ** callgrind, this causes a certain test case to hit the CPU 4.7
1437       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1438       ** sqlite3MemRelease() were called from here. With -O2, this jumps
1439       ** to 6.6 percent. The test case is inserting 1000 rows into a table
1440       ** with no indexes using a single prepared INSERT statement, bind()
1441       ** and reset(). Inserts are grouped into a transaction.
1442       */
1443       testcase( p->flags & MEM_Agg );
1444       testcase( p->flags & MEM_Dyn );
1445       testcase( p->flags & MEM_Frame );
1446       testcase( p->flags & MEM_RowSet );
1447       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
1448         sqlite3VdbeMemRelease(p);
1449       }else if( p->szMalloc ){
1450         sqlite3DbFree(db, p->zMalloc);
1451         p->szMalloc = 0;
1452       }
1453 
1454       p->flags = MEM_Undefined;
1455     }while( (++p)<pEnd );
1456     db->mallocFailed = malloc_failed;
1457   }
1458 }
1459 
1460 /*
1461 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1462 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
1463 */
1464 void sqlite3VdbeFrameDelete(VdbeFrame *p){
1465   int i;
1466   Mem *aMem = VdbeFrameMem(p);
1467   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1468   for(i=0; i<p->nChildCsr; i++){
1469     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1470   }
1471   releaseMemArray(aMem, p->nChildMem);
1472   sqlite3DbFree(p->v->db, p);
1473 }
1474 
1475 #ifndef SQLITE_OMIT_EXPLAIN
1476 /*
1477 ** Give a listing of the program in the virtual machine.
1478 **
1479 ** The interface is the same as sqlite3VdbeExec().  But instead of
1480 ** running the code, it invokes the callback once for each instruction.
1481 ** This feature is used to implement "EXPLAIN".
1482 **
1483 ** When p->explain==1, each instruction is listed.  When
1484 ** p->explain==2, only OP_Explain instructions are listed and these
1485 ** are shown in a different format.  p->explain==2 is used to implement
1486 ** EXPLAIN QUERY PLAN.
1487 **
1488 ** When p->explain==1, first the main program is listed, then each of
1489 ** the trigger subprograms are listed one by one.
1490 */
1491 int sqlite3VdbeList(
1492   Vdbe *p                   /* The VDBE */
1493 ){
1494   int nRow;                            /* Stop when row count reaches this */
1495   int nSub = 0;                        /* Number of sub-vdbes seen so far */
1496   SubProgram **apSub = 0;              /* Array of sub-vdbes */
1497   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
1498   sqlite3 *db = p->db;                 /* The database connection */
1499   int i;                               /* Loop counter */
1500   int rc = SQLITE_OK;                  /* Return code */
1501   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
1502 
1503   assert( p->explain );
1504   assert( p->magic==VDBE_MAGIC_RUN );
1505   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
1506 
1507   /* Even though this opcode does not use dynamic strings for
1508   ** the result, result columns may become dynamic if the user calls
1509   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
1510   */
1511   releaseMemArray(pMem, 8);
1512   p->pResultSet = 0;
1513 
1514   if( p->rc==SQLITE_NOMEM ){
1515     /* This happens if a malloc() inside a call to sqlite3_column_text() or
1516     ** sqlite3_column_text16() failed.  */
1517     db->mallocFailed = 1;
1518     return SQLITE_ERROR;
1519   }
1520 
1521   /* When the number of output rows reaches nRow, that means the
1522   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1523   ** nRow is the sum of the number of rows in the main program, plus
1524   ** the sum of the number of rows in all trigger subprograms encountered
1525   ** so far.  The nRow value will increase as new trigger subprograms are
1526   ** encountered, but p->pc will eventually catch up to nRow.
1527   */
1528   nRow = p->nOp;
1529   if( p->explain==1 ){
1530     /* The first 8 memory cells are used for the result set.  So we will
1531     ** commandeer the 9th cell to use as storage for an array of pointers
1532     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
1533     ** cells.  */
1534     assert( p->nMem>9 );
1535     pSub = &p->aMem[9];
1536     if( pSub->flags&MEM_Blob ){
1537       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
1538       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
1539       nSub = pSub->n/sizeof(Vdbe*);
1540       apSub = (SubProgram **)pSub->z;
1541     }
1542     for(i=0; i<nSub; i++){
1543       nRow += apSub[i]->nOp;
1544     }
1545   }
1546 
1547   do{
1548     i = p->pc++;
1549   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1550   if( i>=nRow ){
1551     p->rc = SQLITE_OK;
1552     rc = SQLITE_DONE;
1553   }else if( db->u1.isInterrupted ){
1554     p->rc = SQLITE_INTERRUPT;
1555     rc = SQLITE_ERROR;
1556     sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
1557   }else{
1558     char *zP4;
1559     Op *pOp;
1560     if( i<p->nOp ){
1561       /* The output line number is small enough that we are still in the
1562       ** main program. */
1563       pOp = &p->aOp[i];
1564     }else{
1565       /* We are currently listing subprograms.  Figure out which one and
1566       ** pick up the appropriate opcode. */
1567       int j;
1568       i -= p->nOp;
1569       for(j=0; i>=apSub[j]->nOp; j++){
1570         i -= apSub[j]->nOp;
1571       }
1572       pOp = &apSub[j]->aOp[i];
1573     }
1574     if( p->explain==1 ){
1575       pMem->flags = MEM_Int;
1576       pMem->u.i = i;                                /* Program counter */
1577       pMem++;
1578 
1579       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
1580       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
1581       assert( pMem->z!=0 );
1582       pMem->n = sqlite3Strlen30(pMem->z);
1583       pMem->enc = SQLITE_UTF8;
1584       pMem++;
1585 
1586       /* When an OP_Program opcode is encounter (the only opcode that has
1587       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1588       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1589       ** has not already been seen.
1590       */
1591       if( pOp->p4type==P4_SUBPROGRAM ){
1592         int nByte = (nSub+1)*sizeof(SubProgram*);
1593         int j;
1594         for(j=0; j<nSub; j++){
1595           if( apSub[j]==pOp->p4.pProgram ) break;
1596         }
1597         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
1598           apSub = (SubProgram **)pSub->z;
1599           apSub[nSub++] = pOp->p4.pProgram;
1600           pSub->flags |= MEM_Blob;
1601           pSub->n = nSub*sizeof(SubProgram*);
1602         }
1603       }
1604     }
1605 
1606     pMem->flags = MEM_Int;
1607     pMem->u.i = pOp->p1;                          /* P1 */
1608     pMem++;
1609 
1610     pMem->flags = MEM_Int;
1611     pMem->u.i = pOp->p2;                          /* P2 */
1612     pMem++;
1613 
1614     pMem->flags = MEM_Int;
1615     pMem->u.i = pOp->p3;                          /* P3 */
1616     pMem++;
1617 
1618     if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
1619       assert( p->db->mallocFailed );
1620       return SQLITE_ERROR;
1621     }
1622     pMem->flags = MEM_Str|MEM_Term;
1623     zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
1624     if( zP4!=pMem->z ){
1625       sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
1626     }else{
1627       assert( pMem->z!=0 );
1628       pMem->n = sqlite3Strlen30(pMem->z);
1629       pMem->enc = SQLITE_UTF8;
1630     }
1631     pMem++;
1632 
1633     if( p->explain==1 ){
1634       if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
1635         assert( p->db->mallocFailed );
1636         return SQLITE_ERROR;
1637       }
1638       pMem->flags = MEM_Str|MEM_Term;
1639       pMem->n = 2;
1640       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
1641       pMem->enc = SQLITE_UTF8;
1642       pMem++;
1643 
1644 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1645       if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
1646         assert( p->db->mallocFailed );
1647         return SQLITE_ERROR;
1648       }
1649       pMem->flags = MEM_Str|MEM_Term;
1650       pMem->n = displayComment(pOp, zP4, pMem->z, 500);
1651       pMem->enc = SQLITE_UTF8;
1652 #else
1653       pMem->flags = MEM_Null;                       /* Comment */
1654 #endif
1655     }
1656 
1657     p->nResColumn = 8 - 4*(p->explain-1);
1658     p->pResultSet = &p->aMem[1];
1659     p->rc = SQLITE_OK;
1660     rc = SQLITE_ROW;
1661   }
1662   return rc;
1663 }
1664 #endif /* SQLITE_OMIT_EXPLAIN */
1665 
1666 #ifdef SQLITE_DEBUG
1667 /*
1668 ** Print the SQL that was used to generate a VDBE program.
1669 */
1670 void sqlite3VdbePrintSql(Vdbe *p){
1671   const char *z = 0;
1672   if( p->zSql ){
1673     z = p->zSql;
1674   }else if( p->nOp>=1 ){
1675     const VdbeOp *pOp = &p->aOp[0];
1676     if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
1677       z = pOp->p4.z;
1678       while( sqlite3Isspace(*z) ) z++;
1679     }
1680   }
1681   if( z ) printf("SQL: [%s]\n", z);
1682 }
1683 #endif
1684 
1685 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1686 /*
1687 ** Print an IOTRACE message showing SQL content.
1688 */
1689 void sqlite3VdbeIOTraceSql(Vdbe *p){
1690   int nOp = p->nOp;
1691   VdbeOp *pOp;
1692   if( sqlite3IoTrace==0 ) return;
1693   if( nOp<1 ) return;
1694   pOp = &p->aOp[0];
1695   if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
1696     int i, j;
1697     char z[1000];
1698     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
1699     for(i=0; sqlite3Isspace(z[i]); i++){}
1700     for(j=0; z[i]; i++){
1701       if( sqlite3Isspace(z[i]) ){
1702         if( z[i-1]!=' ' ){
1703           z[j++] = ' ';
1704         }
1705       }else{
1706         z[j++] = z[i];
1707       }
1708     }
1709     z[j] = 0;
1710     sqlite3IoTrace("SQL %s\n", z);
1711   }
1712 }
1713 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1714 
1715 /* An instance of this object describes bulk memory available for use
1716 ** by subcomponents of a prepared statement.  Space is allocated out
1717 ** of a ReusableSpace object by the allocSpace() routine below.
1718 */
1719 struct ReusableSpace {
1720   u8 *pSpace;          /* Available memory */
1721   int nFree;           /* Bytes of available memory */
1722   int nNeeded;         /* Total bytes that could not be allocated */
1723 };
1724 
1725 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
1726 ** from the ReusableSpace object.  Return a pointer to the allocated
1727 ** memory on success.  If insufficient memory is available in the
1728 ** ReusableSpace object, increase the ReusableSpace.nNeeded
1729 ** value by the amount needed and return NULL.
1730 **
1731 ** If pBuf is not initially NULL, that means that the memory has already
1732 ** been allocated by a prior call to this routine, so just return a copy
1733 ** of pBuf and leave ReusableSpace unchanged.
1734 **
1735 ** This allocator is employed to repurpose unused slots at the end of the
1736 ** opcode array of prepared state for other memory needs of the prepared
1737 ** statement.
1738 */
1739 static void *allocSpace(
1740   struct ReusableSpace *p,  /* Bulk memory available for allocation */
1741   void *pBuf,               /* Pointer to a prior allocation */
1742   int nByte                 /* Bytes of memory needed */
1743 ){
1744   assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
1745   if( pBuf==0 ){
1746     nByte = ROUND8(nByte);
1747     if( nByte <= p->nFree ){
1748       p->nFree -= nByte;
1749       pBuf = &p->pSpace[p->nFree];
1750     }else{
1751       p->nNeeded += nByte;
1752     }
1753   }
1754   assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
1755   return pBuf;
1756 }
1757 
1758 /*
1759 ** Rewind the VDBE back to the beginning in preparation for
1760 ** running it.
1761 */
1762 void sqlite3VdbeRewind(Vdbe *p){
1763 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1764   int i;
1765 #endif
1766   assert( p!=0 );
1767   assert( p->magic==VDBE_MAGIC_INIT );
1768 
1769   /* There should be at least one opcode.
1770   */
1771   assert( p->nOp>0 );
1772 
1773   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
1774   p->magic = VDBE_MAGIC_RUN;
1775 
1776 #ifdef SQLITE_DEBUG
1777   for(i=1; i<p->nMem; i++){
1778     assert( p->aMem[i].db==p->db );
1779   }
1780 #endif
1781   p->pc = -1;
1782   p->rc = SQLITE_OK;
1783   p->errorAction = OE_Abort;
1784   p->nChange = 0;
1785   p->cacheCtr = 1;
1786   p->minWriteFileFormat = 255;
1787   p->iStatement = 0;
1788   p->nFkConstraint = 0;
1789 #ifdef VDBE_PROFILE
1790   for(i=0; i<p->nOp; i++){
1791     p->aOp[i].cnt = 0;
1792     p->aOp[i].cycles = 0;
1793   }
1794 #endif
1795 }
1796 
1797 /*
1798 ** Prepare a virtual machine for execution for the first time after
1799 ** creating the virtual machine.  This involves things such
1800 ** as allocating registers and initializing the program counter.
1801 ** After the VDBE has be prepped, it can be executed by one or more
1802 ** calls to sqlite3VdbeExec().
1803 **
1804 ** This function may be called exactly once on each virtual machine.
1805 ** After this routine is called the VM has been "packaged" and is ready
1806 ** to run.  After this routine is called, further calls to
1807 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
1808 ** the Vdbe from the Parse object that helped generate it so that the
1809 ** the Vdbe becomes an independent entity and the Parse object can be
1810 ** destroyed.
1811 **
1812 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1813 ** to its initial state after it has been run.
1814 */
1815 void sqlite3VdbeMakeReady(
1816   Vdbe *p,                       /* The VDBE */
1817   Parse *pParse                  /* Parsing context */
1818 ){
1819   sqlite3 *db;                   /* The database connection */
1820   int nVar;                      /* Number of parameters */
1821   int nMem;                      /* Number of VM memory registers */
1822   int nCursor;                   /* Number of cursors required */
1823   int nArg;                      /* Number of arguments in subprograms */
1824   int nOnce;                     /* Number of OP_Once instructions */
1825   int n;                         /* Loop counter */
1826   struct ReusableSpace x;        /* Reusable bulk memory */
1827 
1828   assert( p!=0 );
1829   assert( p->nOp>0 );
1830   assert( pParse!=0 );
1831   assert( p->magic==VDBE_MAGIC_INIT );
1832   assert( pParse==p->pParse );
1833   db = p->db;
1834   assert( db->mallocFailed==0 );
1835   nVar = pParse->nVar;
1836   nMem = pParse->nMem;
1837   nCursor = pParse->nTab;
1838   nArg = pParse->nMaxArg;
1839   nOnce = pParse->nOnce;
1840   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
1841 
1842   /* For each cursor required, also allocate a memory cell. Memory
1843   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1844   ** the vdbe program. Instead they are used to allocate memory for
1845   ** VdbeCursor/BtCursor structures. The blob of memory associated with
1846   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1847   ** stores the blob of memory associated with cursor 1, etc.
1848   **
1849   ** See also: allocateCursor().
1850   */
1851   nMem += nCursor;
1852 
1853   /* Figure out how much reusable memory is available at the end of the
1854   ** opcode array.  This extra memory will be reallocated for other elements
1855   ** of the prepared statement.
1856   */
1857   n = ROUND8(sizeof(Op)*p->nOp);              /* Bytes of opcode memory used */
1858   x.pSpace = &((u8*)p->aOp)[n];               /* Unused opcode memory */
1859   assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
1860   x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);  /* Bytes of unused memory */
1861   assert( x.nFree>=0 );
1862   if( x.nFree>0 ){
1863     memset(x.pSpace, 0, x.nFree);
1864     assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
1865   }
1866 
1867   resolveP2Values(p, &nArg);
1868   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1869   if( pParse->explain && nMem<10 ){
1870     nMem = 10;
1871   }
1872   p->expired = 0;
1873 
1874   /* Memory for registers, parameters, cursor, etc, is allocated in one or two
1875   ** passes.  On the first pass, we try to reuse unused memory at the
1876   ** end of the opcode array.  If we are unable to satisfy all memory
1877   ** requirements by reusing the opcode array tail, then the second
1878   ** pass will fill in the remainder using a fresh memory allocation.
1879   **
1880   ** This two-pass approach that reuses as much memory as possible from
1881   ** the leftover memory at the end of the opcode array.  This can significantly
1882   ** reduce the amount of memory held by a prepared statement.
1883   */
1884   do {
1885     x.nNeeded = 0;
1886     p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
1887     p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
1888     p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
1889     p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
1890     p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce);
1891 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1892     p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
1893 #endif
1894     if( x.nNeeded==0 ) break;
1895     x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded);
1896     x.nFree = x.nNeeded;
1897   }while( !db->mallocFailed );
1898 
1899   p->nCursor = nCursor;
1900   p->nOnceFlag = nOnce;
1901   if( p->aVar ){
1902     p->nVar = (ynVar)nVar;
1903     for(n=0; n<nVar; n++){
1904       p->aVar[n].flags = MEM_Null;
1905       p->aVar[n].db = db;
1906     }
1907   }
1908   p->nzVar = pParse->nzVar;
1909   p->azVar = pParse->azVar;
1910   pParse->nzVar =  0;
1911   pParse->azVar = 0;
1912   if( p->aMem ){
1913     p->aMem--;                      /* aMem[] goes from 1..nMem */
1914     p->nMem = nMem;                 /*       not from 0..nMem-1 */
1915     for(n=1; n<=nMem; n++){
1916       p->aMem[n].flags = MEM_Undefined;
1917       p->aMem[n].db = db;
1918     }
1919   }
1920   p->explain = pParse->explain;
1921   sqlite3VdbeRewind(p);
1922 }
1923 
1924 /*
1925 ** Close a VDBE cursor and release all the resources that cursor
1926 ** happens to hold.
1927 */
1928 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
1929   if( pCx==0 ){
1930     return;
1931   }
1932   assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE );
1933   switch( pCx->eCurType ){
1934     case CURTYPE_SORTER: {
1935       sqlite3VdbeSorterClose(p->db, pCx);
1936       break;
1937     }
1938     case CURTYPE_BTREE: {
1939       if( pCx->pBt ){
1940         sqlite3BtreeClose(pCx->pBt);
1941         /* The pCx->pCursor will be close automatically, if it exists, by
1942         ** the call above. */
1943       }else{
1944         assert( pCx->uc.pCursor!=0 );
1945         sqlite3BtreeCloseCursor(pCx->uc.pCursor);
1946       }
1947       break;
1948     }
1949 #ifndef SQLITE_OMIT_VIRTUALTABLE
1950     case CURTYPE_VTAB: {
1951       sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
1952       const sqlite3_module *pModule = pVCur->pVtab->pModule;
1953       assert( pVCur->pVtab->nRef>0 );
1954       pVCur->pVtab->nRef--;
1955       pModule->xClose(pVCur);
1956       break;
1957     }
1958 #endif
1959   }
1960 }
1961 
1962 /*
1963 ** Close all cursors in the current frame.
1964 */
1965 static void closeCursorsInFrame(Vdbe *p){
1966   if( p->apCsr ){
1967     int i;
1968     for(i=0; i<p->nCursor; i++){
1969       VdbeCursor *pC = p->apCsr[i];
1970       if( pC ){
1971         sqlite3VdbeFreeCursor(p, pC);
1972         p->apCsr[i] = 0;
1973       }
1974     }
1975   }
1976 }
1977 
1978 /*
1979 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1980 ** is used, for example, when a trigger sub-program is halted to restore
1981 ** control to the main program.
1982 */
1983 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1984   Vdbe *v = pFrame->v;
1985   closeCursorsInFrame(v);
1986 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1987   v->anExec = pFrame->anExec;
1988 #endif
1989   v->aOnceFlag = pFrame->aOnceFlag;
1990   v->nOnceFlag = pFrame->nOnceFlag;
1991   v->aOp = pFrame->aOp;
1992   v->nOp = pFrame->nOp;
1993   v->aMem = pFrame->aMem;
1994   v->nMem = pFrame->nMem;
1995   v->apCsr = pFrame->apCsr;
1996   v->nCursor = pFrame->nCursor;
1997   v->db->lastRowid = pFrame->lastRowid;
1998   v->nChange = pFrame->nChange;
1999   v->db->nChange = pFrame->nDbChange;
2000   return pFrame->pc;
2001 }
2002 
2003 /*
2004 ** Close all cursors.
2005 **
2006 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
2007 ** cell array. This is necessary as the memory cell array may contain
2008 ** pointers to VdbeFrame objects, which may in turn contain pointers to
2009 ** open cursors.
2010 */
2011 static void closeAllCursors(Vdbe *p){
2012   if( p->pFrame ){
2013     VdbeFrame *pFrame;
2014     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
2015     sqlite3VdbeFrameRestore(pFrame);
2016     p->pFrame = 0;
2017     p->nFrame = 0;
2018   }
2019   assert( p->nFrame==0 );
2020   closeCursorsInFrame(p);
2021   if( p->aMem ){
2022     releaseMemArray(&p->aMem[1], p->nMem);
2023   }
2024   while( p->pDelFrame ){
2025     VdbeFrame *pDel = p->pDelFrame;
2026     p->pDelFrame = pDel->pParent;
2027     sqlite3VdbeFrameDelete(pDel);
2028   }
2029 
2030   /* Delete any auxdata allocations made by the VM */
2031   if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
2032   assert( p->pAuxData==0 );
2033 }
2034 
2035 /*
2036 ** Clean up the VM after a single run.
2037 */
2038 static void Cleanup(Vdbe *p){
2039   sqlite3 *db = p->db;
2040 
2041 #ifdef SQLITE_DEBUG
2042   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
2043   ** Vdbe.aMem[] arrays have already been cleaned up.  */
2044   int i;
2045   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
2046   if( p->aMem ){
2047     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
2048   }
2049 #endif
2050 
2051   sqlite3DbFree(db, p->zErrMsg);
2052   p->zErrMsg = 0;
2053   p->pResultSet = 0;
2054 }
2055 
2056 /*
2057 ** Set the number of result columns that will be returned by this SQL
2058 ** statement. This is now set at compile time, rather than during
2059 ** execution of the vdbe program so that sqlite3_column_count() can
2060 ** be called on an SQL statement before sqlite3_step().
2061 */
2062 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
2063   Mem *pColName;
2064   int n;
2065   sqlite3 *db = p->db;
2066 
2067   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
2068   sqlite3DbFree(db, p->aColName);
2069   n = nResColumn*COLNAME_N;
2070   p->nResColumn = (u16)nResColumn;
2071   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
2072   if( p->aColName==0 ) return;
2073   while( n-- > 0 ){
2074     pColName->flags = MEM_Null;
2075     pColName->db = p->db;
2076     pColName++;
2077   }
2078 }
2079 
2080 /*
2081 ** Set the name of the idx'th column to be returned by the SQL statement.
2082 ** zName must be a pointer to a nul terminated string.
2083 **
2084 ** This call must be made after a call to sqlite3VdbeSetNumCols().
2085 **
2086 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
2087 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
2088 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
2089 */
2090 int sqlite3VdbeSetColName(
2091   Vdbe *p,                         /* Vdbe being configured */
2092   int idx,                         /* Index of column zName applies to */
2093   int var,                         /* One of the COLNAME_* constants */
2094   const char *zName,               /* Pointer to buffer containing name */
2095   void (*xDel)(void*)              /* Memory management strategy for zName */
2096 ){
2097   int rc;
2098   Mem *pColName;
2099   assert( idx<p->nResColumn );
2100   assert( var<COLNAME_N );
2101   if( p->db->mallocFailed ){
2102     assert( !zName || xDel!=SQLITE_DYNAMIC );
2103     return SQLITE_NOMEM;
2104   }
2105   assert( p->aColName!=0 );
2106   pColName = &(p->aColName[idx+var*p->nResColumn]);
2107   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
2108   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
2109   return rc;
2110 }
2111 
2112 /*
2113 ** A read or write transaction may or may not be active on database handle
2114 ** db. If a transaction is active, commit it. If there is a
2115 ** write-transaction spanning more than one database file, this routine
2116 ** takes care of the master journal trickery.
2117 */
2118 static int vdbeCommit(sqlite3 *db, Vdbe *p){
2119   int i;
2120   int nTrans = 0;  /* Number of databases with an active write-transaction */
2121   int rc = SQLITE_OK;
2122   int needXcommit = 0;
2123 
2124 #ifdef SQLITE_OMIT_VIRTUALTABLE
2125   /* With this option, sqlite3VtabSync() is defined to be simply
2126   ** SQLITE_OK so p is not used.
2127   */
2128   UNUSED_PARAMETER(p);
2129 #endif
2130 
2131   /* Before doing anything else, call the xSync() callback for any
2132   ** virtual module tables written in this transaction. This has to
2133   ** be done before determining whether a master journal file is
2134   ** required, as an xSync() callback may add an attached database
2135   ** to the transaction.
2136   */
2137   rc = sqlite3VtabSync(db, p);
2138 
2139   /* This loop determines (a) if the commit hook should be invoked and
2140   ** (b) how many database files have open write transactions, not
2141   ** including the temp database. (b) is important because if more than
2142   ** one database file has an open write transaction, a master journal
2143   ** file is required for an atomic commit.
2144   */
2145   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2146     Btree *pBt = db->aDb[i].pBt;
2147     if( sqlite3BtreeIsInTrans(pBt) ){
2148       needXcommit = 1;
2149       if( i!=1 ) nTrans++;
2150       sqlite3BtreeEnter(pBt);
2151       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
2152       sqlite3BtreeLeave(pBt);
2153     }
2154   }
2155   if( rc!=SQLITE_OK ){
2156     return rc;
2157   }
2158 
2159   /* If there are any write-transactions at all, invoke the commit hook */
2160   if( needXcommit && db->xCommitCallback ){
2161     rc = db->xCommitCallback(db->pCommitArg);
2162     if( rc ){
2163       return SQLITE_CONSTRAINT_COMMITHOOK;
2164     }
2165   }
2166 
2167   /* The simple case - no more than one database file (not counting the
2168   ** TEMP database) has a transaction active.   There is no need for the
2169   ** master-journal.
2170   **
2171   ** If the return value of sqlite3BtreeGetFilename() is a zero length
2172   ** string, it means the main database is :memory: or a temp file.  In
2173   ** that case we do not support atomic multi-file commits, so use the
2174   ** simple case then too.
2175   */
2176   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
2177    || nTrans<=1
2178   ){
2179     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2180       Btree *pBt = db->aDb[i].pBt;
2181       if( pBt ){
2182         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
2183       }
2184     }
2185 
2186     /* Do the commit only if all databases successfully complete phase 1.
2187     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
2188     ** IO error while deleting or truncating a journal file. It is unlikely,
2189     ** but could happen. In this case abandon processing and return the error.
2190     */
2191     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2192       Btree *pBt = db->aDb[i].pBt;
2193       if( pBt ){
2194         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
2195       }
2196     }
2197     if( rc==SQLITE_OK ){
2198       sqlite3VtabCommit(db);
2199     }
2200   }
2201 
2202   /* The complex case - There is a multi-file write-transaction active.
2203   ** This requires a master journal file to ensure the transaction is
2204   ** committed atomically.
2205   */
2206 #ifndef SQLITE_OMIT_DISKIO
2207   else{
2208     sqlite3_vfs *pVfs = db->pVfs;
2209     int needSync = 0;
2210     char *zMaster = 0;   /* File-name for the master journal */
2211     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
2212     sqlite3_file *pMaster = 0;
2213     i64 offset = 0;
2214     int res;
2215     int retryCount = 0;
2216     int nMainFile;
2217 
2218     /* Select a master journal file name */
2219     nMainFile = sqlite3Strlen30(zMainFile);
2220     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
2221     if( zMaster==0 ) return SQLITE_NOMEM;
2222     do {
2223       u32 iRandom;
2224       if( retryCount ){
2225         if( retryCount>100 ){
2226           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2227           sqlite3OsDelete(pVfs, zMaster, 0);
2228           break;
2229         }else if( retryCount==1 ){
2230           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2231         }
2232       }
2233       retryCount++;
2234       sqlite3_randomness(sizeof(iRandom), &iRandom);
2235       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
2236                                (iRandom>>8)&0xffffff, iRandom&0xff);
2237       /* The antipenultimate character of the master journal name must
2238       ** be "9" to avoid name collisions when using 8+3 filenames. */
2239       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
2240       sqlite3FileSuffix3(zMainFile, zMaster);
2241       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2242     }while( rc==SQLITE_OK && res );
2243     if( rc==SQLITE_OK ){
2244       /* Open the master journal. */
2245       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2246           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2247           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2248       );
2249     }
2250     if( rc!=SQLITE_OK ){
2251       sqlite3DbFree(db, zMaster);
2252       return rc;
2253     }
2254 
2255     /* Write the name of each database file in the transaction into the new
2256     ** master journal file. If an error occurs at this point close
2257     ** and delete the master journal file. All the individual journal files
2258     ** still have 'null' as the master journal pointer, so they will roll
2259     ** back independently if a failure occurs.
2260     */
2261     for(i=0; i<db->nDb; i++){
2262       Btree *pBt = db->aDb[i].pBt;
2263       if( sqlite3BtreeIsInTrans(pBt) ){
2264         char const *zFile = sqlite3BtreeGetJournalname(pBt);
2265         if( zFile==0 ){
2266           continue;  /* Ignore TEMP and :memory: databases */
2267         }
2268         assert( zFile[0]!=0 );
2269         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2270           needSync = 1;
2271         }
2272         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2273         offset += sqlite3Strlen30(zFile)+1;
2274         if( rc!=SQLITE_OK ){
2275           sqlite3OsCloseFree(pMaster);
2276           sqlite3OsDelete(pVfs, zMaster, 0);
2277           sqlite3DbFree(db, zMaster);
2278           return rc;
2279         }
2280       }
2281     }
2282 
2283     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2284     ** flag is set this is not required.
2285     */
2286     if( needSync
2287      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2288      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2289     ){
2290       sqlite3OsCloseFree(pMaster);
2291       sqlite3OsDelete(pVfs, zMaster, 0);
2292       sqlite3DbFree(db, zMaster);
2293       return rc;
2294     }
2295 
2296     /* Sync all the db files involved in the transaction. The same call
2297     ** sets the master journal pointer in each individual journal. If
2298     ** an error occurs here, do not delete the master journal file.
2299     **
2300     ** If the error occurs during the first call to
2301     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2302     ** master journal file will be orphaned. But we cannot delete it,
2303     ** in case the master journal file name was written into the journal
2304     ** file before the failure occurred.
2305     */
2306     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2307       Btree *pBt = db->aDb[i].pBt;
2308       if( pBt ){
2309         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
2310       }
2311     }
2312     sqlite3OsCloseFree(pMaster);
2313     assert( rc!=SQLITE_BUSY );
2314     if( rc!=SQLITE_OK ){
2315       sqlite3DbFree(db, zMaster);
2316       return rc;
2317     }
2318 
2319     /* Delete the master journal file. This commits the transaction. After
2320     ** doing this the directory is synced again before any individual
2321     ** transaction files are deleted.
2322     */
2323     rc = sqlite3OsDelete(pVfs, zMaster, needSync);
2324     sqlite3DbFree(db, zMaster);
2325     zMaster = 0;
2326     if( rc ){
2327       return rc;
2328     }
2329 
2330     /* All files and directories have already been synced, so the following
2331     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2332     ** deleting or truncating journals. If something goes wrong while
2333     ** this is happening we don't really care. The integrity of the
2334     ** transaction is already guaranteed, but some stray 'cold' journals
2335     ** may be lying around. Returning an error code won't help matters.
2336     */
2337     disable_simulated_io_errors();
2338     sqlite3BeginBenignMalloc();
2339     for(i=0; i<db->nDb; i++){
2340       Btree *pBt = db->aDb[i].pBt;
2341       if( pBt ){
2342         sqlite3BtreeCommitPhaseTwo(pBt, 1);
2343       }
2344     }
2345     sqlite3EndBenignMalloc();
2346     enable_simulated_io_errors();
2347 
2348     sqlite3VtabCommit(db);
2349   }
2350 #endif
2351 
2352   return rc;
2353 }
2354 
2355 /*
2356 ** This routine checks that the sqlite3.nVdbeActive count variable
2357 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
2358 ** currently active. An assertion fails if the two counts do not match.
2359 ** This is an internal self-check only - it is not an essential processing
2360 ** step.
2361 **
2362 ** This is a no-op if NDEBUG is defined.
2363 */
2364 #ifndef NDEBUG
2365 static void checkActiveVdbeCnt(sqlite3 *db){
2366   Vdbe *p;
2367   int cnt = 0;
2368   int nWrite = 0;
2369   int nRead = 0;
2370   p = db->pVdbe;
2371   while( p ){
2372     if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
2373       cnt++;
2374       if( p->readOnly==0 ) nWrite++;
2375       if( p->bIsReader ) nRead++;
2376     }
2377     p = p->pNext;
2378   }
2379   assert( cnt==db->nVdbeActive );
2380   assert( nWrite==db->nVdbeWrite );
2381   assert( nRead==db->nVdbeRead );
2382 }
2383 #else
2384 #define checkActiveVdbeCnt(x)
2385 #endif
2386 
2387 /*
2388 ** If the Vdbe passed as the first argument opened a statement-transaction,
2389 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2390 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2391 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
2392 ** statement transaction is committed.
2393 **
2394 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2395 ** Otherwise SQLITE_OK.
2396 */
2397 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
2398   sqlite3 *const db = p->db;
2399   int rc = SQLITE_OK;
2400 
2401   /* If p->iStatement is greater than zero, then this Vdbe opened a
2402   ** statement transaction that should be closed here. The only exception
2403   ** is that an IO error may have occurred, causing an emergency rollback.
2404   ** In this case (db->nStatement==0), and there is nothing to do.
2405   */
2406   if( db->nStatement && p->iStatement ){
2407     int i;
2408     const int iSavepoint = p->iStatement-1;
2409 
2410     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2411     assert( db->nStatement>0 );
2412     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2413 
2414     for(i=0; i<db->nDb; i++){
2415       int rc2 = SQLITE_OK;
2416       Btree *pBt = db->aDb[i].pBt;
2417       if( pBt ){
2418         if( eOp==SAVEPOINT_ROLLBACK ){
2419           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2420         }
2421         if( rc2==SQLITE_OK ){
2422           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2423         }
2424         if( rc==SQLITE_OK ){
2425           rc = rc2;
2426         }
2427       }
2428     }
2429     db->nStatement--;
2430     p->iStatement = 0;
2431 
2432     if( rc==SQLITE_OK ){
2433       if( eOp==SAVEPOINT_ROLLBACK ){
2434         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2435       }
2436       if( rc==SQLITE_OK ){
2437         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2438       }
2439     }
2440 
2441     /* If the statement transaction is being rolled back, also restore the
2442     ** database handles deferred constraint counter to the value it had when
2443     ** the statement transaction was opened.  */
2444     if( eOp==SAVEPOINT_ROLLBACK ){
2445       db->nDeferredCons = p->nStmtDefCons;
2446       db->nDeferredImmCons = p->nStmtDefImmCons;
2447     }
2448   }
2449   return rc;
2450 }
2451 
2452 /*
2453 ** This function is called when a transaction opened by the database
2454 ** handle associated with the VM passed as an argument is about to be
2455 ** committed. If there are outstanding deferred foreign key constraint
2456 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2457 **
2458 ** If there are outstanding FK violations and this function returns
2459 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2460 ** and write an error message to it. Then return SQLITE_ERROR.
2461 */
2462 #ifndef SQLITE_OMIT_FOREIGN_KEY
2463 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
2464   sqlite3 *db = p->db;
2465   if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2466    || (!deferred && p->nFkConstraint>0)
2467   ){
2468     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
2469     p->errorAction = OE_Abort;
2470     sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
2471     return SQLITE_ERROR;
2472   }
2473   return SQLITE_OK;
2474 }
2475 #endif
2476 
2477 /*
2478 ** This routine is called the when a VDBE tries to halt.  If the VDBE
2479 ** has made changes and is in autocommit mode, then commit those
2480 ** changes.  If a rollback is needed, then do the rollback.
2481 **
2482 ** This routine is the only way to move the state of a VM from
2483 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
2484 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
2485 **
2486 ** Return an error code.  If the commit could not complete because of
2487 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
2488 ** means the close did not happen and needs to be repeated.
2489 */
2490 int sqlite3VdbeHalt(Vdbe *p){
2491   int rc;                         /* Used to store transient return codes */
2492   sqlite3 *db = p->db;
2493 
2494   /* This function contains the logic that determines if a statement or
2495   ** transaction will be committed or rolled back as a result of the
2496   ** execution of this virtual machine.
2497   **
2498   ** If any of the following errors occur:
2499   **
2500   **     SQLITE_NOMEM
2501   **     SQLITE_IOERR
2502   **     SQLITE_FULL
2503   **     SQLITE_INTERRUPT
2504   **
2505   ** Then the internal cache might have been left in an inconsistent
2506   ** state.  We need to rollback the statement transaction, if there is
2507   ** one, or the complete transaction if there is no statement transaction.
2508   */
2509 
2510   if( p->db->mallocFailed ){
2511     p->rc = SQLITE_NOMEM;
2512   }
2513   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
2514   closeAllCursors(p);
2515   if( p->magic!=VDBE_MAGIC_RUN ){
2516     return SQLITE_OK;
2517   }
2518   checkActiveVdbeCnt(db);
2519 
2520   /* No commit or rollback needed if the program never started or if the
2521   ** SQL statement does not read or write a database file.  */
2522   if( p->pc>=0 && p->bIsReader ){
2523     int mrc;   /* Primary error code from p->rc */
2524     int eStatementOp = 0;
2525     int isSpecialError;            /* Set to true if a 'special' error */
2526 
2527     /* Lock all btrees used by the statement */
2528     sqlite3VdbeEnter(p);
2529 
2530     /* Check for one of the special errors */
2531     mrc = p->rc & 0xff;
2532     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
2533                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
2534     if( isSpecialError ){
2535       /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2536       ** no rollback is necessary. Otherwise, at least a savepoint
2537       ** transaction must be rolled back to restore the database to a
2538       ** consistent state.
2539       **
2540       ** Even if the statement is read-only, it is important to perform
2541       ** a statement or transaction rollback operation. If the error
2542       ** occurred while writing to the journal, sub-journal or database
2543       ** file as part of an effort to free up cache space (see function
2544       ** pagerStress() in pager.c), the rollback is required to restore
2545       ** the pager to a consistent state.
2546       */
2547       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
2548         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
2549           eStatementOp = SAVEPOINT_ROLLBACK;
2550         }else{
2551           /* We are forced to roll back the active transaction. Before doing
2552           ** so, abort any other statements this handle currently has active.
2553           */
2554           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2555           sqlite3CloseSavepoints(db);
2556           db->autoCommit = 1;
2557           p->nChange = 0;
2558         }
2559       }
2560     }
2561 
2562     /* Check for immediate foreign key violations. */
2563     if( p->rc==SQLITE_OK ){
2564       sqlite3VdbeCheckFk(p, 0);
2565     }
2566 
2567     /* If the auto-commit flag is set and this is the only active writer
2568     ** VM, then we do either a commit or rollback of the current transaction.
2569     **
2570     ** Note: This block also runs if one of the special errors handled
2571     ** above has occurred.
2572     */
2573     if( !sqlite3VtabInSync(db)
2574      && db->autoCommit
2575      && db->nVdbeWrite==(p->readOnly==0)
2576     ){
2577       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
2578         rc = sqlite3VdbeCheckFk(p, 1);
2579         if( rc!=SQLITE_OK ){
2580           if( NEVER(p->readOnly) ){
2581             sqlite3VdbeLeave(p);
2582             return SQLITE_ERROR;
2583           }
2584           rc = SQLITE_CONSTRAINT_FOREIGNKEY;
2585         }else{
2586           /* The auto-commit flag is true, the vdbe program was successful
2587           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2588           ** key constraints to hold up the transaction. This means a commit
2589           ** is required. */
2590           rc = vdbeCommit(db, p);
2591         }
2592         if( rc==SQLITE_BUSY && p->readOnly ){
2593           sqlite3VdbeLeave(p);
2594           return SQLITE_BUSY;
2595         }else if( rc!=SQLITE_OK ){
2596           p->rc = rc;
2597           sqlite3RollbackAll(db, SQLITE_OK);
2598           p->nChange = 0;
2599         }else{
2600           db->nDeferredCons = 0;
2601           db->nDeferredImmCons = 0;
2602           db->flags &= ~SQLITE_DeferFKs;
2603           sqlite3CommitInternalChanges(db);
2604         }
2605       }else{
2606         sqlite3RollbackAll(db, SQLITE_OK);
2607         p->nChange = 0;
2608       }
2609       db->nStatement = 0;
2610     }else if( eStatementOp==0 ){
2611       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
2612         eStatementOp = SAVEPOINT_RELEASE;
2613       }else if( p->errorAction==OE_Abort ){
2614         eStatementOp = SAVEPOINT_ROLLBACK;
2615       }else{
2616         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2617         sqlite3CloseSavepoints(db);
2618         db->autoCommit = 1;
2619         p->nChange = 0;
2620       }
2621     }
2622 
2623     /* If eStatementOp is non-zero, then a statement transaction needs to
2624     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2625     ** do so. If this operation returns an error, and the current statement
2626     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2627     ** current statement error code.
2628     */
2629     if( eStatementOp ){
2630       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
2631       if( rc ){
2632         if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
2633           p->rc = rc;
2634           sqlite3DbFree(db, p->zErrMsg);
2635           p->zErrMsg = 0;
2636         }
2637         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2638         sqlite3CloseSavepoints(db);
2639         db->autoCommit = 1;
2640         p->nChange = 0;
2641       }
2642     }
2643 
2644     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2645     ** has been rolled back, update the database connection change-counter.
2646     */
2647     if( p->changeCntOn ){
2648       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
2649         sqlite3VdbeSetChanges(db, p->nChange);
2650       }else{
2651         sqlite3VdbeSetChanges(db, 0);
2652       }
2653       p->nChange = 0;
2654     }
2655 
2656     /* Release the locks */
2657     sqlite3VdbeLeave(p);
2658   }
2659 
2660   /* We have successfully halted and closed the VM.  Record this fact. */
2661   if( p->pc>=0 ){
2662     db->nVdbeActive--;
2663     if( !p->readOnly ) db->nVdbeWrite--;
2664     if( p->bIsReader ) db->nVdbeRead--;
2665     assert( db->nVdbeActive>=db->nVdbeRead );
2666     assert( db->nVdbeRead>=db->nVdbeWrite );
2667     assert( db->nVdbeWrite>=0 );
2668   }
2669   p->magic = VDBE_MAGIC_HALT;
2670   checkActiveVdbeCnt(db);
2671   if( p->db->mallocFailed ){
2672     p->rc = SQLITE_NOMEM;
2673   }
2674 
2675   /* If the auto-commit flag is set to true, then any locks that were held
2676   ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2677   ** to invoke any required unlock-notify callbacks.
2678   */
2679   if( db->autoCommit ){
2680     sqlite3ConnectionUnlocked(db);
2681   }
2682 
2683   assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
2684   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
2685 }
2686 
2687 
2688 /*
2689 ** Each VDBE holds the result of the most recent sqlite3_step() call
2690 ** in p->rc.  This routine sets that result back to SQLITE_OK.
2691 */
2692 void sqlite3VdbeResetStepResult(Vdbe *p){
2693   p->rc = SQLITE_OK;
2694 }
2695 
2696 /*
2697 ** Copy the error code and error message belonging to the VDBE passed
2698 ** as the first argument to its database handle (so that they will be
2699 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2700 **
2701 ** This function does not clear the VDBE error code or message, just
2702 ** copies them to the database handle.
2703 */
2704 int sqlite3VdbeTransferError(Vdbe *p){
2705   sqlite3 *db = p->db;
2706   int rc = p->rc;
2707   if( p->zErrMsg ){
2708     u8 mallocFailed = db->mallocFailed;
2709     sqlite3BeginBenignMalloc();
2710     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
2711     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2712     sqlite3EndBenignMalloc();
2713     db->mallocFailed = mallocFailed;
2714     db->errCode = rc;
2715   }else{
2716     sqlite3Error(db, rc);
2717   }
2718   return rc;
2719 }
2720 
2721 #ifdef SQLITE_ENABLE_SQLLOG
2722 /*
2723 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2724 ** invoke it.
2725 */
2726 static void vdbeInvokeSqllog(Vdbe *v){
2727   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2728     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2729     assert( v->db->init.busy==0 );
2730     if( zExpanded ){
2731       sqlite3GlobalConfig.xSqllog(
2732           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2733       );
2734       sqlite3DbFree(v->db, zExpanded);
2735     }
2736   }
2737 }
2738 #else
2739 # define vdbeInvokeSqllog(x)
2740 #endif
2741 
2742 /*
2743 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
2744 ** Write any error messages into *pzErrMsg.  Return the result code.
2745 **
2746 ** After this routine is run, the VDBE should be ready to be executed
2747 ** again.
2748 **
2749 ** To look at it another way, this routine resets the state of the
2750 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2751 ** VDBE_MAGIC_INIT.
2752 */
2753 int sqlite3VdbeReset(Vdbe *p){
2754   sqlite3 *db;
2755   db = p->db;
2756 
2757   /* If the VM did not run to completion or if it encountered an
2758   ** error, then it might not have been halted properly.  So halt
2759   ** it now.
2760   */
2761   sqlite3VdbeHalt(p);
2762 
2763   /* If the VDBE has be run even partially, then transfer the error code
2764   ** and error message from the VDBE into the main database structure.  But
2765   ** if the VDBE has just been set to run but has not actually executed any
2766   ** instructions yet, leave the main database error information unchanged.
2767   */
2768   if( p->pc>=0 ){
2769     vdbeInvokeSqllog(p);
2770     sqlite3VdbeTransferError(p);
2771     sqlite3DbFree(db, p->zErrMsg);
2772     p->zErrMsg = 0;
2773     if( p->runOnlyOnce ) p->expired = 1;
2774   }else if( p->rc && p->expired ){
2775     /* The expired flag was set on the VDBE before the first call
2776     ** to sqlite3_step(). For consistency (since sqlite3_step() was
2777     ** called), set the database error in this case as well.
2778     */
2779     sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
2780     sqlite3DbFree(db, p->zErrMsg);
2781     p->zErrMsg = 0;
2782   }
2783 
2784   /* Reclaim all memory used by the VDBE
2785   */
2786   Cleanup(p);
2787 
2788   /* Save profiling information from this VDBE run.
2789   */
2790 #ifdef VDBE_PROFILE
2791   {
2792     FILE *out = fopen("vdbe_profile.out", "a");
2793     if( out ){
2794       int i;
2795       fprintf(out, "---- ");
2796       for(i=0; i<p->nOp; i++){
2797         fprintf(out, "%02x", p->aOp[i].opcode);
2798       }
2799       fprintf(out, "\n");
2800       if( p->zSql ){
2801         char c, pc = 0;
2802         fprintf(out, "-- ");
2803         for(i=0; (c = p->zSql[i])!=0; i++){
2804           if( pc=='\n' ) fprintf(out, "-- ");
2805           putc(c, out);
2806           pc = c;
2807         }
2808         if( pc!='\n' ) fprintf(out, "\n");
2809       }
2810       for(i=0; i<p->nOp; i++){
2811         char zHdr[100];
2812         sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
2813            p->aOp[i].cnt,
2814            p->aOp[i].cycles,
2815            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2816         );
2817         fprintf(out, "%s", zHdr);
2818         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
2819       }
2820       fclose(out);
2821     }
2822   }
2823 #endif
2824   p->iCurrentTime = 0;
2825   p->magic = VDBE_MAGIC_INIT;
2826   return p->rc & db->errMask;
2827 }
2828 
2829 /*
2830 ** Clean up and delete a VDBE after execution.  Return an integer which is
2831 ** the result code.  Write any error message text into *pzErrMsg.
2832 */
2833 int sqlite3VdbeFinalize(Vdbe *p){
2834   int rc = SQLITE_OK;
2835   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
2836     rc = sqlite3VdbeReset(p);
2837     assert( (rc & p->db->errMask)==rc );
2838   }
2839   sqlite3VdbeDelete(p);
2840   return rc;
2841 }
2842 
2843 /*
2844 ** If parameter iOp is less than zero, then invoke the destructor for
2845 ** all auxiliary data pointers currently cached by the VM passed as
2846 ** the first argument.
2847 **
2848 ** Or, if iOp is greater than or equal to zero, then the destructor is
2849 ** only invoked for those auxiliary data pointers created by the user
2850 ** function invoked by the OP_Function opcode at instruction iOp of
2851 ** VM pVdbe, and only then if:
2852 **
2853 **    * the associated function parameter is the 32nd or later (counting
2854 **      from left to right), or
2855 **
2856 **    * the corresponding bit in argument mask is clear (where the first
2857 **      function parameter corresponds to bit 0 etc.).
2858 */
2859 void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2860   AuxData **pp = &pVdbe->pAuxData;
2861   while( *pp ){
2862     AuxData *pAux = *pp;
2863     if( (iOp<0)
2864      || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
2865     ){
2866       testcase( pAux->iArg==31 );
2867       if( pAux->xDelete ){
2868         pAux->xDelete(pAux->pAux);
2869       }
2870       *pp = pAux->pNext;
2871       sqlite3DbFree(pVdbe->db, pAux);
2872     }else{
2873       pp= &pAux->pNext;
2874     }
2875   }
2876 }
2877 
2878 /*
2879 ** Free all memory associated with the Vdbe passed as the second argument,
2880 ** except for object itself, which is preserved.
2881 **
2882 ** The difference between this function and sqlite3VdbeDelete() is that
2883 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
2884 ** the database connection and frees the object itself.
2885 */
2886 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
2887   SubProgram *pSub, *pNext;
2888   int i;
2889   assert( p->db==0 || p->db==db );
2890   releaseMemArray(p->aVar, p->nVar);
2891   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
2892   for(pSub=p->pProgram; pSub; pSub=pNext){
2893     pNext = pSub->pNext;
2894     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2895     sqlite3DbFree(db, pSub);
2896   }
2897   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
2898   sqlite3DbFree(db, p->azVar);
2899   vdbeFreeOpArray(db, p->aOp, p->nOp);
2900   sqlite3DbFree(db, p->aColName);
2901   sqlite3DbFree(db, p->zSql);
2902   sqlite3DbFree(db, p->pFree);
2903 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2904   for(i=0; i<p->nScan; i++){
2905     sqlite3DbFree(db, p->aScan[i].zName);
2906   }
2907   sqlite3DbFree(db, p->aScan);
2908 #endif
2909 }
2910 
2911 /*
2912 ** Delete an entire VDBE.
2913 */
2914 void sqlite3VdbeDelete(Vdbe *p){
2915   sqlite3 *db;
2916 
2917   if( NEVER(p==0) ) return;
2918   db = p->db;
2919   assert( sqlite3_mutex_held(db->mutex) );
2920   sqlite3VdbeClearObject(db, p);
2921   if( p->pPrev ){
2922     p->pPrev->pNext = p->pNext;
2923   }else{
2924     assert( db->pVdbe==p );
2925     db->pVdbe = p->pNext;
2926   }
2927   if( p->pNext ){
2928     p->pNext->pPrev = p->pPrev;
2929   }
2930   p->magic = VDBE_MAGIC_DEAD;
2931   p->db = 0;
2932   sqlite3DbFree(db, p);
2933 }
2934 
2935 /*
2936 ** The cursor "p" has a pending seek operation that has not yet been
2937 ** carried out.  Seek the cursor now.  If an error occurs, return
2938 ** the appropriate error code.
2939 */
2940 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
2941   int res, rc;
2942 #ifdef SQLITE_TEST
2943   extern int sqlite3_search_count;
2944 #endif
2945   assert( p->deferredMoveto );
2946   assert( p->isTable );
2947   assert( p->eCurType==CURTYPE_BTREE );
2948   rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
2949   if( rc ) return rc;
2950   if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2951 #ifdef SQLITE_TEST
2952   sqlite3_search_count++;
2953 #endif
2954   p->deferredMoveto = 0;
2955   p->cacheStatus = CACHE_STALE;
2956   return SQLITE_OK;
2957 }
2958 
2959 /*
2960 ** Something has moved cursor "p" out of place.  Maybe the row it was
2961 ** pointed to was deleted out from under it.  Or maybe the btree was
2962 ** rebalanced.  Whatever the cause, try to restore "p" to the place it
2963 ** is supposed to be pointing.  If the row was deleted out from under the
2964 ** cursor, set the cursor to point to a NULL row.
2965 */
2966 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
2967   int isDifferentRow, rc;
2968   assert( p->eCurType==CURTYPE_BTREE );
2969   assert( p->uc.pCursor!=0 );
2970   assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
2971   rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
2972   p->cacheStatus = CACHE_STALE;
2973   if( isDifferentRow ) p->nullRow = 1;
2974   return rc;
2975 }
2976 
2977 /*
2978 ** Check to ensure that the cursor is valid.  Restore the cursor
2979 ** if need be.  Return any I/O error from the restore operation.
2980 */
2981 int sqlite3VdbeCursorRestore(VdbeCursor *p){
2982   assert( p->eCurType==CURTYPE_BTREE );
2983   if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
2984     return handleMovedCursor(p);
2985   }
2986   return SQLITE_OK;
2987 }
2988 
2989 /*
2990 ** Make sure the cursor p is ready to read or write the row to which it
2991 ** was last positioned.  Return an error code if an OOM fault or I/O error
2992 ** prevents us from positioning the cursor to its correct position.
2993 **
2994 ** If a MoveTo operation is pending on the given cursor, then do that
2995 ** MoveTo now.  If no move is pending, check to see if the row has been
2996 ** deleted out from under the cursor and if it has, mark the row as
2997 ** a NULL row.
2998 **
2999 ** If the cursor is already pointing to the correct row and that row has
3000 ** not been deleted out from under the cursor, then this routine is a no-op.
3001 */
3002 int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
3003   VdbeCursor *p = *pp;
3004   if( p->eCurType==CURTYPE_BTREE ){
3005     if( p->deferredMoveto ){
3006       int iMap;
3007       if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
3008         *pp = p->pAltCursor;
3009         *piCol = iMap - 1;
3010         return SQLITE_OK;
3011       }
3012       return handleDeferredMoveto(p);
3013     }
3014     if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
3015       return handleMovedCursor(p);
3016     }
3017   }
3018   return SQLITE_OK;
3019 }
3020 
3021 /*
3022 ** The following functions:
3023 **
3024 ** sqlite3VdbeSerialType()
3025 ** sqlite3VdbeSerialTypeLen()
3026 ** sqlite3VdbeSerialLen()
3027 ** sqlite3VdbeSerialPut()
3028 ** sqlite3VdbeSerialGet()
3029 **
3030 ** encapsulate the code that serializes values for storage in SQLite
3031 ** data and index records. Each serialized value consists of a
3032 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
3033 ** integer, stored as a varint.
3034 **
3035 ** In an SQLite index record, the serial type is stored directly before
3036 ** the blob of data that it corresponds to. In a table record, all serial
3037 ** types are stored at the start of the record, and the blobs of data at
3038 ** the end. Hence these functions allow the caller to handle the
3039 ** serial-type and data blob separately.
3040 **
3041 ** The following table describes the various storage classes for data:
3042 **
3043 **   serial type        bytes of data      type
3044 **   --------------     ---------------    ---------------
3045 **      0                     0            NULL
3046 **      1                     1            signed integer
3047 **      2                     2            signed integer
3048 **      3                     3            signed integer
3049 **      4                     4            signed integer
3050 **      5                     6            signed integer
3051 **      6                     8            signed integer
3052 **      7                     8            IEEE float
3053 **      8                     0            Integer constant 0
3054 **      9                     0            Integer constant 1
3055 **     10,11                               reserved for expansion
3056 **    N>=12 and even       (N-12)/2        BLOB
3057 **    N>=13 and odd        (N-13)/2        text
3058 **
3059 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
3060 ** of SQLite will not understand those serial types.
3061 */
3062 
3063 /*
3064 ** Return the serial-type for the value stored in pMem.
3065 */
3066 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
3067   int flags = pMem->flags;
3068   u32 n;
3069 
3070   assert( pLen!=0 );
3071   if( flags&MEM_Null ){
3072     *pLen = 0;
3073     return 0;
3074   }
3075   if( flags&MEM_Int ){
3076     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3077 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
3078     i64 i = pMem->u.i;
3079     u64 u;
3080     if( i<0 ){
3081       u = ~i;
3082     }else{
3083       u = i;
3084     }
3085     if( u<=127 ){
3086       if( (i&1)==i && file_format>=4 ){
3087         *pLen = 0;
3088         return 8+(u32)u;
3089       }else{
3090         *pLen = 1;
3091         return 1;
3092       }
3093     }
3094     if( u<=32767 ){ *pLen = 2; return 2; }
3095     if( u<=8388607 ){ *pLen = 3; return 3; }
3096     if( u<=2147483647 ){ *pLen = 4; return 4; }
3097     if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
3098     *pLen = 8;
3099     return 6;
3100   }
3101   if( flags&MEM_Real ){
3102     *pLen = 8;
3103     return 7;
3104   }
3105   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
3106   assert( pMem->n>=0 );
3107   n = (u32)pMem->n;
3108   if( flags & MEM_Zero ){
3109     n += pMem->u.nZero;
3110   }
3111   *pLen = n;
3112   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
3113 }
3114 
3115 /*
3116 ** The sizes for serial types less than 128
3117 */
3118 static const u8 sqlite3SmallTypeSizes[] = {
3119         /*  0   1   2   3   4   5   6   7   8   9 */
3120 /*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
3121 /*  10 */   0,  0,  0,  0,  1,  1,  2,  2,  3,  3,
3122 /*  20 */   4,  4,  5,  5,  6,  6,  7,  7,  8,  8,
3123 /*  30 */   9,  9, 10, 10, 11, 11, 12, 12, 13, 13,
3124 /*  40 */  14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
3125 /*  50 */  19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
3126 /*  60 */  24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
3127 /*  70 */  29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
3128 /*  80 */  34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
3129 /*  90 */  39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
3130 /* 100 */  44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
3131 /* 110 */  49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
3132 /* 120 */  54, 54, 55, 55, 56, 56, 57, 57
3133 };
3134 
3135 /*
3136 ** Return the length of the data corresponding to the supplied serial-type.
3137 */
3138 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
3139   if( serial_type>=128 ){
3140     return (serial_type-12)/2;
3141   }else{
3142     assert( serial_type<12
3143             || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
3144     return sqlite3SmallTypeSizes[serial_type];
3145   }
3146 }
3147 u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
3148   assert( serial_type<128 );
3149   return sqlite3SmallTypeSizes[serial_type];
3150 }
3151 
3152 /*
3153 ** If we are on an architecture with mixed-endian floating
3154 ** points (ex: ARM7) then swap the lower 4 bytes with the
3155 ** upper 4 bytes.  Return the result.
3156 **
3157 ** For most architectures, this is a no-op.
3158 **
3159 ** (later):  It is reported to me that the mixed-endian problem
3160 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
3161 ** that early versions of GCC stored the two words of a 64-bit
3162 ** float in the wrong order.  And that error has been propagated
3163 ** ever since.  The blame is not necessarily with GCC, though.
3164 ** GCC might have just copying the problem from a prior compiler.
3165 ** I am also told that newer versions of GCC that follow a different
3166 ** ABI get the byte order right.
3167 **
3168 ** Developers using SQLite on an ARM7 should compile and run their
3169 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
3170 ** enabled, some asserts below will ensure that the byte order of
3171 ** floating point values is correct.
3172 **
3173 ** (2007-08-30)  Frank van Vugt has studied this problem closely
3174 ** and has send his findings to the SQLite developers.  Frank
3175 ** writes that some Linux kernels offer floating point hardware
3176 ** emulation that uses only 32-bit mantissas instead of a full
3177 ** 48-bits as required by the IEEE standard.  (This is the
3178 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
3179 ** byte swapping becomes very complicated.  To avoid problems,
3180 ** the necessary byte swapping is carried out using a 64-bit integer
3181 ** rather than a 64-bit float.  Frank assures us that the code here
3182 ** works for him.  We, the developers, have no way to independently
3183 ** verify this, but Frank seems to know what he is talking about
3184 ** so we trust him.
3185 */
3186 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
3187 static u64 floatSwap(u64 in){
3188   union {
3189     u64 r;
3190     u32 i[2];
3191   } u;
3192   u32 t;
3193 
3194   u.r = in;
3195   t = u.i[0];
3196   u.i[0] = u.i[1];
3197   u.i[1] = t;
3198   return u.r;
3199 }
3200 # define swapMixedEndianFloat(X)  X = floatSwap(X)
3201 #else
3202 # define swapMixedEndianFloat(X)
3203 #endif
3204 
3205 /*
3206 ** Write the serialized data blob for the value stored in pMem into
3207 ** buf. It is assumed that the caller has allocated sufficient space.
3208 ** Return the number of bytes written.
3209 **
3210 ** nBuf is the amount of space left in buf[].  The caller is responsible
3211 ** for allocating enough space to buf[] to hold the entire field, exclusive
3212 ** of the pMem->u.nZero bytes for a MEM_Zero value.
3213 **
3214 ** Return the number of bytes actually written into buf[].  The number
3215 ** of bytes in the zero-filled tail is included in the return value only
3216 ** if those bytes were zeroed in buf[].
3217 */
3218 u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
3219   u32 len;
3220 
3221   /* Integer and Real */
3222   if( serial_type<=7 && serial_type>0 ){
3223     u64 v;
3224     u32 i;
3225     if( serial_type==7 ){
3226       assert( sizeof(v)==sizeof(pMem->u.r) );
3227       memcpy(&v, &pMem->u.r, sizeof(v));
3228       swapMixedEndianFloat(v);
3229     }else{
3230       v = pMem->u.i;
3231     }
3232     len = i = sqlite3SmallTypeSizes[serial_type];
3233     assert( i>0 );
3234     do{
3235       buf[--i] = (u8)(v&0xFF);
3236       v >>= 8;
3237     }while( i );
3238     return len;
3239   }
3240 
3241   /* String or blob */
3242   if( serial_type>=12 ){
3243     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
3244              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
3245     len = pMem->n;
3246     if( len>0 ) memcpy(buf, pMem->z, len);
3247     return len;
3248   }
3249 
3250   /* NULL or constants 0 or 1 */
3251   return 0;
3252 }
3253 
3254 /* Input "x" is a sequence of unsigned characters that represent a
3255 ** big-endian integer.  Return the equivalent native integer
3256 */
3257 #define ONE_BYTE_INT(x)    ((i8)(x)[0])
3258 #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
3259 #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
3260 #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
3261 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
3262 
3263 /*
3264 ** Deserialize the data blob pointed to by buf as serial type serial_type
3265 ** and store the result in pMem.  Return the number of bytes read.
3266 **
3267 ** This function is implemented as two separate routines for performance.
3268 ** The few cases that require local variables are broken out into a separate
3269 ** routine so that in most cases the overhead of moving the stack pointer
3270 ** is avoided.
3271 */
3272 static u32 SQLITE_NOINLINE serialGet(
3273   const unsigned char *buf,     /* Buffer to deserialize from */
3274   u32 serial_type,              /* Serial type to deserialize */
3275   Mem *pMem                     /* Memory cell to write value into */
3276 ){
3277   u64 x = FOUR_BYTE_UINT(buf);
3278   u32 y = FOUR_BYTE_UINT(buf+4);
3279   x = (x<<32) + y;
3280   if( serial_type==6 ){
3281     /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
3282     ** twos-complement integer. */
3283     pMem->u.i = *(i64*)&x;
3284     pMem->flags = MEM_Int;
3285     testcase( pMem->u.i<0 );
3286   }else{
3287     /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
3288     ** floating point number. */
3289 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
3290     /* Verify that integers and floating point values use the same
3291     ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3292     ** defined that 64-bit floating point values really are mixed
3293     ** endian.
3294     */
3295     static const u64 t1 = ((u64)0x3ff00000)<<32;
3296     static const double r1 = 1.0;
3297     u64 t2 = t1;
3298     swapMixedEndianFloat(t2);
3299     assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
3300 #endif
3301     assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
3302     swapMixedEndianFloat(x);
3303     memcpy(&pMem->u.r, &x, sizeof(x));
3304     pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
3305   }
3306   return 8;
3307 }
3308 u32 sqlite3VdbeSerialGet(
3309   const unsigned char *buf,     /* Buffer to deserialize from */
3310   u32 serial_type,              /* Serial type to deserialize */
3311   Mem *pMem                     /* Memory cell to write value into */
3312 ){
3313   switch( serial_type ){
3314     case 10:   /* Reserved for future use */
3315     case 11:   /* Reserved for future use */
3316     case 0: {  /* Null */
3317       /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
3318       pMem->flags = MEM_Null;
3319       break;
3320     }
3321     case 1: {
3322       /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
3323       ** integer. */
3324       pMem->u.i = ONE_BYTE_INT(buf);
3325       pMem->flags = MEM_Int;
3326       testcase( pMem->u.i<0 );
3327       return 1;
3328     }
3329     case 2: { /* 2-byte signed integer */
3330       /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
3331       ** twos-complement integer. */
3332       pMem->u.i = TWO_BYTE_INT(buf);
3333       pMem->flags = MEM_Int;
3334       testcase( pMem->u.i<0 );
3335       return 2;
3336     }
3337     case 3: { /* 3-byte signed integer */
3338       /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
3339       ** twos-complement integer. */
3340       pMem->u.i = THREE_BYTE_INT(buf);
3341       pMem->flags = MEM_Int;
3342       testcase( pMem->u.i<0 );
3343       return 3;
3344     }
3345     case 4: { /* 4-byte signed integer */
3346       /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
3347       ** twos-complement integer. */
3348       pMem->u.i = FOUR_BYTE_INT(buf);
3349 #ifdef __HP_cc
3350       /* Work around a sign-extension bug in the HP compiler for HP/UX */
3351       if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
3352 #endif
3353       pMem->flags = MEM_Int;
3354       testcase( pMem->u.i<0 );
3355       return 4;
3356     }
3357     case 5: { /* 6-byte signed integer */
3358       /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
3359       ** twos-complement integer. */
3360       pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
3361       pMem->flags = MEM_Int;
3362       testcase( pMem->u.i<0 );
3363       return 6;
3364     }
3365     case 6:   /* 8-byte signed integer */
3366     case 7: { /* IEEE floating point */
3367       /* These use local variables, so do them in a separate routine
3368       ** to avoid having to move the frame pointer in the common case */
3369       return serialGet(buf,serial_type,pMem);
3370     }
3371     case 8:    /* Integer 0 */
3372     case 9: {  /* Integer 1 */
3373       /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
3374       /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
3375       pMem->u.i = serial_type-8;
3376       pMem->flags = MEM_Int;
3377       return 0;
3378     }
3379     default: {
3380       /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
3381       ** length.
3382       ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
3383       ** (N-13)/2 bytes in length. */
3384       static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
3385       pMem->z = (char *)buf;
3386       pMem->n = (serial_type-12)/2;
3387       pMem->flags = aFlag[serial_type&1];
3388       return pMem->n;
3389     }
3390   }
3391   return 0;
3392 }
3393 /*
3394 ** This routine is used to allocate sufficient space for an UnpackedRecord
3395 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3396 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
3397 **
3398 ** The space is either allocated using sqlite3DbMallocRaw() or from within
3399 ** the unaligned buffer passed via the second and third arguments (presumably
3400 ** stack space). If the former, then *ppFree is set to a pointer that should
3401 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3402 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3403 ** before returning.
3404 **
3405 ** If an OOM error occurs, NULL is returned.
3406 */
3407 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3408   KeyInfo *pKeyInfo,              /* Description of the record */
3409   char *pSpace,                   /* Unaligned space available */
3410   int szSpace,                    /* Size of pSpace[] in bytes */
3411   char **ppFree                   /* OUT: Caller should free this pointer */
3412 ){
3413   UnpackedRecord *p;              /* Unpacked record to return */
3414   int nOff;                       /* Increment pSpace by nOff to align it */
3415   int nByte;                      /* Number of bytes required for *p */
3416 
3417   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
3418   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3419   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
3420   */
3421   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
3422   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
3423   if( nByte>szSpace+nOff ){
3424     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3425     *ppFree = (char *)p;
3426     if( !p ) return 0;
3427   }else{
3428     p = (UnpackedRecord*)&pSpace[nOff];
3429     *ppFree = 0;
3430   }
3431 
3432   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
3433   assert( pKeyInfo->aSortOrder!=0 );
3434   p->pKeyInfo = pKeyInfo;
3435   p->nField = pKeyInfo->nField + 1;
3436   return p;
3437 }
3438 
3439 /*
3440 ** Given the nKey-byte encoding of a record in pKey[], populate the
3441 ** UnpackedRecord structure indicated by the fourth argument with the
3442 ** contents of the decoded record.
3443 */
3444 void sqlite3VdbeRecordUnpack(
3445   KeyInfo *pKeyInfo,     /* Information about the record format */
3446   int nKey,              /* Size of the binary record */
3447   const void *pKey,      /* The binary record */
3448   UnpackedRecord *p      /* Populate this structure before returning. */
3449 ){
3450   const unsigned char *aKey = (const unsigned char *)pKey;
3451   int d;
3452   u32 idx;                        /* Offset in aKey[] to read from */
3453   u16 u;                          /* Unsigned loop counter */
3454   u32 szHdr;
3455   Mem *pMem = p->aMem;
3456 
3457   p->default_rc = 0;
3458   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
3459   idx = getVarint32(aKey, szHdr);
3460   d = szHdr;
3461   u = 0;
3462   while( idx<szHdr && d<=nKey ){
3463     u32 serial_type;
3464 
3465     idx += getVarint32(&aKey[idx], serial_type);
3466     pMem->enc = pKeyInfo->enc;
3467     pMem->db = pKeyInfo->db;
3468     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
3469     pMem->szMalloc = 0;
3470     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
3471     pMem++;
3472     if( (++u)>=p->nField ) break;
3473   }
3474   assert( u<=pKeyInfo->nField + 1 );
3475   p->nField = u;
3476 }
3477 
3478 #if SQLITE_DEBUG
3479 /*
3480 ** This function compares two index or table record keys in the same way
3481 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3482 ** this function deserializes and compares values using the
3483 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3484 ** in assert() statements to ensure that the optimized code in
3485 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
3486 **
3487 ** Return true if the result of comparison is equivalent to desiredResult.
3488 ** Return false if there is a disagreement.
3489 */
3490 static int vdbeRecordCompareDebug(
3491   int nKey1, const void *pKey1, /* Left key */
3492   const UnpackedRecord *pPKey2, /* Right key */
3493   int desiredResult             /* Correct answer */
3494 ){
3495   u32 d1;            /* Offset into aKey[] of next data element */
3496   u32 idx1;          /* Offset into aKey[] of next header element */
3497   u32 szHdr1;        /* Number of bytes in header */
3498   int i = 0;
3499   int rc = 0;
3500   const unsigned char *aKey1 = (const unsigned char *)pKey1;
3501   KeyInfo *pKeyInfo;
3502   Mem mem1;
3503 
3504   pKeyInfo = pPKey2->pKeyInfo;
3505   if( pKeyInfo->db==0 ) return 1;
3506   mem1.enc = pKeyInfo->enc;
3507   mem1.db = pKeyInfo->db;
3508   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
3509   VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
3510 
3511   /* Compilers may complain that mem1.u.i is potentially uninitialized.
3512   ** We could initialize it, as shown here, to silence those complaints.
3513   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3514   ** the unnecessary initialization has a measurable negative performance
3515   ** impact, since this routine is a very high runner.  And so, we choose
3516   ** to ignore the compiler warnings and leave this variable uninitialized.
3517   */
3518   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
3519 
3520   idx1 = getVarint32(aKey1, szHdr1);
3521   if( szHdr1>98307 ) return SQLITE_CORRUPT;
3522   d1 = szHdr1;
3523   assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3524   assert( pKeyInfo->aSortOrder!=0 );
3525   assert( pKeyInfo->nField>0 );
3526   assert( idx1<=szHdr1 || CORRUPT_DB );
3527   do{
3528     u32 serial_type1;
3529 
3530     /* Read the serial types for the next element in each key. */
3531     idx1 += getVarint32( aKey1+idx1, serial_type1 );
3532 
3533     /* Verify that there is enough key space remaining to avoid
3534     ** a buffer overread.  The "d1+serial_type1+2" subexpression will
3535     ** always be greater than or equal to the amount of required key space.
3536     ** Use that approximation to avoid the more expensive call to
3537     ** sqlite3VdbeSerialTypeLen() in the common case.
3538     */
3539     if( d1+serial_type1+2>(u32)nKey1
3540      && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3541     ){
3542       break;
3543     }
3544 
3545     /* Extract the values to be compared.
3546     */
3547     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
3548 
3549     /* Do the comparison
3550     */
3551     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3552     if( rc!=0 ){
3553       assert( mem1.szMalloc==0 );  /* See comment below */
3554       if( pKeyInfo->aSortOrder[i] ){
3555         rc = -rc;  /* Invert the result for DESC sort order. */
3556       }
3557       goto debugCompareEnd;
3558     }
3559     i++;
3560   }while( idx1<szHdr1 && i<pPKey2->nField );
3561 
3562   /* No memory allocation is ever used on mem1.  Prove this using
3563   ** the following assert().  If the assert() fails, it indicates a
3564   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3565   */
3566   assert( mem1.szMalloc==0 );
3567 
3568   /* rc==0 here means that one of the keys ran out of fields and
3569   ** all the fields up to that point were equal. Return the default_rc
3570   ** value.  */
3571   rc = pPKey2->default_rc;
3572 
3573 debugCompareEnd:
3574   if( desiredResult==0 && rc==0 ) return 1;
3575   if( desiredResult<0 && rc<0 ) return 1;
3576   if( desiredResult>0 && rc>0 ) return 1;
3577   if( CORRUPT_DB ) return 1;
3578   if( pKeyInfo->db->mallocFailed ) return 1;
3579   return 0;
3580 }
3581 #endif
3582 
3583 #if SQLITE_DEBUG
3584 /*
3585 ** Count the number of fields (a.k.a. columns) in the record given by
3586 ** pKey,nKey.  The verify that this count is less than or equal to the
3587 ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
3588 **
3589 ** If this constraint is not satisfied, it means that the high-speed
3590 ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
3591 ** not work correctly.  If this assert() ever fires, it probably means
3592 ** that the KeyInfo.nField or KeyInfo.nXField values were computed
3593 ** incorrectly.
3594 */
3595 static void vdbeAssertFieldCountWithinLimits(
3596   int nKey, const void *pKey,   /* The record to verify */
3597   const KeyInfo *pKeyInfo       /* Compare size with this KeyInfo */
3598 ){
3599   int nField = 0;
3600   u32 szHdr;
3601   u32 idx;
3602   u32 notUsed;
3603   const unsigned char *aKey = (const unsigned char*)pKey;
3604 
3605   if( CORRUPT_DB ) return;
3606   idx = getVarint32(aKey, szHdr);
3607   assert( nKey>=0 );
3608   assert( szHdr<=(u32)nKey );
3609   while( idx<szHdr ){
3610     idx += getVarint32(aKey+idx, notUsed);
3611     nField++;
3612   }
3613   assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
3614 }
3615 #else
3616 # define vdbeAssertFieldCountWithinLimits(A,B,C)
3617 #endif
3618 
3619 /*
3620 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
3621 ** using the collation sequence pColl. As usual, return a negative , zero
3622 ** or positive value if *pMem1 is less than, equal to or greater than
3623 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3624 */
3625 static int vdbeCompareMemString(
3626   const Mem *pMem1,
3627   const Mem *pMem2,
3628   const CollSeq *pColl,
3629   u8 *prcErr                      /* If an OOM occurs, set to SQLITE_NOMEM */
3630 ){
3631   if( pMem1->enc==pColl->enc ){
3632     /* The strings are already in the correct encoding.  Call the
3633      ** comparison function directly */
3634     return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3635   }else{
3636     int rc;
3637     const void *v1, *v2;
3638     int n1, n2;
3639     Mem c1;
3640     Mem c2;
3641     sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
3642     sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
3643     sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3644     sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3645     v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3646     n1 = v1==0 ? 0 : c1.n;
3647     v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3648     n2 = v2==0 ? 0 : c2.n;
3649     rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3650     if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
3651     sqlite3VdbeMemRelease(&c1);
3652     sqlite3VdbeMemRelease(&c2);
3653     return rc;
3654   }
3655 }
3656 
3657 /*
3658 ** Compare two blobs.  Return negative, zero, or positive if the first
3659 ** is less than, equal to, or greater than the second, respectively.
3660 ** If one blob is a prefix of the other, then the shorter is the lessor.
3661 */
3662 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
3663   int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
3664   if( c ) return c;
3665   return pB1->n - pB2->n;
3666 }
3667 
3668 /*
3669 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
3670 ** number.  Return negative, zero, or positive if the first (i64) is less than,
3671 ** equal to, or greater than the second (double).
3672 */
3673 static int sqlite3IntFloatCompare(i64 i, double r){
3674   if( sizeof(LONGDOUBLE_TYPE)>8 ){
3675     LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
3676     if( x<r ) return -1;
3677     if( x>r ) return +1;
3678     return 0;
3679   }else{
3680     i64 y;
3681     double s;
3682     if( r<-9223372036854775808.0 ) return +1;
3683     if( r>9223372036854775807.0 ) return -1;
3684     y = (i64)r;
3685     if( i<y ) return -1;
3686     if( i>y ){
3687       if( y==SMALLEST_INT64 && r>0.0 ) return -1;
3688       return +1;
3689     }
3690     s = (double)i;
3691     if( s<r ) return -1;
3692     if( s>r ) return +1;
3693     return 0;
3694   }
3695 }
3696 
3697 /*
3698 ** Compare the values contained by the two memory cells, returning
3699 ** negative, zero or positive if pMem1 is less than, equal to, or greater
3700 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3701 ** and reals) sorted numerically, followed by text ordered by the collating
3702 ** sequence pColl and finally blob's ordered by memcmp().
3703 **
3704 ** Two NULL values are considered equal by this function.
3705 */
3706 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3707   int f1, f2;
3708   int combined_flags;
3709 
3710   f1 = pMem1->flags;
3711   f2 = pMem2->flags;
3712   combined_flags = f1|f2;
3713   assert( (combined_flags & MEM_RowSet)==0 );
3714 
3715   /* If one value is NULL, it is less than the other. If both values
3716   ** are NULL, return 0.
3717   */
3718   if( combined_flags&MEM_Null ){
3719     return (f2&MEM_Null) - (f1&MEM_Null);
3720   }
3721 
3722   /* At least one of the two values is a number
3723   */
3724   if( combined_flags&(MEM_Int|MEM_Real) ){
3725     if( (f1 & f2 & MEM_Int)!=0 ){
3726       if( pMem1->u.i < pMem2->u.i ) return -1;
3727       if( pMem1->u.i > pMem2->u.i ) return +1;
3728       return 0;
3729     }
3730     if( (f1 & f2 & MEM_Real)!=0 ){
3731       if( pMem1->u.r < pMem2->u.r ) return -1;
3732       if( pMem1->u.r > pMem2->u.r ) return +1;
3733       return 0;
3734     }
3735     if( (f1&MEM_Int)!=0 ){
3736       if( (f2&MEM_Real)!=0 ){
3737         return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
3738       }else{
3739         return -1;
3740       }
3741     }
3742     if( (f1&MEM_Real)!=0 ){
3743       if( (f2&MEM_Int)!=0 ){
3744         return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
3745       }else{
3746         return -1;
3747       }
3748     }
3749     return +1;
3750   }
3751 
3752   /* If one value is a string and the other is a blob, the string is less.
3753   ** If both are strings, compare using the collating functions.
3754   */
3755   if( combined_flags&MEM_Str ){
3756     if( (f1 & MEM_Str)==0 ){
3757       return 1;
3758     }
3759     if( (f2 & MEM_Str)==0 ){
3760       return -1;
3761     }
3762 
3763     assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
3764     assert( pMem1->enc==SQLITE_UTF8 ||
3765             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3766 
3767     /* The collation sequence must be defined at this point, even if
3768     ** the user deletes the collation sequence after the vdbe program is
3769     ** compiled (this was not always the case).
3770     */
3771     assert( !pColl || pColl->xCmp );
3772 
3773     if( pColl ){
3774       return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
3775     }
3776     /* If a NULL pointer was passed as the collate function, fall through
3777     ** to the blob case and use memcmp().  */
3778   }
3779 
3780   /* Both values must be blobs.  Compare using memcmp().  */
3781   return sqlite3BlobCompare(pMem1, pMem2);
3782 }
3783 
3784 
3785 /*
3786 ** The first argument passed to this function is a serial-type that
3787 ** corresponds to an integer - all values between 1 and 9 inclusive
3788 ** except 7. The second points to a buffer containing an integer value
3789 ** serialized according to serial_type. This function deserializes
3790 ** and returns the value.
3791 */
3792 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
3793   u32 y;
3794   assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
3795   switch( serial_type ){
3796     case 0:
3797     case 1:
3798       testcase( aKey[0]&0x80 );
3799       return ONE_BYTE_INT(aKey);
3800     case 2:
3801       testcase( aKey[0]&0x80 );
3802       return TWO_BYTE_INT(aKey);
3803     case 3:
3804       testcase( aKey[0]&0x80 );
3805       return THREE_BYTE_INT(aKey);
3806     case 4: {
3807       testcase( aKey[0]&0x80 );
3808       y = FOUR_BYTE_UINT(aKey);
3809       return (i64)*(int*)&y;
3810     }
3811     case 5: {
3812       testcase( aKey[0]&0x80 );
3813       return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
3814     }
3815     case 6: {
3816       u64 x = FOUR_BYTE_UINT(aKey);
3817       testcase( aKey[0]&0x80 );
3818       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3819       return (i64)*(i64*)&x;
3820     }
3821   }
3822 
3823   return (serial_type - 8);
3824 }
3825 
3826 /*
3827 ** This function compares the two table rows or index records
3828 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
3829 ** or positive integer if key1 is less than, equal to or
3830 ** greater than key2.  The {nKey1, pKey1} key must be a blob
3831 ** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
3832 ** key must be a parsed key such as obtained from
3833 ** sqlite3VdbeParseRecord.
3834 **
3835 ** If argument bSkip is non-zero, it is assumed that the caller has already
3836 ** determined that the first fields of the keys are equal.
3837 **
3838 ** Key1 and Key2 do not have to contain the same number of fields. If all
3839 ** fields that appear in both keys are equal, then pPKey2->default_rc is
3840 ** returned.
3841 **
3842 ** If database corruption is discovered, set pPKey2->errCode to
3843 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
3844 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
3845 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
3846 */
3847 int sqlite3VdbeRecordCompareWithSkip(
3848   int nKey1, const void *pKey1,   /* Left key */
3849   UnpackedRecord *pPKey2,         /* Right key */
3850   int bSkip                       /* If true, skip the first field */
3851 ){
3852   u32 d1;                         /* Offset into aKey[] of next data element */
3853   int i;                          /* Index of next field to compare */
3854   u32 szHdr1;                     /* Size of record header in bytes */
3855   u32 idx1;                       /* Offset of first type in header */
3856   int rc = 0;                     /* Return value */
3857   Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
3858   KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3859   const unsigned char *aKey1 = (const unsigned char *)pKey1;
3860   Mem mem1;
3861 
3862   /* If bSkip is true, then the caller has already determined that the first
3863   ** two elements in the keys are equal. Fix the various stack variables so
3864   ** that this routine begins comparing at the second field. */
3865   if( bSkip ){
3866     u32 s1;
3867     idx1 = 1 + getVarint32(&aKey1[1], s1);
3868     szHdr1 = aKey1[0];
3869     d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
3870     i = 1;
3871     pRhs++;
3872   }else{
3873     idx1 = getVarint32(aKey1, szHdr1);
3874     d1 = szHdr1;
3875     if( d1>(unsigned)nKey1 ){
3876       pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3877       return 0;  /* Corruption */
3878     }
3879     i = 0;
3880   }
3881 
3882   VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
3883   assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3884        || CORRUPT_DB );
3885   assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3886   assert( pPKey2->pKeyInfo->nField>0 );
3887   assert( idx1<=szHdr1 || CORRUPT_DB );
3888   do{
3889     u32 serial_type;
3890 
3891     /* RHS is an integer */
3892     if( pRhs->flags & MEM_Int ){
3893       serial_type = aKey1[idx1];
3894       testcase( serial_type==12 );
3895       if( serial_type>=10 ){
3896         rc = +1;
3897       }else if( serial_type==0 ){
3898         rc = -1;
3899       }else if( serial_type==7 ){
3900         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3901         rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
3902       }else{
3903         i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3904         i64 rhs = pRhs->u.i;
3905         if( lhs<rhs ){
3906           rc = -1;
3907         }else if( lhs>rhs ){
3908           rc = +1;
3909         }
3910       }
3911     }
3912 
3913     /* RHS is real */
3914     else if( pRhs->flags & MEM_Real ){
3915       serial_type = aKey1[idx1];
3916       if( serial_type>=10 ){
3917         /* Serial types 12 or greater are strings and blobs (greater than
3918         ** numbers). Types 10 and 11 are currently "reserved for future
3919         ** use", so it doesn't really matter what the results of comparing
3920         ** them to numberic values are.  */
3921         rc = +1;
3922       }else if( serial_type==0 ){
3923         rc = -1;
3924       }else{
3925         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3926         if( serial_type==7 ){
3927           if( mem1.u.r<pRhs->u.r ){
3928             rc = -1;
3929           }else if( mem1.u.r>pRhs->u.r ){
3930             rc = +1;
3931           }
3932         }else{
3933           rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
3934         }
3935       }
3936     }
3937 
3938     /* RHS is a string */
3939     else if( pRhs->flags & MEM_Str ){
3940       getVarint32(&aKey1[idx1], serial_type);
3941       testcase( serial_type==12 );
3942       if( serial_type<12 ){
3943         rc = -1;
3944       }else if( !(serial_type & 0x01) ){
3945         rc = +1;
3946       }else{
3947         mem1.n = (serial_type - 12) / 2;
3948         testcase( (d1+mem1.n)==(unsigned)nKey1 );
3949         testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
3950         if( (d1+mem1.n) > (unsigned)nKey1 ){
3951           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3952           return 0;                /* Corruption */
3953         }else if( pKeyInfo->aColl[i] ){
3954           mem1.enc = pKeyInfo->enc;
3955           mem1.db = pKeyInfo->db;
3956           mem1.flags = MEM_Str;
3957           mem1.z = (char*)&aKey1[d1];
3958           rc = vdbeCompareMemString(
3959               &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
3960           );
3961         }else{
3962           int nCmp = MIN(mem1.n, pRhs->n);
3963           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3964           if( rc==0 ) rc = mem1.n - pRhs->n;
3965         }
3966       }
3967     }
3968 
3969     /* RHS is a blob */
3970     else if( pRhs->flags & MEM_Blob ){
3971       getVarint32(&aKey1[idx1], serial_type);
3972       testcase( serial_type==12 );
3973       if( serial_type<12 || (serial_type & 0x01) ){
3974         rc = -1;
3975       }else{
3976         int nStr = (serial_type - 12) / 2;
3977         testcase( (d1+nStr)==(unsigned)nKey1 );
3978         testcase( (d1+nStr+1)==(unsigned)nKey1 );
3979         if( (d1+nStr) > (unsigned)nKey1 ){
3980           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3981           return 0;                /* Corruption */
3982         }else{
3983           int nCmp = MIN(nStr, pRhs->n);
3984           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3985           if( rc==0 ) rc = nStr - pRhs->n;
3986         }
3987       }
3988     }
3989 
3990     /* RHS is null */
3991     else{
3992       serial_type = aKey1[idx1];
3993       rc = (serial_type!=0);
3994     }
3995 
3996     if( rc!=0 ){
3997       if( pKeyInfo->aSortOrder[i] ){
3998         rc = -rc;
3999       }
4000       assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
4001       assert( mem1.szMalloc==0 );  /* See comment below */
4002       return rc;
4003     }
4004 
4005     i++;
4006     pRhs++;
4007     d1 += sqlite3VdbeSerialTypeLen(serial_type);
4008     idx1 += sqlite3VarintLen(serial_type);
4009   }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
4010 
4011   /* No memory allocation is ever used on mem1.  Prove this using
4012   ** the following assert().  If the assert() fails, it indicates a
4013   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
4014   assert( mem1.szMalloc==0 );
4015 
4016   /* rc==0 here means that one or both of the keys ran out of fields and
4017   ** all the fields up to that point were equal. Return the default_rc
4018   ** value.  */
4019   assert( CORRUPT_DB
4020        || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
4021        || pKeyInfo->db->mallocFailed
4022   );
4023   pPKey2->eqSeen = 1;
4024   return pPKey2->default_rc;
4025 }
4026 int sqlite3VdbeRecordCompare(
4027   int nKey1, const void *pKey1,   /* Left key */
4028   UnpackedRecord *pPKey2          /* Right key */
4029 ){
4030   return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
4031 }
4032 
4033 
4034 /*
4035 ** This function is an optimized version of sqlite3VdbeRecordCompare()
4036 ** that (a) the first field of pPKey2 is an integer, and (b) the
4037 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
4038 ** byte (i.e. is less than 128).
4039 **
4040 ** To avoid concerns about buffer overreads, this routine is only used
4041 ** on schemas where the maximum valid header size is 63 bytes or less.
4042 */
4043 static int vdbeRecordCompareInt(
4044   int nKey1, const void *pKey1, /* Left key */
4045   UnpackedRecord *pPKey2        /* Right key */
4046 ){
4047   const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
4048   int serial_type = ((const u8*)pKey1)[1];
4049   int res;
4050   u32 y;
4051   u64 x;
4052   i64 v = pPKey2->aMem[0].u.i;
4053   i64 lhs;
4054 
4055   vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
4056   assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
4057   switch( serial_type ){
4058     case 1: { /* 1-byte signed integer */
4059       lhs = ONE_BYTE_INT(aKey);
4060       testcase( lhs<0 );
4061       break;
4062     }
4063     case 2: { /* 2-byte signed integer */
4064       lhs = TWO_BYTE_INT(aKey);
4065       testcase( lhs<0 );
4066       break;
4067     }
4068     case 3: { /* 3-byte signed integer */
4069       lhs = THREE_BYTE_INT(aKey);
4070       testcase( lhs<0 );
4071       break;
4072     }
4073     case 4: { /* 4-byte signed integer */
4074       y = FOUR_BYTE_UINT(aKey);
4075       lhs = (i64)*(int*)&y;
4076       testcase( lhs<0 );
4077       break;
4078     }
4079     case 5: { /* 6-byte signed integer */
4080       lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
4081       testcase( lhs<0 );
4082       break;
4083     }
4084     case 6: { /* 8-byte signed integer */
4085       x = FOUR_BYTE_UINT(aKey);
4086       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
4087       lhs = *(i64*)&x;
4088       testcase( lhs<0 );
4089       break;
4090     }
4091     case 8:
4092       lhs = 0;
4093       break;
4094     case 9:
4095       lhs = 1;
4096       break;
4097 
4098     /* This case could be removed without changing the results of running
4099     ** this code. Including it causes gcc to generate a faster switch
4100     ** statement (since the range of switch targets now starts at zero and
4101     ** is contiguous) but does not cause any duplicate code to be generated
4102     ** (as gcc is clever enough to combine the two like cases). Other
4103     ** compilers might be similar.  */
4104     case 0: case 7:
4105       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
4106 
4107     default:
4108       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
4109   }
4110 
4111   if( v>lhs ){
4112     res = pPKey2->r1;
4113   }else if( v<lhs ){
4114     res = pPKey2->r2;
4115   }else if( pPKey2->nField>1 ){
4116     /* The first fields of the two keys are equal. Compare the trailing
4117     ** fields.  */
4118     res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
4119   }else{
4120     /* The first fields of the two keys are equal and there are no trailing
4121     ** fields. Return pPKey2->default_rc in this case. */
4122     res = pPKey2->default_rc;
4123     pPKey2->eqSeen = 1;
4124   }
4125 
4126   assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
4127   return res;
4128 }
4129 
4130 /*
4131 ** This function is an optimized version of sqlite3VdbeRecordCompare()
4132 ** that (a) the first field of pPKey2 is a string, that (b) the first field
4133 ** uses the collation sequence BINARY and (c) that the size-of-header varint
4134 ** at the start of (pKey1/nKey1) fits in a single byte.
4135 */
4136 static int vdbeRecordCompareString(
4137   int nKey1, const void *pKey1, /* Left key */
4138   UnpackedRecord *pPKey2        /* Right key */
4139 ){
4140   const u8 *aKey1 = (const u8*)pKey1;
4141   int serial_type;
4142   int res;
4143 
4144   assert( pPKey2->aMem[0].flags & MEM_Str );
4145   vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
4146   getVarint32(&aKey1[1], serial_type);
4147   if( serial_type<12 ){
4148     res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
4149   }else if( !(serial_type & 0x01) ){
4150     res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
4151   }else{
4152     int nCmp;
4153     int nStr;
4154     int szHdr = aKey1[0];
4155 
4156     nStr = (serial_type-12) / 2;
4157     if( (szHdr + nStr) > nKey1 ){
4158       pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
4159       return 0;    /* Corruption */
4160     }
4161     nCmp = MIN( pPKey2->aMem[0].n, nStr );
4162     res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
4163 
4164     if( res==0 ){
4165       res = nStr - pPKey2->aMem[0].n;
4166       if( res==0 ){
4167         if( pPKey2->nField>1 ){
4168           res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
4169         }else{
4170           res = pPKey2->default_rc;
4171           pPKey2->eqSeen = 1;
4172         }
4173       }else if( res>0 ){
4174         res = pPKey2->r2;
4175       }else{
4176         res = pPKey2->r1;
4177       }
4178     }else if( res>0 ){
4179       res = pPKey2->r2;
4180     }else{
4181       res = pPKey2->r1;
4182     }
4183   }
4184 
4185   assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
4186        || CORRUPT_DB
4187        || pPKey2->pKeyInfo->db->mallocFailed
4188   );
4189   return res;
4190 }
4191 
4192 /*
4193 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
4194 ** suitable for comparing serialized records to the unpacked record passed
4195 ** as the only argument.
4196 */
4197 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
4198   /* varintRecordCompareInt() and varintRecordCompareString() both assume
4199   ** that the size-of-header varint that occurs at the start of each record
4200   ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
4201   ** also assumes that it is safe to overread a buffer by at least the
4202   ** maximum possible legal header size plus 8 bytes. Because there is
4203   ** guaranteed to be at least 74 (but not 136) bytes of padding following each
4204   ** buffer passed to varintRecordCompareInt() this makes it convenient to
4205   ** limit the size of the header to 64 bytes in cases where the first field
4206   ** is an integer.
4207   **
4208   ** The easiest way to enforce this limit is to consider only records with
4209   ** 13 fields or less. If the first field is an integer, the maximum legal
4210   ** header size is (12*5 + 1 + 1) bytes.  */
4211   if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
4212     int flags = p->aMem[0].flags;
4213     if( p->pKeyInfo->aSortOrder[0] ){
4214       p->r1 = 1;
4215       p->r2 = -1;
4216     }else{
4217       p->r1 = -1;
4218       p->r2 = 1;
4219     }
4220     if( (flags & MEM_Int) ){
4221       return vdbeRecordCompareInt;
4222     }
4223     testcase( flags & MEM_Real );
4224     testcase( flags & MEM_Null );
4225     testcase( flags & MEM_Blob );
4226     if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
4227       assert( flags & MEM_Str );
4228       return vdbeRecordCompareString;
4229     }
4230   }
4231 
4232   return sqlite3VdbeRecordCompare;
4233 }
4234 
4235 /*
4236 ** pCur points at an index entry created using the OP_MakeRecord opcode.
4237 ** Read the rowid (the last field in the record) and store it in *rowid.
4238 ** Return SQLITE_OK if everything works, or an error code otherwise.
4239 **
4240 ** pCur might be pointing to text obtained from a corrupt database file.
4241 ** So the content cannot be trusted.  Do appropriate checks on the content.
4242 */
4243 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
4244   i64 nCellKey = 0;
4245   int rc;
4246   u32 szHdr;        /* Size of the header */
4247   u32 typeRowid;    /* Serial type of the rowid */
4248   u32 lenRowid;     /* Size of the rowid */
4249   Mem m, v;
4250 
4251   /* Get the size of the index entry.  Only indices entries of less
4252   ** than 2GiB are support - anything large must be database corruption.
4253   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
4254   ** this code can safely assume that nCellKey is 32-bits
4255   */
4256   assert( sqlite3BtreeCursorIsValid(pCur) );
4257   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
4258   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
4259   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
4260 
4261   /* Read in the complete content of the index entry */
4262   sqlite3VdbeMemInit(&m, db, 0);
4263   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
4264   if( rc ){
4265     return rc;
4266   }
4267 
4268   /* The index entry must begin with a header size */
4269   (void)getVarint32((u8*)m.z, szHdr);
4270   testcase( szHdr==3 );
4271   testcase( szHdr==m.n );
4272   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
4273     goto idx_rowid_corruption;
4274   }
4275 
4276   /* The last field of the index should be an integer - the ROWID.
4277   ** Verify that the last entry really is an integer. */
4278   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
4279   testcase( typeRowid==1 );
4280   testcase( typeRowid==2 );
4281   testcase( typeRowid==3 );
4282   testcase( typeRowid==4 );
4283   testcase( typeRowid==5 );
4284   testcase( typeRowid==6 );
4285   testcase( typeRowid==8 );
4286   testcase( typeRowid==9 );
4287   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
4288     goto idx_rowid_corruption;
4289   }
4290   lenRowid = sqlite3SmallTypeSizes[typeRowid];
4291   testcase( (u32)m.n==szHdr+lenRowid );
4292   if( unlikely((u32)m.n<szHdr+lenRowid) ){
4293     goto idx_rowid_corruption;
4294   }
4295 
4296   /* Fetch the integer off the end of the index record */
4297   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
4298   *rowid = v.u.i;
4299   sqlite3VdbeMemRelease(&m);
4300   return SQLITE_OK;
4301 
4302   /* Jump here if database corruption is detected after m has been
4303   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
4304 idx_rowid_corruption:
4305   testcase( m.szMalloc!=0 );
4306   sqlite3VdbeMemRelease(&m);
4307   return SQLITE_CORRUPT_BKPT;
4308 }
4309 
4310 /*
4311 ** Compare the key of the index entry that cursor pC is pointing to against
4312 ** the key string in pUnpacked.  Write into *pRes a number
4313 ** that is negative, zero, or positive if pC is less than, equal to,
4314 ** or greater than pUnpacked.  Return SQLITE_OK on success.
4315 **
4316 ** pUnpacked is either created without a rowid or is truncated so that it
4317 ** omits the rowid at the end.  The rowid at the end of the index entry
4318 ** is ignored as well.  Hence, this routine only compares the prefixes
4319 ** of the keys prior to the final rowid, not the entire key.
4320 */
4321 int sqlite3VdbeIdxKeyCompare(
4322   sqlite3 *db,                     /* Database connection */
4323   VdbeCursor *pC,                  /* The cursor to compare against */
4324   UnpackedRecord *pUnpacked,       /* Unpacked version of key */
4325   int *res                         /* Write the comparison result here */
4326 ){
4327   i64 nCellKey = 0;
4328   int rc;
4329   BtCursor *pCur;
4330   Mem m;
4331 
4332   assert( pC->eCurType==CURTYPE_BTREE );
4333   pCur = pC->uc.pCursor;
4334   assert( sqlite3BtreeCursorIsValid(pCur) );
4335   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
4336   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
4337   /* nCellKey will always be between 0 and 0xffffffff because of the way
4338   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
4339   if( nCellKey<=0 || nCellKey>0x7fffffff ){
4340     *res = 0;
4341     return SQLITE_CORRUPT_BKPT;
4342   }
4343   sqlite3VdbeMemInit(&m, db, 0);
4344   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
4345   if( rc ){
4346     return rc;
4347   }
4348   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
4349   sqlite3VdbeMemRelease(&m);
4350   return SQLITE_OK;
4351 }
4352 
4353 /*
4354 ** This routine sets the value to be returned by subsequent calls to
4355 ** sqlite3_changes() on the database handle 'db'.
4356 */
4357 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
4358   assert( sqlite3_mutex_held(db->mutex) );
4359   db->nChange = nChange;
4360   db->nTotalChange += nChange;
4361 }
4362 
4363 /*
4364 ** Set a flag in the vdbe to update the change counter when it is finalised
4365 ** or reset.
4366 */
4367 void sqlite3VdbeCountChanges(Vdbe *v){
4368   v->changeCntOn = 1;
4369 }
4370 
4371 /*
4372 ** Mark every prepared statement associated with a database connection
4373 ** as expired.
4374 **
4375 ** An expired statement means that recompilation of the statement is
4376 ** recommend.  Statements expire when things happen that make their
4377 ** programs obsolete.  Removing user-defined functions or collating
4378 ** sequences, or changing an authorization function are the types of
4379 ** things that make prepared statements obsolete.
4380 */
4381 void sqlite3ExpirePreparedStatements(sqlite3 *db){
4382   Vdbe *p;
4383   for(p = db->pVdbe; p; p=p->pNext){
4384     p->expired = 1;
4385   }
4386 }
4387 
4388 /*
4389 ** Return the database associated with the Vdbe.
4390 */
4391 sqlite3 *sqlite3VdbeDb(Vdbe *v){
4392   return v->db;
4393 }
4394 
4395 /*
4396 ** Return a pointer to an sqlite3_value structure containing the value bound
4397 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
4398 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
4399 ** constants) to the value before returning it.
4400 **
4401 ** The returned value must be freed by the caller using sqlite3ValueFree().
4402 */
4403 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
4404   assert( iVar>0 );
4405   if( v ){
4406     Mem *pMem = &v->aVar[iVar-1];
4407     if( 0==(pMem->flags & MEM_Null) ){
4408       sqlite3_value *pRet = sqlite3ValueNew(v->db);
4409       if( pRet ){
4410         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
4411         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
4412       }
4413       return pRet;
4414     }
4415   }
4416   return 0;
4417 }
4418 
4419 /*
4420 ** Configure SQL variable iVar so that binding a new value to it signals
4421 ** to sqlite3_reoptimize() that re-preparing the statement may result
4422 ** in a better query plan.
4423 */
4424 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
4425   assert( iVar>0 );
4426   if( iVar>32 ){
4427     v->expmask = 0xffffffff;
4428   }else{
4429     v->expmask |= ((u32)1 << (iVar-1));
4430   }
4431 }
4432 
4433 #ifndef SQLITE_OMIT_VIRTUALTABLE
4434 /*
4435 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4436 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4437 ** in memory obtained from sqlite3DbMalloc).
4438 */
4439 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
4440   if( pVtab->zErrMsg ){
4441     sqlite3 *db = p->db;
4442     sqlite3DbFree(db, p->zErrMsg);
4443     p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
4444     sqlite3_free(pVtab->zErrMsg);
4445     pVtab->zErrMsg = 0;
4446   }
4447 }
4448 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4449