table.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "sql/recover_module/table.h"
  5. #include "base/check_op.h"
  6. #include "base/strings/strcat.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/strings/string_util.h"
  9. #include "sql/recover_module/cursor.h"
  10. #include "sql/recover_module/integers.h"
  11. #include "sql/recover_module/pager.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace sql {
  14. namespace recover {
  15. // Returns the number of the page holding the root page of a table's B-tree.
  16. //
  17. // Returns a null optional if the operation fails in any way. The failure is
  18. // most likely due to an incorrect table spec (missing attachment or table).
  19. // Corrupted SQLite metadata can cause failures here.
  20. absl::optional<int> GetTableRootPageId(sqlite3* sqlite_db,
  21. const TargetTableSpec& table) {
  22. if (table.table_name == "sqlite_schema") {
  23. // The sqlite_schema table is always rooted at the first page.
  24. // SQLite page IDs use 1-based indexing.
  25. return absl::optional<int64_t>(1);
  26. }
  27. std::string select_sql =
  28. base::StrCat({"SELECT rootpage FROM ", table.db_name,
  29. ".sqlite_schema WHERE type='table' AND tbl_name=?"});
  30. sqlite3_stmt* sqlite_statement;
  31. if (sqlite3_prepare_v3(sqlite_db, select_sql.c_str(), select_sql.size() + 1,
  32. SQLITE_PREPARE_NO_VTAB, &sqlite_statement,
  33. nullptr) != SQLITE_OK) {
  34. // The sqlite_schema table is missing or its schema is corrupted.
  35. return absl::nullopt;
  36. }
  37. if (sqlite3_bind_text(sqlite_statement, 1, table.table_name.c_str(),
  38. static_cast<int>(table.table_name.size()),
  39. SQLITE_STATIC) != SQLITE_OK) {
  40. // Binding the table name failed. This shouldn't happen.
  41. sqlite3_finalize(sqlite_statement);
  42. return absl::nullopt;
  43. }
  44. if (sqlite3_step(sqlite_statement) != SQLITE_ROW) {
  45. // The database attachment point or table does not exist.
  46. sqlite3_finalize(sqlite_statement);
  47. return absl::nullopt;
  48. }
  49. int64_t root_page = sqlite3_column_int64(sqlite_statement, 0);
  50. sqlite3_finalize(sqlite_statement);
  51. if (!DatabasePageReader::IsValidPageId(root_page)) {
  52. // Database corruption.
  53. return absl::nullopt;
  54. }
  55. static_assert(
  56. DatabasePageReader::kMaxPageId <= std::numeric_limits<int>::max(),
  57. "Converting the page ID to int may overflow");
  58. return absl::make_optional(static_cast<int>(root_page));
  59. }
  60. // Returns (SQLite status, a SQLite database's page size).
  61. std::pair<int, int> GetDatabasePageSize(sqlite3_file* sqlite_file) {
  62. // The SQLite header is documented at:
  63. // https://www.sqlite.org/fileformat.html#the_database_header
  64. //
  65. // Read the entire header.
  66. static constexpr int kHeaderOffset = 0;
  67. static constexpr int kHeaderSize = 100;
  68. uint8_t header_bytes[kHeaderSize];
  69. int sqlite_status = DatabasePageReader::RawRead(sqlite_file, kHeaderSize,
  70. kHeaderOffset, header_bytes);
  71. if (sqlite_status != SQLITE_OK)
  72. return {sqlite_status, 0};
  73. // This computation uses the alternate interpretation that the page size
  74. // header field is a little-endian number encoding the page size divided by
  75. // 256.
  76. static constexpr int kPageSizeHeaderOffset = 16;
  77. const int page_size =
  78. LoadBigEndianUint16(header_bytes + kPageSizeHeaderOffset);
  79. if (!DatabasePageReader::IsValidPageSize(page_size)) {
  80. // Invalid page numbers are considered irrecoverable corruption.
  81. return {SQLITE_CORRUPT, 0};
  82. }
  83. // TODO(pwnall): This method needs a better name. It also checks the database
  84. // header for unsupported edge cases.
  85. static constexpr int kReservedSizeHeaderOffset = 20;
  86. const uint8_t page_reserved_size = header_bytes[kReservedSizeHeaderOffset];
  87. if (page_reserved_size != 0) {
  88. // Chrome does not use any extension that requires reserved page space.
  89. return {SQLITE_CORRUPT, 0};
  90. }
  91. // The text encoding is stored at offset 56, as a 4-byte big-endian integer.
  92. // However, the range of values is 1-3, so reading the last byte is
  93. // sufficient.
  94. static_assert(SQLITE_UTF8 <= std::numeric_limits<uint8_t>::max(),
  95. "Text encoding field reading shortcut is invalid.");
  96. static constexpr int kTextEncodingHeaderOffset = 59;
  97. const uint8_t text_encoding = header_bytes[kTextEncodingHeaderOffset];
  98. if (text_encoding != SQLITE_UTF8) {
  99. // This extension only supports databases that use UTF-8 encoding.
  100. return {SQLITE_CORRUPT, 0};
  101. }
  102. return {SQLITE_OK, page_size};
  103. }
  104. // static
  105. std::pair<int, std::unique_ptr<VirtualTable>> VirtualTable::Create(
  106. sqlite3* sqlite_db,
  107. TargetTableSpec backing_table_spec,
  108. std::vector<RecoveredColumnSpec> column_specs) {
  109. DCHECK(backing_table_spec.IsValid());
  110. absl::optional<int64_t> backing_table_root_page_id =
  111. GetTableRootPageId(sqlite_db, backing_table_spec);
  112. if (!backing_table_root_page_id.has_value()) {
  113. // Either the backing table specification is incorrect, or the database
  114. // metadata is corrupted beyond hope.
  115. //
  116. // This virtual table is intended to be used by Chrome features, whose code
  117. // is covered by tests. Therefore, the most likely cause is metadata
  118. // corruption.
  119. return {SQLITE_CORRUPT, nullptr};
  120. }
  121. sqlite3_file* sqlite_file;
  122. int sqlite_status =
  123. sqlite3_file_control(sqlite_db, backing_table_spec.db_name.c_str(),
  124. SQLITE_FCNTL_FILE_POINTER, &sqlite_file);
  125. if (sqlite_status != SQLITE_OK) {
  126. // Failed to get the backing store's file. GetTableRootPage() succeeded, so
  127. // the attachment point name must be correct. So, this is definitely a
  128. // SQLite error, not a virtual table use error. Report the error code as-is,
  129. // so it can be captured in histograms.
  130. return {sqlite_status, nullptr};
  131. }
  132. int page_size;
  133. std::tie(sqlite_status, page_size) = GetDatabasePageSize(sqlite_file);
  134. if (sqlite_status != SQLITE_OK) {
  135. // By the same reasoning as above, report the error code as-is.
  136. return {sqlite_status, nullptr};
  137. }
  138. return {SQLITE_OK,
  139. std::make_unique<VirtualTable>(sqlite_db, sqlite_file,
  140. backing_table_root_page_id.value(),
  141. page_size, std::move(column_specs))};
  142. }
  143. VirtualTable::VirtualTable(sqlite3* sqlite_db,
  144. sqlite3_file* sqlite_file,
  145. int root_page_id,
  146. int page_size,
  147. std::vector<RecoveredColumnSpec> column_specs)
  148. : sqlite_file_(sqlite_file),
  149. root_page_id_(root_page_id),
  150. page_size_(page_size),
  151. column_specs_(std::move(column_specs)) {
  152. DCHECK(sqlite_db != nullptr);
  153. DCHECK(sqlite_file != nullptr);
  154. DCHECK_GT(root_page_id_, 0);
  155. DCHECK(DatabasePageReader::IsValidPageSize(page_size));
  156. DCHECK(!column_specs_.empty());
  157. }
  158. VirtualTable::~VirtualTable() {
  159. #if DCHECK_IS_ON()
  160. DCHECK_EQ(0, open_cursor_count_.load(std::memory_order_relaxed))
  161. << "SQLite forgot to xClose() an xOpen()ed cursor";
  162. #endif // DCHECK_IS_ON()
  163. }
  164. std::string VirtualTable::ToCreateTableSql() const {
  165. std::vector<std::string> column_sqls;
  166. column_sqls.reserve(column_specs_.size());
  167. for (const RecoveredColumnSpec& column_spec : column_specs_)
  168. column_sqls.push_back(column_spec.ToCreateTableSql());
  169. static constexpr base::StringPiece kCreateTableSqlStart("CREATE TABLE t(");
  170. static constexpr base::StringPiece kCreateTableSqlEnd(")");
  171. static constexpr base::StringPiece kColumnSqlSeparator(",");
  172. return base::StrCat({kCreateTableSqlStart,
  173. base::JoinString(column_sqls, kColumnSqlSeparator),
  174. kCreateTableSqlEnd});
  175. }
  176. VirtualCursor* VirtualTable::CreateCursor() {
  177. #if DCHECK_IS_ON()
  178. open_cursor_count_.fetch_add(1, std::memory_order_relaxed);
  179. #endif // DCHECK_IS_ON()
  180. VirtualCursor* result = new VirtualCursor(this);
  181. return result;
  182. }
  183. void VirtualTable::WillDeleteCursor(VirtualCursor* cursor) {
  184. #if DCHECK_IS_ON()
  185. DCHECK_GT(open_cursor_count_.load(std::memory_order_relaxed), 0);
  186. open_cursor_count_.fetch_sub(1, std::memory_order_relaxed);
  187. #endif // DCHECK_IS_ON()
  188. }
  189. } // namespace recover
  190. } // namespace sql