Răsfoiți Sursa

Fix sgx porting issues: incorrect compile flags, porting func impl, document, etc. (#145)

* Fix sgx porting issues: incorrect compilation flags, porting function impl, document, etc.

* Update bh_platform.c

Add check for function bh_vprintf_sgx: check whether print_function is not NULL.
qdaoming-intel 6 ani în urmă
părinte
comite
74f74b6490

+ 6 - 1
core/iwasm/products/linux-sgx/CMakeLists.txt

@@ -16,6 +16,7 @@ add_definitions(-DOPS_INPUT_OUTPUT=1)
 add_definitions(-DOPS_UNSAFE_BUFFERS=0)
 add_definitions(-DOPS_UNSAFE_BUFFERS=0)
 add_definitions(-DWASM_ENABLE_LOG=0)
 add_definitions(-DWASM_ENABLE_LOG=0)
 add_definitions(-Dbh_printf=bh_printf_sgx)
 add_definitions(-Dbh_printf=bh_printf_sgx)
+add_definitions(-Dvprintf=bh_vprintf_sgx)
 
 
 if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
 if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
   add_definitions(-DNVALGRIND)
   add_definitions(-DNVALGRIND)
@@ -78,13 +79,17 @@ endif ()
 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
 
 
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections")
+
 set (SHARED_LIB_DIR ../../../shared-lib)
 set (SHARED_LIB_DIR ../../../shared-lib)
 
 
 include_directories (.
 include_directories (.
                      ../../runtime/include
                      ../../runtime/include
                      ../../runtime/platform/include
                      ../../runtime/platform/include
                      ${SHARED_LIB_DIR}/include
                      ${SHARED_LIB_DIR}/include
-                     $ENV{SGX_SDK}/include)
+                     $ENV{SGX_SDK}/include
+                     $ENV{SGX_SDK}/include/tlibc
+                     $ENV{SGX_SDK}/include/libcxx)
 
 
 enable_language (ASM)
 enable_language (ASM)
 
 

+ 2 - 0
core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl

@@ -4,6 +4,8 @@
  */
  */
 
 
 enclave {
 enclave {
+    from "sgx_tstdc.edl" import *;
+
     trusted {
     trusted {
         /* define ECALLs here. */
         /* define ECALLs here. */
         public void ecall_iwasm_main(void);
         public void ecall_iwasm_main(void);

+ 0 - 6
core/iwasm/runtime/platform/linux-sgx/wasm_native.c

@@ -14,14 +14,8 @@
 #include "wasm_platform_log.h"
 #include "wasm_platform_log.h"
 #include "bh_common.h"
 #include "bh_common.h"
 
 
-#include <sys/ioctl.h>
-#include <sys/uio.h>
-#include <sys/syscall.h>
 #include <sys/types.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <unistd.h>
 #include <unistd.h>
-#include <pwd.h>
-#include <fcntl.h>
 #include <errno.h>
 #include <errno.h>
 
 
 
 

+ 7 - 7
core/shared-lib/platform/linux-sgx/bh_assert.c

@@ -17,6 +17,7 @@
 /* for exception throwing */
 /* for exception throwing */
 jmp_buf bh_test_jb;
 jmp_buf bh_test_jb;
 #endif
 #endif
+#define FIXED_BUFFER_SIZE (1<<9)
 
 
 void bh_assert_internal(int v, const char *file_name, int line_number,
 void bh_assert_internal(int v, const char *file_name, int line_number,
         const char *expr_string)
         const char *expr_string)
@@ -29,7 +30,7 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
     if (!expr_string)
     if (!expr_string)
         expr_string = "NULL EXPR_STRING";
         expr_string = "NULL EXPR_STRING";
 
 
-    printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
+    bh_printf_sgx("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
             file_name, line_number);
             file_name, line_number);
 
 
 #ifdef BH_TEST
 #ifdef BH_TEST
@@ -44,15 +45,14 @@ void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
 {
 {
 #ifndef JEFF_TEST_VERIFIER
 #ifndef JEFF_TEST_VERIFIER
     va_list args;
     va_list args;
+    char msg[FIXED_BUFFER_SIZE] = { '\0' };
 
 
     va_start(args, fmt);
     va_start(args, fmt);
-    bh_assert(file_name);
-
-    printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
-    vprintf(fmt, args);
-
+    vsnprintf(msg, FIXED_BUFFER_SIZE, fmt, args);
     va_end(args);
     va_end(args);
-    printf("\n");
+    bh_printf_sgx("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
+    bh_printf_sgx(msg);
+    bh_printf_sgx("\n");
 #endif
 #endif
 }
 }
 
 

+ 1 - 1
core/shared-lib/platform/linux-sgx/bh_definition.c

@@ -34,7 +34,7 @@ int b_strcat_s(char * s1, size_t s1max, const char * s2)
         return -1;
         return -1;
     }
     }
 
 
-    strcat(s1, s2);
+    strncat(s1, s2, strlen(s2));
 
 
     return 0;
     return 0;
 }
 }

+ 12 - 21
core/shared-lib/platform/linux-sgx/bh_platform.c

@@ -6,11 +6,9 @@
 #include "bh_common.h"
 #include "bh_common.h"
 #include "bh_platform.h"
 #include "bh_platform.h"
 
 
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <unistd.h>
 
 
-#define FIXED_BUFFER_SIZE (1<<14)
+#define FIXED_BUFFER_SIZE (1<<9)
 static bh_print_function_t print_function = NULL;
 static bh_print_function_t print_function = NULL;
 
 
 char *bh_strdup(const char *s)
 char *bh_strdup(const char *s)
@@ -26,24 +24,6 @@ char *bh_strdup(const char *s)
     return s1;
     return s1;
 }
 }
 
 
