You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2020/01/24 03:30:56 UTC

[incubator-teaclave] branch develop updated: [binder] Simplify binder implementation and reduce dependency of service_app_utils

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git


The following commit(s) were added to refs/heads/develop by this push:
     new 0c064af  [binder] Simplify binder implementation and reduce dependency of service_app_utils
0c064af is described below

commit 0c064af2dc58a738f464b968f424b0e848baaaad
Author: Mingshen Sun <bo...@mssun.me>
AuthorDate: Thu Jan 23 19:23:32 2020 -0800

    [binder] Simplify binder implementation and reduce dependency of service_app_utils
---
 binder/src/binder.rs                       | 21 ++++-----------------
 services/authentication/app/Cargo.toml     |  1 -
 services/authentication/app/src/main.rs    | 11 +++++------
 services/database/app/Cargo.toml           |  1 -
 services/database/app/src/main.rs          | 12 +++++-------
 services/execution/app/Cargo.toml          |  1 -
 services/execution/app/src/main.rs         | 11 +++++------
 tests/functional_tests/app/Cargo.toml      |  1 -
 tests/functional_tests/app/src/main.rs     | 11 +++++------
 tests/integration_tests/app/Cargo.toml     |  1 -
 tests/integration_tests/app/src/main.rs    | 12 ++++++------
 tests/integration_tests/enclave/src/lib.rs |  1 +
 tests/unit_tests/app/Cargo.toml            |  1 -
 tests/unit_tests/app/src/main.rs           | 12 ++++++------
 utils/service_app_utils/src/lib.rs         | 28 +++-------------------------
 15 files changed, 40 insertions(+), 85 deletions(-)

diff --git a/binder/src/binder.rs b/binder/src/binder.rs
index 7d025d3..24cef88 100644
--- a/binder/src/binder.rs
+++ b/binder/src/binder.rs
@@ -30,29 +30,16 @@ use teaclave_ipc::IpcSender;
 
 static ENCLAVE_FILE_SUFFIX: &str = "_enclave.signed.so";
 
