From dfe52ab42f8c371215d857e5be0e555dba3852bc Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 9 Apr 2021 14:55:58 +0800 Subject: [PATCH] Fix compile warnings on windows platform: dll linkage and others (#604) --- core/iwasm/aot/arch/aot_reloc_x86_64.c | 2 + core/iwasm/common/wasm_runtime_common.c | 4 +- core/iwasm/common/wasm_runtime_common.h | 84 +++++++++---------- core/iwasm/include/wasm_c_api.h | 8 +- core/iwasm/include/wasm_export.h | 2 +- .../shared/platform/include/platform_common.h | 11 ++- product-mini/platforms/windows/CMakeLists.txt | 3 +- product-mini/platforms/windows/main.c | 4 +- wamr-compiler/CMakeLists.txt | 1 + 9 files changed, 66 insertions(+), 53 deletions(-) diff --git a/core/iwasm/aot/arch/aot_reloc_x86_64.c b/core/iwasm/aot/arch/aot_reloc_x86_64.c index 41e678033..af090429d 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_64.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_64.c @@ -12,11 +12,13 @@ #define R_X86_64_32 10 /* Direct 32 bit zero extended */ #define R_X86_64_32S 11 /* Direct 32 bit sign extended */ #else +#ifndef IMAGE_REL_AMD64_ADDR64 #define IMAGE_REL_AMD64_ADDR64 1 /* The 64-bit VA of the relocation target */ #define IMAGE_REL_AMD64_ADDR32 2 /* The 32-bit VA of the relocation target */ #define IMAGE_REL_AMD64_REL32 4 /* The 32-bit relative address from the byte following the relocation*/ #endif +#endif #if defined(BH_PLATFORM_WINDOWS) #pragma function (floor) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index b6ce8eff9..97d939979 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2325,7 +2325,7 @@ resolve_function(const WASMModuleInstanceCommon *module_inst, char *orig_name = NULL; char *sub_module_name = NULL; char *function_name = NULL; - uint32 length = strlen(name) + 1; + uint32 length = (uint32)(strlen(name) + 1); orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0); if (!orig_name) { @@ -3387,7 +3387,7 @@ fail: #endif #if defined(_WIN32) || defined(_WIN32_) -typedef union __declspec(intrin_type) __declspec(align(1)) v128 { +typedef union __declspec(intrin_type) __declspec(align(8)) v128 { __int8 m128i_i8[16]; __int16 m128i_i16[8]; __int32 m128i_i32[4]; diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 06c8bfc8f..d61df2751 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -337,34 +337,34 @@ typedef package_type_t PackageType; typedef wasm_section_t WASMSection, AOTSection; /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_init(void); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_full_init(RuntimeInitArgs *init_args); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy(void); /* See wasm_export.h for description */ -PackageType +WASM_RUNTIME_API_EXTERN PackageType get_package_type(const uint8 *buf, uint32 size); /* See wasm_export.h for description */ -WASMModuleCommon * +WASM_RUNTIME_API_EXTERN WASMModuleCommon * wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size); /* See wasm_export.h for description */ -WASMModuleCommon * +WASM_RUNTIME_API_EXTERN WASMModuleCommon * wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot, char *error_buf, uint32 error_buf_size); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_unload(WASMModuleCommon *module); /* Internal API */ @@ -379,58 +379,58 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst, bool is_sub_inst); /* See wasm_export.h for description */ -WASMModuleInstanceCommon * +WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, uint32 heap_size, char *error_buf, uint32 error_buf_size); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ -WASMFunctionInstanceCommon * +WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst, const char *name, const char *signature); /* See wasm_export.h for description */ -WASMExecEnv * +WASM_RUNTIME_API_EXTERN WASMExecEnv * wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst, uint32 stack_size); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env); /* See wasm_export.h for description */ -WASMModuleInstanceCommon * +WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * wasm_runtime_get_module_inst(WASMExecEnv *exec_env); /* See wasm_export.h for description */ -void * +WASM_RUNTIME_API_EXTERN void * wasm_runtime_get_function_attachment(WASMExecEnv *exec_env); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data); /* See wasm_export.h for description */ -void * +WASM_RUNTIME_API_EXTERN void * wasm_runtime_get_user_data(WASMExecEnv *exec_env); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_call_wasm(WASMExecEnv *exec_env, WASMFunctionInstanceCommon *function, uint32 argc, uint32 argv[]); -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_call_wasm_a(WASMExecEnv *exec_env, WASMFunctionInstanceCommon *function, uint32 num_results, wasm_val_t *results, uint32 num_args, wasm_val_t *args); -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, WASMFunctionInstanceCommon *function, uint32 num_results, wasm_val_t *results, @@ -464,85 +464,85 @@ wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst uint32 argc, uint32 argv[]); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[]); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, const char *name, int32 argc, char *argv[]); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_set_exception(WASMModuleInstanceCommon *module, const char *exception); /* See wasm_export.h for description */ -const char * +WASM_RUNTIME_API_EXTERN const char * wasm_runtime_get_exception(WASMModuleInstanceCommon *module); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst, void *custom_data); /* See wasm_export.h for description */ -void * +WASM_RUNTIME_API_EXTERN void * wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ -uint32 +WASM_RUNTIME_API_EXTERN uint32 wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size, void **p_native_addr); /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr); /* See wasm_export.h for description */ -uint32 +WASM_RUNTIME_API_EXTERN uint32 wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst, const char *src, uint32 size); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst, uint32 app_offset, uint32 size); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst, uint32 app_str_offset); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst, void *native_ptr, uint32 size); /* See wasm_export.h for description */ -void * +WASM_RUNTIME_API_EXTERN void * wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst, uint32 app_offset); /* See wasm_export.h for description */ -uint32 +WASM_RUNTIME_API_EXTERN uint32 wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst, void *native_ptr); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst, uint32 app_offset, uint32 *p_app_start_offset, uint32 *p_app_end_offset); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst, uint8 *native_ptr, uint8 **p_native_start_addr, @@ -563,7 +563,7 @@ wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst, uint32 llvm_stack); #if WASM_ENABLE_MULTI_MODULE != 0 -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_set_module_reader(const module_reader reader, const module_destroyer destroyer); @@ -616,7 +616,7 @@ wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, #if WASM_ENABLE_LIBC_WASI != 0 /* See wasm_export.h for description */ -void +WASM_RUNTIME_API_EXTERN void wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[], uint32 dir_count, const char *map_dir_list[], uint32 map_dir_count, @@ -624,11 +624,11 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, char *argv[], int argc); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ -WASMFunctionInstanceCommon * +WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst); bool @@ -666,13 +666,13 @@ bool wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols); /* See wasm_export.h for description */ -bool +WASM_RUNTIME_API_EXTERN bool wasm_runtime_register_natives_raw(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols); diff --git a/core/iwasm/include/wasm_c_api.h b/core/iwasm/include/wasm_c_api.h index fdc072327..6f1a9c519 100644 --- a/core/iwasm/include/wasm_c_api.h +++ b/core/iwasm/include/wasm_c_api.h @@ -10,8 +10,12 @@ #include #ifndef WASM_API_EXTERN -#ifdef _WIN32 -#define WASM_API_EXTERN __declspec(dllimport) +#if defined(_MSC_BUILD) + #if defined(COMPILING_WASM_RUNTIME_API) + #define WASM_API_EXTERN __declspec(dllexport) + #else + #define WASM_API_EXTERN __declspec(dllimport) + #endif #else #define WASM_API_EXTERN #endif diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 768ee36ea..137692911 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -12,7 +12,7 @@ #ifndef WASM_RUNTIME_API_EXTERN -#if defined(MSVC) +#if defined(_MSC_BUILD ) #if defined(COMPILING_WASM_RUNTIME_API) #define WASM_RUNTIME_API_EXTERN __declspec(dllexport) #else diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index ca2780206..9cd5c2d8e 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -33,9 +33,14 @@ extern "C" { #define BH_FREE os_free #endif -#if defined(MSVC) -__declspec(dllimport) void *BH_MALLOC(unsigned int size); -__declspec(dllimport) void BH_FREE(void *ptr); +#if defined(_MSC_BUILD) +#if defined(COMPILING_WASM_RUNTIME_API) +__declspec(dllexport) void *BH_MALLOC(unsigned int size); +__declspec(dllexport) void BH_FREE(void *ptr); +#else +__declspec(dllimport) void* BH_MALLOC(unsigned int size); +__declspec(dllimport) void BH_FREE(void* ptr); +#endif #else void *BH_MALLOC(unsigned int size); void BH_FREE(void *ptr); diff --git a/product-mini/platforms/windows/CMakeLists.txt b/product-mini/platforms/windows/CMakeLists.txt index 04d505d6c..377297a34 100644 --- a/product-mini/platforms/windows/CMakeLists.txt +++ b/product-mini/platforms/windows/CMakeLists.txt @@ -15,6 +15,8 @@ set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") set (CMAKE_C_STANDARD 99) +add_definitions(-DCOMPILING_WASM_RUNTIME_API=1) + # Set WAMR_BUILD_TARGET, currently values supported: # "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS", "XTENSA" if (NOT DEFINED WAMR_BUILD_TARGET) @@ -126,4 +128,3 @@ set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm) target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS}) -target_compile_definitions(libiwasm PRIVATE COMPILING_WASM_RUNTIME_API=1) diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index c9ad6821c..1e857283d 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -185,8 +185,8 @@ module_reader_callback(const char *module_name, uint8 **p_buffer, uint32 *p_size) { const char *format = "%s/%s.wasm"; - int sz = strlen(module_search_path) + strlen("/") + strlen(module_name) + - strlen(".wasm") + 1; + uint32 sz = (uint32)(strlen(module_search_path) + strlen("/") + + strlen(module_name) + strlen(".wasm") + 1); char *wasm_file_name = BH_MALLOC(sz); if (!wasm_file_name) { return false; diff --git a/wamr-compiler/CMakeLists.txt b/wamr-compiler/CMakeLists.txt index 4d81b09cc..e47ce833d 100644 --- a/wamr-compiler/CMakeLists.txt +++ b/wamr-compiler/CMakeLists.txt @@ -10,6 +10,7 @@ if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows") else() project (aot-compiler C ASM CXX) enable_language (ASM_MASM) + add_definitions(-DCOMPILING_WASM_RUNTIME_API=1) endif() set (CMAKE_CXX_STANDARD 14)