Skip to content

Commit

Permalink
No longer error when runtime has custom sections mismatch (#607)
Browse files Browse the repository at this point in the history
* No longer error when runtime has custom sections mismatch

* Add regression test

* Rustfmt

* PR link
  • Loading branch information
tomaka authored May 25, 2023
1 parent 8f6e978 commit e896cee
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
47 changes: 43 additions & 4 deletions lib/src/executor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ use tiny_keccak::Hasher as _;

pub mod runtime_version;

pub use runtime_version::{CoreVersion, CoreVersionError, CoreVersionRef};
pub use runtime_version::{
CoreVersion, CoreVersionApisFromSliceErr, CoreVersionError, CoreVersionRef,
FindEncodedEmbeddedRuntimeVersionApisError,
};
pub use trie::TrieEntryVersion;
pub use vm::HeapPages;
pub use zstd::Error as ModuleFormatError;
Expand Down Expand Up @@ -284,8 +287,31 @@ impl HostVmPrototype {
// initialization. `Ok(None)` can also be returned, in which case the sections are
// missing, and we will instead try to retrieve the version through a runtime call later
// down this function.
let runtime_version = runtime_version::find_embedded_runtime_version(&module_bytes)
.map_err(NewErr::RuntimeVersion)?;
// In the case of `CustomSectionsPresenceMismatch`, indicating that one section is present
// but not the other, we must ignore the custom sections. This is necessary due to some
// historical accidents.
let runtime_version = match runtime_version::find_embedded_runtime_version(&module_bytes) {
Ok(Some(r)) => Some(r),
Ok(None) => None,
Err(
runtime_version::FindEmbeddedRuntimeVersionError::CustomSectionsPresenceMismatch,
) => None,
Err(runtime_version::FindEmbeddedRuntimeVersionError::FindSections(err)) => {
return Err(NewErr::RuntimeVersion(
FindEmbeddedRuntimeVersionError::FindSections(err),
))
}
Err(runtime_version::FindEmbeddedRuntimeVersionError::RuntimeApisDecode(err)) => {
return Err(NewErr::RuntimeVersion(
FindEmbeddedRuntimeVersionError::RuntimeApisDecode(err),
))
}
Err(runtime_version::FindEmbeddedRuntimeVersionError::RuntimeVersionDecode) => {
return Err(NewErr::RuntimeVersion(
FindEmbeddedRuntimeVersionError::RuntimeVersionDecode,
))
}
};

// Initialize the virtual machine.
// Each symbol requested by the Wasm runtime will be put in `registered_functions`. Later,
Expand Down Expand Up @@ -3659,7 +3685,7 @@ pub enum NewErr {
VirtualMachine(vm::NewErr),
/// Error while finding the runtime-version-related sections in the Wasm blob.
#[display(fmt = "Error in runtime spec Wasm sections: {_0}")]
RuntimeVersion(runtime_version::FindEmbeddedRuntimeVersionError),
RuntimeVersion(FindEmbeddedRuntimeVersionError),
/// Error while calling `Core_version` to determine the runtime version.
#[display(fmt = "Error while calling Core_version: {_0}")]
CoreVersion(CoreVersionError),
Expand All @@ -3670,6 +3696,19 @@ pub enum NewErr {
MemoryMaxSizeTooLow,
}

/// Error while determining .
#[derive(Debug, derive_more::Display, Clone)]
pub enum FindEmbeddedRuntimeVersionError {
/// Error while finding the custom section.
#[display(fmt = "{_0}")]
FindSections(FindEncodedEmbeddedRuntimeVersionApisError),
/// Error while decoding the runtime version.
RuntimeVersionDecode,
/// Error while decoding the runtime APIs.
#[display(fmt = "{_0}")]
RuntimeApisDecode(CoreVersionApisFromSliceErr),
}

/// Error that can happen when starting a VM.
#[derive(Debug, Clone, derive_more::From, derive_more::Display)]
pub enum StartErr {
Expand Down
17 changes: 17 additions & 0 deletions lib/src/executor/host/tests/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,21 @@ fn host_function_bad_signature() {
}
}

#[test]
fn rococo_genesis_works() {
// The Rococo genesis runtime has the particularity that it has a `runtime_apis` custom
// section but no `runtime_version` custom section.
let module_bytes = &include_bytes!("./rococo-genesis.wasm")[..];

for exec_hint in ExecHint::available_engines() {
assert!(HostVmPrototype::new(Config {
allow_unresolved_imports: true,
exec_hint,
heap_pages: HeapPages::new(1024),
module: &module_bytes,
})
.is_ok());
}
}

// TODO: add tests for the runtime version gathering after clarifying the errors in host.rs
Binary file added lib/src/executor/host/tests/rococo-genesis.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

- When a runtime contains a `runtime_apis` custom section but no `runtime_version` custom section, or vice-versa, smoldot now falls back to calling `Core_version`. ([#607](https://github.com/smol-dot/smoldot/pull/607))
- Fix panic when the checkpoint in the chain specification is invalid, which can normally only happen if the checkpoint was modified manually. ([#603](https://github.com/smol-dot/smoldot/pull/603))
- Fix panic when the checkpoint in the chain specification contains zero or one Babe epochs, which can happen if the checkpoint was generated before any block was authored. ([#603](https://github.com/smol-dot/smoldot/pull/603))

Expand Down

0 comments on commit e896cee

Please sign in to comment.