Skip to content

Commit 5470159

Browse files
rmacnak-googlecommit-bot@chromium.org
authored and
commit-bot@chromium.org
committed
[vm, gc] Produce a proper error message when crashing due to lack of memory at isolate startup.
Bug: flutter/flutter#35651 Change-Id: I2639baa4daaeb1d70d71a83da69e2c51800bdbf5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108365 Reviewed-by: Siva Annamalai <asiva@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
1 parent bfc7d21 commit 5470159

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

runtime/vm/heap/heap.cc

+21
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ void Heap::NotifyLowMemory() {
439439

440440
void Heap::EvacuateNewSpace(Thread* thread, GCReason reason) {
441441
ASSERT((reason != kOldSpace) && (reason != kPromotion));
442+
if (thread->isolate() == Dart::vm_isolate()) {
443+
// The vm isolate cannot safely collect garbage due to unvisited read-only
444+
// handles and slots bootstrapped with RAW_NULL. Ignore GC requests to
445+
// trigger a nice out-of-memory message instead of a crash in the middle of
446+
// visiting pointers.
447+
return;
448+
}
442449
if (BeginNewSpaceGC(thread)) {
443450
RecordBeforeGC(kScavenge, reason);
444451
VMTagScope tagScope(thread, reason == kIdle ? VMTag::kGCIdleTagId
@@ -454,6 +461,13 @@ void Heap::EvacuateNewSpace(Thread* thread, GCReason reason) {
454461

455462
void Heap::CollectNewSpaceGarbage(Thread* thread, GCReason reason) {
456463
ASSERT((reason != kOldSpace) && (reason != kPromotion));
464+
if (thread->isolate() == Dart::vm_isolate()) {
465+
// The vm isolate cannot safely collect garbage due to unvisited read-only
466+
// handles and slots bootstrapped with RAW_NULL. Ignore GC requests to
467+
// trigger a nice out-of-memory message instead of a crash in the middle of
468+
// visiting pointers.
469+
return;
470+
}
457471
if (BeginNewSpaceGC(thread)) {
458472
RecordBeforeGC(kScavenge, reason);
459473
{
@@ -484,6 +498,13 @@ void Heap::CollectOldSpaceGarbage(Thread* thread,
484498
if (FLAG_use_compactor) {
485499
type = kMarkCompact;
486500
}
501+
if (thread->isolate() == Dart::vm_isolate()) {
502+
// The vm isolate cannot safely collect garbage due to unvisited read-only
503+
// handles and slots bootstrapped with RAW_NULL. Ignore GC requests to
504+
// trigger a nice out-of-memory message instead of a crash in the middle of
505+
// visiting pointers.
506+
return;
507+
}
487508
if (BeginOldSpaceGC(thread)) {
488509
RecordBeforeGC(type, reason);
489510
VMTagScope tagScope(thread, reason == kIdle ? VMTag::kGCIdleTagId

runtime/vm/object.cc

+11-6
Original file line numberDiff line numberDiff line change
@@ -2156,12 +2156,17 @@ RawObject* Object::Allocate(intptr_t cls_id, intptr_t size, Heap::Space space) {
21562156
address = heap->Allocate(size, space);
21572157
}
21582158
if (UNLIKELY(address == 0)) {
2159-
// Use the preallocated out of memory exception to avoid calling
2160-
// into dart code or allocating any code.
2161-
const Instance& exception =
2162-
Instance::Handle(thread->isolate()->object_store()->out_of_memory());
2163-
Exceptions::Throw(thread, exception);
2164-
UNREACHABLE();
2159+
if (thread->top_exit_frame_info() != 0) {
2160+
// Use the preallocated out of memory exception to avoid calling
2161+
// into dart code or allocating any code.
2162+
const Instance& exception =
2163+
Instance::Handle(thread->isolate()->object_store()->out_of_memory());
2164+
Exceptions::Throw(thread, exception);
2165+
UNREACHABLE();
2166+
} else {
2167+
// No Dart to propagate an exception to.
2168+
OUT_OF_MEMORY();
2169+
}
21652170
}
21662171
#ifndef PRODUCT
21672172
ClassTable* class_table = thread->isolate()->class_table();

0 commit comments

Comments
 (0)