Skip to content

Commit

Permalink
Add ability to fill YG with invalid values after collection
Browse files Browse the repository at this point in the history
Summary:
Add a config in the GC to overwrite the YG with `kInvalidHeapValue`
after all objects have been evacuated. This is sufficiently lightweight
to be turned on in production if needed to identify memory safety
issues.

Reviewed By: jpporto

Differential Revision: D38009831

fbshipit-source-id: 483daa6b72697d02d9116b19966fddb79730d916
  • Loading branch information
neildhar authored and facebook-github-bot committed Jul 21, 2022
1 parent e4bec17 commit 248cf25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/hermes/VM/HadesGC.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ class HadesGC final : public GCBase {
/// If true, turn off promoteYGToOG_ as soon as the first OG GC occurs.
bool revertToYGAtTTI_;

/// If true, overwrite the allocation region in the YG with kInvalidHeapValue
/// at the end of each YG collection.
bool overwriteDeadYGObjects_;

/// Target OG occupancy ratio at the end of an OG collection.
const double occupancyTarget_;

Expand Down
5 changes: 5 additions & 0 deletions lib/VM/gcs/HadesGC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ HadesGC::HadesGC(
kConcurrentGC ? std::make_unique<Executor>() : nullptr},
promoteYGToOG_{!gcConfig.getAllocInYoung()},
revertToYGAtTTI_{gcConfig.getRevertToYGAtTTI()},
overwriteDeadYGObjects_{gcConfig.getOverwriteDeadYGObjects()},
occupancyTarget_(gcConfig.getOccupancyTarget()),
ygAverageSurvivalBytes_{
/*weight*/ 0.5,
Expand Down Expand Up @@ -2503,6 +2504,10 @@ void HadesGC::youngGenCollection(
// This was modified by debitExternalMemoryFromFinalizer, called by
// finalizers. The difference in the value before to now was the swept bytes
externalBytes.after = getYoungGenExternalBytes();

if (overwriteDeadYGObjects_)
memset(yg.start(), kInvalidHeapValue, yg.used());

// Now the copy list is drained, and all references point to the old
// gen. Clear the level of the young gen.
yg.resetLevel();
Expand Down
3 changes: 3 additions & 0 deletions public/hermes/Public/GCConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ enum class GCEventKind {
/* old gen (false). */ \
F(constexpr, bool, AllocInYoung, true) \
\
/* Whether to fill the YG with invalid data after each collection. */ \
F(constexpr, bool, OverwriteDeadYGObjects, false) \
\
/* Whether to revert, if necessary, to young-gen allocation at TTI. */ \
F(constexpr, bool, RevertToYGAtTTI, false) \
\
Expand Down

0 comments on commit 248cf25

Please sign in to comment.