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