Implement multi-module feature and bulk-memory feature (#271)

Refine wasm loader and aot loader
Fix potential issue of os_mmap/os_munmap
Update document
This commit is contained in:
wenyongh
2020-06-02 14:53:06 +08:00
committed by GitHub
parent e81f72d41f
commit 752826a667
57 changed files with 4902 additions and 818 deletions

View File

@ -11,13 +11,11 @@ os_mmap(void *hint, uint32 size, int prot, int flags)
int map_prot = PROT_NONE;
int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
uint64 request_size, page_size;
uint8 *addr, *addr_aligned;
uint8 *addr;
uint32 i;
/* align to 2M if no less than 2M, else align to 4K */
page_size = size < 2 * 1024 * 1024 ? 4096 : 2 * 1024 * 1024;
page_size = getpagesize();
request_size = (size + page_size - 1) & ~(page_size - 1);
request_size += page_size;
if (request_size >= UINT32_MAX)
return NULL;
@ -47,43 +45,25 @@ os_mmap(void *hint, uint32 size, int prot, int flags)
if (addr != MAP_FAILED)
break;
}
if (addr == MAP_FAILED)
return NULL;
addr_aligned = (uint8*)(uintptr_t)
(((uint64)(uintptr_t)addr + page_size - 1) & ~(page_size - 1));
/* Unmap memory allocated before the aligned base address */
if (addr != addr_aligned) {
uint32 prefix_size = (uint32)(addr_aligned - addr);
munmap(addr, prefix_size);
request_size -= prefix_size;
}
/* Unmap memory allocated after the potentially unaligned end */
if (size != request_size) {
uint32 suffix_size = (uint32)(request_size - size);
munmap(addr_aligned + size, suffix_size);
request_size -= size;
}
#ifndef __APPLE__
if (size >= 2 * 1024 * 1024) {
/* Try to use huge page to improve performance */
if (!madvise(addr, size, MADV_HUGEPAGE))
/* make huge page become effective */
memset(addr, 0, size);
}
#endif
return addr_aligned;
return addr;
}
void
os_munmap(void *addr, uint32 size)
{
if (addr)
munmap(addr, size);
uint64 page_size = getpagesize();
uint64 request_size = (size + page_size - 1) & ~(page_size - 1);
if (addr) {
if (munmap(addr, request_size)) {
os_printf("os_munmap error addr:%p, size:0x%lx, errno:%d\n",
addr, request_size, errno);
}
}
}
int

View File

@ -249,5 +249,4 @@ uint8 *os_thread_get_stack_boundary()
#endif
return addr;
}
}

View File

