1# 2007 May 05 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# This file contains common code used by many different malloc tests 13# within the test suite. 14# 15# $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $ 16 17# If we did not compile with malloc testing enabled, then do nothing. 18# 19ifcapable builtin_test { 20 set MEMDEBUG 1 21} else { 22 set MEMDEBUG 0 23 return 0 24} 25 26# Transient and persistent OOM errors: 27# 28set FAULTSIM(oom-transient) [list \ 29 -injectstart {oom_injectstart 0} \ 30 -injectstop oom_injectstop \ 31 -injecterrlist {{1 {out of memory}}} \ 32] 33set FAULTSIM(oom-persistent) [list \ 34 -injectstart {oom_injectstart 1000000} \ 35 -injectstop oom_injectstop \ 36 -injecterrlist {{1 {out of memory}}} \ 37] 38 39# Transient and persistent IO errors: 40# 41set FAULTSIM(ioerr-transient) [list \ 42 -injectstart {ioerr_injectstart 0} \ 43 -injectstop ioerr_injectstop \ 44 -injecterrlist {{1 {disk I/O error}}} \ 45] 46set FAULTSIM(ioerr-persistent) [list \ 47 -injectstart {ioerr_injectstart 1} \ 48 -injectstop ioerr_injectstop \ 49 -injecterrlist {{1 {disk I/O error}}} \ 50] 51 52# SQLITE_FULL errors (always persistent): 53# 54set FAULTSIM(full) [list \ 55 -injectinstall fullerr_injectinstall \ 56 -injectstart fullerr_injectstart \ 57 -injectstop fullerr_injectstop \ 58 -injecterrlist {{1 {database or disk is full}}} \ 59 -injectuninstall fullerr_injectuninstall \ 60] 61 62# Transient and persistent SHM errors: 63# 64set FAULTSIM(shmerr-transient) [list \ 65 -injectinstall shmerr_injectinstall \ 66 -injectstart {shmerr_injectstart 0} \ 67 -injectstop shmerr_injectstop \ 68 -injecterrlist {{1 {disk I/O error}}} \ 69 -injectuninstall shmerr_injectuninstall \ 70] 71set FAULTSIM(shmerr-persistent) [list \ 72 -injectinstall shmerr_injectinstall \ 73 -injectstart {shmerr_injectstart 1} \ 74 -injectstop shmerr_injectstop \ 75 -injecterrlist {{1 {disk I/O error}}} \ 76 -injectuninstall shmerr_injectuninstall \ 77] 78 79# Transient and persistent CANTOPEN errors: 80# 81set FAULTSIM(cantopen-transient) [list \ 82 -injectinstall cantopen_injectinstall \ 83 -injectstart {cantopen_injectstart 0} \ 84 -injectstop cantopen_injectstop \ 85 -injecterrlist {{1 {unable to open database file}}} \ 86 -injectuninstall cantopen_injectuninstall \ 87] 88set FAULTSIM(cantopen-persistent) [list \ 89 -injectinstall cantopen_injectinstall \ 90 -injectstart {cantopen_injectstart 1} \ 91 -injectstop cantopen_injectstop \ 92 -injecterrlist {{1 {unable to open database file}}} \ 93 -injectuninstall cantopen_injectuninstall \ 94] 95 96 97 98#-------------------------------------------------------------------------- 99# Usage do_faultsim_test NAME ?OPTIONS...? 100# 101# -faults List of fault types to simulate. 102# 103# -prep Script to execute before -body. 104# 105# -body Script to execute (with fault injection). 106# 107# -test Script to execute after -body. 108# 109proc do_faultsim_test {name args} { 110 global FAULTSIM 111 112 set DEFAULT(-faults) [array names FAULTSIM] 113 set DEFAULT(-prep) "" 114 set DEFAULT(-body) "" 115 set DEFAULT(-test) "" 116 117 fix_testname name 118 119 array set O [array get DEFAULT] 120 array set O $args 121 foreach o [array names O] { 122 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" } 123 } 124 125 set faultlist [list] 126 foreach f $O(-faults) { 127 set flist [array names FAULTSIM $f] 128 if {[llength $flist]==0} { error "unknown fault: $f" } 129 set faultlist [concat $faultlist $flist] 130 } 131 132 set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)] 133 foreach f [lsort -unique $faultlist] { 134 eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec 135 } 136} 137 138#------------------------------------------------------------------------- 139# Procedures to save and restore the current file-system state: 140# 141# faultsim_save 142# faultsim_restore 143# faultsim_save_and_close 144# faultsim_restore_and_reopen 145# faultsim_delete_and_reopen 146# 147proc faultsim_save {} { 148 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } 149 foreach f [glob -nocomplain test.db*] { 150 set f2 "sv_$f" 151 file copy -force $f $f2 152 } 153} 154proc faultsim_save_and_close {} { 155 faultsim_save 156 catch { db close } 157 return "" 158} 159proc faultsim_restore {} { 160 foreach f [glob -nocomplain test.db*] { forcedelete $f } 161 foreach f2 [glob -nocomplain sv_test.db*] { 162 set f [string range $f2 3 end] 163 file copy -force $f2 $f 164 } 165} 166proc faultsim_restore_and_reopen {{dbfile test.db}} { 167 catch { db close } 168 faultsim_restore 169 sqlite3 db $dbfile 170 sqlite3_extended_result_codes db 1 171 sqlite3_db_config_lookaside db 0 0 0 172} 173 174proc faultsim_integrity_check {{db db}} { 175 set ic [$db eval { PRAGMA integrity_check }] 176 if {$ic != "ok"} { error "Integrity check: $ic" } 177} 178 179proc faultsim_delete_and_reopen {{file test.db}} { 180 catch { db close } 181 foreach f [glob -nocomplain test.db*] { file delete -force $f } 182 sqlite3 db $file 183} 184 185 186# The following procs are used as [do_one_faultsim_test] callbacks when 187# injecting OOM faults into test cases. 188# 189proc oom_injectstart {nRepeat iFail} { 190 sqlite3_memdebug_fail [expr $iFail-1] -repeat $nRepeat 191} 192proc oom_injectstop {} { 193 sqlite3_memdebug_fail -1 194} 195 196# The following procs are used as [do_one_faultsim_test] callbacks when 197# injecting IO error faults into test cases. 198# 199proc ioerr_injectstart {persist iFail} { 200 set ::sqlite_io_error_persist $persist 201 set ::sqlite_io_error_pending $iFail 202} 203proc ioerr_injectstop {} { 204 set sv $::sqlite_io_error_hit 205 set ::sqlite_io_error_persist 0 206 set ::sqlite_io_error_pending 0 207 set ::sqlite_io_error_hardhit 0 208 set ::sqlite_io_error_hit 0 209 set ::sqlite_io_error_pending 0 210 return $sv 211} 212 213# The following procs are used as [do_one_faultsim_test] callbacks when 214# injecting shared-memory related error faults into test cases. 215# 216proc shmerr_injectinstall {} { 217 testvfs shmfault -default true 218 shmfault filter {xShmOpen xShmMap xShmLock} 219} 220proc shmerr_injectuninstall {} { 221 catch {db close} 222 catch {db2 close} 223 shmfault delete 224} 225proc shmerr_injectstart {persist iFail} { 226 shmfault ioerr $iFail $persist 227} 228proc shmerr_injectstop {} { 229 shmfault ioerr 230} 231 232# The following procs are used as [do_one_faultsim_test] callbacks when 233# injecting SQLITE_FULL error faults into test cases. 234# 235proc fullerr_injectinstall {} { 236 testvfs shmfault -default true 237} 238proc fullerr_injectuninstall {} { 239 catch {db close} 240 catch {db2 close} 241 shmfault delete 242} 243proc fullerr_injectstart {iFail} { 244 shmfault full $iFail 1 245} 246proc fullerr_injectstop {} { 247 shmfault full 248} 249 250# The following procs are used as [do_one_faultsim_test] callbacks when 251# injecting SQLITE_CANTOPEN error faults into test cases. 252# 253proc cantopen_injectinstall {} { 254 testvfs shmfault -default true 255} 256proc cantopen_injectuninstall {} { 257 catch {db close} 258 catch {db2 close} 259 shmfault delete 260} 261proc cantopen_injectstart {persist iFail} { 262 shmfault cantopen $iFail $persist 263} 264proc cantopen_injectstop {} { 265 shmfault cantopen 266} 267 268# This command is not called directly. It is used by the 269# [faultsim_test_result] command created by [do_faultsim_test] and used 270# by -test scripts. 271# 272proc faultsim_test_result_int {args} { 273 upvar testrc testrc testresult testresult testnfail testnfail 274 set t [list $testrc $testresult] 275 set r $args 276 if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } { 277 error "nfail=$testnfail rc=$testrc result=$testresult" 278 } 279} 280 281#-------------------------------------------------------------------------- 282# Usage do_one_faultsim_test NAME ?OPTIONS...? 283# 284# The first argument, <test number>, is used as a prefix of the test names 285# taken by tests executed by this command. Options are as follows. All 286# options take a single argument. 287# 288# -injectstart Script to enable fault-injection. 289# 290# -injectstop Script to disable fault-injection. 291# 292# -injecterrlist List of generally acceptable test results (i.e. error 293# messages). Example: [list {1 {out of memory}}] 294# 295# -injectinstall 296# 297# -injectuninstall 298# 299# -prep Script to execute before -body. 300# 301# -body Script to execute (with fault injection). 302# 303# -test Script to execute after -body. 304# 305proc do_one_faultsim_test {testname args} { 306 307 set DEFAULT(-injectstart) "expr" 308 set DEFAULT(-injectstop) "expr 0" 309 set DEFAULT(-injecterrlist) [list] 310 set DEFAULT(-injectinstall) "" 311 set DEFAULT(-injectuninstall) "" 312 set DEFAULT(-prep) "" 313 set DEFAULT(-body) "" 314 set DEFAULT(-test) "" 315 316 array set O [array get DEFAULT] 317 array set O $args 318 foreach o [array names O] { 319 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" } 320 } 321 322 proc faultsim_test_proc {testrc testresult testnfail} $O(-test) 323 proc faultsim_test_result {args} " 324 uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)] 325 " 326 327 eval $O(-injectinstall) 328 329 set stop 0 330 for {set iFail 1} {!$stop} {incr iFail} { 331 332 # Evaluate the -prep script. 333 # 334 eval $O(-prep) 335 336 # Start the fault-injection. Run the -body script. Stop the fault 337 # injection. Local var $nfail is set to the total number of faults 338 # injected into the system this trial. 339 # 340 eval $O(-injectstart) $iFail 341 set rc [catch $O(-body) res] 342 set nfail [eval $O(-injectstop)] 343 344 # Run the -test script. If it throws no error, consider this trial 345 # sucessful. If it does throw an error, cause a [do_test] test to 346 # fail (and print out the unexpected exception thrown by the -test 347 # script at the same time). 348 # 349 set rc [catch [list faultsim_test_proc $rc $res $nfail] res] 350 if {$rc == 0} {set res ok} 351 do_test $testname.$iFail [list list $rc $res] {0 ok} 352 353 # If no faults where injected this trial, don't bother running 354 # any more. This test is finished. 355 # 356 if {$nfail==0} { set stop 1 } 357 } 358 359 eval $O(-injectuninstall) 360} 361 362# Usage: do_malloc_test <test number> <options...> 363# 364# The first argument, <test number>, is an integer used to name the 365# tests executed by this proc. Options are as follows: 366# 367# -tclprep TCL script to run to prepare test. 368# -sqlprep SQL script to run to prepare test. 369# -tclbody TCL script to run with malloc failure simulation. 370# -sqlbody TCL script to run with malloc failure simulation. 371# -cleanup TCL script to run after the test. 372# 373# This command runs a series of tests to verify SQLite's ability 374# to handle an out-of-memory condition gracefully. It is assumed 375# that if this condition occurs a malloc() call will return a 376# NULL pointer. Linux, for example, doesn't do that by default. See 377# the "BUGS" section of malloc(3). 378# 379# Each iteration of a loop, the TCL commands in any argument passed 380# to the -tclbody switch, followed by the SQL commands in any argument 381# passed to the -sqlbody switch are executed. Each iteration the 382# Nth call to sqliteMalloc() is made to fail, where N is increased 383# each time the loop runs starting from 1. When all commands execute 384# successfully, the loop ends. 385# 386proc do_malloc_test {tn args} { 387 array unset ::mallocopts 388 array set ::mallocopts $args 389 390 if {[string is integer $tn]} { 391 set tn malloc-$tn 392 } 393 if {[info exists ::mallocopts(-start)]} { 394 set start $::mallocopts(-start) 395 } else { 396 set start 0 397 } 398 if {[info exists ::mallocopts(-end)]} { 399 set end $::mallocopts(-end) 400 } else { 401 set end 50000 402 } 403 save_prng_state 404 405 foreach ::iRepeat {0 10000000} { 406 set ::go 1 407 for {set ::n $start} {$::go && $::n <= $end} {incr ::n} { 408 409 # If $::iRepeat is 0, then the malloc() failure is transient - it 410 # fails and then subsequent calls succeed. If $::iRepeat is 1, 411 # then the failure is persistent - once malloc() fails it keeps 412 # failing. 413 # 414 set zRepeat "transient" 415 if {$::iRepeat} {set zRepeat "persistent"} 416 restore_prng_state 417 foreach file [glob -nocomplain test.db-mj*] {file delete -force $file} 418 419 do_test ${tn}.${zRepeat}.${::n} { 420 421 # Remove all traces of database files test.db and test2.db 422 # from the file-system. Then open (empty database) "test.db" 423 # with the handle [db]. 424 # 425 catch {db close} 426 catch {db2 close} 427 forcedelete test.db 428 forcedelete test.db-journal 429 forcedelete test.db-wal 430 forcedelete test2.db 431 forcedelete test2.db-journal 432 forcedelete test2.db-wal 433 if {[info exists ::mallocopts(-testdb)]} { 434 file copy $::mallocopts(-testdb) test.db 435 } 436 catch { sqlite3 db test.db } 437 if {[info commands db] ne ""} { 438 sqlite3_extended_result_codes db 1 439 } 440 sqlite3_db_config_lookaside db 0 0 0 441 442 # Execute any -tclprep and -sqlprep scripts. 443 # 444 if {[info exists ::mallocopts(-tclprep)]} { 445 eval $::mallocopts(-tclprep) 446 } 447 if {[info exists ::mallocopts(-sqlprep)]} { 448 execsql $::mallocopts(-sqlprep) 449 } 450 451 # Now set the ${::n}th malloc() to fail and execute the -tclbody 452 # and -sqlbody scripts. 453 # 454 sqlite3_memdebug_fail $::n -repeat $::iRepeat 455 set ::mallocbody {} 456 if {[info exists ::mallocopts(-tclbody)]} { 457 append ::mallocbody "$::mallocopts(-tclbody)\n" 458 } 459 if {[info exists ::mallocopts(-sqlbody)]} { 460 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" 461 } 462 463 # The following block sets local variables as follows: 464 # 465 # isFail - True if an error (any error) was reported by sqlite. 466 # nFail - The total number of simulated malloc() failures. 467 # nBenign - The number of benign simulated malloc() failures. 468 # 469 set isFail [catch $::mallocbody msg] 470 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] 471 # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) " 472 473 # If one or more mallocs failed, run this loop body again. 474 # 475 set go [expr {$nFail>0}] 476 477 if {($nFail-$nBenign)==0} { 478 if {$isFail} { 479 set v2 $msg 480 } else { 481 set isFail 1 482 set v2 1 483 } 484 } elseif {!$isFail} { 485 set v2 $msg 486 } elseif { 487 [info command db]=="" || 488 [db errorcode]==7 || 489 $msg=="out of memory" 490 } { 491 set v2 1 492 } else { 493 set v2 $msg 494 puts [db errorcode] 495 } 496 lappend isFail $v2 497 } {1 1} 498 499 if {[info exists ::mallocopts(-cleanup)]} { 500 catch [list uplevel #0 $::mallocopts(-cleanup)] msg 501 } 502 } 503 } 504 unset ::mallocopts 505 sqlite3_memdebug_fail -1 506} 507 508 509#------------------------------------------------------------------------- 510# This proc is used to test a single SELECT statement. Parameter $name is 511# passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter 512# $sql is passed the text of the SELECT statement. Parameter $result is 513# set to the expected output if the SELECT statement is successfully 514# executed using [db eval]. 515# 516# Example: 517# 518# do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2} 519# 520# If global variable DO_MALLOC_TEST is set to a non-zero value, or if 521# it is not defined at all, then OOM testing is performed on the SELECT 522# statement. Each OOM test case is said to pass if either (a) executing 523# the SELECT statement succeeds and the results match those specified 524# by parameter $result, or (b) TCL throws an "out of memory" error. 525# 526# If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement 527# is executed just once. In this case the test case passes if the results 528# match the expected results passed via parameter $result. 529# 530proc do_select_test {name sql result} { 531 uplevel [list doPassiveTest 0 $name $sql [list 0 [list {*}$result]]] 532} 533 534proc do_restart_select_test {name sql result} { 535 uplevel [list doPassiveTest 1 $name $sql [list 0 $result]] 536} 537 538proc do_error_test {name sql error} { 539 uplevel [list doPassiveTest 0 $name $sql [list 1 $error]] 540} 541 542proc doPassiveTest {isRestart name sql catchres} { 543 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } 544 545 if {[info exists ::testprefix] 546 && [string is integer [string range $name 0 0]] 547 } { 548 set name $::testprefix.$name 549 } 550 551 switch $::DO_MALLOC_TEST { 552 0 { # No malloc failures. 553 do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres 554 return 555 } 556 1 { # Simulate transient failures. 557 set nRepeat 1 558 set zName "transient" 559 set nStartLimit 100000 560 set nBackup 1 561 } 562 2 { # Simulate persistent failures. 563 set nRepeat 1 564 set zName "persistent" 565 set nStartLimit 100000 566 set nBackup 1 567 } 568 3 { # Simulate transient failures with extra brute force. 569 set nRepeat 100000 570 set zName "ridiculous" 571 set nStartLimit 1 572 set nBackup 10 573 } 574 } 575 576 # The set of acceptable results from running [catchsql $sql]. 577 # 578 set answers [list {1 {out of memory}} $catchres] 579 set str [join $answers " OR "] 580 581 set nFail 1 582 for {set iLimit $nStartLimit} {$nFail} {incr iLimit} { 583 for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} { 584 for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} { 585 586 if {$isRestart} { sqlite3 db test.db } 587 588 sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat 589 set res [uplevel [list catchsql $sql]] 590 if {[lsearch -exact $answers $res]>=0} { set res $str } 591 set testname "$name.$zName.$iFail" 592 do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str 593 594 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] 595 } 596 } 597 } 598} 599 600 601#------------------------------------------------------------------------- 602# Test a single write to the database. In this case a "write" is a 603# DELETE, UPDATE or INSERT statement. 604# 605# If OOM testing is performed, there are several acceptable outcomes: 606# 607# 1) The write succeeds. No error is returned. 608# 609# 2) An "out of memory" exception is thrown and: 610# 611# a) The statement has no effect, OR 612# b) The current transaction is rolled back, OR 613# c) The statement succeeds. This can only happen if the connection 614# is in auto-commit mode (after the statement is executed, so this 615# includes COMMIT statements). 616# 617# If the write operation eventually succeeds, zero is returned. If a 618# transaction is rolled back, non-zero is returned. 619# 620# Parameter $name is the name to use for the test case (or test cases). 621# The second parameter, $tbl, should be the name of the database table 622# being modified. Parameter $sql contains the SQL statement to test. 623# 624proc do_write_test {name tbl sql} { 625 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } 626 627 # Figure out an statement to get a checksum for table $tbl. 628 db eval "SELECT * FROM $tbl" V break 629 set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl" 630 631 # Calculate the initial table checksum. 632 set cksum1 [db one $cksumsql] 633 634 if {$::DO_MALLOC_TEST } { 635 set answers [list {1 {out of memory}} {0 {}}] 636 if {$::DO_MALLOC_TEST==1} { 637 set modes {100000 persistent} 638 } else { 639 set modes {1 transient} 640 } 641 } else { 642 set answers [list {0 {}}] 643 set modes [list 0 nofail] 644 } 645 set str [join $answers " OR "] 646 647 foreach {nRepeat zName} $modes { 648 for {set iFail 1} 1 {incr iFail} { 649 if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat} 650 651 set res [uplevel [list catchsql $sql]] 652 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] 653 if {$nFail==0} { 654 do_test $name.$zName.$iFail [list set {} $res] {0 {}} 655 return 656 } else { 657 if {[lsearch $answers $res]>=0} { 658 set res $str 659 } 660 do_test $name.$zName.$iFail [list set {} $res] $str 661 set cksum2 [db one $cksumsql] 662 if {$cksum1 != $cksum2} return 663 } 664 } 665 } 666} 667