/ Check-in [716d99f3]
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:Changes to the way faults are injected into xShmXXX VFS calls.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 716d99f3929b466c7a17190e0f18de8ab0e7f1fa
User & Date: dan 2010-06-01 19:15:19
Context
2010-06-01
21:02
The shared-memory used by WAL on linux now really is shared memory in /dev/shm. On other unix flavors, the file is in a temporary directory rather than in the same directory as the database. check-in: fc18c4aa user: drh tags: trunk
19:15
Changes to the way faults are injected into xShmXXX VFS calls. check-in: 716d99f3 user: dan tags: trunk
17:46
Change the OOM and IO error test cases in walfault.test so that each test case runs both types of error simulation. check-in: b627e153 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test_vfs.c.

44
45
46
47
48
49
50




51
52
53
54
55
56
57
58
59
60
61
62










63
64
65
66
67
68
69
...
455
456
457
458
459
460
461


462

463
464
465
466
467
468
469
...
494
495
496
497
498
499
500

501
502
503
504

505
506
507
508
509
510
511
...
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
...
550
551
552
553
554
555
556

557
558
559
560
561
562
563
564
565
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
...
596
597
598
599
600
601
602

603
604
605
606

607
608
609
610
611
612
613
...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
...
663
664
665
666
667
668
669

































































670
671
672
673
674
675
676
...
800
801
802
803
804
805
806

807
808
809
810
811
812
813
  sqlite3_vfs *pParent;           /* The VFS to use for file IO */
  sqlite3_vfs *pVfs;              /* The testvfs registered with SQLite */
  Tcl_Interp *interp;             /* Interpreter to run script in */
  int nScript;                    /* Number of elements in array apScript */
  Tcl_Obj **apScript;             /* Script to execute */
  TestvfsBuffer *pBuffer;         /* List of shared buffers */
  int isNoshm;




};

/*
** A shared-memory buffer.
*/
struct TestvfsBuffer {
  char *zFile;                    /* Associated file name */
  int n;                          /* Size of allocated buffer in bytes */
  u8 *a;                          /* Buffer allocated using ckalloc() */
  int nRef;                       /* Number of references to this object */
  TestvfsBuffer *pNext;           /* Next in linked list of all buffers */
};












#define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)


/*
** Method declarations for TestvfsFile.
................................................................................
  **   SCRIPT xShmOpen FILENAME
  **
  ** If the script returns an SQLite error code other than SQLITE_OK, an
  ** error is returned to the caller. If it returns SQLITE_OK, the new
  ** connection is named "anon". Otherwise, the value returned by the
  ** script is used as the connection name.
  */


  tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0);

  if( tvfsResultCode(p, &rc) ){
    if( rc!=SQLITE_OK ) return rc;
    pId = Tcl_NewStringObj("anon", -1);
  }else{
    pId = Tcl_GetObjResult(p->interp);
  }
  Tcl_IncrRefCount(pId);
................................................................................
  int reqSize,
  int *pNewSize
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmSize", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);

  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqSize, pNewSize);
  }
  return rc;
}

static int tvfsShmGet(
................................................................................
  int *pMapSize, 
  volatile void **pp
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmGet", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);

  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqMapSize, pMapSize);
    *pp = pFd->pShm->a;
  }
  return rc;
}

static int tvfsShmRelease(sqlite3_file *pFile){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmRelease", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);


  return rc;
}

static int tvfsShmLock(
  sqlite3_file *pFile,
  int ofst,
................................................................................
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
  int nLock;
  char zLock[80];


  sqlite3_snprintf(sizeof(zLock), zLock, "%d %d", ofst, n);
  nLock = strlen(zLock);
  if( flags & SQLITE_SHM_LOCK ){
    strcpy(&zLock[nLock], " lock");
  }else{
    strcpy(&zLock[nLock], " unlock");
  }
  nLock += strlen(&zLock[nLock]);
  if( flags & SQLITE_SHM_SHARED ){
    strcpy(&zLock[nLock], " shared");
  }else{
    strcpy(&zLock[nLock], " exclusive");
  }
  tvfsExecTcl(p, "xShmLock", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId,
      Tcl_NewStringObj(zLock, -1)
  );
  tvfsResultCode(p, &rc);

  return rc;
}

static void tvfsShmBarrier(sqlite3_file *pFile){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmBarrier", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);

}

