Bug 1593329 - Fix problems handling OOM related to FinalizationGroup r=sfink
authorJon Coppeard <jcoppeard@mozilla.com>
Fri, 08 Nov 2019 13:40:12 +0000
changeset 501285 52be2a1daacd90ef14c410106b0cb470464bb486
parent 501284 526d08f2c191cdf8f5bbf13c620d9f9d54562e24
child 501286 1de3fc411adadda1b2bb87f392d754039942b3fc
push id36784
push userrgurzau@mozilla.com
push dateFri, 08 Nov 2019 21:36:09 +0000
treeherdermozilla-central@f414b9e6d857 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfink
bugs1593329
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1593329 - Fix problems handling OOM related to FinalizationGroup r=sfink Differential Revision: https://phabricator.services.mozilla.com/D52021
js/src/builtin/FinalizationGroupObject.cpp
js/src/gc/FinalizationGroup.cpp
--- a/js/src/builtin/FinalizationGroupObject.cpp
+++ b/js/src/builtin/FinalizationGroupObject.cpp
@@ -210,32 +210,34 @@ bool FinalizationGroupObject::construct(
   if (!proto) {
     proto =
         GlobalObject::getOrCreateFinalizationGroupPrototype(cx, cx->global());
     if (!proto) {
       return false;
     }
   }
 
-  FinalizationGroupObject* group =
-      NewObjectWithClassProto<FinalizationGroupObject>(cx, proto);
-  if (!group) {
-    return false;
-  }
-
-  auto registrations = cx->make_unique<ObjectWeakMap>(cx);
+  Rooted<UniquePtr<ObjectWeakMap>> registrations(
+      cx, cx->make_unique<ObjectWeakMap>(cx));
   if (!registrations) {
     return false;
   }
 
-  auto holdings = cx->make_unique<HoldingsVector>(cx->zone());
+  Rooted<UniquePtr<HoldingsVector>> holdings(
+      cx, cx->make_unique<HoldingsVector>(cx->zone()));
   if (!holdings) {
     return false;
   }
 
+  FinalizationGroupObject* group =
+      NewObjectWithClassProto<FinalizationGroupObject>(cx, proto);
+  if (!group) {
+    return false;
+  }
+
   group->initReservedSlot(CleanupCallbackSlot, ObjectValue(*cleanupCallback));
   InitReservedSlot(group, RegistrationsSlot, registrations.release(),
                    MemoryUse::FinalizationGroupRegistrations);
   InitReservedSlot(group, HoldingsToBeCleanedUpSlot, holdings.release(),
                    MemoryUse::FinalizationGroupHoldingsVector);
   group->initReservedSlot(IsQueuedForCleanupSlot, BooleanValue(false));
   group->initReservedSlot(IsCleanupJobActiveSlot, BooleanValue(false));
 
@@ -418,17 +420,22 @@ bool FinalizationGroupObject::addRegistr
     recordsObject = &obj->as<FinalizationRecordVectorObject>();
   } else {
     recordsObject = FinalizationRecordVectorObject::create(cx);
     if (!recordsObject || !map.add(cx, unregisterToken, recordsObject)) {
       return false;
     }
   }
 
-  return recordsObject->append(record);
+  if (!recordsObject->append(record)) {
+    ReportOutOfMemory(cx);
+    return false;
+  }
+
+  return true;
 }
 
 /* static */ void FinalizationGroupObject::removeRegistrationOnError(
     HandleFinalizationGroupObject group, HandleObject unregisterToken,
     HandleFinalizationRecordObject record) {
   // Remove a registration if something went wrong before we added it to the
   // target zone's map. Note that this can't remove a registration after that
   // point.
--- a/js/src/gc/FinalizationGroup.cpp
+++ b/js/src/gc/FinalizationGroup.cpp
@@ -24,16 +24,17 @@ bool GCRuntime::registerWithFinalization
   MOZ_ASSERT(
       UncheckedUnwrapWithoutExpose(record)->is<FinalizationRecordObject>());
   MOZ_ASSERT(target->compartment() == record->compartment());
 
   auto& map = target->zone()->finalizationRecordMap();
   auto ptr = map.lookupForAdd(target);
   if (!ptr) {
     if (!map.add(ptr, target, FinalizationRecordVector(target->zone()))) {
+      ReportOutOfMemory(cx);
       return false;
     }
   }
   return ptr->value().append(record);
 }
 
 void GCRuntime::markFinalizationGroupData(JSTracer* trc) {
   // The finalization groups and holdings for all targets are marked as roots.