xref: /sqlite-3.40.0/test/malloc.test (revision 8a29dfde)
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.60 2008/04/03 10:13:01 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#
26source $testdir/malloc_common.tcl
27if {!$MEMDEBUG} {
28   puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
29   finish_test
30   return
31}
32
33# Do a couple of memory dumps just to exercise the memory dump logic
34# that that we can say that we have.
35#
36puts stderr "This is a test.  Ignore the error that follows:"
37sqlite3_memdebug_dump $testdir
38puts "Memory dump to file memdump.txt..."
39sqlite3_memdebug_dump memdump.txt
40
41ifcapable bloblit&&subquery {
42  do_malloc_test 1 -tclprep {
43    db close
44  } -tclbody {
45    if {[catch {sqlite3 db test.db}]} {
46      error "out of memory"
47    }
48    sqlite3_extended_result_codes db 1
49  } -sqlbody {
50    DROP TABLE IF EXISTS t1;
51    CREATE TABLE t1(
52       a int, b float, c double, d text, e varchar(20),
53       primary key(a,b,c)
54    );
55    CREATE INDEX i1 ON t1(a,b);
56    INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500');
57    INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
58    SELECT * FROM t1;
59    SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
60    DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
61    SELECT count(*), group_concat(e) FROM t1;
62    SELECT b FROM t1 ORDER BY 1 COLLATE nocase;
63  }
64}
65
66# Ensure that no file descriptors were leaked.
67do_test malloc-1.X {
68  catch {db close}
69  set sqlite_open_file_count
70} {0}
71
72ifcapable subquery {
73  do_malloc_test 2 -sqlbody {
74    CREATE TABLE t1(a int, b int default 'abc', c int default 1);
75    CREATE INDEX i1 ON t1(a,b);
76    INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
77    INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
78    INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
79    INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
80    INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
81    INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
82    SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
83    UPDATE t1 SET b=b||b||b||b;
84    UPDATE t1 SET b=a WHERE a in (10,12,22);
85    INSERT INTO t1(c,b,a) VALUES(20,10,5);
86    INSERT INTO t1 SELECT * FROM t1
87        WHERE a IN (SELECT a FROM t1 WHERE a<10);
88    DELETE FROM t1 WHERE a>=10;
89    DROP INDEX i1;
90    DELETE FROM t1;
91  }
92}
93
94# Ensure that no file descriptors were leaked.
95do_test malloc-2.X {
96  catch {db close}
97  set sqlite_open_file_count
98} {0}
99
100do_malloc_test 3 -sqlbody {
101  BEGIN TRANSACTION;
102  CREATE TABLE t1(a int, b int, c int);
103  CREATE INDEX i1 ON t1(a,b);
104  INSERT INTO t1 VALUES(1,1,99);
105  INSERT INTO t1 VALUES(2,4,98);
106  INSERT INTO t1 VALUES(3,9,97);
107  INSERT INTO t1 VALUES(4,16,96);
108  INSERT INTO t1 VALUES(5,25,95);
109  INSERT INTO t1 VALUES(6,36,94);
110  INSERT INTO t1(c,b,a) VALUES(20,10,5);
111  DELETE FROM t1 WHERE a>=10;
112  DROP INDEX i1;
113  DELETE FROM t1;
114  ROLLBACK;
115}
116
117
118# Ensure that no file descriptors were leaked.
119do_test malloc-3.X {
120  catch {db close}
121  set sqlite_open_file_count
122} {0}
123
124ifcapable subquery {
125  do_malloc_test 4 -sqlbody {
126    BEGIN TRANSACTION;
127    CREATE TABLE t1(a int, b int, c int);
128    CREATE INDEX i1 ON t1(a,b);
129    INSERT INTO t1 VALUES(1,1,99);
130    INSERT INTO t1 VALUES(2,4,98);
131    INSERT INTO t1 VALUES(3,9,97);
132    INSERT INTO t1 VALUES(4,16,96);
133    INSERT INTO t1 VALUES(5,25,95);
134    INSERT INTO t1 VALUES(6,36,94);
135    UPDATE t1 SET b=a WHERE a in (10,12,22);
136    INSERT INTO t1 SELECT * FROM t1
137       WHERE a IN (SELECT a FROM t1 WHERE a<10);
138    DROP INDEX i1;
139    DELETE FROM t1;
140    COMMIT;
141  }
142}
143
144# Ensure that no file descriptors were leaked.
145do_test malloc-4.X {
146  catch {db close}
147  set sqlite_open_file_count
148} {0}
149
150ifcapable trigger {
151  do_malloc_test 5 -sqlbody {
152    BEGIN TRANSACTION;
153    CREATE TABLE t1(a,b);
154    CREATE TABLE t2(x,y);
155    CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN
156      INSERT INTO t2(x,y) VALUES(new.rowid,1);
157      INSERT INTO t2(x,y) SELECT * FROM t2;
158      INSERT INTO t2 SELECT * FROM t2;
159      UPDATE t2 SET y=y+1 WHERE x=new.rowid;
160      SELECT 123;
161      DELETE FROM t2 WHERE x=new.rowid;
162    END;
163    INSERT INTO t1(a,b) VALUES(2,3);
164    COMMIT;
165  }
166}
167
168# Ensure that no file descriptors were leaked.
169do_test malloc-5.X {
170  catch {db close}
171  set sqlite_open_file_count
172} {0}
173
174ifcapable vacuum {
175  do_malloc_test 6 -sqlprep {
176    BEGIN TRANSACTION;
177    CREATE TABLE t1(a);
178    INSERT INTO t1 VALUES(1);
179    INSERT INTO t1 SELECT a*2 FROM t1;
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    DELETE FROM t1 where rowid%5 = 0;
190    COMMIT;
191  } -sqlbody {
192    VACUUM;
193  }
194}
195
196do_malloc_test 7 -sqlprep {
197  CREATE TABLE t1(a, b);
198  INSERT INTO t1 VALUES(1, 2);
199  INSERT INTO t1 VALUES(3, 4);
200  INSERT INTO t1 VALUES(5, 6);
201  INSERT INTO t1 VALUES(7, randstr(1200,1200));
202} -sqlbody {
203  SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
204  SELECT a FROM t1 WHERE a<6 ORDER BY a;
205  SELECT b FROM t1 WHERE a>6;
206}
207
208# This block is designed to test that some malloc failures that may
209# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
210# when converting UTF-16 text to integers and real numbers is handled
211# correctly.
212#
213# This is done by retrieving a string from the database engine and
214# manipulating it using the sqlite3_column_*** APIs. This doesn't
215# actually return an error to the user when a malloc() fails.. That
216# could be viewed as a bug.
217#
218# These tests only run if UTF-16 support is compiled in.
219#
220ifcapable utf16 {
221  set ::STMT {}
222  do_malloc_test 8 -tclprep {
223    set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
224    set ::STMT [sqlite3_prepare db $sql -1 X]
225    sqlite3_step $::STMT
226    if { $::tcl_platform(byteOrder)=="littleEndian" } {
227      set ::bomstr "\xFF\xFE"
228    } else {
229      set ::bomstr "\xFE\xFF"
230    }
231    append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
232  } -tclbody {
233    sqlite3_column_text16 $::STMT 0
234    sqlite3_column_int $::STMT 0
235    sqlite3_column_text16 $::STMT 1
236    sqlite3_column_double $::STMT 1
237    set rc [sqlite3_reset $::STMT]
238    if {$rc eq "SQLITE_NOMEM"} {error "out of memory"}
239    sqlite3_bind_text16 $::STMT 1 $::bomstr 60
240    #catch {sqlite3_finalize $::STMT}
241    #if {[lindex [sqlite_malloc_stat] 2]<=0} {
242    #  error "out of memory"
243    #}
244  } -cleanup {
245    if {$::STMT!=""} {
246      sqlite3_finalize $::STMT
247      set ::STMT {}
248    }
249  }
250}
251
252# This block tests that malloc() failures that occur whilst commiting
253# a multi-file transaction are handled correctly.
254#
255breakpoint
256do_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;
265}
266
267# This block tests malloc() failures that occur while opening a
268# connection to a database.
269do_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
274  sqlite3_extended_result_codes db 1
275  db eval {CREATE TABLE abc(a, b, c)}
276} -tclbody {
277  db close
278  sqlite3 db2 test.db
279  sqlite3_extended_result_codes db2 1
280  db2 eval {SELECT * FROM sqlite_master}
281  db2 close
282}
283
284# This block tests malloc() failures that occur within calls to
285# sqlite3_create_function().
286do_malloc_test 11 -tclbody {
287  set rc [sqlite3_create_function db]
288  if {[string match $rc SQLITE_OK]} {
289    set rc [sqlite3_create_aggregate db]
290  }
291  if {[string match $rc SQLITE_NOMEM]} {
292    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"
299  set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY]
300  sqlite3_finalize $::STMT
301}
302
303# Test malloc errors when replaying two hot journals from a 2-file
304# transaction.
305ifcapable crashtest&&attach {
306  do_malloc_test 13 -tclprep {
307    set rc [crashsql -delay 1 -file test2.db {
308      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    }
318  } -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    }
327  }
328}
329
330if {$tcl_platform(platform)!="windows"} {
331  do_malloc_test 14 -tclprep {
332    catch {db close}
333    sqlite3 db2 test2.db
334    sqlite3_extended_result_codes db2 1
335    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
347    sqlite3_extended_result_codes db 1
348    db eval {
349      SELECT * FROM t1;
350    }
351  }
352}
353
354proc string_compare {a b} {
355  return [string compare $a $b]
356}
357
358# Test for malloc() failures in sqlite3_create_collation() and
359# sqlite3_create_collation16().
360#
361ifcapable utf16 {
362  do_malloc_test 15 -start 4 -tclbody {
363    db collate string_compare string_compare
364    if {[catch {add_test_collate db 1 1 1} msg]} {
365      if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
366      error $msg
367    }
368
369    db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
370    db complete {-- Useful comment}
371
372    execsql {
373      CREATE TABLE t1(a, b COLLATE string_compare);
374      INSERT INTO t1 VALUES(10, 'string');
375      INSERT INTO t1 VALUES(10, 'string2');
376    }
377  }
378}
379
380# Also test sqlite3_complete(). There are (currently) no malloc()
381# calls in this function, but test anyway against future changes.
382#
383do_malloc_test 16 -tclbody {
384  db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
385  db complete {-- Useful comment}
386  db eval {
387    SELECT * FROM sqlite_master;
388  }
389}
390
391# Test handling of malloc() failures in sqlite3_open16().
392#
393ifcapable utf16 {
394  do_malloc_test 17 -tclbody {
395    set DB2 0
396    set STMT 0
397
398    # open database using sqlite3_open16()
399    set filename [encoding convertto unicode test.db]
400    append filename "\x00\x00"
401    set DB2 [sqlite3_open16 $filename -unused]
402    if {0==$DB2} {
403      error "out of memory"
404    }
405    sqlite3_extended_result_codes $DB2 1
406
407    # Prepare statement
408    set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
409    if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} {
410      error "out of memory"
411    }
412    if {$rc} {
413      error [string range $msg 4 end]
414    }
415    set STMT $msg
416
417    # Finalize statement
418    set rc [sqlite3_finalize $STMT]
419    if {$rc!="SQLITE_OK"} {
420      error [sqlite3_errmsg $DB2]
421    }
422    set STMT 0
423
424    # Close database
425    set rc [sqlite3_close $DB2]
426    if {$rc!="SQLITE_OK"} {
427      error [sqlite3_errmsg $DB2]
428    }
429    set DB2 0
430  } -cleanup {
431    if {$STMT!="0"} {
432      sqlite3_finalize $STMT
433    }
434    if {$DB2!="0"} {
435      set rc [sqlite3_close $DB2]
436    }
437  }
438}
439
440# Test handling of malloc() failures in sqlite3_errmsg16().
441#
442ifcapable utf16 {
443  do_malloc_test 18 -tclprep {
444    catch {
445      db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
446    }
447  } -tclbody {
448    set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
449    binary scan $utf16 c* bytes
450    if {[llength $bytes]==0} {
451      error "out of memory"
452    }
453  }
454}
455
456# This test is aimed at coverage testing. Specificly, it is supposed to
457# cause a malloc() only used when converting between the two utf-16
458# encodings to fail (i.e. little-endian->big-endian). It only actually
459# hits this malloc() on little-endian hosts.
460#
461set static_string "\x00h\x00e\x00l\x00l\x00o"
462for {set l 0} {$l<10} {incr l} {
463  append static_string $static_string
464}
465append static_string "\x00\x00"
466do_malloc_test 19 -tclprep {
467  execsql {
468    PRAGMA encoding = "UTF16be";
469    CREATE TABLE abc(a, b, c);
470  }
471} -tclbody {
472  unset -nocomplain ::STMT
473  set r [catch {
474    set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY]
475    sqlite3_bind_text16 -static $::STMT 1 $static_string 112
476  } msg]
477  if {$r} {error [string range $msg 4 end]}
478  set msg
479} -cleanup {
480  if {[info exists ::STMT]} {
481    sqlite3_finalize $::STMT
482  }
483}
484unset static_string
485
486# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
487# when the malloc failure occurs within the nested parse.
488#
489ifcapable attach {
490  do_malloc_test 20 -tclprep {
491    db close
492    file delete -force test2.db test2.db-journal
493    sqlite3 db test2.db
494    sqlite3_extended_result_codes db 1
495    db eval {CREATE TABLE t1(x);}
496    db close
497  } -tclbody {
498    if {[catch {sqlite3 db test.db}]} {
499      error "out of memory"
500    }
501    sqlite3_extended_result_codes db 1
502  } -sqlbody {
503    ATTACH DATABASE 'test2.db' AS t2;
504    SELECT * FROM t1;
505    DETACH DATABASE t2;
506  }
507}
508
509# Test malloc failure whilst installing a foreign key.
510#
511ifcapable foreignkey {
512  do_malloc_test 21 -sqlbody {
513    CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
514  }
515}
516
517# Test malloc failure in an sqlite3_prepare_v2() call.
518#
519do_malloc_test 22 -tclbody {
520  set ::STMT ""
521  set r [catch {
522    set ::STMT [
523      sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY
524    ]
525  } msg]
526  if {$r} {error [string range $msg 4 end]}
527} -cleanup {
528  if {$::STMT ne ""} {
529    sqlite3_finalize $::STMT
530    set ::STMT ""
531  }
532}
533
534ifcapable {pager_pragmas} {
535  # This tests a special case - that an error that occurs while the pager
536  # is trying to recover from error-state in exclusive-access mode works.
537  #
538  do_malloc_test 23 -tclprep {
539    db eval {
540      PRAGMA cache_size = 10;
541      PRAGMA locking_mode = exclusive;
542      BEGIN;
543      CREATE TABLE abc(a, b, c);
544      CREATE INDEX abc_i ON abc(a, b, c);
545      INSERT INTO abc
546        VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
547      INSERT INTO abc
548        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
549      INSERT INTO abc
550        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
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      COMMIT;
558    }
559
560    # This puts the pager into error state.
561    #
562    db eval BEGIN
563    db eval {UPDATE abc SET a = 0 WHERE oid%2}
564    set ::sqlite_io_error_pending 10
565    catch {db eval {ROLLBACK}} msg
566
567  } -sqlbody {
568    SELECT * FROM abc LIMIT 10;
569  } -cleanup {
570    set e [db eval {PRAGMA integrity_check}]
571    if {$e ne "ok"} {error $e}
572  }
573}
574
575ifcapable compound {
576  do_malloc_test 24 -sqlprep {
577    CREATE TABLE t1(a, b, c)
578  } -sqlbody {
579    SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1
580  }
581}
582
583ifcapable view&&trigger {
584  do_malloc_test 25 -sqlprep {
585    CREATE TABLE t1(a, b, c);
586    CREATE VIEW v1 AS SELECT * FROM t1;
587    CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END;
588    CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END;
589    CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END;
590  } -sqlbody {
591    DELETE FROM v1 WHERE a = 1;
592    INSERT INTO v1 VALUES(1, 2, 3);
593    UPDATE v1 SET a = 1 WHERE b = 2;
594  }
595}
596
597# Ensure that no file descriptors were leaked.
598do_test malloc-99.X {
599  catch {db close}
600  set sqlite_open_file_count
601} {0}
602
603puts open-file-count=$sqlite_open_file_count
604finish_test
605