Skip to content

Commit

Permalink
feat: use refactored walrus API
Browse files Browse the repository at this point in the history
After the work done upstreaming operations used in `src/walrus_ops.rs`
to walrus, the code for WASI-Virt can be updated to use the new APIs
and reduce custom code.

This commit updates the WASI-Virt codebase to use the upstreamed
functions and adds some light refactors to the more mechanical pieces
of code for stubbing/denying/stripping.

see: bytecodealliance#20

Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
  • Loading branch information
vados-cosmonic committed Oct 18, 2023
1 parent 4f416bd commit c78cb79
Show file tree
Hide file tree
Showing 14 changed files with 1,347 additions and 1,336 deletions.
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ members = [
"tests/components/do-everything",
"tests/components/file-read",
"tests/components/get-env",
"tests/components/stdio"
"tests/components/stdio",
]

[profile.release]
Expand All @@ -33,7 +33,8 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
toml = "0.7"
walrus = "0.20.1"
# TODO: use published version of walrus w/ improved ergonomics
walrus = { git = "https://github.com/vados-cosmonic/walrus", branch = "refactor/improve-ergonomics-of-fn-replace" }
wasm-compose = "0.4.2"
wasm-metadata = "0.10.5"
wasm-opt = "0.114.1"
Expand All @@ -45,12 +46,14 @@ anyhow = "1"
[dev-dependencies]
anyhow = "1"
cap-std = "1.0.12"
heck = { version = "0.4" }
heck = { version = "0.4" }
tokio = { version = "1.30.0", features = ["macros"] }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"] }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = [
"component-model",
] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime" }
wasmparser = "0.113.1"

[workspace.dependencies]
anyhow = "1"
wit-bindgen = "0.12.0"
wit-bindgen = "0.12.0"
13 changes: 6 additions & 7 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::walrus_ops::{
bump_stack_global, get_exported_func, get_memory_id, remove_exported_func,
};
use anyhow::{bail, Result};
use std::collections::HashMap;
use walrus::{
ActiveData, ActiveDataLocation, DataKind, ElementKind, FunctionBuilder, FunctionId,
FunctionKind, InitExpr, Module, ValType,
};

use crate::walrus_ops::bump_stack_global;

/// Data section
/// Because data is stack-allocated we create a corresponding byte vector as large
/// as the stack, zero fill it then populate it backwards from the
Expand Down Expand Up @@ -115,7 +114,7 @@ impl Data {
}
pub fn finish(mut self, module: &mut Module) -> Result<()> {
// stack embedding
let memory = get_memory_id(module)?;
let memory = module.get_memory_id()?;
let rem = (self.stack_start - self.stack_ptr) % 8;
if rem != 0 {
self.stack_ptr -= 8 - rem;
Expand All @@ -132,7 +131,7 @@ impl Data {
// passive segment embedding
// we create one function for each passive segment, due to
if self.passive_segments.len() > 0 {
let alloc_fid = get_exported_func(module, "cabi_realloc")?;
let alloc_fid = module.exports.get_func_by_name("cabi_realloc")?;

let offset_local = module.locals.add(ValType::I32);
let len_local = module.locals.add(ValType::I32);
Expand Down Expand Up @@ -206,13 +205,13 @@ impl Data {
.call_indirect(passive_fn_alloc_type, passive_tid);

// update the existing passive_alloc function export with the new function body
let passive_alloc_fid = get_exported_func(module, "passive_alloc")?;
let passive_alloc_fid = module.exports.get_func_by_name("passive_alloc")?;
let passive_alloc_func = module.funcs.get_mut(passive_alloc_fid);
passive_alloc_func.kind =
FunctionKind::Local(builder.local_func(vec![passive_idx, offset_local, len_local]));
}

remove_exported_func(module, "passive_alloc")?;
module.exports.delete_func_by_name("passive_alloc")?;

Ok(())
}
Expand Down
Loading

0 comments on commit c78cb79

Please sign in to comment.