Skip to content

feat!: reach foreign code only through a contract - #87

Open
owenthcarey wants to merge 2 commits into
mainfrom
rfc0057-foreign-code-by-contract
Open

owenthcarey wants to merge 2 commits into
mainfrom
rfc0057-foreign-code-by-contract

Conversation

@owenthcarey

Copy link
Copy Markdown
Contributor

Implements RFC 0057 — Foreign code by contract (Accepted; its Implementation amendments and Measured gates record what was built differently and what was measured).

What changes

A safe function reaches code WeaveC did not compile (the C library, a system library, a unit built with -fno-weavec) only through a contract:

  • The library table is complete for its headers. 247 function rows, 19 global rows and 22 aliases are added; scripts/check-table.py (the hygiene gate's table-row) holds the table to the platform's headers. The grammar gains *aN, aE-aS, handle, kept, ptr:str, ptr:bytes, ptr:of and global rows.
  • WEAVEC_FOREIGN on a redeclared prototype (with *p counts) and WEAVEC_FOREIGN_FIELD for a foreign struct's members are lowered to rows (lib/Frontend/SafeForeign.cpp), so safe callers compare, guarded callers guard and address-taken functions get thunks exactly as for the table. -fweavec-contracts=<header> gives a contract header to every unit.
  • foreign-call at the link. Each safe unit records what it calls with no contract (weavec-defs 3: X, K, N, U); the driver refuses a link in which no input WeaveC compiled defines one, and prints the contract it proposes. Images WeaveC linked carry a mark.
  • The checked build's monitor watches the bytes after what a contract lets foreign code write (__weavec_rt_foreign_before, __weavec_rt_foreign_after) and reports foreign-overflow.
  • The lists. Ledger 17 (foreign, expects, uncrossed, unchecked), audit 12 (closed), the link's summary.

Breaking

  • Runtime ABI 53, weavec-ledger 17, weavec-audit 12, weavec-defs 3, weavec.h 3.6.
  • A safe program that calls a foreign function through which a pointer crosses, with no contract, no longer links ([weavec-safe:foreign-call]).
  • In safe C a pointer passed through ... to a function with no row is foreign-call when the unit compiles; calls that pass no pointer are no longer variadic.
  • Table rows that keep a pointer say so (setvbuf, setbuf, setbuffer, putenv, tsearch, initstate, setstate, openlog, aio_read, aio_write): a borrow or a local given to them is escape in safe code.
  • A contract that gives foreign code a struct with a WEAVEC_ENDED_BY member is bad-annotation.

Gates (RFC §14; details in Measured gates)

Gate Result
S0 Every limit met.
G1 Tests Pass on darwin-arm64 and linux-aarch64 (container, ci-release).
G2 Probes Pass.
G3 Nothing breaks Pass: the fifteen surveyed projects, RFC 0056's G3 projects, and the corpus quick tier (main's, but for two more guarded accesses in lua; main's own compiler shows the same zlib ratchet note on the measuring machine).
G4 New code Fails on time: all-safe and closed in 7 min (Safe C) and 6.5 min (safe C++) against Rust's 4.5 (1.56 and 1.44; limit 1.25). Injected bugs: 10 of 11 caught in Safe C, 6 of 10 in safe C++.
G5 Unmodified code Pass: libpng with a zlib contract, 33 of 33 tests, 1.0004 of the time.
G6 Wrong contracts Pass: 9 of 9 reported by the checked build.
G7 A round Fails (the gate asks for no hole): two found, both closed here (amendments 4 and 22).
G8 Nothing slower Pass.
G9 Lines Library +2,615 code lines, runtime +209, the table's file 1,388 → 1,854 lines.

The RFC stays Accepted, not Implemented, because G4's time and G7 fail.

Found on main, not changed here

  • Safe C++ rejects static_cast<const unsigned char *> of a const void * (pointer-cast) and accepts the non-const form.
  • int-to-pointer does not see a cast a system header's macro makes (SQLITE_TRANSIENT) in safe C++.
  • RFC 0049's libzmq conversion no longer compiles as safe on main, and main reports definition-mismatch for six of its types whose descriptions print alike.

Implements RFC 0057 (docs/rfcs/0057-foreign-code-by-contract.md).

A safe function reaches code WeaveC did not compile (the C library, a
system library, a unit built with -fno-weavec) only through a contract: a
row of the library table, which now has one for every function and global
of the covered headers through which a pointer crosses
(scripts/check-table.py), 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). Each safe unit records what it calls with
no contract, and the driver refuses a link in which no input WeaveC
compiled defines one ([weavec-safe:foreign-call], with the contract
proposed). The checked build watches the bytes after what a contract lets
foreign code write (foreign-overflow). The ledger and the audit list
every contract and say whether the program is closed.

BREAKING CHANGE: runtime ABI 53, weavec-ledger 17, weavec-audit 12,
weavec-defs 3, weavec.h 3.6. A safe program that calls a foreign function
through which a pointer crosses, with no contract, no longer links. In
safe C a pointer passed through `...` to a function with no row is
foreign-call. Table rows that keep a pointer say `kept`, and a contract
that gives foreign code a struct with a WEAVEC_ENDED_BY member is
bad-annotation.

The RFC stays Accepted: its gates G4 (time) and G7 fail; see its Measured
gates.
A zero-byte watch at the end of an object now finds the slot `end` lies
in, so `__weavec_rt_foreign_before` no longer writes its pattern into the
next slot. `__weavec_rt_foreign_after` compares and reports the bytes
only while they are still the redzone `before` watched, and puts them
back only then or while they still hold the pattern: a callback that
grows its block in its slot with `realloc`, or releases it, during the
call keeps its bytes and gets no false foreign-overflow (rt_test
`foreign-watch-changes`).

The link's records keep the bytes of an LTO object named on the command
line for as long as the module read lazily from them.

Refs RFC 0057 (docs/rfcs/0057-foreign-code-by-contract.md).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant