1 /* 2 ** 2004 May 22 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 ** 13 ** This file contains code that modified the OS layer in order to simulate 14 ** the effect on the database file of an OS crash or power failure. This 15 ** is used to test the ability of SQLite to recover from those situations. 16 */ 17 #if SQLITE_TEST /* This file is used for testing only */ 18 #include "sqliteInt.h" 19 #include "tcl.h" 20 21 #ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */ 22 23 /* #define TRACE_CRASHTEST */ 24 25 typedef struct CrashFile CrashFile; 26 typedef struct CrashGlobal CrashGlobal; 27 typedef struct WriteBuffer WriteBuffer; 28 29 /* 30 ** Method: 31 ** 32 ** This layer is implemented as a wrapper around the "real" 33 ** sqlite3_file object for the host system. Each time data is 34 ** written to the file object, instead of being written to the 35 ** underlying file, the write operation is stored in an in-memory 36 ** structure (type WriteBuffer). This structure is placed at the 37 ** end of a global ordered list (the write-list). 38 ** 39 ** When data is read from a file object, the requested region is 40 ** first retrieved from the real file. The write-list is then 41 ** traversed and data copied from any overlapping WriteBuffer 42 ** structures to the output buffer. i.e. a read() operation following 43 ** one or more write() operations works as expected, even if no 44 ** data has actually been written out to the real file. 45 ** 46 ** When a fsync() operation is performed, an operating system crash 47 ** may be simulated, in which case exit(-1) is called (the call to 48 ** xSync() never returns). Whether or not a crash is simulated, 49 ** the data associated with a subset of the WriteBuffer structures 50 ** stored in the write-list is written to the real underlying files 51 ** and the entries removed from the write-list. If a crash is simulated, 52 ** a subset of the buffers may be corrupted before the data is written. 53 ** 54 ** The exact subset of the write-list written and/or corrupted is 55 ** determined by the simulated device characteristics and sector-size. 56 ** 57 ** "Normal" mode: 58 ** 59 ** Normal mode is used when the simulated device has none of the 60 ** SQLITE_IOCAP_XXX flags set. 61 ** 62 ** In normal mode, if the fsync() is not a simulated crash, the 63 ** write-list is traversed from beginning to end. Each WriteBuffer 64 ** structure associated with the file handle used to call xSync() 65 ** is written to the real file and removed from the write-list. 66 ** 67 ** If a crash is simulated, one of the following takes place for 68 ** each WriteBuffer in the write-list, regardless of which 69 ** file-handle it is associated with: 70 ** 71 ** 1. The buffer is correctly written to the file, just as if 72 ** a crash were not being simulated. 73 ** 74 ** 2. Nothing is done. 75 ** 76 ** 3. Garbage data is written to all sectors of the file that 77 ** overlap the region specified by the WriteBuffer. Or garbage 78 ** data is written to some contiguous section within the 79 ** overlapped sectors. 80 ** 81 ** Device Characteristic flag handling: 82 ** 83 ** If the IOCAP_ATOMIC flag is set, then option (3) above is 84 ** never selected. 85 ** 86 ** If the IOCAP_ATOMIC512 flag is set, and the WriteBuffer represents 87 ** an aligned write() of an integer number of 512 byte regions, then 88 ** option (3) above is never selected. Instead, each 512 byte region 89 ** is either correctly written or left completely untouched. Similar 90 ** logic governs the behaviour if any of the other ATOMICXXX flags 91 ** is set. 92 ** 93 ** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set 94 ** and a crash is being simulated, then an entry of the write-list is 95 ** selected at random. Everything in the list after the selected entry 96 ** is discarded before processing begins. 97 ** 98 ** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option 99 ** (1) is selected for all write-list entries except the last. If a 100 ** crash is not being simulated, then all entries in the write-list 101 ** that occur before at least one write() on the file-handle specified 102 ** as part of the xSync() are written to their associated real files. 103 ** 104 ** If IOCAP_SAFEAPPEND is set and the first byte written by the write() 105 ** operation is one byte past the current end of the file, then option 106 ** (1) is always selected. 107 */ 108 109 /* 110 ** Each write operation in the write-list is represented by an instance 111 ** of the following structure. 112 ** 113 ** If zBuf is 0, then this structure represents a call to xTruncate(), 114 ** not xWrite(). In that case, iOffset is the size that the file is 115 ** truncated to. 116 */ 117 struct WriteBuffer { 118 i64 iOffset; /* Byte offset of the start of this write() */ 119 int nBuf; /* Number of bytes written */ 120 u8 *zBuf; /* Pointer to copy of written data */ 121 CrashFile *pFile; /* File this write() applies to */ 122 123 WriteBuffer *pNext; /* Next in CrashGlobal.pWriteList */ 124 }; 125 126 struct CrashFile { 127 const sqlite3_io_methods *pMethod; /* Must be first */ 128 sqlite3_file *pRealFile; /* Underlying "real" file handle */ 129 char *zName; 130 131 /* Cache of the entire file. This is used to speed up OsRead() and 132 ** OsFileSize() calls. Although both could be done by traversing the 133 ** write-list, in practice this is impractically slow. 134 */ 135 int iSize; /* Size of file in bytes */ 136 int nData; /* Size of buffer allocated at zData */ 137 u8 *zData; /* Buffer containing file contents */ 138 }; 139 140 struct CrashGlobal { 141 WriteBuffer *pWriteList; /* Head of write-list */ 142 WriteBuffer *pWriteListEnd; /* End of write-list */ 143 144 int iSectorSize; /* Value of simulated sector size */ 145 int iDeviceCharacteristics; /* Value of simulated device characteristics */ 146 147 int iCrash; /* Crash on the iCrash'th call to xSync() */ 148 char zCrashFile[500]; /* Crash during an xSync() on this file */ 149 }; 150 151 static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; 152 153 /* 154 ** Set this global variable to 1 to enable crash testing. 155 */ 156 static int sqlite3CrashTestEnable = 0; 157 158 static void *crash_malloc(int nByte){ 159 return (void *)Tcl_Alloc((size_t)nByte); 160 } 161 static void crash_free(void *p){ 162 Tcl_Free(p); 163 } 164 static void *crash_realloc(void *p, int n){ 165 return (void *)Tcl_Realloc(p, (size_t)n); 166 } 167 168 /* 169 ** Flush the write-list as if xSync() had been called on file handle 170 ** pFile. If isCrash is true, simulate a crash. 171 */ 172 static int writeListSync(CrashFile *pFile, int isCrash){ 173 int rc = SQLITE_OK; 174 int iDc = g.iDeviceCharacteristics; 175 176 WriteBuffer *pWrite; 177 WriteBuffer **ppPtr; 178 179 /* If this is not a crash simulation, set pFinal to point to the 180 ** last element of the write-list that is associated with file handle 181 ** pFile. 182 ** 183 ** If this is a crash simulation, set pFinal to an arbitrarily selected 184 ** element of the write-list. 185 */ 186 WriteBuffer *pFinal = 0; 187 if( !isCrash ){ 188 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){ 189 if( pWrite->pFile==pFile ){ 190 pFinal = pWrite; 191 } 192 } 193 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){ 194 int nWrite = 0; 195 int iFinal; 196 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++; 197 sqlite3_randomness(sizeof(int), &iFinal); 198 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite; 199 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--; 200 pFinal = pWrite; 201 } 202 203 #ifdef TRACE_CRASHTEST 204 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); 205 #endif 206 207 ppPtr = &g.pWriteList; 208 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){ 209 sqlite3_file *pRealFile = pWrite->pFile->pRealFile; 210 211 /* (eAction==1) -> write block out normally, 212 ** (eAction==2) -> do nothing, 213 ** (eAction==3) -> trash sectors. 214 */ 215 int eAction = 0; 216 if( !isCrash ){ 217 eAction = 2; 218 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){ 219 eAction = 1; 220 } 221 }else{ 222 char random; 223 sqlite3_randomness(1, &random); 224 225 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag 226 ** is set or this is an OsTruncate(), not an Oswrite(). 227 */ 228 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ 229 random &= 0x01; 230 } 231 232 /* If IOCAP_SEQUENTIAL is set and this is not the final entry 233 ** in the truncated write-list, always select option 1 (write 234 ** out correctly). 235 */ 236 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){ 237 random = 0; 238 } 239 240 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is 241 ** an append (first byte of the written region is 1 byte past the 242 ** current EOF), always select option 1 (write out correctly). 243 */ 244 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){ 245 i64 iSize; 246 sqlite3OsFileSize(pRealFile, &iSize); 247 if( iSize==pWrite->iOffset ){ 248 random = 0; 249 } 250 } 251 252 if( (random&0x06)==0x06 ){ 253 eAction = 3; 254 }else{ 255 eAction = ((random&0x01)?2:1); 256 } 257 } 258 259 switch( eAction ){ 260 case 1: { /* Write out correctly */ 261 if( pWrite->zBuf ){ 262 rc = sqlite3OsWrite( 263 pRealFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset 264 ); 265 }else{ 266 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset); 267 } 268 *ppPtr = pWrite->pNext; 269 #ifdef TRACE_CRASHTEST 270 if( isCrash ){ 271 printf("Writing %d bytes @ %d (%s)\n", 272 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 273 ); 274 } 275 #endif 276 crash_free(pWrite); 277 break; 278 } 279 case 2: { /* Do nothing */ 280 ppPtr = &pWrite->pNext; 281 #ifdef TRACE_CRASHTEST 282 if( isCrash ){ 283 printf("Omiting %d bytes @ %d (%s)\n", 284 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 285 ); 286 } 287 #endif 288 break; 289 } 290 case 3: { /* Trash sectors */ 291 u8 *zGarbage; 292 int iFirst = (pWrite->iOffset/g.iSectorSize); 293 int iLast = (pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize; 294 295 assert(pWrite->zBuf); 296 297 #ifdef TRACE_CRASHTEST 298 printf("Trashing %d sectors @ sector %d (%s)\n", 299 1+iLast-iFirst, iFirst, pWrite->pFile->zName 300 ); 301 #endif 302 303 zGarbage = crash_malloc(g.iSectorSize); 304 if( zGarbage ){ 305 sqlite3_int64 i; 306 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ 307 sqlite3_randomness(g.iSectorSize, zGarbage); 308 rc = sqlite3OsWrite( 309 pRealFile, zGarbage, g.iSectorSize, i*g.iSectorSize 310 ); 311 } 312 crash_free(zGarbage); 313 }else{ 314 rc = SQLITE_NOMEM; 315 } 316 317 ppPtr = &pWrite->pNext; 318 break; 319 } 320 321 default: 322 assert(!"Cannot happen"); 323 } 324 325 if( pWrite==pFinal ) break; 326 } 327 328 if( rc==SQLITE_OK && isCrash ){ 329 exit(-1); 330 } 331 332 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext); 333 g.pWriteListEnd = pWrite; 334 335 return rc; 336 } 337 338 /* 339 ** Add an entry to the end of the write-list. 340 */ 341 static int writeListAppend( 342 sqlite3_file *pFile, 343 sqlite3_int64 iOffset, 344 const u8 *zBuf, 345 int nBuf 346 ){ 347 WriteBuffer *pNew; 348 349 assert((zBuf && nBuf) || (!nBuf && !zBuf)); 350 351 pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf); 352 if( pNew==0 ){ 353 fprintf(stderr, "out of memory in the crash simulator\n"); 354 } 355 memset(pNew, 0, sizeof(WriteBuffer)+nBuf); 356 pNew->iOffset = iOffset; 357 pNew->nBuf = nBuf; 358 pNew->pFile = (CrashFile *)pFile; 359 if( zBuf ){ 360 pNew->zBuf = (u8 *)&pNew[1]; 361 memcpy(pNew->zBuf, zBuf, nBuf); 362 } 363 364 if( g.pWriteList ){ 365 assert(g.pWriteListEnd); 366 g.pWriteListEnd->pNext = pNew; 367 }else{ 368 g.pWriteList = pNew; 369 } 370 g.pWriteListEnd = pNew; 371 372 return SQLITE_OK; 373 } 374 375 /* 376 ** Close a crash-file. 377 */ 378 static int cfClose(sqlite3_file *pFile){ 379 CrashFile *pCrash = (CrashFile *)pFile; 380 writeListSync(pCrash, 0); 381 sqlite3OsClose(pCrash->pRealFile); 382 return SQLITE_OK; 383 } 384 385 /* 386 ** Read data from a crash-file. 387 */ 388 static int cfRead( 389 sqlite3_file *pFile, 390 void *zBuf, 391 int iAmt, 392 sqlite_int64 iOfst 393 ){ 394 CrashFile *pCrash = (CrashFile *)pFile; 395 396 /* Check the file-size to see if this is a short-read */ 397 if( pCrash->iSize<(iOfst+iAmt) ){ 398 return SQLITE_IOERR_SHORT_READ; 399 } 400 401 memcpy(zBuf, &pCrash->zData[iOfst], iAmt); 402 return SQLITE_OK; 403 } 404 405 /* 406 ** Write data to a crash-file. 407 */ 408 static int cfWrite( 409 sqlite3_file *pFile, 410 const void *zBuf, 411 int iAmt, 412 sqlite_int64 iOfst 413 ){ 414 CrashFile *pCrash = (CrashFile *)pFile; 415 if( iAmt+iOfst>pCrash->iSize ){ 416 pCrash->iSize = iAmt+iOfst; 417 } 418 while( pCrash->iSize>pCrash->nData ){ 419 u8 *zNew; 420 int nNew = (pCrash->nData*2) + 4096; 421 zNew = crash_realloc(pCrash->zData, nNew); 422 if( !zNew ){ 423 return SQLITE_NOMEM; 424 } 425 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData); 426 pCrash->nData = nNew; 427 pCrash->zData = zNew; 428 } 429 memcpy(&pCrash->zData[iOfst], zBuf, iAmt); 430 return writeListAppend(pFile, iOfst, zBuf, iAmt); 431 } 432 433 /* 434 ** Truncate a crash-file. 435 */ 436 static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){ 437 CrashFile *pCrash = (CrashFile *)pFile; 438 assert(size>=0); 439 if( pCrash->iSize>size ){ 440 pCrash->iSize = size; 441 } 442 return writeListAppend(pFile, size, 0, 0); 443 } 444 445 /* 446 ** Sync a crash-file. 447 */ 448 static int cfSync(sqlite3_file *pFile, int flags){ 449 CrashFile *pCrash = (CrashFile *)pFile; 450 int isCrash = 0; 451 452 const char *zName = pCrash->zName; 453 const char *zCrashFile = g.zCrashFile; 454 int nName = strlen(zName); 455 int nCrashFile = strlen(zCrashFile); 456 457 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){ 458 nCrashFile--; 459 if( nName>nCrashFile ) nName = nCrashFile; 460 } 461 462 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){ 463 if( (--g.iCrash)==0 ) isCrash = 1; 464 } 465 466 return writeListSync(pCrash, isCrash); 467 } 468 469 /* 470 ** Return the current file-size of the crash-file. 471 */ 472 static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 473 CrashFile *pCrash = (CrashFile *)pFile; 474 *pSize = (i64)pCrash->iSize; 475 return SQLITE_OK; 476 } 477 478 /* 479 ** Calls related to file-locks are passed on to the real file handle. 480 */ 481 static int cfLock(sqlite3_file *pFile, int eLock){ 482 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock); 483 } 484 static int cfUnlock(sqlite3_file *pFile, int eLock){ 485 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock); 486 } 487 static int cfCheckReservedLock(sqlite3_file *pFile){ 488 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile); 489 } 490 static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){ 491 return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg); 492 } 493 494 /* 495 ** The xSectorSize() and xDeviceCharacteristics() functions return 496 ** the global values configured by the [sqlite_crashparams] tcl 497 * interface. 498 */ 499 static int cfSectorSize(sqlite3_file *pFile){ 500 return g.iSectorSize; 501 } 502 static int cfDeviceCharacteristics(sqlite3_file *pFile){ 503 return g.iDeviceCharacteristics; 504 } 505 506 static const sqlite3_io_methods CrashFileVtab = { 507 1, /* iVersion */ 508 cfClose, /* xClose */ 509 cfRead, /* xRead */ 510 cfWrite, /* xWrite */ 511 cfTruncate, /* xTruncate */ 512 cfSync, /* xSync */ 513 cfFileSize, /* xFileSize */ 514 cfLock, /* xLock */ 515 cfUnlock, /* xUnlock */ 516 cfCheckReservedLock, /* xCheckReservedLock */ 517 cfFileControl, /* xFileControl */ 518 cfSectorSize, /* xSectorSize */ 519 cfDeviceCharacteristics /* xDeviceCharacteristics */ 520 }; 521 522 /* 523 ** Application data for the crash VFS 524 */ 525 struct crashAppData { 526 sqlite3_vfs *pOrig; /* Wrapped vfs structure */ 527 }; 528 529 /* 530 ** Open a crash-file file handle. 531 ** 532 ** The caller will have allocated pVfs->szOsFile bytes of space 533 ** at pFile. This file uses this space for the CrashFile structure 534 ** and allocates space for the "real" file structure using 535 ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is 536 ** equal or greater than sizeof(CrashFile). 537 */ 538 static int cfOpen( 539 sqlite3_vfs *pCfVfs, 540 const char *zName, 541 sqlite3_file *pFile, 542 int flags, 543 int *pOutFlags 544 ){ 545 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 546 int rc; 547 CrashFile *pWrapper = (CrashFile *)pFile; 548 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1]; 549 550 memset(pWrapper, 0, sizeof(CrashFile)); 551 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags); 552 553 if( rc==SQLITE_OK ){ 554 i64 iSize; 555 pWrapper->pMethod = &CrashFileVtab; 556 pWrapper->zName = (char *)zName; 557 pWrapper->pRealFile = pReal; 558 rc = sqlite3OsFileSize(pReal, &iSize); 559 pWrapper->iSize = (int)iSize; 560 } 561 if( rc==SQLITE_OK ){ 562 pWrapper->nData = (4096 + pWrapper->iSize); 563 pWrapper->zData = crash_malloc(pWrapper->nData); 564 if( pWrapper->zData ){ 565 memset(pWrapper->zData, 0, pWrapper->nData); 566 rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0); 567 }else{ 568 rc = SQLITE_NOMEM; 569 } 570 } 571 if( rc!=SQLITE_OK && pWrapper->pMethod ){ 572 sqlite3OsClose(pFile); 573 } 574 return rc; 575 } 576 577 static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ 578 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 579 return pVfs->xDelete(pVfs, zPath, dirSync); 580 } 581 static int cfAccess(sqlite3_vfs *pCfVfs, const char *zPath, int flags){ 582 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 583 return pVfs->xAccess(pVfs, zPath, flags); 584 } 585 static int cfGetTempname(sqlite3_vfs *pCfVfs, int nBufOut, char *zBufOut){ 586 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 587 return pVfs->xGetTempname(pVfs, nBufOut, zBufOut); 588 } 589 static int cfFullPathname( 590 sqlite3_vfs *pCfVfs, 591 const char *zPath, 592 int nPathOut, 593 char *zPathOut 594 ){ 595 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 596 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); 597 } 598 static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ 599 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 600 return pVfs->xDlOpen(pVfs, zPath); 601 } 602 static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ 603 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 604 pVfs->xDlError(pVfs, nByte, zErrMsg); 605 } 606 static void *cfDlSym(sqlite3_vfs *pCfVfs, void *pHandle, const char *zSymbol){ 607 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 608 return pVfs->xDlSym(pVfs, pHandle, zSymbol); 609 } 610 static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ 611 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 612 pVfs->xDlClose(pVfs, pHandle); 613 } 614 static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ 615 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 616 return pVfs->xRandomness(pVfs, nByte, zBufOut); 617 } 618 static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ 619 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 620 return pVfs->xSleep(pVfs, nMicro); 621 } 622 static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ 623 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 624 return pVfs->xCurrentTime(pVfs, pTimeOut); 625 } 626 627 static int processDevSymArgs( 628 Tcl_Interp *interp, 629 int objc, 630 Tcl_Obj *CONST objv[], 631 int *piDeviceChar, 632 int *piSectorSize 633 ){ 634 struct DeviceFlag { 635 char *zName; 636 int iValue; 637 } aFlag[] = { 638 { "atomic", SQLITE_IOCAP_ATOMIC }, 639 { "atomic512", SQLITE_IOCAP_ATOMIC512 }, 640 { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, 641 { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, 642 { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, 643 { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, 644 { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, 645 { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, 646 { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, 647 { "sequential", SQLITE_IOCAP_SEQUENTIAL }, 648 { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, 649 { 0, 0 } 650 }; 651 652 int i; 653 int iDc = 0; 654 int iSectorSize = 0; 655 int setSectorsize = 0; 656 int setDeviceChar = 0; 657 658 for(i=0; i<objc; i+=2){ 659 int nOpt; 660 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); 661 662 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) 663 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) 664 ){ 665 Tcl_AppendResult(interp, 666 "Bad option: \"", zOpt, 667 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 668 ); 669 return TCL_ERROR; 670 } 671 if( i==objc-1 ){ 672 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); 673 return TCL_ERROR; 674 } 675 676 if( zOpt[1]=='s' ){ 677 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){ 678 return TCL_ERROR; 679 } 680 setSectorsize = 1; 681 }else{ 682 int j; 683 Tcl_Obj **apObj; 684 int nObj; 685 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ 686 return TCL_ERROR; 687 } 688 for(j=0; j<nObj; j++){ 689 int rc; 690 int iChoice; 691 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); 692 Tcl_IncrRefCount(pFlag); 693 Tcl_UtfToLower(Tcl_GetString(pFlag)); 694 695 rc = Tcl_GetIndexFromObjStruct( 696 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice 697 ); 698 Tcl_DecrRefCount(pFlag); 699 if( rc ){ 700 return TCL_ERROR; 701 } 702 703 iDc |= aFlag[iChoice].iValue; 704 } 705 setDeviceChar = 1; 706 } 707 } 708 709 if( setDeviceChar ){ 710 *piDeviceChar = iDc; 711 } 712 if( setSectorsize ){ 713 *piSectorSize = iSectorSize; 714 } 715 716 return TCL_OK; 717 } 718 719 /* 720 ** tclcmd: sqlite_crash_enable ENABLE 721 ** 722 ** Parameter ENABLE must be a boolean value. If true, then the "crash" 723 ** vfs is added to the system. If false, it is removed. 724 */ 725 static int crashEnableCmd( 726 void * clientData, 727 Tcl_Interp *interp, 728 int objc, 729 Tcl_Obj *CONST objv[] 730 ){ 731 int isEnable; 732 static sqlite3_vfs crashVfs = { 733 1, /* iVersion */ 734 0, /* szOsFile */ 735 0, /* mxPathname */ 736 0, /* pNext */ 737 "crash", /* zName */ 738 0, /* pAppData */ 739 740 cfOpen, /* xOpen */ 741 cfDelete, /* xDelete */ 742 cfAccess, /* xAccess */ 743 cfGetTempname, /* xGetTempName */ 744 cfFullPathname, /* xFullPathname */ 745 cfDlOpen, /* xDlOpen */ 746 cfDlError, /* xDlError */ 747 cfDlSym, /* xDlSym */ 748 cfDlClose, /* xDlClose */ 749 cfRandomness, /* xRandomness */ 750 cfSleep, /* xSleep */ 751 cfCurrentTime /* xCurrentTime */ 752 }; 753 754 if( objc!=2 ){ 755 Tcl_WrongNumArgs(interp, 1, objv, "ENABLE"); 756 return TCL_ERROR; 757 } 758 759 if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){ 760 return TCL_ERROR; 761 } 762 763 if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){ 764 return TCL_OK; 765 } 766 767 if( crashVfs.pAppData==0 ){ 768 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); 769 crashVfs.mxPathname = pOriginalVfs->mxPathname; 770 crashVfs.pAppData = (void *)pOriginalVfs; 771 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; 772 sqlite3_vfs_register(&crashVfs, 0); 773 }else{ 774 crashVfs.pAppData = 0; 775 sqlite3_vfs_unregister(&crashVfs); 776 } 777 778 return TCL_OK; 779 } 780 781 /* 782 ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE 783 ** 784 ** This procedure implements a TCL command that enables crash testing 785 ** in testfixture. Once enabled, crash testing cannot be disabled. 786 ** 787 ** Available options are "-characteristics" and "-sectorsize". Both require 788 ** an argument. For -sectorsize, this is the simulated sector size in 789 ** bytes. For -characteristics, the argument must be a list of io-capability 790 ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", 791 ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", 792 ** "atomic64K", "sequential" and "safe_append". 793 ** 794 ** Example: 795 ** 796 ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 797 ** 798 */ 799 static int crashParamsObjCmd( 800 void * clientData, 801 Tcl_Interp *interp, 802 int objc, 803 Tcl_Obj *CONST objv[] 804 ){ 805 int iDelay; 806 const char *zCrashFile; 807 int nCrashFile, iDc, iSectorSize; 808 809 iDc = -1; 810 iSectorSize = -1; 811 812 if( objc<3 ){ 813 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE"); 814 goto error; 815 } 816 817 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); 818 if( nCrashFile>=sizeof(g.zCrashFile) ){ 819 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); 820 goto error; 821 } 822 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ 823 goto error; 824 } 825 826 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){ 827 return TCL_ERROR; 828 } 829 830 if( iDc>=0 ){ 831 g.iDeviceCharacteristics = iDc; 832 } 833 if( iSectorSize>=0 ){ 834 g.iSectorSize = iSectorSize; 835 } 836 837 g.iCrash = iDelay; 838 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1); 839 sqlite3CrashTestEnable = 1; 840 return TCL_OK; 841 842 error: 843 return TCL_ERROR; 844 } 845 846 static int devSymObjCmd( 847 void * clientData, 848 Tcl_Interp *interp, 849 int objc, 850 Tcl_Obj *CONST objv[] 851 ){ 852 void devsym_register(int iDeviceChar, int iSectorSize); 853 854 int iDc = -1; 855 int iSectorSize = -1; 856 857 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){ 858 return TCL_ERROR; 859 } 860 devsym_register(iDc, iSectorSize); 861 862 return TCL_OK; 863 } 864 865 #endif /* SQLITE_OMIT_DISKIO */ 866 867 /* 868 ** This procedure registers the TCL procedures defined in this file. 869 */ 870 int Sqlitetest6_Init(Tcl_Interp *interp){ 871 #ifndef SQLITE_OMIT_DISKIO 872 Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0); 873 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); 874 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); 875 #endif 876 return TCL_OK; 877 } 878 879 #endif /* SQLITE_TEST */ 880