This commit is contained in:
debris 2015-12-22 17:00:30 +01:00
parent 61a4b59da5
commit ff747ba349

View File

@ -1,33 +1,22 @@
//! Bare rust wrapper around evmjit //! Bare rust wrapper around evmjit
//! //!
//! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit //! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit
//!
//! ```
//! extern crate evmjit;
//! use evmjit::*;
//!
//! fn main() {
//! let mut context = ContextHandle::new(RuntimeDataHandle::new(), EnvHandle::empty());
//! assert_eq!(context.exec(), ReturnCode::Stop);
//! }
//!
//! ```
extern crate libc; extern crate libc;
use self::ffi::*;
#[repr(C)] pub use self::ffi::JitReturnCode as ReturnCode;
pub struct JitI256 {
pub words: [u64; 4]
}
#[repr(C)]
pub struct JitRuntimeData {
pub gas: i64,
pub gas_price: i64,
pub call_data: *const libc::c_char,
pub call_data_size: u64,
pub address: JitI256,
pub caller: JitI256,
pub origin: JitI256,
pub call_value: JitI256,
pub coinbase: JitI256,
pub difficulty: JitI256,
pub gas_limit: JitI256,
pub number: u64,
pub timestamp: i64,
pub code: *const libc::c_char,
pub code_size: u64,
pub code_hash: JitI256
}
/// Component oriented safe handle to `JitRuntimeData`. /// Component oriented safe handle to `JitRuntimeData`.
pub struct RuntimeDataHandle { pub struct RuntimeDataHandle {
@ -59,22 +48,6 @@ impl Drop for RuntimeDataHandle {
} }
} }
#[repr(C)]
#[derive(Debug, Eq, PartialEq)]
pub enum JitReturnCode {
Stop = 0,
Return = 1,
Suicide = 2,
OutOfGas = -1,
LLVMError = -101,
UnexpectedError = -111
}
/// JitContext struct declaration.
pub enum JitContext {}
/// Safe handle for jit context. /// Safe handle for jit context.
pub struct ContextHandle { pub struct ContextHandle {
context: *mut JitContext, context: *mut JitContext,
@ -83,7 +56,7 @@ pub struct ContextHandle {
impl ContextHandle { impl ContextHandle {
/// Creates new context handle. /// Creates new context handle.
pub fn new(mut data_handle: RuntimeDataHandle, mut env: Env) -> Self { pub fn new(mut data_handle: RuntimeDataHandle, mut env: EnvHandle) -> Self {
let context = unsafe { evmjit_create_context(data_handle.mut_runtime_data(), &mut env) }; let context = unsafe { evmjit_create_context(data_handle.mut_runtime_data(), &mut env) };
ContextHandle { ContextHandle {
context: context, context: context,
@ -104,29 +77,29 @@ impl Drop for ContextHandle {
} }
/// Component oriented wrapper around jit env c interface. /// Component oriented wrapper around jit env c interface.
pub trait JitEnv { pub trait Env {
fn sload(&mut self); fn sload(&mut self);
} }
/// C abi compatible wrapper for JitEnvTrait implementers. /// C abi compatible wrapper for jit env implementers.
pub struct Env { pub struct EnvHandle {
env_impl: Option<Box<JitEnv>> env_impl: Option<Box<Env>>
} }
impl Env { impl EnvHandle {
/// Creates new environment wrapper for given implementation /// Creates new environment wrapper for given implementation
pub fn new<T>(env_impl: T) -> Self where T: JitEnv + 'static { pub fn new<T>(env_impl: T) -> Self where T: Env + 'static {
Env { env_impl: Some(Box::new(env_impl)) } EnvHandle { env_impl: Some(Box::new(env_impl)) }
} }
/// Creates empty environment. /// Creates empty environment.
/// It can be used to for any operations. /// It can be used to for any operations.
pub fn empty() -> Self { pub fn empty() -> Self {
Env { env_impl: None } EnvHandle { env_impl: None }
} }
} }
impl JitEnv for Env { impl Env for EnvHandle {
fn sload(&mut self) { fn sload(&mut self) {
match self.env_impl { match self.env_impl {
Some(ref mut env) => env.sload(), Some(ref mut env) => env.sload(),
@ -135,93 +108,143 @@ impl JitEnv for Env {
} }
} }
#[no_mangle] /// ffi functions
pub unsafe extern fn env_sload(env: *mut Env, _index: *const JitI256, _value: *const JitI256) { pub mod ffi {
let env = &mut *env; use super::*;
env.sload(); use libc;
}
#[no_mangle] /// Jit context struct declaration.
pub extern fn env_sstore(_env: *mut Env, _index: *const JitI256, _value: *const JitI256) { pub enum JitContext {}
unimplemented!()
}
#[no_mangle] #[repr(C)]
pub extern fn env_balance(_env: *mut Env, _address: *const JitI256, _value: *const JitI256) { #[derive(Debug, Eq, PartialEq)]
unimplemented!() /// Jit context execution return code.
} pub enum JitReturnCode {
Stop = 0,
Return = 1,
Suicide = 2,
#[no_mangle] OutOfGas = -1,
pub extern fn env_blockhash(_env: *mut Env, _number: *const JitI256, _hash: *const JitI256) {
unimplemented!()
}
#[no_mangle] LLVMError = -101,
pub extern fn env_create(_env: *mut Env, UnexpectedError = -111
_io_gas: *const u64, }
_endowment: *const JitI256,
_init_beg: *const u8,
_init_size: *const u64,
_address: *const JitI256) {
unimplemented!()
}
#[no_mangle] #[repr(C)]
pub extern fn env_call(_env: *mut Env, /// Signed 256 bit integer.
_io_gas: *const u64, pub struct JitI256 {
_call_gas: *const u64, pub words: [u64; 4]
_receive_address: *const JitI256, }
_value: *const JitI256,
_in_beg: *const u8,
_in_size: *const u64,
_out_beg: *const u8,
_out_size: *const u64,
_code_address: JitI256) {
unimplemented!()
}
#[no_mangle] #[repr(C)]
pub extern fn env_sha3(_begin: *const u8, _size: *const u64, _hash: *const JitI256) { /// Jit runtime data.
unimplemented!() pub struct JitRuntimeData {
} pub gas: i64,
pub gas_price: i64,
pub call_data: *const libc::c_char,
pub call_data_size: u64,
pub address: JitI256,
pub caller: JitI256,
pub origin: JitI256,
pub call_value: JitI256,
pub coinbase: JitI256,
pub difficulty: JitI256,
pub gas_limit: JitI256,
pub number: u64,
pub timestamp: i64,
pub code: *const libc::c_char,
pub code_size: u64,
pub code_hash: JitI256
}
#[no_mangle] #[no_mangle]
pub extern fn env_extcode(_env: *mut Env, _address: *const JitI256, _size: *const u64) { pub unsafe extern fn env_sload(env: *mut EnvHandle, _index: *const JitI256, _value: *const JitI256) {
unimplemented!() let env = &mut *env;
} env.sload();
}
#[no_mangle] #[no_mangle]
pub extern fn env_log(_env: *mut Env, pub extern fn env_sstore(_env: *mut EnvHandle, _index: *const JitI256, _value: *const JitI256) {
_beg: *const u8, unimplemented!()
_size: *const u64, }
_topic1: *const JitI256,
_topic2: *const JitI256, #[no_mangle]
_topic3: *const JitI256, pub extern fn env_balance(_env: *mut EnvHandle, _address: *const JitI256, _value: *const JitI256) {
_topic4: *const JitI256) { unimplemented!()
unimplemented!() }
}
#[no_mangle]
pub extern fn env_blockhash(_env: *mut EnvHandle, _number: *const JitI256, _hash: *const JitI256) {
unimplemented!()
}
#[no_mangle]
pub extern fn env_create(_env: *mut EnvHandle,
_io_gas: *const u64,
_endowment: *const JitI256,
_init_beg: *const u8,
_init_size: *const u64,
_address: *const JitI256) {
unimplemented!()
}
#[no_mangle]
pub extern fn env_call(_env: *mut EnvHandle,
_io_gas: *const u64,
_call_gas: *const u64,
_receive_address: *const JitI256,
_value: *const JitI256,
_in_beg: *const u8,
_in_size: *const u64,
_out_beg: *const u8,
_out_size: *const u64,
_code_address: JitI256) {
unimplemented!()
}
#[no_mangle]
pub extern fn env_sha3(_begin: *const u8, _size: *const u64, _hash: *const JitI256) {
unimplemented!()
}
#[no_mangle]
pub extern fn env_extcode(_env: *mut EnvHandle, _address: *const JitI256, _size: *const u64) {
unimplemented!()
}
#[no_mangle]
pub extern fn env_log(_env: *mut EnvHandle,
_beg: *const u8,
_size: *const u64,
_topic1: *const JitI256,
_topic2: *const JitI256,
_topic3: *const JitI256,
_topic4: *const JitI256) {
unimplemented!()
}
#[link(name="evmjit")] #[link(name="evmjit")]
extern "C" { extern "C" {
pub fn evmjit_create_runtime_data() -> *mut JitRuntimeData; pub fn evmjit_create_runtime_data() -> *mut JitRuntimeData;
pub fn evmjit_destroy_runtime_data(data: *mut JitRuntimeData); pub fn evmjit_destroy_runtime_data(data: *mut JitRuntimeData);
pub fn evmjit_destroy_context(context: *mut JitContext); pub fn evmjit_destroy_context(context: *mut JitContext);
pub fn evmjit_exec(context: *mut JitContext) -> JitReturnCode; pub fn evmjit_exec(context: *mut JitContext) -> JitReturnCode;
} }
#[link(name="evmjit")] #[link(name="evmjit")]
// Env does not have to by a C type // EnvHandle does not have to by a C type
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
extern "C" { extern "C" {
pub fn evmjit_create_context(data: *mut JitRuntimeData, env: *mut Env) -> *mut JitContext; pub fn evmjit_create_context(data: *mut JitRuntimeData, env: *mut EnvHandle) -> *mut JitContext;
}
} }
#[test] #[test]
fn ffi_test() { fn ffi_test() {
unsafe { unsafe {
let data = evmjit_create_runtime_data(); let data = evmjit_create_runtime_data();
let context = evmjit_create_context(data, &mut Env::empty()); let context = evmjit_create_context(data, &mut EnvHandle::empty());
let code = evmjit_exec(context); let code = evmjit_exec(context);
assert_eq!(code, JitReturnCode::Stop); assert_eq!(code, JitReturnCode::Stop);
@ -233,6 +256,6 @@ fn ffi_test() {
#[test] #[test]
fn handle_test() { fn handle_test() {
let mut context = ContextHandle::new(RuntimeDataHandle::new(), Env::empty()); let mut context = ContextHandle::new(RuntimeDataHandle::new(), EnvHandle::empty());
assert_eq!(context.exec(), JitReturnCode::Stop); assert_eq!(context.exec(), ReturnCode::Stop);
} }