Fix use-after-destroy on self-move-assign and self-swap of SOO tables. For 1-element SOO tables with !PolicyTraits::transfer_uses_memcpy(), both self-move-assignment and self-swap invoked PolicyTraits::transfer() on an already-destroyed inline slot when `this == &that`: - In raw_hash_set::assign_impl(), destructor_impl() calls DestructSoo(), which destroys soo_data() without clearing inline_data_ so that.is_full_soo() remains true, causing the subsequent move_common() call to transfer the destroyed soo_data() slot onto itself and run its destructor a second time. - In raw_hash_set::swap_common(), the first move_common() call transfers that.soo_data() into tmp and destroys this->soo_data() while move_non_heap_or_soo_fields() leaves this->inline_data_ unchanged, causing the second move_common() call to transfer the destroyed this->soo_data() slot onto itself. Scope both aliasing checks strictly to `SooEnabled() && !PolicyTraits::transfer_uses_memcpy()` via `if constexpr`: in assign_impl(), suppress `that_is_full_soo` when `this == &that`, and in swap_common(), take the direct `swap(common(), that.common())` fast path for `!SooEnabled() || PolicyTraits::transfer_uses_memcpy()` and guard against `this == &that` only in the full-SOO non-memcpy branch. PiperOrigin-RevId: 1023785469
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 94bbfd1..6257b94 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h
@@ -4206,18 +4206,19 @@ // aren't allowed to do so. void swap_common(raw_hash_set& that) { using std::swap; - if (PolicyTraits::transfer_uses_memcpy()) { + if constexpr (!SooEnabled() || PolicyTraits::transfer_uses_memcpy()) { swap(common(), that.common()); - return; + } else { + if (ABSL_PREDICT_FALSE(this == &that)) return; + CommonFields tmp = CommonFields(uninitialized_tag_t{}); + const bool that_is_full_soo = that.is_full_soo(); + move_common(that_is_full_soo, that.char_alloc_ref(), tmp, + std::move(that.common())); + move_common(is_full_soo(), char_alloc_ref(), that.common(), + std::move(common())); + move_common(that_is_full_soo, that.char_alloc_ref(), common(), + std::move(tmp)); } - CommonFields tmp = CommonFields(uninitialized_tag_t{}); - const bool that_is_full_soo = that.is_full_soo(); - move_common(that_is_full_soo, that.char_alloc_ref(), tmp, - std::move(that.common())); - move_common(is_full_soo(), char_alloc_ref(), that.common(), - std::move(common())); - move_common(that_is_full_soo, that.char_alloc_ref(), common(), - std::move(tmp)); } // Only references `GetPolicyFunctions()` when generations are enabled, so @@ -4231,10 +4232,15 @@ template <bool propagate_alloc> raw_hash_set& assign_impl(raw_hash_set&& that) { - // We don't bother checking for this/that aliasing. We just need to avoid - // breaking the invariants in that case. + // We don't bother checking for this/that aliasing unless move_common() + // could transfer the inline SOO slot that destructor_impl() just destroyed. + // Otherwise, we just need to avoid breaking the invariants in that case. destructor_impl(); - move_common(that.is_full_soo(), that.char_alloc_ref(), common(), + bool that_is_full_soo = that.is_full_soo(); + if constexpr (SooEnabled() && !PolicyTraits::transfer_uses_memcpy()) { + that_is_full_soo = that_is_full_soo && ABSL_PREDICT_TRUE(this != &that); + } + move_common(that_is_full_soo, that.char_alloc_ref(), common(), std::move(that.common())); hash_ref() = that.hash_ref(); eq_ref() = that.eq_ref();
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index e0a6cb1..379bd0e 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -4954,6 +4954,43 @@ EXPECT_EQ(tracker.live_instances(), 0); } +TYPED_TEST(InstanceTrackerTest, SelfMoveAssignAndSelfSwap) { + using Table = TypeParam; + static_assert(FitsInSooSlot(sizeof(CopyableMovableInstance), + alignof(CopyableMovableInstance))); + static_assert( + !absl::is_trivially_relocatable<CopyableMovableInstance>::value); + + InstanceTracker tracker; + for (int size : {0, 1, 10}) { + SCOPED_TRACE(size); + Table t; + for (int i = 0; i < size; ++i) t.emplace(i); + + tracker.ResetCopiesMovesSwaps(); + t.swap(t); + using std::swap; + swap(t, t); + EXPECT_EQ(tracker.copies(), 0); + EXPECT_EQ(tracker.moves(), 0); + EXPECT_EQ(tracker.instances(), size); + EXPECT_EQ(tracker.live_instances(), size); + EXPECT_EQ(t.size(), size); + + tracker.ResetCopiesMovesSwaps(); + t = std::move(*&t); + EXPECT_EQ(tracker.copies(), 0); + EXPECT_EQ(tracker.moves(), 0); + EXPECT_EQ(tracker.instances(), 0); + EXPECT_EQ(tracker.live_instances(), 0); + if (SwisstableGenerationsEnabled()) { + // NOLINTNEXTLINE(bugprone-use-after-move) + EXPECT_DEATH_IF_SUPPORTED(t.contains(CopyableMovableInstance(0)), + "self-move-assigned"); + } + } +} + TEST(Table, EraseBeginEndResetsReservedGrowth) { bool frozen = false; BadHashFreezableIntTable t{FreezableAlloc<int64_t>(&frozen)};