Skip to content

Commit 213d58e

Browse files
committed
automatically generate rust edl code
1 parent 2d1e0a4 commit 213d58e

File tree

43 files changed

+2100
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2100
-29
lines changed

samplecode/helloworld/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ $(RustEnclave_Name): enclave/enclave_t.o enclave
155155
@mkdir -p $(CUSTOM_LIBRARY_PATH)
156156
@mkdir -p $(CUSTOM_BIN_PATH)
157157
@cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a
158-
@$(CXX) enclave/enclave_t.o -o $@ $(RustEnclave_Link_Flags)
158+
$(CXX) enclave/enclave_t.o -o $@ $(RustEnclave_Link_Flags)
159159
@echo "LINK => $@"
160160

161161
$(RustEnclave_Signed_Name): $(RustEnclave_Name) enclave/config.xml

samplecode/helloworld/enclave/src/lib.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,9 @@ extern crate sgx_tstd as std;
2424
extern crate sgx_types;
2525

2626
use sgx_types::error::SgxStatus;
27-
use std::io::{self, Write};
28-
use std::slice;
29-
use std::string::String;
30-
use std::vec::Vec;
3127

3228
/// # Safety
3329
#[no_mangle]
3430
pub unsafe extern "C" fn say_something(some_string: *const u8, some_len: usize) -> SgxStatus {
35-
let str_slice = slice::from_raw_parts(some_string, some_len);
36-
let _ = io::stdout().write(str_slice);
37-
38-
// A sample &'static string
39-
let rust_raw_string = "This is a in-Enclave ";
40-
// An array
41-
let word: [u8; 4] = [82, 117, 115, 116];
42-
// An vector
43-
let word_vec: Vec<u8> = vec![32, 115, 116, 114, 105, 110, 103, 33];
44-
45-
// Construct a string from &'static string
46-
let mut hello_string = String::from(rust_raw_string);
47-
48-
// Iterate on word array
49-
for c in word.iter() {
50-
hello_string.push(*c as char);
51-
}
52-
53-
// Rust style convertion
54-
hello_string += String::from_utf8(word_vec).expect("Invalid UTF-8").as_str();
55-
56-
// Ocall to normal world for output
57-
println!("{}", &hello_string);
58-
5931
SgxStatus::Success
6032
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[alias]
2+
xrun = "run --package xtask --"
3+
make = "run --package xtask -- build"
4+
# xbuild = "build --package xtask"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = ["app", "enclave", "edl","xtask"]
3+
resolver = "2"
4+

samplecode/new_helloworld/Makefile

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
######## SGX SDK Settings ########
19+
20+
SGX_SDK ?= /opt/intel/sgxsdk
21+
SGX_MODE ?= HW
22+
SGX_ARCH ?= x64
23+
24+
TOP_DIR := ../..
25+
include $(TOP_DIR)/buildenv.mk
26+
27+
ifeq ($(shell getconf LONG_BIT), 32)
28+
SGX_ARCH := x86
29+
else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
30+
SGX_ARCH := x86
31+
endif
32+
33+
ifeq ($(SGX_ARCH), x86)
34+
SGX_COMMON_CFLAGS := -m32
35+
SGX_LIBRARY_PATH := $(SGX_SDK)/lib
36+
SGX_BIN_PATH := $(SGX_SDK)/bin/x86
37+
else
38+
SGX_COMMON_CFLAGS := -m64
39+
SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
40+
SGX_BIN_PATH := $(SGX_SDK)/bin/x64
41+
endif
42+
43+
ifeq ($(SGX_DEBUG), 1)
44+
SGX_COMMON_CFLAGS += -O0 -g
45+
Rust_Build_Flags :=
46+
Rust_Build_Out := debug
47+
else
48+
SGX_COMMON_CFLAGS += -O2
49+
Rust_Build_Flags := --release
50+
Rust_Build_Out := release
51+
endif
52+
53+
SGX_EDGER8R := $(SGX_BIN_PATH)/sgx_edger8r
54+
ifneq ($(SGX_MODE), HYPER)
55+
SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign
56+
else
57+
SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign_hyper
58+
SGX_EDGER8R_MODE := --sgx-mode $(SGX_MODE)
59+
endif
60+
61+
######## CUSTOM Settings ########
62+
63+
CUSTOM_LIBRARY_PATH := ./lib
64+
CUSTOM_BIN_PATH := ./bin
65+
CUSTOM_SYSROOT_PATH := ./sysroot
66+
CUSTOM_EDL_PATH := $(ROOT_DIR)/sgx_edl/edl
67+
CUSTOM_COMMON_PATH := $(ROOT_DIR)/common
68+
69+
######## EDL Settings ########
70+
71+
Enclave_EDL_Files := enclave/enclave_t.c enclave/enclave_t.h app/enclave_u.c app/enclave_u.h
72+
73+
######## APP Settings ########
74+
75+
App_Rust_Flags := $(Rust_Build_Flags)
76+
App_Src_Files := $(shell find app/ -type f -name '*.rs') $(shell find app/ -type f -name 'Cargo.toml')
77+
App_Include_Paths := -I ./app -I$(SGX_SDK)/include -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_EDL_PATH)
78+
App_C_Flags := $(CFLAGS) $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
79+
80+
App_Rust_Path := ./target/$(Rust_Build_Out)
81+
App_Enclave_u_Object := $(CUSTOM_LIBRARY_PATH)/libenclave_u.a
82+
App_Name := $(CUSTOM_BIN_PATH)/app
83+
84+
######## Enclave Settings ########
85+
86+
# BUILD_STD=no use no_std
87+
# BUILD_STD=cargo use cargo-std-aware
88+
# BUILD_STD=xargo use xargo
89+
# BUILD_STD ?= cargo
90+
91+
Rust_Build_Target := x86_64-unknown-linux-sgx
92+
Rust_Target_Path := $(ROOT_DIR)/rustlib
93+
94+
Rust_Build_Std := $(Rust_Build_Flags) -Zbuild-std=core,alloc
95+
Rust_Std_Features :=
96+
Rust_Target_Flags := --target $(Rust_Target_Path)/$(Rust_Build_Target).json
97+
Rust_Sysroot_Path := $(CURDIR)/sysroot
98+
Rust_Sysroot_Flags := RUSTFLAGS="--sysroot $(Rust_Sysroot_Path)"
99+
100+
RustEnclave_Build_Flags := $(Rust_Build_Flags)
101+
RustEnclave_Src_Files := $(shell find enclave/ -type f -name '*.rs') $(shell find enclave/ -type f -name 'Cargo.toml')
102+
RustEnclave_Include_Paths := -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_COMMON_PATH)/inc/tlibc -I$(CUSTOM_EDL_PATH)
103+
104+
RustEnclave_Link_Libs := -L$(CUSTOM_LIBRARY_PATH) -lenclave
105+
RustEnclave_C_Flags := $(CFLAGS) $(ENCLAVE_CFLAGS) $(SGX_COMMON_CFLAGS) $(RustEnclave_Include_Paths)
106+
RustEnclave_Link_Flags := -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles \
107+
-Wl,--start-group $(RustEnclave_Link_Libs) -Wl,--end-group \
108+
-Wl,--version-script=enclave/enclave.lds \
109+
$(ENCLAVE_LDFLAGS)
110+
111+
# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Target)/$(Rust_Build_Out)
112+
# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Out)
113+
RustEnclave_Out_Path := ./target/$(Rust_Build_Out)
114+
Rust_Out_Path := ./target/$(Rust_Build_Target)/$(Rust_Build_Out)
115+
116+
RustEnclave_Lib_Name := $(RustEnclave_Out_Path)/libenclave.a
117+
# RustEnclave_Lib_Name := $(Rust_Out_Path)/libenclave.a
118+
RustEnclave_Name := $(CUSTOM_BIN_PATH)/enclave.so
119+
RustEnclave_Signed_Name := $(CUSTOM_BIN_PATH)/enclave.signed.so
120+
121+
.PHONY: all
122+
# all: $(RustEnclave_Signed_Name)
123+
all: app enclave sign
124+
125+
######## EDL Objects ########
126+
127+
128+
$(Enclave_EDL_Files): $(SGX_EDGER8R) edl/enclave.edl
129+
$(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --trusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --trusted-dir enclave
130+
$(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --untrusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --untrusted-dir app
131+
@echo "GEN => $(Enclave_EDL_Files)"
132+
133+
######## App Objects ########
134+
135+
# SYMBOLS := $(shell nm $@ | awk '$$2 == "t" && $$3 ~ /^enclave_.*_ocall$$/ {print $$3}')
136+
137+
app/enclave_u.o: $(Enclave_EDL_Files)
138+
@$(CC) $(App_C_Flags) -c app/enclave_u.c -o $@
139+
@$(OBJCOPY) --strip-symbol=ocall_table_enclave $@ $@
140+
@nm $@ | awk '/enclave_.*_ocall/{print $$3}' | while read sym; do \
141+
$(OBJCOPY) --globalize-symbol=$$sym $@; \
142+
done
143+
144+
$(App_Enclave_u_Object): app/enclave_u.o
145+
@mkdir -p $(CUSTOM_LIBRARY_PATH)
146+
@$(AR) rcsD $@ $^
147+
148+
$(App_Name): $(App_Enclave_u_Object) app
149+
@mkdir -p $(CUSTOM_BIN_PATH)
150+
@cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app
151+
@echo "LINK => $@"
152+
153+
######## Enclave Objects ########
154+
155+
156+
enclave/enclave_t.o: $(Enclave_EDL_Files)
157+
@$(CC) $(RustEnclave_C_Flags) -c enclave/enclave_t.c -o $@
158+
# @$(OBJCOPY) --localize-symbol=g_ecall_table --strip-symbol=g_ecall_table $@ $@
159+
@$(OBJCOPY) --localize-symbol=g_ecall_table --localize-symbol=g_dyn_entry_table $@ $@
160+
161+
# $(RustEnclave_Name): enclave/enclave_t.o enclave
162+
$(RustEnclave_Name): enclave/enclave_t.o enclave
163+
@mkdir -p $(CUSTOM_LIBRARY_PATH)
164+
@mkdir -p $(CUSTOM_BIN_PATH)
165+
@cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a
166+
# @$(CXX) -o $@ $(RustEnclave_Link_Flags)
167+
@$(CXX) enclave/enclave_t.o -o $@ $(RustEnclave_Link_Flags)
168+
@echo "LINK => $@"
169+
170+
$(RustEnclave_Signed_Name): $(RustEnclave_Name) enclave/config.xml
171+
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml
172+
@echo "SIGN => $@"
173+
174+
######## Sign Enclave ########
175+
.PHONY: enclave/config.xml
176+
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml
177+
178+
######## Build App ########
179+
180+
.PHONY: app
181+
app: $(App_Enclave_u_Object)
182+
@cd app && SGX_SDK=$(SGX_SDK) cargo build $(App_Rust_Flags)
183+
@mkdir -p $(CUSTOM_BIN_PATH)
184+
@cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app
185+
186+
######## Build Enclave ########
187+
.PHONY: enclave
188+
enclave: enclave/enclave_t.o
189+
@mkdir -p $(CUSTOM_LIBRARY_PATH)
190+
@mkdir -p $(CUSTOM_BIN_PATH)
191+
@cd enclave && cargo build $(RustEnclave_Build_Flags)
192+
@cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a
193+
$(CXX) enclave/enclave_t.o -o $(RustEnclave_Name) $(RustEnclave_Link_Flags)
194+
@echo "LINK => $@"
195+
196+
197+
.PHONY: enclave_std
198+
enclave_std:
199+
@mkdir -p $(CUSTOM_BIN_PATH)
200+
@cd $(Rust_Target_Path)/std && cargo build $(Rust_Build_Std) $(Rust_Target_Flags) $(Rust_Std_Features)
201+
202+
@rm -rf $(Rust_Sysroot_Path)
203+
@mkdir -p $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib
204+
@cp -r $(Rust_Target_Path)/std/target/$(Rust_Build_Target)/$(Rust_Build_Out)/deps/* $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib
205+
206+
@cd enclave && $(Rust_Sysroot_Flags) cargo build $(Rust_Target_Flags) $(RustEnclave_Build_Flags)
207+
208+
209+
######## Sign Enclave ########
210+
sign: enclave/config.xml
211+
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $(RustEnclave_Signed_Name) -config enclave/config.xml
212+
213+
######## Run Enclave ########
214+
215+
.PHONY: run
216+
run: $(App_Name) $(RustEnclave_Signed_Name)
217+
@echo -e '\n===== Run Enclave =====\n'
218+
@cd bin && ./app
219+
220+
.PHONY: clean
221+
clean:
222+
@rm -f $(App_Name) $(RustEnclave_Name) $(RustEnclave_Signed_Name) enclave/*_t.* app/*_u.*
223+
@cd enclave && cargo clean
224+
@cd app && cargo clean
225+
@cd $(Rust_Target_Path)/std && cargo clean
226+
@rm -rf $(CUSTOM_BIN_PATH) $(CUSTOM_LIBRARY_PATH) $(CUSTOM_SYSROOT_PATH)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
```
4+
cargo run --package xtask build
5+
cargo run --package xtask clean
6+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "app"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
edl = { path = "../edl", default-features = false, features = ["app"] }
10+
sgx_new_edl = { path = "../../../sgx_new_edl", default-features = false, features = [
11+
"app",
12+
] }
13+
sgx_types = { path = "../../../sgx_types" }
14+
sgx_urts = { path = "../../../sgx_urts" }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-env-changed=SGX_MODE");
5+
println!("cargo:rerun-if-changed=build.rs");
6+
7+
let sdk_dir = env::var("SGX_SDK").unwrap_or_else(|_| "/opt/intel/sgxsdk".to_string());
8+
let mode = env::var("SGX_MODE").unwrap_or_else(|_| "HW".to_string());
9+
10+
// let pwd = env::current_dir().unwrap();
11+
// println!("cargo:rustc-link-search=native={}/../lib", pwd.display());
12+
// println!("cargo:rustc-link-lib=static=enclave_u");
13+
14+
println!("cargo:rustc-link-search=native={}/lib64", sdk_dir);
15+
16+
match mode.as_ref() {
17+
"SIM" | "SW" => println!("cargo:rustc-link-lib=dylib=sgx_urts_sim"),
18+
"HYPER" => println!("cargo:rustc-link-lib=dylib=sgx_urts_hyper"),
19+
"HW" => println!("cargo:rustc-link-lib=dylib=sgx_urts"),
20+
_ => println!("cargo:rustc-link-lib=dylib=sgx_urts"),
21+
}
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use edl::ecalls;
2+
use sgx_new_edl::{ocall, In, Out};
3+
4+
extern crate sgx_types;
5+
extern crate sgx_urts;
6+
7+
use edl::ocalls;
8+
use sgx_types::error::SgxStatus;
9+
use sgx_types::types::*;
10+
use sgx_urts::enclave::SgxEnclave;
11+
12+
static ENCLAVE_FILE: &str = "enclave.signed.so";
13+
14+
fn main() {
15+
let enclave = match SgxEnclave::create(ENCLAVE_FILE, true) {
16+
Ok(enclave) => {
17+
println!("[+] Init Enclave Successful {}!", enclave.eid());
18+
enclave
19+
}
20+
Err(err) => {
21+
println!("[-] Init Enclave Failed {}!", err.as_str());
22+
return;
23+
}
24+
};
25+
26+
let input_string = String::from("This is a normal world string passed into Enclave!\n");
27+
let mut retval = SgxStatus::Success;
28+
29+
let a1 = String::new();
30+
let a1 = In::new(&a1);
31+
let mut o1 = String::with_capacity(100);
32+
o1.push_str("Hello ");
33+
let arg0 = Out::new(&mut o1);
34+
35+
let res = ecalls::foo::ecall(enclave.eid(), arg0);
36+
println!("res: {}", res);
37+
println!("o1: {}", o1);
38+
39+
// let result = unsafe {
40+
// say_something(
41+
// enclave.eid(),
42+
// &mut retval,
43+
// input_string.as_ptr() as *const u8,
44+
// input_string.len(),
45+
// )
46+
// };
47+
// match result {
48+
// SgxStatus::Success => println!("[+] ECall Success..."),
49+
// _ => println!("[-] ECall Enclave Failed {}!", result.as_str()),
50+
// }
51+
}
52+
53+
#[ocall]
54+
fn bar(arg0: In<'_, String>) -> SgxStatus {
55+
println!("bar: {}", arg0.get());
56+
SgxStatus::Success
57+
}

0 commit comments

Comments
 (0)