Files
wasmtime/tests/misc_testsuite/component-model/adapter.wast
Alex Crichton fb59de15af Implement fused adapters for (list T) types (#4558)
* Implement fused adapters for `(list T)` types

This commit implements one of the two remaining types for adapter
fusion, lists. This implementation is particularly tricky for a number
of reasons:

* Lists have a number of validity checks which need to be carefully
  implemented. For example the byte length of the list passed to
  allocation in the destination module could overflow the 32-bit index
  space. Additionally lists in 32-bit memories need a check that their
  final address is in-bounds in the address space.

* In the effort to go ahead and support memory64 at the lowest layers
  this is where much of the magic happens. Lists are naturally always
  stored in memory and shifting between 64/32-bit address spaces
  is done here. This notably required plumbing an `Options` around
  during flattening/size/alignment calculations due to the size/types of
  lists changing depending on the memory configuration.

I've also added a small `factc` program in this commit which should
hopefully assist in exploring and debugging adapter modules. This takes
as input a component (text or binary format) and then generates an
adapter module for all component function signatures found internally.

This commit notably does not include tests for lists. I tried to figure
out a good way to add these but I felt like there were too many cases to
test and the tests would otherwise be extremely verbose. Instead I think
the best testing strategy for this commit will be through #4537 which
should be relatively extensible to testing adapters between modules in
addition to host-based lifting/lowering.

* Improve handling of lists of 0-size types

* Skip overflow checks on byte sizes for 0-size types
* Skip the copy loop entirely when src/dst are both 0
* Skip the increments of src/dst pointers if either is 0-size

* Update semantics for zero-sized lists/strings

When a list/string has a 0-byte-size the base pointer is no longer
verified to be in-bounds to match the supposedly desired adapter
semantics where no trap happens because no turn of the loop happens.
2022-08-01 17:02:08 -05:00

134 lines
2.8 KiB
Plaintext

;; basic function lifting
(component
(core module $m
(func (export ""))
)
(core instance $i (instantiate $m))
(func (export "thunk")
(canon lift (core func $i ""))
)
)
;; use an aliased type
(component $c
(core module $m
(func (export ""))
)
(core instance $i (instantiate $m))
(type $to_alias (func))
(alias outer $c $to_alias (type $alias))
(func (export "thunk") (type $alias)
(canon lift (core func $i ""))
)
)
;; test out some various canonical abi
(component $c
(core module $m
(func (export "") (param i32 i32))
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32)
unreachable)
)
(core instance $i (instantiate $m))
(func (export "thunk") (param string)
(canon lift
(core func $i "")
(memory $i "memory")
(realloc (func $i "realloc"))
)
)
(func (export "thunk8") (param string)
(canon lift
(core func $i "")
string-encoding=utf8
(memory $i "memory")
(realloc (func $i "realloc"))
)
)
(func (export "thunk16") (param string)
(canon lift
(core func $i "")
string-encoding=utf16
(memory $i "memory")
(realloc (func $i "realloc"))
)
)
(func (export "thunklatin16") (param string)
(canon lift
(core func $i "")
string-encoding=latin1+utf16
(memory $i "memory")
(realloc (func $i "realloc"))
)
)
)
;; lower something then immediately lift it
(component $c
(import "host-return-two" (func $f (result u32)))
(core func $f_lower
(canon lower (func $f))
)
(func $f2 (result s32)
(canon lift (core func $f_lower))
)
(export "f" (func $f2))
)
;; valid, but odd
(component
(core module $m (func (export "")))
(core instance $m (instantiate $m))
(func $f1 (canon lift (core func $m "")))
(core func $f2 (canon lower (func $f1)))
)
(assert_trap
(component
(core module $m (func (export "")))
(core instance $m (instantiate $m))
(func $f1 (canon lift (core func $m "")))
(core func $f2 (canon lower (func $f1)))
(core module $m2
(import "" "" (func $f))
(func $start
call $f)
(start $start)
)
(core instance (instantiate $m2
(with "" (instance (export "" (func $f2))))
))
)
"degenerate component adapter called")
;; fiddling with 0-sized lists
(component $c
(core module $m
(func (export "x") (param i32 i32))
(func (export "realloc") (param i32 i32 i32 i32) (result i32)
i32.const -1)
(memory (export "memory") 0)
)
(core instance $m (instantiate $m))
(func $f (param (list unit))
(canon lift
(core func $m "x")
(realloc (func $m "realloc"))
(memory $m "memory")
)
)
(export "empty-list" (func $f))
)
(assert_return (invoke "empty-list" (list.const)) (unit.const))