Skip to content

Commit 68deb95

Browse files
committed
Add traces in the wasm_runtime guest to track execution
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent d2da14e commit 68deb95

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

Cargo.lock

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wasm_runtime/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wasm_runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ hyperlight-guest = { version = "0.11.0" }
1717
wasmtime = { version = "36.0.2", default-features = false, features = [ "runtime", "custom-virtual-memory", "custom-native-signals", "component-model" ] }
1818
hyperlight-wasm-macro = { path = "../hyperlight_wasm_macro" }
1919
spin = "0.10.0"
20+
tracing = { version = "0.1.41", default-features = false, features = ["attributes", "log"] }
2021

2122
[build-dependencies]
2223
cfg_aliases = "0.2.1"

src/wasm_runtime/src/component.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition;
3030
use hyperlight_guest_bin::guest_function::register::register_function;
3131
use hyperlight_guest_bin::host_comm::call_host_function;
3232
use spin::Mutex;
33+
use tracing::instrument;
3334
use wasmtime::component::{Component, Instance, Linker};
3435
use wasmtime::{Config, Engine, Store};
3536

@@ -43,10 +44,12 @@ static CUR_INSTANCE: Mutex<Option<Instance>> = Mutex::new(None);
4344
hyperlight_wasm_macro::wasm_guest_bindgen!();
4445

4546
// dummy for compatibility with the module loading approach
47+
#[instrument(skip_all, level = "Info")]
4648
fn init_wasm_runtime(_function_call: &FunctionCall) -> Result<Vec<u8>> {
4749
Ok(get_flatbuffer_result::<i32>(0))
4850
}
4951

52+
#[instrument(skip_all, level = "Info")]
5053
fn load_component_common(engine: &Engine, component: Component) -> Result<()> {
5154
let mut store = Store::new(engine, ());
5255
let instance = (*CUR_LINKER.lock())
@@ -58,6 +61,7 @@ fn load_component_common(engine: &Engine, component: Component) -> Result<()> {
5861
Ok(())
5962
}
6063

64+
#[instrument(skip_all, level = "Info")]
6165
fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
6266
if let (
6367
ParameterValue::VecBytes(ref wasm_bytes),
@@ -79,6 +83,7 @@ fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
7983
}
8084
}
8185

86+
#[instrument(skip_all, level = "Info")]
8287
fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
8388
if let (ParameterValue::ULong(ref phys), ParameterValue::ULong(ref len), Some(ref engine)) = (
8489
&function_call.parameters.as_ref().unwrap()[0],
@@ -98,6 +103,7 @@ fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
98103
}
99104

100105
#[no_mangle]
106+
#[instrument(skip_all, level = "Info")]
101107
pub extern "C" fn hyperlight_main() {
102108
platform::register_page_fault_handler();
103109

@@ -133,6 +139,7 @@ pub extern "C" fn hyperlight_main() {
133139
}
134140

135141
#[no_mangle]
142+
#[instrument(skip_all, level = "Info")]
136143
pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
137144
Err(HyperlightGuestError::new(
138145
ErrorCode::GuestFunctionNotFound,

src/wasm_runtime/src/marshal.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
5757
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
5858
use hyperlight_guest::error::{HyperlightGuestError, Result};
5959
use spin::Mutex;
60+
use tracing::instrument;
6061
use wasmtime::{AsContextMut, Extern, Val};
6162

6263
// Global tracking for return value allocations that need to be freed on next VM entry
6364
static RETURN_VALUE_ALLOCATIONS: Mutex<Vec<i32>> = Mutex::new(Vec::new());
6465

6566
/// Track a return value allocation that should be freed on the next VM entry
67+
#[instrument(skip_all, level = "Trace")]
6668
fn track_return_value_allocation(addr: i32) {
6769
RETURN_VALUE_ALLOCATIONS.lock().push(addr);
6870
}
6971

7072
/// Free all tracked return value allocations from previous VM calls
73+
#[instrument(skip_all, level = "Trace")]
7174
pub fn free_return_value_allocations<C: AsContextMut>(
7275
ctx: &mut C,
7376
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -79,6 +82,7 @@ pub fn free_return_value_allocations<C: AsContextMut>(
7982
Ok(())
8083
}
8184

85+
#[instrument(skip_all, level = "Trace")]
8286
fn malloc<C: AsContextMut>(
8387
ctx: &mut C,
8488
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -96,6 +100,7 @@ fn malloc<C: AsContextMut>(
96100
Ok(addr)
97101
}
98102

103+
#[instrument(skip_all, level = "Trace")]
99104
fn free<C: AsContextMut>(
100105
ctx: &mut C,
101106
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -111,6 +116,7 @@ fn free<C: AsContextMut>(
111116
Ok(())
112117
}
113118

119+
#[instrument(skip_all, level = "Trace")]
114120
fn write<C: AsContextMut>(
115121
ctx: &mut C,
116122
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -132,6 +138,7 @@ fn write<C: AsContextMut>(
132138
Ok(())
133139
}
134140

141+
#[instrument(skip_all, level = "Trace")]
135142
fn read<C: AsContextMut>(
136143
ctx: &mut C,
137144
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -153,6 +160,7 @@ fn read<C: AsContextMut>(
153160
Ok(())
154161
}
155162

163+
#[instrument(skip_all, level = "Trace")]
156164
fn read_cstr<C: AsContextMut>(
157165
ctx: &mut C,
158166
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -196,6 +204,7 @@ fn read_cstr<C: AsContextMut>(
196204
/// For String and VecBytes parameter types, this allocates memory in the guest's memory space
197205
/// and returns a pointer. The guest function is responsible for freeing this memory when it is no
198206
/// longer needed using the `free` function exported from the guest module.
207+
#[instrument(skip_all, level = "Trace")]
199208
pub fn hl_param_to_val<C: AsContextMut>(
200209
mut ctx: C,
201210
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -230,6 +239,7 @@ pub fn hl_param_to_val<C: AsContextMut>(
230239
/// For String and VecBytes return types, the guest has allocated memory in its own memory space
231240
/// and returned pointers. The host takes ownership of these allocations and tracks them for
232241
/// automatic cleanup on the next VM entry to prevent memory leaks.
242+
#[instrument(skip_all, level = "Trace")]
233243
pub fn val_to_hl_result<C: AsContextMut>(
234244
mut ctx: C,
235245
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -284,6 +294,7 @@ pub fn val_to_hl_result<C: AsContextMut>(
284294
/// For String and VecBytes parameter types, the guest passes pointers to data in its own
285295
/// memory space. The guest retains ownership of these allocations and remains responsible
286296
/// for freeing them. This function only reads the data without taking ownership.
297+
#[instrument(skip_all, level = "Trace")]
287298
pub fn val_to_hl_param<'a, C: AsContextMut>(
288299
ctx: &mut C,
289300
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -339,6 +350,7 @@ pub fn val_to_hl_param<'a, C: AsContextMut>(
339350
/// For String and VecBytes return types, this allocates memory in the guest's memory space
340351
/// and returns a pointer. The guest owns these allocations and must free them when no longer needed
341352
/// using the `free` function exported from the guest module.
353+
#[instrument(skip_all, level = "Trace")]
342354
pub fn hl_return_to_val<C: AsContextMut>(
343355
ctx: &mut C,
344356
get_export: impl Fn(&mut C, &str) -> Option<Extern>,

0 commit comments

Comments
 (0)