Commit f40a8757 authored by Victor Costan's avatar Victor Costan Committed by Chromium LUCI CQ

sql: Add sequence checks to sql::Statement.

sql::Statement is not thread-safe. Add checks documenting this.

The checks are currently excluded on Android, because the Android
implementation of History uses sql::Statement in a thread-unsafe way --
see https://crbug.com/866218. Landing the checks on all other platforms
now is a bit ugly, but has the benefit of preventing new errors from
creeping into the codebase while the Android issues are figured out.

Bug: 863724
Change-Id: Ia0d087f40bd892c2ed084e3acfc3cf1ecf12e6af
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1137851
Commit-Queue: Victor Costan <pwnall@chromium.org>
Commit-Queue: Darwin Huang <huangdarwin@chromium.org>
Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Auto-Submit: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#841165}
parent 909a9407
This diff is collapsed.
......@@ -6,6 +6,7 @@
#define SQL_STATEMENT_H_
#include <stdint.h>
#include <string>
#include <vector>
......@@ -15,6 +16,7 @@
#include "base/sequence_checker.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece_forward.h"
#include "build/build_config.h" // TODO(crbug.com/866218): Remove this include.
#include "sql/database.h"
namespace sql {
......@@ -29,6 +31,11 @@ enum class ColumnType {
kNull = 5,
};
// Compiles and executes SQL statements.
//
// This class is not thread-safe. An instance must be accessed from a single
// sequence. This is enforced in DCHECK-enabled builds.
//
// Normal usage:
// sql::Statement s(connection_.GetUniqueStatement(...));
// s.BindInt(0, a);
......@@ -66,7 +73,13 @@ class COMPONENT_EXPORT(SQL) Statement {
// default value. This is because the statement can become invalid in the
// middle of executing a command if there is a serious error and the database
// has to be reset.
bool is_valid() const { return ref_->is_valid(); }
bool is_valid() const {
#if !defined(OS_ANDROID) // TODO(crbug.com/866218): Remove this conditional
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
#endif // !defined(OS_ANDROID)
return ref_->is_valid();
}
// Running -------------------------------------------------------------------
......@@ -195,6 +208,8 @@ class COMPONENT_EXPORT(SQL) Statement {
// See Succeeded() for what this holds.
bool succeeded_ = false;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(Statement);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment