openethereum/evmjit/src/lib.rs

431 lines
11 KiB
Rust
Raw Normal View History

2015-12-22 20:05:14 +01:00
//! Bare rust wrapper around evmjit.
2015-12-05 19:21:07 +01:00
//!
//! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit
2015-12-22 17:00:30 +01:00
//!
//! ```
//! extern crate evmjit;
//! use evmjit::*;
//!
//! fn main() {
2016-01-07 19:20:23 +01:00
//! let mut context = ContextHandle::new(RuntimeDataHandle::new(), ExtHandle::empty());
2015-12-22 17:00:30 +01:00
//! assert_eq!(context.exec(), ReturnCode::Stop);
//! }
//! ```
2016-01-07 19:20:23 +01:00
//!
//!
//! To verify that c abi is "imported" correctly, run:
//!
//! ```bash
//! nm your_executable -g | grep ext
//! ```
//!
//! It should give the following output:
//!
//! ```bash
//! 00000001000779e0 T _ext_balance
//! 0000000100077a10 T _ext_blockhash
//! 0000000100077a90 T _ext_call
//! 0000000100077a40 T _ext_create
//! 0000000100077b50 T _ext_extcode
//! 0000000100077b80 T _ext_log
//! 0000000100077b20 T _ext_sha3
//! 0000000100077980 T _ext_sload
//! 00000001000779b0 T _ext_sstore
//! ```
2015-12-05 19:21:07 +01:00
2015-12-22 20:05:14 +01:00
extern crate tiny_keccak;
use std::ops::{Deref, DerefMut};
2015-12-22 17:00:30 +01:00
use self::ffi::*;
2015-12-05 19:21:07 +01:00
2015-12-22 17:00:30 +01:00
pub use self::ffi::JitReturnCode as ReturnCode;
2015-12-23 13:02:01 +01:00
pub use self::ffi::JitI256 as I256;
pub use self::ffi::JitH256 as H256;
2015-12-05 19:21:07 +01:00
/// Takes care of proper initialization and destruction of `RuntimeData`.
///
/// This handle must be used to create runtime data,
/// cause underneath it's a `C++` structure. Incombatible with rust
/// structs.
2015-12-22 15:17:28 +01:00
pub struct RuntimeDataHandle {
runtime_data: *mut JitRuntimeData
}
impl RuntimeDataHandle {
/// Creates new `RuntimeData` handle.
2015-12-22 16:27:19 +01:00
pub fn new() -> Self {
2015-12-22 15:17:28 +01:00
RuntimeDataHandle {
2015-12-22 16:27:19 +01:00
runtime_data: unsafe { evmjit_create_runtime_data() }
2015-12-22 15:17:28 +01:00
}
}
/// Returns immutable reference to runtime data.
2015-12-22 16:27:19 +01:00
pub fn runtime_data(&self) -> &JitRuntimeData {
unsafe { &*self.runtime_data }
2015-12-22 15:17:28 +01:00
}
/// Returns mutable reference to runtime data.
2015-12-22 16:27:19 +01:00
pub fn mut_runtime_data(&mut self) -> &mut JitRuntimeData {
unsafe { &mut *self.runtime_data }
2015-12-22 15:17:28 +01:00
}
}
impl Drop for RuntimeDataHandle {
fn drop(&mut self) {
unsafe { evmjit_destroy_runtime_data(self.runtime_data) }
}
}
2015-12-23 13:02:01 +01:00
impl Deref for RuntimeDataHandle {
type Target = JitRuntimeData;
fn deref(&self) -> &Self::Target {
self.runtime_data()
}
}
impl DerefMut for RuntimeDataHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
self.mut_runtime_data()
}
}
/// Takes care of proper initialization and destruction of jit context.
///
/// This handle must be used to create context,
/// cause underneath it's a `C++` structure. Incombatible with rust
/// structs.
2015-12-22 15:17:28 +01:00
pub struct ContextHandle {
2015-12-22 16:27:19 +01:00
context: *mut JitContext,
2015-12-28 15:11:05 +01:00
data_handle: RuntimeDataHandle,
2015-12-22 15:17:28 +01:00
}
impl ContextHandle {
2015-12-22 16:27:19 +01:00
/// Creates new context handle.
///
2016-01-07 19:20:23 +01:00
/// This function is unsafe cause ext lifetime is not considered
/// We also can't make ExtHandle a member of `ContextHandle` structure,
/// cause this would be a move operation or it would require a template
/// lifetime to a reference. Both solutions are not possible.
2016-01-07 19:20:23 +01:00
pub unsafe fn new(data_handle: RuntimeDataHandle, ext: &mut ExtHandle) -> Self {
2015-12-28 15:11:05 +01:00
let mut handle = ContextHandle {
2015-12-28 22:37:15 +01:00
context: std::mem::uninitialized(),
2015-12-28 15:11:05 +01:00
data_handle: data_handle,
};
2016-01-07 19:20:23 +01:00
handle.context = evmjit_create_context(handle.data_handle.mut_runtime_data(), ext);
2015-12-28 15:11:05 +01:00
handle
2015-12-22 15:17:28 +01:00
}
2015-12-22 16:27:19 +01:00
/// Executes context.
pub fn exec(&mut self) -> JitReturnCode {
unsafe { evmjit_exec(self.context) }
}
2016-01-09 18:25:18 +01:00
/// Returns output data.
pub fn output_data(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data_handle.call_data, self.data_handle.call_data_size as usize) }
}
2016-01-13 16:16:21 +01:00
/// Returns address to which funds should be transfered after suicide.
pub fn suicide_refund_address(&self) -> JitI256 {
// evmjit reuses data_handle address field to store suicide address
self.data_handle.address
}
/// Returns gas left.
pub fn gas_left(&self) -> u64 {
self.data_handle.gas as u64
}
2015-12-22 16:27:19 +01:00
}
impl Drop for ContextHandle {
fn drop(&mut self) {
unsafe { evmjit_destroy_context(self.context); }
}
2015-12-22 15:17:28 +01:00
}
2016-01-07 19:20:23 +01:00
/// Component oriented wrapper around jit ext c interface.
pub trait Ext {
2015-12-22 17:13:03 +01:00
fn sload(&self, index: *const JitI256, out_value: *mut JitI256);
fn sstore(&mut self, index: *const JitI256, value: *const JitI256);
fn balance(&self, address: *const JitH256, out_value: *mut JitI256);
fn blockhash(&self, number: *const JitI256, out_hash: *mut JitH256);
2015-12-22 18:00:47 +01:00
fn create(&mut self,
io_gas: *mut u64,
endowment: *const JitI256,
init_beg: *const u8,
2016-01-06 20:00:32 +01:00
init_size: u64,
address: *mut JitH256);
2015-12-22 18:00:47 +01:00
fn call(&mut self,
io_gas: *mut u64,
2016-01-06 20:00:32 +01:00
call_gas: u64,
receive_address: *const JitH256,
2015-12-22 18:00:47 +01:00
value: *const JitI256,
in_beg: *const u8,
2016-01-06 20:00:32 +01:00
in_size: u64,
2015-12-22 18:00:47 +01:00
out_beg: *mut u8,
2016-01-06 20:00:32 +01:00
out_size: u64,
code_address: *const JitH256) -> bool;
2015-12-22 18:00:47 +01:00
2015-12-22 20:05:14 +01:00
fn log(&mut self,
beg: *const u8,
2016-01-06 13:00:14 +01:00
size: u64,
topic1: *const JitH256,
topic2: *const JitH256,
topic3: *const JitH256,
topic4: *const JitH256);
2015-12-22 20:05:14 +01:00
fn extcode(&self, address: *const JitH256, size: *mut u64) -> *const u8;
2015-12-22 15:17:28 +01:00
}
2016-01-07 19:20:23 +01:00
/// C abi compatible wrapper for jit ext implementers.
pub struct ExtHandle {
ext_impl: Option<Box<Ext>>
2015-12-22 15:17:28 +01:00
}
2016-01-07 19:20:23 +01:00
impl ExtHandle {
/// Creates new extironment wrapper for given implementation
pub fn new<T>(ext_impl: T) -> Self where T: Ext + 'static {
ExtHandle { ext_impl: Some(Box::new(ext_impl)) }
2015-12-22 15:17:28 +01:00
}
2015-12-22 16:27:19 +01:00
2016-01-07 19:20:23 +01:00
/// Creates empty extironment.
2015-12-22 16:27:19 +01:00
/// It can be used to for any operations.
pub fn empty() -> Self {
2016-01-07 19:20:23 +01:00
ExtHandle { ext_impl: None }
2015-12-22 16:27:19 +01:00
}
2015-12-22 15:17:28 +01:00
}
2016-01-07 19:20:23 +01:00
impl Deref for ExtHandle {
type Target = Box<Ext>;
fn deref(&self) -> &Self::Target {
2016-01-07 19:20:23 +01:00
match self.ext_impl {
Some(ref ext) => ext,
2015-12-28 15:11:05 +01:00
None => { panic!("Handle is empty!"); }
2015-12-22 15:17:28 +01:00
}
}
}
2015-12-22 17:13:03 +01:00
2016-01-07 19:20:23 +01:00
impl DerefMut for ExtHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
2016-01-07 19:20:23 +01:00
match self.ext_impl {
Some(ref mut ext) => ext,
2015-12-28 15:11:05 +01:00
None => { panic!("Handle is empty!"); }
2015-12-22 17:13:03 +01:00
}
}
2015-12-22 15:17:28 +01:00
}
2015-12-22 17:00:30 +01:00
/// ffi functions
pub mod ffi {
2015-12-22 20:05:14 +01:00
use std::slice;
use std::mem;
2015-12-22 20:05:14 +01:00
use tiny_keccak::Keccak;
use super::*;
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
/// Jit context struct declaration.
pub enum JitContext {}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[repr(C)]
#[derive(Debug, Eq, PartialEq)]
/// Jit context execution return code.
pub enum JitReturnCode {
Stop = 0,
Return = 1,
Suicide = 2,
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
OutOfGas = -1,
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
LLVMError = -101,
UnexpectedError = -111
}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[repr(C)]
#[derive(Debug, Copy, Clone)]
2015-12-22 17:00:30 +01:00
/// Signed 256 bit integer.
pub struct JitI256 {
pub words: [u64; 4]
}
2015-12-22 15:17:28 +01:00
#[repr(C)]
#[derive(Debug, Copy, Clone)]
/// Jit Hash
pub struct JitH256 {
pub words: [u64; 4]
}
impl From<JitH256> for JitI256 {
fn from(mut hash: JitH256) -> JitI256 {
unsafe {
{
let bytes: &mut [u8] = slice::from_raw_parts_mut(hash.words.as_mut_ptr() as *mut u8, 32);
bytes.reverse();
}
mem::transmute(hash)
}
}
}
2016-01-06 13:00:14 +01:00
impl From<JitI256> for JitH256 {
fn from(mut i: JitI256) -> JitH256 {
unsafe {
{
let bytes: &mut [u8] = slice::from_raw_parts_mut(i.words.as_mut_ptr() as *mut u8, 32);
bytes.reverse();
}
mem::transmute(i)
}
}
}
2015-12-22 17:00:30 +01:00
#[repr(C)]
2015-12-28 15:11:05 +01:00
#[derive(Debug)]
2015-12-22 17:00:30 +01:00
/// Jit runtime data.
pub struct JitRuntimeData {
pub gas: i64,
pub gas_price: i64,
2015-12-23 13:02:01 +01:00
pub call_data: *const u8,
2015-12-22 17:00:30 +01:00
pub call_data_size: u64,
pub address: JitI256,
pub caller: JitI256,
pub origin: JitI256,
pub call_value: JitI256,
2016-01-11 22:32:01 +01:00
pub author: JitI256,
2015-12-22 17:00:30 +01:00
pub difficulty: JitI256,
pub gas_limit: JitI256,
pub number: u64,
pub timestamp: i64,
2015-12-23 13:02:01 +01:00
pub code: *const u8,
2015-12-22 17:00:30 +01:00
pub code_size: u64,
pub code_hash: JitI256
}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_sload(ext: *const ExtHandle, index: *const JitI256, out_value: *mut JitI256) {
let ext = &*ext;
ext.sload(index, out_value);
2015-12-22 17:00:30 +01:00
}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_sstore(ext: *mut ExtHandle, index: *mut JitI256, value: *mut JitI256) {
let ext = &mut *ext;
ext.sstore(index, value);
2015-12-22 17:00:30 +01:00
}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_balance(ext: *const ExtHandle, address: *const JitH256, out_value: *mut JitI256) {
let ext = &*ext;
ext.balance(address, out_value);
2015-12-22 17:00:30 +01:00
}
2015-12-22 15:17:28 +01:00
2015-12-22 17:00:30 +01:00
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_blockhash(ext: *const ExtHandle, number: *const JitI256, out_hash: *mut JitH256) {
let ext = &*ext;
ext.blockhash(number, out_hash);
2015-12-22 17:00:30 +01:00
}
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_create(ext: *mut ExtHandle,
2015-12-22 18:00:47 +01:00
io_gas: *mut u64,
endowment: *const JitI256,
init_beg: *const u8,
2016-01-06 20:00:32 +01:00
init_size: u64,
address: *mut JitH256) {
2016-01-07 19:20:23 +01:00
let ext = &mut *ext;
ext.create(io_gas, endowment, init_beg, init_size, address);
2015-12-22 17:00:30 +01:00
}
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_call(ext: *mut ExtHandle,
2015-12-22 18:00:47 +01:00
io_gas: *mut u64,
2016-01-06 20:00:32 +01:00
call_gas: u64,
receive_address: *const JitH256,
2015-12-22 18:00:47 +01:00
value: *const JitI256,
in_beg: *const u8,
2016-01-06 20:00:32 +01:00
in_size: u64,
2015-12-22 18:00:47 +01:00
out_beg: *mut u8,
2016-01-06 20:00:32 +01:00
out_size: u64,
code_address: *const JitH256) -> bool {
2016-01-07 19:20:23 +01:00
let ext = &mut *ext;
ext.call(io_gas, call_gas, receive_address, value, in_beg, in_size, out_beg, out_size, code_address)
2015-12-22 17:00:30 +01:00
}
2015-12-10 14:59:46 +01:00
2015-12-22 17:00:30 +01:00
#[no_mangle]
2016-01-06 20:00:32 +01:00
pub unsafe extern "C" fn env_sha3(begin: *const u8, size: u64, out_hash: *mut JitH256) {
2015-12-22 20:05:14 +01:00
let out_hash = &mut *out_hash;
2015-12-24 01:07:46 +01:00
let input = slice::from_raw_parts(begin, size as usize);
2015-12-22 20:05:14 +01:00
let outlen = out_hash.words.len() * 8;
let output = slice::from_raw_parts_mut(out_hash.words.as_mut_ptr() as *mut u8, outlen);
2015-12-30 12:03:40 +01:00
let mut sha3 = Keccak::new_keccak256();
2015-12-22 20:05:14 +01:00
sha3.update(input);
sha3.finalize(output);
2015-12-22 17:00:30 +01:00
}
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_extcode(ext: *const ExtHandle, address: *const JitH256, size: *mut u64) -> *const u8 {
let ext = &*ext;
ext.extcode(address, size)
2015-12-22 17:00:30 +01:00
}
#[no_mangle]
2016-01-07 19:20:23 +01:00
pub unsafe extern "C" fn env_log(ext: *mut ExtHandle,
2015-12-22 20:05:14 +01:00
beg: *const u8,
2016-01-06 13:00:14 +01:00
size: u64,
topic1: *const JitH256,
topic2: *const JitH256,
topic3: *const JitH256,
topic4: *const JitH256) {
2016-01-07 19:20:23 +01:00
let ext = &mut *ext;
ext.log(beg, size, topic1, topic2, topic3, topic4);
2015-12-22 17:00:30 +01:00
}
#[link(name="evmjit")]
extern "C" {
pub fn evmjit_create_runtime_data() -> *mut JitRuntimeData;
pub fn evmjit_destroy_runtime_data(data: *mut JitRuntimeData);
pub fn evmjit_destroy_context(context: *mut JitContext);
pub fn evmjit_exec(context: *mut JitContext) -> JitReturnCode;
}
// ExtHandle is not a C type, so we need to allow "improper_ctypes"
2015-12-22 17:00:30 +01:00
#[link(name="evmjit")]
#[allow(improper_ctypes)]
extern "C" {
2016-01-07 19:20:23 +01:00
pub fn evmjit_create_context(data: *mut JitRuntimeData, ext: *mut ExtHandle) -> *mut JitContext;
2015-12-22 17:00:30 +01:00
}
2015-12-05 19:21:07 +01:00
}
#[test]
2015-12-22 16:27:19 +01:00
fn ffi_test() {
2015-12-05 19:21:07 +01:00
unsafe {
2015-12-10 14:59:46 +01:00
let data = evmjit_create_runtime_data();
2016-01-07 19:20:23 +01:00
let context = evmjit_create_context(data, &mut ExtHandle::empty());
2015-12-10 14:59:46 +01:00
let code = evmjit_exec(context);
assert_eq!(code, JitReturnCode::Stop);
evmjit_destroy_runtime_data(data);
evmjit_destroy_context(context);
2015-12-05 19:21:07 +01:00
}
}
2015-12-22 16:27:19 +01:00
#[test]
fn handle_test() {
unsafe {
2016-01-07 19:20:23 +01:00
let mut context = ContextHandle::new(RuntimeDataHandle::new(), &mut ExtHandle::empty());
assert_eq!(context.exec(), ReturnCode::Stop);
}
}
#[test]
fn hash_to_int() {
let h = H256 { words:[0x0123456789abcdef, 0, 0, 0] };
let i = I256::from(h);
assert_eq!([0u64, 0, 0, 0xefcdab8967452301], i.words);
2016-01-06 13:00:14 +01:00
assert_eq!(H256::from(i).words, h.words);
2015-12-22 16:27:19 +01:00
}