sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2026 October 23 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # |
| 12 | # Tests for race conditions between a connection with a read-only *-shm |
| 13 | # file and a writer. |
| 14 | # |
| 15 | # |
| 16 | |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | set testprefix walroshm |
| 21 | |
| 22 | ifcapable !wal {finish_test ; return } |
| 23 | |
| 24 | |
| 25 | #------------------------------------------------------------------------- |
| 26 | # Test that a connection with a read-only *-shm file does not return |
| 27 | # SQLITE_READONLY_CANTINIT if a writer appends to the wal file between the |
| 28 | # time the reader reads the wal-index header and the time it scans the |
| 29 | # aReadMark[] array in walTryBeginRead(). This test runs for all builds, |
| 30 | # but because it depends on intercepting an xFileControl(LOCK_TIMEOUT) |
| 31 | # call it is only effective in SQLITE_ENABLE_SETLK_TIMEOUT builds. |
| 32 | # |
| 33 | |
| 34 | # While $::ro is true, all xShmMap calls return SQLITE_READONLY, which |
| 35 | # causes the wal module to set WAL_SHM_RDONLY. |
| 36 | # |
| 37 | # While $::count is true, LOCK_TIMEOUT file-controls are counted in |
| 38 | # $::nCall. When the count reaches $::iInject, connection [db] writes to |
| 39 | # table t2 and then starts a read transaction, which moves aReadMark[1] |
| 40 | # past the snapshot that the reader may have just loaded from the |
| 41 | # wal-index. $::bInjected is set to true if this happens. |
| 42 | # |
| 43 | set ::ro 0 |
| 44 | set ::count 0 |
| 45 | proc tvfs_cb {method file args} { |
| 46 | switch -- $method { |
| 47 | xShmMap { |
| 48 | if {$::ro} { return SQLITE_READONLY } |
| 49 | } |
| 50 | xFileControl { |
| 51 | if {$::count && [lindex $args 0]=="LOCK_TIMEOUT"} { |
| 52 | incr ::nCall |
| 53 | if {$::nCall==$::iInject} { |
| 54 | set ::count 0 |
| 55 | set ::ro 0 |
| 56 | db eval { |
| 57 | INSERT INTO t2 VALUES(2, 200); |
| 58 | SELECT count(*) FROM t2; |
| 59 | } |
| 60 | set ::ro 1 |
| 61 | set ::count 1 |
| 62 | set ::bInjected 1 |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return SQLITE_OK |
| 68 | } |
| 69 | |
| 70 | db close |
| 71 | testvfs tvfs |
| 72 | tvfs filter {xShmMap xFileControl} |
| 73 | tvfs script tvfs_cb |
| 74 | |
| 75 | # Iteration $i injects the write into the $i'th LOCK_TIMEOUT file-control |
| 76 | # made by the SELECT statement run by [db2]. The loop stops after the |
| 77 | # first iteration in which the SELECT makes fewer than $i such calls. |
| 78 | # |
| 79 | for {set i 1} {$i<100} {incr i} { |
| 80 | forcedelete test.db test.db-wal test.db-shm |
| 81 | sqlite3 db test.db -vfs tvfs |
| 82 | do_execsql_test 1.$i.0 { |
| 83 | PRAGMA journal_mode = wal; |
| 84 | CREATE TABLE t1(k INTEGER PRIMARY KEY, v INT); |
| 85 | CREATE TABLE t2(k INTEGER PRIMARY KEY, v INT); |
| 86 | INSERT INTO t1 VALUES(1, 100); |
| 87 | INSERT INTO t2 VALUES(1, 100); |
| 88 | } {wal} |
| 89 | |
| 90 | # Open the read-only connection and load the schema, so that the shm is |
| 91 | # mapped read-only and the next query does not need to reload the schema. |
| 92 | # |
| 93 | set ::ro 1 |
| 94 | sqlite3 db2 test.db -vfs tvfs -readonly 1 |
| 95 | do_execsql_test -db db2 1.$i.1 { |
| 96 | SELECT v FROM t1 WHERE k=1; |
| 97 | } {100} |
| 98 | |
| 99 | set ::iInject $i |
| 100 | set ::nCall 0 |
| 101 | set ::bInjected 0 |
| 102 | set ::count 1 |
| 103 | do_test 1.$i.2 { |
| 104 | catchsql { SELECT v FROM t1 WHERE k=1 } db2 |
| 105 | } {0 100} |
| 106 | set ::count 0 |
| 107 | set ::ro 0 |
| 108 | |
| 109 | db2 close |
| 110 | db close |
| 111 | if {$::bInjected==0} break |
| 112 | } |
| 113 | |
| 114 | tvfs delete |
| 115 | |
| 116 | #------------------------------------------------------------------------- |
| 117 | # Test that the reader does not return SQLITE_READONLY_RECOVERY if a |
| 118 | # writer commits a transaction while the reader is reading the |
| 119 | # wal-index header in walIndexTryHdr(). This depends on intercepting |
| 120 | # xShmBarrier() calls, so it is effective in all builds. |
| 121 | |
| 122 | # While $::ro2 is true, all xShmMap calls return SQLITE_READONLY, which |
| 123 | # causes the wal module to set WAL_SHM_RDONLY. |
| 124 | # |
| 125 | # While $::count2 is true, xShmBarrier calls are counted in $::nCall2. |
| 126 | # When the count reaches $::iInject2, connection [db] commits a write to |
| 127 | # table t2, which modifies the wal-index header while the reader may be |
| 128 | # in the middle of reading it. $::bInjected2 is set to true if this |
| 129 | # happens. |
| 130 | # |
| 131 | set ::ro2 0 |
| 132 | set ::count2 0 |
| 133 | proc tvfs2_cb {method file args} { |
| 134 | switch -- $method { |
| 135 | xShmMap { |
| 136 | if {$::ro2} { return SQLITE_READONLY } |
| 137 | } |
| 138 | xShmBarrier { |
| 139 | if {$::count2} { |
| 140 | incr ::nCall2 |
| 141 | if {$::nCall2==$::iInject2} { |
| 142 | set ::count2 0 |
| 143 | set ::ro2 0 |
| 144 | db eval { INSERT INTO t2 VALUES(2, 200) } |
| 145 | set ::ro2 1 |
| 146 | set ::count2 1 |
| 147 | set ::bInjected2 1 |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return SQLITE_OK |
| 153 | } |
| 154 | |
| 155 | testvfs tvfs2 |
| 156 | tvfs2 filter {xShmMap xShmBarrier} |
| 157 | tvfs2 script tvfs2_cb |
| 158 | |
| 159 | # Iteration $i injects the write into the $i'th xShmBarrier call made by |
| 160 | # the SELECT statement run by [db2]. The loop stops after the first |
| 161 | # iteration in which the SELECT makes fewer than $i such calls. |
| 162 | # |
| 163 | for {set i 1} {$i<100} {incr i} { |
| 164 | forcedelete test.db test.db-wal test.db-shm |
| 165 | sqlite3 db test.db -vfs tvfs2 |
| 166 | do_execsql_test 2.$i.0 { |
| 167 | PRAGMA journal_mode = wal; |
| 168 | CREATE TABLE t1(k INTEGER PRIMARY KEY, v INT); |
| 169 | CREATE TABLE t2(k INTEGER PRIMARY KEY, v INT); |
| 170 | INSERT INTO t1 VALUES(1, 100); |
| 171 | INSERT INTO t2 VALUES(1, 100); |
| 172 | } {wal} |
| 173 | |
| 174 | # Open the read-only connection and load the schema, so that the shm is |
| 175 | # mapped read-only and the next query does not need to reload the schema. |
| 176 | # |
| 177 | set ::ro2 1 |
| 178 | sqlite3 db2 test.db -vfs tvfs2 -readonly 1 |
| 179 | do_execsql_test -db db2 2.$i.1 { |
| 180 | SELECT v FROM t1 WHERE k=1; |
| 181 | } {100} |
| 182 | |
| 183 | set ::iInject2 $i |
| 184 | set ::nCall2 0 |
| 185 | set ::bInjected2 0 |
| 186 | set ::count2 1 |
| 187 | do_test 2.$i.2 { |
| 188 | catchsql { SELECT v FROM t1 WHERE k=1 } db2 |
| 189 | } {0 100} |
| 190 | set ::count2 0 |
| 191 | set ::ro2 0 |
| 192 | |
| 193 | db2 close |
| 194 | db close |
| 195 | if {$::bInjected2==0} break |
| 196 | } |
| 197 | |
| 198 | tvfs2 delete |
| 199 | finish_test |