static int tvfsShmClose(
  sqlite3_file *pFile,
  int deleteFlag
){
  int rc = SQLITE_OK;
................................................................................
  TestvfsBuffer *pBuffer = pFd->pShm;

  assert( pFd->pShmId && pFd->pShm );
#if 0
  assert( (deleteFlag!=0)==(pBuffer->nRef==1) );
#endif


  tvfsExecTcl(p, "xShmClose", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);


  pBuffer->nRef--;
  if( pBuffer->nRef==0 ){
    TestvfsBuffer **pp;
    for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext));
    *pp = (*pp)->pNext;
    ckfree((char *)pBuffer->a);
................................................................................
  ClientData cd,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  Testvfs *p = (Testvfs *)cd;

  static const char *CMD_strs[] = { "shm", "delete", 0 };
  enum DB_enum { CMD_SHM, CMD_DELETE };
  int i;
  
  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
    return TCL_ERROR;
  }
  if( Tcl_GetIndexFromObj(interp, objv[1], CMD_strs, "subcommand", 0, &i) ){
................................................................................
        pBuffer->a = (u8 *)ckrealloc((char *)pBuffer->a, n);
        pBuffer->n = n;
        memcpy(pBuffer->a, a, n);
      }
      Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBuffer->a, pBuffer->n));
      break;
    }

































































    case CMD_DELETE: {
      Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
      break;
    }
  }

  return TCL_OK;
................................................................................
  memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
  pVfs->pAppData = (void *)p;
  pVfs->zName = p->zName;
  pVfs->mxPathname = p->pParent->mxPathname;
  pVfs->szOsFile += p->pParent->szOsFile;
  p->pVfs = pVfs;
  p->isNoshm = isNoshm;


  Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del);
  sqlite3_vfs_register(pVfs, 0);

  return TCL_OK;

 bad_args:







>
>
>
>












>
>
>
>
>
>
>
>
>
>







 







>
>
|
>







 







>
|
|
|
|
>







 







>
|
|
|
|
>












>
|
|
|
|
>







 







>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>




<



>
|
|
|
<
>







 







>
|
|
|
|
>







 







|
|







 







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







 







>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
...
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
...
533
534
535
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
564
565
566
567
568
569
570
...
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
616
617
618
...
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
...
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
...
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  sqlite3_vfs *pParent;           /* The VFS to use for file IO */
  sqlite3_vfs *pVfs;              /* The testvfs registered with SQLite */
  Tcl_Interp *interp;             /* Interpreter to run script in */
  int nScript;                    /* Number of elements in array apScript */
  Tcl_Obj **apScript;             /* Script to execute */
  TestvfsBuffer *pBuffer;         /* List of shared buffers */
  int isNoshm;

  int mask;
  int iIoerrCnt;
  int ioerr;
};

/*
** A shared-memory buffer.
*/
struct TestvfsBuffer {
  char *zFile;                    /* Associated file name */
  int n;                          /* Size of allocated buffer in bytes */
  u8 *a;                          /* Buffer allocated using ckalloc() */
  int nRef;                       /* Number of references to this object */
  TestvfsBuffer *pNext;           /* Next in linked list of all buffers */
};

#define TESTVFS_SHMOPEN_MASK    0x00000001
#define TESTVFS_SHMSIZE_MASK    0x00000002
#define TESTVFS_SHMGET_MASK     0x00000004
#define TESTVFS_SHMRELEASE_MASK 0x00000008
#define TESTVFS_SHMLOCK_MASK    0x00000010
#define TESTVFS_SHMBARRIER_MASK 0x00000020
#define TESTVFS_SHMCLOSE_MASK   0x00000040

#define TESTVFS_ALL_MASK        0x0000007F


#define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)


/*
** Method declarations for TestvfsFile.
................................................................................
  **   SCRIPT xShmOpen FILENAME
  **
  ** If the script returns an SQLite error code other than SQLITE_OK, an
  ** error is returned to the caller. If it returns SQLITE_OK, the new
  ** connection is named "anon". Otherwise, the value returned by the
  ** script is used as the connection name.
  */
  Tcl_ResetResult(p->interp);
  if( p->mask&TESTVFS_SHMOPEN_MASK ){
    tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0);
  }
  if( tvfsResultCode(p, &rc) ){
    if( rc!=SQLITE_OK ) return rc;
    pId = Tcl_NewStringObj("anon", -1);
  }else{
    pId = Tcl_GetObjResult(p->interp);
  }
  Tcl_IncrRefCount(pId);
................................................................................
  int reqSize,
  int *pNewSize
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);

  if( p->mask&TESTVFS_SHMSIZE_MASK ){
    tvfsExecTcl(p, "xShmSize", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
    );
    tvfsResultCode(p, &rc);
  }
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqSize, pNewSize);
  }
  return rc;
}

