Component output is not byte-reproducible for identical inputs

未關閉
#247 12 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
需要釐清
活躍度
活躍
技術堆疊
python, rust, wasm

研究方向

使用 sandbox.wit componentize-py 命令重現問題,並比較兩份完整輸出。接著,圍繞巢狀 init 呼叫所產生的 canonical-ABI post-return 路徑建立最小化重現,利用已注意到的 section 和 memory 差異來定位原因。確認根本原因,且 regression test 與 opt-in 可重現模式的範圍獲得 maintainer 核准,即視為完成。

由索引模型根據 Issue 內容生成。

描述

Summary

Running componentize-py componentize twice with the same command and byte-identical
inputs can produce valid but byte-different WebAssembly components. This prevents use in
content-addressed build and artifact-verification pipelines.

I would like maintainer guidance on the shape of an opt-in reproducible-build mode before
opening the larger implementation PR. A small independent PR that makes WASI stub-adapter
emission deterministic is ready separately.

Reproduction

Using current main at aa3d6d1, Rust 1.95.0, WASI SDK 34, and the repository sandbox
example:

for output in first.wasm second.wasm; do
  componentize-py \
    -d sandbox.wit \
    componentize \
    --stub-wasi \
    guest \
    -o "$output"
done

shasum -a 256 first.wasm second.wasm
cmp first.wasm second.wasm

Expected: both files are byte-identical.

Actual: the files differ while remaining valid components.

Investigation

An experimental --reproducible implementation fixed or controlled all of the following:

  • build-time secure and insecure randomness;
  • build-time wall and monotonic clocks;
  • PYTHONHASHSEED;
  • CPython allocator initialization with PYTHONMALLOC=malloc_debug;
  • bytecode cache writes;
  • WASI stub-adapter map iteration order;
  • metadata on generated helper, standard-library, bindings, and input trees.

Even with all of those controls, two current-main outputs still differed: one run produced
19,323,845 bytes, the other 19,325,725 bytes, with the first difference at component offset
9.

I then repeated the experiment with ambient user/system site-packages excluded and the
same input, generated bindings, bundled helpers, and standard library exposed through stable
host directories. The outputs still differed (292a9320... versus f35b0e6d...). Both had
the same 208-page memory and 1,710 data segments, but reconstructing linear memory from those
segments found roughly 4.1 million differing bytes across 141 pages.

The difference is localized more narrowly than an encoder or linker problem:

  • both outputs contain 205 top-level component sections;
  • 204 sections are byte-identical;
  • only the first embedded core module differs;
  • within that module, all type, import, function, table, global, export, and code sections are
    byte-identical;
  • only the memory snapshot/data section differs.

Additional two-run probes showed identical values for Python string hashes, random, wall and
monotonic clocks, process/thread identity, visible directory ordering, generated Symbols, and
the component immediately before pre-initialization. Full linear-memory fingerprints taken
inside the guest were identical after app import and after every do_init phase (exports, type
tables, constructors, environment, runtime hooks, argv, and full-generation GC).

However, immediately after call_init returned through the Component Model boundary, before
component-init-transform measured globals or memory, the linear-memory fingerprints differed.
Disabling either or both WASI adapter/libc reset calls did not change that result. Ordering the
internal component-init-transform maps, reusing stable-inode directories, collecting Python
free lists after return through a second no-argument guest call, and retaining the Rust-level
app_name/Symbols values also did not fix it.

This localizes the remaining nondeterminism to the generated canonical-ABI post-return path for
the large nested init(app-name, symbols, stub-wasi) -> result<_, string> call (or allocator
state changed by that path), rather than Python initialization, component encoding, or snapshot
measurement. The current-main update moved from wit-bindgen 0.53.1 to 0.61.0 and Wasmtime 46.0.1
to 48.0.0; the exact responsible layer still needs a minimal reduction.

The experiment deliberately did not use post-generation byte rewriting or a volatile-byte
allowlist.

Proposed direction

  1. Add a regression test that componentizes one fixture twice and compares the complete
    bytes.
  2. Add an opt-in reproducible mode so existing build-time clock/random semantics do not
    change by default.
  3. Give pre-initialization fixed clocks, random sources, Python hash seed, allocator state,
    and bytecode behavior in that mode.
  4. Canonicalize generated directory metadata and provide input files through a staged or
    virtualized tree. The implementation should not modify user source-file metadata.
  5. Keep output ordering deterministic throughout stub generation and component encoding.
  6. Add a reduced test around the init canonical-ABI boundary. Candidate fixes include capturing
    memory before post-return cleanup, making that cleanup allocator-deterministic, or changing
    the private init protocol so the large nested argument graph does not leave volatile allocator
    state in the captured memory. Any fix must preserve matching allocator globals and memory.

Would the maintainers prefer this as one opt-in feature PR, or as smaller PRs after the
stub-ordering fix?

Additional context

This was found while building a digest-pinned Python/WASI runtime. Reproducibility is a
supply-chain requirement there: the generated component digest is part of the runtime
identity, not merely a build-cache optimization.

主要語言
Rust
星號
284
分支
56
平均合併
2 天 4 小時
30 天內合併 PR
3

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

bytecodealliance/componentize-py 的其他 Issue

查看 bytecodealliance/componentize-py 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。