diff --git a/enigma-core/app/src/esgx/ocalls_u.rs b/enigma-core/app/src/esgx/ocalls_u.rs index e059aa76..e89c13c9 100644 --- a/enigma-core/app/src/esgx/ocalls_u.rs +++ b/enigma-core/app/src/esgx/ocalls_u.rs @@ -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}; @@ -13,7 +14,7 @@ lazy_static! { pub static ref DELTAS_CACHE: Mutex< LruCache<[u8; 32], Vec(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)> { diff --git a/enigma-core/app/src/evm_u/evm.rs b/enigma-core/app/src/evm_u/evm.rs index 4f192ad0..44356048 100644 --- a/enigma-core/app/src/evm_u/evm.rs +++ b/enigma-core/app/src/evm_u/evm.rs @@ -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; @@ -75,17 +77,17 @@ pub fn exec_evm(eid: sgx_enclave_id_t, evm_input: EvmRequest) -> Result Result, 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()); } @@ -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::(), &mut sig, &mut serialized_ptr as *mut u64 diff --git a/enigma-core/app/src/wasm_u/wasm.rs b/enigma-core/app/src/wasm_u/wasm.rs index 16090165..419b94d5 100755 --- a/enigma-core/app/src/wasm_u/wasm.rs +++ b/enigma-core/app/src/wasm_u/wasm.rs @@ -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::*; @@ -73,7 +74,7 @@ pub fn deploy(eid: sgx_enclave_id_t, bytecode: &[u8])-> Result, 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) }; @@ -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, diff --git a/enigma-core/enclave/src/lib.rs b/enigma-core/enclave/src/lib.rs index 8c988dce..45b72d05 100755 --- a/enigma-core/enclave/src/lib.rs +++ b/enigma-core/enclave/src/lib.rs @@ -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; @@ -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(()) } _ => { diff --git a/enigma-core/enclave/src/ocalls_t.rs b/enigma-core/enclave/src/ocalls_t.rs index 1a3c4856..015777c6 100644 --- a/enigma-core/enclave/src/ocalls_t.rs +++ b/enigma-core/enclave/src/ocalls_t.rs @@ -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}; @@ -24,13 +25,7 @@ pub fn get_home_path() -> Result { pub fn save_to_untrusted_memory(data: &[u8]) -> Result { 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()), } diff --git a/enigma-principal/app/src/esgx/general.rs b/enigma-principal/app/src/esgx/general.rs index 2f18dc96..6fc004d3 100644 --- a/enigma-principal/app/src/esgx/general.rs +++ b/enigma-principal/app/src/esgx/general.rs @@ -1,3 +1,5 @@ +use enigma_tools_u; + use sgx_types::*; use sgx_urts::SgxEnclave; use std::io::{Read, Write}; @@ -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"; diff --git a/enigma-runtime-t/Cargo.lock b/enigma-runtime-t/Cargo.lock index c882e10c..9f64c967 100644 --- a/enigma-runtime-t/Cargo.lock +++ b/enigma-runtime-t/Cargo.lock @@ -140,6 +140,7 @@ name = "enigma-runtime-t" version = "0.1.0" dependencies = [ "enigma-tools-t 0.1.3", + "enigma-types 0.1.0", "etcommon-hexutil 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "json-patch 0.2.2 (git+https://github.com/enigmampc/json-patch.git?rev=sgx-0.2.2-v1.0.4)", "rmp-serde 0.14.0 (git+https://github.com/enigmampc/msgpack-rust.git?rev=sgx-v1.0.4)", diff --git a/enigma-runtime-t/Cargo.toml b/enigma-runtime-t/Cargo.toml index 12a54f0b..64ae0090 100644 --- a/enigma-runtime-t/Cargo.toml +++ b/enigma-runtime-t/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Elichai Turkel "] [dependencies] +enigma-types = { path = "../enigma-types" } enigma-tools-t = { path = "../enigma-tools-t" } etcommon-hexutil = { version = "0.2", default-features = false } diff --git a/enigma-runtime-t/src/lib.rs b/enigma-runtime-t/src/lib.rs index 1f4459ac..af2fd4ac 100755 --- a/enigma-runtime-t/src/lib.rs +++ b/enigma-runtime-t/src/lib.rs @@ -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; diff --git a/enigma-runtime-t/src/ocalls_t.rs b/enigma-runtime-t/src/ocalls_t.rs index e3908e6b..ef385ff0 100644 --- a/enigma-runtime-t/src/ocalls_t.rs +++ b/enigma-runtime-t/src/ocalls_t.rs @@ -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}; @@ -17,7 +18,7 @@ extern "C" { pub fn save_state(enc: &EncryptedContractState) -> 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 @@ -33,7 +34,7 @@ pub fn save_state(enc: &EncryptedContractState) -> 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 diff --git a/enigma-types/src/lib.rs b/enigma-types/src/lib.rs index e6c5726c..488deb52 100644 --- a/enigma-types/src/lib.rs +++ b/enigma-types/src/lib.rs @@ -1,6 +1,7 @@ #![no_std] mod types; +pub mod traits; pub use crate::types::{EnclaveReturn, ResultToEnclaveReturn}; diff --git a/enigma-types/src/traits.rs b/enigma-types/src/traits.rs new file mode 100644 index 00000000..e42ddbb3 --- /dev/null +++ b/enigma-types/src/traits.rs @@ -0,0 +1,28 @@ +static EMPTY: [u8; 1] = [0]; + +pub trait SliceCPtr { + type Target; + fn as_c_ptr(&self) -> *const Self::Target; +} + +impl 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() + } + } +} \ No newline at end of file