-const unsigned short ** __ctype_b_loc(void)
-{
-    /* TODO */
-    return NULL;
-}
-
-const int32_t ** __ctype_toupper_loc(void)
-{
-    /* TODO */
-    return NULL;
-}
-
-const int32_t ** __ctype_tolower_loc(void)
-{
-    /* TODO */
-    return NULL;
-}
-
 int bh_platform_init()
 int bh_platform_init()
 {
 {
     return 0;
     return 0;
@@ -77,3 +57,14 @@ int bh_printf_sgx(const char *message, ...)
 
 
     return 0;
     return 0;
 }
 }
+
+int bh_vprintf_sgx(const char * format, va_list arg)
+{
+    if (print_function != NULL) {
+        char msg[FIXED_BUFFER_SIZE] = { '\0' };
+        vsnprintf(msg, FIXED_BUFFER_SIZE, format, arg);
+        print_function(msg);
+    }
+
+    return 0;
+}

+ 6 - 6
core/shared-lib/platform/linux-sgx/bh_platform.h

@@ -19,16 +19,16 @@
 #include <math.h>
 #include <math.h>
 #include <stdarg.h>
 #include <stdarg.h>
 #include <ctype.h>
 #include <ctype.h>
-#include <pthread.h>
 #include <limits.h>
 #include <limits.h>
-#include <fcntl.h>
 #include <errno.h>
 #include <errno.h>
