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: malloc.test,v 1.36 2006/10/18 23:26:39 drh Exp $ 18 19set testdir [file dirname $argv0] 20source $testdir/tester.tcl 21 22# Only run these tests if memory debugging is turned on. 23# 24if {[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# 54proc 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 malloc-$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 126do_malloc_test 1 -tclprep { 127 db close 128} -tclbody { 129 if {[catch {sqlite3 db test.db}]} { 130 error "out of memory" 131 } 132} -sqlbody { 133 DROP TABLE IF EXISTS t1; 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); 139 INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); 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} 146 147# Ensure that no file descriptors were leaked. 148do_test malloc-1.X { 149 catch {db close} 150 set sqlite_open_file_count 151} {0} 152 153do_malloc_test 2 -sqlbody { 154 CREATE TABLE t1(a int, b int default 'abc', c int default 1); 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} 172 173# Ensure that no file descriptors were leaked. 174do_test malloc-2.X { 175 catch {db close} 176 set sqlite_open_file_count 177} {0} 178 179do_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 196 197# Ensure that no file descriptors were leaked. 198do_test malloc-3.X { 199 catch {db close} 200 set sqlite_open_file_count 201} {0} 202 203do_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} 220 221# Ensure that no file descriptors were leaked. 222do_test malloc-4.X { 223 catch {db close} 224 set sqlite_open_file_count 225} {0} 226 227do_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 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; 236 END; 237 INSERT INTO t1(a,b) VALUES(2,3); 238 COMMIT; 239} 240 241# Ensure that no file descriptors were leaked. 242do_test malloc-5.X { 243 catch {db close} 244 set sqlite_open_file_count 245} {0} 246 247do_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} 266 267do_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} 278 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# 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. 290# 291if {$::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 } 316} 317 318# This block tests that malloc() failures that occur whilst commiting 319# a multi-file transaction are handled correctly. 320# 321do_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; 330} 331 332# This block tests malloc() failures that occur while opening a 333# connection to a database. 334do_malloc_test 10 -sqlprep { 335 CREATE TABLE abc(a, b, c); 336} -tclbody { 337 sqlite3 db2 test.db 338 db2 eval {SELECT * FROM sqlite_master} 339 db2 close 340} 341 342# This block tests malloc() failures that occur within calls to 343# sqlite3_create_function(). 344do_malloc_test 11 -tclbody { 345 set rc [sqlite3_create_function $::DB] 346 if {[string match $rc SQLITE_NOMEM]} { 347 error "out of memory" 348 } 349} 350 351do_malloc_test 12 -tclbody { 352 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] 353 append sql16 "\00\00" 354 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY] 355 sqlite3_finalize $::STMT 356} 357 358# Test malloc errors when replaying two hot journals from a 2-file 359# transaction. 360ifcapable crashtest { 361 do_malloc_test 13 -tclprep { 362 set rc [crashsql 1 test2.db { 363 ATTACH 'test2.db' as aux; 364 PRAGMA cache_size = 10; 365 BEGIN; 366 CREATE TABLE aux.t2(a, b, c); 367 CREATE TABLE t1(a, b, c); 368 COMMIT; 369 }] 370 if {$rc!="1 {child process exited abnormally}"} { 371 error "Wrong error message: $rc" 372 } 373 } -tclbody { 374 db eval {ATTACH 'test2.db' as aux;} 375 set rc [catch {db eval { 376 SELECT * FROM t1; 377 SELECT * FROM t2; 378 }} err] 379 if {$rc && $err!="no such table: t1"} { 380 error $err 381 } 382 } 383} 384 385if {$tcl_platform(platform)!="windows"} { 386 do_malloc_test 14 -tclprep { 387 catch {db close} 388 sqlite3 db2 test2.db 389 db2 eval { 390 PRAGMA synchronous = 0; 391 CREATE TABLE t1(a, b); 392 INSERT INTO t1 VALUES(1, 2); 393 BEGIN; 394 INSERT INTO t1 VALUES(3, 4); 395 } 396 copy_file test2.db test.db 397 copy_file test2.db-journal test.db-journal 398 db2 close 399 } -tclbody { 400 sqlite3 db test.db 401 db eval { 402 SELECT * FROM t1; 403 } 404 } 405} 406 407proc string_compare {a b} { 408 return [string compare $a $b] 409} 410 411# Test for malloc() failures in sqlite3_create_collation() and 412# sqlite3_create_collation16(). 413# 414do_malloc_test 15 -tclbody { 415 db collate string_compare string_compare 416 if {[catch {add_test_collate $::DB 1 1 1} msg]} { 417 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} 418 error $msg 419 } 420 421 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 422 db complete {-- Useful comment} 423 424 execsql { 425 CREATE TABLE t1(a, b COLLATE string_compare); 426 INSERT INTO t1 VALUES(10, 'string'); 427 INSERT INTO t1 VALUES(10, 'string2'); 428 } 429} 430 431# Also test sqlite3_complete(). There are (currently) no malloc() 432# calls in this function, but test anyway against future changes. 433# 434do_malloc_test 16 -tclbody { 435 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 436 db complete {-- Useful comment} 437 db eval { 438 SELECT * FROM sqlite_master; 439 } 440} 441 442# Test handling of malloc() failures in sqlite3_open16(). 443# 444do_malloc_test 17 -tclbody { 445 set DB2 0 446 set STMT 0 447 448 # open database using sqlite3_open16() 449 set filename [encoding convertto unicode test.db] 450 append filename "\x00\x00" 451 set DB2 [sqlite3_open16 $filename -unused] 452 if {0==$DB2} { 453 error "out of memory" 454 } 455 456 # Prepare statement 457 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] 458 if {$rc} { 459 error [string range $msg 4 end] 460 } 461 set STMT $msg 462 463 # Finalize statement 464 set rc [sqlite3_finalize $STMT] 465 if {$rc!="SQLITE_OK"} { 466 error [sqlite3_errmsg $DB2] 467 } 468 set STMT 0 469 470 # Close database 471 set rc [sqlite3_close $DB2] 472 if {$rc!="SQLITE_OK"} { 473 error [sqlite3_errmsg $DB2] 474 } 475 set DB2 0 476} -cleanup { 477 if {$STMT!="0"} { 478 sqlite3_finalize $STMT 479 } 480 if {$DB2!="0"} { 481 set rc [sqlite3_close $DB2] 482 } 483} 484 485# Test handling of malloc() failures in sqlite3_errmsg16(). 486# 487do_malloc_test 18 -tclbody { 488 catch { 489 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" 490 } msg 491 if {$msg=="out of memory"} {error $msg} 492 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] 493 binary scan $utf16 c* bytes 494 if {[llength $bytes]==0} { 495 error "out of memory" 496 } 497} 498 499# This test is aimed at coverage testing. Specificly, it is supposed to 500# cause a malloc() only used when converting between the two utf-16 501# encodings to fail (i.e. little-endian->big-endian). It only actually 502# hits this malloc() on little-endian hosts. 503# 504set static_string "\x00h\x00e\x00l\x00l\x00o" 505for {set l 0} {$l<10} {incr l} { 506 append static_string $static_string 507} 508append static_string "\x00\x00" 509do_malloc_test 19 -tclprep { 510 execsql { 511 PRAGMA encoding = "UTF16be"; 512 CREATE TABLE abc(a, b, c); 513 } 514} -tclbody { 515 unset -nocomplain ::STMT 516 set r [catch { 517 set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY] 518 sqlite3_bind_text16 -static $::STMT 1 $static_string 112 519 } msg] 520 if {$r} {error [string range $msg 4 end]} 521 set msg 522} -cleanup { 523 if {[info exists ::STMT]} { 524 sqlite3_finalize $::STMT 525 } 526} 527unset static_string 528 529# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even 530# when the malloc failure occurs within the nested parse. 531# 532do_malloc_test 20 -tclprep { 533 db close 534 file delete -force test2.db test2.db-journal 535 sqlite3 db test2.db 536 db eval {CREATE TABLE t1(x);} 537 db close 538} -tclbody { 539 if {[catch {sqlite3 db test.db}]} { 540 error "out of memory" 541 } 542} -sqlbody { 543 ATTACH DATABASE 'test2.db' AS t2; 544 SELECT * FROM t1; 545 DETACH DATABASE t2; 546} 547 548 549# Ensure that no file descriptors were leaked. 550do_test malloc-99.X { 551 catch {db close} 552 set sqlite_open_file_count 553} {0} 554 555puts open-file-count=$sqlite_open_file_count 556sqlite_malloc_fail 0 557finish_test 558