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 897adf2  [worker] Implement MesaPy IO and FFI (#223)
897adf2 is described below

commit 897adf202639e322a39ed7a8439f7dc8f9405ae1
Author: Zhaofeng Chen <[email protected]>
AuthorDate: Wed Feb 12 18:11:14 2020 -0800

    [worker] Implement MesaPy IO and FFI (#223)
---
 CMakeLists.txt                 |  2 +-
 worker/src/function/context.rs | 63 ++++++++++++++++++++++++++++++------------
 worker/src/function/mesapy.rs  | 48 ++++++++++++++++++++++++++++----
 3 files changed, 90 insertions(+), 23 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 28c8ad1..028f2ea 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,7 @@ set_strvar_from_env_or(SGX_SDK "/opt/sgxsdk" "Path of SGX 
SDK")
 set_strvar_from_env_or(RUSTFLAGS "" "Rust flags")
 set_strvar_from_env_or(MESATEE_CMAKE_DBG ""
                        "set to turn on debug message for cmake")
-set(MESAPY_VERSION 2b7ea38773d337d106c613a239661a60a4fa7528)
+set(MESAPY_VERSION 668d16ff4bf3389f67f238e72e687aa4de342bde)
 option(COV "Turn on/off coverage" OFF)
 option(OFFLINE "Turn on/off cargo offline" ON)
 option(TEST_MODE "Turn on/off test mode" OFF)
diff --git a/worker/src/function/context.rs b/worker/src/function/context.rs
index f0ac184..e2db011 100644
--- a/worker/src/function/context.rs
+++ b/worker/src/function/context.rs
@@ -5,7 +5,7 @@ use std::cell::RefCell;
 use std::slice;
 use std::thread_local;
 
-use sgx_types::{c_char, c_int, c_uchar, size_t};
+use sgx_types::{c_char, c_int, c_uchar, c_uint, size_t};
 
 use anyhow;
 use std::collections::HashMap;
@@ -13,8 +13,8 @@ use std::format;
 
 use crate::runtime::TeaclaveRuntime;
 
-const FFI_OK: c_int = 0;
-const FFI_FILE_ERROR: c_int = -1;
+const FFI_OK: c_uint = 0;
+const FFI_FILE_ERROR: c_uint = 1;
 
 pub struct Context {
     runtime: Box<dyn TeaclaveRuntime + Send + Sync>,
@@ -289,9 +289,15 @@ pub mod tests {
 
 use std::ffi::CStr;
 
+/*
+ * uint c_open_input(char* file_id, int* out_fd);
+ *
+ */
+
 #[allow(unused)]
 #[no_mangle]
-extern "C" fn c_open_input(fid: *mut c_char, out_handle: *mut c_int) -> c_int {
+extern "C" fn c_open_input(fid: *mut c_char, out_handle: *mut c_int) -> c_uint 
{
+    debug!("c_open_input");
     let fid = unsafe { CStr::from_ptr(fid).to_string_lossy().into_owned() };
     match rtc_open_input(&fid) {
         Ok(handle) => {
@@ -301,15 +307,20 @@ extern "C" fn c_open_input(fid: *mut c_char, out_handle: 
*mut c_int) -> c_int {
             FFI_OK
         }
         Err(e) => {
-            error!("c_open_file: {:?}", e);
+            info!("c_open_file: {:?}", e);
             FFI_FILE_ERROR
         }
     }
 }
 
+/*
+ * uint c_create_output(char* file_id, int* out_fd);
+ *
+ */
 #[allow(unused)]
 #[no_mangle]
-extern "C" fn c_create_output(fid: *mut c_char, out_handle: *mut c_int) -> 
c_int {
+extern "C" fn c_create_output(fid: *mut c_char, out_handle: *mut c_int) -> 
c_uint {
+    debug!("c_create_input");
     let fid = unsafe { CStr::from_ptr(fid).to_string_lossy().into_owned() };
     match rtc_create_output(&fid) {
         Ok(handle) => {
@@ -319,21 +330,27 @@ extern "C" fn c_create_output(fid: *mut c_char, 
out_handle: *mut c_int) -> c_int
             FFI_OK
         }
         Err(e) => {
-            error!("c_open_file: {:?}", e);
+            info!("c_open_file: {:?}", e);
             FFI_FILE_ERROR
         }
     }
 }
 
+/*
+ * uint c_read_file(int fd, void* out_buf, size_t buf_size, size_t* 
out_size_read);
+ *
+ */
+
 #[allow(unused)]
 #[no_mangle]
 extern "C" fn c_read_file(
     handle: c_int,
     out_buf: *mut c_uchar,
+    buf_size: size_t,
     out_buf_size_p: *mut size_t,
-) -> c_int {
-    let out_buf_size = unsafe { *out_buf_size_p };
-    let out: &mut [u8] = unsafe { slice::from_raw_parts_mut(out_buf, 
out_buf_size) };
+) -> c_uint {
+    debug!("c_read_file");
+    let out: &mut [u8] = unsafe { slice::from_raw_parts_mut(out_buf, buf_size) 
};
 
     match rtc_read_handle(handle, out) {
         Ok(size) => {
@@ -343,17 +360,25 @@ extern "C" fn c_read_file(
             FFI_OK
         }
         Err(e) => {
-            error!("c_read_file: {:?}", e);
+            info!("c_read_file: {:?}", e);
             FFI_FILE_ERROR
         }
     }
 }
 
+/*
+ * uint c_write_file(int fd, void* buf, size_t buf_size, size_t* 
out_size_written);
+ */
 #[allow(unused)]
 #[no_mangle]
-extern "C" fn c_write_file(handle: c_int, in_buf: *mut c_uchar, buf_size_p: 
*mut size_t) -> c_int {
-    let out_buf_size = unsafe { *buf_size_p };
-    let in_buf: &[u8] = unsafe { slice::from_raw_parts_mut(in_buf, 
out_buf_size) };
+extern "C" fn c_write_file(
+    handle: c_int,
+    in_buf: *mut c_uchar,
+    buf_size: size_t,
+    buf_size_p: *mut size_t,
+) -> c_uint {
+    debug!("c_write_file");
+    let in_buf: &[u8] = unsafe { slice::from_raw_parts_mut(in_buf, buf_size) };
 
     match rtc_write_handle(handle, in_buf) {
         Ok(size) => {
@@ -363,19 +388,23 @@ extern "C" fn c_write_file(handle: c_int, in_buf: *mut 
c_uchar, buf_size_p: *mut
             FFI_OK
         }
         Err(e) => {
-            error!("c_write_file: {:?}", e);
+            info!("c_write_file: {:?}", e);
             FFI_FILE_ERROR
         }
     }
 }
 
+/*
+ * uint c_close_file(int fd);
+ */
 #[allow(unused)]
 #[no_mangle]
-extern "C" fn c_close_file(handle: c_int) -> c_int {
+extern "C" fn c_close_file(handle: c_int) -> c_uint {
+    debug!("c_close_file");
     match rtc_close_handle(handle) {
         Ok(size) => FFI_OK,
         Err(e) => {
-            error!("c_close_file: {:?}", e);
+            info!("c_close_file: {:?}", e);
             FFI_FILE_ERROR
         }
     }
diff --git a/worker/src/function/mesapy.rs b/worker/src/function/mesapy.rs
index b386c21..fb825a1 100644
--- a/worker/src/function/mesapy.rs
+++ b/worker/src/function/mesapy.rs
@@ -119,12 +119,50 @@ pub mod tests {
 
     fn test_mesapy() {
         let py_args = TeaclaveFunctionArguments::new(&hashmap!("--name" => 
"Teaclave"));
-        let py_payload = "
-import sys
+        let py_payload = r#"
 def entrypoint(argv):
-    print argv[0]
-    print argv[1]
-";
+    in_file_id = "in_f1"
+    out_file_id = "out_f1"
+
+    # open input via built-in teaclave_open
+    with teaclave_open(in_file_id, "rb") as f:
+        line = f.readline()
+        assert line == "Hello\n"
+
+    # open input via teaclave module
+    from teaclave import open
+    with open(in_file_id, "rb") as f:
+        line = f.readline()
+        assert line == "Hello\n"
+
+    # open invalid input
+    try:
+        teaclave_open("invalid_key", "rb")
+    except RuntimeError as e:
+        assert e.message == "fileio_init: teaclave_ffi_error"
+
+    # open invalid option
+    try:
+        teaclave_open(in_file_id, "r")
+    except RuntimeError as e:
+        assert e.message == "Teaclave Not Supported"
+
+    # write valid output
+    with teaclave_open(out_file_id, "wb") as f:
+        f.write("This message is from Mesapy!")
+
+    # open invalid output
+    try:
+        teaclave_open("invalid_key", "wb")
+    except RuntimeError as e:
+        assert e.message == "fileio_init: teaclave_ffi_error"
+    
+    # open invalid option    
+    try:
+        teaclave_open(out_file_id, "w")
+    except RuntimeError as e:
+        assert e.message == "Teaclave Not Supported"
+"#;
 
         let input = "test_cases/mesapy/input.txt";
         let output = "test_cases/mesapy/output.txt";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to