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

Auto generate ecalls FFI rust code using bindgen in enigma-core enclave #166

Merged
merged 4 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dockerfile/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ RUN apt-get update && \
&& rm -rf /var/lib/apt/lists/*

RUN /root/.cargo/bin/rustup target add wasm32-unknown-unknown && \
/root/.cargo/bin/rustup component add rustfmt && \
/root/.cargo/bin/cargo install bindgen cargo-audit && \
rm -rf /root/.cargo/registry && rm -rf /root/.cargo/git


Expand Down
2 changes: 1 addition & 1 deletion enigma-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ $(Signed_RustEnclave_Name): $(RustEnclave_Name)
@echo "SIGN => $@"

.PHONY: enclave
enclave:
enclave: $(Enclave_EDL_Files)
mkdir -p ./lib
$(MAKE) -C ./enclave/ CARGO_FLAGS=$(App_Rust_Flags) Rust_target_dir=$(Rust_target_dir)

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

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

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

all: $(Rust_Enclave_Name)

all: bindgen $(Rust_Enclave_Name)

$(Rust_Enclave_Name): $(Rust_Enclave_Files)
ifeq ($(XARGO_SGX), 1)
Expand All @@ -13,4 +22,11 @@ ifeq ($(XARGO_SGX), 1)
else
cargo build $(CARGO_FLAGS)
cp ./target/$(Rust_target_dir)/libenigmacoreenclave.a ../lib/libenclave.a
endif
endif


.PHONY: bindgen
bindgen: Enclave_t.h
cargo build -p enigma-types # Meant to make sure `enigma-types.h` already exists and can be included.
bindgen Enclave_t.h $(BINDGEN_FLAGS) -- $(BINDGEN_CLANG_FLAGS) > $(BINDGEN_OUTPUT_FILE)
rustfmt $(BINDGEN_OUTPUT_FILE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you use rustfmt option of builder in #165?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll recheck but I'm pretty sure that it doesn't run on the added raw_lines which I add them all at the same line, so rustfmt will sort them later nicely

46 changes: 46 additions & 0 deletions enigma-core/enclave/src/auto_ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// automatically generated by rust-bindgen

#![allow(dead_code)]
use enigma_types::*;
use sgx_types::*;

extern "C" {
pub fn ocall_get_home(output: *mut u8, result_length: *mut usize) -> sgx_status_t;
}
extern "C" {
pub fn ocall_update_state(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, contract_address: *const ContractAddress, enc_state: *const u8,
len: usize,
) -> sgx_status_t;
}
extern "C" {
pub fn ocall_new_delta(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, enc_delta: *const u8, len: usize,
contract_address: *const ContractAddress, delta_index: *mut u32,
) -> sgx_status_t;
}
extern "C" {
pub fn ocall_save_to_memory(retval: *mut u64, data_ptr: *const u8, data_len: usize) -> sgx_status_t;
}
extern "C" {
pub fn ocall_get_deltas_sizes(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, start: *const u32, end: *const u32,
res_ptr: *mut usize, res_len: usize,
) -> sgx_status_t;
}
extern "C" {
pub fn ocall_get_deltas(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, start: *const u32, end: *const u32,
res_ptr: *mut u8, res_len: usize,
) -> sgx_status_t;
}
extern "C" {
pub fn ocall_get_state_size(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, state_size: *mut usize,
) -> sgx_status_t;
}
extern "C" {
pub fn ocall_get_state(
retval: *mut EnclaveReturn, db_ptr: *const RawPointer, addr: *const ContractAddress, state_pt: *mut u8, state_len: usize,
) -> sgx_status_t;
}