danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1 | |
| 2 | # Usage: do_malloc_test <test number> <options...> |
| 3 | # |
| 4 | # The first argument, <test number>, is an integer used to name the |
| 5 | # tests executed by this proc. Options are as follows: |
| 6 | # |
| 7 | # -tclprep TCL script to run to prepare test. |
| 8 | # -sqlprep SQL script to run to prepare test. |
| 9 | # -tclbody TCL script to run with malloc failure simulation. |
| 10 | # -sqlbody TCL script to run with malloc failure simulation. |
| 11 | # -cleanup TCL script to run after the test. |
| 12 | # |
| 13 | # This command runs a series of tests to verify SQLite's ability |
| 14 | # to handle an out-of-memory condition gracefully. It is assumed |
| 15 | # that if this condition occurs a malloc() call will return a |
| 16 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 17 | # the "BUGS" section of malloc(3). |
| 18 | # |
| 19 | # Each iteration of a loop, the TCL commands in any argument passed |
| 20 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 21 | # passed to the -sqlbody switch are executed. Each iteration the |
| 22 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 23 | # each time the loop runs starting from 1. When all commands execute |
| 24 | # successfully, the loop ends. |
| 25 | # |
| 26 | proc do_malloc_test {tn args} { |
| 27 | array unset ::mallocopts |
| 28 | array set ::mallocopts $args |
| 29 | |
| 30 | if {[string is integer $tn]} { |
| 31 | set tn malloc-$tn |
| 32 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 33 | if {[info exists ::mallocopts(-start)]} { |
| 34 | set start $::mallocopts(-start) |
| 35 | } else { |
| 36 | set start 1 |
| 37 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 38 | |
| 39 | set ::go 1 |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 40 | for {set ::n $start} {$::go && $::n < 50000} {incr ::n} { |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 41 | do_test $tn.$::n { |
| 42 | |
| 43 | # Remove all traces of database files test.db and test2.db from the files |
| 44 | # system. Then open (empty database) "test.db" with the handle [db]. |
| 45 | # |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 46 | catch {db close} |
| 47 | catch {file delete -force test.db} |
| 48 | catch {file delete -force test.db-journal} |
| 49 | catch {file delete -force test2.db} |
| 50 | catch {file delete -force test2.db-journal} |
| 51 | catch {sqlite3 db test.db} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 52 | |
| 53 | # Execute any -tclprep and -sqlprep scripts. |
| 54 | # |
| 55 | if {[info exists ::mallocopts(-tclprep)]} { |
| 56 | eval $::mallocopts(-tclprep) |
| 57 | } |
| 58 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 59 | execsql $::mallocopts(-sqlprep) |
| 60 | } |
| 61 | |
| 62 | # Now set the ${::n}th malloc() to fail and execute the -tclbody and |
| 63 | # -sqlbody scripts. |
| 64 | # |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 65 | sqlite3_memdebug_fail $::n 1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 66 | set ::mallocbody {} |
| 67 | if {[info exists ::mallocopts(-tclbody)]} { |
| 68 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 69 | } |
| 70 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 71 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 72 | } |
| 73 | set v [catch $::mallocbody msg] |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 74 | set failFlag [sqlite3_memdebug_fail -1 0] |
| 75 | set go [expr {$failFlag>0}] |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 76 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 77 | |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 78 | if {$failFlag==0} { |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 79 | if {$v} { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 80 | set v2 $msg |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 81 | } else { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 82 | set v 1 |
| 83 | set v2 1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 84 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 85 | } elseif {!$v} { |
| 86 | set v2 $msg |
| 87 | } elseif {[info command db]=="" || [db errorcode]==7 |
| 88 | || $msg=="out of memory"} { |
| 89 | set v2 1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 90 | } else { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 91 | set v2 $msg |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 92 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame^] | 93 | lappend v $v2 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 94 | } {1 1} |
| 95 | |
| 96 | if {[info exists ::mallocopts(-cleanup)]} { |
| 97 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 98 | } |
| 99 | } |
| 100 | unset ::mallocopts |
| 101 | } |