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