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.
This commit is contained in:
qdaoming-intel 2019-11-22 15:33:37 +08:00 committed by wenyongh
parent a7a7d04dc6
commit 74f74b6490
12 changed files with 64 additions and 88 deletions

View File

@ -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)

View File

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

View File

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

View File

@ -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
}

View File

@ -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;
}

View File

@ -6,11 +6,9 @@
#include "bh_common.h"
#include "bh_platform.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#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;
}

View File

@ -19,16 +19,16 @@
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <sgx_thread.h>
#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

View File

@ -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;
}

View File

@ -8,7 +8,7 @@
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sgx_thread.h>
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)

View File

@ -7,7 +7,6 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
/*
@ -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;
}

View File

@ -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;
}

View File

@ -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 <SGX_SDK dir>/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 <SGX_SDK dir>/environment
cd enclave-sample
make
```