os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/malloc3.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/malloc3.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,666 @@
     1.4 +# 2005 November 30
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +#
    1.15 +# This file contains tests to ensure that the library handles malloc() failures
    1.16 +# correctly. The emphasis of these tests are the _prepare(), _step() and
    1.17 +# _finalize() calls.
    1.18 +#
    1.19 +# $Id: malloc3.test,v 1.23 2008/05/13 19:41:54 shane Exp $
    1.20 +
    1.21 +set testdir [file dirname $argv0]
    1.22 +source $testdir/tester.tcl
    1.23 +source $testdir/malloc_common.tcl
    1.24 +
    1.25 +# Only run these tests if memory debugging is turned on.
    1.26 +#
    1.27 +if {!$MEMDEBUG} {
    1.28 +   puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..."
    1.29 +   finish_test
    1.30 +   return
    1.31 +}
    1.32 +
    1.33 +#--------------------------------------------------------------------------
    1.34 +# NOTES ON RECOVERING FROM A MALLOC FAILURE
    1.35 +# 
    1.36 +# The tests in this file test the behaviours described in the following
    1.37 +# paragraphs. These tests test the behaviour of the system when malloc() fails
    1.38 +# inside of a call to _prepare(), _step(), _finalize() or _reset(). The
    1.39 +# handling of malloc() failures within ancillary procedures is tested
    1.40 +# elsewhere.
    1.41 +#
    1.42 +# Overview:
    1.43 +#
    1.44 +# Executing a statement is done in three stages (prepare, step and finalize). A
    1.45 +# malloc() failure may occur within any stage. If a memory allocation fails
    1.46 +# during statement preparation, no statement handle is returned. From the users
    1.47 +# point of view the system state is as if _prepare() had never been called.
    1.48 +#
    1.49 +# If the memory allocation fails during the _step() or _finalize() calls, then
    1.50 +# the database may be left in one of two states (after finalize() has been
    1.51 +# called):
    1.52 +#
    1.53 +#     * As if the neither _step() nor _finalize() had ever been called on
    1.54 +#       the statement handle (i.e. any changes made by the statement are
    1.55 +#       rolled back).
    1.56 +#     * The current transaction may be rolled back. In this case a hot-journal
    1.57 +#       may or may not actually be present in the filesystem.
    1.58 +#
    1.59 +# The caller can tell the difference between these two scenarios by invoking
    1.60 +# _get_autocommit().
    1.61 +#
    1.62 +#
    1.63 +# Handling of sqlite3_reset():
    1.64 +#
    1.65 +# If a malloc() fails while executing an sqlite3_reset() call, this is handled
    1.66 +# in the same way as a failure within _finalize(). The statement handle
    1.67 +# is not deleted and must be passed to _finalize() for resource deallocation.
    1.68 +# Attempting to _step() or _reset() the statement after a failed _reset() will
    1.69 +# always return SQLITE_NOMEM.
    1.70 +#
    1.71 +#
    1.72 +# Other active SQL statements:
    1.73 +#
    1.74 +# The effect of a malloc failure on concurrently executing SQL statements,
    1.75 +# particularly when the statement is executing with READ_UNCOMMITTED set and
    1.76 +# the malloc() failure mandates statement rollback only. Currently, if
    1.77 +# transaction rollback is required, all other vdbe's are aborted.
    1.78 +#
    1.79 +#     Non-transient mallocs in btree.c:
    1.80 +#         * The Btree structure itself
    1.81 +#         * Each BtCursor structure
    1.82 +#
    1.83 +#     Mallocs in pager.c:
    1.84 +#         readMasterJournal()  - Space to read the master journal name
    1.85 +#         pager_delmaster()    - Space for the entire master journal file
    1.86 +#
    1.87 +#         sqlite3pager_open()  - The pager structure itself
    1.88 +#         sqlite3_pagerget()   - Space for a new page
    1.89 +#         pager_open_journal() - Pager.aInJournal[] bitmap
    1.90 +#         sqlite3pager_write() - For in-memory databases only: history page and
    1.91 +#                                statement history page.
    1.92 +#         pager_stmt_begin()   - Pager.aInStmt[] bitmap
    1.93 +#
    1.94 +# None of the above are a huge problem. The most troublesome failures are the
    1.95 +# transient malloc() calls in btree.c, which can occur during the tree-balance
    1.96 +# operation. This means the tree being balanced will be internally inconsistent
    1.97 +# after the malloc() fails. To avoid the corrupt tree being read by a
    1.98 +# READ_UNCOMMITTED query, we have to make sure the transaction or statement
    1.99 +# rollback occurs before sqlite3_step() returns, not during a subsequent
   1.100 +# sqlite3_finalize().
   1.101 +#--------------------------------------------------------------------------
   1.102 +
   1.103 +#--------------------------------------------------------------------------
   1.104 +# NOTES ON TEST IMPLEMENTATION
   1.105 +#
   1.106 +# The tests in this file are implemented differently from those in other
   1.107 +# files. Instead, tests are specified using three primitives: SQL, PREP and
   1.108 +# TEST. Each primitive has a single argument. Primitives are processed in
   1.109 +# the order they are specified in the file.
   1.110 +#
   1.111 +# A TEST primitive specifies a TCL script as its argument. When a TEST
   1.112 +# directive is encountered the Tcl script is evaluated. Usually, this Tcl
   1.113 +# script contains one or more calls to [do_test].
   1.114 +#
   1.115 +# A PREP primitive specifies an SQL script as its argument. When a PREP
   1.116 +# directive is encountered the SQL is evaluated using database connection
   1.117 +# [db].
   1.118 +#
   1.119 +# The SQL primitives are where the action happens. An SQL primitive must
   1.120 +# contain a single, valid SQL statement as its argument. When an SQL
   1.121 +# primitive is encountered, it is evaluated one or more times to test the
   1.122 +# behaviour of the system when malloc() fails during preparation or
   1.123 +# execution of said statement. The Nth time the statement is executed,
   1.124 +# the Nth malloc is said to fail. The statement is executed until it
   1.125 +# succeeds, i.e. (M+1) times, where M is the number of mallocs() required
   1.126 +# to prepare and execute the statement.
   1.127 +#
   1.128 +# Each time an SQL statement fails, the driver program (see proc [run_test]
   1.129 +# below) figures out if a transaction has been automatically rolled back.
   1.130 +# If not, it executes any TEST block immediately proceeding the SQL
   1.131 +# statement, then reexecutes the SQL statement with the next value of N.
   1.132 +#
   1.133 +# If a transaction has been automatically rolled back, then the driver
   1.134 +# program executes all the SQL specified as part of SQL or PREP primitives
   1.135 +# between the current SQL statement and the most recent "BEGIN". Any 
   1.136 +# TEST block immediately proceeding the SQL statement is evaluated, and
   1.137 +# then the SQL statement reexecuted with the incremented N value.
   1.138 +#
   1.139 +# That make any sense? If not, read the code in [run_test] and it might.
   1.140 +#
   1.141 +# Extra restriction imposed by the implementation:
   1.142 +#
   1.143 +# * If a PREP block starts a transaction, it must finish it.
   1.144 +# * A PREP block may not close a transaction it did not start.
   1.145 +#
   1.146 +#--------------------------------------------------------------------------
   1.147 +
   1.148 +# These procs are used to build up a "program" in global variable
   1.149 +# ::run_test_script. At the end of this file, the proc [run_test] is used
   1.150 +# to execute the program (and all test cases contained therein).
   1.151 +#
   1.152 +set ::run_test_script [list]
   1.153 +proc TEST {id t} {lappend ::run_test_script -test [list $id $t]}
   1.154 +proc PREP {p} {lappend ::run_test_script -prep [string trim $p]}
   1.155 +proc DEBUG {s} {lappend ::run_test_script -debug $s}
   1.156 +
   1.157 +# SQL --
   1.158 +#
   1.159 +#     SQL ?-norollback? <sql-text>
   1.160 +#
   1.161 +# Add an 'SQL' primitive to the program (see notes above). If the -norollback
   1.162 +# switch is present, then the statement is not allowed to automatically roll
   1.163 +# back any active transaction if malloc() fails. It must rollback the statement
   1.164 +# transaction only.
   1.165 +#
   1.166 +proc SQL  {a1 {a2 ""}} {
   1.167 +  # An SQL primitive parameter is a list of two elements, a boolean value
   1.168 +  # indicating if the statement may cause transaction rollback when malloc()
   1.169 +  # fails, and the sql statement itself.
   1.170 +  if {$a2 == ""} {
   1.171 +    lappend ::run_test_script -sql [list true [string trim $a1]]
   1.172 +  } else {
   1.173 +    lappend ::run_test_script -sql [list false [string trim $a2]]
   1.174 +  }
   1.175 +}
   1.176 +
   1.177 +# TEST_AUTOCOMMIT --
   1.178 +# 
   1.179 +#     A shorthand test to see if a transaction is active or not. The first
   1.180 +#     argument - $id - is the integer number of the test case. The second
   1.181 +#     argument is either 1 or 0, the expected value of the auto-commit flag.
   1.182 +#
   1.183 +proc TEST_AUTOCOMMIT {id a} {
   1.184 +    TEST $id "do_test \$testid { sqlite3_get_autocommit \$::DB } {$a}"
   1.185 +}
   1.186 +
   1.187 +#--------------------------------------------------------------------------
   1.188 +# Start of test program declaration
   1.189 +#
   1.190 +
   1.191 +
   1.192 +# Warm body test. A malloc() fails in the middle of a CREATE TABLE statement
   1.193 +# in a single-statement transaction on an empty database. Not too much can go
   1.194 +# wrong here.
   1.195 +#
   1.196 +TEST 1 {
   1.197 +  do_test $testid {
   1.198 +    execsql {SELECT tbl_name FROM sqlite_master;}
   1.199 +  } {}
   1.200 +}
   1.201 +SQL { 
   1.202 +  CREATE TABLE abc(a, b, c); 
   1.203 +}
   1.204 +TEST 2 {
   1.205 +  do_test $testid.1 {
   1.206 +    execsql {SELECT tbl_name FROM sqlite_master;}
   1.207 +  } {abc}
   1.208 +}
   1.209 +
   1.210 +# Insert a couple of rows into the table. each insert is in its own
   1.211 +# transaction. test that the table is unpopulated before running the inserts
   1.212 +# (and hence after each failure of the first insert), and that it has been
   1.213 +# populated correctly after the final insert succeeds.
   1.214 +#
   1.215 +TEST 3 {
   1.216 +  do_test $testid.2 {
   1.217 +    execsql {SELECT * FROM abc}
   1.218 +  } {}
   1.219 +}
   1.220 +SQL {INSERT INTO abc VALUES(1, 2, 3);}
   1.221 +SQL {INSERT INTO abc VALUES(4, 5, 6);}
   1.222 +SQL {INSERT INTO abc VALUES(7, 8, 9);}
   1.223 +TEST 4 {
   1.224 +  do_test $testid {
   1.225 +    execsql {SELECT * FROM abc}
   1.226 +  } {1 2 3 4 5 6 7 8 9}
   1.227 +}
   1.228 +
   1.229 +# Test a CREATE INDEX statement. Because the table 'abc' is so small, the index
   1.230 +# will all fit on a single page, so this doesn't test too much that the CREATE
   1.231 +# TABLE statement didn't test. A few of the transient malloc()s in btree.c
   1.232 +# perhaps.
   1.233 +#
   1.234 +SQL {CREATE INDEX abc_i ON abc(a, b, c);}
   1.235 +TEST 4 {
   1.236 +  do_test $testid {
   1.237 +    execsql {
   1.238 +      SELECT * FROM abc ORDER BY a DESC;
   1.239 +    }
   1.240 +  } {7 8 9 4 5 6 1 2 3}
   1.241 +}
   1.242 +
   1.243 +# Test a DELETE statement. Also create a trigger and a view, just to make sure
   1.244 +# these statements don't have any obvious malloc() related bugs in them. Note
   1.245 +# that the test above will be executed each time the DELETE fails, so we're
   1.246 +# also testing rollback of a DELETE from a table with an index on it.
   1.247 +#
   1.248 +SQL {DELETE FROM abc WHERE a > 2;}
   1.249 +SQL {CREATE TRIGGER abc_t AFTER INSERT ON abc BEGIN SELECT 'trigger!'; END;}
   1.250 +SQL {CREATE VIEW abc_v AS SELECT * FROM abc;}
   1.251 +TEST 5 {
   1.252 +  do_test $testid {
   1.253 +    execsql {
   1.254 +      SELECT name, tbl_name FROM sqlite_master ORDER BY name;
   1.255 +      SELECT * FROM abc;
   1.256 +    }
   1.257 +  } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3}
   1.258 +}
   1.259 +
   1.260 +set sql {
   1.261 +  BEGIN;DELETE FROM abc;
   1.262 +}
   1.263 +for {set i 1} {$i < 15} {incr i} {
   1.264 +  set a $i
   1.265 +  set b "String value $i"
   1.266 +  set c [string repeat X $i]
   1.267 +  append sql "INSERT INTO abc VALUES ($a, '$b', '$c');"
   1.268 +}
   1.269 +append sql {COMMIT;}
   1.270 +PREP $sql
   1.271 +
   1.272 +SQL {
   1.273 +  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
   1.274 +}
   1.275 +TEST 6 {
   1.276 +  do_test $testid.1 {
   1.277 +    execsql {SELECT count(*) FROM abc}
   1.278 +  } {94}
   1.279 +  do_test $testid.2 {
   1.280 +    execsql {
   1.281 +      SELECT min(
   1.282 +          (oid == a) AND 'String value ' || a == b AND a == length(c) 
   1.283 +      ) FROM abc;
   1.284 +    }
   1.285 +  } {1}
   1.286 +}
   1.287 +SQL {
   1.288 +  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
   1.289 +}
   1.290 +TEST 7 {
   1.291 +  do_test $testid {
   1.292 +    execsql {SELECT count(*) FROM abc}
   1.293 +  } {89}
   1.294 +  do_test $testid {
   1.295 +    execsql {
   1.296 +      SELECT min(
   1.297 +          (oid == a) AND 'String value ' || a == b AND a == length(c) 
   1.298 +      ) FROM abc;
   1.299 +    }
   1.300 +  } {1}
   1.301 +}
   1.302 +SQL {
   1.303 +  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
   1.304 +}
   1.305 +TEST 9 {
   1.306 +  do_test $testid {
   1.307 +    execsql {SELECT count(*) FROM abc}
   1.308 +  } {84}
   1.309 +  do_test $testid {
   1.310 +    execsql {
   1.311 +      SELECT min(
   1.312 +          (oid == a) AND 'String value ' || a == b AND a == length(c) 
   1.313 +      ) FROM abc;
   1.314 +    }
   1.315 +  } {1}
   1.316 +}
   1.317 +
   1.318 +set padding [string repeat X 500]
   1.319 +PREP [subst {
   1.320 +  DROP TABLE abc;
   1.321 +  CREATE TABLE abc(a PRIMARY KEY, padding, b, c);
   1.322 +  INSERT INTO abc VALUES(0, '$padding', 2, 2);
   1.323 +  INSERT INTO abc VALUES(3, '$padding', 5, 5);
   1.324 +  INSERT INTO abc VALUES(6, '$padding', 8, 8);
   1.325 +}]
   1.326 +
   1.327 +TEST 10 {
   1.328 +  do_test $testid {
   1.329 +    execsql {SELECT a, b, c FROM abc}
   1.330 +  } {0 2 2 3 5 5 6 8 8}
   1.331 +}
   1.332 +
   1.333 +SQL {BEGIN;}
   1.334 +SQL {INSERT INTO abc VALUES(9, 'XXXXX', 11, 12);}
   1.335 +TEST_AUTOCOMMIT 11 0
   1.336 +SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;}
   1.337 +TEST_AUTOCOMMIT 12 0
   1.338 +SQL {DELETE FROM abc WHERE a = 10;}
   1.339 +TEST_AUTOCOMMIT 13 0
   1.340 +SQL {COMMIT;}
   1.341 +
   1.342 +TEST 14 {
   1.343 +  do_test $testid.1 {
   1.344 +    sqlite3_get_autocommit $::DB
   1.345 +  } {1}
   1.346 +  do_test $testid.2 {
   1.347 +    execsql {SELECT a, b, c FROM abc}
   1.348 +  } {1 2 3 4 5 6 7 8 9}
   1.349 +}
   1.350 +
   1.351 +PREP [subst {
   1.352 +  DROP TABLE abc;
   1.353 +  CREATE TABLE abc(a, padding, b, c);
   1.354 +  INSERT INTO abc VALUES(1, '$padding', 2, 3);
   1.355 +  INSERT INTO abc VALUES(4, '$padding', 5, 6);
   1.356 +  INSERT INTO abc VALUES(7, '$padding', 8, 9);
   1.357 +  CREATE INDEX abc_i ON abc(a, padding, b, c);
   1.358 +}]
   1.359 +
   1.360 +TEST 15 {
   1.361 +  db eval {PRAGMA cache_size = 10}
   1.362 +}
   1.363 +
   1.364 +SQL {BEGIN;}
   1.365 +SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
   1.366 +TEST 16 {
   1.367 +  do_test $testid {
   1.368 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.369 +  } {1 2 4 2 7 2}
   1.370 +}
   1.371 +SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
   1.372 +TEST 17 {
   1.373 +  do_test $testid {
   1.374 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.375 +  } {1 4 4 4 7 4}
   1.376 +}
   1.377 +SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
   1.378 +TEST 18 {
   1.379 +  do_test $testid {
   1.380 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.381 +  } {1 8 4 8 7 8}
   1.382 +}
   1.383 +SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
   1.384 +TEST 19 {
   1.385 +  do_test $testid {
   1.386 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.387 +  } {1 16 4 16 7 16}
   1.388 +}
   1.389 +SQL {COMMIT;}
   1.390 +TEST 21 {
   1.391 +  do_test $testid {
   1.392 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.393 +  } {1 16 4 16 7 16}
   1.394 +}
   1.395 +
   1.396 +SQL {BEGIN;}
   1.397 +SQL {DELETE FROM abc WHERE oid %2}
   1.398 +TEST 22 {
   1.399 +  do_test $testid {
   1.400 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.401 +  } {1 8 4 8 7 8}
   1.402 +}
   1.403 +SQL {DELETE FROM abc}
   1.404 +TEST 23 {
   1.405 +  do_test $testid {
   1.406 +    execsql {SELECT * FROM abc}
   1.407 +  } {}
   1.408 +}
   1.409 +SQL {ROLLBACK;}
   1.410 +TEST 24 {
   1.411 +  do_test $testid {
   1.412 +    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
   1.413 +  } {1 16 4 16 7 16}
   1.414 +}
   1.415 +
   1.416 +# Test some schema modifications inside of a transaction. These should all
   1.417 +# cause transaction rollback if they fail. Also query a view, to cover a bit
   1.418 +# more code.
   1.419 +#
   1.420 +PREP {DROP VIEW abc_v;}
   1.421 +TEST 25 {
   1.422 +  do_test $testid {
   1.423 +    execsql {
   1.424 +      SELECT name, tbl_name FROM sqlite_master;
   1.425 +    }
   1.426 +  } {abc abc abc_i abc}
   1.427 +}
   1.428 +SQL {BEGIN;}
   1.429 +SQL {CREATE TABLE def(d, e, f);}
   1.430 +SQL {CREATE TABLE ghi(g, h, i);}
   1.431 +TEST 26 {
   1.432 +  do_test $testid {
   1.433 +    execsql {
   1.434 +      SELECT name, tbl_name FROM sqlite_master;
   1.435 +    }
   1.436 +  } {abc abc abc_i abc def def ghi ghi}
   1.437 +}
   1.438 +SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi}
   1.439 +SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);}
   1.440 +TEST 27 {
   1.441 +  do_test $testid {
   1.442 +    execsql {
   1.443 +      SELECT name, tbl_name FROM sqlite_master;
   1.444 +    }
   1.445 +  } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi}
   1.446 +}
   1.447 +SQL {INSERT INTO def VALUES('a', 'b', 'c')}
   1.448 +SQL {INSERT INTO def VALUES(1, 2, 3)}
   1.449 +SQL -norollback {INSERT INTO ghi SELECT * FROM def}
   1.450 +TEST 28 {
   1.451 +  do_test $testid {
   1.452 +    execsql {
   1.453 +      SELECT * FROM def, ghi WHERE d = g;
   1.454 +    }
   1.455 +  } {a b c a b c 1 2 3 1 2 3}
   1.456 +}
   1.457 +SQL {COMMIT}
   1.458 +TEST 29 {
   1.459 +  do_test $testid {
   1.460 +    execsql {
   1.461 +      SELECT * FROM v1 WHERE d = g;
   1.462 +    }
   1.463 +  } {a b c a b c 1 2 3 1 2 3}
   1.464 +}
   1.465 +
   1.466 +# Test a simple multi-file transaction 
   1.467 +#
   1.468 +file delete -force test2.db
   1.469 +ifcapable attach {
   1.470 +  SQL {ATTACH 'test2.db' AS aux;}
   1.471 +  SQL {BEGIN}
   1.472 +  SQL {CREATE TABLE aux.tbl2(x, y, z)}
   1.473 +  SQL {INSERT INTO tbl2 VALUES(1, 2, 3)}
   1.474 +  SQL {INSERT INTO def VALUES(4, 5, 6)}
   1.475 +  TEST 30 {
   1.476 +    do_test $testid {
   1.477 +      execsql {
   1.478 +        SELECT * FROM tbl2, def WHERE d = x;
   1.479 +      }
   1.480 +    } {1 2 3 1 2 3}
   1.481 +  }
   1.482 +  SQL {COMMIT}
   1.483 +  TEST 31 {
   1.484 +    do_test $testid {
   1.485 +      execsql {
   1.486 +        SELECT * FROM tbl2, def WHERE d = x;
   1.487 +      }
   1.488 +    } {1 2 3 1 2 3}
   1.489 +  }
   1.490 +}
   1.491 +
   1.492 +# Test what happens when a malloc() fails while there are other active
   1.493 +# statements. This changes the way sqlite3VdbeHalt() works.
   1.494 +TEST 32 {
   1.495 +  if {![info exists ::STMT32]} {
   1.496 +    set sql "SELECT name FROM sqlite_master"
   1.497 +    set ::STMT32 [sqlite3_prepare $::DB $sql -1 DUMMY]
   1.498 +    do_test $testid {
   1.499 +      sqlite3_step $::STMT32
   1.500 +    } {SQLITE_ROW}
   1.501 +  }
   1.502 +}
   1.503 +SQL BEGIN
   1.504 +TEST 33 { 
   1.505 +  do_test $testid {
   1.506 +    execsql {SELECT * FROM ghi}
   1.507 +  } {a b c 1 2 3}
   1.508 +}
   1.509 +SQL -norollback { 
   1.510 +  -- There is a unique index on ghi(g), so this statement may not cause
   1.511 +  -- an automatic ROLLBACK. Hence the "-norollback" switch.
   1.512 +  INSERT INTO ghi SELECT '2'||g, h, i FROM ghi;
   1.513 +}
   1.514 +TEST 34 {
   1.515 +  if {[info exists ::STMT32]} {
   1.516 +    do_test $testid {
   1.517 +      sqlite3_finalize $::STMT32
   1.518 +    } {SQLITE_OK}
   1.519 +    unset ::STMT32
   1.520 +  }
   1.521 +}
   1.522 +SQL COMMIT
   1.523 +
   1.524 +#
   1.525 +# End of test program declaration
   1.526 +#--------------------------------------------------------------------------
   1.527 +
   1.528 +proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
   1.529 +  if {[llength $arglist] %2} {
   1.530 +    error "Uneven number of arguments to TEST"
   1.531 +  }
   1.532 +
   1.533 +  for {set i 0} {$i < $pcstart} {incr i} {
   1.534 +    set k2 [lindex $arglist [expr 2 * $i]]
   1.535 +    set v2 [lindex $arglist [expr 2 * $i + 1]]
   1.536 +    set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
   1.537 +    switch -- $k2 {
   1.538 +      -sql  {db eval [lindex $v2 1]}
   1.539 +      -prep {db eval $v2}
   1.540 +    }
   1.541 +    set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit 
   1.542 +    if {$ac && !$nac} {set begin_pc $i}
   1.543 +  }
   1.544 +
   1.545 +  db rollback_hook [list incr ::rollback_hook_count]
   1.546 +
   1.547 +  set iFail $iFailStart
   1.548 +  set pc $pcstart
   1.549 +  while {$pc*2 < [llength $arglist]} {
   1.550 +
   1.551 +    # Id of this iteration:
   1.552 +    set k [lindex $arglist [expr 2 * $pc]]
   1.553 +    set iterid "pc=$pc.iFail=$iFail$k"
   1.554 +    set v [lindex $arglist [expr 2 * $pc + 1]]
   1.555 +
   1.556 +    puts $iterid
   1.557 +    flush stdout
   1.558 +
   1.559 +    switch -- $k {
   1.560 +
   1.561 +      -test { 
   1.562 +        foreach {id script} $v {}
   1.563 +        incr pc
   1.564 +      }
   1.565 +
   1.566 +      -sql {
   1.567 +        set ::rollback_hook_count 0
   1.568 +
   1.569 +        set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
   1.570 +        sqlite3_memdebug_fail $iFail -repeat 0
   1.571 +        set rc [catch {db eval [lindex $v 1]} msg]   ;# True error occurs
   1.572 +        set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit 
   1.573 +
   1.574 +        if {$rc != 0 && $nac && !$ac} {
   1.575 +          # Before [db eval] the auto-commit flag was clear. Now it
   1.576 +          # is set. Since an error occured we assume this was not a
   1.577 +          # commit - therefore a rollback occured. Check that the
   1.578 +          # rollback-hook was invoked.
   1.579 +          do_test malloc3-rollback_hook.$iterid {
   1.580 +            set ::rollback_hook_count
   1.581 +          } {1}
   1.582 +        }
   1.583 +
   1.584 +        set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
   1.585 +        if {$rc == 0} {
   1.586 +            # Successful execution of sql. The number of failed malloc()
   1.587 +            # calls should be equal to the number of benign failures.
   1.588 +            # Otherwise a malloc() failed and the error was not reported.
   1.589 +            # 
   1.590 +            if {$nFail!=$nBenign} {
   1.591 +              error "Unreported malloc() failure"
   1.592 +            }
   1.593 +
   1.594 +            if {$ac && !$nac} {
   1.595 +              # Before the [db eval] the auto-commit flag was set, now it
   1.596 +              # is clear. We can deduce that a "BEGIN" statement has just
   1.597 +              # been successfully executed.
   1.598 +              set begin_pc $pc
   1.599 +            } 
   1.600 +
   1.601 +            incr pc
   1.602 +            set iFail 1
   1.603 +            integrity_check "malloc3-(integrity).$iterid"
   1.604 +        } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} {
   1.605 +            # Out of memory error, as expected.
   1.606 +            #
   1.607 +            integrity_check "malloc3-(integrity).$iterid"
   1.608 +            incr iFail
   1.609 +            if {$nac && !$ac} {
   1.610 +
   1.611 +              if {![lindex $v 0] && [db errorcode] != 3082} {
   1.612 +                # error "Statement \"[lindex $v 1]\" caused a rollback"
   1.613 +              }
   1.614 +
   1.615 +              for {set i $begin_pc} {$i < $pc} {incr i} {
   1.616 +                set k2 [lindex $arglist [expr 2 * $i]]
   1.617 +                set v2 [lindex $arglist [expr 2 * $i + 1]]
   1.618 +                set catchupsql ""
   1.619 +                switch -- $k2 {
   1.620 +                  -sql  {set catchupsql [lindex $v2 1]}
   1.621 +                  -prep {set catchupsql $v2}
   1.622 +                }
   1.623 +                db eval $catchupsql
   1.624 +              }
   1.625 +            }
   1.626 +        } else {
   1.627 +            error $msg
   1.628 +        }
   1.629 +
   1.630 +        while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} {
   1.631 +          incr pc -1
   1.632 +        }
   1.633 +      }
   1.634 +
   1.635 +      -prep {
   1.636 +        db eval $v
   1.637 +        incr pc
   1.638 +      }
   1.639 +
   1.640 +      -debug {
   1.641 +        eval $v
   1.642 +        incr pc
   1.643 +      }
   1.644 +
   1.645 +      default { error "Unknown switch: $k" }
   1.646 +    }
   1.647 +  }
   1.648 +}
   1.649 +
   1.650 +# Turn of the Tcl interface's prepared statement caching facility. Then
   1.651 +# run the tests with "persistent" malloc failures.
   1.652 +sqlite3_extended_result_codes db 1
   1.653 +db cache size 0
   1.654 +run_test $::run_test_script 1
   1.655 +
   1.656 +# Close and reopen the db.
   1.657 +db close
   1.658 +file delete -force test.db test.db-journal test2.db test2.db-journal
   1.659 +sqlite3 db test.db
   1.660 +sqlite3_extended_result_codes db 1
   1.661 +set ::DB [sqlite3_connection_pointer db]
   1.662 +
   1.663 +# Turn of the Tcl interface's prepared statement caching facility in
   1.664 +# the new connnection. Then run the tests with "transient" malloc failures.
   1.665 +db cache size 0
   1.666 +run_test $::run_test_script 0
   1.667 +
   1.668 +sqlite3_memdebug_fail -1
   1.669 +finish_test