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 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 17 | # $Id: malloc.test,v 1.20 2005/01/13 13:35:58 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]==""} { |
drh | c89b91b | 2005-01-03 01:32:59 +0000 | [diff] [blame] | 25 | puts "Skipping malloc tests: not compiled with -DSQLITE_DEBUG..." |
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} { |
| 55 | array set ::mallocopts $args |
| 56 | |
| 57 | set ::go 1 |
| 58 | for {set ::n 1} {$::go} {incr ::n} { |
| 59 | |
| 60 | do_test malloc-$tn.$::n { |
| 61 | |
| 62 | sqlite_malloc_fail 0 |
| 63 | catch {db close} |
| 64 | catch {file delete -force test.db} |
| 65 | catch {file delete -force test.db-journal} |
| 66 | catch {file delete -force test2.db} |
| 67 | catch {file delete -force test2.db-journal} |
| 68 | set ::DB [sqlite3 db test.db] |
| 69 | |
| 70 | if {[info exists ::mallocopts(-tclprep)]} { |
| 71 | eval $::mallocopts(-tclprep) |
| 72 | } |
| 73 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 74 | execsql $::mallocopts(-sqlprep) |
| 75 | } |
| 76 | |
| 77 | sqlite_malloc_fail $::n |
| 78 | set ::mallocbody {} |
| 79 | if {[info exists ::mallocopts(-tclbody)]} { |
| 80 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 81 | } |
| 82 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 83 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 84 | } |
| 85 | |
| 86 | set v [catch $::mallocbody msg] |
| 87 | |
| 88 | set leftover [lindex [sqlite_malloc_stat] 2] |
| 89 | if {$leftover>0} { |
| 90 | if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"} |
| 91 | set ::go 0 |
| 92 | set v {1 1} |
| 93 | } else { |
| 94 | set v2 [expr {$msg=="" || $msg=="out of memory"}] |
| 95 | if {!$v2} {puts "\nError message returned: $msg"} |
| 96 | lappend v $v2 |
| 97 | } |
| 98 | } {1 1} |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 99 | |
| 100 | if {[info exists ::mallocopts(-cleanup)]} { |
| 101 | catch $::mallocopts(-cleanup) |
| 102 | } |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 103 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 104 | unset ::mallocopts |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 105 | } |
| 106 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 107 | do_malloc_test 1 -tclprep { |
| 108 | db close |
| 109 | } -tclbody { |
| 110 | if {[catch {sqlite3 db test.db}]} { |
| 111 | error "out of memory" |
| 112 | } |
| 113 | } -sqlbody { |
| 114 | CREATE TABLE t1( |
| 115 | a int, b float, c double, d text, e varchar(20), |
| 116 | primary key(a,b,c) |
| 117 | ); |
| 118 | CREATE INDEX i1 ON t1(a,b); |
| 119 | INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there'); |
| 120 | INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); |
| 121 | SELECT * FROM t1; |
| 122 | SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; |
| 123 | DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); |
| 124 | SELECT count(*) FROM t1; |
| 125 | } |
drh | d400728 | 2001-04-12 23:21:58 +0000 | [diff] [blame] | 126 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 127 | # Ensure that no file descriptors were leaked. |
| 128 | do_test malloc-1.X { |
| 129 | catch {db close} |
| 130 | set sqlite_open_file_count |
| 131 | } {0} |
| 132 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 133 | do_malloc_test 2 -sqlbody { |
| 134 | CREATE TABLE t1(a int, b int, c int); |
| 135 | CREATE INDEX i1 ON t1(a,b); |
| 136 | INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); |
| 137 | INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); |
| 138 | INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); |
| 139 | INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); |
| 140 | INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); |
| 141 | INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); |
| 142 | SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; |
| 143 | UPDATE t1 SET b=b||b||b||b; |
| 144 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 145 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 146 | INSERT INTO t1 SELECT * FROM t1 |
| 147 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 148 | DELETE FROM t1 WHERE a>=10; |
| 149 | DROP INDEX i1; |
| 150 | DELETE FROM t1; |
| 151 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 152 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 153 | # Ensure that no file descriptors were leaked. |
| 154 | do_test malloc-2.X { |
| 155 | catch {db close} |
| 156 | set sqlite_open_file_count |
| 157 | } {0} |
| 158 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 159 | do_malloc_test 3 -sqlbody { |
| 160 | BEGIN TRANSACTION; |
| 161 | CREATE TABLE t1(a int, b int, c int); |
| 162 | CREATE INDEX i1 ON t1(a,b); |
| 163 | INSERT INTO t1 VALUES(1,1,99); |
| 164 | INSERT INTO t1 VALUES(2,4,98); |
| 165 | INSERT INTO t1 VALUES(3,9,97); |
| 166 | INSERT INTO t1 VALUES(4,16,96); |
| 167 | INSERT INTO t1 VALUES(5,25,95); |
| 168 | INSERT INTO t1 VALUES(6,36,94); |
| 169 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 170 | DELETE FROM t1 WHERE a>=10; |
| 171 | DROP INDEX i1; |
| 172 | DELETE FROM t1; |
| 173 | ROLLBACK; |
| 174 | } |
| 175 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 176 | |
| 177 | # Ensure that no file descriptors were leaked. |
| 178 | do_test malloc-3.X { |
| 179 | catch {db close} |
| 180 | set sqlite_open_file_count |
| 181 | } {0} |
| 182 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 183 | do_malloc_test 4 -sqlbody { |
| 184 | BEGIN TRANSACTION; |
| 185 | CREATE TABLE t1(a int, b int, c int); |
| 186 | CREATE INDEX i1 ON t1(a,b); |
| 187 | INSERT INTO t1 VALUES(1,1,99); |
| 188 | INSERT INTO t1 VALUES(2,4,98); |
| 189 | INSERT INTO t1 VALUES(3,9,97); |
| 190 | INSERT INTO t1 VALUES(4,16,96); |
| 191 | INSERT INTO t1 VALUES(5,25,95); |
| 192 | INSERT INTO t1 VALUES(6,36,94); |
| 193 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 194 | INSERT INTO t1 SELECT * FROM t1 |
| 195 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 196 | DROP INDEX i1; |
| 197 | DELETE FROM t1; |
| 198 | COMMIT; |
| 199 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 200 | |
| 201 | # Ensure that no file descriptors were leaked. |
| 202 | do_test malloc-4.X { |
| 203 | catch {db close} |
| 204 | set sqlite_open_file_count |
| 205 | } {0} |
| 206 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 207 | do_malloc_test 5 -sqlbody { |
| 208 | BEGIN TRANSACTION; |
| 209 | CREATE TABLE t1(a,b); |
| 210 | CREATE TABLE t2(x,y); |
| 211 | CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN |
| 212 | INSERT INTO t2(x,y) VALUES(new.rowid,1); |
| 213 | END; |
| 214 | INSERT INTO t1(a,b) VALUES(2,3); |
| 215 | COMMIT; |
| 216 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 217 | |
| 218 | # Ensure that no file descriptors were leaked. |
| 219 | do_test malloc-5.X { |
| 220 | catch {db close} |
| 221 | set sqlite_open_file_count |
| 222 | } {0} |
| 223 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 224 | do_malloc_test 6 -sqlprep { |
| 225 | BEGIN TRANSACTION; |
| 226 | CREATE TABLE t1(a); |
| 227 | INSERT INTO t1 VALUES(1); |
| 228 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 229 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 230 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 231 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 232 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 233 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 234 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 235 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 236 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 237 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 238 | DELETE FROM t1 where rowid%5 = 0; |
| 239 | COMMIT; |
| 240 | } -sqlbody { |
| 241 | VACUUM; |
| 242 | } |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 243 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 244 | do_malloc_test 7 -sqlprep { |
| 245 | CREATE TABLE t1(a, b); |
| 246 | INSERT INTO t1 VALUES(1, 2); |
| 247 | INSERT INTO t1 VALUES(3, 4); |
| 248 | INSERT INTO t1 VALUES(5, 6); |
| 249 | INSERT INTO t1 VALUES(7, randstr(1200,1200)); |
| 250 | } -sqlbody { |
| 251 | SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; |
| 252 | SELECT a FROM t1 WHERE a<6 ORDER BY a; |
| 253 | SELECT b FROM t1 WHERE a>6; |
| 254 | } |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 255 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 256 | # This block is designed to test that some malloc failures that may |
| 257 | # occur in vdbeapi.c. Specifically, if a malloc failure that occurs |
| 258 | # when converting UTF-16 text to integers and real numbers is handled |
| 259 | # correctly. |
| 260 | # |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 261 | # This is done by retrieving a string from the database engine and |
| 262 | # manipulating it using the sqlite3_column_*** APIs. This doesn't |
| 263 | # actually return an error to the user when a malloc() fails.. That |
| 264 | # could be viewed as a bug. |
| 265 | # |
| 266 | # These tests only run if UTF-16 support is compiled in. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 267 | # |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame^] | 268 | if {$::sqlite_options(utf16)} { |
| 269 | do_malloc_test 8 -tclprep { |
| 270 | set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" |
| 271 | set ::STMT [sqlite3_prepare $::DB $sql -1 X] |
| 272 | sqlite3_step $::STMT |
| 273 | if { $::tcl_platform(byteOrder)=="littleEndian" } { |
| 274 | set ::bomstr "\xFF\xFE" |
| 275 | } else { |
| 276 | set ::bomstr "\xFE\xFF" |
| 277 | } |
| 278 | append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] |
| 279 | } -tclbody { |
| 280 | sqlite3_column_text16 $::STMT 0 |
| 281 | sqlite3_column_int $::STMT 0 |
| 282 | sqlite3_column_text16 $::STMT 1 |
| 283 | sqlite3_column_double $::STMT 1 |
| 284 | sqlite3_reset $::STMT |
| 285 | sqlite3_bind_text16 $::STMT 1 $::bomstr 60 |
| 286 | catch {sqlite3_finalize $::STMT} |
| 287 | if {[lindex [sqlite_malloc_stat] 2]<=0} { |
| 288 | error "out of memory" |
| 289 | } |
| 290 | } -cleanup { |
| 291 | sqlite3_finalize $::STMT |
| 292 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 293 | } |
| 294 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 295 | # This block tests that malloc() failures that occur whilst commiting |
| 296 | # a multi-file transaction are handled correctly. |
| 297 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 298 | do_malloc_test 9 -sqlprep { |
| 299 | ATTACH 'test2.db' as test2; |
| 300 | CREATE TABLE abc1(a, b, c); |
| 301 | CREATE TABLE test2.abc2(a, b, c); |
| 302 | } -sqlbody { |
| 303 | BEGIN; |
| 304 | INSERT INTO abc1 VALUES(1, 2, 3); |
| 305 | INSERT INTO abc2 VALUES(1, 2, 3); |
| 306 | COMMIT; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 309 | # This block tests malloc() failures that occur while opening a |
| 310 | # connection to a database. |
| 311 | do_malloc_test 10 -sqlprep { |
| 312 | CREATE TABLE abc(a, b, c); |
| 313 | } -tclbody { |
| 314 | set ::DB [sqlite3 db2 test.db] |
| 315 | db2 eval {SELECT * FROM sqlite_master} |
| 316 | db2 close |
| 317 | } |
| 318 | |
| 319 | # This block tests malloc() failures that occur within calls to |
| 320 | # sqlite3_create_function(). |
| 321 | do_malloc_test 11 -tclbody { |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 322 | set rc [sqlite3_create_function $::DB] |
| 323 | if {[string match $rc SQLITE_NOMEM]} { |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 324 | error "out of memory" |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | do_malloc_test 12 -tclbody { |
| 329 | set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] |
| 330 | append sql16 "\00\00" |
| 331 | set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY] |
| 332 | sqlite3_finalize $::STMT |
| 333 | } |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 334 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 335 | # Test malloc errors when replaying two hot journals from a 2-file |
| 336 | # transaction. This test only runs on UNIX. |
| 337 | if {$tcl_platform(platform)=="unix"} { |
| 338 | do_malloc_test 13 -tclprep { |
| 339 | set rc [crashsql 1 test2.db { |
| 340 | ATTACH 'test2.db' as aux; |
| 341 | PRAGMA cache_size = 10; |
| 342 | BEGIN; |
| 343 | CREATE TABLE aux.t2(a, b, c); |
| 344 | CREATE TABLE t1(a, b, c); |
| 345 | COMMIT; |
| 346 | }] |
| 347 | if {$rc!="1 {child process exited abnormally}"} { |
| 348 | error "Wrong error message: $rc" |
| 349 | } |
| 350 | } -sqlbody { |
| 351 | ATTACH 'test2.db' as aux; |
| 352 | SELECT * FROM t1; |
| 353 | SELECT * FROM t2; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | do_malloc_test 14 -tclprep { |
| 358 | catch {db close} |
| 359 | sqlite3 db2 test2.db |
| 360 | db2 eval { |
| 361 | PRAGMA synchronous = 0; |
| 362 | CREATE TABLE t1(a, b); |
| 363 | INSERT INTO t1 VALUES(1, 2); |
| 364 | BEGIN; |
| 365 | INSERT INTO t1 VALUES(3, 4); |
| 366 | } |
| 367 | file copy -force test2.db test.db |
| 368 | file copy -force test2.db-journal test.db-journal |
| 369 | db2 close |
| 370 | } -tclbody { |
| 371 | sqlite3 db test.db |
| 372 | db eval { |
| 373 | SELECT * FROM t1; |
| 374 | } |
| 375 | } |
| 376 | |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 377 | # Ensure that no file descriptors were leaked. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 378 | do_test malloc-99.X { |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 379 | catch {db close} |
| 380 | set sqlite_open_file_count |
| 381 | } {0} |
| 382 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 383 | sqlite_malloc_fail 0 |
| 384 | finish_test |