Skip to content

Commit 9a66fcf

Browse files
committed
Factored out core RR interface into separate crate; fix rr tests to just disable async with component-model-async
1 parent 1fcfc40 commit 9a66fcf

File tree

20 files changed

+716
-565
lines changed

20 files changed

+716
-565
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ wasmtime-slab = { path = "crates/slab", version = "=40.0.0", package = 'wasmtime
272272
wasmtime-jit-icache-coherence = { path = "crates/jit-icache-coherence", version = "=40.0.0", package = 'wasmtime-internal-jit-icache-coherence' }
273273
wasmtime-wit-bindgen = { path = "crates/wit-bindgen", version = "=40.0.0", package = 'wasmtime-internal-wit-bindgen' }
274274
wasmtime-math = { path = "crates/math", version = "=40.0.0", package = 'wasmtime-internal-math' }
275+
wasmtime-rr = { path = "crates/rr", version = "40.0.0" }
275276
wasmtime-unwinder = { path = "crates/unwinder", version = "=40.0.0", package = 'wasmtime-internal-unwinder' }
276277
wasmtime-wizer = { path = "crates/wizer", version = "40.0.0" }
277278

crates/environ/src/component.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//!
1818
//! Note that this entire module is gated behind the `component-model` Cargo
1919
//! feature.
20+
use serde::{Deserialize, Serialize};
2021

2122
/// Canonical ABI-defined constant for the maximum number of "flat" parameters
2223
/// to a wasm function, or the maximum number of parameters a core wasm function
@@ -272,3 +273,7 @@ declare_builtin_index! {
272273
/// An index type for component builtin functions.
273274
pub struct ComponentBuiltinFunctionIndex: foreach_builtin_component_function;
274275
}
276+
277+
/// Return value from `resource.drop` builtin.
278+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
279+
pub struct ResourceDropRet(pub Option<u32>);

crates/rr/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "wasmtime-rr"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
documentation = "https://docs.rs/wasmtime-rr"
8+
description = "Record-replay interface for Wasm modules and components"
9+
license = "Apache-2.0 WITH LLVM-exception"
10+
repository = "https://github.com/bytecodealliance/wasmtime"
11+
12+
[lints]
13+
workspace = true
14+
15+
[package.metadata.docs.rs]
16+
all-features = true
17+
18+
[dependencies]
19+
wasmtime-environ = { workspace = true, features = ["component-model"] }
20+
serde = { workspace = true }
21+
postcard = { workspace = true }
22+
anyhow = { workspace = true }
23+
log = { workspace = true }
24+
cfg-if = { workspace = true }
25+
26+
[features]
27+
default = ["std"]
28+
std = ["postcard/use-std", "wasmtime-environ/std"]

crates/wasmtime/src/runtime/rr/core/events.rs renamed to crates/rr/src/events.rs

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use super::ReplayError;
2-
use crate::rr::FlatBytes;
3-
use crate::{AsContextMut, Val, prelude::*};
4-
use crate::{ValRaw, ValType};
1+
use crate::ReplayError;
2+
use anyhow::Result;
53
use core::fmt;
64
use serde::{Deserialize, Serialize};
7-
use wasmtime_environ::component::FlatTypesStorage;
85

96
/// A serde compatible representation of errors produced during execution
107
/// of certain events
@@ -26,12 +23,12 @@ pub trait EventError: core::error::Error + Send + Sync + 'static {
2623
#[derive(Serialize, Deserialize, Clone, PartialEq)]
2724
pub struct RRFuncArgVals {
2825
/// Flat data vector of bytes
29-
bytes: Vec<u8>,
26+
pub bytes: Vec<u8>,
3027
/// Descriptor vector of sizes of each flat types
3128
///
3229
/// The length of this vector equals the number of flat types,
3330
/// and the sum of this vector equals the length of `bytes`
34-
sizes: Vec<u8>,
31+
pub sizes: Vec<u8>,
3532
}
3633

3734
impl fmt::Debug for RRFuncArgVals {
@@ -58,60 +55,6 @@ impl fmt::Debug for RRFuncArgVals {
5855
}
5956
}
6057

61-
impl RRFuncArgVals {
62-
/// Construct [`RRFuncArgVals`] from raw value buffer and a flat size iterator
63-
#[inline]
64-
pub fn from_flat_iter<T>(args: &[T], flat: impl Iterator<Item = u8>) -> RRFuncArgVals
65-
where
66-
T: FlatBytes,
67-
{
68-
let mut bytes = Vec::new();
69-
let mut sizes = Vec::new();
70-
for (flat_size, arg) in flat.zip(args.iter()) {
71-
bytes.extend_from_slice(unsafe { &arg.bytes(flat_size) });
72-
sizes.push(flat_size);
73-
}
74-
RRFuncArgVals { bytes, sizes }
75-
}
76-
77-
/// Construct [`RRFuncArgVals`] from raw value buffer and a [`FlatTypesStorage`]
78-
#[inline]
79-
pub fn from_flat_storage<T>(args: &[T], flat: FlatTypesStorage) -> RRFuncArgVals
80-
where
81-
T: FlatBytes,
82-
{
83-
RRFuncArgVals::from_flat_iter(args, flat.iter32())
84-
}
85-
86-
/// Encode [`RRFuncArgVals`] back into raw value buffer
87-
#[inline]
88-
pub fn into_raw_slice<T>(self, raw_args: &mut [T])
89-
where
90-
T: FlatBytes,
91-
{
92-
let mut pos = 0;
93-
for (flat_size, dst) in self.sizes.into_iter().zip(raw_args.iter_mut()) {
94-
*dst = T::from_bytes(&self.bytes[pos..pos + flat_size as usize]);
95-
pos += flat_size as usize;
96-
}
97-
}
98-
99-
/// Generate a vector of [`crate::Val`] from [`RRFuncArgVals`] and [`ValType`]s
100-
#[inline]
101-
pub fn to_val_vec(self, mut store: impl AsContextMut, val_types: Vec<ValType>) -> Vec<Val> {
102-
let mut pos = 0;
103-
let mut vals = Vec::new();
104-
for (flat_size, val_type) in self.sizes.into_iter().zip(val_types.into_iter()) {
105-
let raw = ValRaw::from_bytes(&self.bytes[pos..pos + flat_size as usize]);
106-
// SAFETY: The safety contract here is the same as that of [`Val::from_raw`].
107-
// The caller must ensure that raw has the type provided.
108-
vals.push(unsafe { Val::from_raw(&mut store, raw, val_type) });
109-
pos += flat_size as usize;
110-
}
111-
vals
112-
}
113-
}
114-
11558
/// Trait signifying types that can be validated on replay
11659
///
11760
/// All `PartialEq` types are directly validatable with themselves.
@@ -234,7 +177,6 @@ macro_rules! event_error_types {
234177
/// Marker events should be injectable at any point in a record
235178
/// trace without impacting functional correctness of replay
236179
pub mod marker_events {
237-
use crate::prelude::*;
238180
use serde::{Deserialize, Serialize};
239181

240182
/// A Nop event

crates/wasmtime/src/runtime/rr/core/events/common_events.rs renamed to crates/rr/src/events/common_events.rs

File renamed without changes.

crates/wasmtime/src/runtime/rr/core/events/component_events.rs renamed to crates/rr/src/events/component_events.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
//! Module comprising of component model wasm events
22
33
use super::*;
4-
use crate::component::ComponentInstanceId;
5-
use crate::vm::component::libcalls::ResourceDropRet;
64
use wasmtime_environ::{
75
self, WasmChecksum,
8-
component::{ExportIndex, InterfaceType},
6+
component::{ExportIndex, InterfaceType, ResourceDropRet},
97
};
108

11-
/// Beginning marker for a Wasm component function call from host
9+
/// Representation of a component instance identifier during record/replay.
10+
///
11+
/// This ID is tied to component instantiation events in the trace, and used
12+
/// during replay to refer to specific component instances.
13+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
14+
pub struct RRComponentInstanceId(pub u32);
15+
16+
/// Beginning marker for a Wasm component function call from host.
1217
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1318
pub struct WasmFuncBeginEvent {
14-
/// Instance ID for the component instance
15-
pub instance: ComponentInstanceId,
16-
/// Export index for the invoked function
17-
pub func_idx: ExportIndex,
19+
/// Instance ID for the component instance.
20+
pub instance: RRComponentInstanceId,
21+
/// Export index for the invoked function.
22+
pub func_index: ExportIndex,
1823
}
1924

20-
/// A [`Component`] instantiatation event
25+
/// A instantiatation event for a Wasm component.
2126
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
2227
pub struct InstantiationEvent {
2328
/// Checksum of the bytecode used to instantiate the component
2429
pub component: WasmChecksum,
25-
/// Instance ID for the instantiated component
26-
pub instance: ComponentInstanceId,
30+
/// Instance ID for the instantiated component.
31+
pub instance: RRComponentInstanceId,
2732
}
2833

29-
/// A call to `post_return` (after the function call)
34+
/// A call to `post_return` (after the function call).
3035
#[derive(Debug, Clone, Serialize, Deserialize)]
3136
pub struct PostReturnEvent {
32-
/// Instance ID for the component instance
33-
pub instance: ComponentInstanceId,
34-
/// Export index for the function on which post_return is invoked
35-
pub func_idx: ExportIndex,
37+
/// Instance ID for the component instance.
38+
pub instance: RRComponentInstanceId,
39+
/// Export index for the function on which post_return is invoked.
40+
pub func_index: ExportIndex,
3641
}
3742

38-
/// A call event from Host into a Wasm component function
43+
/// A call event from Host into a Wasm component function.
3944
#[derive(Debug, Clone, Serialize, Deserialize)]
4045
pub struct WasmFuncEntryEvent {
41-
/// Raw values passed across call boundary
46+
/// Raw values passed across call boundary.
4247
pub args: RRFuncArgVals,
4348
}
4449

45-
/// A reallocation call event in the Component Model canonical ABI
46-
///
47-
/// Usually performed during lowering of complex [`ComponentType`]s to Wasm
50+
/// A reallocation call event in the Wasm Component Model canonical ABI.
4851
#[derive(Debug, Clone, Serialize, Deserialize)]
4952
pub struct ReallocEntryEvent {
5053
pub old_addr: usize,
@@ -53,13 +56,13 @@ pub struct ReallocEntryEvent {
5356
pub new_size: usize,
5457
}
5558

56-
/// Entry to a type lowering invocation to flat destination
59+
/// Entry to a type lowering invocation to flat destination.
5760
#[derive(Debug, Clone, Serialize, Deserialize)]
5861
pub struct LowerFlatEntryEvent {
5962
pub ty: InterfaceType,
6063
}
6164

62-
/// Entry to type lowering invocation to destination in memory
65+
/// Entry to type lowering invocation to destination in memory.
6366
#[derive(Debug, Clone, Serialize, Deserialize)]
6467
pub struct LowerMemoryEntryEvent {
6568
pub ty: InterfaceType,
@@ -69,6 +72,7 @@ pub struct LowerMemoryEntryEvent {
6972
/// A write to a mutable slice of Wasm linear memory by the host. This is the
7073
/// fundamental representation of host-written data to Wasm and is usually
7174
/// performed during lowering of a [`ComponentType`].
75+
///
7276
/// Note that this currently signifies a single mutable operation at the smallest granularity
7377
/// on a given linear memory slice. These can be optimized and coalesced into
7478
/// larger granularity operations in the future at either the recording or the replay level.
@@ -85,18 +89,20 @@ event_error_types! {
8589
pub struct BuiltinError(..)
8690
}
8791

92+
/// Return from a reallocation call in the Component Model canonical ABI, providing
93+
/// the address of allocation if successful.
8894
#[derive(Debug, Clone, Serialize, Deserialize)]
8995
pub struct ReallocReturnEvent(pub ResultEvent<usize, ReallocError>);
9096

91-
/// Return from type lowering to flat destination
97+
/// Return from type lowering to flat destination.
9298
#[derive(Debug, Clone, Serialize, Deserialize)]
9399
pub struct LowerFlatReturnEvent(pub ResultEvent<(), LowerFlatError>);
94100

95-
/// Return from type lowering to destination in memory
101+
/// Return from type lowering to destination in memory.
96102
#[derive(Debug, Clone, Serialize, Deserialize)]
97103
pub struct LowerMemoryReturnEvent(pub ResultEvent<(), LowerMemoryError>);
98104

99-
// Macro to generate RR events from the builtin descriptions
105+
// Macro to generate RR events from the builtin descriptions.
100106
macro_rules! builtin_events {
101107
// Main rule matching component function definitions
102108
(
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Module comprising of core wasm events
2+
use super::*;
3+
use wasmtime_environ::{EntityIndex, FuncIndex, WasmChecksum};
4+
5+
/// Representation of a Wasm module instance identifier during record/replay.
6+
///
7+
/// This ID is tied to module instantiation events in the trace, and used
8+
/// during replay to refer to specific module instances.
9+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
10+
pub struct RRModuleInstanceId(pub u32);
11+
12+
/// Representation of a Wasm module function index during record/replay.
13+
///
14+
/// This index is used to identify target call functions within a module during replay.
15+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
16+
pub struct RRModuleFuncIndex(pub u32);
17+
18+
impl Into<FuncIndex> for RRModuleFuncIndex {
19+
fn into(self) -> FuncIndex {
20+
FuncIndex::from_u32(self.0)
21+
}
22+
}
23+
24+
impl Into<EntityIndex> for RRModuleFuncIndex {
25+
fn into(self) -> EntityIndex {
26+
EntityIndex::from(Into::<FuncIndex>::into(self))
27+
}
28+
}
29+
30+
impl From<FuncIndex> for RRModuleFuncIndex {
31+
fn from(f: FuncIndex) -> Self {
32+
RRModuleFuncIndex(f.as_u32())
33+
}
34+
}
35+
36+
/// A core Wasm module instantiatation event.
37+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
38+
pub struct InstantiationEvent {
39+
/// Checksum of the bytecode used to instantiate the module.
40+
pub module: WasmChecksum,
41+
/// Instance ID for the instantiated module.
42+
pub instance: RRModuleInstanceId,
43+
}
44+
45+
/// A call event from Host into a core Wasm function.
46+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
47+
pub struct WasmFuncEntryEvent {
48+
/// Instance ID for the instantiated module.
49+
pub instance: RRModuleInstanceId,
50+
/// Function index of callee within the module.
51+
pub func_index: RRModuleFuncIndex,
52+
/// Raw values passed across call boundary
53+
pub args: RRFuncArgVals,
54+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::prelude::*;
1+
use anyhow::Result;
22
use core::any::Any;
33
use postcard;
44
use serde::{Deserialize, Serialize};
@@ -133,7 +133,7 @@ cfg_if::cfg_if! {
133133
/// Serialize and write `value` to a `RecordWriter`
134134
///
135135
/// Currently uses `postcard` serializer
136-
pub(super) fn to_record_writer<T, W>(value: &T, writer: &mut W) -> Result<()>
136+
pub fn to_record_writer<T, W>(value: &T, writer: &mut W) -> Result<()>
137137
where
138138
T: Serialize + ?Sized,
139139
W: RecordWriter + ?Sized,
@@ -154,7 +154,7 @@ where
154154
///
155155
/// Currently uses `postcard` deserializer, with optional scratch
156156
/// buffer to deserialize into
157-
pub(super) fn from_replay_reader<'a, T, R>(reader: &'a mut R, scratch: &'a mut [u8]) -> Result<T>
157+
pub fn from_replay_reader<'a, T, R>(reader: &'a mut R, scratch: &'a mut [u8]) -> Result<T>
158158
where
159159
T: Deserialize<'a>,
160160
R: ReplayReader + ?Sized,

0 commit comments

Comments
 (0)