diff options
author | Evan Brown <ezb@google.com> | 2023-02-01 07:49:45 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-02-01 07:50:49 -0800 |
commit | 385ad37dac01bb65a2cef93565fd3731b142b702 (patch) | |
tree | ab85b0567e75b78673ccf21e3fd630ca66b99135 /absl/container/internal/raw_hash_set.cc | |
parent | 1a38beaaaf4cbdcc61a03e07a1212be17a5cd5c8 (diff) |
Rollforward: in sanitizer mode, detect when references become invalidated by randomly rehashing on insertions when there is no reserved growth.
Rollforward of ed59f62f8bbc5f05bcba2f89ee16f107e03813f2
PiperOrigin-RevId: 506314970
Change-Id: I7a654aef36bb169da9ea5c618789ee771f05fe28
Diffstat (limited to 'absl/container/internal/raw_hash_set.cc')
-rw-r--r-- | absl/container/internal/raw_hash_set.cc | 15 |
1 files changed, 15 insertions, 0 deletions
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 <cstring> #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<size_t>(reinterpret_cast<uintptr_t>(&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 |