From 4b1bcc03888e6f84585e77ad6826d55cdc30ede5 Mon Sep 17 00:00:00 2001 From: Gang Zhao Date: Mon, 3 Feb 2025 21:24:17 -0800 Subject: [PATCH] Add a findObject function to CardTable for assertions in writer barriers (#1513) Summary: Pull Request resolved: https://github.com/facebook/hermes/pull/1513 Differential Revision: D62169632 --- include/hermes/VM/AlignedHeapSegment.h | 10 ++++++++++ include/hermes/VM/CardBoundaryTable.h | 6 ++++++ lib/VM/gcs/CardBoundaryTable.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+) diff --git a/include/hermes/VM/AlignedHeapSegment.h b/include/hermes/VM/AlignedHeapSegment.h index fdf6f7d779d..feb404f6d87 100644 --- a/include/hermes/VM/AlignedHeapSegment.h +++ b/include/hermes/VM/AlignedHeapSegment.h @@ -742,6 +742,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