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

Commit 7cb0060

Browse files
committed
DEV: Implemented as_c_ptr() to fix the empty Vec 0x1 ptr problem
1 parent 4611462 commit 7cb0060

File tree

13 files changed

+60
-25
lines changed

13 files changed

+60
-25
lines changed

enigma-core/app/src/esgx/ocalls_u.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::km_u::ContractAddress;
22
use crate::db::{DeltaKey, Stype, DATABASE, P2PCalls, ResultType, ResultTypeVec, CRUDInterface};
33
use crate::esgx::general;
4+
use enigma_types::traits::SliceCPtr;
45
use enigma_tools_u::common_u::{LockExpectMutex, Sha256};
56
use byteorder::{BigEndian, WriteBytesExt};
67
use std::{slice,ptr, mem};
@@ -13,7 +14,7 @@ lazy_static! { pub static ref DELTAS_CACHE: Mutex< LruCache<[u8; 32], Vec<Vec<u8
1314
pub unsafe extern "C" fn ocall_get_home(output: *mut u8, result_len: &mut usize) {
1415
let path = general::storage_dir();
1516
let path_str = path.to_str().unwrap();
16-
ptr::copy_nonoverlapping(path_str.as_ptr(), output, path_str.len());
17+
ptr::copy_nonoverlapping(path_str.as_c_ptr(), output, path_str.len());
1718
*result_len = path_str.len();
1819
}
1920

@@ -168,7 +169,7 @@ unsafe fn write_ptr<T>(src: &[T], dst: *mut T, count: usize) {
168169
if src.len() > count {
169170
unimplemented!()
170171
}
171-
ptr::copy_nonoverlapping(src.as_ptr(), dst, src.len());
172+
ptr::copy_nonoverlapping(src.as_c_ptr(), dst, src.len());
172173
}
173174

174175
fn get_deltas(addr: ContractAddress, start: u32, end: u32) -> ResultTypeVec<(DeltaKey, Vec<u8>)> {

enigma-core/app/src/evm_u/evm.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ extern crate sgx_urts;
55
use sgx_types::*;
66

77
use std::iter::FromIterator;
8+
use enigma_types::traits::SliceCPtr;
9+
810
//failure
911
use failure::Error;
1012
use hex::ToHex;
@@ -75,17 +77,17 @@ pub fn exec_evm(eid: sgx_enclave_id_t, evm_input: EvmRequest) -> Result<EvmRespo
7577
let result = unsafe {
7678
ecall_evm(eid,
7779
&mut retval,
78-
evm_input.bytecode.as_ptr() as *const u8,
80+
evm_input.bytecode.as_c_ptr() as *const u8,
7981
evm_input.bytecode.len(),
80-
evm_input.callable.as_ptr() as *const u8,
82+
evm_input.callable.as_c_ptr() as *const u8,
8183
evm_input.callable.len(),
82-
evm_input.callable_args.as_ptr(),
84+
evm_input.callable_args.as_c_ptr(),
8385
evm_input.callable_args.len(),
84-
//evm_input.preprocessor.as_ptr(),
85-
prep.as_ptr(),
86+
//evm_input.preprocessor.as_c_ptr(),
87+
prep.as_c_ptr(),
8688
//evm_input.preprocessor.len(),
8789
prep.len(),
88-
evm_input.callback.as_ptr(),
90+
evm_input.callback.as_c_ptr(),
8991
evm_input.callback.len(),
9092
slice.as_mut_ptr() as *mut u8,
9193
&mut signature,

enigma-core/app/src/km_u.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use sgx_types::{sgx_status_t, sgx_enclave_id_t};
44
use enigma_types::EnclaveReturn;
5+
use enigma_types::traits::SliceCPtr;
56
use failure::Error;
67
use crate::common_u::errors::EnclaveFailError;
78
use std::mem;
@@ -34,7 +35,7 @@ pub fn ptt_build_state(eid: sgx_enclave_id_t) -> Result<Vec<ContractAddress>, Er
3435

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

5152
let status = unsafe { ecall_ptt_req(eid,
5253
&mut ret as *mut EnclaveReturn,
53-
addresses.as_ptr() as *const ContractAddress,
54+
addresses.as_c_ptr() as *const ContractAddress,
5455
addresses.len() * mem::size_of::<ContractAddress>(),
5556
&mut sig,
5657
&mut serialized_ptr as *mut u64

enigma-core/app/src/wasm_u/wasm.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate sgx_urts;
44

55
use common_u::errors::EnclaveFailError;
66
use enigma_types::EnclaveReturn;
7+
use enigma_types::traits::SliceCPtr;
78
use failure::Error;
89
use sgx_types::*;
910

@@ -73,7 +74,7 @@ pub fn deploy(eid: sgx_enclave_id_t, bytecode: &[u8])-> Result<Box<[u8]>, Error
7374
let result = unsafe {
7475
ecall_deploy(eid,
7576
&mut retval,
76-
deploy_bytecode.as_ptr() as *const u8,
77+
deploy_bytecode.as_c_ptr() as *const u8,
7778
deploy_bytecode.len(),
7879
&mut output_ptr as *mut u64)
7980
};
@@ -103,11 +104,11 @@ pub fn execute(eid: sgx_enclave_id_t, bytecode: &[u8], callable: &str, args: &s
103104
let status = unsafe {
104105
ecall_execute(eid,
105106
&mut retval,
106-
bytecode.as_ptr() as *const u8,
107+
bytecode.as_c_ptr() as *const u8,
107108
bytecode.len(),
108-
callable.as_ptr() as *const u8,
109+
callable.as_c_ptr() as *const u8,
109110
callable.len(),
110-
args.as_ptr() as *const u8,
111+
args.as_c_ptr() as *const u8,
111112
args.len(),
112113
&mut output as *mut u64,
113114
&mut delta_data_ptr as *mut u64,

enigma-core/enclave/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use enigma_tools_t::cryptography_t::asymmetric;
5555
use enigma_tools_t::{common, cryptography_t, quote_t};
5656
use enigma_tools_t::build_arguments_g::*;
5757
use enigma_types::EnclaveReturn;
58+
use enigma_types::traits::SliceCPtr;
5859

5960
use sgx_types::*;
6061
use std::string::ToString;
@@ -201,7 +202,7 @@ unsafe fn ecall_evm_internal(bytecode_slice: &[u8], callable_slice: &[u8], calla
201202
match res.0 {
202203
0 => {
203204
*result_len = callback_data.len();
204-
ptr::copy_nonoverlapping(callback_data.as_ptr(), output, callback_data.len());
205+
ptr::copy_nonoverlapping(callback_data.as_c_ptr(), output, callback_data.len());
205206
Ok(())
206207
}
207208
_ => {

enigma-core/enclave/src/ocalls_t.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use enigma_types::traits::SliceCPtr;
12
use enigma_tools_t::common::errors_t::EnclaveError;
23
use sgx_types::sgx_status_t;
34
use std::{path, str};
@@ -24,13 +25,7 @@ pub fn get_home_path() -> Result<path::PathBuf, EnclaveError> {
2425

2526
pub fn save_to_untrusted_memory(data: &[u8]) -> Result<u64, EnclaveError> {
2627
let mut ptr = 0u64;
27-
28-
//TODO: change this temporary solution for a problematic as_ptr implementation if empty vec
29-
let mut data = data.clone();
30-
if data.is_empty() {
31-
data = &[];
32-
}
33-
match unsafe { ocall_save_to_memory(&mut ptr as *mut u64, data.as_ptr(), data.len()) } {
28+
match unsafe { ocall_save_to_memory(&mut ptr as *mut u64, data.as_c_ptr(), data.len()) } {
3429
sgx_status_t::SGX_SUCCESS => Ok(ptr),
3530
e => Err(e.into()),
3631
}

enigma-principal/app/src/esgx/general.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use enigma_tools_u;
2+
13
use sgx_types::*;
24
use sgx_urts::SgxEnclave;
35
use std::io::{Read, Write};
@@ -6,7 +8,6 @@ use std::path;
68
use std::env;
79
use std::ptr;
810
use dirs;
9-
use enigma_tools_u;
1011

1112
static ENCLAVE_FILE: &'static str = "../bin/enclave.signed.so";
1213
static ENCLAVE_TOKEN: &'static str = "enclave.token";

enigma-runtime-t/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enigma-runtime-t/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
authors = ["Elichai Turkel <[email protected]>"]
55

66
[dependencies]
7+
enigma-types = { path = "../enigma-types" }
78
enigma-tools-t = { path = "../enigma-tools-t" }
89

910
etcommon-hexutil = { version = "0.2", default-features = false }

enigma-runtime-t/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern crate serde_json;
1111
extern crate serde_derive;
1212
extern crate serde;
1313
extern crate rmp_serde as rmps;
14+
extern crate enigma_types;
1415
extern crate enigma_tools_t;
1516
extern crate json_patch;
1617
extern crate wasmi;

enigma-runtime-t/src/ocalls_t.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use sgx_types::sgx_status_t;
22
use std::string::ToString;
3+
use enigma_types::traits::SliceCPtr;
34
use enigma_tools_t::common::errors_t::EnclaveError::{self, OcallError};
45
use enigma_tools_t::km_primitives::ContractAddress;
56
use crate::data::{EncryptedContractState, EncryptedPatch};
@@ -17,7 +18,7 @@ extern "C" {
1718
pub fn save_state(enc: &EncryptedContractState<u8>) -> Result<(), EnclaveError> {
1819
let mut res_int: i8 = -1;
1920
let res_status: sgx_status_t = unsafe {
20-
ocall_update_state(&mut res_int as *mut i8, &enc.contract_id, enc.json.as_ptr(), enc.json.len())
21+
ocall_update_state(&mut res_int as *mut i8, &enc.contract_id, enc.json.as_c_ptr(), enc.json.len())
2122
};
2223
match res_int {
2324
0 => (), // 0 is the OK result
@@ -33,7 +34,7 @@ pub fn save_state(enc: &EncryptedContractState<u8>) -> Result<(), EnclaveError>
3334
pub fn save_delta(enc: &EncryptedPatch) -> Result<(), EnclaveError> {
3435
let mut res_int: i8 = -1;
3536
let res_status: sgx_status_t = unsafe {
36-
ocall_new_delta(&mut res_int as *mut i8, enc.data.as_ptr(), enc.data.len(), &enc.contract_id, &enc.index as *const u32)
37+
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)
3738
};
3839

3940
// TODO: Maybe use some sort of ErrorKind to differentiate between the errors outside

enigma-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![no_std]
22

33
mod types;
4+
pub mod traits;
45

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

enigma-types/src/traits.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
static EMPTY: [u8; 1] = [0];
2+
3+
pub trait SliceCPtr {
4+
type Target;
5+
fn as_c_ptr(&self) -> *const Self::Target;
6+
}
7+
8+
impl<T> SliceCPtr for [T] {
9+
type Target = T;
10+
fn as_c_ptr(&self) -> *const Self::Target {
11+
if self.is_empty() {
12+
EMPTY.as_ptr() as *const _
13+
} else {
14+
self.as_ptr()
15+
}
16+
}
17+
}
18+
19+
impl SliceCPtr for str {
20+
type Target = u8;
21+
fn as_c_ptr(&self) -> *const Self::Target {
22+
if self.is_empty() {
23+
EMPTY.as_ptr() as *const _
24+
} else {
25+
self.as_ptr()
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)