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.44 2007/08/22 20:18:22 drh Exp $ 20 21set testdir [file dirname $argv0] 22source $testdir/tester.tcl 23 24# Only run these tests if memory debugging is turned on. 25# 26ifcapable !memdebug { 27 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." 28 finish_test 29 return 30} 31 32source $testdir/malloc_common.tcl 33 34do_malloc_test 1 -tclprep { 35 db close 36} -tclbody { 37 if {[catch {sqlite3 db test.db}]} { 38 error "out of memory" 39 } 40} -sqlbody { 41 DROP TABLE IF EXISTS t1; 42 CREATE TABLE t1( 43 a int, b float, c double, d text, e varchar(20), 44 primary key(a,b,c) 45 ); 46 CREATE INDEX i1 ON t1(a,b); 47 INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); 48 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); 49 SELECT * FROM t1; 50 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; 51 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); 52 SELECT count(*) FROM t1; 53} 54 55# Ensure that no file descriptors were leaked. 56do_test malloc-1.X { 57 catch {db close} 58 set sqlite_open_file_count 59} {0} 60 61do_malloc_test 2 -sqlbody { 62 CREATE TABLE t1(a int, b int default 'abc', c int default 1); 63 CREATE INDEX i1 ON t1(a,b); 64 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); 65 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); 66 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); 67 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); 68 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); 69 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); 70 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; 71 UPDATE t1 SET b=b||b||b||b; 72 UPDATE t1 SET b=a WHERE a in (10,12,22); 73 INSERT INTO t1(c,b,a) VALUES(20,10,5); 74 INSERT INTO t1 SELECT * FROM t1 75 WHERE a IN (SELECT a FROM t1 WHERE a<10); 76 DELETE FROM t1 WHERE a>=10; 77 DROP INDEX i1; 78 DELETE FROM t1; 79} 80 81# Ensure that no file descriptors were leaked. 82do_test malloc-2.X { 83 catch {db close} 84 set sqlite_open_file_count 85} {0} 86 87do_malloc_test 3 -sqlbody { 88 BEGIN TRANSACTION; 89 CREATE TABLE t1(a int, b int, c int); 90 CREATE INDEX i1 ON t1(a,b); 91 INSERT INTO t1 VALUES(1,1,99); 92 INSERT INTO t1 VALUES(2,4,98); 93 INSERT INTO t1 VALUES(3,9,97); 94 INSERT INTO t1 VALUES(4,16,96); 95 INSERT INTO t1 VALUES(5,25,95); 96 INSERT INTO t1 VALUES(6,36,94); 97 INSERT INTO t1(c,b,a) VALUES(20,10,5); 98 DELETE FROM t1 WHERE a>=10; 99 DROP INDEX i1; 100 DELETE FROM t1; 101 ROLLBACK; 102} 103 104 105# Ensure that no file descriptors were leaked. 106do_test malloc-3.X { 107 catch {db close} 108 set sqlite_open_file_count 109} {0} 110 111do_malloc_test 4 -sqlbody { 112 BEGIN TRANSACTION; 113 CREATE TABLE t1(a int, b int, c int); 114 CREATE INDEX i1 ON t1(a,b); 115 INSERT INTO t1 VALUES(1,1,99); 116 INSERT INTO t1 VALUES(2,4,98); 117 INSERT INTO t1 VALUES(3,9,97); 118 INSERT INTO t1 VALUES(4,16,96); 119 INSERT INTO t1 VALUES(5,25,95); 120 INSERT INTO t1 VALUES(6,36,94); 121 UPDATE t1 SET b=a WHERE a in (10,12,22); 122 INSERT INTO t1 SELECT * FROM t1 123 WHERE a IN (SELECT a FROM t1 WHERE a<10); 124 DROP INDEX i1; 125 DELETE FROM t1; 126 COMMIT; 127} 128 129# Ensure that no file descriptors were leaked. 130do_test malloc-4.X { 131 catch {db close} 132 set sqlite_open_file_count 133} {0} 134 135do_malloc_test 5 -sqlbody { 136 BEGIN TRANSACTION; 137 CREATE TABLE t1(a,b); 138 CREATE TABLE t2(x,y); 139 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN 140 INSERT INTO t2(x,y) VALUES(new.rowid,1); 141 INSERT INTO t2(x,y) SELECT * FROM t2; 142 INSERT INTO t2 SELECT * FROM t2; 143 UPDATE t2 SET y=y+1 WHERE x=new.rowid; 144 SELECT 123; 145 DELETE FROM t2 WHERE x=new.rowid; 146 END; 147 INSERT INTO t1(a,b) VALUES(2,3); 148 COMMIT; 149} 150 151# Ensure that no file descriptors were leaked. 152do_test malloc-5.X { 153 catch {db close} 154 set sqlite_open_file_count 155} {0} 156 157do_malloc_test 6 -sqlprep { 158 BEGIN TRANSACTION; 159 CREATE TABLE t1(a); 160 INSERT INTO t1 VALUES(1); 161 INSERT INTO t1 SELECT a*2 FROM t1; 162 INSERT INTO t1 SELECT a*2 FROM t1; 163 INSERT INTO t1 SELECT a*2 FROM t1; 164 INSERT INTO t1 SELECT a*2 FROM t1; 165 INSERT INTO t1 SELECT a*2 FROM t1; 166 INSERT INTO t1 SELECT a*2 FROM t1; 167 INSERT INTO t1 SELECT a*2 FROM t1; 168 INSERT INTO t1 SELECT a*2 FROM t1; 169 INSERT INTO t1 SELECT a*2 FROM t1; 170 INSERT INTO t1 SELECT a*2 FROM t1; 171 DELETE FROM t1 where rowid%5 = 0; 172 COMMIT; 173} -sqlbody { 174 VACUUM; 175} 176 177do_malloc_test 7 -sqlprep { 178 CREATE TABLE t1(a, b); 179 INSERT INTO t1 VALUES(1, 2); 180 INSERT INTO t1 VALUES(3, 4); 181 INSERT INTO t1 VALUES(5, 6); 182 INSERT INTO t1 VALUES(7, randstr(1200,1200)); 183} -sqlbody { 184 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; 185 SELECT a FROM t1 WHERE a<6 ORDER BY a; 186 SELECT b FROM t1 WHERE a>6; 187} 188 189# This block is designed to test that some malloc failures that may 190# occur in vdbeapi.c. Specifically, if a malloc failure that occurs 191# when converting UTF-16 text to integers and real numbers is handled 192# correctly. 193# 194# This is done by retrieving a string from the database engine and 195# manipulating it using the sqlite3_column_*** APIs. This doesn't 196# actually return an error to the user when a malloc() fails.. That 197# could be viewed as a bug. 198# 199# These tests only run if UTF-16 support is compiled in. 200# 201if {$::sqlite_options(utf16)} { 202 set ::STMT {} 203 do_malloc_test 8 -tclprep { 204 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" 205 set ::STMT [sqlite3_prepare db $sql -1 X] 206 sqlite3_step $::STMT 207 if { $::tcl_platform(byteOrder)=="littleEndian" } { 208 set ::bomstr "\xFF\xFE" 209 } else { 210 set ::bomstr "\xFE\xFF" 211 } 212 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] 213 } -tclbody { 214 sqlite3_column_text16 $::STMT 0 215 sqlite3_column_int $::STMT 0 216 sqlite3_column_text16 $::STMT 1 217 sqlite3_column_double $::STMT 1 218 sqlite3_reset $::STMT 219 sqlite3_bind_text16 $::STMT 1 $::bomstr 60 220 #catch {sqlite3_finalize $::STMT} 221 #if {[lindex [sqlite_malloc_stat] 2]<=0} { 222 # error "out of memory" 223 #} 224 } -cleanup { 225 if {$::STMT!=""} { 226 sqlite3_finalize $::STMT 227 set ::STMT {} 228 } 229 } 230} 231 232# This block tests that malloc() failures that occur whilst commiting 233# a multi-file transaction are handled correctly. 234# 235do_malloc_test 9 -sqlprep { 236 ATTACH 'test2.db' as test2; 237 CREATE TABLE abc1(a, b, c); 238 CREATE TABLE test2.abc2(a, b, c); 239} -sqlbody { 240 BEGIN; 241 INSERT INTO abc1 VALUES(1, 2, 3); 242 INSERT INTO abc2 VALUES(1, 2, 3); 243 COMMIT; 244} 245 246# This block tests malloc() failures that occur while opening a 247# connection to a database. 248do_malloc_test 10 -sqlprep { 249 CREATE TABLE abc(a, b, c); 250} -tclbody { 251 db close 252 sqlite3 db2 test.db 253 db2 eval {SELECT * FROM sqlite_master} 254 db2 close 255} 256 257# This block tests malloc() failures that occur within calls to 258# sqlite3_create_function(). 259do_malloc_test 11 -tclbody { 260 set rc [sqlite3_create_function db] 261 if {[string match $rc SQLITE_NOMEM]} { 262 error "out of memory" 263 } 264} 265 266do_malloc_test 12 -tclbody { 267 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] 268 append sql16 "\00\00" 269 set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY] 270 sqlite3_finalize $::STMT 271} 272 273# Test malloc errors when replaying two hot journals from a 2-file 274# transaction. 275ifcapable crashtest { 276 do_malloc_test 13 -tclprep { 277 set rc [crashsql -delay 1 -file test2.db { 278 ATTACH 'test2.db' as aux; 279 PRAGMA cache_size = 10; 280 BEGIN; 281 CREATE TABLE aux.t2(a, b, c); 282 CREATE TABLE t1(a, b, c); 283 COMMIT; 284 }] 285 if {$rc!="1 {child process exited abnormally}"} { 286 error "Wrong error message: $rc" 287 } 288 } -tclbody { 289 db eval {ATTACH 'test2.db' as aux;} 290 set rc [catch {db eval { 291 SELECT * FROM t1; 292 SELECT * FROM t2; 293 }} err] 294 if {$rc && $err!="no such table: t1"} { 295 error $err 296 } 297 } 298} 299 300if {$tcl_platform(platform)!="windows"} { 301 do_malloc_test 14 -tclprep { 302 catch {db close} 303 sqlite3 db2 test2.db 304 db2 eval { 305 PRAGMA synchronous = 0; 306 CREATE TABLE t1(a, b); 307 INSERT INTO t1 VALUES(1, 2); 308 BEGIN; 309 INSERT INTO t1 VALUES(3, 4); 310 } 311 copy_file test2.db test.db 312 copy_file test2.db-journal test.db-journal 313 db2 close 314 } -tclbody { 315 sqlite3 db test.db 316 db eval { 317 SELECT * FROM t1; 318 } 319 } 320} 321 322proc string_compare {a b} { 323 return [string compare $a $b] 324} 325 326# Test for malloc() failures in sqlite3_create_collation() and 327# sqlite3_create_collation16(). 328# 329do_malloc_test 15 -start 4 -tclbody { 330 db collate string_compare string_compare 331 if {[catch {add_test_collate db 1 1 1} msg]} { 332 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} 333 error $msg 334 } 335 336 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 337 db complete {-- Useful comment} 338 339 execsql { 340 CREATE TABLE t1(a, b COLLATE string_compare); 341 INSERT INTO t1 VALUES(10, 'string'); 342 INSERT INTO t1 VALUES(10, 'string2'); 343 } 344} 345 346# Also test sqlite3_complete(). There are (currently) no malloc() 347# calls in this function, but test anyway against future changes. 348# 349do_malloc_test 16 -tclbody { 350 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} 351 db complete {-- Useful comment} 352 db eval { 353 SELECT * FROM sqlite_master; 354 } 355} 356 357# Test handling of malloc() failures in sqlite3_open16(). 358# 359do_malloc_test 17 -tclbody { 360 set DB2 0 361 set STMT 0 362 363 # open database using sqlite3_open16() 364 set filename [encoding convertto unicode test.db] 365 append filename "\x00\x00" 366 set DB2 [sqlite3_open16 $filename -unused] 367 if {0==$DB2} { 368 error "out of memory" 369 } 370 371 # Prepare statement 372 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] 373 if {$rc} { 374 error [string range $msg 4 end] 375 } 376 set STMT $msg 377 378 # Finalize statement 379 set rc [sqlite3_finalize $STMT] 380 if {$rc!="SQLITE_OK"} { 381 error [sqlite3_errmsg $DB2] 382 } 383 set STMT 0 384 385 # Close database 386 set rc [sqlite3_close $DB2] 387 if {$rc!="SQLITE_OK"} { 388 error [sqlite3_errmsg $DB2] 389 } 390 set DB2 0 391} -cleanup { 392 if {$STMT!="0"} { 393 sqlite3_finalize $STMT 394 } 395 if {$DB2!="0"} { 396 set rc [sqlite3_close $DB2] 397 } 398} 399 400# Test handling of malloc() failures in sqlite3_errmsg16(). 401# 402do_malloc_test 18 -tclbody { 403 catch { 404 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" 405 } msg 406 if {$msg=="out of memory"} {error $msg} 407 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] 408 binary scan $utf16 c* bytes 409 if {[llength $bytes]==0} { 410 error "out of memory" 411 } 412} 413 414# This test is aimed at coverage testing. Specificly, it is supposed to 415# cause a malloc() only used when converting between the two utf-16 416# encodings to fail (i.e. little-endian->big-endian). It only actually 417# hits this malloc() on little-endian hosts. 418# 419set static_string "\x00h\x00e\x00l\x00l\x00o" 420for {set l 0} {$l<10} {incr l} { 421 append static_string $static_string 422} 423append static_string "\x00\x00" 424do_malloc_test 19 -tclprep { 425 execsql { 426 PRAGMA encoding = "UTF16be"; 427 CREATE TABLE abc(a, b, c); 428 } 429} -tclbody { 430 unset -nocomplain ::STMT 431 set r [catch { 432 set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY] 433 sqlite3_bind_text16 -static $::STMT 1 $static_string 112 434 } msg] 435 if {$r} {error [string range $msg 4 end]} 436 set msg 437} -cleanup { 438 if {[info exists ::STMT]} { 439 sqlite3_finalize $::STMT 440 } 441} 442unset static_string 443 444# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even 445# when the malloc failure occurs within the nested parse. 446# 447do_malloc_test 20 -tclprep { 448 db close 449 file delete -force test2.db test2.db-journal 450 sqlite3 db test2.db 451 db eval {CREATE TABLE t1(x);} 452 db close 453} -tclbody { 454 if {[catch {sqlite3 db test.db}]} { 455 error "out of memory" 456 } 457} -sqlbody { 458 ATTACH DATABASE 'test2.db' AS t2; 459 SELECT * FROM t1; 460 DETACH DATABASE t2; 461} 462 463# Test malloc failure whilst installing a foreign key. 464# 465do_malloc_test 21 -sqlbody { 466 CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b)) 467} 468 469# Test malloc failure in an sqlite3_prepare_v2() call. 470# 471do_malloc_test 22 -tclbody { 472 set ::STMT "" 473 set r [catch { 474 set ::STMT [ 475 sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY 476 ] 477 } msg] 478 if {$r} {error [string range $msg 4 end]} 479} -cleanup { 480 if {$::STMT ne ""} { 481 sqlite3_finalize $::STMT 482 set ::STMT "" 483 } 484} 485 486# Ensure that no file descriptors were leaked. 487do_test malloc-99.X { 488 catch {db close} 489 set sqlite_open_file_count 490} {0} 491 492puts open-file-count=$sqlite_open_file_count 493finish_test 494