Skip to content

Commit 728769d

Browse files
committed
Added flat type encoding/decoding for events; reformatted validation events
1 parent 1606da1 commit 728769d

File tree

15 files changed

+316
-199
lines changed

15 files changed

+316
-199
lines changed

crates/environ/src/component/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ pub enum FlatType {
13431343
}
13441344

13451345
impl FlatType {
1346-
const fn byte_size(&self) -> u8 {
1346+
/// Return the size in bytes for this flat type
1347+
pub const fn byte_size(&self) -> u8 {
13471348
match self {
13481349
FlatType::I32 | FlatType::F32 => 4,
13491350
FlatType::I64 | FlatType::F64 => 8,

crates/environ/src/component/types_builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,14 @@ impl FlatTypesStorage {
879879
false
880880
}
881881
}
882+
883+
/// Generate an iterator over the 32-bit flat encoding
884+
pub fn iter32(&self) -> impl Iterator<Item = u8> {
885+
self.memory32
886+
.iter()
887+
.take(self.len as usize)
888+
.map(|f| f.byte_size())
889+
}
882890
}
883891

884892
impl FlatType {

crates/environ/src/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ impl WasmValType {
233233
}
234234
}
235235

236+
/// Return the number of bytes needed to represent this value
237+
pub fn byte_size(&self) -> u8 {
238+
match self {
239+
WasmValType::I32 | WasmValType::F32 => 4,
240+
WasmValType::I64 | WasmValType::F64 => 8,
241+
WasmValType::V128 | WasmValType::Ref(_) => 16,
242+
}
243+
}
244+
236245
/// Returns the contained reference type.
237246
///
238247
/// Panics if the value type is not a vmgcref

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ impl Func {
596596
|store| crate::Func::call_unchecked_raw(store, export, params_and_returns),
597597
params_and_returns.as_ref(),
598598
self.index,
599-
*self.instance.id().get(store.0).component().checksum(),
599+
self.abi_info(store.0).2,
600+
self.instance.id(),
600601
&mut store,
601602
)?;
602603
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ where
257257

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

260-
rr::component_hooks::record_replay_host_func_entry(storage, &ty, store.0)?;
260+
rr::component_hooks::record_replay_host_func_entry(storage, &types, &ty, store.0)?;
261261

262-
let ty = &types[ty];
263-
let param_tys = InterfaceType::Tuple(ty.params);
264-
let result_tys = InterfaceType::Tuple(ty.results);
262+
let func_ty = &types[ty];
263+
let param_tys = InterfaceType::Tuple(func_ty.params);
264+
let result_tys = InterfaceType::Tuple(func_ty.results);
265265

266266
let storage_type = if async_ {
267267
#[cfg(feature = "component-model-async")]
@@ -628,6 +628,8 @@ where
628628
);
629629
rr::component_hooks::record_host_func_return(
630630
unsafe { storage_as_slice_mut(storage) },
631+
cx.types,
632+
&ty,
631633
cx.store.0,
632634
)?;
633635
result
@@ -641,7 +643,7 @@ where
641643
ptr,
642644
);
643645
// Recording here is just for marking the return event
644-
rr::component_hooks::record_host_func_return(&[], cx.store.0)?;
646+
rr::component_hooks::record_host_func_return(&[], cx.types, &ty, cx.store.0)?;
645647
result
646648
}
647649
}
@@ -811,7 +813,7 @@ where
811813

812814
let types = instance.id().get(store.0).component().types().clone();
813815

814-
rr::component_hooks::record_replay_host_func_entry(storage, &ty, store.0)?;
816+
rr::component_hooks::record_replay_host_func_entry(storage, &types, &ty, store.0)?;
815817

