bytecodealliance/wac

`wac_types::Package::from_bytes` fails with "should have owner" after #74

Closed this issue · 2 comments

With https://github.com/bytecodealliance/wac/tree/66c067254ecf9ad862a195e49d3465b656f4e517, I was able to transition my project from wac-parser to wac-graph and run my component composition successfully.

However, with #74 (tested on https://github.com/bytecodealliance/wac/tree/d3f6be29f2629d866be4b1e476b373d7feaf77b5), wac_types::Package::from_bytes fails with the following panic:

thread 'main' panicked at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:770:26:
should have owner
stack backtrace:
   0: rust_begin_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:647:5
   1: core::panicking::panic_fmt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_display
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:196:5
   3: core::panicking::panic_str
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:171:5
   4: core::option::expect_failed
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/option.rs:1988:5
   5: core::option::Option<T>::expect
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/option.rs:894:21
   6: wac_types::package::TypeConverter::resource
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:768:34
   7: wac_types::package::TypeConverter::ty
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:502:73
   8: wac_types::package::TypeConverter::entity
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:562:35
   9: wac_types::package::TypeConverter::component_instance_type
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:537:26
  10: wac_types::package::TypeConverter::entity
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:568:17
  11: wac_types::package::TypeConverter::export
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:415:9
  12: wac_types::package::Package::from_bytes::{{closure}}
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:239:65
  13: core::iter::adapters::map::map_try_fold::{{closure}}
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/adapters/map.rs:96:28
  14: core::iter::traits::iterator::Iterator::try_fold
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs:2462:21
  15: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/adapters/map.rs:122:9
  16: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::try_fold
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/adapters/mod.rs:201:9
  17: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/mod.rs:378:13
  18: core::iter::traits::iterator::Iterator::for_each
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs:858:9
  19: <indexmap::map::IndexMap<K,V,S> as core::iter::traits::collect::Extend<(K,V)>>::extend
             at /workspace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.2.6/src/map.rs:1364:9
  20: <indexmap::map::IndexMap<K,V,S> as core::iter::traits::collect::FromIterator<(K,V)>>::from_iter
             at /workspace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.2.6/src/map.rs:1312:9
  21: core::iter::traits::iterator::Iterator::collect
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs:2054:9
  22: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter::{{closure}}
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/result.rs:1930:51
  23: core::iter::adapters::try_process
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/adapters/mod.rs:170:17
  24: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/result.rs:1930:9
  25: core::iter::traits::iterator::Iterator::collect
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs:2054:9
  26: wac_types::package::Package::from_bytes
             at /workspace/.cargo/git/checkouts/wac-6a66731467f4795b/d3f6be2/crates/wac-types/src/package.rs:237:75
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I have attached one of my components which now fails to load:
virtual_wasi_cli.component.wasm

Did a bit of research on the issue here. I may have find the issue.

The panic is happening when we're processing the following exported instance:

  (component (;7;)
    (import "import-type-terminal-input" (type (;0;) (sub resource))) ;; The problem export uses the type imported here
    (import "import-type-terminal-input0" (type (;1;) (eq 0)))
    (type (;2;) (own 1))
    (type (;3;) (option 2))
    (type (;4;) (func (result 3)))
    (import "import-func-get-terminal-stdin" (func (;0;) (type 4)))
    (export (;5;) "terminal-input" (type 0)) ;; Issue happens here
    (type (;6;) (own 5))
    (type (;7;) (option 6))
    (type (;8;) (func (result 7)))
    (export (;1;) "get-terminal-stdin" (func 0) (func (type 8)))
  )
  (instance (;16;) (instantiate 7
      (with "import-func-get-terminal-stdin" (func 9))
      (with "import-type-terminal-input" (type 27))
      (with "import-type-terminal-input0" (type 27))
    )
  )
  (export (;17;) "wasi:cli/terminal-stdin@0.2.0" (instance 16))

The issue happens inside of the component type definition with the line (export (;5;) "terminal-input" (type 0)) which exports a resource which we've seen to have seen before two exports previous:

  (component (;5;)
    (import "import-type-terminal-input" (type (;0;) (sub resource))) ;; This type has the same `ResourceId`
    (export (;1;) "terminal-input" (type 0))
  )
  (instance (;12;) (instantiate 5
      (with "import-type-terminal-input" (type 6))
    )
  )
  (export (;13;) "wasi:cli/terminal-input@0.2.0" (instance 12))

The ResourceId of resource import in component 5 is the same ResourceId as the resource import in component 7.

I think the expectation is wrong, as in this case it's an alias that isn't owned by an interface, and that's okay.

A think a failing test case in the graph tests might be having the graph load a dummy component created from:

world w {
  resource x;
  type x = y;
  import f: func(x: x) -> y;
}

I suspect we can just relax the expectation in the type converter and allow the alias owner to be None when find_owner returns None.