Skip to content

Commit de05551

Browse files
tsaichienmeta-codesync[bot]
authored andcommitted
Use a RAII class to track StackOverflowGuard depths
Summary: `StackOverflowGuard` has two types of heuristics. If `HERMES_CHECK_NATIVE_STACK` is defined, it will use real stack checking using the current stack address. Otherwise, it will use a simple depth count. In the latter, users of the stack overflow guard are expected to check for `HERMES_CHECK_NATIVE_STACK`, then increment/decrement the depth count if necessary. This change introduces a simple RAII class that will handle this and makes it simpler to use. Reviewed By: avp Differential Revision: D82996952 fbshipit-source-id: d058403c1d86f1a3ca77a5dacc14115e6641b5ef
1 parent 14f9d2d commit de05551

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

include/hermes/Support/StackOverflowGuard.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ class StackOverflowGuard {
115115
inline bool isOverflowing() {
116116
return callDepth > maxCallDepth;
117117
}
118+
119+
class CallFrameRAII;
120+
};
121+
122+
/// A simple RAII class to help users increment/decrement frame depth if
123+
/// necessary.
124+
class [[nodiscard]] StackOverflowGuard::CallFrameRAII {
125+
StackOverflowGuard &guard_;
126+
127+
public:
128+
explicit CallFrameRAII(StackOverflowGuard &guard) : guard_(guard) {
129+
++guard_.callDepth;
130+
}
131+
132+
~CallFrameRAII() {
133+
--guard_.callDepth;
134+
}
118135
};
119136

120137
#endif

include/hermes/VM/Runtime.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,20 +1733,21 @@ class ScopedNativeDepthTracker {
17331733
Runtime &runtime_;
17341734
/// Whether the stack overflowed when the tracker was constructed.
17351735
bool overflowed_;
1736+
#ifndef HERMES_CHECK_NATIVE_STACK
1737+
StackOverflowGuard::CallFrameRAII frame_;
1738+
#endif
17361739

17371740
public:
1738-
explicit ScopedNativeDepthTracker(Runtime &runtime) : runtime_(runtime) {
1739-
(void)runtime_;
1741+
explicit ScopedNativeDepthTracker(Runtime &runtime)
1742+
: runtime_(runtime)
17401743
#ifndef HERMES_CHECK_NATIVE_STACK
1741-
++runtime.overflowGuard_.callDepth;
1744+
,
1745+
frame_(StackOverflowGuard::CallFrameRAII{runtime_.overflowGuard_})
17421746
#endif
1747+
{
1748+
(void)runtime_;
17431749
overflowed_ = runtime.isStackOverflowing();
17441750
}
1745-
~ScopedNativeDepthTracker() {
1746-
#ifndef HERMES_CHECK_NATIVE_STACK
1747-
--runtime_.overflowGuard_.callDepth;
1748-
#endif
1749-
}
17501751

17511752
/// \return whether we overflowed the native call frame depth.
17521753
bool overflowed() const {

lib/AST/ESTreeJSONDumper.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ class ESTreeJSONDumper {
202202
return;
203203
}
204204
#ifndef HERMES_CHECK_NATIVE_STACK
205-
++stackOverflowGuard_.callDepth;
206-
auto decrementDepth =
207-
llvh::make_scope_exit([this] { --stackOverflowGuard_.callDepth; });
205+
StackOverflowGuard::CallFrameRAII callFrame{stackOverflowGuard_};
208206
#endif
209207
if (stackOverflowGuard_.isOverflowing()) {
210208
json_.emitNullValue();

lib/Regex/Executor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,7 @@ auto Context<Traits>::match(State<Traits> *s, bool onlyAtStart)
999999
"Can only check one location when cursor is backwards");
10001000

10011001
#ifndef HERMES_CHECK_NATIVE_STACK
1002-
++overflowGuard_.callDepth;
1003-
auto decrement =
1004-
llvh::make_scope_exit([this] { --overflowGuard_.callDepth; });
1002+
StackOverflowGuard::CallFrameRAII frame{overflowGuard_};
10051003
#endif
10061004

10071005
// Make sure we are not exceeding the set limit of the amount of times we can

0 commit comments

Comments
 (0)