@ -82,29 +82,39 @@ int os_vprintf(const char * format, va_list arg)
return 0;
}
void* os_mmap(void *hint, unsigned int size, int prot, int flags)
void* os_mmap(void *hint, uint32 size, int prot, int flags)
{
#if WASM_ENABLE_AOT != 0
int mprot = 0;
unsigned alignedSize = (size+4095) & (unsigned)~4095; //Page aligned
uint64 aligned_size, page_size;
void* ret = NULL;
sgx_status_t st = 0;
ret = sgx_alloc_rsrv_mem(alignedSize);
page_size = getpagesize();
aligned_size = (size + page_size - 1) & ~(page_size - 1);
if (aligned_size >= UINT32_MAX)
return NULL;
ret = sgx_alloc_rsrv_mem(aligned_size);
if (ret == NULL) {
os_printf("os_mmap(size=%d, alignedSize=%d, prot=0x%x) failed.",size, alignedSize, prot);
os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.",
size, aligned_size, prot);
return NULL;
}
if (prot & MMAP_PROT_READ)
mprot |= SGX_PROT_READ;
if (prot & MMAP_PROT_WRITE)
mprot |= SGX_PROT_WRITE;
if (prot & MMAP_PROT_EXEC)
mprot |= SGX_PROT_EXEC;
st = sgx_tprotect_rsrv_mem(ret, alignedSize, mprot);
if (st != SGX_SUCCESS){
os_printf("os_mmap(size=%d,prot=0x%x) failed to set protect.",size, prot);
sgx_free_rsrv_mem(ret, alignedSize);
st = sgx_tprotect_rsrv_mem(ret, aligned_size, mprot);
if (st != SGX_SUCCESS) {
os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.",
size, prot);
sgx_free_rsrv_mem(ret, aligned_size);
return NULL;
}
@ -117,7 +127,11 @@ void* os_mmap(void *hint, unsigned int size, int prot, int flags)
void os_munmap(void *addr, uint32 size)
{
#if WASM_ENABLE_AOT != 0
sgx_free_rsrv_mem(addr, size);
uint64 aligned_size, page_size;
page_size = getpagesize();
aligned_size = (size + page_size - 1) & ~(page_size - 1);
sgx_free_rsrv_mem(addr, aligned_size);
#endif
}
@ -135,7 +149,8 @@ int os_mprotect(void *addr, uint32 size, int prot)
mprot |= SGX_PROT_EXEC;
st = sgx_tprotect_rsrv_mem(addr, size, mprot);
if (st != SGX_SUCCESS)
os_printf("os_mprotect(addr=0x%lx,size=%d,prot=0x%x) failed.", addr, size, prot);
os_printf("os_mprotect(addr=0x%lx, size=%u, prot=0x%x) failed.",
addr, size, prot);
return (st == SGX_SUCCESS? 0:-1);
#else

View File

@ -32,6 +32,26 @@ b_memcpy_s(void * s1, unsigned int s1max,
return 0;
}
int b_memmove_s(void * s1, unsigned int s1max,
const void * s2, unsigned int n)
{
char *dest = (char*)s1;
char *src = (char*)s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memmove(dest, src, n);
return 0;
}
int
b_strcat_s(char * s1, unsigned int s1max, const char * s2)
{

View File

@ -18,6 +18,12 @@ extern "C" {
bh_assert (_ret == 0); \
} while (0)
#define bh_memmove_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memmove_s (dest, dlen, src, slen); \
(void)_ret; \
bh_assert (_ret == 0); \
} while (0)
#define bh_strcat_s(dest, dlen, src) do { \
int _ret = b_strcat_s (dest, dlen, src); \
(void)_ret; \
@ -31,6 +37,7 @@ extern "C" {
} while (0)
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
int b_memmove_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
int b_strcat_s(char * s1, unsigned int s1max, const char * s2);
int b_strcpy_s(char * s1, unsigned int s1max, const char * s2);

View File

@ -41,12 +41,22 @@ bh_log_set_verbose_level(uint32 level);
void
bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
#if BH_DEBUG == 1
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
#else
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FUNCTION__, __LINE__, __VA_ARGS__)
#endif
#define LOG_ERROR(...) bh_log(BH_LOG_LEVEL_ERROR, NULL, 0, __VA_ARGS__)
#define LOG_DEBUG(...) bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, 0, __VA_ARGS__)
#define LOG_WARNING(...) bh_log(BH_LOG_LEVEL_WARNING, NULL, 0, __VA_ARGS__)
#define LOG_VERBOSE(...) bh_log(BH_LOG_LEVEL_VERBOSE, NULL, 0, __VA_ARGS__)
#if BH_DEBUG == 1
#define LOG_DEBUG(...) bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#else
#define LOG_DEBUG(...) /* do nothing */
#endif
void
bh_print_time(const char *prompt);

View File

@ -17,7 +17,7 @@ uint32 bh_get_elpased_ms(uint32 *last_system_clock);
struct _timer_ctx;
typedef struct _timer_ctx * timer_ctx_t;
typedef void (*timer_callback_f)(uint32 id, unsigned int owner);
typedef void (*timer_callback_f)(unsigned int id, unsigned int owner);
typedef void (*check_timer_expiry_f)(timer_ctx_t ctx);
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,