-use std::sync::Arc;
-#[derive(Clone)]
 pub struct TeeBinder {
-    name: String,
-    debug_launch: i32,
-    enclave_id: sgx_enclave_id_t,
-    enclave: Arc<SgxEnclave>,
+    enclave: SgxEnclave,
 }
 
 impl TeeBinder {
     pub fn new(name: &str, debug_launch: i32) -> Result<TeeBinder> {
-        let name = name.to_string();
         let enclave = init_enclave(&name, debug_launch)?;
-        let enclave_id = enclave.geteid();
+        debug!("EnclaveID: {}", enclave.geteid());
 
-        let tee = TeeBinder {
-            name,
-            debug_launch,
-            enclave: Arc::new(enclave),
-            enclave_id,
-        };
-
-        debug!("EnclaveID: {}", enclave_id);
+        let tee = TeeBinder { enclave };
 
         let args_info = InitEnclaveInput::default();
         let _ret_info = tee.invoke::<InitEnclaveInput, InitEnclaveOutput>(
@@ -68,7 +55,7 @@ impl TeeBinder {
         U: Serialize,
         V: DeserializeOwned,
     {
-        let mut channel = ECallChannel::new(self.enclave_id);
+        let mut channel = ECallChannel::new(self.enclave.geteid());
         channel.invoke::<U, V>(cmd, args_info)
     }
 
diff --git a/services/authentication/app/Cargo.toml b/services/authentication/app/Cargo.toml
index 925acfd..6146a88 100644
--- a/services/authentication/app/Cargo.toml
+++ b/services/authentication/app/Cargo.toml
@@ -13,7 +13,6 @@ env_logger = { version = "0.7.1" }
 anyhow     = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 teaclave_config            = { path = "../../../config" }
 
diff --git a/services/authentication/app/src/main.rs b/services/authentication/app/src/main.rs
index 05d9b4d..00620b2 100644
--- a/services/authentication/app/src/main.rs
+++ b/services/authentication/app/src/main.rs
@@ -25,18 +25,17 @@ use anyhow;
 use std::net::TcpListener;
 use std::os::unix::io::IntoRawFd;
 
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
-    run(tee)?;
+    env_logger::init();
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
+    run(&tee)?;
 
     Ok(())
 }
 
-fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_service(tee: &TeeBinder) -> anyhow::Result<()> {
     info!("Start enclave service");
     let config =
         teaclave_config::runtime_config::RuntimeConfig::from_toml("runtime.config.toml").unwrap();
@@ -50,7 +49,7 @@ fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
     Ok(())
 }
 
-fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn run(tee: &TeeBinder) -> anyhow::Result<()> {
     start_enclave_service(tee)?;
 
     Ok(())
diff --git a/services/database/app/Cargo.toml b/services/database/app/Cargo.toml
index 9579407..d016d69 100644
--- a/services/database/app/Cargo.toml
+++ b/services/database/app/Cargo.toml
@@ -13,7 +13,6 @@ env_logger = { version = "0.7.1" }
 anyhow     = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 teaclave_config            = { path = "../../../config" }
 
diff --git a/services/database/app/src/main.rs b/services/database/app/src/main.rs
index 47fabb3..e14fb52 100644
--- a/services/database/app/src/main.rs
+++ b/services/database/app/src/main.rs
@@ -25,20 +25,18 @@ use anyhow;
 use std::net::TcpListener;
 use std::os::unix::io::IntoRawFd;
 
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
 use teaclave_config;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
-    println!("{}", env!("CARGO_PKG_NAME"));
-    run(tee)?;
+    env_logger::init();
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
+    run(&tee)?;
 
     Ok(())
 }
 
-fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_service(tee: &TeeBinder) -> anyhow::Result<()> {
     info!("Start enclave service");
     let config =
         teaclave_config::runtime_config::RuntimeConfig::from_toml("runtime.config.toml").unwrap();
@@ -52,7 +50,7 @@ fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
     Ok(())
 }
 
-fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn run(tee: &TeeBinder) -> anyhow::Result<()> {
     start_enclave_service(tee)?;
 
     Ok(())
diff --git a/services/execution/app/Cargo.toml b/services/execution/app/Cargo.toml
index 7fa48f9..00b36e0 100644
--- a/services/execution/app/Cargo.toml
+++ b/services/execution/app/Cargo.toml
@@ -16,7 +16,6 @@ env_logger  = { version = "0.7.1" }
 anyhow      = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 teaclave_config            = { path = "../../../config" }
 
diff --git a/services/execution/app/src/main.rs b/services/execution/app/src/main.rs
index 8bbf059..c5186c2 100644
--- a/services/execution/app/src/main.rs
+++ b/services/execution/app/src/main.rs
@@ -24,17 +24,16 @@ use anyhow;
 use std::net::TcpListener;
 use std::os::unix::io::IntoRawFd;
 
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
-    run(tee)?;
+    env_logger::init();
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
+    run(&tee)?;
     Ok(())
 }
 
-fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_service(tee: &TeeBinder) -> anyhow::Result<()> {
     info!("Start enclave service");
     let config =
         teaclave_config::runtime_config::RuntimeConfig::from_toml("runtime.config.toml").unwrap();
@@ -48,7 +47,7 @@ fn start_enclave_service(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
     Ok(())
 }
 
-fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn run(tee: &TeeBinder) -> anyhow::Result<()> {
     start_enclave_service(tee)?;
     Ok(())
 }
diff --git a/tests/functional_tests/app/Cargo.toml b/tests/functional_tests/app/Cargo.toml
index df64f86..76ffb97 100644
--- a/tests/functional_tests/app/Cargo.toml
+++ b/tests/functional_tests/app/Cargo.toml
@@ -13,7 +13,6 @@ env_logger = { version = "0.7.1" }
 anyhow     = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 
 sgx_types = { version = "1.1.0" }
diff --git a/tests/functional_tests/app/src/main.rs b/tests/functional_tests/app/src/main.rs
index a3d4909..6746ec6 100644
--- a/tests/functional_tests/app/src/main.rs
+++ b/tests/functional_tests/app/src/main.rs
@@ -16,27 +16,26 @@
 // under the License.
 
 use anyhow;
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
 use teaclave_ipc::protos::ecall::{RunTestInput, RunTestOutput};
 use teaclave_ipc::protos::ECallCommand;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
-    run(tee)?;
+    env_logger::init();
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
+    run(&tee)?;
 
     Ok(())
 }
 
-fn start_enclave_unit_test_driver(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_unit_test_driver(tee: &TeeBinder) -> anyhow::Result<()> {
     let cmd = ECallCommand::RunTest;
     let _ = tee.invoke::<RunTestInput, RunTestOutput>(cmd.into(), RunTestInput);
 
     Ok(())
 }
 
-fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn run(tee: &TeeBinder) -> anyhow::Result<()> {
     start_enclave_unit_test_driver(tee)?;
 
     Ok(())
diff --git a/tests/integration_tests/app/Cargo.toml b/tests/integration_tests/app/Cargo.toml
index 7abf424..9485345 100644
--- a/tests/integration_tests/app/Cargo.toml
+++ b/tests/integration_tests/app/Cargo.toml
@@ -13,7 +13,6 @@ env_logger = { version = "0.7.1" }
 anyhow     = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 teaclave_config            = { path = "../../../config" }
 teaclave_test_utils        = { path = "../../test_utils" }
diff --git a/tests/integration_tests/app/src/main.rs b/tests/integration_tests/app/src/main.rs
index 1b80635..fd99511 100644
--- a/tests/integration_tests/app/src/main.rs
+++ b/tests/integration_tests/app/src/main.rs
@@ -16,25 +16,25 @@
 // under the License.
 
 use anyhow;
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
 use teaclave_ipc::protos::ecall::{RunTestInput, RunTestOutput};
 use teaclave_ipc::protos::ECallCommand;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 mod teaclave_config_tests;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
+    env_logger::init();
+
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
     run_app_integration_tests();
-    start_enclave_integration_tests(tee)?;
+    start_enclave_integration_tests(&tee)?;
 
     Ok(())
 }
 
-fn start_enclave_integration_tests(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_integration_tests(tee: &TeeBinder) -> anyhow::Result<()> {
     let cmd = ECallCommand::RunTest;
-    let _ = tee.invoke::<RunTestInput, RunTestOutput>(cmd.into(), RunTestInput);
+    tee.invoke::<RunTestInput, RunTestOutput>(cmd.into(), RunTestInput)?;
 
     Ok(())
 }
diff --git a/tests/integration_tests/enclave/src/lib.rs b/tests/integration_tests/enclave/src/lib.rs
index efbbcda..ceadea7 100644
--- a/tests/integration_tests/enclave/src/lib.rs
+++ b/tests/integration_tests/enclave/src/lib.rs
@@ -45,6 +45,7 @@ fn handle_run_test(_args: &RunTestInput) -> Result<RunTestOutput> {
     rusty_leveldb_sgx::run_tests();
     protected_fs_rs::run_tests();
     teaclave_rpc::run_tests();
+
     Ok(RunTestOutput::default())
 }
 
diff --git a/tests/unit_tests/app/Cargo.toml b/tests/unit_tests/app/Cargo.toml
index 5fb72d9..cc9e0b7 100644
--- a/tests/unit_tests/app/Cargo.toml
+++ b/tests/unit_tests/app/Cargo.toml
@@ -13,7 +13,6 @@ env_logger = { version = "0.7.1" }
 anyhow     = { version = "1.0.26" }
 
 teaclave_ipc               = { path = "../../../ipc" }
-teaclave_service_app_utils = { path = "../../../utils/service_app_utils" }
 teaclave_binder            = { path = "../../../binder" }
 
 sgx_types = { version = "1.1.0" }
diff --git a/tests/unit_tests/app/src/main.rs b/tests/unit_tests/app/src/main.rs
index a3d4909..e1f9ccd 100644
--- a/tests/unit_tests/app/src/main.rs
+++ b/tests/unit_tests/app/src/main.rs
@@ -16,27 +16,27 @@
 // under the License.
 
 use anyhow;
-use std::sync::Arc;
 use teaclave_binder::TeeBinder;
 use teaclave_ipc::protos::ecall::{RunTestInput, RunTestOutput};
 use teaclave_ipc::protos::ECallCommand;
-use teaclave_service_app_utils::ServiceEnclaveBuilder;
 
 fn main() -> anyhow::Result<()> {
-    let tee = ServiceEnclaveBuilder::init_tee_binder(env!("CARGO_PKG_NAME"))?;
-    run(tee)?;
+    env_logger::init();
+
+    let tee = TeeBinder::new(env!("CARGO_PKG_NAME"), 1)?;
+    run(&tee)?;
 
     Ok(())
 }
 
-fn start_enclave_unit_test_driver(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn start_enclave_unit_test_driver(tee: &TeeBinder) -> anyhow::Result<()> {
     let cmd = ECallCommand::RunTest;
     let _ = tee.invoke::<RunTestInput, RunTestOutput>(cmd.into(), RunTestInput);
 
     Ok(())
 }
 
-fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
+fn run(tee: &TeeBinder) -> anyhow::Result<()> {
     start_enclave_unit_test_driver(tee)?;
 
     Ok(())
diff --git a/utils/service_app_utils/src/lib.rs b/utils/service_app_utils/src/lib.rs
index ed22023..1202dd1 100644
--- a/utils/service_app_utils/src/lib.rs
+++ b/utils/service_app_utils/src/lib.rs
@@ -1,3 +1,4 @@
+use anyhow::Result;
 use log::{error, info};
 use std::sync::Arc;
 use teaclave_binder::TeeBinder;
@@ -5,32 +6,9 @@ use teaclave_binder::TeeBinder;
 pub struct ServiceEnclaveBuilder;
 
 impl ServiceEnclaveBuilder {
-    pub fn init_tee_binder(enclave_name: &str) -> anyhow::Result<std::sync::Arc<TeeBinder>> {
+    pub fn init_tee_binder(enclave_name: &str) -> Result<TeeBinder> {
         env_logger::init();
 
-        let tee = match TeeBinder::new(enclave_name, 1) {
-            Ok(r) => {
-                info!("Init TEE Successfully!");
-                r
-            }
-            Err(x) => {
-                error!("Init TEE Failed {}!", x);
-                std::process::exit(-1)
-            }
-        };
-
-        let tee = Arc::new(tee);
-
-        {
-            let ref_tee = tee.clone();
-            ctrlc::set_handler(move || {
-                info!("CTRL+C pressed. Destroying service enclave");
-                ref_tee.finalize();
-                std::process::exit(0);
-            })
-            .expect("Error setting Ctrl-C handler");
-        }
-
-        Ok(tee)
+        TeeBinder::new(enclave_name, 1)
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org