Skip to content
This repository was archived by the owner on Nov 9, 2026. It is now read-only.

Commit 3cf11ba

Browse files
committed
QuickJS: measure the JS stack budget from the current stack pointer to the thread's base (the nominal size under-budgeted the Android runtime thread: depth-128 recursion hit the limit)
1 parent 5cd790d commit 3cf11ba

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

‎Core/AppRuntime/Source/AppRuntime_QuickJS.cpp‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,48 @@ namespace Babylon
4848
// (SIGSEGV) before QuickJS can raise "InternalError: stack overflow". Derive the limit
4949
// from the thread that actually runs the runtime instead, keeping a margin for the native
5050
// frames QuickJS and the host add between the check and the guard page.
51+
// Bytes of C stack below the current frame, measured from the actual stack pointer to the
52+
// thread's stack base, minus a margin for the native frames QuickJS and the host add between
53+
// the check and the guard page. Measuring from the current position (rather than trusting
54+
// the nominal size) also absorbs whatever the host already consumed above this call.
5155
size_t JavaScriptStackLimit()
5256
{
53-
constexpr size_t Margin{256 * 1024};
57+
constexpr size_t Margin{96 * 1024};
5458
constexpr size_t Fallback{256 * 1024};
55-
size_t threadStack{};
59+
const auto here = reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
60+
uintptr_t base{};
5661
#if defined(_WIN32)
5762
ULONG_PTR low{};
5863
ULONG_PTR high{};
5964
GetCurrentThreadStackLimits(&low, &high);
60-
threadStack = static_cast<size_t>(high - low);
65+
base = static_cast<uintptr_t>(low);
6166
#elif defined(__APPLE__)
62-
// pthread_getattr_np is a GNU/bionic extension; Apple exposes the size directly.
63-
threadStack = pthread_get_stacksize_np(pthread_self());
67+
// pthread_getattr_np is a GNU/bionic extension; Apple exposes the bounds directly.
68+
const auto top = reinterpret_cast<uintptr_t>(pthread_get_stackaddr_np(pthread_self()));
69+
base = top - pthread_get_stacksize_np(pthread_self());
6470
#else
6571
pthread_attr_t attributes;
6672
if (pthread_getattr_np(pthread_self(), &attributes) == 0)
6773
{
68-
void* base{};
74+
void* address{};
6975
size_t size{};
70-
if (pthread_attr_getstack(&attributes, &base, &size) == 0)
76+
if (pthread_attr_getstack(&attributes, &address, &size) == 0)
7177
{
72-
threadStack = size;
78+
base = reinterpret_cast<uintptr_t>(address);
7379
}
7480
pthread_attr_destroy(&attributes);
7581
}
7682
#endif
77-
if (threadStack == 0)
83+
if (base == 0 || here <= base)
7884
{
7985
return Fallback; // unknown: conservative, well under any plausible thread
8086
}
81-
if (threadStack <= 2 * Margin)
87+
const size_t usable = here - base;
88+
if (usable <= 2 * Margin)
8289
{
83-
return threadStack / 2; // a known small stack must not get a limit larger than itself
90+
return usable / 2; // a known small stack must not get a limit larger than itself
8491
}
85-
return (std::min)(threadStack - Margin, static_cast<size_t>(JS_DEFAULT_STACK_SIZE) * 8);
92+
return (std::min)(usable - Margin, static_cast<size_t>(JS_DEFAULT_STACK_SIZE) * 8);
8693
}
8794
}
8895

0 commit comments

Comments
 (0)