openethereum/ethcore/src/evm/wasm/ptr.rs
Nikolay Volf 62210fb932 WASM contracts MVP (#5679)
* lifetime issues

* refactor to new 'native env'

* descriptors and such

* wasm mvp continued

* finalized env/ext bindings

* descriptor -> call_args

* inject gas counter

* result processing and engine activation

* tabify some source files

* needs return new

* wasm tests initial

* erradicate warnings

* origin in the descriptor

* update test repo

* payload verification tests

* identity return payload test

* some test description

* dispersion test

* check length here

* suicidal contract

* engine params

* fix typo

* review fixes

* submodule update

* update - purge reserved space

* doc effort

* more review fixes

* fix error message

* fix dependency url

* reorg error handling

* update submodule

* update utils

* update to latest parity-wasm

* tabify

* fix wasm magic header

* update dependencies

* external create and tests

* update to latest tests

* extra trace info

* Update parity-wasm

* update wasm-utils also

* few traces and result handle change

* alter trace content

* fix issues with optimizer, update to latest parity with validator, etc

* static initialization

* license preamble

* update wasm crates and gas costs

* fix grumbles

* bring back lifetime

* fix compilation
2017-07-10 17:42:10 +02:00

52 lines
1.6 KiB
Rust

// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Wasm bound-checked ptr
use parity_wasm::interpreter;
/// Bound-checked wrapper for webassembly memory
pub struct WasmPtr(u32);
/// Error in bound check
#[derive(Debug)]
pub enum Error {
AccessViolation,
}
impl From<u32> for WasmPtr {
fn from(raw: u32) -> Self {
WasmPtr(raw)
}
}
impl WasmPtr {
// todo: use memory view when they are on
/// Check memory range and return data with given length starting from the current pointer value
pub fn slice(&self, len: u32, mem: &interpreter::MemoryInstance) -> Result<Vec<u8>, Error> {
mem.get(self.0, len as usize).map_err(|_| Error::AccessViolation)
}
// todo: maybe 2gb limit can be enhanced
/// Convert i32 from wasm stack to the wrapped pointer
pub fn from_i32(raw_ptr: i32) -> Result<Self, Error> {
if raw_ptr < 0 { return Err(Error::AccessViolation); }
Ok(WasmPtr(raw_ptr as u32))
}
/// Return pointer raw value
pub fn as_raw(&self) -> u32 { self.0 }
}