xref: /sqlite-3.40.0/test/malloc.test (revision 961303c1)
1# 2001 September 15
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
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#
19# $Id: malloc.test,v 1.48 2007/09/12 17:01:45 danielk1977 Exp $
20
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
23
24# Only run these tests if memory debugging is turned on.
25#
26ifcapable !memdebug {
27   puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
28   finish_test
29   return
30}
31
32source $testdir/malloc_common.tcl
33
34ifcapable bloblit&&subquery {
35  do_malloc_test 1 -tclprep {
36    db close
37  } -tclbody {
38    if {[catch {sqlite3 db test.db}]} {
39      error "out of memory"
40    }
41  } -sqlbody {
42    DROP TABLE IF EXISTS t1;
43    CREATE TABLE t1(
44       a int, b float, c double, d text, e varchar(20),
45       primary key(a,b,c)
46    );
47    CREATE INDEX i1 ON t1(a,b);
48    INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500');
49    INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
50    SELECT * FROM t1;
51    SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
52    DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
53    SELECT count(*) FROM t1;
54  }
55}
56
57# Ensure that no file descriptors were leaked.
58do_test malloc-1.X {
59  catch {db close}
60  set sqlite_open_file_count
61} {0}
62
63ifcapable subquery {
64  do_malloc_test 2 -sqlbody {
65    CREATE TABLE t1(a int, b int default 'abc', c int default 1);
66    CREATE INDEX i1 ON t1(a,b);
67    INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
68    INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
69    INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
70    INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
71    INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
72    INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
73    SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
74    UPDATE t1 SET b=b||b||b||b;
75    UPDATE t1 SET b=a WHERE a in (10,12,22);
76    INSERT INTO t1(c,b,a) VALUES(20,10,5);
77    INSERT INTO t1 SELECT * FROM t1
78        WHERE a IN (SELECT a FROM t1 WHERE a<10);
79    DELETE FROM t1 WHERE a>=10;
80    DROP INDEX i1;
81    DELETE FROM t1;
82  }
83}
84
85# Ensure that no file descriptors were leaked.
86do_test malloc-2.X {
87  catch {db close}
88  set sqlite_open_file_count
89} {0}
90
91do_malloc_test 3 -sqlbody {
92  BEGIN TRANSACTION;
93  CREATE TABLE t1(a int, b int, c int);
94  CREATE INDEX i1 ON t1(a,b);
95  INSERT INTO t1 VALUES(1,1,99);
96  INSERT INTO t1 VALUES(2,4,98);
97  INSERT INTO t1 VALUES(3,9,97);
98  INSERT INTO t1 VALUES(4,16,96);
99  INSERT INTO t1 VALUES(5,25,95);
100  INSERT INTO t1 VALUES(6,36,94);
101  INSERT INTO t1(c,b,a) VALUES(20,10,5);
102  DELETE FROM t1 WHERE a>=10;
103  DROP INDEX i1;
104  DELETE FROM t1;
105  ROLLBACK;
106}
107
108
109# Ensure that no file descriptors were leaked.
110do_test malloc-3.X {
111  catch {db close}
112  set sqlite_open_file_count
113} {0}
114
115ifcapable subquery {
116  do_malloc_test 4 -sqlbody {
117    BEGIN TRANSACTION;
118    CREATE TABLE t1(a int, b int, c int);
119    CREATE INDEX i1 ON t1(a,b);
120    INSERT INTO t1 VALUES(1,1,99);
121    INSERT INTO t1 VALUES(2,4,98);
122    INSERT INTO t1 VALUES(3,9,97);
123    INSERT INTO t1 VALUES(4,16,96);
124    INSERT INTO t1 VALUES(5,25,95);
125    INSERT INTO t1 VALUES(6,36,94);
126    UPDATE t1 SET b=a WHERE a in (10,12,22);
127    INSERT INTO t1 SELECT * FROM t1
128       WHERE a IN (SELECT a FROM t1 WHERE a<10);
129    DROP INDEX i1;
130    DELETE FROM t1;
131    COMMIT;
132  }
133}
134
135# Ensure that no file descriptors were leaked.
136do_test malloc-4.X {
137  catch {db close}
138  set sqlite_open_file_count
139} {0}
140
141ifcapable trigger {
142  do_malloc_test 5 -sqlbody {
143    BEGIN TRANSACTION;
144    CREATE TABLE t1(a,b);
145    CREATE TABLE t2(x,y);
146    CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
147      INSERT INTO t2(x,y) VALUES(new.rowid,1);
148      INSERT INTO t2(x,y) SELECT * FROM t2;
149      INSERT INTO t2 SELECT * FROM t2;
150      UPDATE t2 SET y=y+1 WHERE x=new.rowid;
151      SELECT 123;
152      DELETE FROM t2 WHERE x=new.rowid;
153    END;
154    INSERT INTO t1(a,b) VALUES(2,3);
155    COMMIT;
156  }
157}
158
159# Ensure that no file descriptors were leaked.
160do_test malloc-5.X {
161  catch {db close}
162  set sqlite_open_file_count
163} {0}
164
165ifcapable vacuum {
166  do_malloc_test 6 -sqlprep {
167    BEGIN TRANSACTION;
168    CREATE TABLE t1(a);
169    INSERT INTO t1 VALUES(1);
170    INSERT INTO t1 SELECT a*2 FROM t1;
171    INSERT INTO t1 SELECT a*2 FROM t1;
172    INSERT INTO t1 SELECT a*2 FROM t1;
173    INSERT INTO t1 SELECT a*2 FROM t1;
174    INSERT INTO t1 SELECT a*2 FROM t1;
175    INSERT INTO t1 SELECT a*2 FROM t1;
176    INSERT INTO t1 SELECT a*2 FROM t1;
177    INSERT INTO t1 SELECT a*2 FROM t1;
178    INSERT INTO t1 SELECT a*2 FROM t1;
179    INSERT INTO t1 SELECT a*2 FROM t1;
180    DELETE FROM t1 where rowid%5 = 0;
181    COMMIT;
182  } -sqlbody {
183    VACUUM;
184  }
185}
186
187do_malloc_test 7 -sqlprep {
188  CREATE TABLE t1(a, b);
189  INSERT INTO t1 VALUES(1, 2);
190  INSERT INTO t1 VALUES(3, 4);
191  INSERT INTO t1 VALUES(5, 6);
192  INSERT INTO t1 VALUES(7, randstr(1200,1200));
193} -sqlbody {
194  SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
195  SELECT a FROM t1 WHERE a<6 ORDER BY a;
196  SELECT b FROM t1 WHERE a>6;
197}
198
199# This block is designed to test that some malloc failures that may
200# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
201# when converting UTF-16 text to integers and real numbers is handled
202# correctly.
203#
204# This is done by retrieving a string from the database engine and
205# manipulating it using the sqlite3_column_*** APIs. This doesn't
206# actually return an error to the user when a malloc() fails.. That
207# could be viewed as a bug.
208#
209# These tests only run if UTF-16 support is compiled in.
210#
211ifcapable utf16 {
212  set ::STMT {}
213  do_malloc_test 8 -tclprep {
214    set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
215    set ::STMT [sqlite3_prepare db $sql -1 X]
216    sqlite3_step $::STMT
217    if { $::tcl_platform(byteOrder)=="littleEndian" } {
218      set ::bomstr "\xFF\xFE"
219    } else {
220      set ::bomstr "\xFE\xFF"
221    }
222    append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
223  } -tclbody {
224    sqlite3_column_text16 $::STMT 0
225    sqlite3_column_int $::STMT 0
226    sqlite3_column_text16 $::STMT 1
227    sqlite3_column_double $::STMT 1
228    set rc [sqlite3_reset $::STMT]
229    if {$rc eq "SQLITE_NOMEM"} {error "out of memory"}
230    sqlite3_bind_text16 $::STMT 1 $::bomstr 60
231    #catch {sqlite3_finalize $::STMT}
232    #if {[lindex [sqlite_malloc_stat] 2]<=0} {
233    #  error "out of memory"
234    #}
235  } -cleanup {
236    if {$::STMT!=""} {
237      sqlite3_finalize $::STMT
238      set ::STMT {}
239    }
240  }
241}
242
243# This block tests that malloc() failures that occur whilst commiting
244# a multi-file transaction are handled correctly.
245#
246do_malloc_test 9 -sqlprep {
247  ATTACH 'test2.db' as test2;
248  CREATE TABLE abc1(a, b, c);
249  CREATE TABLE test2.abc2(a, b, c);
250} -sqlbody {
251  BEGIN;
252  INSERT INTO abc1 VALUES(1, 2, 3);
253  INSERT INTO abc2 VALUES(1, 2, 3);
254  COMMIT;
255}
256
257# This block tests malloc() failures that occur while opening a
258# connection to a database.
259do_malloc_test 10 -tclprep {
260  catch {db2 close}
261  db close
262  file delete -force test.db test.db-journal
263  sqlite3 db test.db
264  db eval {CREATE TABLE abc(a, b, c)}
265} -tclbody {
266  db close
267  sqlite3 db2 test.db
268  db2 eval {SELECT * FROM sqlite_master}
269  db2 close
270}
271
272# This block tests malloc() failures that occur within calls to
273# sqlite3_create_function().
274do_malloc_test 11 -tclbody {
275  set rc [sqlite3_create_function db]
276  if {[string match $rc SQLITE_OK]} {
277    set rc [sqlite3_create_aggregate db]
278  }
279  if {[string match $rc SQLITE_NOMEM]} {
280    error "out of memory"
281  }
282}
283
284do_malloc_test 12 -tclbody {
285  set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
286  append sql16 "\00\00"
287  set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY]
288  sqlite3_finalize $::STMT
289}
290
291# Test malloc errors when replaying two hot journals from a 2-file
292# transaction.
293ifcapable crashtest {
294  do_malloc_test 13 -tclprep {
295    set rc [crashsql -delay 1 -file test2.db {
296      ATTACH 'test2.db' as aux;
297      PRAGMA cache_size = 10;
298      BEGIN;
299      CREATE TABLE aux.t2(a, b, c);
300      CREATE TABLE t1(a, b, c);
301      COMMIT;
302    }]
303    if {$rc!="1 {child process exited abnormally}"} {
304      error "Wrong error message: $rc"
305    }
306  } -tclbody {
307    db eval {ATTACH 'test2.db' as aux;}
308    set rc [catch {db eval {
309      SELECT * FROM t1;
310      SELECT * FROM t2;
311    }} err]
312    if {$rc && $err!="no such table: t1"} {
313      error $err
314    }
315  }
316}
317
318if {$tcl_platform(platform)!="windows"} {
319  do_malloc_test 14 -tclprep {
320    catch {db close}
321    sqlite3 db2 test2.db
322    db2 eval {
323      PRAGMA synchronous = 0;
324      CREATE TABLE t1(a, b);
325      INSERT INTO t1 VALUES(1, 2);
326      BEGIN;
327      INSERT INTO t1 VALUES(3, 4);
328    }
329    copy_file test2.db test.db
330    copy_file test2.db-journal test.db-journal
331    db2 close
332  } -tclbody {
333    sqlite3 db test.db
334    db eval {
335      SELECT * FROM t1;
336    }
337  }
338}
339
340proc string_compare {a b} {
341  return [string compare $a $b]
342}
343
344# Test for malloc() failures in sqlite3_create_collation() and
345# sqlite3_create_collation16().
346#
347ifcapable utf16 {
348  do_malloc_test 15 -start 4 -tclbody {
349    db collate string_compare string_compare
350    if {[catch {add_test_collate db 1 1 1} msg]} {
351      if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
352      error $msg
353    }
354
355    db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
356    db complete {-- Useful comment}
357
358    execsql {
359      CREATE TABLE t1(a, b COLLATE string_compare);
360      INSERT INTO t1 VALUES(10, 'string');
361      INSERT INTO t1 VALUES(10, 'string2');
362    }
363  }
364}
365
366# Also test sqlite3_complete(). There are (currently) no malloc()
367# calls in this function, but test anyway against future changes.
368#
369do_malloc_test 16 -tclbody {
370  db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
371  db complete {-- Useful comment}
372  db eval {
373    SELECT * FROM sqlite_master;
374  }
375}
376
377# Test handling of malloc() failures in sqlite3_open16().
378#
379ifcapable utf16 {
380  do_malloc_test 17 -tclbody {
381    set DB2 0
382    set STMT 0
383
384    # open database using sqlite3_open16()
385    set filename [encoding convertto unicode test.db]
386    append filename "\x00\x00"
387    set DB2 [sqlite3_open16 $filename -unused]
388    if {0==$DB2} {
389      error "out of memory"
390    }
391
392    # Prepare statement
393    set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
394    if {$rc} {
395      error [string range $msg 4 end]
396    }
397    set STMT $msg
398
399    # Finalize statement
400    set rc [sqlite3_finalize $STMT]
401    if {$rc!="SQLITE_OK"} {
402      error [sqlite3_errmsg $DB2]
403    }
404    set STMT 0
405
406    # Close database
407    set rc [sqlite3_close $DB2]
408    if {$rc!="SQLITE_OK"} {
409      error [sqlite3_errmsg $DB2]
410    }
411    set DB2 0
412  } -cleanup {
413    if {$STMT!="0"} {
414      sqlite3_finalize $STMT
415    }
416    if {$DB2!="0"} {
417      set rc [sqlite3_close $DB2]
418    }
419  }
420}
421
422# Test handling of malloc() failures in sqlite3_errmsg16().
423#
424ifcapable utf16 {
425  do_malloc_test 18 -tclbody {
426    catch {
427      db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
428    } msg
429    if {$msg=="out of memory"} {error $msg}
430    set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
431    binary scan $utf16 c* bytes
432    if {[llength $bytes]==0} {
433      error "out of memory"
434    }
435  }
436}
437
438# This test is aimed at coverage testing. Specificly, it is supposed to
439# cause a malloc() only used when converting between the two utf-16
440# encodings to fail (i.e. little-endian->big-endian). It only actually
441# hits this malloc() on little-endian hosts.
442#
443set static_string "\x00h\x00e\x00l\x00l\x00o"
444for {set l 0} {$l<10} {incr l} {
445  append static_string $static_string
446}
447append static_string "\x00\x00"
448do_malloc_test 19 -tclprep {
449  execsql {
450    PRAGMA encoding = "UTF16be";
451    CREATE TABLE abc(a, b, c);
452  }
453} -tclbody {
454  unset -nocomplain ::STMT
455  set r [catch {
456    set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY]
457    sqlite3_bind_text16 -static $::STMT 1 $static_string 112
458  } msg]
459  if {$r} {error [string range $msg 4 end]}
460  set msg
461} -cleanup {
462  if {[info exists ::STMT]} {
463    sqlite3_finalize $::STMT
464  }
465}
466unset static_string
467
468# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
469# when the malloc failure occurs within the nested parse.
470#
471do_malloc_test 20 -tclprep {
472  db close
473  file delete -force test2.db test2.db-journal
474  sqlite3 db test2.db
475  db eval {CREATE TABLE t1(x);}
476  db close
477} -tclbody {
478  if {[catch {sqlite3 db test.db}]} {
479    error "out of memory"
480  }
481} -sqlbody {
482  ATTACH DATABASE 'test2.db' AS t2;
483  SELECT * FROM t1;
484  DETACH DATABASE t2;
485}
486
487# Test malloc failure whilst installing a foreign key.
488#
489ifcapable foreignkey {
490  do_malloc_test 21 -sqlbody {
491    CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
492  }
493}
494
495# Test malloc failure in an sqlite3_prepare_v2() call.
496#
497do_malloc_test 22 -tclbody {
498  set ::STMT ""
499  set r [catch {
500    set ::STMT [
501      sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY
502    ]
503  } msg]
504  if {$r} {error [string range $msg 4 end]}
505} -cleanup {
506  if {$::STMT ne ""} {
507    sqlite3_finalize $::STMT
508    set ::STMT ""
509  }
510}
511
512# Ensure that no file descriptors were leaked.
513do_test malloc-99.X {
514  catch {db close}
515  set sqlite_open_file_count
516} {0}
517
518puts open-file-count=$sqlite_open_file_count
519finish_test
520