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
32 changes: 27 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,28 @@ callee's body, and every access is compared with the answer
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:
(`index-single`, `call-args`). Since RFC 0057 a safe function reaches code
WeaveC did not compile (*foreign* code: the C library, a system library, a
unit built with `-fno-weavec`) only through a *contract*: a row of the
library table, which has one for every function and global of the covered
headers through which a pointer crosses (`scripts/check-table.py` holds it
to the platform's headers), or a declaration the program marks
`WEAVEC_FOREIGN` (with `*p` counts and `WEAVEC_FOREIGN_FIELD` for a foreign
struct's members), which the checker lowers to a row
(`lib/Frontend/SafeForeign.cpp`) so that the guards, the comparisons and
the thunks read it as they read the table; each safe unit records what it
calls with no contract, and the driver refuses a link in which no input
WeaveC compiled defines one (`foreign-call`, with the contract proposed);
the checked build watches the bytes after what a contract lets foreign code
write (`__weavec_rt_foreign_before`, `__weavec_rt_foreign_after`,
`foreign-overflow`); and the ledger and the audit list every contract and
say whether the program is `closed`. Full picture:
`docs/architecture.md`. Design and the reasoning behind it: `docs/rfcs/`.
Read `0056-bounds-without-declarations.md` first (found bounds: the
Read `0057-foreign-code-by-contract.md` first (the three kinds of code,
what crosses, contracts and their rows, the checks at the call, the link's
check and the image mark, the monitor, the lists; its *Implementation
amendments* and *Measured gates*), then
`0056-bounds-without-declarations.md` (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
Expand Down Expand Up @@ -286,12 +305,14 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
`-Wformat-security` and `-Wunsequenced` (and in C
`-Wdeprecated-non-prototype`) are errors under Clang's own names
(`test/Safe/defined.cpp`, `defined.c`), and a link's
`definition-mismatch` ends in the rule's tag. A new run-time report
`definition-mismatch` and `foreign-call` (RFC 0057,
`test/Driver/foreign-link.c`) end in the rule's tag. A new run-time report
kind gets a `test/Guards/` test that pins its exact report line, and an
entry in the command-line reference's list of kinds
(`docs/pages/reference/cli.md`); the case runner's list of kinds
(`scripts/run-cases.py`) must know it too.
3. The annotations are `WEAVEC_UNSAFE` and Safe C's (RFC 0041). Each
3. The annotations are `WEAVEC_UNSAFE`, Safe C's (RFC 0041) and the
contracts' `WEAVEC_FOREIGN` and `WEAVEC_FOREIGN_FIELD` (RFC 0057). Each
spelling lives in exactly two places that must agree:
`resources/include/weavec.h`, and `UnsafeAnnotation` in
`include/weavec/Frontend/UnsafeRegions.h` (`annotate("weavec.unsafe")`)
Expand Down Expand Up @@ -376,6 +397,7 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
| 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 foreign code by contract (what crosses, `WEAVEC_FOREIGN` declarations and their rows, `*p` counts, `WEAVEC_FOREIGN_FIELD`, foreign globals, callbacks, the link's `foreign-call`, the image mark, the monitor, the table's completeness, `-fweavec-contracts`; RFC 0057) | `lib/Frontend/SafeForeign.cpp` (`crosses`, `contractsIn`, `lowerContracts`, `contractDeclaration`, `adoptContract`, `proposeContract` (evidence E10), the `...` case of `foreign-call`, `givesEnded` for a struct with a member an end bounds, `keptLocals`); `markIn`, `parameterNamed`, the `*p` checks and `storesBounded` (a member's pair stored by an initializer in a checked C++ unit) in `SafeCheck.cpp`; `lent` in `SafeTypes.cpp` and `lentByContract` in `GuardExpand.cpp` (a local lent to a contract call keeps its plain frame); `keeps` in `SafeCxxFlow.cpp` (a table row's `kept`); the unit rows in `lib/Core/LibrarySpec.cpp` (`*aN`, `aE-aS` for a bound that is an end, `handle`, `kept`, `ptr:str`, `ptr:bytes`, `ptr:of`, `global` rows) and the rows of `lib/Core/LibrarySpec.txt`; the comparisons, guards, thunks and the monitor's calls in `GuardLibrary.cpp` and `GuardSafe.cpp`; the records' `X`, `K`, `N`, `U` entries, the image mark and `checkForeignUses` in `lib/Frontend/Definitions.cpp`, `-fweavec-contracts` and the link summary in `Driver.cpp`; the ledger's `foreign`, `expects`, `uncrossed`, `unchecked` and the audit's `closed` in `EnforcementLedger.cpp`; the monitor's calls (`monitor` in `GuardLibrary.cpp`, made only at a redzone by `gateWatches` in `GuardExpand.cpp`), `__weavec_rt_foreign_before`, `__weavec_rt_foreign_after` in `runtime/weavec_guard.c` (`weavecRtHeapSlotEnd` in `weavec_alloc.c`), `weavecRtFailForeign` and the contract line in `weavec_report.c`; `scripts/check-table.py` (the hygiene gate's `table-row`); tests `test/Safe/foreign-*.c`, `cxx-foreign-call.cpp`, `test/Guards/foreign-*.c`, `foreign-member.cpp`, `ir-foreign.c`, `test/Driver/foreign-link.c`, `foreign-audit.c`, `contracts-flag.c`, cases `test/cases/foreign/`, `rt_test` `foreign-watch*`, `unittests/Core/LibrarySpecTest.cpp` (`ContractGrammar`, the contract rows); the rule's page `foreign-call`, the guide `docs/pages/guides/foreign-code.md`, `docs/pages/reference/guarantees.md` (F1)–(F4) |
| Add an annotation | Don't, without an RFC: `WEAVEC_UNSAFE`, Safe C's and RFC 0057's `WEAVEC_FOREIGN` 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` |
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ Other presets are `dev-asan`, `dev-tidy`, `release` and `relwithdebinfo`; see [`
| Guarantees and scope | [docs/pages/reference/guarantees.md](docs/pages/reference/guarantees.md) |
| Safe C | [docs/pages/guides/safe-c.md](docs/pages/guides/safe-c.md), [its rules](resources/safe/rules.md) |
| Safe C++ | [docs/pages/guides/safe-cxx.md](docs/pages/guides/safe-cxx.md), [the same rules page](resources/safe/rules.md) |
| Calling code WeaveC did not compile | [docs/pages/guides/foreign-code.md](docs/pages/guides/foreign-code.md) |
| Unsafe regions | [docs/pages/guides/unsafe.md](docs/pages/guides/unsafe.md) |
| Architecture | [docs/architecture.md](docs/architecture.md) |
| Design decisions (RFCs) | [docs/rfcs/](docs/rfcs/) |
Expand Down
26 changes: 24 additions & 2 deletions docs/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ in C++, [RFC 0044](rfcs/0044-safe-cxx.md) and
[its guide](pages/guides/safe-cxx.md)).
`weavec-cc` puts the header on the system include path
(`#include <weavec.h>`) and defines `__WEAVEC__` to `1` in every unit it
compiles with WeaveC enabled. The header is at version 3.5
compiles with WeaveC enabled. The header is at version 3.6
(`WEAVEC_H_VERSION_MAJOR`, `WEAVEC_H_VERSION_MINOR`): 3.0 added
`WEAVEC_OWNED` and made `WEAVEC_SAFE` name the one safe tier
([RFC 0043](rfcs/0043-ownership.md)), below; 3.1 adds no macro and gives
Expand All @@ -18,7 +18,10 @@ meaning in C++ ([RFC 0047](rfcs/0047-conversion-in-place.md)); 3.4 adds no
macro and lets `WEAVEC_ESCAPES` follow a reference's `&` and a member
function's parameter list ([RFC 0048](rfcs/0048-safe-code-without-the-shadow.md));
3.5 adds `WEAVEC_TAGGED_BY` and `WEAVEC_WHEN`, a union's tag
([RFC 0054](rfcs/0054-one-of-several-types.md)).
([RFC 0054](rfcs/0054-one-of-several-types.md)); 3.6 adds `WEAVEC_FOREIGN`
and `WEAVEC_FOREIGN_FIELD`, the contracts of code WeaveC did not compile,
and `*p` as a count in one
([RFC 0057](rfcs/0057-foreign-code-by-contract.md), _Contracts_ below).

`WEAVEC_UNSAFE` expands to `__attribute__((annotate("weavec.unsafe")))`
under compilers that understand Clang's `annotate` attribute and to nothing
Expand Down Expand Up @@ -87,6 +90,25 @@ 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, …)`. |

### Contracts

A safe function reaches code WeaveC did not compile only through a
contract ([RFC 0057](rfcs/0057-foreign-code-by-contract.md),
[the guide](pages/guides/foreign-code.md), the rule `foreign-call`). The C
library's are the rows of the library table. A program declares the rest:

| Form | Placement | Meaning |
| ------------------------------------------------------------------------- | ------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `WEAVEC_FOREIGN int f(char *WEAVEC_COUNTED_BY(n) p, size_t n);` | before a function declaration with no body | The declaration is the contract of a function no code WeaveC compiled defines: what it does with what it is given, said with the annotations of this page. Through a pointer it uses at most what the bound gives (one object with no bound; nothing through a pointer to an incomplete type or `void`), it keeps only a `WEAVEC_ESCAPES` parameter, releases only a `WEAVEC_OWNED` one, and its result has the bound and the owner its declaration states and lives as long as a `WEAVEC_LIFETIMEBOUND` parameter. Safe code may then call it, and every call, from safe and from guarded code, is checked against it. The language checks the declaration against the library's prototype, before or after it. In a unit that defines the function the mark means nothing. `bad-annotation` on a definition, on a member function, and for a function the library table has a row for. Expands to `annotate("weavec.foreign")`. |
| `WEAVEC_FOREIGN extern char *WEAVEC_NULL_TERMINATED name;` | before an `extern` variable declaration | The same for a global foreign code defines: its type's annotations are believed. |
| `WEAVEC_COUNTED_BY(*p)`, `WEAVEC_SIZED_BY(*p)` and their `_OR_NULL` forms | on a parameter of a contract | The count is the integer the parameter `p` points to when the call is made (a capacity passed by reference, which the function overwrites with a length). A null `p` is a count of zero. Only in a contract, and not on a result. |
| `WEAVEC_FOREIGN_FIELD(type, member, annotations);` | at file scope | The pointer member `member` of the struct or union `type`, which a header the program does not own defines, has `annotations` (a bound that names another member, `WEAVEC_OWNED`, `WEAVEC_BORROWED`) as if its declaration carried them: it is a member of typed memory. `bad-annotation` for a member that is not a pointer, one with an annotation of its own, a second that differs, and a contract that gives foreign code a struct with a member `WEAVEC_ENDED_BY` bounds (typed memory keeps a count beside a member's pointer, and no end). Expands to two `typedef`s that are never used (the member's type under a name, then that name with the annotations and `annotate("weavec.foreign_field", "<type>", "<member>")`). |

Under a compiler that is not WeaveC, `WEAVEC_FOREIGN` expands to nothing
(the declaration is a plain redeclaration) and `WEAVEC_FOREIGN_FIELD` to an
unused `typedef`. `-fweavec-contracts=<header>` includes a header of
contracts in every unit of a build.

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)):
Expand Down
Loading
Loading