[gardening] Fix VM zone allocator assertion and size accounting

When reallocating a piece of memory that was just allocated one can
re-allocate to larger or smaller new size.

The latter was disallowed by an assertion, which seems like a bug.

Closes https://github.com/dart-lang/sdk/issues/50060

TEST=vm/cc/ZoneRealloc

Change-Id: Iee9f72fead285fb8150ba8639b6de655afc515ba
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/261340
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Martin Kustermann 2022-09-27 12:55:43 +00:00 committed by Commit Queue
parent e87f2d895c
commit d42d7429b9
2 changed files with 23 additions and 2 deletions

View file

@ -310,9 +310,8 @@ inline ElementType* Zone::Realloc(ElementType* old_data,
reinterpret_cast<uword>(old_data) + (new_len * kElementSize);
// ...and there is sufficient space.
if (new_end <= limit_) {
ASSERT(new_len >= old_len);
position_ = Utils::RoundUp(new_end, kAlignment);
size_ += (new_end - old_end);
size_ += static_cast<intptr_t>(new_len - old_len);
return old_data;
}
}

View file

@ -111,6 +111,28 @@ VM_UNIT_TEST_CASE_WITH_EXPECTATION(AllocGeneric_Overflow, "Crash") {
Dart_ShutdownIsolate();
}
VM_UNIT_TEST_CASE(ZoneRealloc) {
TestCase::CreateTestIsolate();
Thread* thread = Thread::Current();
{
TransitionNativeToVM transition(thread);
StackZone stack_zone(thread);
auto zone = thread->zone();
const intptr_t kOldLen = 32;
const intptr_t kNewLen = 16;
const intptr_t kNewLen2 = 16;
auto data_old = zone->Alloc<uint8_t>(kOldLen);
auto data_new = zone->Realloc<uint8_t>(data_old, kOldLen, kNewLen);
RELEASE_ASSERT(data_old == data_new);
auto data_new2 = zone->Realloc<uint8_t>(data_old, kNewLen, kNewLen2);
RELEASE_ASSERT(data_old == data_new2);
}
Dart_ShutdownIsolate();
}
VM_UNIT_TEST_CASE(ZoneAllocated) {
#if defined(DEBUG)
FLAG_trace_zones = true;