From eba902e5c4cd4aa66550460934ceaaeb2042d40e Mon Sep 17 00:00:00 2001 From: Richard Reingruber Date: Wed, 20 Nov 2024 09:28:36 +0100 Subject: [PATCH] Check GrowableArray nesting if allocating on alternative ResourceArea --- src/hotspot/share/utilities/growableArray.cpp | 13 +++++++++++++ src/hotspot/share/utilities/growableArray.hpp | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/hotspot/share/utilities/growableArray.cpp b/src/hotspot/share/utilities/growableArray.cpp index 60ed0a477e8e0..b18c279a5638a 100644 --- a/src/hotspot/share/utilities/growableArray.cpp +++ b/src/hotspot/share/utilities/growableArray.cpp @@ -61,6 +61,10 @@ GrowableArrayNestingCheck::GrowableArrayNestingCheck(bool on_resource_area) : _nesting(on_resource_area ? Thread::current()->resource_area()->nesting() : 0) { } +GrowableArrayNestingCheck::GrowableArrayNestingCheck(Arena* arena) : + _nesting((arena->get_tag() == Arena::Tag::tag_ra) ? static_cast(arena)->nesting() : 0) { +} + void GrowableArrayNestingCheck::on_resource_area_alloc() const { // Check for insidious allocation bug: if a GrowableArray overflows, the // grown array must be allocated under the same ResourceMark as the original. @@ -70,6 +74,11 @@ void GrowableArrayNestingCheck::on_resource_area_alloc() const { } } +void GrowableArrayNestingCheck::on_arena_alloc(Arena* arena) const { + if ((arena->get_tag() == Arena::Tag::tag_ra) && _nesting != static_cast(arena)->nesting()) { + fatal("allocation bug: GrowableArray is growing within nested ResourceMark"); + } +} void GrowableArrayMetadata::init_checks(const GrowableArrayBase* array) const { // Stack allocated arrays support all three element allocation locations if (array->allocated_on_stack_or_embedded()) { @@ -89,4 +98,8 @@ void GrowableArrayMetadata::on_resource_area_alloc_check() const { _nesting_check.on_resource_area_alloc(); } +void GrowableArrayMetadata::on_arena_alloc_check() const { + _nesting_check.on_arena_alloc(arena()); +} + #endif // ASSERT diff --git a/src/hotspot/share/utilities/growableArray.hpp b/src/hotspot/share/utilities/growableArray.hpp index 2eb8e6fd09e12..5dec089a4fba5 100644 --- a/src/hotspot/share/utilities/growableArray.hpp +++ b/src/hotspot/share/utilities/growableArray.hpp @@ -608,8 +608,10 @@ class GrowableArrayNestingCheck { public: GrowableArrayNestingCheck(bool on_resource_area); + GrowableArrayNestingCheck(Arena* arena); void on_resource_area_alloc() const; + void on_arena_alloc(Arena* arena) const; }; #endif // ASSERT @@ -649,7 +651,7 @@ class GrowableArrayMetadata { // Arena allocation GrowableArrayMetadata(Arena* arena) : _bits(bits(arena)) - debug_only(COMMA _nesting_check(false)) { + debug_only(COMMA _nesting_check(arena)) { } // CHeap allocation @@ -676,6 +678,7 @@ class GrowableArrayMetadata { void init_checks(const GrowableArrayBase* array) const; void on_resource_area_alloc_check() const; + void on_arena_alloc_check() const; #endif // ASSERT bool on_C_heap() const { return (_bits & 1) == 1; } @@ -740,6 +743,7 @@ class GrowableArray : public GrowableArrayWithAllocator> { } assert(on_arena(), "Sanity"); + debug_only(_metadata.on_arena_alloc_check()); return allocate(this->_capacity, _metadata.arena()); }