Stream: general

Topic: Adding a new syscall to WASI and implement in Wasmtime


view this post on Zulip Radu Matei (Mar 31 2020 at 06:00):

Hi, everyone!

I've been playing around with trying to define a new syscall in wasi_snapshot_preview1 that I could import in modules.
I've added the function interface in the witx file from WASI/phases/snapshot:

  ;;; Print a greeting message.
  (@interface func (export "print_greeting")
    (result $error $errno)
  )

Which updated wasi_common::snapshots::WasiSnapshotPreview1, so had to add the implementation for WasiCtx:

    fn print_greeting(&self) -> Result<()> {
        debug!("DEBUG PRINT_GREETING");
        log::warn!("WARN PRINT_GREETING");
        println!("Hello World from the new print_greeting syscall in WASI");
        Ok(())
    }

At this point I can write a simple module that tries to import the newly added syscall, and _it doesn't fail_, but the actual function doesn't get executed:

(module
    ;; (import "wasi_snapshot_preview1" "print_greeting" (func $print_greeting))
    (import "wasi_snapshot_preview1" "print_greeting" (func $print_greeting (result i32)))
    (func $main (export "_start")

        (call $print_greeting)
        return
    )
)
$ RUST_LOG=info ./target/debug/wasmtime run examples/new-syscall.wat -g
 INFO  faerie::elf > strtab: 0x271 symtab 0x358 relocs 0x4a8 sh_offset 0x658

The function itself does get wired up in the functions Wasmtime links - for example trying to import a non-existing function:

    import `non-existing-function` was not found in module `wasi_snapshot_preview1`

So my questions are whether the above is reasonable (keep in mind all this is educational) and what are the other places where the actual execution should be wired up.

Thanks!

view this post on Zulip Jakub Konka (Mar 31 2020 at 07:28):

Hey @Radu M! You're actually on the right track. The thing you're missing is memory export directive. This WAT should work:

(module
    ;; (import "wasi_snapshot_preview1" "print_greeting" (func $print_greeting))
    (import "wasi_snapshot_preview1" "print_greeting" (func $print_greeting (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    (func $main (export "_start")

        (call $print_greeting)
        return
    )
)

view this post on Zulip Jakub Konka (Mar 31 2020 at 07:30):

Oh, and if you run with the following invocation in your shell (assuming you've got the latest master built)

env RUST_LOG=wasi_common=trace wasmtime new-syscall.wat

You should get auto-generated traces for all invoked syscalls. So you should see something like this:

 DEBUG wasi_common::ctx > WasiCtx inserting entry PendingEntry::Thunk(0x7ffee4071978)
 DEBUG wasi_common::sys::unix::entry > Host fd 0 is a char device
 DEBUG wasi_common::ctx              > WasiCtx inserted at Fd(0)
 DEBUG wasi_common::ctx              > WasiCtx inserting entry PendingEntry::Thunk(0x7ffee4071978)
 DEBUG wasi_common::sys::unix::entry > Host fd 1 is a char device
 DEBUG wasi_common::ctx              > WasiCtx inserted at Fd(1)
 DEBUG wasi_common::ctx              > WasiCtx inserting entry PendingEntry::Thunk(0x7ffee4071978)
 DEBUG wasi_common::sys::unix::entry > Host fd 2 is a char device
 DEBUG wasi_common::ctx              > WasiCtx inserted at Fd(2)
 DEBUG wasi_common::old::snapshot_0::ctx > WasiCtx inserting (0, Some(PendingEntry::Thunk(0x7ffee4072fa0)))
 DEBUG wasi_common::old::snapshot_0::sys::unix::entry_impl > Host fd 0 is a char device
 DEBUG wasi_common::old::snapshot_0::ctx                   > WasiCtx inserting (1, Some(PendingEntry::Thunk(0x7ffee4072fb0)))
 DEBUG wasi_common::old::snapshot_0::sys::unix::entry_impl > Host fd 1 is a char device
 DEBUG wasi_common::old::snapshot_0::ctx                   > WasiCtx inserting (2, Some(PendingEntry::Thunk(0x7ffee4072fc0)))
 DEBUG wasi_common::old::snapshot_0::sys::unix::entry_impl > Host fd 2 is a char device
 TRACE wasi_common::wasi::wasi_snapshot_preview1           > print_greeting()
 TRACE wasi_common::snapshots::wasi_snapshot_preview1      > Hmm
 TRACE wasi_common::wasi::wasi_snapshot_preview1           >      | errno=No error occurred. System call completed successfully. (Errno::Success(0))

view this post on Zulip Jakub Konka (Mar 31 2020 at 07:31):

Hope this helps! If things are still amiss, lemme know and I'll try to help!

view this post on Zulip Radu Matei (Mar 31 2020 at 07:31):

Ah!
That works, thank you so much, @Jakub Konka !

view this post on Zulip Jakub Konka (Mar 31 2020 at 07:31):

No probs!


Last updated: Oct 23 2024 at 20:03 UTC