Add get_global for Sandbox (#4756)

* Add `get_global` for `Sandbox`

This pr adds `get_global` to retrieve a `global` variable from an
instantiated sandbox wasm blob.

* Bump `spec_version`

* Update primitives/wasm-interface/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* `get_global` -> `get_global_val`

Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
Co-authored-by: Gavin Wood <github@gavwood.com>
This commit is contained in:
Bastian Köcher
2020-01-29 16:24:40 +01:00
committed by GitHub
parent ae1e9002d7
commit 4c36143375
23 changed files with 275 additions and 195 deletions
@@ -126,7 +126,7 @@ macro_rules! define_func {
( < E: $ext_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
fn $name< E: $ext_ty >(
$ctx: &mut $crate::wasm::Runtime<E>,
args: &[sp_sandbox::TypedValue],
args: &[sp_sandbox::Value],
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> {
#[allow(unused)]
let mut args = args.iter();
@@ -196,7 +196,7 @@ mod tests {
use parity_wasm::elements::FunctionType;
use parity_wasm::elements::ValueType;
use sp_runtime::traits::Zero;
use sp_sandbox::{self, ReturnValue, TypedValue};
use sp_sandbox::{ReturnValue, Value};
use crate::wasm::tests::MockExt;
use crate::wasm::Runtime;
use crate::exec::Ext;
@@ -206,7 +206,7 @@ mod tests {
fn macro_unmarshall_then_body_then_marshall_value_or_trap() {
fn test_value(
_ctx: &mut u32,
args: &[sp_sandbox::TypedValue],
args: &[sp_sandbox::Value],
) -> Result<ReturnValue, sp_sandbox::HostError> {
let mut args = args.iter();
unmarshall_then_body_then_marshall!(
@@ -224,17 +224,17 @@ mod tests {
let ctx = &mut 0;
assert_eq!(
test_value(ctx, &[TypedValue::I32(15), TypedValue::I32(3)]).unwrap(),
ReturnValue::Value(TypedValue::I32(5)),
test_value(ctx, &[Value::I32(15), Value::I32(3)]).unwrap(),
ReturnValue::Value(Value::I32(5)),
);
assert!(test_value(ctx, &[TypedValue::I32(15), TypedValue::I32(0)]).is_err());
assert!(test_value(ctx, &[Value::I32(15), Value::I32(0)]).is_err());
}
#[test]
fn macro_unmarshall_then_body_then_marshall_unit() {
fn test_unit(
ctx: &mut u32,
args: &[sp_sandbox::TypedValue],
args: &[sp_sandbox::Value],
) -> Result<ReturnValue, sp_sandbox::HostError> {
let mut args = args.iter();
unmarshall_then_body_then_marshall!(
@@ -248,7 +248,7 @@ mod tests {
}
let ctx = &mut 0;
let result = test_unit(ctx, &[TypedValue::I32(2), TypedValue::I32(3)]).unwrap();
let result = test_unit(ctx, &[Value::I32(2), Value::I32(3)]).unwrap();
assert_eq!(result, ReturnValue::Unit);
assert_eq!(*ctx, 5);
}
@@ -263,7 +263,7 @@ mod tests {
Err(sp_sandbox::HostError)
}
});
let _f: fn(&mut Runtime<MockExt>, &[sp_sandbox::TypedValue])
let _f: fn(&mut Runtime<MockExt>, &[sp_sandbox::Value])
-> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> = ext_gas::<MockExt>;
}
@@ -282,7 +282,7 @@ mod tests {
#[test]
fn macro_unmarshall_then_body() {
let args = vec![TypedValue::I32(5), TypedValue::I32(3)];
let args = vec![Value::I32(5), Value::I32(3)];
let mut args = args.iter();
let ctx: &mut u32 = &mut 0;
@@ -17,7 +17,7 @@
use super::Runtime;
use crate::exec::Ext;
use sp_sandbox::{self, TypedValue};
use sp_sandbox::Value;
use parity_wasm::elements::{FunctionType, ValueType};
#[macro_use]
@@ -26,28 +26,28 @@ pub(crate) mod macros;
pub trait ConvertibleToWasm: Sized {
const VALUE_TYPE: ValueType;
type NativeType;
fn to_typed_value(self) -> TypedValue;
fn from_typed_value(_: TypedValue) -> Option<Self>;
fn to_typed_value(self) -> Value;
fn from_typed_value(_: Value) -> Option<Self>;
}
impl ConvertibleToWasm for i32 {
type NativeType = i32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> TypedValue {
TypedValue::I32(self)
fn to_typed_value(self) -> Value {
Value::I32(self)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
fn from_typed_value(v: Value) -> Option<Self> {
v.as_i32()
}
}
impl ConvertibleToWasm for u32 {
type NativeType = u32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> TypedValue {
TypedValue::I32(self as i32)
fn to_typed_value(self) -> Value {
Value::I32(self as i32)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
fn from_typed_value(v: Value) -> Option<Self> {
match v {
TypedValue::I32(v) => Some(v as u32),
Value::I32(v) => Some(v as u32),
_ => None,
}
}
@@ -55,12 +55,12 @@ impl ConvertibleToWasm for u32 {
impl ConvertibleToWasm for u64 {
type NativeType = u64;
const VALUE_TYPE: ValueType = ValueType::I64;
fn to_typed_value(self) -> TypedValue {
TypedValue::I64(self as i64)
fn to_typed_value(self) -> Value {
Value::I64(self as i64)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
fn from_typed_value(v: Value) -> Option<Self> {
match v {
TypedValue::I64(v) => Some(v as u64),
Value::I64(v) => Some(v as u64),
_ => None,
}
}
@@ -69,7 +69,7 @@ impl ConvertibleToWasm for u64 {
pub(crate) type HostFunc<E> =
fn(
&mut Runtime<E>,
&[sp_sandbox::TypedValue]
&[sp_sandbox::Value]
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError>;
pub(crate) trait FunctionImplProvider<E: Ext> {
@@ -23,9 +23,7 @@ use crate::exec::{
use crate::gas::{Gas, GasMeter, Token, GasMeterResult, approx_gas_for_balance};
use sp_sandbox;
use frame_system;
use sp_std::prelude::*;
use sp_std::convert::TryInto;
use sp_std::mem;
use sp_std::{prelude::*, mem, convert::TryInto};
use codec::{Decode, Encode};
use sp_runtime::traits::{Bounded, SaturatedConversion};
@@ -89,7 +87,7 @@ pub(crate) fn to_execution_result<E: Ext>(
buffer.clear();
Ok(ExecReturnValue { status: STATUS_SUCCESS, data: buffer })
}
Ok(sp_sandbox::ReturnValue::Value(sp_sandbox::TypedValue::I32(exit_code))) => {
Ok(sp_sandbox::ReturnValue::Value(sp_sandbox::Value::I32(exit_code))) => {
let status = (exit_code & 0xFF).try_into()
.expect("exit_code is masked into the range of a u8; qed");
Ok(ExecReturnValue { status, data: runtime.scratch_buf })