crash5.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # 2007 Aug 13
  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 tests aspects of recovery from a malloc() failure
  13. # in a CREATE INDEX statement.
  14. #
  15. # $Id: crash5.test,v 1.3 2008/07/12 14:52:20 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Only run these tests if memory debugging is turned on.
  19. #
  20. ifcapable !memdebug||!crashtest||!memorymanage {
  21. puts "Skipping crash5 tests: not compiled with -DSQLITE_MEMDEBUG..."
  22. finish_test
  23. return
  24. }
  25. db close
  26. for {set ii 0} {$ii < 10} {incr ii} {
  27. for {set jj 50} {$jj < 100} {incr jj} {
  28. # Set up the database so that it is an auto-vacuum database
  29. # containing a single table (root page 3) with a single row.
  30. # The row has an overflow page (page 4).
  31. forcedelete test.db test.db-journal
  32. sqlite3 db test.db
  33. set c [string repeat 3 1500]
  34. db eval {
  35. pragma auto_vacuum = 1;
  36. CREATE TABLE t1(a, b, c);
  37. INSERT INTO t1 VALUES('1111111111', '2222222222', $c);
  38. }
  39. db close
  40. do_test crash5-$ii.$jj.1 {
  41. crashsql -delay 1 -file test.db-journal -seed $ii -tclbody [join [list \
  42. [list set iFail $jj] {
  43. sqlite3_crashparams 0 [file join [get_pwd] test.db-journal]
  44. # Begin a transaction and evaluate a "CREATE INDEX" statement
  45. # with the iFail'th malloc() set to fail. This operation will
  46. # have to move the current contents of page 4 (the overflow
  47. # page) to make room for the new root page. The bug is that
  48. # if malloc() fails at a particular point in sqlite3PagerMovepage(),
  49. # sqlite mistakenly thinks that the page being moved (page 4) has
  50. # been safely synced into the journal. If the page is written
  51. # to later in the transaction, it may be written out to the database
  52. # before the relevant part of the journal has been synced.
  53. #
  54. db eval BEGIN
  55. sqlite3_memdebug_fail $iFail -repeat 0
  56. catch {db eval { CREATE UNIQUE INDEX i1 ON t1(a); }} msg
  57. # puts "$n $msg ac=[sqlite3_get_autocommit db]"
  58. # If the transaction is still active (it may not be if the malloc()
  59. # failure occurred in the OS layer), write to the database. Make sure
  60. # page 4 is among those written.
  61. #
  62. if {![sqlite3_get_autocommit db]} {
  63. db eval {
  64. DELETE FROM t1; -- This will put page 4 on the free list.
  65. INSERT INTO t1 VALUES('111111111', '2222222222', '33333333');
  66. INSERT INTO t1 SELECT * FROM t1; -- 2
  67. INSERT INTO t1 SELECT * FROM t1; -- 4
  68. INSERT INTO t1 SELECT * FROM t1; -- 8
  69. INSERT INTO t1 SELECT * FROM t1; -- 16
  70. INSERT INTO t1 SELECT * FROM t1; -- 32
  71. INSERT INTO t1 SELECT * FROM t1 WHERE rowid%2; -- 48
  72. }
  73. }
  74. # If the right malloc() failed during the 'CREATE INDEX' above and
  75. # the transaction was not rolled back, then the sqlite cache now
  76. # has a dirty page 4 that it incorrectly believes is already safely
  77. # in the synced part of the journal file. When
  78. # sqlite3_release_memory() is called sqlite tries to free memory
  79. # by writing page 4 out to the db file. If it crashes later on,
  80. # before syncing the journal... Corruption!
  81. #
  82. sqlite3_crashparams 1 [file join [get_pwd] test.db-journal]
  83. sqlite3_release_memory 8092
  84. }]] {}
  85. expr 1
  86. } {1}
  87. sqlite3 db test.db
  88. do_test crash5-$ii.$jj.2 {
  89. db eval {pragma integrity_check}
  90. } {ok}
  91. do_test crash5-$ii.$jj.3 {
  92. db eval {SELECT * FROM t1}
  93. } [list 1111111111 2222222222 $::c]
  94. db close
  95. }
  96. }
  97. finish_test