Skip to content

Commit

Permalink
Check GrowableArray nesting if allocating on alternative ResourceArea
Browse files Browse the repository at this point in the history
  • Loading branch information
reinrich committed Nov 20, 2024
1 parent 499186b commit eba902e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/hotspot/share/utilities/growableArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceArea*>(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.
Expand All @@ -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<ResourceArea*>(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()) {
Expand All @@ -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
6 changes: 5 additions & 1 deletion src/hotspot/share/utilities/growableArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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; }
Expand Down Expand Up @@ -740,6 +743,7 @@ class GrowableArray : public GrowableArrayWithAllocator<E, GrowableArray<E>> {
}

assert(on_arena(), "Sanity");
debug_only(_metadata.on_arena_alloc_check());
return allocate(this->_capacity, _metadata.arena());
}

Expand Down

0 comments on commit eba902e

Please sign in to comment.