Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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` |
Expand Down
25 changes: 19 additions & 6 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/pages/guides/safe-c.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, "<!--", 4)`) tests the bytes it steps over where it is made, a load of each up to the terminator, and is then a string position too: it is handed on and returned with no search, and a read-only library call on strings the function holds (`strncmp`, `strlen`) calls the C library itself. A string that code which is not safe hands in is taken in constant time when it lies in a live heap block.
- Calls between safe functions, in one file or in another file that declares the callee `WEAVEC_SAFE`, go to the callee's body with no check of the shadow. Code that is not safe calls the function's entry, which checks each pointer argument against the shadow once, a bounded one over its bounds; an escaping or owned argument must not point into a stack (`stack-escape`, reported at the call). In a fast unit a local placed on the heap takes a block of its type's pool (RFC 0048 §5.1).
- Library calls whose accesses are given by sizes (`memcpy`, `memset`, `fread`, …) compare the sizes with their arguments' bounds. A pointer to one object given to one with a size that may be more than the object is `call-args` when the function compiles (RFC 0050 §5.1), not a stop in a test: declare it with its count.
- A reference loaded from a field and used is tested not to point into a released block (`heap-use-after-free`).
- In the checked build, a reference loaded from a field and used is tested not to point into a released block (`heap-use-after-free`).
- An access through a pointer whose bound is unknown (loaded from a place no declaration bounds, such as `*pp` or an array of pointers outside a struct, or through `void *`) keeps its guard.

- In the fast mode (`-fweavec-safe` with optimization, or `-fweavec-safe-checks=bounds`; without optimization the unit is checked, RFC 0048) every allocation whose type the compiler can tell (`malloc(sizeof *p)`, `calloc(n, sizeof *p)`) carries it, and a block a safe function releases is zeroed and handed to the next allocation of the same type and size, at once, instead of waiting in the quarantine for a sweep. A stale pointer to it then reads an empty object, or the object of its type that lives there; it never reads another type. Releases by code that is not safe, and blocks of no known type, still wait for a sweep.
- In the fast mode (`-fweavec-safe` with optimization, or `-fweavec-safe-checks=bounds`; without optimization the unit is checked, RFC 0048) every allocation whose type the compiler can tell (`malloc(sizeof *p)`, `calloc(n, sizeof *p)`) carries it, and a block a safe function releases is zeroed and handed to the next allocation of the same type and size, at once, instead of waiting in the quarantine for a sweep. A stale pointer to it then reads an empty object, or the object of its type that lives there; it never reads another type. A `realloc` keeps its block's type: the block it gives comes from the pool of the old block's type, whatever type the call's site infers ([RFC 0055](/rfcs/0055-a-heap-for-safe-code/) §3). Releases by code that is not safe, and blocks of no known type, still wait for a sweep. Each thread keeps the free blocks of each type it uses, so an allocation and its release take a few loads and stores and no lock.

The ownership rules are checked only when the program compiles. In the checked mode (`-fweavec-safe-checks=full`) nothing at run time rests on them: every released block waits in the quarantine for a sweep, and no guard is removed because of them.

Expand Down
Loading
Loading