static int tvfsShmGet(
................................................................................
  int *pMapSize, 
  volatile void **pp
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);

  if( p->mask&TESTVFS_SHMGET_MASK ){
    tvfsExecTcl(p, "xShmGet", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
    );
    tvfsResultCode(p, &rc);
  }
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqMapSize, pMapSize);
    *pp = pFd->pShm->a;
  }
  return rc;
}

static int tvfsShmRelease(sqlite3_file *pFile){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);

  if( p->mask&TESTVFS_SHMRELEASE_MASK ){
    tvfsExecTcl(p, "xShmRelease", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
    );
    tvfsResultCode(p, &rc);
  }

  return rc;
}

static int tvfsShmLock(
  sqlite3_file *pFile,
  int ofst,
................................................................................
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
  int nLock;
  char zLock[80];

  if( p->mask&TESTVFS_SHMLOCK_MASK ){
    sqlite3_snprintf(sizeof(zLock), zLock, "%d %d", ofst, n);
    nLock = strlen(zLock);
    if( flags & SQLITE_SHM_LOCK ){
      strcpy(&zLock[nLock], " lock");
    }else{
      strcpy(&zLock[nLock], " unlock");
    }
    nLock += strlen(&zLock[nLock]);
    if( flags & SQLITE_SHM_SHARED ){
      strcpy(&zLock[nLock], " shared");
    }else{
      strcpy(&zLock[nLock], " exclusive");
    }
    tvfsExecTcl(p, "xShmLock", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId,
        Tcl_NewStringObj(zLock, -1)
    );
    tvfsResultCode(p, &rc);
  }
  return rc;
}

static void tvfsShmBarrier(sqlite3_file *pFile){

  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);

  if( p->mask&TESTVFS_SHMBARRIER_MASK ){
    tvfsExecTcl(p, "xShmBarrier", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
    );

  }
}

static int tvfsShmClose(
  sqlite3_file *pFile,
  int deleteFlag
){
  int rc = SQLITE_OK;
................................................................................
  TestvfsBuffer *pBuffer = pFd->pShm;

  assert( pFd->pShmId && pFd->pShm );
#if 0
  assert( (deleteFlag!=0)==(pBuffer->nRef==1) );
#endif

  if( p->mask&TESTVFS_SHMCLOSE_MASK ){
    tvfsExecTcl(p, "xShmClose", 
        Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
    );
    tvfsResultCode(p, &rc);
  }

  pBuffer->nRef--;
  if( pBuffer->nRef==0 ){
    TestvfsBuffer **pp;
    for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext));
    *pp = (*pp)->pNext;
    ckfree((char *)pBuffer->a);
................................................................................
  ClientData cd,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  Testvfs *p = (Testvfs *)cd;

  static const char *CMD_strs[] = { "shm", "delete", "filter", "ioerr", 0 };
  enum DB_enum { CMD_SHM, CMD_DELETE, CMD_FILTER, CMD_IOERR };
  int i;
  
  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
    return TCL_ERROR;
  }
  if( Tcl_GetIndexFromObj(interp, objv[1], CMD_strs, "subcommand", 0, &i) ){
................................................................................
        pBuffer->a = (u8 *)ckrealloc((char *)pBuffer->a, n);
        pBuffer->n = n;
        memcpy(pBuffer->a, a, n);
      }
      Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBuffer->a, pBuffer->n));
      break;
    }

    case CMD_FILTER: {
      static struct VfsMethod {
        char *zName;
        int mask;
      } vfsmethod [] = {
        { "xShmOpen",    TESTVFS_SHMOPEN_MASK },
        { "xShmSize",    TESTVFS_SHMSIZE_MASK },
        { "xShmGet",     TESTVFS_SHMGET_MASK },
        { "xShmRelease", TESTVFS_SHMRELEASE_MASK },
        { "xShmLock",    TESTVFS_SHMLOCK_MASK },
        { "xShmBarrier", TESTVFS_SHMBARRIER_MASK },
        { "xShmClose",   TESTVFS_SHMCLOSE_MASK },
      };
      Tcl_Obj **apElem = 0;
      int nElem = 0;
      int i;
      int mask = 0;
      if( objc!=3 ){
        Tcl_WrongNumArgs(interp, 2, objv, "LIST");
        return TCL_ERROR;
      }
      if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
        return TCL_ERROR;
      }
      Tcl_ResetResult(interp);
      for(i=0; i<nElem; i++){
        int iMethod;
        char *zElem = Tcl_GetString(apElem[i]);
        for(iMethod=0; iMethod<ArraySize(vfsmethod); iMethod++){
          if( strcmp(zElem, vfsmethod[iMethod].zName)==0 ){
            mask |= vfsmethod[iMethod].mask;
            break;
          }
        }
        if( iMethod==ArraySize(vfsmethod) ){
          Tcl_AppendResult(interp, "unknown method: ", zElem, 0);
          return TCL_ERROR;
        }
      }
      p->mask = mask;
      break;
    }

    case CMD_IOERR: {
      int iRet = ((p->iIoerrCnt<0) ? (1+(p->iIoerrCnt*-1)) : 0);
      if( objc==2 ){
        p->ioerr = 0;
        p->iIoerrCnt = 0;
      }else if( objc==4 ){
        int iCnt, iPersist;
        if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iCnt)
         || TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &iPersist)
        ){
          return TCL_ERROR;
        }
        p->ioerr = (iPersist!=0) + 1;
        p->iIoerrCnt = iCnt;
      }else{
        Tcl_AppendResult(interp, "Bad args", 0);
      }
      Tcl_SetObjResult(interp, Tcl_NewIntObj(iRet));
      break;
    }

    case CMD_DELETE: {
      Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
      break;
    }
  }

  return TCL_OK;
