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

Commit bc68dc0

Browse files
authored
Auto generate ecalls FFI rust code using bindgen in enigma-core enclave (#166)
Auto generate ecalls FFI rust code using bindgen in enigma-core enclave
2 parents 916203c + edec707 commit bc68dc0

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

dockerfile/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ RUN apt-get update && \
1313
&& rm -rf /var/lib/apt/lists/*
1414

1515
RUN /root/.cargo/bin/rustup target add wasm32-unknown-unknown && \
16+
/root/.cargo/bin/rustup component add rustfmt && \
17+
/root/.cargo/bin/cargo install bindgen cargo-audit && \
1618
rm -rf /root/.cargo/registry && rm -rf /root/.cargo/git
1719

1820

enigma-core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ $(Signed_RustEnclave_Name): $(RustEnclave_Name)
139139
@echo "SIGN => $@"
140140

141141
.PHONY: enclave
142-
enclave:
142+
enclave: $(Enclave_EDL_Files)
143143
mkdir -p ./lib
144144
$(MAKE) -C ./enclave/ CARGO_FLAGS=$(App_Rust_Flags) Rust_target_dir=$(Rust_target_dir)
145145

enigma-core/enclave/Makefile

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
# Because build-dependencies and regular dependencies are mixed together it's not possible to import
2+
# bindgen into the enclave's build.rs (https://github.com/rust-lang/cargo/issues/2589)
3+
# The solution is to install the bindgen CLI in the docker and use it manually in the Makefile.
14

25
Rust_Enclave_Name := libenclave.a
36
Rust_Enclave_Files := $(wildcard src/*.rs)
47

5-
.PHONY: all
8+
BINDGEN_OUTPUT_FILE := src/auto_ffi.rs
9+
BINDGEN_RAW_LINES := "\#![allow(dead_code)] use enigma_types::*; use sgx_types::*;"
10+
BINDGEN_CLANG_FLAGS := -I/opt/sgxsdk/include -I$(HOME)/sgx/edl
11+
BINDGEN_FLAGS := --default-enum-style=rust --rust-target=nightly \
12+
--no-recursive-whitelist --use-array-pointers-in-arguments \
13+
--whitelist-function ocall_.* --raw-line $(BINDGEN_RAW_LINES)
614

7-
all: $(Rust_Enclave_Name)
15+
16+
all: bindgen $(Rust_Enclave_Name)
817

918
$(Rust_Enclave_Name): $(Rust_Enclave_Files)
1019
ifeq ($(XARGO_SGX), 1)
@@ -13,4 +22,11 @@ ifeq ($(XARGO_SGX), 1)
1322
else
1423
cargo build $(CARGO_FLAGS)
1524
cp ./target/$(Rust_target_dir)/libenigmacoreenclave.a ../lib/libenclave.a
16-
endif
25+
endif
26+
27+
28+
.PHONY: bindgen
29+
bindgen: Enclave_t.h
30+
cargo build -p enigma-types # Meant to make sure `enigma-types.h` already exists and can be included.
31+
bindgen Enclave_t.h $(BINDGEN_FLAGS) -- $(BINDGEN_CLANG_FLAGS) > $(BINDGEN_OUTPUT_FILE)
32+
rustfmt $(BINDGEN_OUTPUT_FILE)

enigma-core/enclave/src/auto_ffi.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// automatically generated by rust-bindgen
2+
3+
#![allow(dead_code)]
4+
use enigma_types::*;
5+
use sgx_types::*;
6+
7+
extern "C" {
8+
pub fn ocall_get_home(output: *mut u8, result_length: *mut usize) -> sgx_status_t;
9+
}
10+
extern "C" {
11+
pub fn ocall_update_state(
12+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, contract_address: *const ContractAddress, enc_state: *const u8,
13+
len: usize,
14+
) -> sgx_status_t;
15+
}
16+
extern "C" {
17+
pub fn ocall_new_delta(
18+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, enc_delta: *const u8, len: usize,
19+
contract_address: *const ContractAddress, delta_index: *mut u32,
20+
) -> sgx_status_t;
21+
}
22+
extern "C" {
23+
pub fn ocall_save_to_memory(retval: *mut u64, data_ptr: *const u8, data_len: usize) -> sgx_status_t;
24+
}
25+
extern "C" {
26+
pub fn ocall_get_deltas_sizes(
27+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, start: *const u32, end: *const u32,
28+
res_ptr: *mut usize, res_len: usize,
29+
) -> sgx_status_t;
30+
}
31+
extern "C" {
32+
pub fn ocall_get_deltas(
33+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, start: *const u32, end: *const u32,
34+
res_ptr: *mut u8, res_len: usize,
35+
) -> sgx_status_t;
36+
}
37+
extern "C" {
38+
pub fn ocall_get_state_size(
39+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, state_size: *mut usize,
40+
) -> sgx_status_t;
41+
}
42+
extern "C" {
43+
pub fn ocall_get_state(
44+
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, state_pt: *mut u8, state_len: usize,
45+
) -> sgx_status_t;
46+
}

0 commit comments

Comments
 (0)