mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-06-19 02:51:03 +00:00
refactored rust runtime
This commit is contained in:
+16
-11
@@ -1,22 +1,27 @@
|
|||||||
use parity_wasm::interpreter::ModuleInstanceInterface;
|
use parity_wasm::interpreter;
|
||||||
|
use runtime::Runtime;
|
||||||
|
|
||||||
pub struct Arena {
|
pub struct Arena {
|
||||||
dynamic_top: u32,
|
pub runtime: Runtime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error;
|
pub struct Error;
|
||||||
|
|
||||||
impl Arena {
|
impl Arena {
|
||||||
pub fn new(stack_top: u32) -> Self {
|
pub fn alloc(&self, size: u32) -> Result<u32, Error> {
|
||||||
Arena {
|
// todo: maybe use unsafe cell since it has nothing to do with threads
|
||||||
dynamic_top: stack_top,
|
let previous_top = self.runtime.env().dynamic_top.get();
|
||||||
}
|
self.runtime.env().dynamic_top.set(previous_top + size);
|
||||||
}
|
|
||||||
|
|
||||||
pub fn alloc(&mut self, size: u32) -> Result<u32, Error> {
|
|
||||||
let previous_top = self.dynamic_top;
|
|
||||||
self.dynamic_top += size;
|
|
||||||
Ok(previous_top)
|
Ok(previous_top)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl interpreter::UserFunctionInterface for Arena {
|
||||||
|
fn call(&mut self, context: interpreter::CallerContext) -> Result<Option<interpreter::RuntimeValue>, interpreter::Error> {
|
||||||
|
let amount = context.value_stack.pop_as::<i32>()?;
|
||||||
|
self.alloc(amount as u32)
|
||||||
|
.map(|val| Some((val as i32).into()))
|
||||||
|
.map_err(|e| interpreter::Error::Trap(format!("Allocator failure: {}", "todo: format arg")))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use parity_wasm::interpreter::{self, ModuleInstanceInterface};
|
use parity_wasm::interpreter::{self, ModuleInstanceInterface};
|
||||||
use alloc;
|
use {alloc, runtime};
|
||||||
|
|
||||||
use {DEFAULT_MEMORY_INDEX, WasmMemoryPtr};
|
use {DEFAULT_MEMORY_INDEX, WasmMemoryPtr};
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ impl From<interpreter::Error> for Error {
|
|||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
env: &interpreter::ModuleInstanceInterface,
|
env: &interpreter::ModuleInstanceInterface,
|
||||||
allocator: &mut alloc::Arena,
|
runtime: &runtime::Runtime,
|
||||||
context: &[u8],
|
context: &[u8],
|
||||||
input: &[u8],
|
input: &[u8],
|
||||||
) -> Result<WasmMemoryPtr, Error> {
|
) -> Result<WasmMemoryPtr, Error> {
|
||||||
@@ -39,6 +39,8 @@ pub fn init(
|
|||||||
let mut input_ptr_slc = [0u8; 4];
|
let mut input_ptr_slc = [0u8; 4];
|
||||||
let mut input_length = [0u8; 4];
|
let mut input_length = [0u8; 4];
|
||||||
|
|
||||||
|
let allocator = runtime.allocator();
|
||||||
|
|
||||||
let descriptor_ptr = allocator.alloc(16)?;
|
let descriptor_ptr = allocator.alloc(16)?;
|
||||||
let context_ptr = allocator.alloc(context.len() as u32)?;
|
let context_ptr = allocator.alloc(context.len() as u32)?;
|
||||||
let input_ptr = allocator.alloc(input.len() as u32)?;
|
let input_ptr = allocator.alloc(input.len() as u32)?;
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ extern crate wasm_utils;
|
|||||||
mod alloc;
|
mod alloc;
|
||||||
mod storage;
|
mod storage;
|
||||||
mod call_args;
|
mod call_args;
|
||||||
|
mod runtime;
|
||||||
|
mod gas_counter;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use parity_wasm::interpreter::{self, ModuleInstanceInterface, RuntimeValue};
|
use parity_wasm::interpreter::{self, ModuleInstanceInterface};
|
||||||
|
|
||||||
pub const DEFAULT_MEMORY_INDEX: interpreter::ItemIndex = interpreter::ItemIndex::Internal(0);
|
pub const DEFAULT_MEMORY_INDEX: interpreter::ItemIndex = interpreter::ItemIndex::Internal(0);
|
||||||
pub type WasmMemoryPtr = i32;
|
pub type WasmMemoryPtr = i32;
|
||||||
@@ -36,12 +38,13 @@ fn main() {
|
|||||||
let module_instance = program.add_module("contract", module).expect("Module to be added successfully");
|
let module_instance = program.add_module("contract", module).expect("Module to be added successfully");
|
||||||
|
|
||||||
// Create allocator
|
// Create allocator
|
||||||
let mut allocator = alloc::Arena::new(5*1024*1024);
|
let runtime = runtime::Runtime::default();
|
||||||
|
runtime.allocator().alloc(5*1024*1024).expect("to allocate 5mb successfully"); // reserve stack space
|
||||||
|
|
||||||
// Initialize call descriptor
|
// Initialize call descriptor
|
||||||
let descriptor = call_args::init(
|
let descriptor = call_args::init(
|
||||||
&*program.module("env").expect("env module to exist"),
|
&*program.module("env").expect("env module to exist"),
|
||||||
&mut allocator,
|
&runtime,
|
||||||
&[],
|
&[],
|
||||||
&[0u8; 128],
|
&[0u8; 128],
|
||||||
).expect("call descriptor initialization to succeed");
|
).expect("call descriptor initialization to succeed");
|
||||||
|
|||||||
Reference in New Issue
Block a user