Bug report: Generated C++ glue code bug.

Offen
#1,585 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Ruhig
Tech-Stack
cpp, python, wasm
Bereich
compilers, tooling

Rechercherichtung

Reproduzieren Sie den Fehler mithilfe der vereinfachten wasmtime- und jco-Befehle und untersuchen Sie anschließend die generierte Bindung unter xxx/cplusplus/world1-consumer/world1-consumer_generated.cpp, insbesondere den File-Destruktor und den Aufrufpfad von list<own>. Vergleichen Sie das generierte C++-Verhalten mit der Rust-Implementierung aus der angehängten Reproduktion und verifizieren Sie, dass die kombinierte Komponente in beiden Runtimes ohne einen invalid-handle- oder unknown-handle-Fehler abgeschlossen wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

gen-cpp

cplusplus.zip
generated_wit_00000.wit.zip
generated_wit_00000b.wit.zip
python.zip
world0-pvwxtgtmlk_fromPython.wasm.zip
world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm.zip
world1-consumer_fromC++.wasm.zip

Summary

Generate C++ glue code via wit-bindgen, then generate a Wasm component.
When executed, both wasmtime and jco crash.
It may be caused by the glue code generated by wit-bindgen causing resource dropped multiple times.

Environment

jco 1.16.1
wasmtime 41.0.0
wit-bindgen 0.51.0
OS: macOS Sequoia 15.7
CPU: Intel Core i7

Reproduce steps

The specific reproduction steps are as follows:

  1. Use the following command to generate Python binding files from generated_wit_00000.wit.
    componentize-py -d xxx/witfiles/package3hl16smu1p -w world0-pvwxtgtmlk bindings xxx/python/world0-pvwxtgtmlk

  2. Implement the Python program as shown in python.zip.

  3. Use the following command to generate Wasm component file from Python.
    cd xxx/python/world0-pvwxtgtmlk
    componentize-py --wit-path xxx/witfiles/package3hl16smu1p --world world0-pvwxtgtmlk componentize world0-pvwxtgtmlk_generated -o xxx/componentfiles/world0-pvwxtgtmlk_fromPython.wasm

  4. Use the following command to generate C++ binding files from generated_wit_00000b.wit.
    wit-bindgen cpp xxx/witfiles/package3hl16smu1p --world world1-consumer --out-dir xxx/cplusplus/world1-consumer

  5. Implement the C++ program as shown in cplusplus.zip.

  6. Use the following command to generate Wasm component file from C++.
    xxx/WASI/wasi-sdk-27.0/wasi-sdk-27.0-x86_64-macos/bin/wasm32-wasip2-clang++ -std=c++23 -o xxx/componentfiles/world1-consumer_fromC++.wasm -mexec-model=reactor xxx/cplusplus/world1-consumer/world1-consumer_generated.cpp xxx/cplusplus/world1-consumer/world1_consumer.cpp xxx/cplusplus/world1-consumer/world1_consumer_component_type.o

  7. Use wac to combine the two Wasm component files:
    wac plug xxx/componentfiles/world1-consumer_fromC++.wasm --plug xxx/componentfiles/world0-pvwxtgtmlk_fromPython.wasm -o xxx/componentfiles/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm

  8. Use wasmtime to run the final Wasm component file:
    wasmtime run world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm
    Wasmtime prints:

[min-list-own] run begin
[py-provider] make_seq n=2 begin
[py-provider] file.__init__ path=mem://file-1 id=1
[py-provider] file.__init__ path=mem://file-2 id=2
[py-provider] make_seq end ids=[1, 2]
[min-list-own] after make_seq size=2
[py-provider] drain_ids begin len=2
[py-provider] drain_ids consumed ids=[1, 2]
[py-provider] drain_ids end len=2
[min-list-own] drain_ids len=2 ids=[1,2]
[min-list-own] PASS
Error: failed to run main module `world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm`

Caused by:
    0: failed to invoke `run` function
    1: error while executing at wasm backtrace:
           0: 0x22813ea - world1-consumer_fromC++.wasm!mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File::~File()
           1: 0x227fdba - world1-consumer_fromC++.wasm!wit::vector<mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File>::~vector()
           2: 0x227f7dd - world1-consumer_fromC++.wasm!exports::wasi::cli::run::Run()
           3: 0x2281dae - world1-consumer_fromC++.wasm!__wasm_export_wasiX3AcliX2FrunX400X2E2X2E0X23run
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: unknown handle index 5
  1. Use jco to run the same final Wasm component file:
    jco run world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm
    jco prints:
