blob: fce8de41293e01195102cd07ddc2b3423515e886 [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#
danielk1977c08d4052005-01-13 13:35:57 +000017# $Id: malloc.test,v 1.20 2005/01/13 13:35:58 danielk1977 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]==""} {
drhc89b91b2005-01-03 01:32:59 +000025 puts "Skipping malloc tests: not compiled with -DSQLITE_DEBUG..."
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} {
55 array set ::mallocopts $args
56
57 set ::go 1
58 for {set ::n 1} {$::go} {incr ::n} {
59
60 do_test malloc-$tn.$::n {
61
62 sqlite_malloc_fail 0
63 catch {db close}
64 catch {file delete -force test.db}
65 catch {file delete -force test.db-journal}
66 catch {file delete -force test2.db}
67 catch {file delete -force test2.db-journal}
68 set ::DB [sqlite3 db test.db]
69
70 if {[info exists ::mallocopts(-tclprep)]} {
71 eval $::mallocopts(-tclprep)
72 }
73 if {[info exists ::mallocopts(-sqlprep)]} {
74 execsql $::mallocopts(-sqlprep)
75 }
76
77 sqlite_malloc_fail $::n
78 set ::mallocbody {}
79 if {[info exists ::mallocopts(-tclbody)]} {
80 append ::mallocbody "$::mallocopts(-tclbody)\n"
81 }
82 if {[info exists ::mallocopts(-sqlbody)]} {
83 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
84 }
85
86 set v [catch $::mallocbody msg]
87
88 set leftover [lindex [sqlite_malloc_stat] 2]
89 if {$leftover>0} {
90 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
91 set ::go 0
92 set v {1 1}
93 } else {
94 set v2 [expr {$msg=="" || $msg=="out of memory"}]
95 if {!$v2} {puts "\nError message returned: $msg"}
96 lappend v $v2
97 }
98 } {1 1}
danielk1977c08d4052005-01-13 13:35:57 +000099
100 if {[info exists ::mallocopts(-cleanup)]} {
101 catch $::mallocopts(-cleanup)
102 }
danielk19774397de52005-01-12 12:44:03 +0000103 }
danielk1977aca790a2005-01-13 11:07:52 +0000104 unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +0000105}
106
danielk1977c08d4052005-01-13 13:35:57 +0000107do_malloc_test 1 -tclprep {
108 db close
109} -tclbody {
110 if {[catch {sqlite3 db test.db}]} {
111 error "out of memory"
112 }
113} -sqlbody {
114 CREATE TABLE t1(
115 a int, b float, c double, d text, e varchar(20),
116 primary key(a,b,c)
117 );
118 CREATE INDEX i1 ON t1(a,b);
119 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
120 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
121 SELECT * FROM t1;
122 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
123 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
124 SELECT count(*) FROM t1;
125}
drhd4007282001-04-12 23:21:58 +0000126
danielk1977b5548a82004-06-26 13:51:33 +0000127# Ensure that no file descriptors were leaked.
128do_test malloc-1.X {
129 catch {db close}
130 set sqlite_open_file_count
131} {0}
132
danielk1977c08d4052005-01-13 13:35:57 +0000133do_malloc_test 2 -sqlbody {
134 CREATE TABLE t1(a int, b int, c int);
135 CREATE INDEX i1 ON t1(a,b);
136 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
137 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
138 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
139 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
140 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
141 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
142 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
143 UPDATE t1 SET b=b||b||b||b;
144 UPDATE t1 SET b=a WHERE a in (10,12,22);
145 INSERT INTO t1(c,b,a) VALUES(20,10,5);
146 INSERT INTO t1 SELECT * FROM t1
147 WHERE a IN (SELECT a FROM t1 WHERE a<10);
148 DELETE FROM t1 WHERE a>=10;
149 DROP INDEX i1;
150 DELETE FROM t1;
151}
drh6d4abfb2001-10-22 02:58:08 +0000152
danielk1977b5548a82004-06-26 13:51:33 +0000153# Ensure that no file descriptors were leaked.
154do_test malloc-2.X {
155 catch {db close}
156 set sqlite_open_file_count
157} {0}
158
danielk1977c08d4052005-01-13 13:35:57 +0000159do_malloc_test 3 -sqlbody {
160 BEGIN TRANSACTION;
161 CREATE TABLE t1(a int, b int, c int);
162 CREATE INDEX i1 ON t1(a,b);
163 INSERT INTO t1 VALUES(1,1,99);
164 INSERT INTO t1 VALUES(2,4,98);
165 INSERT INTO t1 VALUES(3,9,97);
166 INSERT INTO t1 VALUES(4,16,96);
167 INSERT INTO t1 VALUES(5,25,95);
168 INSERT INTO t1 VALUES(6,36,94);
169 INSERT INTO t1(c,b,a) VALUES(20,10,5);
170 DELETE FROM t1 WHERE a>=10;
171 DROP INDEX i1;
172 DELETE FROM t1;
173 ROLLBACK;
174}
175
danielk1977b5548a82004-06-26 13:51:33 +0000176
177# Ensure that no file descriptors were leaked.
178do_test malloc-3.X {
179 catch {db close}
180 set sqlite_open_file_count
181} {0}
182
danielk1977c08d4052005-01-13 13:35:57 +0000183do_malloc_test 4 -sqlbody {
184 BEGIN TRANSACTION;
185 CREATE TABLE t1(a int, b int, c int);
186 CREATE INDEX i1 ON t1(a,b);
187 INSERT INTO t1 VALUES(1,1,99);
188 INSERT INTO t1 VALUES(2,4,98);
189 INSERT INTO t1 VALUES(3,9,97);
190 INSERT INTO t1 VALUES(4,16,96);
191 INSERT INTO t1 VALUES(5,25,95);
192 INSERT INTO t1 VALUES(6,36,94);
193 UPDATE t1 SET b=a WHERE a in (10,12,22);
194 INSERT INTO t1 SELECT * FROM t1
195 WHERE a IN (SELECT a FROM t1 WHERE a<10);
196 DROP INDEX i1;
197 DELETE FROM t1;
198 COMMIT;
199}
danielk1977b5548a82004-06-26 13:51:33 +0000200
201# Ensure that no file descriptors were leaked.
202do_test malloc-4.X {
203 catch {db close}
204 set sqlite_open_file_count
205} {0}
206
danielk1977c08d4052005-01-13 13:35:57 +0000207do_malloc_test 5 -sqlbody {
208 BEGIN TRANSACTION;
209 CREATE TABLE t1(a,b);
210 CREATE TABLE t2(x,y);
211 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
212 INSERT INTO t2(x,y) VALUES(new.rowid,1);
213 END;
214 INSERT INTO t1(a,b) VALUES(2,3);
215 COMMIT;
216}
danielk1977b5548a82004-06-26 13:51:33 +0000217
218# Ensure that no file descriptors were leaked.
219do_test malloc-5.X {
220 catch {db close}
221 set sqlite_open_file_count
222} {0}
223
danielk1977c08d4052005-01-13 13:35:57 +0000224do_malloc_test 6 -sqlprep {
225 BEGIN TRANSACTION;
226 CREATE TABLE t1(a);
227 INSERT INTO t1 VALUES(1);
228 INSERT INTO t1 SELECT a*2 FROM t1;
229 INSERT INTO t1 SELECT a*2 FROM t1;
230 INSERT INTO t1 SELECT a*2 FROM t1;
231 INSERT INTO t1 SELECT a*2 FROM t1;
232 INSERT INTO t1 SELECT a*2 FROM t1;
233 INSERT INTO t1 SELECT a*2 FROM t1;
234 INSERT INTO t1 SELECT a*2 FROM t1;
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 DELETE FROM t1 where rowid%5 = 0;
239 COMMIT;
240} -sqlbody {
241 VACUUM;
242}
danielk197796fb0dd2004-06-30 09:49:22 +0000243
danielk1977c08d4052005-01-13 13:35:57 +0000244do_malloc_test 7 -sqlprep {
245 CREATE TABLE t1(a, b);
246 INSERT INTO t1 VALUES(1, 2);
247 INSERT INTO t1 VALUES(3, 4);
248 INSERT INTO t1 VALUES(5, 6);
249 INSERT INTO t1 VALUES(7, randstr(1200,1200));
250} -sqlbody {
251 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
252 SELECT a FROM t1 WHERE a<6 ORDER BY a;
253 SELECT b FROM t1 WHERE a>6;
254}
danielk197701427a62005-01-11 13:02:33 +0000255
danielk1977b5402fb2005-01-12 07:15:04 +0000256# This block is designed to test that some malloc failures that may
257# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
258# when converting UTF-16 text to integers and real numbers is handled
259# correctly.
260#
danielk19778b60e0f2005-01-12 09:10:39 +0000261# This is done by retrieving a string from the database engine and
262# manipulating it using the sqlite3_column_*** APIs. This doesn't
263# actually return an error to the user when a malloc() fails.. That
264# could be viewed as a bug.
265#
266# These tests only run if UTF-16 support is compiled in.
danielk1977b5402fb2005-01-12 07:15:04 +0000267#
danielk1977c08d4052005-01-13 13:35:57 +0000268if {$::sqlite_options(utf16)} {
269 do_malloc_test 8 -tclprep {
270 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
271 set ::STMT [sqlite3_prepare $::DB $sql -1 X]
272 sqlite3_step $::STMT
273 if { $::tcl_platform(byteOrder)=="littleEndian" } {
274 set ::bomstr "\xFF\xFE"
275 } else {
276 set ::bomstr "\xFE\xFF"
277 }
278 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
279 } -tclbody {
280 sqlite3_column_text16 $::STMT 0
281 sqlite3_column_int $::STMT 0
282 sqlite3_column_text16 $::STMT 1
283 sqlite3_column_double $::STMT 1
284 sqlite3_reset $::STMT
285 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
286 catch {sqlite3_finalize $::STMT}
287 if {[lindex [sqlite_malloc_stat] 2]<=0} {
288 error "out of memory"
289 }
290 } -cleanup {
291 sqlite3_finalize $::STMT
292 }
danielk1977b5402fb2005-01-12 07:15:04 +0000293}
294
danielk19778b60e0f2005-01-12 09:10:39 +0000295# This block tests that malloc() failures that occur whilst commiting
296# a multi-file transaction are handled correctly.
297#
danielk19774397de52005-01-12 12:44:03 +0000298do_malloc_test 9 -sqlprep {
299 ATTACH 'test2.db' as test2;
300 CREATE TABLE abc1(a, b, c);
301 CREATE TABLE test2.abc2(a, b, c);
302} -sqlbody {
303 BEGIN;
304 INSERT INTO abc1 VALUES(1, 2, 3);
305 INSERT INTO abc2 VALUES(1, 2, 3);
306 COMMIT;
danielk19778b60e0f2005-01-12 09:10:39 +0000307}
308
danielk19774397de52005-01-12 12:44:03 +0000309# This block tests malloc() failures that occur while opening a
310# connection to a database.
311do_malloc_test 10 -sqlprep {
312 CREATE TABLE abc(a, b, c);
313} -tclbody {
314 set ::DB [sqlite3 db2 test.db]
315 db2 eval {SELECT * FROM sqlite_master}
316 db2 close
317}
318
319# This block tests malloc() failures that occur within calls to
320# sqlite3_create_function().
321do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000322 set rc [sqlite3_create_function $::DB]
323 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000324 error "out of memory"
325 }
326}
327
328do_malloc_test 12 -tclbody {
329 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
330 append sql16 "\00\00"
331 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
332 sqlite3_finalize $::STMT
333}
danielk19778b60e0f2005-01-12 09:10:39 +0000334
danielk1977aca790a2005-01-13 11:07:52 +0000335# Test malloc errors when replaying two hot journals from a 2-file
336# transaction. This test only runs on UNIX.
337if {$tcl_platform(platform)=="unix"} {
338 do_malloc_test 13 -tclprep {
339 set rc [crashsql 1 test2.db {
340 ATTACH 'test2.db' as aux;
341 PRAGMA cache_size = 10;
342 BEGIN;
343 CREATE TABLE aux.t2(a, b, c);
344 CREATE TABLE t1(a, b, c);
345 COMMIT;
346 }]
347 if {$rc!="1 {child process exited abnormally}"} {
348 error "Wrong error message: $rc"
349 }
350 } -sqlbody {
351 ATTACH 'test2.db' as aux;
352 SELECT * FROM t1;
353 SELECT * FROM t2;
354 }
355}
356
357do_malloc_test 14 -tclprep {
358 catch {db close}
359 sqlite3 db2 test2.db
360 db2 eval {
361 PRAGMA synchronous = 0;
362 CREATE TABLE t1(a, b);
363 INSERT INTO t1 VALUES(1, 2);
364 BEGIN;
365 INSERT INTO t1 VALUES(3, 4);
366 }
367 file copy -force test2.db test.db
368 file copy -force test2.db-journal test.db-journal
369 db2 close
370} -tclbody {
371 sqlite3 db test.db
372 db eval {
373 SELECT * FROM t1;
374 }
375}
376
danielk197796fb0dd2004-06-30 09:49:22 +0000377# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000378do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000379 catch {db close}
380 set sqlite_open_file_count
381} {0}
382
drhed7c8552001-04-11 14:29:21 +0000383sqlite_malloc_fail 0
384finish_test