................................................................................
  memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
  pVfs->pAppData = (void *)p;
  pVfs->zName = p->zName;
  pVfs->mxPathname = p->pParent->mxPathname;
  pVfs->szOsFile += p->pParent->szOsFile;
  p->pVfs = pVfs;
  p->isNoshm = isNoshm;
  p->mask = TESTVFS_ALL_MASK;

  Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del);
  sqlite3_vfs_register(pVfs, 0);

  return TCL_OK;

 bad_args:

Changes to test/walfault.test.

88
89
90
91
92
93
94
95
96
97
98

99
100
101
102
103
104
105
...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
} {8}
do_faultsim_test walfault-2 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { SELECT count(*) FROM x }
} -test {

  # Test that all the rows in the WAL were recovered.
  faultsim_test_result {0 8}

  # Run the integrity_check to make sure nothing strange has occurred.

  set ic [db eval { PRAGMA integrity_check }]
  if {$ic != "ok"} { error "Integrity check: $ic" }
}

#--------------------------------------------------------------------------
# Test fault injection while writing and checkpointing a small WAL file.
#
................................................................................
#   set ::shmfault_ioerr_methods(xShmOpen) 1
# 
# Faults are not injected into xShmRelease, xShmClose or xShmLock method 
# calls. The global tcl variables used are:
#
#   $::shmfault_ioerr_countdown
#   $::shmfault_ioerr_persist
#   $::shmfault_ioerr_methods
#
proc shmfault_vfs_cb {method args} {

  # If ::shmfault_ioerr_countdown is not set, always return SQLITE_OK.
  #
  if {[info exists ::shmfault_ioerr_countdown]==0} { return SQLITE_OK }

  if {[info exists ::shmfault_ioerr_methods($method)]} {
    incr ::shmfault_ioerr_countdown -1
    if { ($::shmfault_ioerr_countdown==0)
      || ($::shmfault_ioerr_countdown<=0 && $::shmfault_ioerr_persist)
    } {
      return SQLITE_IOERR
    }
  }
  return SQLITE_OK
}

# Options are:
#
#   -tclprep TCL
................................................................................
  set A(-sqlbody) ""
  set A(-methods) [list xShmGet xShmOpen xShmSize]
  set A(-coverageonly) 0
  array set A $args

  # Create a VFS to use:
  testvfs shmfault shmfault_vfs_cb

  unset -nocomplain ::shmfault_ioerr_methods
  foreach m $A(-methods) { set ::shmfault_ioerr_methods($m) 1 }
  
  foreach mode {transient persistent} {
    set ::shmfault_ioerr_persist [expr {$mode == "persistent"}]

    for {set nDelay 1} {$nDelay < 10000} {incr nDelay} {
  
      file delete -force test.db test.db-wal test.db-journal
................................................................................
    INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 8192 */
    INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 16384 */
  COMMIT;
}

do_shmfault_test walfault-shm-3 -methods xShmSize -tclprep {
  sqlite3 db test.db -vfs shmfault
  unset -nocomplain ::shmfault_ioerr_countdown
  db eval {
    PRAGMA page_size = 512;
    PRAGMA journal_mode = WAL;
    PRAGMA wal_autocheckpoint = 0;
    CREATE TABLE t1(x);
    BEGIN;
      INSERT INTO t1 VALUES(randomblob(400));           /* 1 */
................................................................................
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 4096 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 8192 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 16384 */
    COMMIT;
  }

  set ::shmfault_ioerr_countdown 1
  set ::shmfault_ioerr_methods(xShmGet) 1
  db close
  unset ::shmfault_ioerr_methods(xShmGet)
  if {[file exists test.db-wal]==0} {error "Failed to create WAL file!"}

  sqlite3 db test.db -vfs shmfault
} -sqlbody {
  SELECT count(*) FROM t1;
}

................................................................................
      INSERT INTO t1 VALUES(randomblob(400));           /* 1 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 2 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 4 */
    COMMIT;
  }

  set ::shmfault_ioerr_countdown 1
  set ::shmfault_ioerr_methods(xShmGet) 1
  db close
  unset ::shmfault_ioerr_methods(xShmGet)
  if {[file exists test.db-wal]==0} {error "Failed to create WAL file!"}
  sqlite3 db test.db -vfs shmfault
} -sqlbody {
  SELECT count(*) FROM t1;
}