[min-list-own] run begin
[py-provider] make_seq n=2 begin
[py-provider] file.__init__ path=mem://file-1 id=1
[py-provider] file.__init__ path=mem://file-2 id=2
[py-provider] make_seq end ids=[1, 2]
[min-list-own] after make_seq size=2
[py-provider] drain_ids begin len=2
[py-provider] drain_ids consumed ids=[1, 2]
[py-provider] drain_ids end len=2
[min-list-own] drain_ids len=2 ids=[1,2]
[min-list-own] PASS
TypeError: Invalid handle
    at rscTableRemove (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:1597:13)
    at trampoline165 (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:37995:23)
    at world1-consumer_fromC++.wasm.mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File::~File() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[84]:0x2e9d)
    at world1-consumer_fromC++.wasm.wit::vector<mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File>::~vector() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[36]:0x186d)
    at world1-consumer_fromC++.wasm.exports::wasi::cli::run::Run() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[23]:0x1290)
    at world1-consumer_fromC++.wasm.__wasm_export_wasiX3AcliX2FrunX400X2E2X2E0X23run (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[103]:0x3861)
    at fn (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:38984:15)
    at _withGlobalCurrentTaskMeta (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:487:12)
    at Object.run (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:38981:15)
    at file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/_run.js:14:15

Simplified reproduction steps

  1. Use wasmtime to run the final Wasm component file:
    wasmtime run world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm
    Wasmtime prints:
[min-list-own] run begin
[py-provider] make_seq n=2 begin
[py-provider] file.__init__ path=mem://file-1 id=1
[py-provider] file.__init__ path=mem://file-2 id=2
[py-provider] make_seq end ids=[1, 2]
[min-list-own] after make_seq size=2
[py-provider] drain_ids begin len=2
[py-provider] drain_ids consumed ids=[1, 2]
[py-provider] drain_ids end len=2
[min-list-own] drain_ids len=2 ids=[1,2]
[min-list-own] PASS
Error: failed to run main module `world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm`

Caused by:
    0: failed to invoke `run` function
    1: error while executing at wasm backtrace:
           0: 0x22813ea - world1-consumer_fromC++.wasm!mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File::~File()
           1: 0x227fdba - world1-consumer_fromC++.wasm!wit::vector<mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File>::~vector()
           2: 0x227f7dd - world1-consumer_fromC++.wasm!exports::wasi::cli::run::Run()
           3: 0x2281dae - world1-consumer_fromC++.wasm!__wasm_export_wasiX3AcliX2FrunX400X2E2X2E0X23run
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: unknown handle index 5
  1. Use jco to run the same final Wasm component file:
    jco run world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.wasm
    jco prints:
[min-list-own] run begin
[py-provider] make_seq n=2 begin
[py-provider] file.__init__ path=mem://file-1 id=1
[py-provider] file.__init__ path=mem://file-2 id=2
[py-provider] make_seq end ids=[1, 2]
[min-list-own] after make_seq size=2
[py-provider] drain_ids begin len=2
[py-provider] drain_ids consumed ids=[1, 2]
[py-provider] drain_ids end len=2
[min-list-own] drain_ids len=2 ids=[1,2]
[min-list-own] PASS
TypeError: Invalid handle
    at rscTableRemove (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:1597:13)
    at trampoline165 (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:37995:23)
    at world1-consumer_fromC++.wasm.mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File::~File() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[84]:0x2e9d)
    at world1-consumer_fromC++.wasm.wit::vector<mydefined::package3hl16smu1p::itf0_tmmdqdfhit::File>::~vector() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[36]:0x186d)
    at world1-consumer_fromC++.wasm.exports::wasi::cli::run::Run() (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[23]:0x1290)
    at world1-consumer_fromC++.wasm.__wasm_export_wasiX3AcliX2FrunX400X2E2X2E0X23run (wasm://wasm/world1-consumer_fromC++.wasm-00abfbfa:wasm-function[103]:0x3861)
    at fn (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:38984:15)
    at _withGlobalCurrentTaskMeta (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:487:12)
    at Object.run (file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/world1-consumer_fromC++_importworld0-pvwxtgtmlk_fromPython.js:38981:15)
    at file:///private/var/folders/ng/263gml_52h33wsbthhfjjr980000gn/T/6vhFlu/_run.js:14:15

Result Analysis

It may due to:
wit-bindgen C++: list<own> imported argument is not invalidated after call, causing invalid-handle/double-drop in destructor.
When importing a function with signature drain-ids: func(xs: list<own>) -> list, generated C++ bindings map it to DrainIds(std::span xs).
After the call returns, caller-side xs elements are still destructed as if they own live handles, leading to a second drop and runtime error (unknown handle index / Invalid handle).

And when we replace C++ with Rust to implement the same logic, this bug disappers in both wasmtime and jco. So, it is not due to the Wasm runtime.

Vorherrschende Sprache
Rust
Sterne
1.5k
Forks
306
Ø Merge
1 T. 4 Std.
Gemergte PRs (30 T.)
21

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Keine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus bytecodealliance/wit-bindgen

Alle Issues in bytecodealliance/wit-bindgen

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.