From 2a18ba753bca64e79f6d9857a9f64e638b32c371 Mon Sep 17 00:00:00 2001 From: Evan Brown Date: Mon, 16 Oct 2023 12:41:17 -0700 Subject: Add sanitizer mode checks that element constructors/destructors don't make reentrant calls to raw_hash_set member functions. PiperOrigin-RevId: 573897598 Change-Id: If40c23ac3cd9fff315ee18774e27c480cbca3a81 --- absl/container/internal/raw_hash_set_test.cc | 84 ++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 10 deletions(-) (limited to 'absl/container/internal/raw_hash_set_test.cc') diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index d194ca1b..4e67b79e 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -49,8 +49,10 @@ #include "absl/container/internal/hash_policy_testing.h" #include "absl/container/internal/hashtable_debug.h" #include "absl/container/internal/test_allocator.h" +#include "absl/functional/function_ref.h" #include "absl/log/log.h" #include "absl/strings/string_view.h" +#include "absl/types/optional.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -409,19 +411,15 @@ struct StringTable using Base::Base; }; -struct IntTable - : raw_hash_set, - std::equal_to, std::allocator> { - using Base = typename IntTable::raw_hash_set; +template +struct ValueTable : raw_hash_set, hash_default_hash, + std::equal_to, std::allocator> { + using Base = typename ValueTable::raw_hash_set; using Base::Base; }; -struct Uint8Table - : raw_hash_set, - std::equal_to, std::allocator> { - using Base = typename Uint8Table::raw_hash_set; - using Base::Base; -}; +using IntTable = ValueTable; +using Uint8Table = ValueTable; template struct CustomAlloc : std::allocator { @@ -2489,6 +2487,72 @@ using RawHashSetAlloc = raw_hash_set, TEST(Table, AllocatorPropagation) { TestAllocPropagation(); } +struct ConstructCaller { + explicit ConstructCaller(int v) : val(v) {} + ConstructCaller(int v, absl::FunctionRef func) : val(v) { func(); } + template + friend H AbslHashValue(H h, const ConstructCaller& d) { + return H::combine(std::move(h), d.val); + } + bool operator==(const ConstructCaller& c) const { return val == c.val; } + + int val; +}; + +struct DestroyCaller { + explicit DestroyCaller(int v) : val(v) {} + DestroyCaller(int v, absl::FunctionRef func) + : val(v), destroy_func(func) {} + DestroyCaller(DestroyCaller&& that) + : val(that.val), destroy_func(std::move(that.destroy_func)) { + that.Deactivate(); + } + ~DestroyCaller() { + if (destroy_func) (*destroy_func)(); + } + void Deactivate() { destroy_func = absl::nullopt; } + + template + friend H AbslHashValue(H h, const DestroyCaller& d) { + return H::combine(std::move(h), d.val); + } + bool operator==(const DestroyCaller& d) const { return val == d.val; } + + int val; + absl::optional> destroy_func; +}; + +#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || defined(ABSL_HAVE_MEMORY_SANITIZER) +TEST(Table, ReentrantCallsFail) { + constexpr const char* kDeathMessage = + "use-after-poison|use-of-uninitialized-value"; + { + ValueTable t; + t.insert(ConstructCaller{0}); + auto erase_begin = [&] { t.erase(t.begin()); }; + EXPECT_DEATH_IF_SUPPORTED(t.emplace(1, erase_begin), kDeathMessage); + } + { + ValueTable t; + t.insert(DestroyCaller{0}); + auto find_0 = [&] { t.find(DestroyCaller{0}); }; + t.insert(DestroyCaller{1, find_0}); + for (int i = 10; i < 20; ++i) t.insert(DestroyCaller{i}); + EXPECT_DEATH_IF_SUPPORTED(t.clear(), kDeathMessage); + for (auto& elem : t) elem.Deactivate(); + } + { + ValueTable t; + t.insert(DestroyCaller{0}); + auto insert_1 = [&] { t.insert(DestroyCaller{1}); }; + t.insert(DestroyCaller{1, insert_1}); + for (int i = 10; i < 20; ++i) t.insert(DestroyCaller{i}); + EXPECT_DEATH_IF_SUPPORTED(t.clear(), kDeathMessage); + for (auto& elem : t) elem.Deactivate(); + } +} +#endif + } // namespace } // namespace container_internal ABSL_NAMESPACE_END -- cgit v1.2.3