Skip to content

24-3: Fix use-after-free in CommittingOps tracking #8925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ydb/core/tx/datashard/datashard__engine_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ class TDataShardEngineHost final
TSmallVec<NTable::TUpdateOp> ops;
ConvertTableValues(Scheme, tableInfo, commands, ops, nullptr);

UserDb.UpdateRow(tableId, key, ops);
UserDb.UpsertRow(tableId, key, ops);
}

void UpdateRow(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops) override {
UserDb.UpdateRow(tableId, key, ops);
void UpsertRow(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops) override {
UserDb.UpsertRow(tableId, key, ops);
}

void ReplaceRow(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops) override {
Expand All @@ -385,6 +385,10 @@ class TDataShardEngineHost final
UserDb.InsertRow(tableId, key, ops);
}

void UpdateRow(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops) override {
UserDb.UpdateRow(tableId, key, ops);
}

void EraseRow(const TTableId& tableId, const TArrayRef<const TCell>& row) override {
if (TSysTables::IsSystemTable(tableId)) {
DataShardSysTable(tableId).EraseRow(row);
Expand Down
18 changes: 11 additions & 7 deletions ydb/core/tx/datashard/datashard_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2285,11 +2285,15 @@ void TPipeline::AddCommittingOp(const TOperation::TPtr& op) {
if (!Self->IsMvccEnabled() || op->IsReadOnly())
return;

Y_VERIFY_S(!op->GetCommittingOpsVersion(),
"Trying to AddCommittingOp " << *op << " more than once");

TRowVersion version = Self->GetReadWriteVersions(op.Get()).WriteVersion;
if (op->IsImmediate())
CommittingOps.Add(op->GetTxId(), version);
else
CommittingOps.Add(version);
op->SetCommittingOpsVersion(version);
}

void TPipeline::RemoveCommittingOp(const TRowVersion& version) {
Expand All @@ -2299,13 +2303,13 @@ void TPipeline::RemoveCommittingOp(const TRowVersion& version) {
}

void TPipeline::RemoveCommittingOp(const TOperation::TPtr& op) {
if (!Self->IsMvccEnabled() || op->IsReadOnly())
return;

if (op->IsImmediate())
CommittingOps.Remove(op->GetTxId());
else
CommittingOps.Remove(TRowVersion(op->GetStep(), op->GetTxId()));
if (const auto& version = op->GetCommittingOpsVersion()) {
if (op->IsImmediate())
CommittingOps.Remove(op->GetTxId(), *version);
else
CommittingOps.Remove(*version);
op->ResetCommittingOpsVersion();
}
}

bool TPipeline::WaitCompletion(const TOperation::TPtr& op) const {
Expand Down
30 changes: 23 additions & 7 deletions ydb/core/tx/datashard/datashard_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,13 @@ class TPipeline : TNonCopyable {
ui64 Step;
ui64 TxId;
mutable ui32 Counter;
mutable ui32 TxCounter;

TItem(const TRowVersion& from)
: Step(from.Step)
, TxId(from.TxId)
, Counter(1u)
, TxCounter(0u)
{}

friend constexpr bool operator<(const TItem& a, const TItem& b) {
Expand All @@ -442,6 +444,7 @@ class TPipeline : TNonCopyable {

using TItemsSet = TSet<TItem>;
using TTxIdMap = THashMap<ui64, TItemsSet::iterator>;

public:
inline void Add(ui64 txId, TRowVersion version) {
auto res = ItemsSet.emplace(version);
Expand All @@ -450,6 +453,7 @@ class TPipeline : TNonCopyable {
auto res2 = TxIdMap.emplace(txId, res.first);
Y_VERIFY_S(res2.second, "Unexpected duplicate immediate tx " << txId
<< " committing at " << version);
res.first->TxCounter += 1;
}

inline void Add(TRowVersion version) {
Expand All @@ -458,17 +462,29 @@ class TPipeline : TNonCopyable {
res.first->Counter += 1;
}

inline void Remove(ui64 txId) {
if (auto it = TxIdMap.find(txId); it != TxIdMap.end()) {
if (--it->second->Counter == 0)
ItemsSet.erase(it->second);
TxIdMap.erase(it);
}
inline void Remove(ui64 txId, TRowVersion version) {
auto it = TxIdMap.find(txId);
Y_VERIFY_S(it != TxIdMap.end(), "Removing immediate tx " << txId << " " << version
<< " does not match a previous Add");
Y_VERIFY_S(TRowVersion(it->second->Step, it->second->TxId) == version, "Removing immediate tx " << txId << " " << version
<< " does not match a previous Add " << TRowVersion(it->second->Step, it->second->TxId));
Y_VERIFY_S(it->second->TxCounter > 0, "Removing immediate tx " << txId << " " << version
<< " with a mismatching TxCounter");
--it->second->TxCounter;
if (--it->second->Counter == 0)
ItemsSet.erase(it->second);
TxIdMap.erase(it);
}

inline void Remove(TRowVersion version) {
if (auto it = ItemsSet.find(version); it != ItemsSet.end() && --it->Counter == 0)
auto it = ItemsSet.find(version);
Y_VERIFY_S(it != ItemsSet.end(), "Removing version " << version
<< " does not match a previous Add");
if (--it->Counter == 0) {
Y_VERIFY_S(it->TxCounter == 0, "Removing version " << version
<< " while TxCounter has active references, possible Add/Remove mismatch");
ItemsSet.erase(it);
}
}

inline bool HasOpsBelow(TRowVersion upperBound) const {
Expand Down
41 changes: 28 additions & 13 deletions ydb/core/tx/datashard/datashard_user_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ui64 CalculateValueBytes(const TArrayRef<const NIceDb::TUpdateOp> ops) {
return bytes;
};

void TDataShardUserDb::UpdateRow(
void TDataShardUserDb::UpsertRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops)
Expand Down Expand Up @@ -108,11 +108,11 @@ void TDataShardUserDb::UpdateRow(
if (specUpdates.ColIdUpdateNo != Max<ui32>()) {
addExtendedOp(specUpdates.ColIdUpdateNo, specUpdates.UpdateNo);
}
UpdateRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, extendedOps);
UpsertRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, extendedOps);

IncreaseUpdateCounters(key, extendedOps);
} else {
UpdateRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, ops);
UpsertRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, ops);

IncreaseUpdateCounters(key, ops);
}
Expand All @@ -126,7 +126,7 @@ void TDataShardUserDb::ReplaceRow(
auto localTableId = Self.GetLocalTableId(tableId);
Y_ABORT_UNLESS(localTableId != 0, "Unexpected ReplaceRow for an unknown table");

UpdateRowInt(NTable::ERowOp::Reset, tableId, localTableId, key, ops);
UpsertRowInt(NTable::ERowOp::Reset, tableId, localTableId, key, ops);

IncreaseUpdateCounters(key, ops);
}
Expand All @@ -139,9 +139,26 @@ void TDataShardUserDb::InsertRow(
auto localTableId = Self.GetLocalTableId(tableId);
Y_ABORT_UNLESS(localTableId != 0, "Unexpected InsertRow for an unknown table");

EnsureMissingRow(tableId, key);
if (RowExists(tableId, key))
throw TUniqueConstrainException();

UpsertRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, ops);

IncreaseUpdateCounters(key, ops);
}

void TDataShardUserDb::UpdateRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops)
{
auto localTableId = Self.GetLocalTableId(tableId);
Y_ABORT_UNLESS(localTableId != 0, "Unexpected UpdateRow for an unknown table");

if (!RowExists(tableId, key))
return;

UpdateRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, ops);
UpsertRowInt(NTable::ERowOp::Upsert, tableId, localTableId, key, ops);

IncreaseUpdateCounters(key, ops);
}
Expand All @@ -153,7 +170,7 @@ void TDataShardUserDb::EraseRow(
auto localTableId = Self.GetLocalTableId(tableId);
Y_ABORT_UNLESS(localTableId != 0, "Unexpected UpdateRow for an unknown table");

UpdateRowInt(NTable::ERowOp::Erase, tableId, localTableId, key, {});
UpsertRowInt(NTable::ERowOp::Erase, tableId, localTableId, key, {});

ui64 keyBytes = CalculateKeyBytes(key);

Expand All @@ -172,7 +189,7 @@ void TDataShardUserDb::IncreaseUpdateCounters(
Counters.UpdateRowBytes += keyBytes + valueBytes;
}

void TDataShardUserDb::UpdateRowInt(
void TDataShardUserDb::UpsertRowInt(
NTable::ERowOp rowOp,
const TTableId& tableId,
ui64 localTableId,
Expand Down Expand Up @@ -216,7 +233,7 @@ void TDataShardUserDb::UpdateRowInt(
Self.GetKeyAccessSampler()->AddSample(tableId, keyCells);
}

void TDataShardUserDb::EnsureMissingRow (
bool TDataShardUserDb::RowExists (
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key)
{
Expand All @@ -227,12 +244,10 @@ void TDataShardUserDb::EnsureMissingRow (
throw TNotReadyTabletException();
}
case NTable::EReady::Data: {
if (rowState == NTable::ERowOp::Upsert)
throw TUniqueConstrainException();
break;
return true;
}
case NTable::EReady::Gone: {
break;
return false;
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions ydb/core/tx/datashard/datashard_user_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class IDataShardUserDb {
NTable::TRowState& row,
const TMaybe<TRowVersion>& readVersion = {}) = 0;

virtual void UpdateRow(
virtual void UpsertRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) = 0;
Expand All @@ -52,6 +52,11 @@ class IDataShardUserDb {
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) = 0;

virtual void UpdateRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) = 0;

virtual void EraseRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key) = 0;
Expand Down Expand Up @@ -107,7 +112,7 @@ class TDataShardUserDb final
NTable::TRowState& row,
const TMaybe<TRowVersion>& readVersion = {}) override;

void UpdateRow(
void UpsertRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) override;
Expand All @@ -122,6 +127,11 @@ class TDataShardUserDb final
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) override;

void UpdateRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key,
const TArrayRef<const NIceDb::TUpdateOp> ops) override;

void EraseRow(
const TTableId& tableId,
const TArrayRef<const TRawTypeValue> key) override;
Expand Down Expand Up @@ -169,8 +179,8 @@ class TDataShardUserDb final
private:
static TSmallVec<TCell> ConvertTableKeys(const TArrayRef<const TRawTypeValue> key);

void UpdateRowInt(NTable::ERowOp rowOp, const TTableId& tableId, ui64 localTableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops);
void EnsureMissingRow(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key);
void UpsertRowInt(NTable::ERowOp rowOp, const TTableId& tableId, ui64 localTableId, const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops);
bool RowExists(const TTableId& tableId, const TArrayRef<const TRawTypeValue> key);

void IncreaseUpdateCounters(const TArrayRef<const TRawTypeValue> key, const TArrayRef<const NIceDb::TUpdateOp> ops);
private:
Expand Down
Loading
Loading