Files
wasmtime/crates/wasi-crypto
Andrew Brown 7c67378ab6 wiggle: copy guest strings from shared memory (#5475)
* wiggle: copy guest strings from shared memory

Along the same lines as #5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.

This change updates the uses of Wiggle strings in both wasi-common and
wasi-crypto.

* review: perform UTF-8 check on `GuestStr` construction
2023-01-04 10:10:00 -06:00
..
2021-10-10 21:34:43 +02:00
2021-10-10 21:34:43 +02:00

wasmtime-wasi-crypto

This crate enables support for the wasi-crypto APIs in Wasmtime.

The sole purpose of the implementation is to allow bindings and application developers to test the proposed APIs. This implementation is not meant to be used in production. Like the specification, it is currently experimental and its functionality can quickly change.

Since the wasi-crypto API is expected to be an optional feature of WASI, this crate is currently separate from the wasi-common crate.

Wasmtime integration

Use the Wasmtime APIs to instantiate a Wasm module and link the wasi-crypto modules as follows:

use wasmtime_wasi_crypto::{
    WasiCryptoAsymmetricCommon, WasiCryptoCommon, WasiCryptoCtx, WasiCryptoSignatures,
    WasiCryptoSymmetric,
};

let cx_crypto = WasiCryptoCtx::new();
WasiCryptoCommon::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;
WasiCryptoAsymmetricCommon::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;
WasiCryptoSignatures::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;
WasiCryptoSymmetric::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;

let wasi = wasmtime_wasi::old::snapshot_0::Wasi::new(linker.store(), mk_cx()?);
wasi.add_to_linker(linker)?;

Building Wasmtime

Wasmtime must be compiled with the wasi-crypto feature flag (disabled by default) in order to include the crypto APIs.

Examples

Example rust bindings and assemblyscript bindings are provided to demonstrate how these APIs can be used and exposed to applications in an idiomatic way.