arXiv:2604.02399cs.SEcs.AI2026-04

用彩色有向自动机自动生成符合内存安全的Rust代码

A Synthesis Method of Safe Rust Code Based on Pushdown Colored Petri Nets

  • 基于新型栈式彩色佩特里网建模所有权、借用和生命周期约束
  • 通过状态标记与栈操作确保生成代码满足编译时安全检查
  • 适合开发自动化代码生成工具的开发者参考

Rust通过严格的编译时约束保证内存安全:所有权可转移,借用可临时保证共享只读或独占写访问,且所有权与借用受生命周期作用域限制。自动生成正确且安全的Rust代码极具挑战性,因为生成的代码不仅需满足所有权、借用和生命周期约束,还需在编译时符合类型与接口要求。本文提出一种基于新定义的栈式彩色佩特里网(PCPN)的合成方法,直接从公共API签名建模这些编译约束,以合成有效调用序列。令牌颜色编码动态资源状态,并结合作用域层级表示借用有效的生命周期区域;栈结构跟踪生命周期参数的进入或离开,通过压栈和出栈实现。仅当类型匹配、接口义务均成立且所需资源状态可用时,转换才被激活。基于双仿真理论,证明了PCPN的触发与执行规则与这三个约束的编译时检查一致。我们开发了基于PCPN的自动合成工具,实验结果表明合成代码全部正确。

原文摘要 · Abstract (English)

Safe Rust guarantees memory safety through strict compile-time constraints: ownership can be transferred, borrowing can temporarily guarantee either shared read-only or exclusive write access, and ownership and borrowing are scoped by lifetime. Automatically synthesizing correct and safe Rust code is challenging, as the generated code must not only satisfy ownership, borrowing, and lifetime constraints, but also meet type and interface requirements at compile time. This work proposes a synthesis method based on our newly defined Pushdown Colored Petri Net (PCPN) that models these compilation constraints directly from public API signatures to synthesize valid call sequences. Token colors encode dynamic resource states together with a scope level indicating the lifetime region in which a borrow is valid. The pushdown stack tracks the entering or leaving of lifetime parameter via pushing and popping tokens. A transition is enabled only when type matching and interface obligations both hold and the required resource states are available. Based on the bisimulation theory, we prove that the enabling and firing rules of PCPN are consistent with the compile-time check of these three constraints. We develop an automatic synthesis tool based on PCPN and the experimental results show that the synthesized codes are all correct.

代码生成Rust形式化验证

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。