diff --git a/include/hermes/VM/AlignedHeapSegment.h b/include/hermes/VM/AlignedHeapSegment.h index 8d3d6fb8c70..7361da7b862 100644 --- a/include/hermes/VM/AlignedHeapSegment.h +++ b/include/hermes/VM/AlignedHeapSegment.h @@ -747,6 +747,16 @@ class FixedSizeHeapSegment : public AlignedHeapSegment { static void checkUnwritten(char *start, char *end); #endif +#ifdef HERMES_SLOW_DEBUG + /// Find the object containing \p loc. + static GCCell *findObjectContaining(const void *loc) { + auto *lowLim = static_cast(storageStart(loc)); + auto *hiLim = lowLim + kSize; + return contents(lowLim)->boundaryTable_.findObjectContaining( + lowLim, hiLim, loc); + } +#endif + private: FixedSizeHeapSegment(StorageProvider *provider, void *lowLim); }; diff --git a/include/hermes/VM/CardBoundaryTable.h b/include/hermes/VM/CardBoundaryTable.h index ea31adc46ec..422fe53fd26 100644 --- a/include/hermes/VM/CardBoundaryTable.h +++ b/include/hermes/VM/CardBoundaryTable.h @@ -109,6 +109,12 @@ class CardBoundaryTable { /// /// \pre start is card-aligned. void verifyBoundaries(char *start, char *level) const; + + /// Find the object that owns the memory at \p loc. + GCCell *findObjectContaining( + const char *lowLim, + const char *hiLim, + const void *loc) const; #endif // HERMES_SLOW_DEBUG #ifndef UNIT_TEST diff --git a/lib/VM/gcs/CardBoundaryTable.cpp b/lib/VM/gcs/CardBoundaryTable.cpp index d5a1efeb172..f9fe5ac348d 100644 --- a/lib/VM/gcs/CardBoundaryTable.cpp +++ b/lib/VM/gcs/CardBoundaryTable.cpp @@ -110,6 +110,16 @@ void CardBoundaryTable::verifyBoundaries(char *start, char *level) const { "Card object boundary is broken: first obj doesn't extend into card"); } } + +GCCell *CardBoundaryTable::findObjectContaining( + const char *lowLim, + const char *hiLim, + const void *loc) const { + GCCell *obj = firstObjForCard(lowLim, hiLim, addressToIndex(loc)); + while (obj->nextCell() < loc) + obj = obj->nextCell(); + return obj; +} #endif // HERMES_SLOW_DEBUG } // namespace vm