Stream: wasm

Topic: "new" Import/Export Name Rules


view this post on Zulip Robin Brown (Jun 23 2023 at 22:32):

I'm trying to update my template compiler project and its tests to the latest versions of wasmtime and wasm-tools. However, after resolving some WIT syntax and minor API changes, I'm getting stuck on the new export validation naming rules (as seen here).

(component
  ...
  (type (;0;) (record))
  (type (;1;) (func (param "params" 0) (result string)))
  (func (;0;) (type 1) (canon lift (core func 1) string-encoding=utf8 (memory 0) (realloc 0)))
  (export (;1;) "apply" (func 0))
)
Error: failed to parse WebAssembly module

Caused by:
    func not valid to be used as export (at offset 0x40b)

From what I'm reading, I think I need to export the record type holding my parameters (in this case there are no params, it has no fields). However, when I add that logic and an additional export before the function export the problem doesn't go away.

(component
  ...
  (type (;0;) (record))
  (type (;1;) (func (param "params" 0) (result string)))
  (func (;0;) (type 1) (canon lift (core func 1) string-encoding=utf8 (memory 0) (realloc 0)))
  (export (;2;) "params" (type 0))
  (export (;1;) "apply" (func 0))
)
Error: failed to parse WebAssembly module

Caused by:
    func not valid to be used as export (at offset 0x416)

What does the component need to look like for this to work?

Contribute to Kylebrown9/template-compiler development by creating an account on GitHub.
Low level tooling for WebAssembly in Rust. Contribute to bytecodealliance/wasm-tools development by creating an account on GitHub.

view this post on Zulip Robin Brown (Jun 23 2023 at 22:37):

I've also tried splitting up the export sections to match the tests more closely but get an even more confusing error.

(component
  ...
  (type (;0;) (record))
  (export (;1;) "params" (type 0))
  (type (;2;) (func (param "params" 0) (result string)))
  (func (;0;) (type 1) (canon lift (core func 1) string-encoding=utf8 (memory 0) (realloc 0)))
  (export (;1;) "apply" (func 0))
)
Error: failed to parse WebAssembly module

Caused by:
    type index 1 is not a function type (at offset 0x40f)

view this post on Zulip Robin Brown (Jun 23 2023 at 22:38):

I'm also don't understand why wasmprinter is labeling the indices the way it does and why splitting sections up behaves so strangely.

view this post on Zulip Alex Crichton (Jun 23 2023 at 23:10):

it's subtle, but the fix here is to change (param "params" 0) to (param "params" 1)

view this post on Zulip Alex Crichton (Jun 23 2023 at 23:11):

indices in comments like (;0;) mean that the item is introducing and defining index 0, and those comments can in theory be replaced with $foo for human-readability

view this post on Zulip Alex Crichton (Jun 23 2023 at 23:11):

indicies like (type 1) are referring to the index 1

view this post on Zulip Alex Crichton (Jun 23 2023 at 23:12):

as for sections, at least for now in components the seconds are often interspersed and occur frequently, often with 1 element in them, which is why everything looks jumbled up

view this post on Zulip Alex Crichton (Jun 23 2023 at 23:12):

the text format preserves the order in the original binary

view this post on Zulip Robin Brown (Jun 27 2023 at 15:37):

Do exports introduce new indices in their respective index spaces? If so, I think that's the concept/detail I was missing.

view this post on Zulip Robin Brown (Jun 27 2023 at 15:38):

Thanks for your help! The upgrade was successful with the index change you suggested.
https://github.com/Kylebrown9/template-compiler/pull/2

Update dependencies and make changes to support Wasmtime 10.

Last updated: Dec 23 2024 at 12:05 UTC