+#include <sgx_thread.h>
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 extern "C" {
 extern "C" {
 #endif
 #endif
 
 
 extern int bh_printf_sgx(const char *message, ...);
 extern int bh_printf_sgx(const char *message, ...);
+extern int bh_vprintf_sgx(const char * format, va_list arg);
 
 
 typedef uint64_t uint64;
 typedef uint64_t uint64;
 typedef int64_t int64;
 typedef int64_t int64;
@@ -53,12 +53,12 @@ typedef int64_t int64;
 
 
 #define INVALID_THREAD_ID 0xFFffFFff
 #define INVALID_THREAD_ID 0xFFffFFff
 
 
-typedef int korp_tid;
-typedef int korp_mutex;
 typedef int korp_sem;
 typedef int korp_sem;
-typedef int korp_cond;
-typedef int korp_thread;
 typedef void* (*thread_start_routine_t)(void*);
 typedef void* (*thread_start_routine_t)(void*);
+typedef sgx_thread_mutex_t korp_mutex;
+typedef sgx_thread_t korp_tid;
+typedef sgx_thread_t korp_thread;
+typedef sgx_thread_cond_t korp_cond;
 
 
 #define wa_malloc bh_malloc
 #define wa_malloc bh_malloc
 #define wa_free bh_free
 #define wa_free bh_free

+ 6 - 11
core/shared-lib/platform/linux-sgx/bh_platform_log.c

@@ -8,23 +8,18 @@
 
 
 void bh_log_emit(const char *fmt, va_list ap)
 void bh_log_emit(const char *fmt, va_list ap)
 {
 {
-    vprintf(fmt, ap);
-    fflush(stdout);
+   //TODO: stub impl
 }
 }
 
 
+/*
 int bh_fprintf(FILE *stream, const char *fmt, ...)
 int bh_fprintf(FILE *stream, const char *fmt, ...)
 {
 {
-    va_list ap;
-    int ret;
-
-    va_start(ap, fmt);
-    ret = vfprintf(stream ? stream : stdout, fmt, ap);
-    va_end(ap);
-
-    return ret;
+   return 0;
 }
 }
+*/
 
 
 int bh_fflush(void *stream)
 int bh_fflush(void *stream)
 {
 {
-    return fflush(stream ? stream : stdout);
+    //TODO: stub impl
+    return 0;
 }
 }

+ 11 - 8
core/shared-lib/platform/linux-sgx/bh_thread.c

@@ -8,7 +8,7 @@
 #include "bh_memory.h"
 #include "bh_memory.h"
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
-#include <sys/time.h>
+#include <sgx_thread.h>
 
 
 int _vm_thread_sys_init()
 int _vm_thread_sys_init()
 {
 {
@@ -35,7 +35,7 @@ int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
 
 
 korp_tid _vm_self_thread()
 korp_tid _vm_self_thread()
 {
 {
-    return 0;
+    return sgx_thread_self();
 }
 }
 
 
 void vm_thread_exit(void * code)
 void vm_thread_exit(void * code)
@@ -43,7 +43,7 @@ void vm_thread_exit(void * code)
 }
 }
 
 
 // storage for one thread
 // storage for one thread
-static void *_tls_store = NULL;
+static __thread void *_tls_store = NULL;
 
 
 void *_vm_tls_get(unsigned idx)
 void *_vm_tls_get(unsigned idx)
 {
 {
@@ -59,20 +59,22 @@ int _vm_tls_put(unsigned idx, void * tls)
 
 
 int _vm_mutex_init(korp_mutex *mutex)
 int _vm_mutex_init(korp_mutex *mutex)
 {
 {
+    sgx_thread_mutex_t m = SGX_THREAD_MUTEX_INITIALIZER;
+    *mutex = m;
     return BHT_OK;
     return BHT_OK;
-    //return BHT_ERROR;
 }
 }
 
 
 int _vm_recursive_mutex_init(korp_mutex *mutex)
 int _vm_recursive_mutex_init(korp_mutex *mutex)
 {
 {
+    sgx_thread_mutex_t m = SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER;
+    *mutex = m;
     return BHT_OK;
     return BHT_OK;
-    //return BHT_ERROR;
 }
 }
 
 
 int _vm_mutex_destroy(korp_mutex *mutex)
 int _vm_mutex_destroy(korp_mutex *mutex)
 {
 {
+    sgx_thread_mutex_destroy(mutex);
     return BHT_OK;
     return BHT_OK;
-    //return BHT_ERROR;
 }
 }
 
 
 /* Returned error (EINVAL, EAGAIN and EDEADLK) from
 /* Returned error (EINVAL, EAGAIN and EDEADLK) from
@@ -81,12 +83,12 @@ int _vm_mutex_destroy(korp_mutex *mutex)
  Don't try to recover error for an existing unknown error.*/
  Don't try to recover error for an existing unknown error.*/
 void vm_mutex_lock(korp_mutex *mutex)
 void vm_mutex_lock(korp_mutex *mutex)
 {
 {
+    sgx_thread_mutex_lock(mutex);
 }
 }
 
 
 int vm_mutex_trylock(korp_mutex *mutex)
 int vm_mutex_trylock(korp_mutex *mutex)
 {
 {
-    return BHT_OK;
-    //return BHT_ERROR;
+    return (sgx_thread_mutex_trylock(mutex) == 0? BHT_OK: BHT_ERROR);
 }
 }
 
 
 /* Returned error (EINVAL, EAGAIN and EPERM) from
 /* Returned error (EINVAL, EAGAIN and EPERM) from
@@ -95,6 +97,7 @@ int vm_mutex_trylock(korp_mutex *mutex)
  Don't try to recover error for an existing unknown error.*/
  Don't try to recover error for an existing unknown error.*/
 void vm_mutex_unlock(korp_mutex *mutex)
 void vm_mutex_unlock(korp_mutex *mutex)
 {
 {
+    sgx_thread_mutex_unlock(mutex);
 }
 }
 
 
 int _vm_sem_init(korp_sem* sem, unsigned int c)
 int _vm_sem_init(korp_sem* sem, unsigned int c)

+ 10 - 26
core/shared-lib/platform/linux-sgx/bh_time.c

@@ -7,7 +7,6 @@
 
 
 #include <unistd.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <sys/timeb.h>
 #include <time.h>
 #include <time.h>
 
 
 /*
 /*
@@ -16,7 +15,8 @@
  */
  */
 uint64 _bh_time_get_tick_millisecond()
 uint64 _bh_time_get_tick_millisecond()
 {
 {
-    return sysconf(_SC_CLK_TCK);
+    //TODO:
+    return 0;
 }
 }
 
 
 /*
 /*
@@ -25,17 +25,14 @@ uint64 _bh_time_get_tick_millisecond()
  */
  */
 uint64 _bh_time_get_boot_millisecond()
 uint64 _bh_time_get_boot_millisecond()
 {
 {
-    struct timespec ts;
-    if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
-        return 0;
-    }
-
-    return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
+   //TODO
+   return 0;
 }
 }
 
 
 uint32 bh_get_tick_sec()
 uint32 bh_get_tick_sec()
 {
 {
-    return _bh_time_get_boot_millisecond() / 1000;
+    //TODO
+    return 0;
 }
 }
 
 
 /*
 /*
@@ -44,26 +41,13 @@ uint32 bh_get_tick_sec()
  */
  */
 uint64 _bh_time_get_millisecond_from_1970()
 uint64 _bh_time_get_millisecond_from_1970()
 {
 {
-    struct timeb tp;
-    ftime(&tp);
-
-    return ((uint64) tp.time) * 1000 + tp.millitm
-            - (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
+   //TODO
+   return 0;
 }
 }
 
 
 size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
 size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
 {
 {
-    time_t time_sec = time / 1000;
-    struct timeb tp;
-    struct tm *ltp;
-
-    ftime(&tp);
-    time_sec -= tp.timezone * 60;
-
-    ltp = localtime(&time_sec);
-    if (ltp == NULL) {
-        return 0;
-    }
-    return strftime(s, max, format, ltp);
+  //TODO
+  return 0;
 }
 }
 
 

+ 1 - 1
core/shared-lib/utils/runtime_timer.c

@@ -233,7 +233,7 @@ timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
         release_timer_list(&ctx->free_timers);
         release_timer_list(&ctx->free_timers);
         bh_free(ctx);
         bh_free(ctx);
     }
     }
-    printf("timer ctx create failed\n");
+    PRINT("timer ctx create failed\n");
     return NULL;
     return NULL;
 }
 }
 
 

+ 2 - 0
doc/building.md

@@ -45,6 +45,7 @@ And then install the [Intel SGX SDK](https://software.intel.com/en-us/sgx/sdk).
 
 
 After installing dependencies, build the source code:
 After installing dependencies, build the source code:
 ``` Bash
 ``` Bash
+source <SGX_SDK dir>/environment
 cd core/iwasm/products/linux-sgx/
 cd core/iwasm/products/linux-sgx/
 mkdir build
 mkdir build
 cd build
 cd build
@@ -55,6 +56,7 @@ This builds the libraries used by SGX enclave sample, the generated file libvmli
 
 
 Then build the enclave sample:
 Then build the enclave sample:
 ``` Bash
 ``` Bash
+source <SGX_SDK dir>/environment
 cd enclave-sample
 cd enclave-sample
 make
 make
 ```
 ```