blob: 2d0e57e4fcaf0bac561519f1723172c6a4bfe601 [file] [log] [blame]
drh5a3032b2007-09-03 16:12:09 +00001# 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#
danielk197798c21902008-09-23 16:41:29 +000015# $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $
danielk1977c9cf9012007-05-30 10:36:47 +000016
drh5a3032b2007-09-03 16:12:09 +000017# If we did not compile with malloc testing enabled, then do nothing.
18#
drh3088d592008-03-21 16:45:47 +000019ifcapable builtin_test {
drh5efaf072008-03-18 00:07:10 +000020 set MEMDEBUG 1
21} else {
drheee4c8c2008-02-18 22:24:57 +000022 set MEMDEBUG 0
danielk1977369ff422007-09-03 07:31:09 +000023 return 0
danielk1977cdc3a6b2007-08-25 13:09:26 +000024}
25
dan1f55e282010-06-03 09:25:10 +000026# 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
dan146ed782010-06-19 17:26:37 +000052# 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
dan1f55e282010-06-03 09:25:10 +000062# 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
danf9b44192010-06-25 19:09:48 +000079# 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
dan40449062013-08-20 16:08:39 +000096set 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
dan1f55e282010-06-03 09:25:10 +0000104
dana4bc1232010-06-01 17:46:38 +0000105
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#
danf3aef492011-01-21 15:52:02 +0000117# -install Script to execute after faultsim -injectinstall
118#
119# -uninstall Script to execute after faultsim -uninjectinstall
120#
dana4bc1232010-06-01 17:46:38 +0000121proc do_faultsim_test {name args} {
dan1f55e282010-06-03 09:25:10 +0000122 global FAULTSIM
123
dan22c745a2013-08-21 07:25:03 +0000124 foreach n [array names FAULTSIM] {
125 if {$n != "interrupt"} {lappend DEFAULT(-faults) $n}
126 }
dana4bc1232010-06-01 17:46:38 +0000127 set DEFAULT(-prep) ""
128 set DEFAULT(-body) ""
129 set DEFAULT(-test) ""
dan213ca0a2011-03-28 19:10:06 +0000130 set DEFAULT(-install) ""
131 set DEFAULT(-uninstall) ""
dane4bec372014-12-18 18:25:48 +0000132 set DEFAULT(-start) 1
133 set DEFAULT(-end) 0
dana4bc1232010-06-01 17:46:38 +0000134
danddf80eb2010-10-25 12:47:43 +0000135 fix_testname name
136
dana4bc1232010-06-01 17:46:38 +0000137 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
dan1f55e282010-06-03 09:25:10 +0000143 set faultlist [list]
dana4bc1232010-06-01 17:46:38 +0000144 foreach f $O(-faults) {
dan1f55e282010-06-03 09:25:10 +0000145 set flist [array names FAULTSIM $f]
146 if {[llength $flist]==0} { error "unknown fault: $f" }
147 set faultlist [concat $faultlist $flist]
dana4bc1232010-06-01 17:46:38 +0000148 }
dan1f55e282010-06-03 09:25:10 +0000149
danf3aef492011-01-21 15:52:02 +0000150 set testspec [list -prep $O(-prep) -body $O(-body) \
dane4bec372014-12-18 18:25:48 +0000151 -test $O(-test) -install $O(-install) -uninstall $O(-uninstall) \
152 -start $O(-start) -end $O(-end)
danf3aef492011-01-21 15:52:02 +0000153 ]
dan1f55e282010-06-03 09:25:10 +0000154 foreach f [lsort -unique $faultlist] {
155 eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
dana4bc1232010-06-01 17:46:38 +0000156 }
157}
158
danccc7ff42010-11-29 12:06:45 +0000159
dana4bc1232010-06-01 17:46:38 +0000160#-------------------------------------------------------------------------
161# Procedures to save and restore the current file-system state:
162#
danb0ac3e32010-06-16 10:55:42 +0000163# faultsim_save
dan50833e32010-07-14 16:37:17 +0000164# faultsim_restore
dana4bc1232010-06-01 17:46:38 +0000165# faultsim_save_and_close
166# faultsim_restore_and_reopen
danb0ac3e32010-06-16 10:55:42 +0000167# faultsim_delete_and_reopen
dana4bc1232010-06-01 17:46:38 +0000168#
danccc7ff42010-11-29 12:06:45 +0000169proc faultsim_save {args} { uplevel db_save $args }
170proc faultsim_save_and_close {args} { uplevel db_save_and_close $args }
171proc faultsim_restore {args} { uplevel db_restore $args }
172proc 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
danb0ac3e32010-06-16 10:55:42 +0000176}
danccc7ff42010-11-29 12:06:45 +0000177proc faultsim_delete_and_reopen {args} {
178 uplevel db_delete_and_reopen $args
dana4bc1232010-06-01 17:46:38 +0000179 sqlite3_extended_result_codes db 1
180 sqlite3_db_config_lookaside db 0 0 0
181}
182
dan1f55e282010-06-03 09:25:10 +0000183proc faultsim_integrity_check {{db db}} {
184 set ic [$db eval { PRAGMA integrity_check }]
185 if {$ic != "ok"} { error "Integrity check: $ic" }
186}
dana4bc1232010-06-01 17:46:38 +0000187
dan1f55e282010-06-03 09:25:10 +0000188
189# The following procs are used as [do_one_faultsim_test] callbacks when
190# injecting OOM faults into test cases.
dan8521abf2010-05-31 06:38:34 +0000191#
192proc oom_injectstart {nRepeat iFail} {
dan7bddb752010-08-30 15:43:45 +0000193 sqlite3_memdebug_fail [expr $iFail-1] -repeat $nRepeat
dan8521abf2010-05-31 06:38:34 +0000194}
195proc oom_injectstop {} {
196 sqlite3_memdebug_fail -1
197}
198
dan1f55e282010-06-03 09:25:10 +0000199# The following procs are used as [do_one_faultsim_test] callbacks when
200# injecting IO error faults into test cases.
201#
dana4bc1232010-06-01 17:46:38 +0000202proc ioerr_injectstart {persist iFail} {
203 set ::sqlite_io_error_persist $persist
204 set ::sqlite_io_error_pending $iFail
205}
206proc ioerr_injectstop {} {
207 set sv $::sqlite_io_error_hit
208 set ::sqlite_io_error_persist 0
209 set ::sqlite_io_error_pending 0
210 set ::sqlite_io_error_hardhit 0
211 set ::sqlite_io_error_hit 0
212 set ::sqlite_io_error_pending 0
213 return $sv
214}
215
dan1f55e282010-06-03 09:25:10 +0000216# The following procs are used as [do_one_faultsim_test] callbacks when
217# injecting shared-memory related error faults into test cases.
218#
219proc shmerr_injectinstall {} {
220 testvfs shmfault -default true
dan346e4262010-06-23 19:27:36 +0000221 shmfault filter {xShmOpen xShmMap xShmLock}
dan1f55e282010-06-03 09:25:10 +0000222}
223proc shmerr_injectuninstall {} {
224 catch {db close}
225 catch {db2 close}
226 shmfault delete
227}
228proc shmerr_injectstart {persist iFail} {
229 shmfault ioerr $iFail $persist
230}
231proc shmerr_injectstop {} {
danf9b44192010-06-25 19:09:48 +0000232 shmfault ioerr
dan1f55e282010-06-03 09:25:10 +0000233}
234
dandca321a2010-06-24 10:50:17 +0000235# The following procs are used as [do_one_faultsim_test] callbacks when
236# injecting SQLITE_FULL error faults into test cases.
237#
dan146ed782010-06-19 17:26:37 +0000238proc fullerr_injectinstall {} {
239 testvfs shmfault -default true
240}
241proc fullerr_injectuninstall {} {
242 catch {db close}
243 catch {db2 close}
244 shmfault delete
245}
246proc fullerr_injectstart {iFail} {
danf9b44192010-06-25 19:09:48 +0000247 shmfault full $iFail 1
dan146ed782010-06-19 17:26:37 +0000248}
249proc fullerr_injectstop {} {
danf9b44192010-06-25 19:09:48 +0000250 shmfault full
251}
252
253# The following procs are used as [do_one_faultsim_test] callbacks when
254# injecting SQLITE_CANTOPEN error faults into test cases.
255#
256proc cantopen_injectinstall {} {
257 testvfs shmfault -default true
258}
259proc cantopen_injectuninstall {} {
260 catch {db close}
261 catch {db2 close}
262 shmfault delete
263}
264proc cantopen_injectstart {persist iFail} {
265 shmfault cantopen $iFail $persist
266}
267proc cantopen_injectstop {} {
268 shmfault cantopen
dan146ed782010-06-19 17:26:37 +0000269}
270
dan40449062013-08-20 16:08:39 +0000271# The following procs are used as [do_one_faultsim_test] callbacks
272# when injecting SQLITE_INTERRUPT error faults into test cases.
273#
274proc interrupt_injectinstall {} {
275}
276proc interrupt_injectuninstall {} {
277}
278proc interrupt_injectstart {iFail} {
279 set ::sqlite_interrupt_count $iFail
280}
281proc interrupt_injectstop {} {
282 set res [expr $::sqlite_interrupt_count<=0]
283 set ::sqlite_interrupt_count 0
284 set res
285}
286
dana4bc1232010-06-01 17:46:38 +0000287# This command is not called directly. It is used by the
288# [faultsim_test_result] command created by [do_faultsim_test] and used
289# by -test scripts.
dan8521abf2010-05-31 06:38:34 +0000290#
dana4bc1232010-06-01 17:46:38 +0000291proc faultsim_test_result_int {args} {
dan8521abf2010-05-31 06:38:34 +0000292 upvar testrc testrc testresult testresult testnfail testnfail
293 set t [list $testrc $testresult]
dana4bc1232010-06-01 17:46:38 +0000294 set r $args
dane0569a42015-05-16 20:04:43 +0000295 if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch -exact $r $t]<0 } {
dan893c0ff2013-03-25 19:05:07 +0000296 error "nfail=$testnfail rc=$testrc result=$testresult list=$r"
dan8521abf2010-05-31 06:38:34 +0000297 }
298}
299
dana4bc1232010-06-01 17:46:38 +0000300#--------------------------------------------------------------------------
301# Usage do_one_faultsim_test NAME ?OPTIONS...?
dan8521abf2010-05-31 06:38:34 +0000302#
303# The first argument, <test number>, is used as a prefix of the test names
304# taken by tests executed by this command. Options are as follows. All
305# options take a single argument.
306#
307# -injectstart Script to enable fault-injection.
308#
309# -injectstop Script to disable fault-injection.
310#
dana4bc1232010-06-01 17:46:38 +0000311# -injecterrlist List of generally acceptable test results (i.e. error
312# messages). Example: [list {1 {out of memory}}]
313#
dan1f55e282010-06-03 09:25:10 +0000314# -injectinstall
315#
316# -injectuninstall
317#
dan8521abf2010-05-31 06:38:34 +0000318# -prep Script to execute before -body.
319#
320# -body Script to execute (with fault injection).
321#
322# -test Script to execute after -body.
323#
dane4bec372014-12-18 18:25:48 +0000324# -start Index of first fault to inject (default 1)
325#
dana4bc1232010-06-01 17:46:38 +0000326proc do_one_faultsim_test {testname args} {
dan8521abf2010-05-31 06:38:34 +0000327
dan1f55e282010-06-03 09:25:10 +0000328 set DEFAULT(-injectstart) "expr"
329 set DEFAULT(-injectstop) "expr 0"
330 set DEFAULT(-injecterrlist) [list]
331 set DEFAULT(-injectinstall) ""
332 set DEFAULT(-injectuninstall) ""
333 set DEFAULT(-prep) ""
334 set DEFAULT(-body) ""
335 set DEFAULT(-test) ""
danf3aef492011-01-21 15:52:02 +0000336 set DEFAULT(-install) ""
337 set DEFAULT(-uninstall) ""
dane4bec372014-12-18 18:25:48 +0000338 set DEFAULT(-start) 1
339 set DEFAULT(-end) 0
dan8521abf2010-05-31 06:38:34 +0000340
341 array set O [array get DEFAULT]
342 array set O $args
343 foreach o [array names O] {
344 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
345 }
346
347 proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
dana4bc1232010-06-01 17:46:38 +0000348 proc faultsim_test_result {args} "
349 uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
350 "
dan8521abf2010-05-31 06:38:34 +0000351
dan1f55e282010-06-03 09:25:10 +0000352 eval $O(-injectinstall)
danf3aef492011-01-21 15:52:02 +0000353 eval $O(-install)
dan1f55e282010-06-03 09:25:10 +0000354
dan8521abf2010-05-31 06:38:34 +0000355 set stop 0
dane4bec372014-12-18 18:25:48 +0000356 for {set iFail $O(-start)} \
357 {!$stop && ($O(-end)==0 || $iFail<=$O(-end))} \
358 {incr iFail} \
359 {
dan8521abf2010-05-31 06:38:34 +0000360
361 # Evaluate the -prep script.
362 #
363 eval $O(-prep)
364
365 # Start the fault-injection. Run the -body script. Stop the fault
366 # injection. Local var $nfail is set to the total number of faults
367 # injected into the system this trial.
368 #
369 eval $O(-injectstart) $iFail
370 set rc [catch $O(-body) res]
371 set nfail [eval $O(-injectstop)]
372
373 # Run the -test script. If it throws no error, consider this trial
374 # sucessful. If it does throw an error, cause a [do_test] test to
375 # fail (and print out the unexpected exception thrown by the -test
376 # script at the same time).
377 #
378 set rc [catch [list faultsim_test_proc $rc $res $nfail] res]
379 if {$rc == 0} {set res ok}
380 do_test $testname.$iFail [list list $rc $res] {0 ok}
381
382 # If no faults where injected this trial, don't bother running
383 # any more. This test is finished.
384 #
385 if {$nfail==0} { set stop 1 }
386 }
dan1f55e282010-06-03 09:25:10 +0000387
danf3aef492011-01-21 15:52:02 +0000388 eval $O(-uninstall)
dan1f55e282010-06-03 09:25:10 +0000389 eval $O(-injectuninstall)
dan8521abf2010-05-31 06:38:34 +0000390}
391
danielk1977c9cf9012007-05-30 10:36:47 +0000392# Usage: do_malloc_test <test number> <options...>
393#
394# The first argument, <test number>, is an integer used to name the
395# tests executed by this proc. Options are as follows:
396#
397# -tclprep TCL script to run to prepare test.
398# -sqlprep SQL script to run to prepare test.
399# -tclbody TCL script to run with malloc failure simulation.
400# -sqlbody TCL script to run with malloc failure simulation.
401# -cleanup TCL script to run after the test.
402#
403# This command runs a series of tests to verify SQLite's ability
404# to handle an out-of-memory condition gracefully. It is assumed
405# that if this condition occurs a malloc() call will return a
406# NULL pointer. Linux, for example, doesn't do that by default. See
407# the "BUGS" section of malloc(3).
408#
409# Each iteration of a loop, the TCL commands in any argument passed
410# to the -tclbody switch, followed by the SQL commands in any argument
411# passed to the -sqlbody switch are executed. Each iteration the
412# Nth call to sqliteMalloc() is made to fail, where N is increased
413# each time the loop runs starting from 1. When all commands execute
414# successfully, the loop ends.
415#
416proc do_malloc_test {tn args} {
417 array unset ::mallocopts
418 array set ::mallocopts $args
419
420 if {[string is integer $tn]} {
421 set tn malloc-$tn
dan76ccd892014-08-12 13:38:52 +0000422 catch { set tn $::testprefix-$tn }
danielk1977c9cf9012007-05-30 10:36:47 +0000423 }
drhf3a65f72007-08-22 20:18:21 +0000424 if {[info exists ::mallocopts(-start)]} {
425 set start $::mallocopts(-start)
426 } else {
danielk1977ae72d982007-10-03 08:46:44 +0000427 set start 0
drhf3a65f72007-08-22 20:18:21 +0000428 }
drh1527ff42008-01-18 17:03:32 +0000429 if {[info exists ::mallocopts(-end)]} {
430 set end $::mallocopts(-end)
431 } else {
432 set end 50000
433 }
drh93aed5a2008-01-16 17:46:38 +0000434 save_prng_state
danielk1977c9cf9012007-05-30 10:36:47 +0000435
drh643167f2008-01-22 21:30:53 +0000436 foreach ::iRepeat {0 10000000} {
danielk1977a1644fd2007-08-29 12:31:25 +0000437 set ::go 1
drh1527ff42008-01-18 17:03:32 +0000438 for {set ::n $start} {$::go && $::n <= $end} {incr ::n} {
danielk1977c9cf9012007-05-30 10:36:47 +0000439
danielk1977a1644fd2007-08-29 12:31:25 +0000440 # If $::iRepeat is 0, then the malloc() failure is transient - it
441 # fails and then subsequent calls succeed. If $::iRepeat is 1,
442 # then the failure is persistent - once malloc() fails it keeps
443 # failing.
danielk1977c9cf9012007-05-30 10:36:47 +0000444 #
danielk1977a1644fd2007-08-29 12:31:25 +0000445 set zRepeat "transient"
446 if {$::iRepeat} {set zRepeat "persistent"}
drh93aed5a2008-01-16 17:46:38 +0000447 restore_prng_state
mistachkinfda06be2011-08-02 00:57:34 +0000448 foreach file [glob -nocomplain test.db-mj*] {forcedelete $file}
danielk1977c9cf9012007-05-30 10:36:47 +0000449
danielk1977a1644fd2007-08-29 12:31:25 +0000450 do_test ${tn}.${zRepeat}.${::n} {
451
452 # Remove all traces of database files test.db and test2.db
453 # from the file-system. Then open (empty database) "test.db"
454 # with the handle [db].
455 #
456 catch {db close}
dan304feff2010-07-16 10:39:54 +0000457 catch {db2 close}
dand78fe8e2010-09-16 15:23:07 +0000458 forcedelete test.db
459 forcedelete test.db-journal
460 forcedelete test.db-wal
461 forcedelete test2.db
462 forcedelete test2.db-journal
463 forcedelete test2.db-wal
danielk1977a1644fd2007-08-29 12:31:25 +0000464 if {[info exists ::mallocopts(-testdb)]} {
mistachkin5ea42982011-08-02 20:03:36 +0000465 copy_file $::mallocopts(-testdb) test.db
danielk1977c9cf9012007-05-30 10:36:47 +0000466 }
danielk1977ae72d982007-10-03 08:46:44 +0000467 catch { sqlite3 db test.db }
468 if {[info commands db] ne ""} {
469 sqlite3_extended_result_codes db 1
470 }
drhe9d1c722008-08-04 20:13:26 +0000471 sqlite3_db_config_lookaside db 0 0 0
danielk1977a1644fd2007-08-29 12:31:25 +0000472
473 # Execute any -tclprep and -sqlprep scripts.
474 #
475 if {[info exists ::mallocopts(-tclprep)]} {
476 eval $::mallocopts(-tclprep)
477 }
478 if {[info exists ::mallocopts(-sqlprep)]} {
479 execsql $::mallocopts(-sqlprep)
480 }
481
482 # Now set the ${::n}th malloc() to fail and execute the -tclbody
483 # and -sqlbody scripts.
484 #
485 sqlite3_memdebug_fail $::n -repeat $::iRepeat
486 set ::mallocbody {}
487 if {[info exists ::mallocopts(-tclbody)]} {
488 append ::mallocbody "$::mallocopts(-tclbody)\n"
489 }
490 if {[info exists ::mallocopts(-sqlbody)]} {
491 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
492 }
danielk1977c9cf9012007-05-30 10:36:47 +0000493
danielk1977a1644fd2007-08-29 12:31:25 +0000494 # The following block sets local variables as follows:
495 #
496 # isFail - True if an error (any error) was reported by sqlite.
497 # nFail - The total number of simulated malloc() failures.
498 # nBenign - The number of benign simulated malloc() failures.
499 #
500 set isFail [catch $::mallocbody msg]
501 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
drh643167f2008-01-22 21:30:53 +0000502 # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) "
danielk1977a1644fd2007-08-29 12:31:25 +0000503
504 # If one or more mallocs failed, run this loop body again.
505 #
506 set go [expr {$nFail>0}]
507
508 if {($nFail-$nBenign)==0} {
509 if {$isFail} {
510 set v2 $msg
511 } else {
512 set isFail 1
513 set v2 1
514 }
515 } elseif {!$isFail} {
516 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000517 } elseif {
518 [info command db]=="" ||
519 [db errorcode]==7 ||
danielk1977ae72d982007-10-03 08:46:44 +0000520 $msg=="out of memory"
521 } {
danielk1977a1644fd2007-08-29 12:31:25 +0000522 set v2 1
523 } else {
524 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000525 puts [db errorcode]
danielk1977a1644fd2007-08-29 12:31:25 +0000526 }
527 lappend isFail $v2
528 } {1 1}
529
530 if {[info exists ::mallocopts(-cleanup)]} {
531 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
532 }
danielk1977c9cf9012007-05-30 10:36:47 +0000533 }
534 }
535 unset ::mallocopts
danielk197777519402007-08-30 11:48:31 +0000536 sqlite3_memdebug_fail -1
danielk1977c9cf9012007-05-30 10:36:47 +0000537}
danef378022010-05-04 11:06:03 +0000538
539
540#-------------------------------------------------------------------------
541# This proc is used to test a single SELECT statement. Parameter $name is
542# passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter
543# $sql is passed the text of the SELECT statement. Parameter $result is
544# set to the expected output if the SELECT statement is successfully
545# executed using [db eval].
546#
547# Example:
548#
549# do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2}
550#
551# If global variable DO_MALLOC_TEST is set to a non-zero value, or if
552# it is not defined at all, then OOM testing is performed on the SELECT
553# statement. Each OOM test case is said to pass if either (a) executing
554# the SELECT statement succeeds and the results match those specified
555# by parameter $result, or (b) TCL throws an "out of memory" error.
556#
557# If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement
558# is executed just once. In this case the test case passes if the results
559# match the expected results passed via parameter $result.
560#
561proc do_select_test {name sql result} {
dan4f7c5e62010-10-19 14:07:59 +0000562 uplevel [list doPassiveTest 0 $name $sql [list 0 [list {*}$result]]]
danef378022010-05-04 11:06:03 +0000563}
564
565proc do_restart_select_test {name sql result} {
566 uplevel [list doPassiveTest 1 $name $sql [list 0 $result]]
567}
568
569proc do_error_test {name sql error} {
570 uplevel [list doPassiveTest 0 $name $sql [list 1 $error]]
571}
572
573proc doPassiveTest {isRestart name sql catchres} {
574 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
575
dan4f7c5e62010-10-19 14:07:59 +0000576 if {[info exists ::testprefix]
577 && [string is integer [string range $name 0 0]]
578 } {
579 set name $::testprefix.$name
580 }
581
danef378022010-05-04 11:06:03 +0000582 switch $::DO_MALLOC_TEST {
583 0 { # No malloc failures.
584 do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres
585 return
586 }
587 1 { # Simulate transient failures.
588 set nRepeat 1
589 set zName "transient"
590 set nStartLimit 100000
591 set nBackup 1
592 }
593 2 { # Simulate persistent failures.
594 set nRepeat 1
595 set zName "persistent"
596 set nStartLimit 100000
597 set nBackup 1
598 }
599 3 { # Simulate transient failures with extra brute force.
600 set nRepeat 100000
601 set zName "ridiculous"
602 set nStartLimit 1
603 set nBackup 10
604 }
605 }
606
607 # The set of acceptable results from running [catchsql $sql].
608 #
609 set answers [list {1 {out of memory}} $catchres]
610 set str [join $answers " OR "]
611
612 set nFail 1
613 for {set iLimit $nStartLimit} {$nFail} {incr iLimit} {
614 for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} {
615 for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} {
616
617 if {$isRestart} { sqlite3 db test.db }
618
619 sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat
620 set res [uplevel [list catchsql $sql]]
621 if {[lsearch -exact $answers $res]>=0} { set res $str }
622 set testname "$name.$zName.$iFail"
623 do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str
624
625 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
626 }
627 }
628 }
629}
630
631
632#-------------------------------------------------------------------------
633# Test a single write to the database. In this case a "write" is a
634# DELETE, UPDATE or INSERT statement.
635#
636# If OOM testing is performed, there are several acceptable outcomes:
637#
638# 1) The write succeeds. No error is returned.
639#
640# 2) An "out of memory" exception is thrown and:
641#
642# a) The statement has no effect, OR
643# b) The current transaction is rolled back, OR
644# c) The statement succeeds. This can only happen if the connection
645# is in auto-commit mode (after the statement is executed, so this
646# includes COMMIT statements).
647#
648# If the write operation eventually succeeds, zero is returned. If a
649# transaction is rolled back, non-zero is returned.
650#
651# Parameter $name is the name to use for the test case (or test cases).
652# The second parameter, $tbl, should be the name of the database table
653# being modified. Parameter $sql contains the SQL statement to test.
654#
655proc do_write_test {name tbl sql} {
656 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
657
658 # Figure out an statement to get a checksum for table $tbl.
659 db eval "SELECT * FROM $tbl" V break
660 set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl"
661
662 # Calculate the initial table checksum.
663 set cksum1 [db one $cksumsql]
664
665 if {$::DO_MALLOC_TEST } {
666 set answers [list {1 {out of memory}} {0 {}}]
667 if {$::DO_MALLOC_TEST==1} {
shanehdecd09c2010-07-09 18:43:40 +0000668 set modes {100000 persistent}
danef378022010-05-04 11:06:03 +0000669 } else {
shanehdecd09c2010-07-09 18:43:40 +0000670 set modes {1 transient}
danef378022010-05-04 11:06:03 +0000671 }
672 } else {
673 set answers [list {0 {}}]
674 set modes [list 0 nofail]
675 }
676 set str [join $answers " OR "]
677
678 foreach {nRepeat zName} $modes {
679 for {set iFail 1} 1 {incr iFail} {
680 if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat}
681
682 set res [uplevel [list catchsql $sql]]
683 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
684 if {$nFail==0} {
685 do_test $name.$zName.$iFail [list set {} $res] {0 {}}
686 return
687 } else {
688 if {[lsearch $answers $res]>=0} {
689 set res $str
690 }
691 do_test $name.$zName.$iFail [list set {} $res] $str
692 set cksum2 [db one $cksumsql]
693 if {$cksum1 != $cksum2} return
694 }
695 }
696 }
697}