Run the following file in wasmtime 0.15.0 and in whatever version of wasmer is installed with curl https://get.wasmer.io -sSfL | sh
wasmer prints the expected output:
no foo.
bar
wasmtime prints nothing
;; Taken from: https://github.com/bytecodealliance/wasmtime/blob/master/docs/WASI-tutorial.md and modified
(module
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
(memory 1)
(data (i32.const 1024) "no foo.\nbar\n")
(func $print_msg (param $array_ptr i32)
(call $fd_write
(i32.const 1) ;; file descriptor
(local.get $array_ptr) ;; pointer to first iovec in array
(i32.const 1) ;; len of array
(i32.const 2048) ;; place to write number of bytes written
)
drop
)
(func $main (export "_start")
;; Creating a new io vector within linear memory
(i32.store (i32.const 0) (i32.const 1024))
(i32.store (i32.const 4) (i32.const 8))
(i32.store (i32.const 8) (i32.const 1032))
(i32.store (i32.const 12) (i32.const 4))
(call $print_msg (i32.const 0))
(call $print_msg (i32.const 8))
)
)
I assume the issue is with wasmtime.
Thanks for the report! The issue here is that the wasm linear memory isn't exported, but it's expected to be exported for wasi to work with it. You can fix this by replacing (memory 1) with (memory (export "memory") 1).
The exact particulars here are a little up-in-the-air since historically wasi implementations would use a memory without requiring it be exported, but semantically that felt a little odd and we tweaked it to require an export. The fd_write call in the example you've written is returning an error because memory isn't exported, but the error is being ignored.
Thank you for the explanation. What is the difference between (memory (export "memory") 1) and
(memory 1)
(export "memory" (memory 0))
I assume the 2nd one does the same thing, because (memory 1) indicates "give this program 1 page of memory" while (export "memory" (memory 0)) indicates "export the memory starting at location 0".
Oh one is just shorthand for the other. The (export "memory" (memory 0)) isn't necessarily exporting a memory that starts at 0, but rather exports the 0th memory (memory index zero), which in this case was defined by (memory 1) (which was indeed "define a memory with at least one page in size")
cc @kubkon
This issue or pull request has been labeled: "wasi"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.
@alexcrichton This question is off topic, but I would greatly appreciate if you could answer it. What would be an easy way to print out the error coder returned by $fd_write(or any other wasi system call)? I could try storing the integer in an array pointed to by an iovec, but I suspect I need to encode the integer into a UTF-8 format first.
Ah yeah currently WASI has no way to print an integer, so there's not really an easy way to do that with *.wat. At that point you'd probably want to use a source language like Rust/C/etc.