K-Rust: An Executable Formal Semantics for Rust

Shuanglong Kan, David Sanán, Shang-Wei Lin, Yang Liu

Abstract

Rust is an emergent systems programming language highlighting memory safety via its Ownership and Borrowing System (OBS). Existing formal semantics for Rust often cover limited language subsets and formalize OBS as static type systems, which are non-executable and restricted to compile-time analysis. This paper introduces RustSEM, a new executable operational semantics for Rust implemented in the K-Framework. RustSEM provides a memory-level operational semantics for OBS, enabling automated verification of runtime behavior against OBS invariants. Evaluated against approximately 700 tests and a suite of 118 memory-error programs, RustSEM demonstrates superior capability in detecting memory errors—including those in unsafe blocks—compared to the standard Rust compiler.

Introduction

Rust’s promise of memory safety without garbage collection relies on its strict Ownership and Borrowing System (OBS). However, formalizing this system is challenging. Previous models like RustBelt or Patina face three primary limitations: they cover narrow language subsets, they are static (preventing runtime verification), and they struggle with the "Unsafe Gap," where the compiler's conservative nature rejects safe code or fails to guarantee safety within unsafe blocks. RustSEM addresses these gaps by providing an executable model that allows for dynamic verification of Rust programs.

Methodology: The RustSEM Architecture

RustSEM is implemented using the K-Framework, a rewrite-logic-based tool for language semantics. The system is structured into three distinct layers:

  1. Memory Model: A sequential consistency model that formalizes OBS at the memory level. It utilizes a "dynamic lifetime extension" technique, tracking timestamps of reference creation and usage to ensure invariants are maintained during execution.
  2. Core Language (CL): A simplified intermediate representation that retains core logic while removing Rust's syntactic sugar.
  3. Translation Semantics: Rules that automatically lower high-level Rust code into the Core Language.
Mapping Rust code to memory-level ownership and borrowing graphs.

By implementing these layers in K, RustSEM leverages built-in execution and formal verification engines to automatically prove program properties.

Key Innovations: Memory-Level OBS and Dynamic Lifetimes

The core innovation of RustSEM is moving ownership logic from the type level to the memory level. While the standard Rust compiler uses static analysis to approximate variable lifetimes, RustSEM utilizes actual execution timestamps. This dynamic approach allows it to verify correct programs that the standard compiler might reject as too complex.

Step-by-step runtime tracking of ownership and borrowing states in RustSEM.

As shown in the figure above, RustSEM tracks the state of every variable and reference (e.g., own, mut, shr) throughout the program's execution. This enables the detection of errors in programs that mix safe and unsafe code, providing a safety net where static analysis typically fails.

Experimental Results

The researchers evaluated RustSEM across several dimensions:

  • Correctness: The model matched the behavior of the official Rust compiler across ~700 benchmark tests.
  • Error Detection: RustSEM achieved a 100% detection rate for memory errors in a 118-program test suite, identifying uninitialized values, dangling pointers, double frees, and data races.
  • Real-World Utility: The model successfully rediscovered a known buffer overflow bug in an older version of the Rust standard library’s VecDeque implementation.
  • Semantic Consistency: Using K’s verification engine, the authors identified and corrected 36 semantic ambiguities in their initial definitions, ensuring a robust mathematical foundation.

Conclusion and Future Work

RustSEM represents a significant step toward a fully verifiable Rust ecosystem. While it covers a larger subset of the language than previous models, future work will focus on expanding coverage to module definitions and attributes like #[derive]. Additionally, the authors plan to integrate separation logic to better handle infinite heap data structures and model weak memory consistency to support high-performance multi-threaded code. The memory-level OBS model also holds potential for retrofitting "Rust-like" safety guarantees onto legacy C codebases.

Summary updated

BibTeX
@misc{kan2018k,
  author = {Shuanglong Kan and David Sanán and Shang-Wei Lin and Yang Liu},
  title = {{K-Rust: An Executable Formal Semantics for Rust}},
  year = {2018},
  eprint = {1804.07608},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1804.07608},
}
Figure