Skip to content

Commit b298f37

Browse files
authored
RR #2: Sha256 checksum for components (#12576)
* Add sha256 checksum for component for record/replay consistency * Move sha2 crate as workspace dependency * Run checksum digest only on recording configs * Fix CI error and restructure from_binary
1 parent bda02c1 commit b298f37

16 files changed

Lines changed: 105 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 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
@@ -437,6 +437,7 @@ arbtest = "0.3.2"
437437
rayon = "1.5.3"
438438
regex = "1.9.1"
439439
pin-project-lite = "0.2.14"
440+
sha2 = { version = "0.10.2", default-features = false }
440441

441442
# =============================================================================
442443
#

crates/cache/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ directories-next = "2.0"
1919
log = { workspace = true }
2020
serde = { workspace = true }
2121
serde_derive = { workspace = true }
22-
sha2 = "0.10.2"
22+
sha2 = { workspace = true, features = ['std'] }
2323
toml = { workspace = true }
2424
zstd = { version = "0.13.0", default-features = false }
2525
wasmtime-environ = { workspace = true }

crates/environ/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ wasmtime-component-util = { workspace = true, optional = true }
3939
wasmtime-core = { workspace = true }
4040
semver = { workspace = true, optional = true, features = ['serde'] }
4141
smallvec = { workspace = true, features = ['serde'] }
42+
sha2 = { workspace = true }
4243

4344
[dev-dependencies]
4445
clap = { workspace = true, features = ['default'] }
@@ -85,3 +86,6 @@ std = [
8586
'indexmap/std',
8687
'wasmtime-core/std',
8788
]
89+
rr = [
90+
"component-model"
91+
]

crates/environ/src/compile/module_artifacts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Definitions of runtime structures and metadata which are serialized into ELF
22
//! with `postcard` as part of a module's compilation process.
33
4+
use crate::WasmChecksum;
45
use crate::error::{Result, bail};
56
use crate::prelude::*;
67
use crate::{
@@ -120,6 +121,7 @@ impl<'a> ObjectBuilder<'a> {
120121
data,
121122
data_align,
122123
passive_data,
124+
wasm,
123125
..
124126
} = translation;
125127

@@ -220,6 +222,7 @@ impl<'a> ObjectBuilder<'a> {
220222
has_wasm_debuginfo: self.tunables.parse_wasm_debuginfo,
221223
dwarf,
222224
},
225+
checksum: WasmChecksum::from_binary(wasm, self.tunables.recording),
223226
})
224227
}
225228

crates/environ/src/component/artifacts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! which are serialized with `bincode` into output ELF files.
33
44
use crate::{
5-
CompiledFunctionsTable, CompiledModuleInfo, PrimaryMap, StaticModuleIndex,
5+
CompiledFunctionsTable, CompiledModuleInfo, PrimaryMap, StaticModuleIndex, WasmChecksum,
66
component::{Component, ComponentTypes, TypeComponentIndex},
77
};
88
use serde_derive::{Deserialize, Serialize};
@@ -20,6 +20,8 @@ pub struct ComponentArtifacts {
2020
pub types: ComponentTypes,
2121
/// Serialized metadata about all included core wasm modules.
2222
pub static_modules: PrimaryMap<StaticModuleIndex, CompiledModuleInfo>,
23+
/// A checksum of the source Wasm binary from which the component was compiled.
24+
pub checksum: WasmChecksum,
2325
}
2426

2527
/// Runtime state that a component retains to support its operation.

crates/environ/src/module_artifacts.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use core::{fmt, u32};
88
use core::{iter, str};
99
use cranelift_entity::{EntityRef, PrimaryMap};
1010
use serde_derive::{Deserialize, Serialize};
11+
#[cfg(feature = "rr")]
12+
use sha2::{Digest, Sha256};
1113

1214
/// Description of where a function is located in the text section of a
1315
/// compiled image.
@@ -28,6 +30,42 @@ impl FunctionLoc {
2830
}
2931
}
3032

