Instrument the Rust standard library with safety contracts (original) (raw)

Summary

Finish the implementation of the contract attributes proposed in the compiler MCP-759, and port safety contracts from the verify-rust-std fork to the Rust standard library.

Motivation

Safety contracts serve as formal specifications that define the preconditions, postconditions, and invariants, that must be maintained for functions and data structures to operate correctly and safely. Currently, the Rust standard library already contains safety pre- and postconditions specified in the unsafe functions' documentation.

Contract attributes will enable developers to define safety requirements and behavioral specifications through programmatic contracts, which can be automatically converted into runtime checks when needed. These contracts can also express conditions that are verifiable through static analysis tools, and also provide foundation for formal verification of the standard library implementation, and other Rust code.

The status quo

Safety conditions are already well documented, and the Rust standard library is also instrumented usingcheck_library_ub and check_language_ub in many different places for conditions that are checkable at runtime.

The compiler team has also accepted Felix Klock's proposal MCP-759 to add experimental contracts attributes, and the initial implementation is currently under review.

Finally, we have annotated and verified around 200 functions in the verify-rust-std fork with safety contracts using contract attributes similar to the ones proposed in MCP-759.

The next 6 months

First, we will keep working with the compiler team to finish the implementation of contract attributes. We'll add support to #[contracts::requires] and #[contracts::ensures] attributes as described in MCP-759, as well type invariant specification.

This will allow users to convert contracts into runtime checks, as well as, provide compiler interface for external tools, such as verification tools, to retrieve the annotated contracts.

Once that has been merged to the compiler, we will work with the library to annotate functions of the standard library with their safety contracts.

The "shiny future" we are working towards

All unsafe functions in Rust should have their safety conditions specified using contracts, and verified that those conditions are enough to guarantee absence of undefined behavior.

Rust users should be able to check that their code do not violate the safety contracts of unsafe functions, which would rule out the possibility that their applications could have a safety bug.

Design axioms

Ownership and team asks

Owner: Celina G. Val and Michael Tautschnig

Task Owner(s) or team(s) Notes
Discussion and moral support Team libs

Experimental Contract attributes

Standard Library Contracts

Definitions

Definitions for terms used above:

Frequently asked questions