Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add test file for new malloc() failure handling. (CVS 2801) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
0b82f9623cf25b1cb02f70628c992903 |
User & Date: | danielk1977 2005-12-06 12:57:59.000 |
Context
2005-12-06
| ||
13:19 | Report errors out of sqlite3_open16(). (CVS 2802) (check-in: f5b58163d4 user: drh tags: trunk) | |
12:57 | Add test file for new malloc() failure handling. (CVS 2801) (check-in: 0b82f9623c user: danielk1977 tags: trunk) | |
12:52 | Some elements of the new malloc() failure handling. Not all cases work properly yet. Also, library is not threadsafe if malloc() fails right now. (CVS 2800) (check-in: e1606658f1 user: danielk1977 tags: trunk) | |
Changes
Added test/malloc3.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 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 548 549 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 | # 2005 November 30 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # $Id: malloc3.test,v 1.1 2005/12/06 12:57:59 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Only run these tests if memory debugging is turned on. if {[info command sqlite_malloc_stat]==""} { puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." finish_test return } #-------------------------------------------------------------------------- # NOTES ON RECOVERING FROM A MALLOC FAILURE # # The tests in this file test the behaviours described in the following # paragraphs. These tests test the behaviour of the system when malloc() fails # inside of a call to _prepare(), _step(), _finalize() or _reset(). The # handling of malloc() failures within ancillary procedures is tested # elsewhere. # # Overview: # # Executing a statement is done in three stages (prepare, step and finalize). A # malloc() failure may occur within any stage. If a memory allocation fails # during statement preparation, no statement handle is returned. From the users # point of view the system state is as if _prepare() had never been called. # # If the memory allocation fails during the _step() or _finalize() calls, then # the database may be left in one of two states (after finalize() has been # called): # # * As if the neither _step() nor _finalize() had ever been called on # the statement handle (i.e. any changes made by the statement are # rolled back). # * The current transaction may be rolled back. In this case a hot-journal # may or may not actually be present in the filesystem. # # The caller can tell the difference between these two scenarios by invoking # _get_autocommit(). # # # Handling of sqlite3_reset(): # # If a malloc() fails while executing an sqlite3_reset() call, this is handled # in the same way as a failure within _finalize(). The statement handle # is not deleted and must be passed to _finalize() for resource deallocation. # Attempting to _step() or _reset() the statement after a failed _reset() will # always return SQLITE_NOMEM. # # # Other active SQL statements: # # The effect of a malloc failure on concurrently executing SQL statements, # particularly when the statement is executing with READ_UNCOMMITTED set and # the malloc() failure mandates statement rollback only. Currently, if # transaction rollback is required, all other vdbe's are aborted. # # Non-transient mallocs in btree.c: # * The Btree structure itself # * Each BtCursor structure # # Mallocs in pager.c: # readMasterJournal() - Space to read the master journal name # pager_delmaster() - Space for the entire master journal file # # sqlite3pager_open() - The pager structure itself # sqlite3_pagerget() - Space for a new page # pager_open_journal() - Pager.aInJournal[] bitmap # sqlite3pager_write() - For in-memory databases only: history page and # statement history page. # pager_stmt_begin() - Pager.aInStmt[] bitmap # # None of the above are a huge problem. The most troublesome failures are the # transient malloc() calls in btree.c, which can occur during the tree-balance # operation. This means the tree being balanced will be internally inconsistent # after the malloc() fails. To avoid the corrupt tree being read by a # READ_UNCOMMITTED query, we have to make sure the transaction or statement # rollback occurs before sqlite3_step() returns, not during a subsequent # sqlite3_finalize(). #-------------------------------------------------------------------------- #-------------------------------------------------------------------------- # NOTES ON TEST IMPLEMENTATION # # The tests in this file are implemented differently from those in other # files. Instead, tests are specified using three primitives: SQL, PREP and # TEST. Each primitive has a single argument. Primitives are processed in # the order they are specified in the file. # # A TEST primitive specifies a TCL script as it's argument. When a TEST # directive is encountered the Tcl script is evaluated. Usually, this Tcl # script contains one or more calls to [do_test]. # # A PREP primitive specifies an SQL script as it's argument. When a PREP # directive is encountered the SQL is evaluated using database connection # [db]. # # The SQL primitives are where the action happens. An SQL primitive must # contain a single, valid SQL statement as it's argument. When an SQL # primitive is encountered, it is evaluated one or more times to test the # behaviour of the system when malloc() fails during preparation or # execution of said statement. The Nth time the statement is executed, # the Nth malloc is said to fail. The statement is executed until it # succeeds, i.e. (M+1) times, where M is the number of mallocs() required # to prepare and execute the statement. # # Each time an SQL statement fails, the driver program (see proc [run_test] # below) figures out if a transaction has been automatically rolled back. # If not, it executes any TEST block immediately proceeding the SQL # statement, then reexecutes the SQL statement with the next value of N. # # If a transaction has been automatically rolled back, then the driver # program executes all the SQL specified as part of SQL or PREP primitives # between the current SQL statement and the most recent "BEGIN". Any # TEST block immediately proceeding the SQL statement is evaluated, and # then the SQL statement reexecuted with the incremented N value. # # That make any sense? If not, read the code in [run_test] and it might. # # Extra restriction imposed by the implementation: # # * If a PREP block starts a transaction, it must finish it. # * A PREP block may not close a transaction it did not start. # #-------------------------------------------------------------------------- # These procs are used to build up a "program" in global variable # ::run_test_script. At the end of this file, the proc [run_test] is used # to execute the program (and all test cases contained therein). # proc TEST {id t} {lappend ::run_test_script -test [list $id $t]} proc PREP {p} {lappend ::run_test_script -prep [string trim $p]} # SQL -- # # SQL ?-norollback? <sql-text> # # Add an 'SQL' primitive to the program (see notes above). If the -norollback # switch is present, then the statement is not allowed to automatically roll # back any active transaction if malloc() fails. It must rollback the statement # transaction only. # proc SQL {a1 {a2 ""}} { # An SQL primitive parameter is a list of two elements, a boolean value # indicating if the statement may cause transaction rollback when malloc() # fails, and the sql statement itself. if {$a2 == ""} { lappend ::run_test_script -sql [list true [string trim $a1]] } else { lappend ::run_test_script -sql [list false [string trim $a2]] } } # TEST_AUTOCOMMIT -- # # A shorthand test to see if a transaction is active or not. The first # argument - $id - is the integer number of the test case. The second # argument is either 1 or 0, the expected value of the auto-commit flag. # proc TEST_AUTOCOMMIT {id a} { TEST $id "do_test \$testid { sqlite3_get_autocommit $::DB } {$a}" } #-------------------------------------------------------------------------- # Start of test program declaration # # Warm body test. A malloc() fails in the middle of a CREATE TABLE statement # in a single-statement transaction on an empty database. Not too much can go # wrong here. # TEST 1 { do_test $testid { execsql {SELECT tbl_name FROM sqlite_master;} } {} } SQL { CREATE TABLE abc(a, b, c); } TEST 2 { do_test $testid.1 { execsql {SELECT tbl_name FROM sqlite_master;} } {abc} } # Insert a couple of rows into the table. each insert is in it's own # transaction. test that the table is unpopulated before running the inserts # (and hence after each failure of the first insert), and that it has been # populated correctly after the final insert succeeds. # TEST 3 { do_test $testid.2 { execsql {SELECT * FROM abc} } {} } SQL {INSERT INTO abc VALUES(1, 2, 3);} SQL {INSERT INTO abc VALUES(4, 5, 6);} SQL {INSERT INTO abc VALUES(7, 8, 9);} TEST 4 { do_test $testid { execsql {SELECT * FROM abc} } {1 2 3 4 5 6 7 8 9} } # Test a CREATE INDEX statement. Because the table 'abc' is so small, the index # will all fit on a single page, so this doesn't test too much that the CREATE # TABLE statement didn't test. A few of the transient malloc()s in btree.c # perhaps. # SQL {CREATE INDEX abc_i ON abc(a, b, c);} TEST 4 { do_test $testid { execsql { SELECT * FROM abc ORDER BY a DESC; } } {7 8 9 4 5 6 1 2 3} } # Test a DELETE statement. Also create a trigger and a view, just to make sure # these statements don't have any obvious malloc() related bugs in them. Note # that the test above will be executed each time the DELETE fails, so we're # also testing rollback of a DELETE from a table with an index on it. # SQL {DELETE FROM abc WHERE a > 2;} SQL {CREATE TRIGGER abc_t AFTER INSERT ON abc BEGIN SELECT 'trigger!'; END;} SQL {CREATE VIEW abc_v AS SELECT * FROM abc;} TEST 5 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master ORDER BY name; SELECT * FROM abc; } } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3} } set sql { BEGIN;DELETE FROM abc; } for {set i 1} {$i < 100} {incr i} { set a $i set b "String value $i" set c [string repeat X $i] append sql "INSERT INTO abc VALUES ($a, '$b', '$c');" } append sql {COMMIT;} PREP $sql SQL { DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); } TEST 6 { do_test $testid.1 { execsql {SELECT count(*) FROM abc} } {94} do_test $testid.2 { execsql { SELECT min( (oid == a) AND 'String value ' || a == b AND a == length(c) ) FROM abc; } } {1} } SQL { DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); } TEST 7 { do_test $testid { execsql {SELECT count(*) FROM abc} } {89} do_test $testid { execsql { SELECT min( (oid == a) AND 'String value ' || a == b AND a == length(c) ) FROM abc; } } {1} } SQL { DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); } TEST 9 { do_test $testid { execsql {SELECT count(*) FROM abc} } {84} do_test $testid { execsql { SELECT min( (oid == a) AND 'String value ' || a == b AND a == length(c) ) FROM abc; } } {1} } set padding [string repeat X 500] PREP [subst { DROP TABLE abc; CREATE TABLE abc(a PRIMARY KEY, padding, b, c); INSERT INTO abc VALUES(0, '$padding', 2, 2); INSERT INTO abc VALUES(3, '$padding', 5, 5); INSERT INTO abc VALUES(6, '$padding', 8, 8); }] TEST 10 { do_test $testid { execsql {SELECT a, b, c FROM abc} } {0 2 2 3 5 5 6 8 8} } SQL {BEGIN;} SQL {INSERT INTO abc VALUES(9, 'XXXXX', 11, 12);} TEST_AUTOCOMMIT 11 0 SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;} TEST_AUTOCOMMIT 12 0 SQL {DELETE FROM abc WHERE a = 10;} TEST_AUTOCOMMIT 13 0 SQL {COMMIT;} TEST 14 { do_test $testid.1 { sqlite3_get_autocommit $::DB } {1} do_test $testid.2 { execsql {SELECT a, b, c FROM abc} } {1 2 3 4 5 6 7 8 9} } PREP [subst { DROP TABLE abc; CREATE TABLE abc(a, padding, b, c); INSERT INTO abc VALUES(1, '$padding', 2, 3); INSERT INTO abc VALUES(4, '$padding', 5, 6); INSERT INTO abc VALUES(7, '$padding', 8, 9); CREATE INDEX abc_i ON abc(a, padding, b, c); }] TEST 15 { db eval {PRAGMA cache_size = 10} } SQL {BEGIN;} SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} TEST 16 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 2 4 2 7 2} } SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} TEST 17 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 4 4 4 7 4} } SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} TEST 18 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 8 4 8 7 8} } SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc} TEST 19 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16} } SQL {COMMIT;} TEST 21 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16} } SQL {BEGIN;} SQL {DELETE FROM abc WHERE oid %2} TEST 22 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 8 4 8 7 8} } SQL {DELETE FROM abc} TEST 23 { do_test $testid { execsql {SELECT * FROM abc} } {} } SQL {ROLLBACK;} TEST 24 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16} } # Test some schema modifications inside of a transaction. These should all # cause transaction rollback if they fail. Also query a view, to cover a bit # more code. # PREP {DROP VIEW abc_v;} TEST 25 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc} } SQL {BEGIN;} SQL {CREATE TABLE def(d, e, f);} SQL {CREATE TABLE ghi(g, h, i);} TEST 26 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc def def ghi ghi} } SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi} SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);} TEST 27 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi} } SQL {INSERT INTO def VALUES('a', 'b', 'c')} SQL {INSERT INTO def VALUES(1, 2, 3)} SQL -norollback {INSERT INTO ghi SELECT * FROM def} TEST 28 { do_test $testid { execsql { SELECT * FROM def, ghi WHERE d = g; } } {a b c a b c 1 2 3 1 2 3} } SQL {COMMIT} TEST 29 { do_test $testid { execsql { SELECT * FROM v1 WHERE d = g; } } {a b c a b c 1 2 3 1 2 3} } # Test a simple multi-file transaction # file delete -force test2.db PREP {ATTACH 'test2.db' AS aux;} SQL {BEGIN} SQL {CREATE TABLE aux.tbl2(x, y, z)} SQL {INSERT INTO tbl2 VALUES(1, 2, 3)} SQL {INSERT INTO def VALUES(4, 5, 6)} TEST 30 { do_test $testid { execsql { SELECT * FROM tbl2, def WHERE d = x; } } {1 2 3 1 2 3} } SQL {COMMIT} TEST 31 { do_test $testid { execsql { SELECT * FROM tbl2, def WHERE d = x; } } {1 2 3 1 2 3} } # # End of test program declaration #-------------------------------------------------------------------------- proc run_test {arglist {pcstart 0} {iFailStart 1}} { if {[llength $arglist] %2} { error "Uneven number of arguments to TEST" } for {set i 0} {$i < $pcstart} {incr i} { set k2 [lindex $arglist [expr 2 * $i]] set v2 [lindex $arglist [expr 2 * $i + 1]] set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit # puts "STARTUP" switch -- $k2 { -sql {db eval [lindex $v2 1]} -prep {db eval $v2} } set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit if {$ac && !$nac} {set begin_pc $i} } set iFail $iFailStart set pc $pcstart while {$pc*2 < [llength $arglist]} { # Id of this iteration: set iterid "(pc $pc).(iFail $iFail)" set k [lindex $arglist [expr 2 * $pc]] set v [lindex $arglist [expr 2 * $pc + 1]] switch -- $k { -test { foreach {id script} $v {} set testid "malloc3-(test $id).$iterid" eval $script incr pc } -sql { set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit sqlite_malloc_fail $iFail # puts "SQL $iterid [lindex $v 1]" set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs # puts "rc = $rc msg = \"$msg\"" set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit if {$rc == 0} { # Successful execution of sql. if {[lindex [sqlite_malloc_stat] 2] <= 0} { error "Unreported malloc() failure" } if {$ac && !$nac} { set begin_pc $pc } incr pc set iFail 1 sqlite_malloc_fail 0 integrity_check "malloc3-(integrity).$iterid" } elseif {[regexp {.*out of memory} $msg]} { # Out of memory error, as expected integrity_check "malloc3-(integrity).$iterid" incr iFail if {$nac && !$ac} { if {![lindex $v 0]} { error "Statement \"[lindex $v 1]\" caused a rollback" } # puts "Statement \"[lindex $v 1]\" caused a rollback" for {set i $begin_pc} {$i < $pc} {incr i} { set k2 [lindex $arglist [expr 2 * $i]] set v2 [lindex $arglist [expr 2 * $i + 1]] set catchupsql "" switch -- $k2 { -sql {set catchupsql [lindex $v2 1]} -prep {set catchupsql $v2} } # puts "CATCHUP $iterid $i $catchupsql" db eval $catchupsql } } } else { error $msg } while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} { incr pc -1 } } -prep { # puts "PREP $iterid $v" db eval $v incr pc } default { error "Unknown switch: $k" } } } } # Turn of the Tcl interface's prepared statement caching facility. db cache size 0 #run_test $::run_test_script 59 97 run_test $::run_test_script sqlite_malloc_fail 0 finish_test |