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

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