Skip to content

Commit 78308e7

Browse files
authored
GetCurrentThreadStackLimits dynamically for Windows platform (#939)
GetCurrentThreadStackLimits dynamically for Windows platform according to suggestion in #902 And fix some compiling warnings on Windows platform
1 parent cb51dbb commit 78308e7

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

core/iwasm/compilation/aot_llvm.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
16431643
vendor_sys = strstr(default_triple, "-");
16441644
bh_assert(vendor_sys);
16451645
bh_memcpy_s(default_arch, sizeof(default_arch), default_triple,
1646-
vendor_sys - default_triple);
1646+
(uint32)(vendor_sys - default_triple));
16471647
arch1 = default_arch;
16481648

16491649
LLVMDisposeMessage(default_triple);
@@ -1668,13 +1668,15 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
16681668

16691669
bh_assert(strlen(arch1) + strlen(vendor_sys) + strlen(abi)
16701670
< sizeof(triple_buf));
1671-
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch1, strlen(arch1));
1671+
bh_memcpy_s(triple_buf, (uint32)sizeof(triple_buf), arch1,
1672+
(uint32)strlen(arch1));
16721673
bh_memcpy_s(triple_buf + strlen(arch1),
1673-
sizeof(triple_buf) - strlen(arch1), vendor_sys,
1674-
strlen(vendor_sys));
1674+
(uint32)(sizeof(triple_buf) - strlen(arch1)),
1675+
vendor_sys, (uint32)strlen(vendor_sys));
16751676
bh_memcpy_s(triple_buf + strlen(arch1) + strlen(vendor_sys),
1676-
sizeof(triple_buf) - strlen(arch1) - strlen(vendor_sys),
1677-
abi, strlen(abi));
1677+
(uint32)(sizeof(triple_buf) - strlen(arch1)
1678+
- strlen(vendor_sys)),
1679+
abi, (uint32)strlen(abi));
16781680
triple = triple_buf;
16791681
}
16801682
else if (arch) {
@@ -1707,13 +1709,15 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
17071709

17081710
bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi)
17091711
< sizeof(triple_buf));
1710-
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch, strlen(arch));
1712+
bh_memcpy_s(triple_buf, (uint32)sizeof(triple_buf), arch,
1713+
(uint32)strlen(arch));
17111714
bh_memcpy_s(triple_buf + strlen(arch),
1712-
sizeof(triple_buf) - strlen(arch), vendor_sys,
1713-
strlen(vendor_sys));
1715+
(uint32)(sizeof(triple_buf) - strlen(arch)), vendor_sys,
1716+
(uint32)strlen(vendor_sys));
17141717
bh_memcpy_s(triple_buf + strlen(arch) + strlen(vendor_sys),
1715-
sizeof(triple_buf) - strlen(arch) - strlen(vendor_sys),
1716-
abi, strlen(abi));
1718+
(uint32)(sizeof(triple_buf) - strlen(arch)
1719+
- strlen(vendor_sys)),
1720+
abi, (uint32)strlen(abi));
17171721
triple = triple_buf;
17181722
}
17191723

@@ -2604,4 +2608,4 @@ aot_load_const_from_table(AOTCompContext *comp_ctx, LLVMValueRef base,
26042608
}
26052609

26062610
return const_value;
2607-
}
2611+
}

core/shared/platform/windows/win_thread.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ static os_thread_data supervisor_thread_data;
4747
/* Thread data key */
4848
static DWORD thread_data_key;
4949

50+
/* The GetCurrentThreadStackLimits API from "kernel32" */
51+
static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR,
52+
PULONG_PTR) = NULL;
53+
5054
int
5155
os_sem_init(korp_sem *sem);
5256
int
@@ -61,6 +65,8 @@ os_sem_signal(korp_sem *sem);
6165
int
6266
os_thread_sys_init()
6367
{
68+
HMODULE module;
69+
6470
if (is_thread_sys_inited)
6571
return BHT_OK;
6672

@@ -84,6 +90,11 @@ os_thread_sys_init()
8490
if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
8591
goto fail4;
8692

93+
if ((module = GetModuleHandle((LPSTR) "kernel32"))) {
94+
*(void **)&GetCurrentThreadStackLimits_Kernel32 =
95+
GetProcAddress(module, "GetCurrentThreadStackLimits");
96+
}
97+
8798
is_thread_sys_inited = true;
8899
return BHT_OK;
89100

@@ -556,7 +567,6 @@ os_cond_signal(korp_cond *cond)
556567

557568
static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
558569

559-
#if _WIN32_WINNT < 0x0602
560570
static ULONG
561571
GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
562572
PULONG_PTR p_high_limit)
@@ -579,7 +589,6 @@ GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
579589
os_printf("warning: VirtualQuery() failed\n");
580590
return GetLastError();
581591
}
582-
#endif
583592

584593
uint8 *
585594
os_thread_get_stack_boundary()
@@ -591,13 +600,14 @@ os_thread_get_stack_boundary()
591600
return thread_stack_boundary;
592601

593602
page_size = os_getpagesize();
594-
#if _WIN32_WINNT >= 0x0602
595-
GetCurrentThreadStackLimits(&low_limit, &high_limit);
596-
#else
597-
if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit)) {
598-
return NULL;
603+
if (GetCurrentThreadStackLimits_Kernel32) {
604+
GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
599605
}
600-
#endif
606+
else {
607+
if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
608+
return NULL;
609+
}
610+
601611
/* 4 pages are set unaccessible by system, we reserved
602612
one more page at least for safety */
603613
thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;

product-mini/platforms/windows/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
103103
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
104104
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
105105

106-
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN")
106+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS")
107107
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
108108
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
109109

wamr-compiler/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ if (NOT MSVC)
222222
endif()
223223
endif()
224224

225+
if (MSVC)
226+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_WINSOCK_DEPRECATED_NO_WARNINGS")
227+
endif()
228+
225229
# message ("-- CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
226230

227231
add_library (vmlib

0 commit comments

Comments
 (0)