sl@0: # 2005 December 30 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # The focus of the tests in this file are IO errors that occur in a shared sl@0: # cache context. What happens to connection B if one connection A encounters sl@0: # an IO-error whilst reading or writing the file-system? sl@0: # sl@0: # $Id: shared_err.test,v 1.23 2008/09/29 14:12:57 danielk1977 Exp $ sl@0: sl@0: proc skip {args} {} sl@0: sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: source $testdir/malloc_common.tcl sl@0: db close sl@0: sl@0: ifcapable !shared_cache||!subquery { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: set ::enable_shared_cache [sqlite3_enable_shared_cache 1] sl@0: sl@0: do_ioerr_test shared_ioerr-1 -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: CREATE TABLE t1(a,b,c); sl@0: BEGIN; sl@0: SELECT * FROM sqlite_master; sl@0: } db2 sl@0: } -sqlbody { sl@0: SELECT * FROM sqlite_master; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN TRANSACTION; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: INSERT INTO t1 VALUES(4,5,6); sl@0: ROLLBACK; sl@0: SELECT * FROM t1; sl@0: BEGIN TRANSACTION; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: INSERT INTO t1 VALUES(4,5,6); sl@0: COMMIT; sl@0: SELECT * FROM t1; sl@0: DELETE FROM t1 WHERE a<100; sl@0: } -cleanup { sl@0: do_test shared_ioerr-1.$n.cleanup.1 { sl@0: set res [catchsql { sl@0: SELECT * FROM t1; sl@0: } db2] sl@0: set possible_results [list \ sl@0: "1 {disk I/O error}" \ sl@0: "0 {1 2 3}" \ sl@0: "0 {1 2 3 1 2 3 4 5 6}" \ sl@0: "0 {1 2 3 1 2 3 4 5 6 1 2 3 4 5 6}" \ sl@0: "0 {}" \ sl@0: "1 {database disk image is malformed}" \ sl@0: ] sl@0: set rc [expr [lsearch -exact $possible_results $res] >= 0] sl@0: if {$rc != 1} { sl@0: puts "" sl@0: puts "Result: $res" sl@0: } sl@0: set rc sl@0: } {1} sl@0: sl@0: # The "database disk image is malformed" is a special case that can sl@0: # occur if an IO error occurs during a rollback in the {SELECT * FROM t1} sl@0: # statement above. This test is to make sure there is no real database sl@0: # corruption. sl@0: db2 close sl@0: do_test shared_ioerr-1.$n.cleanup.2 { sl@0: execsql {pragma integrity_check} db sl@0: } {ok} sl@0: } sl@0: sl@0: do_ioerr_test shared_ioerr-2 -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b); sl@0: INSERT INTO t1(oid) VALUES(NULL); sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: INSERT INTO t1(oid) SELECT NULL FROM t1; sl@0: UPDATE t1 set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789'; sl@0: CREATE INDEX i1 ON t1(a); sl@0: COMMIT; sl@0: BEGIN; sl@0: SELECT * FROM sqlite_master; sl@0: } db2 sl@0: } -tclbody { sl@0: set ::residx 0 sl@0: execsql {DELETE FROM t1 WHERE 0 = (a % 2);} sl@0: incr ::residx sl@0: sl@0: # When this transaction begins the table contains 512 entries. The sl@0: # two statements together add 512+146 more if it succeeds. sl@0: # (1024/7==146) sl@0: execsql {BEGIN;} sl@0: execsql {INSERT INTO t1 SELECT a+1, b FROM t1;} sl@0: execsql {INSERT INTO t1 SELECT 'string' || a, b FROM t1 WHERE 0 = (a%7);} sl@0: execsql {COMMIT;} sl@0: sl@0: incr ::residx sl@0: } -cleanup { sl@0: catchsql ROLLBACK sl@0: do_test shared_ioerr-2.$n.cleanup.1 { sl@0: set res [catchsql { sl@0: SELECT max(a), min(a), count(*) FROM (SELECT a FROM t1 order by a); sl@0: } db2] sl@0: set possible_results [list \ sl@0: {0 {1024 1 1024}} \ sl@0: {0 {1023 1 512}} \ sl@0: {0 {string994 1 1170}} \ sl@0: ] sl@0: set idx [lsearch -exact $possible_results $res] sl@0: set success [expr {$idx==$::residx || $res=="1 {disk I/O error}"}] sl@0: if {!$success} { sl@0: puts "" sl@0: puts "Result: \"$res\" ($::residx)" sl@0: } sl@0: set success sl@0: } {1} sl@0: db2 close sl@0: } sl@0: sl@0: # This test is designed to provoke an IO error when a cursor position is sl@0: # "saved" (because another cursor is going to modify the underlying table). sl@0: # sl@0: do_ioerr_test shared_ioerr-3 -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, UNIQUE(a, b)); sl@0: } db2 sl@0: for {set i 0} {$i < 200} {incr i} { sl@0: set a [string range [string repeat "[format %03d $i]." 5] 0 end-1] sl@0: sl@0: set b [string repeat $i 2000] sl@0: execsql {INSERT INTO t1 VALUES($a, $b)} db2 sl@0: } sl@0: execsql {COMMIT} db2 sl@0: set ::DB2 [sqlite3_connection_pointer db2] sl@0: set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] sl@0: sqlite3_step $::STMT ;# Cursor points at 000.000.000.000 sl@0: sqlite3_step $::STMT ;# Cursor points at 001.001.001.001 sl@0: sl@0: } -tclbody { sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES('201.201.201.201.201', NULL); sl@0: UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%'; sl@0: COMMIT; sl@0: } sl@0: } -cleanup { sl@0: set ::steprc [sqlite3_step $::STMT] sl@0: set ::column [sqlite3_column_text $::STMT 0] sl@0: set ::finalrc [sqlite3_finalize $::STMT] sl@0: sl@0: # There are three possible outcomes here (assuming persistent IO errors): sl@0: # sl@0: # 1. If the [sqlite3_step] did not require any IO (required pages in sl@0: # the cache), then the next row ("002...") may be retrieved sl@0: # successfully. sl@0: # sl@0: # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns sl@0: # SQLITE_ERROR and [sqlite3_finalize] returns IOERR. sl@0: # sl@0: # 3. If, after the initial IO error, SQLite tried to rollback the sl@0: # active transaction and a second IO error was encountered, then sl@0: # statement $::STMT will have been aborted. This means [sqlite3_stmt] sl@0: # returns SQLITE_ABORT, and the statement cursor does not move. i.e. sl@0: # [sqlite3_column] still returns the current row ("001...") and sl@0: # [sqlite3_finalize] returns SQLITE_OK. sl@0: # sl@0: sl@0: do_test shared_ioerr-3.$n.cleanup.1 { sl@0: expr { sl@0: $::steprc eq "SQLITE_ROW" || sl@0: $::steprc eq "SQLITE_ERROR" || sl@0: $::steprc eq "SQLITE_ABORT" sl@0: } sl@0: } {1} sl@0: do_test shared_ioerr-3.$n.cleanup.2 { sl@0: expr { sl@0: ($::steprc eq "SQLITE_ROW" && $::column eq "002.002.002.002.002") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::column eq "") || sl@0: ($::steprc eq "SQLITE_ABORT" && $::column eq "001.001.001.001.001") sl@0: } sl@0: } {1} sl@0: do_test shared_ioerr-3.$n.cleanup.3 { sl@0: expr { sl@0: ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT") sl@0: } sl@0: } {1} sl@0: sl@0: # db2 eval {select * from sqlite_master} sl@0: db2 close sl@0: } sl@0: sl@0: # This is a repeat of the previous test except that this time we sl@0: # are doing a reverse-order scan of the table when the cursor is sl@0: # "saved". sl@0: # sl@0: do_ioerr_test shared_ioerr-3rev -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, UNIQUE(a, b)); sl@0: } db2 sl@0: for {set i 0} {$i < 200} {incr i} { sl@0: set a [string range [string repeat "[format %03d $i]." 5] 0 end-1] sl@0: sl@0: set b [string repeat $i 2000] sl@0: execsql {INSERT INTO t1 VALUES($a, $b)} db2 sl@0: } sl@0: execsql {COMMIT} db2 sl@0: set ::DB2 [sqlite3_connection_pointer db2] sl@0: set ::STMT [sqlite3_prepare $::DB2 \ sl@0: "SELECT a FROM t1 ORDER BY a DESC" -1 DUMMY] sl@0: sqlite3_step $::STMT ;# Cursor points at 199.199.199.199.199 sl@0: sqlite3_step $::STMT ;# Cursor points at 198.198.198.198.198 sl@0: sl@0: } -tclbody { sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES('201.201.201.201.201', NULL); sl@0: UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%'; sl@0: COMMIT; sl@0: } sl@0: } -cleanup { sl@0: set ::steprc [sqlite3_step $::STMT] sl@0: set ::column [sqlite3_column_text $::STMT 0] sl@0: set ::finalrc [sqlite3_finalize $::STMT] sl@0: sl@0: # There are three possible outcomes here (assuming persistent IO errors): sl@0: # sl@0: # 1. If the [sqlite3_step] did not require any IO (required pages in sl@0: # the cache), then the next row ("002...") may be retrieved sl@0: # successfully. sl@0: # sl@0: # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns sl@0: # SQLITE_ERROR and [sqlite3_finalize] returns IOERR. sl@0: # sl@0: # 3. If, after the initial IO error, SQLite tried to rollback the sl@0: # active transaction and a second IO error was encountered, then sl@0: # statement $::STMT will have been aborted. This means [sqlite3_stmt] sl@0: # returns SQLITE_ABORT, and the statement cursor does not move. i.e. sl@0: # [sqlite3_column] still returns the current row ("001...") and sl@0: # [sqlite3_finalize] returns SQLITE_OK. sl@0: # sl@0: sl@0: do_test shared_ioerr-3rev.$n.cleanup.1 { sl@0: expr { sl@0: $::steprc eq "SQLITE_ROW" || sl@0: $::steprc eq "SQLITE_ERROR" || sl@0: $::steprc eq "SQLITE_ABORT" sl@0: } sl@0: } {1} sl@0: do_test shared_ioerr-3rev.$n.cleanup.2 { sl@0: expr { sl@0: ($::steprc eq "SQLITE_ROW" && $::column eq "197.197.197.197.197") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::column eq "") || sl@0: ($::steprc eq "SQLITE_ABORT" && $::column eq "198.198.198.198.198") sl@0: } sl@0: } {1} sl@0: do_test shared_ioerr-3rev.$n.cleanup.3 { sl@0: expr { sl@0: ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") || sl@0: ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT") sl@0: } sl@0: } {1} sl@0: sl@0: # db2 eval {select * from sqlite_master} sl@0: db2 close sl@0: } sl@0: sl@0: # Provoke a malloc() failure when a cursor position is being saved. This sl@0: # only happens with index cursors (because they malloc() space to save the sl@0: # current key value). It does not happen with tables, because an integer sl@0: # key does not require a malloc() to store. sl@0: # sl@0: # The library should return an SQLITE_NOMEM to the caller. The query that sl@0: # owns the cursor (the one for which the position is not saved) should sl@0: # continue unaffected. sl@0: # sl@0: do_malloc_test shared_err-4 -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, UNIQUE(a, b)); sl@0: } db2 sl@0: for {set i 0} {$i < 5} {incr i} { sl@0: set a [string repeat $i 10] sl@0: set b [string repeat $i 2000] sl@0: execsql {INSERT INTO t1 VALUES($a, $b)} db2 sl@0: } sl@0: execsql {COMMIT} db2 sl@0: set ::DB2 [sqlite3_connection_pointer db2] sl@0: set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] sl@0: sqlite3_step $::STMT ;# Cursor points at 0000000000 sl@0: sqlite3_step $::STMT ;# Cursor points at 1111111111 sl@0: } -tclbody { sl@0: execsql { sl@0: INSERT INTO t1 VALUES(6, NULL); sl@0: } sl@0: } -cleanup { sl@0: do_test shared_malloc-4.$::n.cleanup.1 { sl@0: set ::rc [sqlite3_step $::STMT] sl@0: expr {$::rc=="SQLITE_ROW" || $::rc=="SQLITE_ERROR"} sl@0: } {1} sl@0: if {$::rc=="SQLITE_ROW"} { sl@0: do_test shared_malloc-4.$::n.cleanup.2 { sl@0: sqlite3_column_text $::STMT 0 sl@0: } {2222222222} sl@0: } sl@0: do_test shared_malloc-4.$::n.cleanup.3 { sl@0: set rc [sqlite3_finalize $::STMT] sl@0: expr {$rc=="SQLITE_OK" || $rc=="SQLITE_ABORT" || sl@0: $rc=="SQLITE_NOMEM" || $rc=="SQLITE_IOERR"} sl@0: } {1} sl@0: # db2 eval {select * from sqlite_master} sl@0: db2 close sl@0: } sl@0: sl@0: do_malloc_test shared_err-5 -tclbody { sl@0: db close sl@0: sqlite3 dbX test.db sl@0: sqlite3 dbY test.db sl@0: dbX close sl@0: dbY close sl@0: } -cleanup { sl@0: catch {dbX close} sl@0: catch {dbY close} sl@0: } sl@0: sl@0: do_malloc_test shared_err-6 -tclbody { sl@0: catch {db close} sl@0: sqlite3_thread_cleanup sl@0: sqlite3_enable_shared_cache 0 sl@0: } -cleanup { sl@0: sqlite3_enable_shared_cache 1 sl@0: } sl@0: sl@0: # As of 3.5.0, sqlite3_enable_shared_cache can be called at sl@0: # any time and from any thread sl@0: #do_test shared_err-misuse-7.1 { sl@0: # sqlite3 db test.db sl@0: # catch { sl@0: # sqlite3_enable_shared_cache 0 sl@0: # } msg sl@0: # set msg sl@0: #} {library routine called out of sequence} sl@0: sl@0: # Again provoke a malloc() failure when a cursor position is being saved, sl@0: # this time during a ROLLBACK operation by some other handle. sl@0: # sl@0: # The library should return an SQLITE_NOMEM to the caller. The query that sl@0: # owns the cursor (the one for which the position is not saved) should sl@0: # be aborted. sl@0: # sl@0: set ::aborted 0 sl@0: do_malloc_test shared_err-8 -tclprep { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: PRAGMA read_uncommitted = 1; sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, UNIQUE(a, b)); sl@0: } db2 sl@0: for {set i 0} {$i < 2} {incr i} { sl@0: set a [string repeat $i 10] sl@0: set b [string repeat $i 2000] sl@0: execsql {INSERT INTO t1 VALUES($a, $b)} db2 sl@0: } sl@0: execsql {COMMIT} db2 sl@0: set ::DB2 [sqlite3_connection_pointer db2] sl@0: set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] sl@0: sqlite3_step $::STMT ;# Cursor points at 0000000000 sl@0: sqlite3_step $::STMT ;# Cursor points at 1111111111 sl@0: } -tclbody { sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(6, NULL); sl@0: ROLLBACK; sl@0: } sl@0: } -cleanup { sl@0: # UPDATE: As of [5668], if the rollback fails SQLITE_CORRUPT is returned. sl@0: # So these tests have been updated to expect SQLITE_CORRUPT and its sl@0: # associated English language error message. sl@0: # sl@0: do_test shared_malloc-8.$::n.cleanup.1 { sl@0: set res [catchsql {SELECT a FROM t1} db2] sl@0: set ans [lindex $res 1] sl@0: if {[lindex $res 0]} { sl@0: set r [expr { sl@0: $ans=="disk I/O error" || sl@0: $ans=="out of memory" || sl@0: $ans=="database disk image is malformed" sl@0: }] sl@0: } else { sl@0: set r [expr {[lrange $ans 0 1]=="0000000000 1111111111"}] sl@0: } sl@0: } {1} sl@0: do_test shared_malloc-8.$::n.cleanup.2 { sl@0: set rc1 [sqlite3_step $::STMT] sl@0: set rc2 [sqlite3_finalize $::STMT] sl@0: if {$rc2=="SQLITE_ABORT"} { sl@0: incr ::aborted sl@0: } sl@0: expr { sl@0: ($rc1=="SQLITE_DONE" && $rc2=="SQLITE_OK") || sl@0: ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_ABORT") || sl@0: ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_NOMEM") || sl@0: ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_IOERR") || sl@0: ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_CORRUPT") sl@0: } sl@0: } {1} sl@0: db2 close sl@0: } sl@0: do_test shared_malloc-8.X { sl@0: # Test that one or more queries were aborted due to the malloc() failure. sl@0: expr $::aborted>=1 sl@0: } {1} sl@0: sl@0: # This test is designed to catch a specific bug that was present during sl@0: # development of 3.5.0. If a malloc() failed while setting the page-size, sl@0: # a buffer (Pager.pTmpSpace) was being freed. This could cause a seg-fault sl@0: # later if another connection tried to use the pager. sl@0: # sl@0: # This test will crash 3.4.2. sl@0: # sl@0: do_malloc_test shared_err-9 -tclprep { sl@0: sqlite3 db2 test.db sl@0: } -sqlbody { sl@0: PRAGMA page_size = 4096; sl@0: PRAGMA page_size = 1024; sl@0: } -cleanup { sl@0: db2 eval { sl@0: CREATE TABLE abc(a, b, c); sl@0: BEGIN; sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: ROLLBACK; sl@0: } sl@0: db2 close sl@0: } sl@0: sl@0: catch {db close} sl@0: catch {db2 close} sl@0: do_malloc_test shared_err-10 -tclprep { sl@0: sqlite3 db test.db sl@0: sqlite3 db2 test.db sl@0: sl@0: db eval { SELECT * FROM sqlite_master } sl@0: db2 eval { sl@0: BEGIN; sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: } -tclbody { sl@0: catch {db eval {SELECT * FROM sqlite_master}} sl@0: error 1 sl@0: } -cleanup { sl@0: execsql { SELECT * FROM sqlite_master } sl@0: } sl@0: sl@0: do_malloc_test shared_err-11 -tclprep { sl@0: sqlite3 db test.db sl@0: sqlite3 db2 test.db sl@0: sl@0: db eval { SELECT * FROM sqlite_master } sl@0: db2 eval { sl@0: BEGIN; sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: } -tclbody { sl@0: catch {db eval {SELECT * FROM sqlite_master}} sl@0: catch {sqlite3_errmsg16 db} sl@0: error 1 sl@0: } -cleanup { sl@0: execsql { SELECT * FROM sqlite_master } sl@0: } sl@0: sl@0: catch {db close} sl@0: catch {db2 close} sl@0: sl@0: do_malloc_test shared_err-12 -sqlbody { sl@0: CREATE TABLE abc(a, b, c); sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: } sl@0: sl@0: catch {db close} sl@0: catch {db2 close} sl@0: sqlite3_enable_shared_cache $::enable_shared_cache sl@0: finish_test