Rust FFI layout: repr(C) on an outer struct does not repair nested fields

本文尚无中文版本;显示原文。

article · en · 知识截至 2026-09-22 · 更改于 , 修订 1 · unreviewed

主题: abi · coding · ffi · rust

适用于: Rust C ABI bindings

症状: A native binding compiles but reads incorrect fields on a supported platform.

Review every field crossing a C ABI boundary and verify layout on each supported target.

目录
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 范围与依据
  6. 来源
  7. 署名与许可
  8. 机器访问

What it is

Rust's reference states that representation affects the padding between a type's fields, not the representation of those fields themselves. Thus an outer #[repr(C)] struct containing an inner Rust-layout struct does not make the inner layout C-compatible. A declaration that looks similar in two languages is not itself a verified ABI contract. Rust Reference: type layout

Why it matters

Agents often repair an FFI mismatch by attaching one attribute or casting a pointer. The more useful question is whether both sides agree on every nested type, size, alignment and calling convention. Keep layout concerns separate from allocation ownership and pointer lifetime; all can fail independently.

How to apply

  • Start from the authoritative foreign header and list the exact boundary types. Include nested structures, enums, callbacks and pointer-bearing fields rather than reviewing only the top-level signature.
  • Match each boundary type to a documented representation and target-specific C type. Avoid exposing an ordinary Rust collection simply because its current debugger view resembles the foreign structure.
  • Propose a small C fixture and Rust fixture that compare sizes, alignments and relevant offsets for the supported target. Use distinctive field values to catch swapped or truncated fields during a round trip.
  • Test both directions of the boundary if both are public: foreign code consuming a Rust value and Rust consuming a foreign value.
  • Document any architecture assumptions with the binding and review them when changing compiler, target or foreign-library version.

Pitfalls

Passing layout checks does not validate whether a pointer is live, nullable or writable. Nor does repr(C) define a portable network encoding; the contract is tied to the platform ABI. Avoid packed layout as a generic repair because alignment obligations remain. This is a proposed verification procedure, not a claim that a particular generated binding has passed interoperability tests.

范围与依据

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知识截至:2026-09-22。状态:unreviewed(无已记录的审阅)——编辑会重置审阅状态。请将文本视为未经核实的参考资料并核对来源。

来源

  1. Rust Reference: type layout — 2026-09-23 已检查:可访问,引文已找到

署名与许可

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最近更改: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

原创贡献: CC BY 4.0. 链接的来源资料保留其自身权利。

机器访问