blob: 5e3605c010b7b7c823803b43ad9bca27dad4b43a [file] [log] [blame]
[email protected]64021042012-02-10 20:02:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Victor Costancfbfa602018-08-01 23:24:465#include "sql/database.h"
[email protected]e5ffd0e42009-09-11 21:30:566
avi51ba3e692015-12-26 17:30:507#include <limits.h>
avi0b519202015-12-21 07:25:198#include <stddef.h>
9#include <stdint.h>
[email protected]e5ffd0e42009-09-11 21:30:5610#include <string.h>
mostynbd82cd9952016-04-11 20:05:3411
Shubham Aggarwalbe4f97ce2020-06-19 15:58:5712#include "base/feature_list.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4414#include "base/files/file_util.h"
shessc8cd2a162015-10-22 20:30:4615#include "base/format_macros.h"
fdoray2dfa76452016-06-07 13:11:2216#include "base/location.h"
[email protected]e5ffd0e42009-09-11 21:30:5617#include "base/logging.h"
Ilya Sherman1c811db2017-12-14 10:36:1818#include "base/metrics/histogram_functions.h"
asvitkine3033081a2016-08-30 04:01:0819#include "base/metrics/histogram_macros.h"
[email protected]210ce0af2013-05-15 09:10:3920#include "base/metrics/sparse_histogram.h"
Victor Costan3653df62018-02-08 21:38:1621#include "base/no_destructor.h"
Will Harrisb8693592018-08-28 22:58:4422#include "base/numerics/safe_conversions.h"
fdoray2dfa76452016-06-07 13:11:2223#include "base/single_thread_task_runner.h"
[email protected]80abf152013-05-22 12:42:4224#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1925#include "base/strings/string_util.h"
26#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4527#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1828#include "base/synchronization/lock.h"
Etienne Pierre-Doray0400dfb62018-12-03 19:12:2529#include "base/threading/scoped_blocking_call.h"
ssid9f8022f2015-10-12 17:49:0330#include "base/trace_event/memory_dump_manager.h"
Etienne Bergeron6cb35c72020-02-12 18:09:4831#include "base/trace_event/trace_event.h"
Kevin Marshalla9f05ec2017-07-14 02:10:0532#include "build/build_config.h"
Victor Costancfbfa602018-08-01 23:24:4633#include "sql/database_memory_dump_provider.h"
Victor Costan3653df62018-02-08 21:38:1634#include "sql/initialization.h"
shess9bf2c672015-12-18 01:18:0835#include "sql/meta_table.h"
Victor Costan3ffd9a372019-05-22 00:46:5436#include "sql/sql_features.h"
[email protected]f0a54b22011-07-19 18:40:2137#include "sql/statement.h"
shess5f2c3442017-01-24 02:15:1038#include "sql/vfs_wrapper.h"
[email protected]e33cba42010-08-18 23:37:0339#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5640
[email protected]5b96f3772010-09-28 16:30:5741namespace {
42
Ken Rockot01687422020-08-17 18:00:5943bool enable_mmap_by_default_ = true;
44
[email protected]5b96f3772010-09-28 16:30:5745// Spin for up to a second waiting for the lock to clear when setting
46// up the database.
47// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2748const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5749
50class ScopedBusyTimeout {
51 public:
Victor Costancfbfa602018-08-01 23:24:4652 explicit ScopedBusyTimeout(sqlite3* db) : db_(db) {}
53 ~ScopedBusyTimeout() { sqlite3_busy_timeout(db_, 0); }
[email protected]5b96f3772010-09-28 16:30:5754
55 int SetTimeout(base::TimeDelta timeout) {
56 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
57 return sqlite3_busy_timeout(db_,
58 static_cast<int>(timeout.InMilliseconds()));
59 }
60
61 private:
62 sqlite3* db_;
63};
64
[email protected]6d42f152012-11-10 00:38:2465// Helper to "safely" enable writable_schema. No error checking
66// because it is reasonable to just forge ahead in case of an error.
67// If turning it on fails, then most likely nothing will work, whereas
68// if turning it off fails, it only matters if some code attempts to
69// continue working with the database and tries to modify the
70// sqlite_master table (none of our code does this).
71class ScopedWritableSchema {
72 public:
Victor Costancfbfa602018-08-01 23:24:4673 explicit ScopedWritableSchema(sqlite3* db) : db_(db) {
Victor Costanbd623112018-07-18 04:17:2774 sqlite3_exec(db_, "PRAGMA writable_schema=1", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2475 }
76 ~ScopedWritableSchema() {
Victor Costanbd623112018-07-18 04:17:2777 sqlite3_exec(db_, "PRAGMA writable_schema=0", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2478 }
79
80 private:
81 sqlite3* db_;
82};
83
[email protected]7bae5742013-07-10 20:46:1684// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
85// SQLite error code from running the backup step.
86int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
87 DCHECK_NE(src, dst);
88 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
89 if (!backup) {
90 // Since this call only sets things up, this indicates a gross
91 // error in SQLite.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:5292 DLOG(DCHECK) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
[email protected]7bae5742013-07-10 20:46:1693 return sqlite3_errcode(dst);
94 }
95
96 // -1 backs up the entire database.
97 int rc = sqlite3_backup_step(backup, -1);
98 int pages = sqlite3_backup_pagecount(backup);
99 sqlite3_backup_finish(backup);
100
101 // If successful, exactly one page should have been backed up. If
102 // this breaks, check this function to make sure assumptions aren't
103 // being broken.
104 if (rc == SQLITE_DONE)
105 DCHECK_EQ(pages, 1);
106
107 return rc;
108}
109
[email protected]8d409412013-07-19 18:25:30110// Be very strict on attachment point. SQLite can handle a much wider
111// character set with appropriate quoting, but Chromium code should
112// just use clean names to start with.
113bool ValidAttachmentPoint(const char* attachment_point) {
114 for (size_t i = 0; attachment_point[i]; ++i) {
zhongyi23960342016-04-12 23:13:20115 if (!(base::IsAsciiDigit(attachment_point[i]) ||
116 base::IsAsciiAlpha(attachment_point[i]) ||
[email protected]8d409412013-07-19 18:25:30117 attachment_point[i] == '_')) {
118 return false;
119 }
120 }
121 return true;
122}
123
[email protected]8ada10f2013-12-21 00:42:34124// Helper to get the sqlite3_file* associated with the "main" database.
125int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
Victor Costanbd623112018-07-18 04:17:27126 *file = nullptr;
127 int rc = sqlite3_file_control(db, nullptr, SQLITE_FCNTL_FILE_POINTER, file);
[email protected]8ada10f2013-12-21 00:42:34128 if (rc != SQLITE_OK)
129 return rc;
130
Victor Costanbd623112018-07-18 04:17:27131 // TODO(shess): null in file->pMethods has been observed on android_dbg
[email protected]8ada10f2013-12-21 00:42:34132 // content_unittests, even though it should not be possible.
133 // http://crbug.com/329982
134 if (!*file || !(*file)->pMethods)
135 return SQLITE_ERROR;
136
137 return rc;
138}
139
shess5dac334f2015-11-05 20:47:42140// Convenience to get the sqlite3_file* and the size for the "main" database.
141int GetSqlite3FileAndSize(sqlite3* db,
Victor Costancfbfa602018-08-01 23:24:46142 sqlite3_file** file,
143 sqlite3_int64* db_size) {
shess5dac334f2015-11-05 20:47:42144 int rc = GetSqlite3File(db, file);
145 if (rc != SQLITE_OK)
146 return rc;
147
148 return (*file)->pMethods->xFileSize(*file, db_size);
149}
150
erg102ceb412015-06-20 01:38:13151std::string AsUTF8ForSQL(const base::FilePath& path) {
152#if defined(OS_WIN)
Jan Wilken Dörrie9720dce2020-07-21 17:14:23153 return base::WideToUTF8(path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18154#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
erg102ceb412015-06-20 01:38:13155 return path.value();
156#endif
157}
158
[email protected]5b96f3772010-09-28 16:30:57159} // namespace
160
[email protected]e5ffd0e42009-09-11 21:30:56161namespace sql {
162
[email protected]4350e322013-06-18 22:18:10163// static
Victor Costancfbfa602018-08-01 23:24:46164Database::ErrorExpecterCallback* Database::current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10165
166// static
Victor Costancfbfa602018-08-01 23:24:46167bool Database::IsExpectedSqliteError(int error) {
shess976814402016-06-21 06:56:25168 if (!current_expecter_cb_)
[email protected]4350e322013-06-18 22:18:10169 return false;
shess976814402016-06-21 06:56:25170 return current_expecter_cb_->Run(error);
[email protected]4350e322013-06-18 22:18:10171}
172
173// static
Victor Costancfbfa602018-08-01 23:24:46174void Database::SetErrorExpecter(Database::ErrorExpecterCallback* cb) {
Victor Costanbd623112018-07-18 04:17:27175 CHECK(!current_expecter_cb_);
shess976814402016-06-21 06:56:25176 current_expecter_cb_ = cb;
[email protected]4350e322013-06-18 22:18:10177}
178
179// static
Victor Costancfbfa602018-08-01 23:24:46180void Database::ResetErrorExpecter() {
shess976814402016-06-21 06:56:25181 CHECK(current_expecter_cb_);
Victor Costanbd623112018-07-18 04:17:27182 current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10183}
184
Victor Costance678e72018-07-24 10:25:00185// static
Victor Costancfbfa602018-08-01 23:24:46186base::FilePath Database::JournalPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00187 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-journal"));
188}
189
190// static
Victor Costancfbfa602018-08-01 23:24:46191base::FilePath Database::WriteAheadLogPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00192 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-wal"));
193}
194
195// static
Victor Costancfbfa602018-08-01 23:24:46196base::FilePath Database::SharedMemoryFilePath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00197 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-shm"));
198}
199
Victor Costancfbfa602018-08-01 23:24:46200Database::StatementRef::StatementRef(Database* database,
201 sqlite3_stmt* stmt,
202 bool was_valid)
203 : database_(database), stmt_(stmt), was_valid_(was_valid) {
204 if (database)
205 database_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56206}
207
Victor Costancfbfa602018-08-01 23:24:46208Database::StatementRef::~StatementRef() {
209 if (database_)
210 database_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38211 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56212}
213
Victor Costancfbfa602018-08-01 23:24:46214void Database::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56215 if (stmt_) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54216 // Call to InitScopedBlockingCall() cannot go at the beginning of the
217 // function because Close() is called unconditionally from destructor to
218 // clean database_. And if this is inactive statement this won't cause any
219 // disk access and destructor most probably will be called on thread not
220 // allowing disk access.
[email protected]35f7e5392012-07-27 19:54:50221 // TODO([email protected]): This should move to the beginning
222 // of the function. http://crbug.com/136655.
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54223 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:20224 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
[email protected]e5ffd0e42009-09-11 21:30:56225 sqlite3_finalize(stmt_);
Victor Costanbd623112018-07-18 04:17:27226 stmt_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56227 }
Victor Costancfbfa602018-08-01 23:24:46228 database_ = nullptr; // The Database may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38229
230 // Forced close is expected to happen from a statement error
231 // handler. In that case maintain the sense of |was_valid_| which
232 // previously held for this ref.
233 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56234}
235
Victor Costan7f6abbbe2018-07-29 02:57:27236static_assert(
Victor Costancfbfa602018-08-01 23:24:46237 Database::kDefaultPageSize == SQLITE_DEFAULT_PAGE_SIZE,
238 "Database::kDefaultPageSize must match the value configured into SQLite");
Victor Costan7f6abbbe2018-07-29 02:57:27239
Victor Costancfbfa602018-08-01 23:24:46240constexpr int Database::kDefaultPageSize;
Victor Costan7f6abbbe2018-07-29 02:57:27241
Victor Costancfbfa602018-08-01 23:24:46242Database::Database()
Victor Costanbd623112018-07-18 04:17:27243 : db_(nullptr),
Victor Costan7f6abbbe2018-07-29 02:57:27244 page_size_(kDefaultPageSize),
[email protected]e5ffd0e42009-09-11 21:30:56245 cache_size_(0),
246 exclusive_locking_(false),
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57247 want_wal_mode_(
248 base::FeatureList::IsEnabled(features::kEnableWALModeByDefault)),
[email protected]e5ffd0e42009-09-11 21:30:56249 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50250 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16251 in_memory_(false),
shess58b8df82015-06-03 00:19:32252 poisoned_(false),
shessa62504d2016-11-07 19:26:12253 mmap_alt_status_(false),
Ken Rockot01687422020-08-17 18:00:59254 mmap_disabled_(!enable_mmap_by_default_),
shess7dbd4dee2015-10-06 17:39:16255 mmap_enabled_(false),
256 total_changes_at_last_release_(0),
Victor Costan5e785e32019-02-26 20:39:31257 stats_histogram_(nullptr) {}
[email protected]e5ffd0e42009-09-11 21:30:56258
Victor Costancfbfa602018-08-01 23:24:46259Database::~Database() {
[email protected]e5ffd0e42009-09-11 21:30:56260 Close();
261}
262
Ken Rockot01687422020-08-17 18:00:59263void Database::DisableMmapByDefault() {
264 enable_mmap_by_default_ = false;
265}
266
Victor Costancfbfa602018-08-01 23:24:46267void Database::RecordEvent(Events event, size_t count) {
shess58b8df82015-06-03 00:19:32268 for (size_t i = 0; i < count; ++i) {
Victor Costanb0e7fc02019-03-01 03:36:27269 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats2", event, EVENT_MAX_VALUE);
shess58b8df82015-06-03 00:19:32270 }
271
272 if (stats_histogram_) {
273 for (size_t i = 0; i < count; ++i) {
274 stats_histogram_->Add(event);
275 }
276 }
277}
278
Victor Costancfbfa602018-08-01 23:24:46279bool Database::Open(const base::FilePath& path) {
Etienne Bergeron6cb35c72020-02-12 18:09:48280 TRACE_EVENT1("sql", "Database::Open", "path", path.MaybeAsASCII());
erg102ceb412015-06-20 01:38:13281 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42282}
[email protected]e5ffd0e42009-09-11 21:30:56283
Victor Costancfbfa602018-08-01 23:24:46284bool Database::OpenInMemory() {
Etienne Bergeron6cb35c72020-02-12 18:09:48285 TRACE_EVENT0("sql", "Database::OpenInMemory");
[email protected]35f7e5392012-07-27 19:54:50286 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13287 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56288}
289
Victor Costancfbfa602018-08-01 23:24:46290bool Database::OpenTemporary() {
Etienne Bergeron6cb35c72020-02-12 18:09:48291 TRACE_EVENT0("sql", "Database::OpenTemporary");
[email protected]8d409412013-07-19 18:25:30292 return OpenInternal("", NO_RETRY);
293}
294
Victor Costancfbfa602018-08-01 23:24:46295void Database::CloseInternal(bool forced) {
Etienne Bergeron6cb35c72020-02-12 18:09:48296 TRACE_EVENT0("sql", "Database::CloseInternal");
[email protected]4e179ba62012-03-17 16:06:47297 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
298 // will delete the -journal file. For ChromiumOS or other more
299 // embedded systems, this is probably not appropriate, whereas on
300 // desktop it might make some sense.
301
[email protected]4b350052012-02-24 20:40:48302 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48303
[email protected]41a97c812013-02-07 02:35:38304 // Release cached statements.
305 statement_cache_.clear();
306
307 // With cached statements released, in-use statements will remain.
308 // Closing the database while statements are in use is an API
309 // violation, except for forced close (which happens from within a
310 // statement's error handler).
311 DCHECK(forced || open_statements_.empty());
312
313 // Deactivate any outstanding statements so sqlite3_close() works.
Victor Costanc7e7f2e2018-07-18 20:07:55314 for (StatementRef* statement_ref : open_statements_)
315 statement_ref->Close(forced);
[email protected]41a97c812013-02-07 02:35:38316 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48317
[email protected]e5ffd0e42009-09-11 21:30:56318 if (db_) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54319 // Call to InitScopedBlockingCall() cannot go at the beginning of the
320 // function because Close() must be called from destructor to clean
[email protected]35f7e5392012-07-27 19:54:50321 // statement_cache_, it won't cause any disk access and it most probably
322 // will happen on thread not allowing disk access.
323 // TODO([email protected]): This should move to the beginning
324 // of the function. http://crbug.com/136655.
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54325 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:20326 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
[email protected]73fb8d52013-07-24 05:04:28327
Etienne Bergerone7681c72020-01-17 00:51:20328 // Resetting acquires a lock to ensure no dump is happening on the database
ssid3be5b1ec2016-01-13 14:21:57329 // at the same time. Unregister takes ownership of provider and it is safe
330 // since the db is reset. memory_dump_provider_ could be null if db_ was
331 // poisoned.
332 if (memory_dump_provider_) {
333 memory_dump_provider_->ResetDatabase();
334 base::trace_event::MemoryDumpManager::GetInstance()
335 ->UnregisterAndDeleteDumpProviderSoon(
336 std::move(memory_dump_provider_));
337 }
338
[email protected]73fb8d52013-07-24 05:04:28339 int rc = sqlite3_close(db_);
340 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:18341 base::UmaHistogramSparse("Sqlite.CloseFailure", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52342 DLOG(DCHECK) << "sqlite3_close failed: " << GetErrorMessage();
[email protected]73fb8d52013-07-24 05:04:28343 }
[email protected]e5ffd0e42009-09-11 21:30:56344 }
Victor Costanbd623112018-07-18 04:17:27345 db_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56346}
347
Victor Costancfbfa602018-08-01 23:24:46348void Database::Close() {
Etienne Bergeron6cb35c72020-02-12 18:09:48349 TRACE_EVENT0("sql", "Database::Close");
[email protected]41a97c812013-02-07 02:35:38350 // If the database was already closed by RazeAndClose(), then no
351 // need to close again. Clear the |poisoned_| bit so that incorrect
352 // API calls are caught.
353 if (poisoned_) {
354 poisoned_ = false;
355 return;
356 }
357
358 CloseInternal(false);
359}
360
Victor Costancfbfa602018-08-01 23:24:46361void Database::Preload() {
Etienne Bergeron6cb35c72020-02-12 18:09:48362 TRACE_EVENT0("sql", "Database::Preload");
Victor Costan3ffd9a372019-05-22 00:46:54363 if (base::FeatureList::IsEnabled(features::kSqlSkipPreload))
364 return;
365
[email protected]e5ffd0e42009-09-11 21:30:56366 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52367 DCHECK(poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56368 return;
369 }
370
Victor Costanb5a0a97002019-09-08 04:55:15371 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:20372 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
Victor Costanb5a0a97002019-09-08 04:55:15373
Victor Costanbcfeb5b2019-10-11 21:38:10374 // Maximum number of bytes that will be prefetched from the database.
375 //
376 // This limit is very aggressive. Here are the trade-offs involved.
377 // 1) Accessing bytes that weren't preread is very expensive on
378 // performance-critical databases, so the limit must exceed the expected
379 // sizes of feature databases.
380 // 2) On some platforms (Windows 7 and, currently, macOS), base::PreReadFile()
381 // falls back to a synchronous read, and blocks until the entire file is
382 // read into memory. So, there's a tangible cost to reading data that would
383 // get evicted before base::PreReadFile() completes. This cost needs to be
384 // balanced with the benefit reading the entire database at once, and
385 // avoiding seeks on spinning disks.
386 constexpr int kPreReadSize = 128 * 1024 * 1024; // 128 MB
387 base::PreReadFile(DbPath(), /*is_executable=*/false, kPreReadSize);
[email protected]e5ffd0e42009-09-11 21:30:56388}
389
Victor Costancfbfa602018-08-01 23:24:46390// SQLite keeps unused pages associated with a database in a cache. It asks
shess7dbd4dee2015-10-06 17:39:16391// the cache for pages by an id, and if the page is present and the database is
392// unchanged, it considers the content of the page valid and doesn't read it
393// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
394// structures created from the memory map data before consulting the cache. On
395// write SQLite creates a new in-memory page structure, copies the data from the
396// memory map, and later writes it, releasing the updated page back to the
397// cache.
398//
399// This means that in memory-mapped mode, the contents of the cached pages are
400// not re-used for reads, but they are re-used for writes if the re-written page
401// is still in the cache. The implementation of sqlite3_db_release_memory() as
402// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
Victor Costancfbfa602018-08-01 23:24:46403// database, so it should free these pages.
shess7dbd4dee2015-10-06 17:39:16404//
405// Unfortunately, the zero page is also freed. That page is never accessed
406// using memory-mapped I/O, and the cached copy can be re-used after verifying
407// the file change counter on disk. Also, fresh pages from cache receive some
408// pager-level initialization before they can be used. Since the information
409// involved will immediately be accessed in various ways, it is unclear if the
410// additional overhead is material, or just moving processor cache effects
411// around.
412//
413// TODO(shess): It would be better to release the pages immediately when they
414// are no longer needed. This would basically happen after SQLite commits a
415// transaction. I had implemented a pcache wrapper to do this, but it involved
416// layering violations, and it had to be setup before any other sqlite call,
417// which was brittle. Also, for large files it would actually make sense to
418// maintain the existing pcache behavior for blocks past the memory-mapped
419// segment. I think drh would accept a reasonable implementation of the overall
420// concept for upstreaming to SQLite core.
421//
422// TODO(shess): Another possibility would be to set the cache size small, which
423// would keep the zero page around, plus some pre-initialized pages, and SQLite
424// can manage things. The downside is that updates larger than the cache would
425// spill to the journal. That could be compensated by setting cache_spill to
426// false. The downside then is that it allows open-ended use of memory for
427// large transactions.
Victor Costancfbfa602018-08-01 23:24:46428void Database::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
Etienne Bergeron6cb35c72020-02-12 18:09:48429 TRACE_EVENT0("sql", "Database::ReleaseCacheMemoryIfNeeded");
shess644fc8a2016-02-26 18:15:58430 // The database could have been closed during a transaction as part of error
431 // recovery.
432 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:46433 DCHECK(poisoned_) << "Illegal use of Database without a db";
shess644fc8a2016-02-26 18:15:58434 return;
435 }
shess7dbd4dee2015-10-06 17:39:16436
437 // If memory-mapping is not enabled, the page cache helps performance.
438 if (!mmap_enabled_)
439 return;
440
441 // On caller request, force the change comparison to fail. Done before the
442 // transaction-nesting test so that the signal can carry to transaction
443 // commit.
444 if (implicit_change_performed)
445 --total_changes_at_last_release_;
446
447 // Cached pages may be re-used within the same transaction.
448 if (transaction_nesting())
449 return;
450
451 // If no changes have been made, skip flushing. This allows the first page of
452 // the database to remain in cache across multiple reads.
453 const int total_changes = sqlite3_total_changes(db_);
454 if (total_changes == total_changes_at_last_release_)
455 return;
456
457 total_changes_at_last_release_ = total_changes;
458 sqlite3_db_release_memory(db_);
459}
460
Victor Costancfbfa602018-08-01 23:24:46461base::FilePath Database::DbPath() const {
shessc8cd2a162015-10-22 20:30:46462 if (!is_open())
463 return base::FilePath();
464
465 const char* path = sqlite3_db_filename(db_, "main");
466 const base::StringPiece db_path(path);
467#if defined(OS_WIN)
Jan Wilken Dörrie9720dce2020-07-21 17:14:23468 return base::FilePath(base::UTF8ToWide(db_path));
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18469#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46470 return base::FilePath(db_path);
471#else
472 NOTREACHED();
473 return base::FilePath();
474#endif
475}
476
Victor Costancfbfa602018-08-01 23:24:46477std::string Database::CollectErrorInfo(int error, Statement* stmt) const {
Etienne Bergeron6cb35c72020-02-12 18:09:48478 TRACE_EVENT0("sql", "Database::CollectErrorInfo");
shessc8cd2a162015-10-22 20:30:46479 // Buffer for accumulating debugging info about the error. Place
480 // more-relevant information earlier, in case things overflow the
481 // fixed-size reporting buffer.
482 std::string debug_info;
483
484 // The error message from the failed operation.
Victor Costancfbfa602018-08-01 23:24:46485 base::StringAppendF(&debug_info, "db error: %d/%s\n", GetErrorCode(),
486 GetErrorMessage());
shessc8cd2a162015-10-22 20:30:46487
488 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
489 // reading code does not entirely convince me. Remove if they turn out to be
490 // the same.
491 if (error != GetErrorCode())
492 base::StringAppendF(&debug_info, "reported error: %d\n", error);
493
Victor Costancfbfa602018-08-01 23:24:46494// System error information. Interpretation of Windows errors is different
495// from posix.
shessc8cd2a162015-10-22 20:30:46496#if defined(OS_WIN)
497 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18498#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46499 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
500#else
501 NOTREACHED(); // Add appropriate log info.
502#endif
503
504 if (stmt) {
505 base::StringAppendF(&debug_info, "statement: %s\n",
506 stmt->GetSQLStatement());
507 } else {
508 base::StringAppendF(&debug_info, "statement: NULL\n");
509 }
510
511 // SQLITE_ERROR often indicates some sort of mismatch between the statement
512 // and the schema, possibly due to a failed schema migration.
513 if (error == SQLITE_ERROR) {
Victor Costanf37f7ae2019-04-11 19:26:12514 static const char kVersionSql[] =
515 "SELECT value FROM meta WHERE key='version'";
516 sqlite3_stmt* sqlite_statement;
517 // When the number of bytes passed to sqlite3_prepare_v3() includes the null
518 // terminator, SQLite avoids a buffer copy.
519 int rc = sqlite3_prepare_v3(db_, kVersionSql, sizeof(kVersionSql),
520 SQLITE_PREPARE_NO_VTAB, &sqlite_statement,
521 /* pzTail= */ nullptr);
shessc8cd2a162015-10-22 20:30:46522 if (rc == SQLITE_OK) {
Victor Costanf37f7ae2019-04-11 19:26:12523 rc = sqlite3_step(sqlite_statement);
shessc8cd2a162015-10-22 20:30:46524 if (rc == SQLITE_ROW) {
525 base::StringAppendF(&debug_info, "version: %d\n",
Victor Costanf37f7ae2019-04-11 19:26:12526 sqlite3_column_int(sqlite_statement, 0));
shessc8cd2a162015-10-22 20:30:46527 } else if (rc == SQLITE_DONE) {
528 debug_info += "version: none\n";
529 } else {
530 base::StringAppendF(&debug_info, "version: error %d\n", rc);
531 }
Victor Costanf37f7ae2019-04-11 19:26:12532 sqlite3_finalize(sqlite_statement);
shessc8cd2a162015-10-22 20:30:46533 } else {
534 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
535 }
536
537 debug_info += "schema:\n";
538
539 // sqlite_master has columns:
540 // type - "index" or "table".
541 // name - name of created element.
542 // tbl_name - name of element, or target table in case of index.
543 // rootpage - root page of the element in database file.
544 // sql - SQL to create the element.
545 // In general, the |sql| column is sufficient to derive the other columns.
546 // |rootpage| is not interesting for debugging, without the contents of the
547 // database. The COALESCE is because certain automatic elements will have a
548 // |name| but no |sql|,
Victor Costanf37f7ae2019-04-11 19:26:12549 static const char kSchemaSql[] =
550 "SELECT COALESCE(sql,name) FROM sqlite_master";
551 rc = sqlite3_prepare_v3(db_, kSchemaSql, sizeof(kSchemaSql),
552 SQLITE_PREPARE_NO_VTAB, &sqlite_statement,
553 /* pzTail= */ nullptr);
shessc8cd2a162015-10-22 20:30:46554 if (rc == SQLITE_OK) {
Victor Costanf37f7ae2019-04-11 19:26:12555 while ((rc = sqlite3_step(sqlite_statement)) == SQLITE_ROW) {
556 base::StringAppendF(&debug_info, "%s\n",
557 sqlite3_column_text(sqlite_statement, 0));
shessc8cd2a162015-10-22 20:30:46558 }
559 if (rc != SQLITE_DONE)
560 base::StringAppendF(&debug_info, "error %d\n", rc);
Victor Costanf37f7ae2019-04-11 19:26:12561 sqlite3_finalize(sqlite_statement);
shessc8cd2a162015-10-22 20:30:46562 } else {
563 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
564 }
565 }
566
567 return debug_info;
568}
569
570// TODO(shess): Since this is only called in an error situation, it might be
571// prudent to rewrite in terms of SQLite API calls, and mark the function const.
Victor Costancfbfa602018-08-01 23:24:46572std::string Database::CollectCorruptionInfo() {
Etienne Bergeron6cb35c72020-02-12 18:09:48573 TRACE_EVENT0("sql", "Database::CollectCorruptionInfo");
shessc8cd2a162015-10-22 20:30:46574 // If the file cannot be accessed it is unlikely that an integrity check will
575 // turn up actionable information.
576 const base::FilePath db_path = DbPath();
avi0b519202015-12-21 07:25:19577 int64_t db_size = -1;
shessc8cd2a162015-10-22 20:30:46578 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
579 return std::string();
580
581 // Buffer for accumulating debugging info about the error. Place
582 // more-relevant information earlier, in case things overflow the
583 // fixed-size reporting buffer.
584 std::string debug_info;
585 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
586 db_size);
587
588 // Only check files up to 8M to keep things from blocking too long.
avi0b519202015-12-21 07:25:19589 const int64_t kMaxIntegrityCheckSize = 8192 * 1024;
shessc8cd2a162015-10-22 20:30:46590 if (db_size > kMaxIntegrityCheckSize) {
591 debug_info += "integrity_check skipped due to size\n";
592 } else {
593 std::vector<std::string> messages;
594
595 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
596 // into a string. Probably should be refactored.
597 const base::TimeTicks before = base::TimeTicks::Now();
598 FullIntegrityCheck(&messages);
599 base::StringAppendF(
Victor Costancfbfa602018-08-01 23:24:46600 &debug_info, "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
601 (base::TimeTicks::Now() - before).InMilliseconds(), messages.size());
shessc8cd2a162015-10-22 20:30:46602
603 // SQLite returns up to 100 messages by default, trim deeper to
604 // keep close to the 2000-character size limit for dumping.
605 const size_t kMaxMessages = 20;
606 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
607 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
608 }
609 }
610
611 return debug_info;
612}
613
Victor Costancfbfa602018-08-01 23:24:46614bool Database::GetMmapAltStatus(int64_t* status) {
Etienne Bergeron6cb35c72020-02-12 18:09:48615 TRACE_EVENT0("sql", "Database::GetMmapAltStatus");
616
shessa62504d2016-11-07 19:26:12617 // The [meta] version uses a missing table as a signal for a fresh database.
618 // That will not work for the view, which would not exist in either a new or
619 // an existing database. A new database _should_ be only one page long, so
620 // just don't bother optimizing this case (start at offset 0).
621 // TODO(shess): Could the [meta] case also get simpler, then?
622 if (!DoesViewExist("MmapStatus")) {
623 *status = 0;
624 return true;
625 }
626
627 const char* kMmapStatusSql = "SELECT * FROM MmapStatus";
628 Statement s(GetUniqueStatement(kMmapStatusSql));
629 if (s.Step())
630 *status = s.ColumnInt64(0);
631 return s.Succeeded();
632}
633
Victor Costancfbfa602018-08-01 23:24:46634bool Database::SetMmapAltStatus(int64_t status) {
shessa62504d2016-11-07 19:26:12635 if (!BeginTransaction())
636 return false;
637
638 // View may not exist on first run.
639 if (!Execute("DROP VIEW IF EXISTS MmapStatus")) {
640 RollbackTransaction();
641 return false;
642 }
643
644 // Views live in the schema, so they cannot be parameterized. For an integer
645 // value, this construct should be safe from SQL injection, if the value
646 // becomes more complicated use "SELECT quote(?)" to generate a safe quoted
647 // value.
Victor Costancfbfa602018-08-01 23:24:46648 const std::string create_view_sql = base::StringPrintf(
649 "CREATE VIEW MmapStatus (value) AS SELECT %" PRId64, status);
650 if (!Execute(create_view_sql.c_str())) {
shessa62504d2016-11-07 19:26:12651 RollbackTransaction();
652 return false;
653 }
654
655 return CommitTransaction();
656}
657
Victor Costancfbfa602018-08-01 23:24:46658size_t Database::GetAppropriateMmapSize() {
Etienne Bergeron6cb35c72020-02-12 18:09:48659 TRACE_EVENT0("sql", "Database::GetAppropriateMmapSize");
660
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54661 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:20662 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
shessd90aeea82015-11-13 02:24:31663
shess9bf2c672015-12-18 01:18:08664 // How much to map if no errors are found. 50MB encompasses the 99th
665 // percentile of Chrome databases in the wild, so this should be good.
666 const size_t kMmapEverything = 256 * 1024 * 1024;
667
shessa62504d2016-11-07 19:26:12668 // Progress information is tracked in the [meta] table for databases which use
669 // sql::MetaTable, otherwise it is tracked in a special view.
670 // TODO(shess): Move all cases to the view implementation.
shess9bf2c672015-12-18 01:18:08671 int64_t mmap_ofs = 0;
shessa62504d2016-11-07 19:26:12672 if (mmap_alt_status_) {
673 if (!GetMmapAltStatus(&mmap_ofs)) {
674 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_READ);
675 return 0;
676 }
677 } else {
678 // If [meta] doesn't exist, yet, it's a new database, assume the best.
679 // sql::MetaTable::Init() will preload kMmapSuccess.
680 if (!MetaTable::DoesTableExist(this)) {
681 RecordOneEvent(EVENT_MMAP_META_MISSING);
682 return kMmapEverything;
683 }
684
685 if (!MetaTable::GetMmapStatus(this, &mmap_ofs)) {
686 RecordOneEvent(EVENT_MMAP_META_FAILURE_READ);
687 return 0;
688 }
shessd90aeea82015-11-13 02:24:31689 }
690
691 // Database read failed in the past, don't memory map.
shess9bf2c672015-12-18 01:18:08692 if (mmap_ofs == MetaTable::kMmapFailure) {
shessd90aeea82015-11-13 02:24:31693 RecordOneEvent(EVENT_MMAP_FAILED);
694 return 0;
Victor Costan5e785e32019-02-26 20:39:31695 }
696
697 if (mmap_ofs != MetaTable::kMmapSuccess) {
shessd90aeea82015-11-13 02:24:31698 // Continue reading from previous offset.
699 DCHECK_GE(mmap_ofs, 0);
700
701 // TODO(shess): Could this reading code be shared with Preload()? It would
702 // require locking twice (this code wouldn't be able to access |db_size| so
703 // the helper would have to return amount read).
704
705 // Read more of the database looking for errors. The VFS interface is used
706 // to assure that the reads are valid for SQLite. |g_reads_allowed| is used
707 // to limit checking to 20MB per run of Chromium.
Victor Costanbd623112018-07-18 04:17:27708 sqlite3_file* file = nullptr;
shessd90aeea82015-11-13 02:24:31709 sqlite3_int64 db_size = 0;
710 if (SQLITE_OK != GetSqlite3FileAndSize(db_, &file, &db_size)) {
711 RecordOneEvent(EVENT_MMAP_VFS_FAILURE);
712 return 0;
713 }
714
715 // Read the data left, or |g_reads_allowed|, whichever is smaller.
716 // |g_reads_allowed| limits the total amount of I/O to spend verifying data
717 // in a single Chromium run.
718 sqlite3_int64 amount = db_size - mmap_ofs;
719 if (amount < 0)
720 amount = 0;
721 if (amount > 0) {
Victor Costan3653df62018-02-08 21:38:16722 static base::NoDestructor<base::Lock> lock;
723 base::AutoLock auto_lock(*lock);
shessd90aeea82015-11-13 02:24:31724 static sqlite3_int64 g_reads_allowed = 20 * 1024 * 1024;
725 if (g_reads_allowed < amount)
726 amount = g_reads_allowed;
727 g_reads_allowed -= amount;
728 }
729
730 // |amount| can be <= 0 if |g_reads_allowed| ran out of quota, or if the
731 // database was truncated after a previous pass.
732 if (amount <= 0 && mmap_ofs < db_size) {
733 DCHECK_EQ(0, amount);
shessd90aeea82015-11-13 02:24:31734 } else {
735 static const int kPageSize = 4096;
736 char buf[kPageSize];
737 while (amount > 0) {
738 int rc = file->pMethods->xRead(file, buf, sizeof(buf), mmap_ofs);
739 if (rc == SQLITE_OK) {
740 mmap_ofs += sizeof(buf);
741 amount -= sizeof(buf);
742 } else if (rc == SQLITE_IOERR_SHORT_READ) {
743 // Reached EOF for a database with page size < |kPageSize|.
744 mmap_ofs = db_size;
745 break;
746 } else {
747 // TODO(shess): Consider calling OnSqliteError().
shess9bf2c672015-12-18 01:18:08748 mmap_ofs = MetaTable::kMmapFailure;
shessd90aeea82015-11-13 02:24:31749 break;
750 }
751 }
752
753 // Log these events after update to distinguish meta update failure.
shessd90aeea82015-11-13 02:24:31754 if (mmap_ofs >= db_size) {
shess9bf2c672015-12-18 01:18:08755 mmap_ofs = MetaTable::kMmapSuccess;
shessd90aeea82015-11-13 02:24:31756 } else {
Victor Costan5e785e32019-02-26 20:39:31757 DCHECK(mmap_ofs > 0 || mmap_ofs == MetaTable::kMmapFailure);
shessd90aeea82015-11-13 02:24:31758 }
759
shessa62504d2016-11-07 19:26:12760 if (mmap_alt_status_) {
761 if (!SetMmapAltStatus(mmap_ofs)) {
762 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_UPDATE);
763 return 0;
764 }
765 } else {
766 if (!MetaTable::SetMmapStatus(this, mmap_ofs)) {
767 RecordOneEvent(EVENT_MMAP_META_FAILURE_UPDATE);
768 return 0;
769 }
shessd90aeea82015-11-13 02:24:31770 }
771
Victor Costan5e785e32019-02-26 20:39:31772 if (mmap_ofs == MetaTable::kMmapFailure)
773 RecordOneEvent(EVENT_MMAP_FAILED_NEW);
shessd90aeea82015-11-13 02:24:31774 }
775 }
776
shess9bf2c672015-12-18 01:18:08777 if (mmap_ofs == MetaTable::kMmapFailure)
shessd90aeea82015-11-13 02:24:31778 return 0;
shess9bf2c672015-12-18 01:18:08779 if (mmap_ofs == MetaTable::kMmapSuccess)
780 return kMmapEverything;
shessd90aeea82015-11-13 02:24:31781 return mmap_ofs;
782}
783
Victor Costan52bef812018-12-05 07:41:49784void Database::TrimMemory() {
Etienne Bergeron6cb35c72020-02-12 18:09:48785 TRACE_EVENT0("sql", "Database::TrimMemory");
786
[email protected]be7995f12013-07-18 18:49:14787 if (!db_)
788 return;
789
Victor Costan52bef812018-12-05 07:41:49790 sqlite3_db_release_memory(db_);
[email protected]be7995f12013-07-18 18:49:14791
Victor Costan52bef812018-12-05 07:41:49792 // It is tempting to use sqlite3_release_memory() here as well. However, the
793 // API is documented to be a no-op unless SQLite is built with
794 // SQLITE_ENABLE_MEMORY_MANAGEMENT. We do not use this option, because it is
795 // incompatible with per-database page cache pools. Behind the scenes,
796 // SQLITE_ENABLE_MEMORY_MANAGEMENT causes SQLite to use a global page cache
797 // pool, and sqlite3_release_memory() releases unused pages from this global
798 // pool.
[email protected]be7995f12013-07-18 18:49:14799}
800
[email protected]8e0c01282012-04-06 19:36:49801// Create an in-memory database with the existing database's page
802// size, then backup that database over the existing database.
Victor Costancfbfa602018-08-01 23:24:46803bool Database::Raze() {
Etienne Bergeron6cb35c72020-02-12 18:09:48804 TRACE_EVENT0("sql", "Database::Raze");
805
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54806 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:20807 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
[email protected]35f7e5392012-07-27 19:54:50808
[email protected]8e0c01282012-04-06 19:36:49809 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52810 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49811 return false;
812 }
813
814 if (transaction_nesting_ > 0) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52815 DLOG(DCHECK) << "Cannot raze within a transaction";
[email protected]8e0c01282012-04-06 19:36:49816 return false;
817 }
818
Victor Costancfbfa602018-08-01 23:24:46819 sql::Database null_db;
[email protected]8e0c01282012-04-06 19:36:49820 if (!null_db.OpenInMemory()) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52821 DLOG(DCHECK) << "Unable to open in-memory database.";
[email protected]8e0c01282012-04-06 19:36:49822 return false;
823 }
824
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57825 const std::string page_size_sql =
826 base::StringPrintf("PRAGMA page_size=%d", page_size_);
827 if (!null_db.Execute(page_size_sql.c_str()))
Victor Costan7f6abbbe2018-07-29 02:57:27828 return false;
[email protected]69c58452012-08-06 19:22:42829
[email protected]6d42f152012-11-10 00:38:24830#if defined(OS_ANDROID)
831 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
832 // in-memory databases do not respect this define.
833 // TODO(shess): Figure out a way to set this without using platform
834 // specific code. AFAICT from sqlite3.c, the only way to do it
835 // would be to create an actual filesystem database, which is
836 // unfortunate.
837 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
838 return false;
839#endif
[email protected]8e0c01282012-04-06 19:36:49840
841 // The page size doesn't take effect until a database has pages, and
842 // at this point the null database has none. Changing the schema
843 // version will create the first page. This will not affect the
844 // schema version in the resulting database, as SQLite's backup
845 // implementation propagates the schema version from the original
Victor Costancfbfa602018-08-01 23:24:46846 // database to the new version of the database, incremented by one
[email protected]8e0c01282012-04-06 19:36:49847 // so that other readers see the schema change and act accordingly.
848 if (!null_db.Execute("PRAGMA schema_version = 1"))
849 return false;
850
[email protected]6d42f152012-11-10 00:38:24851 // SQLite tracks the expected number of database pages in the first
852 // page, and if it does not match the total retrieved from a
853 // filesystem call, treats the database as corrupt. This situation
854 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
855 // used to hint to SQLite to soldier on in that case, specifically
856 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
857 // sqlite3.c lockBtree().]
858 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
859 // page_size" can be used to query such a database.
860 ScopedWritableSchema writable_schema(db_);
861
shess92a6fb22017-04-23 04:33:30862#if defined(OS_WIN)
863 // On Windows, truncate silently fails when applied to memory-mapped files.
864 // Disable memory-mapping so that the truncate succeeds. Note that other
Victor Costancfbfa602018-08-01 23:24:46865 // Database connections may have memory-mapped the file, so this may not
866 // entirely prevent the problem.
shess92a6fb22017-04-23 04:33:30867 // [Source: <https://sqlite.org/mmap.html> plus experiments.]
868 ignore_result(Execute("PRAGMA mmap_size = 0"));
869#endif
870
[email protected]7bae5742013-07-10 20:46:16871 const char* kMain = "main";
872 int rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:18873 base::UmaHistogramSparse("Sqlite.RazeDatabase", rc);
[email protected]8e0c01282012-04-06 19:36:49874
875 // The destination database was locked.
876 if (rc == SQLITE_BUSY) {
877 return false;
878 }
879
[email protected]7bae5742013-07-10 20:46:16880 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
881 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
882 // isn't even big enough for one page. Either way, reach in and
883 // truncate it before trying again.
884 // TODO(shess): Maybe it would be worthwhile to just truncate from
885 // the get-go?
886 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
Victor Costanbd623112018-07-18 04:17:27887 sqlite3_file* file = nullptr;
[email protected]8ada10f2013-12-21 00:42:34888 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:16889 if (rc != SQLITE_OK) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52890 DLOG(DCHECK) << "Failure getting file handle.";
[email protected]7bae5742013-07-10 20:46:16891 return false;
[email protected]7bae5742013-07-10 20:46:16892 }
893
894 rc = file->pMethods->xTruncate(file, 0);
895 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:18896 base::UmaHistogramSparse("Sqlite.RazeDatabaseTruncate", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52897 DLOG(DCHECK) << "Failed to truncate file.";
[email protected]7bae5742013-07-10 20:46:16898 return false;
899 }
900
901 rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:18902 base::UmaHistogramSparse("Sqlite.RazeDatabase2", rc);
[email protected]7bae5742013-07-10 20:46:16903
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52904 DCHECK_EQ(rc, SQLITE_DONE) << "Failed retrying Raze().";
[email protected]7bae5742013-07-10 20:46:16905 }
906
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57907 // Page size of |db_| and |null_db| differ.
908 if (rc == SQLITE_READONLY) {
909 // Enter TRUNCATE mode to change page size.
910 // TODO([email protected]): Need a guarantee here that there is no other
911 // database connection open.
912 ignore_result(Execute("PRAGMA journal_mode=TRUNCATE;"));
913 if (!Execute(page_size_sql.c_str())) {
914 return false;
915 }
916 // Page size isn't changed until the database is vacuumed.
917 ignore_result(Execute("VACUUM"));
918 // Re-enter WAL mode.
919 if (UseWALMode()) {
920 ignore_result(Execute("PRAGMA journal_mode=WAL;"));
921 }
922
923 rc = BackupDatabase(null_db.db_, db_, kMain);
924 base::UmaHistogramSparse("Sqlite.RazeDatabase2", rc);
925
926 DCHECK_EQ(rc, SQLITE_DONE) << "Failed retrying Raze().";
927 }
928
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52929 // TODO(shess): Figure out which other cases can happen.
930 DCHECK_EQ(rc, SQLITE_DONE) << "Unable to copy entire null database.";
931
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57932 // Checkpoint to propagate transactions to the database file and empty the WAL
933 // file.
934 // The database can still contain old data if the Checkpoint fails so fail the
935 // Raze.
936 if (!CheckpointDatabase()) {
937 return false;
938 }
939
[email protected]8e0c01282012-04-06 19:36:49940 // The entire database should have been backed up.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52941 return rc == SQLITE_DONE;
[email protected]8e0c01282012-04-06 19:36:49942}
943
Victor Costancfbfa602018-08-01 23:24:46944bool Database::RazeAndClose() {
Etienne Bergeron6cb35c72020-02-12 18:09:48945 TRACE_EVENT0("sql", "Database::RazeAndClose");
946
[email protected]41a97c812013-02-07 02:35:38947 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52948 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]41a97c812013-02-07 02:35:38949 return false;
950 }
951
952 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:30953 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:38954
955 bool result = Raze();
956
957 CloseInternal(true);
958
959 // Mark the database so that future API calls fail appropriately,
960 // but don't DCHECK (because after calling this function they are
961 // expected to fail).
962 poisoned_ = true;
963
964 return result;
965}
966
Victor Costancfbfa602018-08-01 23:24:46967void Database::Poison() {
Etienne Bergeron6cb35c72020-02-12 18:09:48968 TRACE_EVENT0("sql", "Database::Poison");
969
[email protected]8d409412013-07-19 18:25:30970 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52971 DCHECK(poisoned_) << "Cannot poison null db";
[email protected]8d409412013-07-19 18:25:30972 return;
973 }
974
975 RollbackAllTransactions();
976 CloseInternal(true);
977
978 // Mark the database so that future API calls fail appropriately,
979 // but don't DCHECK (because after calling this function they are
980 // expected to fail).
981 poisoned_ = true;
982}
983
[email protected]8d2e39e2013-06-24 05:55:08984// TODO(shess): To the extent possible, figure out the optimal
Victor Costancfbfa602018-08-01 23:24:46985// ordering for these deletes which will prevent other Database connections
[email protected]8d2e39e2013-06-24 05:55:08986// from seeing odd behavior. For instance, it may be necessary to
987// manually lock the main database file in a SQLite-compatible fashion
988// (to prevent other processes from opening it), then delete the
989// journal files, then delete the main database file. Another option
990// might be to lock the main database file and poison the header with
991// junk to prevent other processes from opening it successfully (like
992// Gears "SQLite poison 3" trick).
993//
994// static
Victor Costancfbfa602018-08-01 23:24:46995bool Database::Delete(const base::FilePath& path) {
Etienne Bergeron6cb35c72020-02-12 18:09:48996 TRACE_EVENT1("sql", "Database::Delete", "path", path.MaybeAsASCII());
997
Etienne Bergeron436d42212019-02-26 17:15:12998 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
999 base::BlockingType::MAY_BLOCK);
[email protected]8d2e39e2013-06-24 05:55:081000
Victor Costancfbfa602018-08-01 23:24:461001 base::FilePath journal_path = Database::JournalPath(path);
1002 base::FilePath wal_path = Database::WriteAheadLogPath(path);
[email protected]8d2e39e2013-06-24 05:55:081003
erg102ceb412015-06-20 01:38:131004 std::string journal_str = AsUTF8ForSQL(journal_path);
1005 std::string wal_str = AsUTF8ForSQL(wal_path);
1006 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081007
Victor Costan3653df62018-02-08 21:38:161008 EnsureSqliteInitialized();
shess702467622015-09-16 19:04:551009
Victor Costanbd623112018-07-18 04:17:271010 sqlite3_vfs* vfs = sqlite3_vfs_find(nullptr);
erg102ceb412015-06-20 01:38:131011 CHECK(vfs);
1012 CHECK(vfs->xDelete);
1013 CHECK(vfs->xAccess);
1014
Ken Rockot01687422020-08-17 18:00:591015 // We only work with the VFS implementations listed below. If you're trying to
erg102ceb412015-06-20 01:38:131016 // use this code with any other VFS, you're not in a good place.
1017 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1018 strncmp(vfs->zName, "win32", 5) == 0 ||
Ken Rockot01687422020-08-17 18:00:591019 strcmp(vfs->zName, "storage_service") == 0);
erg102ceb412015-06-20 01:38:131020
1021 vfs->xDelete(vfs, journal_str.c_str(), 0);
1022 vfs->xDelete(vfs, wal_str.c_str(), 0);
1023 vfs->xDelete(vfs, path_str.c_str(), 0);
1024
1025 int journal_exists = 0;
Victor Costance678e72018-07-24 10:25:001026 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS, &journal_exists);
erg102ceb412015-06-20 01:38:131027
1028 int wal_exists = 0;
Victor Costance678e72018-07-24 10:25:001029 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS, &wal_exists);
erg102ceb412015-06-20 01:38:131030
1031 int path_exists = 0;
Victor Costance678e72018-07-24 10:25:001032 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS, &path_exists);
erg102ceb412015-06-20 01:38:131033
1034 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081035}
1036
Victor Costancfbfa602018-08-01 23:24:461037bool Database::BeginTransaction() {
Etienne Bergeron6cb35c72020-02-12 18:09:481038 TRACE_EVENT0("sql", "Database::BeginTransaction");
1039
[email protected]e5ffd0e42009-09-11 21:30:561040 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331041 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561042
1043 // When we're going to rollback, fail on this begin and don't actually
1044 // mark us as entering the nested transaction.
1045 return false;
1046 }
1047
1048 bool success = true;
1049 if (!transaction_nesting_) {
1050 needs_rollback_ = false;
1051
1052 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
[email protected]eff1fa522011-12-12 23:50:591053 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561054 return false;
1055 }
1056 transaction_nesting_++;
1057 return success;
1058}
1059
Victor Costancfbfa602018-08-01 23:24:461060void Database::RollbackTransaction() {
Etienne Bergeron6cb35c72020-02-12 18:09:481061 TRACE_EVENT0("sql", "Database::RollbackTransaction");
1062
[email protected]e5ffd0e42009-09-11 21:30:561063 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521064 DCHECK(poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561065 return;
1066 }
1067
1068 transaction_nesting_--;
1069
1070 if (transaction_nesting_ > 0) {
1071 // Mark the outermost transaction as needing rollback.
1072 needs_rollback_ = true;
1073 return;
1074 }
1075
1076 DoRollback();
1077}
1078
Victor Costancfbfa602018-08-01 23:24:461079bool Database::CommitTransaction() {
Etienne Bergeron6cb35c72020-02-12 18:09:481080 TRACE_EVENT0("sql", "Database::CommitTransaction");
1081
[email protected]e5ffd0e42009-09-11 21:30:561082 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521083 DCHECK(poisoned_) << "Committing a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561084 return false;
1085 }
1086 transaction_nesting_--;
1087
1088 if (transaction_nesting_ > 0) {
1089 // Mark any nested transactions as failing after we've already got one.
1090 return !needs_rollback_;
1091 }
1092
1093 if (needs_rollback_) {
1094 DoRollback();
1095 return false;
1096 }
1097
1098 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321099
Victor Costan5e785e32019-02-26 20:39:311100 bool succeeded = commit.Run();
shess58b8df82015-06-03 00:19:321101
shess7dbd4dee2015-10-06 17:39:161102 // Release dirty cache pages after the transaction closes.
1103 ReleaseCacheMemoryIfNeeded(false);
1104
Victor Costan5e785e32019-02-26 20:39:311105 return succeeded;
[email protected]e5ffd0e42009-09-11 21:30:561106}
1107
Victor Costancfbfa602018-08-01 23:24:461108void Database::RollbackAllTransactions() {
Etienne Bergeron6cb35c72020-02-12 18:09:481109 TRACE_EVENT0("sql", "Database::RollbackAllTransactions");
1110
[email protected]8d409412013-07-19 18:25:301111 if (transaction_nesting_ > 0) {
1112 transaction_nesting_ = 0;
1113 DoRollback();
1114 }
1115}
1116
Victor Costancfbfa602018-08-01 23:24:461117bool Database::AttachDatabase(const base::FilePath& other_db_path,
1118 const char* attachment_point,
1119 InternalApiToken) {
Etienne Bergeron6cb35c72020-02-12 18:09:481120 TRACE_EVENT0("sql", "Database::AttachDatabase");
1121
[email protected]8d409412013-07-19 18:25:301122 DCHECK(ValidAttachmentPoint(attachment_point));
1123
1124 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1125#if OS_WIN
Jan Wilken Dörrie9720dce2020-07-21 17:14:231126 s.BindString16(0, base::AsStringPiece16(other_db_path.value()));
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181127#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
Fabrice de Gans-Riberibd1301f2018-05-18 21:00:091128 s.BindString(0, other_db_path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181129#else
1130#error Unsupported platform
[email protected]8d409412013-07-19 18:25:301131#endif
1132 s.BindString(1, attachment_point);
1133 return s.Run();
1134}
1135
Victor Costancfbfa602018-08-01 23:24:461136bool Database::DetachDatabase(const char* attachment_point, InternalApiToken) {
Etienne Bergeron6cb35c72020-02-12 18:09:481137 TRACE_EVENT0("sql", "Database::DetachDatabase");
1138
[email protected]8d409412013-07-19 18:25:301139 DCHECK(ValidAttachmentPoint(attachment_point));
1140
1141 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1142 s.BindString(0, attachment_point);
1143 return s.Run();
1144}
1145
shess58b8df82015-06-03 00:19:321146// TODO(shess): Consider changing this to execute exactly one statement. If a
1147// caller wishes to execute multiple statements, that should be explicit, and
1148// perhaps tucked into an explicit transaction with rollback in case of error.
Victor Costancfbfa602018-08-01 23:24:461149int Database::ExecuteAndReturnErrorCode(const char* sql) {
Etienne Bergeron6cb35c72020-02-12 18:09:481150 TRACE_EVENT0("sql", "Database::ExecuteAndReturnErrorCode");
1151
Victor Costan5e785e32019-02-26 20:39:311152 DCHECK(sql);
Etienne Pierre-Doraya71d7af2019-02-07 02:07:541153
[email protected]41a97c812013-02-07 02:35:381154 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461155 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381156 return SQLITE_ERROR;
1157 }
shess58b8df82015-06-03 00:19:321158
Victor Costan5e785e32019-02-26 20:39:311159 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:201160 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
Victor Costan5e785e32019-02-26 20:39:311161
shess58b8df82015-06-03 00:19:321162 int rc = SQLITE_OK;
1163 while ((rc == SQLITE_OK) && *sql) {
Victor Costanf37f7ae2019-04-11 19:26:121164 sqlite3_stmt* sqlite_statement;
Victor Costancfbfa602018-08-01 23:24:461165 const char* leftover_sql;
Victor Costanf37f7ae2019-04-11 19:26:121166 rc = sqlite3_prepare_v3(db_, sql, /* nByte= */ -1, /* prepFlags= */ 0,
1167 &sqlite_statement, &leftover_sql);
shess58b8df82015-06-03 00:19:321168 // Stop if an error is encountered.
1169 if (rc != SQLITE_OK)
1170 break;
1171
Victor Costanf37f7ae2019-04-11 19:26:121172 sql = leftover_sql;
1173
shess58b8df82015-06-03 00:19:321174 // This happens if |sql| originally only contained comments or whitespace.
1175 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1176 // extraneous comments and whitespace in the SQL statements increases
1177 // runtime cost and can easily be shifted out to the C++ layer.
Victor Costanf37f7ae2019-04-11 19:26:121178 if (!sqlite_statement)
shess58b8df82015-06-03 00:19:321179 continue;
1180
Victor Costanf37f7ae2019-04-11 19:26:121181 while ((rc = sqlite3_step(sqlite_statement)) == SQLITE_ROW) {
shess58b8df82015-06-03 00:19:321182 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
Victor Costan5e785e32019-02-26 20:39:311183 // is the only legitimate case for this. Previously recorded histograms
1184 // show significant use of this code path.
shess58b8df82015-06-03 00:19:321185 }
1186
1187 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1188 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
Victor Costanf37f7ae2019-04-11 19:26:121189 rc = sqlite3_finalize(sqlite_statement);
shess58b8df82015-06-03 00:19:321190
1191 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1192 // trailing whitespace.
1193 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021194 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321195 sql++;
1196 }
shess58b8df82015-06-03 00:19:321197 }
shess7dbd4dee2015-10-06 17:39:161198
1199 // Most calls to Execute() modify the database. The main exceptions would be
1200 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1201 // but sometimes don't.
1202 ReleaseCacheMemoryIfNeeded(true);
1203
shess58b8df82015-06-03 00:19:321204 return rc;
[email protected]eff1fa522011-12-12 23:50:591205}
1206
Victor Costancfbfa602018-08-01 23:24:461207bool Database::Execute(const char* sql) {
Etienne Bergeron6cb35c72020-02-12 18:09:481208 TRACE_EVENT1("sql", "Database::Execute", "query", TRACE_STR_COPY(sql));
1209
[email protected]41a97c812013-02-07 02:35:381210 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461211 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381212 return false;
1213 }
1214
[email protected]eff1fa522011-12-12 23:50:591215 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001216 if (error != SQLITE_OK)
Victor Costanbd623112018-07-18 04:17:271217 error = OnSqliteError(error, nullptr, sql);
[email protected]473ad792012-11-10 00:55:001218
[email protected]28fe0ff2012-02-25 00:40:331219 // This needs to be a FATAL log because the error case of arriving here is
1220 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101221 // a change alters the schema but not all queries adjust. This can happen
1222 // in production if the schema is corrupted.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521223 DCHECK_NE(error, SQLITE_ERROR)
1224 << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591225 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561226}
1227
Victor Costancfbfa602018-08-01 23:24:461228bool Database::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
Etienne Bergeron6cb35c72020-02-12 18:09:481229 TRACE_EVENT0("sql", "Database::ExecuteWithTimeout");
1230
[email protected]41a97c812013-02-07 02:35:381231 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461232 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]5b96f3772010-09-28 16:30:571233 return false;
[email protected]41a97c812013-02-07 02:35:381234 }
[email protected]5b96f3772010-09-28 16:30:571235
1236 ScopedBusyTimeout busy_timeout(db_);
1237 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591238 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571239}
1240
Victor Costancfbfa602018-08-01 23:24:461241scoped_refptr<Database::StatementRef> Database::GetCachedStatement(
Victor Costan12daa3ac92018-07-19 01:05:581242 StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:561243 const char* sql) {
Victor Costanc7e7f2e2018-07-18 20:07:551244 auto it = statement_cache_.find(id);
1245 if (it != statement_cache_.end()) {
Victor Costan613b4302018-11-20 05:32:431246 // Statement is in the cache. It should still be valid. We're the only
1247 // entity invalidating cached statements, and we remove them from the cache
Victor Costan87cf8c72018-07-19 19:36:041248 // when we do that.
Victor Costanc7e7f2e2018-07-18 20:07:551249 DCHECK(it->second->is_valid());
Victor Costan613b4302018-11-20 05:32:431250 DCHECK_EQ(std::string(sqlite3_sql(it->second->stmt())), std::string(sql))
Victor Costan87cf8c72018-07-19 19:36:041251 << "GetCachedStatement used with same ID but different SQL";
1252
1253 // Reset the statement so it can be reused.
Victor Costanc7e7f2e2018-07-18 20:07:551254 sqlite3_reset(it->second->stmt());
1255 return it->second;
[email protected]e5ffd0e42009-09-11 21:30:561256 }
1257
1258 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
Victor Costan613b4302018-11-20 05:32:431259 if (statement->is_valid()) {
[email protected]e5ffd0e42009-09-11 21:30:561260 statement_cache_[id] = statement; // Only cache valid statements.
Victor Costan613b4302018-11-20 05:32:431261 DCHECK_EQ(std::string(sqlite3_sql(statement->stmt())), std::string(sql))
1262 << "Input SQL does not match SQLite's normalized version";
1263 }
[email protected]e5ffd0e42009-09-11 21:30:561264 return statement;
1265}
1266
Victor Costancfbfa602018-08-01 23:24:461267scoped_refptr<Database::StatementRef> Database::GetUniqueStatement(
[email protected]e5ffd0e42009-09-11 21:30:561268 const char* sql) {
shess9e77283d2016-06-13 23:53:201269 return GetStatementImpl(this, sql);
1270}
1271
Victor Costancfbfa602018-08-01 23:24:461272scoped_refptr<Database::StatementRef> Database::GetStatementImpl(
1273 sql::Database* tracking_db,
1274 const char* sql) const {
shess9e77283d2016-06-13 23:53:201275 DCHECK(sql);
Victor Costan87cf8c72018-07-19 19:36:041276 DCHECK(!tracking_db || tracking_db == this);
[email protected]35f7e5392012-07-27 19:54:501277
[email protected]41a97c812013-02-07 02:35:381278 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561279 if (!db_)
Victor Costan3b02cdf2018-07-18 00:39:561280 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561281
Victor Costan5e785e32019-02-26 20:39:311282 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:201283 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
Victor Costan5e785e32019-02-26 20:39:311284
Victor Costanf37f7ae2019-04-11 19:26:121285 // TODO(pwnall): Cached statements (but not unique statements) should be
1286 // prepared with prepFlags set to SQLITE_PREPARE_PERSISTENT.
1287 sqlite3_stmt* sqlite_statement;
1288 int rc = sqlite3_prepare_v3(db_, sql, /* nByte= */ -1, /* prepFlags= */ 0,
1289 &sqlite_statement, /* pzTail= */ nullptr);
[email protected]473ad792012-11-10 00:55:001290 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591291 // This is evidence of a syntax error in the incoming SQL.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521292 DCHECK_NE(rc, SQLITE_ERROR) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001293
1294 // It could also be database corruption.
Victor Costanbd623112018-07-18 04:17:271295 OnSqliteError(rc, nullptr, sql);
Victor Costan3b02cdf2018-07-18 00:39:561296 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, false);
[email protected]e5ffd0e42009-09-11 21:30:561297 }
Victor Costanf37f7ae2019-04-11 19:26:121298 return base::MakeRefCounted<StatementRef>(tracking_db, sqlite_statement,
1299 true);
[email protected]e5ffd0e42009-09-11 21:30:561300}
1301
Victor Costancfbfa602018-08-01 23:24:461302scoped_refptr<Database::StatementRef> Database::GetUntrackedStatement(
[email protected]2eec0a22012-07-24 01:59:581303 const char* sql) const {
Victor Costanbd623112018-07-18 04:17:271304 return GetStatementImpl(nullptr, sql);
[email protected]2eec0a22012-07-24 01:59:581305}
1306
Victor Costancfbfa602018-08-01 23:24:461307std::string Database::GetSchema() const {
[email protected]92cd00a2013-08-16 11:09:581308 // The ORDER BY should not be necessary, but relying on organic
1309 // order for something like this is questionable.
Victor Costan87cf8c72018-07-19 19:36:041310 static const char kSql[] =
[email protected]92cd00a2013-08-16 11:09:581311 "SELECT type, name, tbl_name, sql "
1312 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1313 Statement statement(GetUntrackedStatement(kSql));
1314
1315 std::string schema;
1316 while (statement.Step()) {
1317 schema += statement.ColumnString(0);
1318 schema += '|';
1319 schema += statement.ColumnString(1);
1320 schema += '|';
1321 schema += statement.ColumnString(2);
1322 schema += '|';
1323 schema += statement.ColumnString(3);
1324 schema += '\n';
1325 }
1326
1327 return schema;
1328}
1329
Victor Costancfbfa602018-08-01 23:24:461330bool Database::IsSQLValid(const char* sql) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:541331 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:201332 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
[email protected]41a97c812013-02-07 02:35:381333 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461334 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381335 return false;
1336 }
1337
Victor Costanf37f7ae2019-04-11 19:26:121338 sqlite3_stmt* sqlite_statement = nullptr;
1339 if (sqlite3_prepare_v3(db_, sql, /* nByte= */ -1, /* prepFlags= */ 0,
1340 &sqlite_statement,
1341 /* pzTail= */ nullptr) != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591342 return false;
Victor Costanf37f7ae2019-04-11 19:26:121343 }
[email protected]eff1fa522011-12-12 23:50:591344
Victor Costanf37f7ae2019-04-11 19:26:121345 sqlite3_finalize(sqlite_statement);
[email protected]eff1fa522011-12-12 23:50:591346 return true;
1347}
1348
Victor Costancfbfa602018-08-01 23:24:461349bool Database::DoesIndexExist(const char* index_name) const {
shessa62504d2016-11-07 19:26:121350 return DoesSchemaItemExist(index_name, "index");
[email protected]e2cadec82011-12-13 02:00:531351}
1352
Victor Costancfbfa602018-08-01 23:24:461353bool Database::DoesTableExist(const char* table_name) const {
shessa62504d2016-11-07 19:26:121354 return DoesSchemaItemExist(table_name, "table");
1355}
1356
Victor Costancfbfa602018-08-01 23:24:461357bool Database::DoesViewExist(const char* view_name) const {
shessa62504d2016-11-07 19:26:121358 return DoesSchemaItemExist(view_name, "view");
1359}
1360
Victor Costancfbfa602018-08-01 23:24:461361bool Database::DoesSchemaItemExist(const char* name, const char* type) const {
Victor Costanf85512e52019-04-10 20:51:361362 static const char kSql[] =
1363 "SELECT 1 FROM sqlite_master WHERE type=? AND name=?";
[email protected]2eec0a22012-07-24 01:59:581364 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471365
Victor Costanf85512e52019-04-10 20:51:361366 if (!statement.is_valid()) {
1367 // The database is corrupt.
shess92a2ab12015-04-09 01:59:471368 return false;
Victor Costanf85512e52019-04-10 20:51:361369 }
shess92a2ab12015-04-09 01:59:471370
[email protected]e2cadec82011-12-13 02:00:531371 statement.BindString(0, type);
1372 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331373
[email protected]e5ffd0e42009-09-11 21:30:561374 return statement.Step(); // Table exists if any row was returned.
1375}
1376
Victor Costancfbfa602018-08-01 23:24:461377bool Database::DoesColumnExist(const char* table_name,
1378 const char* column_name) const {
Victor Costan1ff47e92018-12-07 11:10:431379 // sqlite3_table_column_metadata uses out-params to return column definition
1380 // details, such as the column type and whether it allows NULL values. These
1381 // aren't needed to compute the current method's result, so we pass in nullptr
1382 // for all the out-params.
1383 int error = sqlite3_table_column_metadata(
1384 db_, "main", table_name, column_name, /* pzDataType= */ nullptr,
1385 /* pzCollSeq= */ nullptr, /* pNotNull= */ nullptr,
1386 /* pPrimaryKey= */ nullptr, /* pAutoinc= */ nullptr);
1387 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561388}
1389
Victor Costancfbfa602018-08-01 23:24:461390int64_t Database::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561391 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461392 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]e5ffd0e42009-09-11 21:30:561393 return 0;
1394 }
1395 return sqlite3_last_insert_rowid(db_);
1396}
1397
Victor Costancfbfa602018-08-01 23:24:461398int Database::GetLastChangeCount() const {
[email protected]1ed78a32009-09-15 20:24:171399 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461400 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]1ed78a32009-09-15 20:24:171401 return 0;
1402 }
1403 return sqlite3_changes(db_);
1404}
1405
Victor Costancfbfa602018-08-01 23:24:461406int Database::GetErrorCode() const {
[email protected]e5ffd0e42009-09-11 21:30:561407 if (!db_)
1408 return SQLITE_ERROR;
1409 return sqlite3_errcode(db_);
1410}
1411
Victor Costancfbfa602018-08-01 23:24:461412int Database::GetLastErrno() const {
[email protected]767718e52010-09-21 23:18:491413 if (!db_)
1414 return -1;
1415
1416 int err = 0;
Victor Costanbd623112018-07-18 04:17:271417 if (SQLITE_OK != sqlite3_file_control(db_, nullptr, SQLITE_LAST_ERRNO, &err))
[email protected]767718e52010-09-21 23:18:491418 return -2;
1419
1420 return err;
1421}
1422
Victor Costancfbfa602018-08-01 23:24:461423const char* Database::GetErrorMessage() const {
[email protected]e5ffd0e42009-09-11 21:30:561424 if (!db_)
Victor Costancfbfa602018-08-01 23:24:461425 return "sql::Database is not opened.";
[email protected]e5ffd0e42009-09-11 21:30:561426 return sqlite3_errmsg(db_);
1427}
1428
Victor Costancfbfa602018-08-01 23:24:461429bool Database::OpenInternal(const std::string& file_name,
1430 Database::Retry retry_flag) {
Etienne Bergeron6cb35c72020-02-12 18:09:481431 TRACE_EVENT1("sql", "Database::OpenInternal", "path", file_name);
1432
[email protected]9cfbc922009-11-17 20:13:171433 if (db_) {
Victor Costancfbfa602018-08-01 23:24:461434 DLOG(DCHECK) << "sql::Database is already open.";
[email protected]9cfbc922009-11-17 20:13:171435 return false;
1436 }
1437
Victor Costan5e785e32019-02-26 20:39:311438 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
Etienne Bergerone7681c72020-01-17 00:51:201439 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
Victor Costan5e785e32019-02-26 20:39:311440
Victor Costan3653df62018-02-08 21:38:161441 EnsureSqliteInitialized();
[email protected]a7ec1292013-07-22 22:02:181442
shess58b8df82015-06-03 00:19:321443 // Setup the stats histograms immediately rather than allocating lazily.
Victor Costancfbfa602018-08-01 23:24:461444 // Databases which won't exercise all of these probably shouldn't exist.
shess58b8df82015-06-03 00:19:321445 if (!histogram_tag_.empty()) {
Victor Costancfbfa602018-08-01 23:24:461446 stats_histogram_ = base::LinearHistogram::FactoryGet(
Victor Costanb0e7fc02019-03-01 03:36:271447 "Sqlite.Stats2." + histogram_tag_, 1, EVENT_MAX_VALUE,
Victor Costancfbfa602018-08-01 23:24:461448 EVENT_MAX_VALUE + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
shess58b8df82015-06-03 00:19:321449 }
1450
[email protected]41a97c812013-02-07 02:35:381451 // If |poisoned_| is set, it means an error handler called
1452 // RazeAndClose(). Until regular Close() is called, the caller
1453 // should be treating the database as open, but is_open() currently
1454 // only considers the sqlite3 handle's state.
1455 // TODO(shess): Revise is_open() to consider poisoned_, and review
1456 // to see if any non-testing code even depends on it.
Victor Costancfbfa602018-08-01 23:24:461457 DCHECK(!poisoned_) << "sql::Database is already open.";
[email protected]7bae5742013-07-10 20:46:161458 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381459
shess5f2c3442017-01-24 02:15:101460 // Custom memory-mapping VFS which reads pages using regular I/O on first hit.
1461 sqlite3_vfs* vfs = VFSWrapper();
1462 const char* vfs_name = (vfs ? vfs->zName : nullptr);
Victor Costanc6d3a862018-11-20 18:41:231463
1464 // The flags are documented at https://www.sqlite.org/c3ref/open.html.
1465 //
1466 // Chrome uses SQLITE_OPEN_PRIVATECACHE because SQLite is used by many
1467 // disparate features with their own databases, and having separate page
1468 // caches makes it easier to reason about each feature's performance in
1469 // isolation.
1470 int err = sqlite3_open_v2(
1471 file_name.c_str(), &db_,
1472 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_PRIVATECACHE,
1473 vfs_name);
[email protected]765b44502009-10-02 05:01:421474 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281475 // Extended error codes cannot be enabled until a handle is
1476 // available, fetch manually.
1477 err = sqlite3_extended_errcode(db_);
1478
[email protected]bd2ccdb4a2012-12-07 22:14:501479 // Histogram failures specific to initial open for debugging
1480 // purposes.
Ilya Sherman1c811db2017-12-14 10:36:181481 base::UmaHistogramSparse("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501482
Victor Costanbd623112018-07-18 04:17:271483 OnSqliteError(err, nullptr, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131484 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291485 Close();
[email protected]fed734a2013-07-17 04:45:131486
1487 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1488 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421489 return false;
1490 }
1491
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571492 // If indicated, lock up the database before doing anything else, so
1493 // that the following code doesn't have to deal with locking.
1494 //
1495 // Needs to happen before any other operation is performed in WAL mode so that
1496 // no operation relies on shared memory if exclusive locking is turned on.
1497 //
1498 // TODO(shess): This code is brittle. Find the cases where code
1499 // doesn't request |exclusive_locking_| and audit that it does the
1500 // right thing with SQLITE_BUSY, and that it doesn't make
1501 // assumptions about who might change things in the database.
1502 // http://crbug.com/56559
1503 if (exclusive_locking_) {
1504 // TODO(shess): This should probably be a failure. Code which
1505 // requests exclusive locking but doesn't get it is almost certain
1506 // to be ill-tested.
1507 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
1508 }
1509
[email protected]73fb8d52013-07-24 05:04:281510 // Enable extended result codes to provide more color on I/O errors.
1511 // Not having extended result codes is not a fatal problem, as
1512 // Chromium code does not attempt to handle I/O errors anyhow. The
1513 // current implementation always returns SQLITE_OK, the DCHECK is to
1514 // quickly notify someone if SQLite changes.
1515 err = sqlite3_extended_result_codes(db_, 1);
1516 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1517
shessbccd300e2016-08-20 00:06:561518 // sqlite3_open() does not actually read the database file (unless a hot
1519 // journal is found). Successfully executing this pragma on an existing
1520 // database requires a valid header on page 1. ExecuteAndReturnErrorCode() to
1521 // get the error code before error callback (potentially) overwrites.
[email protected]bd2ccdb4a2012-12-07 22:14:501522 // TODO(shess): For now, just probing to see what the lay of the
1523 // land is. If it's mostly SQLITE_NOTADB, then the database should
1524 // be razed.
1525 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
shessbccd300e2016-08-20 00:06:561526 if (err != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:181527 base::UmaHistogramSparse("Sqlite.OpenProbeFailure", err);
shessbccd300e2016-08-20 00:06:561528 OnSqliteError(err, nullptr, "PRAGMA auto_vacuum");
1529
1530 // Retry or bail out if the error handler poisoned the handle.
Victor Costanf1e9443b2018-12-05 04:35:531531 // TODO(shess): Move this handling to one place (see also sqlite3_open).
1532 // Possibly a wrapper function?
shessbccd300e2016-08-20 00:06:561533 if (poisoned_) {
1534 Close();
1535 if (retry_flag == RETRY_ON_POISON)
1536 return OpenInternal(file_name, NO_RETRY);
1537 return false;
1538 }
1539 }
[email protected]658f8332010-09-18 04:40:431540
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571541 const base::TimeDelta kBusyTimeout =
1542 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1543
1544 // Needs to happen before entering WAL mode. Will only work if this the first
1545 // time the database is being opened in WAL mode.
1546 const std::string page_size_sql =
1547 base::StringPrintf("PRAGMA page_size=%d", page_size_);
1548 ignore_result(ExecuteWithTimeout(page_size_sql.c_str(), kBusyTimeout));
[email protected]5b96f3772010-09-28 16:30:571549
[email protected]4e179ba62012-03-17 16:06:471550 // http://www.sqlite.org/pragma.html#pragma_journal_mode
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571551 // WAL - Use a write-ahead log instead of a journal file.
[email protected]4e179ba62012-03-17 16:06:471552 // DELETE (default) - delete -journal file to commit.
1553 // TRUNCATE - truncate -journal file to commit.
1554 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091555 // TRUNCATE should be faster than DELETE because it won't need directory
1556 // changes for each transaction. PERSIST may break the spirit of using
1557 // secure_delete.
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571558 //
1559 // Needs to be performed after setting exclusive locking mode. Otherwise can
1560 // fail if underlying VFS doesn't support shared memory.
1561 if (UseWALMode()) {
1562 // Set the synchronous flag to NORMAL. This means that writers don't flush
1563 // the WAL file after every write. The WAL file is only flushed on a
1564 // checkpoint. In this case, transcations might lose durability on a power
1565 // loss (but still durable after an application crash).
1566 // TODO([email protected]): Evaluate if this loss of durability is a
1567 // concern.
1568 ignore_result(Execute("PRAGMA synchronous=NORMAL"));
[email protected]4e179ba62012-03-17 16:06:471569
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571570 // Opening the db in WAL mode can fail (eg if the underlying VFS doesn't
1571 // support shared memory and we are not in exclusive locking mode).
1572 //
1573 // TODO([email protected]): We should probably catch a failure here.
1574 ignore_result(Execute("PRAGMA journal_mode=WAL"));
1575 } else {
1576 ignore_result(Execute("PRAGMA journal_mode=TRUNCATE"));
1577 }
[email protected]765b44502009-10-02 05:01:421578
1579 if (cache_size_ != 0) {
Victor Costancfbfa602018-08-01 23:24:461580 const std::string cache_size_sql =
[email protected]7d3cbc92013-03-18 22:33:041581 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
Victor Costancfbfa602018-08-01 23:24:461582 ignore_result(ExecuteWithTimeout(cache_size_sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421583 }
1584
Victor Costanf1e9443b2018-12-05 04:35:531585 static_assert(SQLITE_SECURE_DELETE == 1,
1586 "Chrome assumes secure_delete is on by default.");
[email protected]6e0b1442011-08-09 23:23:581587
shess5dac334f2015-11-05 20:47:421588 // Set a reasonable chunk size for larger files. This reduces churn from
1589 // remapping memory on size changes. It also reduces filesystem
1590 // fragmentation.
1591 // TODO(shess): It may make sense to have this be hinted by the client.
1592 // Database sizes seem to be bimodal, some clients have consistently small
1593 // databases (<20k) while other clients have a broad distribution of sizes
1594 // (hundreds of kilobytes to many megabytes).
Victor Costanbd623112018-07-18 04:17:271595 sqlite3_file* file = nullptr;
shess5dac334f2015-11-05 20:47:421596 sqlite3_int64 db_size = 0;
1597 int rc = GetSqlite3FileAndSize(db_, &file, &db_size);
1598 if (rc == SQLITE_OK && db_size > 16 * 1024) {
1599 int chunk_size = 4 * 1024;
1600 if (db_size > 128 * 1024)
1601 chunk_size = 32 * 1024;
Victor Costanbd623112018-07-18 04:17:271602 sqlite3_file_control(db_, nullptr, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size);
shess5dac334f2015-11-05 20:47:421603 }
1604
shess2f3a814b2015-11-05 18:11:101605 // Enable memory-mapped access. The explicit-disable case is because SQLite
shessd90aeea82015-11-13 02:24:311606 // can be built to default-enable mmap. GetAppropriateMmapSize() calculates a
1607 // safe range to memory-map based on past regular I/O. This value will be
1608 // capped by SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and
1609 // 64-bit platforms.
1610 size_t mmap_size = mmap_disabled_ ? 0 : GetAppropriateMmapSize();
1611 std::string mmap_sql =
Victor Costan4c2f3e922018-08-21 04:47:591612 base::StringPrintf("PRAGMA mmap_size=%" PRIuS, mmap_size);
shessd90aeea82015-11-13 02:24:311613 ignore_result(Execute(mmap_sql.c_str()));
shess2f3a814b2015-11-05 18:11:101614
1615 // Determine if memory-mapping has actually been enabled. The Execute() above
1616 // can succeed without changing the amount mapped.
1617 mmap_enabled_ = false;
1618 {
1619 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1620 if (s.Step() && s.ColumnInt64(0) > 0)
1621 mmap_enabled_ = true;
1622 }
1623
ssid3be5b1ec2016-01-13 14:21:571624 DCHECK(!memory_dump_provider_);
1625 memory_dump_provider_.reset(
Victor Costancfbfa602018-08-01 23:24:461626 new DatabaseMemoryDumpProvider(db_, histogram_tag_));
ssid3be5b1ec2016-01-13 14:21:571627 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
Victor Costancfbfa602018-08-01 23:24:461628 memory_dump_provider_.get(), "sql::Database", nullptr);
ssid3be5b1ec2016-01-13 14:21:571629
[email protected]765b44502009-10-02 05:01:421630 return true;
1631}
1632
Victor Costancfbfa602018-08-01 23:24:461633void Database::DoRollback() {
Etienne Bergeron6cb35c72020-02-12 18:09:481634 TRACE_EVENT0("sql", "Database::DoRollback");
1635
[email protected]e5ffd0e42009-09-11 21:30:561636 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321637
Victor Costan5e785e32019-02-26 20:39:311638 rollback.Run();
shess58b8df82015-06-03 00:19:321639
shess7dbd4dee2015-10-06 17:39:161640 // The cache may have been accumulating dirty pages for commit. Note that in
1641 // some cases sql::Transaction can fire rollback after a database is closed.
1642 if (is_open())
1643 ReleaseCacheMemoryIfNeeded(false);
1644
[email protected]44ad7d902012-03-23 00:09:051645 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561646}
1647
Victor Costancfbfa602018-08-01 23:24:461648void Database::StatementRefCreated(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551649 DCHECK(!open_statements_.count(ref))
1650 << __func__ << " already called with this statement";
[email protected]e5ffd0e42009-09-11 21:30:561651 open_statements_.insert(ref);
1652}
1653
Victor Costancfbfa602018-08-01 23:24:461654void Database::StatementRefDeleted(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551655 DCHECK(open_statements_.count(ref))
1656 << __func__ << " called with non-existing statement";
1657 open_statements_.erase(ref);
[email protected]e5ffd0e42009-09-11 21:30:561658}
1659
Victor Costancfbfa602018-08-01 23:24:461660void Database::set_histogram_tag(const std::string& tag) {
shess58b8df82015-06-03 00:19:321661 DCHECK(!is_open());
Victor Costan87cf8c72018-07-19 19:36:041662
shess58b8df82015-06-03 00:19:321663 histogram_tag_ = tag;
1664}
1665
Will Harrisb8693592018-08-28 22:58:441666void Database::AddTaggedHistogram(const std::string& name, int sample) const {
[email protected]210ce0af2013-05-15 09:10:391667 if (histogram_tag_.empty())
1668 return;
1669
1670 // TODO(shess): The histogram macros create a bit of static storage
1671 // for caching the histogram object. This code shouldn't execute
1672 // often enough for such caching to be crucial. If it becomes an
1673 // issue, the object could be cached alongside histogram_prefix_.
1674 std::string full_histogram_name = name + "." + histogram_tag_;
Victor Costancfbfa602018-08-01 23:24:461675 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
1676 full_histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]210ce0af2013-05-15 09:10:391677 if (histogram)
1678 histogram->Add(sample);
1679}
1680
Victor Costancfbfa602018-08-01 23:24:461681int Database::OnSqliteError(int err,
1682 sql::Statement* stmt,
1683 const char* sql) const {
Etienne Bergeron6cb35c72020-02-12 18:09:481684 TRACE_EVENT0("sql", "Database::OnSqliteError");
1685
Ilya Sherman1c811db2017-12-14 10:36:181686 base::UmaHistogramSparse("Sqlite.Error", err);
[email protected]210ce0af2013-05-15 09:10:391687 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141688
1689 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581690 if (!sql && stmt)
1691 sql = stmt->GetSQLStatement();
1692 if (!sql)
1693 sql = "-- unknown";
shessf7e988f2015-11-13 00:41:061694
1695 std::string id = histogram_tag_;
1696 if (id.empty())
1697 id = DbPath().BaseName().AsUTF8Unsafe();
Victor Costancfbfa602018-08-01 23:24:461698 LOG(ERROR) << id << " sqlite error " << err << ", errno " << GetLastErrno()
1699 << ": " << GetErrorMessage() << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141700
[email protected]c3881b372013-05-17 08:39:461701 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561702 // Fire from a copy of the callback in case of reentry into
1703 // re/set_error_callback().
1704 // TODO(shess): <http://crbug.com/254584>
1705 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461706 return err;
1707 }
1708
[email protected]faa604e2009-09-25 22:38:591709 // The default handling is to assert on debug and to ignore on release.
shess976814402016-06-21 06:56:251710 if (!IsExpectedSqliteError(err))
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521711 DLOG(DCHECK) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591712 return err;
1713}
1714
Victor Costancfbfa602018-08-01 23:24:461715bool Database::FullIntegrityCheck(std::vector<std::string>* messages) {
[email protected]579446c2013-12-16 18:36:521716 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1717}
1718
Victor Costancfbfa602018-08-01 23:24:461719bool Database::QuickIntegrityCheck() {
[email protected]579446c2013-12-16 18:36:521720 std::vector<std::string> messages;
1721 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1722 return false;
1723 return messages.size() == 1 && messages[0] == "ok";
1724}
1725
Victor Costancfbfa602018-08-01 23:24:461726std::string Database::GetDiagnosticInfo(int extended_error,
1727 Statement* statement) {
afakhry7c9abe72016-08-05 17:33:191728 // Prevent reentrant calls to the error callback.
1729 ErrorCallback original_callback = std::move(error_callback_);
1730 reset_error_callback();
1731
1732 // Trim extended error codes.
1733 const int error = (extended_error & 0xFF);
Victor Costancfbfa602018-08-01 23:24:461734 // CollectCorruptionInfo() is implemented in terms of sql::Database,
afakhry7c9abe72016-08-05 17:33:191735 // TODO(shess): Rewrite IntegrityCheckHelper() in terms of raw SQLite.
1736 std::string result = (error == SQLITE_CORRUPT)
1737 ? CollectCorruptionInfo()
1738 : CollectErrorInfo(extended_error, statement);
1739
1740 // The following queries must be executed after CollectErrorInfo() above, so
1741 // if they result in their own errors, they don't interfere with
1742 // CollectErrorInfo().
1743 const bool has_valid_header =
1744 (ExecuteAndReturnErrorCode("PRAGMA auto_vacuum") == SQLITE_OK);
1745 const bool select_sqlite_master_result =
1746 (ExecuteAndReturnErrorCode("SELECT COUNT(*) FROM sqlite_master") ==
1747 SQLITE_OK);
1748
1749 // Restore the original error callback.
1750 error_callback_ = std::move(original_callback);
1751
1752 base::StringAppendF(&result, "Has valid header: %s\n",
1753 (has_valid_header ? "Yes" : "No"));
1754 base::StringAppendF(&result, "Has valid schema: %s\n",
1755 (select_sqlite_master_result ? "Yes" : "No"));
1756
1757 return result;
1758}
1759
[email protected]80abf152013-05-22 12:42:421760// TODO(shess): Allow specifying maximum results (default 100 lines).
Victor Costancfbfa602018-08-01 23:24:461761bool Database::IntegrityCheckHelper(const char* pragma_sql,
1762 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421763 messages->clear();
1764
[email protected]4658e2a02013-06-06 23:05:001765 // This has the side effect of setting SQLITE_RecoveryMode, which
1766 // allows SQLite to process through certain cases of corruption.
1767 // Failing to set this pragma probably means that the database is
1768 // beyond recovery.
Victor Costan4c2f3e922018-08-21 04:47:591769 static const char kWritableSchemaSql[] = "PRAGMA writable_schema=ON";
Victor Costan1d868352018-06-26 19:06:481770 if (!Execute(kWritableSchemaSql))
[email protected]4658e2a02013-06-06 23:05:001771 return false;
1772
1773 bool ret = false;
1774 {
[email protected]579446c2013-12-16 18:36:521775 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001776
1777 // The pragma appears to return all results (up to 100 by default)
1778 // as a single string. This doesn't appear to be an API contract,
1779 // it could return separate lines, so loop _and_ split.
1780 while (stmt.Step()) {
1781 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181782 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1783 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001784 }
1785 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421786 }
[email protected]4658e2a02013-06-06 23:05:001787
1788 // Best effort to put things back as they were before.
Victor Costan4c2f3e922018-08-21 04:47:591789 static const char kNoWritableSchemaSql[] = "PRAGMA writable_schema=OFF";
Victor Costan1d868352018-06-26 19:06:481790 ignore_result(Execute(kNoWritableSchemaSql));
[email protected]4658e2a02013-06-06 23:05:001791
1792 return ret;
[email protected]80abf152013-05-22 12:42:421793}
1794
Victor Costancfbfa602018-08-01 23:24:461795bool Database::ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
1796 const std::string& dump_name) {
dskibab4199f82016-11-21 20:16:131797 return memory_dump_provider_ &&
ssid1f4e5362016-12-08 20:41:381798 memory_dump_provider_->ReportMemoryUsage(pmd, dump_name);
dskibab4199f82016-11-21 20:16:131799}
1800
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571801bool Database::UseWALMode() const {
1802#if defined(OS_FUCHSIA)
1803 // WAL mode is only enabled on Fuchsia for databases with exclusive
1804 // locking, because this case does not require shared memory support.
1805 // At the time this was implemented (May 2020), Fuchsia's shared
1806 // memory support was insufficient for SQLite's needs.
1807 return want_wal_mode_ && exclusive_locking_;
1808#else
1809 return want_wal_mode_;
1810#endif // defined(OS_FUCHSIA)
1811}
1812
1813bool Database::CheckpointDatabase() {
1814 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
1815 InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
1816
1817 static const char* kMainDb = "main";
1818 int rc = sqlite3_wal_checkpoint_v2(db_, kMainDb, SQLITE_CHECKPOINT_PASSIVE,
1819 /*pnLog=*/nullptr,
1820 /*pnCkpt=*/nullptr);
1821
1822 return rc == SQLITE_OK;
1823}
1824
[email protected]e5ffd0e42009-09-11 21:30:561825} // namespace sql