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
24 changes: 21 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,25 @@ target type is there (`bad-cast`); the runtime keeps the type of every
heap block, registered global and recorded local, from layouts the checker
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:
answers). Since RFC 0056 a safe function indexes a pointer with no
declared bound: where such a pointer arrives (a parameter, a load, a
call's result, a global) and is indexed or stepped, the checker marks it
(`__weavec.arrival`, `Arrivals` in `SafeTypes.cpp`), the pass asks the
runtime for its bound once (`__weavec_rt_bound`, the run of objects of its
type the records give: a heap block, a registered global's extent, a
recorded local; memory with no record is one object), a safe caller that
holds the bound passes it in the hidden last parameter `held` of the
callee's body, and every access is compared with the answer
(`pointer-out-of-bounds`, its report saying where the bound came from);
the checker lists each such declaration (`found`, ledger 16, audit 11) with
the bound the planner proposes, `weavec-cc --weavec-fix-bounds` writes
them, and `-fweavec-safe-bounds=declared` keeps the language before it
(`index-single`, `call-args`). Full picture:
`docs/architecture.md`. Design and the reasoning behind it: `docs/rfcs/`.
Read `0055-a-heap-for-safe-code.md` first (the entries and the short
Read `0056-bounds-without-declarations.md` first (found bounds: the
arrivals and their questions, the records' answers, `held`, the list and
`--weavec-fix-bounds`; its *Implementation amendments* and *Measured
gates*), then `0055-a-heap-for-safe-code.md` (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
Expand Down Expand Up @@ -264,7 +280,8 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
2. WeaveC's only compile-time diagnostics are Safe C's and Safe C++'s
errors (RFC 0041, RFC 0044), each ending in `[weavec-safe:<rule>]` with
a note, and each pinned by a `test/Safe/` test (`cxx-*.cpp` for C++);
RFC 0051's one exception: in a unit compiled with `-fweavec-safe`,
under RFC 0056 a found bound is a remark (`-Rpass=weavec-safe`), not a
diagnostic; RFC 0051's one exception: in a unit compiled with `-fweavec-safe`,
Clang's `-Wreturn-type`, `-Winvalid-noreturn`, `-Wformat`,
`-Wformat-security` and `-Wunsequenced` (and in C
`-Wdeprecated-non-prototype`) are errors under Clang's own names
Expand Down Expand Up @@ -358,6 +375,7 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
| Change what a callable or a container holds (callables by their loans, the narrowing, binders and pointers to member functions, the library's containers of views, out parameters, polymorphic objects, globals, `mutable`, `= default`, results a type does not show; RFC 0053) | `anyCallable`, `reachOf`, `lambdaReach`, `changedBy`, `deepen`, `holdsCallable`, `closures`, `keptCallable`, `heldIn`, `outStored`, `onlyPolymorphic`, `known`, `shows`, `changesGlobals`, `referencesIn`, the `Global` roots and the `escape` cases of `returned` in `lib/Frontend/SafeCxxFlow.cpp`; `libraryOfViews`, `readsOnly`, `polymorphicHolder`, `mutableOwners`, `runsProgram`, `ownsBlocks`, `changesOnly` in `SafeCxxKinds.h`; tests `test/Safe/cxx-loans-*.cpp` |
| Change what a call may reach (reach by types, results, views' shallow `const`, lambdas' changes, `call-alias` for callables, kept callables, values built from borrows; RFC 0052) | `reaches`, `writesThrough`, `pointsAt`, `borrowing`, `mayBorrow`, `keptCapture` in `lib/Frontend/SafeCxxKinds.h`; `result`, `fit`, `lambdaChanges`, `lambdaObjects`, `closures`, `keptCallable`, `keptLambdas`, the `Reached` and `Runs` ends in `lib/Frontend/SafeCxxFlow.cpp`; `keptDefault` in `SafeCxx.cpp`; E8 in `lifetime` (`SafeFix.cpp`); tests `test/Safe/cxx-reach-*.cpp` |
| Change one of several types (tagged unions, checked conversions and downcasts, the type records, `bad-cast`, `union-member`; RFC 0054) | `lib/Frontend/SafeTypes.cpp` (`taggedUnion`, `checkedConversion`, `TypeLayouts`, `TypeMarks`: the AST marks undone while the rules run, the rules `UnionRules`, E9 `tagWant`), the gates in `SafeCheck.cpp` (`checkPointerCast`, `byteViewAllowed`, `checkGlobalInits`), `SafeCxx.cpp`, `SafeUndefined.cpp`, `clearTagged` in `SafeOwnership.cpp`; `lib/Frontend/GuardTypes.cpp` (the markers' expansion, `layoutGlobal`, `typeRecord`, `recordLocals`, `noteAllocations`, `dropObjects`), the checked object as a root in `GuardSafe.cpp` and `GuardFast.cpp`; `runtime/weavec_types.c`, the slot types in `weavec_alloc.c`; tests `test/Safe/tagged-union*.c`, `cxx-tagged-union-uses.cpp`, `checked-cast.c`, `cxx-checked-cast.cpp`, `fix-tagged.c`, `test/Guards/union-member.c`, `bad-cast.c*`, `ir-tagged.c`, `ir-cast.c*`, cases `test/cases/sum/`, `rt_test` `type-at-*`, `frame-records`, `union-member` |
| Change found bounds (pointers with no declared bound in safe code, their questions, the records' answers, `held`, the list, `--weavec-fix-bounds`, `-fweavec-safe-bounds`; RFC 0056) | `Arrivals` and `arrivalDeclared` in `lib/Frontend/SafeTypes.cpp`; the `found` reports and `listFound` in `SafeCheck.cpp` (`libraryAccess`, the `held` flag in `Declarations`), `lambdasOf` in `SafeCxx.cpp`; `boundTarget` and `placeOf` in `SafeFix.cpp`; `findArrivals`, `placeQuestions`, `boundLibraryCalls`, `heldParameters`, `addHeld`, `pruneHeld`, `proposalOf` in `GuardSafe.cpp`, `readArrivals` and `recordLocals` in `GuardTypes.cpp`, the extents in `GuardGlobals.cpp`; the ledger's `found` list in `EnforcementLedger.cpp`, the bounds mode of `lib/Frontend/Fixer.cpp`, `-fweavec-safe-bounds` in `Driver.cpp`; `__weavec_rt_bound` in `runtime/weavec_alloc.c`, `boundOf`, `runIn`, `weavecRtBoundSlow`, `weavecRtBoundRecord` in `weavec_types.c`, the extents and `__weavec_rt_found_fail` in `weavec_guard.c`, the report's bounds line and the statistics in `weavec_report.c`; tests `test/Safe/found-bounds.c`, `bounds-declared.c`, `cxx-found-bounds.cpp`, `test/Guards/found-bounds*.c*`, `ir-found-bounds.c`, `test/Driver/found-bounds.c`, cases `test/cases/bounds/`, `rt_test` `bound-*`; the guide's "Found bounds", `docs/pages/reference/guarantees.md` (B1)–(B3) |
| Add an annotation | Don't, without an RFC: `WEAVEC_UNSAFE` and Safe C's are the only ones (`UnsafeRegions.h`, `SafeCheck.h`, `weavec.h`, `docs/annotations.md`) |
| Change build flags / warnings | `cmake/WeaveCWarnings.cmake`, `cmake/WeaveCLLVM.cmake` |
| Add a CI job | `.github/workflows/ci.yml` |
13 changes: 13 additions & 0 deletions docs/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ region too, with no source change.
| `union { … } u WEAVEC_TAGGED_BY(tag)`, `WEAVEC_TAGGED_BY(tag, mask)` | after a union field's name, or after an anonymous union's closing brace | RFC 0054 §3: `tag`, an integer or enumeration field of the same struct (at most 64 bits, the tag of no other union), says which member is live (`tag & mask` with a mask). A safe function uses a member after a comparison of the tag the compiler adds (`union-member` when it fails), a store of a new value to the tag clears the union, and no member's address is taken. Expands to `annotate("weavec.tagged_by", "<tag>[, <mask>]")`. |
| `T m WEAVEC_WHEN(value, …)` | after a member of a tagged union | The constant values of the tag the member is for. Every member that holds a pointer has one; a member without one is not used by safe code. Expands to `annotate("weavec.when", value, …)`. |

The bounds (`WEAVEC_COUNTED_BY`, `WEAVEC_SIZED_BY`, `WEAVEC_ENDED_BY`,
their `_OR_NULL` forms and `WEAVEC_NULL_TERMINATED`) are no longer needed
for a function to be safe ([RFC 0056](rfcs/0056-bounds-without-declarations.md)):
a pointer with none that a safe function indexes has its bound found when
the function runs, the extent of the object the runtime's records place it
in. A declared bound promises the length the program means, which can be
less than the object (an index past it stops where a found bound lets it
pass); it is compared with a count, with no lookup when the function runs;
a safe caller passes it to its callees; and readers and other compilers see
it. `weavec-cc --weavec-audit` lists the declarations whose bound is found,
and `weavec-cc --weavec-fix-bounds` writes the proposed ones.
`-fweavec-safe-bounds=declared` requires them, as before.

### In C++

`weavec-c++ -fweavec-safe` makes a unit's functions
Expand Down
34 changes: 34 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,40 @@ from a field and uses against the shadow (`__weavec_rt_reference_fail`, a
fields' annotations, count reads and store pairs. Accesses whose pointer has
no known bound keep their guards (`typed-unbounded`).

A pointer a safe function indexes or steps with no declared bound has its
bound found when the function runs (RFC 0056; `-fweavec-safe-bounds=declared`
turns it off). The checker reports such a use as a *found* bound instead
of an `index-single` or `call-args` error: `listFound` keeps one entry per
declaration (a parameter, a member, a result, a global; keyed as the fix
planner keys its fixes, with the proposals of RFC 0049's evidence) for the
ledger's `found` list. Before code is made, `Arrivals`
(`lib/Frontend/SafeTypes.cpp`) wraps each place such a pointer arrives (a
parameter's first use, a load, a call's result, a global) in a
`__weavec.arrival` marker that names the element's layout and the
declaration, transparent to constant evaluation in C++ and never around a
`musttail` call. GuardSafe (`findArrivals`, `placeQuestions`) turns the
arrivals whose accesses need a bound into one `__weavec_rt_bound` call each
(the *question*, with a per-declaration record of its site and counts),
placed before the loop that uses it when the loop calls nothing, and
compares the accesses, library calls (`boundLibraryCalls`) and conversions
to declared bounds with the `{low, high}` it returns
(`__weavec_rt_found_fail`). A safe function's body whose table has a
plain pointer parameter (`held` in `FunctionBounds`) takes a hidden last
parameter, an array of the bounds its safe callers hold (zeroes when they
hold none); a caller passes what it knows (a declared count, an array, a
bound it found), the body uses an element that is not zero instead of
asking, and `pruneHeld` drops the parameter of a body no caller fills.
Local arrays passed to a function of the program are recorded in their
guarded frame (`recordLocals`), and GuardGlobals registers each global's
extent and layout (`__weavec_rt_extents_register`). The runtime answers
(`boundOf` in `runtime/weavec_types.c`) from the heap block's type, a
registered global or extent, a recorded local, or the program's arguments
and environment: the run of objects of the element's type around the
pointer (`runIn`: an array member's elements, the data after a header, a
trailing array), one object where nothing is recorded, nothing in a
released block; a heap block of the element's type takes a short path in
`__weavec_rt_bound`.

## The enforcement ledger

GuardInsert records each access it collected, before any rule removes its
Expand Down
10 changes: 6 additions & 4 deletions docs/pages/guides/convert-cxx.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A rewrite into Rust starts over: a new build, bindings at the boundary, tests po
cmake --build build-survey
```

2. **Let the compiler write its part** ([RFC 0049](/rfcs/0049-errors-that-carry-their-fix/)). Most errors of a conversion ask for one annotation on an existing declaration that the code already implies: a pointer that `memcmp` reads `size_` bytes of is `WEAVEC_COUNTED_BY(size_)`, a parameter stored in a member is kept. `--weavec-fix` writes those, runs the rules again on the units an edit reached (each unit's recorded command, with `-fsyntax-only`), repeats until a round writes nothing, and marks every file left with no error `WEAVEC_SAFE_FILE`:
2. **Let the compiler write its part** ([RFC 0049](/rfcs/0049-errors-that-carry-their-fix/)). Most errors of a conversion ask for one annotation on an existing declaration that the code already implies: a parameter stored in a member is kept, a returned end is `WEAVEC_COUNTED_BY(0)`. A pointer indexed with no declared bound is not an error: its bound is found when the function runs ([RFC 0056](/rfcs/0056-bounds-without-declarations/)), and the survey lists it. `--weavec-fix` writes those, runs the rules again on the units an edit reached (each unit's recorded command, with `-fsyntax-only`), repeats until a round writes nothing, and marks every file left with no error `WEAVEC_SAFE_FILE`:

```sh
weavec-c++ --weavec-fix -j8 ledger/
Expand Down Expand Up @@ -48,7 +48,9 @@ A rewrite into Rust starts over: a new build, bindings at the boundary, tests po

5. **Rewrite what has no fix,** the file with the fewest errors first. Compile it with the rules on (`-fweavec-safe`, or mark it and build) and fix what they say; each note says what to write. `weavec-c++ --weavec-explain <rule>` prints a rule with a rejected example and its rewrite. `WEAVEC_GUARDED` keeps one function or class guarded inside a marked file.

6. **Run the project's tests in the checked build** (`-fweavec-safe-checks=full`, the default of a build without optimization): every guard stays, so a mistake the rules do not see stops with a full report. Then repeat from 1.
6. **Declare the bounds the list proposes.** The audit's list of found bounds names each declaration a safe function indexes with no bound, the functions that need it, and the bound its uses agree on (a pointer that `memcmp` reads `size_` bytes of is `WEAVEC_COUNTED_BY(size_)`). `weavec-c++ --weavec-fix-bounds ledger/` writes the certain ones, `--weavec-fix-bounds=likely` the likely ones too; a declared bound is compared with a count, asks nothing when it runs, and stops an index past the length the program means, which a found bound (the object's extent) lets pass. Run the tests after it: a likely bound that is too small stops a program that ran.

7. **Run the project's tests in the checked build** (`-fweavec-safe-checks=full`, the default of a build without optimization): every guard stays, so a mistake the rules do not see stops with a full report. Then repeat from 1.

`WEAVEC_SAFE_FILE`, written once after `#include <weavec.h>`, makes every function the file defines after it safe, with the member functions of the classes defined there, in every unit that includes it. It does not reach the files it includes.

Expand All @@ -58,7 +60,7 @@ The annotations the fixer writes are the ones a person writes for pointers a fas

## Raw pointers keep their place

Existing C++ indexes raw pointers. Safe C++ indexes one whose declaration says how many elements it has, and compares each access with that bound when it runs. Declare the bound where the pointer is declared; the signature stays the same for callers that compile without WeaveC.
Existing C++ indexes raw pointers, and Safe C++ compiles it as written: a pointer with no declared bound is compared with the bound found for it when the function runs, the run of objects of its type that the runtime's records give (a heap block, a global, an array member, a local array the caller passed). Declaring the bound where the pointer is declared makes the comparison one with a count, which asks nothing, and says what the program means; the signature stays the same for callers that compile without WeaveC. Under `-fweavec-safe-bounds=declared` a pointer with no bound is one object, as before.

| The code has | Declare |
| ---------------------- | ------------------------------------------------------------------- |
Expand All @@ -68,7 +70,7 @@ Existing C++ indexes raw pointers. Safe C++ indexes one whose declaration says h
| a member and its count | `char *WEAVEC_COUNTED_BY(size_) data_;` beside `std::size_t size_;` |
| an accessor | `const char *WEAVEC_COUNTED_BY(size_) data() const` |

Arrays keep their length as pointers, a container's `data()` has `size()` elements, and the library's pointer-and-count functions (`std::string(p, n)`, `append`, `std::string_view(p, n)`, `std::span(p, n)`) ask that their pointer have a bound. A count changes beside its pointer (`counted-pair`), and neither is passed by a reference that is not `const` (`field-address`).
Arrays keep their length as pointers, a container's `data()` has `size()` elements, and the library's pointer-and-count functions (`std::string(p, n)`, `append`, `std::string_view(p, n)`, `std::span(p, n)`) compare their count with the pointer's bound, declared or found. A count changes beside its pointer (`counted-pair`), and neither is passed by a reference that is not `const` (`field-address`).

```cpp accepted
class Slice {
Expand Down
Loading
Loading