xref: /sqlite-3.40.0/test/malloc.test (revision bb77b753)
1b19a2bc6Sdrh# 2001 September 15
2ed7c855cSdrh#
3b19a2bc6Sdrh# The author disclaims copyright to this source code.  In place of
4b19a2bc6Sdrh# a legal notice, here is a blessing:
5ed7c855cSdrh#
6b19a2bc6Sdrh#    May you do good and not evil.
7b19a2bc6Sdrh#    May you find forgiveness for yourself and forgive others.
8b19a2bc6Sdrh#    May you share freely, never taking more than you give.
9ed7c855cSdrh#
10ed7c855cSdrh#***********************************************************************
11ed7c855cSdrh#
12f3a65f7eSdrh# This file attempts to check the behavior of the SQLite library in
13f3a65f7eSdrh# an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1,
14f3a65f7eSdrh# the SQLite library accepts a special command (sqlite3_memdebug_fail N C)
15f3a65f7eSdrh# which causes the N-th malloc to fail.  This special feature is used
16f3a65f7eSdrh# to see what happens in the library if a malloc were to really fail
17f3a65f7eSdrh# due to an out-of-memory situation.
18f3a65f7eSdrh#
19*bb77b753Sdrh# $Id: malloc.test,v 1.78 2009/04/09 01:23:49 drh Exp $
20ed7c855cSdrh
21ed7c855cSdrhset testdir [file dirname $argv0]
22ed7c855cSdrhsource $testdir/tester.tcl
23ed7c855cSdrh
24d09414cdSdanielk1977
25ed7c855cSdrh# Only run these tests if memory debugging is turned on.
26ed7c855cSdrh#
27eee4c8caSdrhsource $testdir/malloc_common.tcl
28eee4c8caSdrhif {!$MEMDEBUG} {
29261919ccSdanielk1977   puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
30ed7c855cSdrh   finish_test
31ed7c855cSdrh   return
32ed7c855cSdrh}
33ed7c855cSdrh
34de4fcfddSdrh# Do a couple of memory dumps just to exercise the memory dump logic
35de4fcfddSdrh# that that we can say that we have.
36de4fcfddSdrh#
37de4fcfddSdrhputs stderr "This is a test.  Ignore the error that follows:"
38de4fcfddSdrhsqlite3_memdebug_dump $testdir
39de4fcfddSdrhputs "Memory dump to file memdump.txt..."
40de4fcfddSdrhsqlite3_memdebug_dump memdump.txt
41de4fcfddSdrh
424152e677Sdanielk1977ifcapable bloblit&&subquery {
43c08d405dSdanielk1977  do_malloc_test 1 -tclprep {
44c08d405dSdanielk1977    db close
45c08d405dSdanielk1977  } -tclbody {
46c08d405dSdanielk1977    if {[catch {sqlite3 db test.db}]} {
47c08d405dSdanielk1977      error "out of memory"
48c08d405dSdanielk1977    }
49ae72d982Sdanielk1977    sqlite3_extended_result_codes db 1
50c08d405dSdanielk1977  } -sqlbody {
51344a6276Sdrh    DROP TABLE IF EXISTS t1;
52ed7c855cSdrh    CREATE TABLE t1(
53ed7c855cSdrh       a int, b float, c double, d text, e varchar(20),
54ed7c855cSdrh       primary key(a,b,c)
55ed7c855cSdrh    );
56ed7c855cSdrh    CREATE INDEX i1 ON t1(a,b);
5776f80796Sdrh    INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500');
58ed7c855cSdrh    INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
59ed7c855cSdrh    SELECT * FROM t1;
60d400728aSdrh    SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
616d4abfbeSdrh    DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
62ade86483Sdrh    SELECT count(*), group_concat(e) FROM t1;
6315cdbebeSdanielk1977    SELECT b FROM t1 ORDER BY 1 COLLATE nocase;
64ed7c855cSdrh  }
654152e677Sdanielk1977}
66d400728aSdrh
67b5548a8bSdanielk1977# Ensure that no file descriptors were leaked.
68b5548a8bSdanielk1977do_test malloc-1.X {
69b5548a8bSdanielk1977  catch {db close}
70b5548a8bSdanielk1977  set sqlite_open_file_count
71b5548a8bSdanielk1977} {0}
72b5548a8bSdanielk1977
734152e677Sdanielk1977ifcapable subquery {
74c08d405dSdanielk1977  do_malloc_test 2 -sqlbody {
75fc23314aSdrh    CREATE TABLE t1(a int, b int default 'abc', c int default 1);
76d400728aSdrh    CREATE INDEX i1 ON t1(a,b);
775f3b4ab5Sdrh    INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
785f3b4ab5Sdrh    INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
795f3b4ab5Sdrh    INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
805f3b4ab5Sdrh    INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
815f3b4ab5Sdrh    INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
825f3b4ab5Sdrh    INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
836d4abfbeSdrh    SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
846d4abfbeSdrh    UPDATE t1 SET b=b||b||b||b;
85d400728aSdrh    UPDATE t1 SET b=a WHERE a in (10,12,22);
866d4abfbeSdrh    INSERT INTO t1(c,b,a) VALUES(20,10,5);
876d4abfbeSdrh    INSERT INTO t1 SELECT * FROM t1
886d4abfbeSdrh        WHERE a IN (SELECT a FROM t1 WHERE a<10);
896d4abfbeSdrh    DELETE FROM t1 WHERE a>=10;
90d400728aSdrh    DROP INDEX i1;
916d4abfbeSdrh    DELETE FROM t1;
926d4abfbeSdrh  }
934152e677Sdanielk1977}
946d4abfbeSdrh
95b5548a8bSdanielk1977# Ensure that no file descriptors were leaked.
96b5548a8bSdanielk1977do_test malloc-2.X {
97b5548a8bSdanielk1977  catch {db close}
98b5548a8bSdanielk1977  set sqlite_open_file_count
99b5548a8bSdanielk1977} {0}
100b5548a8bSdanielk1977
101c08d405dSdanielk1977do_malloc_test 3 -sqlbody {
1026d4abfbeSdrh  BEGIN TRANSACTION;
1036d4abfbeSdrh  CREATE TABLE t1(a int, b int, c int);
1046d4abfbeSdrh  CREATE INDEX i1 ON t1(a,b);
1055f3b4ab5Sdrh  INSERT INTO t1 VALUES(1,1,99);
1065f3b4ab5Sdrh  INSERT INTO t1 VALUES(2,4,98);
1075f3b4ab5Sdrh  INSERT INTO t1 VALUES(3,9,97);
1085f3b4ab5Sdrh  INSERT INTO t1 VALUES(4,16,96);
1095f3b4ab5Sdrh  INSERT INTO t1 VALUES(5,25,95);
1105f3b4ab5Sdrh  INSERT INTO t1 VALUES(6,36,94);
1116d4abfbeSdrh  INSERT INTO t1(c,b,a) VALUES(20,10,5);
1126d4abfbeSdrh  DELETE FROM t1 WHERE a>=10;
1136d4abfbeSdrh  DROP INDEX i1;
1146d4abfbeSdrh  DELETE FROM t1;
1156d4abfbeSdrh  ROLLBACK;
1166d4abfbeSdrh}
117c08d405dSdanielk1977
118b5548a8bSdanielk1977
119b5548a8bSdanielk1977# Ensure that no file descriptors were leaked.
120b5548a8bSdanielk1977do_test malloc-3.X {
121b5548a8bSdanielk1977  catch {db close}
122b5548a8bSdanielk1977  set sqlite_open_file_count
123b5548a8bSdanielk1977} {0}
124b5548a8bSdanielk1977
1254152e677Sdanielk1977ifcapable subquery {
126c08d405dSdanielk1977  do_malloc_test 4 -sqlbody {
1276d4abfbeSdrh    BEGIN TRANSACTION;
1286d4abfbeSdrh    CREATE TABLE t1(a int, b int, c int);
1296d4abfbeSdrh    CREATE INDEX i1 ON t1(a,b);
1305f3b4ab5Sdrh    INSERT INTO t1 VALUES(1,1,99);
1315f3b4ab5Sdrh    INSERT INTO t1 VALUES(2,4,98);
1325f3b4ab5Sdrh    INSERT INTO t1 VALUES(3,9,97);
1335f3b4ab5Sdrh    INSERT INTO t1 VALUES(4,16,96);
1345f3b4ab5Sdrh    INSERT INTO t1 VALUES(5,25,95);
1355f3b4ab5Sdrh    INSERT INTO t1 VALUES(6,36,94);
1366d4abfbeSdrh    UPDATE t1 SET b=a WHERE a in (10,12,22);
1376d4abfbeSdrh    INSERT INTO t1 SELECT * FROM t1
1386d4abfbeSdrh       WHERE a IN (SELECT a FROM t1 WHERE a<10);
1396d4abfbeSdrh    DROP INDEX i1;
1406d4abfbeSdrh    DELETE FROM t1;
1416d4abfbeSdrh    COMMIT;
142d400728aSdrh  }
1434152e677Sdanielk1977}
144b5548a8bSdanielk1977
145b5548a8bSdanielk1977# Ensure that no file descriptors were leaked.
146b5548a8bSdanielk1977do_test malloc-4.X {
147b5548a8bSdanielk1977  catch {db close}
148b5548a8bSdanielk1977  set sqlite_open_file_count
149b5548a8bSdanielk1977} {0}
150b5548a8bSdanielk1977
1514152e677Sdanielk1977ifcapable trigger {
152c08d405dSdanielk1977  do_malloc_test 5 -sqlbody {
153e4697f5eSdrh    BEGIN TRANSACTION;
154e4697f5eSdrh    CREATE TABLE t1(a,b);
155e4697f5eSdrh    CREATE TABLE t2(x,y);
156932083c6Sdanielk1977    CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN
157e4697f5eSdrh      INSERT INTO t2(x,y) VALUES(new.rowid,1);
1589fb3ecbcSdanielk1977      INSERT INTO t2(x,y) SELECT * FROM t2;
1599fb3ecbcSdanielk1977      INSERT INTO t2 SELECT * FROM t2;
16028f45914Sdrh      UPDATE t2 SET y=y+1 WHERE x=new.rowid;
16128f45914Sdrh      SELECT 123;
16228f45914Sdrh      DELETE FROM t2 WHERE x=new.rowid;
163e4697f5eSdrh    END;
164e4697f5eSdrh    INSERT INTO t1(a,b) VALUES(2,3);
165e4697f5eSdrh    COMMIT;
166e4697f5eSdrh  }
1674152e677Sdanielk1977}
168b5548a8bSdanielk1977
169b5548a8bSdanielk1977# Ensure that no file descriptors were leaked.
170b5548a8bSdanielk1977do_test malloc-5.X {
171b5548a8bSdanielk1977  catch {db close}
172b5548a8bSdanielk1977  set sqlite_open_file_count
173b5548a8bSdanielk1977} {0}
174b5548a8bSdanielk1977
1754152e677Sdanielk1977ifcapable vacuum {
176c08d405dSdanielk1977  do_malloc_test 6 -sqlprep {
17796fb0dd5Sdanielk1977    BEGIN TRANSACTION;
17896fb0dd5Sdanielk1977    CREATE TABLE t1(a);
17996fb0dd5Sdanielk1977    INSERT INTO t1 VALUES(1);
18096fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18196fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18296fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18396fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18496fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18596fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18696fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18796fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18896fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
18996fb0dd5Sdanielk1977    INSERT INTO t1 SELECT a*2 FROM t1;
19096fb0dd5Sdanielk1977    DELETE FROM t1 where rowid%5 = 0;
19196fb0dd5Sdanielk1977    COMMIT;
192c08d405dSdanielk1977  } -sqlbody {
19396fb0dd5Sdanielk1977    VACUUM;
19496fb0dd5Sdanielk1977  }
1954152e677Sdanielk1977}
19696fb0dd5Sdanielk1977
197*bb77b753Sdrhautoinstall_test_functions
198c08d405dSdanielk1977do_malloc_test 7 -sqlprep {
19901427a62Sdanielk1977  CREATE TABLE t1(a, b);
20001427a62Sdanielk1977  INSERT INTO t1 VALUES(1, 2);
20101427a62Sdanielk1977  INSERT INTO t1 VALUES(3, 4);
20201427a62Sdanielk1977  INSERT INTO t1 VALUES(5, 6);
203b5402fbfSdanielk1977  INSERT INTO t1 VALUES(7, randstr(1200,1200));
204c08d405dSdanielk1977} -sqlbody {
205b5402fbfSdanielk1977  SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
206b5402fbfSdanielk1977  SELECT a FROM t1 WHERE a<6 ORDER BY a;
207b5402fbfSdanielk1977  SELECT b FROM t1 WHERE a>6;
20801427a62Sdanielk1977}
20901427a62Sdanielk1977
210b5402fbfSdanielk1977# This block is designed to test that some malloc failures that may
211b5402fbfSdanielk1977# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
212b5402fbfSdanielk1977# when converting UTF-16 text to integers and real numbers is handled
213b5402fbfSdanielk1977# correctly.
214b5402fbfSdanielk1977#
2158b60e0f1Sdanielk1977# This is done by retrieving a string from the database engine and
2168b60e0f1Sdanielk1977# manipulating it using the sqlite3_column_*** APIs. This doesn't
2178b60e0f1Sdanielk1977# actually return an error to the user when a malloc() fails.. That
2188b60e0f1Sdanielk1977# could be viewed as a bug.
2198b60e0f1Sdanielk1977#
2208b60e0f1Sdanielk1977# These tests only run if UTF-16 support is compiled in.
221b5402fbfSdanielk1977#
2224152e677Sdanielk1977ifcapable utf16 {
223f3a65f7eSdrh  set ::STMT {}
224c08d405dSdanielk1977  do_malloc_test 8 -tclprep {
225b5402fbfSdanielk1977    set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
226f3a65f7eSdrh    set ::STMT [sqlite3_prepare db $sql -1 X]
227b5402fbfSdanielk1977    sqlite3_step $::STMT
228b5402fbfSdanielk1977    if { $::tcl_platform(byteOrder)=="littleEndian" } {
229b5402fbfSdanielk1977      set ::bomstr "\xFF\xFE"
230b5402fbfSdanielk1977    } else {
231b5402fbfSdanielk1977      set ::bomstr "\xFE\xFF"
232b5402fbfSdanielk1977    }
233b5402fbfSdanielk1977    append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
234c08d405dSdanielk1977  } -tclbody {
235b5402fbfSdanielk1977    sqlite3_column_text16 $::STMT 0
236b5402fbfSdanielk1977    sqlite3_column_int $::STMT 0
237b5402fbfSdanielk1977    sqlite3_column_text16 $::STMT 1
238b5402fbfSdanielk1977    sqlite3_column_double $::STMT 1
239a1644fd8Sdanielk1977    set rc [sqlite3_reset $::STMT]
240a1644fd8Sdanielk1977    if {$rc eq "SQLITE_NOMEM"} {error "out of memory"}
241b5402fbfSdanielk1977    sqlite3_bind_text16 $::STMT 1 $::bomstr 60
242f3a65f7eSdrh    #catch {sqlite3_finalize $::STMT}
243f3a65f7eSdrh    #if {[lindex [sqlite_malloc_stat] 2]<=0} {
244f3a65f7eSdrh    #  error "out of memory"
245f3a65f7eSdrh    #}
246c08d405dSdanielk1977  } -cleanup {
247f3a65f7eSdrh    if {$::STMT!=""} {
248b5402fbfSdanielk1977      sqlite3_finalize $::STMT
249f3a65f7eSdrh      set ::STMT {}
250f3a65f7eSdrh    }
251b5402fbfSdanielk1977  }
252b5402fbfSdanielk1977}
253b5402fbfSdanielk1977
2548b60e0f1Sdanielk1977# This block tests that malloc() failures that occur whilst commiting
2558b60e0f1Sdanielk1977# a multi-file transaction are handled correctly.
2568b60e0f1Sdanielk1977#
2574397de57Sdanielk1977do_malloc_test 9 -sqlprep {
2588b60e0f1Sdanielk1977  ATTACH 'test2.db' as test2;
2598b60e0f1Sdanielk1977  CREATE TABLE abc1(a, b, c);
2608b60e0f1Sdanielk1977  CREATE TABLE test2.abc2(a, b, c);
2614397de57Sdanielk1977} -sqlbody {
2628b60e0f1Sdanielk1977  BEGIN;
2638b60e0f1Sdanielk1977  INSERT INTO abc1 VALUES(1, 2, 3);
2648b60e0f1Sdanielk1977  INSERT INTO abc2 VALUES(1, 2, 3);
2658b60e0f1Sdanielk1977  COMMIT;
2668b60e0f1Sdanielk1977}
2678b60e0f1Sdanielk1977
2684397de57Sdanielk1977# This block tests malloc() failures that occur while opening a
2694397de57Sdanielk1977# connection to a database.
270dc05efb4Sdrhdo_malloc_test 10 -tclprep {
271dc05efb4Sdrh  catch {db2 close}
272dc05efb4Sdrh  db close
273dc05efb4Sdrh  file delete -force test.db test.db-journal
274dc05efb4Sdrh  sqlite3 db test.db
275ae72d982Sdanielk1977  sqlite3_extended_result_codes db 1
276dc05efb4Sdrh  db eval {CREATE TABLE abc(a, b, c)}
2774397de57Sdanielk1977} -tclbody {
278c5859718Sdanielk1977  db close
279771151b6Sdanielk1977  sqlite3 db2 test.db
280ae72d982Sdanielk1977  sqlite3_extended_result_codes db2 1
2814397de57Sdanielk1977  db2 eval {SELECT * FROM sqlite_master}
2824397de57Sdanielk1977  db2 close
2834397de57Sdanielk1977}
2844397de57Sdanielk1977
2854397de57Sdanielk1977# This block tests malloc() failures that occur within calls to
2864397de57Sdanielk1977# sqlite3_create_function().
2874397de57Sdanielk1977do_malloc_test 11 -tclbody {
288f3a65f7eSdrh  set rc [sqlite3_create_function db]
289fa18beceSdanielk1977  if {[string match $rc SQLITE_OK]} {
290fa18beceSdanielk1977    set rc [sqlite3_create_aggregate db]
291fa18beceSdanielk1977  }
2922c336549Sdanielk1977  if {[string match $rc SQLITE_NOMEM]} {
2934397de57Sdanielk1977    error "out of memory"
2944397de57Sdanielk1977  }
2954397de57Sdanielk1977}
2964397de57Sdanielk1977
2974397de57Sdanielk1977do_malloc_test 12 -tclbody {
2984397de57Sdanielk1977  set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
2994397de57Sdanielk1977  append sql16 "\00\00"
300f3a65f7eSdrh  set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY]
3014397de57Sdanielk1977  sqlite3_finalize $::STMT
3024397de57Sdanielk1977}
3038b60e0f1Sdanielk1977
304aca790acSdanielk1977# Test malloc errors when replaying two hot journals from a 2-file
30566560adaSdrh# transaction.
3065a8f9374Sdanielk1977ifcapable crashtest&&attach {
307aca790acSdanielk1977  do_malloc_test 13 -tclprep {
30859a33f98Sdanielk1977    set rc [crashsql -delay 1 -file test2.db {
309aca790acSdanielk1977      ATTACH 'test2.db' as aux;
310aca790acSdanielk1977      PRAGMA cache_size = 10;
311aca790acSdanielk1977      BEGIN;
312aca790acSdanielk1977      CREATE TABLE aux.t2(a, b, c);
313aca790acSdanielk1977      CREATE TABLE t1(a, b, c);
314aca790acSdanielk1977      COMMIT;
315aca790acSdanielk1977    }]
316aca790acSdanielk1977    if {$rc!="1 {child process exited abnormally}"} {
317aca790acSdanielk1977      error "Wrong error message: $rc"
318aca790acSdanielk1977    }
319950f054cSdanielk1977  } -tclbody {
320950f054cSdanielk1977    db eval {ATTACH 'test2.db' as aux;}
321950f054cSdanielk1977    set rc [catch {db eval {
322aca790acSdanielk1977      SELECT * FROM t1;
323aca790acSdanielk1977      SELECT * FROM t2;
324950f054cSdanielk1977    }} err]
325950f054cSdanielk1977    if {$rc && $err!="no such table: t1"} {
326950f054cSdanielk1977      error $err
327950f054cSdanielk1977    }
328aca790acSdanielk1977  }
329aca790acSdanielk1977}
330aca790acSdanielk1977
33176b047d9Sdanielk1977if {$tcl_platform(platform)!="windows"} {
332aca790acSdanielk1977  do_malloc_test 14 -tclprep {
333aca790acSdanielk1977    catch {db close}
334aca790acSdanielk1977    sqlite3 db2 test2.db
335ae72d982Sdanielk1977    sqlite3_extended_result_codes db2 1
336aca790acSdanielk1977    db2 eval {
337aca790acSdanielk1977      PRAGMA synchronous = 0;
338aca790acSdanielk1977      CREATE TABLE t1(a, b);
339aca790acSdanielk1977      INSERT INTO t1 VALUES(1, 2);
340aca790acSdanielk1977      BEGIN;
341aca790acSdanielk1977      INSERT INTO t1 VALUES(3, 4);
342aca790acSdanielk1977    }
34332554c10Sdanielk1977    copy_file test2.db test.db
34432554c10Sdanielk1977    copy_file test2.db-journal test.db-journal
345aca790acSdanielk1977    db2 close
346aca790acSdanielk1977  } -tclbody {
347aca790acSdanielk1977    sqlite3 db test.db
348ae72d982Sdanielk1977    sqlite3_extended_result_codes db 1
34998c21903Sdanielk1977
35098c21903Sdanielk1977    # If an out-of-memory occurs within a call to a VFS layer function during
35198c21903Sdanielk1977    # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
35298c21903Sdanielk1977    # [5668] for details.
35398c21903Sdanielk1977    set rc [catch {db eval { SELECT * FROM t1 }} msg]
35498c21903Sdanielk1977    if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
35598c21903Sdanielk1977    if {$rc} { error $msg }
35698c21903Sdanielk1977    set msg
357aca790acSdanielk1977  }
35876b047d9Sdanielk1977}
359aca790acSdanielk1977
3609a30cf65Sdanielk1977proc string_compare {a b} {
3619a30cf65Sdanielk1977  return [string compare $a $b]
3629a30cf65Sdanielk1977}
3639a30cf65Sdanielk1977
3649a30cf65Sdanielk1977# Test for malloc() failures in sqlite3_create_collation() and
3659a30cf65Sdanielk1977# sqlite3_create_collation16().
366950f054cSdanielk1977#
3674152e677Sdanielk1977ifcapable utf16 {
368f3a65f7eSdrh  do_malloc_test 15 -start 4 -tclbody {
3699a30cf65Sdanielk1977    db collate string_compare string_compare
370f3a65f7eSdrh    if {[catch {add_test_collate db 1 1 1} msg]} {
3719a30cf65Sdanielk1977      if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
3729a30cf65Sdanielk1977      error $msg
3739a30cf65Sdanielk1977    }
374950f054cSdanielk1977
375950f054cSdanielk1977    db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
376950f054cSdanielk1977    db complete {-- Useful comment}
377950f054cSdanielk1977
3789a30cf65Sdanielk1977    execsql {
3799a30cf65Sdanielk1977      CREATE TABLE t1(a, b COLLATE string_compare);
3809a30cf65Sdanielk1977      INSERT INTO t1 VALUES(10, 'string');
3819a30cf65Sdanielk1977      INSERT INTO t1 VALUES(10, 'string2');
3829a30cf65Sdanielk1977    }
3839a30cf65Sdanielk1977  }
3844152e677Sdanielk1977}
3859a30cf65Sdanielk1977
386950f054cSdanielk1977# Also test sqlite3_complete(). There are (currently) no malloc()
387950f054cSdanielk1977# calls in this function, but test anyway against future changes.
388950f054cSdanielk1977#
389950f054cSdanielk1977do_malloc_test 16 -tclbody {
390950f054cSdanielk1977  db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
391950f054cSdanielk1977  db complete {-- Useful comment}
392950f054cSdanielk1977  db eval {
393950f054cSdanielk1977    SELECT * FROM sqlite_master;
394950f054cSdanielk1977  }
395950f054cSdanielk1977}
396950f054cSdanielk1977
397950f054cSdanielk1977# Test handling of malloc() failures in sqlite3_open16().
398950f054cSdanielk1977#
3994152e677Sdanielk1977ifcapable utf16 {
400950f054cSdanielk1977  do_malloc_test 17 -tclbody {
401950f054cSdanielk1977    set DB2 0
402950f054cSdanielk1977    set STMT 0
403950f054cSdanielk1977
404950f054cSdanielk1977    # open database using sqlite3_open16()
405d9910fe5Sdrh    set filename [encoding convertto unicode test.db]
406d9910fe5Sdrh    append filename "\x00\x00"
407d9910fe5Sdrh    set DB2 [sqlite3_open16 $filename -unused]
408950f054cSdanielk1977    if {0==$DB2} {
409950f054cSdanielk1977      error "out of memory"
410950f054cSdanielk1977    }
411ae72d982Sdanielk1977    sqlite3_extended_result_codes $DB2 1
412950f054cSdanielk1977
413950f054cSdanielk1977    # Prepare statement
414950f054cSdanielk1977    set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
415ae72d982Sdanielk1977    if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} {
416ae72d982Sdanielk1977      error "out of memory"
417ae72d982Sdanielk1977    }
41859633aeeSdanielk1977    if {[regexp ".*automatic extension loading.*" [sqlite3_errmsg $DB2]]} {
41959633aeeSdanielk1977      error "out of memory"
42059633aeeSdanielk1977    }
421950f054cSdanielk1977    if {$rc} {
422950f054cSdanielk1977      error [string range $msg 4 end]
423950f054cSdanielk1977    }
424950f054cSdanielk1977    set STMT $msg
425950f054cSdanielk1977
426950f054cSdanielk1977    # Finalize statement
427950f054cSdanielk1977    set rc [sqlite3_finalize $STMT]
428950f054cSdanielk1977    if {$rc!="SQLITE_OK"} {
429950f054cSdanielk1977      error [sqlite3_errmsg $DB2]
430950f054cSdanielk1977    }
431950f054cSdanielk1977    set STMT 0
432950f054cSdanielk1977
433950f054cSdanielk1977    # Close database
434950f054cSdanielk1977    set rc [sqlite3_close $DB2]
435950f054cSdanielk1977    if {$rc!="SQLITE_OK"} {
436950f054cSdanielk1977      error [sqlite3_errmsg $DB2]
437950f054cSdanielk1977    }
438950f054cSdanielk1977    set DB2 0
439950f054cSdanielk1977  } -cleanup {
440950f054cSdanielk1977    if {$STMT!="0"} {
441950f054cSdanielk1977      sqlite3_finalize $STMT
442950f054cSdanielk1977    }
443950f054cSdanielk1977    if {$DB2!="0"} {
444950f054cSdanielk1977      set rc [sqlite3_close $DB2]
445950f054cSdanielk1977    }
446950f054cSdanielk1977  }
4474152e677Sdanielk1977}
448950f054cSdanielk1977
449950f054cSdanielk1977# Test handling of malloc() failures in sqlite3_errmsg16().
450950f054cSdanielk1977#
4514152e677Sdanielk1977ifcapable utf16 {
452ae72d982Sdanielk1977  do_malloc_test 18 -tclprep {
453950f054cSdanielk1977    catch {
454950f054cSdanielk1977      db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
455ae72d982Sdanielk1977    }
456ae72d982Sdanielk1977  } -tclbody {
457950f054cSdanielk1977    set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
458950f054cSdanielk1977    binary scan $utf16 c* bytes
459950f054cSdanielk1977    if {[llength $bytes]==0} {
460950f054cSdanielk1977      error "out of memory"
461950f054cSdanielk1977    }
462950f054cSdanielk1977  }
4634152e677Sdanielk1977}
464950f054cSdanielk1977
465161fb796Sdanielk1977# This test is aimed at coverage testing. Specificly, it is supposed to
466161fb796Sdanielk1977# cause a malloc() only used when converting between the two utf-16
467161fb796Sdanielk1977# encodings to fail (i.e. little-endian->big-endian). It only actually
468161fb796Sdanielk1977# hits this malloc() on little-endian hosts.
469161fb796Sdanielk1977#
470161fb796Sdanielk1977set static_string "\x00h\x00e\x00l\x00l\x00o"
471161fb796Sdanielk1977for {set l 0} {$l<10} {incr l} {
472161fb796Sdanielk1977  append static_string $static_string
473161fb796Sdanielk1977}
474161fb796Sdanielk1977append static_string "\x00\x00"
475161fb796Sdanielk1977do_malloc_test 19 -tclprep {
476161fb796Sdanielk1977  execsql {
477161fb796Sdanielk1977    PRAGMA encoding = "UTF16be";
478161fb796Sdanielk1977    CREATE TABLE abc(a, b, c);
479161fb796Sdanielk1977  }
480161fb796Sdanielk1977} -tclbody {
481161fb796Sdanielk1977  unset -nocomplain ::STMT
482161fb796Sdanielk1977  set r [catch {
483f3a65f7eSdrh    set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY]
484161fb796Sdanielk1977    sqlite3_bind_text16 -static $::STMT 1 $static_string 112
485161fb796Sdanielk1977  } msg]
486161fb796Sdanielk1977  if {$r} {error [string range $msg 4 end]}
487161fb796Sdanielk1977  set msg
488161fb796Sdanielk1977} -cleanup {
489161fb796Sdanielk1977  if {[info exists ::STMT]} {
490161fb796Sdanielk1977    sqlite3_finalize $::STMT
491161fb796Sdanielk1977  }
492161fb796Sdanielk1977}
493161fb796Sdanielk1977unset static_string
494161fb796Sdanielk1977
4956103fe97Sdrh# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
4966103fe97Sdrh# when the malloc failure occurs within the nested parse.
4976103fe97Sdrh#
4985a8f9374Sdanielk1977ifcapable attach {
4996103fe97Sdrh  do_malloc_test 20 -tclprep {
5006103fe97Sdrh    db close
5016103fe97Sdrh    file delete -force test2.db test2.db-journal
5026103fe97Sdrh    sqlite3 db test2.db
503ae72d982Sdanielk1977    sqlite3_extended_result_codes db 1
5046103fe97Sdrh    db eval {CREATE TABLE t1(x);}
5056103fe97Sdrh    db close
5066103fe97Sdrh  } -tclbody {
5076103fe97Sdrh    if {[catch {sqlite3 db test.db}]} {
5086103fe97Sdrh      error "out of memory"
5096103fe97Sdrh    }
510ae72d982Sdanielk1977    sqlite3_extended_result_codes db 1
5116103fe97Sdrh  } -sqlbody {
5126103fe97Sdrh    ATTACH DATABASE 'test2.db' AS t2;
5136103fe97Sdrh    SELECT * FROM t1;
5146103fe97Sdrh    DETACH DATABASE t2;
5156103fe97Sdrh  }
5165a8f9374Sdanielk1977}
5176103fe97Sdrh
518b5584c0cSdanielk1977# Test malloc failure whilst installing a foreign key.
51969b637b5Sdanielk1977#
5204152e677Sdanielk1977ifcapable foreignkey {
52169b637b5Sdanielk1977  do_malloc_test 21 -sqlbody {
52269b637b5Sdanielk1977    CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
52369b637b5Sdanielk1977  }
5244152e677Sdanielk1977}
52569b637b5Sdanielk1977
5267e29e956Sdanielk1977# Test malloc failure in an sqlite3_prepare_v2() call.
5277e29e956Sdanielk1977#
5287e29e956Sdanielk1977do_malloc_test 22 -tclbody {
5297e29e956Sdanielk1977  set ::STMT ""
5307e29e956Sdanielk1977  set r [catch {
5317e29e956Sdanielk1977    set ::STMT [
532f3a65f7eSdrh      sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY
5337e29e956Sdanielk1977    ]
5347e29e956Sdanielk1977  } msg]
5357e29e956Sdanielk1977  if {$r} {error [string range $msg 4 end]}
5367e29e956Sdanielk1977} -cleanup {
5377e29e956Sdanielk1977  if {$::STMT ne ""} {
5387e29e956Sdanielk1977    sqlite3_finalize $::STMT
5397e29e956Sdanielk1977    set ::STMT ""
5407e29e956Sdanielk1977  }
5417e29e956Sdanielk1977}
5427e29e956Sdanielk1977
5432a180ff9Sdanielk1977ifcapable {pager_pragmas} {
5442a180ff9Sdanielk1977  # This tests a special case - that an error that occurs while the pager
5452a180ff9Sdanielk1977  # is trying to recover from error-state in exclusive-access mode works.
5462a180ff9Sdanielk1977  #
5472a180ff9Sdanielk1977  do_malloc_test 23 -tclprep {
5482a180ff9Sdanielk1977    db eval {
5492a180ff9Sdanielk1977      PRAGMA cache_size = 10;
5502a180ff9Sdanielk1977      PRAGMA locking_mode = exclusive;
5512a180ff9Sdanielk1977      BEGIN;
5522a180ff9Sdanielk1977      CREATE TABLE abc(a, b, c);
5532a180ff9Sdanielk1977      CREATE INDEX abc_i ON abc(a, b, c);
5542a180ff9Sdanielk1977      INSERT INTO abc
5552a180ff9Sdanielk1977        VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
5562a180ff9Sdanielk1977      INSERT INTO abc
5572a180ff9Sdanielk1977        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
5582a180ff9Sdanielk1977      INSERT INTO abc
5592a180ff9Sdanielk1977        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
5602a180ff9Sdanielk1977      INSERT INTO abc
5612a180ff9Sdanielk1977        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
5622a180ff9Sdanielk1977      INSERT INTO abc
5632a180ff9Sdanielk1977        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
5642a180ff9Sdanielk1977      INSERT INTO abc
5652a180ff9Sdanielk1977        SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
5662a180ff9Sdanielk1977      COMMIT;
5672a180ff9Sdanielk1977    }
5682a180ff9Sdanielk1977
5692a180ff9Sdanielk1977    # This puts the pager into error state.
5702a180ff9Sdanielk1977    #
5712a180ff9Sdanielk1977    db eval BEGIN
5722a180ff9Sdanielk1977    db eval {UPDATE abc SET a = 0 WHERE oid%2}
5732a180ff9Sdanielk1977    set ::sqlite_io_error_pending 10
5742a180ff9Sdanielk1977    catch {db eval {ROLLBACK}} msg
5752a180ff9Sdanielk1977
57698c21903Sdanielk1977  } -tclbody {
57798c21903Sdanielk1977    # If an out-of-memory occurs within a call to a VFS layer function during
57898c21903Sdanielk1977    # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
57998c21903Sdanielk1977    # [5668] for details.
58098c21903Sdanielk1977    set rc [catch {db eval { SELECT * FROM abc LIMIT 10 }} msg]
58198c21903Sdanielk1977    if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
58298c21903Sdanielk1977    if {$rc} { error $msg }
58398c21903Sdanielk1977    set msg
5842a180ff9Sdanielk1977  } -cleanup {
5852a180ff9Sdanielk1977    set e [db eval {PRAGMA integrity_check}]
5862a180ff9Sdanielk1977    if {$e ne "ok"} {error $e}
5872a180ff9Sdanielk1977  }
5882a180ff9Sdanielk1977}
5892a180ff9Sdanielk1977
590ac559264Sdanielk1977ifcapable compound {
591cdf3020cSdanielk1977  do_malloc_test 24 -sqlprep {
592ac559264Sdanielk1977    CREATE TABLE t1(a, b, c)
593ac559264Sdanielk1977  } -sqlbody {
594ac559264Sdanielk1977    SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1
595ac559264Sdanielk1977  }
596ac559264Sdanielk1977}
597ac559264Sdanielk1977
598cdf3020cSdanielk1977ifcapable view&&trigger {
599cdf3020cSdanielk1977  do_malloc_test 25 -sqlprep {
600cdf3020cSdanielk1977    CREATE TABLE t1(a, b, c);
601cdf3020cSdanielk1977    CREATE VIEW v1 AS SELECT * FROM t1;
602cdf3020cSdanielk1977    CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END;
603cdf3020cSdanielk1977    CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END;
604cdf3020cSdanielk1977    CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END;
605cdf3020cSdanielk1977  } -sqlbody {
606cdf3020cSdanielk1977    DELETE FROM v1 WHERE a = 1;
607cdf3020cSdanielk1977    INSERT INTO v1 VALUES(1, 2, 3);
608cdf3020cSdanielk1977    UPDATE v1 SET a = 1 WHERE b = 2;
609cdf3020cSdanielk1977  }
610cdf3020cSdanielk1977}
611cdf3020cSdanielk1977
6127eaabcdbSdanielk1977do_malloc_test 25 -sqlprep {
6137eaabcdbSdanielk1977  CREATE TABLE abc(a, b, c);
6147eaabcdbSdanielk1977  CREATE INDEX i1 ON abc(a, b);
6157eaabcdbSdanielk1977  INSERT INTO abc VALUES(1, 2, 3);
6167eaabcdbSdanielk1977  INSERT INTO abc VALUES(4, 5, 6);
6177eaabcdbSdanielk1977} -tclbody {
6187eaabcdbSdanielk1977  # For each UPDATE executed, the cursor used for the SELECT statement
6197eaabcdbSdanielk1977  # must be "saved". Because the cursor is open on an index, this requires
6207eaabcdbSdanielk1977  # a malloc() to allocate space to save the index key. This test case is
6217eaabcdbSdanielk1977  # aimed at testing the response of the library to a failure in that
6227eaabcdbSdanielk1977  # particular malloc() call.
6237eaabcdbSdanielk1977  db eval {SELECT a FROM abc ORDER BY a} {
6247eaabcdbSdanielk1977    db eval {UPDATE abc SET b = b - 1 WHERE a = $a}
6257eaabcdbSdanielk1977  }
6267eaabcdbSdanielk1977}
6277eaabcdbSdanielk1977
628518002e4Sdanielk1977# This test is designed to test a specific juncture in the sqlite code.
629518002e4Sdanielk1977# The database set up by -sqlprep script contains a single table B-Tree
630518002e4Sdanielk1977# of height 2. In the -tclbody script, the existing database connection
631518002e4Sdanielk1977# is closed and a new one opened and used to insert a new row into the
632518002e4Sdanielk1977# table B-Tree. By using a new connection, the outcome of a malloc()
633518002e4Sdanielk1977# failure while seeking to the right-hand side of the B-Tree to insert
634518002e4Sdanielk1977# a new record can be tested.
635518002e4Sdanielk1977#
636518002e4Sdanielk1977do_malloc_test 26 -sqlprep {
637518002e4Sdanielk1977  BEGIN;
638518002e4Sdanielk1977  CREATE TABLE t1(a, b);
639518002e4Sdanielk1977  INSERT INTO t1 VALUES(1, randomblob(210));
640518002e4Sdanielk1977  INSERT INTO t1 VALUES(1, randomblob(210));
641518002e4Sdanielk1977  INSERT INTO t1 VALUES(1, randomblob(210));
642518002e4Sdanielk1977  INSERT INTO t1 VALUES(1, randomblob(210));
643518002e4Sdanielk1977  INSERT INTO t1 VALUES(1, randomblob(210));
644518002e4Sdanielk1977  COMMIT;
645518002e4Sdanielk1977} -tclbody {
646518002e4Sdanielk1977  db close
647518002e4Sdanielk1977  sqlite3 db test.db
648518002e4Sdanielk1977  db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
649518002e4Sdanielk1977}
650518002e4Sdanielk1977
65177eb5bb6Sdanielk1977# Test that no memory is leaked following a malloc() failure in
65277eb5bb6Sdanielk1977# sqlite3_initialize().
65377eb5bb6Sdanielk1977#
65477eb5bb6Sdanielk1977do_malloc_test 27 -tclprep {
65577eb5bb6Sdanielk1977  db close
65677eb5bb6Sdanielk1977  sqlite3_shutdown
65777eb5bb6Sdanielk1977} -tclbody {
65877eb5bb6Sdanielk1977  set rc [sqlite3_initialize]
65977eb5bb6Sdanielk1977  if {$rc == "SQLITE_NOMEM"} {
66077eb5bb6Sdanielk1977    error "out of memory"
66177eb5bb6Sdanielk1977  }
66277eb5bb6Sdanielk1977}
663*bb77b753Sdrhautoinstall_test_functions
66477eb5bb6Sdanielk1977
66585574e31Sdanielk1977# Test that malloc failures that occur while processing INDEXED BY
66685574e31Sdanielk1977# clauses are handled correctly.
66785574e31Sdanielk1977do_malloc_test 28 -sqlprep {
66885574e31Sdanielk1977  CREATE TABLE t1(a, b);
66985574e31Sdanielk1977  CREATE INDEX i1 ON t1(a);
67085574e31Sdanielk1977  CREATE VIEW v1 AS SELECT * FROM t1 INDEXED BY i1 WHERE a = 10;
67185574e31Sdanielk1977} -sqlbody {
67285574e31Sdanielk1977  SELECT * FROM t1 INDEXED BY i1 ORDER BY a;
67385574e31Sdanielk1977  SELECT * FROM v1;
67485574e31Sdanielk1977}
67585574e31Sdanielk1977
676e4359750Sdanielk1977do_malloc_test 29 -sqlprep {
677e4359750Sdanielk1977  CREATE TABLE t1(a TEXT, b TEXT);
678e4359750Sdanielk1977} -sqlbody {
679e4359750Sdanielk1977  INSERT INTO t1 VALUES(1, -234);
680956f4319Sdanielk1977  INSERT INTO t1 SELECT * FROM t1 UNION ALL SELECT * FROM t1;
681e4359750Sdanielk1977}
682e4359750Sdanielk1977
6837cbd589dSdanielk1977do_malloc_test 30 -tclprep {
68495855288Sdanielk1977  db eval {
68595855288Sdanielk1977    CREATE TABLE t1(x PRIMARY KEY);
68695855288Sdanielk1977    INSERT INTO t1 VALUES(randstr(500,500));
68795855288Sdanielk1977    INSERT INTO t1 VALUES(randstr(500,500));
68895855288Sdanielk1977    INSERT INTO t1 VALUES(randstr(500,500));
68995855288Sdanielk1977  }
69095855288Sdanielk1977  db close
69195855288Sdanielk1977  sqlite3 db test.db
69295855288Sdanielk1977
69395855288Sdanielk1977  # The DELETE command in the following block moves the overflow pages that
69495855288Sdanielk1977  # are part of the primary key index to the free-list. But it does not
69595855288Sdanielk1977  # actually load the content of the pages. This leads to the peculiar
69695855288Sdanielk1977  # situation where cache entries exist, but are not populated with data.
69795855288Sdanielk1977  # They are populated next time they are requested by the b-tree layer.
69895855288Sdanielk1977  #
69995855288Sdanielk1977  db eval {
70095855288Sdanielk1977    BEGIN;
70195855288Sdanielk1977      DELETE FROM t1;
70295855288Sdanielk1977    ROLLBACK;
70395855288Sdanielk1977  }
70495855288Sdanielk1977} -sqlbody {
705c87239fbSdanielk1977  -- This statement requires the 'no-content' pages loaded by the DELETE
706c87239fbSdanielk1977  -- statement above. When requesting the pages, the content is loaded
707c87239fbSdanielk1977  -- from the database file. The point of this test case is to test handling
708c87239fbSdanielk1977  -- of malloc errors (including SQLITE_IOERR_NOMEM errors) when loading
709c87239fbSdanielk1977  -- the content.
71095855288Sdanielk1977  SELECT * FROM t1 ORDER BY x;
71195855288Sdanielk1977}
71295855288Sdanielk1977
7137cbd589dSdanielk1977# After committing a transaction in persistent-journal mode, if a journal
7147cbd589dSdanielk1977# size limit is configured SQLite may attempt to truncate the journal file.
7157cbd589dSdanielk1977# This test verifies the libraries response to a malloc() failure during
7167cbd589dSdanielk1977# this operation.
7177cbd589dSdanielk1977#
7187cbd589dSdanielk1977do_malloc_test 31 -sqlprep {
7197cbd589dSdanielk1977  PRAGMA journal_mode = persist;
7207cbd589dSdanielk1977  PRAGMA journal_size_limit = 1024;
7217cbd589dSdanielk1977  CREATE TABLE t1(a PRIMARY KEY, b);
7227cbd589dSdanielk1977} -sqlbody {
7237cbd589dSdanielk1977  INSERT INTO t1 VALUES(1, 2);
7247cbd589dSdanielk1977}
725956f4319Sdanielk1977
726db340397Sdanielk1977# When written, this test provoked an obscure change-counter bug.
727db340397Sdanielk1977#
728db340397Sdanielk1977# If, when running in exclusive mode, a malloc() failure occurs
729db340397Sdanielk1977# after the database file change-counter has been written but
730db340397Sdanielk1977# before the transaction has been committed, then the transaction
731db340397Sdanielk1977# is automatically rolled back. However, internally the
732db340397Sdanielk1977# Pager.changeCounterDone flag was being left set. This means
733db340397Sdanielk1977# that if the same connection attempts another transaction following
734db340397Sdanielk1977# the malloc failure and rollback, the change counter will not
735db340397Sdanielk1977# be updated. This could corrupt another processes cache.
736db340397Sdanielk1977#
737db340397Sdanielk1977do_malloc_test 32 -tclprep {
738db340397Sdanielk1977  # Build a small database containing an indexed table.
739db340397Sdanielk1977  #
740db340397Sdanielk1977  db eval {
741de06e9f2Sdanielk1977    PRAGMA locking_mode = normal;
742db340397Sdanielk1977    BEGIN;
743db340397Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY, b);
744db340397Sdanielk1977    INSERT INTO t1 VALUES(1, 'one');
745db340397Sdanielk1977    INSERT INTO t1 VALUES(2, 'two');
746db340397Sdanielk1977    INSERT INTO t1 VALUES(3, 'three');
747db340397Sdanielk1977    COMMIT;
748db340397Sdanielk1977    PRAGMA locking_mode = exclusive;
749db340397Sdanielk1977  }
750db340397Sdanielk1977
751db340397Sdanielk1977  # Open a second database connection. Load the table (but not index)
752db340397Sdanielk1977  # into the second connections pager cache.
753db340397Sdanielk1977  #
754db340397Sdanielk1977  sqlite3 db2 test.db
755de06e9f2Sdanielk1977  db2 eval {
756de06e9f2Sdanielk1977    PRAGMA locking_mode = normal;
757de06e9f2Sdanielk1977    SELECT b FROM t1;
758de06e9f2Sdanielk1977  }
759db340397Sdanielk1977
760db340397Sdanielk1977} -tclbody {
761db340397Sdanielk1977  # Running in exclusive mode, perform a database transaction that
762db340397Sdanielk1977  # modifies both the database table and index. For iterations where
763db340397Sdanielk1977  # the malloc failure occurs after updating the change counter but
764db340397Sdanielk1977  # before committing the transaction, this should result in the
765db340397Sdanielk1977  # transaction being rolled back but the changeCounterDone flag
766db340397Sdanielk1977  # left set.
767db340397Sdanielk1977  #
768db340397Sdanielk1977  db eval { UPDATE t1 SET a = a + 3 }
769db340397Sdanielk1977} -cleanup {
770db340397Sdanielk1977
771db340397Sdanielk1977  # Perform another transaction using the first connection. Unlock
772db340397Sdanielk1977  # the database after doing so. If this is one of the right iterations,
773db340397Sdanielk1977  # then this should result in the database contents being updated but
774db340397Sdanielk1977  # the change-counter left as it is.
775db340397Sdanielk1977  #
776db340397Sdanielk1977  db eval {
777db340397Sdanielk1977    PRAGMA locking_mode = normal;
778db340397Sdanielk1977    UPDATE t1 SET a = a + 3;
779db340397Sdanielk1977  }
780db340397Sdanielk1977
781db340397Sdanielk1977  # Now do an integrity check with the second connection. The second
782db340397Sdanielk1977  # connection still has the database table in its cache. If this is
783db340397Sdanielk1977  # one of the magic iterations and the change counter was not modified,
784db340397Sdanielk1977  # then it won't realize that the cached data is out of date. Since
785db340397Sdanielk1977  # the cached data won't match the up to date index data read from
786db340397Sdanielk1977  # the database file, the integrity check should fail.
787db340397Sdanielk1977  #
788db340397Sdanielk1977  set zRepeat "transient"
789db340397Sdanielk1977  if {$::iRepeat} {set zRepeat "persistent"}
790db340397Sdanielk1977  do_test malloc-32.$zRepeat.${::n}.integrity {
791db340397Sdanielk1977    execsql {PRAGMA integrity_check} db2
792db340397Sdanielk1977  } {ok}
793db340397Sdanielk1977  db2 close
794db340397Sdanielk1977}
795db340397Sdanielk1977
79602f33725Sdanielk1977# The following two OOM tests verify that OOM handling works in the
79702f33725Sdanielk1977# code used to optimize "SELECT count(*) FROM <tbl>".
79802f33725Sdanielk1977#
79902f33725Sdanielk1977do_malloc_test 33 -tclprep {
80002f33725Sdanielk1977  db eval { PRAGMA cache_size = 10 }
80102f33725Sdanielk1977  db transaction {
80202f33725Sdanielk1977    db eval { CREATE TABLE abc(a, b) }
80302f33725Sdanielk1977    for {set i 0} {$i<500} {incr i} {
80402f33725Sdanielk1977      db eval {INSERT INTO abc VALUES(randstr(100,100), randstr(1000,1000))}
80502f33725Sdanielk1977    }
80602f33725Sdanielk1977  }
80702f33725Sdanielk1977} -sqlbody {
80802f33725Sdanielk1977  SELECT count(*) FROM abc;
80902f33725Sdanielk1977}
81002f33725Sdanielk1977do_malloc_test 34 -tclprep {
81102f33725Sdanielk1977  db eval { PRAGMA cache_size = 10 }
81202f33725Sdanielk1977  db transaction {
81302f33725Sdanielk1977    db eval { CREATE TABLE abc(a PRIMARY KEY, b) }
81402f33725Sdanielk1977    for {set i 0} {$i<500} {incr i} {
81502f33725Sdanielk1977      db eval {INSERT INTO abc VALUES(randstr(100,100), randstr(1000,1000))}
81602f33725Sdanielk1977    }
81702f33725Sdanielk1977  }
81802f33725Sdanielk1977} -sqlbody {
81902f33725Sdanielk1977  SELECT count(*) FROM abc;
82002f33725Sdanielk1977}
82102f33725Sdanielk1977
822238746a6Sdanielk1977proc f {args} { error "Quite a long error!" }
823238746a6Sdanielk1977do_malloc_test 35 -tclprep {
824238746a6Sdanielk1977  db func f f
825238746a6Sdanielk1977  set ::STMT [sqlite3_prepare db "SELECT f()" -1 DUMMY]
826238746a6Sdanielk1977  sqlite3_step $::STMT
827238746a6Sdanielk1977} -tclbody {
828238746a6Sdanielk1977  sqlite3_finalize $::STMT
829238746a6Sdanielk1977} -cleanup {
830238746a6Sdanielk1977  # At one point an assert( !db->mallocFailed ) could fail in the following
831238746a6Sdanielk1977  # call to sqlite3_errmsg(). Because sqlite3_finalize() had failed to clear
832238746a6Sdanielk1977  # the flag before returning.
833238746a6Sdanielk1977  sqlite3_errmsg16 db
834238746a6Sdanielk1977}
835238746a6Sdanielk1977
836238746a6Sdanielk1977do_malloc_test 36 -sqlprep {
837238746a6Sdanielk1977  CREATE TABLE t1(a, b);
838238746a6Sdanielk1977  INSERT INTO t1 VALUES(1, 2);
839238746a6Sdanielk1977  INSERT INTO t1 VALUES(3, 4);
840238746a6Sdanielk1977} -sqlbody {
841238746a6Sdanielk1977  SELECT test_agg_errmsg16(), group_concat(a) FROM t1
842238746a6Sdanielk1977}
843238746a6Sdanielk1977
84496fb0dd5Sdanielk1977# Ensure that no file descriptors were leaked.
845b5402fbfSdanielk1977do_test malloc-99.X {
84696fb0dd5Sdanielk1977  catch {db close}
84796fb0dd5Sdanielk1977  set sqlite_open_file_count
84896fb0dd5Sdanielk1977} {0}
84996fb0dd5Sdanielk1977
850344a6276Sdrhputs open-file-count=$sqlite_open_file_count
851ed7c855cSdrhfinish_test
852