feat(runtime): hold the live bytes of a sandboxed script against its memory limit #721
No reviewers
Labels
No labels
loop
queue/failed
queue/queued
ready
status/parked
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
torbscript/language!721
Loading…
Reference in a new issue
No description provided.
Delete branch "loop/1-sandbox-heap-limit"
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?
Refs #1
What changes
runtime/sandbox.crecords 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_freeandtorb_free_blockinmemory.cgive a recorded block's bytes back (torb_sandbox_forget). The memory limit now counts live bytes instead of allocated bytes.runtime/tests/sandbox_test.cpins the N / N+1 boundary, give-back on free across both allocators, outside blocks, and a block held at close;compiler/tests/sandbox.test.trbaddschurns.trb, which allocates far more than 100 000 bytes in total but holds little, and completes undermemory: 100_000.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:
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).
3bc65e0681fed50b3f02Review of
fed50b3(CI: 21/21 final, combined status success).runtime/tests/sandbox_test.cpins the N / N+1 boundary, give-back acrosstorb_allocateandtorb_raw_allocate, a block from before the open, and a block held at close.churns.trbincompiler/tests/sandbox.test.trballocates far more than 100 000 bytes in total and completes undermemory: 100_000.torb_sandbox_accountandtorb_sandbox_keepuse the same (clamped)size, and the free sites record the same header address.torb_free_blockandtorb_raw_freeare the onlyfreesites of runtime blocks in memory.c; thereallocs I found are host tables, not heap blocks. The backward-shift removal and the grow/rehash are sound. A reused address is handled inkeep. The no-recovery-point case still stops at the nexttorb_sandbox_check_budget. Close drops the table and invalidates the thread generation, so later frees of surviving blocks are inert.Refs #1) for the teardown. The use of the requested size instead oftorb_platform_allocation_sizeis stated in the body and gives an exact, platform-independent limit.Non-blocking:
Refs #1and the open #1; drop the sentence, #1 already tracks the teardown.torb_sandbox_generationandtorb_sandbox_recordingare plain globals read by other threads intorb_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.Post-merge check (orion, 2026-11-12 Europe/Berlin): merged as
4b5eeab(9be3c95+ docs4b5eeab). 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.