xref: /sqlite-3.40.0/test/malloc3.test (revision 5d00d0a8)
1# 2005 November 30
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 contains tests to ensure that the library handles malloc() failures
13# correctly. The emphasis of these tests are the _prepare(), _step() and
14# _finalize() calls.
15#
16# $Id: malloc3.test,v 1.24 2008/10/14 15:54:08 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20source $testdir/malloc_common.tcl
21
22# Only run these tests if memory debugging is turned on.
23#
24if {!$MEMDEBUG} {
25   puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..."
26   finish_test
27   return
28}
29
30#--------------------------------------------------------------------------
31# NOTES ON RECOVERING FROM A MALLOC FAILURE
32#
33# The tests in this file test the behaviours described in the following
34# paragraphs. These tests test the behaviour of the system when malloc() fails
35# inside of a call to _prepare(), _step(), _finalize() or _reset(). The
36# handling of malloc() failures within ancillary procedures is tested
37# elsewhere.
38#
39# Overview:
40#
41# Executing a statement is done in three stages (prepare, step and finalize). A
42# malloc() failure may occur within any stage. If a memory allocation fails
43# during statement preparation, no statement handle is returned. From the users
44# point of view the system state is as if _prepare() had never been called.
45#
46# If the memory allocation fails during the _step() or _finalize() calls, then
47# the database may be left in one of two states (after finalize() has been
48# called):
49#
50#     * As if the neither _step() nor _finalize() had ever been called on
51#       the statement handle (i.e. any changes made by the statement are
52#       rolled back).
53#     * The current transaction may be rolled back. In this case a hot-journal
54#       may or may not actually be present in the filesystem.
55#
56# The caller can tell the difference between these two scenarios by invoking
57# _get_autocommit().
58#
59#
60# Handling of sqlite3_reset():
61#
62# If a malloc() fails while executing an sqlite3_reset() call, this is handled
63# in the same way as a failure within _finalize(). The statement handle
64# is not deleted and must be passed to _finalize() for resource deallocation.
65# Attempting to _step() or _reset() the statement after a failed _reset() will
66# always return SQLITE_NOMEM.
67#
68#
69# Other active SQL statements:
70#
71# The effect of a malloc failure on concurrently executing SQL statements,
72# particularly when the statement is executing with READ_UNCOMMITTED set and
73# the malloc() failure mandates statement rollback only. Currently, if
74# transaction rollback is required, all other vdbe's are aborted.
75#
76#     Non-transient mallocs in btree.c:
77#         * The Btree structure itself
78#         * Each BtCursor structure
79#
80#     Mallocs in pager.c:
81#         readMasterJournal()  - Space to read the master journal name
82#         pager_delmaster()    - Space for the entire master journal file
83#
84#         sqlite3pager_open()  - The pager structure itself
85#         sqlite3_pagerget()   - Space for a new page
86#         pager_open_journal() - Pager.aInJournal[] bitmap
87#         sqlite3pager_write() - For in-memory databases only: history page and
88#                                statement history page.
89#         pager_stmt_begin()   - Pager.aInStmt[] bitmap
90#
91# None of the above are a huge problem. The most troublesome failures are the
92# transient malloc() calls in btree.c, which can occur during the tree-balance
93# operation. This means the tree being balanced will be internally inconsistent
94# after the malloc() fails. To avoid the corrupt tree being read by a
95# READ_UNCOMMITTED query, we have to make sure the transaction or statement
96# rollback occurs before sqlite3_step() returns, not during a subsequent
97# sqlite3_finalize().
98#--------------------------------------------------------------------------
99
100#--------------------------------------------------------------------------
101# NOTES ON TEST IMPLEMENTATION
102#
103# The tests in this file are implemented differently from those in other
104# files. Instead, tests are specified using three primitives: SQL, PREP and
105# TEST. Each primitive has a single argument. Primitives are processed in
106# the order they are specified in the file.
107#
108# A TEST primitive specifies a TCL script as its argument. When a TEST
109# directive is encountered the Tcl script is evaluated. Usually, this Tcl
110# script contains one or more calls to [do_test].
111#
112# A PREP primitive specifies an SQL script as its argument. When a PREP
113# directive is encountered the SQL is evaluated using database connection
114# [db].
115#
116# The SQL primitives are where the action happens. An SQL primitive must
117# contain a single, valid SQL statement as its argument. When an SQL
118# primitive is encountered, it is evaluated one or more times to test the
119# behaviour of the system when malloc() fails during preparation or
120# execution of said statement. The Nth time the statement is executed,
121# the Nth malloc is said to fail. The statement is executed until it
122# succeeds, i.e. (M+1) times, where M is the number of mallocs() required
123# to prepare and execute the statement.
124#
125# Each time an SQL statement fails, the driver program (see proc [run_test]
126# below) figures out if a transaction has been automatically rolled back.
127# If not, it executes any TEST block immediately proceeding the SQL
128# statement, then reexecutes the SQL statement with the next value of N.
129#
130# If a transaction has been automatically rolled back, then the driver
131# program executes all the SQL specified as part of SQL or PREP primitives
132# between the current SQL statement and the most recent "BEGIN". Any
133# TEST block immediately proceeding the SQL statement is evaluated, and
134# then the SQL statement reexecuted with the incremented N value.
135#
136# That make any sense? If not, read the code in [run_test] and it might.
137#
138# Extra restriction imposed by the implementation:
139#
140# * If a PREP block starts a transaction, it must finish it.
141# * A PREP block may not close a transaction it did not start.
142#
143#--------------------------------------------------------------------------
144
145
146# These procs are used to build up a "program" in global variable
147# ::run_test_script. At the end of this file, the proc [run_test] is used
148# to execute the program (and all test cases contained therein).
149#
150set ::run_test_script [list]
151proc TEST {id t} {lappend ::run_test_script -test [list $id $t]}
152proc PREP {p} {lappend ::run_test_script -prep [string trim $p]}
153proc DEBUG {s} {lappend ::run_test_script -debug $s}
154
155# SQL --
156#
157#     SQL ?-norollback? <sql-text>
158#
159# Add an 'SQL' primitive to the program (see notes above). If the -norollback
160# switch is present, then the statement is not allowed to automatically roll
161# back any active transaction if malloc() fails. It must rollback the statement
162# transaction only.
163#
164proc SQL  {a1 {a2 ""}} {
165  # An SQL primitive parameter is a list of two elements, a boolean value
166  # indicating if the statement may cause transaction rollback when malloc()
167  # fails, and the sql statement itself.
168  if {$a2 == ""} {
169    lappend ::run_test_script -sql [list true [string trim $a1]]
170  } else {
171    lappend ::run_test_script -sql [list false [string trim $a2]]
172  }
173}
174
175# TEST_AUTOCOMMIT --
176#
177#     A shorthand test to see if a transaction is active or not. The first
178#     argument - $id - is the integer number of the test case. The second
179#     argument is either 1 or 0, the expected value of the auto-commit flag.
180#
181proc TEST_AUTOCOMMIT {id a} {
182    TEST $id "do_test \$testid { sqlite3_get_autocommit \$::DB } {$a}"
183}
184
185#--------------------------------------------------------------------------
186# Start of test program declaration
187#
188
189
190# Warm body test. A malloc() fails in the middle of a CREATE TABLE statement
191# in a single-statement transaction on an empty database. Not too much can go
192# wrong here.
193#
194TEST 1 {
195  do_test $testid {
196    execsql {SELECT tbl_name FROM sqlite_master;}
197  } {}
198}
199SQL {
200  CREATE TABLE IF NOT EXISTS abc(a, b, c);
201}
202TEST 2 {
203  do_test $testid.1 {
204    execsql {SELECT tbl_name FROM sqlite_master;}
205  } {abc}
206}
207
208# Insert a couple of rows into the table. each insert is in its own
209# transaction. test that the table is unpopulated before running the inserts
210# (and hence after each failure of the first insert), and that it has been
211# populated correctly after the final insert succeeds.
212#
213TEST 3 {
214  do_test $testid.2 {
215    execsql {SELECT * FROM abc}
216  } {}
217}
218SQL {INSERT INTO abc VALUES(1, 2, 3);}
219SQL {INSERT INTO abc VALUES(4, 5, 6);}
220SQL {INSERT INTO abc VALUES(7, 8, 9);}
221TEST 4 {
222  do_test $testid {
223    execsql {SELECT * FROM abc}
224  } {1 2 3 4 5 6 7 8 9}
225}
226
227# Test a CREATE INDEX statement. Because the table 'abc' is so small, the index
228# will all fit on a single page, so this doesn't test too much that the CREATE
229# TABLE statement didn't test. A few of the transient malloc()s in btree.c
230# perhaps.
231#
232SQL {CREATE INDEX abc_i ON abc(a, b, c);}
233TEST 4 {
234  do_test $testid {
235    execsql {
236      SELECT * FROM abc ORDER BY a DESC;
237    }
238  } {7 8 9 4 5 6 1 2 3}
239}
240
241# Test a DELETE statement. Also create a trigger and a view, just to make sure
242# these statements don't have any obvious malloc() related bugs in them. Note
243# that the test above will be executed each time the DELETE fails, so we're
244# also testing rollback of a DELETE from a table with an index on it.
245#
246SQL {DELETE FROM abc WHERE a > 2;}
247SQL {CREATE TRIGGER abc_t AFTER INSERT ON abc BEGIN SELECT 'trigger!'; END;}
248SQL {CREATE VIEW abc_v AS SELECT * FROM abc;}
249TEST 5 {
250  do_test $testid {
251    execsql {
252      SELECT name, tbl_name FROM sqlite_master ORDER BY name;
253      SELECT * FROM abc;
254    }
255  } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3}
256}
257
258set sql {
259  BEGIN;DELETE FROM abc;
260}
261for {set i 1} {$i < 15} {incr i} {
262  set a $i
263  set b "String value $i"
264  set c [string repeat X $i]
265  append sql "INSERT INTO abc VALUES ($a, '$b', '$c');"
266}
267append sql {COMMIT;}
268PREP $sql
269
270SQL {
271  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
272}
273TEST 6 {
274  do_test $testid.1 {
275    execsql {SELECT count(*) FROM abc}
276  } {94}
277  do_test $testid.2 {
278    execsql {
279      SELECT min(
280          (oid == a) AND 'String value ' || a == b AND a == length(c)
281      ) FROM abc;
282    }
283  } {1}
284}
285SQL {
286  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
287}
288TEST 7 {
289  do_test $testid {
290    execsql {SELECT count(*) FROM abc}
291  } {89}
292  do_test $testid {
293    execsql {
294      SELECT min(
295          (oid == a) AND 'String value ' || a == b AND a == length(c)
296      ) FROM abc;
297    }
298  } {1}
299}
300SQL {
301  DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
302}
303TEST 9 {
304  do_test $testid {
305    execsql {SELECT count(*) FROM abc}
306  } {84}
307  do_test $testid {
308    execsql {
309      SELECT min(
310          (oid == a) AND 'String value ' || a == b AND a == length(c)
311      ) FROM abc;
312    }
313  } {1}
314}
315
316set padding [string repeat X 500]
317PREP [subst {
318  DROP TABLE abc;
319  CREATE TABLE abc(a PRIMARY KEY, padding, b, c);
320  INSERT INTO abc VALUES(0, '$padding', 2, 2);
321  INSERT INTO abc VALUES(3, '$padding', 5, 5);
322  INSERT INTO abc VALUES(6, '$padding', 8, 8);
323}]
324
325TEST 10 {
326  do_test $testid {
327    execsql {SELECT a, b, c FROM abc}
328  } {0 2 2 3 5 5 6 8 8}
329}
330
331SQL {BEGIN;}
332SQL {INSERT INTO abc VALUES(9, 'XXXXX', 11, 12);}
333TEST_AUTOCOMMIT 11 0
334SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;}
335TEST_AUTOCOMMIT 12 0
336SQL {DELETE FROM abc WHERE a = 10;}
337TEST_AUTOCOMMIT 13 0
338SQL {COMMIT;}
339
340TEST 14 {
341  do_test $testid.1 {
342    sqlite3_get_autocommit $::DB
343  } {1}
344  do_test $testid.2 {
345    execsql {SELECT a, b, c FROM abc}
346  } {1 2 3 4 5 6 7 8 9}
347}
348
349PREP [subst {
350  DROP TABLE abc;
351  CREATE TABLE abc(a, padding, b, c);
352  INSERT INTO abc VALUES(1, '$padding', 2, 3);
353  INSERT INTO abc VALUES(4, '$padding', 5, 6);
354  INSERT INTO abc VALUES(7, '$padding', 8, 9);
355  CREATE INDEX abc_i ON abc(a, padding, b, c);
356}]
357
358TEST 15 {
359  db eval {PRAGMA cache_size = 10}
360}
361
362SQL {BEGIN;}
363SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
364TEST 16 {
365  do_test $testid {
366    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
367  } {1 2 4 2 7 2}
368}
369SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
370TEST 17 {
371  do_test $testid {
372    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
373  } {1 4 4 4 7 4}
374}
375SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
376TEST 18 {
377  do_test $testid {
378    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
379  } {1 8 4 8 7 8}
380}
381SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
382TEST 19 {
383  do_test $testid {
384    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
385  } {1 16 4 16 7 16}
386}
387SQL {COMMIT;}
388TEST 21 {
389  do_test $testid {
390    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
391  } {1 16 4 16 7 16}
392}
393
394SQL {BEGIN;}
395SQL {DELETE FROM abc WHERE oid %2}
396TEST 22 {
397  do_test $testid {
398    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
399  } {1 8 4 8 7 8}
400}
401SQL {DELETE FROM abc}
402TEST 23 {
403  do_test $testid {
404    execsql {SELECT * FROM abc}
405  } {}
406}
407SQL {ROLLBACK;}
408TEST 24 {
409  do_test $testid {
410    execsql {SELECT a, count(*) FROM abc GROUP BY a;}
411  } {1 16 4 16 7 16}
412}
413
414# Test some schema modifications inside of a transaction. These should all
415# cause transaction rollback if they fail. Also query a view, to cover a bit
416# more code.
417#
418PREP {DROP VIEW abc_v;}
419TEST 25 {
420  do_test $testid {
421    execsql {
422      SELECT name, tbl_name FROM sqlite_master;
423    }
424  } {abc abc abc_i abc}
425}
426SQL {BEGIN;}
427SQL {CREATE TABLE def(d, e, f);}
428SQL {CREATE TABLE ghi(g, h, i);}
429TEST 26 {
430  do_test $testid {
431    execsql {
432      SELECT name, tbl_name FROM sqlite_master;
433    }
434  } {abc abc abc_i abc def def ghi ghi}
435}
436SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi}
437SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);}
438TEST 27 {
439  do_test $testid {
440    execsql {
441      SELECT name, tbl_name FROM sqlite_master;
442    }
443  } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi}
444}
445SQL {INSERT INTO def VALUES('a', 'b', 'c')}
446SQL {INSERT INTO def VALUES(1, 2, 3)}
447SQL -norollback {INSERT INTO ghi SELECT * FROM def}
448TEST 28 {
449  do_test $testid {
450    execsql {
451      SELECT * FROM def, ghi WHERE d = g;
452    }
453  } {a b c a b c 1 2 3 1 2 3}
454}
455SQL {COMMIT}
456TEST 29 {
457  do_test $testid {
458    execsql {
459      SELECT * FROM v1 WHERE d = g;
460    }
461  } {a b c a b c 1 2 3 1 2 3}
462}
463
464# Test a simple multi-file transaction
465#
466file delete -force test2.db
467ifcapable attach {
468  SQL {ATTACH 'test2.db' AS aux;}
469  SQL {BEGIN}
470  SQL {CREATE TABLE aux.tbl2(x, y, z)}
471  SQL {INSERT INTO tbl2 VALUES(1, 2, 3)}
472  SQL {INSERT INTO def VALUES(4, 5, 6)}
473  TEST 30 {
474    do_test $testid {
475      execsql {
476        SELECT * FROM tbl2, def WHERE d = x;
477      }
478    } {1 2 3 1 2 3}
479  }
480  SQL {COMMIT}
481  TEST 31 {
482    do_test $testid {
483      execsql {
484        SELECT * FROM tbl2, def WHERE d = x;
485      }
486    } {1 2 3 1 2 3}
487  }
488}
489
490# Test what happens when a malloc() fails while there are other active
491# statements. This changes the way sqlite3VdbeHalt() works.
492TEST 32 {
493  if {![info exists ::STMT32]} {
494    set sql "SELECT name FROM sqlite_master"
495    set ::STMT32 [sqlite3_prepare $::DB $sql -1 DUMMY]
496    do_test $testid {
497      sqlite3_step $::STMT32
498    } {SQLITE_ROW}
499  }
500}
501SQL BEGIN
502TEST 33 {
503  do_test $testid {
504    execsql {SELECT * FROM ghi}
505  } {a b c 1 2 3}
506}
507SQL -norollback {
508  -- There is a unique index on ghi(g), so this statement may not cause
509  -- an automatic ROLLBACK. Hence the "-norollback" switch.
510  INSERT INTO ghi SELECT '2'||g, h, i FROM ghi;
511}
512TEST 34 {
513  if {[info exists ::STMT32]} {
514    do_test $testid {
515      sqlite3_finalize $::STMT32
516    } {SQLITE_OK}
517    unset ::STMT32
518  }
519}
520SQL COMMIT
521
522#
523# End of test program declaration
524#--------------------------------------------------------------------------
525
526proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
527  if {[llength $arglist] %2} {
528    error "Uneven number of arguments to TEST"
529  }
530
531  for {set i 0} {$i < $pcstart} {incr i} {
532    set k2 [lindex $arglist [expr 2 * $i]]
533    set v2 [lindex $arglist [expr 2 * $i + 1]]
534    set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
535    switch -- $k2 {
536      -sql  {db eval [lindex $v2 1]}
537      -prep {db eval $v2}
538    }
539    set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit
540    if {$ac && !$nac} {set begin_pc $i}
541  }
542
543  db rollback_hook [list incr ::rollback_hook_count]
544
545  set iFail $iFailStart
546  set pc $pcstart
547  while {$pc*2 < [llength $arglist]} {
548
549    # Id of this iteration:
550    set k [lindex $arglist [expr 2 * $pc]]
551    set iterid "pc=$pc.iFail=$iFail$k"
552    set v [lindex $arglist [expr 2 * $pc + 1]]
553
554    switch -- $k {
555
556      -test {
557        foreach {id script} $v {}
558        incr pc
559      }
560
561      -sql {
562        set ::rollback_hook_count 0
563
564        set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
565        sqlite3_memdebug_fail $iFail -repeat 0
566        set rc [catch {db eval [lindex $v 1]} msg]   ;# True error occurs
567        set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit
568
569        if {$rc != 0 && $nac && !$ac} {
570          # Before [db eval] the auto-commit flag was clear. Now it
571          # is set. Since an error occured we assume this was not a
572          # commit - therefore a rollback occured. Check that the
573          # rollback-hook was invoked.
574          do_test malloc3-rollback_hook.$iterid {
575            set ::rollback_hook_count
576          } {1}
577        }
578
579        set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
580        if {$rc == 0} {
581            # Successful execution of sql. The number of failed malloc()
582            # calls should be equal to the number of benign failures.
583            # Otherwise a malloc() failed and the error was not reported.
584            #
585            if {$nFail!=$nBenign} {
586              error "Unreported malloc() failure"
587            }
588
589            if {$ac && !$nac} {
590              # Before the [db eval] the auto-commit flag was set, now it
591              # is clear. We can deduce that a "BEGIN" statement has just
592              # been successfully executed.
593              set begin_pc $pc
594            }
595
596            incr pc
597            set iFail 1
598            integrity_check "malloc3-(integrity).$iterid"
599        } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} {
600            # Out of memory error, as expected.
601            #
602            integrity_check "malloc3-(integrity).$iterid"
603            incr iFail
604            if {$nac && !$ac} {
605
606              if {![lindex $v 0] && [db errorcode] != 3082} {
607                # error "Statement \"[lindex $v 1]\" caused a rollback"
608              }
609
610              for {set i $begin_pc} {$i < $pc} {incr i} {
611                set k2 [lindex $arglist [expr 2 * $i]]
612                set v2 [lindex $arglist [expr 2 * $i + 1]]
613                set catchupsql ""
614                switch -- $k2 {
615                  -sql  {set catchupsql [lindex $v2 1]}
616                  -prep {set catchupsql $v2}
617                }
618                db eval $catchupsql
619              }
620            }
621        } else {
622            error $msg
623        }
624
625        while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} {
626          incr pc -1
627        }
628      }
629
630      -prep {
631        db eval $v
632        incr pc
633      }
634
635      -debug {
636        eval $v
637        incr pc
638      }
639
640      default { error "Unknown switch: $k" }
641    }
642  }
643}
644
645# Turn of the Tcl interface's prepared statement caching facility. Then
646# run the tests with "persistent" malloc failures.
647sqlite3_extended_result_codes db 1
648db cache size 0
649run_test $::run_test_script 1
650
651# Close and reopen the db.
652db close
653file delete -force test.db test.db-journal test2.db test2.db-journal
654sqlite3 db test.db
655sqlite3_extended_result_codes db 1
656set ::DB [sqlite3_connection_pointer db]
657
658# Turn of the Tcl interface's prepared statement caching facility in
659# the new connnection. Then run the tests with "transient" malloc failures.
660db cache size 0
661run_test $::run_test_script 0
662
663sqlite3_memdebug_fail -1
664finish_test
665