Enable WASI feature, enhance security and add SGX sample (#142)

Change emcc to clang
Refine interpreter to improve perforamnce
This commit is contained in:
Weining
2019-11-20 21:16:36 +08:00
committed by wenyongh
parent 29c7c743e9
commit 27f246b5f3
159 changed files with 9543 additions and 3789 deletions

View File

@ -3,7 +3,7 @@
include_directories (./include ../include ./${PLATFORM})
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE)
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE)
file (GLOB_RECURSE source_all ${PLATFORM}/*.c)
add_library (supportlib ${source_all})

View File

@ -4,14 +4,20 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}

18
core/shared-lib/platform/darwin/bh_platform.c Normal file → Executable file
View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <sys/stat.h>
#include <fcntl.h>
@ -11,9 +12,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -23,11 +29,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -48,7 +54,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -56,7 +62,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -21,6 +21,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
#include <sys/socket.h>
@ -98,7 +99,7 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2);
int fopen_s(FILE ** pFile, const char *filename, const char *mode);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_wait(korp_sem *sem)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -330,8 +330,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -344,7 +344,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -16,7 +16,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -30,12 +30,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -48,12 +48,13 @@ uint64 _bh_time_get_millisecond_from_1970()
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000)
+ ((uint64)tp.timezone) * 60 * 1000;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct timeb tp;
struct tm *ltp;

View File

@ -52,16 +52,3 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2)
return 0;
}
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
{
if (NULL == pFile || NULL == filename || NULL == mode) {
return -1;
}
*pFile = fopen(filename, mode);
if (NULL == *pFile)
return -1;
return 0;
}

View File

@ -15,12 +15,35 @@ static bh_print_function_t print_function = NULL;
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
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;

View File

@ -14,6 +14,7 @@
#include <assert.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -21,6 +22,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#ifdef __cplusplus

View File

@ -20,14 +20,14 @@ void vm_thread_sys_destroy(void)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
return BHT_ERROR;
// return BHT_OK;
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);

View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <sys/stat.h>
#include <fcntl.h>
@ -11,9 +12,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -23,11 +29,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -48,7 +54,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -56,7 +62,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -20,8 +20,9 @@
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <semaphore.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -98,7 +99,7 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2);
int fopen_s(FILE ** pFile, const char *filename, const char *mode);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_reltimedwait(korp_sem *sem, int mills)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -329,8 +329,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -343,7 +343,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -16,7 +16,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -30,12 +30,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -48,12 +48,13 @@ uint64 _bh_time_get_millisecond_from_1970()
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000)
+ ((uint64)tp.timezone) * 60 * 1000;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct timeb tp;
struct tm *ltp;

View File

@ -41,8 +41,9 @@ int b_strcat_s(char * s1, size_t s1max, const char * s2)
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
{
if (NULL == s1|| NULL == s2
|| s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
if (NULL == s1 || NULL == s2
|| s1max < (strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}

View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
@ -15,9 +16,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -27,11 +33,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -52,7 +58,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -60,7 +66,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -22,6 +22,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
@ -96,7 +97,7 @@ int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_reltimedwait(korp_sem *sem, int mills)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -329,8 +329,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -343,7 +343,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -15,7 +15,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -29,12 +29,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -49,12 +49,12 @@ uint64 _bh_time_get_millisecond_from_1970()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct tm *ltp;
ltp = localtime(&time_sec);

View File

@ -4,14 +4,20 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}

View File

@ -19,7 +19,9 @@
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#ifndef CONFIG_NET_BUF_USER_DATA_SIZE
#define CONFIG_NET_BUF_USER_DATA_SIZE 0
#endif