blob: bcd2b9229439c79ca199bb13c39e2c6acfc90dab [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
tzikb9dae932017-02-10 03:57:3012#include "base/debug/alias.h"
shessc8cd2a162015-10-22 20:30:4613#include "base/debug/dump_without_crashing.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4415#include "base/files/file_util.h"
shessc8cd2a162015-10-22 20:30:4616#include "base/format_macros.h"
17#include "base/json/json_file_value_serializer.h"
fdoray2dfa76452016-06-07 13:11:2218#include "base/location.h"
[email protected]e5ffd0e42009-09-11 21:30:5619#include "base/logging.h"
Ilya Sherman1c811db2017-12-14 10:36:1820#include "base/metrics/histogram_functions.h"
asvitkine30330812016-08-30 04:01:0821#include "base/metrics/histogram_macros.h"
[email protected]210ce0af2013-05-15 09:10:3922#include "base/metrics/sparse_histogram.h"
Victor Costan3653df62018-02-08 21:38:1623#include "base/no_destructor.h"
Will Harrisb8693592018-08-28 22:58:4424#include "base/numerics/safe_conversions.h"
fdoray2dfa76452016-06-07 13:11:2225#include "base/single_thread_task_runner.h"
[email protected]80abf152013-05-22 12:42:4226#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1927#include "base/strings/string_util.h"
28#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4529#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1830#include "base/synchronization/lock.h"
Etienne Pierre-Doray0400dfb62018-12-03 19:12:2531#include "base/threading/scoped_blocking_call.h"
ssid9f8022f2015-10-12 17:49:0332#include "base/trace_event/memory_dump_manager.h"
Kevin Marshalla9f05ec2017-07-14 02:10:0533#include "build/build_config.h"
Victor Costancfbfa602018-08-01 23:24:4634#include "sql/database_memory_dump_provider.h"
Victor Costan3653df62018-02-08 21:38:1635#include "sql/initialization.h"
shess9bf2c672015-12-18 01:18:0836#include "sql/meta_table.h"
Victor Costan4c2f3e922018-08-21 04:47:5937#include "sql/sql_features.h"
[email protected]f0a54b22011-07-19 18:40:2138#include "sql/statement.h"
shess5f2c3442017-01-24 02:15:1039#include "sql/vfs_wrapper.h"
[email protected]e33cba42010-08-18 23:37:0340#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5641
[email protected]5b96f3772010-09-28 16:30:5742namespace {
43
44// Spin for up to a second waiting for the lock to clear when setting
45// up the database.
46// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2747const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5748
49class ScopedBusyTimeout {
50 public:
Victor Costancfbfa602018-08-01 23:24:4651 explicit ScopedBusyTimeout(sqlite3* db) : db_(db) {}
52 ~ScopedBusyTimeout() { sqlite3_busy_timeout(db_, 0); }
[email protected]5b96f3772010-09-28 16:30:5753
54 int SetTimeout(base::TimeDelta timeout) {
55 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
56 return sqlite3_busy_timeout(db_,
57 static_cast<int>(timeout.InMilliseconds()));
58 }
59
60 private:
61 sqlite3* db_;
62};
63
[email protected]6d42f152012-11-10 00:38:2464// Helper to "safely" enable writable_schema. No error checking
65// because it is reasonable to just forge ahead in case of an error.
66// If turning it on fails, then most likely nothing will work, whereas
67// if turning it off fails, it only matters if some code attempts to
68// continue working with the database and tries to modify the
69// sqlite_master table (none of our code does this).
70class ScopedWritableSchema {
71 public:
Victor Costancfbfa602018-08-01 23:24:4672 explicit ScopedWritableSchema(sqlite3* db) : db_(db) {
Victor Costanbd623112018-07-18 04:17:2773 sqlite3_exec(db_, "PRAGMA writable_schema=1", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2474 }
75 ~ScopedWritableSchema() {
Victor Costanbd623112018-07-18 04:17:2776 sqlite3_exec(db_, "PRAGMA writable_schema=0", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2477 }
78
79 private:
80 sqlite3* db_;
81};
82
[email protected]7bae5742013-07-10 20:46:1683// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
84// SQLite error code from running the backup step.
85int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
86 DCHECK_NE(src, dst);
87 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
88 if (!backup) {
89 // Since this call only sets things up, this indicates a gross
90 // error in SQLite.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:5291 DLOG(DCHECK) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
[email protected]7bae5742013-07-10 20:46:1692 return sqlite3_errcode(dst);
93 }
94
95 // -1 backs up the entire database.
96 int rc = sqlite3_backup_step(backup, -1);
97 int pages = sqlite3_backup_pagecount(backup);
98 sqlite3_backup_finish(backup);
99
100 // If successful, exactly one page should have been backed up. If
101 // this breaks, check this function to make sure assumptions aren't
102 // being broken.
103 if (rc == SQLITE_DONE)
104 DCHECK_EQ(pages, 1);
105
106 return rc;
107}
108
[email protected]8d409412013-07-19 18:25:30109// Be very strict on attachment point. SQLite can handle a much wider
110// character set with appropriate quoting, but Chromium code should
111// just use clean names to start with.
112bool ValidAttachmentPoint(const char* attachment_point) {
113 for (size_t i = 0; attachment_point[i]; ++i) {
zhongyi23960342016-04-12 23:13:20114 if (!(base::IsAsciiDigit(attachment_point[i]) ||
115 base::IsAsciiAlpha(attachment_point[i]) ||
[email protected]8d409412013-07-19 18:25:30116 attachment_point[i] == '_')) {
117 return false;
118 }
119 }
120 return true;
121}
122
[email protected]8ada10f2013-12-21 00:42:34123// Helper to get the sqlite3_file* associated with the "main" database.
124int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
Victor Costanbd623112018-07-18 04:17:27125 *file = nullptr;
126 int rc = sqlite3_file_control(db, nullptr, SQLITE_FCNTL_FILE_POINTER, file);
[email protected]8ada10f2013-12-21 00:42:34127 if (rc != SQLITE_OK)
128 return rc;
129
Victor Costanbd623112018-07-18 04:17:27130 // TODO(shess): null in file->pMethods has been observed on android_dbg
[email protected]8ada10f2013-12-21 00:42:34131 // content_unittests, even though it should not be possible.
132 // http://crbug.com/329982
133 if (!*file || !(*file)->pMethods)
134 return SQLITE_ERROR;
135
136 return rc;
137}
138
shess5dac334f2015-11-05 20:47:42139// Convenience to get the sqlite3_file* and the size for the "main" database.
140int GetSqlite3FileAndSize(sqlite3* db,
Victor Costancfbfa602018-08-01 23:24:46141 sqlite3_file** file,
142 sqlite3_int64* db_size) {
shess5dac334f2015-11-05 20:47:42143 int rc = GetSqlite3File(db, file);
144 if (rc != SQLITE_OK)
145 return rc;
146
147 return (*file)->pMethods->xFileSize(*file, db_size);
148}
149
erg102ceb412015-06-20 01:38:13150std::string AsUTF8ForSQL(const base::FilePath& path) {
151#if defined(OS_WIN)
jdoerrie6312bf62019-02-01 22:03:42152 return base::UTF16ToUTF8(path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18153#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
erg102ceb412015-06-20 01:38:13154 return path.value();
155#endif
156}
157
[email protected]5b96f3772010-09-28 16:30:57158} // namespace
159
[email protected]e5ffd0e42009-09-11 21:30:56160namespace sql {
161
[email protected]4350e322013-06-18 22:18:10162// static
Victor Costancfbfa602018-08-01 23:24:46163Database::ErrorExpecterCallback* Database::current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10164
165// static
Victor Costancfbfa602018-08-01 23:24:46166bool Database::IsExpectedSqliteError(int error) {
shess976814402016-06-21 06:56:25167 if (!current_expecter_cb_)
[email protected]4350e322013-06-18 22:18:10168 return false;
shess976814402016-06-21 06:56:25169 return current_expecter_cb_->Run(error);
[email protected]4350e322013-06-18 22:18:10170}
171
Victor Costancfbfa602018-08-01 23:24:46172void Database::ReportDiagnosticInfo(int extended_error, Statement* stmt) {
afakhry7c9abe72016-08-05 17:33:19173 std::string debug_info = GetDiagnosticInfo(extended_error, stmt);
shessc8cd2a162015-10-22 20:30:46174 if (!debug_info.empty() && RegisterIntentToUpload()) {
Lukasz Anforowicz68c21772018-01-13 03:42:44175 DEBUG_ALIAS_FOR_CSTR(debug_buf, debug_info.c_str(), 2000);
shessc8cd2a162015-10-22 20:30:46176 base::debug::DumpWithoutCrashing();
177 }
178}
179
[email protected]4350e322013-06-18 22:18:10180// static
Victor Costancfbfa602018-08-01 23:24:46181void Database::SetErrorExpecter(Database::ErrorExpecterCallback* cb) {
Victor Costanbd623112018-07-18 04:17:27182 CHECK(!current_expecter_cb_);
shess976814402016-06-21 06:56:25183 current_expecter_cb_ = cb;
[email protected]4350e322013-06-18 22:18:10184}
185
186// static
Victor Costancfbfa602018-08-01 23:24:46187void Database::ResetErrorExpecter() {
shess976814402016-06-21 06:56:25188 CHECK(current_expecter_cb_);
Victor Costanbd623112018-07-18 04:17:27189 current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10190}
191
Victor Costance678e72018-07-24 10:25:00192// static
Victor Costancfbfa602018-08-01 23:24:46193base::FilePath Database::JournalPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00194 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-journal"));
195}
196
197// static
Victor Costancfbfa602018-08-01 23:24:46198base::FilePath Database::WriteAheadLogPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00199 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-wal"));
200}
201
202// static
Victor Costancfbfa602018-08-01 23:24:46203base::FilePath Database::SharedMemoryFilePath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00204 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-shm"));
205}
206
Victor Costancfbfa602018-08-01 23:24:46207Database::StatementRef::StatementRef(Database* database,
208 sqlite3_stmt* stmt,
209 bool was_valid)
210 : database_(database), stmt_(stmt), was_valid_(was_valid) {
211 if (database)
212 database_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56213}
214
Victor Costancfbfa602018-08-01 23:24:46215Database::StatementRef::~StatementRef() {
216 if (database_)
217 database_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38218 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56219}
220
Victor Costancfbfa602018-08-01 23:24:46221void Database::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56222 if (stmt_) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54223 // Call to InitScopedBlockingCall() cannot go at the beginning of the
224 // function because Close() is called unconditionally from destructor to
225 // clean database_. And if this is inactive statement this won't cause any
226 // disk access and destructor most probably will be called on thread not
227 // allowing disk access.
[email protected]35f7e5392012-07-27 19:54:50228 // TODO([email protected]): This should move to the beginning
229 // of the function. http://crbug.com/136655.
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54230 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
231 InitScopedBlockingCall(&scoped_blocking_call);
[email protected]e5ffd0e42009-09-11 21:30:56232 sqlite3_finalize(stmt_);
Victor Costanbd623112018-07-18 04:17:27233 stmt_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56234 }
Victor Costancfbfa602018-08-01 23:24:46235 database_ = nullptr; // The Database may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38236
237 // Forced close is expected to happen from a statement error
238 // handler. In that case maintain the sense of |was_valid_| which
239 // previously held for this ref.
240 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56241}
242
Victor Costan7f6abbbe2018-07-29 02:57:27243static_assert(
Victor Costancfbfa602018-08-01 23:24:46244 Database::kDefaultPageSize == SQLITE_DEFAULT_PAGE_SIZE,
245 "Database::kDefaultPageSize must match the value configured into SQLite");
Victor Costan7f6abbbe2018-07-29 02:57:27246
Victor Costancfbfa602018-08-01 23:24:46247constexpr int Database::kDefaultPageSize;
Victor Costan7f6abbbe2018-07-29 02:57:27248
Victor Costancfbfa602018-08-01 23:24:46249Database::Database()
Victor Costanbd623112018-07-18 04:17:27250 : db_(nullptr),
Victor Costan7f6abbbe2018-07-29 02:57:27251 page_size_(kDefaultPageSize),
[email protected]e5ffd0e42009-09-11 21:30:56252 cache_size_(0),
253 exclusive_locking_(false),
254 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50255 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16256 in_memory_(false),
shess58b8df82015-06-03 00:19:32257 poisoned_(false),
shessa62504d2016-11-07 19:26:12258 mmap_alt_status_(false),
kerz42ff2a012016-04-27 04:50:06259 mmap_disabled_(false),
shess7dbd4dee2015-10-06 17:39:16260 mmap_enabled_(false),
261 total_changes_at_last_release_(0),
Victor Costan5e785e32019-02-26 20:39:31262 stats_histogram_(nullptr) {}
[email protected]e5ffd0e42009-09-11 21:30:56263
Victor Costancfbfa602018-08-01 23:24:46264Database::~Database() {
[email protected]e5ffd0e42009-09-11 21:30:56265 Close();
266}
267
Victor Costancfbfa602018-08-01 23:24:46268void Database::RecordEvent(Events event, size_t count) {
shess58b8df82015-06-03 00:19:32269 for (size_t i = 0; i < count; ++i) {
Victor Costanb0e7fc02019-03-01 03:36:27270 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats2", event, EVENT_MAX_VALUE);
shess58b8df82015-06-03 00:19:32271 }
272
273 if (stats_histogram_) {
274 for (size_t i = 0; i < count; ++i) {
275 stats_histogram_->Add(event);
276 }
277 }
278}
279
Victor Costancfbfa602018-08-01 23:24:46280bool Database::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02281 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26282 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49283 if (base::GetFileSize(path, &size_64)) {
Will Harrisb8693592018-08-28 22:58:44284 int sample = base::saturated_cast<int>(size_64 / 1024);
[email protected]348ac8f52013-05-21 03:27:02285 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
Victor Costancfbfa602018-08-01 23:24:46286 base::HistogramBase* histogram = base::Histogram::FactoryGet(
287 full_histogram_name, 1, 1000000, 50,
288 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]348ac8f52013-05-21 03:27:02289 if (histogram)
290 histogram->Add(sample);
Steven Holte95922222018-09-14 20:06:23291 UMA_HISTOGRAM_COUNTS_1M("Sqlite.SizeKB", sample);
[email protected]348ac8f52013-05-21 03:27:02292 }
293 }
294
erg102ceb412015-06-20 01:38:13295 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42296}
[email protected]e5ffd0e42009-09-11 21:30:56297
Victor Costancfbfa602018-08-01 23:24:46298bool Database::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50299 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13300 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56301}
302
Victor Costancfbfa602018-08-01 23:24:46303bool Database::OpenTemporary() {
[email protected]8d409412013-07-19 18:25:30304 return OpenInternal("", NO_RETRY);
305}
306
Victor Costancfbfa602018-08-01 23:24:46307void Database::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47308 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
309 // will delete the -journal file. For ChromiumOS or other more
310 // embedded systems, this is probably not appropriate, whereas on
311 // desktop it might make some sense.
312
[email protected]4b350052012-02-24 20:40:48313 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48314
[email protected]41a97c812013-02-07 02:35:38315 // Release cached statements.
316 statement_cache_.clear();
317
318 // With cached statements released, in-use statements will remain.
319 // Closing the database while statements are in use is an API
320 // violation, except for forced close (which happens from within a
321 // statement's error handler).
322 DCHECK(forced || open_statements_.empty());
323
324 // Deactivate any outstanding statements so sqlite3_close() works.
Victor Costanc7e7f2e2018-07-18 20:07:55325 for (StatementRef* statement_ref : open_statements_)
326 statement_ref->Close(forced);
[email protected]41a97c812013-02-07 02:35:38327 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48328
[email protected]e5ffd0e42009-09-11 21:30:56329 if (db_) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54330 // Call to InitScopedBlockingCall() cannot go at the beginning of the
331 // function because Close() must be called from destructor to clean
[email protected]35f7e5392012-07-27 19:54:50332 // statement_cache_, it won't cause any disk access and it most probably
333 // will happen on thread not allowing disk access.
334 // TODO([email protected]): This should move to the beginning
335 // of the function. http://crbug.com/136655.
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54336 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
337 InitScopedBlockingCall(&scoped_blocking_call);
[email protected]73fb8d52013-07-24 05:04:28338
ssid3be5b1ec2016-01-13 14:21:57339 // Reseting acquires a lock to ensure no dump is happening on the database
340 // at the same time. Unregister takes ownership of provider and it is safe
341 // since the db is reset. memory_dump_provider_ could be null if db_ was
342 // poisoned.
343 if (memory_dump_provider_) {
344 memory_dump_provider_->ResetDatabase();
345 base::trace_event::MemoryDumpManager::GetInstance()
346 ->UnregisterAndDeleteDumpProviderSoon(
347 std::move(memory_dump_provider_));
348 }
349
[email protected]73fb8d52013-07-24 05:04:28350 int rc = sqlite3_close(db_);
351 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:18352 base::UmaHistogramSparse("Sqlite.CloseFailure", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52353 DLOG(DCHECK) << "sqlite3_close failed: " << GetErrorMessage();
[email protected]73fb8d52013-07-24 05:04:28354 }
[email protected]e5ffd0e42009-09-11 21:30:56355 }
Victor Costanbd623112018-07-18 04:17:27356 db_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56357}
358
Victor Costancfbfa602018-08-01 23:24:46359void Database::Close() {
[email protected]41a97c812013-02-07 02:35:38360 // If the database was already closed by RazeAndClose(), then no
361 // need to close again. Clear the |poisoned_| bit so that incorrect
362 // API calls are caught.
363 if (poisoned_) {
364 poisoned_ = false;
365 return;
366 }
367
368 CloseInternal(false);
369}
370
Victor Costancfbfa602018-08-01 23:24:46371void Database::Preload() {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54372 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
373 InitScopedBlockingCall(&scoped_blocking_call);
[email protected]35f7e5392012-07-27 19:54:50374
[email protected]e5ffd0e42009-09-11 21:30:56375 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52376 DCHECK(poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56377 return;
378 }
379
Victor Costan7f6abbbe2018-07-29 02:57:27380 // The constructor and set_page_size() ensure that page_size_ is never zero.
381 const int page_size = page_size_;
382 DCHECK(page_size);
383
[email protected]8ada10f2013-12-21 00:42:34384 // Use local settings if provided, otherwise use documented defaults. The
385 // actual results could be fetching via PRAGMA calls.
[email protected]8ada10f2013-12-21 00:42:34386 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
387 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56388 return;
389
Victor Costanbd623112018-07-18 04:17:27390 sqlite3_file* file = nullptr;
[email protected]8ada10f2013-12-21 00:42:34391 sqlite3_int64 file_size = 0;
shess5dac334f2015-11-05 20:47:42392 int rc = GetSqlite3FileAndSize(db_, &file, &file_size);
[email protected]8ada10f2013-12-21 00:42:34393 if (rc != SQLITE_OK)
394 return;
395
396 // Don't preload more than the file contains.
397 if (preload_size > file_size)
398 preload_size = file_size;
399
mostynbd82cd9952016-04-11 20:05:34400 std::unique_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46401 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34402 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
shessd90aeea82015-11-13 02:24:31403
404 // TODO(shess): Consider calling OnSqliteError().
[email protected]8ada10f2013-12-21 00:42:34405 if (rc != SQLITE_OK)
406 return;
407 }
[email protected]e5ffd0e42009-09-11 21:30:56408}
409
Victor Costancfbfa602018-08-01 23:24:46410// SQLite keeps unused pages associated with a database in a cache. It asks
shess7dbd4dee2015-10-06 17:39:16411// the cache for pages by an id, and if the page is present and the database is
412// unchanged, it considers the content of the page valid and doesn't read it
413// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
414// structures created from the memory map data before consulting the cache. On
415// write SQLite creates a new in-memory page structure, copies the data from the
416// memory map, and later writes it, releasing the updated page back to the
417// cache.
418//
419// This means that in memory-mapped mode, the contents of the cached pages are
420// not re-used for reads, but they are re-used for writes if the re-written page
421// is still in the cache. The implementation of sqlite3_db_release_memory() as
422// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
Victor Costancfbfa602018-08-01 23:24:46423// database, so it should free these pages.
shess7dbd4dee2015-10-06 17:39:16424//
425// Unfortunately, the zero page is also freed. That page is never accessed
426// using memory-mapped I/O, and the cached copy can be re-used after verifying
427// the file change counter on disk. Also, fresh pages from cache receive some
428// pager-level initialization before they can be used. Since the information
429// involved will immediately be accessed in various ways, it is unclear if the
430// additional overhead is material, or just moving processor cache effects
431// around.
432//
433// TODO(shess): It would be better to release the pages immediately when they
434// are no longer needed. This would basically happen after SQLite commits a
435// transaction. I had implemented a pcache wrapper to do this, but it involved
436// layering violations, and it had to be setup before any other sqlite call,
437// which was brittle. Also, for large files it would actually make sense to
438// maintain the existing pcache behavior for blocks past the memory-mapped
439// segment. I think drh would accept a reasonable implementation of the overall
440// concept for upstreaming to SQLite core.
441//
442// TODO(shess): Another possibility would be to set the cache size small, which
443// would keep the zero page around, plus some pre-initialized pages, and SQLite
444// can manage things. The downside is that updates larger than the cache would
445// spill to the journal. That could be compensated by setting cache_spill to
446// false. The downside then is that it allows open-ended use of memory for
447// large transactions.
Victor Costancfbfa602018-08-01 23:24:46448void Database::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
shess644fc8a2016-02-26 18:15:58449 // The database could have been closed during a transaction as part of error
450 // recovery.
451 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:46452 DCHECK(poisoned_) << "Illegal use of Database without a db";
shess644fc8a2016-02-26 18:15:58453 return;
454 }
shess7dbd4dee2015-10-06 17:39:16455
456 // If memory-mapping is not enabled, the page cache helps performance.
457 if (!mmap_enabled_)
458 return;
459
460 // On caller request, force the change comparison to fail. Done before the
461 // transaction-nesting test so that the signal can carry to transaction
462 // commit.
463 if (implicit_change_performed)
464 --total_changes_at_last_release_;
465
466 // Cached pages may be re-used within the same transaction.
467 if (transaction_nesting())
468 return;
469
470 // If no changes have been made, skip flushing. This allows the first page of
471 // the database to remain in cache across multiple reads.
472 const int total_changes = sqlite3_total_changes(db_);
473 if (total_changes == total_changes_at_last_release_)
474 return;
475
476 total_changes_at_last_release_ = total_changes;
477 sqlite3_db_release_memory(db_);
478}
479
Victor Costancfbfa602018-08-01 23:24:46480base::FilePath Database::DbPath() const {
shessc8cd2a162015-10-22 20:30:46481 if (!is_open())
482 return base::FilePath();
483
484 const char* path = sqlite3_db_filename(db_, "main");
485 const base::StringPiece db_path(path);
486#if defined(OS_WIN)
jdoerrie6312bf62019-02-01 22:03:42487 return base::FilePath(base::UTF8ToUTF16(db_path));
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18488#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46489 return base::FilePath(db_path);
490#else
491 NOTREACHED();
492 return base::FilePath();
493#endif
494}
495
496// Data is persisted in a file shared between databases in the same directory.
497// The "sqlite-diag" file contains a dictionary with the version number, and an
498// array of histogram tags for databases which have been dumped.
Victor Costancfbfa602018-08-01 23:24:46499bool Database::RegisterIntentToUpload() const {
shessc8cd2a162015-10-22 20:30:46500 static const char* kVersionKey = "version";
501 static const char* kDiagnosticDumpsKey = "DiagnosticDumps";
502 static int kVersion = 1;
503
shessc8cd2a162015-10-22 20:30:46504 if (histogram_tag_.empty())
505 return false;
506
507 if (!is_open())
508 return false;
509
510 if (in_memory_)
511 return false;
512
513 const base::FilePath db_path = DbPath();
514 if (db_path.empty())
515 return false;
516
517 // Put the collection of diagnostic data next to the databases. In most
518 // cases, this is the profile directory, but safe-browsing stores a Cookies
519 // file in the directory above the profile directory.
Victor Costance678e72018-07-24 10:25:00520 base::FilePath breadcrumb_path = db_path.DirName().AppendASCII("sqlite-diag");
shessc8cd2a162015-10-22 20:30:46521
522 // Lock against multiple updates to the diagnostics file. This code should
523 // seldom be called in the first place, and when called it should seldom be
524 // called for multiple databases, and when called for multiple databases there
525 // is _probably_ something systemic wrong with the user's system. So the lock
526 // should never be contended, but when it is the database experience is
527 // already bad.
Victor Costan3653df62018-02-08 21:38:16528 static base::NoDestructor<base::Lock> lock;
529 base::AutoLock auto_lock(*lock);
shessc8cd2a162015-10-22 20:30:46530
mostynbd82cd9952016-04-11 20:05:34531 std::unique_ptr<base::Value> root;
shessc8cd2a162015-10-22 20:30:46532 if (!base::PathExists(breadcrumb_path)) {
mostynbd82cd9952016-04-11 20:05:34533 std::unique_ptr<base::DictionaryValue> root_dict(
534 new base::DictionaryValue());
shessc8cd2a162015-10-22 20:30:46535 root_dict->SetInteger(kVersionKey, kVersion);
536
mostynbd82cd9952016-04-11 20:05:34537 std::unique_ptr<base::ListValue> dumps(new base::ListValue);
shessc8cd2a162015-10-22 20:30:46538 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50539 root_dict->Set(kDiagnosticDumpsKey, std::move(dumps));
shessc8cd2a162015-10-22 20:30:46540
dchenge48600452015-12-28 02:24:50541 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46542 } else {
543 // Failure to read a valid dictionary implies that something is going wrong
544 // on the system.
545 JSONFileValueDeserializer deserializer(breadcrumb_path);
mostynbd82cd9952016-04-11 20:05:34546 std::unique_ptr<base::Value> read_root(
shessc8cd2a162015-10-22 20:30:46547 deserializer.Deserialize(nullptr, nullptr));
548 if (!read_root.get())
549 return false;
mostynbd82cd9952016-04-11 20:05:34550 std::unique_ptr<base::DictionaryValue> root_dict =
dchenge48600452015-12-28 02:24:50551 base::DictionaryValue::From(std::move(read_root));
shessc8cd2a162015-10-22 20:30:46552 if (!root_dict)
553 return false;
554
555 // Don't upload if the version is missing or newer.
556 int version = 0;
557 if (!root_dict->GetInteger(kVersionKey, &version) || version > kVersion)
558 return false;
559
560 base::ListValue* dumps = nullptr;
561 if (!root_dict->GetList(kDiagnosticDumpsKey, &dumps))
562 return false;
563
564 const size_t size = dumps->GetSize();
565 for (size_t i = 0; i < size; ++i) {
566 std::string s;
567
568 // Don't upload if the value isn't a string, or indicates a prior upload.
569 if (!dumps->GetString(i, &s) || s == histogram_tag_)
570 return false;
571 }
572
573 // Record intention to proceed with upload.
574 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50575 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46576 }
577
578 const base::FilePath breadcrumb_new =
579 breadcrumb_path.AddExtension(FILE_PATH_LITERAL("new"));
580 base::DeleteFile(breadcrumb_new, false);
581
582 // No upload if the breadcrumb file cannot be updated.
583 // TODO(shess): Consider ImportantFileWriter::WriteFileAtomically() to land
584 // the data on disk. For now, losing the data is not a big problem, so the
585 // sync overhead would probably not be worth it.
586 JSONFileValueSerializer serializer(breadcrumb_new);
587 if (!serializer.Serialize(*root))
588 return false;
589 if (!base::PathExists(breadcrumb_new))
590 return false;
591 if (!base::ReplaceFile(breadcrumb_new, breadcrumb_path, nullptr)) {
592 base::DeleteFile(breadcrumb_new, false);
593 return false;
594 }
595
596 return true;
597}
598
Victor Costancfbfa602018-08-01 23:24:46599std::string Database::CollectErrorInfo(int error, Statement* stmt) const {
shessc8cd2a162015-10-22 20:30:46600 // Buffer for accumulating debugging info about the error. Place
601 // more-relevant information earlier, in case things overflow the
602 // fixed-size reporting buffer.
603 std::string debug_info;
604
605 // The error message from the failed operation.
Victor Costancfbfa602018-08-01 23:24:46606 base::StringAppendF(&debug_info, "db error: %d/%s\n", GetErrorCode(),
607 GetErrorMessage());
shessc8cd2a162015-10-22 20:30:46608
609 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
610 // reading code does not entirely convince me. Remove if they turn out to be
611 // the same.
612 if (error != GetErrorCode())
613 base::StringAppendF(&debug_info, "reported error: %d\n", error);
614
Victor Costancfbfa602018-08-01 23:24:46615// System error information. Interpretation of Windows errors is different
616// from posix.
shessc8cd2a162015-10-22 20:30:46617#if defined(OS_WIN)
618 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18619#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46620 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
621#else
622 NOTREACHED(); // Add appropriate log info.
623#endif
624
625 if (stmt) {
626 base::StringAppendF(&debug_info, "statement: %s\n",
627 stmt->GetSQLStatement());
628 } else {
629 base::StringAppendF(&debug_info, "statement: NULL\n");
630 }
631
632 // SQLITE_ERROR often indicates some sort of mismatch between the statement
633 // and the schema, possibly due to a failed schema migration.
634 if (error == SQLITE_ERROR) {
635 const char* kVersionSql = "SELECT value FROM meta WHERE key = 'version'";
636 sqlite3_stmt* s;
637 int rc = sqlite3_prepare_v2(db_, kVersionSql, -1, &s, nullptr);
638 if (rc == SQLITE_OK) {
639 rc = sqlite3_step(s);
640 if (rc == SQLITE_ROW) {
641 base::StringAppendF(&debug_info, "version: %d\n",
642 sqlite3_column_int(s, 0));
643 } else if (rc == SQLITE_DONE) {
644 debug_info += "version: none\n";
645 } else {
646 base::StringAppendF(&debug_info, "version: error %d\n", rc);
647 }
648 sqlite3_finalize(s);
649 } else {
650 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
651 }
652
653 debug_info += "schema:\n";
654
655 // sqlite_master has columns:
656 // type - "index" or "table".
657 // name - name of created element.
658 // tbl_name - name of element, or target table in case of index.
659 // rootpage - root page of the element in database file.
660 // sql - SQL to create the element.
661 // In general, the |sql| column is sufficient to derive the other columns.
662 // |rootpage| is not interesting for debugging, without the contents of the
663 // database. The COALESCE is because certain automatic elements will have a
664 // |name| but no |sql|,
665 const char* kSchemaSql = "SELECT COALESCE(sql, name) FROM sqlite_master";
666 rc = sqlite3_prepare_v2(db_, kSchemaSql, -1, &s, nullptr);
667 if (rc == SQLITE_OK) {
668 while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
669 base::StringAppendF(&debug_info, "%s\n", sqlite3_column_text(s, 0));
670 }
671 if (rc != SQLITE_DONE)
672 base::StringAppendF(&debug_info, "error %d\n", rc);
673 sqlite3_finalize(s);
674 } else {
675 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
676 }
677 }
678
679 return debug_info;
680}
681
682// TODO(shess): Since this is only called in an error situation, it might be
683// prudent to rewrite in terms of SQLite API calls, and mark the function const.
Victor Costancfbfa602018-08-01 23:24:46684std::string Database::CollectCorruptionInfo() {
shessc8cd2a162015-10-22 20:30:46685 // If the file cannot be accessed it is unlikely that an integrity check will
686 // turn up actionable information.
687 const base::FilePath db_path = DbPath();
avi0b519202015-12-21 07:25:19688 int64_t db_size = -1;
shessc8cd2a162015-10-22 20:30:46689 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
690 return std::string();
691
692 // Buffer for accumulating debugging info about the error. Place
693 // more-relevant information earlier, in case things overflow the
694 // fixed-size reporting buffer.
695 std::string debug_info;
696 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
697 db_size);
698
699 // Only check files up to 8M to keep things from blocking too long.
avi0b519202015-12-21 07:25:19700 const int64_t kMaxIntegrityCheckSize = 8192 * 1024;
shessc8cd2a162015-10-22 20:30:46701 if (db_size > kMaxIntegrityCheckSize) {
702 debug_info += "integrity_check skipped due to size\n";
703 } else {
704 std::vector<std::string> messages;
705
706 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
707 // into a string. Probably should be refactored.
708 const base::TimeTicks before = base::TimeTicks::Now();
709 FullIntegrityCheck(&messages);
710 base::StringAppendF(
Victor Costancfbfa602018-08-01 23:24:46711 &debug_info, "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
712 (base::TimeTicks::Now() - before).InMilliseconds(), messages.size());
shessc8cd2a162015-10-22 20:30:46713
714 // SQLite returns up to 100 messages by default, trim deeper to
715 // keep close to the 2000-character size limit for dumping.
716 const size_t kMaxMessages = 20;
717 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
718 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
719 }
720 }
721
722 return debug_info;
723}
724
Victor Costancfbfa602018-08-01 23:24:46725bool Database::GetMmapAltStatus(int64_t* status) {
shessa62504d2016-11-07 19:26:12726 // The [meta] version uses a missing table as a signal for a fresh database.
727 // That will not work for the view, which would not exist in either a new or
728 // an existing database. A new database _should_ be only one page long, so
729 // just don't bother optimizing this case (start at offset 0).
730 // TODO(shess): Could the [meta] case also get simpler, then?
731 if (!DoesViewExist("MmapStatus")) {
732 *status = 0;
733 return true;
734 }
735
736 const char* kMmapStatusSql = "SELECT * FROM MmapStatus";
737 Statement s(GetUniqueStatement(kMmapStatusSql));
738 if (s.Step())
739 *status = s.ColumnInt64(0);
740 return s.Succeeded();
741}
742
Victor Costancfbfa602018-08-01 23:24:46743bool Database::SetMmapAltStatus(int64_t status) {
shessa62504d2016-11-07 19:26:12744 if (!BeginTransaction())
745 return false;
746
747 // View may not exist on first run.
748 if (!Execute("DROP VIEW IF EXISTS MmapStatus")) {
749 RollbackTransaction();
750 return false;
751 }
752
753 // Views live in the schema, so they cannot be parameterized. For an integer
754 // value, this construct should be safe from SQL injection, if the value
755 // becomes more complicated use "SELECT quote(?)" to generate a safe quoted
756 // value.
Victor Costancfbfa602018-08-01 23:24:46757 const std::string create_view_sql = base::StringPrintf(
758 "CREATE VIEW MmapStatus (value) AS SELECT %" PRId64, status);
759 if (!Execute(create_view_sql.c_str())) {
shessa62504d2016-11-07 19:26:12760 RollbackTransaction();
761 return false;
762 }
763
764 return CommitTransaction();
765}
766
Victor Costancfbfa602018-08-01 23:24:46767size_t Database::GetAppropriateMmapSize() {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54768 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
769 InitScopedBlockingCall(&scoped_blocking_call);
shessd90aeea82015-11-13 02:24:31770
shess9bf2c672015-12-18 01:18:08771 // How much to map if no errors are found. 50MB encompasses the 99th
772 // percentile of Chrome databases in the wild, so this should be good.
773 const size_t kMmapEverything = 256 * 1024 * 1024;
774
shessa62504d2016-11-07 19:26:12775 // Progress information is tracked in the [meta] table for databases which use
776 // sql::MetaTable, otherwise it is tracked in a special view.
777 // TODO(shess): Move all cases to the view implementation.
shess9bf2c672015-12-18 01:18:08778 int64_t mmap_ofs = 0;
shessa62504d2016-11-07 19:26:12779 if (mmap_alt_status_) {
780 if (!GetMmapAltStatus(&mmap_ofs)) {
781 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_READ);
782 return 0;
783 }
784 } else {
785 // If [meta] doesn't exist, yet, it's a new database, assume the best.
786 // sql::MetaTable::Init() will preload kMmapSuccess.
787 if (!MetaTable::DoesTableExist(this)) {
788 RecordOneEvent(EVENT_MMAP_META_MISSING);
789 return kMmapEverything;
790 }
791
792 if (!MetaTable::GetMmapStatus(this, &mmap_ofs)) {
793 RecordOneEvent(EVENT_MMAP_META_FAILURE_READ);
794 return 0;
795 }
shessd90aeea82015-11-13 02:24:31796 }
797
798 // Database read failed in the past, don't memory map.
shess9bf2c672015-12-18 01:18:08799 if (mmap_ofs == MetaTable::kMmapFailure) {
shessd90aeea82015-11-13 02:24:31800 RecordOneEvent(EVENT_MMAP_FAILED);
801 return 0;
Victor Costan5e785e32019-02-26 20:39:31802 }
803
804 if (mmap_ofs != MetaTable::kMmapSuccess) {
shessd90aeea82015-11-13 02:24:31805 // Continue reading from previous offset.
806 DCHECK_GE(mmap_ofs, 0);
807
808 // TODO(shess): Could this reading code be shared with Preload()? It would
809 // require locking twice (this code wouldn't be able to access |db_size| so
810 // the helper would have to return amount read).
811
812 // Read more of the database looking for errors. The VFS interface is used
813 // to assure that the reads are valid for SQLite. |g_reads_allowed| is used
814 // to limit checking to 20MB per run of Chromium.
Victor Costanbd623112018-07-18 04:17:27815 sqlite3_file* file = nullptr;
shessd90aeea82015-11-13 02:24:31816 sqlite3_int64 db_size = 0;
817 if (SQLITE_OK != GetSqlite3FileAndSize(db_, &file, &db_size)) {
818 RecordOneEvent(EVENT_MMAP_VFS_FAILURE);
819 return 0;
820 }
821
822 // Read the data left, or |g_reads_allowed|, whichever is smaller.
823 // |g_reads_allowed| limits the total amount of I/O to spend verifying data
824 // in a single Chromium run.
825 sqlite3_int64 amount = db_size - mmap_ofs;
826 if (amount < 0)
827 amount = 0;
828 if (amount > 0) {
Victor Costan3653df62018-02-08 21:38:16829 static base::NoDestructor<base::Lock> lock;
830 base::AutoLock auto_lock(*lock);
shessd90aeea82015-11-13 02:24:31831 static sqlite3_int64 g_reads_allowed = 20 * 1024 * 1024;
832 if (g_reads_allowed < amount)
833 amount = g_reads_allowed;
834 g_reads_allowed -= amount;
835 }
836
837 // |amount| can be <= 0 if |g_reads_allowed| ran out of quota, or if the
838 // database was truncated after a previous pass.
839 if (amount <= 0 && mmap_ofs < db_size) {
840 DCHECK_EQ(0, amount);
shessd90aeea82015-11-13 02:24:31841 } else {
842 static const int kPageSize = 4096;
843 char buf[kPageSize];
844 while (amount > 0) {
845 int rc = file->pMethods->xRead(file, buf, sizeof(buf), mmap_ofs);
846 if (rc == SQLITE_OK) {
847 mmap_ofs += sizeof(buf);
848 amount -= sizeof(buf);
849 } else if (rc == SQLITE_IOERR_SHORT_READ) {
850 // Reached EOF for a database with page size < |kPageSize|.
851 mmap_ofs = db_size;
852 break;
853 } else {
854 // TODO(shess): Consider calling OnSqliteError().
shess9bf2c672015-12-18 01:18:08855 mmap_ofs = MetaTable::kMmapFailure;
shessd90aeea82015-11-13 02:24:31856 break;
857 }
858 }
859
860 // Log these events after update to distinguish meta update failure.
shessd90aeea82015-11-13 02:24:31861 if (mmap_ofs >= db_size) {
shess9bf2c672015-12-18 01:18:08862 mmap_ofs = MetaTable::kMmapSuccess;
shessd90aeea82015-11-13 02:24:31863 } else {
Victor Costan5e785e32019-02-26 20:39:31864 DCHECK(mmap_ofs > 0 || mmap_ofs == MetaTable::kMmapFailure);
shessd90aeea82015-11-13 02:24:31865 }
866
shessa62504d2016-11-07 19:26:12867 if (mmap_alt_status_) {
868 if (!SetMmapAltStatus(mmap_ofs)) {
869 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_UPDATE);
870 return 0;
871 }
872 } else {
873 if (!MetaTable::SetMmapStatus(this, mmap_ofs)) {
874 RecordOneEvent(EVENT_MMAP_META_FAILURE_UPDATE);
875 return 0;
876 }
shessd90aeea82015-11-13 02:24:31877 }
878
Victor Costan5e785e32019-02-26 20:39:31879 if (mmap_ofs == MetaTable::kMmapFailure)
880 RecordOneEvent(EVENT_MMAP_FAILED_NEW);
shessd90aeea82015-11-13 02:24:31881 }
882 }
883
shess9bf2c672015-12-18 01:18:08884 if (mmap_ofs == MetaTable::kMmapFailure)
shessd90aeea82015-11-13 02:24:31885 return 0;
shess9bf2c672015-12-18 01:18:08886 if (mmap_ofs == MetaTable::kMmapSuccess)
887 return kMmapEverything;
shessd90aeea82015-11-13 02:24:31888 return mmap_ofs;
889}
890
Victor Costan52bef812018-12-05 07:41:49891void Database::TrimMemory() {
[email protected]be7995f12013-07-18 18:49:14892 if (!db_)
893 return;
894
Victor Costan52bef812018-12-05 07:41:49895 sqlite3_db_release_memory(db_);
[email protected]be7995f12013-07-18 18:49:14896
Victor Costan52bef812018-12-05 07:41:49897 // It is tempting to use sqlite3_release_memory() here as well. However, the
898 // API is documented to be a no-op unless SQLite is built with
899 // SQLITE_ENABLE_MEMORY_MANAGEMENT. We do not use this option, because it is
900 // incompatible with per-database page cache pools. Behind the scenes,
901 // SQLITE_ENABLE_MEMORY_MANAGEMENT causes SQLite to use a global page cache
902 // pool, and sqlite3_release_memory() releases unused pages from this global
903 // pool.
[email protected]be7995f12013-07-18 18:49:14904}
905
[email protected]8e0c01282012-04-06 19:36:49906// Create an in-memory database with the existing database's page
907// size, then backup that database over the existing database.
Victor Costancfbfa602018-08-01 23:24:46908bool Database::Raze() {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54909 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
910 InitScopedBlockingCall(&scoped_blocking_call);
[email protected]35f7e5392012-07-27 19:54:50911
[email protected]8e0c01282012-04-06 19:36:49912 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52913 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49914 return false;
915 }
916
917 if (transaction_nesting_ > 0) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52918 DLOG(DCHECK) << "Cannot raze within a transaction";
[email protected]8e0c01282012-04-06 19:36:49919 return false;
920 }
921
Victor Costancfbfa602018-08-01 23:24:46922 sql::Database null_db;
[email protected]8e0c01282012-04-06 19:36:49923 if (!null_db.OpenInMemory()) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52924 DLOG(DCHECK) << "Unable to open in-memory database.";
[email protected]8e0c01282012-04-06 19:36:49925 return false;
926 }
927
Victor Costan7f6abbbe2018-07-29 02:57:27928 const std::string sql = base::StringPrintf("PRAGMA page_size=%d", page_size_);
929 if (!null_db.Execute(sql.c_str()))
930 return false;
[email protected]69c58452012-08-06 19:22:42931
[email protected]6d42f152012-11-10 00:38:24932#if defined(OS_ANDROID)
933 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
934 // in-memory databases do not respect this define.
935 // TODO(shess): Figure out a way to set this without using platform
936 // specific code. AFAICT from sqlite3.c, the only way to do it
937 // would be to create an actual filesystem database, which is
938 // unfortunate.
939 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
940 return false;
941#endif
[email protected]8e0c01282012-04-06 19:36:49942
943 // The page size doesn't take effect until a database has pages, and
944 // at this point the null database has none. Changing the schema
945 // version will create the first page. This will not affect the
946 // schema version in the resulting database, as SQLite's backup
947 // implementation propagates the schema version from the original
Victor Costancfbfa602018-08-01 23:24:46948 // database to the new version of the database, incremented by one
[email protected]8e0c01282012-04-06 19:36:49949 // so that other readers see the schema change and act accordingly.
950 if (!null_db.Execute("PRAGMA schema_version = 1"))
951 return false;
952
[email protected]6d42f152012-11-10 00:38:24953 // SQLite tracks the expected number of database pages in the first
954 // page, and if it does not match the total retrieved from a
955 // filesystem call, treats the database as corrupt. This situation
956 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
957 // used to hint to SQLite to soldier on in that case, specifically
958 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
959 // sqlite3.c lockBtree().]
960 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
961 // page_size" can be used to query such a database.
962 ScopedWritableSchema writable_schema(db_);
963
shess92a6fb22017-04-23 04:33:30964#if defined(OS_WIN)
965 // On Windows, truncate silently fails when applied to memory-mapped files.
966 // Disable memory-mapping so that the truncate succeeds. Note that other
Victor Costancfbfa602018-08-01 23:24:46967 // Database connections may have memory-mapped the file, so this may not
968 // entirely prevent the problem.
shess92a6fb22017-04-23 04:33:30969 // [Source: <https://sqlite.org/mmap.html> plus experiments.]
970 ignore_result(Execute("PRAGMA mmap_size = 0"));
971#endif
972
[email protected]7bae5742013-07-10 20:46:16973 const char* kMain = "main";
974 int rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:18975 base::UmaHistogramSparse("Sqlite.RazeDatabase", rc);
[email protected]8e0c01282012-04-06 19:36:49976
977 // The destination database was locked.
978 if (rc == SQLITE_BUSY) {
979 return false;
980 }
981
[email protected]7bae5742013-07-10 20:46:16982 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
983 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
984 // isn't even big enough for one page. Either way, reach in and
985 // truncate it before trying again.
986 // TODO(shess): Maybe it would be worthwhile to just truncate from
987 // the get-go?
988 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
Victor Costanbd623112018-07-18 04:17:27989 sqlite3_file* file = nullptr;
[email protected]8ada10f2013-12-21 00:42:34990 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:16991 if (rc != SQLITE_OK) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52992 DLOG(DCHECK) << "Failure getting file handle.";
[email protected]7bae5742013-07-10 20:46:16993 return false;
[email protected]7bae5742013-07-10 20:46:16994 }
995
996 rc = file->pMethods->xTruncate(file, 0);
997 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:18998 base::UmaHistogramSparse("Sqlite.RazeDatabaseTruncate", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52999 DLOG(DCHECK) << "Failed to truncate file.";
[email protected]7bae5742013-07-10 20:46:161000 return false;
1001 }
1002
1003 rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:181004 base::UmaHistogramSparse("Sqlite.RazeDatabase2", rc);
[email protected]7bae5742013-07-10 20:46:161005
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521006 DCHECK_EQ(rc, SQLITE_DONE) << "Failed retrying Raze().";
[email protected]7bae5742013-07-10 20:46:161007 }
1008
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521009 // TODO(shess): Figure out which other cases can happen.
1010 DCHECK_EQ(rc, SQLITE_DONE) << "Unable to copy entire null database.";
1011
[email protected]8e0c01282012-04-06 19:36:491012 // The entire database should have been backed up.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521013 return rc == SQLITE_DONE;
[email protected]8e0c01282012-04-06 19:36:491014}
1015
Victor Costancfbfa602018-08-01 23:24:461016bool Database::RazeAndClose() {
[email protected]41a97c812013-02-07 02:35:381017 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521018 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]41a97c812013-02-07 02:35:381019 return false;
1020 }
1021
1022 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:301023 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:381024
1025 bool result = Raze();
1026
1027 CloseInternal(true);
1028
1029 // Mark the database so that future API calls fail appropriately,
1030 // but don't DCHECK (because after calling this function they are
1031 // expected to fail).
1032 poisoned_ = true;
1033
1034 return result;
1035}
1036
Victor Costancfbfa602018-08-01 23:24:461037void Database::Poison() {
[email protected]8d409412013-07-19 18:25:301038 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521039 DCHECK(poisoned_) << "Cannot poison null db";
[email protected]8d409412013-07-19 18:25:301040 return;
1041 }
1042
1043 RollbackAllTransactions();
1044 CloseInternal(true);
1045
1046 // Mark the database so that future API calls fail appropriately,
1047 // but don't DCHECK (because after calling this function they are
1048 // expected to fail).
1049 poisoned_ = true;
1050}
1051
[email protected]8d2e39e2013-06-24 05:55:081052// TODO(shess): To the extent possible, figure out the optimal
Victor Costancfbfa602018-08-01 23:24:461053// ordering for these deletes which will prevent other Database connections
[email protected]8d2e39e2013-06-24 05:55:081054// from seeing odd behavior. For instance, it may be necessary to
1055// manually lock the main database file in a SQLite-compatible fashion
1056// (to prevent other processes from opening it), then delete the
1057// journal files, then delete the main database file. Another option
1058// might be to lock the main database file and poison the header with
1059// junk to prevent other processes from opening it successfully (like
1060// Gears "SQLite poison 3" trick).
1061//
1062// static
Victor Costancfbfa602018-08-01 23:24:461063bool Database::Delete(const base::FilePath& path) {
Etienne Bergeron436d42212019-02-26 17:15:121064 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
1065 base::BlockingType::MAY_BLOCK);
[email protected]8d2e39e2013-06-24 05:55:081066
Victor Costancfbfa602018-08-01 23:24:461067 base::FilePath journal_path = Database::JournalPath(path);
1068 base::FilePath wal_path = Database::WriteAheadLogPath(path);
[email protected]8d2e39e2013-06-24 05:55:081069
erg102ceb412015-06-20 01:38:131070 std::string journal_str = AsUTF8ForSQL(journal_path);
1071 std::string wal_str = AsUTF8ForSQL(wal_path);
1072 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081073
Victor Costan3653df62018-02-08 21:38:161074 EnsureSqliteInitialized();
shess702467622015-09-16 19:04:551075
Victor Costanbd623112018-07-18 04:17:271076 sqlite3_vfs* vfs = sqlite3_vfs_find(nullptr);
erg102ceb412015-06-20 01:38:131077 CHECK(vfs);
1078 CHECK(vfs->xDelete);
1079 CHECK(vfs->xAccess);
1080
1081 // We only work with unix, win32 and mojo filesystems. If you're trying to
1082 // use this code with any other VFS, you're not in a good place.
1083 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1084 strncmp(vfs->zName, "win32", 5) == 0 ||
1085 strcmp(vfs->zName, "mojo") == 0);
1086
1087 vfs->xDelete(vfs, journal_str.c_str(), 0);
1088 vfs->xDelete(vfs, wal_str.c_str(), 0);
1089 vfs->xDelete(vfs, path_str.c_str(), 0);
1090
1091 int journal_exists = 0;
Victor Costance678e72018-07-24 10:25:001092 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS, &journal_exists);
erg102ceb412015-06-20 01:38:131093
1094 int wal_exists = 0;
Victor Costance678e72018-07-24 10:25:001095 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS, &wal_exists);
erg102ceb412015-06-20 01:38:131096
1097 int path_exists = 0;
Victor Costance678e72018-07-24 10:25:001098 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS, &path_exists);
erg102ceb412015-06-20 01:38:131099
1100 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081101}
1102
Victor Costancfbfa602018-08-01 23:24:461103bool Database::BeginTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561104 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331105 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561106
1107 // When we're going to rollback, fail on this begin and don't actually
1108 // mark us as entering the nested transaction.
1109 return false;
1110 }
1111
1112 bool success = true;
1113 if (!transaction_nesting_) {
1114 needs_rollback_ = false;
1115
1116 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
[email protected]eff1fa522011-12-12 23:50:591117 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561118 return false;
1119 }
1120 transaction_nesting_++;
1121 return success;
1122}
1123
Victor Costancfbfa602018-08-01 23:24:461124void Database::RollbackTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561125 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521126 DCHECK(poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561127 return;
1128 }
1129
1130 transaction_nesting_--;
1131
1132 if (transaction_nesting_ > 0) {
1133 // Mark the outermost transaction as needing rollback.
1134 needs_rollback_ = true;
1135 return;
1136 }
1137
1138 DoRollback();
1139}
1140
Victor Costancfbfa602018-08-01 23:24:461141bool Database::CommitTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561142 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521143 DCHECK(poisoned_) << "Committing a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561144 return false;
1145 }
1146 transaction_nesting_--;
1147
1148 if (transaction_nesting_ > 0) {
1149 // Mark any nested transactions as failing after we've already got one.
1150 return !needs_rollback_;
1151 }
1152
1153 if (needs_rollback_) {
1154 DoRollback();
1155 return false;
1156 }
1157
1158 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321159
Victor Costan5e785e32019-02-26 20:39:311160 bool succeeded = commit.Run();
shess58b8df82015-06-03 00:19:321161
shess7dbd4dee2015-10-06 17:39:161162 // Release dirty cache pages after the transaction closes.
1163 ReleaseCacheMemoryIfNeeded(false);
1164
Victor Costan5e785e32019-02-26 20:39:311165 return succeeded;
[email protected]e5ffd0e42009-09-11 21:30:561166}
1167
Victor Costancfbfa602018-08-01 23:24:461168void Database::RollbackAllTransactions() {
[email protected]8d409412013-07-19 18:25:301169 if (transaction_nesting_ > 0) {
1170 transaction_nesting_ = 0;
1171 DoRollback();
1172 }
1173}
1174
Victor Costancfbfa602018-08-01 23:24:461175bool Database::AttachDatabase(const base::FilePath& other_db_path,
1176 const char* attachment_point,
1177 InternalApiToken) {
[email protected]8d409412013-07-19 18:25:301178 DCHECK(ValidAttachmentPoint(attachment_point));
1179
1180 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1181#if OS_WIN
1182 s.BindString16(0, other_db_path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181183#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
Fabrice de Gans-Riberibd1301f2018-05-18 21:00:091184 s.BindString(0, other_db_path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181185#else
1186#error Unsupported platform
[email protected]8d409412013-07-19 18:25:301187#endif
1188 s.BindString(1, attachment_point);
1189 return s.Run();
1190}
1191
Victor Costancfbfa602018-08-01 23:24:461192bool Database::DetachDatabase(const char* attachment_point, InternalApiToken) {
[email protected]8d409412013-07-19 18:25:301193 DCHECK(ValidAttachmentPoint(attachment_point));
1194
1195 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1196 s.BindString(0, attachment_point);
1197 return s.Run();
1198}
1199
shess58b8df82015-06-03 00:19:321200// TODO(shess): Consider changing this to execute exactly one statement. If a
1201// caller wishes to execute multiple statements, that should be explicit, and
1202// perhaps tucked into an explicit transaction with rollback in case of error.
Victor Costancfbfa602018-08-01 23:24:461203int Database::ExecuteAndReturnErrorCode(const char* sql) {
Victor Costan5e785e32019-02-26 20:39:311204 DCHECK(sql);
Etienne Pierre-Doraya71d7af2019-02-07 02:07:541205
[email protected]41a97c812013-02-07 02:35:381206 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461207 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381208 return SQLITE_ERROR;
1209 }
shess58b8df82015-06-03 00:19:321210
Victor Costan5e785e32019-02-26 20:39:311211 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
1212 InitScopedBlockingCall(&scoped_blocking_call);
1213
shess58b8df82015-06-03 00:19:321214 int rc = SQLITE_OK;
1215 while ((rc == SQLITE_OK) && *sql) {
Victor Costanbd623112018-07-18 04:17:271216 sqlite3_stmt* stmt = nullptr;
Victor Costancfbfa602018-08-01 23:24:461217 const char* leftover_sql;
shess58b8df82015-06-03 00:19:321218
shess58b8df82015-06-03 00:19:321219 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
1220 sql = leftover_sql;
1221
1222 // Stop if an error is encountered.
1223 if (rc != SQLITE_OK)
1224 break;
1225
1226 // This happens if |sql| originally only contained comments or whitespace.
1227 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1228 // extraneous comments and whitespace in the SQL statements increases
1229 // runtime cost and can easily be shifted out to the C++ layer.
1230 if (!stmt)
1231 continue;
1232
shess58b8df82015-06-03 00:19:321233 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1234 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
Victor Costan5e785e32019-02-26 20:39:311235 // is the only legitimate case for this. Previously recorded histograms
1236 // show significant use of this code path.
shess58b8df82015-06-03 00:19:321237 }
1238
1239 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1240 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
1241 rc = sqlite3_finalize(stmt);
shess58b8df82015-06-03 00:19:321242
1243 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1244 // trailing whitespace.
1245 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021246 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321247 sql++;
1248 }
shess58b8df82015-06-03 00:19:321249 }
shess7dbd4dee2015-10-06 17:39:161250
1251 // Most calls to Execute() modify the database. The main exceptions would be
1252 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1253 // but sometimes don't.
1254 ReleaseCacheMemoryIfNeeded(true);
1255
shess58b8df82015-06-03 00:19:321256 return rc;
[email protected]eff1fa522011-12-12 23:50:591257}
1258
Victor Costancfbfa602018-08-01 23:24:461259bool Database::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:381260 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461261 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381262 return false;
1263 }
1264
[email protected]eff1fa522011-12-12 23:50:591265 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001266 if (error != SQLITE_OK)
Victor Costanbd623112018-07-18 04:17:271267 error = OnSqliteError(error, nullptr, sql);
[email protected]473ad792012-11-10 00:55:001268
[email protected]28fe0ff2012-02-25 00:40:331269 // This needs to be a FATAL log because the error case of arriving here is
1270 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101271 // a change alters the schema but not all queries adjust. This can happen
1272 // in production if the schema is corrupted.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521273 DCHECK_NE(error, SQLITE_ERROR)
1274 << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591275 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561276}
1277
Victor Costancfbfa602018-08-01 23:24:461278bool Database::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:381279 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461280 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]5b96f3772010-09-28 16:30:571281 return false;
[email protected]41a97c812013-02-07 02:35:381282 }
[email protected]5b96f3772010-09-28 16:30:571283
1284 ScopedBusyTimeout busy_timeout(db_);
1285 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591286 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571287}
1288
Victor Costancfbfa602018-08-01 23:24:461289scoped_refptr<Database::StatementRef> Database::GetCachedStatement(
Victor Costan12daa3ac92018-07-19 01:05:581290 StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:561291 const char* sql) {
Victor Costanc7e7f2e2018-07-18 20:07:551292 auto it = statement_cache_.find(id);
1293 if (it != statement_cache_.end()) {
Victor Costan613b4302018-11-20 05:32:431294 // Statement is in the cache. It should still be valid. We're the only
1295 // entity invalidating cached statements, and we remove them from the cache
Victor Costan87cf8c72018-07-19 19:36:041296 // when we do that.
Victor Costanc7e7f2e2018-07-18 20:07:551297 DCHECK(it->second->is_valid());
Victor Costan613b4302018-11-20 05:32:431298 DCHECK_EQ(std::string(sqlite3_sql(it->second->stmt())), std::string(sql))
Victor Costan87cf8c72018-07-19 19:36:041299 << "GetCachedStatement used with same ID but different SQL";
1300
1301 // Reset the statement so it can be reused.
Victor Costanc7e7f2e2018-07-18 20:07:551302 sqlite3_reset(it->second->stmt());
1303 return it->second;
[email protected]e5ffd0e42009-09-11 21:30:561304 }
1305
1306 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
Victor Costan613b4302018-11-20 05:32:431307 if (statement->is_valid()) {
[email protected]e5ffd0e42009-09-11 21:30:561308 statement_cache_[id] = statement; // Only cache valid statements.
Victor Costan613b4302018-11-20 05:32:431309 DCHECK_EQ(std::string(sqlite3_sql(statement->stmt())), std::string(sql))
1310 << "Input SQL does not match SQLite's normalized version";
1311 }
[email protected]e5ffd0e42009-09-11 21:30:561312 return statement;
1313}
1314
Victor Costancfbfa602018-08-01 23:24:461315scoped_refptr<Database::StatementRef> Database::GetUniqueStatement(
[email protected]e5ffd0e42009-09-11 21:30:561316 const char* sql) {
shess9e77283d2016-06-13 23:53:201317 return GetStatementImpl(this, sql);
1318}
1319
Victor Costancfbfa602018-08-01 23:24:461320scoped_refptr<Database::StatementRef> Database::GetStatementImpl(
1321 sql::Database* tracking_db,
1322 const char* sql) const {
shess9e77283d2016-06-13 23:53:201323 DCHECK(sql);
Victor Costan87cf8c72018-07-19 19:36:041324 DCHECK(!tracking_db || tracking_db == this);
[email protected]35f7e5392012-07-27 19:54:501325
[email protected]41a97c812013-02-07 02:35:381326 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561327 if (!db_)
Victor Costan3b02cdf2018-07-18 00:39:561328 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561329
Victor Costan5e785e32019-02-26 20:39:311330 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
1331 InitScopedBlockingCall(&scoped_blocking_call);
1332
Victor Costanbd623112018-07-18 04:17:271333 sqlite3_stmt* stmt = nullptr;
1334 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
[email protected]473ad792012-11-10 00:55:001335 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591336 // This is evidence of a syntax error in the incoming SQL.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521337 DCHECK_NE(rc, SQLITE_ERROR) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001338
1339 // It could also be database corruption.
Victor Costanbd623112018-07-18 04:17:271340 OnSqliteError(rc, nullptr, sql);
Victor Costan3b02cdf2018-07-18 00:39:561341 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, false);
[email protected]e5ffd0e42009-09-11 21:30:561342 }
Victor Costan3b02cdf2018-07-18 00:39:561343 return base::MakeRefCounted<StatementRef>(tracking_db, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561344}
1345
Victor Costancfbfa602018-08-01 23:24:461346scoped_refptr<Database::StatementRef> Database::GetUntrackedStatement(
[email protected]2eec0a22012-07-24 01:59:581347 const char* sql) const {
Victor Costanbd623112018-07-18 04:17:271348 return GetStatementImpl(nullptr, sql);
[email protected]2eec0a22012-07-24 01:59:581349}
1350
Victor Costancfbfa602018-08-01 23:24:461351std::string Database::GetSchema() const {
[email protected]92cd00a2013-08-16 11:09:581352 // The ORDER BY should not be necessary, but relying on organic
1353 // order for something like this is questionable.
Victor Costan87cf8c72018-07-19 19:36:041354 static const char kSql[] =
[email protected]92cd00a2013-08-16 11:09:581355 "SELECT type, name, tbl_name, sql "
1356 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1357 Statement statement(GetUntrackedStatement(kSql));
1358
1359 std::string schema;
1360 while (statement.Step()) {
1361 schema += statement.ColumnString(0);
1362 schema += '|';
1363 schema += statement.ColumnString(1);
1364 schema += '|';
1365 schema += statement.ColumnString(2);
1366 schema += '|';
1367 schema += statement.ColumnString(3);
1368 schema += '\n';
1369 }
1370
1371 return schema;
1372}
1373
Victor Costancfbfa602018-08-01 23:24:461374bool Database::IsSQLValid(const char* sql) {
Etienne Pierre-Doraya71d7af2019-02-07 02:07:541375 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
1376 InitScopedBlockingCall(&scoped_blocking_call);
[email protected]41a97c812013-02-07 02:35:381377 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461378 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381379 return false;
1380 }
1381
Victor Costanbd623112018-07-18 04:17:271382 sqlite3_stmt* stmt = nullptr;
1383 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK)
[email protected]eff1fa522011-12-12 23:50:591384 return false;
1385
1386 sqlite3_finalize(stmt);
1387 return true;
1388}
1389
Victor Costancfbfa602018-08-01 23:24:461390bool Database::DoesIndexExist(const char* index_name) const {
shessa62504d2016-11-07 19:26:121391 return DoesSchemaItemExist(index_name, "index");
[email protected]e2cadec82011-12-13 02:00:531392}
1393
Victor Costancfbfa602018-08-01 23:24:461394bool Database::DoesTableExist(const char* table_name) const {
shessa62504d2016-11-07 19:26:121395 return DoesSchemaItemExist(table_name, "table");
1396}
1397
Victor Costancfbfa602018-08-01 23:24:461398bool Database::DoesViewExist(const char* view_name) const {
shessa62504d2016-11-07 19:26:121399 return DoesSchemaItemExist(view_name, "view");
1400}
1401
Victor Costancfbfa602018-08-01 23:24:461402bool Database::DoesSchemaItemExist(const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471403 const char* kSql =
1404 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581405 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471406
shess976814402016-06-21 06:56:251407 // This can happen if the database is corrupt and the error is a test
1408 // expectation.
shess92a2ab12015-04-09 01:59:471409 if (!statement.is_valid())
1410 return false;
1411
[email protected]e2cadec82011-12-13 02:00:531412 statement.BindString(0, type);
1413 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331414
[email protected]e5ffd0e42009-09-11 21:30:561415 return statement.Step(); // Table exists if any row was returned.
1416}
1417
Victor Costancfbfa602018-08-01 23:24:461418bool Database::DoesColumnExist(const char* table_name,
1419 const char* column_name) const {
Victor Costan1ff47e92018-12-07 11:10:431420 // sqlite3_table_column_metadata uses out-params to return column definition
1421 // details, such as the column type and whether it allows NULL values. These
1422 // aren't needed to compute the current method's result, so we pass in nullptr
1423 // for all the out-params.
1424 int error = sqlite3_table_column_metadata(
1425 db_, "main", table_name, column_name, /* pzDataType= */ nullptr,
1426 /* pzCollSeq= */ nullptr, /* pNotNull= */ nullptr,
1427 /* pPrimaryKey= */ nullptr, /* pAutoinc= */ nullptr);
1428 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561429}
1430
Victor Costancfbfa602018-08-01 23:24:461431int64_t Database::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561432 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461433 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]e5ffd0e42009-09-11 21:30:561434 return 0;
1435 }
1436 return sqlite3_last_insert_rowid(db_);
1437}
1438
Victor Costancfbfa602018-08-01 23:24:461439int Database::GetLastChangeCount() const {
[email protected]1ed78a32009-09-15 20:24:171440 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461441 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]1ed78a32009-09-15 20:24:171442 return 0;
1443 }
1444 return sqlite3_changes(db_);
1445}
1446
Victor Costancfbfa602018-08-01 23:24:461447int Database::GetErrorCode() const {
[email protected]e5ffd0e42009-09-11 21:30:561448 if (!db_)
1449 return SQLITE_ERROR;
1450 return sqlite3_errcode(db_);
1451}
1452
Victor Costancfbfa602018-08-01 23:24:461453int Database::GetLastErrno() const {
[email protected]767718e52010-09-21 23:18:491454 if (!db_)
1455 return -1;
1456
1457 int err = 0;
Victor Costanbd623112018-07-18 04:17:271458 if (SQLITE_OK != sqlite3_file_control(db_, nullptr, SQLITE_LAST_ERRNO, &err))
[email protected]767718e52010-09-21 23:18:491459 return -2;
1460
1461 return err;
1462}
1463
Victor Costancfbfa602018-08-01 23:24:461464const char* Database::GetErrorMessage() const {
[email protected]e5ffd0e42009-09-11 21:30:561465 if (!db_)
Victor Costancfbfa602018-08-01 23:24:461466 return "sql::Database is not opened.";
[email protected]e5ffd0e42009-09-11 21:30:561467 return sqlite3_errmsg(db_);
1468}
1469
Victor Costancfbfa602018-08-01 23:24:461470bool Database::OpenInternal(const std::string& file_name,
1471 Database::Retry retry_flag) {
[email protected]9cfbc922009-11-17 20:13:171472 if (db_) {
Victor Costancfbfa602018-08-01 23:24:461473 DLOG(DCHECK) << "sql::Database is already open.";
[email protected]9cfbc922009-11-17 20:13:171474 return false;
1475 }
1476
Victor Costan5e785e32019-02-26 20:39:311477 base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
1478 InitScopedBlockingCall(&scoped_blocking_call);
1479
Victor Costan3653df62018-02-08 21:38:161480 EnsureSqliteInitialized();
[email protected]a7ec1292013-07-22 22:02:181481
shess58b8df82015-06-03 00:19:321482 // Setup the stats histograms immediately rather than allocating lazily.
Victor Costancfbfa602018-08-01 23:24:461483 // Databases which won't exercise all of these probably shouldn't exist.
shess58b8df82015-06-03 00:19:321484 if (!histogram_tag_.empty()) {
Victor Costancfbfa602018-08-01 23:24:461485 stats_histogram_ = base::LinearHistogram::FactoryGet(
Victor Costanb0e7fc02019-03-01 03:36:271486 "Sqlite.Stats2." + histogram_tag_, 1, EVENT_MAX_VALUE,
Victor Costancfbfa602018-08-01 23:24:461487 EVENT_MAX_VALUE + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
shess58b8df82015-06-03 00:19:321488 }
1489
[email protected]41a97c812013-02-07 02:35:381490 // If |poisoned_| is set, it means an error handler called
1491 // RazeAndClose(). Until regular Close() is called, the caller
1492 // should be treating the database as open, but is_open() currently
1493 // only considers the sqlite3 handle's state.
1494 // TODO(shess): Revise is_open() to consider poisoned_, and review
1495 // to see if any non-testing code even depends on it.
Victor Costancfbfa602018-08-01 23:24:461496 DCHECK(!poisoned_) << "sql::Database is already open.";
[email protected]7bae5742013-07-10 20:46:161497 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381498
shess5f2c3442017-01-24 02:15:101499 // Custom memory-mapping VFS which reads pages using regular I/O on first hit.
1500 sqlite3_vfs* vfs = VFSWrapper();
1501 const char* vfs_name = (vfs ? vfs->zName : nullptr);
Victor Costanc6d3a862018-11-20 18:41:231502
1503 // The flags are documented at https://www.sqlite.org/c3ref/open.html.
1504 //
1505 // Chrome uses SQLITE_OPEN_PRIVATECACHE because SQLite is used by many
1506 // disparate features with their own databases, and having separate page
1507 // caches makes it easier to reason about each feature's performance in
1508 // isolation.
1509 int err = sqlite3_open_v2(
1510 file_name.c_str(), &db_,
1511 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_PRIVATECACHE,
1512 vfs_name);
[email protected]765b44502009-10-02 05:01:421513 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281514 // Extended error codes cannot be enabled until a handle is
1515 // available, fetch manually.
1516 err = sqlite3_extended_errcode(db_);
1517
[email protected]bd2ccdb4a2012-12-07 22:14:501518 // Histogram failures specific to initial open for debugging
1519 // purposes.
Ilya Sherman1c811db2017-12-14 10:36:181520 base::UmaHistogramSparse("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501521
Victor Costanbd623112018-07-18 04:17:271522 OnSqliteError(err, nullptr, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131523 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291524 Close();
[email protected]fed734a2013-07-17 04:45:131525
1526 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1527 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421528 return false;
1529 }
1530
[email protected]73fb8d52013-07-24 05:04:281531 // Enable extended result codes to provide more color on I/O errors.
1532 // Not having extended result codes is not a fatal problem, as
1533 // Chromium code does not attempt to handle I/O errors anyhow. The
1534 // current implementation always returns SQLITE_OK, the DCHECK is to
1535 // quickly notify someone if SQLite changes.
1536 err = sqlite3_extended_result_codes(db_, 1);
1537 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1538
shessbccd300e2016-08-20 00:06:561539 // sqlite3_open() does not actually read the database file (unless a hot
1540 // journal is found). Successfully executing this pragma on an existing
1541 // database requires a valid header on page 1. ExecuteAndReturnErrorCode() to
1542 // get the error code before error callback (potentially) overwrites.
[email protected]bd2ccdb4a2012-12-07 22:14:501543 // TODO(shess): For now, just probing to see what the lay of the
1544 // land is. If it's mostly SQLITE_NOTADB, then the database should
1545 // be razed.
1546 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
shessbccd300e2016-08-20 00:06:561547 if (err != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:181548 base::UmaHistogramSparse("Sqlite.OpenProbeFailure", err);
shessbccd300e2016-08-20 00:06:561549 OnSqliteError(err, nullptr, "PRAGMA auto_vacuum");
1550
1551 // Retry or bail out if the error handler poisoned the handle.
Victor Costanf1e9443b2018-12-05 04:35:531552 // TODO(shess): Move this handling to one place (see also sqlite3_open).
1553 // Possibly a wrapper function?
shessbccd300e2016-08-20 00:06:561554 if (poisoned_) {
1555 Close();
1556 if (retry_flag == RETRY_ON_POISON)
1557 return OpenInternal(file_name, NO_RETRY);
1558 return false;
1559 }
1560 }
[email protected]658f8332010-09-18 04:40:431561
[email protected]5b96f3772010-09-28 16:30:571562 // If indicated, lock up the database before doing anything else, so
1563 // that the following code doesn't have to deal with locking.
1564 // TODO(shess): This code is brittle. Find the cases where code
1565 // doesn't request |exclusive_locking_| and audit that it does the
1566 // right thing with SQLITE_BUSY, and that it doesn't make
1567 // assumptions about who might change things in the database.
1568 // http://crbug.com/56559
1569 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101570 // TODO(shess): This should probably be a failure. Code which
1571 // requests exclusive locking but doesn't get it is almost certain
1572 // to be ill-tested.
1573 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571574 }
1575
Victor Costan4c2f3e922018-08-21 04:47:591576 if (base::FeatureList::IsEnabled(features::kSqlTempStoreMemory)) {
1577 err = ExecuteAndReturnErrorCode("PRAGMA temp_store=MEMORY");
1578 // This operates on in-memory configuration, so it should not fail.
1579 DCHECK_EQ(err, SQLITE_OK) << "Failed switching to in-RAM temporary storage";
1580 }
1581
[email protected]4e179ba2012-03-17 16:06:471582 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1583 // DELETE (default) - delete -journal file to commit.
1584 // TRUNCATE - truncate -journal file to commit.
1585 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091586 // TRUNCATE should be faster than DELETE because it won't need directory
1587 // changes for each transaction. PERSIST may break the spirit of using
1588 // secure_delete.
Victor Costan4c2f3e922018-08-21 04:47:591589 ignore_result(Execute("PRAGMA journal_mode=TRUNCATE"));
[email protected]4e179ba2012-03-17 16:06:471590
[email protected]c68ce172011-11-24 22:30:271591 const base::TimeDelta kBusyTimeout =
Victor Costancfbfa602018-08-01 23:24:461592 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
[email protected]c68ce172011-11-24 22:30:271593
Victor Costancfbfa602018-08-01 23:24:461594 const std::string page_size_sql =
1595 base::StringPrintf("PRAGMA page_size=%d", page_size_);
1596 ignore_result(ExecuteWithTimeout(page_size_sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421597
1598 if (cache_size_ != 0) {
Victor Costancfbfa602018-08-01 23:24:461599 const std::string cache_size_sql =
[email protected]7d3cbc92013-03-18 22:33:041600 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
Victor Costancfbfa602018-08-01 23:24:461601 ignore_result(ExecuteWithTimeout(cache_size_sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421602 }
1603
Victor Costanf1e9443b2018-12-05 04:35:531604 static_assert(SQLITE_SECURE_DELETE == 1,
1605 "Chrome assumes secure_delete is on by default.");
[email protected]6e0b1442011-08-09 23:23:581606
shess5dac334f2015-11-05 20:47:421607 // Set a reasonable chunk size for larger files. This reduces churn from
1608 // remapping memory on size changes. It also reduces filesystem
1609 // fragmentation.
1610 // TODO(shess): It may make sense to have this be hinted by the client.
1611 // Database sizes seem to be bimodal, some clients have consistently small
1612 // databases (<20k) while other clients have a broad distribution of sizes
1613 // (hundreds of kilobytes to many megabytes).
Victor Costanbd623112018-07-18 04:17:271614 sqlite3_file* file = nullptr;
shess5dac334f2015-11-05 20:47:421615 sqlite3_int64 db_size = 0;
1616 int rc = GetSqlite3FileAndSize(db_, &file, &db_size);
1617 if (rc == SQLITE_OK && db_size > 16 * 1024) {
1618 int chunk_size = 4 * 1024;
1619 if (db_size > 128 * 1024)
1620 chunk_size = 32 * 1024;
Victor Costanbd623112018-07-18 04:17:271621 sqlite3_file_control(db_, nullptr, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size);
shess5dac334f2015-11-05 20:47:421622 }
1623
shess2f3a814b2015-11-05 18:11:101624 // Enable memory-mapped access. The explicit-disable case is because SQLite
shessd90aeea82015-11-13 02:24:311625 // can be built to default-enable mmap. GetAppropriateMmapSize() calculates a
1626 // safe range to memory-map based on past regular I/O. This value will be
1627 // capped by SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and
1628 // 64-bit platforms.
1629 size_t mmap_size = mmap_disabled_ ? 0 : GetAppropriateMmapSize();
1630 std::string mmap_sql =
Victor Costan4c2f3e922018-08-21 04:47:591631 base::StringPrintf("PRAGMA mmap_size=%" PRIuS, mmap_size);
shessd90aeea82015-11-13 02:24:311632 ignore_result(Execute(mmap_sql.c_str()));
shess2f3a814b2015-11-05 18:11:101633
1634 // Determine if memory-mapping has actually been enabled. The Execute() above
1635 // can succeed without changing the amount mapped.
1636 mmap_enabled_ = false;
1637 {
1638 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1639 if (s.Step() && s.ColumnInt64(0) > 0)
1640 mmap_enabled_ = true;
1641 }
1642
ssid3be5b1ec2016-01-13 14:21:571643 DCHECK(!memory_dump_provider_);
1644 memory_dump_provider_.reset(
Victor Costancfbfa602018-08-01 23:24:461645 new DatabaseMemoryDumpProvider(db_, histogram_tag_));
ssid3be5b1ec2016-01-13 14:21:571646 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
Victor Costancfbfa602018-08-01 23:24:461647 memory_dump_provider_.get(), "sql::Database", nullptr);
ssid3be5b1ec2016-01-13 14:21:571648
[email protected]765b44502009-10-02 05:01:421649 return true;
1650}
1651
Victor Costancfbfa602018-08-01 23:24:461652void Database::DoRollback() {
[email protected]e5ffd0e42009-09-11 21:30:561653 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321654
Victor Costan5e785e32019-02-26 20:39:311655 rollback.Run();
shess58b8df82015-06-03 00:19:321656
shess7dbd4dee2015-10-06 17:39:161657 // The cache may have been accumulating dirty pages for commit. Note that in
1658 // some cases sql::Transaction can fire rollback after a database is closed.
1659 if (is_open())
1660 ReleaseCacheMemoryIfNeeded(false);
1661
[email protected]44ad7d902012-03-23 00:09:051662 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561663}
1664
Victor Costancfbfa602018-08-01 23:24:461665void Database::StatementRefCreated(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551666 DCHECK(!open_statements_.count(ref))
1667 << __func__ << " already called with this statement";
[email protected]e5ffd0e42009-09-11 21:30:561668 open_statements_.insert(ref);
1669}
1670
Victor Costancfbfa602018-08-01 23:24:461671void Database::StatementRefDeleted(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551672 DCHECK(open_statements_.count(ref))
1673 << __func__ << " called with non-existing statement";
1674 open_statements_.erase(ref);
[email protected]e5ffd0e42009-09-11 21:30:561675}
1676
Victor Costancfbfa602018-08-01 23:24:461677void Database::set_histogram_tag(const std::string& tag) {
shess58b8df82015-06-03 00:19:321678 DCHECK(!is_open());
Victor Costan87cf8c72018-07-19 19:36:041679
shess58b8df82015-06-03 00:19:321680 histogram_tag_ = tag;
1681}
1682
Will Harrisb8693592018-08-28 22:58:441683void Database::AddTaggedHistogram(const std::string& name, int sample) const {
[email protected]210ce0af2013-05-15 09:10:391684 if (histogram_tag_.empty())
1685 return;
1686
1687 // TODO(shess): The histogram macros create a bit of static storage
1688 // for caching the histogram object. This code shouldn't execute
1689 // often enough for such caching to be crucial. If it becomes an
1690 // issue, the object could be cached alongside histogram_prefix_.
1691 std::string full_histogram_name = name + "." + histogram_tag_;
Victor Costancfbfa602018-08-01 23:24:461692 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
1693 full_histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]210ce0af2013-05-15 09:10:391694 if (histogram)
1695 histogram->Add(sample);
1696}
1697
Victor Costancfbfa602018-08-01 23:24:461698int Database::OnSqliteError(int err,
1699 sql::Statement* stmt,
1700 const char* sql) const {
Ilya Sherman1c811db2017-12-14 10:36:181701 base::UmaHistogramSparse("Sqlite.Error", err);
[email protected]210ce0af2013-05-15 09:10:391702 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141703
1704 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581705 if (!sql && stmt)
1706 sql = stmt->GetSQLStatement();
1707 if (!sql)
1708 sql = "-- unknown";
shessf7e988f2015-11-13 00:41:061709
1710 std::string id = histogram_tag_;
1711 if (id.empty())
1712 id = DbPath().BaseName().AsUTF8Unsafe();
Victor Costancfbfa602018-08-01 23:24:461713 LOG(ERROR) << id << " sqlite error " << err << ", errno " << GetLastErrno()
1714 << ": " << GetErrorMessage() << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141715
[email protected]c3881b372013-05-17 08:39:461716 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561717 // Fire from a copy of the callback in case of reentry into
1718 // re/set_error_callback().
1719 // TODO(shess): <http://crbug.com/254584>
1720 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461721 return err;
1722 }
1723
[email protected]faa604e2009-09-25 22:38:591724 // The default handling is to assert on debug and to ignore on release.
shess976814402016-06-21 06:56:251725 if (!IsExpectedSqliteError(err))
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521726 DLOG(DCHECK) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591727 return err;
1728}
1729
Victor Costancfbfa602018-08-01 23:24:461730bool Database::FullIntegrityCheck(std::vector<std::string>* messages) {
[email protected]579446c2013-12-16 18:36:521731 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1732}
1733
Victor Costancfbfa602018-08-01 23:24:461734bool Database::QuickIntegrityCheck() {
[email protected]579446c2013-12-16 18:36:521735 std::vector<std::string> messages;
1736 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1737 return false;
1738 return messages.size() == 1 && messages[0] == "ok";
1739}
1740
Victor Costancfbfa602018-08-01 23:24:461741std::string Database::GetDiagnosticInfo(int extended_error,
1742 Statement* statement) {
afakhry7c9abe72016-08-05 17:33:191743 // Prevent reentrant calls to the error callback.
1744 ErrorCallback original_callback = std::move(error_callback_);
1745 reset_error_callback();
1746
1747 // Trim extended error codes.
1748 const int error = (extended_error & 0xFF);
Victor Costancfbfa602018-08-01 23:24:461749 // CollectCorruptionInfo() is implemented in terms of sql::Database,
afakhry7c9abe72016-08-05 17:33:191750 // TODO(shess): Rewrite IntegrityCheckHelper() in terms of raw SQLite.
1751 std::string result = (error == SQLITE_CORRUPT)
1752 ? CollectCorruptionInfo()
1753 : CollectErrorInfo(extended_error, statement);
1754
1755 // The following queries must be executed after CollectErrorInfo() above, so
1756 // if they result in their own errors, they don't interfere with
1757 // CollectErrorInfo().
1758 const bool has_valid_header =
1759 (ExecuteAndReturnErrorCode("PRAGMA auto_vacuum") == SQLITE_OK);
1760 const bool select_sqlite_master_result =
1761 (ExecuteAndReturnErrorCode("SELECT COUNT(*) FROM sqlite_master") ==
1762 SQLITE_OK);
1763
1764 // Restore the original error callback.
1765 error_callback_ = std::move(original_callback);
1766
1767 base::StringAppendF(&result, "Has valid header: %s\n",
1768 (has_valid_header ? "Yes" : "No"));
1769 base::StringAppendF(&result, "Has valid schema: %s\n",
1770 (select_sqlite_master_result ? "Yes" : "No"));
1771
1772 return result;
1773}
1774
[email protected]80abf152013-05-22 12:42:421775// TODO(shess): Allow specifying maximum results (default 100 lines).
Victor Costancfbfa602018-08-01 23:24:461776bool Database::IntegrityCheckHelper(const char* pragma_sql,
1777 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421778 messages->clear();
1779
[email protected]4658e2a02013-06-06 23:05:001780 // This has the side effect of setting SQLITE_RecoveryMode, which
1781 // allows SQLite to process through certain cases of corruption.
1782 // Failing to set this pragma probably means that the database is
1783 // beyond recovery.
Victor Costan4c2f3e922018-08-21 04:47:591784 static const char kWritableSchemaSql[] = "PRAGMA writable_schema=ON";
Victor Costan1d868352018-06-26 19:06:481785 if (!Execute(kWritableSchemaSql))
[email protected]4658e2a02013-06-06 23:05:001786 return false;
1787
1788 bool ret = false;
1789 {
[email protected]579446c2013-12-16 18:36:521790 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001791
1792 // The pragma appears to return all results (up to 100 by default)
1793 // as a single string. This doesn't appear to be an API contract,
1794 // it could return separate lines, so loop _and_ split.
1795 while (stmt.Step()) {
1796 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181797 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1798 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001799 }
1800 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421801 }
[email protected]4658e2a02013-06-06 23:05:001802
1803 // Best effort to put things back as they were before.
Victor Costan4c2f3e922018-08-21 04:47:591804 static const char kNoWritableSchemaSql[] = "PRAGMA writable_schema=OFF";
Victor Costan1d868352018-06-26 19:06:481805 ignore_result(Execute(kNoWritableSchemaSql));
[email protected]4658e2a02013-06-06 23:05:001806
1807 return ret;
[email protected]80abf152013-05-22 12:42:421808}
1809
Victor Costancfbfa602018-08-01 23:24:461810bool Database::ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
1811 const std::string& dump_name) {
dskibab4199f82016-11-21 20:16:131812 return memory_dump_provider_ &&
ssid1f4e5362016-12-08 20:41:381813 memory_dump_provider_->ReportMemoryUsage(pmd, dump_name);
dskibab4199f82016-11-21 20:16:131814}
1815
[email protected]e5ffd0e42009-09-11 21:30:561816} // namespace sql