drh | aa0fe80 | 2006-07-26 14:49:19 +0000 | [diff] [blame^] | 1 | # 2001 September 15 |
| 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 | # This file attempts to check the library in an out-of-memory situation. |
| 12 | # When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special |
| 13 | # command (sqlite_malloc_fail N) which causes the N-th malloc to fail. This |
| 14 | # special feature is used to see what happens in the library if a malloc |
| 15 | # were to really fail due to an out-of-memory situation. |
| 16 | # |
| 17 | # $Id: malloc7.test,v 1.1 2006/07/26 14:49:19 drh Exp $ |
| 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
| 22 | # Only run these tests if memory debugging is turned on. |
| 23 | # |
| 24 | if {[info command sqlite_malloc_stat]==""} { |
| 25 | puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." |
| 26 | finish_test |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | # Usage: do_malloc_test <test number> <options...> |
| 31 | # |
| 32 | # The first argument, <test number>, is an integer used to name the |
| 33 | # tests executed by this proc. Options are as follows: |
| 34 | # |
| 35 | # -tclprep TCL script to run to prepare test. |
| 36 | # -sqlprep SQL script to run to prepare test. |
| 37 | # -tclbody TCL script to run with malloc failure simulation. |
| 38 | # -sqlbody TCL script to run with malloc failure simulation. |
| 39 | # -cleanup TCL script to run after the test. |
| 40 | # |
| 41 | # This command runs a series of tests to verify SQLite's ability |
| 42 | # to handle an out-of-memory condition gracefully. It is assumed |
| 43 | # that if this condition occurs a malloc() call will return a |
| 44 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 45 | # the "BUGS" section of malloc(3). |
| 46 | # |
| 47 | # Each iteration of a loop, the TCL commands in any argument passed |
| 48 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 49 | # passed to the -sqlbody switch are executed. Each iteration the |
| 50 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 51 | # each time the loop runs starting from 1. When all commands execute |
| 52 | # successfully, the loop ends. |
| 53 | # |
| 54 | proc do_malloc_test {tn args} { |
| 55 | array unset ::mallocopts |
| 56 | array set ::mallocopts $args |
| 57 | |
| 58 | set ::go 1 |
| 59 | for {set ::n 1} {$::go && $::n < 50000} {incr ::n} { |
| 60 | do_test malloc7-$tn.$::n { |
| 61 | |
| 62 | # Remove all traces of database files test.db and test2.db from the files |
| 63 | # system. Then open (empty database) "test.db" with the handle [db]. |
| 64 | # |
| 65 | sqlite_malloc_fail 0 |
| 66 | catch {db close} |
| 67 | catch {file delete -force test.db} |
| 68 | catch {file delete -force test.db-journal} |
| 69 | catch {file delete -force test2.db} |
| 70 | catch {file delete -force test2.db-journal} |
| 71 | catch {sqlite3 db test.db} |
| 72 | set ::DB [sqlite3_connection_pointer db] |
| 73 | |
| 74 | # Execute any -tclprep and -sqlprep scripts. |
| 75 | # |
| 76 | if {[info exists ::mallocopts(-tclprep)]} { |
| 77 | eval $::mallocopts(-tclprep) |
| 78 | } |
| 79 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 80 | execsql $::mallocopts(-sqlprep) |
| 81 | } |
| 82 | |
| 83 | # Now set the ${::n}th malloc() to fail and execute the -tclbody and |
| 84 | # -sqlbody scripts. |
| 85 | # |
| 86 | sqlite_malloc_fail $::n |
| 87 | set ::mallocbody {} |
| 88 | if {[info exists ::mallocopts(-tclbody)]} { |
| 89 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 90 | } |
| 91 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 92 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 93 | } |
| 94 | set v [catch $::mallocbody msg] |
| 95 | |
| 96 | # If the test fails (if $v!=0) and the database connection actually |
| 97 | # exists, make sure the failure code is SQLITE_NOMEM. |
| 98 | if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)] |
| 99 | && [db errorcode]!=7} { |
| 100 | set v 999 |
| 101 | } |
| 102 | |
| 103 | set leftover [lindex [sqlite_malloc_stat] 2] |
| 104 | if {$leftover>0} { |
| 105 | if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"} |
| 106 | set ::go 0 |
| 107 | if {$v} { |
| 108 | puts "\nError message returned: $msg" |
| 109 | } else { |
| 110 | set v {1 1} |
| 111 | } |
| 112 | } else { |
| 113 | set v2 [expr {$msg=="" || $msg=="out of memory"}] |
| 114 | if {!$v2} {puts "\nError message returned: $msg"} |
| 115 | lappend v $v2 |
| 116 | } |
| 117 | } {1 1} |
| 118 | |
| 119 | if {[info exists ::mallocopts(-cleanup)]} { |
| 120 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 121 | } |
| 122 | } |
| 123 | unset ::mallocopts |
| 124 | } |
| 125 | |
| 126 | db eval { |
| 127 | CREATE TABLE t1(a,b,c,d); |
| 128 | CREATE INDEX i1 ON t1(b,c); |
| 129 | } |
| 130 | |
| 131 | do_malloc_test 1 -tclbody { |
| 132 | set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] |
| 133 | append sql16 "\00\00" |
| 134 | set nbyte [string length $sql16] |
| 135 | set ::STMT [sqlite3_prepare16 $::DB $sql16 $nbyte DUMMY] |
| 136 | sqlite3_finalize $::STMT |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | # Ensure that no file descriptors were leaked. |
| 142 | do_test malloc-99.X { |
| 143 | catch {db close} |
| 144 | set sqlite_open_file_count |
| 145 | } {0} |
| 146 | |
| 147 | puts open-file-count=$sqlite_open_file_count |
| 148 | sqlite_malloc_fail 0 |
| 149 | finish_test |