GetCurrentThreadStackLimits dynamically for Windows platform (#939)

GetCurrentThreadStackLimits dynamically for Windows platform
according to suggestion in #902
And fix some compiling warnings on Windows platform
This commit is contained in:
Wenyong Huang 2022-01-07 17:00:38 +08:00 committed by GitHub
parent cb51dbb513
commit 78308e7bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 21 deletions

View File

@ -1643,7 +1643,7 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
vendor_sys = strstr(default_triple, "-"); vendor_sys = strstr(default_triple, "-");
bh_assert(vendor_sys); bh_assert(vendor_sys);
bh_memcpy_s(default_arch, sizeof(default_arch), default_triple, bh_memcpy_s(default_arch, sizeof(default_arch), default_triple,
vendor_sys - default_triple); (uint32)(vendor_sys - default_triple));
arch1 = default_arch; arch1 = default_arch;
LLVMDisposeMessage(default_triple); LLVMDisposeMessage(default_triple);
@ -1668,13 +1668,15 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
bh_assert(strlen(arch1) + strlen(vendor_sys) + strlen(abi) bh_assert(strlen(arch1) + strlen(vendor_sys) + strlen(abi)
< sizeof(triple_buf)); < sizeof(triple_buf));
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch1, strlen(arch1)); bh_memcpy_s(triple_buf, (uint32)sizeof(triple_buf), arch1,
(uint32)strlen(arch1));
bh_memcpy_s(triple_buf + strlen(arch1), bh_memcpy_s(triple_buf + strlen(arch1),
sizeof(triple_buf) - strlen(arch1), vendor_sys, (uint32)(sizeof(triple_buf) - strlen(arch1)),
strlen(vendor_sys)); vendor_sys, (uint32)strlen(vendor_sys));
bh_memcpy_s(triple_buf + strlen(arch1) + strlen(vendor_sys), bh_memcpy_s(triple_buf + strlen(arch1) + strlen(vendor_sys),
sizeof(triple_buf) - strlen(arch1) - strlen(vendor_sys), (uint32)(sizeof(triple_buf) - strlen(arch1)
abi, strlen(abi)); - strlen(vendor_sys)),
abi, (uint32)strlen(abi));
triple = triple_buf; triple = triple_buf;
} }
else if (arch) { else if (arch) {
@ -1707,13 +1709,15 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi) bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi)
< sizeof(triple_buf)); < sizeof(triple_buf));
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch, strlen(arch)); bh_memcpy_s(triple_buf, (uint32)sizeof(triple_buf), arch,
(uint32)strlen(arch));
bh_memcpy_s(triple_buf + strlen(arch), bh_memcpy_s(triple_buf + strlen(arch),
sizeof(triple_buf) - strlen(arch), vendor_sys, (uint32)(sizeof(triple_buf) - strlen(arch)), vendor_sys,
strlen(vendor_sys)); (uint32)strlen(vendor_sys));
bh_memcpy_s(triple_buf + strlen(arch) + strlen(vendor_sys), bh_memcpy_s(triple_buf + strlen(arch) + strlen(vendor_sys),
sizeof(triple_buf) - strlen(arch) - strlen(vendor_sys), (uint32)(sizeof(triple_buf) - strlen(arch)
abi, strlen(abi)); - strlen(vendor_sys)),
abi, (uint32)strlen(abi));
triple = triple_buf; triple = triple_buf;
} }

View File

@ -47,6 +47,10 @@ static os_thread_data supervisor_thread_data;
/* Thread data key */ /* Thread data key */
static DWORD thread_data_key; static DWORD thread_data_key;
/* The GetCurrentThreadStackLimits API from "kernel32" */
static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR,
PULONG_PTR) = NULL;
int int
os_sem_init(korp_sem *sem); os_sem_init(korp_sem *sem);
int int
@ -61,6 +65,8 @@ os_sem_signal(korp_sem *sem);
int int
os_thread_sys_init() os_thread_sys_init()
{ {
HMODULE module;
if (is_thread_sys_inited) if (is_thread_sys_inited)
return BHT_OK; return BHT_OK;
@ -84,6 +90,11 @@ os_thread_sys_init()
if (!TlsSetValue(thread_data_key, &supervisor_thread_data)) if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
goto fail4; goto fail4;
if ((module = GetModuleHandle((LPSTR) "kernel32"))) {
*(void **)&GetCurrentThreadStackLimits_Kernel32 =
GetProcAddress(module, "GetCurrentThreadStackLimits");
}
is_thread_sys_inited = true; is_thread_sys_inited = true;
return BHT_OK; return BHT_OK;
@ -556,7 +567,6 @@ os_cond_signal(korp_cond *cond)
static os_thread_local_attribute uint8 *thread_stack_boundary = NULL; static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
#if _WIN32_WINNT < 0x0602
static ULONG static ULONG
GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit, GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
PULONG_PTR p_high_limit) PULONG_PTR p_high_limit)
@ -579,7 +589,6 @@ GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
os_printf("warning: VirtualQuery() failed\n"); os_printf("warning: VirtualQuery() failed\n");
return GetLastError(); return GetLastError();
} }
#endif
uint8 * uint8 *
os_thread_get_stack_boundary() os_thread_get_stack_boundary()
@ -591,13 +600,14 @@ os_thread_get_stack_boundary()
return thread_stack_boundary; return thread_stack_boundary;
page_size = os_getpagesize(); page_size = os_getpagesize();
#if _WIN32_WINNT >= 0x0602 if (GetCurrentThreadStackLimits_Kernel32) {
GetCurrentThreadStackLimits(&low_limit, &high_limit); GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
#else }
if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit)) { else {
if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
return NULL; return NULL;
} }
#endif
/* 4 pages are set unaccessible by system, we reserved /* 4 pages are set unaccessible by system, we reserved
one more page at least for safety */ one more page at least for safety */
thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5; thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;

View File

@ -103,7 +103,7 @@ set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO") set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")

View File

@ -222,6 +222,10 @@ if (NOT MSVC)
endif() endif()
endif() endif()
if (MSVC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_WINSOCK_DEPRECATED_NO_WARNINGS")
endif()
# message ("-- CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}") # message ("-- CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
add_library (vmlib add_library (vmlib