Skip to content
Open
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
4 changes: 4 additions & 0 deletions samplecode/new_helloworld/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[alias]
xrun = "run --package xtask --"
make = "run --package xtask -- build"
# xbuild = "build --package xtask"
4 changes: 4 additions & 0 deletions samplecode/new_helloworld/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = ["app", "enclave", "edl","xtask"]
resolver = "2"

226 changes: 226 additions & 0 deletions samplecode/new_helloworld/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

######## SGX SDK Settings ########

SGX_SDK ?= /opt/intel/sgxsdk
SGX_MODE ?= HW
SGX_ARCH ?= x64

TOP_DIR := ../..
include $(TOP_DIR)/buildenv.mk

ifeq ($(shell getconf LONG_BIT), 32)
SGX_ARCH := x86
else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
SGX_ARCH := x86
endif

ifeq ($(SGX_ARCH), x86)
SGX_COMMON_CFLAGS := -m32
SGX_LIBRARY_PATH := $(SGX_SDK)/lib
SGX_BIN_PATH := $(SGX_SDK)/bin/x86
else
SGX_COMMON_CFLAGS := -m64
SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
SGX_BIN_PATH := $(SGX_SDK)/bin/x64
endif

ifeq ($(SGX_DEBUG), 1)
SGX_COMMON_CFLAGS += -O0 -g
Rust_Build_Flags :=
Rust_Build_Out := debug
else
SGX_COMMON_CFLAGS += -O2
Rust_Build_Flags := --release
Rust_Build_Out := release
endif

SGX_EDGER8R := $(SGX_BIN_PATH)/sgx_edger8r
ifneq ($(SGX_MODE), HYPER)
SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign
else
SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign_hyper
SGX_EDGER8R_MODE := --sgx-mode $(SGX_MODE)
endif

######## CUSTOM Settings ########

CUSTOM_LIBRARY_PATH := ./lib
CUSTOM_BIN_PATH := ./bin
CUSTOM_SYSROOT_PATH := ./sysroot
CUSTOM_EDL_PATH := $(ROOT_DIR)/sgx_edl/edl
CUSTOM_COMMON_PATH := $(ROOT_DIR)/common

######## EDL Settings ########

Enclave_EDL_Files := enclave/enclave_t.c enclave/enclave_t.h app/enclave_u.c app/enclave_u.h

######## APP Settings ########

App_Rust_Flags := $(Rust_Build_Flags)
App_Src_Files := $(shell find app/ -type f -name '*.rs') $(shell find app/ -type f -name 'Cargo.toml')
App_Include_Paths := -I ./app -I$(SGX_SDK)/include -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_EDL_PATH)
App_C_Flags := $(CFLAGS) $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)

App_Rust_Path := ./target/$(Rust_Build_Out)
App_Enclave_u_Object := $(CUSTOM_LIBRARY_PATH)/libenclave_u.a
App_Name := $(CUSTOM_BIN_PATH)/app

######## Enclave Settings ########

# BUILD_STD=no use no_std
# BUILD_STD=cargo use cargo-std-aware
# BUILD_STD=xargo use xargo
# BUILD_STD ?= cargo

Rust_Build_Target := x86_64-unknown-linux-sgx
Rust_Target_Path := $(ROOT_DIR)/rustlib

Rust_Build_Std := $(Rust_Build_Flags) -Zbuild-std=core,alloc
Rust_Std_Features :=
Rust_Target_Flags := --target $(Rust_Target_Path)/$(Rust_Build_Target).json
Rust_Sysroot_Path := $(CURDIR)/sysroot
Rust_Sysroot_Flags := RUSTFLAGS="--sysroot $(Rust_Sysroot_Path)"

RustEnclave_Build_Flags := $(Rust_Build_Flags)
RustEnclave_Src_Files := $(shell find enclave/ -type f -name '*.rs') $(shell find enclave/ -type f -name 'Cargo.toml')
RustEnclave_Include_Paths := -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_COMMON_PATH)/inc/tlibc -I$(CUSTOM_EDL_PATH)

RustEnclave_Link_Libs := -L$(CUSTOM_LIBRARY_PATH) -lenclave
RustEnclave_C_Flags := $(CFLAGS) $(ENCLAVE_CFLAGS) $(SGX_COMMON_CFLAGS) $(RustEnclave_Include_Paths)
RustEnclave_Link_Flags := -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles \
-Wl,--start-group $(RustEnclave_Link_Libs) -Wl,--end-group \
-Wl,--version-script=enclave/enclave.lds \
$(ENCLAVE_LDFLAGS)

# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Target)/$(Rust_Build_Out)
# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Out)
RustEnclave_Out_Path := ./target/$(Rust_Build_Out)
Rust_Out_Path := ./target/$(Rust_Build_Target)/$(Rust_Build_Out)

