Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
768 changes: 430 additions & 338 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
members = [
"components/debug",
"components/recorder"
]
, "crates/trace"]
[workspace.dependencies]
wit-bindgen = { version = "0.52.0", default-features = false, features = ["bitflags", "std", "macros"] }
serde = { version = "1.0.226", features = ["derive"] }
serde_json = "1.0.145"

[package]
name = "proxy-component"
version = "0.1.0"
edition = "2024"

[dependencies]
trace = { path = "crates/trace" }
anyhow = "1.0.98"
clap = { version = "4.5.40", features = ["derive"] }
heck = "0.5.0"
prettyplease = "0.2.37"
quote = "1.0.41"
proc-macro2 = "1.0.103"
serde = "1.0.226"
serde_json = "1.0.145"
syn = { version = "2.0.108", features = ["visit-mut"] }
tempfile = "3.20.0"
#wit-bindgen-core = { git = "https://github.com/chenyan2002/wit-bindgen.git", branch = "proxy" }
#wit-bindgen-rust = { git = "https://github.com/chenyan2002/wit-bindgen.git", branch = "proxy" }
wit-bindgen-core = "0.52.0"
wit-component = "0.244.0"
wit-bindgen-core = "0.53.1"
wit-component = "0.245.0"

wasmtime = { version = "41.0.0", features = ["wave"], optional = true }
wasmtime-wasi = { version = "41.0.0", optional = true }
wasmtime = { version = "42.0.0", features = ["wave"], optional = true }
wasmtime-wasi = { version = "42.0.0", optional = true }

[features]
run = ["wasmtime", "wasmtime-wasi"]
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

all: build-components build-cli
build-cli:
cargo build --release
cargo build --all-features --release
build-components:
cargo build -p debug --target wasm32-wasip2 --release
cargo build -p recorder --target wasm32-wasip2 --release
Expand Down Expand Up @@ -31,6 +31,9 @@ run-record:
$(MAKE) run-viceroy URL=localhost:7676
target/release/proxy-component instrument -m replay $(WASM)
wasmtime --invoke 'start()' composed.wasm < trace.out
# test host replay
target/release/proxy-component instrument -m replay --use-host-recorder $(WASM)
target/release/proxy-component run composed.wasm --invoke 'start()' --trace trace.out

run-viceroy:
viceroy composed.wasm > trace.out & echo $$! > viceroy.pid
Expand Down
Binary file modified assets/debug.wasm
Binary file not shown.
Binary file modified assets/recorder.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions components/recorder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2024"
crate-type = ["cdylib"]

[dependencies]
trace = { path = "../../crates/trace" }
serde_json.workspace = true
wit-bindgen.workspace = true
serde = { version = "1.0.226", features = ["derive"] }
serde_json = "1.0.145"
96 changes: 20 additions & 76 deletions components/recorder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,120 +4,64 @@ mod bindings {
world: "guest",
});
}
mod trace;

use trace::FuncCall;
use trace::Logger;
struct Component;
impl bindings::exports::proxy::recorder::record::Guest for Component {
fn record_args(method: Option<String>, args: Vec<String>, is_export: bool) {
let call = if is_export {
FuncCall::ExportArgs {
method: method.unwrap(),
args,
}
} else {
FuncCall::ImportArgs { method, args }
};
let mut logger = Logger::new();
let call = logger.record_args(method, args, is_export);
let json = serde_json::to_string(&call).unwrap();
println!("{json}");
}
fn record_ret(method: Option<String>, ret: Option<String>, is_export: bool) {
let call = if is_export {
FuncCall::ExportRet { method, ret }
} else {
FuncCall::ImportRet { method, ret }
};
let mut logger = Logger::new();
let call = logger.record_ret(method, ret, is_export);
let json = serde_json::to_string(&call).unwrap();
println!("{json}");
}
}

use std::cell::RefCell;
use std::collections::VecDeque;
thread_local! {
static TRACE: RefCell<Option<VecDeque<FuncCall>>> = RefCell::new(None);
static TRACE: RefCell<Option<Logger>> = RefCell::new(None);
}

fn load_trace() {
let load = TRACE.with_borrow(|v| v.is_none());
if load {
TRACE.with_borrow_mut(|v| {
use std::io::BufRead;
let mut res = VecDeque::new();
let f = std::io::stdin();
let reader = std::io::BufReader::new(f);
for line in reader.lines() {
let line = line.unwrap();
match serde_json::from_str::<FuncCall>(&line) {
Ok(item) => res.push_back(item),
Err(_) => continue,
}
}
*v = Some(res);
use std::io::Read;
let mut input = String::new();
std::io::stdin().read_to_string(&mut input).unwrap();
let mut logger = Logger::new();
logger.load_trace(&input);
*v = Some(logger);
});
}
}

impl bindings::exports::proxy::recorder::replay::Guest for Component {
fn replay_export() -> Option<(String, Vec<String>)> {
load_trace();
TRACE.with_borrow_mut(|v| {
let call = v.as_mut().unwrap().pop_front()?;
println!("export call: {}", call.to_string());
let FuncCall::ExportArgs { method, args } = call else {
panic!()
};
Some((method, args))
})
TRACE.with_borrow_mut(|v| v.as_mut().unwrap().replay_export())
}
fn assert_export_ret(assert_method: Option<String>, assert_ret: Option<String>) {
TRACE.with_borrow_mut(|v| {
if let Some(FuncCall::ExportRet { .. }) = v.as_mut().unwrap().front() {
let call = v.as_mut().unwrap().pop_front().unwrap();
println!("export ret: {}", call.to_string());
let FuncCall::ExportRet { method, ret } = call else {
panic!()
};
if let (Some(method), Some(assert_method)) = (method, assert_method) {
assert_eq!(method, assert_method);
}
assert_eq!(ret, assert_ret);
}
v.as_mut()
.unwrap()
.assert_export_ret(assert_method, assert_ret)
});
}
fn replay_import(
assert_method: Option<String>,
assert_args: Option<Vec<String>>,
) -> Option<String> {
TRACE.with_borrow_mut(|v| {
let mut call = v.as_mut().unwrap().pop_front().unwrap();
if let FuncCall::ImportArgs { method, args } = &call {
if let (Some(method), Some(assert_method)) = (method, assert_method) {
assert_eq!(method, &assert_method);
}
if let Some(assert_args) = &assert_args {
assert_eq!(args, assert_args);
}
println!("import call: {}", call.to_string());
if method
.as_ref()
.is_some_and(|m| m.starts_with("wasi:cli/exit"))
{
let code = if assert_args
.is_some_and(|args| args.get(0).is_some_and(|arg| arg.starts_with("err")))
{
1
} else {
0
};
std::process::exit(code);
}
call = v.as_mut().unwrap().pop_front().unwrap();
}
println!("import ret: {}", call.to_string());
let FuncCall::ImportRet { ret, .. } = call else {
panic!()
};
let (_, ret) = v
.as_mut()
.unwrap()
.replay_import(assert_method, assert_args, true);
ret
})
}
Expand Down
36 changes: 0 additions & 36 deletions components/recorder/src/trace.rs

This file was deleted.

8 changes: 8 additions & 0 deletions crates/trace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "trace"
version = "0.1.0"
edition = "2024"

[dependencies]
serde.workspace = true
serde_json.workspace = true
Loading
Loading