blob: b9af7426c0b7e1c3668e75098fcef540b997d149 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhed7c8552001-04-11 14:29:21 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhed7c8552001-04-11 14:29:21 +00009#
10#***********************************************************************
drhed7c8552001-04-11 14:29:21 +000011#
drhf3a65f72007-08-22 20:18:21 +000012# 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#
danielk1977db340392009-01-16 16:40:14 +000019# $Id: malloc.test,v 1.74 2009/01/16 16:40:14 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000020
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
23
danielk1977d09414c2008-06-19 18:17:49 +000024
drhed7c8552001-04-11 14:29:21 +000025# Only run these tests if memory debugging is turned on.
26#
drheee4c8c2008-02-18 22:24:57 +000027source $testdir/malloc_common.tcl
28if {!$MEMDEBUG} {
danielk1977261919c2005-12-06 12:52:59 +000029 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed7c8552001-04-11 14:29:21 +000030 finish_test
31 return
32}
33
drhde4fcfd2008-01-19 23:50:26 +000034# 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
danielk19774152e672007-09-12 17:01:45 +000042ifcapable 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 }
danielk1977ae72d982007-10-03 08:46:44 +000049 sqlite3_extended_result_codes db 1
danielk19774152e672007-09-12 17:01:45 +000050 } -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);
drhade86482007-11-28 22:36:40 +000062 SELECT count(*), group_concat(e) FROM t1;
danielk197715cdbeb2008-01-23 15:44:51 +000063 SELECT b FROM t1 ORDER BY 1 COLLATE nocase;
danielk19774152e672007-09-12 17:01:45 +000064 }
65}
drhd4007282001-04-12 23:21:58 +000066
danielk1977b5548a82004-06-26 13:51:33 +000067# 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
danielk19774152e672007-09-12 17:01:45 +000073ifcapable 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}
drh6d4abfb2001-10-22 02:58:08 +000094
danielk1977b5548a82004-06-26 13:51:33 +000095# 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
danielk1977c08d4052005-01-13 13:35:57 +0000101do_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
danielk1977b5548a82004-06-26 13:51:33 +0000118
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
danielk19774152e672007-09-12 17:01:45 +0000125ifcapable 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}
danielk1977b5548a82004-06-26 13:51:33 +0000144
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
danielk19774152e672007-09-12 17:01:45 +0000151ifcapable trigger {
152 do_malloc_test 5 -sqlbody {
153 BEGIN TRANSACTION;
154 CREATE TABLE t1(a,b);
155 CREATE TABLE t2(x,y);
danielk1977932083c2007-11-16 14:55:46 +0000156 CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN
danielk19774152e672007-09-12 17:01:45 +0000157 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}
danielk1977b5548a82004-06-26 13:51:33 +0000168
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
danielk19774152e672007-09-12 17:01:45 +0000175ifcapable 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}
danielk197796fb0dd2004-06-30 09:49:22 +0000196
danielk1977c08d4052005-01-13 13:35:57 +0000197do_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;
drhf3a65f72007-08-22 20:18:21 +0000207}
danielk197701427a62005-01-11 13:02:33 +0000208
danielk1977b5402fb2005-01-12 07:15:04 +0000209# 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#
danielk19778b60e0f2005-01-12 09:10:39 +0000214# 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.
danielk1977b5402fb2005-01-12 07:15:04 +0000220#
danielk19774152e672007-09-12 17:01:45 +0000221ifcapable utf16 {
drhf3a65f72007-08-22 20:18:21 +0000222 set ::STMT {}
danielk1977c08d4052005-01-13 13:35:57 +0000223 do_malloc_test 8 -tclprep {
224 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
drhf3a65f72007-08-22 20:18:21 +0000225 set ::STMT [sqlite3_prepare db $sql -1 X]
danielk1977c08d4052005-01-13 13:35:57 +0000226 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
danielk1977a1644fd2007-08-29 12:31:25 +0000238 set rc [sqlite3_reset $::STMT]
239 if {$rc eq "SQLITE_NOMEM"} {error "out of memory"}
danielk1977c08d4052005-01-13 13:35:57 +0000240 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
drhf3a65f72007-08-22 20:18:21 +0000241 #catch {sqlite3_finalize $::STMT}
242 #if {[lindex [sqlite_malloc_stat] 2]<=0} {
243 # error "out of memory"
244 #}
danielk1977c08d4052005-01-13 13:35:57 +0000245 } -cleanup {
drhf3a65f72007-08-22 20:18:21 +0000246 if {$::STMT!=""} {
247 sqlite3_finalize $::STMT
248 set ::STMT {}
249 }
danielk1977c08d4052005-01-13 13:35:57 +0000250 }
danielk1977b5402fb2005-01-12 07:15:04 +0000251}
252
danielk19778b60e0f2005-01-12 09:10:39 +0000253# This block tests that malloc() failures that occur whilst commiting
254# a multi-file transaction are handled correctly.
255#
danielk19774397de52005-01-12 12:44:03 +0000256do_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;
danielk19778b60e0f2005-01-12 09:10:39 +0000265}
266
danielk19774397de52005-01-12 12:44:03 +0000267# This block tests malloc() failures that occur while opening a
268# connection to a database.
drhdc05efb2007-09-04 01:25:48 +0000269do_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
danielk1977ae72d982007-10-03 08:46:44 +0000274 sqlite3_extended_result_codes db 1
drhdc05efb2007-09-04 01:25:48 +0000275 db eval {CREATE TABLE abc(a, b, c)}
danielk19774397de52005-01-12 12:44:03 +0000276} -tclbody {
danielk1977c5859712007-03-26 12:26:27 +0000277 db close
danielk1977771151b2006-01-17 13:21:40 +0000278 sqlite3 db2 test.db
danielk1977ae72d982007-10-03 08:46:44 +0000279 sqlite3_extended_result_codes db2 1
danielk19774397de52005-01-12 12:44:03 +0000280 db2 eval {SELECT * FROM sqlite_master}
281 db2 close
drh93aed5a2008-01-16 17:46:38 +0000282}
danielk19774397de52005-01-12 12:44:03 +0000283
284# This block tests malloc() failures that occur within calls to
285# sqlite3_create_function().
drhf3a65f72007-08-22 20:18:21 +0000286do_malloc_test 11 -tclbody {
287 set rc [sqlite3_create_function db]
danielk1977fa18bec2007-09-03 11:04:22 +0000288 if {[string match $rc SQLITE_OK]} {
289 set rc [sqlite3_create_aggregate db]
290 }
danielk19772c336542005-01-13 02:14:23 +0000291 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000292 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"
drhf3a65f72007-08-22 20:18:21 +0000299 set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY]
danielk19774397de52005-01-12 12:44:03 +0000300 sqlite3_finalize $::STMT
danielk1977c5859712007-03-26 12:26:27 +0000301}
danielk19778b60e0f2005-01-12 09:10:39 +0000302
danielk1977aca790a2005-01-13 11:07:52 +0000303# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000304# transaction.
danielk19775a8f9372007-10-09 08:29:32 +0000305ifcapable crashtest&&attach {
danielk1977aca790a2005-01-13 11:07:52 +0000306 do_malloc_test 13 -tclprep {
danielk197759a33f92007-03-17 10:26:59 +0000307 set rc [crashsql -delay 1 -file test2.db {
danielk1977aca790a2005-01-13 11:07:52 +0000308 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 }
danielk1977950f0542006-01-18 05:51:57 +0000318 } -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 }
danielk1977aca790a2005-01-13 11:07:52 +0000327 }
328}
329
danielk197776b047d2005-01-19 03:52:54 +0000330if {$tcl_platform(platform)!="windows"} {
danielk19779a30cf62006-01-18 04:26:07 +0000331 do_malloc_test 14 -tclprep {
332 catch {db close}
333 sqlite3 db2 test2.db
danielk1977ae72d982007-10-03 08:46:44 +0000334 sqlite3_extended_result_codes db2 1
danielk19779a30cf62006-01-18 04:26:07 +0000335 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
danielk1977ae72d982007-10-03 08:46:44 +0000347 sqlite3_extended_result_codes db 1
danielk197798c21902008-09-23 16:41:29 +0000348
349 # If an out-of-memory occurs within a call to a VFS layer function during
350 # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
351 # [5668] for details.
352 set rc [catch {db eval { SELECT * FROM t1 }} msg]
353 if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
354 if {$rc} { error $msg }
355 set msg
danielk1977aca790a2005-01-13 11:07:52 +0000356 }
danielk1977aca790a2005-01-13 11:07:52 +0000357}
danielk19779a30cf62006-01-18 04:26:07 +0000358
359proc string_compare {a b} {
360 return [string compare $a $b]
361}
362
363# Test for malloc() failures in sqlite3_create_collation() and
364# sqlite3_create_collation16().
danielk1977950f0542006-01-18 05:51:57 +0000365#
danielk19774152e672007-09-12 17:01:45 +0000366ifcapable utf16 {
367 do_malloc_test 15 -start 4 -tclbody {
368 db collate string_compare string_compare
369 if {[catch {add_test_collate db 1 1 1} msg]} {
370 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
371 error $msg
372 }
373
374 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
375 db complete {-- Useful comment}
376
377 execsql {
378 CREATE TABLE t1(a, b COLLATE string_compare);
379 INSERT INTO t1 VALUES(10, 'string');
380 INSERT INTO t1 VALUES(10, 'string2');
381 }
danielk19779a30cf62006-01-18 04:26:07 +0000382 }
danielk197776b047d2005-01-19 03:52:54 +0000383}
danielk1977aca790a2005-01-13 11:07:52 +0000384
danielk1977950f0542006-01-18 05:51:57 +0000385# Also test sqlite3_complete(). There are (currently) no malloc()
386# calls in this function, but test anyway against future changes.
387#
388do_malloc_test 16 -tclbody {
389 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
390 db complete {-- Useful comment}
391 db eval {
392 SELECT * FROM sqlite_master;
393 }
394}
395
396# Test handling of malloc() failures in sqlite3_open16().
397#
danielk19774152e672007-09-12 17:01:45 +0000398ifcapable utf16 {
399 do_malloc_test 17 -tclbody {
400 set DB2 0
401 set STMT 0
402
403 # open database using sqlite3_open16()
404 set filename [encoding convertto unicode test.db]
405 append filename "\x00\x00"
406 set DB2 [sqlite3_open16 $filename -unused]
407 if {0==$DB2} {
408 error "out of memory"
409 }
danielk1977ae72d982007-10-03 08:46:44 +0000410 sqlite3_extended_result_codes $DB2 1
danielk19774152e672007-09-12 17:01:45 +0000411
412 # Prepare statement
413 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
danielk1977ae72d982007-10-03 08:46:44 +0000414 if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} {
415 error "out of memory"
416 }
danielk197759633ae2008-08-27 19:01:57 +0000417 if {[regexp ".*automatic extension loading.*" [sqlite3_errmsg $DB2]]} {
418 error "out of memory"
419 }
danielk19774152e672007-09-12 17:01:45 +0000420 if {$rc} {
421 error [string range $msg 4 end]
422 }
423 set STMT $msg
424
425 # Finalize statement
426 set rc [sqlite3_finalize $STMT]
427 if {$rc!="SQLITE_OK"} {
428 error [sqlite3_errmsg $DB2]
429 }
430 set STMT 0
431
432 # Close database
danielk1977950f0542006-01-18 05:51:57 +0000433 set rc [sqlite3_close $DB2]
danielk19774152e672007-09-12 17:01:45 +0000434 if {$rc!="SQLITE_OK"} {
435 error [sqlite3_errmsg $DB2]
436 }
437 set DB2 0
438 } -cleanup {
439 if {$STMT!="0"} {
440 sqlite3_finalize $STMT
441 }
442 if {$DB2!="0"} {
443 set rc [sqlite3_close $DB2]
444 }
danielk1977950f0542006-01-18 05:51:57 +0000445 }
446}
447
448# Test handling of malloc() failures in sqlite3_errmsg16().
449#
danielk19774152e672007-09-12 17:01:45 +0000450ifcapable utf16 {
danielk1977ae72d982007-10-03 08:46:44 +0000451 do_malloc_test 18 -tclprep {
danielk19774152e672007-09-12 17:01:45 +0000452 catch {
453 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
danielk1977ae72d982007-10-03 08:46:44 +0000454 }
455 } -tclbody {
danielk19774152e672007-09-12 17:01:45 +0000456 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
457 binary scan $utf16 c* bytes
458 if {[llength $bytes]==0} {
459 error "out of memory"
460 }
danielk1977950f0542006-01-18 05:51:57 +0000461 }
462}
463
danielk1977161fb792006-01-24 10:58:21 +0000464# This test is aimed at coverage testing. Specificly, it is supposed to
465# cause a malloc() only used when converting between the two utf-16
466# encodings to fail (i.e. little-endian->big-endian). It only actually
467# hits this malloc() on little-endian hosts.
468#
469set static_string "\x00h\x00e\x00l\x00l\x00o"
470for {set l 0} {$l<10} {incr l} {
471 append static_string $static_string
472}
473append static_string "\x00\x00"
474do_malloc_test 19 -tclprep {
475 execsql {
476 PRAGMA encoding = "UTF16be";
477 CREATE TABLE abc(a, b, c);
478 }
479} -tclbody {
480 unset -nocomplain ::STMT
481 set r [catch {
drhf3a65f72007-08-22 20:18:21 +0000482 set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY]
danielk1977161fb792006-01-24 10:58:21 +0000483 sqlite3_bind_text16 -static $::STMT 1 $static_string 112
484 } msg]
485 if {$r} {error [string range $msg 4 end]}
486 set msg
487} -cleanup {
488 if {[info exists ::STMT]} {
489 sqlite3_finalize $::STMT
490 }
491}
492unset static_string
493
drh6103fe92006-04-05 11:57:37 +0000494# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
495# when the malloc failure occurs within the nested parse.
496#
danielk19775a8f9372007-10-09 08:29:32 +0000497ifcapable attach {
498 do_malloc_test 20 -tclprep {
499 db close
500 file delete -force test2.db test2.db-journal
501 sqlite3 db test2.db
502 sqlite3_extended_result_codes db 1
503 db eval {CREATE TABLE t1(x);}
504 db close
505 } -tclbody {
506 if {[catch {sqlite3 db test.db}]} {
507 error "out of memory"
508 }
509 sqlite3_extended_result_codes db 1
510 } -sqlbody {
511 ATTACH DATABASE 'test2.db' AS t2;
512 SELECT * FROM t1;
513 DETACH DATABASE t2;
514 }
515}
drh6103fe92006-04-05 11:57:37 +0000516
danielk1977b5584c02007-03-30 07:10:50 +0000517# Test malloc failure whilst installing a foreign key.
danielk197769b637b2007-03-29 17:07:52 +0000518#
danielk19774152e672007-09-12 17:01:45 +0000519ifcapable foreignkey {
520 do_malloc_test 21 -sqlbody {
521 CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
522 }
523}
danielk197769b637b2007-03-29 17:07:52 +0000524
danielk19777e29e952007-04-19 11:09:01 +0000525# Test malloc failure in an sqlite3_prepare_v2() call.
526#
527do_malloc_test 22 -tclbody {
528 set ::STMT ""
529 set r [catch {
530 set ::STMT [
drhf3a65f72007-08-22 20:18:21 +0000531 sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY
danielk19777e29e952007-04-19 11:09:01 +0000532 ]
533 } msg]
534 if {$r} {error [string range $msg 4 end]}
535} -cleanup {
536 if {$::STMT ne ""} {
537 sqlite3_finalize $::STMT
538 set ::STMT ""
539 }
540}
541
danielk19772a180ff2007-10-03 15:02:40 +0000542ifcapable {pager_pragmas} {
543 # This tests a special case - that an error that occurs while the pager
544 # is trying to recover from error-state in exclusive-access mode works.
545 #
546 do_malloc_test 23 -tclprep {
547 db eval {
548 PRAGMA cache_size = 10;
549 PRAGMA locking_mode = exclusive;
550 BEGIN;
551 CREATE TABLE abc(a, b, c);
552 CREATE INDEX abc_i ON abc(a, b, c);
553 INSERT INTO abc
554 VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
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 INSERT INTO abc
562 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
563 INSERT INTO abc
564 SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
565 COMMIT;
566 }
567
568 # This puts the pager into error state.
569 #
570 db eval BEGIN
571 db eval {UPDATE abc SET a = 0 WHERE oid%2}
572 set ::sqlite_io_error_pending 10
573 catch {db eval {ROLLBACK}} msg
574
danielk197798c21902008-09-23 16:41:29 +0000575 } -tclbody {
576 # If an out-of-memory occurs within a call to a VFS layer function during
577 # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
578 # [5668] for details.
579 set rc [catch {db eval { SELECT * FROM abc LIMIT 10 }} msg]
580 if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
581 if {$rc} { error $msg }
582 set msg
danielk19772a180ff2007-10-03 15:02:40 +0000583 } -cleanup {
584 set e [db eval {PRAGMA integrity_check}]
585 if {$e ne "ok"} {error $e}
586 }
587}
588
danielk1977ac559262008-01-23 17:13:40 +0000589ifcapable compound {
danielk1977cdf30202008-01-24 14:27:44 +0000590 do_malloc_test 24 -sqlprep {
danielk1977ac559262008-01-23 17:13:40 +0000591 CREATE TABLE t1(a, b, c)
592 } -sqlbody {
593 SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1
594 }
595}
596
danielk1977cdf30202008-01-24 14:27:44 +0000597ifcapable view&&trigger {
598 do_malloc_test 25 -sqlprep {
599 CREATE TABLE t1(a, b, c);
600 CREATE VIEW v1 AS SELECT * FROM t1;
601 CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END;
602 CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END;
603 CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END;
604 } -sqlbody {
605 DELETE FROM v1 WHERE a = 1;
606 INSERT INTO v1 VALUES(1, 2, 3);
607 UPDATE v1 SET a = 1 WHERE b = 2;
608 }
609}
610
danielk19777eaabcd2008-07-07 14:56:56 +0000611do_malloc_test 25 -sqlprep {
612 CREATE TABLE abc(a, b, c);
613 CREATE INDEX i1 ON abc(a, b);
614 INSERT INTO abc VALUES(1, 2, 3);
615 INSERT INTO abc VALUES(4, 5, 6);
616} -tclbody {
617 # For each UPDATE executed, the cursor used for the SELECT statement
618 # must be "saved". Because the cursor is open on an index, this requires
619 # a malloc() to allocate space to save the index key. This test case is
620 # aimed at testing the response of the library to a failure in that
621 # particular malloc() call.
622 db eval {SELECT a FROM abc ORDER BY a} {
623 db eval {UPDATE abc SET b = b - 1 WHERE a = $a}
624 }
625}
626
danielk1977518002e2008-09-05 05:02:46 +0000627# This test is designed to test a specific juncture in the sqlite code.
628# The database set up by -sqlprep script contains a single table B-Tree
629# of height 2. In the -tclbody script, the existing database connection
630# is closed and a new one opened and used to insert a new row into the
631# table B-Tree. By using a new connection, the outcome of a malloc()
632# failure while seeking to the right-hand side of the B-Tree to insert
633# a new record can be tested.
634#
635do_malloc_test 26 -sqlprep {
636 BEGIN;
637 CREATE TABLE t1(a, b);
638 INSERT INTO t1 VALUES(1, randomblob(210));
639 INSERT INTO t1 VALUES(1, randomblob(210));
640 INSERT INTO t1 VALUES(1, randomblob(210));
641 INSERT INTO t1 VALUES(1, randomblob(210));
642 INSERT INTO t1 VALUES(1, randomblob(210));
643 COMMIT;
644} -tclbody {
645 db close
646 sqlite3 db test.db
647 db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
648}
649
danielk197777eb5bb2008-09-22 17:22:19 +0000650# Test that no memory is leaked following a malloc() failure in
651# sqlite3_initialize().
652#
653do_malloc_test 27 -tclprep {
654 db close
655 sqlite3_shutdown
656} -tclbody {
657 set rc [sqlite3_initialize]
658 if {$rc == "SQLITE_NOMEM"} {
659 error "out of memory"
660 }
661}
662
danielk197785574e32008-10-06 05:32:18 +0000663# Test that malloc failures that occur while processing INDEXED BY
664# clauses are handled correctly.
665do_malloc_test 28 -sqlprep {
666 CREATE TABLE t1(a, b);
667 CREATE INDEX i1 ON t1(a);
668 CREATE VIEW v1 AS SELECT * FROM t1 INDEXED BY i1 WHERE a = 10;
669} -sqlbody {
670 SELECT * FROM t1 INDEXED BY i1 ORDER BY a;
671 SELECT * FROM v1;
672}
673
danielk1977e4359752008-11-03 09:39:45 +0000674do_malloc_test 29 -sqlprep {
675 CREATE TABLE t1(a TEXT, b TEXT);
676} -sqlbody {
677 INSERT INTO t1 VALUES(1, -234);
danielk1977956f4312008-11-21 09:43:20 +0000678 INSERT INTO t1 SELECT * FROM t1 UNION ALL SELECT * FROM t1;
danielk1977e4359752008-11-03 09:39:45 +0000679}
680
danielk19777cbd5892009-01-10 16:15:09 +0000681do_malloc_test 30 -tclprep {
danielk197795855282009-01-10 11:10:18 +0000682 db eval {
683 CREATE TABLE t1(x PRIMARY KEY);
684 INSERT INTO t1 VALUES(randstr(500,500));
685 INSERT INTO t1 VALUES(randstr(500,500));
686 INSERT INTO t1 VALUES(randstr(500,500));
687 }
688 db close
689 sqlite3 db test.db
690
691 # The DELETE command in the following block moves the overflow pages that
692 # are part of the primary key index to the free-list. But it does not
693 # actually load the content of the pages. This leads to the peculiar
694 # situation where cache entries exist, but are not populated with data.
695 # They are populated next time they are requested by the b-tree layer.
696 #
697 db eval {
698 BEGIN;
699 DELETE FROM t1;
700 ROLLBACK;
701 }
702} -sqlbody {
danielk1977c87239f2009-01-10 11:13:39 +0000703 -- This statement requires the 'no-content' pages loaded by the DELETE
704 -- statement above. When requesting the pages, the content is loaded
705 -- from the database file. The point of this test case is to test handling
706 -- of malloc errors (including SQLITE_IOERR_NOMEM errors) when loading
707 -- the content.
danielk197795855282009-01-10 11:10:18 +0000708 SELECT * FROM t1 ORDER BY x;
709}
710
danielk19777cbd5892009-01-10 16:15:09 +0000711# After committing a transaction in persistent-journal mode, if a journal
712# size limit is configured SQLite may attempt to truncate the journal file.
713# This test verifies the libraries response to a malloc() failure during
714# this operation.
715#
716do_malloc_test 31 -sqlprep {
717 PRAGMA journal_mode = persist;
718 PRAGMA journal_size_limit = 1024;
719 CREATE TABLE t1(a PRIMARY KEY, b);
720} -sqlbody {
721 INSERT INTO t1 VALUES(1, 2);
722}
danielk1977956f4312008-11-21 09:43:20 +0000723
danielk1977db340392009-01-16 16:40:14 +0000724# When written, this test provoked an obscure change-counter bug.
725#
726# If, when running in exclusive mode, a malloc() failure occurs
727# after the database file change-counter has been written but
728# before the transaction has been committed, then the transaction
729# is automatically rolled back. However, internally the
730# Pager.changeCounterDone flag was being left set. This means
731# that if the same connection attempts another transaction following
732# the malloc failure and rollback, the change counter will not
733# be updated. This could corrupt another processes cache.
734#
735do_malloc_test 32 -tclprep {
736 # Build a small database containing an indexed table.
737 #
738 db eval {
739 BEGIN;
740 CREATE TABLE t1(a PRIMARY KEY, b);
741 INSERT INTO t1 VALUES(1, 'one');
742 INSERT INTO t1 VALUES(2, 'two');
743 INSERT INTO t1 VALUES(3, 'three');
744 COMMIT;
745 PRAGMA locking_mode = exclusive;
746 }
747
748 # Open a second database connection. Load the table (but not index)
749 # into the second connections pager cache.
750 #
751 sqlite3 db2 test.db
752 db2 eval { SELECT b FROM t1 }
753
754} -tclbody {
755 # Running in exclusive mode, perform a database transaction that
756 # modifies both the database table and index. For iterations where
757 # the malloc failure occurs after updating the change counter but
758 # before committing the transaction, this should result in the
759 # transaction being rolled back but the changeCounterDone flag
760 # left set.
761 #
762 db eval { UPDATE t1 SET a = a + 3 }
763} -cleanup {
764
765 # Perform another transaction using the first connection. Unlock
766 # the database after doing so. If this is one of the right iterations,
767 # then this should result in the database contents being updated but
768 # the change-counter left as it is.
769 #
770 db eval {
771 PRAGMA locking_mode = normal;
772 UPDATE t1 SET a = a + 3;
773 }
774
775 # Now do an integrity check with the second connection. The second
776 # connection still has the database table in its cache. If this is
777 # one of the magic iterations and the change counter was not modified,
778 # then it won't realize that the cached data is out of date. Since
779 # the cached data won't match the up to date index data read from
780 # the database file, the integrity check should fail.
781 #
782 set zRepeat "transient"
783 if {$::iRepeat} {set zRepeat "persistent"}
784 do_test malloc-32.$zRepeat.${::n}.integrity {
785 execsql {PRAGMA integrity_check} db2
786 } {ok}
787 db2 close
788}
789
danielk197796fb0dd2004-06-30 09:49:22 +0000790# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000791do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000792 catch {db close}
793 set sqlite_open_file_count
794} {0}
795
drh344a6272006-06-26 12:50:09 +0000796puts open-file-count=$sqlite_open_file_count
drhed7c8552001-04-11 14:29:21 +0000797finish_test