From 15dd6515395236d1edbdda2de5740613c781ce3d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 31 May 2021 10:56:47 +0900 Subject: [PATCH] Fix os_cond_timedwait and other issues for NuttX sim/macOS (#562) --- core/iwasm/interpreter/wasm_loader.c | 2 +- core/shared/mem-alloc/ems/ems_kfc.c | 2 +- .../shared/platform/common/posix/posix_thread.c | 17 ++++++++++++----- core/shared/platform/include/platform_common.h | 4 ++++ core/shared/platform/nuttx/platform_internal.h | 3 +++ product-mini/platforms/nuttx/wamr.mk | 2 +- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 7ab03fd2..57e46edf 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -3618,7 +3618,7 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, uint32 block_nested_depth = 1, count, i, j, t; uint32 error_buf_size = sizeof(error_buf); uint8 opcode, u8; - BlockAddr block_stack[16] = { 0 }, *block; + BlockAddr block_stack[16] = {{0}}, *block; i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index 905a4da2..c4f7195b 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -92,7 +92,7 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, } if (struct_buf_size < sizeof(gc_handle_t)) { - os_printf("[GC_ERROR]heap init struct buf size (%u) < %u\n", + os_printf("[GC_ERROR]heap init struct buf size (%u) < %zu\n", struct_buf_size, sizeof(gc_handle_t)); return NULL; } diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index f0b315d5..60a13fb6 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -31,7 +31,7 @@ static void *os_thread_wrapper(void *arg) os_signal_handler handler = targ->signal_handler; #endif - os_printf("THREAD CREATED %p\n", pthread_self()); + os_printf("THREAD CREATED %jx\n", (uintmax_t)(uintptr_t)pthread_self()); BH_FREE(targ); #ifdef OS_ENABLE_HW_BOUND_CHECK if (os_thread_signal_init(handler) != 0) @@ -184,17 +184,18 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex) static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec) { struct timeval tv; - long int tv_sec_new, tv_nsec_new; + time_t tv_sec_new; + long int tv_nsec_new; gettimeofday(&tv, NULL); - tv_sec_new = (long int)(tv.tv_sec + usec / 1000000); + tv_sec_new = (time_t)(tv.tv_sec + usec / 1000000); if (tv_sec_new >= tv.tv_sec) { ts->tv_sec = tv_sec_new; } else { /* integer overflow */ - ts->tv_sec = LONG_MAX; + ts->tv_sec = BH_TIME_T_MAX; os_printf("Warning: os_cond_reltimedwait exceeds limit, " "set to max timeout instead\n"); } @@ -211,7 +212,7 @@ static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec) "set to max timeout instead\n"); } - if (ts->tv_nsec >= 1000000000L && ts->tv_sec < LONG_MAX) { + if (ts->tv_nsec >= 1000000000L && ts->tv_sec < BH_TIME_T_MAX) { ts->tv_sec++; ts->tv_nsec -= 1000000000L; } @@ -263,7 +264,9 @@ void os_thread_exit(void *retval) return pthread_exit(retval); } +#if defined(os_thread_local_attribute) static os_thread_local_attribute uint8 *thread_stack_boundary = NULL; +#endif uint8 *os_thread_get_stack_boundary() { @@ -276,8 +279,10 @@ uint8 *os_thread_get_stack_boundary() size_t stack_size, max_stack_size; int page_size; +#if defined(os_thread_local_attribute) if (thread_stack_boundary) return thread_stack_boundary; +#endif page_size = getpagesize(); self = pthread_self(); @@ -312,7 +317,9 @@ uint8 *os_thread_get_stack_boundary() } #endif +#if defined(os_thread_local_attribute) thread_stack_boundary = addr; +#endif return addr; } diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index 48bffe10..8a331d97 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -33,6 +33,10 @@ extern "C" { #define BH_FREE os_free #endif +#ifndef BH_TIME_T_MAX +#define BH_TIME_T_MAX LONG_MAX +#endif + #if defined(_MSC_BUILD) #if defined(COMPILING_WASM_RUNTIME_API) __declspec(dllexport) void *BH_MALLOC(unsigned int size); diff --git a/core/shared/platform/nuttx/platform_internal.h b/core/shared/platform/nuttx/platform_internal.h index 77a18e8e..2e476200 100644 --- a/core/shared/platform/nuttx/platform_internal.h +++ b/core/shared/platform/nuttx/platform_internal.h @@ -40,6 +40,9 @@ typedef pthread_t korp_thread; #define os_printf printf #define os_vprintf vprintf +/* On NuttX, time_t is uint32_t */ +#define BH_TIME_T_MAX 0xffffffff + #ifdef __cplusplus } #endif diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 9978553f..8806d948 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -23,7 +23,7 @@ WAMR_BUILD_TARGET := X86_64 endif ifeq ($(CONFIG_HOST_MACOS),y) # Note: invokeNative_em64.s needs BH_PLATFORM_DARWIN -CFLAGS += -DBH_PLATFORM_DARWIN +AFLAGS += -DBH_PLATFORM_DARWIN endif endif