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