Skip to content

Keep the stack check within the stack in two more cases - #22

Merged
marcobambini merged 3 commits into
sqliteai:mainfrom
davidschachterADFA:keep-stack-check-within-stack
Nov 13, 2026
Merged

marcobambini merged 3 commits into
sqliteai:mainfrom
davidschachterADFA:keep-stack-check-within-stack

Conversation

@davidschachterADFA

Copy link
Copy Markdown
Contributor

Code that calls itself without end can still crash the process instead of failing with "Maximum call stack size exceeded", in two cases:

  • A thread with less than 64 KB of stack left. js_stack_room() then reports less than SQLITE_JS_STACK_MIN, and js_enter() keeps the connection's size, 1 MB by default, which that stack cannot hold. On a thread with a 64 KB stack, SELECT forever() (from Stack on other threads) crashes. js_enter() now keeps any measurement, or 1 rather than 0, which would turn the check off. The minimum still applies where a runtime is created, since QuickJS needs it to create a context.
  • The main thread on Linux, first measured deep in its stack. RLIMIT_STACK is counted from sp at the first measurement, so the bound lands below the real one by that depth. With the extension loaded 2 MB deep, js_set_max_stack_size() allowed up to 10.2 MB of an 8 MB stack, and forever() at that size crashed. At 6 MB deep it allowed 14.4 MB. The bound is now counted from the stack's top (addr + size from pthread_attr_getstack), as the kernel counts it. That is also where glibc and bionic put it, and musl's addr + size is the top of the initial stack too.

Test: Stack on other threads now also runs forever() on a 64 KB thread, a connection opened on the main thread. It crashes without this change. The main-thread case cannot be tested in the suite, since each thread measures its stack once and the suite's main thread has measured long before. A separate program that loads the extension 0, 2 and 6 MB deep shows the largest size allowed as about 7.75 MB of 8 MB after the change, where forever() stops with the error.

The suite passes 15 of 15 on x86_64 Linux on top of 1.4.1, also under ASan and UBSan.

This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

On the initial thread on Linux, RLIMIT_STACK is now counted from the top
of the stack, as the kernel counts it, rather than from where the first
measurement was taken. Counted from there, a first measurement deep in
the stack put the bound below the real one by that depth: with the
extension loaded 2 MB deep on the main thread, js_set_max_stack_size()
allowed 10 MB of an 8 MB stack, and code that called itself without end
at that size crashed the process.

A call into JavaScript on a thread with less room than
SQLITE_JS_STACK_MIN now keeps the measured room rather than the size
set for the connection, 1 MB by default: on a thread with a 64 KB stack,
code that called itself without end crashed. JavaScript there now fails
with "Maximum call stack size exceeded". The minimum still applies where
a runtime is created, since QuickJS cannot create a context in less.

Stack on other threads now also runs forever() on a 64 KB thread. It
crashes without this change and passes with it, also under ASan and
UBSan, and the suite passes 16 of 16. The first case is checked by a
program that loads the extension deep in the main thread's stack: at 2
and 6 MB deep, the largest size allowed was 10.2 and 14.4 MB of 8 MB
before, and about 7.75 MB after, where forever() stops with an error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
marcobambini and others added 2 commits November 13, 2026 08:18
… small

The stack-on-other-threads test asks for a thread with a 64 KB stack, which
aarch64 glibc refuses: PTHREAD_STACK_MIN is 128 KB there, so
pthread_attr_setstacksize() fails with EINVAL and the test reported FAILED
on arm64 Linux. stack_thread() returned SQLITE_ERROR without saying why, and
the test then printed the stale error message of the previous case, so the
failure looked like a stack check gone wrong rather than a thread that could
not be created.

stack_thread() now tells a stack the platform has no room for from a real
failure, returning SQLITE_NOTFOUND and saying so, and the 64 KB case skips
on that. The other failure paths say what went wrong too. The case still
runs where a 64 KB stack can be had, which is where it catches the bug: on
x86_64 glibc it crashes the process without the fix in this branch.

Co-Authored-By: Claude Opus 5 <[email protected]>
…from

Two cases where the bound for the initial thread still allowed more stack
than the thread has, so js_set_max_stack_size() offered a size at which
runaway recursion crashes the process instead of failing with "Maximum call
stack size exceeded".

A large argv and environment block. pthread_attr_getstack() reaches only to
the top of what the program may use, which is below the block of argv,
environment and auxiliary vector that the kernel puts at the top of the
stack, while RLIMIT_STACK is counted from above that block. The bound
therefore landed below the real one by the size of the block, which Linux
lets grow to a quarter of RLIMIT_STACK: 2 MB of an 8 MB stack. Measured on
glibc, where pthread_attr_getstack() reports the real floor as addr and the
current top of the usable region as addr + size, so that addr + size is not
the stack's top at all. With 240 KB of environment the error still fits in
SQLITE_JS_STACK_MARGIN; with 360 KB, forever() crashed. The top now comes
from the kernel's own mapping in /proc/self/maps, which is how glibc finds
the stack in the first place, and pthread's is only the fallback.

No finite RLIMIT_STACK. The bound was then left at what
pthread_attr_getstack() reported, which on glibc is nearly the whole address
space: js_set_max_stack_size() offered and accepted about 85 TB, and
forever() took the memory of the machine until the kernel killed the
process. Such a stack now counts as unmeasurable, which allows up to the
1 MB default the thread certainly has; js_set_max_stack_size(0) still turns
the check off for anyone who wants that.

