corruptD.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # 2009 June 3
  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. # $Id: corruptD.test,v 1.2 2009/06/05 17:09:12 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. # Do not use a codec for tests in this file, as the database file is
  16. # manipulated directly using tcl scripts (using the [hexio_write] command).
  17. #
  18. do_not_use_codec
  19. #--------------------------------------------------------------------------
  20. # OVERVIEW
  21. #
  22. # This test file attempts to verify that SQLite does not read past the
  23. # end of any in-memory buffers as a result of corrupted database page
  24. # images. Usually this happens because a field within a database page
  25. # that contains an offset to some other structure within the same page
  26. # is set to too large a value. A database page contains the following
  27. # such fields:
  28. #
  29. # 1. The page header field that contains the offset to the first
  30. # free block of space.
  31. #
  32. # 2. The first two bytes of all but the last free block on the free-block
  33. # list (the offset to the next free block).
  34. #
  35. # 3. The page header field containing the number of cells on the page
  36. # (implicitly defines the offset to the final element in the cell offset
  37. # array, which could potentially be off the end of the page).
  38. #
  39. # 4. The page header field containing the offset to the start of the cell
  40. # content area.
  41. #
  42. # 5. The contents of the cell offset array.
  43. #
  44. # 6. The first few bytes of each cell determine the size of the cell
  45. # stored within the page, and hence the offset to the final byte of
  46. # the cell.
  47. #
  48. # If any of the above fields are set to too large a value, then a buffer
  49. # overread may occur. This test script creates and operates on various
  50. # strategically corrupted database files to attempt to provoke such buffer
  51. # overreads.
  52. #
  53. # Very often, a buffer overread passes unnoticed, particularly in workstation
  54. # environments. For this reason, this test script should be run using valgrind
  55. # (or similar) in order to verify that no overreads occur.
  56. #
  57. # TEST PLAN
  58. #
  59. # Test cases corruptD-1.* are white-box tests. They attempt to corrupt
  60. # one of the above fields, then exercise each part of the code in btree.c
  61. # that uses said field.
  62. #
  63. # Offset variables 1, 2, 3 and 4 are all checked to make sure they
  64. # will not result in buffer overruns as part of page initialization in
  65. # sqlite3BtreeInitPage(). Offsets 5 and 6 cannot be tested as part of
  66. # page initialization, as trying to do so causes a performance hit.
  67. #
  68. do_test corruptD-1.0 {
  69. execsql {
  70. PRAGMA auto_vacuum = 0;
  71. PRAGMA page_size = 1024;
  72. CREATE TABLE t1(a, b);
  73. CREATE INDEX i1 ON t1(a, b);
  74. }
  75. for {set ii 1} {$ii < 50} {incr ii} {
  76. execsql { INSERT INTO t1 VALUES($ii, $ii * $ii) }
  77. }
  78. execsql {
  79. DELETE FROM t1 WHERE a = 10;
  80. DELETE FROM t1 WHERE a = 20;
  81. DELETE FROM t1 WHERE a = 30;
  82. DELETE FROM t1 WHERE a = 40;
  83. }
  84. forcecopy test.db test.bu
  85. } {}
  86. proc incr_change_counter {} {
  87. hexio_write test.db 24 [
  88. hexio_render_int32 [expr [hexio_get_int [hexio_read test.db 24 4]] + 1]
  89. ]
  90. }
  91. proc restore_file {} {
  92. db close
  93. forcecopy test.bu test.db
  94. sqlite3 db test.db
  95. }
  96. #-------------------------------------------------------------------------
  97. # The following tests, corruptD-1.1.*, focus on the page header field
  98. # containing the offset of the first free block in a page.
  99. #
  100. do_test corruptD-1.1.1 {
  101. incr_change_counter
  102. hexio_write test.db [expr 1024+1] FFFF
  103. catchsql { SELECT * FROM t1 ORDER BY rowid }
  104. } {1 {database disk image is malformed}}
  105. do_test corruptD-1.1.2 {
  106. incr_change_counter
  107. hexio_write test.db [expr 1024+1] [hexio_render_int32 1021]
  108. catchsql { SELECT * FROM t1 ORDER BY rowid }
  109. } {1 {database disk image is malformed}}
  110. #-------------------------------------------------------------------------
  111. # The following tests, corruptD-1.2.*, focus on the offsets contained
  112. # in the first 2 byte of each free-block on the free-list.
  113. #
  114. do_test corruptD-1.2.1 {
  115. restore_file
  116. } {}
  117. do_test corruptD-1.2.2 {
  118. } {}
  119. #-------------------------------------------------------------------------
  120. # The following tests, corruptD-1.4.*, ...
  121. #
  122. #-------------------------------------------------------------------------
  123. # The following tests, corruptD-1.5.*, focus on the offsets contained
  124. # in the cell offset array.
  125. #
  126. # defragmentPage
  127. #
  128. finish_test