Skip to content

Commit

Permalink
Add a PolkaVM-based executor (#3458)
Browse files Browse the repository at this point in the history
This PR adds a new PolkaVM-based executor to Substrate.

- The executor can now be used to actually run a PolkaVM-based runtime,
and successfully produces blocks.
- The executor is always compiled-in, but is disabled by default.
- The `SUBSTRATE_ENABLE_POLKAVM` environment variable must be set to `1`
to enable the executor, in which case the node will accept both WASM and
PolkaVM program blobs (otherwise it'll default to WASM-only). This is
deliberately undocumented and not explicitly exposed anywhere (e.g. in
the command line arguments, or in the API) to disincentivize anyone from
enabling it in production. If/when we'll move this into production usage
I'll remove the environment variable and do it "properly".
- I did not use our legacy runtime allocator for the PolkaVM executor,
so currently every allocation inside of the runtime will leak guest
memory until that particular instance is destroyed. The idea here is
that I will work on the polkadot-fellows/RFCs#4
which will remove the need for the legacy allocator under WASM, and that
will also allow us to use a proper non-leaking allocator under PolkaVM.
- I also did some minor cleanups of the WASM executor and deleted some
dead code.

No prdocs included since this is not intended to be an end-user feature,
but an unofficial experiment, and shouldn't affect any current
production user. Once this is production-ready a full Polkadot
Fellowship RFC will be necessary anyway.
  • Loading branch information
koute committed Mar 12, 2024
1 parent 7315a9b commit b0f34e4
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 300 deletions.
74 changes: 59 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ members = [
"substrate/client/db",
"substrate/client/executor",
"substrate/client/executor/common",
"substrate/client/executor/polkavm",
"substrate/client/executor/runtime-test",
"substrate/client/executor/wasmtime",
"substrate/client/informant",
Expand Down Expand Up @@ -543,8 +544,9 @@ extra-unused-type-parameters = { level = "allow", priority = 2 } # stylistic
default_constructed_unit_structs = { level = "allow", priority = 2 } # stylistic

[workspace.dependencies]
polkavm-linker = "0.8.2"
polkavm-derive = "0.8.0"
polkavm = "0.9.3"
polkavm-linker = "0.9.2"
polkavm-derive = "0.9.1"
log = { version = "0.4.20", default-features = false }
quote = { version = "1.0.33" }
serde = { version = "1.0.197", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions polkadot/node/core/pvf/common/src/executor_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use polkadot_primitives::{
use sc_executor_common::{
error::WasmError,
runtime_blob::RuntimeBlob,
wasm_runtime::{HeapAllocStrategy, InvokeMethod, WasmModule as _},
wasm_runtime::{HeapAllocStrategy, WasmModule as _},
};
use sc_executor_wasmtime::{Config, DeterministicStackLimit, Semantics, WasmtimeRuntime};
use sp_core::storage::{ChildInfo, TrackedStorageKey};
Expand Down Expand Up @@ -119,7 +119,7 @@ pub unsafe fn execute_artifact(

match sc_executor::with_externalities_safe(&mut ext, || {
let runtime = create_runtime_from_artifact_bytes(compiled_artifact_blob, executor_params)?;
runtime.new_instance()?.call(InvokeMethod::Export("validate_block"), params)
runtime.new_instance()?.call("validate_block", params)
}) {
Ok(Ok(ok)) => Ok(ok),
Ok(Err(err)) | Err(err) => Err(err),
Expand Down
1 change: 1 addition & 0 deletions substrate/client/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tracing = "0.1.29"

codec = { package = "parity-scale-codec", version = "3.6.1" }
sc-executor-common = { path = "common" }
sc-executor-polkavm = { path = "polkavm" }
sc-executor-wasmtime = { path = "wasmtime" }
sp-api = { path = "../../primitives/api" }
sp-core = { path = "../../primitives/core" }
Expand Down
1 change: 1 addition & 0 deletions substrate/client/executor/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ wasm-instrument = "0.4"
sc-allocator = { path = "../../allocator" }
sp-maybe-compressed-blob = { path = "../../../primitives/maybe-compressed-blob" }
sp-wasm-interface = { path = "../../../primitives/wasm-interface" }
polkavm = { workspace = true }

[features]
default = []
18 changes: 18 additions & 0 deletions substrate/client/executor/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ pub enum WasmError {
Other(String),
}

impl From<polkavm::ProgramParseError> for WasmError {
fn from(error: polkavm::ProgramParseError) -> Self {
WasmError::Other(error.to_string())
}
}

impl From<polkavm::Error> for WasmError {
fn from(error: polkavm::Error) -> Self {
WasmError::Other(error.to_string())
}
}

impl From<polkavm::Error> for Error {
fn from(error: polkavm::Error) -> Self {
Error::Other(error.to_string())
}
}

/// An error message with an attached backtrace.
#[derive(Debug)]
pub struct MessageWithBacktrace {
Expand Down
4 changes: 4 additions & 0 deletions substrate/client/executor/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ pub mod error;
pub mod runtime_blob;
pub mod util;
pub mod wasm_runtime;

pub(crate) fn is_polkavm_enabled() -> bool {
std::env::var_os("SUBSTRATE_ENABLE_POLKAVM").map_or(false, |value| value == "1")
}
Loading

0 comments on commit b0f34e4

Please sign in to comment.