Poison slot and update generation on 1-element non-SOO clear and re-insert. For non-SOO tables at capacity 1, single_iterator() uses SooControl(), whose control byte permanently satisfies IsFull(). When Clear() retained the 1-slot backing array, it destroyed the element without poisoning the slot for ASan/MSan and without incrementing the generation counter, leaving the destroyed slot unpoisoned and allowing invalidated iterators to pass AssertIsFull() and AssertIsValidForComparison(). Similarly, PrepareInsertSmallNonSoo() reused an empty 1-slot backing array via IncrementSmallSizeNonSoo() without calling maybe_increment_generation_on_insert(), allowing iterators invalidated by a prior erase() to dereference the newly inserted element when generations are enabled. Poison the single slot and increment the generation in Clear() when `!kSooEnabled`, and call maybe_increment_generation_on_insert() in PrepareInsertSmallNonSoo() before reusing an empty capacity-1 allocation. PiperOrigin-RevId: 1023784729
diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc index 9c188a2..85a34a3 100644 --- a/absl/container/internal/raw_hash_set.cc +++ b/absl/container/internal/raw_hash_set.cc
@@ -1006,6 +1006,11 @@ destroy_slot(&c, SingleSlotAddress<kSooEnabled>(c)); } DecrementSmallSize<kSooEnabled>(c); + if constexpr (!kSooEnabled) { + c.increment_generation(); + SanitizerPoisonMemoryRegion( + SingleSlotAddress</*kSooEnabled=*/false>(c), policy.slot_size); + } c.infoz().RecordStorageChanged(0, cap); } } else { @@ -2088,6 +2093,7 @@ ABSL_SWISSTABLE_ASSERT(!policy.alloc_policy->soo_enabled); const bool is_empty = common.empty_when_small(); if (common.capacity() == 1 && is_empty) { + common.maybe_increment_generation_on_insert(); IncrementSmallSizeNonSoo(common, *policy.alloc_policy); // Compute before `RecordInsertMissCold` to avoid reloading `control_`. void* res = common.single_non_soo_slot();
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index becc6a7..c2d223b 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -4790,6 +4790,17 @@ t.erase(0); EXPECT_TRUE(__asan_address_is_poisoned(&v)); } + +TEST(Sanitizer, PoisoningOnClear) { + NonSooIntTable t; + auto& v = *t.insert(0).first; + ASSERT_EQ(t.capacity(), 1); + + EXPECT_FALSE(__asan_address_is_poisoned(&v)); + t.clear(); + ASSERT_EQ(t.capacity(), 1); + EXPECT_TRUE(__asan_address_is_poisoned(&v)); +} #endif // ABSL_HAVE_ADDRESS_SANITIZER template <typename T> @@ -4841,6 +4852,44 @@ } } +TEST(Iterator, InvalidUseAfterClearSmallNonSooCrashesWithSanitizers) { + if (!SwisstableGenerationsEnabled()) GTEST_SKIP() << "Generations disabled."; + + NonSooIntTable t; + t.insert(1); + ASSERT_EQ(t.capacity(), 1); + auto it = t.begin(); + t.clear(); + EXPECT_DEATH_IF_SUPPORTED(*it, InvalidIteratorMatcher()); + EXPECT_DEATH_IF_SUPPORTED(void(it == t.begin()), InvalidIteratorMatcher()); +} + +TEST(Iterator, InvalidUseAfterEraseAndInsertSmallNonSooCrashesWithSanitizers) { + if (!SwisstableGenerationsEnabled()) GTEST_SKIP() << "Generations disabled."; + + NonSooIntTable t; + t.insert(1); + ASSERT_EQ(t.capacity(), 1); + auto it = t.begin(); + t.erase(it); + t.insert(2); + ASSERT_EQ(t.capacity(), 1); + EXPECT_DEATH_IF_SUPPORTED(*it, InvalidIteratorMatcher()); + EXPECT_DEATH_IF_SUPPORTED(void(it == t.begin()), InvalidIteratorMatcher()); + + // Verify that reserved 1-element insertion and post-increment erase keep + // end() valid. + NonSooIntTable reserved; + reserved.reserve(1); + auto end_it = reserved.end(); + reserved.insert(10); + EXPECT_NE(reserved.begin(), end_it); + for (auto cur = reserved.begin(); cur != end_it;) { + reserved.erase(cur++); + } + EXPECT_EQ(reserved.begin(), end_it); +} + TEST(Iterator, InvalidUseWithReserveCrashesWithSanitizers) { if (!SwisstableGenerationsEnabled()) GTEST_SKIP() << "Generations disabled.";