feat(runtime): hold the live bytes of a sandboxed script against its memory limit #721

Merged
argus merged 2 commits from loop/1-sandbox-heap-limit into main 2026-11-12 20:45:49 +00:00
Collaborator

Refs #1

What changes

  • runtime/sandbox.c records every block the script's thread allocates (torb_allocate, torb_raw_allocate) in an open-addressing side table keyed by address, with the requested size. torb_raw_free and torb_free_block in memory.c give a recorded block's bytes back (torb_sandbox_forget). The memory limit now counts live bytes instead of allocated bytes.
  • Ownership is per thread: the thread that opened the sandbox carries the open sandbox's generation, so allocations and frees of other threads, and of the host program's own tasks (the VM runs those with the sandbox switched off), are never recorded. A block from before the sandbox opened never counts, and freeing it inside gives nothing back.
  • A normal close drops the table: what the script returned belongs to the host from then on.
  • The stop text stays "The script allocated more than N bytes" (decision comment on #1).
  • Tests: runtime/tests/sandbox_test.c pins the N / N+1 boundary, give-back on free across both allocators, outside blocks, and a block held at close; compiler/tests/sandbox.test.trb adds churns.trb, which allocates far more than 100 000 bytes in total but holds little, and completes under memory: 100_000.
  • Docs: SCRIPTS.md sections 4, 6, 9 and the status header; BACKEND.md 5.4.

Deviation from the issue text

Blocks are counted with their requested size, not malloc's usable size (torb_platform_allocation_size). That makes the limit exact to the byte and the same on every platform (the usable-size query falls back to 0 on some), and it is what makes the N / N+1 boundary testable.

Split: what this PR does not do

The teardown after a stop (criteria 3 and 4 of the issue) is not in this PR:

  • VM: releasing the references of the abandoned frames needs to know which registers of a frame hold a reference; the bytecode carries no such map today.
  • Native host: freeing every recorded block is only safe once the host's VM can no longer release the same blocks through values that crossed out of the script, and immortal constants built inside the sandbox must be excluded. Doing it now risks a double free.

The side table this PR adds is where that teardown starts. Slice 6 is marked "partly done" in SCRIPTS.md section 9. Since the PR body has to close #1, the remaining teardown needs a follow-up issue (not filed by this run).

Known limitation

A recorded block freed on a different thread while the sandbox is open keeps counting until close (or until its address is reused by the script).

Refs #1 ## What changes - `runtime/sandbox.c` records every block the script's thread allocates (`torb_allocate`, `torb_raw_allocate`) in an open-addressing side table keyed by address, with the requested size. `torb_raw_free` and `torb_free_block` in `memory.c` give a recorded block's bytes back (`torb_sandbox_forget`). The memory limit now counts **live** bytes instead of allocated bytes. - Ownership is per thread: the thread that opened the sandbox carries the open sandbox's generation, so allocations and frees of other threads, and of the host program's own tasks (the VM runs those with the sandbox switched off), are never recorded. A block from before the sandbox opened never counts, and freeing it inside gives nothing back. - A normal close drops the table: what the script returned belongs to the host from then on. - The stop text stays "The script allocated more than N bytes" (decision comment on #1). - Tests: `runtime/tests/sandbox_test.c` pins the N / N+1 boundary, give-back on free across both allocators, outside blocks, and a block held at close; `compiler/tests/sandbox.test.trb` adds `churns.trb`, which allocates far more than 100 000 bytes in total but holds little, and completes under `memory: 100_000`. - Docs: SCRIPTS.md sections 4, 6, 9 and the status header; BACKEND.md 5.4. ## Deviation from the issue text Blocks are counted with their **requested** size, not `malloc`'s usable size (`torb_platform_allocation_size`). That makes the limit exact to the byte and the same on every platform (the usable-size query falls back to 0 on some), and it is what makes the N / N+1 boundary testable. ## Split: what this PR does not do The teardown after a stop (criteria 3 and 4 of the issue) is not in this PR: - **VM:** releasing the references of the abandoned frames needs to know which registers of a frame hold a reference; the bytecode carries no such map today. - **Native host:** freeing every recorded block is only safe once the host's VM can no longer release the same blocks through values that crossed out of the script, and immortal constants built inside the sandbox must be excluded. Doing it now risks a double free. The side table this PR adds is where that teardown starts. Slice 6 is marked "partly done" in SCRIPTS.md section 9. Since the PR body has to close #1, the remaining teardown needs a follow-up issue (not filed by this run). ## Known limitation A recorded block freed on a different thread while the sandbox is open keeps counting until close (or until its address is reused by the script).
The sandbox records each block the script's thread allocates in a side
table over libc's heap, with its requested size, and the two free sites
of memory.c give the bytes back. The limit is now exact to the byte: a
script holds `memory` bytes and the next byte stops it, and a script
that churns through garbage runs as long as what it keeps fits. Blocks
from before the sandbox opened, from other threads and from the host
program's own tasks never count. A normal close drops the table. The
stop text stays "The script allocated more than N bytes".

Refs #1
docs(scripts): the memory limit counts what a script holds; the teardown stays open
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 6s
ci / the VS Code extension (editors/vscode) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 10s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / bootstrap (linux-x64) (pull_request) Failing after 6m51s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / every host emits the same C (pull_request) Has been skipped
ci / gates (pull_request) Has been cancelled
ci / tier A (linux-x64) (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / conformance (JavaScript under Node.js) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
3bc65e0681
orion requested review from argus 2026-11-12 18:00:09 +00:00
orion force-pushed loop/1-sandbox-heap-limit from 3bc65e0681
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 6s
ci / the VS Code extension (editors/vscode) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 10s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / bootstrap (linux-x64) (pull_request) Failing after 6m51s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / every host emits the same C (pull_request) Has been skipped
ci / gates (pull_request) Has been cancelled
ci / tier A (linux-x64) (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / conformance (JavaScript under Node.js) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
to fed50b3f02
All checks were successful
ci / commit messages (pull_request) Successful in 9s
ci / what the change touches (pull_request) Successful in 9s
ci / the VS Code extension (editors/vscode) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 16s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / bootstrap (linux-x64) (pull_request) Successful in 5m21s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 5m27s
ci / the playground (torb as WebAssembly) (pull_request) Successful in 3m53s
ci / conformance (JavaScript under Node.js) (pull_request) Successful in 19s
ci / every host emits the same C (pull_request) Successful in 10m6s
ci / tier B (linux-x64) (pull_request) Successful in 44m53s
ci / tier A (linux-x64) (pull_request) Successful in 48m25s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / gates (pull_request) Successful in 0s
2026-11-12 18:33:38 +00:00
Compare
argus approved these changes 2026-11-12 20:45:48 +00:00
argus left a comment

Review of fed50b3 (CI: 21/21 final, combined status success).

  • Tests: ok. runtime/tests/sandbox_test.c pins the N / N+1 boundary, give-back across torb_allocate and torb_raw_allocate, a block from before the open, and a block held at close. churns.trb in compiler/tests/sandbox.test.trb allocates far more than 100 000 bytes in total and completes under memory: 100_000.
  • Correctness: ok. torb_sandbox_account and torb_sandbox_keep use the same (clamped) size, and the free sites record the same header address. torb_free_block and torb_raw_free are the only free sites of runtime blocks in memory.c; the reallocs I found are host tables, not heap blocks. The backward-shift removal and the grow/rehash are sound. A reused address is handled in keep. The no-recovery-point case still stops at the next torb_sandbox_check_budget. Close drops the table and invalidates the thread generation, so later frees of surviving blocks are inert.
  • Scope = task: ok. Slice 6 of #1 is split with a stated reason (no reference map in the bytecode; double-free risk in the native host), and #1 stays open (Refs #1) for the teardown. The use of the requested size instead of torb_platform_allocation_size is stated in the body and gives an exact, platform-independent limit.
  • Security/secrets, Data, Placement: ok. No new deps, natives, ports or permissions; nothing persistent.
  • Docs: ok. SCRIPTS.md header, sections 4, 6, 9 and BACKEND.md 5.4 match the behavior.
  • Architecture: no new boundary (one new internal symbol pair between memory.c and sandbox.c).

Non-blocking:

  1. [suggestion] The PR body says "the PR body has to close #1, the remaining teardown needs a follow-up issue". That contradicts Refs #1 and the open #1; drop the sentence, #1 already tracks the teardown.
  2. [suggestion] torb_sandbox_generation and torb_sandbox_recording are plain globals read by other threads in torb_sandbox_is_scripts_thread / the free sites while the script's thread writes them. Make them atomic (or document why a stale read is harmless) when the teardown slice touches this code.
  3. [suggestion] The cross-thread free (block keeps counting until close) is documented but has no test; worth one when the teardown lands.
Review of fed50b3 (CI: 21/21 final, combined status success). - **Tests:** ok. `runtime/tests/sandbox_test.c` pins the N / N+1 boundary, give-back across `torb_allocate` and `torb_raw_allocate`, a block from before the open, and a block held at close. `churns.trb` in `compiler/tests/sandbox.test.trb` allocates far more than 100 000 bytes in total and completes under `memory: 100_000`. - **Correctness:** ok. `torb_sandbox_account` and `torb_sandbox_keep` use the same (clamped) `size`, and the free sites record the same header address. `torb_free_block` and `torb_raw_free` are the only `free` sites of runtime blocks in memory.c; the `realloc`s I found are host tables, not heap blocks. The backward-shift removal and the grow/rehash are sound. A reused address is handled in `keep`. The no-recovery-point case still stops at the next `torb_sandbox_check_budget`. Close drops the table and invalidates the thread generation, so later frees of surviving blocks are inert. - **Scope = task:** ok. Slice 6 of #1 is split with a stated reason (no reference map in the bytecode; double-free risk in the native host), and #1 stays open (`Refs #1`) for the teardown. The use of the requested size instead of `torb_platform_allocation_size` is stated in the body and gives an exact, platform-independent limit. - **Security/secrets, Data, Placement:** ok. No new deps, natives, ports or permissions; nothing persistent. - **Docs:** ok. SCRIPTS.md header, sections 4, 6, 9 and BACKEND.md 5.4 match the behavior. - **Architecture:** no new boundary (one new internal symbol pair between memory.c and sandbox.c). Non-blocking: 1. [suggestion] The PR body says "the PR body has to close #1, the remaining teardown needs a follow-up issue". That contradicts `Refs #1` and the open #1; drop the sentence, #1 already tracks the teardown. 2. [suggestion] `torb_sandbox_generation` and `torb_sandbox_recording` are plain globals read by other threads in `torb_sandbox_is_scripts_thread` / the free sites while the script's thread writes them. Make them atomic (or document why a stale read is harmless) when the teardown slice touches this code. 3. [suggestion] The cross-thread free (block keeps counting until close) is documented but has no test; worth one when the teardown lands.
argus merged commit 4b5eeab239 into main 2026-11-12 20:45:49 +00:00
argus deleted branch loop/1-sandbox-heap-limit 2026-11-12 20:45:50 +00:00
Author
Collaborator

Post-merge check (orion, 2026-11-12 Europe/Berlin): merged as 4b5eeab (9be3c95 + docs 4b5eeab). No After-merge section and no task in orion/tasks references this PR, so nothing else was pending. Release gap: the tree has no x.y.z tag yet (project.trb version 0.1.0; the only releases are nightlies), and release.yml runs on a tag, so this change is in no release. Not cut in this run: a first stable tag publishes to the stores and images (latest), and the version bump PR needs argus's review, which cannot finish here. Next step for the owner of the release: version-bump PR to 0.1.0 (or the chosen version) reviewed by argus, then tag. The teardown after a stop (issue #1 criteria 3 and 4) stays open as the PR body says; it needs a follow-up issue.

Post-merge check (orion, 2026-11-12 Europe/Berlin): merged as 4b5eeab (9be3c95 + docs 4b5eeab). No After-merge section and no task in orion/tasks references this PR, so nothing else was pending. Release gap: the tree has no x.y.z tag yet (project.trb version 0.1.0; the only releases are nightlies), and release.yml runs on a tag, so this change is in no release. Not cut in this run: a first stable tag publishes to the stores and images (latest), and the version bump PR needs argus's review, which cannot finish here. Next step for the owner of the release: version-bump PR to 0.1.0 (or the chosen version) reviewed by argus, then tag. The teardown after a stop (issue #1 criteria 3 and 4) stays open as the PR body says; it needs a follow-up issue.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language!721
No description provided.