Checked with a sweep of 14 cases, each in a process of its own, over thread
stacks from 128 KB to 8 MB, the initial thread entered 0 to 6 MB deep, and
JavaScript that runs SQL that runs JavaScript: all clean on glibc and musl,
x86_64 and arm64, where five of the 14 crashed before the parent commit and
the environment cases crashed after it. Neither case can be tested from the
suite, which measures its own main thread long before a test runs, as the
parent commit's own main-thread case cannot.

Co-Authored-By: Claude Opus 5 <[email protected]>
@marcobambini
marcobambini merged commit 9c363b4 into sqliteai:main Nov 13, 2026
@marcobambini

Copy link
Copy Markdown
Member

Merged, thank you — the bug is real and wider than the description claims. A sweep of 14 cases, each in a process of its own, over thread stacks from 128 KB to 8 MB and the initial thread entered 0 to 6 MB deep: without this branch 5 of the 14 crash the process on all four of glibc and musl × arm64 and x86_64, and the initial-thread case crashes at 1 MB deep, not only at the 2 MB the description gives. With it, all 14 stop with "Maximum call stack size exceeded".

Two commits were pushed to this branch before merging, since maintainer edits were allowed. Both are in the merge:

bd3d6c4 — skip the 64 KB thread where the platform has no stack that small. The new test asks for a thread with a 64 KB stack, which aarch64 glibc refuses: PTHREAD_STACK_MIN is 131072 there (and a run-time value, not a constant), so pthread_attr_setstacksize() fails with EINVAL and Stack on other threads reported FAILED on arm64 Linux. stack_thread() returned SQLITE_ERROR without saying why, and the test then printed the stale error message of the previous case, so it looked like a stack check gone wrong rather than a thread that could not be created. It now tells a stack the platform has no room for from a real failure and skips that case, and says so. The case still runs where a 64 KB stack can be had, which is where it catches the bug: on x86_64 glibc it crashes without the fix. Worth knowing for Code on the Go, since the AAR is arm64.

31ed714 — count the initial thread's stack from the top the kernel counts from. Two cases where the new bound still allowed more stack than the thread has.

pthread_attr_getstack() reaches only to the top of what the program may use, which is below the block of argv, environment and auxiliary vector the kernel puts at the top of the stack, while RLIMIT_STACK is counted from above that block. Measured on glibc with 8 × 120 KB of environment, where addr is already the real floor and addr + size is roughly the current stack pointer:

kernel [stack]           = 0x…d24b4000-0x…d25bf000
floor (hi - rlimit)      = 0x…d1dbf000
getstack addr            = 0x…d1dbf000   <- already the floor
getstack addr + size     = 0x…d24d4000   <- ~sp, not the top of the stack
addr + size - rlimit     = 0x…d1cd4000   <- 0.92 MB BELOW the real floor

240 KB of environment still fits in SQLITE_JS_STACK_MARGIN; with 360 KB, forever() at the size this branch offered crashed, on glibc and musl, arm64 and x86_64. Linux lets argv+env grow to a quarter of RLIMIT_STACK, so the error can reach 2 MB of an 8 MB stack. The top now comes from the kernel's own mapping in /proc/self/maps, which is how glibc finds the stack in the first place, with pthread's as the fallback.

And with no finite RLIMIT_STACK there is no bound to measure: glibc then reports nearly the whole address space, js_set_max_stack_size() offered and accepted about 85 TB, and forever() took the memory of the machine until the kernel killed the process — present on 1.4.1 too, so not something this branch introduced. Such a stack now counts as unmeasurable, which allows up to the 1 MB default the thread certainly has.

Note for future pull requests: this workflow has only a push trigger, so a pull request from a fork never runs it — #22, #23 and #24 all show "no checks reported". This branch went through the 14 jobs before merging by pushing main-with-it-merged to a throwaway branch in this repo, all green. Adding a pull_request trigger is on the list.

Reviewed and tested with Claude, Anthropic's AI model: the suite plus a stack sweep, an AddressSanitizer run and a separate main-thread program, on macOS arm64 and on Linux glibc and musl in both arm64 and x86_64 through Docker.

Penguinjanator pushed a commit to Penguinjanator/sqlite-js that referenced this pull request Nov 14, 2026
A pull request never ran this workflow: the only trigger was a push, and a
push to a fork does not run the workflow of the base repository. Pull
requests sqliteai#22, sqliteai#23 and sqliteai#24 all show "no checks reported" and were merged
without a single job having seen them; the jobs ran on main afterwards.
That is how the test that sqliteai#22 added reached main-adjacent review still
failing on arm64 Linux, which linux-arm64 and linux-musl-arm64 would have
caught at once.

Pushes are now limited to main, since a pull request from a branch of this
repository would otherwise run all 14 jobs twice, once for the push and once
for the pull request. To run the jobs on a branch of its own, as a dry run
of a fork's pull request for instance, use the workflow_dispatch that is
already here: gh workflow run main.yml --ref <branch>.

The signing steps are skipped where there are no secrets to sign with, which
is every pull request from a fork: the build and the tests still run, which
is what the pull request is for, and without this the six macOS, iOS and
xcframework jobs would fail on every outside contribution. Their artifacts
are then unsigned, and the release job runs only for main, so an unsigned
artifact is never released. A secret can be read in a job's env but not in a
step's if, which is why SIGNING is an env entry.

This takes effect for pull requests opened after it is merged: GitHub reads
the workflow of the base branch for a pull_request event, so this branch
cannot run its own new trigger. It was checked with a workflow_dispatch run
on the branch instead.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants