@@ -8,6 +8,8 @@ use core::{fmt, u32};
88use core:: { iter, str} ;
99use cranelift_entity:: { EntityRef , PrimaryMap } ;
1010use 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`.
3270pub 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
0 commit comments