vdbeaux.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322
  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.) Prior
  14. ** to version 2.8.7, all this code was combined into the vdbe.c source file.
  15. ** But that file was getting too big so this subroutines were split out.
  16. */
  17. #include "sqliteInt.h"
  18. #include "vdbeInt.h"
  19. /*
  20. ** Create a new virtual database engine.
  21. */
  22. Vdbe *sqlite3VdbeCreate(sqlite3 *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. return p;
  35. }
  36. /*
  37. ** Remember the SQL string for a prepared statement.
  38. */
  39. void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
  40. assert( isPrepareV2==1 || isPrepareV2==0 );
  41. if( p==0 ) return;
  42. #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
  43. if( !isPrepareV2 ) return;
  44. #endif
  45. assert( p->zSql==0 );
  46. p->zSql = sqlite3DbStrNDup(p->db, z, n);
  47. p->isPrepareV2 = (u8)isPrepareV2;
  48. }
  49. /*
  50. ** Return the SQL associated with a prepared statement
  51. */
  52. const char *sqlite3_sql(sqlite3_stmt *pStmt){
  53. Vdbe *p = (Vdbe *)pStmt;
  54. return (p && p->isPrepareV2) ? p->zSql : 0;
  55. }
  56. /*
  57. ** Swap all content between two VDBE structures.
  58. */
  59. void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
  60. Vdbe tmp, *pTmp;
  61. char *zTmp;
  62. tmp = *pA;
  63. *pA = *pB;
  64. *pB = tmp;
  65. pTmp = pA->pNext;
  66. pA->pNext = pB->pNext;
  67. pB->pNext = pTmp;
  68. pTmp = pA->pPrev;
  69. pA->pPrev = pB->pPrev;
  70. pB->pPrev = pTmp;
  71. zTmp = pA->zSql;
  72. pA->zSql = pB->zSql;
  73. pB->zSql = zTmp;
  74. pB->isPrepareV2 = pA->isPrepareV2;
  75. }
  76. #ifdef SQLITE_DEBUG
  77. /*
  78. ** Turn tracing on or off
  79. */
  80. void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
  81. p->trace = trace;
  82. }
  83. #endif
  84. /*
  85. ** Resize the Vdbe.aOp array so that it is at least one op larger than
  86. ** it was.
  87. **
  88. ** If an out-of-memory error occurs while resizing the array, return
  89. ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
  90. ** unchanged (this is so that any opcodes already allocated can be
  91. ** correctly deallocated along with the rest of the Vdbe).
  92. */
  93. static int growOpArray(Vdbe *p){
  94. VdbeOp *pNew;
  95. int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
  96. pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
  97. if( pNew ){
  98. p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
  99. p->aOp = pNew;
  100. }
  101. return (pNew ? SQLITE_OK : SQLITE_NOMEM);
  102. }
  103. /*
  104. ** Add a new instruction to the list of instructions current in the
  105. ** VDBE. Return the address of the new instruction.
  106. **
  107. ** Parameters:
  108. **
  109. ** p Pointer to the VDBE
  110. **
  111. ** op The opcode for this instruction
  112. **
  113. ** p1, p2, p3 Operands
  114. **
  115. ** Use the sqlite3VdbeResolveLabel() function to fix an address and
  116. ** the sqlite3VdbeChangeP4() function to change the value of the P4
  117. ** operand.
  118. */
  119. int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
  120. int i;
  121. VdbeOp *pOp;
  122. i = p->nOp;
  123. assert( p->magic==VDBE_MAGIC_INIT );
  124. assert( op>0 && op<0xff );
  125. if( p->nOpAlloc<=i ){
  126. if( growOpArray(p) ){
  127. return 1;
  128. }
  129. }
  130. p->nOp++;
  131. pOp = &p->aOp[i];
  132. pOp->opcode = (u8)op;
  133. pOp->p5 = 0;
  134. pOp->p1 = p1;
  135. pOp->p2 = p2;
  136. pOp->p3 = p3;
  137. pOp->p4.p = 0;
  138. pOp->p4type = P4_NOTUSED;
  139. #ifdef SQLITE_DEBUG
  140. pOp->zComment = 0;
  141. if( p->db->flags & SQLITE_VdbeAddopTrace ){
  142. sqlite3VdbePrintOp(0, i, &p->aOp[i]);
  143. }
  144. #endif
  145. #ifdef VDBE_PROFILE
  146. pOp->cycles = 0;
  147. pOp->cnt = 0;
  148. #endif
  149. return i;
  150. }
  151. int sqlite3VdbeAddOp0(Vdbe *p, int op){
  152. return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
  153. }
  154. int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
  155. return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
  156. }
  157. int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
  158. return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
  159. }
  160. /*
  161. ** Add an opcode that includes the p4 value as a pointer.
  162. */
  163. int sqlite3VdbeAddOp4(
  164. Vdbe *p, /* Add the opcode to this VM */
  165. int op, /* The new opcode */
  166. int p1, /* The P1 operand */
  167. int p2, /* The P2 operand */
  168. int p3, /* The P3 operand */
  169. const char *zP4, /* The P4 operand */
  170. int p4type /* P4 operand type */
  171. ){
  172. int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
  173. sqlite3VdbeChangeP4(p, addr, zP4, p4type);
  174. return addr;
  175. }
  176. /*
  177. ** Add an OP_ParseSchema opcode. This routine is broken out from
  178. ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
  179. ** as having been used.
  180. **
  181. ** The zWhere string must have been obtained from sqlite3_malloc().
  182. ** This routine will take ownership of the allocated memory.
  183. */
  184. void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
  185. int j;
  186. int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
  187. sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
  188. for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
  189. }
  190. /*
  191. ** Add an opcode that includes the p4 value as an integer.
  192. */
  193. int sqlite3VdbeAddOp4Int(
  194. Vdbe *p, /* Add the opcode to this VM */
  195. int op, /* The new opcode */
  196. int p1, /* The P1 operand */
  197. int p2, /* The P2 operand */
  198. int p3, /* The P3 operand */
  199. int p4 /* The P4 operand as an integer */
  200. ){
  201. int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
  202. sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
  203. return addr;
  204. }
  205. /*
  206. ** Create a new symbolic label for an instruction that has yet to be
  207. ** coded. The symbolic label is really just a negative number. The
  208. ** label can be used as the P2 value of an operation. Later, when
  209. ** the label is resolved to a specific address, the VDBE will scan
  210. ** through its operation list and change all values of P2 which match
  211. ** the label into the resolved address.
  212. **
  213. ** The VDBE knows that a P2 value is a label because labels are
  214. ** always negative and P2 values are suppose to be non-negative.
  215. ** Hence, a negative P2 value is a label that has yet to be resolved.
  216. **
  217. ** Zero is returned if a malloc() fails.
  218. */
  219. int sqlite3VdbeMakeLabel(Vdbe *p){
  220. int i = p->nLabel++;
  221. assert( p->magic==VDBE_MAGIC_INIT );
  222. if( (i & (i-1))==0 ){
  223. p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
  224. (i*2+1)*sizeof(p->aLabel[0]));
  225. }
  226. if( p->aLabel ){
  227. p->aLabel[i] = -1;
  228. }
  229. return -1-i;
  230. }
  231. /*
  232. ** Resolve label "x" to be the address of the next instruction to
  233. ** be inserted. The parameter "x" must have been obtained from
  234. ** a prior call to sqlite3VdbeMakeLabel().
  235. */
  236. void sqlite3VdbeResolveLabel(Vdbe *p, int x){
  237. int j = -1-x;
  238. assert( p->magic==VDBE_MAGIC_INIT );
  239. assert( j<p->nLabel );
  240. if( j>=0 && p->aLabel ){
  241. p->aLabel[j] = p->nOp;
  242. }
  243. }
  244. /*
  245. ** Mark the VDBE as one that can only be run one time.
  246. */
  247. void sqlite3VdbeRunOnlyOnce(Vdbe *p){
  248. p->runOnlyOnce = 1;
  249. }
  250. #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
  251. /*
  252. ** The following type and function are used to iterate through all opcodes
  253. ** in a Vdbe main program and each of the sub-programs (triggers) it may
  254. ** invoke directly or indirectly. It should be used as follows:
  255. **
  256. ** Op *pOp;
  257. ** VdbeOpIter sIter;
  258. **
  259. ** memset(&sIter, 0, sizeof(sIter));
  260. ** sIter.v = v; // v is of type Vdbe*
  261. ** while( (pOp = opIterNext(&sIter)) ){
  262. ** // Do something with pOp
  263. ** }
  264. ** sqlite3DbFree(v->db, sIter.apSub);
  265. **
  266. */
  267. typedef struct VdbeOpIter VdbeOpIter;
  268. struct VdbeOpIter {
  269. Vdbe *v; /* Vdbe to iterate through the opcodes of */
  270. SubProgram **apSub; /* Array of subprograms */
  271. int nSub; /* Number of entries in apSub */
  272. int iAddr; /* Address of next instruction to return */
  273. int iSub; /* 0 = main program, 1 = first sub-program etc. */
  274. };
  275. static Op *opIterNext(VdbeOpIter *p){
  276. Vdbe *v = p->v;
  277. Op *pRet = 0;
  278. Op *aOp;
  279. int nOp;
  280. if( p->iSub<=p->nSub ){
  281. if( p->iSub==0 ){
  282. aOp = v->aOp;
  283. nOp = v->nOp;
  284. }else{
  285. aOp = p->apSub[p->iSub-1]->aOp;
  286. nOp = p->apSub[p->iSub-1]->nOp;
  287. }
  288. assert( p->iAddr<nOp );
  289. pRet = &aOp[p->iAddr];
  290. p->iAddr++;
  291. if( p->iAddr==nOp ){
  292. p->iSub++;
  293. p->iAddr = 0;
  294. }
  295. if( pRet->p4type==P4_SUBPROGRAM ){
  296. int nByte = (p->nSub+1)*sizeof(SubProgram*);
  297. int j;
  298. for(j=0; j<p->nSub; j++){
  299. if( p->apSub[j]==pRet->p4.pProgram ) break;
  300. }
  301. if( j==p->nSub ){
  302. p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
  303. if( !p->apSub ){
  304. pRet = 0;
  305. }else{
  306. p->apSub[p->nSub++] = pRet->p4.pProgram;
  307. }
  308. }
  309. }
  310. }
  311. return pRet;
  312. }
  313. /*
  314. ** Check if the program stored in the VM associated with pParse may
  315. ** throw an ABORT exception (causing the statement, but not entire transaction
  316. ** to be rolled back). This condition is true if the main program or any
  317. ** sub-programs contains any of the following:
  318. **
  319. ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
  320. ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
  321. ** * OP_Destroy
  322. ** * OP_VUpdate
  323. ** * OP_VRename
  324. ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
  325. **
  326. ** Then check that the value of Parse.mayAbort is true if an
  327. ** ABORT may be thrown, or false otherwise. Return true if it does
  328. ** match, or false otherwise. This function is intended to be used as
  329. ** part of an assert statement in the compiler. Similar to:
  330. **
  331. ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
  332. */
  333. int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
  334. int hasAbort = 0;
  335. Op *pOp;
  336. VdbeOpIter sIter;
  337. memset(&sIter, 0, sizeof(sIter));
  338. sIter.v = v;
  339. while( (pOp = opIterNext(&sIter))!=0 ){
  340. int opcode = pOp->opcode;
  341. if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
  342. #ifndef SQLITE_OMIT_FOREIGN_KEY
  343. || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
  344. #endif
  345. || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
  346. && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
  347. ){
  348. hasAbort = 1;
  349. break;
  350. }
  351. }
  352. sqlite3DbFree(v->db, sIter.apSub);
  353. /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
  354. ** If malloc failed, then the while() loop above may not have iterated
  355. ** through all opcodes and hasAbort may be set incorrectly. Return
  356. ** true for this case to prevent the assert() in the callers frame
  357. ** from failing. */
  358. return ( v->db->mallocFailed || hasAbort==mayAbort );
  359. }
  360. #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
  361. /*
  362. ** Loop through the program looking for P2 values that are negative
  363. ** on jump instructions. Each such value is a label. Resolve the
  364. ** label by setting the P2 value to its correct non-zero value.
  365. **
  366. ** This routine is called once after all opcodes have been inserted.
  367. **
  368. ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
  369. ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
  370. ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
  371. **
  372. ** The Op.opflags field is set on all opcodes.
  373. */
  374. static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
  375. int i;
  376. int nMaxArgs = *pMaxFuncArgs;
  377. Op *pOp;
  378. int *aLabel = p->aLabel;
  379. p->readOnly = 1;
  380. p->bIsReader = 0;
  381. for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
  382. u8 opcode = pOp->opcode;
  383. /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
  384. ** cases from this switch! */
  385. switch( opcode ){
  386. case OP_Function:
  387. case OP_AggStep: {
  388. if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
  389. break;
  390. }
  391. case OP_Transaction: {
  392. if( pOp->p2!=0 ) p->readOnly = 0;
  393. /* fall thru */
  394. }
  395. case OP_AutoCommit:
  396. case OP_Savepoint: {
  397. p->bIsReader = 1;
  398. break;
  399. }
  400. #ifndef SQLITE_OMIT_WAL
  401. case OP_Checkpoint:
  402. #endif
  403. case OP_Vacuum:
  404. case OP_JournalMode: {
  405. p->readOnly = 0;
  406. p->bIsReader = 1;
  407. break;
  408. }
  409. #ifndef SQLITE_OMIT_VIRTUALTABLE
  410. case OP_VUpdate: {
  411. if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
  412. break;
  413. }
  414. case OP_VFilter: {
  415. int n;
  416. assert( p->nOp - i >= 3 );
  417. assert( pOp[-1].opcode==OP_Integer );
  418. n = pOp[-1].p1;
  419. if( n>nMaxArgs ) nMaxArgs = n;
  420. break;
  421. }
  422. #endif
  423. case OP_Next:
  424. case OP_SorterNext: {
  425. pOp->p4.xAdvance = sqlite3BtreeNext;
  426. pOp->p4type = P4_ADVANCE;
  427. break;
  428. }
  429. case OP_Prev: {
  430. pOp->p4.xAdvance = sqlite3BtreePrevious;
  431. pOp->p4type = P4_ADVANCE;
  432. break;
  433. }
  434. }
  435. pOp->opflags = sqlite3OpcodeProperty[opcode];
  436. if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
  437. assert( -1-pOp->p2<p->nLabel );
  438. pOp->p2 = aLabel[-1-pOp->p2];
  439. }
  440. }
  441. sqlite3DbFree(p->db, p->aLabel);
  442. p->aLabel = 0;
  443. *pMaxFuncArgs = nMaxArgs;
  444. assert( p->bIsReader!=0 || p->btreeMask==0 );
  445. }
  446. /*
  447. ** Return the address of the next instruction to be inserted.
  448. */
  449. int sqlite3VdbeCurrentAddr(Vdbe *p){
  450. assert( p->magic==VDBE_MAGIC_INIT );
  451. return p->nOp;
  452. }
  453. /*
  454. ** This function returns a pointer to the array of opcodes associated with
  455. ** the Vdbe passed as the first argument. It is the callers responsibility
  456. ** to arrange for the returned array to be eventually freed using the
  457. ** vdbeFreeOpArray() function.
  458. **
  459. ** Before returning, *pnOp is set to the number of entries in the returned
  460. ** array. Also, *pnMaxArg is set to the larger of its current value and
  461. ** the number of entries in the Vdbe.apArg[] array required to execute the
  462. ** returned program.
  463. */
  464. VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
  465. VdbeOp *aOp = p->aOp;
  466. assert( aOp && !p->db->mallocFailed );
  467. /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
  468. assert( p->btreeMask==0 );
  469. resolveP2Values(p, pnMaxArg);
  470. *pnOp = p->nOp;
  471. p->aOp = 0;
  472. return aOp;
  473. }
  474. /*
  475. ** Add a whole list of operations to the operation stack. Return the
  476. ** address of the first operation added.
  477. */
  478. int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
  479. int addr;
  480. assert( p->magic==VDBE_MAGIC_INIT );
  481. if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
  482. return 0;
  483. }
  484. addr = p->nOp;
  485. if( ALWAYS(nOp>0) ){
  486. int i;
  487. VdbeOpList const *pIn = aOp;
  488. for(i=0; i<nOp; i++, pIn++){
  489. int p2 = pIn->p2;
  490. VdbeOp *pOut = &p->aOp[i+addr];
  491. pOut->opcode = pIn->opcode;
  492. pOut->p1 = pIn->p1;
  493. if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
  494. pOut->p2 = addr + ADDR(p2);
  495. }else{
  496. pOut->p2 = p2;
  497. }
  498. pOut->p3 = pIn->p3;
  499. pOut->p4type = P4_NOTUSED;
  500. pOut->p4.p = 0;
  501. pOut->p5 = 0;
  502. #ifdef SQLITE_DEBUG
  503. pOut->zComment = 0;
  504. if( p->db->flags & SQLITE_VdbeAddopTrace ){
  505. sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
  506. }
  507. #endif
  508. }
  509. p->nOp += nOp;
  510. }
  511. return addr;
  512. }
  513. /*
  514. ** Change the value of the P1 operand for a specific instruction.
  515. ** This routine is useful when a large program is loaded from a
  516. ** static array using sqlite3VdbeAddOpList but we want to make a
  517. ** few minor changes to the program.
  518. */
  519. void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
  520. assert( p!=0 );
  521. if( ((u32)p->nOp)>addr ){
  522. p->aOp[addr].p1 = val;
  523. }
  524. }
  525. /*
  526. ** Change the value of the P2 operand for a specific instruction.
  527. ** This routine is useful for setting a jump destination.
  528. */
  529. void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
  530. assert( p!=0 );
  531. if( ((u32)p->nOp)>addr ){
  532. p->aOp[addr].p2 = val;
  533. }
  534. }
  535. /*
  536. ** Change the value of the P3 operand for a specific instruction.
  537. */
  538. void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
  539. assert( p!=0 );
  540. if( ((u32)p->nOp)>addr ){
  541. p->aOp[addr].p3 = val;
  542. }
  543. }
  544. /*
  545. ** Change the value of the P5 operand for the most recently
  546. ** added operation.
  547. */
  548. void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
  549. assert( p!=0 );
  550. if( p->aOp ){
  551. assert( p->nOp>0 );
  552. p->aOp[p->nOp-1].p5 = val;
  553. }
  554. }
  555. /*
  556. ** Change the P2 operand of instruction addr so that it points to
  557. ** the address of the next instruction to be coded.
  558. */
  559. void sqlite3VdbeJumpHere(Vdbe *p, int addr){
  560. if( ALWAYS(addr>=0) ) sqlite3VdbeChangeP2(p, addr, p->nOp);
  561. }
  562. /*
  563. ** If the input FuncDef structure is ephemeral, then free it. If
  564. ** the FuncDef is not ephermal, then do nothing.
  565. */
  566. static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
  567. if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
  568. sqlite3DbFree(db, pDef);
  569. }
  570. }
  571. static void vdbeFreeOpArray(sqlite3 *, Op *, int);
  572. /*
  573. ** Delete a P4 value if necessary.
  574. */
  575. static void freeP4(sqlite3 *db, int p4type, void *p4){
  576. if( p4 ){
  577. assert( db );
  578. switch( p4type ){
  579. case P4_REAL:
  580. case P4_INT64:
  581. case P4_DYNAMIC:
  582. case P4_KEYINFO:
  583. case P4_INTARRAY:
  584. case P4_KEYINFO_HANDOFF: {
  585. sqlite3DbFree(db, p4);
  586. break;
  587. }
  588. case P4_MPRINTF: {
  589. if( db->pnBytesFreed==0 ) sqlite3_free(p4);
  590. break;
  591. }
  592. case P4_FUNCDEF: {
  593. freeEphemeralFunction(db, (FuncDef*)p4);
  594. break;
  595. }
  596. case P4_MEM: {
  597. if( db->pnBytesFreed==0 ){
  598. sqlite3ValueFree((sqlite3_value*)p4);
  599. }else{
  600. Mem *p = (Mem*)p4;
  601. sqlite3DbFree(db, p->zMalloc);
  602. sqlite3DbFree(db, p);
  603. }
  604. break;
  605. }
  606. case P4_VTAB : {
  607. if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
  608. break;
  609. }
  610. }
  611. }
  612. }
  613. /*
  614. ** Free the space allocated for aOp and any p4 values allocated for the
  615. ** opcodes contained within. If aOp is not NULL it is assumed to contain
  616. ** nOp entries.
  617. */
  618. static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
  619. if( aOp ){
  620. Op *pOp;
  621. for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
  622. freeP4(db, pOp->p4type, pOp->p4.p);
  623. #ifdef SQLITE_DEBUG
  624. sqlite3DbFree(db, pOp->zComment);
  625. #endif
  626. }
  627. }
  628. sqlite3DbFree(db, aOp);
  629. }
  630. /*
  631. ** Link the SubProgram object passed as the second argument into the linked
  632. ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
  633. ** objects when the VM is no longer required.
  634. */
  635. void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
  636. p->pNext = pVdbe->pProgram;
  637. pVdbe->pProgram = p;
  638. }
  639. /*
  640. ** Change the opcode at addr into OP_Noop
  641. */
  642. void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
  643. if( p->aOp ){
  644. VdbeOp *pOp = &p->aOp[addr];
  645. sqlite3 *db = p->db;
  646. freeP4(db, pOp->p4type, pOp->p4.p);
  647. memset(pOp, 0, sizeof(pOp[0]));
  648. pOp->opcode = OP_Noop;
  649. }
  650. }
  651. /*
  652. ** Change the value of the P4 operand for a specific instruction.
  653. ** This routine is useful when a large program is loaded from a
  654. ** static array using sqlite3VdbeAddOpList but we want to make a
  655. ** few minor changes to the program.
  656. **
  657. ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
  658. ** the string is made into memory obtained from sqlite3_malloc().
  659. ** A value of n==0 means copy bytes of zP4 up to and including the
  660. ** first null byte. If n>0 then copy n+1 bytes of zP4.
  661. **
  662. ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
  663. ** A copy is made of the KeyInfo structure into memory obtained from
  664. ** sqlite3_malloc, to be freed when the Vdbe is finalized.
  665. ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
  666. ** stored in memory that the caller has obtained from sqlite3_malloc. The
  667. ** caller should not free the allocation, it will be freed when the Vdbe is
  668. ** finalized.
  669. **
  670. ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
  671. ** to a string or structure that is guaranteed to exist for the lifetime of
  672. ** the Vdbe. In these cases we can just copy the pointer.
  673. **
  674. ** If addr<0 then change P4 on the most recently inserted instruction.
  675. */
  676. void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
  677. Op *pOp;
  678. sqlite3 *db;
  679. assert( p!=0 );
  680. db = p->db;
  681. assert( p->magic==VDBE_MAGIC_INIT );
  682. if( p->aOp==0 || db->mallocFailed ){
  683. if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
  684. freeP4(db, n, (void*)*(char**)&zP4);
  685. }
  686. return;
  687. }
  688. assert( p->nOp>0 );
  689. assert( addr<p->nOp );
  690. if( addr<0 ){
  691. addr = p->nOp - 1;
  692. }
  693. pOp = &p->aOp[addr];
  694. assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
  695. freeP4(db, pOp->p4type, pOp->p4.p);
  696. pOp->p4.p = 0;
  697. if( n==P4_INT32 ){
  698. /* Note: this cast is safe, because the origin data point was an int
  699. ** that was cast to a (const char *). */
  700. pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
  701. pOp->p4type = P4_INT32;
  702. }else if( zP4==0 ){
  703. pOp->p4.p = 0;
  704. pOp->p4type = P4_NOTUSED;
  705. }else if( n==P4_KEYINFO ){
  706. KeyInfo *pOrig, *pNew;
  707. pOrig = (KeyInfo*)zP4;
  708. pOp->p4.pKeyInfo = pNew = sqlite3KeyInfoAlloc(db, pOrig->nField);
  709. if( pNew ){
  710. memcpy(pNew->aColl, pOrig->aColl, pOrig->nField*sizeof(pNew->aColl[0]));
  711. memcpy(pNew->aSortOrder, pOrig->aSortOrder, pOrig->nField);
  712. pOp->p4type = P4_KEYINFO;
  713. }else{
  714. p->db->mallocFailed = 1;
  715. pOp->p4type = P4_NOTUSED;
  716. }
  717. }else if( n==P4_KEYINFO_HANDOFF ){
  718. pOp->p4.p = (void*)zP4;
  719. pOp->p4type = P4_KEYINFO;
  720. }else if( n==P4_VTAB ){
  721. pOp->p4.p = (void*)zP4;
  722. pOp->p4type = P4_VTAB;
  723. sqlite3VtabLock((VTable *)zP4);
  724. assert( ((VTable *)zP4)->db==p->db );
  725. }else if( n<0 ){
  726. pOp->p4.p = (void*)zP4;
  727. pOp->p4type = (signed char)n;
  728. }else{
  729. if( n==0 ) n = sqlite3Strlen30(zP4);
  730. pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
  731. pOp->p4type = P4_DYNAMIC;
  732. }
  733. }
  734. #ifndef NDEBUG
  735. /*
  736. ** Change the comment on the most recently coded instruction. Or
  737. ** insert a No-op and add the comment to that new instruction. This
  738. ** makes the code easier to read during debugging. None of this happens
  739. ** in a production build.
  740. */
  741. static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
  742. assert( p->nOp>0 || p->aOp==0 );
  743. assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
  744. if( p->nOp ){
  745. assert( p->aOp );
  746. sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
  747. p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
  748. }
  749. }
  750. void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
  751. va_list ap;
  752. if( p ){
  753. va_start(ap, zFormat);
  754. vdbeVComment(p, zFormat, ap);
  755. va_end(ap);
  756. }
  757. }
  758. void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
  759. va_list ap;
  760. if( p ){
  761. sqlite3VdbeAddOp0(p, OP_Noop);
  762. va_start(ap, zFormat);
  763. vdbeVComment(p, zFormat, ap);
  764. va_end(ap);
  765. }
  766. }
  767. #endif /* NDEBUG */
  768. /*
  769. ** Return the opcode for a given address. If the address is -1, then
  770. ** return the most recently inserted opcode.
  771. **
  772. ** If a memory allocation error has occurred prior to the calling of this
  773. ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
  774. ** is readable but not writable, though it is cast to a writable value.
  775. ** The return of a dummy opcode allows the call to continue functioning
  776. ** after a OOM fault without having to check to see if the return from
  777. ** this routine is a valid pointer. But because the dummy.opcode is 0,
  778. ** dummy will never be written to. This is verified by code inspection and
  779. ** by running with Valgrind.
  780. **
  781. ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
  782. ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
  783. ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
  784. ** a new VDBE is created. So we are free to set addr to p->nOp-1 without
  785. ** having to double-check to make sure that the result is non-negative. But
  786. ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
  787. ** check the value of p->nOp-1 before continuing.
  788. */
  789. VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
  790. /* C89 specifies that the constant "dummy" will be initialized to all
  791. ** zeros, which is correct. MSVC generates a warning, nevertheless. */
  792. static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
  793. assert( p->magic==VDBE_MAGIC_INIT );
  794. if( addr<0 ){
  795. #ifdef SQLITE_OMIT_TRACE
  796. if( p->nOp==0 ) return (VdbeOp*)&dummy;
  797. #endif
  798. addr = p->nOp - 1;
  799. }
  800. assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
  801. if( p->db->mallocFailed ){
  802. return (VdbeOp*)&dummy;
  803. }else{
  804. return &p->aOp[addr];
  805. }
  806. }
  807. #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
  808. || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
  809. /*
  810. ** Compute a string that describes the P4 parameter for an opcode.
  811. ** Use zTemp for any required temporary buffer space.
  812. */
  813. static char *displayP4(Op *pOp, char *zTemp, int nTemp){
  814. char *zP4 = zTemp;
  815. assert( nTemp>=20 );
  816. switch( pOp->p4type ){
  817. case P4_KEYINFO_STATIC:
  818. case P4_KEYINFO: {
  819. int i, j;
  820. KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
  821. assert( pKeyInfo->aSortOrder!=0 );
  822. sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
  823. i = sqlite3Strlen30(zTemp);
  824. for(j=0; j<pKeyInfo->nField; j++){
  825. CollSeq *pColl = pKeyInfo->aColl[j];
  826. const char *zColl = pColl ? pColl->zName : "nil";
  827. int n = sqlite3Strlen30(zColl);
  828. if( i+n>nTemp-6 ){
  829. memcpy(&zTemp[i],",...",4);
  830. break;
  831. }
  832. zTemp[i++] = ',';
  833. if( pKeyInfo->aSortOrder[j] ){
  834. zTemp[i++] = '-';
  835. }
  836. memcpy(&zTemp[i], zColl, n+1);
  837. i += n;
  838. }
  839. zTemp[i++] = ')';
  840. zTemp[i] = 0;
  841. assert( i<nTemp );
  842. break;
  843. }
  844. case P4_COLLSEQ: {
  845. CollSeq *pColl = pOp->p4.pColl;
  846. sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
  847. break;
  848. }
  849. case P4_FUNCDEF: {
  850. FuncDef *pDef = pOp->p4.pFunc;
  851. sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
  852. break;
  853. }
  854. case P4_INT64: {
  855. sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
  856. break;
  857. }
  858. case P4_INT32: {
  859. sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
  860. break;
  861. }
  862. case P4_REAL: {
  863. sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
  864. break;
  865. }
  866. case P4_MEM: {
  867. Mem *pMem = pOp->p4.pMem;
  868. if( pMem->flags & MEM_Str ){
  869. zP4 = pMem->z;
  870. }else if( pMem->flags & MEM_Int ){
  871. sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
  872. }else if( pMem->flags & MEM_Real ){
  873. sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
  874. }else if( pMem->flags & MEM_Null ){
  875. sqlite3_snprintf(nTemp, zTemp, "NULL");
  876. }else{
  877. assert( pMem->flags & MEM_Blob );
  878. zP4 = "(blob)";
  879. }
  880. break;
  881. }
  882. #ifndef SQLITE_OMIT_VIRTUALTABLE
  883. case P4_VTAB: {
  884. sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
  885. sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
  886. break;
  887. }
  888. #endif
  889. case P4_INTARRAY: {
  890. sqlite3_snprintf(nTemp, zTemp, "intarray");
  891. break;
  892. }
  893. case P4_SUBPROGRAM: {
  894. sqlite3_snprintf(nTemp, zTemp, "program");
  895. break;
  896. }
  897. case P4_ADVANCE: {
  898. zTemp[0] = 0;
  899. break;
  900. }
  901. default: {
  902. zP4 = pOp->p4.z;
  903. if( zP4==0 ){
  904. zP4 = zTemp;
  905. zTemp[0] = 0;
  906. }
  907. }
  908. }
  909. assert( zP4!=0 );
  910. return zP4;
  911. }
  912. #endif
  913. /*
  914. ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
  915. **
  916. ** The prepared statements need to know in advance the complete set of
  917. ** attached databases that will be use. A mask of these databases
  918. ** is maintained in p->btreeMask. The p->lockMask value is the subset of
  919. ** p->btreeMask of databases that will require a lock.
  920. */
  921. void sqlite3VdbeUsesBtree(Vdbe *p, int i){
  922. assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
  923. assert( i<(int)sizeof(p->btreeMask)*8 );
  924. p->btreeMask |= ((yDbMask)1)<<i;
  925. if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
  926. p->lockMask |= ((yDbMask)1)<<i;
  927. }
  928. }
  929. #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
  930. /*
  931. ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
  932. ** this routine obtains the mutex associated with each BtShared structure
  933. ** that may be accessed by the VM passed as an argument. In doing so it also
  934. ** sets the BtShared.db member of each of the BtShared structures, ensuring
  935. ** that the correct busy-handler callback is invoked if required.
  936. **
  937. ** If SQLite is not threadsafe but does support shared-cache mode, then
  938. ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
  939. ** of all of BtShared structures accessible via the database handle
  940. ** associated with the VM.
  941. **
  942. ** If SQLite is not threadsafe and does not support shared-cache mode, this
  943. ** function is a no-op.
  944. **
  945. ** The p->btreeMask field is a bitmask of all btrees that the prepared
  946. ** statement p will ever use. Let N be the number of bits in p->btreeMask
  947. ** corresponding to btrees that use shared cache. Then the runtime of
  948. ** this routine is N*N. But as N is rarely more than 1, this should not
  949. ** be a problem.
  950. */
  951. void sqlite3VdbeEnter(Vdbe *p){
  952. int i;
  953. yDbMask mask;
  954. sqlite3 *db;
  955. Db *aDb;
  956. int nDb;
  957. if( p->lockMask==0 ) return; /* The common case */
  958. db = p->db;
  959. aDb = db->aDb;
  960. nDb = db->nDb;
  961. for(i=0, mask=1; i<nDb; i++, mask += mask){
  962. if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
  963. sqlite3BtreeEnter(aDb[i].pBt);
  964. }
  965. }
  966. }
  967. #endif
  968. #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
  969. /*
  970. ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
  971. */
  972. void sqlite3VdbeLeave(Vdbe *p){
  973. int i;
  974. yDbMask mask;
  975. sqlite3 *db;
  976. Db *aDb;
  977. int nDb;
  978. if( p->lockMask==0 ) return; /* The common case */
  979. db = p->db;
  980. aDb = db->aDb;
  981. nDb = db->nDb;
  982. for(i=0, mask=1; i<nDb; i++, mask += mask){
  983. if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
  984. sqlite3BtreeLeave(aDb[i].pBt);
  985. }
  986. }
  987. }
  988. #endif
  989. #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
  990. /*
  991. ** Print a single opcode. This routine is used for debugging only.
  992. */
  993. void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
  994. char *zP4;
  995. char zPtr[50];
  996. static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
  997. if( pOut==0 ) pOut = stdout;
  998. zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
  999. fprintf(pOut, zFormat1, pc,
  1000. sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
  1001. #ifdef SQLITE_DEBUG
  1002. pOp->zComment ? pOp->zComment : ""
  1003. #else
  1004. ""
  1005. #endif
  1006. );
  1007. fflush(pOut);
  1008. }
  1009. #endif
  1010. /*
  1011. ** Release an array of N Mem elements
  1012. */
  1013. static void releaseMemArray(Mem *p, int N){
  1014. if( p && N ){
  1015. Mem *pEnd;
  1016. sqlite3 *db = p->db;
  1017. u8 malloc_failed = db->mallocFailed;
  1018. if( db->pnBytesFreed ){
  1019. for(pEnd=&p[N]; p<pEnd; p++){
  1020. sqlite3DbFree(db, p->zMalloc);
  1021. }
  1022. return;
  1023. }
  1024. for(pEnd=&p[N]; p<pEnd; p++){
  1025. assert( (&p[1])==pEnd || p[0].db==p[1].db );
  1026. /* This block is really an inlined version of sqlite3VdbeMemRelease()
  1027. ** that takes advantage of the fact that the memory cell value is
  1028. ** being set to NULL after releasing any dynamic resources.
  1029. **
  1030. ** The justification for duplicating code is that according to
  1031. ** callgrind, this causes a certain test case to hit the CPU 4.7
  1032. ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
  1033. ** sqlite3MemRelease() were called from here. With -O2, this jumps
  1034. ** to 6.6 percent. The test case is inserting 1000 rows into a table
  1035. ** with no indexes using a single prepared INSERT statement, bind()
  1036. ** and reset(). Inserts are grouped into a transaction.
  1037. */
  1038. if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1039. sqlite3VdbeMemRelease(p);
  1040. }else if( p->zMalloc ){
  1041. sqlite3DbFree(db, p->zMalloc);
  1042. p->zMalloc = 0;
  1043. }
  1044. p->flags = MEM_Invalid;
  1045. }
  1046. db->mallocFailed = malloc_failed;
  1047. }
  1048. }
  1049. /*
  1050. ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
  1051. ** allocated by the OP_Program opcode in sqlite3VdbeExec().
  1052. */
  1053. void sqlite3VdbeFrameDelete(VdbeFrame *p){
  1054. int i;
  1055. Mem *aMem = VdbeFrameMem(p);
  1056. VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
  1057. for(i=0; i<p->nChildCsr; i++){
  1058. sqlite3VdbeFreeCursor(p->v, apCsr[i]);
  1059. }
  1060. releaseMemArray(aMem, p->nChildMem);
  1061. sqlite3DbFree(p->v->db, p);
  1062. }
  1063. #ifndef SQLITE_OMIT_EXPLAIN
  1064. /*
  1065. ** Give a listing of the program in the virtual machine.
  1066. **
  1067. ** The interface is the same as sqlite3VdbeExec(). But instead of
  1068. ** running the code, it invokes the callback once for each instruction.
  1069. ** This feature is used to implement "EXPLAIN".
  1070. **
  1071. ** When p->explain==1, each instruction is listed. When
  1072. ** p->explain==2, only OP_Explain instructions are listed and these
  1073. ** are shown in a different format. p->explain==2 is used to implement
  1074. ** EXPLAIN QUERY PLAN.
  1075. **
  1076. ** When p->explain==1, first the main program is listed, then each of
  1077. ** the trigger subprograms are listed one by one.
  1078. */
  1079. int sqlite3VdbeList(
  1080. Vdbe *p /* The VDBE */
  1081. ){
  1082. int nRow; /* Stop when row count reaches this */
  1083. int nSub = 0; /* Number of sub-vdbes seen so far */
  1084. SubProgram **apSub = 0; /* Array of sub-vdbes */
  1085. Mem *pSub = 0; /* Memory cell hold array of subprogs */
  1086. sqlite3 *db = p->db; /* The database connection */
  1087. int i; /* Loop counter */
  1088. int rc = SQLITE_OK; /* Return code */
  1089. Mem *pMem = &p->aMem[1]; /* First Mem of result set */
  1090. assert( p->explain );
  1091. assert( p->magic==VDBE_MAGIC_RUN );
  1092. assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
  1093. /* Even though this opcode does not use dynamic strings for
  1094. ** the result, result columns may become dynamic if the user calls
  1095. ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
  1096. */
  1097. releaseMemArray(pMem, 8);
  1098. p->pResultSet = 0;
  1099. if( p->rc==SQLITE_NOMEM ){
  1100. /* This happens if a malloc() inside a call to sqlite3_column_text() or
  1101. ** sqlite3_column_text16() failed. */
  1102. db->mallocFailed = 1;
  1103. return SQLITE_ERROR;
  1104. }
  1105. /* When the number of output rows reaches nRow, that means the
  1106. ** listing has finished and sqlite3_step() should return SQLITE_DONE.
  1107. ** nRow is the sum of the number of rows in the main program, plus
  1108. ** the sum of the number of rows in all trigger subprograms encountered
  1109. ** so far. The nRow value will increase as new trigger subprograms are
  1110. ** encountered, but p->pc will eventually catch up to nRow.
  1111. */
  1112. nRow = p->nOp;
  1113. if( p->explain==1 ){
  1114. /* The first 8 memory cells are used for the result set. So we will
  1115. ** commandeer the 9th cell to use as storage for an array of pointers
  1116. ** to trigger subprograms. The VDBE is guaranteed to have at least 9
  1117. ** cells. */
  1118. assert( p->nMem>9 );
  1119. pSub = &p->aMem[9];
  1120. if( pSub->flags&MEM_Blob ){
  1121. /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
  1122. ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
  1123. nSub = pSub->n/sizeof(Vdbe*);
  1124. apSub = (SubProgram **)pSub->z;
  1125. }
  1126. for(i=0; i<nSub; i++){
  1127. nRow += apSub[i]->nOp;
  1128. }
  1129. }
  1130. do{
  1131. i = p->pc++;
  1132. }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
  1133. if( i>=nRow ){
  1134. p->rc = SQLITE_OK;
  1135. rc = SQLITE_DONE;
  1136. }else if( db->u1.isInterrupted ){
  1137. p->rc = SQLITE_INTERRUPT;
  1138. rc = SQLITE_ERROR;
  1139. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
  1140. }else{
  1141. char *z;
  1142. Op *pOp;
  1143. if( i<p->nOp ){
  1144. /* The output line number is small enough that we are still in the
  1145. ** main program. */
  1146. pOp = &p->aOp[i];
  1147. }else{
  1148. /* We are currently listing subprograms. Figure out which one and
  1149. ** pick up the appropriate opcode. */
  1150. int j;
  1151. i -= p->nOp;
  1152. for(j=0; i>=apSub[j]->nOp; j++){
  1153. i -= apSub[j]->nOp;
  1154. }
  1155. pOp = &apSub[j]->aOp[i];
  1156. }
  1157. if( p->explain==1 ){
  1158. pMem->flags = MEM_Int;
  1159. pMem->type = SQLITE_INTEGER;
  1160. pMem->u.i = i; /* Program counter */
  1161. pMem++;
  1162. pMem->flags = MEM_Static|MEM_Str|MEM_Term;
  1163. pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
  1164. assert( pMem->z!=0 );
  1165. pMem->n = sqlite3Strlen30(pMem->z);
  1166. pMem->type = SQLITE_TEXT;
  1167. pMem->enc = SQLITE_UTF8;
  1168. pMem++;
  1169. /* When an OP_Program opcode is encounter (the only opcode that has
  1170. ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1171. ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
  1172. ** has not already been seen.
  1173. */
  1174. if( pOp->p4type==P4_SUBPROGRAM ){
  1175. int nByte = (nSub+1)*sizeof(SubProgram*);
  1176. int j;
  1177. for(j=0; j<nSub; j++){
  1178. if( apSub[j]==pOp->p4.pProgram ) break;
  1179. }
  1180. if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
  1181. apSub = (SubProgram **)pSub->z;
  1182. apSub[nSub++] = pOp->p4.pProgram;
  1183. pSub->flags |= MEM_Blob;
  1184. pSub->n = nSub*sizeof(SubProgram*);
  1185. }
  1186. }
  1187. }
  1188. pMem->flags = MEM_Int;
  1189. pMem->u.i = pOp->p1; /* P1 */
  1190. pMem->type = SQLITE_INTEGER;
  1191. pMem++;
  1192. pMem->flags = MEM_Int;
  1193. pMem->u.i = pOp->p2; /* P2 */
  1194. pMem->type = SQLITE_INTEGER;
  1195. pMem++;
  1196. pMem->flags = MEM_Int;
  1197. pMem->u.i = pOp->p3; /* P3 */
  1198. pMem->type = SQLITE_INTEGER;
  1199. pMem++;
  1200. if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
  1201. assert( p->db->mallocFailed );
  1202. return SQLITE_ERROR;
  1203. }
  1204. pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1205. z = displayP4(pOp, pMem->z, 32);
  1206. if( z!=pMem->z ){
  1207. sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
  1208. }else{
  1209. assert( pMem->z!=0 );
  1210. pMem->n = sqlite3Strlen30(pMem->z);
  1211. pMem->enc = SQLITE_UTF8;
  1212. }
  1213. pMem->type = SQLITE_TEXT;
  1214. pMem++;
  1215. if( p->explain==1 ){
  1216. if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
  1217. assert( p->db->mallocFailed );
  1218. return SQLITE_ERROR;
  1219. }
  1220. pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1221. pMem->n = 2;
  1222. sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
  1223. pMem->type = SQLITE_TEXT;
  1224. pMem->enc = SQLITE_UTF8;
  1225. pMem++;
  1226. #ifdef SQLITE_DEBUG
  1227. if( pOp->zComment ){
  1228. pMem->flags = MEM_Str|MEM_Term;
  1229. pMem->z = pOp->zComment;
  1230. pMem->n = sqlite3Strlen30(pMem->z);
  1231. pMem->enc = SQLITE_UTF8;
  1232. pMem->type = SQLITE_TEXT;
  1233. }else
  1234. #endif
  1235. {
  1236. pMem->flags = MEM_Null; /* Comment */
  1237. pMem->type = SQLITE_NULL;
  1238. }
  1239. }
  1240. p->nResColumn = 8 - 4*(p->explain-1);
  1241. p->pResultSet = &p->aMem[1];
  1242. p->rc = SQLITE_OK;
  1243. rc = SQLITE_ROW;
  1244. }
  1245. return rc;
  1246. }
  1247. #endif /* SQLITE_OMIT_EXPLAIN */
  1248. #ifdef SQLITE_DEBUG
  1249. /*
  1250. ** Print the SQL that was used to generate a VDBE program.
  1251. */
  1252. void sqlite3VdbePrintSql(Vdbe *p){
  1253. int nOp = p->nOp;
  1254. VdbeOp *pOp;
  1255. if( nOp<1 ) return;
  1256. pOp = &p->aOp[0];
  1257. if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
  1258. const char *z = pOp->p4.z;
  1259. while( sqlite3Isspace(*z) ) z++;
  1260. printf("SQL: [%s]\n", z);
  1261. }
  1262. }
  1263. #endif
  1264. #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
  1265. /*
  1266. ** Print an IOTRACE message showing SQL content.
  1267. */
  1268. void sqlite3VdbeIOTraceSql(Vdbe *p){
  1269. int nOp = p->nOp;
  1270. VdbeOp *pOp;
  1271. if( sqlite3IoTrace==0 ) return;
  1272. if( nOp<1 ) return;
  1273. pOp = &p->aOp[0];
  1274. if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
  1275. int i, j;
  1276. char z[1000];
  1277. sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
  1278. for(i=0; sqlite3Isspace(z[i]); i++){}
  1279. for(j=0; z[i]; i++){
  1280. if( sqlite3Isspace(z[i]) ){
  1281. if( z[i-1]!=' ' ){
  1282. z[j++] = ' ';
  1283. }
  1284. }else{
  1285. z[j++] = z[i];
  1286. }
  1287. }
  1288. z[j] = 0;
  1289. sqlite3IoTrace("SQL %s\n", z);
  1290. }
  1291. }
  1292. #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
  1293. /*
  1294. ** Allocate space from a fixed size buffer and return a pointer to
  1295. ** that space. If insufficient space is available, return NULL.
  1296. **
  1297. ** The pBuf parameter is the initial value of a pointer which will
  1298. ** receive the new memory. pBuf is normally NULL. If pBuf is not
  1299. ** NULL, it means that memory space has already been allocated and that
  1300. ** this routine should not allocate any new memory. When pBuf is not
  1301. ** NULL simply return pBuf. Only allocate new memory space when pBuf
  1302. ** is NULL.
  1303. **
  1304. ** nByte is the number of bytes of space needed.
  1305. **
  1306. ** *ppFrom points to available space and pEnd points to the end of the
  1307. ** available space. When space is allocated, *ppFrom is advanced past
  1308. ** the end of the allocated space.
  1309. **
  1310. ** *pnByte is a counter of the number of bytes of space that have failed
  1311. ** to allocate. If there is insufficient space in *ppFrom to satisfy the
  1312. ** request, then increment *pnByte by the amount of the request.
  1313. */
  1314. static void *allocSpace(
  1315. void *pBuf, /* Where return pointer will be stored */
  1316. int nByte, /* Number of bytes to allocate */
  1317. u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
  1318. u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
  1319. int *pnByte /* If allocation cannot be made, increment *pnByte */
  1320. ){
  1321. assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
  1322. if( pBuf ) return pBuf;
  1323. nByte = ROUND8(nByte);
  1324. if( &(*ppFrom)[nByte] <= pEnd ){
  1325. pBuf = (void*)*ppFrom;
  1326. *ppFrom += nByte;
  1327. }else{
  1328. *pnByte += nByte;
  1329. }
  1330. return pBuf;
  1331. }
  1332. /*
  1333. ** Rewind the VDBE back to the beginning in preparation for
  1334. ** running it.
  1335. */
  1336. void sqlite3VdbeRewind(Vdbe *p){
  1337. #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
  1338. int i;
  1339. #endif
  1340. assert( p!=0 );
  1341. assert( p->magic==VDBE_MAGIC_INIT );
  1342. /* There should be at least one opcode.
  1343. */
  1344. assert( p->nOp>0 );
  1345. /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
  1346. p->magic = VDBE_MAGIC_RUN;
  1347. #ifdef SQLITE_DEBUG
  1348. for(i=1; i<p->nMem; i++){
  1349. assert( p->aMem[i].db==p->db );
  1350. }
  1351. #endif
  1352. p->pc = -1;
  1353. p->rc = SQLITE_OK;
  1354. p->errorAction = OE_Abort;
  1355. p->magic = VDBE_MAGIC_RUN;
  1356. p->nChange = 0;
  1357. p->cacheCtr = 1;
  1358. p->minWriteFileFormat = 255;
  1359. p->iStatement = 0;
  1360. p->nFkConstraint = 0;
  1361. #ifdef VDBE_PROFILE
  1362. for(i=0; i<p->nOp; i++){
  1363. p->aOp[i].cnt = 0;
  1364. p->aOp[i].cycles = 0;
  1365. }
  1366. #endif
  1367. }
  1368. /*
  1369. ** Prepare a virtual machine for execution for the first time after
  1370. ** creating the virtual machine. This involves things such
  1371. ** as allocating stack space and initializing the program counter.
  1372. ** After the VDBE has be prepped, it can be executed by one or more
  1373. ** calls to sqlite3VdbeExec().
  1374. **
  1375. ** This function may be called exact once on a each virtual machine.
  1376. ** After this routine is called the VM has been "packaged" and is ready
  1377. ** to run. After this routine is called, futher calls to
  1378. ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
  1379. ** the Vdbe from the Parse object that helped generate it so that the
  1380. ** the Vdbe becomes an independent entity and the Parse object can be
  1381. ** destroyed.
  1382. **
  1383. ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
  1384. ** to its initial state after it has been run.
  1385. */
  1386. void sqlite3VdbeMakeReady(
  1387. Vdbe *p, /* The VDBE */
  1388. Parse *pParse /* Parsing context */
  1389. ){
  1390. sqlite3 *db; /* The database connection */
  1391. int nVar; /* Number of parameters */
  1392. int nMem; /* Number of VM memory registers */
  1393. int nCursor; /* Number of cursors required */
  1394. int nArg; /* Number of arguments in subprograms */
  1395. int nOnce; /* Number of OP_Once instructions */
  1396. int n; /* Loop counter */
  1397. u8 *zCsr; /* Memory available for allocation */
  1398. u8 *zEnd; /* First byte past allocated memory */
  1399. int nByte; /* How much extra memory is needed */
  1400. assert( p!=0 );
  1401. assert( p->nOp>0 );
  1402. assert( pParse!=0 );
  1403. assert( p->magic==VDBE_MAGIC_INIT );
  1404. db = p->db;
  1405. assert( db->mallocFailed==0 );
  1406. nVar = pParse->nVar;
  1407. nMem = pParse->nMem;
  1408. nCursor = pParse->nTab;
  1409. nArg = pParse->nMaxArg;
  1410. nOnce = pParse->nOnce;
  1411. if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
  1412. /* For each cursor required, also allocate a memory cell. Memory
  1413. ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
  1414. ** the vdbe program. Instead they are used to allocate space for
  1415. ** VdbeCursor/BtCursor structures. The blob of memory associated with
  1416. ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
  1417. ** stores the blob of memory associated with cursor 1, etc.
  1418. **
  1419. ** See also: allocateCursor().
  1420. */
  1421. nMem += nCursor;
  1422. /* Allocate space for memory registers, SQL variables, VDBE cursors and
  1423. ** an array to marshal SQL function arguments in.
  1424. */
  1425. zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
  1426. zEnd = (u8*)&p->aOp[p->nOpAlloc]; /* First byte past end of zCsr[] */
  1427. resolveP2Values(p, &nArg);
  1428. p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
  1429. if( pParse->explain && nMem<10 ){
  1430. nMem = 10;
  1431. }
  1432. memset(zCsr, 0, zEnd-zCsr);
  1433. zCsr += (zCsr - (u8*)0)&7;
  1434. assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
  1435. p->expired = 0;
  1436. /* Memory for registers, parameters, cursor, etc, is allocated in two
  1437. ** passes. On the first pass, we try to reuse unused space at the
  1438. ** end of the opcode array. If we are unable to satisfy all memory
  1439. ** requirements by reusing the opcode array tail, then the second
  1440. ** pass will fill in the rest using a fresh allocation.
  1441. **
  1442. ** This two-pass approach that reuses as much memory as possible from
  1443. ** the leftover space at the end of the opcode array can significantly
  1444. ** reduce the amount of memory held by a prepared statement.
  1445. */
  1446. do {
  1447. nByte = 0;
  1448. p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
  1449. p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
  1450. p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
  1451. p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
  1452. p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
  1453. &zCsr, zEnd, &nByte);
  1454. p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
  1455. if( nByte ){
  1456. p->pFree = sqlite3DbMallocZero(db, nByte);
  1457. }
  1458. zCsr = p->pFree;
  1459. zEnd = &zCsr[nByte];
  1460. }while( nByte && !db->mallocFailed );
  1461. p->nCursor = nCursor;
  1462. p->nOnceFlag = nOnce;
  1463. if( p->aVar ){
  1464. p->nVar = (ynVar)nVar;
  1465. for(n=0; n<nVar; n++){
  1466. p->aVar[n].flags = MEM_Null;
  1467. p->aVar[n].db = db;
  1468. }
  1469. }
  1470. if( p->azVar ){
  1471. p->nzVar = pParse->nzVar;
  1472. memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
  1473. memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
  1474. }
  1475. if( p->aMem ){
  1476. p->aMem--; /* aMem[] goes from 1..nMem */
  1477. p->nMem = nMem; /* not from 0..nMem-1 */
  1478. for(n=1; n<=nMem; n++){
  1479. p->aMem[n].flags = MEM_Invalid;
  1480. p->aMem[n].db = db;
  1481. }
  1482. }
  1483. p->explain = pParse->explain;
  1484. sqlite3VdbeRewind(p);
  1485. }
  1486. /*
  1487. ** Close a VDBE cursor and release all the resources that cursor
  1488. ** happens to hold.
  1489. */
  1490. void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
  1491. if( pCx==0 ){
  1492. return;
  1493. }
  1494. sqlite3VdbeSorterClose(p->db, pCx);
  1495. if( pCx->pBt ){
  1496. sqlite3BtreeClose(pCx->pBt);
  1497. /* The pCx->pCursor will be close automatically, if it exists, by
  1498. ** the call above. */
  1499. }else if( pCx->pCursor ){
  1500. sqlite3BtreeCloseCursor(pCx->pCursor);
  1501. }
  1502. #ifndef SQLITE_OMIT_VIRTUALTABLE
  1503. if( pCx->pVtabCursor ){
  1504. sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  1505. const sqlite3_module *pModule = pCx->pModule;
  1506. p->inVtabMethod = 1;
  1507. pModule->xClose(pVtabCursor);
  1508. p->inVtabMethod = 0;
  1509. }
  1510. #endif
  1511. }
  1512. /*
  1513. ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
  1514. ** is used, for example, when a trigger sub-program is halted to restore
  1515. ** control to the main program.
  1516. */
  1517. int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
  1518. Vdbe *v = pFrame->v;
  1519. v->aOnceFlag = pFrame->aOnceFlag;
  1520. v->nOnceFlag = pFrame->nOnceFlag;
  1521. v->aOp = pFrame->aOp;
  1522. v->nOp = pFrame->nOp;
  1523. v->aMem = pFrame->aMem;
  1524. v->nMem = pFrame->nMem;
  1525. v->apCsr = pFrame->apCsr;
  1526. v->nCursor = pFrame->nCursor;
  1527. v->db->lastRowid = pFrame->lastRowid;
  1528. v->nChange = pFrame->nChange;
  1529. return pFrame->pc;
  1530. }
  1531. /*
  1532. ** Close all cursors.
  1533. **
  1534. ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
  1535. ** cell array. This is necessary as the memory cell array may contain
  1536. ** pointers to VdbeFrame objects, which may in turn contain pointers to
  1537. ** open cursors.
  1538. */
  1539. static void closeAllCursors(Vdbe *p){
  1540. if( p->pFrame ){
  1541. VdbeFrame *pFrame;
  1542. for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  1543. sqlite3VdbeFrameRestore(pFrame);
  1544. }
  1545. p->pFrame = 0;
  1546. p->nFrame = 0;
  1547. if( p->apCsr ){
  1548. int i;
  1549. for(i=0; i<p->nCursor; i++){
  1550. VdbeCursor *pC = p->apCsr[i];
  1551. if( pC ){
  1552. sqlite3VdbeFreeCursor(p, pC);
  1553. p->apCsr[i] = 0;
  1554. }
  1555. }
  1556. }
  1557. if( p->aMem ){
  1558. releaseMemArray(&p->aMem[1], p->nMem);
  1559. }
  1560. while( p->pDelFrame ){
  1561. VdbeFrame *pDel = p->pDelFrame;
  1562. p->pDelFrame = pDel->pParent;
  1563. sqlite3VdbeFrameDelete(pDel);
  1564. }
  1565. /* Delete any auxdata allocations made by the VM */
  1566. sqlite3VdbeDeleteAuxData(p, -1, 0);
  1567. assert( p->pAuxData==0 );
  1568. }
  1569. /*
  1570. ** Clean up the VM after execution.
  1571. **
  1572. ** This routine will automatically close any cursors, lists, and/or
  1573. ** sorters that were left open. It also deletes the values of
  1574. ** variables in the aVar[] array.
  1575. */
  1576. static void Cleanup(Vdbe *p){
  1577. sqlite3 *db = p->db;
  1578. #ifdef SQLITE_DEBUG
  1579. /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
  1580. ** Vdbe.aMem[] arrays have already been cleaned up. */
  1581. int i;
  1582. if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
  1583. if( p->aMem ){
  1584. for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
  1585. }
  1586. #endif
  1587. sqlite3DbFree(db, p->zErrMsg);
  1588. p->zErrMsg = 0;
  1589. p->pResultSet = 0;
  1590. }
  1591. /*
  1592. ** Set the number of result columns that will be returned by this SQL
  1593. ** statement. This is now set at compile time, rather than during
  1594. ** execution of the vdbe program so that sqlite3_column_count() can
  1595. ** be called on an SQL statement before sqlite3_step().
  1596. */
  1597. void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
  1598. Mem *pColName;
  1599. int n;
  1600. sqlite3 *db = p->db;
  1601. releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  1602. sqlite3DbFree(db, p->aColName);
  1603. n = nResColumn*COLNAME_N;
  1604. p->nResColumn = (u16)nResColumn;
  1605. p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
  1606. if( p->aColName==0 ) return;
  1607. while( n-- > 0 ){
  1608. pColName->flags = MEM_Null;
  1609. pColName->db = p->db;
  1610. pColName++;
  1611. }
  1612. }
  1613. /*
  1614. ** Set the name of the idx'th column to be returned by the SQL statement.
  1615. ** zName must be a pointer to a nul terminated string.
  1616. **
  1617. ** This call must be made after a call to sqlite3VdbeSetNumCols().
  1618. **
  1619. ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
  1620. ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
  1621. ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
  1622. */
  1623. int sqlite3VdbeSetColName(
  1624. Vdbe *p, /* Vdbe being configured */
  1625. int idx, /* Index of column zName applies to */
  1626. int var, /* One of the COLNAME_* constants */
  1627. const char *zName, /* Pointer to buffer containing name */
  1628. void (*xDel)(void*) /* Memory management strategy for zName */
  1629. ){
  1630. int rc;
  1631. Mem *pColName;
  1632. assert( idx<p->nResColumn );
  1633. assert( var<COLNAME_N );
  1634. if( p->db->mallocFailed ){
  1635. assert( !zName || xDel!=SQLITE_DYNAMIC );
  1636. return SQLITE_NOMEM;
  1637. }
  1638. assert( p->aColName!=0 );
  1639. pColName = &(p->aColName[idx+var*p->nResColumn]);
  1640. rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
  1641. assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
  1642. return rc;
  1643. }
  1644. /*
  1645. ** A read or write transaction may or may not be active on database handle
  1646. ** db. If a transaction is active, commit it. If there is a
  1647. ** write-transaction spanning more than one database file, this routine
  1648. ** takes care of the master journal trickery.
  1649. */
  1650. static int vdbeCommit(sqlite3 *db, Vdbe *p){
  1651. int i;
  1652. int nTrans = 0; /* Number of databases with an active write-transaction */
  1653. int rc = SQLITE_OK;
  1654. int needXcommit = 0;
  1655. #ifdef SQLITE_OMIT_VIRTUALTABLE
  1656. /* With this option, sqlite3VtabSync() is defined to be simply
  1657. ** SQLITE_OK so p is not used.
  1658. */
  1659. UNUSED_PARAMETER(p);
  1660. #endif
  1661. /* Before doing anything else, call the xSync() callback for any
  1662. ** virtual module tables written in this transaction. This has to
  1663. ** be done before determining whether a master journal file is
  1664. ** required, as an xSync() callback may add an attached database
  1665. ** to the transaction.
  1666. */
  1667. rc = sqlite3VtabSync(db, p);
  1668. /* This loop determines (a) if the commit hook should be invoked and
  1669. ** (b) how many database files have open write transactions, not
  1670. ** including the temp database. (b) is important because if more than
  1671. ** one database file has an open write transaction, a master journal
  1672. ** file is required for an atomic commit.
  1673. */
  1674. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1675. Btree *pBt = db->aDb[i].pBt;
  1676. if( sqlite3BtreeIsInTrans(pBt) ){
  1677. needXcommit = 1;
  1678. if( i!=1 ) nTrans++;
  1679. sqlite3BtreeEnter(pBt);
  1680. rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
  1681. sqlite3BtreeLeave(pBt);
  1682. }
  1683. }
  1684. if( rc!=SQLITE_OK ){
  1685. return rc;
  1686. }
  1687. /* If there are any write-transactions at all, invoke the commit hook */
  1688. if( needXcommit && db->xCommitCallback ){
  1689. rc = db->xCommitCallback(db->pCommitArg);
  1690. if( rc ){
  1691. return SQLITE_CONSTRAINT_COMMITHOOK;
  1692. }
  1693. }
  1694. /* The simple case - no more than one database file (not counting the
  1695. ** TEMP database) has a transaction active. There is no need for the
  1696. ** master-journal.
  1697. **
  1698. ** If the return value of sqlite3BtreeGetFilename() is a zero length
  1699. ** string, it means the main database is :memory: or a temp file. In
  1700. ** that case we do not support atomic multi-file commits, so use the
  1701. ** simple case then too.
  1702. */
  1703. if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
  1704. || nTrans<=1
  1705. ){
  1706. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1707. Btree *pBt = db->aDb[i].pBt;
  1708. if( pBt ){
  1709. rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
  1710. }
  1711. }
  1712. /* Do the commit only if all databases successfully complete phase 1.
  1713. ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
  1714. ** IO error while deleting or truncating a journal file. It is unlikely,
  1715. ** but could happen. In this case abandon processing and return the error.
  1716. */
  1717. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1718. Btree *pBt = db->aDb[i].pBt;
  1719. if( pBt ){
  1720. rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
  1721. }
  1722. }
  1723. if( rc==SQLITE_OK ){
  1724. sqlite3VtabCommit(db);
  1725. }
  1726. }
  1727. /* The complex case - There is a multi-file write-transaction active.
  1728. ** This requires a master journal file to ensure the transaction is
  1729. ** committed atomicly.
  1730. */
  1731. #ifndef SQLITE_OMIT_DISKIO
  1732. else{
  1733. sqlite3_vfs *pVfs = db->pVfs;
  1734. int needSync = 0;
  1735. char *zMaster = 0; /* File-name for the master journal */
  1736. char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
  1737. sqlite3_file *pMaster = 0;
  1738. i64 offset = 0;
  1739. int res;
  1740. int retryCount = 0;
  1741. int nMainFile;
  1742. /* Select a master journal file name */
  1743. nMainFile = sqlite3Strlen30(zMainFile);
  1744. zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
  1745. if( zMaster==0 ) return SQLITE_NOMEM;
  1746. do {
  1747. u32 iRandom;
  1748. if( retryCount ){
  1749. if( retryCount>100 ){
  1750. sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
  1751. sqlite3OsDelete(pVfs, zMaster, 0);
  1752. break;
  1753. }else if( retryCount==1 ){
  1754. sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
  1755. }
  1756. }
  1757. retryCount++;
  1758. sqlite3_randomness(sizeof(iRandom), &iRandom);
  1759. sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
  1760. (iRandom>>8)&0xffffff, iRandom&0xff);
  1761. /* The antipenultimate character of the master journal name must
  1762. ** be "9" to avoid name collisions when using 8+3 filenames. */
  1763. assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
  1764. sqlite3FileSuffix3(zMainFile, zMaster);
  1765. rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
  1766. }while( rc==SQLITE_OK && res );
  1767. if( rc==SQLITE_OK ){
  1768. /* Open the master journal. */
  1769. rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
  1770. SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
  1771. SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
  1772. );
  1773. }
  1774. if( rc!=SQLITE_OK ){
  1775. sqlite3DbFree(db, zMaster);
  1776. return rc;
  1777. }
  1778. /* Write the name of each database file in the transaction into the new
  1779. ** master journal file. If an error occurs at this point close
  1780. ** and delete the master journal file. All the individual journal files
  1781. ** still have 'null' as the master journal pointer, so they will roll
  1782. ** back independently if a failure occurs.
  1783. */
  1784. for(i=0; i<db->nDb; i++){
  1785. Btree *pBt = db->aDb[i].pBt;
  1786. if( sqlite3BtreeIsInTrans(pBt) ){
  1787. char const *zFile = sqlite3BtreeGetJournalname(pBt);
  1788. if( zFile==0 ){
  1789. continue; /* Ignore TEMP and :memory: databases */
  1790. }
  1791. assert( zFile[0]!=0 );
  1792. if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
  1793. needSync = 1;
  1794. }
  1795. rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
  1796. offset += sqlite3Strlen30(zFile)+1;
  1797. if( rc!=SQLITE_OK ){
  1798. sqlite3OsCloseFree(pMaster);
  1799. sqlite3OsDelete(pVfs, zMaster, 0);
  1800. sqlite3DbFree(db, zMaster);
  1801. return rc;
  1802. }
  1803. }
  1804. }
  1805. /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
  1806. ** flag is set this is not required.
  1807. */
  1808. if( needSync
  1809. && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
  1810. && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
  1811. ){
  1812. sqlite3OsCloseFree(pMaster);
  1813. sqlite3OsDelete(pVfs, zMaster, 0);
  1814. sqlite3DbFree(db, zMaster);
  1815. return rc;
  1816. }
  1817. /* Sync all the db files involved in the transaction. The same call
  1818. ** sets the master journal pointer in each individual journal. If
  1819. ** an error occurs here, do not delete the master journal file.
  1820. **
  1821. ** If the error occurs during the first call to
  1822. ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
  1823. ** master journal file will be orphaned. But we cannot delete it,
  1824. ** in case the master journal file name was written into the journal
  1825. ** file before the failure occurred.
  1826. */
  1827. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1828. Btree *pBt = db->aDb[i].pBt;
  1829. if( pBt ){
  1830. rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
  1831. }
  1832. }
  1833. sqlite3OsCloseFree(pMaster);
  1834. assert( rc!=SQLITE_BUSY );
  1835. if( rc!=SQLITE_OK ){
  1836. sqlite3DbFree(db, zMaster);
  1837. return rc;
  1838. }
  1839. /* Delete the master journal file. This commits the transaction. After
  1840. ** doing this the directory is synced again before any individual
  1841. ** transaction files are deleted.
  1842. */
  1843. rc = sqlite3OsDelete(pVfs, zMaster, 1);
  1844. sqlite3DbFree(db, zMaster);
  1845. zMaster = 0;
  1846. if( rc ){
  1847. return rc;
  1848. }
  1849. /* All files and directories have already been synced, so the following
  1850. ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
  1851. ** deleting or truncating journals. If something goes wrong while
  1852. ** this is happening we don't really care. The integrity of the
  1853. ** transaction is already guaranteed, but some stray 'cold' journals
  1854. ** may be lying around. Returning an error code won't help matters.
  1855. */
  1856. disable_simulated_io_errors();
  1857. sqlite3BeginBenignMalloc();
  1858. for(i=0; i<db->nDb; i++){
  1859. Btree *pBt = db->aDb[i].pBt;
  1860. if( pBt ){
  1861. sqlite3BtreeCommitPhaseTwo(pBt, 1);
  1862. }
  1863. }
  1864. sqlite3EndBenignMalloc();
  1865. enable_simulated_io_errors();
  1866. sqlite3VtabCommit(db);
  1867. }
  1868. #endif
  1869. return rc;
  1870. }
  1871. /*
  1872. ** This routine checks that the sqlite3.nVdbeActive count variable
  1873. ** matches the number of vdbe's in the list sqlite3.pVdbe that are
  1874. ** currently active. An assertion fails if the two counts do not match.
  1875. ** This is an internal self-check only - it is not an essential processing
  1876. ** step.
  1877. **
  1878. ** This is a no-op if NDEBUG is defined.
  1879. */
  1880. #ifndef NDEBUG
  1881. static void checkActiveVdbeCnt(sqlite3 *db){
  1882. Vdbe *p;
  1883. int cnt = 0;
  1884. int nWrite = 0;
  1885. int nRead = 0;
  1886. p = db->pVdbe;
  1887. while( p ){
  1888. if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
  1889. cnt++;
  1890. if( p->readOnly==0 ) nWrite++;
  1891. if( p->bIsReader ) nRead++;
  1892. }
  1893. p = p->pNext;
  1894. }
  1895. assert( cnt==db->nVdbeActive );
  1896. assert( nWrite==db->nVdbeWrite );
  1897. assert( nRead==db->nVdbeRead );
  1898. }
  1899. #else
  1900. #define checkActiveVdbeCnt(x)
  1901. #endif
  1902. /*
  1903. ** If the Vdbe passed as the first argument opened a statement-transaction,
  1904. ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
  1905. ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
  1906. ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
  1907. ** statement transaction is committed.
  1908. **
  1909. ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
  1910. ** Otherwise SQLITE_OK.
  1911. */
  1912. int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
  1913. sqlite3 *const db = p->db;
  1914. int rc = SQLITE_OK;
  1915. /* If p->iStatement is greater than zero, then this Vdbe opened a
  1916. ** statement transaction that should be closed here. The only exception
  1917. ** is that an IO error may have occurred, causing an emergency rollback.
  1918. ** In this case (db->nStatement==0), and there is nothing to do.
  1919. */
  1920. if( db->nStatement && p->iStatement ){
  1921. int i;
  1922. const int iSavepoint = p->iStatement-1;
  1923. assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
  1924. assert( db->nStatement>0 );
  1925. assert( p->iStatement==(db->nStatement+db->nSavepoint) );
  1926. for(i=0; i<db->nDb; i++){
  1927. int rc2 = SQLITE_OK;
  1928. Btree *pBt = db->aDb[i].pBt;
  1929. if( pBt ){
  1930. if( eOp==SAVEPOINT_ROLLBACK ){
  1931. rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
  1932. }
  1933. if( rc2==SQLITE_OK ){
  1934. rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
  1935. }
  1936. if( rc==SQLITE_OK ){
  1937. rc = rc2;
  1938. }
  1939. }
  1940. }
  1941. db->nStatement--;
  1942. p->iStatement = 0;
  1943. if( rc==SQLITE_OK ){
  1944. if( eOp==SAVEPOINT_ROLLBACK ){
  1945. rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
  1946. }
  1947. if( rc==SQLITE_OK ){
  1948. rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
  1949. }
  1950. }
  1951. /* If the statement transaction is being rolled back, also restore the
  1952. ** database handles deferred constraint counter to the value it had when
  1953. ** the statement transaction was opened. */
  1954. if( eOp==SAVEPOINT_ROLLBACK ){
  1955. db->nDeferredCons = p->nStmtDefCons;
  1956. db->nDeferredImmCons = p->nStmtDefImmCons;
  1957. }
  1958. }
  1959. return rc;
  1960. }
  1961. /*
  1962. ** This function is called when a transaction opened by the database
  1963. ** handle associated with the VM passed as an argument is about to be
  1964. ** committed. If there are outstanding deferred foreign key constraint
  1965. ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
  1966. **
  1967. ** If there are outstanding FK violations and this function returns
  1968. ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
  1969. ** and write an error message to it. Then return SQLITE_ERROR.
  1970. */
  1971. #ifndef SQLITE_OMIT_FOREIGN_KEY
  1972. int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
  1973. sqlite3 *db = p->db;
  1974. if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
  1975. || (!deferred && p->nFkConstraint>0)
  1976. ){
  1977. p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
  1978. p->errorAction = OE_Abort;
  1979. sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
  1980. return SQLITE_ERROR;
  1981. }
  1982. return SQLITE_OK;
  1983. }
  1984. #endif
  1985. /*
  1986. ** This routine is called the when a VDBE tries to halt. If the VDBE
  1987. ** has made changes and is in autocommit mode, then commit those
  1988. ** changes. If a rollback is needed, then do the rollback.
  1989. **
  1990. ** This routine is the only way to move the state of a VM from
  1991. ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
  1992. ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
  1993. **
  1994. ** Return an error code. If the commit could not complete because of
  1995. ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
  1996. ** means the close did not happen and needs to be repeated.
  1997. */
  1998. int sqlite3VdbeHalt(Vdbe *p){
  1999. int rc; /* Used to store transient return codes */
  2000. sqlite3 *db = p->db;
  2001. /* This function contains the logic that determines if a statement or
  2002. ** transaction will be committed or rolled back as a result of the
  2003. ** execution of this virtual machine.
  2004. **
  2005. ** If any of the following errors occur:
  2006. **
  2007. ** SQLITE_NOMEM
  2008. ** SQLITE_IOERR
  2009. ** SQLITE_FULL
  2010. ** SQLITE_INTERRUPT
  2011. **
  2012. ** Then the internal cache might have been left in an inconsistent
  2013. ** state. We need to rollback the statement transaction, if there is
  2014. ** one, or the complete transaction if there is no statement transaction.
  2015. */
  2016. if( p->db->mallocFailed ){
  2017. p->rc = SQLITE_NOMEM;
  2018. }
  2019. if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
  2020. closeAllCursors(p);
  2021. if( p->magic!=VDBE_MAGIC_RUN ){
  2022. return SQLITE_OK;
  2023. }
  2024. checkActiveVdbeCnt(db);
  2025. /* No commit or rollback needed if the program never started or if the
  2026. ** SQL statement does not read or write a database file. */
  2027. if( p->pc>=0 && p->bIsReader ){
  2028. int mrc; /* Primary error code from p->rc */
  2029. int eStatementOp = 0;
  2030. int isSpecialError; /* Set to true if a 'special' error */
  2031. /* Lock all btrees used by the statement */
  2032. sqlite3VdbeEnter(p);
  2033. /* Check for one of the special errors */
  2034. mrc = p->rc & 0xff;
  2035. assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
  2036. isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
  2037. || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
  2038. if( isSpecialError ){
  2039. /* If the query was read-only and the error code is SQLITE_INTERRUPT,
  2040. ** no rollback is necessary. Otherwise, at least a savepoint
  2041. ** transaction must be rolled back to restore the database to a
  2042. ** consistent state.
  2043. **
  2044. ** Even if the statement is read-only, it is important to perform
  2045. ** a statement or transaction rollback operation. If the error
  2046. ** occurred while writing to the journal, sub-journal or database
  2047. ** file as part of an effort to free up cache space (see function
  2048. ** pagerStress() in pager.c), the rollback is required to restore
  2049. ** the pager to a consistent state.
  2050. */
  2051. if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
  2052. if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
  2053. eStatementOp = SAVEPOINT_ROLLBACK;
  2054. }else{
  2055. /* We are forced to roll back the active transaction. Before doing
  2056. ** so, abort any other statements this handle currently has active.
  2057. */
  2058. sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  2059. sqlite3CloseSavepoints(db);
  2060. db->autoCommit = 1;
  2061. }
  2062. }
  2063. }
  2064. /* Check for immediate foreign key violations. */
  2065. if( p->rc==SQLITE_OK ){
  2066. sqlite3VdbeCheckFk(p, 0);
  2067. }
  2068. /* If the auto-commit flag is set and this is the only active writer
  2069. ** VM, then we do either a commit or rollback of the current transaction.
  2070. **
  2071. ** Note: This block also runs if one of the special errors handled
  2072. ** above has occurred.
  2073. */
  2074. if( !sqlite3VtabInSync(db)
  2075. && db->autoCommit
  2076. && db->nVdbeWrite==(p->readOnly==0)
  2077. ){
  2078. if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
  2079. rc = sqlite3VdbeCheckFk(p, 1);
  2080. if( rc!=SQLITE_OK ){
  2081. if( NEVER(p->readOnly) ){
  2082. sqlite3VdbeLeave(p);
  2083. return SQLITE_ERROR;
  2084. }
  2085. rc = SQLITE_CONSTRAINT_FOREIGNKEY;
  2086. }else{
  2087. /* The auto-commit flag is true, the vdbe program was successful
  2088. ** or hit an 'OR FAIL' constraint and there are no deferred foreign
  2089. ** key constraints to hold up the transaction. This means a commit
  2090. ** is required. */
  2091. rc = vdbeCommit(db, p);
  2092. }
  2093. if( rc==SQLITE_BUSY && p->readOnly ){
  2094. sqlite3VdbeLeave(p);
  2095. return SQLITE_BUSY;
  2096. }else if( rc!=SQLITE_OK ){
  2097. p->rc = rc;
  2098. sqlite3RollbackAll(db, SQLITE_OK);
  2099. }else{
  2100. db->nDeferredCons = 0;
  2101. db->nDeferredImmCons = 0;
  2102. db->flags &= ~SQLITE_DeferFKs;
  2103. sqlite3CommitInternalChanges(db);
  2104. }
  2105. }else{
  2106. sqlite3RollbackAll(db, SQLITE_OK);
  2107. }
  2108. db->nStatement = 0;
  2109. }else if( eStatementOp==0 ){
  2110. if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
  2111. eStatementOp = SAVEPOINT_RELEASE;
  2112. }else if( p->errorAction==OE_Abort ){
  2113. eStatementOp = SAVEPOINT_ROLLBACK;
  2114. }else{
  2115. sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  2116. sqlite3CloseSavepoints(db);
  2117. db->autoCommit = 1;
  2118. }
  2119. }
  2120. /* If eStatementOp is non-zero, then a statement transaction needs to
  2121. ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
  2122. ** do so. If this operation returns an error, and the current statement
  2123. ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
  2124. ** current statement error code.
  2125. */
  2126. if( eStatementOp ){
  2127. rc = sqlite3VdbeCloseStatement(p, eStatementOp);
  2128. if( rc ){
  2129. if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
  2130. p->rc = rc;
  2131. sqlite3DbFree(db, p->zErrMsg);
  2132. p->zErrMsg = 0;
  2133. }
  2134. sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  2135. sqlite3CloseSavepoints(db);
  2136. db->autoCommit = 1;
  2137. }
  2138. }
  2139. /* If this was an INSERT, UPDATE or DELETE and no statement transaction
  2140. ** has been rolled back, update the database connection change-counter.
  2141. */
  2142. if( p->changeCntOn ){
  2143. if( eStatementOp!=SAVEPOINT_ROLLBACK ){
  2144. sqlite3VdbeSetChanges(db, p->nChange);
  2145. }else{
  2146. sqlite3VdbeSetChanges(db, 0);
  2147. }
  2148. p->nChange = 0;
  2149. }
  2150. /* Release the locks */
  2151. sqlite3VdbeLeave(p);
  2152. }
  2153. /* We have successfully halted and closed the VM. Record this fact. */
  2154. if( p->pc>=0 ){
  2155. db->nVdbeActive--;
  2156. if( !p->readOnly ) db->nVdbeWrite--;
  2157. if( p->bIsReader ) db->nVdbeRead--;
  2158. assert( db->nVdbeActive>=db->nVdbeRead );
  2159. assert( db->nVdbeRead>=db->nVdbeWrite );
  2160. assert( db->nVdbeWrite>=0 );
  2161. }
  2162. p->magic = VDBE_MAGIC_HALT;
  2163. checkActiveVdbeCnt(db);
  2164. if( p->db->mallocFailed ){
  2165. p->rc = SQLITE_NOMEM;
  2166. }
  2167. /* If the auto-commit flag is set to true, then any locks that were held
  2168. ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
  2169. ** to invoke any required unlock-notify callbacks.
  2170. */
  2171. if( db->autoCommit ){
  2172. sqlite3ConnectionUnlocked(db);
  2173. }
  2174. assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
  2175. return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
  2176. }
  2177. /*
  2178. ** Each VDBE holds the result of the most recent sqlite3_step() call
  2179. ** in p->rc. This routine sets that result back to SQLITE_OK.
  2180. */
  2181. void sqlite3VdbeResetStepResult(Vdbe *p){
  2182. p->rc = SQLITE_OK;
  2183. }
  2184. /*
  2185. ** Copy the error code and error message belonging to the VDBE passed
  2186. ** as the first argument to its database handle (so that they will be
  2187. ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
  2188. **
  2189. ** This function does not clear the VDBE error code or message, just
  2190. ** copies them to the database handle.
  2191. */
  2192. int sqlite3VdbeTransferError(Vdbe *p){
  2193. sqlite3 *db = p->db;
  2194. int rc = p->rc;
  2195. if( p->zErrMsg ){
  2196. u8 mallocFailed = db->mallocFailed;
  2197. sqlite3BeginBenignMalloc();
  2198. sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
  2199. sqlite3EndBenignMalloc();
  2200. db->mallocFailed = mallocFailed;
  2201. db->errCode = rc;
  2202. }else{
  2203. sqlite3Error(db, rc, 0);
  2204. }
  2205. return rc;
  2206. }
  2207. #ifdef SQLITE_ENABLE_SQLLOG
  2208. /*
  2209. ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
  2210. ** invoke it.
  2211. */
  2212. static void vdbeInvokeSqllog(Vdbe *v){
  2213. if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
  2214. char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
  2215. assert( v->db->init.busy==0 );
  2216. if( zExpanded ){
  2217. sqlite3GlobalConfig.xSqllog(
  2218. sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
  2219. );
  2220. sqlite3DbFree(v->db, zExpanded);
  2221. }
  2222. }
  2223. }
  2224. #else
  2225. # define vdbeInvokeSqllog(x)
  2226. #endif
  2227. /*
  2228. ** Clean up a VDBE after execution but do not delete the VDBE just yet.
  2229. ** Write any error messages into *pzErrMsg. Return the result code.
  2230. **
  2231. ** After this routine is run, the VDBE should be ready to be executed
  2232. ** again.
  2233. **
  2234. ** To look at it another way, this routine resets the state of the
  2235. ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
  2236. ** VDBE_MAGIC_INIT.
  2237. */
  2238. int sqlite3VdbeReset(Vdbe *p){
  2239. sqlite3 *db;
  2240. db = p->db;
  2241. /* If the VM did not run to completion or if it encountered an
  2242. ** error, then it might not have been halted properly. So halt
  2243. ** it now.
  2244. */
  2245. sqlite3VdbeHalt(p);
  2246. /* If the VDBE has be run even partially, then transfer the error code
  2247. ** and error message from the VDBE into the main database structure. But
  2248. ** if the VDBE has just been set to run but has not actually executed any
  2249. ** instructions yet, leave the main database error information unchanged.
  2250. */
  2251. if( p->pc>=0 ){
  2252. vdbeInvokeSqllog(p);
  2253. sqlite3VdbeTransferError(p);
  2254. sqlite3DbFree(db, p->zErrMsg);
  2255. p->zErrMsg = 0;
  2256. if( p->runOnlyOnce ) p->expired = 1;
  2257. }else if( p->rc && p->expired ){
  2258. /* The expired flag was set on the VDBE before the first call
  2259. ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2260. ** called), set the database error in this case as well.
  2261. */
  2262. sqlite3Error(db, p->rc, 0);
  2263. sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
  2264. sqlite3DbFree(db, p->zErrMsg);
  2265. p->zErrMsg = 0;
  2266. }
  2267. /* Reclaim all memory used by the VDBE
  2268. */
  2269. Cleanup(p);
  2270. /* Save profiling information from this VDBE run.
  2271. */
  2272. #ifdef VDBE_PROFILE
  2273. {
  2274. FILE *out = fopen("vdbe_profile.out", "a");
  2275. if( out ){
  2276. int i;
  2277. fprintf(out, "---- ");
  2278. for(i=0; i<p->nOp; i++){
  2279. fprintf(out, "%02x", p->aOp[i].opcode);
  2280. }
  2281. fprintf(out, "\n");
  2282. for(i=0; i<p->nOp; i++){
  2283. fprintf(out, "%6d %10lld %8lld ",
  2284. p->aOp[i].cnt,
  2285. p->aOp[i].cycles,
  2286. p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
  2287. );
  2288. sqlite3VdbePrintOp(out, i, &p->aOp[i]);
  2289. }
  2290. fclose(out);
  2291. }
  2292. }
  2293. #endif
  2294. p->iCurrentTime = 0;
  2295. p->magic = VDBE_MAGIC_INIT;
  2296. return p->rc & db->errMask;
  2297. }
  2298. /*
  2299. ** Clean up and delete a VDBE after execution. Return an integer which is
  2300. ** the result code. Write any error message text into *pzErrMsg.
  2301. */
  2302. int sqlite3VdbeFinalize(Vdbe *p){
  2303. int rc = SQLITE_OK;
  2304. if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
  2305. rc = sqlite3VdbeReset(p);
  2306. assert( (rc & p->db->errMask)==rc );
  2307. }
  2308. sqlite3VdbeDelete(p);
  2309. return rc;
  2310. }
  2311. /*
  2312. ** If parameter iOp is less than zero, then invoke the destructor for
  2313. ** all auxiliary data pointers currently cached by the VM passed as
  2314. ** the first argument.
  2315. **
  2316. ** Or, if iOp is greater than or equal to zero, then the destructor is
  2317. ** only invoked for those auxiliary data pointers created by the user
  2318. ** function invoked by the OP_Function opcode at instruction iOp of
  2319. ** VM pVdbe, and only then if:
  2320. **
  2321. ** * the associated function parameter is the 32nd or later (counting
  2322. ** from left to right), or
  2323. **
  2324. ** * the corresponding bit in argument mask is clear (where the first
  2325. ** function parameter corrsponds to bit 0 etc.).
  2326. */
  2327. void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
  2328. AuxData **pp = &pVdbe->pAuxData;
  2329. while( *pp ){
  2330. AuxData *pAux = *pp;
  2331. if( (iOp<0)
  2332. || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & ((u32)1<<pAux->iArg))))
  2333. ){
  2334. if( pAux->xDelete ){
  2335. pAux->xDelete(pAux->pAux);
  2336. }
  2337. *pp = pAux->pNext;
  2338. sqlite3DbFree(pVdbe->db, pAux);
  2339. }else{
  2340. pp= &pAux->pNext;
  2341. }
  2342. }
  2343. }
  2344. /*
  2345. ** Free all memory associated with the Vdbe passed as the second argument,
  2346. ** except for object itself, which is preserved.
  2347. **
  2348. ** The difference between this function and sqlite3VdbeDelete() is that
  2349. ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
  2350. ** the database connection and frees the object itself.
  2351. */
  2352. void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
  2353. SubProgram *pSub, *pNext;
  2354. int i;
  2355. assert( p->db==0 || p->db==db );
  2356. releaseMemArray(p->aVar, p->nVar);
  2357. releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  2358. for(pSub=p->pProgram; pSub; pSub=pNext){
  2359. pNext = pSub->pNext;
  2360. vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
  2361. sqlite3DbFree(db, pSub);
  2362. }
  2363. for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
  2364. vdbeFreeOpArray(db, p->aOp, p->nOp);
  2365. sqlite3DbFree(db, p->aLabel);
  2366. sqlite3DbFree(db, p->aColName);
  2367. sqlite3DbFree(db, p->zSql);
  2368. sqlite3DbFree(db, p->pFree);
  2369. #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  2370. sqlite3DbFree(db, p->zExplain);
  2371. sqlite3DbFree(db, p->pExplain);
  2372. #endif
  2373. }
  2374. /*
  2375. ** Delete an entire VDBE.
  2376. */
  2377. void sqlite3VdbeDelete(Vdbe *p){
  2378. sqlite3 *db;
  2379. if( NEVER(p==0) ) return;
  2380. db = p->db;
  2381. assert( sqlite3_mutex_held(db->mutex) );
  2382. sqlite3VdbeClearObject(db, p);
  2383. if( p->pPrev ){
  2384. p->pPrev->pNext = p->pNext;
  2385. }else{
  2386. assert( db->pVdbe==p );
  2387. db->pVdbe = p->pNext;
  2388. }
  2389. if( p->pNext ){
  2390. p->pNext->pPrev = p->pPrev;
  2391. }
  2392. p->magic = VDBE_MAGIC_DEAD;
  2393. p->db = 0;
  2394. sqlite3DbFree(db, p);
  2395. }
  2396. /*
  2397. ** Make sure the cursor p is ready to read or write the row to which it
  2398. ** was last positioned. Return an error code if an OOM fault or I/O error
  2399. ** prevents us from positioning the cursor to its correct position.
  2400. **
  2401. ** If a MoveTo operation is pending on the given cursor, then do that
  2402. ** MoveTo now. If no move is pending, check to see if the row has been
  2403. ** deleted out from under the cursor and if it has, mark the row as
  2404. ** a NULL row.
  2405. **
  2406. ** If the cursor is already pointing to the correct row and that row has
  2407. ** not been deleted out from under the cursor, then this routine is a no-op.
  2408. */
  2409. int sqlite3VdbeCursorMoveto(VdbeCursor *p){
  2410. if( p->deferredMoveto ){
  2411. int res, rc;
  2412. #ifdef SQLITE_TEST
  2413. extern int sqlite3_search_count;
  2414. #endif
  2415. assert( p->isTable );
  2416. rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
  2417. if( rc ) return rc;
  2418. p->lastRowid = p->movetoTarget;
  2419. if( res!=0 ) return SQLITE_CORRUPT_BKPT;
  2420. p->rowidIsValid = 1;
  2421. #ifdef SQLITE_TEST
  2422. sqlite3_search_count++;
  2423. #endif
  2424. p->deferredMoveto = 0;
  2425. p->cacheStatus = CACHE_STALE;
  2426. }else if( ALWAYS(p->pCursor) ){
  2427. int hasMoved;
  2428. int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
  2429. if( rc ) return rc;
  2430. if( hasMoved ){
  2431. p->cacheStatus = CACHE_STALE;
  2432. p->nullRow = 1;
  2433. }
  2434. }
  2435. return SQLITE_OK;
  2436. }
  2437. /*
  2438. ** The following functions:
  2439. **
  2440. ** sqlite3VdbeSerialType()
  2441. ** sqlite3VdbeSerialTypeLen()
  2442. ** sqlite3VdbeSerialLen()
  2443. ** sqlite3VdbeSerialPut()
  2444. ** sqlite3VdbeSerialGet()
  2445. **
  2446. ** encapsulate the code that serializes values for storage in SQLite
  2447. ** data and index records. Each serialized value consists of a
  2448. ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
  2449. ** integer, stored as a varint.
  2450. **
  2451. ** In an SQLite index record, the serial type is stored directly before
  2452. ** the blob of data that it corresponds to. In a table record, all serial
  2453. ** types are stored at the start of the record, and the blobs of data at
  2454. ** the end. Hence these functions allow the caller to handle the
  2455. ** serial-type and data blob separately.
  2456. **
  2457. ** The following table describes the various storage classes for data:
  2458. **
  2459. ** serial type bytes of data type
  2460. ** -------------- --------------- ---------------
  2461. ** 0 0 NULL
  2462. ** 1 1 signed integer
  2463. ** 2 2 signed integer
  2464. ** 3 3 signed integer
  2465. ** 4 4 signed integer
  2466. ** 5 6 signed integer
  2467. ** 6 8 signed integer
  2468. ** 7 8 IEEE float
  2469. ** 8 0 Integer constant 0
  2470. ** 9 0 Integer constant 1
  2471. ** 10,11 reserved for expansion
  2472. ** N>=12 and even (N-12)/2 BLOB
  2473. ** N>=13 and odd (N-13)/2 text
  2474. **
  2475. ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
  2476. ** of SQLite will not understand those serial types.
  2477. */
  2478. /*
  2479. ** Return the serial-type for the value stored in pMem.
  2480. */
  2481. u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
  2482. int flags = pMem->flags;
  2483. int n;
  2484. if( flags&MEM_Null ){
  2485. return 0;
  2486. }
  2487. if( flags&MEM_Int ){
  2488. /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  2489. # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  2490. i64 i = pMem->u.i;
  2491. u64 u;
  2492. if( i<0 ){
  2493. if( i<(-MAX_6BYTE) ) return 6;
  2494. /* Previous test prevents: u = -(-9223372036854775808) */
  2495. u = -i;
  2496. }else{
  2497. u = i;
  2498. }
  2499. if( u<=127 ){
  2500. return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
  2501. }
  2502. if( u<=32767 ) return 2;
  2503. if( u<=8388607 ) return 3;
  2504. if( u<=2147483647 ) return 4;
  2505. if( u<=MAX_6BYTE ) return 5;
  2506. return 6;
  2507. }
  2508. if( flags&MEM_Real ){
  2509. return 7;
  2510. }
  2511. assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
  2512. n = pMem->n;
  2513. if( flags & MEM_Zero ){
  2514. n += pMem->u.nZero;
  2515. }
  2516. assert( n>=0 );
  2517. return ((n*2) + 12 + ((flags&MEM_Str)!=0));
  2518. }
  2519. /*
  2520. ** Return the length of the data corresponding to the supplied serial-type.
  2521. */
  2522. u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
  2523. if( serial_type>=12 ){
  2524. return (serial_type-12)/2;
  2525. }else{
  2526. static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
  2527. return aSize[serial_type];
  2528. }
  2529. }
  2530. /*
  2531. ** If we are on an architecture with mixed-endian floating
  2532. ** points (ex: ARM7) then swap the lower 4 bytes with the
  2533. ** upper 4 bytes. Return the result.
  2534. **
  2535. ** For most architectures, this is a no-op.
  2536. **
  2537. ** (later): It is reported to me that the mixed-endian problem
  2538. ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
  2539. ** that early versions of GCC stored the two words of a 64-bit
  2540. ** float in the wrong order. And that error has been propagated
  2541. ** ever since. The blame is not necessarily with GCC, though.
  2542. ** GCC might have just copying the problem from a prior compiler.
  2543. ** I am also told that newer versions of GCC that follow a different
  2544. ** ABI get the byte order right.
  2545. **
  2546. ** Developers using SQLite on an ARM7 should compile and run their
  2547. ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
  2548. ** enabled, some asserts below will ensure that the byte order of
  2549. ** floating point values is correct.
  2550. **
  2551. ** (2007-08-30) Frank van Vugt has studied this problem closely
  2552. ** and has send his findings to the SQLite developers. Frank
  2553. ** writes that some Linux kernels offer floating point hardware
  2554. ** emulation that uses only 32-bit mantissas instead of a full
  2555. ** 48-bits as required by the IEEE standard. (This is the
  2556. ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
  2557. ** byte swapping becomes very complicated. To avoid problems,
  2558. ** the necessary byte swapping is carried out using a 64-bit integer
  2559. ** rather than a 64-bit float. Frank assures us that the code here
  2560. ** works for him. We, the developers, have no way to independently
  2561. ** verify this, but Frank seems to know what he is talking about
  2562. ** so we trust him.
  2563. */
  2564. #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  2565. static u64 floatSwap(u64 in){
  2566. union {
  2567. u64 r;
  2568. u32 i[2];
  2569. } u;
  2570. u32 t;
  2571. u.r = in;
  2572. t = u.i[0];
  2573. u.i[0] = u.i[1];
  2574. u.i[1] = t;
  2575. return u.r;
  2576. }
  2577. # define swapMixedEndianFloat(X) X = floatSwap(X)
  2578. #else
  2579. # define swapMixedEndianFloat(X)
  2580. #endif
  2581. /*
  2582. ** Write the serialized data blob for the value stored in pMem into
  2583. ** buf. It is assumed that the caller has allocated sufficient space.
  2584. ** Return the number of bytes written.
  2585. **
  2586. ** nBuf is the amount of space left in buf[]. nBuf must always be
  2587. ** large enough to hold the entire field. Except, if the field is
  2588. ** a blob with a zero-filled tail, then buf[] might be just the right
  2589. ** size to hold everything except for the zero-filled tail. If buf[]
  2590. ** is only big enough to hold the non-zero prefix, then only write that
  2591. ** prefix into buf[]. But if buf[] is large enough to hold both the
  2592. ** prefix and the tail then write the prefix and set the tail to all
  2593. ** zeros.
  2594. **
  2595. ** Return the number of bytes actually written into buf[]. The number
  2596. ** of bytes in the zero-filled tail is included in the return value only
  2597. ** if those bytes were zeroed in buf[].
  2598. */
  2599. u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
  2600. u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
  2601. u32 len;
  2602. /* Integer and Real */
  2603. if( serial_type<=7 && serial_type>0 ){
  2604. u64 v;
  2605. u32 i;
  2606. if( serial_type==7 ){
  2607. assert( sizeof(v)==sizeof(pMem->r) );
  2608. memcpy(&v, &pMem->r, sizeof(v));
  2609. swapMixedEndianFloat(v);
  2610. }else{
  2611. v = pMem->u.i;
  2612. }
  2613. len = i = sqlite3VdbeSerialTypeLen(serial_type);
  2614. assert( len<=(u32)nBuf );
  2615. while( i-- ){
  2616. buf[i] = (u8)(v&0xFF);
  2617. v >>= 8;
  2618. }
  2619. return len;
  2620. }
  2621. /* String or blob */
  2622. if( serial_type>=12 ){
  2623. assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
  2624. == (int)sqlite3VdbeSerialTypeLen(serial_type) );
  2625. assert( pMem->n<=nBuf );
  2626. len = pMem->n;
  2627. memcpy(buf, pMem->z, len);
  2628. if( pMem->flags & MEM_Zero ){
  2629. len += pMem->u.nZero;
  2630. assert( nBuf>=0 );
  2631. if( len > (u32)nBuf ){
  2632. len = (u32)nBuf;
  2633. }
  2634. memset(&buf[pMem->n], 0, len-pMem->n);
  2635. }
  2636. return len;
  2637. }
  2638. /* NULL or constants 0 or 1 */
  2639. return 0;
  2640. }
  2641. /*
  2642. ** Deserialize the data blob pointed to by buf as serial type serial_type
  2643. ** and store the result in pMem. Return the number of bytes read.
  2644. */
  2645. u32 sqlite3VdbeSerialGet(
  2646. const unsigned char *buf, /* Buffer to deserialize from */
  2647. u32 serial_type, /* Serial type to deserialize */
  2648. Mem *pMem /* Memory cell to write value into */
  2649. ){
  2650. switch( serial_type ){
  2651. case 10: /* Reserved for future use */
  2652. case 11: /* Reserved for future use */
  2653. case 0: { /* NULL */
  2654. pMem->flags = MEM_Null;
  2655. break;
  2656. }
  2657. case 1: { /* 1-byte signed integer */
  2658. pMem->u.i = (signed char)buf[0];
  2659. pMem->flags = MEM_Int;
  2660. return 1;
  2661. }
  2662. case 2: { /* 2-byte signed integer */
  2663. pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
  2664. pMem->flags = MEM_Int;
  2665. return 2;
  2666. }
  2667. case 3: { /* 3-byte signed integer */
  2668. pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
  2669. pMem->flags = MEM_Int;
  2670. return 3;
  2671. }
  2672. case 4: { /* 4-byte signed integer */
  2673. pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
  2674. pMem->flags = MEM_Int;
  2675. return 4;
  2676. }
  2677. case 5: { /* 6-byte signed integer */
  2678. u64 x = (((signed char)buf[0])<<8) | buf[1];
  2679. u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
  2680. x = (x<<32) | y;
  2681. pMem->u.i = *(i64*)&x;
  2682. pMem->flags = MEM_Int;
  2683. return 6;
  2684. }
  2685. case 6: /* 8-byte signed integer */
  2686. case 7: { /* IEEE floating point */
  2687. u64 x;
  2688. u32 y;
  2689. #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
  2690. /* Verify that integers and floating point values use the same
  2691. ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
  2692. ** defined that 64-bit floating point values really are mixed
  2693. ** endian.
  2694. */
  2695. static const u64 t1 = ((u64)0x3ff00000)<<32;
  2696. static const double r1 = 1.0;
  2697. u64 t2 = t1;
  2698. swapMixedEndianFloat(t2);
  2699. assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
  2700. #endif
  2701. x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
  2702. y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
  2703. x = (x<<32) | y;
  2704. if( serial_type==6 ){
  2705. pMem->u.i = *(i64*)&x;
  2706. pMem->flags = MEM_Int;
  2707. }else{
  2708. assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
  2709. swapMixedEndianFloat(x);
  2710. memcpy(&pMem->r, &x, sizeof(x));
  2711. pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
  2712. }
  2713. return 8;
  2714. }
  2715. case 8: /* Integer 0 */
  2716. case 9: { /* Integer 1 */
  2717. pMem->u.i = serial_type-8;
  2718. pMem->flags = MEM_Int;
  2719. return 0;
  2720. }
  2721. default: {
  2722. u32 len = (serial_type-12)/2;
  2723. pMem->z = (char *)buf;
  2724. pMem->n = len;
  2725. pMem->xDel = 0;
  2726. if( serial_type&0x01 ){
  2727. pMem->flags = MEM_Str | MEM_Ephem;
  2728. }else{
  2729. pMem->flags = MEM_Blob | MEM_Ephem;
  2730. }
  2731. return len;
  2732. }
  2733. }
  2734. return 0;
  2735. }
  2736. /*
  2737. ** This routine is used to allocate sufficient space for an UnpackedRecord
  2738. ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
  2739. ** the first argument is a pointer to KeyInfo structure pKeyInfo.
  2740. **
  2741. ** The space is either allocated using sqlite3DbMallocRaw() or from within
  2742. ** the unaligned buffer passed via the second and third arguments (presumably
  2743. ** stack space). If the former, then *ppFree is set to a pointer that should
  2744. ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
  2745. ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
  2746. ** before returning.
  2747. **
  2748. ** If an OOM error occurs, NULL is returned.
  2749. */
  2750. UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
  2751. KeyInfo *pKeyInfo, /* Description of the record */
  2752. char *pSpace, /* Unaligned space available */
  2753. int szSpace, /* Size of pSpace[] in bytes */
  2754. char **ppFree /* OUT: Caller should free this pointer */
  2755. ){
  2756. UnpackedRecord *p; /* Unpacked record to return */
  2757. int nOff; /* Increment pSpace by nOff to align it */
  2758. int nByte; /* Number of bytes required for *p */
  2759. /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
  2760. ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
  2761. ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
  2762. */
  2763. nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
  2764. nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
  2765. if( nByte>szSpace+nOff ){
  2766. p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
  2767. *ppFree = (char *)p;
  2768. if( !p ) return 0;
  2769. }else{
  2770. p = (UnpackedRecord*)&pSpace[nOff];
  2771. *ppFree = 0;
  2772. }
  2773. p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
  2774. assert( pKeyInfo->aSortOrder!=0 );
  2775. p->pKeyInfo = pKeyInfo;
  2776. p->nField = pKeyInfo->nField + 1;
  2777. return p;
  2778. }
  2779. /*
  2780. ** Given the nKey-byte encoding of a record in pKey[], populate the
  2781. ** UnpackedRecord structure indicated by the fourth argument with the
  2782. ** contents of the decoded record.
  2783. */
  2784. void sqlite3VdbeRecordUnpack(
  2785. KeyInfo *pKeyInfo, /* Information about the record format */
  2786. int nKey, /* Size of the binary record */
  2787. const void *pKey, /* The binary record */
  2788. UnpackedRecord *p /* Populate this structure before returning. */
  2789. ){
  2790. const unsigned char *aKey = (const unsigned char *)pKey;
  2791. int d;
  2792. u32 idx; /* Offset in aKey[] to read from */
  2793. u16 u; /* Unsigned loop counter */
  2794. u32 szHdr;
  2795. Mem *pMem = p->aMem;
  2796. p->flags = 0;
  2797. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  2798. idx = getVarint32(aKey, szHdr);
  2799. d = szHdr;
  2800. u = 0;
  2801. while( idx<szHdr && u<p->nField && d<=nKey ){
  2802. u32 serial_type;
  2803. idx += getVarint32(&aKey[idx], serial_type);
  2804. pMem->enc = pKeyInfo->enc;
  2805. pMem->db = pKeyInfo->db;
  2806. /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  2807. pMem->zMalloc = 0;
  2808. d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  2809. pMem++;
  2810. u++;
  2811. }
  2812. assert( u<=pKeyInfo->nField + 1 );
  2813. p->nField = u;
  2814. }
  2815. /*
  2816. ** This function compares the two table rows or index records
  2817. ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
  2818. ** or positive integer if key1 is less than, equal to or
  2819. ** greater than key2. The {nKey1, pKey1} key must be a blob
  2820. ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
  2821. ** key must be a parsed key such as obtained from
  2822. ** sqlite3VdbeParseRecord.
  2823. **
  2824. ** Key1 and Key2 do not have to contain the same number of fields.
  2825. ** The key with fewer fields is usually compares less than the
  2826. ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
  2827. ** and the common prefixes are equal, then key1 is less than key2.
  2828. ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
  2829. ** equal, then the keys are considered to be equal and
  2830. ** the parts beyond the common prefix are ignored.
  2831. */
  2832. int sqlite3VdbeRecordCompare(
  2833. int nKey1, const void *pKey1, /* Left key */
  2834. UnpackedRecord *pPKey2 /* Right key */
  2835. ){
  2836. u32 d1; /* Offset into aKey[] of next data element */
  2837. u32 idx1; /* Offset into aKey[] of next header element */
  2838. u32 szHdr1; /* Number of bytes in header */
  2839. int i = 0;
  2840. int rc = 0;
  2841. const unsigned char *aKey1 = (const unsigned char *)pKey1;
  2842. KeyInfo *pKeyInfo;
  2843. Mem mem1;
  2844. pKeyInfo = pPKey2->pKeyInfo;
  2845. mem1.enc = pKeyInfo->enc;
  2846. mem1.db = pKeyInfo->db;
  2847. /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
  2848. VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
  2849. /* Compilers may complain that mem1.u.i is potentially uninitialized.
  2850. ** We could initialize it, as shown here, to silence those complaints.
  2851. ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
  2852. ** the unnecessary initialization has a measurable negative performance
  2853. ** impact, since this routine is a very high runner. And so, we choose
  2854. ** to ignore the compiler warnings and leave this variable uninitialized.
  2855. */
  2856. /* mem1.u.i = 0; // not needed, here to silence compiler warning */
  2857. idx1 = getVarint32(aKey1, szHdr1);
  2858. d1 = szHdr1;
  2859. assert( pKeyInfo->nField+1>=pPKey2->nField );
  2860. assert( pKeyInfo->aSortOrder!=0 );
  2861. while( idx1<szHdr1 && i<pPKey2->nField ){
  2862. u32 serial_type1;
  2863. /* Read the serial types for the next element in each key. */
  2864. idx1 += getVarint32( aKey1+idx1, serial_type1 );
  2865. /* Verify that there is enough key space remaining to avoid
  2866. ** a buffer overread. The "d1+serial_type1+2" subexpression will
  2867. ** always be greater than or equal to the amount of required key space.
  2868. ** Use that approximation to avoid the more expensive call to
  2869. ** sqlite3VdbeSerialTypeLen() in the common case.
  2870. */
  2871. if( d1+serial_type1+2>(u32)nKey1
  2872. && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
  2873. ){
  2874. break;
  2875. }
  2876. /* Extract the values to be compared.
  2877. */
  2878. d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  2879. /* Do the comparison
  2880. */
  2881. rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
  2882. if( rc!=0 ){
  2883. assert( mem1.zMalloc==0 ); /* See comment below */
  2884. /* Invert the result if we are using DESC sort order. */
  2885. if( pKeyInfo->aSortOrder[i] ){
  2886. rc = -rc;
  2887. }
  2888. /* If the PREFIX_SEARCH flag is set and all fields except the final
  2889. ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
  2890. ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
  2891. ** This is used by the OP_IsUnique opcode.
  2892. */
  2893. if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
  2894. assert( idx1==szHdr1 && rc );
  2895. assert( mem1.flags & MEM_Int );
  2896. pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
  2897. pPKey2->rowid = mem1.u.i;
  2898. }
  2899. return rc;
  2900. }
  2901. i++;
  2902. }
  2903. /* No memory allocation is ever used on mem1. Prove this using
  2904. ** the following assert(). If the assert() fails, it indicates a
  2905. ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
  2906. */
  2907. assert( mem1.zMalloc==0 );
  2908. /* rc==0 here means that one of the keys ran out of fields and
  2909. ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
  2910. ** flag is set, then break the tie by treating key2 as larger.
  2911. ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
  2912. ** are considered to be equal. Otherwise, the longer key is the
  2913. ** larger. As it happens, the pPKey2 will always be the longer
  2914. ** if there is a difference.
  2915. */
  2916. assert( rc==0 );
  2917. if( pPKey2->flags & UNPACKED_INCRKEY ){
  2918. rc = -1;
  2919. }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
  2920. /* Leave rc==0 */
  2921. }else if( idx1<szHdr1 ){
  2922. rc = 1;
  2923. }
  2924. return rc;
  2925. }
  2926. /*
  2927. ** pCur points at an index entry created using the OP_MakeRecord opcode.
  2928. ** Read the rowid (the last field in the record) and store it in *rowid.
  2929. ** Return SQLITE_OK if everything works, or an error code otherwise.
  2930. **
  2931. ** pCur might be pointing to text obtained from a corrupt database file.
  2932. ** So the content cannot be trusted. Do appropriate checks on the content.
  2933. */
  2934. int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
  2935. i64 nCellKey = 0;
  2936. int rc;
  2937. u32 szHdr; /* Size of the header */
  2938. u32 typeRowid; /* Serial type of the rowid */
  2939. u32 lenRowid; /* Size of the rowid */
  2940. Mem m, v;
  2941. UNUSED_PARAMETER(db);
  2942. /* Get the size of the index entry. Only indices entries of less
  2943. ** than 2GiB are support - anything large must be database corruption.
  2944. ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
  2945. ** this code can safely assume that nCellKey is 32-bits
  2946. */
  2947. assert( sqlite3BtreeCursorIsValid(pCur) );
  2948. VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  2949. assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
  2950. assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
  2951. /* Read in the complete content of the index entry */
  2952. memset(&m, 0, sizeof(m));
  2953. rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
  2954. if( rc ){
  2955. return rc;
  2956. }
  2957. /* The index entry must begin with a header size */
  2958. (void)getVarint32((u8*)m.z, szHdr);
  2959. testcase( szHdr==3 );
  2960. testcase( szHdr==m.n );
  2961. if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
  2962. goto idx_rowid_corruption;
  2963. }
  2964. /* The last field of the index should be an integer - the ROWID.
  2965. ** Verify that the last entry really is an integer. */
  2966. (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
  2967. testcase( typeRowid==1 );
  2968. testcase( typeRowid==2 );
  2969. testcase( typeRowid==3 );
  2970. testcase( typeRowid==4 );
  2971. testcase( typeRowid==5 );
  2972. testcase( typeRowid==6 );
  2973. testcase( typeRowid==8 );
  2974. testcase( typeRowid==9 );
  2975. if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
  2976. goto idx_rowid_corruption;
  2977. }
  2978. lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
  2979. testcase( (u32)m.n==szHdr+lenRowid );
  2980. if( unlikely((u32)m.n<szHdr+lenRowid) ){
  2981. goto idx_rowid_corruption;
  2982. }
  2983. /* Fetch the integer off the end of the index record */
  2984. sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
  2985. *rowid = v.u.i;
  2986. sqlite3VdbeMemRelease(&m);
  2987. return SQLITE_OK;
  2988. /* Jump here if database corruption is detected after m has been
  2989. ** allocated. Free the m object and return SQLITE_CORRUPT. */
  2990. idx_rowid_corruption:
  2991. testcase( m.zMalloc!=0 );
  2992. sqlite3VdbeMemRelease(&m);
  2993. return SQLITE_CORRUPT_BKPT;
  2994. }
  2995. /*
  2996. ** Compare the key of the index entry that cursor pC is pointing to against
  2997. ** the key string in pUnpacked. Write into *pRes a number
  2998. ** that is negative, zero, or positive if pC is less than, equal to,
  2999. ** or greater than pUnpacked. Return SQLITE_OK on success.
  3000. **
  3001. ** pUnpacked is either created without a rowid or is truncated so that it
  3002. ** omits the rowid at the end. The rowid at the end of the index entry
  3003. ** is ignored as well. Hence, this routine only compares the prefixes
  3004. ** of the keys prior to the final rowid, not the entire key.
  3005. */
  3006. int sqlite3VdbeIdxKeyCompare(
  3007. VdbeCursor *pC, /* The cursor to compare against */
  3008. UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */
  3009. int *res /* Write the comparison result here */
  3010. ){
  3011. i64 nCellKey = 0;
  3012. int rc;
  3013. BtCursor *pCur = pC->pCursor;
  3014. Mem m;
  3015. assert( sqlite3BtreeCursorIsValid(pCur) );
  3016. VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  3017. assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
  3018. /* nCellKey will always be between 0 and 0xffffffff because of the say
  3019. ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  3020. if( nCellKey<=0 || nCellKey>0x7fffffff ){
  3021. *res = 0;
  3022. return SQLITE_CORRUPT_BKPT;
  3023. }
  3024. memset(&m, 0, sizeof(m));
  3025. rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
  3026. if( rc ){
  3027. return rc;
  3028. }
  3029. assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
  3030. *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
  3031. sqlite3VdbeMemRelease(&m);
  3032. return SQLITE_OK;
  3033. }
  3034. /*
  3035. ** This routine sets the value to be returned by subsequent calls to
  3036. ** sqlite3_changes() on the database handle 'db'.
  3037. */
  3038. void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
  3039. assert( sqlite3_mutex_held(db->mutex) );
  3040. db->nChange = nChange;
  3041. db->nTotalChange += nChange;
  3042. }
  3043. /*
  3044. ** Set a flag in the vdbe to update the change counter when it is finalised
  3045. ** or reset.
  3046. */
  3047. void sqlite3VdbeCountChanges(Vdbe *v){
  3048. v->changeCntOn = 1;
  3049. }
  3050. /*
  3051. ** Mark every prepared statement associated with a database connection
  3052. ** as expired.
  3053. **
  3054. ** An expired statement means that recompilation of the statement is
  3055. ** recommend. Statements expire when things happen that make their
  3056. ** programs obsolete. Removing user-defined functions or collating
  3057. ** sequences, or changing an authorization function are the types of
  3058. ** things that make prepared statements obsolete.
  3059. */
  3060. void sqlite3ExpirePreparedStatements(sqlite3 *db){
  3061. Vdbe *p;
  3062. for(p = db->pVdbe; p; p=p->pNext){
  3063. p->expired = 1;
  3064. }
  3065. }
  3066. /*
  3067. ** Return the database associated with the Vdbe.
  3068. */
  3069. sqlite3 *sqlite3VdbeDb(Vdbe *v){
  3070. return v->db;
  3071. }
  3072. /*
  3073. ** Return a pointer to an sqlite3_value structure containing the value bound
  3074. ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
  3075. ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
  3076. ** constants) to the value before returning it.
  3077. **
  3078. ** The returned value must be freed by the caller using sqlite3ValueFree().
  3079. */
  3080. sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
  3081. assert( iVar>0 );
  3082. if( v ){
  3083. Mem *pMem = &v->aVar[iVar-1];
  3084. if( 0==(pMem->flags & MEM_Null) ){
  3085. sqlite3_value *pRet = sqlite3ValueNew(v->db);
  3086. if( pRet ){
  3087. sqlite3VdbeMemCopy((Mem *)pRet, pMem);
  3088. sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
  3089. sqlite3VdbeMemStoreType((Mem *)pRet);
  3090. }
  3091. return pRet;
  3092. }
  3093. }
  3094. return 0;
  3095. }
  3096. /*
  3097. ** Configure SQL variable iVar so that binding a new value to it signals
  3098. ** to sqlite3_reoptimize() that re-preparing the statement may result
  3099. ** in a better query plan.
  3100. */
  3101. void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
  3102. assert( iVar>0 );
  3103. if( iVar>32 ){
  3104. v->expmask = 0xffffffff;
  3105. }else{
  3106. v->expmask |= ((u32)1 << (iVar-1));
  3107. }
  3108. }
  3109. #ifndef SQLITE_OMIT_VIRTUALTABLE
  3110. /*
  3111. ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
  3112. ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
  3113. ** in memory obtained from sqlite3DbMalloc).
  3114. */
  3115. void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
  3116. sqlite3 *db = p->db;
  3117. sqlite3DbFree(db, p->zErrMsg);
  3118. p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
  3119. sqlite3_free(pVtab->zErrMsg);
  3120. pVtab->zErrMsg = 0;
  3121. }
  3122. #endif /* SQLITE_OMIT_VIRTUALTABLE */