816818
let func_ty = &types[ty];
817819
let param_tys = &types[func_ty.params];
@@ -931,7 +933,12 @@ where
931933
)?;
932934
}
933935
assert!(dst.next().is_none());
934-
rr::component_hooks::record_host_func_return(storage, cx.store.0)?;
936+
rr::component_hooks::record_host_func_return(
937+
storage,
938+
cx.types,
939+
&InterfaceType::Tuple(func_ty.results),
940+
cx.store.0,
941+
)?;
935942
} else {
936943
let ret_ptr = unsafe { storage[ret_index].assume_init_ref() };
937944
let mut ptr = validate_inbounds_dynamic(&result_tys.abi, cx.as_slice(), ret_ptr)?;
@@ -945,7 +952,12 @@ where
945952
)?;
946953
}
947954
// Recording here is just for marking the return event
948-
rr::component_hooks::record_host_func_return(&[], cx.store.0)?;
955+
rr::component_hooks::record_host_func_return(
956+
&[],
957+
cx.types,
958+
&InterfaceType::Tuple(func_ty.results),
959+
cx.store.0,
960+
)?;
949961
}
950962

951963
unsafe {

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ use crate::component::matching::InstanceType;
44
use crate::component::resources::{HostResourceData, HostResourceIndex, HostResourceTables};
55
use crate::component::{Instance, ResourceType};
66
use crate::prelude::*;
7+
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
8+
use crate::rr::component_events::ResultEvent;
9+
#[cfg(feature = "rr-component")]
10+
use crate::rr::component_hooks::ReplayLoweringPhase;
11+
use crate::rr::{ConstMemorySliceCell, MemorySliceCell};
712
#[cfg(feature = "rr-component")]
813
use crate::rr::{
914
RREvent, RecordBuffer, ReplayError, Replayer, component_events::ReallocEntryEvent,
1015
};
1116
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
1217
use crate::rr::{Validate, component_events::ReallocReturnEvent};
13-
#[cfg(feature = "rr-component")]
14-
use crate::rr::component_hooks::ReplayLoweringPhase;
15-
use crate::rr::{ConstMemorySliceCell, MemorySliceCell};
1618
use crate::runtime::vm::component::{
1719
CallContexts, ComponentInstance, InstanceFlags, ResourceTable, ResourceTables,
1820
};
@@ -404,9 +406,9 @@ impl<'a, T: 'static> LowerContext<'a, T> {
404406
})?;
405407
let result = self.realloc_inner(old, old_size, old_align, new_size);
406408
#[cfg(all(feature = "rr-component", feature = "rr-validate"))]
407-
self.store
408-
.0
409-
.record_event_validation(|| ReallocReturnEvent::from_anyhow_result(&result))?;
409+
self.store.0.record_event_validation(|| {
410+
ReallocReturnEvent(ResultEvent::from_anyhow_result(&result))
411+
})?;
410412
result
411413
}
412414

