Skip to content

Is importname' rule has a typo? #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dannypsnl opened this issue Aug 27, 2024 · 4 comments
Closed

Is importname' rule has a typo? #391

dannypsnl opened this issue Aug 27, 2024 · 4 comments

Comments

@dannypsnl
Copy link

In the binary format

importname' ::= 0x00 len:<u32> in:<importname>                       => in  (if len = |in|)

however, when I dump the binary I got 0x01, is this a typo or version problem?

Tools version

  • wasm-tools 1.216.0
  • cargo-component-component 0.15.0
@alexcrichton
Copy link
Collaborator

Could you clarify what binary you're using and what you're seeing? Locally I see:

$ wasm-tools dump <<< "(component (import \"x\" (func)))"  
  0x0 | 00 61 73 6d | version 13 (Component)
      | 0d 00 01 00
  0x8 | 07 05       | component type section
  0xa | 01          | 1 count
  0xb | 40 00 01 00 | [type 0] Func(ComponentFuncType { params: [], results: Named([]) })
  0xf | 0a 06       | component import section
 0x11 | 01          | 1 count
 0x12 | 00 01 78 01 | [func 0] ComponentImport { name: ComponentImportName("x"), ty: Func(0) }
      | 00

where the import starts at 0x12. That has an 00 leading prefix, 01 78 is "x" and then 01 00 is "func index 0". Given all that it's expected that wasm-tools generates an 0x00 leading byte for imports.

@dannypsnl
Copy link
Author

dannypsnl commented Sep 2, 2024

src/lib.rs

#[allow(warnings)]
mod bindings;

use bindings::Guest;

struct Component;

impl Guest for Component {
     fn add(x: i32, y: i32) -> i32 {
        x + y
    }
}

bindings::export!(Component with_types_in bindings);

wit/world.wit:

package component:add;

/// An example world for the component to target.
world example {
    export add: func(x: s32, y: s32) -> s32;
}

In Cargo.toml:

[dependencies]
wit-bindgen-rt = { version = "0.30.0", features = ["bitflags"] }

Use cargo component build get target/wasm32-wasip1/debug/add.wasm, in this file I got

     0x97 | 0a 18       | component import section
     0x99 | 01          | 1 count
     0x9a | 01 13 77 61 | [instance 2] ComponentImport { name: ComponentImportName("wasi:io/[email protected]"), ty: Instance(2) }
          | 73 69 3a 69
          | 6f 2f 65 72
          | 72 6f 72 40
          | 30 2e 32 2e
          | 30 05 02   

alexcrichton added a commit to alexcrichton/wasm-tools that referenced this issue Sep 3, 2024
Starting in WebAssembly/component-model#263 the component model binary
specification was updated in a technically breaking way to encode
binaries differently. This was intended to be rolled out in a manner
that minimized breakage however so bytecodealliance#1262
implemented validation where the prefix byte which changed was actually
ignored if it was 0 or 1. The encoder at the time still emitted 1 as the
prefix byte, however. The intention was that once the validator had
percolated enough the encoder would switch to using 0.

Unfortunately this change was basically forgotten about until now with
WebAssembly/component-model#391, but now's probably "enough time passed"
so the encoder is updated to emit a 0x00 leading byte in this situation
instead of the historical 0x01 byte. This will start the transition
period to eventually removing the validator's acceptance of the 0x01
byte.
alexcrichton added a commit to alexcrichton/wasm-tools that referenced this issue Sep 3, 2024
Starting in WebAssembly/component-model#263 the component model binary
specification was updated in a technically breaking way to encode
binaries differently. This was intended to be rolled out in a manner
that minimized breakage however so bytecodealliance#1262
implemented validation where the prefix byte which changed was actually
ignored if it was 0 or 1. The encoder at the time still emitted 1 as the
prefix byte, however. The intention was that once the validator had
percolated enough the encoder would switch to using 0.

Unfortunately this change was basically forgotten about until now with
WebAssembly/component-model#391, but now's probably "enough time passed"
so the encoder is updated to emit a 0x00 leading byte in this situation
instead of the historical 0x01 byte. This will start the transition
period to eventually removing the validator's acceptance of the 0x01
byte.
@alexcrichton
Copy link
Collaborator

Ah thanks for clarifying! You've discovered a situation where wasm-tools was in a "halfway state" in transitioning from one format to another. I've opened bytecodealliance/wasm-tools#1753 to fix this to always emit the 0x00 prefix byte. In that sense this bug is in wasm-tools, not the spec itself, and the spec as-written is correct.

@dannypsnl
Copy link
Author

I see, thanks for clarifying.

github-merge-queue bot pushed a commit to bytecodealliance/wasm-tools that referenced this issue Sep 9, 2024
* Proceed to the next step in the "remove `interface`" transition

Starting in WebAssembly/component-model#263 the component model binary
specification was updated in a technically breaking way to encode
binaries differently. This was intended to be rolled out in a manner
that minimized breakage however so #1262
implemented validation where the prefix byte which changed was actually
ignored if it was 0 or 1. The encoder at the time still emitted 1 as the
prefix byte, however. The intention was that once the validator had
percolated enough the encoder would switch to using 0.

Unfortunately this change was basically forgotten about until now with
WebAssembly/component-model#391, but now's probably "enough time passed"
so the encoder is updated to emit a 0x00 leading byte in this situation
instead of the historical 0x01 byte. This will start the transition
period to eventually removing the validator's acceptance of the 0x01
byte.

* Update test expectation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants