From cc831f553f65b3ed6ad026f1b26e2835e24cd86f Mon Sep 17 00:00:00 2001 From: Owen Carey <37121709+owenthcarey@users.noreply.github.com> Date: Sat, 28 Nov 2026 00:14:10 -0800 Subject: [PATCH 1/4] feat!: give fast safe code a heap of per-thread pool entries (RFC 0055) RFC 0055, accepted as drafted. A fast safe unit's heap is one design for C and C++: - Each thread keeps an entry of free slots for each pool it uses. A typed allocation (__weavec_rt_take) is a pop and one stored word; a typed release (__weavec_rt_give) compares the slot's word, stores the released one, zeroes the object and pushes. No compare-and-swap, no records, no counters. Refills and flushes move half an entry, and a pool that grows takes fresh slots by the half entry. - A realloc keeps its block's type (__weavec_rt_realloc_typed). - The reference test (O4) belongs to the checked build in Safe C as in Safe C++, so a program with Safe C keeps its pooled slots' shadow. - Pools are keyed by layout as well as name. The in-line forms (section 6) were measured in S0 and not shipped. Breaking: runtime ABI 50, ledger 15. The library table's typed clauses name take, give, calloc_typed and realloc_typed. A fast Safe C build no longer stops at a stale reference before the slot is reused; test with -fweavec-safe-checks=full. --- AGENTS.md | 20 +- docs/architecture.md | 25 +- docs/pages/guides/safe-c.md | 8 +- docs/pages/guides/safe-cxx.md | 2 +- docs/pages/reference/cli.md | 4 +- docs/pages/reference/guarantees.md | 10 +- docs/rfcs/0055-a-heap-for-safe-code.md | 800 ++++++++++++++++++ docs/rfcs/README.md | 1 + docs/roadmap.md | 34 +- docs/scripts/check-examples.py | 2 +- include/weavec/Core/LibrarySpec.h | 5 +- include/weavec/Frontend/EnforcementLedger.h | 6 +- lib/Core/LibrarySpec.cpp | 17 +- lib/Core/LibrarySpec.txt | 46 +- lib/Frontend/EnforcementLedger.cpp | 13 +- lib/Frontend/GuardExpand.cpp | 6 +- lib/Frontend/GuardFast.cpp | 46 +- lib/Frontend/GuardLibrary.cpp | 35 +- lib/Frontend/GuardPassImpl.h | 7 +- lib/Frontend/GuardSafe.cpp | 25 +- runtime/test/rt_test.c | 198 ++++- runtime/weavec_alloc.c | 856 +++++++++++++++----- runtime/weavec_asan.c | 12 +- runtime/weavec_cxx.c | 7 + runtime/weavec_malloc.c | 12 + runtime/weavec_rt.exports | 3 + runtime/weavec_rt.h | 39 +- scripts/corpus-gate.py | 2 +- scripts/run-cases.py | 2 +- scripts/test_corpus_gate.py | 8 +- scripts/test_run_cases.py | 6 +- test/Driver/safe-cxx-threads.cpp | 2 +- test/Driver/safe-cxx-units.cpp | 2 +- test/Driver/safe-survey.cpp | 2 +- test/Driver/typed-units.c | 8 +- test/Guards/ir-pools.c | 48 +- test/Guards/ir-trusted.cpp | 2 +- test/Guards/ledger.c | 2 +- test/Guards/placed-local.cpp | 8 +- test/Guards/pool-layouts.cpp | 32 + test/Guards/realloc-typed.c | 53 ++ test/Guards/reference-load.c | 20 +- test/Guards/report-json.c | 2 +- test/Guards/typed-local-types.c | 6 +- test/Guards/typed-local-types.cpp | 6 +- test/Guards/typed-reuse.c | 23 +- test/Guards/unsafe-region.c | 2 +- test/cases/ownership/reference-dangling.c | 5 +- unittests/Core/LibrarySpecTest.cpp | 25 +- 49 files changed, 2134 insertions(+), 371 deletions(-) create mode 100644 docs/rfcs/0055-a-heap-for-safe-code.md create mode 100644 test/Guards/pool-layouts.cpp create mode 100644 test/Guards/realloc-typed.c diff --git a/AGENTS.md b/AGENTS.md index f9541ce7..f71dedd6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -61,9 +61,17 @@ RFC 0050 a fast unit checks each fact once: the library's checked views (`std::string_view`, `std::span`; the table's `checked` classes) are trusted as its owners are, a view safe code makes from a pointer is guarded once where it is made, a range-based `for`'s iterator calls are -trusted, a pooled slot safe C++ released keeps its shadow and the -allocator pops and pushes on a short path (`poolTake`, `poolGive`), and -Safe C rejects a library call on a pointer to one object (`call-args`). Since +trusted, a pooled slot safe code released keeps its shadow, and Safe C +rejects a library call on a pointer to one object (`call-args`). Since +RFC 0055 a fast unit's heap is one design for C and C++: the reference +test (O4) is the checked build's, a `realloc` keeps its block's type +(`__weavec_rt_realloc_typed`), and each thread keeps an entry for each pool +it uses, so that a typed allocation is a pop and one stored word +(`__weavec_rt_take`) and a typed release a compared and stored word, the +object zeroed and a push (`__weavec_rt_give`), with no compare-and-swap, +record or counter (`poolTake`, `poolGive`, `refillEntry`, `flushEntry`; +the entries are off, and the slow paths RFC 0050's, in a program with a +checked or verify unit, heap stacks, counters or its own allocator). Since RFC 0049 every rule error is data with the fixes the compiler can write (`lib/Frontend/SafeFix.cpp`: certain, likely from the uses one function shows, or a choice it never writes), its note says the text as a fix-it, @@ -137,7 +145,10 @@ computes (`lib/Frontend/SafeTypes.cpp` marks the forms in the AST before code is made, `GuardTypes.cpp` expands the marks, `runtime/weavec_types.c` answers). Full picture: `docs/architecture.md`. Design and the reasoning behind it: `docs/rfcs/`. -Read `0054-one-of-several-types.md` first (tagged unions, checked +Read `0055-a-heap-for-safe-code.md` first (the entries and the short +paths, typed `realloc`, one contract for both languages; its +*Implementation amendments* and *Measured gates*), then +`0054-one-of-several-types.md` (tagged unions, checked conversions, the type records; its *Implementation amendments* and *Measured gates*), then `0053-callables-and-containers-by-their-loans.md` (callables by their loans, containers of views, polymorphic objects, globals, @@ -340,6 +351,7 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev | Change thread safety (sendable, shareable and frozen types, locked classes, thread starts, loans, guards, globals, `WEAVEC_SENDS`, the `const` path) | `CxxKinds::sendable`, `shareable`, `frozen`, `lockedClass`, `holdsThread` in `lib/Frontend/SafeCxxKinds.h`; in `lib/Frontend/SafeCxxFlow.cpp` `threadStart`, `cross`, `crossCopy`, `slotOf`, `joinsEach`, `lentUse`, `lockedUse`, `lockCall`, `declareGuard`, `threadMember`, `sendsArgument`, `ended`; `global`, `constView` and the `spawns`/`unshared` rows in `lib/Frontend/SafeCxx.cpp`; `threadFacts` and `threadGlobals` for the ledger; the class rows of `lib/Core/LibrarySpec.txt` (`thread`, `joins(…)`, `detaches(…)`, `lock`, `guard`, `locks(…)`, `shareable`, `spawns`; `function std::async : spawns;`), `SendsAnnotation` (RFC 0046); tests `test/Safe/cxx-thread-*.cpp`, `cxx-lock-held.cpp`, `cxx-sends.cpp`, `cxx-const-path.cpp`, `cxx-globals.cpp`, `test/Driver/safe-cxx-threads.cpp`; the guide's "Threads", `docs/pages/reference/guarantees.md` (Z1)–(Z6) | | Change the fast mode (typed allocations and pools, believed pointers, trusted library functions and their clones, frames that share no bytes, the result check, the switching-storage case of `escape`) | `lib/Frontend/GuardFast.cpp` (`markAllocations`, `typeAllocations`, `believedAccess`, `cloneTrusted`, `checkResults`); the symbol lists and `TrustedCalls` in `lib/Frontend/SafeCxx.cpp`; `switchingStore` in `SafeCxxFlow.cpp`, `switches` in `SafeCxxKinds.h`; the `typed(…)` clause of `lib/Core/LibrarySpec.txt`; the pools in `runtime/weavec_alloc.c` (`poolAlloc`, `poolRelease`, `givePooled`), `__weavec_rt_new_typed` in `weavec_cxx.c`, `__weavec_rt_fast_program` in `weavec_asan.c`, `__weavec_rt_frame_escape` in `weavec_guard.c` (RFC 0045); tests `test/Guards/ir-pools.c`, `ir-believed.cpp`, `ir-trusted.cpp`, `typed-reuse.c`, `typed-reuse.cpp`, `stack-escape-result.cpp`, `test/Safe/cxx-switching.cpp`, `test/Driver/safe-checks-modes.cpp`, `rt_test` `pool-*`; the guides' "Fast and checked builds", `docs/pages/reference/guarantees.md` (Y1)–(Y6) | | Change what RFC 0050 checks once (checked views, views made from pointers, range-based `for`, a pooled slot's shadow, the short paths, hardening) | `TrustedCalls` (`checkedView`, `viewLayout`, `madeView`, `rangeFor`) in `lib/Frontend/SafeCxx.cpp`, `libraryHardened` in `SafeCheck.cpp`; `guardMadeViews`, `rangeElement` and the range calls of `cloneTrusted` in `GuardFast.cpp`; `class … : checked` in `lib/Core/LibrarySpec.txt`; `keepsShadow`, `poolTake`, `poolGive`, `lookPool` in `runtime/weavec_alloc.c`, `__weavec_rt_tests_released`, `announce` in `GuardFast.cpp` (RFC 0050); tests `test/Guards/ir-views.cpp`, `view-made.cpp`, `range-instances.cpp`, `ir-pools.c`, `typed-reuse.cpp`, `test/Safe/typed-rules.c`, `rt_test` `pool-keeps-shadow` | +| Change a fast unit's heap path (the threads' entries, `__weavec_rt_take` and `__weavec_rt_give`, refill and flush, joining fresh slots, typed `realloc`, when the short paths are off; RFC 0055) | `PoolEntry`, `takeEntries`, `entryCount`, `shortPaths`, `poolTake`, `poolGive`, `refillEntry`, `flushEntry`, `joinPool`, `weavecRtReallocTyped`, `__weavec_rt_take`, `__weavec_rt_give` in `runtime/weavec_alloc.c`; `weavecRtForeign` (`findProgramAllocator` in `weavec_malloc.c`, `findReplaced` in `weavec_cxx.c`); the `typed(take, F)`, `typed(give, F)` and `typed(realloc_typed, 0)` clauses of `lib/Core/LibrarySpec.txt`; `typeAllocations`, `allocationRecord` and `prepareFast` in `lib/Frontend/GuardFast.cpp`, the `give` and `realloc_typed` redirections in `redirect` (`GuardLibrary.cpp`), `testReferences` in `GuardSafe.cpp`; tests `test/Guards/ir-pools.c`, `typed-reuse.c`, `reference-load.c`, `rt_test` `entries-*`, `realloc-keeps-type`; `docs/pages/reference/guarantees.md` (O4), (Y1) | | Change what a fast unit believes, kept parameters or placed locals (RFC 0048) | `typedAccess`, `typedPointer`, `checkStep`, `inHand`, `escapeCheck`, `placeLocals` and the split of inline functions in `lib/Frontend/GuardSafe.cpp`; `shadowless` in `GuardFast.cpp`; the plain frames in `escapes` (`GuardExpand.cpp`); the heap string check in `expand` (`GuardExpand.cpp`) and `__weavec_rt_string_entry` in `runtime/weavec_guard.c`; `Kept` and `keptAddresses` in `lib/Frontend/SafeCxx.cpp`, the placement marks in `SafeChecker` (`SafeCheck.cpp`); the `call-args` and `index-single` cases `positionIntoSingle`, `libraryAccess`, `readsEnd` in `SafeCheck.cpp`; the pools' slot lists in `runtime/weavec_alloc.c`; tests `test/Safe/cxx-kept.cpp`, `cxx-ends.cpp`, `test/Guards/placed-local.cpp`, `ir-typed-cxx.cpp`, `ir-strings.c`, `stack-escape-entry.cpp`, `test/Driver/safe-checks-modes.cpp` | | Change the fixes errors carry, the evidence, or `--weavec-fix` (RFC 0049) | `FixPlanner` in `lib/Frontend/SafeFix.cpp` (wants, evidence E1–E7, edits, notes), the `want(...)` calls beside the rules' reports in `SafeCheck.cpp`, `SafeCxx.cpp`, `SafeCxxFlow.cpp`; `include/weavec/Frontend/RuleFix.h`; the fixer in `lib/Frontend/Fixer.cpp`; the ledger's `errors`, `evidence`, `command`, `files` and the audit's plan in `EnforcementLedger.cpp`; tests `test/Safe/fix-*.c*`, `notes.c`, `test/Driver/safe-fix*.cpp`, `safe-survey.cpp`, `unittests/Frontend/RuleFixTest.cpp`; the rules' page `fix` | | Change what a safe function leaves undefined (definitions, stops, probes, the rules that reject what asserts, the comparison at the link) | `lib/Frontend/GuardDefine.cpp` (`defineSafe`, run first by GuardInsert), the safe unit's options and `promoteSafeWarnings`, `assumingFlag`, `checkLinkInputs` in `lib/Frontend/Driver.cpp`; `lib/Frontend/SafeUndefined.cpp` (`assumed`, `signal-handler`, `bool` bytes, member pointers, owners of a base); `lib/Frontend/Definitions.cpp` (`recordDefinitions`, `emitDefinitions`, `readLink`); the `handler` row clause of `lib/Core/LibrarySpec.txt`; `__weavec_rt_unreachable`, `__weavec_rt_missing_return`, `__weavec_rt_division`, `__ubsan_handle_missing_return` in `runtime/weavec_guard.c`; the alternate signal stacks and `stack-exhaustion` in `runtime/weavec_report.c` (RFC 0051); the list `resources/safe/undefined.md` and `scripts/check-undefined.py`; tests `test/Guards/ir-defined.c*`, `unreachable.cpp`, `missing-return.c*`, `division.c`, `stack-exhaustion.cpp`, `test/Safe/assumed.c*`, `signal-handler.cpp`, `bool-bytes.cpp`, `defined.c*`, `undefined-translation.c*`, `fix-assumed.cpp`, `test/Driver/safe-flags.cpp`, `definitions.cpp` | diff --git a/docs/architecture.md b/docs/architecture.md index 9fa3e4dd..1b0653f1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -640,14 +640,27 @@ A released slot joins its class's *quarantine* and is never reused from there: only a sweep moves it to the free list (RFC 0036 §3.2), in address order, so that slots are reused in order. RFC 0045 §3: a slot a typed allocation took belongs to the pool of its type and class (`slotPools`, a -side table beside the slot words); a typed release zeroes it, marks it -released in the shadow, leaves its word claimed (so a second release is a -double free) and pushes it on the pool, through a per-thread cache of the -pool, for the next allocation of that type; any other release takes it -out of its pool and quarantines it. Pools hold at most the larger of the +side table beside the slot words); a typed release zeroes it, leaves its +word claimed (so a second release is a double free) and pushes it on the +pool for the next allocation of that type; any other release takes it out +of its pool and quarantines it. RFC 0055 §4–§5: each thread has an *entry* +for each pool, at the pool's number, with room for 128 free slots (each +named by its address, its word and the size of the object whose shadow it +keeps), in a reservation the sweep does not read. In a fast program +(`shortPaths()`: no checked or verify unit, no heap stacks, no counters, +no allocator of the program's own) a take is a pop from the entry and one +stored word (`__weavec_rt_take`), and a give compares the slot's word with +the live word it expects, stores the released one, zeroes the object and +pushes (`__weavec_rt_give`); the slot keeps its object's shadow (RFC 0050 +§3.1). An empty entry is refilled by half from the pool's shared list, or +with fresh slots of the class that join the pool; a full one gives half +back; the bytes are counted then. Otherwise every entry is off (both +counts 0), the slow paths keep the counts apart, and a pooled slot's +shadow says released. A `realloc` of a typed block keeps its type +(`__weavec_rt_realloc_typed`). Pools hold at most the larger of the quarantine's budget and the live heap; past that a pooled slot goes to the quarantine, the only way it changes type. The sweep reads a pooled slot's -bytes as roots and does not flush the threads' pool caches. Each thread caches, per small +bytes as roots and does not flush the threads' entries. Each thread caches, per small class (up to 32 KiB), up to 32 free slots it takes from the free lists at once and up to 32 released ones it gives the quarantine at once (RFC 0037 §2.1): a release claims the slot's word by compare-and-swap, so a racing diff --git a/docs/pages/guides/safe-c.md b/docs/pages/guides/safe-c.md index a820367e..d2303db5 100644 --- a/docs/pages/guides/safe-c.md +++ b/docs/pages/guides/safe-c.md @@ -298,13 +298,13 @@ A local that takes a block out of a place says so: `struct item *WEAVEC_OWNED it ### References -A pointer field that is neither `WEAVEC_OWNED` nor `WEAVEC_BORROWED` is a reference. Storing one is always allowed (`c->last = it` leaves `it` the owner). When a safe function loads a reference and uses it for more than a comparison or a copy into memory, one shadow byte of the address it holds is tested: if it points into a released heap block, the program stops, in every check mode (report mode reports it once and goes on): +A pointer field that is neither `WEAVEC_OWNED` nor `WEAVEC_BORROWED` is a reference. Storing one is always allowed (`c->last = it` leaves `it` the owner). In the checked build (`-fweavec-safe-checks=full`, the default without optimization), when a safe function loads a reference and uses it for more than a comparison or a copy into memory, one shadow byte of the address it holds is tested: if it points into a released heap block, the program stops (report mode reports it once and goes on): ```text weavec: heap-use-after-free at cache.c:12:41: reference to 0xba80000000 loaded from a field ``` -followed by the stack and the released block's paragraph. The test is exact for the heap, since the runtime does not reuse a block while something refers to it. A null pointer, a pointer one past a live block and a pointer into memory the runtime does not track pass. A program that keeps a dangling reference and passes it on (to a function that only uses it as a key) stops too: clear the reference when its block is released, or keep the key as an integer. The ledger records the test with the reason `reference`, and the audit lists the reference fields. +followed by the stack and the released block's paragraph. The test is exact for the heap, since the runtime does not reuse a block while something refers to it. A null pointer, a pointer one past a live block and a pointer into memory the runtime does not track pass. A program that keeps a dangling reference and passes it on (to a function that only uses it as a key) stops too: clear the reference when its block is released, or keep the key as an integer. The ledger records the test with the reason `reference`, and the audit lists the reference fields. The fast build tests no reference, as Safe C++'s does not ([RFC 0055](/rfcs/0055-a-heap-for-safe-code/) §2): a released block of a known type is the next block of that type at once, so a dangling reference reads the zeroed object, or the next object of its type, and never another type. Test with the checked build. ## The rules @@ -345,10 +345,10 @@ A rule never tries to prove something about the code: it rejects a construct. Wh - A string is read at its current byte and stepped forward where a test showed the byte was not zero (the loop's own test in `while (*s) s++;`): no check at all. In a fast unit (RFC 0048) a step no test covers (`p += 4` after `strncmp(p, "