| @@ -87,7 +87,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 87 | 87 | - `@builtin` keeps a checked meaning: it is allowed only on an opaque type in a `std/` module, and documents that the runtime implements it. No phase reads it. |
| 88 | 88 | - Attributes now belong to their item only. Before, constructors, trait methods and effect operations got copies of their parent's; nothing read them. Attributes written on methods were silently dropped; they are now reported. |
| 89 | 89 | |
| 90 | | −- [ ] **H3. HTTP framing and the runtime's pure functions have no tests of their rejecting paths.** Size **S**, PRs **(3)** and **(4)**. |
| 90 | +- [x] **H3. HTTP framing and the runtime's pure functions have no tests of their rejecting paths.** Size **S**, PRs **(3)** and **(4)**. |
| 91 | 91 | - **The code at stake:** |
| 92 | 92 | - `tilrt/src/net.rs:347` `tl_http_parse_request` rejects a non-decimal `Content-Length` and conflicting duplicates. That is the defence against request smuggling on kept-alive connections, as its own doc says. |
| 93 | 93 | - `net.rs:407` `tl_http_parse_chunk` handles chunked bodies. |
| @@ -99,6 +99,12 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 99 | 99 | 1. Factor the pure parsing out of the two externs into Rust functions over `&[u8]` that return Rust enums. The externs only build the cells. |
| 100 | 100 | 2. Unit-test those functions: partial input, invalid input, duplicate and conflicting `Content-Length`, an oversized chunk, and offsets past the end. |
| 101 | 101 | 3. Add `cli.rs` cases that send malformed and smuggling-shaped requests, and expect 400 or 501 with the connection closed. |
| 102 | + - **Status (PR 3, done):** |
| 103 | + - **The parsers** moved from `net.rs` (sockets) to `tilrt/src/http.rs`. `parse_head` and `parse_chunk` are plain Rust over `&[u8]` returning Rust enums; the externs only build `std/http`'s `Head` and `Chunk`, whose tags are named constants. Their bounds use `tilrt-prims`' `SMALL_INT` (the two `1 << 62` M3 left). Behaviour is unchanged. |
| 104 | + - **Unit tests** (12 in `http.rs`): partial input; offsets at and past the end, and negative ones; malformed request lines and headers; whitespace before a colon; more than 64 headers; `Content-Length` that is empty, signed, hexadecimal, non-ASCII, not UTF-8, 2^62 or more, or repeated with a different value (and accepted when repeated with the same one); both framings reported as they are; header values kept byte for byte; chunk sizes with extensions, invalid, cut, overflowing or 2^62 and more; the last chunk and its trailers; and, in a test actor under the leak oracle, the `Head` and `Chunk` values the externs build. |
| 105 | + - **End to end:** `crates/tilc/tests/cli.rs` `http_rejects_malformed_requests` (fixture `tests/fixtures/http/malformed.til`) sends conflicting and non-decimal lengths, both framings (400), `Transfer-Encoding: gzip, chunked` (501), whitespace before a colon, an invalid, an unterminated and an oversized chunk (400), and a chunk over the body limit (413), each with a request pipelined behind it. Each gets exactly one error response, the connection closes, and the pipelined request is never served; agreeing duplicate lengths are served. Nothing leaks. The 413 by `Content-Length` and by summed chunks stays in `http_body_limit_and_write_timeout`. |
| 106 | + - **More broadly:** `tilrt` has 34 `#[test]`s (18 on the v1 base, 9 at the audit), and `tilrt-prims` 7. New: `int::parse`, small ints read without a heap, `utf8_error`, the hash values (pinned in `tilrt-prims` to what `tilc run` prints), and the lists and tuples `tl_string_split` and `tl_string_parse_int` build (a test actor, leak oracle). `url_decode`, `html_escape`, `split` and the rejection sampling are tested in `tilrt-prims` (PR 2). |
| 107 | + - Found on the way: N4. |
| 102 | 108 | |
| 103 | 109 | ### Medium |
| 104 | 110 | |
| @@ -145,6 +151,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 145 | 151 | - The crate is `runtime/tilrt-prims` (it is the runtime's semantics, which the interpreter shares, so it sits with the runtime). It is dependency-free and holds `mix`, `hash_bytes`, `hash_limbs`, `hash_combine`, `split`, `html_escape`, `url_decode`, `random_bytes`, `random_below`, `SMALL_INT` (the 63-bit range) and `PROC_OPS`, with unit tests (URL decoding, escaping, splitting, the rejection of the biased zone). |
| 146 | 152 | - `tilrt` (`prims.rs`, `int.rs`, `bytes.rs`, `process.rs`), the interpreter and codegen call it; their copies are gone. `tl_random_int` in the interpreter now uses rejection sampling, like the runtime. |
| 147 | 153 | - **Left:** `net.rs`'s two `1 << 62` (PR 3 refactors those parsers, H3), and `tilc_hir`'s `i128` range check of literals (front end; a front-end crate should not depend on the runtime's). |
| 154 | + - **Status (PR 3, done):** the HTTP parsers (now `http.rs`) check lengths against `SMALL_INT`. PR 3's tests pin the hash values in `tilrt-prims`. |
| 148 | 155 | |
| 149 | 156 | - [x] **M4. Std items are identified by name and constructor position across all layers.** Size **M**, PRs **(1)**, **(2)** and **(3)**. |
| 150 | 157 | - **Effect names as strings:** |
| @@ -168,6 +175,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 168 | 175 | - Core carries `EffectDecl::builtin: Option<BuiltinEffect>` (`Proc`, `St`, `Alloc`), set by lowering from `WellKnown` (which now resolves `proc` too, and whose `find` is public). Codegen (`check_supported`, the `ref` operations, the never-unwinds analysis) and the interpreter's scheduler match on it; no back end compares an effect's name. Region lowering takes `st` and `alloc` from `WellKnown` instead of `module_by_path("std/ref")`. |
| 169 | 176 | - `tilc_core/tests/runtime_constructors.rs` asserts the tag of every constructor that `tilrt`, the interpreter or lowering builds by number (`Bool`, `List`, `Option`, `Result`, `Order`, the `std/process` types, the HTTP parse results) against the analysed std. |
| 170 | 177 | - **Not done, deliberately:** the runtime and the interpreter still write those tags as numbers. With the test, reordering a constructor fails instead of silently breaking a back end; generating the numbers would add machinery for types that do not change. Lowering still finds `std/html`'s constructors by name (`html_ctor`): lowering is where names become tags. The `ref` operations are told apart by name (`get`) within the `St` effect. |
| 178 | + - **Status (PR 3, runtime part, done):** the runtime writes each tag in fewer places. `tl_string_split`, `tl_string_parse_int` and `tl_os_env` build their lists and tuples with `values::list` and `values::cell` (L2), `process::data` uses `values::cell`, and `http.rs` names `Head`'s and `Chunk`'s tags. Unit tests check the tags and fields of the values these externs return; `docs/runtime-abi.md` lists the constructors the runtime builds. |
| 171 | 179 | |
| 172 | 180 | - [ ] **M5. Which module a path denotes is decided in four places, and one of them is wrong.** Size **S**, PR **(4)**. |
| 173 | 181 | - **The four places:** |
| @@ -181,7 +189,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 181 | 189 | - Add `Db::add_target(path) -> Result<Target, LoadError>` in `tilc_db`. It loads the package or the file, and returns the module(s) the path names, from its place under `src/` or `test/`. The driver subcommands and the LSP all use it. |
| 182 | 190 | - Make manifest errors diagnostics pointing into `tilcayo.toml`, so the CLI and the LSP both report them. |
| 183 | 191 | |
| 184 | | −- [ ] **M6. Oversized files and functions.** Size **M**, PRs **(1)**, **(2)** and **(3)** (mechanical moves along existing section comments). |
| 192 | +- [x] **M6. Oversized files and functions.** Size **M**, PRs **(1)**, **(2)** and **(3)** (mechanical moves along existing section comments). |
| 185 | 193 | - `tilc_core/src/lower.rs` (2,002 lines). Its `// ----` sections are already in place, so the split is mechanical: |
| 186 | 194 | - move "derived impls" (`:679-988`) to `lower/derive.rs`; |
| 187 | 195 | - move "HTML" (`:1164-1290`) to `lower/html.rs`; |
| @@ -208,8 +216,12 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 208 | 216 | - `tilc_core/src/lower.rs` (1,987 lines) became `lower/mod.rs` (1,278), `lower/derive.rs` (299), `lower/html.rs` (104) and `lower/pattern.rs` (332), along its section comments. Methods called across modules are `pub(super)`. |
| 209 | 217 | - `tilc_codegen_c/src/emit.rs` (1,290) became `emit/mod.rs` (1,023) and `emit/effects.rs` (206: the `Perform`, `Handle`, `Region` and `Defer` arms of `value`, now methods). The C prelude is `emit/prelude.h`, read with `include_str!`. `value` lost an unused `held` parameter. |
| 210 | 218 | - **Not done, deliberately:** the interpreter scheduler's `request` (now in `tilc_interp/src/sched.rs`, 193 lines) stays one `match` over the operations; one function per operation is not a mechanical move, and this PR keeps to those. `emit`'s `expr` (205 lines) is one loop over a `match` on Core statements. |
| 219 | + - **Status (PR 3, runtime, done):** moves were verbatim (checked by comparing the moved lines). |
| 220 | + - `net.rs` (431 lines on the v1 base) became `net.rs` (321: sockets) and `http.rs` (488, of which 300 are tests: HTTP framing, H3). |
| 221 | + - `prims.rs` (663 lines after PR 2) became `prims.rs` (447: the std externs over floats, strings, randomness and the environment, panics, shared helpers), `region.rs` (154: regions and their references) and `resource.rs` (60). Both are runtime block kinds with a hook `heap.rs` calls when freeing. |
| 222 | + - **Not done, deliberately:** `sched.rs` (1,367 lines at the audit, 1,639 now) is not split into actor, reactor and clock modules. Its timers already moved to `timers.rs` (ADR 0004, Revision 5), and the rest was reworked and reviewed for Revisions 4 and 5 and ThreadSanitizer; moving it now would churn code under review, for a split that is easier after that review. `handlers.rs` (837) and `fiber.rs` (826) each hold one concern. |
| 211 | 223 | |
| 212 | | −- [ ] **M7. The `unsafe` boundary is not where the pointers are dereferenced.** Size **M**, PR **(3)**; needs engineering review. |
| 224 | +- [x] **M7. The `unsafe` boundary is not where the pointers are dereferenced.** Size **M**, PR **(3)**; needs engineering review. |
| 213 | 225 | - **What works:** every `unsafe` block has a `SAFETY` comment (CI enforces it). |
| 214 | 226 | - **What does not:** |
| 215 | 227 | - **`prims::dup` (`prims.rs:101`)** is a *safe* function that dereferences any `Value` as a block. It also duplicates `heap::dup_value` (`heap.rs:127`), which is correctly `unsafe`. |
| @@ -220,6 +232,15 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 220 | 232 | - Delete `prims::dup` and use `heap::dup_value`. |
| 221 | 233 | - Mark the panicking arithmetic `unsafe extern "C"`. C callers are unaffected. |
| 222 | 234 | - Extend the `Fiber` `SAFETY` argument to cover `LOCAL` and `stats::MINE`: make `stats::add` `#[inline(never)]`, or explain why it is not needed. Have it reviewed. |
| 235 | + - **Status (PR 3, done; engineering review pending):** |
| 236 | + - On the v1 base the panicking arithmetic (`tl_int_div_slow`, `tl_int_rem_slow`) and `tl_random_int` were already `unsafe extern "C"`. |
| 237 | + - `prims::dup` is gone; `heap::dup_value` returns its argument, like the C `tl_dup`. |
| 238 | + - `panic_with` is `unsafe`, and each caller says why its context is the running actor's. `tl_coerce` (a safe extern that dup'd its argument) and `array::out_of_range` (which reads the array) are `unsafe`. A scan of every safe function taking a raw pointer or a `Value` found no other that dereferences one. |
| 239 | + - **The rule** is in the `tilrt` crate doc: a function that may dereference a context, a `Value` or another raw pointer is `unsafe`. Functions that only allocate and fill a fresh block stay safe though they take a context (`values::cell`, `ok`, `error`, `list`, `string`, `bytes_from`, `tl_float_from`, `tl_region_new`, `tl_resource_new`, `tl_closure_alloc`, `int::from_big`): `tl_alloc` never reads its context, which its doc now states as an invariant they rely on. |
| 240 | + - **Not done, deliberately:** turning `*mut Ctx` into `&mut Ctx` at the entry points. Generated C, closures and callbacks the runtime calls write through the same context, so a `&mut Ctx` held across those calls would itself break Rust's aliasing rules; and it would touch every extern for no gain in what is checked. |
| 241 | + - **`Fiber: Send`.** The `SAFETY` comment now names every thread-local reached on an actor's fiber and why its address is not reused after the fiber moves (the optimiser treats a thread-local's address as constant within a function, after inlining). `IN_SLICE` and `DEFERRED` (read inline in `Shared::push` since issue #61) and `LOCAL` in `local_index` are now read in `#[inline(never)]` functions, like `push_local`: no measurable cost. `callback`'s thread-locals are read only while a C call is in progress, when the actor never suspends. |
| 242 | + - **`stats::MINE` stays inline.** Out of line, it cost 12% on `tests/bench/floats.til` and 2–3% on `lists` and `dict` (interleaved runs). Disassembling the release `libtilrt.a` shows no function that both reaches a thread-local and calls something that suspends (only `sched::worker`, which runs on the scheduler thread's own stack). Nothing guarantees this across compiler versions or inlining changes; if it broke, an allocation after a migration would update another thread's count (a wrong leak-oracle total) or, if that thread never allocated, register into its thread-local slot (a data race). |
| 243 | + - **For engineering review:** (a) whether relying on today's inlining for `MINE` is acceptable, or the oracle should pay the call, or count per actor through `ctx->rt` (which would make `tl_alloc` read its context, and the helpers above `unsafe`); (b) the `Fiber` argument as a whole. |
| 223 | 244 | |
| 224 | 245 | - [ ] **M8. The test harnesses duplicate the pipeline and the expectation format.** Size **S**, PRs **(2)** and **(4)**. |
| 225 | 246 | - **The pipeline** (analyse → errors → `lower` → `entry`) is copied 7 times: |
| @@ -263,6 +284,11 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 263 | 284 | - Fix the `sched.rs` doc. |
| 264 | 285 | - Give each ADR section exactly one status, and make the index match it. |
| 265 | 286 | - Mark the binding-facing part of `tilrt.h`. |
| 287 | + - **Status (PR 3, runtime docs, done; PR 4 open for the ADRs):** |
| 288 | + - `docs/runtime-abi.md` describes the ABI the code implements, section by section (values, blocks, context, calls, reference counting, unwinding, preemption, handlers, actors, native dependencies, the standard library in the runtime), with a status table pointing at the ADRs, PR 2's "Adding a runtime extern", and a short History of what changed when. The anchors other documents link to are kept. Fixed on the way: Revision 1's table-driven unwinding (never implemented) presented as current, the shared round-robin queue, a 4000-entry budget (it is 2000), "`TL_ABI_VERSION` stays 1" for callbacks (it is 2), v0's single thread and 256 KiB stacks, and the "Cycles through `Ref`s leak" limitation (regions clear them). |
| 289 | + - The `sched.rs` module doc no longer describes one shared round-robin queue, and names all five revisions of ADR 0004. |
| 290 | + - `tilrt.h` says which functions C bindings use (the rest is for generated code), and loses its "M0: single scheduler thread" and "opaque in ABI v0" notes. Comment changes only; PR 2 added the missing prototypes (H1). |
| 291 | + - The ADRs' contradictory statuses are PR 4's; `runtime-abi.md` points at them instead of restating them. |
| 266 | 292 | |
| 267 | 293 | ### Low |
| 268 | 294 | |
| @@ -270,7 +296,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 270 | 296 | - The `tilc run --native` help says "(M5: programs without `ctl`, `final ctl` or actors)" (`tilc/src/main.rs:39-40`). Everything compiles natively now. |
| 271 | 297 | - The `tilc_lsp` crate doc (`lib.rs:3-5`) lists 6 features. The server implements 10: references, formatting, inlay hints and semantic tokens are missing from the list. |
| 272 | 298 | - **Status (PR 2):** the back end's stale milestone note is gone (`tilc_core/src/ir.rs` said C codegen was to come). The two items above are PR 4's. |
| 273 | | −- [ ] **L2. Small dead code and internal duplication.** Size **S**, PRs **(2)** and **(3)**. |
| 299 | +- [x] **L2. Small dead code and internal duplication.** Size **S**, PRs **(2)** and **(3)**. |
| 274 | 300 | - **Unused or redundant code:** |
| 275 | 301 | - `check_supported` has an unused `_reachable` parameter (`tilc_codegen_c/src/lib.rs:168`); |
| 276 | 302 | - `prims::text_of` only forwards to `text` (`prims.rs:51-64`). |
| @@ -278,6 +304,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 278 | 304 | - `fn string` is defined twice (`prims.rs:67`, `net.rs:431`); |
| 279 | 305 | - `tl_string_split` (`prims.rs:343-354`), `tl_os_env` (`:683`) and `tl_string_parse_int` (`:368`) build cells by hand instead of using `values::list` and `values::cell`. |
| 280 | 306 | - **Status (PR 2):** `check_supported` lost its `_reachable` parameter, and codegen's `value` its unused `held`. The `tilrt` items are PR 3's. |
| 307 | + - **Status (PR 3, done):** `prims::text_of` is gone (`values::text`), the two `fn string` are one `values::string`, and `tl_string_split`, `tl_os_env` and `tl_string_parse_int` use `values::list` and `values::cell`. |
| 281 | 308 | - [x] **L3. A hand-copied ABI constant with no compile-time check.** Size **S**, PR **(2)**. `XARGS = 26` is in `emit.rs:14` ("must match"), `abi.rs:92` and `tilrt.h:119`. The Rust and C sides are asserted against each other; codegen is not. The fix: emit `_Static_assert(TL_XARGS == 26, …)` in the generated prelude. |
| 282 | 309 | - **Status (PR 2, done):** every generated C file asserts `TL_XARGS == 26` (from codegen's `XARGS`) right after the prelude. A mismatch is a C compile error. This is the one change to every generated file. |
| 283 | 310 | - [ ] **L4. Stringly-typed errors in crate APIs.** Size **S**, PR **(4)**. |
| @@ -300,6 +327,10 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 300 | 327 | - The decision-tree snapshot (`tilc_core/tests/snapshots/lowering__decision_trees.snap`, `both`) shows it: matching `(a, b)` against the four combinations of `Some`/`None` and `Ok`/`Error` projects both columns once per row (8 projections), and when a row fails it tests the next row from its first column, so the tree repeats switches already decided and keeps `panic` arms that cannot be reached. The result is correct (every `tests/run` program agrees with the interpreter) but grows with rows times columns. |
| 301 | 328 | - **The fix:** a column-based decision tree (specialise the matrix on the constructor of one column, as in Maranget's "Compiling pattern matching to good decision trees"), sharing projections. Not done here: it changes the generated code, which these PRs keep byte-identical. |
| 302 | 329 | |
| 330 | +- [ ] **N4. `Content-Length` accepts Unicode whitespace around the number.** Size **S**, PR: a behaviour change, outside the behaviour-preserving cleanliness PRs. |
| 331 | + - `tilrt/src/http.rs` `decimal_length` trims the value with `str::trim`, which removes Unicode whitespace, and httparse lets bytes 0x80–0xFF into header values (obs-text). So `Content-Length: \u{a0}5` (a no-break space) is read as 5. RFC 9110 allows only spaces and tabs around a field value, and a proxy in front of the server may read the header differently: a request-smuggling risk, though a small one (most proxies reject the header). |
| 332 | + - **The fix:** trim only spaces and tabs (`trim_ascii` would also do), and add the case to the unit tests and to `http_rejects_malformed_requests`. Left as is because it rejects requests that are served today. |
| 333 | + |
| 303 | 334 | ## Deliberate choices, not defects |
| 304 | 335 | |
| 305 | 336 | - **The reference interpreter and its scheduler** (`tilc_interp`; `tilc_core/src/interp.rs` and `sched.rs` when audited) re-implement what `tilrt` does. They are the oracle: `tilc_codegen_c/tests/native.rs` runs every `tests/run` program natively with `TILC_THREADS=1`, and requires the same `//>` output and zero live blocks. Keep them. Share only the pure leaf helpers (M3). |
| @@ -307,7 +338,7 @@ The reference interpreter duplicates the runtime's semantics on purpose: it is t |
| 307 | 338 | - **There are two parsers.** The rowan parser is the compiler's; the tree-sitter grammar (`editors/tree-sitter-tilcayo`) serves editors. A test keeps them in step: the grammar must parse every `.til` file in the repository (`bindings/rust/lib.rs:78`), and CI regenerates it and diffs. |
| 308 | 339 | - **No query framework; the whole package is re-analysed** (ADR 0006), with a stated condition for revisiting. |
| 309 | 340 | - **`runtime/spike/spike.c`** is the M0 ABI spike. It is still CI's C-side ABI smoke test, including under ASan/UBSan (`scripts/ci.sh`), so it is not dead code. Its name suggests a throwaway, though; consider renaming it `runtime/abi-test`. |
| 310 | | −- **Two failure policies in the runtime:** `expect("tilrt: …")` aborts on a runtime invariant violation, while user errors go through `tl_panic`. This is documented (`runtime-abi.md:175`). |
| 341 | +- **Two failure policies in the runtime:** `expect("tilrt: …")` aborts on a runtime invariant violation, while user errors go through `tl_panic`. This is documented (`runtime-abi.md`, "Known limitations"). |
| 311 | 342 | - **The runtime's own context switch** (ADR 0011) is assembly on x86_64 Linux, with corosensei elsewhere. The fallback is type-checked for aarch64 in CI. |
| 312 | 343 | - **Per-thread leak counters with plain stores** (`heap.rs:320-352`; ADR 0002, implementation note) are a measured performance trade-off. The `stats` feature is on by default. See M7 for the open review point. |
| 313 | 344 | - **`let _ = writeln!(…)` appears 104 times in `emit.rs`.** Writing to a `String` cannot fail. |