/ Check-in [5bced239]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Fix some problems in the crash-test backend. (CVS 4256)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5bced2392ad77aff0aa1ddea83f2ff9e3ffe28a8
User & Date: danielk1977 2007-08-21 13:07:47
Context
2007-08-21
13:11
Avoid journalling an extra page when a btree insert operation uses the 'quick-balance' trick. (CVS 4257) check-in: 0da48209 user: danielk1977 tags: trunk
13:07
Fix some problems in the crash-test backend. (CVS 4256) check-in: 5bced239 user: danielk1977 tags: trunk
10:44
Remove unnecessary #includes of "os.h". New mutex implementations. (CVS 4255) check-in: fbbd5bda user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/os.c.

229
230
231
232
233
234
235

236
237
238
239
240
241
242
243
244
245
246

247
248
249
  if( makeDflt || vfsList==0 ){
    pVfs->pNext = vfsList;
    vfsList = pVfs;
  }else{
    pVfs->pNext = vfsList->pNext;
    pVfs->pNext = pVfs;
  }

  sqlite3_mutex_leave(mutex);
  return SQLITE_OK;
}

/*
** Unregister a VFS so that it is no longer accessible.
*/
int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
  sqlite3_mutex_enter(mutex);
  vfsUnlink(pVfs);

  sqlite3_mutex_leave(mutex);
  return SQLITE_OK;
}







>











>



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  if( makeDflt || vfsList==0 ){
    pVfs->pNext = vfsList;
    vfsList = pVfs;
  }else{
    pVfs->pNext = vfsList->pNext;
    pVfs->pNext = pVfs;
  }
  assert(vfsList);
  sqlite3_mutex_leave(mutex);
  return SQLITE_OK;
}

/*
** Unregister a VFS so that it is no longer accessible.
*/
int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
  sqlite3_mutex_enter(mutex);
  vfsUnlink(pVfs);
  assert(vfsList);
  sqlite3_mutex_leave(mutex);
  return SQLITE_OK;
}

Changes to src/test6.c.

122
123
124
125
126
127
128
129



130
131
132
133
134
135
136
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

478
479
480
481
482

483
484
485
486

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

511



512
513




























514
515
516
517
518
519
520
...
536
537
538
539
540
541
542
543
544
545
546
547


548
549
550
551

















552

553
554




555
556
557
558
559
560
561
562
563
};

struct CrashFile {
  const sqlite3_io_methods *pMethod;   /* Must be first */
  sqlite3_file *pRealFile;             /* Underlying "real" file handle */
  char *zName;

  /* Cache of the entire file. */



  int iSize;                           /* Size of file in bytes */
  int nData;                           /* Size of buffer allocated at zData */
  u8 *zData;                           /* Buffer containing file contents */
};

struct CrashGlobal {
  WriteBuffer *pWriteList;     /* Head of write-list */
................................................................................

/*
** Close a crash-file.
*/
static int cfClose(sqlite3_file *pFile){
  CrashFile *pCrash = (CrashFile *)pFile;
  writeListSync(pCrash, 0);
  sqlite3OsCloseFree(pCrash->pRealFile);
  return SQLITE_OK;
}

/*
** Read data from a crash-file.
*/
static int cfRead(
................................................................................
  cfDeviceCharacteristics       /* xDeviceCharacteristics */
};

/*
** Application data for the crash VFS
*/
struct crashAppData {
  int (*xOpen)(void*,const char*,sqlite3_file*,int,int*); /* Original xOpen */
  void *pAppData;                                      /* Original pAppData */
};

/*
** Open a crash-file file handle. The vfs pVfs is used to open
** the underlying real file.
**
** The caller will have allocated pVfs->szOsFile bytes of space
** at pFile. This file uses this space for the CrashFile structure
** and allocates space for the "real" file structure using 
** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
** equal or greater than sizeof(CrashFile).
*/
static int sqlite3CrashFileOpen(
  sqlite3_vfs *pVfs,
  const char *zName,
  sqlite3_file *pFile,
  int flags,
  int *pOutFlags
){

  int rc;
  if( sqlite3CrashTestEnable ){
    CrashFile *pWrapper = (CrashFile *)pFile;
    sqlite3_file *pReal;
    assert(pVfs->szOsFile>=sizeof(CrashFile));

    memset(pWrapper, 0, sizeof(CrashFile));
    sqlite3CrashTestEnable = 0;
    rc = sqlite3OsOpenMalloc(pVfs, zName, &pReal, flags, pOutFlags);
    sqlite3CrashTestEnable = 1;

    if( rc==SQLITE_OK ){
      i64 iSize;
      pWrapper->pMethod = &CrashFileVtab;
      pWrapper->zName = (char *)zName;
      pWrapper->pRealFile = pReal;
      rc = sqlite3OsFileSize(pReal, &iSize);
      pWrapper->iSize = (int)iSize;
    }
    if( rc==SQLITE_OK ){
      pWrapper->nData = (4096 + pWrapper->iSize);
      pWrapper->zData = (char *)sqlite3_malloc(pWrapper->nData);
      if( pWrapper->zData ){
        memset(pWrapper->zData, 0, pWrapper->nData);
        rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0); 
      }else{
        rc = SQLITE_NOMEM;
      }
    }
    if( rc!=SQLITE_OK && pWrapper->pMethod ){
      sqlite3OsClose(pFile);
    }
  }else{
    struct crashAppData *pData = (struct crashAppData*)pVfs->pAppData;
    rc = pData->xOpen(pData->pAppData, zName, pFile, flags, pOutFlags);

  }



