drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file attempts to check the library in an out-of-memory situation. |
drh | c89b91b | 2005-01-03 01:32:59 +0000 | [diff] [blame] | 12 | # When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 13 | # command (sqlite_malloc_fail N) which causes the N-th malloc to fail. This |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 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 | # |
danielk1977 | b5584c0 | 2007-03-30 07:10:50 +0000 | [diff] [blame^] | 17 | # $Id: malloc.test,v 1.40 2007/03/30 07:10:52 danielk1977 Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 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 | # |
drh | b5f70c2 | 2004-02-14 01:39:50 +0000 | [diff] [blame] | 24 | if {[info command sqlite_malloc_stat]==""} { |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 25 | puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 26 | finish_test |
| 27 | return |
| 28 | } |
| 29 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 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. |
danielk1977 | 656152c | 2005-01-12 13:04:54 +0000 | [diff] [blame] | 37 | # -tclbody TCL script to run with malloc failure simulation. |
| 38 | # -sqlbody TCL script to run with malloc failure simulation. |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 39 | # -cleanup TCL script to run after the test. |
danielk1977 | 656152c | 2005-01-12 13:04:54 +0000 | [diff] [blame] | 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. |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 53 | # |
| 54 | proc do_malloc_test {tn args} { |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 55 | array unset ::mallocopts |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 56 | array set ::mallocopts $args |
| 57 | |
| 58 | set ::go 1 |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 59 | for {set ::n 1} {$::go && $::n < 50000} {incr ::n} { |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 60 | do_test malloc-$tn.$::n { |
| 61 | |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 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 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 65 | sqlite_malloc_fail 0 |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 66 | catch {db close} |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 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} |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 71 | catch {sqlite3 db test.db} |
| 72 | set ::DB [sqlite3_connection_pointer db] |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 73 | |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 74 | # Execute any -tclprep and -sqlprep scripts. |
| 75 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 76 | if {[info exists ::mallocopts(-tclprep)]} { |
| 77 | eval $::mallocopts(-tclprep) |
| 78 | } |
| 79 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 80 | execsql $::mallocopts(-sqlprep) |
| 81 | } |
| 82 | |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 83 | # Now set the ${::n}th malloc() to fail and execute the -tclbody and |
| 84 | # -sqlbody scripts. |
| 85 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 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 | } |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 94 | set v [catch $::mallocbody msg] |
| 95 | |
drh | a06ab2c | 2006-04-10 13:37:47 +0000 | [diff] [blame] | 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 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 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 |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 107 | if {$v} { |
| 108 | puts "\nError message returned: $msg" |
| 109 | } else { |
| 110 | set v {1 1} |
| 111 | } |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 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} |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 118 | |
| 119 | if {[info exists ::mallocopts(-cleanup)]} { |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 120 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 121 | } |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 122 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 123 | unset ::mallocopts |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 126 | do_malloc_test 1 -tclprep { |
| 127 | db close |
| 128 | } -tclbody { |
| 129 | if {[catch {sqlite3 db test.db}]} { |
| 130 | error "out of memory" |
| 131 | } |
| 132 | } -sqlbody { |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 133 | DROP TABLE IF EXISTS t1; |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 134 | CREATE TABLE t1( |
| 135 | a int, b float, c double, d text, e varchar(20), |
| 136 | primary key(a,b,c) |
| 137 | ); |
| 138 | CREATE INDEX i1 ON t1(a,b); |
drh | 76f8079 | 2006-07-11 12:40:25 +0000 | [diff] [blame] | 139 | INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 140 | INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); |
| 141 | SELECT * FROM t1; |
| 142 | SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; |
| 143 | DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); |
| 144 | SELECT count(*) FROM t1; |
| 145 | } |
drh | d400728 | 2001-04-12 23:21:58 +0000 | [diff] [blame] | 146 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 147 | # Ensure that no file descriptors were leaked. |
| 148 | do_test malloc-1.X { |
| 149 | catch {db close} |
| 150 | set sqlite_open_file_count |
| 151 | } {0} |
| 152 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 153 | do_malloc_test 2 -sqlbody { |
drh | fc23314 | 2005-08-19 01:07:15 +0000 | [diff] [blame] | 154 | CREATE TABLE t1(a int, b int default 'abc', c int default 1); |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 155 | CREATE INDEX i1 ON t1(a,b); |
| 156 | INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); |
| 157 | INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); |
| 158 | INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); |
| 159 | INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); |
| 160 | INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); |
| 161 | INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); |
| 162 | SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; |
| 163 | UPDATE t1 SET b=b||b||b||b; |
| 164 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 165 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 166 | INSERT INTO t1 SELECT * FROM t1 |
| 167 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 168 | DELETE FROM t1 WHERE a>=10; |
| 169 | DROP INDEX i1; |
| 170 | DELETE FROM t1; |
| 171 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 172 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 173 | # Ensure that no file descriptors were leaked. |
| 174 | do_test malloc-2.X { |
| 175 | catch {db close} |
| 176 | set sqlite_open_file_count |
| 177 | } {0} |
| 178 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 179 | do_malloc_test 3 -sqlbody { |
| 180 | BEGIN TRANSACTION; |
| 181 | CREATE TABLE t1(a int, b int, c int); |
| 182 | CREATE INDEX i1 ON t1(a,b); |
| 183 | INSERT INTO t1 VALUES(1,1,99); |
| 184 | INSERT INTO t1 VALUES(2,4,98); |
| 185 | INSERT INTO t1 VALUES(3,9,97); |
| 186 | INSERT INTO t1 VALUES(4,16,96); |
| 187 | INSERT INTO t1 VALUES(5,25,95); |
| 188 | INSERT INTO t1 VALUES(6,36,94); |
| 189 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 190 | DELETE FROM t1 WHERE a>=10; |
| 191 | DROP INDEX i1; |
| 192 | DELETE FROM t1; |
| 193 | ROLLBACK; |
| 194 | } |
| 195 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 196 | |
| 197 | # Ensure that no file descriptors were leaked. |
| 198 | do_test malloc-3.X { |
| 199 | catch {db close} |
| 200 | set sqlite_open_file_count |
| 201 | } {0} |
| 202 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 203 | do_malloc_test 4 -sqlbody { |
| 204 | BEGIN TRANSACTION; |
| 205 | CREATE TABLE t1(a int, b int, c int); |
| 206 | CREATE INDEX i1 ON t1(a,b); |
| 207 | INSERT INTO t1 VALUES(1,1,99); |
| 208 | INSERT INTO t1 VALUES(2,4,98); |
| 209 | INSERT INTO t1 VALUES(3,9,97); |
| 210 | INSERT INTO t1 VALUES(4,16,96); |
| 211 | INSERT INTO t1 VALUES(5,25,95); |
| 212 | INSERT INTO t1 VALUES(6,36,94); |
| 213 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 214 | INSERT INTO t1 SELECT * FROM t1 |
| 215 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 216 | DROP INDEX i1; |
| 217 | DELETE FROM t1; |
| 218 | COMMIT; |
| 219 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 220 | |
| 221 | # Ensure that no file descriptors were leaked. |
| 222 | do_test malloc-4.X { |
| 223 | catch {db close} |
| 224 | set sqlite_open_file_count |
| 225 | } {0} |
| 226 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 227 | do_malloc_test 5 -sqlbody { |
| 228 | BEGIN TRANSACTION; |
| 229 | CREATE TABLE t1(a,b); |
| 230 | CREATE TABLE t2(x,y); |
| 231 | CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 232 | INSERT INTO t2(x,y) VALUES(new.rowid,1); |
| 233 | UPDATE t2 SET y=y+1 WHERE x=new.rowid; |
| 234 | SELECT 123; |
| 235 | DELETE FROM t2 WHERE x=new.rowid; |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 236 | END; |
| 237 | INSERT INTO t1(a,b) VALUES(2,3); |
| 238 | COMMIT; |
| 239 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 240 | |
| 241 | # Ensure that no file descriptors were leaked. |
| 242 | do_test malloc-5.X { |
| 243 | catch {db close} |
| 244 | set sqlite_open_file_count |
| 245 | } {0} |
| 246 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 247 | do_malloc_test 6 -sqlprep { |
| 248 | BEGIN TRANSACTION; |
| 249 | CREATE TABLE t1(a); |
| 250 | INSERT INTO t1 VALUES(1); |
| 251 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 252 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 253 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 254 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 255 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 256 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 257 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 258 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 259 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 260 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 261 | DELETE FROM t1 where rowid%5 = 0; |
| 262 | COMMIT; |
| 263 | } -sqlbody { |
| 264 | VACUUM; |
| 265 | } |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 266 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 267 | do_malloc_test 7 -sqlprep { |
| 268 | CREATE TABLE t1(a, b); |
| 269 | INSERT INTO t1 VALUES(1, 2); |
| 270 | INSERT INTO t1 VALUES(3, 4); |
| 271 | INSERT INTO t1 VALUES(5, 6); |
| 272 | INSERT INTO t1 VALUES(7, randstr(1200,1200)); |
| 273 | } -sqlbody { |
| 274 | SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; |
| 275 | SELECT a FROM t1 WHERE a<6 ORDER BY a; |
| 276 | SELECT b FROM t1 WHERE a>6; |
| 277 | } |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 278 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 279 | # This block is designed to test that some malloc failures that may |
| 280 | # occur in vdbeapi.c. Specifically, if a malloc failure that occurs |
| 281 | # when converting UTF-16 text to integers and real numbers is handled |
| 282 | # correctly. |
| 283 | # |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 284 | # This is done by retrieving a string from the database engine and |
| 285 | # manipulating it using the sqlite3_column_*** APIs. This doesn't |
| 286 | # actually return an error to the user when a malloc() fails.. That |
| 287 | # could be viewed as a bug. |
| 288 | # |
| 289 | # These tests only run if UTF-16 support is compiled in. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 290 | # |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 291 | if {$::sqlite_options(utf16)} { |
| 292 | do_malloc_test 8 -tclprep { |
| 293 | set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" |
| 294 | set ::STMT [sqlite3_prepare $::DB $sql -1 X] |
| 295 | sqlite3_step $::STMT |
| 296 | if { $::tcl_platform(byteOrder)=="littleEndian" } { |
| 297 | set ::bomstr "\xFF\xFE" |
| 298 | } else { |
| 299 | set ::bomstr "\xFE\xFF" |
| 300 | } |
| 301 | append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] |
| 302 | } -tclbody { |
| 303 | sqlite3_column_text16 $::STMT 0 |
| 304 | sqlite3_column_int $::STMT 0 |
| 305 | sqlite3_column_text16 $::STMT 1 |
| 306 | sqlite3_column_double $::STMT 1 |
| 307 | sqlite3_reset $::STMT |
| 308 | sqlite3_bind_text16 $::STMT 1 $::bomstr 60 |
| 309 | catch {sqlite3_finalize $::STMT} |
| 310 | if {[lindex [sqlite_malloc_stat] 2]<=0} { |
| 311 | error "out of memory" |
| 312 | } |
| 313 | } -cleanup { |
| 314 | sqlite3_finalize $::STMT |
| 315 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 316 | } |
| 317 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 318 | # This block tests that malloc() failures that occur whilst commiting |
| 319 | # a multi-file transaction are handled correctly. |
| 320 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 321 | do_malloc_test 9 -sqlprep { |
| 322 | ATTACH 'test2.db' as test2; |
| 323 | CREATE TABLE abc1(a, b, c); |
| 324 | CREATE TABLE test2.abc2(a, b, c); |
| 325 | } -sqlbody { |
| 326 | BEGIN; |
| 327 | INSERT INTO abc1 VALUES(1, 2, 3); |
| 328 | INSERT INTO abc2 VALUES(1, 2, 3); |
| 329 | COMMIT; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 332 | # This block tests malloc() failures that occur while opening a |
| 333 | # connection to a database. |
| 334 | do_malloc_test 10 -sqlprep { |
| 335 | CREATE TABLE abc(a, b, c); |
| 336 | } -tclbody { |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 337 | db close |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 338 | sqlite3 db2 test.db |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 339 | db2 eval {SELECT * FROM sqlite_master} |
| 340 | db2 close |
| 341 | } |
| 342 | |
| 343 | # This block tests malloc() failures that occur within calls to |
| 344 | # sqlite3_create_function(). |
| 345 | do_malloc_test 11 -tclbody { |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 346 | set rc [sqlite3_create_function $::DB] |
| 347 | if {[string match $rc SQLITE_NOMEM]} { |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 348 | error "out of memory" |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | do_malloc_test 12 -tclbody { |
| 353 | set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] |
| 354 | append sql16 "\00\00" |
| 355 | set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY] |
| 356 | sqlite3_finalize $::STMT |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 357 | } |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 358 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 359 | # Test malloc errors when replaying two hot journals from a 2-file |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 360 | # transaction. |
| 361 | ifcapable crashtest { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 362 | do_malloc_test 13 -tclprep { |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 363 | set rc [crashsql -delay 1 -file test2.db { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 364 | ATTACH 'test2.db' as aux; |
| 365 | PRAGMA cache_size = 10; |
| 366 | BEGIN; |
| 367 | CREATE TABLE aux.t2(a, b, c); |
| 368 | CREATE TABLE t1(a, b, c); |
| 369 | COMMIT; |
| 370 | }] |
| 371 | if {$rc!="1 {child process exited abnormally}"} { |
| 372 | error "Wrong error message: $rc" |
| 373 | } |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 374 | } -tclbody { |
| 375 | db eval {ATTACH 'test2.db' as aux;} |
| 376 | set rc [catch {db eval { |
| 377 | SELECT * FROM t1; |
| 378 | SELECT * FROM t2; |
| 379 | }} err] |
| 380 | if {$rc && $err!="no such table: t1"} { |
| 381 | error $err |
| 382 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
danielk1977 | 76b047d | 2005-01-19 03:52:54 +0000 | [diff] [blame] | 386 | if {$tcl_platform(platform)!="windows"} { |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 387 | do_malloc_test 14 -tclprep { |
| 388 | catch {db close} |
| 389 | sqlite3 db2 test2.db |
| 390 | db2 eval { |
| 391 | PRAGMA synchronous = 0; |
| 392 | CREATE TABLE t1(a, b); |
| 393 | INSERT INTO t1 VALUES(1, 2); |
| 394 | BEGIN; |
| 395 | INSERT INTO t1 VALUES(3, 4); |
| 396 | } |
| 397 | copy_file test2.db test.db |
| 398 | copy_file test2.db-journal test.db-journal |
| 399 | db2 close |
| 400 | } -tclbody { |
| 401 | sqlite3 db test.db |
| 402 | db eval { |
| 403 | SELECT * FROM t1; |
| 404 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 405 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 406 | } |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 407 | |
| 408 | proc string_compare {a b} { |
| 409 | return [string compare $a $b] |
| 410 | } |
| 411 | |
| 412 | # Test for malloc() failures in sqlite3_create_collation() and |
| 413 | # sqlite3_create_collation16(). |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 414 | # |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 415 | do_malloc_test 15 -tclbody { |
| 416 | db collate string_compare string_compare |
| 417 | if {[catch {add_test_collate $::DB 1 1 1} msg]} { |
| 418 | if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} |
| 419 | error $msg |
| 420 | } |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 421 | |
| 422 | db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} |
| 423 | db complete {-- Useful comment} |
| 424 | |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 425 | execsql { |
| 426 | CREATE TABLE t1(a, b COLLATE string_compare); |
| 427 | INSERT INTO t1 VALUES(10, 'string'); |
| 428 | INSERT INTO t1 VALUES(10, 'string2'); |
| 429 | } |
danielk1977 | 76b047d | 2005-01-19 03:52:54 +0000 | [diff] [blame] | 430 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 431 | |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 432 | # Also test sqlite3_complete(). There are (currently) no malloc() |
| 433 | # calls in this function, but test anyway against future changes. |
| 434 | # |
| 435 | do_malloc_test 16 -tclbody { |
| 436 | db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} |
| 437 | db complete {-- Useful comment} |
| 438 | db eval { |
| 439 | SELECT * FROM sqlite_master; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | # Test handling of malloc() failures in sqlite3_open16(). |
| 444 | # |
| 445 | do_malloc_test 17 -tclbody { |
| 446 | set DB2 0 |
| 447 | set STMT 0 |
| 448 | |
| 449 | # open database using sqlite3_open16() |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 450 | set filename [encoding convertto unicode test.db] |
| 451 | append filename "\x00\x00" |
| 452 | set DB2 [sqlite3_open16 $filename -unused] |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 453 | if {0==$DB2} { |
| 454 | error "out of memory" |
| 455 | } |
| 456 | |
| 457 | # Prepare statement |
| 458 | set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] |
| 459 | if {$rc} { |
| 460 | error [string range $msg 4 end] |
| 461 | } |
| 462 | set STMT $msg |
| 463 | |
| 464 | # Finalize statement |
| 465 | set rc [sqlite3_finalize $STMT] |
| 466 | if {$rc!="SQLITE_OK"} { |
| 467 | error [sqlite3_errmsg $DB2] |
| 468 | } |
| 469 | set STMT 0 |
| 470 | |
| 471 | # Close database |
| 472 | set rc [sqlite3_close $DB2] |
| 473 | if {$rc!="SQLITE_OK"} { |
| 474 | error [sqlite3_errmsg $DB2] |
| 475 | } |
| 476 | set DB2 0 |
| 477 | } -cleanup { |
| 478 | if {$STMT!="0"} { |
| 479 | sqlite3_finalize $STMT |
| 480 | } |
| 481 | if {$DB2!="0"} { |
| 482 | set rc [sqlite3_close $DB2] |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | # Test handling of malloc() failures in sqlite3_errmsg16(). |
| 487 | # |
| 488 | do_malloc_test 18 -tclbody { |
| 489 | catch { |
| 490 | db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" |
| 491 | } msg |
| 492 | if {$msg=="out of memory"} {error $msg} |
| 493 | set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] |
| 494 | binary scan $utf16 c* bytes |
| 495 | if {[llength $bytes]==0} { |
| 496 | error "out of memory" |
| 497 | } |
| 498 | } |
| 499 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 500 | # This test is aimed at coverage testing. Specificly, it is supposed to |
| 501 | # cause a malloc() only used when converting between the two utf-16 |
| 502 | # encodings to fail (i.e. little-endian->big-endian). It only actually |
| 503 | # hits this malloc() on little-endian hosts. |
| 504 | # |
| 505 | set static_string "\x00h\x00e\x00l\x00l\x00o" |
| 506 | for {set l 0} {$l<10} {incr l} { |
| 507 | append static_string $static_string |
| 508 | } |
| 509 | append static_string "\x00\x00" |
| 510 | do_malloc_test 19 -tclprep { |
| 511 | execsql { |
| 512 | PRAGMA encoding = "UTF16be"; |
| 513 | CREATE TABLE abc(a, b, c); |
| 514 | } |
| 515 | } -tclbody { |
| 516 | unset -nocomplain ::STMT |
| 517 | set r [catch { |
| 518 | set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY] |
| 519 | sqlite3_bind_text16 -static $::STMT 1 $static_string 112 |
| 520 | } msg] |
| 521 | if {$r} {error [string range $msg 4 end]} |
| 522 | set msg |
| 523 | } -cleanup { |
| 524 | if {[info exists ::STMT]} { |
| 525 | sqlite3_finalize $::STMT |
| 526 | } |
| 527 | } |
| 528 | unset static_string |
| 529 | |
drh | 6103fe9 | 2006-04-05 11:57:37 +0000 | [diff] [blame] | 530 | # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even |
| 531 | # when the malloc failure occurs within the nested parse. |
| 532 | # |
| 533 | do_malloc_test 20 -tclprep { |
| 534 | db close |
| 535 | file delete -force test2.db test2.db-journal |
| 536 | sqlite3 db test2.db |
| 537 | db eval {CREATE TABLE t1(x);} |
| 538 | db close |
| 539 | } -tclbody { |
| 540 | if {[catch {sqlite3 db test.db}]} { |
| 541 | error "out of memory" |
| 542 | } |
| 543 | } -sqlbody { |
| 544 | ATTACH DATABASE 'test2.db' AS t2; |
| 545 | SELECT * FROM t1; |
| 546 | DETACH DATABASE t2; |
| 547 | } |
| 548 | |
danielk1977 | b5584c0 | 2007-03-30 07:10:50 +0000 | [diff] [blame^] | 549 | # Test malloc failure whilst installing a foreign key. |
danielk1977 | 69b637b | 2007-03-29 17:07:52 +0000 | [diff] [blame] | 550 | # |
| 551 | do_malloc_test 21 -sqlbody { |
| 552 | CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b)) |
| 553 | } |
| 554 | |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 555 | # Ensure that no file descriptors were leaked. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 556 | do_test malloc-99.X { |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 557 | catch {db close} |
| 558 | set sqlite_open_file_count |
| 559 | } {0} |
| 560 | |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 561 | puts open-file-count=$sqlite_open_file_count |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 562 | sqlite_malloc_fail 0 |
| 563 | finish_test |