33+
/// The checksum of a Wasm binary.
34+
///
35+
/// Allows for features requiring the exact same Wasm Module (e.g. deterministic replay)
36+
/// to verify that the binary used matches the one originally compiled.
37+
#[derive(Copy, Clone, Default, PartialEq, Eq, Ord, PartialOrd, Debug, Serialize, Deserialize)]
38+
pub struct WasmChecksum([u8; 32]);
39+
40+
impl WasmChecksum {
41+
/// Construct a [`WasmChecksum`] from the given wasm binary, used primarily for integrity
42+
/// checks on compiled modules when recording configs are enabled. The checksum is not
43+
/// computed when recording is disabled to prevent pessimization of non-recorded compilations.
44+
#[cfg(feature = "rr")]
45+
pub fn from_binary(bin: &[u8], recording: bool) -> WasmChecksum {
46+
if recording {
47+
WasmChecksum(Sha256::digest(bin).into())
48+
} else {
49+
WasmChecksum::default()
50+
}
51+
}
52+
53+
/// This method requires the `rr` feature to actual compute a checksum. Since the `rr`
54+
/// feature is disabled, this only returns a default checksum value of all zeros.
55+
#[cfg(not(feature = "rr"))]
56+
pub fn from_binary(_: &[u8], _: bool) -> WasmChecksum {
57+
WasmChecksum::default()
58+
}
59+
}
60+
61+
impl core::ops::Deref for WasmChecksum {
62+
type Target = [u8; 32];
63+
64+
fn deref(&self) -> &Self::Target {
65+
&self.0
66+
}
67+
}
68+
3169
/// A builder for a `CompiledFunctionsTable`.
3270
pub struct CompiledFunctionsTableBuilder {
3371
inner: CompiledFunctionsTable,
@@ -548,6 +586,9 @@ pub struct CompiledModuleInfo {
548586

549587
/// Sorted list, by function index, of names we have for this module.
550588
pub func_names: Vec<FunctionName>,
589+
590+
/// Checksum of the source Wasm binary from which this module was compiled.
591+
pub checksum: WasmChecksum,
551592
}
552593

553594
/// The name of a function stored in the

crates/environ/src/tunables.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ define_tunables! {
144144
/// Whether any component model feature related to concurrency is
145145
/// enabled.
146146
pub concurrency_support: bool,
147+
148+
/// Whether recording in RR is enabled or not. This is used primarily
149+
/// to signal checksum computation for compiled artifacts.
150+
pub recording: bool,
147151
}
148152

149153
pub struct ConfigTunables {
@@ -220,6 +224,7 @@ impl Tunables {
220224
inlining_sum_size_threshold: 2000,
221225
debug_guest: false,
222226
concurrency_support: true,
227+
recording: false,
223228
}
224229
}
225230

crates/test-programs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ wit-bindgen = { workspace = true, features = ['default', 'async-spawn', 'inter-t
1717
libc = { workspace = true }
1818
futures = { workspace = true, default-features = false, features = ['alloc', 'async-await'] }
1919
url = { workspace = true }
20-
sha2 = "0.10.2"
20+
sha2 = { workspace = true, features = ['std'] }
2121
base64 = { workspace = true }
2222
wasip1 = { version = "1.0.0", default-features = true }
2323
wasip2 = "1.0.0"

crates/wasi-http/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ tracing-subscriber = { workspace = true }
5050
wasmtime = { workspace = true, features = ['default', 'anyhow'] }
5151
tokio = { workspace = true, features = ['fs', 'macros'] }
5252
futures = { workspace = true, default-features = false, features = ['alloc', 'async-await'] }
53-
sha2 = "0.10.2"
53+
sha2 = { workspace = true, features = ['std'] }
5454
base64 = { workspace = true }
5555
flate2 = { workspace = true }
5656
wasm-compose = { workspace = true }

0 commit comments

Comments
 (0)