blob: fc078b3504cb1673d3f1b46ee6d02c60c6ad13ca [file] [log] [blame]
danielk1977a98d7b42008-01-18 13:42:54 +00001
2# 2007 Aug 13
3#
4# The author disclaims copyright to this source code. In place of
5# a legal notice, here is a blessing:
6#
7# May you do good and not evil.
8# May you find forgiveness for yourself and forgive others.
9# May you share freely, never taking more than you give.
10#
11#***********************************************************************
12#
13# This file tests aspects of recovery from a malloc() failure
14# in a CREATE INDEX statement.
15#
drhe8f52c52008-07-12 14:52:20 +000016# $Id: crash5.test,v 1.3 2008/07/12 14:52:20 drh Exp $
danielk1977a98d7b42008-01-18 13:42:54 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Only run these tests if memory debugging is turned on.
22#
dan253c6ee2018-09-18 17:00:06 +000023ifcapable !crashtest||!memorymanage {
mistachkinf3ebea82021-01-18 19:27:56 +000024 puts "Skipping crash5 tests: not compiled with -DSQLITE_ENABLE_MEMORY_MANAGEMENT..."
danielk1977a98d7b42008-01-18 13:42:54 +000025 finish_test
26 return
27}
28
29db close
30
31for {set ii 0} {$ii < 10} {incr ii} {
dan5cb960b2021-01-07 16:29:34 +000032 for {set jj 1} {$jj < 100} {incr jj} {
danielk1977a98d7b42008-01-18 13:42:54 +000033
34 # Set up the database so that it is an auto-vacuum database
35 # containing a single table (root page 3) with a single row.
36 # The row has an overflow page (page 4).
mistachkinfda06be2011-08-02 00:57:34 +000037 forcedelete test.db test.db-journal
danielk1977a98d7b42008-01-18 13:42:54 +000038 sqlite3 db test.db
39 set c [string repeat 3 1500]
40 db eval {
41 pragma auto_vacuum = 1;
42 CREATE TABLE t1(a, b, c);
43 INSERT INTO t1 VALUES('1111111111', '2222222222', $c);
44 }
45 db close
46
47 do_test crash5-$ii.$jj.1 {
48 crashsql -delay 1 -file test.db-journal -seed $ii -tclbody [join [list \
49 [list set iFail $jj] {
dan5cb960b2021-01-07 16:29:34 +000050 proc get_pwd {} {
51 if {$::tcl_platform(platform) eq "windows"} {
mistachkinf3ebea82021-01-18 19:27:56 +000052 if {[info exists ::env(ComSpec)]} {
53 set comSpec $::env(ComSpec)
54 } else {
55 # NOTE: Hard-code the typical default value.
56 set comSpec {C:\Windows\system32\cmd.exe}
57 }
dan5cb960b2021-01-07 16:29:34 +000058 return [string map [list \\ /] \
mistachkinf3ebea82021-01-18 19:27:56 +000059 [string trim [exec -- $comSpec /c echo %CD%]]]
dan5cb960b2021-01-07 16:29:34 +000060 } else {
61 return [pwd]
62 }
63 }
mistachkinf8a78462012-03-08 20:00:36 +000064 sqlite3_crashparams 0 [file join [get_pwd] test.db-journal]
danielk1977a98d7b42008-01-18 13:42:54 +000065
66 # Begin a transaction and evaluate a "CREATE INDEX" statement
67 # with the iFail'th malloc() set to fail. This operation will
68 # have to move the current contents of page 4 (the overflow
69 # page) to make room for the new root page. The bug is that
70 # if malloc() fails at a particular point in sqlite3PagerMovepage(),
71 # sqlite mistakenly thinks that the page being moved (page 4) has
72 # been safely synced into the journal. If the page is written
73 # to later in the transaction, it may be written out to the database
74 # before the relevant part of the journal has been synced.
75 #
76 db eval BEGIN
77 sqlite3_memdebug_fail $iFail -repeat 0
dan5cb960b2021-01-07 16:29:34 +000078 set rc [catch {db eval { CREATE UNIQUE INDEX i1 ON t1(a); }} msg]
79# puts "$msg ac=[sqlite3_get_autocommit db] iFail=$iFail"
80# puts "fail=[sqlite3_memdebug_fail -1]"
danielk1977a98d7b42008-01-18 13:42:54 +000081
dan5cb960b2021-01-07 16:29:34 +000082 if {$rc} {
83 # If the transaction is still active (it may not be if the malloc()
84 # failure occurred in the OS layer), write to the database. Make sure
85 # page 4 is among those written.
86 #
87 if {![sqlite3_get_autocommit db]} {
88 db eval {
89 DELETE FROM t1; -- This will put page 4 on the free list.
90 INSERT INTO t1 VALUES('111111111', '2222222222', '33333333');
91 INSERT INTO t1 SELECT * FROM t1; -- 2
92 INSERT INTO t1 SELECT * FROM t1; -- 4
93 INSERT INTO t1 SELECT * FROM t1; -- 8
94 INSERT INTO t1 SELECT * FROM t1; -- 16
95 INSERT INTO t1 SELECT * FROM t1; -- 32
96 INSERT INTO t1 SELECT * FROM t1 WHERE rowid%2; -- 48
97 }
danielk1977a98d7b42008-01-18 13:42:54 +000098 }
dan5cb960b2021-01-07 16:29:34 +000099
100 # If the right malloc() failed during the 'CREATE INDEX' above and
101 # the transaction was not rolled back, then the sqlite cache now
102 # has a dirty page 4 that it incorrectly believes is already safely
103 # in the synced part of the journal file. When
104 # sqlite3_release_memory() is called sqlite tries to free memory
105 # by writing page 4 out to the db file. If it crashes later on,
106 # before syncing the journal... Corruption!
107 #
108 sqlite3_crashparams 1 [file join [get_pwd] test.db-journal]
109 sqlite3_release_memory 8092
danielk1977a98d7b42008-01-18 13:42:54 +0000110 }
danielk1977a98d7b42008-01-18 13:42:54 +0000111 }]] {}
112 expr 1
113 } {1}
114
115 sqlite3 db test.db
116 do_test crash5-$ii.$jj.2 {
117 db eval {pragma integrity_check}
118 } {ok}
119 do_test crash5-$ii.$jj.3 {
120 db eval {SELECT * FROM t1}
121 } [list 1111111111 2222222222 $::c]
122 db close
123 }
124}
125
126
127finish_test