drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 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 | # |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 15 | # $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $ |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 16 | |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 17 | # If we did not compile with malloc testing enabled, then do nothing. |
| 18 | # |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 19 | ifcapable builtin_test { |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 20 | set MEMDEBUG 1 |
| 21 | } else { |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 22 | set MEMDEBUG 0 |
danielk1977 | 369ff42 | 2007-09-03 07:31:09 +0000 | [diff] [blame] | 23 | return 0 |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 24 | } |
| 25 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 26 | # Usage: do_malloc_test <test number> <options...> |
| 27 | # |
| 28 | # The first argument, <test number>, is an integer used to name the |
| 29 | # tests executed by this proc. Options are as follows: |
| 30 | # |
| 31 | # -tclprep TCL script to run to prepare test. |
| 32 | # -sqlprep SQL script to run to prepare test. |
| 33 | # -tclbody TCL script to run with malloc failure simulation. |
| 34 | # -sqlbody TCL script to run with malloc failure simulation. |
| 35 | # -cleanup TCL script to run after the test. |
| 36 | # |
| 37 | # This command runs a series of tests to verify SQLite's ability |
| 38 | # to handle an out-of-memory condition gracefully. It is assumed |
| 39 | # that if this condition occurs a malloc() call will return a |
| 40 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 41 | # the "BUGS" section of malloc(3). |
| 42 | # |
| 43 | # Each iteration of a loop, the TCL commands in any argument passed |
| 44 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 45 | # passed to the -sqlbody switch are executed. Each iteration the |
| 46 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 47 | # each time the loop runs starting from 1. When all commands execute |
| 48 | # successfully, the loop ends. |
| 49 | # |
| 50 | proc do_malloc_test {tn args} { |
| 51 | array unset ::mallocopts |
| 52 | array set ::mallocopts $args |
| 53 | |
| 54 | if {[string is integer $tn]} { |
| 55 | set tn malloc-$tn |
| 56 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 57 | if {[info exists ::mallocopts(-start)]} { |
| 58 | set start $::mallocopts(-start) |
| 59 | } else { |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 60 | set start 0 |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 61 | } |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 62 | if {[info exists ::mallocopts(-end)]} { |
| 63 | set end $::mallocopts(-end) |
| 64 | } else { |
| 65 | set end 50000 |
| 66 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 67 | save_prng_state |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 68 | |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 69 | foreach ::iRepeat {0 10000000} { |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 70 | set ::go 1 |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 71 | for {set ::n $start} {$::go && $::n <= $end} {incr ::n} { |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 72 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 73 | # If $::iRepeat is 0, then the malloc() failure is transient - it |
| 74 | # fails and then subsequent calls succeed. If $::iRepeat is 1, |
| 75 | # then the failure is persistent - once malloc() fails it keeps |
| 76 | # failing. |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 77 | # |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 78 | set zRepeat "transient" |
| 79 | if {$::iRepeat} {set zRepeat "persistent"} |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 80 | restore_prng_state |
| 81 | foreach file [glob -nocomplain test.db-mj*] {file delete -force $file} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 82 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 83 | do_test ${tn}.${zRepeat}.${::n} { |
| 84 | |
| 85 | # Remove all traces of database files test.db and test2.db |
| 86 | # from the file-system. Then open (empty database) "test.db" |
| 87 | # with the handle [db]. |
| 88 | # |
| 89 | catch {db close} |
| 90 | catch {file delete -force test.db} |
| 91 | catch {file delete -force test.db-journal} |
dan | 5e9e482 | 2010-05-03 18:01:21 +0000 | [diff] [blame] | 92 | catch {file delete -force test.db-wal} |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 93 | catch {file delete -force test2.db} |
| 94 | catch {file delete -force test2.db-journal} |
dan | 5e9e482 | 2010-05-03 18:01:21 +0000 | [diff] [blame] | 95 | catch {file delete -force test2.db-wal} |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 96 | if {[info exists ::mallocopts(-testdb)]} { |
| 97 | file copy $::mallocopts(-testdb) test.db |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 98 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 99 | catch { sqlite3 db test.db } |
| 100 | if {[info commands db] ne ""} { |
| 101 | sqlite3_extended_result_codes db 1 |
| 102 | } |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 103 | sqlite3_db_config_lookaside db 0 0 0 |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 104 | |
| 105 | # Execute any -tclprep and -sqlprep scripts. |
| 106 | # |
| 107 | if {[info exists ::mallocopts(-tclprep)]} { |
| 108 | eval $::mallocopts(-tclprep) |
| 109 | } |
| 110 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 111 | execsql $::mallocopts(-sqlprep) |
| 112 | } |
| 113 | |
| 114 | # Now set the ${::n}th malloc() to fail and execute the -tclbody |
| 115 | # and -sqlbody scripts. |
| 116 | # |
| 117 | sqlite3_memdebug_fail $::n -repeat $::iRepeat |
| 118 | set ::mallocbody {} |
| 119 | if {[info exists ::mallocopts(-tclbody)]} { |
| 120 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 121 | } |
| 122 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 123 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 124 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 125 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 126 | # The following block sets local variables as follows: |
| 127 | # |
| 128 | # isFail - True if an error (any error) was reported by sqlite. |
| 129 | # nFail - The total number of simulated malloc() failures. |
| 130 | # nBenign - The number of benign simulated malloc() failures. |
| 131 | # |
| 132 | set isFail [catch $::mallocbody msg] |
| 133 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 134 | # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) " |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 135 | |
| 136 | # If one or more mallocs failed, run this loop body again. |
| 137 | # |
| 138 | set go [expr {$nFail>0}] |
| 139 | |
| 140 | if {($nFail-$nBenign)==0} { |
| 141 | if {$isFail} { |
| 142 | set v2 $msg |
| 143 | } else { |
| 144 | set isFail 1 |
| 145 | set v2 1 |
| 146 | } |
| 147 | } elseif {!$isFail} { |
| 148 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 149 | } elseif { |
| 150 | [info command db]=="" || |
| 151 | [db errorcode]==7 || |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 152 | $msg=="out of memory" |
| 153 | } { |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 154 | set v2 1 |
| 155 | } else { |
| 156 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 157 | puts [db errorcode] |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 158 | } |
| 159 | lappend isFail $v2 |
| 160 | } {1 1} |
| 161 | |
| 162 | if {[info exists ::mallocopts(-cleanup)]} { |
| 163 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 164 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | unset ::mallocopts |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 168 | sqlite3_memdebug_fail -1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 169 | } |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame^] | 170 | |
| 171 | |
| 172 | #------------------------------------------------------------------------- |
| 173 | # This proc is used to test a single SELECT statement. Parameter $name is |
| 174 | # passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter |
| 175 | # $sql is passed the text of the SELECT statement. Parameter $result is |
| 176 | # set to the expected output if the SELECT statement is successfully |
| 177 | # executed using [db eval]. |
| 178 | # |
| 179 | # Example: |
| 180 | # |
| 181 | # do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2} |
| 182 | # |
| 183 | # If global variable DO_MALLOC_TEST is set to a non-zero value, or if |
| 184 | # it is not defined at all, then OOM testing is performed on the SELECT |
| 185 | # statement. Each OOM test case is said to pass if either (a) executing |
| 186 | # the SELECT statement succeeds and the results match those specified |
| 187 | # by parameter $result, or (b) TCL throws an "out of memory" error. |
| 188 | # |
| 189 | # If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement |
| 190 | # is executed just once. In this case the test case passes if the results |
| 191 | # match the expected results passed via parameter $result. |
| 192 | # |
| 193 | proc do_select_test {name sql result} { |
| 194 | uplevel [list doPassiveTest 0 $name $sql [list 0 $result]] |
| 195 | } |
| 196 | |
| 197 | proc do_restart_select_test {name sql result} { |
| 198 | uplevel [list doPassiveTest 1 $name $sql [list 0 $result]] |
| 199 | } |
| 200 | |
| 201 | proc do_error_test {name sql error} { |
| 202 | uplevel [list doPassiveTest 0 $name $sql [list 1 $error]] |
| 203 | } |
| 204 | |
| 205 | proc doPassiveTest {isRestart name sql catchres} { |
| 206 | if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } |
| 207 | |
| 208 | switch $::DO_MALLOC_TEST { |
| 209 | 0 { # No malloc failures. |
| 210 | do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres |
| 211 | return |
| 212 | } |
| 213 | 1 { # Simulate transient failures. |
| 214 | set nRepeat 1 |
| 215 | set zName "transient" |
| 216 | set nStartLimit 100000 |
| 217 | set nBackup 1 |
| 218 | } |
| 219 | 2 { # Simulate persistent failures. |
| 220 | set nRepeat 1 |
| 221 | set zName "persistent" |
| 222 | set nStartLimit 100000 |
| 223 | set nBackup 1 |
| 224 | } |
| 225 | 3 { # Simulate transient failures with extra brute force. |
| 226 | set nRepeat 100000 |
| 227 | set zName "ridiculous" |
| 228 | set nStartLimit 1 |
| 229 | set nBackup 10 |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | # The set of acceptable results from running [catchsql $sql]. |
| 234 | # |
| 235 | set answers [list {1 {out of memory}} $catchres] |
| 236 | set str [join $answers " OR "] |
| 237 | |
| 238 | set nFail 1 |
| 239 | for {set iLimit $nStartLimit} {$nFail} {incr iLimit} { |
| 240 | for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} { |
| 241 | for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} { |
| 242 | |
| 243 | if {$isRestart} { sqlite3 db test.db } |
| 244 | |
| 245 | sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat |
| 246 | set res [uplevel [list catchsql $sql]] |
| 247 | if {[lsearch -exact $answers $res]>=0} { set res $str } |
| 248 | set testname "$name.$zName.$iFail" |
| 249 | do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str |
| 250 | |
| 251 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | #------------------------------------------------------------------------- |
| 259 | # Test a single write to the database. In this case a "write" is a |
| 260 | # DELETE, UPDATE or INSERT statement. |
| 261 | # |
| 262 | # If OOM testing is performed, there are several acceptable outcomes: |
| 263 | # |
| 264 | # 1) The write succeeds. No error is returned. |
| 265 | # |
| 266 | # 2) An "out of memory" exception is thrown and: |
| 267 | # |
| 268 | # a) The statement has no effect, OR |
| 269 | # b) The current transaction is rolled back, OR |
| 270 | # c) The statement succeeds. This can only happen if the connection |
| 271 | # is in auto-commit mode (after the statement is executed, so this |
| 272 | # includes COMMIT statements). |
| 273 | # |
| 274 | # If the write operation eventually succeeds, zero is returned. If a |
| 275 | # transaction is rolled back, non-zero is returned. |
| 276 | # |
| 277 | # Parameter $name is the name to use for the test case (or test cases). |
| 278 | # The second parameter, $tbl, should be the name of the database table |
| 279 | # being modified. Parameter $sql contains the SQL statement to test. |
| 280 | # |
| 281 | proc do_write_test {name tbl sql} { |
| 282 | if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } |
| 283 | |
| 284 | # Figure out an statement to get a checksum for table $tbl. |
| 285 | db eval "SELECT * FROM $tbl" V break |
| 286 | set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl" |
| 287 | |
| 288 | # Calculate the initial table checksum. |
| 289 | set cksum1 [db one $cksumsql] |
| 290 | |
| 291 | if {$::DO_MALLOC_TEST } { |
| 292 | set answers [list {1 {out of memory}} {0 {}}] |
| 293 | if {$::DO_MALLOC_TEST==1} { |
| 294 | set modes {100000 transient} |
| 295 | } else { |
| 296 | set modes {1 persistent} |
| 297 | } |
| 298 | } else { |
| 299 | set answers [list {0 {}}] |
| 300 | set modes [list 0 nofail] |
| 301 | } |
| 302 | set str [join $answers " OR "] |
| 303 | |
| 304 | foreach {nRepeat zName} $modes { |
| 305 | for {set iFail 1} 1 {incr iFail} { |
| 306 | if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat} |
| 307 | |
| 308 | set res [uplevel [list catchsql $sql]] |
| 309 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
| 310 | if {$nFail==0} { |
| 311 | do_test $name.$zName.$iFail [list set {} $res] {0 {}} |
| 312 | return |
| 313 | } else { |
| 314 | if {[lsearch $answers $res]>=0} { |
| 315 | set res $str |
| 316 | } |
| 317 | do_test $name.$zName.$iFail [list set {} $res] $str |
| 318 | set cksum2 [db one $cksumsql] |
| 319 | if {$cksum1 != $cksum2} return |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |