Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Remove deprecated host functions (#5038)
Browse files Browse the repository at this point in the history
Sadly we need to keep one function `ext_blake2_256`. This function is
manually defined in `sp-core`.
  • Loading branch information
bkchr authored Feb 27, 2020
1 parent 6e6d06c commit 06fae63
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1,139 deletions.
941 changes: 0 additions & 941 deletions client/executor/src/deprecated_host_interface.rs

This file was deleted.

3 changes: 0 additions & 3 deletions client/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
#![warn(missing_docs)]
#![recursion_limit="128"]

#[macro_use]
mod wasm_utils;
#[macro_use]
mod native_executor;
pub mod deprecated_host_interface;
mod wasm_runtime;
#[cfg(test)]
mod integration_tests;
Expand Down
4 changes: 0 additions & 4 deletions client/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ impl<D: NativeExecutionDispatch> NativeExecutor<D> {
/// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided.
pub fn new(fallback_method: WasmExecutionMethod, default_heap_pages: Option<u64>) -> Self {
let mut host_functions = sp_io::SubstrateHostFunctions::host_functions();
// Add the old and deprecated host functions as well, so that we support old wasm runtimes.
host_functions.extend(
crate::deprecated_host_interface::SubstrateExternals::host_functions(),
);

// Add the custom host functions provided by the user.
host_functions.extend(D::ExtendHostFunctions::host_functions());
Expand Down
173 changes: 0 additions & 173 deletions client/executor/src/wasm_utils.rs

This file was deleted.

51 changes: 51 additions & 0 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,56 @@ pub fn oom(_: core::alloc::Layout) -> ! {
#[cfg(feature = "std")]
pub type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher, u64>;

#[cfg(feature = "std")]
mod ext_blake2_256 {
use sp_wasm_interface::{Signature, Function, HostFunctions, ValueType, Value, FunctionContext};

/// There is a custom `extern function` in `sp_core::hasher` for `ext_blake2_256` hasher. This
/// custom extern was missed to remove and requires us to support this now. This type is a custom
/// implementation for the wasm function in native.
pub struct ExtBlake2_256;

impl HostFunctions for ExtBlake2_256 {
fn host_functions() -> Vec<&'static dyn Function> {
vec![&ExtBlake2_256]
}
}

impl Function for ExtBlake2_256 {
fn name(&self) -> &str {
"ext_blake2_256"
}

fn signature(&self) -> Signature {
Signature::new_with_args(&[ValueType::I32, ValueType::I32, ValueType::I32][..])
}

fn execute(
&self,
context: &mut dyn FunctionContext,
args: &mut dyn Iterator<Item = Value>,
) -> sp_wasm_interface::Result<Option<Value>> {
let data = args.next().and_then(|v| v.as_i32())
.ok_or_else(|| "`data` not present or not an `i32`")? as u32;
let len = args.next().and_then(|v| v.as_i32())
.ok_or_else(|| "`len` not present or not an `i32`")? as u32;
let out = args.next().and_then(|v| v.as_i32())
.ok_or_else(|| "`out` not present or not an `i32`")? as u32;

let result: [u8; 32] = if len == 0 {
sp_core::hashing::blake2_256(&[0u8; 0])
} else {
let mem = context.read_memory(data.into(), len)
.map_err(|_| "Invalid attempt to get data in ext_blake2_256")?;
sp_core::hashing::blake2_256(&mem)
};
context.write_memory(out.into(), &result)
.map_err(|_| "Invalid attempt to set result in ext_blake2_256")?;
Ok(None)
}
}
}

/// The host functions Substrate provides for the Wasm runtime environment.
///
/// All these host functions will be callable from inside the Wasm environment.
Expand All @@ -929,6 +979,7 @@ pub type SubstrateHostFunctions = (
logging::HostFunctions,
sandbox::HostFunctions,
crate::trie::HostFunctions,
ext_blake2_256::ExtBlake2_256,
);

#[cfg(test)]
Expand Down
27 changes: 10 additions & 17 deletions primitives/runtime-interface/test-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,6 @@ pub trait TestApi {
}
}

/// Two random external functions from the old runtime interface.
/// This ensures that we still inherently export these functions from the host and that we are still
/// compatible with old wasm runtimes.
#[cfg(not(feature = "std"))]
extern "C" {
pub fn ext_clear_storage(key_data: *const u8, key_len: u32);
pub fn ext_keccak_256(data: *const u8, len: u32, out: *mut u8);
}

/// Make sure the old runtime interface needs to be imported.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub fn force_old_runtime_interface_import() {
unsafe { ext_clear_storage(sp_std::ptr::null(), 0); }
unsafe { ext_keccak_256(sp_std::ptr::null(), 0, sp_std::ptr::null_mut()); }
}

/// This function is not used, but we require it for the compiler to include `sp-io`.
/// `sp-io` is required for its panic and oom handler.
#[no_mangle]
Expand Down Expand Up @@ -248,4 +231,14 @@ wasm_export_functions! {
}
assert_eq!(0, len);
}

fn test_ext_blake2_256() {
use sp_core::Hasher;

let data = "hey, hash me please!";
let hash = sp_core::Blake2Hasher::hash(data.as_bytes());

let expected = sp_io::hashing::blake2_256(data.as_bytes());
assert_eq!(&expected, hash.as_ref());
}
}
6 changes: 5 additions & 1 deletion primitives/runtime-interface/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fn call_wasm_method<HF: HostFunctionsT>(method: &str) -> TestExternalities {
(
HF,
sp_io::SubstrateHostFunctions,
sc_executor::deprecated_host_interface::SubstrateExternals
)
>(
method,
Expand Down Expand Up @@ -128,3 +127,8 @@ fn test_encoded_return_value_memory_is_freed() {
fn test_array_return_value_memory_is_freed() {
call_wasm_method::<HostFunctions>("test_array_return_value_memory_is_freed");
}

#[test]
fn test_ext_blake2_256() {
call_wasm_method::<HostFunctions>("test_ext_blake2_256");
}
6 changes: 6 additions & 0 deletions primitives/wasm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ impl<T: PointerType> Pointer<T> {
}
}

impl<T: PointerType> From<u32> for Pointer<T> {
fn from(ptr: u32) -> Self {
Pointer::new(ptr)
}
}

impl<T: PointerType> From<Pointer<T>> for u32 {
fn from(ptr: Pointer<T>) -> Self {
ptr.ptr
Expand Down

0 comments on commit 06fae63

Please sign in to comment.