fixed env address

This commit is contained in:
debris 2015-12-28 15:11:05 +01:00
parent 4ce53e89ec
commit f2ae8708de
3 changed files with 53 additions and 18 deletions

View File

@ -68,20 +68,23 @@ impl DerefMut for RuntimeDataHandle {
/// Safe handle for jit context. /// Safe handle for jit context.
pub struct ContextHandle { pub struct ContextHandle {
context: *mut JitContext, context: *mut JitContext,
_data_handle: RuntimeDataHandle, data_handle: RuntimeDataHandle,
_env: EnvHandle env: EnvHandle
} }
impl ContextHandle { impl ContextHandle {
/// Creates new context handle. /// Creates new context handle.
pub fn new(mut data_handle: RuntimeDataHandle, mut env: EnvHandle) -> Self { pub fn new(data_handle: RuntimeDataHandle, env: EnvHandle) -> Self {
import_evmjit_abi(); import_evmjit_abi();
let context = unsafe { evmjit_create_context(data_handle.mut_runtime_data(), &mut env) }; let mut handle = ContextHandle {
ContextHandle { context: unsafe {::std::mem::uninitialized()},
context: context, data_handle: data_handle,
_data_handle: data_handle, env: env
_env: env };
}
println!("env address: {:?}", &handle.env as *const _);
handle.context = unsafe { evmjit_create_context(handle.data_handle.mut_runtime_data(), &mut handle.env) };
handle
} }
/// Executes context. /// Executes context.
@ -156,7 +159,7 @@ impl Deref for EnvHandle {
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
match self.env_impl { match self.env_impl {
Some(ref env) => env, Some(ref env) => env,
None => { panic!(); } None => { panic!("Handle is empty!"); }
} }
} }
} }
@ -165,7 +168,7 @@ impl DerefMut for EnvHandle {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
match self.env_impl { match self.env_impl {
Some(ref mut env) => env, Some(ref mut env) => env,
None => { panic!(); } None => { panic!("Handle is empty!"); }
} }
} }
} }
@ -201,6 +204,7 @@ pub mod ffi {
} }
#[repr(C)] #[repr(C)]
#[derive(Debug)]
/// Jit runtime data. /// Jit runtime data.
pub struct JitRuntimeData { pub struct JitRuntimeData {
pub gas: i64, pub gas: i64,
@ -232,7 +236,7 @@ pub mod ffi {
/// nm your_executable -g | grep env /// nm your_executable -g | grep env
/// ``` /// ```
/// ///
/// It Should give the following output: /// It should give the following output:
/// ///
/// ```bash /// ```bash
/// 00000001000779e0 T _env_balance /// 00000001000779e0 T _env_balance
@ -258,13 +262,15 @@ pub mod ffi {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern fn env_sload(env: *const EnvHandle, index: *const JitI256, out_value: *mut JitI256) { pub unsafe extern "C" fn env_sload(env: *const EnvHandle, index: *const JitI256, out_value: *mut JitI256) {
println!("sload env address: {:?}", env);
let env = &*env; let env = &*env;
env.sload(index, out_value); env.sload(index, out_value);
} }
#[no_mangle] #[no_mangle]
pub unsafe extern fn env_sstore(env: *mut EnvHandle, index: *const JitI256, value: *const JitI256) { pub unsafe extern "C" fn env_sstore(env: *mut EnvHandle, index: *mut JitI256, value: *mut JitI256) {
println!("sstore");
let env = &mut *env; let env = &mut *env;
env.sstore(index, value); env.sstore(index, value);
} }

View File

@ -8,11 +8,15 @@ impl Env {
} }
pub fn sload(&self, _index: &H256) -> H256 { pub fn sload(&self, _index: &H256) -> H256 {
unimplemented!(); println!("sload!");
//unimplemented!();
H256::new()
} }
pub fn sstore(&self, _index: &H256, _value: &H256) { pub fn sstore(&self, _index: &H256, _value: &H256) {
unimplemented!(); println!("sstore!");
//unimplemented!();
} }
} }

View File

@ -162,9 +162,10 @@ impl evmjit::Env for EnvAdapter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::str::FromStr;
use util::hash::*; use util::hash::*;
use util::uint::*; use util::uint::*;
use evmjit::{ContextHandle, RuntimeDataHandle, EnvHandle, ReturnCode}; use evmjit::{ContextHandle, RuntimeDataHandle, EnvHandle, ReturnCode, ffi};
use evm::*; use evm::*;
use evm::jit::{FromJit, IntoJit}; use evm::jit::{FromJit, IntoJit};
@ -188,13 +189,37 @@ mod tests {
assert_eq!(h, h2); assert_eq!(h, h2);
} }
#[test]
fn test_env_sload() {
let env = EnvHandle::new(EnvAdapter::new());
let i = U256::from(0).into_jit();
let mut o = U256::from(0).into_jit();
unsafe {
ffi::env_sload(&env as *const _, &i as *const _, &mut o as *mut _);
}
}
#[test] #[test]
fn test_env_adapter() { fn test_env_adapter() {
let mut data = RuntimeData::new(); let mut data = RuntimeData::new();
data.code = vec![0x60, 0x00, 0x60, 0x00, 0x20, 0x60, 0x00, 0x55]; data.coinbase = Address::from_str("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap();
data.difficulty = U256::from(0x0100);
data.gas_limit = U256::from(0x0f4240);
data.number = 0;
data.timestamp = 1;
data.address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
data.caller = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
data.code = vec![0x60, 0x00, 0x60, 0x00, 0x55];
data.gas = 0x174876e800;
data.gas_price = 0x3b9aca00;
data.origin = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
data.call_value = U256::from_str("0de0b6b3a7640000").unwrap();
let env = EnvAdapter::new(); let env = EnvAdapter::new();
let mut context = ContextHandle::new(data.into_jit(), EnvHandle::new(env)); let mut context = ContextHandle::new(data.into_jit(), EnvHandle::new(env));
// crashes with signal 11 on env.sload
assert_eq!(context.exec(), ReturnCode::Stop); assert_eq!(context.exec(), ReturnCode::Stop);
assert!(false);
} }
} }