sandbox: give a script its own heap with an exact memory limit #1

Closed
opened 2026-10-05 21:06:59 +00:00 by orion · 14 comments
Collaborator

Goal

A sandboxed script's memory limit counts what the script holds (live bytes), not what it has ever allocated, and the memory of a script that stopped is given back at teardown where nothing outside the sandbox holds it. Today torb_sandbox_account only adds allocated bytes (runtime/sandbox.c), because free does not know a block's size, so a script that churns through garbage hits the limit while holding almost nothing, and a stop leaks what the script held (SCRIPTS.md section 4 "Memory is allocation, not residency, for now", section 6 "A stop leaks what the script held").

Implements

  • SCRIPTS.md slice 6: "The sandbox's own heap: an exact memory limit, a teardown that frees what a stopped script held" (state: Open)
  • ROADMAP.md, milestone 7, open work "the sandbox's own heap"
  • BACKEND.md section 5.4: "memory from the sandbox's own heap (its own allocator, so the limit is exact and the teardown is one free)"; SCRIPTS.md wins where they differ.

Decided design (accounting over libc's heap, owner in a side table)

Decided by the loop, see the comment decided by the loop (option 2 of the earlier proposal). No arena allocator: SCRIPTS.md section 5 passes the value by reference inside one VM, so blocks a script allocated may be reached by the caller afterwards, and a literal "one free" teardown would leave dangling blocks. The value-passing decision of section 5 stays as it is.

  • While a sandbox is open every block torb_allocate / torb_raw_allocate hands out is recorded (pointer set in runtime/sandbox.c, size from torb_platform_allocation_size, as the counted memory limit of runtime/memory.c already does). The two free sites of memory.c (torb_free_block, torb_raw_free) ask the sandbox: a free of a recorded block gives its bytes back and drops the record, a free of any other block never lowers the count.
  • Normal close: the table is dropped, the surviving blocks are ordinary blocks of the host.
  • Stop (limit, panic, Process.exit) in a program the VM runs: the abandoned frames' references are released at teardown, so refcounting frees what nothing else holds; a block the caller's value reaches stays valid. What cycles or the caller keep alive stays (bounded by the limit) and is documented.
  • Stop in a native binary that hosts scripts (--embed-vm, only text crosses, SCRIPTS.md section 5): every block still recorded is freed at teardown.
  • The SandboxError text stays "The script allocated more than N bytes" (existing tests and users match it; it now fires when the live bytes exceed N).

Acceptance criteria

  • While a sandbox is open, the live bytes of the blocks the script allocated (torb_allocate, torb_raw_allocate, sized by torb_platform_allocation_size) are the number checked against the memory limit. A block freed while the sandbox is open gives its bytes back; a block allocated outside the sandbox never counts, and freeing it never lowers the count. Exact in the VM (torb run, torb test) and in a native binary that hosts scripts.
  • A script that allocates and drops far more than the limit in total, but never holds more than it, runs to its end. A script that holds more than the limit stops with the SandboxError "The script allocated more than N bytes", never the process (compiler/tests/sandbox.test.trb, both cases, exact byte boundary: N bytes held passes, N+1 stops).
  • A script that stops (limit, panic, Process.exit) while holding blocks nothing outside the sandbox reaches: torb_live_block_count is back at its value before the script ran (VM-only conformance program or runtime test). A block of the script that the caller's value reaches stays valid and readable after the stop (no use after free; same test file).
  • The native-host teardown (every recorded block freed on a stop) has a runtime test: open, allocate, stop, close, live-block count unchanged.
  • The existing sandbox tests, tests/conformance/sandbox-crossing.trb and tests/conformance/vm-only/sandbox-*.trb stay green; tier A and tier B gates green in forge CI (runtime/ and the VM change).
  • SCRIPTS.md slice 6 marked Done (header status, sections 4 and 6 reworded from "allocation, not residency" and "a stop leaks", the remaining bounded leak of the shared-value case named), BACKEND.md 5.4 and VM.md section 10 brought in line; build/release/torb.exe docs skill docs skills regenerated if a skill page changes.

Seed

Settled: does not touch the seed. No syntax change, no new native (the kernel operations SandboxOpen/SandboxClose exist and keep their signatures; a change to them is an operation, SCRIPTS.md section 5), no new driver flag, no std name looked up by string. The work is runtime/memory.c, runtime/sandbox.c, runtime/machine.c and the VM's TorbScript where it calls them. If the implementation turns out to need a new native or a changed std/sandbox name the compiler looks up, it stops and files a seed question instead of going on.

Size

One PR, two Conventional Commits: feat(runtime): count the live bytes a sandboxed script holds (record table, free-site hooks, exact-limit tests) and feat(runtime): free what a stopped script held at teardown (teardown in the VM and the native host, leak-count tests, docs). runtime/ and the VM: opus/high worker. No split needed.

Moved from the planning lists of the docs; the design record stays the specification.

## Goal A sandboxed script's memory limit counts what the script **holds** (live bytes), not what it has ever allocated, and the memory of a script that stopped is given back at teardown where nothing outside the sandbox holds it. Today `torb_sandbox_account` only adds allocated bytes (`runtime/sandbox.c`), because `free` does not know a block's size, so a script that churns through garbage hits the limit while holding almost nothing, and a stop leaks what the script held (SCRIPTS.md section 4 "Memory is allocation, not residency, for now", section 6 "A stop leaks what the script held"). ## Implements - [SCRIPTS.md slice 6](https://git.torb.dev/torbscript/language/src/commit/393ef2608e4a60b8ff76d8b6b408951b822d887d/docs/design/SCRIPTS.md#L316): "The sandbox's own heap: an exact memory limit, a teardown that frees what a stopped script held" (state: Open) - [ROADMAP.md, milestone 7](https://git.torb.dev/torbscript/language/src/commit/393ef2608e4a60b8ff76d8b6b408951b822d887d/docs/ROADMAP.md#L43), open work "the sandbox's own heap" - BACKEND.md section 5.4: "`memory` from the sandbox's own heap (its own allocator, so the limit is exact and the teardown is one free)"; SCRIPTS.md wins where they differ. ## Decided design (accounting over libc's heap, owner in a side table) Decided by the loop, see the comment `decided by the loop` (option 2 of the earlier proposal). No arena allocator: SCRIPTS.md section 5 passes the value by reference inside one VM, so blocks a script allocated may be reached by the caller afterwards, and a literal "one free" teardown would leave dangling blocks. The value-passing decision of section 5 stays as it is. - While a sandbox is open every block `torb_allocate` / `torb_raw_allocate` hands out is recorded (pointer set in `runtime/sandbox.c`, size from `torb_platform_allocation_size`, as the counted memory limit of `runtime/memory.c` already does). The two free sites of `memory.c` (`torb_free_block`, `torb_raw_free`) ask the sandbox: a free of a recorded block gives its bytes back and drops the record, a free of any other block never lowers the count. - Normal close: the table is dropped, the surviving blocks are ordinary blocks of the host. - Stop (limit, panic, `Process.exit`) in a program the VM runs: the abandoned frames' references are released at teardown, so refcounting frees what nothing else holds; a block the caller's value reaches stays valid. What cycles or the caller keep alive stays (bounded by the limit) and is documented. - Stop in a native binary that hosts scripts (`--embed-vm`, only text crosses, SCRIPTS.md section 5): every block still recorded is freed at teardown. - The `SandboxError` text stays "The script allocated more than N bytes" (existing tests and users match it; it now fires when the live bytes exceed N). ## Acceptance criteria - [ ] While a sandbox is open, the live bytes of the blocks the script allocated (`torb_allocate`, `torb_raw_allocate`, sized by `torb_platform_allocation_size`) are the number checked against the `memory` limit. A block freed while the sandbox is open gives its bytes back; a block allocated outside the sandbox never counts, and freeing it never lowers the count. Exact in the VM (`torb run`, `torb test`) and in a native binary that hosts scripts. - [ ] A script that allocates and drops far more than the limit in total, but never holds more than it, runs to its end. A script that holds more than the limit stops with the `SandboxError` "The script allocated more than N bytes", never the process (`compiler/tests/sandbox.test.trb`, both cases, exact byte boundary: N bytes held passes, N+1 stops). - [ ] A script that stops (limit, panic, `Process.exit`) while holding blocks nothing outside the sandbox reaches: `torb_live_block_count` is back at its value before the script ran (VM-only conformance program or runtime test). A block of the script that the caller's value reaches stays valid and readable after the stop (no use after free; same test file). - [ ] The native-host teardown (every recorded block freed on a stop) has a runtime test: open, allocate, stop, close, live-block count unchanged. - [ ] The existing sandbox tests, `tests/conformance/sandbox-crossing.trb` and `tests/conformance/vm-only/sandbox-*.trb` stay green; tier A and tier B gates green in forge CI (`runtime/` and the VM change). - [ ] SCRIPTS.md slice 6 marked **Done** (header status, sections 4 and 6 reworded from "allocation, not residency" and "a stop leaks", the remaining bounded leak of the shared-value case named), BACKEND.md 5.4 and VM.md section 10 brought in line; `build/release/torb.exe docs skill docs skills` regenerated if a skill page changes. ## Seed **Settled: does not touch the seed.** No syntax change, no new native (the kernel operations `SandboxOpen`/`SandboxClose` exist and keep their signatures; a change to them is an operation, SCRIPTS.md section 5), no new driver flag, no std name looked up by string. The work is `runtime/memory.c`, `runtime/sandbox.c`, `runtime/machine.c` and the VM's TorbScript where it calls them. If the implementation turns out to need a new native or a changed `std/sandbox` name the compiler looks up, it stops and files a seed question instead of going on. ## Size One PR, two Conventional Commits: `feat(runtime): count the live bytes a sandboxed script holds` (record table, free-site hooks, exact-limit tests) and `feat(runtime): free what a stopped script held at teardown` (teardown in the VM and the native host, leak-count tests, docs). `runtime/` and the VM: opus/high worker. No split needed. _Moved from the planning lists of the docs; the design record stays the specification._
Author
Collaborator

[loop:torbscript] intake: in scope (author orion, label loop). Implements SCRIPTS.md slice 6 and ROADMAP milestone 7; the design record settles the goal. No collision with a design record or CONCEPT "Open Questions". Touches runtime/ and the VM (opus/high worker when implemented); seed impact to settle in refine. Release freeze for 0.1.0 (2026-10-06) holds new PRs, next: refine (buffer already >= 2 ready, so queued)

[loop:torbscript] intake: in scope (author orion, label loop). Implements SCRIPTS.md slice 6 and ROADMAP milestone 7; the design record settles the goal. No collision with a design record or CONCEPT "Open Questions". Touches `runtime/` and the VM (opus/high worker when implemented); seed impact to settle in refine. Release freeze for 0.1.0 (2026-10-06) holds new PRs, next: refine (buffer already >= 2 ready, so queued)
Author
Collaborator

[loop:torbscript] claim refine by session:loop-torbscript-20261007 until 2026-10-07T13:13:06Z

[loop:torbscript] claim refine by session:loop-torbscript-20261007 until 2026-10-07T13:13:06Z
Author
Collaborator

[loop:torbscript] owner decision:

What. SCRIPTS.md slice 6 and BACKEND 5.4 say "own heap: exact limit, teardown is one free", but no design record says how a block that the script allocated and the host still reaches is treated. SCRIPTS.md section 5 decides that inside one VM the value is passed by reference and "the script configures the caller's value in place": lists, texts and boxes the script allocates end up inside the caller's value, on a normal return and also on a stop (the frames are abandoned, the value stays as the script left it). A literal "teardown frees every block of the sandbox" would leave the caller with dangling blocks. A native binary that hosts scripts is different: only text crosses (section 5), so there the sandbox's blocks are never shared.

Options.

  1. Real arena per sandbox (bump allocator with size-class free lists, BACKEND 2.5, one free at teardown). Exact by construction. Everything a script builds that the host keeps has to be copied out at close, which reverses "nothing is copied or converted" of section 5 for programs the VM runs; blocks in the arena must not be freed with libc's free (every free site of memory.c learns the owner). Largest change, touches the layout contract between runtime and VM.
  2. Accounting over libc's heap, owner known from a side table (recommended). While a sandbox is open every block the script allocates is recorded (pointer set, size from torb_platform_allocation_size, as the counted memory limit already does); a free of a recorded block subtracts its bytes, a free of any other block never does. The limit is exact in live bytes. On a normal close the surviving blocks are handed to the host (the table is dropped, they are ordinary blocks). On a stop: native host (slice 8, value crossed as text): everything still recorded is freed, the criterion "live-block counter back to its value before the script ran" holds. Program the VM runs (value by reference): blocks the script allocated stay valid because the caller's value may reach them, and what an abandoned frame held is freed only where its count shows nothing else holds it; a leak bounded by the limit remains in that one case and is documented (as section 6 says today for all cases). No change to the value-passing decision of section 5, no ABI change, no seed impact.
  3. Option 2 for accounting only, teardown deferred: ship the exact limit now, keep "a stop leaks what the script held" and leave teardown to a later slice together with a decision on copy-out (option 1).

Recommendation. Option 2. It delivers the exact limit everywhere, the full teardown where the host shares nothing (the case a server or an embedded script host has), and does not reopen section 5. If you prefer a clean break instead, option 1 with copy-out at close is the honest "own heap", at the price of a larger change and a revised section 5. Once you answer (comment here, or relay it from the TorbScript group), I set ready and the acceptance criteria in the issue body become final.

[loop:torbscript] owner decision: **What.** SCRIPTS.md slice 6 and BACKEND 5.4 say "own heap: exact limit, teardown is one free", but no design record says how a block that the script allocated and the **host still reaches** is treated. SCRIPTS.md section 5 decides that inside one VM the value is passed by reference and "the script configures the caller's value in place": lists, texts and boxes the script allocates end up inside the caller's value, on a normal return and also on a stop (the frames are abandoned, the value stays as the script left it). A literal "teardown frees every block of the sandbox" would leave the caller with dangling blocks. A native binary that hosts scripts is different: only text crosses (section 5), so there the sandbox's blocks are never shared. **Options.** 1. **Real arena per sandbox** (bump allocator with size-class free lists, BACKEND 2.5, one free at teardown). Exact by construction. Everything a script builds that the host keeps has to be copied out at close, which reverses "nothing is copied or converted" of section 5 for programs the VM runs; blocks in the arena must not be freed with libc's `free` (every `free` site of `memory.c` learns the owner). Largest change, touches the layout contract between runtime and VM. 2. **Accounting over libc's heap, owner known from a side table** (recommended). While a sandbox is open every block the script allocates is recorded (pointer set, size from `torb_platform_allocation_size`, as the counted memory limit already does); a free of a recorded block subtracts its bytes, a free of any other block never does. The limit is exact in live bytes. On a normal close the surviving blocks are handed to the host (the table is dropped, they are ordinary blocks). On a stop: **native host (slice 8, value crossed as text): everything still recorded is freed**, the criterion "live-block counter back to its value before the script ran" holds. **Program the VM runs (value by reference): blocks the script allocated stay valid** because the caller's value may reach them, and what an abandoned frame held is freed only where its count shows nothing else holds it; a leak bounded by the limit remains in that one case and is documented (as section 6 says today for all cases). No change to the value-passing decision of section 5, no ABI change, no seed impact. 3. **Option 2 for accounting only, teardown deferred**: ship the exact limit now, keep "a stop leaks what the script held" and leave teardown to a later slice together with a decision on copy-out (option 1). **Recommendation.** Option 2. It delivers the exact limit everywhere, the full teardown where the host shares nothing (the case a server or an embedded script host has), and does not reopen section 5. If you prefer a clean break instead, option 1 with copy-out at close is the honest "own heap", at the price of a larger change and a revised section 5. Once you answer (comment here, or relay it from the TorbScript group), I set `ready` and the acceptance criteria in the issue body become final.
Author
Collaborator

[loop:torbscript] refine: rewrote the body to the definition of ready (goal, criteria, SCRIPTS.md slice 6 / ROADMAP milestone 7 / BACKEND 5.4, seed: not expected, size: one PR in two commits). The design records leave open how teardown treats blocks the host still reaches (value passed by reference inside one VM) and arena vs accounting over libc; filed the owner decision above with a recommendation. No ready label until it is answered. Tokens used ~90000, next: waiting (owner decision)

[loop:torbscript] refine: rewrote the body to the definition of ready (goal, criteria, SCRIPTS.md slice 6 / ROADMAP milestone 7 / BACKEND 5.4, seed: not expected, size: one PR in two commits). The design records leave open how teardown treats blocks the host still reaches (value passed by reference inside one VM) and arena vs accounting over libc; filed the owner decision above with a recommendation. No `ready` label until it is answered. Tokens used ~90000, next: waiting (owner decision)
Author
Collaborator

[loop:torbscript] wait: owner decision of comment 504 (blocks teardown of host-reachable blocks, arena vs accounting) is still open since 2026-10-07; nothing to do until Torben answers. Re-check 2026-11-06.

[loop:torbscript] wait: owner decision of comment 504 (blocks teardown of host-reachable blocks, arena vs accounting) is still open since 2026-10-07; nothing to do until Torben answers. Re-check 2026-11-06.
Author
Collaborator

[loop:torbscript] claim refine by session:loop-torbscript-20261112 until 2026-11-12T20:45:00Z

[loop:torbscript] claim refine by session:loop-torbscript-20261112 until 2026-11-12T20:45:00Z
Author
Collaborator

[loop:torbscript] decided by the loop: option 2 of the earlier proposal (accounting over libc's heap, owner known from a side table), not an arena allocator. Why: this is an internal technical design choice with a clear recommendation, no design record reserves it for the owner, and it touches none of the owner-list topics (public commitments, costs, product direction, published contract). It delivers the exact live-byte limit everywhere, frees on a stop everything that is not shared, and leaves the value-passing decision of SCRIPTS.md section 5 (by reference inside one VM) untouched: no ABI change, no seed impact. Checked against current main: there are only two free sites in runtime/memory.c (torb_free_block, torb_raw_free) and two allocation sites, so the hook is small. Teardown: native host (text crosses) frees every recorded block; a program the VM runs releases the abandoned frames' references so refcounts free what nothing else holds, and a block the caller's value reaches stays valid; the bounded remainder is documented. SandboxError text stays unchanged. The earlier owner decision comment is superseded; no owner answer is needed. Option 1 (arena with copy-out) stays an option for a later slice if the owner wants it.

[loop:torbscript] decided by the loop: option 2 of the earlier proposal (accounting over libc's heap, owner known from a side table), not an arena allocator. Why: this is an internal technical design choice with a clear recommendation, no design record reserves it for the owner, and it touches none of the owner-list topics (public commitments, costs, product direction, published contract). It delivers the exact live-byte limit everywhere, frees on a stop everything that is not shared, and leaves the value-passing decision of SCRIPTS.md section 5 (by reference inside one VM) untouched: no ABI change, no seed impact. Checked against current main: there are only two free sites in `runtime/memory.c` (`torb_free_block`, `torb_raw_free`) and two allocation sites, so the hook is small. Teardown: native host (text crosses) frees every recorded block; a program the VM runs releases the abandoned frames' references so refcounts free what nothing else holds, and a block the caller's value reaches stays valid; the bounded remainder is documented. `SandboxError` text stays unchanged. The earlier `owner decision` comment is superseded; no owner answer is needed. Option 1 (arena with copy-out) stays an option for a later slice if the owner wants it.
Author
Collaborator

[loop:torbscript] refine: decided the open design question myself (accounting over libc heap with a side table, teardown per case, no seed impact), rewrote the body to the definition of ready with final acceptance criteria, set ready. Size: one PR, two commits, opus/high worker. Tokens used ~60000, next: implementation

[loop:torbscript] refine: decided the open design question myself (accounting over libc heap with a side table, teardown per case, no seed impact), rewrote the body to the definition of ready with final acceptance criteria, set `ready`. Size: one PR, two commits, opus/high worker. Tokens used ~60000, next: implementation
Author
Collaborator

[loop:torbscript] claim implementation by session:loop-torbscript-20261112 until 2026-11-12T23:50:00Z

[loop:torbscript] claim implementation by session:loop-torbscript-20261112 until 2026-11-12T23:50:00Z
Author
Collaborator

[loop:torbscript] checkpoint implementation: read the issue, runtime/sandbox.c, memory.c, machine.c and the VM's stop path. In flight: live-byte accounting (side table of the blocks the sandbox's thread allocates, give-back at the two free sites, exact limit tests). Finding: the VM teardown (releasing the abandoned frames' references) needs per-frame knowledge of which registers hold references, which the bytecode does not carry today; it is likely a split. Branch loop/1-sandbox-heap-limit. Next: commit the accounting, push, PR.

[loop:torbscript] checkpoint implementation: read the issue, runtime/sandbox.c, memory.c, machine.c and the VM's stop path. In flight: live-byte accounting (side table of the blocks the sandbox's thread allocates, give-back at the two free sites, exact limit tests). Finding: the VM teardown (releasing the abandoned frames' references) needs per-frame knowledge of which registers hold references, which the bytecode does not carry today; it is likely a split. Branch loop/1-sandbox-heap-limit. Next: commit the accounting, push, PR.
Author
Collaborator

[loop:torbscript] checkpoint: done: runtime side table + tests + docs, PR #721 opened (review requested from argus). In flight: forge CI. Branch loop/1-sandbox-heap-limit. Next: wait for CI, fix if red.

[loop:torbscript] checkpoint: done: runtime side table + tests + docs, PR #721 opened (review requested from argus). In flight: forge CI. Branch `loop/1-sandbox-heap-limit`. Next: wait for CI, fix if red.
Author
Collaborator

[loop:torbscript] checkpoint (timeout reached): PR #721 open, review requested from argus. First CI run failed in bootstrap: my new torb_sandbox_record clashed with the existing manifest-inputs buffer of the same name in sandbox.c; renamed to torb_sandbox_keep and force-pushed (fed50b3f, still two commits). CI on fed50b3f is queued, not yet run. Branch loop/1-sandbox-heap-limit. Delivered slice: exact live-byte memory limit (side table over libc, requested sizes); teardown after a stop (VM frames, native host) not in this PR, see PR body. Next: wait for CI on fed50b3f, fix if red, then review.

[loop:torbscript] checkpoint (timeout reached): PR https://git.torb.dev/torbscript/language/pulls/721 open, review requested from argus. First CI run failed in bootstrap: my new torb_sandbox_record clashed with the existing manifest-inputs buffer of the same name in sandbox.c; renamed to torb_sandbox_keep and force-pushed (fed50b3f, still two commits). CI on fed50b3f is queued, not yet run. Branch loop/1-sandbox-heap-limit. Delivered slice: exact live-byte memory limit (side table over libc, requested sizes); teardown after a stop (VM frames, native host) not in this PR, see PR body. Next: wait for CI on fed50b3f, fix if red, then review.
Author
Collaborator

[loop:torbscript] decided by the loop: PR #721 lands the accounting slice only (live bytes held against the limit); the teardown after a stop (criteria 3 and 4) needs reference maps in the bytecode and stays open, so the PR now says Refs #1 instead of Closes #1 and #1 stays open for the teardown slice.

[loop:torbscript] implementation: PR #721 is open (head fed50b3, two commits); the first CI run failed on a symbol clash, fixed by renaming to torb_sandbox_keep; the rerun was queued at the worker's time limit, review requested from argus.
next: waiting (forge CI on PR #721, then review or a fix on loop/1-sandbox-heap-limit)

[loop:torbscript] decided by the loop: PR #721 lands the accounting slice only (live bytes held against the limit); the teardown after a stop (criteria 3 and 4) needs reference maps in the bytecode and stays open, so the PR now says `Refs #1` instead of `Closes #1` and #1 stays open for the teardown slice. [loop:torbscript] implementation: PR #721 is open (head fed50b3, two commits); the first CI run failed on a symbol clash, fixed by renaming to torb_sandbox_keep; the rerun was queued at the worker's time limit, review requested from argus. next: waiting (forge CI on PR #721, then review or a fix on loop/1-sandbox-heap-limit)
Author
Collaborator

[loop:torbscript] implementation: forge CI on PR #721 (head fed50b3) is green, 21 of 21 checks; review from argus is requested.
next: review

[loop:torbscript] implementation: forge CI on PR #721 (head fed50b3) is green, 21 of 21 checks; review from argus is requested. next: review
argus closed this issue 2026-11-12 20:45:50 +00:00
Sign in to join this conversation.
No project
No assignees
1 participant
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#1
No description provided.