From 2c636f541e63a0182dce7edf0c04a2c63e45f474 Mon Sep 17 00:00:00 2001 From: Gang Zhao Date: Mon, 9 Dec 2024 17:13:35 -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/CardTableNC.h | 3 +++ lib/VM/gcs/CardTableNC.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/include/hermes/VM/CardTableNC.h b/include/hermes/VM/CardTableNC.h index 7bce327636a..6881873e7b8 100644 --- a/include/hermes/VM/CardTableNC.h +++ b/include/hermes/VM/CardTableNC.h @@ -278,6 +278,9 @@ class CardTable { /// /// \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 void *loc) const; #endif // HERMES_SLOW_DEBUG private: diff --git a/lib/VM/gcs/CardTableNC.cpp b/lib/VM/gcs/CardTableNC.cpp index 10937e15192..018ff2c5a06 100644 --- a/lib/VM/gcs/CardTableNC.cpp +++ b/lib/VM/gcs/CardTableNC.cpp @@ -172,6 +172,13 @@ void CardTable::verifyBoundaries(char *start, char *level) const { "Card object boundary is broken: first obj doesn't extend into card"); } } + +GCCell *CardTable::findObjectContaining(const void *loc) const { + GCCell *obj = firstObjForCard(addressToIndex(loc)); + while (obj->nextCell() < loc) + obj = obj->nextCell(); + return obj; +} #endif // HERMES_SLOW_DEBUG } // namespace vm