Skip to content
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

failed to validate output component #1798

Closed
sammyne opened this issue Sep 18, 2024 · 3 comments
Closed

failed to validate output component #1798

sammyne opened this issue Sep 18, 2024 · 3 comments

Comments

@sammyne
Copy link

sammyne commented Sep 18, 2024

Component has been built successfully but failed the invalidation.

This repo https://github.com/sammyne/wasm-component-broken-compose is made to reproduce the problem.

Requirement

  • Rust 1.80.1 (376290515 2024-07-16)
  • wasm-tools 1.216.0
  • wasmtime adapter modules 20.0.0 (9e1084ffa 2024-04-22)

Two components adder and calculator whose wit files go respectively as

  • adder.wit
package docs:[email protected];

interface api {
    use types.{balance};

    variant wallet {
        btc(balance),
        eth(balance),
    }

    incr: func(self: wallet, value: u64) -> result<wallet, string>;
}

interface types {
    record balance {
        value: u64,
    }
}

world docs-adder {
    export api;
}
  • calculator.wit
package docs:[email protected];

interface hi {
  use docs:adder/[email protected].{wallet};

  greet: func(a: wallet);
}

world xml-hello {
  export hi;
}

Reproduction

  1. Setup WIT dependencies

    mkdir -p calculator/wit/deps
    cp -r adder/wit calculator/wit/deps/adder
  2. Build the WASM modules

    cargo build --release -p adder
    
    cargo build --release -p calculator
  3. Componentize

    Assume wasmtime adapter modules is at path /opt/wasmtime/adapter-modules/wasi_snapshot_preview1.reactor.wasm.

    rm -rf _out && mkdir _out
    
    wasm-tools component new target/wasm32-wasip1/release/adder.wasm -o _out/adder-component.wasm \
      --adapt /opt/wasmtime/adapter-modules/wasi_snapshot_preview1.reactor.wasm
    
    wasm-tools component new target/wasm32-wasip1/release/calculator.wasm -o _out/calculator-component.wasm \
      --adapt /opt/wasmtime/adapter-modules/wasi_snapshot_preview1.reactor.wasm
  4. Compose

    wasm-tools compose _out/calculator-component.wasm -d _out/adder-component.wasm -o _out/calculator-composed.wasm

    Log outputs as follow

    WARNING: `wasm-tools compose` has been deprecated.
    
    Please use `wac` instead. You can find more information about `wac` at https://github.com/bytecodealliance/wac.
    [2024-09-18T02:46:09Z WARN ] instance `docs:adder/[email protected]` will be imported because a dependency named `docs:adder/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:cli/[email protected]` will be imported because a dependency named `wasi:cli/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:cli/[email protected]` will be imported because a dependency named `wasi:cli/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:io/[email protected]` will be imported because a dependency named `wasi:io/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:io/[email protected]` will be imported because a dependency named `wasi:io/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:cli/[email protected]` will be imported because a dependency named `wasi:cli/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:cli/[email protected]` will be imported because a dependency named `wasi:cli/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:cli/[email protected]` will be imported because a dependency named `wasi:cli/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:clocks/[email protected]` will be imported because a dependency named `wasi:clocks/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:filesystem/[email protected]` will be imported because a dependency named `wasi:filesystem/[email protected]` could not be found
    [2024-09-18T02:46:09Z WARN ] instance `wasi:filesystem/[email protected]` will be imported because a dependency named `wasi:filesystem/[email protected]` could not be found
    error: failed to validate output component `_out/calculator-composed.wasm`
    
    Caused by:
        0: instance not valid to be used as export (at offset 0x2a8b7)

    INDICATING THE COMPOSED COMPONENT IS INVALID.

@alexcrichton
Copy link
Member

Thanks for the report! I can confirm this locally as well and I think I know what's going on. The problem here has to do with how the component model validates that types are exported, used, and named in components. The root of the issue here is that wac looks like it'll have to go a bit out of its way to get this to be typed correctly. Both wasm-tools compose and wac don't do this because I don't think anyone realized that this was required until just now!

The outline of the problem is that you have a calculator where the hi interface depends on api, and then that interface depends on types. The adder component implements the api interface, meaning that the final component will import the types interface while exporting the hi interface. The problem here is that the hi interface is using a type that's only defined in the api interface. The api interface isn't output from the final component, meaning that hi is referring to a type that doesn't actually exist anywhere. That's what causes the invalid component.

You can fix this for the time being by moving the wallet type to the types interface. Otherwise wac will need to be updated to synthesize an import of the api interface, but only with type information, in the final component.

@alexcrichton
Copy link
Member

Would you be ok reopening this issue on the wac repository? Given the deprecated status of wasm-tools compose it's not going to get fixed, but I can help fill in more information on the wac side of things on that issue to (or mostly just cc myself there)

@sammyne
Copy link
Author

sammyne commented Sep 23, 2024

Would you be ok reopening this issue on the wac repository? Given the deprecated status of wasm-tools compose it's not going to get fixed, but I can help fill in more information on the wac side of things on that issue to (or mostly just cc myself there)

It has been done by estk in bytecodealliance/wac#141. So this one is closed.

@sammyne sammyne closed this as completed Sep 23, 2024
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