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# 12# This file attempts to check the behavior of the SQLite library in 13# an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, 14# the SQLite library accepts a special command (sqlite3_memdebug_fail N C) 15# which causes the N-th malloc to fail. This special feature is used 16# to see what happens in the library if a malloc were to really fail 17# due to an out-of-memory situation. 18# 19# $Id: malloc.test,v 1.65 2008/09/05 05:02:47 danielk1977 Exp $ 20 21set testdir [file dirname $argv0] 22source $testdir/tester.tcl 23 24 25# Only run these tests if memory debugging is turned on. 26# 27source $testdir/malloc_common.tcl 28if {!$MEMDEBUG} { 29 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." 30 finish_test 31 return 32} 33 34# Do a couple of memory dumps just to exercise the memory dump logic 35# that that we can say that we have. 36# 37puts stderr "This is a test. Ignore the error that follows:" 38sqlite3_memdebug_dump $testdir 39puts "Memory dump to file memdump.txt..." 40sqlite3_memdebug_dump memdump.txt 41 42ifcapable bloblit&&subquery { 43 do_malloc_test 1 -tclprep { 44 db close 45 } -tclbody { 46 if {[catch {sqlite3 db test.db}]} { 47 error "out of memory" 48 } 49 sqlite3_extended_result_codes db 1 50 } -sqlbody { 51 DROP TABLE IF EXISTS t1; 52 CREATE TABLE t1( 53 a int, b float, c double, d text, e varchar(20), 54 primary key(a,b,c) 55 ); 56 CREATE INDEX i1 ON t1(a,b); 57 INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); 58 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); 59 SELECT * FROM t1; 60 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; 61 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); 62 SELECT count(*), group_concat(e) FROM t1; 63 SELECT b FROM t1 ORDER BY 1 COLLATE nocase; 64 } 65} 66 67# Ensure that no file descriptors were leaked. 68do_test malloc-1.X { 69 catch {db close} 70 set sqlite_open_file_count 71} {0} 72 73ifcapable subquery { 74 do_malloc_test 2 -sqlbody { 75 CREATE TABLE t1(a int, b int default 'abc', c int default 1); 76 CREATE INDEX i1 ON t1(a,b); 77 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); 78 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); 79 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); 80 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); 81 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); 82 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); 83 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; 84 UPDATE t1 SET b=b||b||b||b; 85 UPDATE t1 SET b=a WHERE a in (10,12,22); 86 INSERT INTO t1(c,b,a) VALUES(20,10,5); 87 INSERT INTO t1 SELECT * FROM t1 88 WHERE a IN (SELECT a FROM t1 WHERE a<10); 89 DELETE FROM t1 WHERE a>=10; 90 DROP INDEX i1; 91 DELETE FROM t1; 92 } 93} 94 95# Ensure that no file descriptors were leaked. 96do_test malloc-2.X { 97 catch {db close} 98 set sqlite_open_file_count 99} {0} 100 101do_malloc_test 3 -sqlbody { 102 BEGIN TRANSACTION; 103 CREATE TABLE t1(a int, b int, c int); 104 CREATE INDEX i1 ON t1(a,b); 105 INSERT INTO t1 VALUES(1,1,99); 106 INSERT INTO t1 VALUES(2,4,98); 107 INSERT INTO t1 VALUES(3,9,97); 108 INSERT INTO t1 VALUES(4,16,96); 109 INSERT INTO t1 VALUES(5,25,95); 110 INSERT INTO t1 VALUES(6,36,94); 111 INSERT INTO t1(c,b,a) VALUES(20,10,5); 112 DELETE FROM t1 WHERE a>=10; 113 DROP INDEX i1; 114 DELETE FROM t1; 115 ROLLBACK; 116} 117 118 119# Ensure that no file descriptors were leaked. 120do_test malloc-3.X { 121 catch {db close} 122 set sqlite_open_file_count 123} {0} 124 125ifcapable subquery { 126 do_malloc_test 4 -sqlbody { 127 BEGIN TRANSACTION; 128 CREATE TABLE t1(a int, b int, c int); 129 CREATE INDEX i1 ON t1(a,b); 130 INSERT INTO t1 VALUES(1,1,99); 131 INSERT INTO t1 VALUES(2,4,98); 132 INSERT INTO t1 VALUES(3,9,97); 133 INSERT INTO t1 VALUES(4,16,96); 134 INSERT INTO t1 VALUES(5,25,95); 135 INSERT INTO t1 VALUES(6,36,94); 136 UPDATE t1 SET b=a WHERE a in (10,12,22); 137 INSERT INTO t1 SELECT * FROM t1 138 WHERE a IN (SELECT a FROM t1 WHERE a<10); 139 DROP INDEX i1; 140 DELETE FROM t1; 141 COMMIT; 142 } 143} 144 145# Ensure that no file descriptors were leaked. 146do_test malloc-4.X { 147 catch {db close} 148 set sqlite_open_file_count 149} {0} 150 151ifcapable trigger { 152 do_malloc_test 5 -sqlbody { 153 BEGIN TRANSACTION; 154 CREATE TABLE t1(a,b); 155 CREATE TABLE t2(x,y); 156 CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN 157 INSERT INTO t2(x,y) VALUES(new.rowid,1); 158 INSERT INTO t2(x,y) SELECT * FROM t2; 159 INSERT INTO t2 SELECT * FROM t2; 160 UPDATE t2 SET y=y+1 WHERE x=new.rowid; 161 SELECT 123; 162 DELETE FROM t2 WHERE x=new.rowid; 163 END; 164 INSERT INTO t1(a,b) VALUES(2,3); 165 COMMIT; 166 } 167} 168 169# Ensure that no file descriptors were leaked. 170do_test malloc-5.X { 171 catch {db close} 172 set sqlite_open_file_count 173} {0} 174 175ifcapable vacuum { 176 do_malloc_test 6 -sqlprep { 177 BEGIN TRANSACTION; 178 CREATE TABLE t1(a); 179 INSERT INTO t1 VALUES(1); 180 INSERT INTO t1 SELECT a*2 FROM t1; 181 INSERT INTO t1 SELECT a*2 FROM t1; 182 INSERT INTO t1 SELECT a*2 FROM t1; 183 INSERT INTO t1 SELECT a*2 FROM t1; 184 INSERT INTO t1 SELECT a*2 FROM t1; 185 INSERT INTO t1 SELECT a*2 FROM t1; 186 INSERT INTO t1 SELECT a*2 FROM t1; 187 INSERT INTO t1 SELECT a*2 FROM t1; 188 INSERT INTO t1 SELECT a*2 FROM t1; 189 INSERT INTO t1 SELECT a*2 FROM t1; 190 DELETE FROM t1 where rowid%5 = 0; 191 COMMIT; 192 } -sqlbody { 193 VACUUM; 194 } 195} 196 197do_malloc_test 7 -sqlprep { 198 CREATE TABLE t1(a, b); 199 INSERT INTO t1 VALUES(1, 2); 200 INSERT INTO t1 VALUES(3, 4); 201 INSERT INTO t1 VALUES(5, 6); 202 INSERT INTO t1 VALUES(7, randstr(1200,1200)); 203} -sqlbody { 204 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; 205 SELECT a FROM t1 WHERE a<6 ORDER BY a; 206 SELECT b FROM t1 WHERE a>6; 207} 208 209# This block is designed to test that some malloc failures that may 210# occur in vdbeapi.c. Specifically, if a malloc failure that occurs 211# when converting UTF-16 text to integers and real numbers is handled 212# correctly. 213# 214# This is done by retrieving a string from the database engine and 215# manipulating it using the sqlite3_column_*** APIs. This doesn't 216# actually return an error to the user when a malloc() fails.. That 217# could be viewed as a bug. 218# 219# These tests only run if UTF-16 support is compiled in. 220# 221ifcapable utf16 { 222 set ::STMT {} 223 do_malloc_test 8 -tclprep { 224 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" 225 set ::STMT [sqlite3_prepare db $sql -1 X] 226 sqlite3_step $::STMT 227 if { $::tcl_platform(byteOrder)=="littleEndian" } { 228 set ::bomstr "\xFF\xFE" 229 } else { 230 set ::bomstr "\xFE\xFF" 231 } 232 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] 233 } -tclbody { 234 sqlite3_column_text16 $::STMT 0 235 sqlite3_column_int $::STMT 0 236 sqlite3_column_text16 $::STMT 1 237 sqlite3_column_double $::STMT 1 238 set rc [sqlite3_reset $::STMT] 239 if {$rc eq "SQLITE_NOMEM"} {error "out of memory"} 240 sqlite3_bind_text16 $::STMT 1 $::bomstr 60 241 #catch {sqlite3_finalize $::STMT} 242 #if {[lindex [sqlite_malloc_stat] 2]<=0} { 243 # error "out of memory" 244 #} 245 } -cleanup { 246 if {$::STMT!=""} { 247 sqlite3_finalize $::STMT 248 set ::STMT {} 249 } 250 } 251} 252 253# This block tests that malloc() failures that occur whilst commiting 254# a multi-file transaction are handled correctly. 255# 256do_malloc_test 9 -sqlprep { 257 ATTACH 'test2.db' as test2; 258 CREATE TABLE abc1(a, b, c); 259 CREATE TABLE test2.abc2(a, b, c); 260} -sqlbody { 261 BEGIN; 262 INSERT INTO abc1 VALUES(1, 2, 3); 263 INSERT INTO abc2 VALUES(1, 2, 3); 264 COMMIT; 265} 266 267# This block tests malloc() failures that occur while opening a 268# connection to a database. 269do_malloc_test 10 -tclprep { 270 catch {db2 close} 271 db close 272 file delete -force test.db test.db-journal 273 sqlite3 db test.db 274 sqlite3_extended_result_codes db 1 275 db eval {CREATE TABLE abc(a, b, c)} 276} -tclbody { 277 db close 278 sqlite3 db2 test.db 279 sqlite3_extended_result_codes db2 1 280 db2 eval {SELECT * FROM sqlite_master} 281 db2 close 282} 283 284# This block tests malloc() failures that occur within calls to 285# sqlite3_create_function(). 286do_malloc_test 11 -tclbody { 287 set rc [sqlite3_create_function db] 288 if {[string match $rc SQLITE_OK]} { 289 set rc [sqlite3_create_aggregate db] 290 } 291 if {[string match $rc SQLITE_NOMEM]} { 292 error "out of memory" 293 } 294} 295 296do_malloc_test 12 -tclbody { 297 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] 298 append sql16 "\00\00" 299 set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY] 300 sqlite3_finalize $::STMT 301} 302 303# Test malloc errors when replaying two hot journals from a 2-file 304# transaction. 305ifcapable crashtest&&attach { 306 do_malloc_test 13 -tclprep { 307 set rc [crashsql -delay 1 -file test2.db { 308 ATTACH 'test2.db' as aux; 309 PRAGMA cache_size = 10; 310 BEGIN; 311 CREATE TABLE aux.t2(a, b, c); 312 CREATE TABLE t1(a, b, c); 313 COMMIT; 314 }] 315 if {$rc!="1 {child process exited abnormally}"} { 316 error "Wrong error message: $rc" 317 } 318 } -tclbody { 319 db eval {ATTACH 'test2.db' as aux;} 320 set rc [catch {db eval { 321 SELECT * FROM t1; 322 SELECT * FROM t2; 323 }} err] 324 if {$rc && $err!="no such table: t1"} { 325 error $err 326 } 327 } 328} 329 330if {$tcl_platform(platform)!="windows"} { 331 do_malloc_test 14 -tclprep { 332 catch {db close} 333 sqlite3 db2 test2.db 334 sqlite3_extended_result_codes db2 1 335 db2 eval { 336 PRAGMA synchronous = 0; 337 CREATE TABLE t1(a, b); 338 INSERT INTO t1 VALUES(1, 2); 339 BEGIN; 340 INSERT INTO t1 VALUES(3, 4); 341 } 342 copy_file test2.db test.db 343 copy_file test2.db-journal test.db-journal 344 db2 close 345 } -tclbody { 346 sqlite3 db test.db 347 sqlite3_extended_result_codes db 1 348 db eval { 349 SELECT * FROM t1; 350 } 351 } 352} 353 354proc string_compare {a b} { 355 return [string compare $a $b] 356} 357 358# Test for malloc() failures in sqlite3_create_collation() and 359# sqlite3_create_collation16(). 360# 361ifcapable utf16 { 362 do_malloc_test 15 -start 4 -tclbody { 363 db collate string_compare string_compare 364 if {[catch {add_test_collate db 1 1 1} msg]} { 365 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} 366 error $msg 367 } 368 369 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 370 db complete {-- Useful comment} 371 372 execsql { 373 CREATE TABLE t1(a, b COLLATE string_compare); 374 INSERT INTO t1 VALUES(10, 'string'); 375 INSERT INTO t1 VALUES(10, 'string2'); 376 } 377 } 378} 379 380# Also test sqlite3_complete(). There are (currently) no malloc() 381# calls in this function, but test anyway against future changes. 382# 383do_malloc_test 16 -tclbody { 384 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 385 db complete {-- Useful comment} 386 db eval { 387 SELECT * FROM sqlite_master; 388 } 389} 390 391# Test handling of malloc() failures in sqlite3_open16(). 392# 393ifcapable utf16 { 394 do_malloc_test 17 -tclbody { 395 set DB2 0 396 set STMT 0 397 398 # open database using sqlite3_open16() 399 set filename [encoding convertto unicode test.db] 400 append filename "\x00\x00" 401 set DB2 [sqlite3_open16 $filename -unused] 402 if {0==$DB2} { 403 error "out of memory" 404 } 405 sqlite3_extended_result_codes $DB2 1 406 407 # Prepare statement 408 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] 409 if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} { 410 error "out of memory" 411 } 412 if {[regexp ".*automatic extension loading.*" [sqlite3_errmsg $DB2]]} { 413 error "out of memory" 414 } 415 if {$rc} { 416 error [string range $msg 4 end] 417 } 418 set STMT $msg 419 420 # Finalize statement 421 set rc [sqlite3_finalize $STMT] 422 if {$rc!="SQLITE_OK"} { 423 error [sqlite3_errmsg $DB2] 424 } 425 set STMT 0 426 427 # Close database 428 set rc [sqlite3_close $DB2] 429 if {$rc!="SQLITE_OK"} { 430 error [sqlite3_errmsg $DB2] 431 } 432 set DB2 0 433 } -cleanup { 434 if {$STMT!="0"} { 435 sqlite3_finalize $STMT 436 } 437 if {$DB2!="0"} { 438 set rc [sqlite3_close $DB2] 439 } 440 } 441} 442 443# Test handling of malloc() failures in sqlite3_errmsg16(). 444# 445ifcapable utf16 { 446 do_malloc_test 18 -tclprep { 447 catch { 448 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" 449 } 450 } -tclbody { 451 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] 452 binary scan $utf16 c* bytes 453 if {[llength $bytes]==0} { 454 error "out of memory" 455 } 456 } 457} 458 459# This test is aimed at coverage testing. Specificly, it is supposed to 460# cause a malloc() only used when converting between the two utf-16 461# encodings to fail (i.e. little-endian->big-endian). It only actually 462# hits this malloc() on little-endian hosts. 463# 464set static_string "\x00h\x00e\x00l\x00l\x00o" 465for {set l 0} {$l<10} {incr l} { 466 append static_string $static_string 467} 468append static_string "\x00\x00" 469do_malloc_test 19 -tclprep { 470 execsql { 471 PRAGMA encoding = "UTF16be"; 472 CREATE TABLE abc(a, b, c); 473 } 474} -tclbody { 475 unset -nocomplain ::STMT 476 set r [catch { 477 set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY] 478 sqlite3_bind_text16 -static $::STMT 1 $static_string 112 479 } msg] 480 if {$r} {error [string range $msg 4 end]} 481 set msg 482} -cleanup { 483 if {[info exists ::STMT]} { 484 sqlite3_finalize $::STMT 485 } 486} 487unset static_string 488 489# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even 490# when the malloc failure occurs within the nested parse. 491# 492ifcapable attach { 493 do_malloc_test 20 -tclprep { 494 db close 495 file delete -force test2.db test2.db-journal 496 sqlite3 db test2.db 497 sqlite3_extended_result_codes db 1 498 db eval {CREATE TABLE t1(x);} 499 db close 500 } -tclbody { 501 if {[catch {sqlite3 db test.db}]} { 502 error "out of memory" 503 } 504 sqlite3_extended_result_codes db 1 505 } -sqlbody { 506 ATTACH DATABASE 'test2.db' AS t2; 507 SELECT * FROM t1; 508 DETACH DATABASE t2; 509 } 510} 511 512# Test malloc failure whilst installing a foreign key. 513# 514ifcapable foreignkey { 515 do_malloc_test 21 -sqlbody { 516 CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b)) 517 } 518} 519 520# Test malloc failure in an sqlite3_prepare_v2() call. 521# 522do_malloc_test 22 -tclbody { 523 set ::STMT "" 524 set r [catch { 525 set ::STMT [ 526 sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY 527 ] 528 } msg] 529 if {$r} {error [string range $msg 4 end]} 530} -cleanup { 531 if {$::STMT ne ""} { 532 sqlite3_finalize $::STMT 533 set ::STMT "" 534 } 535} 536 537ifcapable {pager_pragmas} { 538 # This tests a special case - that an error that occurs while the pager 539 # is trying to recover from error-state in exclusive-access mode works. 540 # 541 do_malloc_test 23 -tclprep { 542 db eval { 543 PRAGMA cache_size = 10; 544 PRAGMA locking_mode = exclusive; 545 BEGIN; 546 CREATE TABLE abc(a, b, c); 547 CREATE INDEX abc_i ON abc(a, b, c); 548 INSERT INTO abc 549 VALUES(randstr(100,100), randstr(100,100), randstr(100,100)); 550 INSERT INTO abc 551 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 552 INSERT INTO abc 553 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 554 INSERT INTO abc 555 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 556 INSERT INTO abc 557 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 558 INSERT INTO abc 559 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 560 COMMIT; 561 } 562 563 # This puts the pager into error state. 564 # 565 db eval BEGIN 566 db eval {UPDATE abc SET a = 0 WHERE oid%2} 567 set ::sqlite_io_error_pending 10 568 catch {db eval {ROLLBACK}} msg 569 570 } -sqlbody { 571 SELECT * FROM abc LIMIT 10; 572 } -cleanup { 573 set e [db eval {PRAGMA integrity_check}] 574 if {$e ne "ok"} {error $e} 575 } 576} 577 578ifcapable compound { 579 do_malloc_test 24 -sqlprep { 580 CREATE TABLE t1(a, b, c) 581 } -sqlbody { 582 SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1 583 } 584} 585 586ifcapable view&&trigger { 587 do_malloc_test 25 -sqlprep { 588 CREATE TABLE t1(a, b, c); 589 CREATE VIEW v1 AS SELECT * FROM t1; 590 CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END; 591 CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END; 592 CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END; 593 } -sqlbody { 594 DELETE FROM v1 WHERE a = 1; 595 INSERT INTO v1 VALUES(1, 2, 3); 596 UPDATE v1 SET a = 1 WHERE b = 2; 597 } 598} 599 600do_malloc_test 25 -sqlprep { 601 CREATE TABLE abc(a, b, c); 602 CREATE INDEX i1 ON abc(a, b); 603 INSERT INTO abc VALUES(1, 2, 3); 604 INSERT INTO abc VALUES(4, 5, 6); 605} -tclbody { 606 # For each UPDATE executed, the cursor used for the SELECT statement 607 # must be "saved". Because the cursor is open on an index, this requires 608 # a malloc() to allocate space to save the index key. This test case is 609 # aimed at testing the response of the library to a failure in that 610 # particular malloc() call. 611 db eval {SELECT a FROM abc ORDER BY a} { 612 db eval {UPDATE abc SET b = b - 1 WHERE a = $a} 613 } 614} 615 616# This test is designed to test a specific juncture in the sqlite code. 617# The database set up by -sqlprep script contains a single table B-Tree 618# of height 2. In the -tclbody script, the existing database connection 619# is closed and a new one opened and used to insert a new row into the 620# table B-Tree. By using a new connection, the outcome of a malloc() 621# failure while seeking to the right-hand side of the B-Tree to insert 622# a new record can be tested. 623# 624do_malloc_test 26 -sqlprep { 625 BEGIN; 626 CREATE TABLE t1(a, b); 627 INSERT INTO t1 VALUES(1, randomblob(210)); 628 INSERT INTO t1 VALUES(1, randomblob(210)); 629 INSERT INTO t1 VALUES(1, randomblob(210)); 630 INSERT INTO t1 VALUES(1, randomblob(210)); 631 INSERT INTO t1 VALUES(1, randomblob(210)); 632 COMMIT; 633} -tclbody { 634 db close 635 sqlite3 db test.db 636 db eval { INSERT INTO t1 VALUES(1, randomblob(210)) } 637} 638 639# Ensure that no file descriptors were leaked. 640do_test malloc-99.X { 641 catch {db close} 642 set sqlite_open_file_count 643} {0} 644 645puts open-file-count=$sqlite_open_file_count 646finish_test 647