Skip to content

Commit 3fed3d9

Browse files
committed
Host function record/replay hooks split up
1 parent d6892f3 commit 3fed3d9

File tree

4 files changed

+95
-57
lines changed

4 files changed

+95
-57
lines changed

crates/wasmtime/src/runtime/component/func/host.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,13 @@ where
257257

258258
let types = vminstance.component().types().clone();
259259

260-
rr::component_hooks::record_and_replay_validate_host_func_entry(storage, &types, &ty, store.0)?;
261-
262260
let func_ty = &types[ty];
263261
let param_tys = InterfaceType::Tuple(func_ty.params);
264262
let result_tys = InterfaceType::Tuple(func_ty.results);
265263

264+
rr::component_hooks::record_validate_host_func_entry(storage, &types, &ty, store.0)?;
265+
rr::component_hooks::replay_validate_host_func_entry(storage, &types, &ty, store.0)?;
266+
266267
let storage_type = if async_ {
267268
#[cfg(feature = "component-model-async")]
268269
{
@@ -373,7 +374,9 @@ where
373374
{
374375
match storage_type {
375376
#[cfg(feature = "component-model-async")]
376-
StorageType::Async(_) => unreachable!("`rr` should not be configurable with async"),
377+
StorageType::Async(_) => {
378+
unreachable!("`rr` should not be configurable with `component-model-async`")
379+
}
377380
StorageType::Sync(mut storage) => {
378381
unsafe {
379382
flags.set_may_leave(false);
@@ -813,13 +816,12 @@ where
813816

814817
let types = instance.id().get(store.0).component().types().clone();
815818

816-
rr::component_hooks::record_and_replay_validate_host_func_entry(storage, &types, &ty, store.0)?;
817-
818819
let func_ty = &types[ty];
819820
let param_tys = &types[func_ty.params];
820821
let result_tys = &types[func_ty.results];
821822

822823
if !store.0.replay_enabled() {
824+
rr::component_hooks::record_validate_host_func_entry(storage, &types, &ty, store.0)?;
823825
let mut params_and_results = Vec::new();
824826
let mut lift = &mut LiftContext::new(store.0.store_opaque_mut(), &options, instance);
825827
lift.enter_call();
@@ -967,9 +969,10 @@ where
967969
cx.exit_call()?;
968970
}
969971
} else {
972+
rr::component_hooks::replay_validate_host_func_entry(storage, &types, &ty, store.0)?;
970973
#[cfg(feature = "rr-component")]
971974
if async_ {
972-
unreachable!("`rr` should not be configurable with async");
975+
unreachable!("`rr` should not be configurable with `component-model-async`");
973976
} else {
974977
unsafe {
975978
flags.set_may_leave(false);

crates/wasmtime/src/runtime/func.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,19 +1287,17 @@ impl Func {
12871287
let nparams = ty.params().len();
12881288
val_vec.reserve(nparams + ty.results().len());
12891289

1290-
// Recording host function entry
12911290
let flat_params = ty
12921291
.params()
12931292
.into_iter()
12941293
.map(|x| x.to_wasm_type().byte_size());
12951294

1296-
rr::core_hooks::record_and_replay_validate_host_func_entry(
1297-
values_vec,
1298-
flat_params,
1299-
&mut caller.store.0,
1300-
)?;
1301-
13021295
if !caller.store.0.replay_enabled() {
1296+
rr::core_hooks::record_validate_host_func_entry(
1297+
values_vec,
1298+
flat_params,
1299+
&mut caller.store.0,
1300+
)?;
13031301
for (i, ty) in ty.params().enumerate() {
13041302
val_vec.push(unsafe { Val::from_raw(&mut caller.store, values_vec[i], ty) })
13051303
}
@@ -1324,6 +1322,11 @@ impl Func {
13241322
.map(|x| x.to_wasm_type().byte_size());
13251323
rr::core_hooks::record_host_func_return(values_vec, flat_results, &mut caller.store.0)?;
13261324
} else {
1325+
rr::core_hooks::replay_validate_host_func_entry(
1326+
values_vec,
1327+
flat_params,
1328+
&mut caller.store.0,
1329+
)?;
13271330
rr::core_hooks::replay_host_func_return(values_vec, &mut caller.store.0)?;
13281331
}
13291332

@@ -2436,15 +2439,13 @@ impl HostContext {
24362439
wasm_func_type.returns().into_iter().map(|x| x.byte_size()),
24372440
);
24382441

2439-
// Record/replay(validation) of the raw parameter arguments
2440-
// Don't need auto-assert GC store here since we aren't using P, just raw args
2441-
rr::core_hooks::record_and_replay_validate_host_func_entry(
2442-
unsafe { &args.as_ref()[..num_params] },
2443-
flat_size_params,
2444-
caller.store.0,
2445-
)?;
2446-
24472442
if !caller.store.0.replay_enabled() {
2443+
// Don't need auto-assert GC store here since we aren't using P, just raw args for recording
2444+
rr::core_hooks::record_validate_host_func_entry(
2445+
unsafe { &args.as_ref()[..num_params] },
2446+
flat_size_params,
2447+
caller.store.0,
2448+
)?;
24482449
let ret = 'ret: {
24492450
if let Err(trap) = caller.store.0.call_hook(CallHook::CallingHost) {
24502451
break 'ret R::fallible_from_error(trap);
@@ -2489,6 +2490,12 @@ impl HostContext {
24892490
caller.store.0,
24902491
)?;
24912492
} else {
2493+
rr::core_hooks::replay_validate_host_func_entry(
2494+
unsafe { &args.as_ref()[..num_params] },
2495+
flat_size_params,
2496+
caller.store.0,
2497+
)?;
2498+
24922499
// Replay the return values
24932500
rr::core_hooks::replay_host_func_return(
24942501
unsafe { &mut args.as_mut()[..num_results] },

crates/wasmtime/src/runtime/rr/hooks/component_hooks.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::ValRaw;
33
use crate::component::func::LowerContext;
44
#[cfg(feature = "rr-component")]
55
use crate::rr::ResultEvent;
6+
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
7+
use crate::rr::component_events::HostFuncEntryEvent;
68
#[cfg(feature = "rr-component")]
79
use crate::rr::component_events::{
810
HostFuncReturnEvent, LowerFlatReturnEvent, LowerMemoryReturnEvent, WasmFuncEntryEvent,
@@ -40,15 +42,15 @@ where
4042
{
4143
let _ = (args, type_idx, &types);
4244
#[cfg(feature = "rr-component")]
43-
{
45+
store.0.record_event(|| {
4446
let flat_params = types.flat_types_storage(
4547
&InterfaceType::Tuple(types[type_idx].params),
4648
MAX_FLAT_PARAMS,
4749
);
48-
store.0.record_event(|| WasmFuncEntryEvent {
50+
WasmFuncEntryEvent {
4951
args: RRFuncArgVals::from_flat_storage(args, flat_params),
50-
})?;
51-
}
52+
}
53+
})?;
5254
let result = wasm_call(store);
5355
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
5456
{
@@ -72,29 +74,32 @@ where
7274
return result;
7375
}
7476

75-
/// Record/replay hook operation for host function entry events
77+
/// Record hook operation for host function entry events
7678
#[inline]
77-
pub fn record_and_replay_validate_host_func_entry(
79+
pub fn record_validate_host_func_entry(
7880
args: &mut [MaybeUninit<ValRaw>],
7981
types: &Arc<ComponentTypes>,
8082
type_idx: &TypeFuncIndex,
8183
store: &mut StoreOpaque,
8284
) -> Result<()> {
8385
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
84-
{
85-
use crate::rr::component_events::HostFuncEntryEvent;
86-
let event = || {
87-
let flat_params = types.flat_types_storage(
88-
&InterfaceType::Tuple(types[*type_idx].params),
89-
MAX_FLAT_PARAMS,
90-
);
91-
HostFuncEntryEvent {
92-
args: RRFuncArgVals::from_flat_storage(args, flat_params),
93-
}
94-
};
95-
store.record_event_validation(|| event())?;
96-
store.next_replay_event_validation::<HostFuncEntryEvent, _, _>(|| event())?;
97-
}
86+
store.record_event_validation(|| create_host_func_entry_event(args, types, type_idx))?;
87+
let _ = (args, types, type_idx, store);
88+
Ok(())
89+
}
90+
91+
/// Replay hook operation for host function entry events
92+
#[inline]
93+
pub fn replay_validate_host_func_entry(
94+
args: &mut [MaybeUninit<ValRaw>],
95+
types: &Arc<ComponentTypes>,
96+
type_idx: &TypeFuncIndex,
97+
store: &mut StoreOpaque,
98+
) -> Result<()> {
99+
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
100+
store.next_replay_event_validation::<HostFuncEntryEvent, _, _>(|| {
101+
create_host_func_entry_event(args, types, type_idx)
102+
})?;
98103
let _ = (args, types, type_idx, store);
99104
Ok(())
100105
}
@@ -168,3 +173,19 @@ where
168173
.record_event(|| LowerFlatReturnEvent(ResultEvent::from_anyhow_result(&lower_result)))?;
169174
lower_result
170175
}
176+
177+
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
178+
#[inline(always)]
179+
fn create_host_func_entry_event(
180+
args: &mut [MaybeUninit<ValRaw>],
181+
types: &Arc<ComponentTypes>,
182+
type_idx: &TypeFuncIndex,
183+
) -> HostFuncEntryEvent {
184+
let flat_params = types.flat_types_storage(
185+
&InterfaceType::Tuple(types[*type_idx].params),
186+
MAX_FLAT_PARAMS,
187+
);
188+
HostFuncEntryEvent {
189+
args: RRFuncArgVals::from_flat_storage(args, flat_params),
190+
}
191+
}

crates/wasmtime/src/runtime/rr/hooks/core_hooks.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ where
6565
return result;
6666
}
6767

68-
/// Record and replay hook operation for host function entry events
68+
/// Record hook operation for host function entry events
6969
#[inline]
70-
pub fn record_and_replay_validate_host_func_entry<T>(
70+
pub fn record_validate_host_func_entry<T>(
7171
args: &[T],
7272
flat: impl Iterator<Item = u8>,
7373
store: &mut StoreOpaque,
@@ -77,20 +77,9 @@ where
7777
{
7878
let _ = (args, &flat, &store);
7979
#[cfg(all(feature = "rr", feature = "rr-validate"))]
80-
{
81-
// Record/replay the raw parameter args
82-
if store.replay_enabled() {
83-
store.next_replay_event_validation::<HostFuncEntryEvent, _, _>(|| {
84-
HostFuncEntryEvent {
85-
args: RRFuncArgVals::from_flat_iter(args, flat),
86-
}
87-
})?;
88-
} else {
89-
store.record_event_validation(|| HostFuncEntryEvent {
90-
args: RRFuncArgVals::from_flat_iter(args, flat),
91-
})?;
92-
}
93-
}
80+
store.record_event_validation(|| HostFuncEntryEvent {
81+
args: RRFuncArgVals::from_flat_iter(args, flat),
82+
})?;
9483
Ok(())
9584
}
9685

@@ -113,6 +102,24 @@ where
113102
Ok(())
114103
}
115104

105+
/// Replay hook operation for host function entry events
106+
#[inline]
107+
pub fn replay_validate_host_func_entry<T>(
108+
args: &[T],
109+
flat: impl Iterator<Item = u8>,
110+
store: &mut StoreOpaque,
111+
) -> Result<()>
112+
where
113+
T: FlatBytes,
114+
{
115+
let _ = (args, &flat, &store);
116+
#[cfg(all(feature = "rr", feature = "rr-validate"))]
117+
store.next_replay_event_validation::<HostFuncEntryEvent, _, _>(|| HostFuncEntryEvent {
118+
args: RRFuncArgVals::from_flat_iter(args, flat),
119+
})?;
120+
Ok(())
121+
}
122+
116123
/// Replay hook operation for host function return events
117124
#[inline]
118125
pub fn replay_host_func_return<T>(args: &mut [T], store: &mut StoreOpaque) -> Result<()>

0 commit comments

Comments
 (0)