sl@0: # 2005 November 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: # This file contains tests to ensure that the library handles malloc() failures sl@0: # correctly. The emphasis of these tests are the _prepare(), _step() and sl@0: # _finalize() calls. sl@0: # sl@0: # $Id: malloc3.test,v 1.23 2008/05/13 19:41:54 shane Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: source $testdir/malloc_common.tcl sl@0: sl@0: # Only run these tests if memory debugging is turned on. sl@0: # sl@0: if {!$MEMDEBUG} { sl@0: puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..." sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # NOTES ON RECOVERING FROM A MALLOC FAILURE sl@0: # sl@0: # The tests in this file test the behaviours described in the following sl@0: # paragraphs. These tests test the behaviour of the system when malloc() fails sl@0: # inside of a call to _prepare(), _step(), _finalize() or _reset(). The sl@0: # handling of malloc() failures within ancillary procedures is tested sl@0: # elsewhere. sl@0: # sl@0: # Overview: sl@0: # sl@0: # Executing a statement is done in three stages (prepare, step and finalize). A sl@0: # malloc() failure may occur within any stage. If a memory allocation fails sl@0: # during statement preparation, no statement handle is returned. From the users sl@0: # point of view the system state is as if _prepare() had never been called. sl@0: # sl@0: # If the memory allocation fails during the _step() or _finalize() calls, then sl@0: # the database may be left in one of two states (after finalize() has been sl@0: # called): sl@0: # sl@0: # * As if the neither _step() nor _finalize() had ever been called on sl@0: # the statement handle (i.e. any changes made by the statement are sl@0: # rolled back). sl@0: # * The current transaction may be rolled back. In this case a hot-journal sl@0: # may or may not actually be present in the filesystem. sl@0: # sl@0: # The caller can tell the difference between these two scenarios by invoking sl@0: # _get_autocommit(). sl@0: # sl@0: # sl@0: # Handling of sqlite3_reset(): sl@0: # sl@0: # If a malloc() fails while executing an sqlite3_reset() call, this is handled sl@0: # in the same way as a failure within _finalize(). The statement handle sl@0: # is not deleted and must be passed to _finalize() for resource deallocation. sl@0: # Attempting to _step() or _reset() the statement after a failed _reset() will sl@0: # always return SQLITE_NOMEM. sl@0: # sl@0: # sl@0: # Other active SQL statements: sl@0: # sl@0: # The effect of a malloc failure on concurrently executing SQL statements, sl@0: # particularly when the statement is executing with READ_UNCOMMITTED set and sl@0: # the malloc() failure mandates statement rollback only. Currently, if sl@0: # transaction rollback is required, all other vdbe's are aborted. sl@0: # sl@0: # Non-transient mallocs in btree.c: sl@0: # * The Btree structure itself sl@0: # * Each BtCursor structure sl@0: # sl@0: # Mallocs in pager.c: sl@0: # readMasterJournal() - Space to read the master journal name sl@0: # pager_delmaster() - Space for the entire master journal file sl@0: # sl@0: # sqlite3pager_open() - The pager structure itself sl@0: # sqlite3_pagerget() - Space for a new page sl@0: # pager_open_journal() - Pager.aInJournal[] bitmap sl@0: # sqlite3pager_write() - For in-memory databases only: history page and sl@0: # statement history page. sl@0: # pager_stmt_begin() - Pager.aInStmt[] bitmap sl@0: # sl@0: # None of the above are a huge problem. The most troublesome failures are the sl@0: # transient malloc() calls in btree.c, which can occur during the tree-balance sl@0: # operation. This means the tree being balanced will be internally inconsistent sl@0: # after the malloc() fails. To avoid the corrupt tree being read by a sl@0: # READ_UNCOMMITTED query, we have to make sure the transaction or statement sl@0: # rollback occurs before sqlite3_step() returns, not during a subsequent sl@0: # sqlite3_finalize(). sl@0: #-------------------------------------------------------------------------- sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # NOTES ON TEST IMPLEMENTATION sl@0: # sl@0: # The tests in this file are implemented differently from those in other sl@0: # files. Instead, tests are specified using three primitives: SQL, PREP and sl@0: # TEST. Each primitive has a single argument. Primitives are processed in sl@0: # the order they are specified in the file. sl@0: # sl@0: # A TEST primitive specifies a TCL script as its argument. When a TEST sl@0: # directive is encountered the Tcl script is evaluated. Usually, this Tcl sl@0: # script contains one or more calls to [do_test]. sl@0: # sl@0: # A PREP primitive specifies an SQL script as its argument. When a PREP sl@0: # directive is encountered the SQL is evaluated using database connection sl@0: # [db]. sl@0: # sl@0: # The SQL primitives are where the action happens. An SQL primitive must sl@0: # contain a single, valid SQL statement as its argument. When an SQL sl@0: # primitive is encountered, it is evaluated one or more times to test the sl@0: # behaviour of the system when malloc() fails during preparation or sl@0: # execution of said statement. The Nth time the statement is executed, sl@0: # the Nth malloc is said to fail. The statement is executed until it sl@0: # succeeds, i.e. (M+1) times, where M is the number of mallocs() required sl@0: # to prepare and execute the statement. sl@0: # sl@0: # Each time an SQL statement fails, the driver program (see proc [run_test] sl@0: # below) figures out if a transaction has been automatically rolled back. sl@0: # If not, it executes any TEST block immediately proceeding the SQL sl@0: # statement, then reexecutes the SQL statement with the next value of N. sl@0: # sl@0: # If a transaction has been automatically rolled back, then the driver sl@0: # program executes all the SQL specified as part of SQL or PREP primitives sl@0: # between the current SQL statement and the most recent "BEGIN". Any sl@0: # TEST block immediately proceeding the SQL statement is evaluated, and sl@0: # then the SQL statement reexecuted with the incremented N value. sl@0: # sl@0: # That make any sense? If not, read the code in [run_test] and it might. sl@0: # sl@0: # Extra restriction imposed by the implementation: sl@0: # sl@0: # * If a PREP block starts a transaction, it must finish it. sl@0: # * A PREP block may not close a transaction it did not start. sl@0: # sl@0: #-------------------------------------------------------------------------- sl@0: sl@0: # These procs are used to build up a "program" in global variable sl@0: # ::run_test_script. At the end of this file, the proc [run_test] is used sl@0: # to execute the program (and all test cases contained therein). sl@0: # sl@0: set ::run_test_script [list] sl@0: proc TEST {id t} {lappend ::run_test_script -test [list $id $t]} sl@0: proc PREP {p} {lappend ::run_test_script -prep [string trim $p]} sl@0: proc DEBUG {s} {lappend ::run_test_script -debug $s} sl@0: sl@0: # SQL -- sl@0: # sl@0: # SQL ?-norollback? sl@0: # sl@0: # Add an 'SQL' primitive to the program (see notes above). If the -norollback sl@0: # switch is present, then the statement is not allowed to automatically roll sl@0: # back any active transaction if malloc() fails. It must rollback the statement sl@0: # transaction only. sl@0: # sl@0: proc SQL {a1 {a2 ""}} { sl@0: # An SQL primitive parameter is a list of two elements, a boolean value sl@0: # indicating if the statement may cause transaction rollback when malloc() sl@0: # fails, and the sql statement itself. sl@0: if {$a2 == ""} { sl@0: lappend ::run_test_script -sql [list true [string trim $a1]] sl@0: } else { sl@0: lappend ::run_test_script -sql [list false [string trim $a2]] sl@0: } sl@0: } sl@0: sl@0: # TEST_AUTOCOMMIT -- sl@0: # sl@0: # A shorthand test to see if a transaction is active or not. The first sl@0: # argument - $id - is the integer number of the test case. The second sl@0: # argument is either 1 or 0, the expected value of the auto-commit flag. sl@0: # sl@0: proc TEST_AUTOCOMMIT {id a} { sl@0: TEST $id "do_test \$testid { sqlite3_get_autocommit \$::DB } {$a}" sl@0: } sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # Start of test program declaration sl@0: # sl@0: sl@0: sl@0: # Warm body test. A malloc() fails in the middle of a CREATE TABLE statement sl@0: # in a single-statement transaction on an empty database. Not too much can go sl@0: # wrong here. sl@0: # sl@0: TEST 1 { sl@0: do_test $testid { sl@0: execsql {SELECT tbl_name FROM sqlite_master;} sl@0: } {} sl@0: } sl@0: SQL { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: TEST 2 { sl@0: do_test $testid.1 { sl@0: execsql {SELECT tbl_name FROM sqlite_master;} sl@0: } {abc} sl@0: } sl@0: sl@0: # Insert a couple of rows into the table. each insert is in its own sl@0: # transaction. test that the table is unpopulated before running the inserts sl@0: # (and hence after each failure of the first insert), and that it has been sl@0: # populated correctly after the final insert succeeds. sl@0: # sl@0: TEST 3 { sl@0: do_test $testid.2 { sl@0: execsql {SELECT * FROM abc} sl@0: } {} sl@0: } sl@0: SQL {INSERT INTO abc VALUES(1, 2, 3);} sl@0: SQL {INSERT INTO abc VALUES(4, 5, 6);} sl@0: SQL {INSERT INTO abc VALUES(7, 8, 9);} sl@0: TEST 4 { sl@0: do_test $testid { sl@0: execsql {SELECT * FROM abc} sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: } sl@0: sl@0: # Test a CREATE INDEX statement. Because the table 'abc' is so small, the index sl@0: # will all fit on a single page, so this doesn't test too much that the CREATE sl@0: # TABLE statement didn't test. A few of the transient malloc()s in btree.c sl@0: # perhaps. sl@0: # sl@0: SQL {CREATE INDEX abc_i ON abc(a, b, c);} sl@0: TEST 4 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT * FROM abc ORDER BY a DESC; sl@0: } sl@0: } {7 8 9 4 5 6 1 2 3} sl@0: } sl@0: sl@0: # Test a DELETE statement. Also create a trigger and a view, just to make sure sl@0: # these statements don't have any obvious malloc() related bugs in them. Note sl@0: # that the test above will be executed each time the DELETE fails, so we're sl@0: # also testing rollback of a DELETE from a table with an index on it. sl@0: # sl@0: SQL {DELETE FROM abc WHERE a > 2;} sl@0: SQL {CREATE TRIGGER abc_t AFTER INSERT ON abc BEGIN SELECT 'trigger!'; END;} sl@0: SQL {CREATE VIEW abc_v AS SELECT * FROM abc;} sl@0: TEST 5 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT name, tbl_name FROM sqlite_master ORDER BY name; sl@0: SELECT * FROM abc; sl@0: } sl@0: } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3} sl@0: } sl@0: sl@0: set sql { sl@0: BEGIN;DELETE FROM abc; sl@0: } sl@0: for {set i 1} {$i < 15} {incr i} { sl@0: set a $i sl@0: set b "String value $i" sl@0: set c [string repeat X $i] sl@0: append sql "INSERT INTO abc VALUES ($a, '$b', '$c');" sl@0: } sl@0: append sql {COMMIT;} sl@0: PREP $sql sl@0: sl@0: SQL { sl@0: DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); sl@0: } sl@0: TEST 6 { sl@0: do_test $testid.1 { sl@0: execsql {SELECT count(*) FROM abc} sl@0: } {94} sl@0: do_test $testid.2 { sl@0: execsql { sl@0: SELECT min( sl@0: (oid == a) AND 'String value ' || a == b AND a == length(c) sl@0: ) FROM abc; sl@0: } sl@0: } {1} sl@0: } sl@0: SQL { sl@0: DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); sl@0: } sl@0: TEST 7 { sl@0: do_test $testid { sl@0: execsql {SELECT count(*) FROM abc} sl@0: } {89} sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT min( sl@0: (oid == a) AND 'String value ' || a == b AND a == length(c) sl@0: ) FROM abc; sl@0: } sl@0: } {1} sl@0: } sl@0: SQL { sl@0: DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); sl@0: } sl@0: TEST 9 { sl@0: do_test $testid { sl@0: execsql {SELECT count(*) FROM abc} sl@0: } {84} sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT min( sl@0: (oid == a) AND 'String value ' || a == b AND a == length(c) sl@0: ) FROM abc; sl@0: } sl@0: } {1} sl@0: } sl@0: sl@0: set padding [string repeat X 500] sl@0: PREP [subst { sl@0: DROP TABLE abc; sl@0: CREATE TABLE abc(a PRIMARY KEY, padding, b, c); sl@0: INSERT INTO abc VALUES(0, '$padding', 2, 2); sl@0: INSERT INTO abc VALUES(3, '$padding', 5, 5); sl@0: INSERT INTO abc VALUES(6, '$padding', 8, 8); sl@0: }] sl@0: sl@0: TEST 10 { sl@0: do_test $testid { sl@0: execsql {SELECT a, b, c FROM abc} sl@0: } {0 2 2 3 5 5 6 8 8} sl@0: } sl@0: sl@0: SQL {BEGIN;} sl@0: SQL {INSERT INTO abc VALUES(9, 'XXXXX', 11, 12);} sl@0: TEST_AUTOCOMMIT 11 0 sl@0: SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;} sl@0: TEST_AUTOCOMMIT 12 0 sl@0: SQL {DELETE FROM abc WHERE a = 10;} sl@0: TEST_AUTOCOMMIT 13 0 sl@0: SQL {COMMIT;} sl@0: sl@0: TEST 14 { sl@0: do_test $testid.1 { sl@0: sqlite3_get_autocommit $::DB sl@0: } {1} sl@0: do_test $testid.2 { sl@0: execsql {SELECT a, b, c FROM abc} sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: } sl@0: sl@0: PREP [subst { sl@0: DROP TABLE abc; sl@0: CREATE TABLE abc(a, padding, b, c); sl@0: INSERT INTO abc VALUES(1, '$padding', 2, 3); sl@0: INSERT INTO abc VALUES(4, '$padding', 5, 6); sl@0: INSERT INTO abc VALUES(7, '$padding', 8, 9); sl@0: CREATE INDEX abc_i ON abc(a, padding, b, c); sl@0: }] sl@0: sl@0: TEST 15 { sl@0: db eval {PRAGMA cache_size = 10} sl@0: } sl@0: sl@0: SQL {BEGIN;} sl@0: SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} sl@0: TEST 16 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 2 4 2 7 2} sl@0: } sl@0: SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} sl@0: TEST 17 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 4 4 4 7 4} sl@0: } sl@0: SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} sl@0: TEST 18 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 8 4 8 7 8} sl@0: } sl@0: SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} sl@0: TEST 19 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 16 4 16 7 16} sl@0: } sl@0: SQL {COMMIT;} sl@0: TEST 21 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 16 4 16 7 16} sl@0: } sl@0: sl@0: SQL {BEGIN;} sl@0: SQL {DELETE FROM abc WHERE oid %2} sl@0: TEST 22 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 8 4 8 7 8} sl@0: } sl@0: SQL {DELETE FROM abc} sl@0: TEST 23 { sl@0: do_test $testid { sl@0: execsql {SELECT * FROM abc} sl@0: } {} sl@0: } sl@0: SQL {ROLLBACK;} sl@0: TEST 24 { sl@0: do_test $testid { sl@0: execsql {SELECT a, count(*) FROM abc GROUP BY a;} sl@0: } {1 16 4 16 7 16} sl@0: } sl@0: sl@0: # Test some schema modifications inside of a transaction. These should all sl@0: # cause transaction rollback if they fail. Also query a view, to cover a bit sl@0: # more code. sl@0: # sl@0: PREP {DROP VIEW abc_v;} sl@0: TEST 25 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT name, tbl_name FROM sqlite_master; sl@0: } sl@0: } {abc abc abc_i abc} sl@0: } sl@0: SQL {BEGIN;} sl@0: SQL {CREATE TABLE def(d, e, f);} sl@0: SQL {CREATE TABLE ghi(g, h, i);} sl@0: TEST 26 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT name, tbl_name FROM sqlite_master; sl@0: } sl@0: } {abc abc abc_i abc def def ghi ghi} sl@0: } sl@0: SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi} sl@0: SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);} sl@0: TEST 27 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT name, tbl_name FROM sqlite_master; sl@0: } sl@0: } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi} sl@0: } sl@0: SQL {INSERT INTO def VALUES('a', 'b', 'c')} sl@0: SQL {INSERT INTO def VALUES(1, 2, 3)} sl@0: SQL -norollback {INSERT INTO ghi SELECT * FROM def} sl@0: TEST 28 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT * FROM def, ghi WHERE d = g; sl@0: } sl@0: } {a b c a b c 1 2 3 1 2 3} sl@0: } sl@0: SQL {COMMIT} sl@0: TEST 29 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT * FROM v1 WHERE d = g; sl@0: } sl@0: } {a b c a b c 1 2 3 1 2 3} sl@0: } sl@0: sl@0: # Test a simple multi-file transaction sl@0: # sl@0: file delete -force test2.db sl@0: ifcapable attach { sl@0: SQL {ATTACH 'test2.db' AS aux;} sl@0: SQL {BEGIN} sl@0: SQL {CREATE TABLE aux.tbl2(x, y, z)} sl@0: SQL {INSERT INTO tbl2 VALUES(1, 2, 3)} sl@0: SQL {INSERT INTO def VALUES(4, 5, 6)} sl@0: TEST 30 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT * FROM tbl2, def WHERE d = x; sl@0: } sl@0: } {1 2 3 1 2 3} sl@0: } sl@0: SQL {COMMIT} sl@0: TEST 31 { sl@0: do_test $testid { sl@0: execsql { sl@0: SELECT * FROM tbl2, def WHERE d = x; sl@0: } sl@0: } {1 2 3 1 2 3} sl@0: } sl@0: } sl@0: sl@0: # Test what happens when a malloc() fails while there are other active sl@0: # statements. This changes the way sqlite3VdbeHalt() works. sl@0: TEST 32 { sl@0: if {![info exists ::STMT32]} { sl@0: set sql "SELECT name FROM sqlite_master" sl@0: set ::STMT32 [sqlite3_prepare $::DB $sql -1 DUMMY] sl@0: do_test $testid { sl@0: sqlite3_step $::STMT32 sl@0: } {SQLITE_ROW} sl@0: } sl@0: } sl@0: SQL BEGIN sl@0: TEST 33 { sl@0: do_test $testid { sl@0: execsql {SELECT * FROM ghi} sl@0: } {a b c 1 2 3} sl@0: } sl@0: SQL -norollback { sl@0: -- There is a unique index on ghi(g), so this statement may not cause sl@0: -- an automatic ROLLBACK. Hence the "-norollback" switch. sl@0: INSERT INTO ghi SELECT '2'||g, h, i FROM ghi; sl@0: } sl@0: TEST 34 { sl@0: if {[info exists ::STMT32]} { sl@0: do_test $testid { sl@0: sqlite3_finalize $::STMT32 sl@0: } {SQLITE_OK} sl@0: unset ::STMT32 sl@0: } sl@0: } sl@0: SQL COMMIT sl@0: sl@0: # sl@0: # End of test program declaration sl@0: #-------------------------------------------------------------------------- sl@0: sl@0: proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} { sl@0: if {[llength $arglist] %2} { sl@0: error "Uneven number of arguments to TEST" sl@0: } sl@0: sl@0: for {set i 0} {$i < $pcstart} {incr i} { sl@0: set k2 [lindex $arglist [expr 2 * $i]] sl@0: set v2 [lindex $arglist [expr 2 * $i + 1]] sl@0: set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit sl@0: switch -- $k2 { sl@0: -sql {db eval [lindex $v2 1]} sl@0: -prep {db eval $v2} sl@0: } sl@0: set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit sl@0: if {$ac && !$nac} {set begin_pc $i} sl@0: } sl@0: sl@0: db rollback_hook [list incr ::rollback_hook_count] sl@0: sl@0: set iFail $iFailStart sl@0: set pc $pcstart sl@0: while {$pc*2 < [llength $arglist]} { sl@0: sl@0: # Id of this iteration: sl@0: set k [lindex $arglist [expr 2 * $pc]] sl@0: set iterid "pc=$pc.iFail=$iFail$k" sl@0: set v [lindex $arglist [expr 2 * $pc + 1]] sl@0: sl@0: puts $iterid sl@0: flush stdout sl@0: sl@0: switch -- $k { sl@0: sl@0: -test { sl@0: foreach {id script} $v {} sl@0: incr pc sl@0: } sl@0: sl@0: -sql { sl@0: set ::rollback_hook_count 0 sl@0: sl@0: set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit sl@0: sqlite3_memdebug_fail $iFail -repeat 0 sl@0: set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs sl@0: set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit sl@0: sl@0: if {$rc != 0 && $nac && !$ac} { sl@0: # Before [db eval] the auto-commit flag was clear. Now it sl@0: # is set. Since an error occured we assume this was not a sl@0: # commit - therefore a rollback occured. Check that the sl@0: # rollback-hook was invoked. sl@0: do_test malloc3-rollback_hook.$iterid { sl@0: set ::rollback_hook_count sl@0: } {1} sl@0: } sl@0: sl@0: set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] sl@0: if {$rc == 0} { sl@0: # Successful execution of sql. The number of failed malloc() sl@0: # calls should be equal to the number of benign failures. sl@0: # Otherwise a malloc() failed and the error was not reported. sl@0: # sl@0: if {$nFail!=$nBenign} { sl@0: error "Unreported malloc() failure" sl@0: } sl@0: sl@0: if {$ac && !$nac} { sl@0: # Before the [db eval] the auto-commit flag was set, now it sl@0: # is clear. We can deduce that a "BEGIN" statement has just sl@0: # been successfully executed. sl@0: set begin_pc $pc sl@0: } sl@0: sl@0: incr pc sl@0: set iFail 1 sl@0: integrity_check "malloc3-(integrity).$iterid" sl@0: } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} { sl@0: # Out of memory error, as expected. sl@0: # sl@0: integrity_check "malloc3-(integrity).$iterid" sl@0: incr iFail sl@0: if {$nac && !$ac} { sl@0: sl@0: if {![lindex $v 0] && [db errorcode] != 3082} { sl@0: # error "Statement \"[lindex $v 1]\" caused a rollback" sl@0: } sl@0: sl@0: for {set i $begin_pc} {$i < $pc} {incr i} { sl@0: set k2 [lindex $arglist [expr 2 * $i]] sl@0: set v2 [lindex $arglist [expr 2 * $i + 1]] sl@0: set catchupsql "" sl@0: switch -- $k2 { sl@0: -sql {set catchupsql [lindex $v2 1]} sl@0: -prep {set catchupsql $v2} sl@0: } sl@0: db eval $catchupsql sl@0: } sl@0: } sl@0: } else { sl@0: error $msg sl@0: } sl@0: sl@0: while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} { sl@0: incr pc -1 sl@0: } sl@0: } sl@0: sl@0: -prep { sl@0: db eval $v sl@0: incr pc sl@0: } sl@0: sl@0: -debug { sl@0: eval $v sl@0: incr pc sl@0: } sl@0: sl@0: default { error "Unknown switch: $k" } sl@0: } sl@0: } sl@0: } sl@0: sl@0: # Turn of the Tcl interface's prepared statement caching facility. Then sl@0: # run the tests with "persistent" malloc failures. sl@0: sqlite3_extended_result_codes db 1 sl@0: db cache size 0 sl@0: run_test $::run_test_script 1 sl@0: sl@0: # Close and reopen the db. sl@0: db close sl@0: file delete -force test.db test.db-journal test2.db test2.db-journal sl@0: sqlite3 db test.db sl@0: sqlite3_extended_result_codes db 1 sl@0: set ::DB [sqlite3_connection_pointer db] sl@0: sl@0: # Turn of the Tcl interface's prepared statement caching facility in sl@0: # the new connnection. Then run the tests with "transient" malloc failures. sl@0: db cache size 0 sl@0: run_test $::run_test_script 0 sl@0: sl@0: sqlite3_memdebug_fail -1 sl@0: finish_test