blob: ffe7c6bffb941b4f9bde7bf3b5c5ddf12c1dfdfb [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"
fdoray2dfa76452016-06-07 13:11:2224#include "base/single_thread_task_runner.h"
[email protected]80abf152013-05-22 12:42:4225#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1926#include "base/strings/string_util.h"
27#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4528#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1829#include "base/synchronization/lock.h"
Victor Costan87cf8c72018-07-19 19:36:0430#include "base/time/default_tick_clock.h"
ssid9f8022f2015-10-12 17:49:0331#include "base/trace_event/memory_dump_manager.h"
Kevin Marshalla9f05ec2017-07-14 02:10:0532#include "build/build_config.h"
Victor Costancfbfa602018-08-01 23:24:4633#include "sql/database_memory_dump_provider.h"
Victor Costan3653df62018-02-08 21:38:1634#include "sql/initialization.h"
shess9bf2c672015-12-18 01:18:0835#include "sql/meta_table.h"
Victor Costan4c2f3e922018-08-21 04:47:5936#include "sql/sql_features.h"
[email protected]f0a54b22011-07-19 18:40:2137#include "sql/statement.h"
shess5f2c3442017-01-24 02:15:1038#include "sql/vfs_wrapper.h"
[email protected]e33cba42010-08-18 23:37:0339#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5640
[email protected]5b96f3772010-09-28 16:30:5741namespace {
42
43// Spin for up to a second waiting for the lock to clear when setting
44// up the database.
45// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2746const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5747
48class ScopedBusyTimeout {
49 public:
Victor Costancfbfa602018-08-01 23:24:4650 explicit ScopedBusyTimeout(sqlite3* db) : db_(db) {}
51 ~ScopedBusyTimeout() { sqlite3_busy_timeout(db_, 0); }
[email protected]5b96f3772010-09-28 16:30:5752
53 int SetTimeout(base::TimeDelta timeout) {
54 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
55 return sqlite3_busy_timeout(db_,
56 static_cast<int>(timeout.InMilliseconds()));
57 }
58
59 private:
60 sqlite3* db_;
61};
62
[email protected]6d42f152012-11-10 00:38:2463// Helper to "safely" enable writable_schema. No error checking
64// because it is reasonable to just forge ahead in case of an error.
65// If turning it on fails, then most likely nothing will work, whereas
66// if turning it off fails, it only matters if some code attempts to
67// continue working with the database and tries to modify the
68// sqlite_master table (none of our code does this).
69class ScopedWritableSchema {
70 public:
Victor Costancfbfa602018-08-01 23:24:4671 explicit ScopedWritableSchema(sqlite3* db) : db_(db) {
Victor Costanbd623112018-07-18 04:17:2772 sqlite3_exec(db_, "PRAGMA writable_schema=1", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2473 }
74 ~ScopedWritableSchema() {
Victor Costanbd623112018-07-18 04:17:2775 sqlite3_exec(db_, "PRAGMA writable_schema=0", nullptr, nullptr, nullptr);
[email protected]6d42f152012-11-10 00:38:2476 }
77
78 private:
79 sqlite3* db_;
80};
81
[email protected]7bae5742013-07-10 20:46:1682// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
83// SQLite error code from running the backup step.
84int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
85 DCHECK_NE(src, dst);
86 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
87 if (!backup) {
88 // Since this call only sets things up, this indicates a gross
89 // error in SQLite.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:5290 DLOG(DCHECK) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
[email protected]7bae5742013-07-10 20:46:1691 return sqlite3_errcode(dst);
92 }
93
94 // -1 backs up the entire database.
95 int rc = sqlite3_backup_step(backup, -1);
96 int pages = sqlite3_backup_pagecount(backup);
97 sqlite3_backup_finish(backup);
98
99 // If successful, exactly one page should have been backed up. If
100 // this breaks, check this function to make sure assumptions aren't
101 // being broken.
102 if (rc == SQLITE_DONE)
103 DCHECK_EQ(pages, 1);
104
105 return rc;
106}
107
[email protected]8d409412013-07-19 18:25:30108// Be very strict on attachment point. SQLite can handle a much wider
109// character set with appropriate quoting, but Chromium code should
110// just use clean names to start with.
111bool ValidAttachmentPoint(const char* attachment_point) {
112 for (size_t i = 0; attachment_point[i]; ++i) {
zhongyi23960342016-04-12 23:13:20113 if (!(base::IsAsciiDigit(attachment_point[i]) ||
114 base::IsAsciiAlpha(attachment_point[i]) ||
[email protected]8d409412013-07-19 18:25:30115 attachment_point[i] == '_')) {
116 return false;
117 }
118 }
119 return true;
120}
121
[email protected]8ada10f2013-12-21 00:42:34122// Helper to get the sqlite3_file* associated with the "main" database.
123int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
Victor Costanbd623112018-07-18 04:17:27124 *file = nullptr;
125 int rc = sqlite3_file_control(db, nullptr, SQLITE_FCNTL_FILE_POINTER, file);
[email protected]8ada10f2013-12-21 00:42:34126 if (rc != SQLITE_OK)
127 return rc;
128
Victor Costanbd623112018-07-18 04:17:27129 // TODO(shess): null in file->pMethods has been observed on android_dbg
[email protected]8ada10f2013-12-21 00:42:34130 // content_unittests, even though it should not be possible.
131 // http://crbug.com/329982
132 if (!*file || !(*file)->pMethods)
133 return SQLITE_ERROR;
134
135 return rc;
136}
137
shess5dac334f2015-11-05 20:47:42138// Convenience to get the sqlite3_file* and the size for the "main" database.
139int GetSqlite3FileAndSize(sqlite3* db,
Victor Costancfbfa602018-08-01 23:24:46140 sqlite3_file** file,
141 sqlite3_int64* db_size) {
shess5dac334f2015-11-05 20:47:42142 int rc = GetSqlite3File(db, file);
143 if (rc != SQLITE_OK)
144 return rc;
145
146 return (*file)->pMethods->xFileSize(*file, db_size);
147}
148
shess58b8df82015-06-03 00:19:32149// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
150base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
151 return base::Histogram::FactoryTimeGet(
Victor Costancfbfa602018-08-01 23:24:46152 name, base::TimeDelta::FromMilliseconds(10),
153 base::TimeDelta::FromMinutes(3), 50,
shess58b8df82015-06-03 00:19:32154 base::HistogramBase::kUmaTargetedHistogramFlag);
155}
156
erg102ceb412015-06-20 01:38:13157std::string AsUTF8ForSQL(const base::FilePath& path) {
158#if defined(OS_WIN)
159 return base::WideToUTF8(path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18160#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
erg102ceb412015-06-20 01:38:13161 return path.value();
162#endif
163}
164
[email protected]5b96f3772010-09-28 16:30:57165} // namespace
166
[email protected]e5ffd0e42009-09-11 21:30:56167namespace sql {
168
[email protected]4350e322013-06-18 22:18:10169// static
Victor Costancfbfa602018-08-01 23:24:46170Database::ErrorExpecterCallback* Database::current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10171
172// static
Victor Costancfbfa602018-08-01 23:24:46173bool Database::IsExpectedSqliteError(int error) {
shess976814402016-06-21 06:56:25174 if (!current_expecter_cb_)
[email protected]4350e322013-06-18 22:18:10175 return false;
shess976814402016-06-21 06:56:25176 return current_expecter_cb_->Run(error);
[email protected]4350e322013-06-18 22:18:10177}
178
Victor Costancfbfa602018-08-01 23:24:46179void Database::ReportDiagnosticInfo(int extended_error, Statement* stmt) {
shessc8cd2a162015-10-22 20:30:46180 AssertIOAllowed();
181
afakhry7c9abe72016-08-05 17:33:19182 std::string debug_info = GetDiagnosticInfo(extended_error, stmt);
shessc8cd2a162015-10-22 20:30:46183 if (!debug_info.empty() && RegisterIntentToUpload()) {
Lukasz Anforowicz68c21772018-01-13 03:42:44184 DEBUG_ALIAS_FOR_CSTR(debug_buf, debug_info.c_str(), 2000);
shessc8cd2a162015-10-22 20:30:46185 base::debug::DumpWithoutCrashing();
186 }
187}
188
[email protected]4350e322013-06-18 22:18:10189// static
Victor Costancfbfa602018-08-01 23:24:46190void Database::SetErrorExpecter(Database::ErrorExpecterCallback* cb) {
Victor Costanbd623112018-07-18 04:17:27191 CHECK(!current_expecter_cb_);
shess976814402016-06-21 06:56:25192 current_expecter_cb_ = cb;
[email protected]4350e322013-06-18 22:18:10193}
194
195// static
Victor Costancfbfa602018-08-01 23:24:46196void Database::ResetErrorExpecter() {
shess976814402016-06-21 06:56:25197 CHECK(current_expecter_cb_);
Victor Costanbd623112018-07-18 04:17:27198 current_expecter_cb_ = nullptr;
[email protected]4350e322013-06-18 22:18:10199}
200
Victor Costance678e72018-07-24 10:25:00201// static
Victor Costancfbfa602018-08-01 23:24:46202base::FilePath Database::JournalPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00203 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-journal"));
204}
205
206// static
Victor Costancfbfa602018-08-01 23:24:46207base::FilePath Database::WriteAheadLogPath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00208 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-wal"));
209}
210
211// static
Victor Costancfbfa602018-08-01 23:24:46212base::FilePath Database::SharedMemoryFilePath(const base::FilePath& db_path) {
Victor Costance678e72018-07-24 10:25:00213 return base::FilePath(db_path.value() + FILE_PATH_LITERAL("-shm"));
214}
215
Victor Costancfbfa602018-08-01 23:24:46216Database::StatementRef::StatementRef(Database* database,
217 sqlite3_stmt* stmt,
218 bool was_valid)
219 : database_(database), stmt_(stmt), was_valid_(was_valid) {
220 if (database)
221 database_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56222}
223
Victor Costancfbfa602018-08-01 23:24:46224Database::StatementRef::~StatementRef() {
225 if (database_)
226 database_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38227 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56228}
229
Victor Costancfbfa602018-08-01 23:24:46230void Database::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56231 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50232 // Call to AssertIOAllowed() cannot go at the beginning of the function
233 // because Close() is called unconditionally from destructor to clean
Victor Costancfbfa602018-08-01 23:24:46234 // database_. And if this is inactive statement this won't cause any
[email protected]35f7e5392012-07-27 19:54:50235 // disk access and destructor most probably will be called on thread
236 // not allowing disk access.
237 // TODO([email protected]): This should move to the beginning
238 // of the function. http://crbug.com/136655.
239 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56240 sqlite3_finalize(stmt_);
Victor Costanbd623112018-07-18 04:17:27241 stmt_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56242 }
Victor Costancfbfa602018-08-01 23:24:46243 database_ = nullptr; // The Database may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38244
245 // Forced close is expected to happen from a statement error
246 // handler. In that case maintain the sense of |was_valid_| which
247 // previously held for this ref.
248 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56249}
250
Victor Costan7f6abbbe2018-07-29 02:57:27251static_assert(
Victor Costancfbfa602018-08-01 23:24:46252 Database::kDefaultPageSize == SQLITE_DEFAULT_PAGE_SIZE,
253 "Database::kDefaultPageSize must match the value configured into SQLite");
Victor Costan7f6abbbe2018-07-29 02:57:27254
Victor Costancfbfa602018-08-01 23:24:46255constexpr int Database::kDefaultPageSize;
Victor Costan7f6abbbe2018-07-29 02:57:27256
Victor Costancfbfa602018-08-01 23:24:46257Database::Database()
Victor Costanbd623112018-07-18 04:17:27258 : db_(nullptr),
Victor Costan7f6abbbe2018-07-29 02:57:27259 page_size_(kDefaultPageSize),
[email protected]e5ffd0e42009-09-11 21:30:56260 cache_size_(0),
261 exclusive_locking_(false),
262 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50263 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16264 in_memory_(false),
shess58b8df82015-06-03 00:19:32265 poisoned_(false),
shessa62504d2016-11-07 19:26:12266 mmap_alt_status_(false),
kerz42ff2a012016-04-27 04:50:06267 mmap_disabled_(false),
shess7dbd4dee2015-10-06 17:39:16268 mmap_enabled_(false),
269 total_changes_at_last_release_(0),
Victor Costanbd623112018-07-18 04:17:27270 stats_histogram_(nullptr),
271 commit_time_histogram_(nullptr),
272 autocommit_time_histogram_(nullptr),
273 update_time_histogram_(nullptr),
274 query_time_histogram_(nullptr),
Victor Costan87cf8c72018-07-19 19:36:04275 clock_(std::make_unique<base::DefaultTickClock>()) {}
[email protected]e5ffd0e42009-09-11 21:30:56276
Victor Costancfbfa602018-08-01 23:24:46277Database::~Database() {
[email protected]e5ffd0e42009-09-11 21:30:56278 Close();
279}
280
Victor Costancfbfa602018-08-01 23:24:46281void Database::RecordEvent(Events event, size_t count) {
shess58b8df82015-06-03 00:19:32282 for (size_t i = 0; i < count; ++i) {
283 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
284 }
285
286 if (stats_histogram_) {
287 for (size_t i = 0; i < count; ++i) {
288 stats_histogram_->Add(event);
289 }
290 }
291}
292
Victor Costancfbfa602018-08-01 23:24:46293void Database::RecordCommitTime(const base::TimeDelta& delta) {
shess58b8df82015-06-03 00:19:32294 RecordUpdateTime(delta);
295 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
296 if (commit_time_histogram_)
297 commit_time_histogram_->AddTime(delta);
298}
299
Victor Costancfbfa602018-08-01 23:24:46300void Database::RecordAutoCommitTime(const base::TimeDelta& delta) {
shess58b8df82015-06-03 00:19:32301 RecordUpdateTime(delta);
302 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
303 if (autocommit_time_histogram_)
304 autocommit_time_histogram_->AddTime(delta);
305}
306
Victor Costancfbfa602018-08-01 23:24:46307void Database::RecordUpdateTime(const base::TimeDelta& delta) {
shess58b8df82015-06-03 00:19:32308 RecordQueryTime(delta);
309 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
310 if (update_time_histogram_)
311 update_time_histogram_->AddTime(delta);
312}
313
Victor Costancfbfa602018-08-01 23:24:46314void Database::RecordQueryTime(const base::TimeDelta& delta) {
shess58b8df82015-06-03 00:19:32315 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
316 if (query_time_histogram_)
317 query_time_histogram_->AddTime(delta);
318}
319
Victor Costancfbfa602018-08-01 23:24:46320void Database::RecordTimeAndChanges(const base::TimeDelta& delta,
321 bool read_only) {
shess58b8df82015-06-03 00:19:32322 if (read_only) {
323 RecordQueryTime(delta);
324 } else {
325 const int changes = sqlite3_changes(db_);
326 if (sqlite3_get_autocommit(db_)) {
327 RecordAutoCommitTime(delta);
328 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
329 } else {
330 RecordUpdateTime(delta);
331 RecordEvent(EVENT_CHANGES, changes);
332 }
333 }
334}
335
Victor Costancfbfa602018-08-01 23:24:46336bool Database::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02337 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26338 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49339 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02340 size_t sample = static_cast<size_t>(size_64 / 1024);
341 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
Victor Costancfbfa602018-08-01 23:24:46342 base::HistogramBase* histogram = base::Histogram::FactoryGet(
343 full_histogram_name, 1, 1000000, 50,
344 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]348ac8f52013-05-21 03:27:02345 if (histogram)
346 histogram->Add(sample);
shess9bf2c672015-12-18 01:18:08347 UMA_HISTOGRAM_COUNTS("Sqlite.SizeKB", sample);
[email protected]348ac8f52013-05-21 03:27:02348 }
349 }
350
erg102ceb412015-06-20 01:38:13351 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42352}
[email protected]e5ffd0e42009-09-11 21:30:56353
Victor Costancfbfa602018-08-01 23:24:46354bool Database::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50355 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13356 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56357}
358
Victor Costancfbfa602018-08-01 23:24:46359bool Database::OpenTemporary() {
[email protected]8d409412013-07-19 18:25:30360 return OpenInternal("", NO_RETRY);
361}
362
Victor Costancfbfa602018-08-01 23:24:46363void Database::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47364 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
365 // will delete the -journal file. For ChromiumOS or other more
366 // embedded systems, this is probably not appropriate, whereas on
367 // desktop it might make some sense.
368
[email protected]4b350052012-02-24 20:40:48369 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48370
[email protected]41a97c812013-02-07 02:35:38371 // Release cached statements.
372 statement_cache_.clear();
373
374 // With cached statements released, in-use statements will remain.
375 // Closing the database while statements are in use is an API
376 // violation, except for forced close (which happens from within a
377 // statement's error handler).
378 DCHECK(forced || open_statements_.empty());
379
380 // Deactivate any outstanding statements so sqlite3_close() works.
Victor Costanc7e7f2e2018-07-18 20:07:55381 for (StatementRef* statement_ref : open_statements_)
382 statement_ref->Close(forced);
[email protected]41a97c812013-02-07 02:35:38383 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48384
[email protected]e5ffd0e42009-09-11 21:30:56385 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50386 // Call to AssertIOAllowed() cannot go at the beginning of the function
387 // because Close() must be called from destructor to clean
388 // statement_cache_, it won't cause any disk access and it most probably
389 // will happen on thread not allowing disk access.
390 // TODO([email protected]): This should move to the beginning
391 // of the function. http://crbug.com/136655.
392 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28393
ssid3be5b1ec2016-01-13 14:21:57394 // Reseting acquires a lock to ensure no dump is happening on the database
395 // at the same time. Unregister takes ownership of provider and it is safe
396 // since the db is reset. memory_dump_provider_ could be null if db_ was
397 // poisoned.
398 if (memory_dump_provider_) {
399 memory_dump_provider_->ResetDatabase();
400 base::trace_event::MemoryDumpManager::GetInstance()
401 ->UnregisterAndDeleteDumpProviderSoon(
402 std::move(memory_dump_provider_));
403 }
404
[email protected]73fb8d52013-07-24 05:04:28405 int rc = sqlite3_close(db_);
406 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:18407 base::UmaHistogramSparse("Sqlite.CloseFailure", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52408 DLOG(DCHECK) << "sqlite3_close failed: " << GetErrorMessage();
[email protected]73fb8d52013-07-24 05:04:28409 }
[email protected]e5ffd0e42009-09-11 21:30:56410 }
Victor Costanbd623112018-07-18 04:17:27411 db_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56412}
413
Victor Costancfbfa602018-08-01 23:24:46414void Database::Close() {
[email protected]41a97c812013-02-07 02:35:38415 // If the database was already closed by RazeAndClose(), then no
416 // need to close again. Clear the |poisoned_| bit so that incorrect
417 // API calls are caught.
418 if (poisoned_) {
419 poisoned_ = false;
420 return;
421 }
422
423 CloseInternal(false);
424}
425
Victor Costancfbfa602018-08-01 23:24:46426void Database::Preload() {
[email protected]35f7e5392012-07-27 19:54:50427 AssertIOAllowed();
428
[email protected]e5ffd0e42009-09-11 21:30:56429 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52430 DCHECK(poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56431 return;
432 }
433
Victor Costan7f6abbbe2018-07-29 02:57:27434 // The constructor and set_page_size() ensure that page_size_ is never zero.
435 const int page_size = page_size_;
436 DCHECK(page_size);
437
[email protected]8ada10f2013-12-21 00:42:34438 // Use local settings if provided, otherwise use documented defaults. The
439 // actual results could be fetching via PRAGMA calls.
[email protected]8ada10f2013-12-21 00:42:34440 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
441 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56442 return;
443
Victor Costanbd623112018-07-18 04:17:27444 sqlite3_file* file = nullptr;
[email protected]8ada10f2013-12-21 00:42:34445 sqlite3_int64 file_size = 0;
shess5dac334f2015-11-05 20:47:42446 int rc = GetSqlite3FileAndSize(db_, &file, &file_size);
[email protected]8ada10f2013-12-21 00:42:34447 if (rc != SQLITE_OK)
448 return;
449
450 // Don't preload more than the file contains.
451 if (preload_size > file_size)
452 preload_size = file_size;
453
mostynbd82cd9952016-04-11 20:05:34454 std::unique_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46455 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34456 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
shessd90aeea82015-11-13 02:24:31457
458 // TODO(shess): Consider calling OnSqliteError().
[email protected]8ada10f2013-12-21 00:42:34459 if (rc != SQLITE_OK)
460 return;
461 }
[email protected]e5ffd0e42009-09-11 21:30:56462}
463
Victor Costancfbfa602018-08-01 23:24:46464// SQLite keeps unused pages associated with a database in a cache. It asks
shess7dbd4dee2015-10-06 17:39:16465// the cache for pages by an id, and if the page is present and the database is
466// unchanged, it considers the content of the page valid and doesn't read it
467// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
468// structures created from the memory map data before consulting the cache. On
469// write SQLite creates a new in-memory page structure, copies the data from the
470// memory map, and later writes it, releasing the updated page back to the
471// cache.
472//
473// This means that in memory-mapped mode, the contents of the cached pages are
474// not re-used for reads, but they are re-used for writes if the re-written page
475// is still in the cache. The implementation of sqlite3_db_release_memory() as
476// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
Victor Costancfbfa602018-08-01 23:24:46477// database, so it should free these pages.
shess7dbd4dee2015-10-06 17:39:16478//
479// Unfortunately, the zero page is also freed. That page is never accessed
480// using memory-mapped I/O, and the cached copy can be re-used after verifying
481// the file change counter on disk. Also, fresh pages from cache receive some
482// pager-level initialization before they can be used. Since the information
483// involved will immediately be accessed in various ways, it is unclear if the
484// additional overhead is material, or just moving processor cache effects
485// around.
486//
487// TODO(shess): It would be better to release the pages immediately when they
488// are no longer needed. This would basically happen after SQLite commits a
489// transaction. I had implemented a pcache wrapper to do this, but it involved
490// layering violations, and it had to be setup before any other sqlite call,
491// which was brittle. Also, for large files it would actually make sense to
492// maintain the existing pcache behavior for blocks past the memory-mapped
493// segment. I think drh would accept a reasonable implementation of the overall
494// concept for upstreaming to SQLite core.
495//
496// TODO(shess): Another possibility would be to set the cache size small, which
497// would keep the zero page around, plus some pre-initialized pages, and SQLite
498// can manage things. The downside is that updates larger than the cache would
499// spill to the journal. That could be compensated by setting cache_spill to
500// false. The downside then is that it allows open-ended use of memory for
501// large transactions.
502//
503// TODO(shess): The TrimMemory() trick of bouncing the cache size would also
504// work. There could be two prepared statements, one for cache_size=1 one for
505// cache_size=goal.
Victor Costancfbfa602018-08-01 23:24:46506void Database::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
shess644fc8a2016-02-26 18:15:58507 // The database could have been closed during a transaction as part of error
508 // recovery.
509 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:46510 DCHECK(poisoned_) << "Illegal use of Database without a db";
shess644fc8a2016-02-26 18:15:58511 return;
512 }
shess7dbd4dee2015-10-06 17:39:16513
514 // If memory-mapping is not enabled, the page cache helps performance.
515 if (!mmap_enabled_)
516 return;
517
518 // On caller request, force the change comparison to fail. Done before the
519 // transaction-nesting test so that the signal can carry to transaction
520 // commit.
521 if (implicit_change_performed)
522 --total_changes_at_last_release_;
523
524 // Cached pages may be re-used within the same transaction.
525 if (transaction_nesting())
526 return;
527
528 // If no changes have been made, skip flushing. This allows the first page of
529 // the database to remain in cache across multiple reads.
530 const int total_changes = sqlite3_total_changes(db_);
531 if (total_changes == total_changes_at_last_release_)
532 return;
533
534 total_changes_at_last_release_ = total_changes;
535 sqlite3_db_release_memory(db_);
536}
537
Victor Costancfbfa602018-08-01 23:24:46538base::FilePath Database::DbPath() const {
shessc8cd2a162015-10-22 20:30:46539 if (!is_open())
540 return base::FilePath();
541
542 const char* path = sqlite3_db_filename(db_, "main");
543 const base::StringPiece db_path(path);
544#if defined(OS_WIN)
545 return base::FilePath(base::UTF8ToWide(db_path));
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18546#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46547 return base::FilePath(db_path);
548#else
549 NOTREACHED();
550 return base::FilePath();
551#endif
552}
553
554// Data is persisted in a file shared between databases in the same directory.
555// The "sqlite-diag" file contains a dictionary with the version number, and an
556// array of histogram tags for databases which have been dumped.
Victor Costancfbfa602018-08-01 23:24:46557bool Database::RegisterIntentToUpload() const {
shessc8cd2a162015-10-22 20:30:46558 static const char* kVersionKey = "version";
559 static const char* kDiagnosticDumpsKey = "DiagnosticDumps";
560 static int kVersion = 1;
561
562 AssertIOAllowed();
563
564 if (histogram_tag_.empty())
565 return false;
566
567 if (!is_open())
568 return false;
569
570 if (in_memory_)
571 return false;
572
573 const base::FilePath db_path = DbPath();
574 if (db_path.empty())
575 return false;
576
577 // Put the collection of diagnostic data next to the databases. In most
578 // cases, this is the profile directory, but safe-browsing stores a Cookies
579 // file in the directory above the profile directory.
Victor Costance678e72018-07-24 10:25:00580 base::FilePath breadcrumb_path = db_path.DirName().AppendASCII("sqlite-diag");
shessc8cd2a162015-10-22 20:30:46581
582 // Lock against multiple updates to the diagnostics file. This code should
583 // seldom be called in the first place, and when called it should seldom be
584 // called for multiple databases, and when called for multiple databases there
585 // is _probably_ something systemic wrong with the user's system. So the lock
586 // should never be contended, but when it is the database experience is
587 // already bad.
Victor Costan3653df62018-02-08 21:38:16588 static base::NoDestructor<base::Lock> lock;
589 base::AutoLock auto_lock(*lock);
shessc8cd2a162015-10-22 20:30:46590
mostynbd82cd9952016-04-11 20:05:34591 std::unique_ptr<base::Value> root;
shessc8cd2a162015-10-22 20:30:46592 if (!base::PathExists(breadcrumb_path)) {
mostynbd82cd9952016-04-11 20:05:34593 std::unique_ptr<base::DictionaryValue> root_dict(
594 new base::DictionaryValue());
shessc8cd2a162015-10-22 20:30:46595 root_dict->SetInteger(kVersionKey, kVersion);
596
mostynbd82cd9952016-04-11 20:05:34597 std::unique_ptr<base::ListValue> dumps(new base::ListValue);
shessc8cd2a162015-10-22 20:30:46598 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50599 root_dict->Set(kDiagnosticDumpsKey, std::move(dumps));
shessc8cd2a162015-10-22 20:30:46600
dchenge48600452015-12-28 02:24:50601 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46602 } else {
603 // Failure to read a valid dictionary implies that something is going wrong
604 // on the system.
605 JSONFileValueDeserializer deserializer(breadcrumb_path);
mostynbd82cd9952016-04-11 20:05:34606 std::unique_ptr<base::Value> read_root(
shessc8cd2a162015-10-22 20:30:46607 deserializer.Deserialize(nullptr, nullptr));
608 if (!read_root.get())
609 return false;
mostynbd82cd9952016-04-11 20:05:34610 std::unique_ptr<base::DictionaryValue> root_dict =
dchenge48600452015-12-28 02:24:50611 base::DictionaryValue::From(std::move(read_root));
shessc8cd2a162015-10-22 20:30:46612 if (!root_dict)
613 return false;
614
615 // Don't upload if the version is missing or newer.
616 int version = 0;
617 if (!root_dict->GetInteger(kVersionKey, &version) || version > kVersion)
618 return false;
619
620 base::ListValue* dumps = nullptr;
621 if (!root_dict->GetList(kDiagnosticDumpsKey, &dumps))
622 return false;
623
624 const size_t size = dumps->GetSize();
625 for (size_t i = 0; i < size; ++i) {
626 std::string s;
627
628 // Don't upload if the value isn't a string, or indicates a prior upload.
629 if (!dumps->GetString(i, &s) || s == histogram_tag_)
630 return false;
631 }
632
633 // Record intention to proceed with upload.
634 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50635 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46636 }
637
638 const base::FilePath breadcrumb_new =
639 breadcrumb_path.AddExtension(FILE_PATH_LITERAL("new"));
640 base::DeleteFile(breadcrumb_new, false);
641
642 // No upload if the breadcrumb file cannot be updated.
643 // TODO(shess): Consider ImportantFileWriter::WriteFileAtomically() to land
644 // the data on disk. For now, losing the data is not a big problem, so the
645 // sync overhead would probably not be worth it.
646 JSONFileValueSerializer serializer(breadcrumb_new);
647 if (!serializer.Serialize(*root))
648 return false;
649 if (!base::PathExists(breadcrumb_new))
650 return false;
651 if (!base::ReplaceFile(breadcrumb_new, breadcrumb_path, nullptr)) {
652 base::DeleteFile(breadcrumb_new, false);
653 return false;
654 }
655
656 return true;
657}
658
Victor Costancfbfa602018-08-01 23:24:46659std::string Database::CollectErrorInfo(int error, Statement* stmt) const {
shessc8cd2a162015-10-22 20:30:46660 // Buffer for accumulating debugging info about the error. Place
661 // more-relevant information earlier, in case things overflow the
662 // fixed-size reporting buffer.
663 std::string debug_info;
664
665 // The error message from the failed operation.
Victor Costancfbfa602018-08-01 23:24:46666 base::StringAppendF(&debug_info, "db error: %d/%s\n", GetErrorCode(),
667 GetErrorMessage());
shessc8cd2a162015-10-22 20:30:46668
669 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
670 // reading code does not entirely convince me. Remove if they turn out to be
671 // the same.
672 if (error != GetErrorCode())
673 base::StringAppendF(&debug_info, "reported error: %d\n", error);
674
Victor Costancfbfa602018-08-01 23:24:46675// System error information. Interpretation of Windows errors is different
676// from posix.
shessc8cd2a162015-10-22 20:30:46677#if defined(OS_WIN)
678 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18679#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
shessc8cd2a162015-10-22 20:30:46680 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
681#else
682 NOTREACHED(); // Add appropriate log info.
683#endif
684
685 if (stmt) {
686 base::StringAppendF(&debug_info, "statement: %s\n",
687 stmt->GetSQLStatement());
688 } else {
689 base::StringAppendF(&debug_info, "statement: NULL\n");
690 }
691
692 // SQLITE_ERROR often indicates some sort of mismatch between the statement
693 // and the schema, possibly due to a failed schema migration.
694 if (error == SQLITE_ERROR) {
695 const char* kVersionSql = "SELECT value FROM meta WHERE key = 'version'";
696 sqlite3_stmt* s;
697 int rc = sqlite3_prepare_v2(db_, kVersionSql, -1, &s, nullptr);
698 if (rc == SQLITE_OK) {
699 rc = sqlite3_step(s);
700 if (rc == SQLITE_ROW) {
701 base::StringAppendF(&debug_info, "version: %d\n",
702 sqlite3_column_int(s, 0));
703 } else if (rc == SQLITE_DONE) {
704 debug_info += "version: none\n";
705 } else {
706 base::StringAppendF(&debug_info, "version: error %d\n", rc);
707 }
708 sqlite3_finalize(s);
709 } else {
710 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
711 }
712
713 debug_info += "schema:\n";
714
715 // sqlite_master has columns:
716 // type - "index" or "table".
717 // name - name of created element.
718 // tbl_name - name of element, or target table in case of index.
719 // rootpage - root page of the element in database file.
720 // sql - SQL to create the element.
721 // In general, the |sql| column is sufficient to derive the other columns.
722 // |rootpage| is not interesting for debugging, without the contents of the
723 // database. The COALESCE is because certain automatic elements will have a
724 // |name| but no |sql|,
725 const char* kSchemaSql = "SELECT COALESCE(sql, name) FROM sqlite_master";
726 rc = sqlite3_prepare_v2(db_, kSchemaSql, -1, &s, nullptr);
727 if (rc == SQLITE_OK) {
728 while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
729 base::StringAppendF(&debug_info, "%s\n", sqlite3_column_text(s, 0));
730 }
731 if (rc != SQLITE_DONE)
732 base::StringAppendF(&debug_info, "error %d\n", rc);
733 sqlite3_finalize(s);
734 } else {
735 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
736 }
737 }
738
739 return debug_info;
740}
741
742// TODO(shess): Since this is only called in an error situation, it might be
743// prudent to rewrite in terms of SQLite API calls, and mark the function const.
Victor Costancfbfa602018-08-01 23:24:46744std::string Database::CollectCorruptionInfo() {
shessc8cd2a162015-10-22 20:30:46745 AssertIOAllowed();
746
747 // If the file cannot be accessed it is unlikely that an integrity check will
748 // turn up actionable information.
749 const base::FilePath db_path = DbPath();
avi0b519202015-12-21 07:25:19750 int64_t db_size = -1;
shessc8cd2a162015-10-22 20:30:46751 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
752 return std::string();
753
754 // Buffer for accumulating debugging info about the error. Place
755 // more-relevant information earlier, in case things overflow the
756 // fixed-size reporting buffer.
757 std::string debug_info;
758 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
759 db_size);
760
761 // Only check files up to 8M to keep things from blocking too long.
avi0b519202015-12-21 07:25:19762 const int64_t kMaxIntegrityCheckSize = 8192 * 1024;
shessc8cd2a162015-10-22 20:30:46763 if (db_size > kMaxIntegrityCheckSize) {
764 debug_info += "integrity_check skipped due to size\n";
765 } else {
766 std::vector<std::string> messages;
767
768 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
769 // into a string. Probably should be refactored.
770 const base::TimeTicks before = base::TimeTicks::Now();
771 FullIntegrityCheck(&messages);
772 base::StringAppendF(
Victor Costancfbfa602018-08-01 23:24:46773 &debug_info, "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
774 (base::TimeTicks::Now() - before).InMilliseconds(), messages.size());
shessc8cd2a162015-10-22 20:30:46775
776 // SQLite returns up to 100 messages by default, trim deeper to
777 // keep close to the 2000-character size limit for dumping.
778 const size_t kMaxMessages = 20;
779 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
780 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
781 }
782 }
783
784 return debug_info;
785}
786
Victor Costancfbfa602018-08-01 23:24:46787bool Database::GetMmapAltStatus(int64_t* status) {
shessa62504d2016-11-07 19:26:12788 // The [meta] version uses a missing table as a signal for a fresh database.
789 // That will not work for the view, which would not exist in either a new or
790 // an existing database. A new database _should_ be only one page long, so
791 // just don't bother optimizing this case (start at offset 0).
792 // TODO(shess): Could the [meta] case also get simpler, then?
793 if (!DoesViewExist("MmapStatus")) {
794 *status = 0;
795 return true;
796 }
797
798 const char* kMmapStatusSql = "SELECT * FROM MmapStatus";
799 Statement s(GetUniqueStatement(kMmapStatusSql));
800 if (s.Step())
801 *status = s.ColumnInt64(0);
802 return s.Succeeded();
803}
804
Victor Costancfbfa602018-08-01 23:24:46805bool Database::SetMmapAltStatus(int64_t status) {
shessa62504d2016-11-07 19:26:12806 if (!BeginTransaction())
807 return false;
808
809 // View may not exist on first run.
810 if (!Execute("DROP VIEW IF EXISTS MmapStatus")) {
811 RollbackTransaction();
812 return false;
813 }
814
815 // Views live in the schema, so they cannot be parameterized. For an integer
816 // value, this construct should be safe from SQL injection, if the value
817 // becomes more complicated use "SELECT quote(?)" to generate a safe quoted
818 // value.
Victor Costancfbfa602018-08-01 23:24:46819 const std::string create_view_sql = base::StringPrintf(
820 "CREATE VIEW MmapStatus (value) AS SELECT %" PRId64, status);
821 if (!Execute(create_view_sql.c_str())) {
shessa62504d2016-11-07 19:26:12822 RollbackTransaction();
823 return false;
824 }
825
826 return CommitTransaction();
827}
828
Victor Costancfbfa602018-08-01 23:24:46829size_t Database::GetAppropriateMmapSize() {
shessd90aeea82015-11-13 02:24:31830 AssertIOAllowed();
831
shess9bf2c672015-12-18 01:18:08832 // How much to map if no errors are found. 50MB encompasses the 99th
833 // percentile of Chrome databases in the wild, so this should be good.
834 const size_t kMmapEverything = 256 * 1024 * 1024;
835
shessa62504d2016-11-07 19:26:12836 // Progress information is tracked in the [meta] table for databases which use
837 // sql::MetaTable, otherwise it is tracked in a special view.
838 // TODO(shess): Move all cases to the view implementation.
shess9bf2c672015-12-18 01:18:08839 int64_t mmap_ofs = 0;
shessa62504d2016-11-07 19:26:12840 if (mmap_alt_status_) {
841 if (!GetMmapAltStatus(&mmap_ofs)) {
842 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_READ);
843 return 0;
844 }
845 } else {
846 // If [meta] doesn't exist, yet, it's a new database, assume the best.
847 // sql::MetaTable::Init() will preload kMmapSuccess.
848 if (!MetaTable::DoesTableExist(this)) {
849 RecordOneEvent(EVENT_MMAP_META_MISSING);
850 return kMmapEverything;
851 }
852
853 if (!MetaTable::GetMmapStatus(this, &mmap_ofs)) {
854 RecordOneEvent(EVENT_MMAP_META_FAILURE_READ);
855 return 0;
856 }
shessd90aeea82015-11-13 02:24:31857 }
858
859 // Database read failed in the past, don't memory map.
shess9bf2c672015-12-18 01:18:08860 if (mmap_ofs == MetaTable::kMmapFailure) {
shessd90aeea82015-11-13 02:24:31861 RecordOneEvent(EVENT_MMAP_FAILED);
862 return 0;
shess9bf2c672015-12-18 01:18:08863 } else if (mmap_ofs != MetaTable::kMmapSuccess) {
shessd90aeea82015-11-13 02:24:31864 // Continue reading from previous offset.
865 DCHECK_GE(mmap_ofs, 0);
866
867 // TODO(shess): Could this reading code be shared with Preload()? It would
868 // require locking twice (this code wouldn't be able to access |db_size| so
869 // the helper would have to return amount read).
870
871 // Read more of the database looking for errors. The VFS interface is used
872 // to assure that the reads are valid for SQLite. |g_reads_allowed| is used
873 // to limit checking to 20MB per run of Chromium.
Victor Costanbd623112018-07-18 04:17:27874 sqlite3_file* file = nullptr;
shessd90aeea82015-11-13 02:24:31875 sqlite3_int64 db_size = 0;
876 if (SQLITE_OK != GetSqlite3FileAndSize(db_, &file, &db_size)) {
877 RecordOneEvent(EVENT_MMAP_VFS_FAILURE);
878 return 0;
879 }
880
881 // Read the data left, or |g_reads_allowed|, whichever is smaller.
882 // |g_reads_allowed| limits the total amount of I/O to spend verifying data
883 // in a single Chromium run.
884 sqlite3_int64 amount = db_size - mmap_ofs;
885 if (amount < 0)
886 amount = 0;
887 if (amount > 0) {
Victor Costan3653df62018-02-08 21:38:16888 static base::NoDestructor<base::Lock> lock;
889 base::AutoLock auto_lock(*lock);
shessd90aeea82015-11-13 02:24:31890 static sqlite3_int64 g_reads_allowed = 20 * 1024 * 1024;
891 if (g_reads_allowed < amount)
892 amount = g_reads_allowed;
893 g_reads_allowed -= amount;
894 }
895
896 // |amount| can be <= 0 if |g_reads_allowed| ran out of quota, or if the
897 // database was truncated after a previous pass.
898 if (amount <= 0 && mmap_ofs < db_size) {
899 DCHECK_EQ(0, amount);
900 RecordOneEvent(EVENT_MMAP_SUCCESS_NO_PROGRESS);
901 } else {
902 static const int kPageSize = 4096;
903 char buf[kPageSize];
904 while (amount > 0) {
905 int rc = file->pMethods->xRead(file, buf, sizeof(buf), mmap_ofs);
906 if (rc == SQLITE_OK) {
907 mmap_ofs += sizeof(buf);
908 amount -= sizeof(buf);
909 } else if (rc == SQLITE_IOERR_SHORT_READ) {
910 // Reached EOF for a database with page size < |kPageSize|.
911 mmap_ofs = db_size;
912 break;
913 } else {
914 // TODO(shess): Consider calling OnSqliteError().
shess9bf2c672015-12-18 01:18:08915 mmap_ofs = MetaTable::kMmapFailure;
shessd90aeea82015-11-13 02:24:31916 break;
917 }
918 }
919
920 // Log these events after update to distinguish meta update failure.
921 Events event;
922 if (mmap_ofs >= db_size) {
shess9bf2c672015-12-18 01:18:08923 mmap_ofs = MetaTable::kMmapSuccess;
shessd90aeea82015-11-13 02:24:31924 event = EVENT_MMAP_SUCCESS_NEW;
925 } else if (mmap_ofs > 0) {
926 event = EVENT_MMAP_SUCCESS_PARTIAL;
927 } else {
shess9bf2c672015-12-18 01:18:08928 DCHECK_EQ(MetaTable::kMmapFailure, mmap_ofs);
shessd90aeea82015-11-13 02:24:31929 event = EVENT_MMAP_FAILED_NEW;
930 }
931
shessa62504d2016-11-07 19:26:12932 if (mmap_alt_status_) {
933 if (!SetMmapAltStatus(mmap_ofs)) {
934 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_UPDATE);
935 return 0;
936 }
937 } else {
938 if (!MetaTable::SetMmapStatus(this, mmap_ofs)) {
939 RecordOneEvent(EVENT_MMAP_META_FAILURE_UPDATE);
940 return 0;
941 }
shessd90aeea82015-11-13 02:24:31942 }
943
944 RecordOneEvent(event);
945 }
946 }
947
shess9bf2c672015-12-18 01:18:08948 if (mmap_ofs == MetaTable::kMmapFailure)
shessd90aeea82015-11-13 02:24:31949 return 0;
shess9bf2c672015-12-18 01:18:08950 if (mmap_ofs == MetaTable::kMmapSuccess)
951 return kMmapEverything;
shessd90aeea82015-11-13 02:24:31952 return mmap_ofs;
953}
954
Victor Costancfbfa602018-08-01 23:24:46955void Database::TrimMemory(bool aggressively) {
[email protected]be7995f12013-07-18 18:49:14956 if (!db_)
957 return;
958
959 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
960 int original_cache_size;
961 {
962 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
963 if (!sql_get_original.Step()) {
964 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
965 return;
966 }
967 original_cache_size = sql_get_original.ColumnInt(0);
968 }
969 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
970
971 // Force sqlite to try to reduce page cache usage.
972 const std::string sql_shrink =
973 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
974 if (!Execute(sql_shrink.c_str()))
975 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
976
977 // Restore cache size.
978 const std::string sql_restore =
979 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
980 if (!Execute(sql_restore.c_str()))
981 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
982}
983
[email protected]8e0c01282012-04-06 19:36:49984// Create an in-memory database with the existing database's page
985// size, then backup that database over the existing database.
Victor Costancfbfa602018-08-01 23:24:46986bool Database::Raze() {
[email protected]35f7e5392012-07-27 19:54:50987 AssertIOAllowed();
988
[email protected]8e0c01282012-04-06 19:36:49989 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52990 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49991 return false;
992 }
993
994 if (transaction_nesting_ > 0) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52995 DLOG(DCHECK) << "Cannot raze within a transaction";
[email protected]8e0c01282012-04-06 19:36:49996 return false;
997 }
998
Victor Costancfbfa602018-08-01 23:24:46999 sql::Database null_db;
[email protected]8e0c01282012-04-06 19:36:491000 if (!null_db.OpenInMemory()) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521001 DLOG(DCHECK) << "Unable to open in-memory database.";
[email protected]8e0c01282012-04-06 19:36:491002 return false;
1003 }
1004
Victor Costan7f6abbbe2018-07-29 02:57:271005 const std::string sql = base::StringPrintf("PRAGMA page_size=%d", page_size_);
1006 if (!null_db.Execute(sql.c_str()))
1007 return false;
[email protected]69c58452012-08-06 19:22:421008
[email protected]6d42f152012-11-10 00:38:241009#if defined(OS_ANDROID)
1010 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
1011 // in-memory databases do not respect this define.
1012 // TODO(shess): Figure out a way to set this without using platform
1013 // specific code. AFAICT from sqlite3.c, the only way to do it
1014 // would be to create an actual filesystem database, which is
1015 // unfortunate.
1016 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
1017 return false;
1018#endif
[email protected]8e0c01282012-04-06 19:36:491019
1020 // The page size doesn't take effect until a database has pages, and
1021 // at this point the null database has none. Changing the schema
1022 // version will create the first page. This will not affect the
1023 // schema version in the resulting database, as SQLite's backup
1024 // implementation propagates the schema version from the original
Victor Costancfbfa602018-08-01 23:24:461025 // database to the new version of the database, incremented by one
[email protected]8e0c01282012-04-06 19:36:491026 // so that other readers see the schema change and act accordingly.
1027 if (!null_db.Execute("PRAGMA schema_version = 1"))
1028 return false;
1029
[email protected]6d42f152012-11-10 00:38:241030 // SQLite tracks the expected number of database pages in the first
1031 // page, and if it does not match the total retrieved from a
1032 // filesystem call, treats the database as corrupt. This situation
1033 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
1034 // used to hint to SQLite to soldier on in that case, specifically
1035 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
1036 // sqlite3.c lockBtree().]
1037 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
1038 // page_size" can be used to query such a database.
1039 ScopedWritableSchema writable_schema(db_);
1040
shess92a6fb22017-04-23 04:33:301041#if defined(OS_WIN)
1042 // On Windows, truncate silently fails when applied to memory-mapped files.
1043 // Disable memory-mapping so that the truncate succeeds. Note that other
Victor Costancfbfa602018-08-01 23:24:461044 // Database connections may have memory-mapped the file, so this may not
1045 // entirely prevent the problem.
shess92a6fb22017-04-23 04:33:301046 // [Source: <https://sqlite.org/mmap.html> plus experiments.]
1047 ignore_result(Execute("PRAGMA mmap_size = 0"));
1048#endif
1049
[email protected]7bae5742013-07-10 20:46:161050 const char* kMain = "main";
1051 int rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:181052 base::UmaHistogramSparse("Sqlite.RazeDatabase", rc);
[email protected]8e0c01282012-04-06 19:36:491053
1054 // The destination database was locked.
1055 if (rc == SQLITE_BUSY) {
1056 return false;
1057 }
1058
[email protected]7bae5742013-07-10 20:46:161059 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
1060 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
1061 // isn't even big enough for one page. Either way, reach in and
1062 // truncate it before trying again.
1063 // TODO(shess): Maybe it would be worthwhile to just truncate from
1064 // the get-go?
1065 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
Victor Costanbd623112018-07-18 04:17:271066 sqlite3_file* file = nullptr;
[email protected]8ada10f2013-12-21 00:42:341067 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:161068 if (rc != SQLITE_OK) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521069 DLOG(DCHECK) << "Failure getting file handle.";
[email protected]7bae5742013-07-10 20:46:161070 return false;
[email protected]7bae5742013-07-10 20:46:161071 }
1072
1073 rc = file->pMethods->xTruncate(file, 0);
1074 if (rc != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:181075 base::UmaHistogramSparse("Sqlite.RazeDatabaseTruncate", rc);
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521076 DLOG(DCHECK) << "Failed to truncate file.";
[email protected]7bae5742013-07-10 20:46:161077 return false;
1078 }
1079
1080 rc = BackupDatabase(null_db.db_, db_, kMain);
Ilya Sherman1c811db2017-12-14 10:36:181081 base::UmaHistogramSparse("Sqlite.RazeDatabase2", rc);
[email protected]7bae5742013-07-10 20:46:161082
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521083 DCHECK_EQ(rc, SQLITE_DONE) << "Failed retrying Raze().";
[email protected]7bae5742013-07-10 20:46:161084 }
1085
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521086 // TODO(shess): Figure out which other cases can happen.
1087 DCHECK_EQ(rc, SQLITE_DONE) << "Unable to copy entire null database.";
1088
[email protected]8e0c01282012-04-06 19:36:491089 // The entire database should have been backed up.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521090 return rc == SQLITE_DONE;
[email protected]8e0c01282012-04-06 19:36:491091}
1092
Victor Costancfbfa602018-08-01 23:24:461093bool Database::RazeAndClose() {
[email protected]41a97c812013-02-07 02:35:381094 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521095 DCHECK(poisoned_) << "Cannot raze null db";
[email protected]41a97c812013-02-07 02:35:381096 return false;
1097 }
1098
1099 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:301100 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:381101
1102 bool result = Raze();
1103
1104 CloseInternal(true);
1105
1106 // Mark the database so that future API calls fail appropriately,
1107 // but don't DCHECK (because after calling this function they are
1108 // expected to fail).
1109 poisoned_ = true;
1110
1111 return result;
1112}
1113
Victor Costancfbfa602018-08-01 23:24:461114void Database::Poison() {
[email protected]8d409412013-07-19 18:25:301115 if (!db_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521116 DCHECK(poisoned_) << "Cannot poison null db";
[email protected]8d409412013-07-19 18:25:301117 return;
1118 }
1119
1120 RollbackAllTransactions();
1121 CloseInternal(true);
1122
1123 // Mark the database so that future API calls fail appropriately,
1124 // but don't DCHECK (because after calling this function they are
1125 // expected to fail).
1126 poisoned_ = true;
1127}
1128
[email protected]8d2e39e2013-06-24 05:55:081129// TODO(shess): To the extent possible, figure out the optimal
Victor Costancfbfa602018-08-01 23:24:461130// ordering for these deletes which will prevent other Database connections
[email protected]8d2e39e2013-06-24 05:55:081131// from seeing odd behavior. For instance, it may be necessary to
1132// manually lock the main database file in a SQLite-compatible fashion
1133// (to prevent other processes from opening it), then delete the
1134// journal files, then delete the main database file. Another option
1135// might be to lock the main database file and poison the header with
1136// junk to prevent other processes from opening it successfully (like
1137// Gears "SQLite poison 3" trick).
1138//
1139// static
Victor Costancfbfa602018-08-01 23:24:461140bool Database::Delete(const base::FilePath& path) {
Francois Doray66bdfd82017-10-20 13:50:371141 base::AssertBlockingAllowed();
[email protected]8d2e39e2013-06-24 05:55:081142
Victor Costancfbfa602018-08-01 23:24:461143 base::FilePath journal_path = Database::JournalPath(path);
1144 base::FilePath wal_path = Database::WriteAheadLogPath(path);
[email protected]8d2e39e2013-06-24 05:55:081145
erg102ceb412015-06-20 01:38:131146 std::string journal_str = AsUTF8ForSQL(journal_path);
1147 std::string wal_str = AsUTF8ForSQL(wal_path);
1148 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081149
Victor Costan3653df62018-02-08 21:38:161150 EnsureSqliteInitialized();
shess702467622015-09-16 19:04:551151
Victor Costanbd623112018-07-18 04:17:271152 sqlite3_vfs* vfs = sqlite3_vfs_find(nullptr);
erg102ceb412015-06-20 01:38:131153 CHECK(vfs);
1154 CHECK(vfs->xDelete);
1155 CHECK(vfs->xAccess);
1156
1157 // We only work with unix, win32 and mojo filesystems. If you're trying to
1158 // use this code with any other VFS, you're not in a good place.
1159 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1160 strncmp(vfs->zName, "win32", 5) == 0 ||
1161 strcmp(vfs->zName, "mojo") == 0);
1162
1163 vfs->xDelete(vfs, journal_str.c_str(), 0);
1164 vfs->xDelete(vfs, wal_str.c_str(), 0);
1165 vfs->xDelete(vfs, path_str.c_str(), 0);
1166
1167 int journal_exists = 0;
Victor Costance678e72018-07-24 10:25:001168 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS, &journal_exists);
erg102ceb412015-06-20 01:38:131169
1170 int wal_exists = 0;
Victor Costance678e72018-07-24 10:25:001171 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS, &wal_exists);
erg102ceb412015-06-20 01:38:131172
1173 int path_exists = 0;
Victor Costance678e72018-07-24 10:25:001174 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS, &path_exists);
erg102ceb412015-06-20 01:38:131175
1176 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081177}
1178
Victor Costancfbfa602018-08-01 23:24:461179bool Database::BeginTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561180 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331181 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561182
1183 // When we're going to rollback, fail on this begin and don't actually
1184 // mark us as entering the nested transaction.
1185 return false;
1186 }
1187
1188 bool success = true;
1189 if (!transaction_nesting_) {
1190 needs_rollback_ = false;
1191
1192 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:321193 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:591194 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561195 return false;
1196 }
1197 transaction_nesting_++;
1198 return success;
1199}
1200
Victor Costancfbfa602018-08-01 23:24:461201void Database::RollbackTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561202 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521203 DCHECK(poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561204 return;
1205 }
1206
1207 transaction_nesting_--;
1208
1209 if (transaction_nesting_ > 0) {
1210 // Mark the outermost transaction as needing rollback.
1211 needs_rollback_ = true;
1212 return;
1213 }
1214
1215 DoRollback();
1216}
1217
Victor Costancfbfa602018-08-01 23:24:461218bool Database::CommitTransaction() {
[email protected]e5ffd0e42009-09-11 21:30:561219 if (!transaction_nesting_) {
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521220 DCHECK(poisoned_) << "Committing a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561221 return false;
1222 }
1223 transaction_nesting_--;
1224
1225 if (transaction_nesting_ > 0) {
1226 // Mark any nested transactions as failing after we've already got one.
1227 return !needs_rollback_;
1228 }
1229
1230 if (needs_rollback_) {
1231 DoRollback();
1232 return false;
1233 }
1234
1235 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321236
1237 // Collect the commit time manually, sql::Statement would register it as query
1238 // time only.
Victor Costan87cf8c72018-07-19 19:36:041239 const base::TimeTicks before = NowTicks();
shess58b8df82015-06-03 00:19:321240 bool ret = commit.RunWithoutTimers();
Victor Costan87cf8c72018-07-19 19:36:041241 const base::TimeDelta delta = NowTicks() - before;
shess58b8df82015-06-03 00:19:321242
1243 RecordCommitTime(delta);
1244 RecordOneEvent(EVENT_COMMIT);
1245
shess7dbd4dee2015-10-06 17:39:161246 // Release dirty cache pages after the transaction closes.
1247 ReleaseCacheMemoryIfNeeded(false);
1248
shess58b8df82015-06-03 00:19:321249 return ret;
[email protected]e5ffd0e42009-09-11 21:30:561250}
1251
Victor Costancfbfa602018-08-01 23:24:461252void Database::RollbackAllTransactions() {
[email protected]8d409412013-07-19 18:25:301253 if (transaction_nesting_ > 0) {
1254 transaction_nesting_ = 0;
1255 DoRollback();
1256 }
1257}
1258
Victor Costancfbfa602018-08-01 23:24:461259bool Database::AttachDatabase(const base::FilePath& other_db_path,
1260 const char* attachment_point,
1261 InternalApiToken) {
[email protected]8d409412013-07-19 18:25:301262 DCHECK(ValidAttachmentPoint(attachment_point));
1263
1264 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1265#if OS_WIN
1266 s.BindString16(0, other_db_path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181267#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
Fabrice de Gans-Riberibd1301f2018-05-18 21:00:091268 s.BindString(0, other_db_path.value());
Fabrice de Gans-Riberi65421f62018-05-22 23:16:181269#else
1270#error Unsupported platform
[email protected]8d409412013-07-19 18:25:301271#endif
1272 s.BindString(1, attachment_point);
1273 return s.Run();
1274}
1275
Victor Costancfbfa602018-08-01 23:24:461276bool Database::DetachDatabase(const char* attachment_point, InternalApiToken) {
[email protected]8d409412013-07-19 18:25:301277 DCHECK(ValidAttachmentPoint(attachment_point));
1278
1279 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1280 s.BindString(0, attachment_point);
1281 return s.Run();
1282}
1283
shess58b8df82015-06-03 00:19:321284// TODO(shess): Consider changing this to execute exactly one statement. If a
1285// caller wishes to execute multiple statements, that should be explicit, and
1286// perhaps tucked into an explicit transaction with rollback in case of error.
Victor Costancfbfa602018-08-01 23:24:461287int Database::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501288 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381289 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461290 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381291 return SQLITE_ERROR;
1292 }
shess58b8df82015-06-03 00:19:321293 DCHECK(sql);
1294
1295 RecordOneEvent(EVENT_EXECUTE);
1296 int rc = SQLITE_OK;
1297 while ((rc == SQLITE_OK) && *sql) {
Victor Costanbd623112018-07-18 04:17:271298 sqlite3_stmt* stmt = nullptr;
Victor Costancfbfa602018-08-01 23:24:461299 const char* leftover_sql;
shess58b8df82015-06-03 00:19:321300
Victor Costan87cf8c72018-07-19 19:36:041301 const base::TimeTicks before = NowTicks();
shess58b8df82015-06-03 00:19:321302 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
1303 sql = leftover_sql;
1304
1305 // Stop if an error is encountered.
1306 if (rc != SQLITE_OK)
1307 break;
1308
1309 // This happens if |sql| originally only contained comments or whitespace.
1310 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1311 // extraneous comments and whitespace in the SQL statements increases
1312 // runtime cost and can easily be shifted out to the C++ layer.
1313 if (!stmt)
1314 continue;
1315
1316 // Save for use after statement is finalized.
1317 const bool read_only = !!sqlite3_stmt_readonly(stmt);
1318
Victor Costancfbfa602018-08-01 23:24:461319 RecordOneEvent(Database::EVENT_STATEMENT_RUN);
shess58b8df82015-06-03 00:19:321320 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1321 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
1322 // is the only legitimate case for this.
Victor Costancfbfa602018-08-01 23:24:461323 RecordOneEvent(Database::EVENT_STATEMENT_ROWS);
shess58b8df82015-06-03 00:19:321324 }
1325
1326 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1327 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
1328 rc = sqlite3_finalize(stmt);
1329 if (rc == SQLITE_OK)
Victor Costancfbfa602018-08-01 23:24:461330 RecordOneEvent(Database::EVENT_STATEMENT_SUCCESS);
shess58b8df82015-06-03 00:19:321331
1332 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1333 // trailing whitespace.
1334 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021335 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321336 sql++;
1337 }
1338
Victor Costan87cf8c72018-07-19 19:36:041339 const base::TimeDelta delta = NowTicks() - before;
shess58b8df82015-06-03 00:19:321340 RecordTimeAndChanges(delta, read_only);
1341 }
shess7dbd4dee2015-10-06 17:39:161342
1343 // Most calls to Execute() modify the database. The main exceptions would be
1344 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1345 // but sometimes don't.
1346 ReleaseCacheMemoryIfNeeded(true);
1347
shess58b8df82015-06-03 00:19:321348 return rc;
[email protected]eff1fa522011-12-12 23:50:591349}
1350
Victor Costancfbfa602018-08-01 23:24:461351bool Database::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:381352 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461353 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381354 return false;
1355 }
1356
[email protected]eff1fa522011-12-12 23:50:591357 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001358 if (error != SQLITE_OK)
Victor Costanbd623112018-07-18 04:17:271359 error = OnSqliteError(error, nullptr, sql);
[email protected]473ad792012-11-10 00:55:001360
[email protected]28fe0ff2012-02-25 00:40:331361 // This needs to be a FATAL log because the error case of arriving here is
1362 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101363 // a change alters the schema but not all queries adjust. This can happen
1364 // in production if the schema is corrupted.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521365 DCHECK_NE(error, SQLITE_ERROR)
1366 << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591367 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561368}
1369
Victor Costancfbfa602018-08-01 23:24:461370bool Database::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:381371 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461372 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]5b96f3772010-09-28 16:30:571373 return false;
[email protected]41a97c812013-02-07 02:35:381374 }
[email protected]5b96f3772010-09-28 16:30:571375
1376 ScopedBusyTimeout busy_timeout(db_);
1377 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591378 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571379}
1380
Victor Costancfbfa602018-08-01 23:24:461381scoped_refptr<Database::StatementRef> Database::GetCachedStatement(
Victor Costan12daa3ac92018-07-19 01:05:581382 StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:561383 const char* sql) {
Victor Costanc7e7f2e2018-07-18 20:07:551384 auto it = statement_cache_.find(id);
1385 if (it != statement_cache_.end()) {
Victor Costan87cf8c72018-07-19 19:36:041386 // Statement is in the cache. It should still be active. We're the only
1387 // one invalidating cached statements, and we remove them from the cache
1388 // when we do that.
Victor Costanc7e7f2e2018-07-18 20:07:551389 DCHECK(it->second->is_valid());
Victor Costan87cf8c72018-07-19 19:36:041390
1391 DCHECK_EQ(std::string(sql), std::string(sqlite3_sql(it->second->stmt())))
1392 << "GetCachedStatement used with same ID but different SQL";
1393
1394 // Reset the statement so it can be reused.
Victor Costanc7e7f2e2018-07-18 20:07:551395 sqlite3_reset(it->second->stmt());
1396 return it->second;
[email protected]e5ffd0e42009-09-11 21:30:561397 }
1398
1399 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
1400 if (statement->is_valid())
1401 statement_cache_[id] = statement; // Only cache valid statements.
1402 return statement;
1403}
1404
Victor Costancfbfa602018-08-01 23:24:461405scoped_refptr<Database::StatementRef> Database::GetUniqueStatement(
[email protected]e5ffd0e42009-09-11 21:30:561406 const char* sql) {
shess9e77283d2016-06-13 23:53:201407 return GetStatementImpl(this, sql);
1408}
1409
Victor Costancfbfa602018-08-01 23:24:461410scoped_refptr<Database::StatementRef> Database::GetStatementImpl(
1411 sql::Database* tracking_db,
1412 const char* sql) const {
[email protected]35f7e5392012-07-27 19:54:501413 AssertIOAllowed();
shess9e77283d2016-06-13 23:53:201414 DCHECK(sql);
Victor Costan87cf8c72018-07-19 19:36:041415 DCHECK(!tracking_db || tracking_db == this);
[email protected]35f7e5392012-07-27 19:54:501416
[email protected]41a97c812013-02-07 02:35:381417 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561418 if (!db_)
Victor Costan3b02cdf2018-07-18 00:39:561419 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561420
Victor Costanbd623112018-07-18 04:17:271421 sqlite3_stmt* stmt = nullptr;
1422 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
[email protected]473ad792012-11-10 00:55:001423 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591424 // This is evidence of a syntax error in the incoming SQL.
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521425 DCHECK_NE(rc, SQLITE_ERROR) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001426
1427 // It could also be database corruption.
Victor Costanbd623112018-07-18 04:17:271428 OnSqliteError(rc, nullptr, sql);
Victor Costan3b02cdf2018-07-18 00:39:561429 return base::MakeRefCounted<StatementRef>(nullptr, nullptr, false);
[email protected]e5ffd0e42009-09-11 21:30:561430 }
Victor Costan3b02cdf2018-07-18 00:39:561431 return base::MakeRefCounted<StatementRef>(tracking_db, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561432}
1433
Victor Costancfbfa602018-08-01 23:24:461434scoped_refptr<Database::StatementRef> Database::GetUntrackedStatement(
[email protected]2eec0a22012-07-24 01:59:581435 const char* sql) const {
Victor Costanbd623112018-07-18 04:17:271436 return GetStatementImpl(nullptr, sql);
[email protected]2eec0a22012-07-24 01:59:581437}
1438
Victor Costancfbfa602018-08-01 23:24:461439std::string Database::GetSchema() const {
[email protected]92cd00a2013-08-16 11:09:581440 // The ORDER BY should not be necessary, but relying on organic
1441 // order for something like this is questionable.
Victor Costan87cf8c72018-07-19 19:36:041442 static const char kSql[] =
[email protected]92cd00a2013-08-16 11:09:581443 "SELECT type, name, tbl_name, sql "
1444 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1445 Statement statement(GetUntrackedStatement(kSql));
1446
1447 std::string schema;
1448 while (statement.Step()) {
1449 schema += statement.ColumnString(0);
1450 schema += '|';
1451 schema += statement.ColumnString(1);
1452 schema += '|';
1453 schema += statement.ColumnString(2);
1454 schema += '|';
1455 schema += statement.ColumnString(3);
1456 schema += '\n';
1457 }
1458
1459 return schema;
1460}
1461
Victor Costancfbfa602018-08-01 23:24:461462bool Database::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501463 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381464 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461465 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]41a97c812013-02-07 02:35:381466 return false;
1467 }
1468
Victor Costanbd623112018-07-18 04:17:271469 sqlite3_stmt* stmt = nullptr;
1470 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK)
[email protected]eff1fa522011-12-12 23:50:591471 return false;
1472
1473 sqlite3_finalize(stmt);
1474 return true;
1475}
1476
Victor Costancfbfa602018-08-01 23:24:461477bool Database::DoesIndexExist(const char* index_name) const {
shessa62504d2016-11-07 19:26:121478 return DoesSchemaItemExist(index_name, "index");
[email protected]e2cadec82011-12-13 02:00:531479}
1480
Victor Costancfbfa602018-08-01 23:24:461481bool Database::DoesTableExist(const char* table_name) const {
shessa62504d2016-11-07 19:26:121482 return DoesSchemaItemExist(table_name, "table");
1483}
1484
Victor Costancfbfa602018-08-01 23:24:461485bool Database::DoesViewExist(const char* view_name) const {
shessa62504d2016-11-07 19:26:121486 return DoesSchemaItemExist(view_name, "view");
1487}
1488
Victor Costancfbfa602018-08-01 23:24:461489bool Database::DoesSchemaItemExist(const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471490 const char* kSql =
1491 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581492 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471493
shess976814402016-06-21 06:56:251494 // This can happen if the database is corrupt and the error is a test
1495 // expectation.
shess92a2ab12015-04-09 01:59:471496 if (!statement.is_valid())
1497 return false;
1498
[email protected]e2cadec82011-12-13 02:00:531499 statement.BindString(0, type);
1500 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331501
[email protected]e5ffd0e42009-09-11 21:30:561502 return statement.Step(); // Table exists if any row was returned.
1503}
1504
Victor Costancfbfa602018-08-01 23:24:461505bool Database::DoesColumnExist(const char* table_name,
1506 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561507 std::string sql("PRAGMA TABLE_INFO(");
1508 sql.append(table_name);
1509 sql.append(")");
1510
[email protected]2eec0a22012-07-24 01:59:581511 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471512
shess976814402016-06-21 06:56:251513 // This can happen if the database is corrupt and the error is a test
1514 // expectation.
shess92a2ab12015-04-09 01:59:471515 if (!statement.is_valid())
1516 return false;
1517
[email protected]e5ffd0e42009-09-11 21:30:561518 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331519 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1520 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561521 return true;
1522 }
1523 return false;
1524}
1525
Victor Costancfbfa602018-08-01 23:24:461526int64_t Database::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561527 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461528 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]e5ffd0e42009-09-11 21:30:561529 return 0;
1530 }
1531 return sqlite3_last_insert_rowid(db_);
1532}
1533
Victor Costancfbfa602018-08-01 23:24:461534int Database::GetLastChangeCount() const {
[email protected]1ed78a32009-09-15 20:24:171535 if (!db_) {
Victor Costancfbfa602018-08-01 23:24:461536 DCHECK(poisoned_) << "Illegal use of Database without a db";
[email protected]1ed78a32009-09-15 20:24:171537 return 0;
1538 }
1539 return sqlite3_changes(db_);
1540}
1541
Victor Costancfbfa602018-08-01 23:24:461542int Database::GetErrorCode() const {
[email protected]e5ffd0e42009-09-11 21:30:561543 if (!db_)
1544 return SQLITE_ERROR;
1545 return sqlite3_errcode(db_);
1546}
1547
Victor Costancfbfa602018-08-01 23:24:461548int Database::GetLastErrno() const {
[email protected]767718e52010-09-21 23:18:491549 if (!db_)
1550 return -1;
1551
1552 int err = 0;
Victor Costanbd623112018-07-18 04:17:271553 if (SQLITE_OK != sqlite3_file_control(db_, nullptr, SQLITE_LAST_ERRNO, &err))
[email protected]767718e52010-09-21 23:18:491554 return -2;
1555
1556 return err;
1557}
1558
Victor Costancfbfa602018-08-01 23:24:461559const char* Database::GetErrorMessage() const {
[email protected]e5ffd0e42009-09-11 21:30:561560 if (!db_)
Victor Costancfbfa602018-08-01 23:24:461561 return "sql::Database is not opened.";
[email protected]e5ffd0e42009-09-11 21:30:561562 return sqlite3_errmsg(db_);
1563}
1564
Victor Costancfbfa602018-08-01 23:24:461565bool Database::OpenInternal(const std::string& file_name,
1566 Database::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501567 AssertIOAllowed();
1568
[email protected]9cfbc922009-11-17 20:13:171569 if (db_) {
Victor Costancfbfa602018-08-01 23:24:461570 DLOG(DCHECK) << "sql::Database is already open.";
[email protected]9cfbc922009-11-17 20:13:171571 return false;
1572 }
1573
Victor Costan3653df62018-02-08 21:38:161574 EnsureSqliteInitialized();
[email protected]a7ec1292013-07-22 22:02:181575
shess58b8df82015-06-03 00:19:321576 // Setup the stats histograms immediately rather than allocating lazily.
Victor Costancfbfa602018-08-01 23:24:461577 // Databases which won't exercise all of these probably shouldn't exist.
shess58b8df82015-06-03 00:19:321578 if (!histogram_tag_.empty()) {
Victor Costancfbfa602018-08-01 23:24:461579 stats_histogram_ = base::LinearHistogram::FactoryGet(
1580 "Sqlite.Stats." + histogram_tag_, 1, EVENT_MAX_VALUE,
1581 EVENT_MAX_VALUE + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
shess58b8df82015-06-03 00:19:321582
1583 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1584 // unreasonable time for any single operation, so there is not much value to
1585 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1586 // things are entirely busted.
1587 commit_time_histogram_ =
1588 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1589
1590 autocommit_time_histogram_ =
1591 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1592
1593 update_time_histogram_ =
1594 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1595
1596 query_time_histogram_ =
1597 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1598 }
1599
[email protected]41a97c812013-02-07 02:35:381600 // If |poisoned_| is set, it means an error handler called
1601 // RazeAndClose(). Until regular Close() is called, the caller
1602 // should be treating the database as open, but is_open() currently
1603 // only considers the sqlite3 handle's state.
1604 // TODO(shess): Revise is_open() to consider poisoned_, and review
1605 // to see if any non-testing code even depends on it.
Victor Costancfbfa602018-08-01 23:24:461606 DCHECK(!poisoned_) << "sql::Database is already open.";
[email protected]7bae5742013-07-10 20:46:161607 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381608
shess5f2c3442017-01-24 02:15:101609 // Custom memory-mapping VFS which reads pages using regular I/O on first hit.
1610 sqlite3_vfs* vfs = VFSWrapper();
1611 const char* vfs_name = (vfs ? vfs->zName : nullptr);
Victor Costancfbfa602018-08-01 23:24:461612 int err =
1613 sqlite3_open_v2(file_name.c_str(), &db_,
1614 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, vfs_name);
[email protected]765b44502009-10-02 05:01:421615 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281616 // Extended error codes cannot be enabled until a handle is
1617 // available, fetch manually.
1618 err = sqlite3_extended_errcode(db_);
1619
[email protected]bd2ccdb4a2012-12-07 22:14:501620 // Histogram failures specific to initial open for debugging
1621 // purposes.
Ilya Sherman1c811db2017-12-14 10:36:181622 base::UmaHistogramSparse("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501623
Victor Costanbd623112018-07-18 04:17:271624 OnSqliteError(err, nullptr, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131625 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291626 Close();
[email protected]fed734a2013-07-17 04:45:131627
1628 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1629 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421630 return false;
1631 }
1632
[email protected]73fb8d52013-07-24 05:04:281633 // Enable extended result codes to provide more color on I/O errors.
1634 // Not having extended result codes is not a fatal problem, as
1635 // Chromium code does not attempt to handle I/O errors anyhow. The
1636 // current implementation always returns SQLITE_OK, the DCHECK is to
1637 // quickly notify someone if SQLite changes.
1638 err = sqlite3_extended_result_codes(db_, 1);
1639 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1640
shessbccd300e2016-08-20 00:06:561641 // sqlite3_open() does not actually read the database file (unless a hot
1642 // journal is found). Successfully executing this pragma on an existing
1643 // database requires a valid header on page 1. ExecuteAndReturnErrorCode() to
1644 // get the error code before error callback (potentially) overwrites.
[email protected]bd2ccdb4a2012-12-07 22:14:501645 // TODO(shess): For now, just probing to see what the lay of the
1646 // land is. If it's mostly SQLITE_NOTADB, then the database should
1647 // be razed.
1648 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
shessbccd300e2016-08-20 00:06:561649 if (err != SQLITE_OK) {
Ilya Sherman1c811db2017-12-14 10:36:181650 base::UmaHistogramSparse("Sqlite.OpenProbeFailure", err);
shessbccd300e2016-08-20 00:06:561651 OnSqliteError(err, nullptr, "PRAGMA auto_vacuum");
1652
1653 // Retry or bail out if the error handler poisoned the handle.
1654 // TODO(shess): Move this handling to one place (see also sqlite3_open and
1655 // secure_delete). Possibly a wrapper function?
1656 if (poisoned_) {
1657 Close();
1658 if (retry_flag == RETRY_ON_POISON)
1659 return OpenInternal(file_name, NO_RETRY);
1660 return false;
1661 }
1662 }
[email protected]658f8332010-09-18 04:40:431663
[email protected]5b96f3772010-09-28 16:30:571664 // If indicated, lock up the database before doing anything else, so
1665 // that the following code doesn't have to deal with locking.
1666 // TODO(shess): This code is brittle. Find the cases where code
1667 // doesn't request |exclusive_locking_| and audit that it does the
1668 // right thing with SQLITE_BUSY, and that it doesn't make
1669 // assumptions about who might change things in the database.
1670 // http://crbug.com/56559
1671 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101672 // TODO(shess): This should probably be a failure. Code which
1673 // requests exclusive locking but doesn't get it is almost certain
1674 // to be ill-tested.
1675 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571676 }
1677
Victor Costan4c2f3e922018-08-21 04:47:591678 if (base::FeatureList::IsEnabled(features::kSqlTempStoreMemory)) {
1679 err = ExecuteAndReturnErrorCode("PRAGMA temp_store=MEMORY");
1680 // This operates on in-memory configuration, so it should not fail.
1681 DCHECK_EQ(err, SQLITE_OK) << "Failed switching to in-RAM temporary storage";
1682 }
1683
[email protected]4e179ba2012-03-17 16:06:471684 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1685 // DELETE (default) - delete -journal file to commit.
1686 // TRUNCATE - truncate -journal file to commit.
1687 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091688 // TRUNCATE should be faster than DELETE because it won't need directory
1689 // changes for each transaction. PERSIST may break the spirit of using
1690 // secure_delete.
Victor Costan4c2f3e922018-08-21 04:47:591691 ignore_result(Execute("PRAGMA journal_mode=TRUNCATE"));
[email protected]4e179ba2012-03-17 16:06:471692
[email protected]c68ce172011-11-24 22:30:271693 const base::TimeDelta kBusyTimeout =
Victor Costancfbfa602018-08-01 23:24:461694 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
[email protected]c68ce172011-11-24 22:30:271695
Victor Costancfbfa602018-08-01 23:24:461696 const std::string page_size_sql =
1697 base::StringPrintf("PRAGMA page_size=%d", page_size_);
1698 ignore_result(ExecuteWithTimeout(page_size_sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421699
1700 if (cache_size_ != 0) {
Victor Costancfbfa602018-08-01 23:24:461701 const std::string cache_size_sql =
[email protected]7d3cbc92013-03-18 22:33:041702 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
Victor Costancfbfa602018-08-01 23:24:461703 ignore_result(ExecuteWithTimeout(cache_size_sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421704 }
1705
[email protected]6e0b1442011-08-09 23:23:581706 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131707 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581708 Close();
[email protected]fed734a2013-07-17 04:45:131709 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1710 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581711 return false;
1712 }
1713
shess5dac334f2015-11-05 20:47:421714 // Set a reasonable chunk size for larger files. This reduces churn from
1715 // remapping memory on size changes. It also reduces filesystem
1716 // fragmentation.
1717 // TODO(shess): It may make sense to have this be hinted by the client.
1718 // Database sizes seem to be bimodal, some clients have consistently small
1719 // databases (<20k) while other clients have a broad distribution of sizes
1720 // (hundreds of kilobytes to many megabytes).
Victor Costanbd623112018-07-18 04:17:271721 sqlite3_file* file = nullptr;
shess5dac334f2015-11-05 20:47:421722 sqlite3_int64 db_size = 0;
1723 int rc = GetSqlite3FileAndSize(db_, &file, &db_size);
1724 if (rc == SQLITE_OK && db_size > 16 * 1024) {
1725 int chunk_size = 4 * 1024;
1726 if (db_size > 128 * 1024)
1727 chunk_size = 32 * 1024;
Victor Costanbd623112018-07-18 04:17:271728 sqlite3_file_control(db_, nullptr, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size);
shess5dac334f2015-11-05 20:47:421729 }
1730
shess2f3a814b2015-11-05 18:11:101731 // Enable memory-mapped access. The explicit-disable case is because SQLite
shessd90aeea82015-11-13 02:24:311732 // can be built to default-enable mmap. GetAppropriateMmapSize() calculates a
1733 // safe range to memory-map based on past regular I/O. This value will be
1734 // capped by SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and
1735 // 64-bit platforms.
1736 size_t mmap_size = mmap_disabled_ ? 0 : GetAppropriateMmapSize();
1737 std::string mmap_sql =
Victor Costan4c2f3e922018-08-21 04:47:591738 base::StringPrintf("PRAGMA mmap_size=%" PRIuS, mmap_size);
shessd90aeea82015-11-13 02:24:311739 ignore_result(Execute(mmap_sql.c_str()));
shess2f3a814b2015-11-05 18:11:101740
1741 // Determine if memory-mapping has actually been enabled. The Execute() above
1742 // can succeed without changing the amount mapped.
1743 mmap_enabled_ = false;
1744 {
1745 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1746 if (s.Step() && s.ColumnInt64(0) > 0)
1747 mmap_enabled_ = true;
1748 }
1749
ssid3be5b1ec2016-01-13 14:21:571750 DCHECK(!memory_dump_provider_);
1751 memory_dump_provider_.reset(
Victor Costancfbfa602018-08-01 23:24:461752 new DatabaseMemoryDumpProvider(db_, histogram_tag_));
ssid3be5b1ec2016-01-13 14:21:571753 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
Victor Costancfbfa602018-08-01 23:24:461754 memory_dump_provider_.get(), "sql::Database", nullptr);
ssid3be5b1ec2016-01-13 14:21:571755
[email protected]765b44502009-10-02 05:01:421756 return true;
1757}
1758
Victor Costancfbfa602018-08-01 23:24:461759void Database::DoRollback() {
[email protected]e5ffd0e42009-09-11 21:30:561760 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321761
1762 // Collect the rollback time manually, sql::Statement would register it as
1763 // query time only.
Victor Costan87cf8c72018-07-19 19:36:041764 const base::TimeTicks before = NowTicks();
shess58b8df82015-06-03 00:19:321765 rollback.RunWithoutTimers();
Victor Costan87cf8c72018-07-19 19:36:041766 const base::TimeDelta delta = NowTicks() - before;
shess58b8df82015-06-03 00:19:321767
1768 RecordUpdateTime(delta);
1769 RecordOneEvent(EVENT_ROLLBACK);
1770
shess7dbd4dee2015-10-06 17:39:161771 // The cache may have been accumulating dirty pages for commit. Note that in
1772 // some cases sql::Transaction can fire rollback after a database is closed.
1773 if (is_open())
1774 ReleaseCacheMemoryIfNeeded(false);
1775
[email protected]44ad7d902012-03-23 00:09:051776 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561777}
1778
Victor Costancfbfa602018-08-01 23:24:461779void Database::StatementRefCreated(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551780 DCHECK(!open_statements_.count(ref))
1781 << __func__ << " already called with this statement";
[email protected]e5ffd0e42009-09-11 21:30:561782 open_statements_.insert(ref);
1783}
1784
Victor Costancfbfa602018-08-01 23:24:461785void Database::StatementRefDeleted(StatementRef* ref) {
Victor Costanc7e7f2e2018-07-18 20:07:551786 DCHECK(open_statements_.count(ref))
1787 << __func__ << " called with non-existing statement";
1788 open_statements_.erase(ref);
[email protected]e5ffd0e42009-09-11 21:30:561789}
1790
Victor Costancfbfa602018-08-01 23:24:461791void Database::set_histogram_tag(const std::string& tag) {
shess58b8df82015-06-03 00:19:321792 DCHECK(!is_open());
Victor Costan87cf8c72018-07-19 19:36:041793
shess58b8df82015-06-03 00:19:321794 histogram_tag_ = tag;
1795}
1796
Victor Costancfbfa602018-08-01 23:24:461797void Database::AddTaggedHistogram(const std::string& name,
1798 size_t sample) const {
[email protected]210ce0af2013-05-15 09:10:391799 if (histogram_tag_.empty())
1800 return;
1801
1802 // TODO(shess): The histogram macros create a bit of static storage
1803 // for caching the histogram object. This code shouldn't execute
1804 // often enough for such caching to be crucial. If it becomes an
1805 // issue, the object could be cached alongside histogram_prefix_.
1806 std::string full_histogram_name = name + "." + histogram_tag_;
Victor Costancfbfa602018-08-01 23:24:461807 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
1808 full_histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]210ce0af2013-05-15 09:10:391809 if (histogram)
1810 histogram->Add(sample);
1811}
1812
Victor Costancfbfa602018-08-01 23:24:461813int Database::OnSqliteError(int err,
1814 sql::Statement* stmt,
1815 const char* sql) const {
Ilya Sherman1c811db2017-12-14 10:36:181816 base::UmaHistogramSparse("Sqlite.Error", err);
[email protected]210ce0af2013-05-15 09:10:391817 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141818
1819 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581820 if (!sql && stmt)
1821 sql = stmt->GetSQLStatement();
1822 if (!sql)
1823 sql = "-- unknown";
shessf7e988f2015-11-13 00:41:061824
1825 std::string id = histogram_tag_;
1826 if (id.empty())
1827 id = DbPath().BaseName().AsUTF8Unsafe();
Victor Costancfbfa602018-08-01 23:24:461828 LOG(ERROR) << id << " sqlite error " << err << ", errno " << GetLastErrno()
1829 << ": " << GetErrorMessage() << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141830
[email protected]c3881b372013-05-17 08:39:461831 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561832 // Fire from a copy of the callback in case of reentry into
1833 // re/set_error_callback().
1834 // TODO(shess): <http://crbug.com/254584>
1835 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461836 return err;
1837 }
1838
[email protected]faa604e2009-09-25 22:38:591839 // The default handling is to assert on debug and to ignore on release.
shess976814402016-06-21 06:56:251840 if (!IsExpectedSqliteError(err))
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:521841 DLOG(DCHECK) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591842 return err;
1843}
1844
Victor Costancfbfa602018-08-01 23:24:461845bool Database::FullIntegrityCheck(std::vector<std::string>* messages) {
[email protected]579446c2013-12-16 18:36:521846 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1847}
1848
Victor Costancfbfa602018-08-01 23:24:461849bool Database::QuickIntegrityCheck() {
[email protected]579446c2013-12-16 18:36:521850 std::vector<std::string> messages;
1851 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1852 return false;
1853 return messages.size() == 1 && messages[0] == "ok";
1854}
1855
Victor Costancfbfa602018-08-01 23:24:461856std::string Database::GetDiagnosticInfo(int extended_error,
1857 Statement* statement) {
afakhry7c9abe72016-08-05 17:33:191858 // Prevent reentrant calls to the error callback.
1859 ErrorCallback original_callback = std::move(error_callback_);
1860 reset_error_callback();
1861
1862 // Trim extended error codes.
1863 const int error = (extended_error & 0xFF);
Victor Costancfbfa602018-08-01 23:24:461864 // CollectCorruptionInfo() is implemented in terms of sql::Database,
afakhry7c9abe72016-08-05 17:33:191865 // TODO(shess): Rewrite IntegrityCheckHelper() in terms of raw SQLite.
1866 std::string result = (error == SQLITE_CORRUPT)
1867 ? CollectCorruptionInfo()
1868 : CollectErrorInfo(extended_error, statement);
1869
1870 // The following queries must be executed after CollectErrorInfo() above, so
1871 // if they result in their own errors, they don't interfere with
1872 // CollectErrorInfo().
1873 const bool has_valid_header =
1874 (ExecuteAndReturnErrorCode("PRAGMA auto_vacuum") == SQLITE_OK);
1875 const bool select_sqlite_master_result =
1876 (ExecuteAndReturnErrorCode("SELECT COUNT(*) FROM sqlite_master") ==
1877 SQLITE_OK);
1878
1879 // Restore the original error callback.
1880 error_callback_ = std::move(original_callback);
1881
1882 base::StringAppendF(&result, "Has valid header: %s\n",
1883 (has_valid_header ? "Yes" : "No"));
1884 base::StringAppendF(&result, "Has valid schema: %s\n",
1885 (select_sqlite_master_result ? "Yes" : "No"));
1886
1887 return result;
1888}
1889
[email protected]80abf152013-05-22 12:42:421890// TODO(shess): Allow specifying maximum results (default 100 lines).
Victor Costancfbfa602018-08-01 23:24:461891bool Database::IntegrityCheckHelper(const char* pragma_sql,
1892 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421893 messages->clear();
1894
[email protected]4658e2a02013-06-06 23:05:001895 // This has the side effect of setting SQLITE_RecoveryMode, which
1896 // allows SQLite to process through certain cases of corruption.
1897 // Failing to set this pragma probably means that the database is
1898 // beyond recovery.
Victor Costan4c2f3e922018-08-21 04:47:591899 static const char kWritableSchemaSql[] = "PRAGMA writable_schema=ON";
Victor Costan1d868352018-06-26 19:06:481900 if (!Execute(kWritableSchemaSql))
[email protected]4658e2a02013-06-06 23:05:001901 return false;
1902
1903 bool ret = false;
1904 {
[email protected]579446c2013-12-16 18:36:521905 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001906
1907 // The pragma appears to return all results (up to 100 by default)
1908 // as a single string. This doesn't appear to be an API contract,
1909 // it could return separate lines, so loop _and_ split.
1910 while (stmt.Step()) {
1911 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181912 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1913 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001914 }
1915 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421916 }
[email protected]4658e2a02013-06-06 23:05:001917
1918 // Best effort to put things back as they were before.
Victor Costan4c2f3e922018-08-21 04:47:591919 static const char kNoWritableSchemaSql[] = "PRAGMA writable_schema=OFF";
Victor Costan1d868352018-06-26 19:06:481920 ignore_result(Execute(kNoWritableSchemaSql));
[email protected]4658e2a02013-06-06 23:05:001921
1922 return ret;
[email protected]80abf152013-05-22 12:42:421923}
1924
Victor Costancfbfa602018-08-01 23:24:461925bool Database::ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
1926 const std::string& dump_name) {
dskibab4199f82016-11-21 20:16:131927 return memory_dump_provider_ &&
ssid1f4e5362016-12-08 20:41:381928 memory_dump_provider_->ReportMemoryUsage(pmd, dump_name);
dskibab4199f82016-11-21 20:16:131929}
1930
[email protected]e5ffd0e42009-09-11 21:30:561931} // namespace sql