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 behavior 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 int flags; /* Flags the file was opened with */ 131 132 /* Cache of the entire file. This is used to speed up OsRead() and 133 ** OsFileSize() calls. Although both could be done by traversing the 134 ** write-list, in practice this is impractically slow. 135 */ 136 u8 *zData; /* Buffer containing file contents */ 137 int nData; /* Size of buffer allocated at zData */ 138 i64 iSize; /* Size of file in bytes */ 139 }; 140 141 struct CrashGlobal { 142 WriteBuffer *pWriteList; /* Head of write-list */ 143 WriteBuffer *pWriteListEnd; /* End of write-list */ 144 145 int iSectorSize; /* Value of simulated sector size */ 146 int iDeviceCharacteristics; /* Value of simulated device characteristics */ 147 148 int iCrash; /* Crash on the iCrash'th call to xSync() */ 149 char zCrashFile[500]; /* Crash during an xSync() on this file */ 150 }; 151 152 static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; 153 154 /* 155 ** Set this global variable to 1 to enable crash testing. 156 */ 157 static int sqlite3CrashTestEnable = 0; 158 159 static void *crash_malloc(int nByte){ 160 return (void *)Tcl_Alloc((size_t)nByte); 161 } 162 static void crash_free(void *p){ 163 Tcl_Free(p); 164 } 165 static void *crash_realloc(void *p, int n){ 166 return (void *)Tcl_Realloc(p, (size_t)n); 167 } 168 169 /* 170 ** Wrapper around the sqlite3OsWrite() function that avoids writing to the 171 ** 512 byte block begining at offset PENDING_BYTE. 172 */ 173 static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){ 174 int rc = SQLITE_OK; 175 int iSkip = 0; 176 if( (iAmt-iSkip)>0 ){ 177 rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip); 178 } 179 return rc; 180 } 181 182 /* 183 ** Flush the write-list as if xSync() had been called on file handle 184 ** pFile. If isCrash is true, simulate a crash. 185 */ 186 static int writeListSync(CrashFile *pFile, int isCrash){ 187 int rc = SQLITE_OK; 188 int iDc = g.iDeviceCharacteristics; 189 190 WriteBuffer *pWrite; 191 WriteBuffer **ppPtr; 192 193 /* If this is not a crash simulation, set pFinal to point to the 194 ** last element of the write-list that is associated with file handle 195 ** pFile. 196 ** 197 ** If this is a crash simulation, set pFinal to an arbitrarily selected 198 ** element of the write-list. 199 */ 200 WriteBuffer *pFinal = 0; 201 if( !isCrash ){ 202 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){ 203 if( pWrite->pFile==pFile ){ 204 pFinal = pWrite; 205 } 206 } 207 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){ 208 int nWrite = 0; 209 int iFinal; 210 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++; 211 sqlite3_randomness(sizeof(int), &iFinal); 212 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite; 213 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--; 214 pFinal = pWrite; 215 } 216 217 #ifdef TRACE_CRASHTEST 218 if( pFile ){ 219 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); 220 } 221 #endif 222 223 ppPtr = &g.pWriteList; 224 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){ 225 sqlite3_file *pRealFile = pWrite->pFile->pRealFile; 226 227 /* (eAction==1) -> write block out normally, 228 ** (eAction==2) -> do nothing, 229 ** (eAction==3) -> trash sectors. 230 */ 231 int eAction = 0; 232 if( !isCrash ){ 233 eAction = 2; 234 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){ 235 eAction = 1; 236 } 237 }else{ 238 char random; 239 sqlite3_randomness(1, &random); 240 241 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag 242 ** is set or this is an OsTruncate(), not an Oswrite(). 243 */ 244 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ 245 random &= 0x01; 246 } 247 248 /* If IOCAP_SEQUENTIAL is set and this is not the final entry 249 ** in the truncated write-list, always select option 1 (write 250 ** out correctly). 251 */ 252 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){ 253 random = 0; 254 } 255 256 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is 257 ** an append (first byte of the written region is 1 byte past the 258 ** current EOF), always select option 1 (write out correctly). 259 */ 260 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){ 261 i64 iSize; 262 sqlite3OsFileSize(pRealFile, &iSize); 263 if( iSize==pWrite->iOffset ){ 264 random = 0; 265 } 266 } 267 268 if( (random&0x06)==0x06 ){ 269 eAction = 3; 270 }else{ 271 eAction = ((random&0x01)?2:1); 272 } 273 } 274 275 switch( eAction ){ 276 case 1: { /* Write out correctly */ 277 if( pWrite->zBuf ){ 278 rc = writeDbFile( 279 pWrite->pFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset 280 ); 281 }else{ 282 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset); 283 } 284 *ppPtr = pWrite->pNext; 285 #ifdef TRACE_CRASHTEST 286 if( isCrash ){ 287 printf("Writing %d bytes @ %d (%s)\n", 288 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 289 ); 290 } 291 #endif 292 crash_free(pWrite); 293 break; 294 } 295 case 2: { /* Do nothing */ 296 ppPtr = &pWrite->pNext; 297 #ifdef TRACE_CRASHTEST 298 if( isCrash ){ 299 printf("Omiting %d bytes @ %d (%s)\n", 300 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 301 ); 302 } 303 #endif 304 break; 305 } 306 case 3: { /* Trash sectors */ 307 u8 *zGarbage; 308 int iFirst = (int)(pWrite->iOffset/g.iSectorSize); 309 int iLast = (int)((pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize); 310 311 assert(pWrite->zBuf); 312 313 #ifdef TRACE_CRASHTEST 314 printf("Trashing %d sectors @ %lld (sector %d) (%s)\n", 315 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName 316 ); 317 #endif 318 319 zGarbage = crash_malloc(g.iSectorSize); 320 if( zGarbage ){ 321 sqlite3_int64 i; 322 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ 323 sqlite3_randomness(g.iSectorSize, zGarbage); 324 rc = writeDbFile( 325 pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize 326 ); 327 } 328 crash_free(zGarbage); 329 }else{ 330 rc = SQLITE_NOMEM; 331 } 332 333 ppPtr = &pWrite->pNext; 334 break; 335 } 336 337 default: 338 assert(!"Cannot happen"); 339 } 340 341 if( pWrite==pFinal ) break; 342 } 343 344 if( rc==SQLITE_OK && isCrash ){ 345 exit(-1); 346 } 347 348 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext); 349 g.pWriteListEnd = pWrite; 350 351 return rc; 352 } 353 354 /* 355 ** Add an entry to the end of the write-list. 356 */ 357 static int writeListAppend( 358 sqlite3_file *pFile, 359 sqlite3_int64 iOffset, 360 const u8 *zBuf, 361 int nBuf 362 ){ 363 WriteBuffer *pNew; 364 365 assert((zBuf && nBuf) || (!nBuf && !zBuf)); 366 367 pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf); 368 if( pNew==0 ){ 369 fprintf(stderr, "out of memory in the crash simulator\n"); 370 } 371 memset(pNew, 0, sizeof(WriteBuffer)+nBuf); 372 pNew->iOffset = iOffset; 373 pNew->nBuf = nBuf; 374 pNew->pFile = (CrashFile *)pFile; 375 if( zBuf ){ 376 pNew->zBuf = (u8 *)&pNew[1]; 377 memcpy(pNew->zBuf, zBuf, nBuf); 378 } 379 380 if( g.pWriteList ){ 381 assert(g.pWriteListEnd); 382 g.pWriteListEnd->pNext = pNew; 383 }else{ 384 g.pWriteList = pNew; 385 } 386 g.pWriteListEnd = pNew; 387 388 return SQLITE_OK; 389 } 390 391 /* 392 ** Close a crash-file. 393 */ 394 static int cfClose(sqlite3_file *pFile){ 395 CrashFile *pCrash = (CrashFile *)pFile; 396 writeListSync(pCrash, 0); 397 sqlite3OsClose(pCrash->pRealFile); 398 return SQLITE_OK; 399 } 400 401 /* 402 ** Read data from a crash-file. 403 */ 404 static int cfRead( 405 sqlite3_file *pFile, 406 void *zBuf, 407 int iAmt, 408 sqlite_int64 iOfst 409 ){ 410 CrashFile *pCrash = (CrashFile *)pFile; 411 int nCopy = (int)MIN((i64)iAmt, (pCrash->iSize - iOfst)); 412 413 if( nCopy>0 ){ 414 memcpy(zBuf, &pCrash->zData[iOfst], nCopy); 415 } 416 417 /* Check the file-size to see if this is a short-read */ 418 if( nCopy<iAmt ){ 419 return SQLITE_IOERR_SHORT_READ; 420 } 421 422 return SQLITE_OK; 423 } 424 425 /* 426 ** Write data to a crash-file. 427 */ 428 static int cfWrite( 429 sqlite3_file *pFile, 430 const void *zBuf, 431 int iAmt, 432 sqlite_int64 iOfst 433 ){ 434 CrashFile *pCrash = (CrashFile *)pFile; 435 if( iAmt+iOfst>pCrash->iSize ){ 436 pCrash->iSize = (int)(iAmt+iOfst); 437 } 438 while( pCrash->iSize>pCrash->nData ){ 439 u8 *zNew; 440 int nNew = (pCrash->nData*2) + 4096; 441 zNew = crash_realloc(pCrash->zData, nNew); 442 if( !zNew ){ 443 return SQLITE_NOMEM; 444 } 445 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData); 446 pCrash->nData = nNew; 447 pCrash->zData = zNew; 448 } 449 memcpy(&pCrash->zData[iOfst], zBuf, iAmt); 450 return writeListAppend(pFile, iOfst, zBuf, iAmt); 451 } 452 453 /* 454 ** Truncate a crash-file. 455 */ 456 static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){ 457 CrashFile *pCrash = (CrashFile *)pFile; 458 assert(size>=0); 459 if( pCrash->iSize>size ){ 460 pCrash->iSize = (int)size; 461 } 462 return writeListAppend(pFile, size, 0, 0); 463 } 464 465 /* 466 ** Sync a crash-file. 467 */ 468 static int cfSync(sqlite3_file *pFile, int flags){ 469 CrashFile *pCrash = (CrashFile *)pFile; 470 int isCrash = 0; 471 472 const char *zName = pCrash->zName; 473 const char *zCrashFile = g.zCrashFile; 474 int nName = (int)strlen(zName); 475 int nCrashFile = (int)strlen(zCrashFile); 476 477 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){ 478 nCrashFile--; 479 if( nName>nCrashFile ) nName = nCrashFile; 480 } 481 482 #ifdef TRACE_CRASHTEST 483 printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n", 484 nName, nCrashFile, zName, zCrashFile); 485 #endif 486 487 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){ 488 #ifdef TRACE_CRASHTEST 489 printf("cfSync(): name matched, g.iCrash = %d\n", g.iCrash); 490 #endif 491 if( (--g.iCrash)==0 ) isCrash = 1; 492 } 493 494 return writeListSync(pCrash, isCrash); 495 } 496 497 /* 498 ** Return the current file-size of the crash-file. 499 */ 500 static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 501 CrashFile *pCrash = (CrashFile *)pFile; 502 *pSize = (i64)pCrash->iSize; 503 return SQLITE_OK; 504 } 505 506 /* 507 ** Calls related to file-locks are passed on to the real file handle. 508 */ 509 static int cfLock(sqlite3_file *pFile, int eLock){ 510 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock); 511 } 512 static int cfUnlock(sqlite3_file *pFile, int eLock){ 513 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock); 514 } 515 static int cfCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 516 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile, pResOut); 517 } 518 static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){ 519 if( op==SQLITE_FCNTL_SIZE_HINT ){ 520 CrashFile *pCrash = (CrashFile *)pFile; 521 i64 nByte = *(i64 *)pArg; 522 if( nByte>pCrash->iSize ){ 523 if( SQLITE_OK==writeListAppend(pFile, nByte, 0, 0) ){ 524 pCrash->iSize = (int)nByte; 525 } 526 } 527 return SQLITE_OK; 528 } 529 return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg); 530 } 531 532 /* 533 ** The xSectorSize() and xDeviceCharacteristics() functions return 534 ** the global values configured by the [sqlite_crashparams] tcl 535 * interface. 536 */ 537 static int cfSectorSize(sqlite3_file *pFile){ 538 return g.iSectorSize; 539 } 540 static int cfDeviceCharacteristics(sqlite3_file *pFile){ 541 return g.iDeviceCharacteristics; 542 } 543 544 /* 545 ** Pass-throughs for WAL support. 546 */ 547 static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ 548 return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags); 549 } 550 static void cfShmBarrier(sqlite3_file *pFile){ 551 sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile); 552 } 553 static int cfShmUnmap(sqlite3_file *pFile, int delFlag){ 554 return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag); 555 } 556 static int cfShmMap( 557 sqlite3_file *pFile, /* Handle open on database file */ 558 int iRegion, /* Region to retrieve */ 559 int sz, /* Size of regions */ 560 int w, /* True to extend file if necessary */ 561 void volatile **pp /* OUT: Mapped memory */ 562 ){ 563 return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp); 564 } 565 566 static const sqlite3_io_methods CrashFileVtab = { 567 2, /* iVersion */ 568 cfClose, /* xClose */ 569 cfRead, /* xRead */ 570 cfWrite, /* xWrite */ 571 cfTruncate, /* xTruncate */ 572 cfSync, /* xSync */ 573 cfFileSize, /* xFileSize */ 574 cfLock, /* xLock */ 575 cfUnlock, /* xUnlock */ 576 cfCheckReservedLock, /* xCheckReservedLock */ 577 cfFileControl, /* xFileControl */ 578 cfSectorSize, /* xSectorSize */ 579 cfDeviceCharacteristics, /* xDeviceCharacteristics */ 580 cfShmMap, /* xShmMap */ 581 cfShmLock, /* xShmLock */ 582 cfShmBarrier, /* xShmBarrier */ 583 cfShmUnmap /* xShmUnmap */ 584 }; 585 586 /* 587 ** Application data for the crash VFS 588 */ 589 struct crashAppData { 590 sqlite3_vfs *pOrig; /* Wrapped vfs structure */ 591 }; 592 593 /* 594 ** Open a crash-file file handle. 595 ** 596 ** The caller will have allocated pVfs->szOsFile bytes of space 597 ** at pFile. This file uses this space for the CrashFile structure 598 ** and allocates space for the "real" file structure using 599 ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is 600 ** equal or greater than sizeof(CrashFile). 601 */ 602 static int cfOpen( 603 sqlite3_vfs *pCfVfs, 604 const char *zName, 605 sqlite3_file *pFile, 606 int flags, 607 int *pOutFlags 608 ){ 609 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 610 int rc; 611 CrashFile *pWrapper = (CrashFile *)pFile; 612 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1]; 613 614 memset(pWrapper, 0, sizeof(CrashFile)); 615 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags); 616 617 if( rc==SQLITE_OK ){ 618 i64 iSize; 619 pWrapper->pMethod = &CrashFileVtab; 620 pWrapper->zName = (char *)zName; 621 pWrapper->pRealFile = pReal; 622 rc = sqlite3OsFileSize(pReal, &iSize); 623 pWrapper->iSize = (int)iSize; 624 pWrapper->flags = flags; 625 } 626 if( rc==SQLITE_OK ){ 627 pWrapper->nData = (int)(4096 + pWrapper->iSize); 628 pWrapper->zData = crash_malloc(pWrapper->nData); 629 if( pWrapper->zData ){ 630 /* os_unix.c contains an assert() that fails if the caller attempts 631 ** to read data from the 512-byte locking region of a file opened 632 ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file 633 ** never contains valid data anyhow. So avoid doing such a read here. 634 ** 635 ** UPDATE: It also contains an assert() verifying that each call 636 ** to the xRead() method reads less than 128KB of data. 637 */ 638 i64 iOff; 639 640 memset(pWrapper->zData, 0, pWrapper->nData); 641 for(iOff=0; iOff<pWrapper->iSize; iOff += 512){ 642 int nRead = (int)(pWrapper->iSize - iOff); 643 if( nRead>512 ) nRead = 512; 644 rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff); 645 } 646 }else{ 647 rc = SQLITE_NOMEM; 648 } 649 } 650 if( rc!=SQLITE_OK && pWrapper->pMethod ){ 651 sqlite3OsClose(pFile); 652 } 653 return rc; 654 } 655 656 static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ 657 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 658 return pVfs->xDelete(pVfs, zPath, dirSync); 659 } 660 static int cfAccess( 661 sqlite3_vfs *pCfVfs, 662 const char *zPath, 663 int flags, 664 int *pResOut 665 ){ 666 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 667 return pVfs->xAccess(pVfs, zPath, flags, pResOut); 668 } 669 static int cfFullPathname( 670 sqlite3_vfs *pCfVfs, 671 const char *zPath, 672 int nPathOut, 673 char *zPathOut 674 ){ 675 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 676 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); 677 } 678 static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ 679 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 680 return pVfs->xDlOpen(pVfs, zPath); 681 } 682 static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ 683 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 684 pVfs->xDlError(pVfs, nByte, zErrMsg); 685 } 686 static void (*cfDlSym(sqlite3_vfs *pCfVfs, void *pH, const char *zSym))(void){ 687 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 688 return pVfs->xDlSym(pVfs, pH, zSym); 689 } 690 static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ 691 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 692 pVfs->xDlClose(pVfs, pHandle); 693 } 694 static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ 695 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 696 return pVfs->xRandomness(pVfs, nByte, zBufOut); 697 } 698 static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ 699 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 700 return pVfs->xSleep(pVfs, nMicro); 701 } 702 static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ 703 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 704 return pVfs->xCurrentTime(pVfs, pTimeOut); 705 } 706 static int cfGetLastError(sqlite3_vfs *pCfVfs, int n, char *z){ 707 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 708 return pVfs->xGetLastError(pVfs, n, z); 709 } 710 711 static int processDevSymArgs( 712 Tcl_Interp *interp, 713 int objc, 714 Tcl_Obj *CONST objv[], 715 int *piDeviceChar, 716 int *piSectorSize 717 ){ 718 struct DeviceFlag { 719 char *zName; 720 int iValue; 721 } aFlag[] = { 722 { "atomic", SQLITE_IOCAP_ATOMIC }, 723 { "atomic512", SQLITE_IOCAP_ATOMIC512 }, 724 { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, 725 { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, 726 { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, 727 { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, 728 { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, 729 { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, 730 { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, 731 { "sequential", SQLITE_IOCAP_SEQUENTIAL }, 732 { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, 733 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE }, 734 { 0, 0 } 735 }; 736 737 int i; 738 int iDc = 0; 739 int iSectorSize = 0; 740 int setSectorsize = 0; 741 int setDeviceChar = 0; 742 743 for(i=0; i<objc; i+=2){ 744 int nOpt; 745 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); 746 747 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) 748 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) 749 ){ 750 Tcl_AppendResult(interp, 751 "Bad option: \"", zOpt, 752 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 753 ); 754 return TCL_ERROR; 755 } 756 if( i==objc-1 ){ 757 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); 758 return TCL_ERROR; 759 } 760 761 if( zOpt[1]=='s' ){ 762 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){ 763 return TCL_ERROR; 764 } 765 setSectorsize = 1; 766 }else{ 767 int j; 768 Tcl_Obj **apObj; 769 int nObj; 770 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ 771 return TCL_ERROR; 772 } 773 for(j=0; j<nObj; j++){ 774 int rc; 775 int iChoice; 776 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); 777 Tcl_IncrRefCount(pFlag); 778 Tcl_UtfToLower(Tcl_GetString(pFlag)); 779 780 rc = Tcl_GetIndexFromObjStruct( 781 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice 782 ); 783 Tcl_DecrRefCount(pFlag); 784 if( rc ){ 785 return TCL_ERROR; 786 } 787 788 iDc |= aFlag[iChoice].iValue; 789 } 790 setDeviceChar = 1; 791 } 792 } 793 794 if( setDeviceChar ){ 795 *piDeviceChar = iDc; 796 } 797 if( setSectorsize ){ 798 *piSectorSize = iSectorSize; 799 } 800 801 return TCL_OK; 802 } 803 804 /* 805 ** tclcmd: sqlite3_crash_now 806 ** 807 ** Simulate a crash immediately. This function does not return 808 ** (writeListSync() calls exit(-1)). 809 */ 810 static int crashNowCmd( 811 void * clientData, 812 Tcl_Interp *interp, 813 int objc, 814 Tcl_Obj *CONST objv[] 815 ){ 816 if( objc!=1 ){ 817 Tcl_WrongNumArgs(interp, 1, objv, ""); 818 return TCL_ERROR; 819 } 820 writeListSync(0, 1); 821 assert( 0 ); 822 return TCL_OK; 823 } 824 825 /* 826 ** tclcmd: sqlite_crash_enable ENABLE 827 ** 828 ** Parameter ENABLE must be a boolean value. If true, then the "crash" 829 ** vfs is added to the system. If false, it is removed. 830 */ 831 static int crashEnableCmd( 832 void * clientData, 833 Tcl_Interp *interp, 834 int objc, 835 Tcl_Obj *CONST objv[] 836 ){ 837 int isEnable; 838 static sqlite3_vfs crashVfs = { 839 2, /* iVersion */ 840 0, /* szOsFile */ 841 0, /* mxPathname */ 842 0, /* pNext */ 843 "crash", /* zName */ 844 0, /* pAppData */ 845 846 cfOpen, /* xOpen */ 847 cfDelete, /* xDelete */ 848 cfAccess, /* xAccess */ 849 cfFullPathname, /* xFullPathname */ 850 cfDlOpen, /* xDlOpen */ 851 cfDlError, /* xDlError */ 852 cfDlSym, /* xDlSym */ 853 cfDlClose, /* xDlClose */ 854 cfRandomness, /* xRandomness */ 855 cfSleep, /* xSleep */ 856 cfCurrentTime, /* xCurrentTime */ 857 cfGetLastError, /* xGetLastError */ 858 0, /* xCurrentTimeInt64 */ 859 }; 860 861 if( objc!=2 ){ 862 Tcl_WrongNumArgs(interp, 1, objv, "ENABLE"); 863 return TCL_ERROR; 864 } 865 866 if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){ 867 return TCL_ERROR; 868 } 869 870 if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){ 871 return TCL_OK; 872 } 873 874 if( crashVfs.pAppData==0 ){ 875 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); 876 crashVfs.mxPathname = pOriginalVfs->mxPathname; 877 crashVfs.pAppData = (void *)pOriginalVfs; 878 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; 879 sqlite3_vfs_register(&crashVfs, 0); 880 }else{ 881 crashVfs.pAppData = 0; 882 sqlite3_vfs_unregister(&crashVfs); 883 } 884 885 return TCL_OK; 886 } 887 888 /* 889 ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE 890 ** 891 ** This procedure implements a TCL command that enables crash testing 892 ** in testfixture. Once enabled, crash testing cannot be disabled. 893 ** 894 ** Available options are "-characteristics" and "-sectorsize". Both require 895 ** an argument. For -sectorsize, this is the simulated sector size in 896 ** bytes. For -characteristics, the argument must be a list of io-capability 897 ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", 898 ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", 899 ** "atomic64K", "sequential" and "safe_append". 900 ** 901 ** Example: 902 ** 903 ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 904 ** 905 */ 906 static int crashParamsObjCmd( 907 void * clientData, 908 Tcl_Interp *interp, 909 int objc, 910 Tcl_Obj *CONST objv[] 911 ){ 912 int iDelay; 913 const char *zCrashFile; 914 int nCrashFile, iDc, iSectorSize; 915 916 iDc = -1; 917 iSectorSize = -1; 918 919 if( objc<3 ){ 920 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE"); 921 goto error; 922 } 923 924 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); 925 if( nCrashFile>=sizeof(g.zCrashFile) ){ 926 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); 927 goto error; 928 } 929 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ 930 goto error; 931 } 932 933 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){ 934 return TCL_ERROR; 935 } 936 937 if( iDc>=0 ){ 938 g.iDeviceCharacteristics = iDc; 939 } 940 if( iSectorSize>=0 ){ 941 g.iSectorSize = iSectorSize; 942 } 943 944 g.iCrash = iDelay; 945 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1); 946 sqlite3CrashTestEnable = 1; 947 return TCL_OK; 948 949 error: 950 return TCL_ERROR; 951 } 952 953 static int devSymObjCmd( 954 void * clientData, 955 Tcl_Interp *interp, 956 int objc, 957 Tcl_Obj *CONST objv[] 958 ){ 959 void devsym_register(int iDeviceChar, int iSectorSize); 960 961 int iDc = -1; 962 int iSectorSize = -1; 963 964 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){ 965 return TCL_ERROR; 966 } 967 devsym_register(iDc, iSectorSize); 968 969 return TCL_OK; 970 971 } 972 973 /* 974 ** tclcmd: unregister_devsim 975 */ 976 static int dsUnregisterObjCmd( 977 void * clientData, 978 Tcl_Interp *interp, 979 int objc, 980 Tcl_Obj *CONST objv[] 981 ){ 982 void devsym_unregister(void); 983 984 if( objc!=1 ){ 985 Tcl_WrongNumArgs(interp, 1, objv, ""); 986 return TCL_ERROR; 987 } 988 989 devsym_unregister(); 990 return TCL_OK; 991 } 992 993 /* 994 ** tclcmd: register_jt_vfs ?-default? PARENT-VFS 995 */ 996 static int jtObjCmd( 997 void * clientData, 998 Tcl_Interp *interp, 999 int objc, 1000 Tcl_Obj *CONST objv[] 1001 ){ 1002 int jt_register(char *, int); 1003 char *zParent = 0; 1004 1005 if( objc!=2 && objc!=3 ){ 1006 Tcl_WrongNumArgs(interp, 1, objv, "?-default? PARENT-VFS"); 1007 return TCL_ERROR; 1008 } 1009 zParent = Tcl_GetString(objv[1]); 1010 if( objc==3 ){ 1011 if( strcmp(zParent, "-default") ){ 1012 Tcl_AppendResult(interp, 1013 "bad option \"", zParent, "\": must be -default", 0 1014 ); 1015 return TCL_ERROR; 1016 } 1017 zParent = Tcl_GetString(objv[2]); 1018 } 1019 1020 if( !(*zParent) ){ 1021 zParent = 0; 1022 } 1023 if( jt_register(zParent, objc==3) ){ 1024 Tcl_AppendResult(interp, "Error in jt_register", 0); 1025 return TCL_ERROR; 1026 } 1027 1028 return TCL_OK; 1029 } 1030 1031 /* 1032 ** tclcmd: unregister_jt_vfs 1033 */ 1034 static int jtUnregisterObjCmd( 1035 void * clientData, 1036 Tcl_Interp *interp, 1037 int objc, 1038 Tcl_Obj *CONST objv[] 1039 ){ 1040 void jt_unregister(void); 1041 1042 if( objc!=1 ){ 1043 Tcl_WrongNumArgs(interp, 1, objv, ""); 1044 return TCL_ERROR; 1045 } 1046 1047 jt_unregister(); 1048 return TCL_OK; 1049 } 1050 1051 #endif /* SQLITE_OMIT_DISKIO */ 1052 1053 /* 1054 ** This procedure registers the TCL procedures defined in this file. 1055 */ 1056 int Sqlitetest6_Init(Tcl_Interp *interp){ 1057 #ifndef SQLITE_OMIT_DISKIO 1058 Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0); 1059 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); 1060 Tcl_CreateObjCommand(interp, "sqlite3_crash_now", crashNowCmd, 0, 0); 1061 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); 1062 Tcl_CreateObjCommand(interp, "unregister_devsim", dsUnregisterObjCmd, 0, 0); 1063 Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0); 1064 Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0); 1065 #endif 1066 return TCL_OK; 1067 } 1068 1069 #endif /* SQLITE_TEST */ 1070