  return rc;
}





























/*
** tclcmd:   sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
**
** This procedure implements a TCL command that enables crash testing
** in testfixture.  Once enabled, crash testing cannot be disabled.
**
................................................................................
  int objc,
  Tcl_Obj *CONST objv[]
){
  int i;
  int iDelay;
  const char *zCrashFile;
  int nCrashFile;
  static sqlite3_vfs crashVfs, *pOriginalVfs;
  static struct crashAppData appData;

  if( pOriginalVfs==0 ){
    pOriginalVfs = sqlite3_vfs_find(0);


    crashVfs = *pOriginalVfs;
    crashVfs.xOpen = sqlite3CrashFileOpen;
    crashVfs.vfsMutex = 0;
    crashVfs.nRef = 0;

















    crashVfs.pAppData = &appData;

    appData.xOpen = pOriginalVfs->xOpen;
    appData.pAppData = pOriginalVfs->pAppData;




    sqlite3_vfs_release(pOriginalVfs);
    sqlite3_vfs_unregister(pOriginalVfs);
    sqlite3_vfs_register(&crashVfs, 1);
  }

  int iDc = 0;
  int iSectorSize = 0;
  int setSectorsize = 0;
  int setDeviceChar = 0;







|
>
>
>







 







|







 







|
<












|
|





>

<
|
|
<
>
|
<
|
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
>
|
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<


<
|
>
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
|
>
>
>
>

|







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
...
453
454
455
456
457
458
459
460

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

482
483

484
485

486

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
...
566
567
568
569
570
571
572

573
574

575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
};

struct CrashFile {
  const sqlite3_io_methods *pMethod;   /* Must be first */
  sqlite3_file *pRealFile;             /* Underlying "real" file handle */
  char *zName;

  /* Cache of the entire file. This is used to speed up OsRead() and 
  ** OsFileSize() calls. Although both could be done by traversing the
  ** write-list, in practice this is impractically slow.
  */
  int iSize;                           /* Size of file in bytes */
  int nData;                           /* Size of buffer allocated at zData */
  u8 *zData;                           /* Buffer containing file contents */
};

struct CrashGlobal {
  WriteBuffer *pWriteList;     /* Head of write-list */
................................................................................

/*
** Close a crash-file.
*/
static int cfClose(sqlite3_file *pFile){
  CrashFile *pCrash = (CrashFile *)pFile;
  writeListSync(pCrash, 0);
  sqlite3OsClose(pCrash->pRealFile);
  return SQLITE_OK;
}

/*
** Read data from a crash-file.
*/
static int cfRead(
................................................................................
  cfDeviceCharacteristics       /* xDeviceCharacteristics */
};

/*
** Application data for the crash VFS
*/
struct crashAppData {
  sqlite3_vfs *pOrig;                   /* Wrapped vfs structure */

};

/*
** Open a crash-file file handle. The vfs pVfs is used to open
** the underlying real file.
**
** The caller will have allocated pVfs->szOsFile bytes of space
** at pFile. This file uses this space for the CrashFile structure
** and allocates space for the "real" file structure using 
** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
** equal or greater than sizeof(CrashFile).
*/
static int cfOpen(
  void *pAppData,
  const char *zName,
  sqlite3_file *pFile,
  int flags,
  int *pOutFlags
){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  int rc;

  CrashFile *pWrapper = (CrashFile *)pFile;
  sqlite3_file *pReal = &pWrapper[1];


  memset(pWrapper, 0, sizeof(CrashFile));

  rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags);


