forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
270 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use wasmtime_versioned_export_macros::versioned_suffix; | ||
|
||
fn main() { | ||
if !cfg!(feature = "gdb_jit_int") { | ||
return; | ||
} | ||
|
||
let mut build = cc::Build::new(); | ||
build.warnings(true); | ||
let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); | ||
build.define(&format!("CFG_TARGET_OS_{os}"), None); | ||
build.define("VERSIONED_SUFFIX", Some(versioned_suffix!())); | ||
|
||
println!("cargo:rerun-if-changed=gdbjit.c"); | ||
build.file("gdbjit.c"); | ||
build.compile("gdbjit-helpers"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
#define CONCAT2(a, b) a##b | ||
#define CONCAT(a, b) CONCAT2(a, b) | ||
#define VERSIONED_SYMBOL(a) CONCAT(a, VERSIONED_SUFFIX) | ||
|
||
#ifdef CFG_TARGET_OS_windows | ||
// export required for external access. | ||
__declspec(dllexport) | ||
#else | ||
// Note the `weak` linkage here, though, which is intended to let other code | ||
// override this symbol if it's defined elsewhere, since this definition doesn't | ||
// matter. | ||
// Just in case cross-language LTO is enabled we set the `noinline` attribute | ||
// and also try to have some sort of side effect in this function with a dummy | ||
// `asm` statement. | ||
__attribute__((weak, noinline)) | ||
#endif | ||
void __jit_debug_register_code() { | ||
#ifndef CFG_TARGET_OS_windows | ||
__asm__(""); | ||
#endif | ||
} | ||
|
||
struct JITDescriptor { | ||
uint32_t version_; | ||
uint32_t action_flag_; | ||
void *relevant_entry_; | ||
void *first_entry_; | ||
}; | ||
|
||
#ifdef CFG_TARGET_OS_windows | ||
// export required for external access. | ||
__declspec(dllexport) | ||
#else | ||
// Note the `weak` linkage here which is the same purpose as above. We want to | ||
// let other runtimes be able to override this since our own definition isn't | ||
// important. | ||
__attribute__((weak)) | ||
#endif | ||
struct JITDescriptor __jit_debug_descriptor = {1, 0, NULL, NULL}; | ||
|
||
struct JITDescriptor *VERSIONED_SYMBOL(wasmtime_jit_debug_descriptor)() { | ||
return &__jit_debug_descriptor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,61 @@ | ||
# WASI | ||
# WASIp2 | ||
|
||
You can also [browse this source code online][code] and clone the wasmtime | ||
repository to run the example locally. | ||
|
||
[code]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasi/main.rs | ||
|
||
This example shows how to use the [`wasi-common`] crate to define WASI | ||
This example shows how to use the [`wasmtime-wasi`] crate to define WASI | ||
functions within a [`Linker`] which can then be used to instantiate a | ||
WebAssembly module. | ||
WebAssembly component. | ||
|
||
[`wasi-common`]: https://crates.io/crates/wasi-common | ||
[`wasmtime-wasi`]: https://crates.io/crates/wasmtime-wasi | ||
[`Linker`]: https://docs.rs/wasmtime/*/wasmtime/struct.Linker.html | ||
|
||
### WebAssembly module source code | ||
## WebAssembly Component Source Code | ||
|
||
For this WASI example, this Hello World program is compiled to a WebAssembly module using the WASI Preview 1 API. | ||
For this WASI example, this Hello World program is compiled to a WebAssembly component using the WASIp2 API. | ||
|
||
`wasi.rs` | ||
```rust | ||
{{#include ../examples/wasi/wasm/wasi.rs}} | ||
{{#include ../examples/wasm/wasi.rs}} | ||
``` | ||
|
||
Building this program generates `target/wasm32-wasip1/debug/wasi.wasm`, used below. | ||
> Building instructions: | ||
> 1. Have Rust installed | ||
> 2. Add WASIp2 target if you haven't already: `rustup target add wasm32-wasip2` | ||
> 3. `cargo build --target wasm32-wasip2` | ||
### Invoke the WASM module | ||
Building this program generates `target/wasm32-wasip2/debug/wasi.wasm`, used below. | ||
|
||
This example shows adding and configuring the WASI imports to invoke the above WASM module. | ||
### Invoke the WASM component | ||
|
||
This example shows adding and configuring the WASI imports to invoke the above WASM component. | ||
|
||
`main.rs` | ||
```rust,ignore | ||
{{#include ../examples/wasi/main.rs}} | ||
``` | ||
|
||
## WASI state with other custom host state | ||
|
||
The [`add_to_linker`] takes a second argument which is a closure to access `&mut | ||
WasiCtx` from within the `T` stored in the `Store<T>` itself. In the above | ||
example this is trivial because the `T` in `Store<T>` is `WasiCtx` itself, but | ||
you can also store other state in `Store` like so: | ||
|
||
[`add_to_linker`]: https://docs.rs/wasi-common/*/wasi_common/sync/fn.add_to_linker.html | ||
[`Store`]: https://docs.rs/wasmtime/*/wasmtime/struct.Store.html | ||
[`BorrowMut<WasiCtx>`]: https://doc.rust-lang.org/stable/std/borrow/trait.BorrowMut.html | ||
[`WasiCtx`]: https://docs.rs/wasi-common/*/wasi_common/struct.WasiCtx.html | ||
|
||
```rust | ||
# extern crate wasmtime; | ||
# extern crate wasi_common; | ||
# extern crate anyhow; | ||
use anyhow::Result; | ||
use std::borrow::{Borrow, BorrowMut}; | ||
use wasmtime::*; | ||
use wasi_common::{WasiCtx, sync::WasiCtxBuilder}; | ||
|
||
struct MyState { | ||
message: String, | ||
wasi: WasiCtx, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let engine = Engine::default(); | ||
let mut linker = Linker::new(&engine); | ||
wasi_common::sync::add_to_linker(&mut linker, |state: &mut MyState| &mut state.wasi)?; | ||
|
||
let wasi = WasiCtxBuilder::new() | ||
.inherit_stdio() | ||
.inherit_args()? | ||
.build(); | ||
let mut store = Store::new(&engine, MyState { | ||
message: format!("hello!"), | ||
wasi, | ||
}); | ||
|
||
// ... | ||
|
||
# let _linker: Linker<MyState> = linker; | ||
Ok(()) | ||
} | ||
``` | ||
|
||
## WASI Preview 2 | ||
|
||
An experimental implementation of the WASI Preview 2 API is also available, along with an adapter layer for WASI Preview 1 WebAssembly modules. In future this `preview2` API will become the default. There are some features which are currently only accessible through the `preview2` API such as async support and overriding the clock and random implementations. | ||
|
||
### Async example | ||
|
||
This [async example code][code2] shows how to use the [wasmtime-wasi::preview2][`preview2`] module to | ||
execute the same WASI Preview 1 WebAssembly module from the example above. This example requires the `wasmtime` crate `async` feature to be enabled. | ||
This [async example code][code2] shows how to use the [wasmtime-wasi][`wasmtime-wasi`] crate to | ||
execute the same WASIp2 component from the example above. This example requires the `wasmtime` crate `async` feature to be enabled. | ||
|
||
This does not require any change to the WebAssembly module, it's just the WASI API host functions which are implemented to be async. See [wasmtime async support](https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.async_support). | ||
This does not require any change to the WASIp2 component, it's just the WASIp2 API host functions which are implemented to be async. See [wasmtime async support](https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.async_support). | ||
|
||
[code2]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasi-async/main.rs | ||
[`preview2`]: https://docs.rs/wasmtime-wasi/*/wasmtime_wasi/preview2/index.html | ||
[`wasmtime-wasi`]: https://docs.rs/wasmtime-wasi/*/wasmtime_wasi/preview2/index.html | ||
|
||
```rust,ignore | ||
{{#include ../examples/wasi-async/main.rs}} | ||
``` | ||
|
||
You can also [browse this source code online][code2] and clone the wasmtime | ||
repository to run the example locally. | ||
|
||
## Beyond Basics | ||
|
||
Please see these references: | ||
* The [book](https://component-model.bytecodealliance.org) for understanding the component model of WASIp2. | ||
* [Bindgen Examples](https://docs.rs/wasmtime/latest/wasmtime/component/bindgen_examples/index.html) for implementing WASIp2 hosts and guests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.