RustEnclave_Lib_Name := $(RustEnclave_Out_Path)/libenclave.a
# RustEnclave_Lib_Name := $(Rust_Out_Path)/libenclave.a
RustEnclave_Name := $(CUSTOM_BIN_PATH)/enclave.so
RustEnclave_Signed_Name := $(CUSTOM_BIN_PATH)/enclave.signed.so

.PHONY: all
# all: $(RustEnclave_Signed_Name)
all: app enclave sign

######## EDL Objects ########


$(Enclave_EDL_Files): $(SGX_EDGER8R) edl/enclave.edl
$(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --trusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --trusted-dir enclave
$(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --untrusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --untrusted-dir app
@echo "GEN => $(Enclave_EDL_Files)"

######## App Objects ########

# SYMBOLS := $(shell nm $@ | awk '$$2 == "t" && $$3 ~ /^enclave_.*_ocall$$/ {print $$3}')

app/enclave_u.o: $(Enclave_EDL_Files)
@$(CC) $(App_C_Flags) -c app/enclave_u.c -o $@
@$(OBJCOPY) --strip-symbol=ocall_table_enclave $@ $@
@nm $@ | awk '/enclave_.*_ocall/{print $$3}' | while read sym; do \
$(OBJCOPY) --globalize-symbol=$$sym $@; \
done

$(App_Enclave_u_Object): app/enclave_u.o
@mkdir -p $(CUSTOM_LIBRARY_PATH)
@$(AR) rcsD $@ $^

$(App_Name): $(App_Enclave_u_Object) app
@mkdir -p $(CUSTOM_BIN_PATH)
@cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app
@echo "LINK => $@"

######## Enclave Objects ########


enclave/enclave_t.o: $(Enclave_EDL_Files)
@$(CC) $(RustEnclave_C_Flags) -c enclave/enclave_t.c -o $@
# @$(OBJCOPY) --localize-symbol=g_ecall_table --strip-symbol=g_ecall_table $@ $@
@$(OBJCOPY) --localize-symbol=g_ecall_table --localize-symbol=g_dyn_entry_table $@ $@

# $(RustEnclave_Name): enclave/enclave_t.o enclave
$(RustEnclave_Name): enclave/enclave_t.o enclave
@mkdir -p $(CUSTOM_LIBRARY_PATH)
@mkdir -p $(CUSTOM_BIN_PATH)
@cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a
# @$(CXX) -o $@ $(RustEnclave_Link_Flags)
@$(CXX) enclave/enclave_t.o -o $@ $(RustEnclave_Link_Flags)
@echo "LINK => $@"

$(RustEnclave_Signed_Name): $(RustEnclave_Name) enclave/config.xml
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml
@echo "SIGN => $@"

######## Sign Enclave ########
.PHONY: enclave/config.xml
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml

######## Build App ########

.PHONY: app
app: $(App_Enclave_u_Object)
@cd app && SGX_SDK=$(SGX_SDK) cargo build $(App_Rust_Flags)
@mkdir -p $(CUSTOM_BIN_PATH)
@cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app

######## Build Enclave ########
.PHONY: enclave
enclave: enclave/enclave_t.o
@mkdir -p $(CUSTOM_LIBRARY_PATH)
@mkdir -p $(CUSTOM_BIN_PATH)
@cd enclave && cargo build $(RustEnclave_Build_Flags)
@cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a
$(CXX) enclave/enclave_t.o -o $(RustEnclave_Name) $(RustEnclave_Link_Flags)
@echo "LINK => $@"


.PHONY: enclave_std
enclave_std:
@mkdir -p $(CUSTOM_BIN_PATH)
@cd $(Rust_Target_Path)/std && cargo build $(Rust_Build_Std) $(Rust_Target_Flags) $(Rust_Std_Features)

@rm -rf $(Rust_Sysroot_Path)
@mkdir -p $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib
@cp -r $(Rust_Target_Path)/std/target/$(Rust_Build_Target)/$(Rust_Build_Out)/deps/* $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib

@cd enclave && $(Rust_Sysroot_Flags) cargo build $(Rust_Target_Flags) $(RustEnclave_Build_Flags)


######## Sign Enclave ########
sign: enclave/config.xml
@$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $(RustEnclave_Signed_Name) -config enclave/config.xml

######## Run Enclave ########

.PHONY: run
run: $(App_Name) $(RustEnclave_Signed_Name)
@echo -e '\n===== Run Enclave =====\n'
@cd bin && ./app

.PHONY: clean
clean:
@rm -f $(App_Name) $(RustEnclave_Name) $(RustEnclave_Signed_Name) enclave/*_t.* app/*_u.*
@cd enclave && cargo clean
@cd app && cargo clean
@cd $(Rust_Target_Path)/std && cargo clean
@rm -rf $(CUSTOM_BIN_PATH) $(CUSTOM_LIBRARY_PATH) $(CUSTOM_SYSROOT_PATH)
6 changes: 6 additions & 0 deletions samplecode/new_helloworld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


```
cargo run --package xtask build
cargo run --package xtask clean
```
14 changes: 14 additions & 0 deletions samplecode/new_helloworld/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "app"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
edl = { path = "../edl", default-features = false, features = ["app"] }
sgx_new_edl = { path = "../../../sgx_new_edl", default-features = false, features = [
"app",
] }
sgx_types = { path = "../../../sgx_types" }
sgx_urts = { path = "../../../sgx_urts" }
22 changes: 22 additions & 0 deletions samplecode/new_helloworld/app/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::env;

fn main() {
println!("cargo:rerun-if-env-changed=SGX_MODE");
println!("cargo:rerun-if-changed=build.rs");

let sdk_dir = env::var("SGX_SDK").unwrap_or_else(|_| "/opt/intel/sgxsdk".to_string());
let mode = env::var("SGX_MODE").unwrap_or_else(|_| "HW".to_string());

// let pwd = env::current_dir().unwrap();
// println!("cargo:rustc-link-search=native={}/../lib", pwd.display());
// println!("cargo:rustc-link-lib=static=enclave_u");

println!("cargo:rustc-link-search=native={}/lib64", sdk_dir);

match mode.as_ref() {
"SIM" | "SW" => println!("cargo:rustc-link-lib=dylib=sgx_urts_sim"),
"HYPER" => println!("cargo:rustc-link-lib=dylib=sgx_urts_hyper"),
"HW" => println!("cargo:rustc-link-lib=dylib=sgx_urts"),
_ => println!("cargo:rustc-link-lib=dylib=sgx_urts"),
}
}
57 changes: 57 additions & 0 deletions samplecode/new_helloworld/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use edl::ecalls;
use sgx_new_edl::{ocall, In, Out};

extern crate sgx_types;
extern crate sgx_urts;

use edl::ocalls;
use sgx_types::error::SgxStatus;
use sgx_types::types::*;
use sgx_urts::enclave::SgxEnclave;

static ENCLAVE_FILE: &str = "enclave.signed.so";

fn main() {
let enclave = match SgxEnclave::create(ENCLAVE_FILE, true) {
Ok(enclave) => {
println!("[+] Init Enclave Successful {}!", enclave.eid());
enclave
}
Err(err) => {
println!("[-] Init Enclave Failed {}!", err.as_str());
return;
}
};

let input_string = String::from("This is a normal world string passed into Enclave!\n");
let mut retval = SgxStatus::Success;

let a1 = String::new();
let a1 = In::new(&a1);
let mut o1 = String::with_capacity(100);
o1.push_str("Hello ");
let arg0 = Out::new(&mut o1);

let res = ecalls::foo::ecall(enclave.eid(), arg0);
println!("res: {}", res);
println!("o1: {}", o1);

// let result = unsafe {
// say_something(
// enclave.eid(),
// &mut retval,
// input_string.as_ptr() as *const u8,
// input_string.len(),
// )
// };
// match result {
// SgxStatus::Success => println!("[+] ECall Success..."),
// _ => println!("[-] ECall Enclave Failed {}!", result.as_str()),
// }
}

#[ocall]
fn bar(arg0: In<'_, String>) -> SgxStatus {
println!("bar: {}", arg0.get());
SgxStatus::Success
}
19 changes: 19 additions & 0 deletions samplecode/new_helloworld/edl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "edl"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
# crate-type = ["staticlib", "cdylib"]

[features]
default = ["enclave", "app"]
enclave = ["sgx_tstd", "sgx_new_edl/enclave"]
app = ["sgx_new_edl/app"]

[dependencies]
sgx_new_edl = { path = "../../../sgx_new_edl", default-features = false }
sgx_types = { path = "../../../sgx_types" }
sgx_tstd = { path = "../../../sgx_tstd", optional = true }
7 changes: 7 additions & 0 deletions samplecode/new_helloworld/edl/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::env;

fn main() {
let pwd = env::current_dir().unwrap();
println!("cargo:rustc-link-search=native={}/../lib", pwd.display());
println!("cargo:rustc-link-lib=static=enclave_u");
}
25 changes: 25 additions & 0 deletions samplecode/new_helloworld/edl/enclave.edl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

enclave {
from "sgx_stdio.edl" import *;
from "sgx_tstd.edl" import *;

trusted {
public sgx_status_t __do_nothing();
};
};
Loading