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