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 | # |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 15 | # $Id: malloc_common.tcl,v 1.10 2008/01/16 17:46:38 drh 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 | # |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 19 | ifcapable !memdebug { |
danielk1977 | 369ff42 | 2007-09-03 07:31:09 +0000 | [diff] [blame] | 20 | return 0 |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 21 | } |
| 22 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 23 | # Usage: do_malloc_test <test number> <options...> |
| 24 | # |
| 25 | # The first argument, <test number>, is an integer used to name the |
| 26 | # tests executed by this proc. Options are as follows: |
| 27 | # |
| 28 | # -tclprep TCL script to run to prepare test. |
| 29 | # -sqlprep SQL script to run to prepare test. |
| 30 | # -tclbody TCL script to run with malloc failure simulation. |
| 31 | # -sqlbody TCL script to run with malloc failure simulation. |
| 32 | # -cleanup TCL script to run after the test. |
| 33 | # |
| 34 | # This command runs a series of tests to verify SQLite's ability |
| 35 | # to handle an out-of-memory condition gracefully. It is assumed |
| 36 | # that if this condition occurs a malloc() call will return a |
| 37 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 38 | # the "BUGS" section of malloc(3). |
| 39 | # |
| 40 | # Each iteration of a loop, the TCL commands in any argument passed |
| 41 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 42 | # passed to the -sqlbody switch are executed. Each iteration the |
| 43 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 44 | # each time the loop runs starting from 1. When all commands execute |
| 45 | # successfully, the loop ends. |
| 46 | # |
| 47 | proc do_malloc_test {tn args} { |
| 48 | array unset ::mallocopts |
| 49 | array set ::mallocopts $args |
| 50 | |
| 51 | if {[string is integer $tn]} { |
| 52 | set tn malloc-$tn |
| 53 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 54 | if {[info exists ::mallocopts(-start)]} { |
| 55 | set start $::mallocopts(-start) |
| 56 | } else { |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 57 | set start 0 |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 58 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 59 | save_prng_state |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 60 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 61 | foreach ::iRepeat {0 1} { |
| 62 | set ::go 1 |
| 63 | for {set ::n $start} {$::go && $::n < 50000} {incr ::n} { |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 64 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 65 | # If $::iRepeat is 0, then the malloc() failure is transient - it |
| 66 | # fails and then subsequent calls succeed. If $::iRepeat is 1, |
| 67 | # then the failure is persistent - once malloc() fails it keeps |
| 68 | # failing. |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 69 | # |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 70 | set zRepeat "transient" |
| 71 | if {$::iRepeat} {set zRepeat "persistent"} |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 72 | restore_prng_state |
| 73 | foreach file [glob -nocomplain test.db-mj*] {file delete -force $file} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 74 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 75 | do_test ${tn}.${zRepeat}.${::n} { |
| 76 | |
| 77 | # Remove all traces of database files test.db and test2.db |
| 78 | # from the file-system. Then open (empty database) "test.db" |
| 79 | # with the handle [db]. |
| 80 | # |
| 81 | catch {db close} |
| 82 | catch {file delete -force test.db} |
| 83 | catch {file delete -force test.db-journal} |
| 84 | catch {file delete -force test2.db} |
| 85 | catch {file delete -force test2.db-journal} |
| 86 | if {[info exists ::mallocopts(-testdb)]} { |
| 87 | file copy $::mallocopts(-testdb) test.db |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 88 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 89 | catch { sqlite3 db test.db } |
| 90 | if {[info commands db] ne ""} { |
| 91 | sqlite3_extended_result_codes db 1 |
| 92 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 93 | |
| 94 | # Execute any -tclprep and -sqlprep scripts. |
| 95 | # |
| 96 | if {[info exists ::mallocopts(-tclprep)]} { |
| 97 | eval $::mallocopts(-tclprep) |
| 98 | } |
| 99 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 100 | execsql $::mallocopts(-sqlprep) |
| 101 | } |
| 102 | |
| 103 | # Now set the ${::n}th malloc() to fail and execute the -tclbody |
| 104 | # and -sqlbody scripts. |
| 105 | # |
| 106 | sqlite3_memdebug_fail $::n -repeat $::iRepeat |
| 107 | set ::mallocbody {} |
| 108 | if {[info exists ::mallocopts(-tclbody)]} { |
| 109 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 110 | } |
| 111 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 112 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 113 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 114 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 115 | # The following block sets local variables as follows: |
| 116 | # |
| 117 | # isFail - True if an error (any error) was reported by sqlite. |
| 118 | # nFail - The total number of simulated malloc() failures. |
| 119 | # nBenign - The number of benign simulated malloc() failures. |
| 120 | # |
| 121 | set isFail [catch $::mallocbody msg] |
| 122 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 123 | |
| 124 | # If one or more mallocs failed, run this loop body again. |
| 125 | # |
| 126 | set go [expr {$nFail>0}] |
| 127 | |
| 128 | if {($nFail-$nBenign)==0} { |
| 129 | if {$isFail} { |
| 130 | set v2 $msg |
| 131 | } else { |
| 132 | set isFail 1 |
| 133 | set v2 1 |
| 134 | } |
| 135 | } elseif {!$isFail} { |
| 136 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 137 | } elseif { |
| 138 | [info command db]=="" || |
| 139 | [db errorcode]==7 || |
| 140 | [db errorcode]==[expr 10+(12<<8)] || |
| 141 | $msg=="out of memory" |
| 142 | } { |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 143 | set v2 1 |
| 144 | } else { |
| 145 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 146 | breakpoint |
| 147 | puts [db errorcode] |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 148 | } |
| 149 | lappend isFail $v2 |
| 150 | } {1 1} |
| 151 | |
| 152 | if {[info exists ::mallocopts(-cleanup)]} { |
| 153 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 154 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | unset ::mallocopts |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 158 | sqlite3_memdebug_fail -1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 159 | } |