  if( rc==SQLITE_OK ){
    i64 iSize;
    pWrapper->pMethod = &CrashFileVtab;
    pWrapper->zName = (char *)zName;
    pWrapper->pRealFile = pReal;
    rc = sqlite3OsFileSize(pReal, &iSize);
    pWrapper->iSize = (int)iSize;
  }
  if( rc==SQLITE_OK ){
    pWrapper->nData = (4096 + pWrapper->iSize);
    pWrapper->zData = (char *)sqlite3_malloc(pWrapper->nData);
    if( pWrapper->zData ){
      memset(pWrapper->zData, 0, pWrapper->nData);
      rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0); 
    }else{
      rc = SQLITE_NOMEM;
    }
  }
  if( rc!=SQLITE_OK && pWrapper->pMethod ){
    sqlite3OsClose(pFile);
  }



  return rc;
}

static int cfDelete(void *pAppData, const char *zPath, int dirSync){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xDelete(pVfs->pAppData, zPath, dirSync);
}
static int cfAccess(void *pAppData, const char *zPath, int flags){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xAccess(pVfs->pAppData, zPath, flags);
}
static int cfGetTempName(void *pAppData, char *zBufOut){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xGetTempName(pVfs->pAppData, zBufOut);
}
static int cfFullPathname(void *pAppData, const char *zPath, char *zPathOut){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xFullPathname(pVfs->pAppData, zPath, zPathOut);
}
static void *cfDlOpen(void *pAppData, const char *zPath){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xDlOpen(pVfs->pAppData, zPath);
}
static int cfRandomness(void *pAppData, int nByte, char *zBufOut){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xRandomness(pVfs->pAppData, nByte, zBufOut);
}
static int cfSleep(void *pAppData, int nMicro){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xSleep(pVfs->pAppData, nMicro);
}
static int cfCurrentTime(void *pAppData, double *pTimeOut){
  sqlite3_vfs *pVfs = (sqlite3_vfs *)pAppData;
  return pVfs->xCurrentTime(pVfs->pAppData, pTimeOut);
}

/*
** tclcmd:   sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
**
** This procedure implements a TCL command that enables crash testing
** in testfixture.  Once enabled, crash testing cannot be disabled.
**
................................................................................
  int objc,
  Tcl_Obj *CONST objv[]
){
  int i;
  int iDelay;
  const char *zCrashFile;
  int nCrashFile;

  static struct crashAppData appData;


  static sqlite3_vfs crashVfs = {
    1,                  /* iVersion */
    0,                  /* szOsFile */
    0,                  /* mxPathname */
    0,                  /* nRef */
    0,                  /* vfsMutex */
    0,                  /* pNext */
    "crash",            /* zName */
    0,                  /* pAppData */
  
    cfOpen,               /* xOpen */
    cfDelete,             /* xDelete */
    cfAccess,             /* xAccess */
    cfGetTempName,        /* xGetTempName */
    cfFullPathname,       /* xFullPathname */
    cfDlOpen,             /* xDlOpen */
    0,                    /* xDlError */
    0,                    /* xDlSym */
    0,                    /* xDlClose */
    cfRandomness,         /* xRandomness */
    cfSleep,              /* xSleep */
    cfCurrentTime         /* xCurrentTime */
  };

  if( crashVfs.pAppData==0 ){
    sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0);
    crashVfs.xDlError = pOriginalVfs->xDlError;
    crashVfs.xDlSym = pOriginalVfs->xDlSym;
    crashVfs.xDlClose = pOriginalVfs->xDlClose;
    crashVfs.mxPathname = pOriginalVfs->mxPathname;
    crashVfs.pAppData = (void *)pOriginalVfs;
    crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile;
    sqlite3_vfs_release(pOriginalVfs);
    /* sqlite3_vfs_unregister(pOriginalVfs); */
    sqlite3_vfs_register(&crashVfs, 1);
  }

  int iDc = 0;
  int iSectorSize = 0;
  int setSectorsize = 0;
  int setDeviceChar = 0;