From ed59f62f8bbc5f05bcba2f89ee16f107e03813f2 Mon Sep 17 00:00:00 2001 From: Evan Brown Date: Mon, 30 Jan 2023 14:59:39 -0800 Subject: In sanitizer mode, detect when references become invalidated by randomly rehashing on insertions when there is no reserved growth. PiperOrigin-RevId: 505807487 Change-Id: I9051a04f6a75e579d16e9ae8defd404bcc377fba --- absl/container/internal/raw_hash_set.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'absl/container/internal/raw_hash_set.cc') diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc index 3677ac59..5dc8b2fa 100644 --- a/absl/container/internal/raw_hash_set.cc +++ b/absl/container/internal/raw_hash_set.cc @@ -19,6 +19,7 @@ #include #include "absl/base/config.h" +#include "absl/hash/hash.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -51,6 +52,20 @@ inline size_t RandomSeed() { return value ^ static_cast(reinterpret_cast(&counter)); } +bool CommonFieldsGenerationInfoEnabled:: + should_rehash_for_bug_detection_on_insert(const ctrl_t* ctrl, + size_t capacity) const { + if (reserved_growth_ == kReservedGrowthJustRanOut) return true; + if (reserved_growth_ > 0) return false; + // Note: we can't use the abseil-random library because abseil-random + // depends on swisstable. We want to return true with probability + // `min(1, RehashProbabilityConstant() / capacity())`. In order to do this, + // we probe based on a random hash and see if the offset is less than + // RehashProbabilityConstant(). + return probe(ctrl, capacity, absl::HashOf(RandomSeed())).offset() < + RehashProbabilityConstant(); +} + bool ShouldInsertBackwards(size_t hash, const ctrl_t* ctrl) { // To avoid problems with weak hashes and single bit tests, we use % 13. // TODO(kfm,sbenza): revisit after we do unconditional mixing -- cgit v1.2.3