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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/diagnostics/sqlite_diagnostics.h"
#include <stdint.h>
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "components/history/core/browser/history_constants.h"
#include "components/webdata/common/webdata_constants.h"
#include "content/public/common/content_constants.h"
#include "sql/database.h"
#include "sql/sqlite_result_code_values.h"
#include "sql/statement.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_constants.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace diagnostics {
namespace {
// Generic diagnostic test class for checking SQLite database integrity.
class SqliteIntegrityTest : public DiagnosticsTest {
public:
// These are bit flags, so each value should be a power of two.
enum Flags {
NO_FLAGS_SET = 0,
CRITICAL = 0x01,
REMOVE_IF_CORRUPT = 0x02,
};
SqliteIntegrityTest(uint32_t flags,
DiagnosticsTestId id,
const base::FilePath& db_path)
: DiagnosticsTest(id), flags_(flags), db_path_(db_path) {}
SqliteIntegrityTest(const SqliteIntegrityTest&) = delete;
SqliteIntegrityTest& operator=(const SqliteIntegrityTest&) = delete;
bool RecoveryImpl(DiagnosticsModel::Observer* observer) override {
int outcome_code = GetOutcomeCode();
if (flags_ & REMOVE_IF_CORRUPT) {
switch (outcome_code) {
case DIAG_SQLITE_ERROR_HANDLER_CALLED:
case DIAG_SQLITE_CANNOT_OPEN_DB:
case DIAG_SQLITE_DB_LOCKED:
case DIAG_SQLITE_PRAGMA_FAILED:
case DIAG_SQLITE_DB_CORRUPTED:
LOG(WARNING) << "Removing broken SQLite database: "
<< db_path_.value();
sql::Database::Delete(db_path_);
break;
case DIAG_SQLITE_SUCCESS:
case DIAG_SQLITE_FILE_NOT_FOUND_OK:
case DIAG_SQLITE_FILE_NOT_FOUND:
break;
default:
DCHECK(false) << "Invalid outcome code: " << outcome_code;
break;
}
}
return true;
}
bool ExecuteImpl(DiagnosticsModel::Observer* observer) override {
// If we're given an absolute path, use it. If not, then assume it's under
// the profile directory.
base::FilePath path;
if (!db_path_.IsAbsolute())
path = GetUserDefaultProfileDir().Append(db_path_);
else
path = db_path_;
if (!base::PathExists(path)) {
if (flags_ & CRITICAL) {
RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND,
"File not found",
DiagnosticsModel::TEST_FAIL_CONTINUE);
} else {
RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND_OK,
"File not found (but that is OK)",
DiagnosticsModel::TEST_OK);
}
return true;
}
int errors = 0;
{ // Scope the statement and database so they close properly.
sql::Database database(/*tag=*/"SQLiteDiagnostics");
scoped_refptr<ErrorRecorder> recorder(new ErrorRecorder);
// Set the error callback so that we can get useful results in a debug
// build for a corrupted database. Without setting the error callback,
// sql::Database will just DCHECK.
database.set_error_callback(base::BindRepeating(
&SqliteIntegrityTest::ErrorRecorder::RecordSqliteError,
recorder->AsWeakPtr(), &database));
if (!database.Open(path)) {
RecordFailure(DIAG_SQLITE_CANNOT_OPEN_DB,
"Cannot open DB. Possibly corrupted");
return true;
}
if (recorder->has_error()) {
RecordFailure(DIAG_SQLITE_ERROR_HANDLER_CALLED,
recorder->FormatError());
return true;
}
sql::Statement statement(
database.GetUniqueStatement("PRAGMA integrity_check;"));
if (recorder->has_error()) {
RecordFailure(DIAG_SQLITE_ERROR_HANDLER_CALLED,
recorder->FormatError());
return true;
}
if (!statement.is_valid()) {
int error = database.GetErrorCode();
if (static_cast<int>(sql::SqliteResultCode::kBusy) == error) {
RecordFailure(DIAG_SQLITE_DB_LOCKED,
"Database locked by another process");
} else {
std::string str("Pragma failed. Error: ");
str += base::NumberToString(error);
RecordFailure(DIAG_SQLITE_PRAGMA_FAILED, str);
}
return false;
}
while (statement.Step()) {
std::string_view result = statement.ColumnStringView(0);
if ("ok" != result)
++errors;
}
if (recorder->has_error()) {
RecordFailure(DIAG_SQLITE_ERROR_HANDLER_CALLED,
recorder->FormatError());
return true;
}
}
// All done. Report to the user.
if (errors != 0) {
std::string str("Database corruption detected: ");
str += base::NumberToString(errors) + " errors";
RecordFailure(DIAG_SQLITE_DB_CORRUPTED, str);
return true;
}
RecordSuccess("No corruption detected");
return true;
}
private:
class ErrorRecorder : public base::RefCounted<ErrorRecorder> {
public:
ErrorRecorder() = default;
ErrorRecorder(const ErrorRecorder&) = delete;
ErrorRecorder& operator=(const ErrorRecorder&) = delete;
void RecordSqliteError(sql::Database* connection,
int sqlite_error,
sql::Statement* statement) {
has_error_ = true;
sqlite_error_ = sqlite_error;
last_errno_ = connection->GetLastErrno();
message_ = connection->GetErrorMessage();
}
bool has_error() const { return has_error_; }
std::string FormatError() {
return base::StringPrintf("SQLite error: %d, Last Errno: %d: %s",
sqlite_error_,
last_errno_,
message_.c_str());
}
base::WeakPtr<ErrorRecorder> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
friend class base::RefCounted<ErrorRecorder>;
~ErrorRecorder() = default;
bool has_error_ = false;
int sqlite_error_ = 0;
int last_errno_ = 0;
std::string message_;
base::WeakPtrFactory<ErrorRecorder> weak_ptr_factory_{this};
};
uint32_t flags_;
base::FilePath db_path_;
};
} // namespace
std::unique_ptr<DiagnosticsTest> MakeSqliteCookiesDbTest() {
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::CRITICAL, DIAGNOSTICS_SQLITE_INTEGRITY_COOKIE_TEST,
base::FilePath(chrome::kCookieFilename));
}
std::unique_ptr<DiagnosticsTest> MakeSqliteHistoryDbTest() {
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::CRITICAL, DIAGNOSTICS_SQLITE_INTEGRITY_HISTORY_TEST,
base::FilePath(history::kHistoryFilename));
}
#if BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<DiagnosticsTest> MakeSqliteNssCertDbTest() {
base::FilePath home_dir;
base::PathService::Get(base::DIR_HOME, &home_dir);
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::REMOVE_IF_CORRUPT,
DIAGNOSTICS_SQLITE_INTEGRITY_NSS_CERT_TEST,
home_dir.Append(ash::kNssCertDbPath));
}
std::unique_ptr<DiagnosticsTest> MakeSqliteNssKeyDbTest() {
base::FilePath home_dir;
base::PathService::Get(base::DIR_HOME, &home_dir);
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::REMOVE_IF_CORRUPT,
DIAGNOSTICS_SQLITE_INTEGRITY_NSS_KEY_TEST,
home_dir.Append(ash::kNssKeyDbPath));
}
#endif // BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<DiagnosticsTest> MakeSqliteFaviconsDbTest() {
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::NO_FLAGS_SET,
DIAGNOSTICS_SQLITE_INTEGRITY_FAVICONS_TEST,
base::FilePath(history::kFaviconsFilename));
}
std::unique_ptr<DiagnosticsTest> MakeSqliteTopSitesDbTest() {
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::NO_FLAGS_SET,
DIAGNOSTICS_SQLITE_INTEGRITY_TOPSITES_TEST,
base::FilePath(history::kTopSitesFilename));
}
std::unique_ptr<DiagnosticsTest> MakeSqliteWebDataDbTest() {
return std::make_unique<SqliteIntegrityTest>(
SqliteIntegrityTest::CRITICAL, DIAGNOSTICS_SQLITE_INTEGRITY_WEB_DATA_TEST,
base::FilePath(kWebDataFilename));
}
} // namespace diagnostics
|