blob: 1ca17850cb76173bc617d5c812614e7d931bf1a4 [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#***********************************************************************
11# This file attempts to check the library in an out-of-memory situation.
drhc89b91b2005-01-03 01:32:59 +000012# When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special
drh6d4abfb2001-10-22 02:58:08 +000013# command (sqlite_malloc_fail N) which causes the N-th malloc to fail. This
drhed7c8552001-04-11 14:29:21 +000014# 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#
drh66560ad2006-01-06 14:32:19 +000017# $Id: malloc.test,v 1.26 2006/01/06 14:32:20 drh Exp $
drhed7c8552001-04-11 14:29:21 +000018
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Only run these tests if memory debugging is turned on.
23#
drhb5f70c22004-02-14 01:39:50 +000024if {[info command sqlite_malloc_stat]==""} {
danielk1977261919c2005-12-06 12:52:59 +000025 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed7c8552001-04-11 14:29:21 +000026 finish_test
27 return
28}
29
danielk19774397de52005-01-12 12:44:03 +000030# 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.
danielk1977656152c2005-01-12 13:04:54 +000037# -tclbody TCL script to run with malloc failure simulation.
38# -sqlbody TCL script to run with malloc failure simulation.
danielk1977c08d4052005-01-13 13:35:57 +000039# -cleanup TCL script to run after the test.
danielk1977656152c2005-01-12 13:04:54 +000040#
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.
danielk19774397de52005-01-12 12:44:03 +000053#
54proc do_malloc_test {tn args} {
danielk1977261919c2005-12-06 12:52:59 +000055 array unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +000056 array set ::mallocopts $args
57
58 set ::go 1
danielk1977261919c2005-12-06 12:52:59 +000059 for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
danielk19774397de52005-01-12 12:44:03 +000060 do_test malloc-$tn.$::n {
61
danielk1977261919c2005-12-06 12:52:59 +000062 # 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 #
danielk19774397de52005-01-12 12:44:03 +000065 sqlite_malloc_fail 0
66 catch {db close}
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}
71 set ::DB [sqlite3 db test.db]
72
danielk1977261919c2005-12-06 12:52:59 +000073 # Execute any -tclprep and -sqlprep scripts.
74 #
danielk19774397de52005-01-12 12:44:03 +000075 if {[info exists ::mallocopts(-tclprep)]} {
76 eval $::mallocopts(-tclprep)
77 }
78 if {[info exists ::mallocopts(-sqlprep)]} {
79 execsql $::mallocopts(-sqlprep)
80 }
81
danielk1977261919c2005-12-06 12:52:59 +000082 # Now set the ${::n}th malloc() to fail and execute the -tclbody and
83 # -sqlbody scripts.
84 #
danielk19774397de52005-01-12 12:44:03 +000085 sqlite_malloc_fail $::n
86 set ::mallocbody {}
87 if {[info exists ::mallocopts(-tclbody)]} {
88 append ::mallocbody "$::mallocopts(-tclbody)\n"
89 }
90 if {[info exists ::mallocopts(-sqlbody)]} {
91 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
92 }
danielk19774397de52005-01-12 12:44:03 +000093 set v [catch $::mallocbody msg]
94
95 set leftover [lindex [sqlite_malloc_stat] 2]
96 if {$leftover>0} {
97 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
98 set ::go 0
99 set v {1 1}
100 } else {
101 set v2 [expr {$msg=="" || $msg=="out of memory"}]
102 if {!$v2} {puts "\nError message returned: $msg"}
103 lappend v $v2
104 }
105 } {1 1}
danielk1977c08d4052005-01-13 13:35:57 +0000106
107 if {[info exists ::mallocopts(-cleanup)]} {
108 catch $::mallocopts(-cleanup)
109 }
danielk19774397de52005-01-12 12:44:03 +0000110 }
danielk1977aca790a2005-01-13 11:07:52 +0000111 unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +0000112}
113
danielk1977c08d4052005-01-13 13:35:57 +0000114do_malloc_test 1 -tclprep {
115 db close
116} -tclbody {
117 if {[catch {sqlite3 db test.db}]} {
118 error "out of memory"
119 }
120} -sqlbody {
121 CREATE TABLE t1(
122 a int, b float, c double, d text, e varchar(20),
123 primary key(a,b,c)
124 );
125 CREATE INDEX i1 ON t1(a,b);
126 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
127 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
128 SELECT * FROM t1;
129 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
130 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
131 SELECT count(*) FROM t1;
132}
drhd4007282001-04-12 23:21:58 +0000133
danielk1977b5548a82004-06-26 13:51:33 +0000134# Ensure that no file descriptors were leaked.
135do_test malloc-1.X {
136 catch {db close}
137 set sqlite_open_file_count
138} {0}
139
danielk1977c08d4052005-01-13 13:35:57 +0000140do_malloc_test 2 -sqlbody {
drhfc233142005-08-19 01:07:15 +0000141 CREATE TABLE t1(a int, b int default 'abc', c int default 1);
danielk1977c08d4052005-01-13 13:35:57 +0000142 CREATE INDEX i1 ON t1(a,b);
143 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
144 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
145 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
146 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
147 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
148 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
149 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
150 UPDATE t1 SET b=b||b||b||b;
151 UPDATE t1 SET b=a WHERE a in (10,12,22);
152 INSERT INTO t1(c,b,a) VALUES(20,10,5);
153 INSERT INTO t1 SELECT * FROM t1
154 WHERE a IN (SELECT a FROM t1 WHERE a<10);
155 DELETE FROM t1 WHERE a>=10;
156 DROP INDEX i1;
157 DELETE FROM t1;
158}
drh6d4abfb2001-10-22 02:58:08 +0000159
danielk1977b5548a82004-06-26 13:51:33 +0000160# Ensure that no file descriptors were leaked.
161do_test malloc-2.X {
162 catch {db close}
163 set sqlite_open_file_count
164} {0}
165
danielk1977c08d4052005-01-13 13:35:57 +0000166do_malloc_test 3 -sqlbody {
167 BEGIN TRANSACTION;
168 CREATE TABLE t1(a int, b int, c int);
169 CREATE INDEX i1 ON t1(a,b);
170 INSERT INTO t1 VALUES(1,1,99);
171 INSERT INTO t1 VALUES(2,4,98);
172 INSERT INTO t1 VALUES(3,9,97);
173 INSERT INTO t1 VALUES(4,16,96);
174 INSERT INTO t1 VALUES(5,25,95);
175 INSERT INTO t1 VALUES(6,36,94);
176 INSERT INTO t1(c,b,a) VALUES(20,10,5);
177 DELETE FROM t1 WHERE a>=10;
178 DROP INDEX i1;
179 DELETE FROM t1;
180 ROLLBACK;
181}
182
danielk1977b5548a82004-06-26 13:51:33 +0000183
184# Ensure that no file descriptors were leaked.
185do_test malloc-3.X {
186 catch {db close}
187 set sqlite_open_file_count
188} {0}
189
danielk1977c08d4052005-01-13 13:35:57 +0000190do_malloc_test 4 -sqlbody {
191 BEGIN TRANSACTION;
192 CREATE TABLE t1(a int, b int, c int);
193 CREATE INDEX i1 ON t1(a,b);
194 INSERT INTO t1 VALUES(1,1,99);
195 INSERT INTO t1 VALUES(2,4,98);
196 INSERT INTO t1 VALUES(3,9,97);
197 INSERT INTO t1 VALUES(4,16,96);
198 INSERT INTO t1 VALUES(5,25,95);
199 INSERT INTO t1 VALUES(6,36,94);
200 UPDATE t1 SET b=a WHERE a in (10,12,22);
201 INSERT INTO t1 SELECT * FROM t1
202 WHERE a IN (SELECT a FROM t1 WHERE a<10);
203 DROP INDEX i1;
204 DELETE FROM t1;
205 COMMIT;
206}
danielk1977b5548a82004-06-26 13:51:33 +0000207
208# Ensure that no file descriptors were leaked.
209do_test malloc-4.X {
210 catch {db close}
211 set sqlite_open_file_count
212} {0}
213
danielk1977c08d4052005-01-13 13:35:57 +0000214do_malloc_test 5 -sqlbody {
215 BEGIN TRANSACTION;
216 CREATE TABLE t1(a,b);
217 CREATE TABLE t2(x,y);
218 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
219 INSERT INTO t2(x,y) VALUES(new.rowid,1);
220 END;
221 INSERT INTO t1(a,b) VALUES(2,3);
222 COMMIT;
223}
danielk1977b5548a82004-06-26 13:51:33 +0000224
225# Ensure that no file descriptors were leaked.
226do_test malloc-5.X {
227 catch {db close}
228 set sqlite_open_file_count
229} {0}
230
danielk1977c08d4052005-01-13 13:35:57 +0000231do_malloc_test 6 -sqlprep {
232 BEGIN TRANSACTION;
233 CREATE TABLE t1(a);
234 INSERT INTO t1 VALUES(1);
235 INSERT INTO t1 SELECT a*2 FROM t1;
236 INSERT INTO t1 SELECT a*2 FROM t1;
237 INSERT INTO t1 SELECT a*2 FROM t1;
238 INSERT INTO t1 SELECT a*2 FROM t1;
239 INSERT INTO t1 SELECT a*2 FROM t1;
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 DELETE FROM t1 where rowid%5 = 0;
246 COMMIT;
247} -sqlbody {
248 VACUUM;
249}
danielk197796fb0dd2004-06-30 09:49:22 +0000250
danielk1977c08d4052005-01-13 13:35:57 +0000251do_malloc_test 7 -sqlprep {
252 CREATE TABLE t1(a, b);
253 INSERT INTO t1 VALUES(1, 2);
254 INSERT INTO t1 VALUES(3, 4);
255 INSERT INTO t1 VALUES(5, 6);
256 INSERT INTO t1 VALUES(7, randstr(1200,1200));
257} -sqlbody {
258 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
259 SELECT a FROM t1 WHERE a<6 ORDER BY a;
260 SELECT b FROM t1 WHERE a>6;
261}
danielk197701427a62005-01-11 13:02:33 +0000262
danielk1977b5402fb2005-01-12 07:15:04 +0000263# This block is designed to test that some malloc failures that may
264# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
265# when converting UTF-16 text to integers and real numbers is handled
266# correctly.
267#
danielk19778b60e0f2005-01-12 09:10:39 +0000268# This is done by retrieving a string from the database engine and
269# manipulating it using the sqlite3_column_*** APIs. This doesn't
270# actually return an error to the user when a malloc() fails.. That
271# could be viewed as a bug.
272#
273# These tests only run if UTF-16 support is compiled in.
danielk1977b5402fb2005-01-12 07:15:04 +0000274#
danielk1977c08d4052005-01-13 13:35:57 +0000275if {$::sqlite_options(utf16)} {
276 do_malloc_test 8 -tclprep {
277 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
278 set ::STMT [sqlite3_prepare $::DB $sql -1 X]
279 sqlite3_step $::STMT
280 if { $::tcl_platform(byteOrder)=="littleEndian" } {
281 set ::bomstr "\xFF\xFE"
282 } else {
283 set ::bomstr "\xFE\xFF"
284 }
285 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
286 } -tclbody {
287 sqlite3_column_text16 $::STMT 0
288 sqlite3_column_int $::STMT 0
289 sqlite3_column_text16 $::STMT 1
290 sqlite3_column_double $::STMT 1
291 sqlite3_reset $::STMT
292 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
293 catch {sqlite3_finalize $::STMT}
294 if {[lindex [sqlite_malloc_stat] 2]<=0} {
295 error "out of memory"
296 }
297 } -cleanup {
298 sqlite3_finalize $::STMT
299 }
danielk1977b5402fb2005-01-12 07:15:04 +0000300}
301
danielk19778b60e0f2005-01-12 09:10:39 +0000302# This block tests that malloc() failures that occur whilst commiting
303# a multi-file transaction are handled correctly.
304#
danielk19774397de52005-01-12 12:44:03 +0000305do_malloc_test 9 -sqlprep {
306 ATTACH 'test2.db' as test2;
307 CREATE TABLE abc1(a, b, c);
308 CREATE TABLE test2.abc2(a, b, c);
309} -sqlbody {
310 BEGIN;
311 INSERT INTO abc1 VALUES(1, 2, 3);
312 INSERT INTO abc2 VALUES(1, 2, 3);
313 COMMIT;
danielk19778b60e0f2005-01-12 09:10:39 +0000314}
315
danielk19774397de52005-01-12 12:44:03 +0000316# This block tests malloc() failures that occur while opening a
317# connection to a database.
318do_malloc_test 10 -sqlprep {
319 CREATE TABLE abc(a, b, c);
320} -tclbody {
321 set ::DB [sqlite3 db2 test.db]
322 db2 eval {SELECT * FROM sqlite_master}
323 db2 close
324}
325
326# This block tests malloc() failures that occur within calls to
327# sqlite3_create_function().
328do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000329 set rc [sqlite3_create_function $::DB]
330 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000331 error "out of memory"
332 }
333}
334
335do_malloc_test 12 -tclbody {
336 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
337 append sql16 "\00\00"
338 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
339 sqlite3_finalize $::STMT
340}
danielk19778b60e0f2005-01-12 09:10:39 +0000341
danielk1977aca790a2005-01-13 11:07:52 +0000342# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000343# transaction.
344ifcapable crashtest {
danielk1977aca790a2005-01-13 11:07:52 +0000345 do_malloc_test 13 -tclprep {
346 set rc [crashsql 1 test2.db {
347 ATTACH 'test2.db' as aux;
348 PRAGMA cache_size = 10;
349 BEGIN;
350 CREATE TABLE aux.t2(a, b, c);
351 CREATE TABLE t1(a, b, c);
352 COMMIT;
353 }]
354 if {$rc!="1 {child process exited abnormally}"} {
355 error "Wrong error message: $rc"
356 }
357 } -sqlbody {
358 ATTACH 'test2.db' as aux;
359 SELECT * FROM t1;
360 SELECT * FROM t2;
361 }
362}
363
danielk197776b047d2005-01-19 03:52:54 +0000364if {$tcl_platform(platform)!="windows"} {
danielk1977aca790a2005-01-13 11:07:52 +0000365do_malloc_test 14 -tclprep {
366 catch {db close}
367 sqlite3 db2 test2.db
368 db2 eval {
369 PRAGMA synchronous = 0;
370 CREATE TABLE t1(a, b);
371 INSERT INTO t1 VALUES(1, 2);
372 BEGIN;
373 INSERT INTO t1 VALUES(3, 4);
374 }
danielk197732554c12005-01-22 03:39:39 +0000375 copy_file test2.db test.db
376 copy_file test2.db-journal test.db-journal
danielk1977aca790a2005-01-13 11:07:52 +0000377 db2 close
378} -tclbody {
379 sqlite3 db test.db
380 db eval {
381 SELECT * FROM t1;
382 }
383}
danielk197776b047d2005-01-19 03:52:54 +0000384}
danielk1977aca790a2005-01-13 11:07:52 +0000385
danielk197796fb0dd2004-06-30 09:49:22 +0000386# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000387do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000388 catch {db close}
389 set sqlite_open_file_count
390} {0}
391
drhed7c8552001-04-11 14:29:21 +0000392sqlite_malloc_fail 0
393finish_test