Security record: the async executor
The record of the executor that runs async tasks: runtime/runtime-async.c,
which parks a task on a descriptor or a timer and wakes it when the kernel
says so, and the driver the compiler emits around it. What it reads from the
outside is the kernel's readiness report for descriptors the program chose,
and those descriptors are often a network peer's sockets, so a peer decides
when, and whether, a task is resumed. It says what each such input can do,
with the test that pins each answer. The design is in
wp49-async-server.md.
It was written on top of main at ba85d3e, in the change
that adds the executor, and names functions rather than lines, so it has no
file:line anchors to move.
Result. No completion can resume a task it was not made for: every wait
records the (index, generation) of the task that made it, and one that
arrives after that task finished is dropped and counted (ASYNC-1). A
descriptor wait registers a duplicate, so a program that closes and reuses a
descriptor number while a task waits cannot have the old registration answer
for the new file (ASYNC-4). A timer's deadline cannot overflow (ASYNC-3).
ASYNC-1 and ASYNC-2 are reached only by driving the runtime from C (the
probe.c of tests/link/async_late_completion): the emitted driver waits on
its own task's token and never holds one past that task, so Nish source has no
way to present a stale completion; the checks are defence in depth.
A peer that never makes its socket ready parked its task for ever, and the
loop with it, until the cancellation stage gave a wait a deadline and a task
a cancel (ASYNC-6, fixed); a cancel names a task by its generation as a
completion does, so a stale handle cancels nothing, and a cancelled or
finished task leaves no wait or timer behind, nor a node on the ready queue
(ASYNC-18, fixed before it merged). What remains is
that readiness is a hint, not a reservation (ASYNC-9, open), and a task that
never awaits holds the thread (ASYNC-5, decided). A task shares its spawner's
objects but may store only numbers into them, so nothing it shares can point
into its region once it ends (ASYNC-10), and nothing it shares can hold a part
its spawner frees, wipes or closes on its own schedule (ASYNC-11), and the
task keeps none of what it shares in memory of its own, so it cannot store
its memory into a shared object through a holder of its own (NL2610,
ASYNC-12). A builtin whose instance has no body to walk (reserve,
jsonDecode) is held to the same rules by the facts it is given
(ASYNC-13). Two calls joined by one awaitAll each run in a region of their
own, so neither can free the other's memory (ASYNC-14); the caller keeps
what the regions answer without growing a chunk per call or keeping a call's
temporaries (ASYNC-15), at a cost per call that does not wait (ASYNC-16);
what it does keep is about 1.3 KB per joined call beyond the bytes the call
answered, until the caller's memory is given back (ASYNC-17). A finished
task's node keeps its region, emptied, for the next spawn of the same async
function, and what that task can read of it is nothing (ASYNC-20, a property
checked and found to hold, not a finding). Nineteen findings (ASYNC-1 to
ASYNC-19): eleven fixed, four open and four decided.
Scope
| File | What |
|---|---|
runtime/runtime-async.c |
the task slab and table (nish_async_spawn, nish_async_done, nish_async_table_add); the running task and its region (nish_async_enter, nish_async_leave); the waits and their tokens (nish_async_wait_fd, nish_async_sleep, nish_async_poll, nish_async_slot_of, nish_async_fire); the timer heap; the loop (nish_async_block, nish_async_next); cancellation (nish_async_task_id, nish_async_cancel, nish_async_cancelled, nish_async_cancel_after, nish_async_cancel_task, nish_async_disarm) and the bounded wait (nish_async_wait_fd_within); nish_async_copy_str |
src/emit-async.ts |
the emitted driver, @nish.async.run, @nish.async.drain and @nish.async.resume, and each task's spawn, argument copies and await polls; each awaitAll call's lane: its start, its region and its poll (emitAsyncCall, emitLanePoll) |
src/async-check.ts, src/region-rules.ts |
what a spawned task may be handed (recordSpawn, NL2673) and what it may do with what it shares (taskFindings, taskShareObstacle, NL2609, NL2615, NL2689, NL2690, NL2691) |
runtime/runtime-region.c |
nish_region_adopt only, which joins a finished awaitAll lane's region to the caller's arena (the rest of the file is runtime.md's) |
runtime/runtime.c |
the carved chunk nish_region_adopt makes (NISH_CHUNK_CARVED) and the walkers that must not free it: nish_free_until, nish_reset_arena, nish_arena_keep |
Out of scope: the region allocator a task's memory lives in
(runtime/runtime-region.c but nish_region_adopt, runtime.md's RT-41 to
RT-47), and the descriptors themselves, which the program opened through
nish:net and which runtime.md covers.
Threat model
Who is trusted. The program's own code, the compiler that lowered it, and the kernel.
Who is the adversary. A network peer at the far end of a socket a task waits on. It controls when the socket becomes readable or writable, whether it ever does, and when it is closed. Its aim is to make the executor resume the wrong task, resume a task twice, read a frame that was given back, or stop serving the program's other peers.
What is not claimed. The executor is cooperative and runs on the thread
that runs main: it does not preempt a task, and it does not bound how long
a task waits unless the program asks. A server that must not let one peer
stall it gives a wait a deadline (readableWithin, writableWithin), or
its task one (cancelAfter), or cancels the task (cancel); std/net/serve
and the async fetch do so for the program. A cancel is cooperative too:
it fails the task's waits, and the task runs on to its end.
Findings
| Id | Severity | Where | Description | Disposition |
|---|---|---|---|---|
| ASYNC-1 | Medium | runtime/runtime-async.c (nish_async_fire, nish_async_slot_of) |
A completion can arrive after the task that made its wait has finished: a timer of a task that returned, or a node that was given back and reused by a later spawn. Taken on trust, it would resume whatever task now holds the node, with an answer meant for another, and the resumed frame would read a state the completion does not belong to. | Fixed before landing. A wait records the index and generation of its task; the generation moves on each time a node is reused, and a completion whose generation does not match is dropped and counted (nish_async_stat(1)). A token is checked the same way against the wait slot's own generation, so a spent token answers 0, never another wait's answer. These checks were written with the executor, so there is no before run; pinned by tests/link/async_late_completion: "node reused: yes", "resumed the new task: yes, late so far: 1", "resumes: 1, late: 1", "the orphan's token: 0, a spent token: 0". |
| ASYNC-2 | Low | runtime/runtime-async.c (nish_async_done, nish_async_wait_free_slot) |
A generation is 32 bits and wraps from UINT32_MAX to 1. A wait made by a task whose node is then reused 2^32 - 1 times before the wait completes would match the new task's generation and resume it. |
Open. It takes four billion spawns of one node while a single stale wait stays pending; a timer is the only wait that outlives its task, and its deadline (at most 24.8 days) is far shorter than that many spawns. A 64-bit generation, which would close it, costs a node header word. |
| ASYNC-3 | Low | runtime/runtime-async.c (nish_async_sleep, nish_async_block) |
sleep(ms) adds ms to the monotonic clock in nanoseconds, and the epoll timeout is an int of milliseconds; an unchecked product or a narrowing cast would wrap a long sleep into a short or negative one. |
Fixed before landing. ms is an i32, so its nanoseconds (at most 2.15 × 10^15) fit an i64 beside the clock; a negative ms answers false at once and never enters the heap; the epoll timeout is rounded up and clamped to INT32_MAX milliseconds, and the loop waits again until the first deadline is due. That clamp cannot be reached from Nish source today: an i32 of milliseconds is at most INT32_MAX already; it stays for a caller of the C entry point and for a later wider sleep. Pinned by "none false" in tests/emit-ts/async/sleep (its native twin is the case test async_sleep) and the deadline order of tests/link/async_timer_order. |
| ASYNC-4 | Medium | runtime/runtime-async.c (nish_async_wait_fd, nish_async_fire) |
A registration keyed on the program's descriptor number would survive the program closing it: once the number is reused (a peer's new connection, a file), the old registration would answer for the new file, and the waiting task would read a socket it never waited on. Two tasks waiting on one number would also overwrite each other's registration. | Fixed before landing. A wait duplicates the descriptor (F_DUPFD_CLOEXEC) and registers the duplicate one-shot, keyed by the wait's token; the duplicate is removed and closed when the wait fires. Closing the program's descriptor leaves the duplicate open, so the registration still names the original file. A descriptor that cannot be duplicated answers false, the same answer as a closed one; that includes a process at its descriptor limit (EMFILE), since each pending wait holds one more descriptor than the program opened. A regular file, which never blocks, answers true: epoll refuses one (EPERM), and on kqueue, which would report a regular file ready only while it is not at end of file, the wait checks fstat for S_ISREG and answers at once. Pinned by tests/link/async_fd_reuse, where a pipe that takes the closed number and is written to does not wake the task ("woken by: the timer", "the wait: -1") and a write to the socket's other end does ("after a write to the pair: 1"), and by tests/link/async_readable ("main: readable(-1) false"). |
| ASYNC-5 | Low | the emitted driver (@nish.async.run) |
The executor is cooperative: a task that loops without an await keeps every other task, and main, from running, so one peer whose request takes a long computation delays all the others. |
Decided. It is the language's model (LANGUAGE.md, "Where it is not Node"), as it is JavaScript's: a task yields only at an await. The ready queue is FIFO, so a task that does await cannot be passed over by later wakes; the kernel's report is taken in its own order and then the expired timers, earliest first, so neither descriptors nor timers can starve the other kind. |
| ASYNC-6 | Medium | runtime/runtime-async.c (nish_async_next, nish_async_block) |
A descriptor that never becomes ready parks its task for ever, and because the loop runs until nothing waits, the program never exits: a peer that connects and sends nothing holds a readable wait, and the process, open indefinitely. |
Fixed by the cancellation stage of WP49. readableWithin(fd, ms) and writableWithin(fd, ms) arm a timer beside the descriptor wait, and whichever fires first answers it: the descriptor true, the timer false, and the other is taken out (nish_async_wait_fd_within, nish_async_fire). cancel(handle) fails each wait the task has pending, at once, with false, and every later wait of the task answers false without parking; cancelAfter(handle, ms) arms a timer that cancels the task (a wait slot with cancels set, one per task, replaced by the next cancelAfter and taken out when the task ends). Closing the program's descriptor still does not end a wait (ASYNC-4). Before, nothing a program could write ended an await readable on a silent peer; after, link/async_cancel's probe: "cancel a waiting reader: 1, it answers 0", "the bounded wait times out: yes, answers 0", "the deadline resumes the task: yes, its sleep answers 0"; and through the library, link/net_serve_hangup, link/net_serve_deadline, link/net_serve_shutdown (SERVE-8 in serve.md) and link/net_fetch_async_deadline (HF-10 in fetch.md). A task that does not await is not reached by a cancel (ASYNC-5). |
| ASYNC-7 | Low | src/emit-async.ts (the spawn), runtime/runtime-async.c (nish_async_copy_str) |
A spawned task outlives its spawner, so an argument that pointed into the spawner's arena would be read after that arena was released. | Decided. A string argument is copied into the task's own region before the task first runs; a number or boolean is copied by value; an enum is copied by value; an object, an array, a Map or a Set is shared, and every function that feeds a task one, itself or through a callee (feedsTasks), releases no arena scope, so the spawner's memory outlives every task that holds it; a function argument must capture nothing (NL2690); a shared argument holding a Region, a Secret, a using handle or a nish/threads value is refused (NL2691, ASYNC-11); any other type is refused at compile time (NL2673, LANGUAGE.md). Pinned by tests/link/async_spawn_args, whose spawner builds 2890 characters of noise in the memory its arguments came from before the tasks read them, and tests/link/async_spawn_shared_outlives, whose spawner returns before its tasks update the objects it built, under noise, with Arena.used() showing nothing given back across the spawn. |
| ASYNC-8 | Low | runtime/runtime-async.c (nish_async_spawn, nish_async_enter, nish_async_leave, nish_async_done) |
A task's allocations land in whatever arena is current when it runs; with one arena for all tasks, a task that finished would leave its memory to be freed only when main returns, and a long-running server would grow by every request it ever served. |
Decided. Each spawned task owns a region, entered around every resume and emptied when the task finishes: the task's memory goes then, all of it but one chunk of at most 64 KiB that the node keeps, empty, for the next task it holds (ASYNC-20). Pinned by tests/link/async_region_per_task and the slab's reuse by tests/link/async_slab_flat ("after a thousand rounds: 12 nodes, peak 10 live, 0 live now"). |
| ASYNC-9 | Low | runtime/runtime-async.c (nish_async_wait_fd, nish_async_fire) |
Readiness is a report, not a reservation. Two tasks waiting on one descriptor are both woken by one byte; the first to read takes it, and the second's read, on a blocking descriptor, blocks the thread and with it every task. | Open. One reader per descriptor is the program's to keep; a server gives each connection to one task. A non-blocking descriptor, which nish:net does not yet offer, would turn the loser's read into a short one. |
| ASYNC-10 | Medium | src/region-rules.ts (taskFindings, the task-mode RegionWalk), src/async-check.ts (recordSpawn) |
A task's own region is freed when it ends, and it shares the spawner's objects: a store of the task's memory into one (a string it built, an object it made), directly, through an alias, a method, a callee, a field or an element, would leave the spawner pointing at freed memory; so would growing a shared array, Map or Set, whose new storage would land in the region; and a task that spawned a task with an object could hand on memory of its own region. |
Fixed. Each spawned function is walked by the region rules with its shared parameters as memory outside the region: a store of region memory is NL2609, growth NL2615; a task that feeds a task a shared argument is NL2689, a capturing function argument NL2690; a callee handed both a shared object it writes into and a string or object of the task's is refused, since a string has no pointer facts to say it is not kept (NL2609); a local the walk does not bind (an awaitAll name before this fix) is unknown, so storing it is NL2609; an object a using arena() block allocates may not reach a task that outlives the block, directly or through a function that spawns (NL2421). Before, spawn refused every object (NL2673); after, reject_async_spawn_store_*, reject_async_spawn_grow*, reject_async_spawn_nested, reject_async_spawn_block_arg, reject_async_spawn_await_all and reject_async_spawn_using_arena* are refused with those codes, and async_spawn_shared runs. |
| ASYNC-11 | Medium | src/region-rules.ts (taskShareObstacle, the task-mode RegionWalk's visitBuiltin) |
ASYNC-10's rules looked at a shared argument's own type and at push, set and add, not at what the argument holds or at every builtin that appends. A shared object with a Region field, a Secret field, a nish/threads scope, lock, guard or channel, or a handle class a using block closes, was accepted, so the spawner could free, wipe or close that part while the task still waited and then read it; and spawnPiped(argv, stdin, out, err) appends to out and err as push does, so a task handed a shared array could grow it into its own region. |
Fixed. spawn refuses a shared argument whose type reaches any of those parts, through nullables, arrays, Result arms, union variants and fields, naming the part (NL2691), and the task walk reads spawnPiped's two output arguments as growth (NL2615), in a task and in a region(...) body alike. Before, all five compiled; after, reject_async_spawn_region_field, reject_async_spawn_secret_field, reject_async_spawn_handle, reject_async_spawn_threads_field and reject_async_spawn_grow_piped are refused with those codes, and reject_async_spawn_grow_map pins Map.set and Set.add on a shared field. |
| ASYNC-12 | Medium | src/region-rules.ts (the task-mode RegionWalk: reportOuter, callOrigin, judgeCallee) |
ASYNC-10's walk took every new, array and object literal in a task as the task's memory and let the task store the shared object into it (NL2610 was waived in tasks). The holder then reached the shared object, and a store of the task's memory through it (new Holder(tally).t.label = s, [tally][0].label = s, a for...of over such an array, an interface literal, a method of the holder) was the task's memory into the task's, so nothing was reported: the shared object pointed at freed memory once the task ended. A helper's fresh answer (wrap(tally), an async helper, awaitAll) was taken as the task's alone in the same way, and Map.set/Set.add on a task-owned container kept the shared object unseen. |
Fixed. In a task, putting memory outside the region into the task's own memory is NL2610, as in a region(...) body: a constructor argument, an array or object literal, a field or element store, push, a method that may keep its argument in its receiver (Map.set, Set.add, a user method), and a callee that stores one argument into another. A fresh helper's answer other than a string joins its arguments' origins, so a holder a helper builds from the shared object is not the task's alone and a store through it is NL2609. Before, all compiled and the spawner read the noise tasks' text; after, reject_async_spawn_keep_holder and reject_async_spawn_keep_returned are refused with those codes, and async_spawn_shared still runs. |
| ASYNC-13 | Medium | src/attributes.ts (the fixpoint's passedToCallees step, markJsonRoute) |
The task walk judges a callee by its parameter facts. An instance with no body of its own is given those facts by a mark* helper, and two gave too few: a nish/map route (reserve, getOrInsert) handed its receiver to the code it is lowered to, but the fixpoint carried only writesThrough, writesShared, captured and outlives along that edge, not writesReach; and jsonDecode said it writes and keeps out, not that it stores what it allocates below it. So reserve(sharedMap, n) in a task grew the map's buckets into the task's region, and jsonDecode(d, shared) stored the decoded strings there: the spawner read freed memory once the task ended (reserve crashed with SIGSEGV). |
Fixed. The fixpoint carries writesReach and writesDeep along a routed instance's passedToCallees edges (an unknown callee is taken to do both; a body's own edges carry them already, through callWrites; polluted is not carried, because the routed table code is type-erased and would call a map of numbers dirty), and a decode instance marks each parameter written below and polluted, so both calls are NL2615 in a task and in a region(...) body (src/region-rules.ts names a callee that grows an argument by its growth before it names it as a store, which a growing callee also is). The other helpers were checked: a channel carries only numbers and copied strings (NL2475), an atomic or a lock writes a number through this, a region intrinsic keeps its root in the region table and is private to std/region.ts, a parallel entry's body is walked and the helper only adds to its facts, and markWipe and markShmWindow already set writesReach. Before, reject_async_spawn_grow_reserve crashed and reject_async_spawn_store_json compiled; after, both are refused. |
| ASYNC-14 | Medium | src/emit-async.ts (emitAsyncCall, emitAwaitAll, emitLanePoll), runtime/runtime-region.c (nish_region_adopt) |
Every call an awaitAll started ran in its caller's arena, so the calls' allocations interleaved in one stack. The first call to return a number released the arena to the mark it took at its start and freed the chunks above it, which a sibling that started later still held across its await: the sibling then read and wrote freed memory. With small allocations the release stayed inside one chunk and nothing was freed; from about 256 KB a chunk was. Reached through fetch as fetch.md's HF-13 (a fetch beside a DNS lookup panicked, or died with SIGSEGV). |
Fixed (the awaitall-regions stage of WP49). Each call gets a region of its own (nish_region_new), entered around every resume from its first, so no call's mark or release reaches another's memory. (At first a call was resumed once in the caller's arena and took a region only when it pended; ASYNC-15 says why that changed.) When the call finishes, nish_region_adopt puts the region's chunks on top of the caller's arena and retires the handle, so an answer that points into the region stays readable and is freed with the caller's memory; adopting an entered region panics, as freeing one does. Before: tests/link/async_await_all_lanes and tests/cases/async_await_all_lanes_large died with SIGSEGV (exit 139); after: both print their expected output, and the link program's Arena.mark() and Arena.used() stay flat over twenty rounds. |
| ASYNC-15 | Medium | runtime/runtime-region.c (nish_region_adopt), runtime/runtime.c (nish_free_until, nish_reset_arena, nish_arena_keep), src/emit-async.ts (emitAsyncCall) |
ASYNC-14's adopt made the region's newest chunk the caller's head, so whatever the caller's own chunk had left was buried beneath it, and the caller's next allocation that did not fit the region's nearly full chunk grew a fresh 64 KiB one. A loop that releases each pass gives that back; one that does not (a function that calls Arena.mark() takes no per-pass release, src/escape.ts, "Arena control", as does a loop that keeps what it makes) grew by a chunk per round of awaitAll, while Arena.used(), which counts bytes allocated, stayed flat. A server's accept loop of that shape is a peer-paced memory leak. Second, a call took the mark it releases to at return during its first resume, in the caller's arena; once it had pended and run in its region, that release found no chunk of its own and freed nothing, so whatever the call allocated after its first wait, its temporaries as well as its answer, moved into the caller at the adopt. |
Fixed. When the caller's chunk has at least 256 bytes left, the adopt carves that tail into a chunk header of its own, marked by bit 0 of its capacity (NISH_CHUNK_CARVED; a malloc'd chunk's capacity is a multiple of 8), and puts it on top of the region's chunks, so the caller goes on bumping where it stopped. A carved chunk always sits above the chunk it was carved from, so a release that frees a newest prefix frees the carve with it; nish_free_until does not free a carved chunk, nish_reset_arena keeps the first chunk that is not carved, and nish_arena_keep leaves a carved head as it is. Under ASan each block is a chunk of its own size, so nothing is carved. Before: tests/link/async_await_all_retain (30000 rounds of two lanes that each answer 100 elements made after a wait, in a function that calls Arena.mark()) peaked at 163 MB, the mark moving 66528 bytes a round; after: 43 MB, 512 bytes a round, for 1.2 KB of arrays a round, and tests/run.js bounds the peak at 80 MB. For the second, each call now runs in its region from its first resume (ASYNC-14), so its mark is taken there: 0 in the empty region, and its release at return rewinds its newest chunk to empty (nish_reset_arena keeps that chunk). The adopt frees such an empty newest chunk instead of moving it. Before (with the carve): 3000 rounds of two calls that each wait 1 ms, make 1000 elements and answer a number peaked at 25.2 to 25.4 MB; after: 1.4 to 1.6 MB, against 1.3 to 1.4 MB for main before WP49. The test now joins, 2000 times, a call that answers 100 elements and one that makes 4 KB it does not answer, both waiting 1 ms so each certainly pends: 11.1 to 11.2 MB before, 3.2 to 3.3 MB after over five runs each, and tests/run.js bounds the peak at 6 MB. A nested awaitAll (2000 rounds of a call that runs three rounds of its own awaitAll of waiting calls) went from 6.9 to 7.0 MB to 4.6 to 4.8 MB, and at 4000 rounds from 12.7 MB to 7.9 to 8.0 MB. tests/link/async_await_all_small pins five small lanes a round, one that finishes in its first resume beside pending siblings. |
| ASYNC-16 | Low | src/emit-async.ts (emitAsyncCall), runtime/runtime-region.c (nish_region_new, nish_region_enter, nish_region_leave, nish_region_adopt) |
Since ASYNC-15, every call an awaitAll joins takes a region before its first resume, whether or not it ever waits: a region made, entered, left, adopted and retired, each an out-of-line runtime call, with a lock in a threaded build. A call that finishes in its first resume used to cost nothing beyond its resume. |
Open. Measured on 1,000,000 rounds of awaitAll of two calls that never wait: 370,331,343 instructions against 12,246,618 before (cachegrind), about 179 more per call, and 18 ms against 5 ms of wall time: about 25 to 29 ns per call, against about 4.6 ns on main before this stage. A program that does not use awaitAll pays nothing: bench/instructions.json's ratchet is unchanged. A cheaper path would take the region only when the call first pends and move what the call allocated in the caller's arena since its mark into the region then; it is not done. |
| ASYNC-17 | Low | runtime/runtime-region.c (nish_region_adopt), runtime/runtime.c (the carved chunk) |
Each call an awaitAll joins runs in a region whose first chunk is made when the region is, and the adopt keeps that chunk with the caller's memory. Only one chunk's unused tail can be where the caller bumps next, and the adopt keeps the caller's (ASYNC-15), so the region's first chunk keeps whatever it had left unused. A loop that takes no release each pass (a function that calls Arena.mark(), src/escape.ts, "Arena control", or a loop that keeps what it makes) therefore grows by that tail for every call it joins, not only by the bytes the calls answered. |
Open. Measured with a loop that calls Arena.mark() and joins two calls a round, each answering 100 i32 after a wait: about 1.3 KB kept per joined call, against about 0.43 KB answered; 30000 rounds peaked at 76 MB (five runs, 76.0 to 78.4 MB) and 150000 rounds at 386 MB, where main before this stage peaked at 202 to 250 MB at 150000 rounds, part of that HF-13's interleaving. A loop that releases each pass, or a caller whose memory is given back when it returns, gives all of it back. Keeping the region's tail as well would need a free list of tails or copying the answer out of the region; neither is a few lines, and making the region's tail the active one instead would bury the caller's, which is ASYNC-15's first cause. tests/link/async_await_all_retain, a loop of this shape, bounds its peak at 6 MB over 2000 rounds, so a call that kept more than this tail would fail it. |
| ASYNC-18 | High | runtime/runtime-async.c (nish_async_task_of, nish_async_cancel, nish_async_cancel_after, nish_async_cancel_task, nish_async_disarm, nish_async_done) |
A cancel names a task by a handle a program keeps: one kept past its task's end, once the node is reused, would cancel whatever task holds the node now, and main must not be cancelled at all. A cancelled or finished task's waits and its deadline must not stay in the wait table or the timer heap, or a server that cancels a task per hang-up would grow them per request, and a deadline that fired late would cancel a later task. A task that cancels itself while awaitAll runs its calls has its own pending waits fired, which queue it; it then finished still on the ready queue, and nish_async_done put the node on the free list, so the loop resumed a freed task and the next spawn reused a node the queue still held: a use-after-free, found by review before the stage merged. |
Fixed before landing. nish_async_done takes a task that is still queued off the ready queue before it frees the node. A handle is the task's generation and index, checked as a completion's is (ASYNC-1), so a stale handle, and main's (index 0), answers false and cancels nothing. A cancel fires each pending wait of the task through nish_async_fire, which takes it out of the heap and closes its duplicate descriptor, and disarms the deadline; a task that ends disarms its own; a deadline that fires frees its slot before it cancels. Pinned by link/async_cancel's probe, which reads nish_async_stat (live tasks, heap timers) after each step: "cancel a finished task: 0, its deadline: 0", "cancel main: 0, a stale handle: 0", "flat after a cancel: yes", "flat after a deadline: yes", "flat after a timeout: yes", "flat after cancelling a reader: yes", "flat after done: yes, nothing left to run: yes", and, for the self-cancel, "a self-cancel: 1, answers 0, finished queued: flat yes, nothing left to run: yes": the probe fails on the runtime before the fix and passes after. |
| ASYNC-19 | Low | runtime/runtime-region.c (nish_region_adopt, nish_region_child_new) |
Child regions (child, childWith) link a region to its parent, and free of the parent walks that tree. Adopting a region that has a parent would move its chunks into the current arena while it stayed on its parent's list, so the parent's later free would free them a second time; adopting a parent with children would retire it and leave its children linked to a dead slot, so their memory would never be freed and the parent's index, once reused, would name children it never made. |
Decided: refused. An awaitAll lane's region is made by nish_region_new with no parent, and child and childWith are refused in a spawned function or anything it calls (NL2351), so no lane's region has a parent; a lane that called child under its own region would make it a parent. nish_region_adopt therefore panics with "nish: adopt of a region with a parent or children" before it touches the slot, rather than unlink or re-home the tree. Pinned by the adopt-child and adopt-parent modes of the region section of tests/run.js (tests/runtime/region-test.c), each exiting 1 with that message, and by tests/cases/reject_region_child_spawned. |
| ASYNC-20 | Low | runtime/runtime-async.c (nish_async_spawn, nish_async_done) |
A finished task's node keeps its region for the next spawn instead of freeing it and making another, so a request's task costs no malloc, free or region slot. Nodes are kept per task kind, one kind per spawned async function, so the next task to hold the node is a later spawn of the same function, never of another. That task then allocates from a chunk the last one wrote, which is not zeroed: one request's bytes must not be readable by the next, a pointer into the chunk must not outlive the task that made it, and a server must not keep every task's high-water mark for ever. |
Holds. Arena memory was never zeroed, recycled or not: a chunk from malloc holds what the allocator last freed, and a region's own reset reuses its chunk the same way. What the language promises fresh the compiler writes before a program can read it: every field of an object, every character of a string, every element of new Array<T>(n) (zero-filled), and a program has no way to read arena bytes it did not write, so the last task's bytes are unreachable from the next. That is a promise about what a program reads, not about every byte the allocation spans: only a declare class layout struct is zero-filled whole, padding included (src/emit-classes.ts, emitNew), because C may copy it out whole; every other object is an uninitialised bump allocation (nish_alloc_struct, runtime/runtime.c) whose fields are then stored one by one, so its padding, and the unused arm of a fresh Result (src/emit-result.ts), still hold whatever the last task wrote there, deterministically so, since the kept chunk comes back at the same address. No construct reads those bytes: nothing in src/ or the runtime copies or compares a whole non-layout struct, and the checker proves no path reads a Result's unused arm. The residual is interop: a way to hand a whole non-layout struct to C would expose its padding, and none is proven absent beyond that search; tests/link/async_recycle_fresh spawns one async function in two roles, since only a spawn of the same function takes the node: a task fills its region with nonzero bytes (every other round through an awaitAll, whose answers are carved into its chunk) and ends, and the next reads new arrays before writing them, after checking by taskId() that it holds the node the writer released, one generation on ("readers that ran on the node the writer released: 200", "elements a new array did not hold as zero: 0" over 200 rounds; with the zero-fill of new Array<T>(n) deleted from the program's IR the second line reads 24970). The zero-fill is the only thing between the two tasks, and it is the compiler's, not the runtime's. A Secret a task did not wipe is therefore still in the kept chunk when the node's next task starts, as before it was still in the memory free took back: unreadable from Nish either way, and overwritten only as the next task allocates over it. Recycling moves where unwiped bytes wait, from the allocator's free memory to a chunk the node holds for as long as it keeps one; it is the wipe, not the task's end, that removes them, as it always was. Nothing points into the chunk once its task has ended, by the rules that let it be freed before: a task stores none of its memory into what it shares (ASYNC-10, ASYNC-12), and a stale completion or cancel is refused by the node's generation, bumped in nish_async_done as before (ASYNC-1, ASYNC-18). The region's own handle never leaves the runtime and a task's region has no children (NL2351, ASYNC-19), so keeping its slot and generation across tasks names nothing a program holds. nish_async_done enters the region once more to rewind it, so the checks of nish_region_enter and nish_region_leave (a stale handle, another thread's region, one already entered) guard the recycle as they guard a resume, and runtime-region.c is as it was. Kept memory is bounded: a node keeps the newest chunk only when it is at most 64 KiB, and no chunk at all while the free nodes together keep 16 MiB, so a task that grew past that gives everything back as before and a burst of large tasks leaves at most 16 MiB and one chunk behind; under AddressSanitizer nothing is kept, so a use after a task's end still reports. A node that has run a task also holds its region's slot for the life of the process, one of the 2^24 the runtime has: as many slots as each task kind's peak of live tasks, where before it was the tasks live at that moment, and a slot's generation no longer moves per spawn. tests/link/async_region_recycle drives it from C: "of 100000 later tasks, entered with the kept chunk: 100000, entered empty: 100000", "peak resident size moved after round 100: no", "of 100000 after it, entered with one chunk, empty and at most 64 KiB: 100000" (after a task that grew its region through several chunks), "peak resident size moved over them: no" (read once the grown region has stopped growing: the task after the grown one is entered with the 32 KiB chunk it kept and links a 64 KiB one, whose malloc moved the peak on a CI runner, so the probe measures from the first task entered with one chunk that ends with one), "a chunk past 64 KiB is not kept: yes", "what they kept is 16 MiB to 16 MiB and one chunk: yes". This is the runtime memory std/net/serve.ts's flat-memory row leaves unmeasured (serve.md, SERVE-7: "what the runtime allocates outside the arena for a task's frame and region"): per node it is now the frame and at most one 64 KiB chunk, and it does not grow per request. The recycle raised RUNTIME_ASYNC_TEXT_BUDGET from 3,840 to 4,096: runtime-async.c measures 3,910 bytes against 3,797, for the rewind, the two bounds and the count of bytes kept. |
Failures that stop the program
The executor ends the process with a message on stderr, and status 1, rather
than run on in a state it cannot keep (nish_async_die):
-
nish: out of memory: the task slab, the wait table or the timer heap could not grow; -
nish: the async event loop could not be made: the poller (epoll_create1orkqueue) could not be made, on the first wait; nish: more than 4294967294 async tasks or waits: too many to index;-
nish: the async event loop could not wait:epoll_waitorkeventfailed with anything butEINTR. Before this, such a failure looked like an empty report, and the loop spun on it.
Properties verified
-
The executor is not linked into a program without
async.scripts/build.shlinksruntime-async.conly when the IR declares a@nish_async_function; a non-async program's IR is byte-identical to its IR before the executor landed, andllvm-nmfinds nonish_asyncsymbol in its binary (tests/run.js, "async: a program withoutasynclinks nonish_asyncsymbol"). -
Nothing in the executor calls into the program. The driver resumes a
node through a
switchover the task kinds the compiler knows; a node holds no function pointer, so a corrupt node header can reach only the switch's trap. - A spawned task's shared objects outlive it, and hold none of its memory. The spawner and its callers keep what they allocated past the spawn, and the task-mode region walk refuses a store of the task's region memory or the growth of a shared container (ASYNC-10), and the shared object put into the task's own memory (ASYNC-12), with a bodiless builtin's facts saying what it writes and stores (ASYNC-13).
-
Its size is budgeted.
tests/runtime-budgets.jsholds its-Oz.texttoRUNTIME_ASYNC_TEXT_BUDGET.
Tests
tests/link/async_late_completion(ASYNC-1, ASYNC-2's check)tests/emit-ts/async/sleep,tests/cases/async_sleep(ASYNC-3)tests/link/async_timer_order(ASYNC-3, ASYNC-5's timer order)tests/link/async_fd_reuse,tests/link/async_readable(ASYNC-4)-
tests/link/async_spawn_args,tests/cases/async_sleep_spawn_string,tests/cases/reject_async_spawn_*(ASYNC-7) -
tests/link/async_spawn_shared_outlives,tests/cases/async_spawn_shared,tests/cases/async_spawn_fn_arg,tests/cases/reject_async_spawn_store_*,tests/cases/reject_async_spawn_grow*,tests/cases/reject_async_spawn_keep_*(ASYNC-12),tests/cases/reject_async_spawn_grow_reserve,tests/cases/reject_async_spawn_store_json(ASYNC-13),tests/cases/reject_async_spawn_nested,tests/cases/reject_async_spawn_block_arg,tests/cases/reject_async_spawn_using_arena(ASYNC-7, ASYNC-10) -
tests/cases/reject_async_spawn_region_field,tests/cases/reject_async_spawn_secret_field,tests/cases/reject_async_spawn_handle,tests/cases/reject_async_spawn_threads_field,tests/cases/reject_async_spawn_grow_piped,tests/cases/reject_async_spawn_grow_map(ASYNC-11) tests/link/async_region_per_task,tests/link/async_slab_flat(ASYNC-8)tests/link/async_region_recycle,tests/link/async_recycle_fresh(ASYNC-20)tests/link/async_await_all_lanes,tests/cases/async_await_all_lanes_large(ASYNC-14)-
tests/link/async_await_all_retainand its peak intests/run.js,tests/link/async_await_all_small(ASYNC-15) tests/link/async_cancel(ASYNC-6, ASYNC-18)-
adopt-child,adopt-parentintests/run.js'sregionsection,tests/cases/reject_region_child_spawned(ASYNC-19) tests/link/async_sync_main_loop(the loop a synchronousmainruns)tests/runtime-budgets.js(RUNTIME_ASYNC_TEXT_BUDGET)

