From 74f74b649049f74b125d1b420c926d7928ef015d Mon Sep 17 00:00:00 2001 From: qdaoming-intel <54129441+qdaoming-intel@users.noreply.github.com> Date: Fri, 22 Nov 2019 15:33:37 +0800 Subject: [PATCH] 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. --- core/iwasm/products/linux-sgx/CMakeLists.txt | 7 +++- .../enclave-sample/Enclave/Enclave.edl | 2 ++ .../runtime/platform/linux-sgx/wasm_native.c | 6 ---- .../shared-lib/platform/linux-sgx/bh_assert.c | 14 ++++---- .../platform/linux-sgx/bh_definition.c | 2 +- .../platform/linux-sgx/bh_platform.c | 33 +++++++---------- .../platform/linux-sgx/bh_platform.h | 12 +++---- .../platform/linux-sgx/bh_platform_log.c | 17 ++++----- .../shared-lib/platform/linux-sgx/bh_thread.c | 19 +++++----- core/shared-lib/platform/linux-sgx/bh_time.c | 36 ++++++------------- core/shared-lib/utils/runtime_timer.c | 2 +- doc/building.md | 2 ++ 12 files changed, 64 insertions(+), 88 deletions(-) diff --git a/core/iwasm/products/linux-sgx/CMakeLists.txt b/core/iwasm/products/linux-sgx/CMakeLists.txt index 1ab2df1b..9edf99e6 100644 --- a/core/iwasm/products/linux-sgx/CMakeLists.txt +++ b/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(-DWASM_ENABLE_LOG=0) add_definitions(-Dbh_printf=bh_printf_sgx) +add_definitions(-Dvprintf=bh_vprintf_sgx) if (NOT ("$ENV{VALGRIND}" STREQUAL "YES")) add_definitions(-DNVALGRIND) @@ -78,13 +79,17 @@ endif () 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} -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections") + set (SHARED_LIB_DIR ../../../shared-lib) include_directories (. ../../runtime/include ../../runtime/platform/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) diff --git a/core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl b/core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl index 9228f5ac..fc7ef56e 100644 --- a/core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl +++ b/core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl @@ -4,6 +4,8 @@ */ enclave { + from "sgx_tstdc.edl" import *; + trusted { /* define ECALLs here. */ public void ecall_iwasm_main(void); diff --git a/core/iwasm/runtime/platform/linux-sgx/wasm_native.c b/core/iwasm/runtime/platform/linux-sgx/wasm_native.c index b59b99d0..1bd38ed6 100644 --- a/core/iwasm/runtime/platform/linux-sgx/wasm_native.c +++ b/core/iwasm/runtime/platform/linux-sgx/wasm_native.c @@ -14,14 +14,8 @@ #include "wasm_platform_log.h" #include "bh_common.h" -#include -#include -#include #include -#include #include -#include -#include #include diff --git a/core/shared-lib/platform/linux-sgx/bh_assert.c b/core/shared-lib/platform/linux-sgx/bh_assert.c index c609fb93..fa378c5e 100644 --- a/core/shared-lib/platform/linux-sgx/bh_assert.c +++ b/core/shared-lib/platform/linux-sgx/bh_assert.c @@ -17,6 +17,7 @@ /* for exception throwing */ jmp_buf bh_test_jb; #endif +#define FIXED_BUFFER_SIZE (1<<9) void bh_assert_internal(int v, const char *file_name, int line_number, const char *expr_string) @@ -29,7 +30,7 @@ void bh_assert_internal(int v, const char *file_name, int line_number, if (!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); #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 va_list args; + char msg[FIXED_BUFFER_SIZE] = { '\0' }; 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); - printf("\n"); + bh_printf_sgx("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number); + bh_printf_sgx(msg); + bh_printf_sgx("\n"); #endif } diff --git a/core/shared-lib/platform/linux-sgx/bh_definition.c b/core/shared-lib/platform/linux-sgx/bh_definition.c index eba9ad6a..2b69af0c 100644 --- a/core/shared-lib/platform/linux-sgx/bh_definition.c +++ b/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; } - strcat(s1, s2); + strncat(s1, s2, strlen(s2)); return 0; } diff --git a/core/shared-lib/platform/linux-sgx/bh_platform.c b/core/shared-lib/platform/linux-sgx/bh_platform.c index 298f4305..7ca838f0 100644 --- a/core/shared-lib/platform/linux-sgx/bh_platform.c +++ b/core/shared-lib/platform/linux-sgx/bh_platform.c @@ -6,11 +6,9 @@ #include "bh_common.h" #include "bh_platform.h" -#include -#include #include -#define FIXED_BUFFER_SIZE (1<<14) +#define FIXED_BUFFER_SIZE (1<<9) static bh_print_function_t print_function = NULL; char *bh_strdup(const char *s) @@ -26,24 +24,6 @@ char *bh_strdup(const char *s) 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() { return 0; @@ -77,3 +57,14 @@ int bh_printf_sgx(const char *message, ...) 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; +} diff --git a/core/shared-lib/platform/linux-sgx/bh_platform.h b/core/shared-lib/platform/linux-sgx/bh_platform.h index 357e4285..fb4d7f10 100644 --- a/core/shared-lib/platform/linux-sgx/bh_platform.h +++ b/core/shared-lib/platform/linux-sgx/bh_platform.h @@ -19,16 +19,16 @@ #include #include #include -#include #include -#include #include +#include #ifdef __cplusplus extern "C" { #endif extern int bh_printf_sgx(const char *message, ...); +extern int bh_vprintf_sgx(const char * format, va_list arg); typedef uint64_t uint64; typedef int64_t int64; @@ -53,12 +53,12 @@ typedef int64_t int64; #define INVALID_THREAD_ID 0xFFffFFff -typedef int korp_tid; -typedef int korp_mutex; typedef int korp_sem; -typedef int korp_cond; -typedef int korp_thread; 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_free bh_free diff --git a/core/shared-lib/platform/linux-sgx/bh_platform_log.c b/core/shared-lib/platform/linux-sgx/bh_platform_log.c index 902ca7d6..9af2f8ec 100644 --- a/core/shared-lib/platform/linux-sgx/bh_platform_log.c +++ b/core/shared-lib/platform/linux-sgx/bh_platform_log.c @@ -8,23 +8,18 @@ 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, ...) { - 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) { - return fflush(stream ? stream : stdout); + //TODO: stub impl + return 0; } diff --git a/core/shared-lib/platform/linux-sgx/bh_thread.c b/core/shared-lib/platform/linux-sgx/bh_thread.c index f29c9060..28667be2 100644 --- a/core/shared-lib/platform/linux-sgx/bh_thread.c +++ b/core/shared-lib/platform/linux-sgx/bh_thread.c @@ -8,7 +8,7 @@ #include "bh_memory.h" #include #include -#include +#include 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() { - return 0; + return sgx_thread_self(); } void vm_thread_exit(void * code) @@ -43,7 +43,7 @@ void vm_thread_exit(void * code) } // storage for one thread -static void *_tls_store = NULL; +static __thread void *_tls_store = NULL; 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) { + sgx_thread_mutex_t m = SGX_THREAD_MUTEX_INITIALIZER; + *mutex = m; return BHT_OK; - //return BHT_ERROR; } 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_ERROR; } int _vm_mutex_destroy(korp_mutex *mutex) { + sgx_thread_mutex_destroy(mutex); return BHT_OK; - //return BHT_ERROR; } /* 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.*/ void vm_mutex_lock(korp_mutex *mutex) { + sgx_thread_mutex_lock(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 @@ -95,6 +97,7 @@ int vm_mutex_trylock(korp_mutex *mutex) Don't try to recover error for an existing unknown error.*/ void vm_mutex_unlock(korp_mutex *mutex) { + sgx_thread_mutex_unlock(mutex); } int _vm_sem_init(korp_sem* sem, unsigned int c) diff --git a/core/shared-lib/platform/linux-sgx/bh_time.c b/core/shared-lib/platform/linux-sgx/bh_time.c index b8d9e8f8..189e668d 100644 --- a/core/shared-lib/platform/linux-sgx/bh_time.c +++ b/core/shared-lib/platform/linux-sgx/bh_time.c @@ -7,7 +7,6 @@ #include #include -#include #include /* @@ -16,7 +15,8 @@ */ 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() { - 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() { - 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() { - 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) { - 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; } diff --git a/core/shared-lib/utils/runtime_timer.c b/core/shared-lib/utils/runtime_timer.c index 31820780..aa5fb111 100644 --- a/core/shared-lib/utils/runtime_timer.c +++ b/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); bh_free(ctx); } - printf("timer ctx create failed\n"); + PRINT("timer ctx create failed\n"); return NULL; } diff --git a/doc/building.md b/doc/building.md index 6f7a2958..03610062 100644 --- a/doc/building.md +++ b/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: ``` Bash +source /environment cd core/iwasm/products/linux-sgx/ mkdir 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: ``` Bash +source /environment cd enclave-sample make ```