do_shmfault_test walfault-shm-5.1 -coverageonly 1 -sqlprep {







<



>







 







<







<
|
|
|
|
|
<







 







|
<
<







 







|







 







|

|







 







|

|







88
89
90
91
92
93
94

95
96
97
98
99
100
101
102
103
104
105
...
134
135
136
137
138
139
140

141
142
143
144
145
146
147

148
149
150
151
152

153
154
155
156
157
158
159
...
167
168
169
170
171
172
173
174


175
176
177
178
179
180
181
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
} {8}
do_faultsim_test walfault-2 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { SELECT count(*) FROM x }
} -test {


  faultsim_test_result {0 8}

  # Run the integrity_check to make sure nothing strange has occurred.
  #
  set ic [db eval { PRAGMA integrity_check }]
  if {$ic != "ok"} { error "Integrity check: $ic" }
}

#--------------------------------------------------------------------------
# Test fault injection while writing and checkpointing a small WAL file.
#
................................................................................
#   set ::shmfault_ioerr_methods(xShmOpen) 1
# 
# Faults are not injected into xShmRelease, xShmClose or xShmLock method 
# calls. The global tcl variables used are:
#
#   $::shmfault_ioerr_countdown
#   $::shmfault_ioerr_persist

#
proc shmfault_vfs_cb {method args} {

  # If ::shmfault_ioerr_countdown is not set, always return SQLITE_OK.
  #
  if {[info exists ::shmfault_ioerr_countdown]==0} { return SQLITE_OK }


  incr ::shmfault_ioerr_countdown -1
  if { ($::shmfault_ioerr_countdown==0)
    || ($::shmfault_ioerr_countdown<=0 && $::shmfault_ioerr_persist)
  } {
    return SQLITE_IOERR

  }
  return SQLITE_OK
}

# Options are:
#
#   -tclprep TCL
................................................................................
  set A(-sqlbody) ""
  set A(-methods) [list xShmGet xShmOpen xShmSize]
  set A(-coverageonly) 0
  array set A $args

  # Create a VFS to use:
  testvfs shmfault shmfault_vfs_cb
  shmfault filter $A(-methods)


  
  foreach mode {transient persistent} {
    set ::shmfault_ioerr_persist [expr {$mode == "persistent"}]

    for {set nDelay 1} {$nDelay < 10000} {incr nDelay} {
  
      file delete -force test.db test.db-wal test.db-journal
................................................................................
    INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 8192 */
    INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 16384 */
  COMMIT;
}

do_shmfault_test walfault-shm-3 -methods xShmSize -tclprep {
  sqlite3 db test.db -vfs shmfault
  shmfault filter {}
  db eval {
    PRAGMA page_size = 512;
    PRAGMA journal_mode = WAL;
    PRAGMA wal_autocheckpoint = 0;
    CREATE TABLE t1(x);
    BEGIN;
      INSERT INTO t1 VALUES(randomblob(400));           /* 1 */
................................................................................
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 4096 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 8192 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 16384 */
    COMMIT;
  }

  set ::shmfault_ioerr_countdown 1
  shmfault filter xShmGet
  db close
  shmfault filter {}
  if {[file exists test.db-wal]==0} {error "Failed to create WAL file!"}

  sqlite3 db test.db -vfs shmfault
} -sqlbody {
  SELECT count(*) FROM t1;
}

................................................................................
      INSERT INTO t1 VALUES(randomblob(400));           /* 1 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 2 */
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    /* 4 */
    COMMIT;
  }

  set ::shmfault_ioerr_countdown 1
  shmfault filter xShmGet
  db close
  shmfault filter {}
  if {[file exists test.db-wal]==0} {error "Failed to create WAL file!"}
  sqlite3 db test.db -vfs shmfault
} -sqlbody {
  SELECT count(*) FROM t1;
}

do_shmfault_test walfault-shm-5.1 -coverageonly 1 -sqlprep {