Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

Commit

Permalink
DEV: Implemented as_c_ptr() to fix the empty Vec 0x1 ptr problem
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Dec 6, 2018
1 parent 4611462 commit 7cb0060
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 25 deletions.
5 changes: 3 additions & 2 deletions enigma-core/app/src/esgx/ocalls_u.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::km_u::ContractAddress;
use crate::db::{DeltaKey, Stype, DATABASE, P2PCalls, ResultType, ResultTypeVec, CRUDInterface};
use crate::esgx::general;
use enigma_types::traits::SliceCPtr;
use enigma_tools_u::common_u::{LockExpectMutex, Sha256};
use byteorder::{BigEndian, WriteBytesExt};
use std::{slice,ptr, mem};
Expand All @@ -13,7 +14,7 @@ lazy_static! { pub static ref DELTAS_CACHE: Mutex< LruCache<[u8; 32], Vec<Vec<u8
pub unsafe extern "C" fn ocall_get_home(output: *mut u8, result_len: &mut usize) {
let path = general::storage_dir();
let path_str = path.to_str().unwrap();
ptr::copy_nonoverlapping(path_str.as_ptr(), output, path_str.len());
ptr::copy_nonoverlapping(path_str.as_c_ptr(), output, path_str.len());
*result_len = path_str.len();
}

Expand Down Expand Up @@ -168,7 +169,7 @@ unsafe fn write_ptr<T>(src: &[T], dst: *mut T, count: usize) {
if src.len() > count {
unimplemented!()
}
ptr::copy_nonoverlapping(src.as_ptr(), dst, src.len());
ptr::copy_nonoverlapping(src.as_c_ptr(), dst, src.len());
}

fn get_deltas(addr: ContractAddress, start: u32, end: u32) -> ResultTypeVec<(DeltaKey, Vec<u8>)> {
Expand Down
14 changes: 8 additions & 6 deletions enigma-core/app/src/evm_u/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ extern crate sgx_urts;
use sgx_types::*;

use std::iter::FromIterator;
use enigma_types::traits::SliceCPtr;

//failure
use failure::Error;
use hex::ToHex;
Expand Down Expand Up @@ -75,17 +77,17 @@ pub fn exec_evm(eid: sgx_enclave_id_t, evm_input: EvmRequest) -> Result<EvmRespo
let result = unsafe {
ecall_evm(eid,
&mut retval,
evm_input.bytecode.as_ptr() as *const u8,
evm_input.bytecode.as_c_ptr() as *const u8,
evm_input.bytecode.len(),
evm_input.callable.as_ptr() as *const u8,
evm_input.callable.as_c_ptr() as *const u8,
evm_input.callable.len(),
evm_input.callable_args.as_ptr(),
evm_input.callable_args.as_c_ptr(),
evm_input.callable_args.len(),
//evm_input.preprocessor.as_ptr(),
prep.as_ptr(),
//evm_input.preprocessor.as_c_ptr(),
prep.as_c_ptr(),
//evm_input.preprocessor.len(),
prep.len(),
evm_input.callback.as_ptr(),
evm_input.callback.as_c_ptr(),
evm_input.callback.len(),
slice.as_mut_ptr() as *mut u8,
&mut signature,
Expand Down
5 changes: 3 additions & 2 deletions enigma-core/app/src/km_u.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use sgx_types::{sgx_status_t, sgx_enclave_id_t};
use enigma_types::EnclaveReturn;
use enigma_types::traits::SliceCPtr;
use failure::Error;
use crate::common_u::errors::EnclaveFailError;
use std::mem;
Expand Down Expand Up @@ -34,7 +35,7 @@ pub fn ptt_build_state(eid: sgx_enclave_id_t) -> Result<Vec<ContractAddress>, Er

pub fn ptt_res(eid: sgx_enclave_id_t, msg: &[u8]) -> Result<(), Error> {
let mut ret = EnclaveReturn::Success;
let status = unsafe { ecall_ptt_res(eid, &mut ret as *mut EnclaveReturn, msg.as_ptr(), msg.len()) };
let status = unsafe { ecall_ptt_res(eid, &mut ret as *mut EnclaveReturn, msg.as_c_ptr(), msg.len()) };
if ret != EnclaveReturn::Success || status != sgx_status_t::SGX_SUCCESS {
return Err(EnclaveFailError{err: ret, status}.into());
}
Expand All @@ -50,7 +51,7 @@ pub fn ptt_req(eid: sgx_enclave_id_t, addresses: &[ContractAddress]) -> Result<(

let status = unsafe { ecall_ptt_req(eid,
&mut ret as *mut EnclaveReturn,
addresses.as_ptr() as *const ContractAddress,
addresses.as_c_ptr() as *const ContractAddress,
addresses.len() * mem::size_of::<ContractAddress>(),
&mut sig,
&mut serialized_ptr as *mut u64
Expand Down
9 changes: 5 additions & 4 deletions enigma-core/app/src/wasm_u/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate sgx_urts;

use common_u::errors::EnclaveFailError;
use enigma_types::EnclaveReturn;
use enigma_types::traits::SliceCPtr;
use failure::Error;
use sgx_types::*;

Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn deploy(eid: sgx_enclave_id_t, bytecode: &[u8])-> Result<Box<[u8]>, Error
let result = unsafe {
ecall_deploy(eid,
&mut retval,
deploy_bytecode.as_ptr() as *const u8,
deploy_bytecode.as_c_ptr() as *const u8,
deploy_bytecode.len(),
&mut output_ptr as *mut u64)
};
Expand Down Expand Up @@ -103,11 +104,11 @@ pub fn execute(eid: sgx_enclave_id_t, bytecode: &[u8], callable: &str, args: &s
let status = unsafe {
ecall_execute(eid,
&mut retval,
bytecode.as_ptr() as *const u8,
bytecode.as_c_ptr() as *const u8,
bytecode.len(),
callable.as_ptr() as *const u8,
callable.as_c_ptr() as *const u8,
callable.len(),
args.as_ptr() as *const u8,
args.as_c_ptr() as *const u8,
args.len(),
&mut output as *mut u64,
&mut delta_data_ptr as *mut u64,
Expand Down
3 changes: 2 additions & 1 deletion enigma-core/enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use enigma_tools_t::cryptography_t::asymmetric;
use enigma_tools_t::{common, cryptography_t, quote_t};
use enigma_tools_t::build_arguments_g::*;
use enigma_types::EnclaveReturn;
use enigma_types::traits::SliceCPtr;

use sgx_types::*;
use std::string::ToString;
Expand Down Expand Up @@ -201,7 +202,7 @@ unsafe fn ecall_evm_internal(bytecode_slice: &[u8], callable_slice: &[u8], calla
match res.0 {
0 => {
*result_len = callback_data.len();
ptr::copy_nonoverlapping(callback_data.as_ptr(), output, callback_data.len());
ptr::copy_nonoverlapping(callback_data.as_c_ptr(), output, callback_data.len());
Ok(())
}
_ => {
Expand Down
9 changes: 2 additions & 7 deletions enigma-core/enclave/src/ocalls_t.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use enigma_types::traits::SliceCPtr;
use enigma_tools_t::common::errors_t::EnclaveError;
use sgx_types::sgx_status_t;
use std::{path, str};
Expand All @@ -24,13 +25,7 @@ pub fn get_home_path() -> Result<path::PathBuf, EnclaveError> {

pub fn save_to_untrusted_memory(data: &[u8]) -> Result<u64, EnclaveError> {
let mut ptr = 0u64;

//TODO: change this temporary solution for a problematic as_ptr implementation if empty vec
let mut data = data.clone();
if data.is_empty() {
data = &[];
}
match unsafe { ocall_save_to_memory(&mut ptr as *mut u64, data.as_ptr(), data.len()) } {
match unsafe { ocall_save_to_memory(&mut ptr as *mut u64, data.as_c_ptr(), data.len()) } {
sgx_status_t::SGX_SUCCESS => Ok(ptr),
e => Err(e.into()),
}
Expand Down
3 changes: 2 additions & 1 deletion enigma-principal/app/src/esgx/general.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use enigma_tools_u;

use sgx_types::*;
use sgx_urts::SgxEnclave;
use std::io::{Read, Write};
Expand All @@ -6,7 +8,6 @@ use std::path;
use std::env;
use std::ptr;
use dirs;
use enigma_tools_u;

static ENCLAVE_FILE: &'static str = "../bin/enclave.signed.so";
static ENCLAVE_TOKEN: &'static str = "enclave.token";
Expand Down
1 change: 1 addition & 0 deletions enigma-runtime-t/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions enigma-runtime-t/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Elichai Turkel <elichai@enigma.co>"]

[dependencies]
enigma-types = { path = "../enigma-types" }
enigma-tools-t = { path = "../enigma-tools-t" }

etcommon-hexutil = { version = "0.2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions enigma-runtime-t/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate serde_json;
extern crate serde_derive;
extern crate serde;
extern crate rmp_serde as rmps;
extern crate enigma_types;
extern crate enigma_tools_t;
extern crate json_patch;
extern crate wasmi;
Expand Down
5 changes: 3 additions & 2 deletions enigma-runtime-t/src/ocalls_t.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sgx_types::sgx_status_t;
use std::string::ToString;
use enigma_types::traits::SliceCPtr;
use enigma_tools_t::common::errors_t::EnclaveError::{self, OcallError};
use enigma_tools_t::km_primitives::ContractAddress;
use crate::data::{EncryptedContractState, EncryptedPatch};
Expand All @@ -17,7 +18,7 @@ extern "C" {
pub fn save_state(enc: &EncryptedContractState<u8>) -> Result<(), EnclaveError> {
let mut res_int: i8 = -1;
let res_status: sgx_status_t = unsafe {
ocall_update_state(&mut res_int as *mut i8, &enc.contract_id, enc.json.as_ptr(), enc.json.len())
ocall_update_state(&mut res_int as *mut i8, &enc.contract_id, enc.json.as_c_ptr(), enc.json.len())
};
match res_int {
0 => (), // 0 is the OK result
Expand All @@ -33,7 +34,7 @@ pub fn save_state(enc: &EncryptedContractState<u8>) -> Result<(), EnclaveError>
pub fn save_delta(enc: &EncryptedPatch) -> Result<(), EnclaveError> {
let mut res_int: i8 = -1;
let res_status: sgx_status_t = unsafe {
ocall_new_delta(&mut res_int as *mut i8, enc.data.as_ptr(), enc.data.len(), &enc.contract_id, &enc.index as *const u32)
ocall_new_delta(&mut res_int as *mut i8, enc.data.as_c_ptr(), enc.data.len(), &enc.contract_id, &enc.index as *const u32)
};

// TODO: Maybe use some sort of ErrorKind to differentiate between the errors outside
Expand Down
1 change: 1 addition & 0 deletions enigma-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]

mod types;
pub mod traits;

pub use crate::types::{EnclaveReturn, ResultToEnclaveReturn};

Expand Down
28 changes: 28 additions & 0 deletions enigma-types/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
static EMPTY: [u8; 1] = [0];

pub trait SliceCPtr {
type Target;
fn as_c_ptr(&self) -> *const Self::Target;
}

impl<T> SliceCPtr for [T] {
type Target = T;
fn as_c_ptr(&self) -> *const Self::Target {
if self.is_empty() {
EMPTY.as_ptr() as *const _
} else {
self.as_ptr()
}
}
}

impl SliceCPtr for str {
type Target = u8;
fn as_c_ptr(&self) -> *const Self::Target {
if self.is_empty() {
EMPTY.as_ptr() as *const _
} else {
self.as_ptr()
}
}
}

0 comments on commit 7cb0060

Please sign in to comment.