sandbox: give a script its own heap with an exact memory limit #1
Labels
No labels
loop
queue/failed
queue/queued
ready
status/parked
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
torbscript/language#1
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_accountonly adds allocated bytes (runtime/sandbox.c), becausefreedoes 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
memoryfrom 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.torb_allocate/torb_raw_allocatehands out is recorded (pointer set inruntime/sandbox.c, size fromtorb_platform_allocation_size, as the counted memory limit ofruntime/memory.calready does). The two free sites ofmemory.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.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.--embed-vm, only text crosses, SCRIPTS.md section 5): every block still recorded is freed at teardown.SandboxErrortext 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
torb_allocate,torb_raw_allocate, sized bytorb_platform_allocation_size) are the number checked against thememorylimit. 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.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).Process.exit) while holding blocks nothing outside the sandbox reaches:torb_live_block_countis 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).tests/conformance/sandbox-crossing.trbandtests/conformance/vm-only/sandbox-*.trbstay green; tier A and tier B gates green in forge CI (runtime/and the VM change).build/release/torb.exe docs skill docs skillsregenerated if a skill page changes.Seed
Settled: does not touch the seed. No syntax change, no new native (the kernel operations
SandboxOpen/SandboxCloseexist 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 isruntime/memory.c,runtime/sandbox.c,runtime/machine.cand the VM's TorbScript where it calls them. If the implementation turns out to need a new native or a changedstd/sandboxname 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) andfeat(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.
[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] claim refine by session:loop-torbscript-20261007 until 2026-10-07T13:13:06Z
[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.
free(everyfreesite ofmemory.clearns the owner). Largest change, touches the layout contract between runtime and VM.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.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
readyand the acceptance criteria in the issue body become final.[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
readylabel until it is answered. Tokens used ~90000, next: waiting (owner decision)[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] claim refine by session:loop-torbscript-20261112 until 2026-11-12T20:45:00Z
[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.SandboxErrortext stays unchanged. The earlierowner decisioncomment 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] 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] claim implementation by session:loop-torbscript-20261112 until 2026-11-12T23:50:00Z
[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: 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 (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 onfed50b3fis 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 onfed50b3f, fix if red, then review.[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 #1instead ofCloses #1and #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] implementation: forge CI on PR #721 (head
fed50b3) is green, 21 of 21 checks; review from argus is requested.next: review