1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sqlite_persistent_store_backend_base.h"
#include <utility>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "sql/error_delegate_util.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif // BUILDFLAG(IS_WIN)
namespace net {
SQLitePersistentStoreBackendBase::SQLitePersistentStoreBackendBase(
const base::FilePath& path,
sql::Database::Tag histogram_tag,
const int current_version_number,
const int compatible_version_number,
scoped_refptr<base::SequencedTaskRunner> background_task_runner,
scoped_refptr<base::SequencedTaskRunner> client_task_runner,
bool enable_exclusive_access)
: path_(path),
histogram_tag_(histogram_tag),
current_version_number_(current_version_number),
compatible_version_number_(compatible_version_number),
background_task_runner_(std::move(background_task_runner)),
client_task_runner_(std::move(client_task_runner)),
enable_exclusive_access_(enable_exclusive_access) {}
SQLitePersistentStoreBackendBase::~SQLitePersistentStoreBackendBase() {
// If `db_` hasn't been reset by the time this destructor is called,
// a use-after-free could occur if the `db_` error callback is ever
// invoked. To guard against this, crash if `db_` hasn't been reset
// so that this use-after-free doesn't happen and so that we'll be
// alerted to the fact that a closer look at this code is needed.
CHECK(!db_.get()) << "Close should already have been called.";
}
void SQLitePersistentStoreBackendBase::Flush(base::OnceClosure callback) {
DCHECK(!background_task_runner_->RunsTasksInCurrentSequence());
PostBackgroundTask(
FROM_HERE,
base::BindOnce(
&SQLitePersistentStoreBackendBase::FlushAndNotifyInBackground, this,
std::move(callback)));
}
void SQLitePersistentStoreBackendBase::Close() {
if (background_task_runner_->RunsTasksInCurrentSequence()) {
DoCloseInBackground();
} else {
// Must close the backend on the background runner.
PostBackgroundTask(
FROM_HERE,
base::BindOnce(&SQLitePersistentStoreBackendBase::DoCloseInBackground,
this));
}
}
void SQLitePersistentStoreBackendBase::SetBeforeCommitCallback(
base::RepeatingClosure callback) {
base::AutoLock locked(before_commit_callback_lock_);
before_commit_callback_ = std::move(callback);
}
bool SQLitePersistentStoreBackendBase::InitializeDatabase() {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
if (initialized_ || corruption_detected_) {
// Return false if we were previously initialized but the DB has since been
// closed, or if corruption caused a database reset during initialization.
return db_ != nullptr;
}
base::ElapsedTimer timer;
const base::FilePath dir = path_.DirName();
if (!base::PathExists(dir) && !base::CreateDirectory(dir)) {
return false;
}
// TODO(crbug.com/40262972): Remove explicit_locking = false. This currently
// needs to be set to false because of several failing MigrationTests.
db_ = std::make_unique<sql::Database>(
sql::DatabaseOptions()
.set_exclusive_locking(false)
.set_exclusive_database_file_lock(enable_exclusive_access_)
.set_preload(true),
histogram_tag_);
// base::Unretained is safe because |this| owns (and therefore outlives) the
// sql::Database held by |db_|.
db_->set_error_callback(base::BindRepeating(
&SQLitePersistentStoreBackendBase::DatabaseErrorCallback,
base::Unretained(this)));
if (!db_->Open(path_)) {
DLOG(ERROR) << "Unable to open " << histogram_tag_.value << " DB.";
RecordOpenDBProblem();
Reset();
return false;
}
if (!MigrateDatabaseSchema() || !CreateDatabaseSchema()) {
DLOG(ERROR) << "Unable to update or initialize " << histogram_tag_.value
<< " DB tables.";
RecordDBMigrationProblem();
Reset();
return false;
}
base::UmaHistogramCustomTimes(
base::StrCat({histogram_tag_.value, ".TimeInitializeDB"}),
timer.Elapsed(), base::Milliseconds(1), base::Minutes(1), 50);
initialized_ = DoInitializeDatabase();
if (!initialized_) {
DLOG(ERROR) << "Unable to initialize " << histogram_tag_.value << " DB.";
RecordOpenDBProblem();
Reset();
return false;
}
return true;
}
bool SQLitePersistentStoreBackendBase::DoInitializeDatabase() {
return true;
}
void SQLitePersistentStoreBackendBase::Reset() {
if (db_ && db_->is_open())
db_->Raze();
meta_table_.Reset();
db_.reset();
}
void SQLitePersistentStoreBackendBase::Commit() {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
{
base::AutoLock locked(before_commit_callback_lock_);
if (!before_commit_callback_.is_null())
before_commit_callback_.Run();
}
DoCommit();
}
void SQLitePersistentStoreBackendBase::PostBackgroundTask(
const base::Location& origin,
base::OnceClosure task) {
if (!background_task_runner_->PostTask(origin, std::move(task))) {
LOG(WARNING) << "Failed to post task from " << origin.ToString()
<< " to background_task_runner_.";
}
}
void SQLitePersistentStoreBackendBase::PostClientTask(
const base::Location& origin,
base::OnceClosure task) {
if (!client_task_runner_->PostTask(origin, std::move(task))) {
LOG(WARNING) << "Failed to post task from " << origin.ToString()
<< " to client_task_runner_.";
}
}
bool SQLitePersistentStoreBackendBase::MigrateDatabaseSchema() {
// Version check.
if (!meta_table_.Init(db_.get(), current_version_number_,
compatible_version_number_)) {
return false;
}
if (meta_table_.GetCompatibleVersionNumber() > current_version_number_) {
LOG(WARNING) << histogram_tag_.value << " database is too new.";
return false;
}
// |cur_version| is the version that the database ends up at, after all the
// database upgrade statements.
std::optional<int> cur_version = DoMigrateDatabaseSchema();
if (!cur_version.has_value())
return false;
// Metatable is corrupted. Try to recover.
if (cur_version.value() < current_version_number_) {
meta_table_.Reset();
db_ = std::make_unique<sql::Database>(histogram_tag_);
bool recovered = sql::Database::Delete(path_) && db()->Open(path_) &&
meta_table_.Init(db(), current_version_number_,
compatible_version_number_);
base::UmaHistogramBoolean(
base::StrCat({histogram_tag_.value, ".CorruptMetaTableRecovered"}),
recovered);
if (!recovered) {
DLOG(ERROR) << "Unable to recover the " << histogram_tag_.value << " DB.";
meta_table_.Reset();
db_.reset();
return false;
}
}
return true;
}
void SQLitePersistentStoreBackendBase::FlushAndNotifyInBackground(
base::OnceClosure callback) {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
Commit();
if (callback)
PostClientTask(FROM_HERE, std::move(callback));
}
void SQLitePersistentStoreBackendBase::DoCloseInBackground() {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
// Commit any pending operations
Commit();
meta_table_.Reset();
db_.reset();
}
void SQLitePersistentStoreBackendBase::DatabaseErrorCallback(
int error,
sql::Statement* stmt) {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
if (!sql::IsErrorCatastrophic(error))
return;
// TODO(shess): Running KillDatabase() multiple times should be
// safe.
if (corruption_detected_)
return;
corruption_detected_ = true;
if (!initialized_) {
sql::UmaHistogramSqliteResult(
base::StrCat({histogram_tag_.value, ".ErrorInitializeDB"}), error);
#if BUILDFLAG(IS_WIN)
base::UmaHistogramSparse(
base::StrCat({histogram_tag_.value, ".WinGetLastErrorInitializeDB"}),
::GetLastError());
#endif // BUILDFLAG(IS_WIN)
}
// Don't just do the close/delete here, as we are being called by |db| and
// that seems dangerous.
// TODO(shess): Consider just calling RazeAndPoison() immediately.
// db_ may not be safe to reset at this point, but RazeAndPoison()
// would cause the stack to unwind safely with errors.
PostBackgroundTask(
FROM_HERE,
base::BindOnce(&SQLitePersistentStoreBackendBase::KillDatabase, this));
}
void SQLitePersistentStoreBackendBase::KillDatabase() {
DCHECK(background_task_runner_->RunsTasksInCurrentSequence());
if (db_) {
// This Backend will now be in-memory only. In a future run we will recreate
// the database. Hopefully things go better then!
db_->RazeAndPoison();
meta_table_.Reset();
db_.reset();
}
}
} // namespace net
|