@@ -636,7 +638,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {
636638
if run_validate {
637639
_lower_stack.pop().ok_or(ReplayError::InvalidOrdering)?;
638640
}
639-
lowering_error = e.ret().map_err(Into::into).err();
641+
lowering_error = e.0.ret().map_err(Into::into).err();
640642
}
641643
RREvent::ComponentLowerMemoryReturn(e) => {
642644
#[cfg(feature = "rr-validate")]
@@ -645,7 +647,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {
645647
.pop()
646648
.ok_or(ReplayError::InvalidOrdering)?;
647649
}
648-
lowering_error = e.ret().map_err(Into::into).err();
650+
lowering_error = e.0.ret().map_err(Into::into).err();
649651
}
650652
RREvent::ComponentMemorySliceWrite(e) => {
651653
// The bounds check is performed here is required here (in the absence of
@@ -665,7 +667,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {
665667
{
666668
#[cfg(feature = "rr-validate")]
667669
if run_validate {
668-
lowering_error = _e.validate(&_realloc_stack.pop().unwrap()).err()
670+
lowering_error = _e.0.validate(&_realloc_stack.pop().unwrap()).err()
669671
}
670672
}
671673
RREvent::ComponentLowerFlatEntry(_) => {

crates/wasmtime/src/runtime/func.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,20 +2366,30 @@ impl HostContext {
23662366
let func = &state.func;
23672367

23682368
let func_type_index = state._ty.index();
2369-
let (num_params, num_results) = {
2369+
let (params_size, results_size) = {
23702370
let type_index = state._ty.index();
23712371
let wasm_func_subtype = caller.engine().signatures().borrow(type_index).unwrap();
23722372
let wasm_func_type = wasm_func_subtype.unwrap_func();
23732373
(
2374-
wasm_func_type.params().len(),
2375-
wasm_func_type.returns().len(),
2374+
wasm_func_type
2375+
.params()
2376+
.into_iter()
2377+
.map(|x| x.byte_size())
2378+
.collect::<Vec<_>>(),
2379+
wasm_func_type
2380+
.returns()
2381+
.into_iter()
2382+
.map(|x| x.byte_size())
2383+
.collect::<Vec<_>>(),
23762384
)
23772385
};
2386+
let (num_params, num_results) = (params_size.len(), results_size.len());
23782387

23792388
// Record/replay(validation) of the raw parameter arguments
23802389
// Don't need auto-assert GC store here since we aren't using P, just raw args
23812390
rr::core_hooks::record_replay_host_func_entry(
23822391
unsafe { &args.as_ref()[..num_params] },
2392+
params_size.as_slice(),
23832393
&func_type_index,
23842394
caller.store.0,
23852395
)?;
@@ -2425,6 +2435,7 @@ impl HostContext {
24252435
// Record the return values
24262436
rr::core_hooks::record_host_func_return(
24272437
unsafe { &args.as_ref()[..num_results] },
2438+
results_size.as_slice(),
24282439
&func_type_index,
24292440
caller.store.0,
24302441
)?;

crates/wasmtime/src/runtime/rr/core.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use serde::{Deserialize, Serialize};
66
// Use component events internally even without feature flags enabled
77
// so that [`RREvent`] has a well-defined serialization format, but export
88
// it for other modules only when enabled
9+
#[cfg(all(feature = "rr-validate", feature = "rr-component"))]
10+
pub use events::RRFuncArgVals;
911
#[cfg(any(feature = "rr-validate", feature = "rr-component"))]
1012
pub use events::Validate;
1113
#[cfg(feature = "rr-component")]
1214
pub use events::component_events;
13-
#[cfg(all(feature = "rr-validate", feature = "rr-component"))]
14-
pub use events::{RRFuncArgVals, func_argvals_from_raw_slice};
1515
use events::{common_events, component_events as __component_events};
1616
pub use events::{core_events, marker_events};
1717
pub use io::{RecordWriter, ReplayReader};
@@ -535,20 +535,24 @@ mod tests {
535535
#[test]
536536
#[cfg(all(feature = "rr", feature = "rr-component"))]
537537
fn rr_buffers() -> Result<()> {
538+
use wasmtime_environ::component::FlatTypesStorage;
539+
538540
let record_settings = RecordSettings::default();
539541
let tmp = NamedTempFile::new()?;
540542
let tmppath = tmp.path().to_str().expect("Filename should be UTF-8");
541543

542-
let values = vec![ValRaw::i32(1), ValRaw::f32(2), ValRaw::i64(3)]
543-
.into_iter()
544-
.map(|x| MaybeUninit::new(x))
545-
.collect::<Vec<_>>();
544+
let values = vec![ValRaw::i32(1), ValRaw::f32(2), ValRaw::i64(3)];
545+
let flat = FlatTypesStorage::new();
546+
flat.push(FlatType::I32, FlatType::I32);
547+
flat.push(FlatType::F32, FlatType::F32);
548+
flat.push(FlatType::I64, FlatType::I64);
546549

547550
// Record values
548551
let mut recorder =
549552
RecordBuffer::new_recorder(Box::new(File::create(tmppath)?), record_settings)?;
550-
recorder
551-
.record_event(|| __component_events::HostFuncReturnEvent::new(values.as_slice()))?;
553+
recorder.record_event(|| {
554+
__component_events::HostFuncReturnEvent::new(values.as_slice(), flat)
555+
})?;
552556
recorder.flush()?;
553557

554558
let tmp = tmp.into_temp_path();

0 commit comments

Comments
 (0)