diff --git a/core/iwasm/aot/arch/aot_reloc_aarch64.c b/core/iwasm/aot/arch/aot_reloc_aarch64.c index 91848086a1..14e4ac7d39 100644 --- a/core/iwasm/aot/arch/aot_reloc_aarch64.c +++ b/core/iwasm/aot/arch/aot_reloc_aarch64.c @@ -111,8 +111,8 @@ get_plt_table_size() } #define SIGN_EXTEND_TO_INT64(val, bits, val_ext) do { \ - int64 m = ((int64)1 << (bits - 1)); \ - val_ext = ((int64)val ^ m) - m; \ + int64 m = (int64)((uint64)1 << (bits - 1)); \ + val_ext = ((int64)val ^ m) - m; \ } while (0) #define Page(expr) ((expr) & ~0xFFF) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 8a128ddabb..b69f1aa553 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -434,14 +434,14 @@ wasm_store_delete(wasm_store_t *store) for (i = 0; i != store_count; ++i) { wasm_store_t *tmp; - if (!bh_vector_get((Vector *)singleton_engine->stores, - (uint32)i, &tmp)) { + if (!bh_vector_get((Vector *)singleton_engine->stores, (uint32)i, + &tmp)) { break; } if (tmp == store) { - bh_vector_remove((Vector *)singleton_engine->stores, - (uint32)i, NULL); + bh_vector_remove((Vector *)singleton_engine->stores, (uint32)i, + NULL); break; } } @@ -1935,8 +1935,8 @@ wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary) return false; } - if ((module_rt = wasm_runtime_load((uint8 *)binary->data, (uint32)binary->size, - error_buf, 128))) { + if ((module_rt = wasm_runtime_load( + (uint8 *)binary->data, (uint32)binary->size, error_buf, 128))) { wasm_runtime_unload(module_rt); return true; } @@ -3415,7 +3415,7 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index) if (table->inst_comm_rt->module_type == Wasm_Module_AoT) { AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt; AOTTableInstance *table_aot = - (AOTTableInstance*)inst_aot->tables.ptr + table->table_idx_rt; + (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt; if (index >= table_aot->cur_size) { return NULL; } diff --git a/core/iwasm/common/wasm_c_api_internal.h b/core/iwasm/common/wasm_c_api_internal.h index 0b6da5a65b..c77fa454a8 100644 --- a/core/iwasm/common/wasm_c_api_internal.h +++ b/core/iwasm/common/wasm_c_api_internal.h @@ -68,6 +68,7 @@ struct wasm_memorytype_t { struct wasm_externtype_t { uint32 extern_kind; + /* reservered space */ uint8 data[1]; }; @@ -199,7 +200,8 @@ struct wasm_extern_t { wasm_name_t *module_name; wasm_name_t *name; wasm_externkind_t kind; - uint8 data[4]; + /* reservered space */ + uint8 data[1]; }; struct wasm_instance_t { diff --git a/core/iwasm/compilation/aot.c b/core/iwasm/compilation/aot.c index 3ab580cc50..42a180bf8b 100644 --- a/core/iwasm/compilation/aot.c +++ b/core/iwasm/compilation/aot.c @@ -461,11 +461,11 @@ aot_create_comp_data(WASMModule *module) } else { j = i - module->import_table_count; - comp_data->tables[i].elem_type = module->tables[i].elem_type; - comp_data->tables[i].table_flags = module->tables[i].flags; - comp_data->tables[i].table_init_size = module->tables[i].init_size; - comp_data->tables[i].table_max_size = module->tables[i].max_size; - comp_data->tables[i].possible_grow = module->tables[i].possible_grow; + comp_data->tables[i].elem_type = module->tables[j].elem_type; + comp_data->tables[i].table_flags = module->tables[j].flags; + comp_data->tables[i].table_init_size = module->tables[j].init_size; + comp_data->tables[i].table_max_size = module->tables[j].max_size; + comp_data->tables[i].possible_grow = module->tables[j].possible_grow; } } } diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 152c017411..69f582a3a8 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -39,9 +39,9 @@ typedef float64 CellType_F64; goto out_of_bounds; \ } while (0) -#define CHECK_ATOMIC_MEMORY_ACCESS() do { \ - if (((uintptr_t)maddr & ((1 << align) - 1)) != 0) \ - goto unaligned_atomic; \ +#define CHECK_ATOMIC_MEMORY_ACCESS() do { \ + if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0)\ + goto unaligned_atomic; \ } while (0) static inline uint32 @@ -189,7 +189,7 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) } if (sign && (shift < maxbits) && (byte & 0x40)) { /* Sign extend */ - result |= - ((uint64)1 << shift); + result |= (~((uint64)0)) << shift; } *p_offset = offset; return result; @@ -2060,31 +2060,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_I32_SHL): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(uint32, uint32, I32, <<); -#else DEF_OP_NUMERIC2(uint32, uint32, I32, <<); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I32_SHR_S): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(int32, uint32, I32, >>); -#else DEF_OP_NUMERIC2(int32, uint32, I32, >>); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I32_SHR_U): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(uint32, uint32, I32, >>); -#else DEF_OP_NUMERIC2(uint32, uint32, I32, >>); -#endif HANDLE_OP_END (); } @@ -2211,31 +2199,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_I64_SHL): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(uint64, uint64, I64, <<); -#else DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I64_SHR_S): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(int64, uint64, I64, >>); -#else DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I64_SHR_U): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(uint64, uint64, I64, >>); -#else DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); -#endif HANDLE_OP_END (); } @@ -2266,12 +2242,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_F32_NEG): { - int32 i32 = (int32)frame_sp[-1]; - int32 sign_bit = i32 & (1 << 31); + uint32 u32 = frame_sp[-1]; + uint32 sign_bit = u32 & ((uint32)1 << 31); if (sign_bit) - frame_sp[-1] = i32 & ~(1 << 31); + frame_sp[-1] = u32 & ~((uint32)1 << 31); else - frame_sp[-1] = (uint32)(i32 | (1 << 31)); + frame_sp[-1] = u32 | ((uint32)1 << 31); HANDLE_OP_END (); } @@ -2360,12 +2336,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_F64_NEG): { - int64 i64 = GET_I64_FROM_ADDR(frame_sp - 2); - int64 sign_bit = i64 & (((int64)1) << 63); + uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2); + uint64 sign_bit = u64 & (((uint64)1) << 63); if (sign_bit) - PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 & ~(((uint64)1) << 63))); + PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63))); else - PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 | (((uint64)1) << 63))); + PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63))); HANDLE_OP_END (); } diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 45e0cdbe4b..f1f0b8e101 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -1989,31 +1989,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_I32_SHL): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(uint32, uint32, I32, <<); -#else DEF_OP_NUMERIC2(uint32, uint32, I32, <<); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I32_SHR_S): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(int32, uint32, I32, >>); -#else DEF_OP_NUMERIC2(int32, uint32, I32, >>); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I32_SHR_U): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC(uint32, uint32, I32, >>); -#else DEF_OP_NUMERIC2(uint32, uint32, I32, >>); -#endif HANDLE_OP_END (); } @@ -2140,31 +2128,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_I64_SHL): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(uint64, uint64, I64, <<); -#else DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I64_SHR_S): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(int64, uint64, I64, >>); -#else DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); -#endif HANDLE_OP_END (); } HANDLE_OP (WASM_OP_I64_SHR_U): { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32) - DEF_OP_NUMERIC_64(uint64, uint64, I64, >>); -#else DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); -#endif HANDLE_OP_END (); } @@ -2195,13 +2171,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_F32_NEG): { - int32 i32 = (int32)frame_lp[GET_OFFSET()]; + uint32 u32 = frame_lp[GET_OFFSET()]; + uint32 sign_bit = u32 & ((uint32)1 << 31); addr_ret = GET_OFFSET(); - int32 sign_bit = i32 & (1 << 31); if (sign_bit) - frame_lp[addr_ret] = i32 & ~(1 << 31); + frame_lp[addr_ret] = u32 & ~((uint32)1 << 31); else - frame_lp[addr_ret] = (uint32)(i32 | (1 << 31)); + frame_lp[addr_ret] = u32 | ((uint32)1 << 31); HANDLE_OP_END (); } @@ -2290,14 +2266,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP (WASM_OP_F64_NEG): { - int64 i64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - int64 sign_bit = i64 & (((int64)1) << 63); + uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + uint64 sign_bit = u64 & (((uint64)1) << 63); if (sign_bit) PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), - ((uint64)i64 & ~(((uint64)1) << 63))); + (u64 & ~(((uint64)1) << 63))); else PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), - ((uint64)i64 | (((uint64)1) << 63))); + (u64 | (((uint64)1) << 63))); HANDLE_OP_END (); } diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 0f365657be..6efd59a407 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -121,9 +121,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, } else if (sign && maxbits == 32) { if (shift < maxbits) { - /* Sign extend */ - result = (((int32)result) << (maxbits - shift)) - >> (maxbits - shift); + /* Sign extend, second highest bit is the sign bit */ + if ((uint8)byte & 0x40) + result |= (~((uint64)0)) << shift; } else { /* The top bits should be a sign-extension of the sign bit */ @@ -136,9 +136,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, } else if (sign && maxbits == 64) { if (shift < maxbits) { - /* Sign extend */ - result = (((int64)result) << (maxbits - shift)) - >> (maxbits - shift); + /* Sign extend, second highest bit is the sign bit */ + if ((uint8)byte & 0x40) + result |= (~((uint64)0)) << shift; } else { /* The top bits should be a sign-extension of the sign bit */ diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 342838e4b1..c4bcb505d4 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -89,9 +89,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, } else if (sign && maxbits == 32) { if (shift < maxbits) { - /* Sign extend */ - result = (((int32)result) << (maxbits - shift)) - >> (maxbits - shift); + /* Sign extend, second highest bit is the sign bit */ + if ((uint8)byte & 0x40) + result |= (~((uint64)0)) << shift; } else { /* The top bits should be a sign-extension of the sign bit */ @@ -105,9 +105,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, } else if (sign && maxbits == 64) { if (shift < maxbits) { - /* Sign extend */ - result = (((int64)result) << (maxbits - shift)) - >> (maxbits - shift); + /* Sign extend, second highest bit is the sign bit */ + if ((uint8)byte & 0x40) + result |= (~((uint64)0)) << shift; } else { /* The top bits should be a sign-extension of the sign bit */ diff --git a/core/shared/mem-alloc/ems/ems_gc_internal.h b/core/shared/mem-alloc/ems/ems_gc_internal.h index e72e470179..b099211c1e 100644 --- a/core/shared/mem-alloc/ems/ems_gc_internal.h +++ b/core/shared/mem-alloc/ems/ems_gc_internal.h @@ -88,16 +88,16 @@ hmu_verify(void *vheap, hmu_t *hmu); * hmu bit operation */ -#define SETBIT(v, offset) (v) |= (1 << (offset)) -#define GETBIT(v, offset) ((v) & (1 << (offset)) ? 1 : 0) -#define CLRBIT(v, offset) (v) &= (uint32)(~(1 << (offset))) +#define SETBIT(v, offset) (v) |= ((uint32)1 << (offset)) +#define GETBIT(v, offset) ((v) & ((uint32)1 << (offset)) ? 1 : 0) +#define CLRBIT(v, offset) (v) &= (~((uint32)1 << (offset))) #define SETBITS(v, offset, size, value) do { \ - (v) &= (uint32)(~(((1 << size) - 1) << offset));\ - (v) |= (uint32)(value << offset); \ + (v) &= ~((((uint32)1 << size) - 1) << offset); \ + (v) |= ((uint32)value << offset); \ } while(0) -#define CLRBITS(v, offset, size) (v) &= ~(((1 << size) - 1) << offset) -#define GETBITS(v, offset, size) (((v) & ((uint32)(((1 << size) - 1) << offset))) >> offset) +#define CLRBITS(v, offset, size) (v) &= ~((((uint32)1 << size) - 1) << offset) +#define GETBITS(v, offset, size) (((v) & (((((uint32)1 << size) - 1) << offset))) >> offset) /** * gc object layout definition diff --git a/product-mini/platforms/darwin/main.c b/product-mini/platforms/darwin/main.c index 6efe6c1d8a..8f0e84a97f 100644 --- a/product-mini/platforms/darwin/main.c +++ b/product-mini/platforms/darwin/main.c @@ -1 +1,6 @@ -#include "../posix/main.c" \ No newline at end of file +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "../posix/main.c" diff --git a/product-mini/platforms/linux/CMakeLists.txt b/product-mini/platforms/linux/CMakeLists.txt index b6d088b895..1449c6c363 100644 --- a/product-mini/platforms/linux/CMakeLists.txt +++ b/product-mini/platforms/linux/CMakeLists.txt @@ -109,8 +109,10 @@ if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register") endif () # UNDEFINED BEHAVIOR, refer to https://en.cppreference.com/w/cpp/language/ub - if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover") + if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT WAMR_BUILD_JIT EQUAL 1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined \ + -fno-sanitize=bounds,bounds-strict,alignment \ + -fno-sanitize-recover") endif() endif () diff --git a/product-mini/platforms/linux/main.c b/product-mini/platforms/linux/main.c index 6efe6c1d8a..8f0e84a97f 100644 --- a/product-mini/platforms/linux/main.c +++ b/product-mini/platforms/linux/main.c @@ -1 +1,6 @@ -#include "../posix/main.c" \ No newline at end of file +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "../posix/main.c" diff --git a/product-mini/platforms/nuttx/main.c b/product-mini/platforms/nuttx/main.c index 6efe6c1d8a..8f0e84a97f 100644 --- a/product-mini/platforms/nuttx/main.c +++ b/product-mini/platforms/nuttx/main.c @@ -1 +1,6 @@ -#include "../posix/main.c" \ No newline at end of file +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "../posix/main.c" diff --git a/product-mini/platforms/vxworks/main.c b/product-mini/platforms/vxworks/main.c index 6efe6c1d8a..8f0e84a97f 100644 --- a/product-mini/platforms/vxworks/main.c +++ b/product-mini/platforms/vxworks/main.c @@ -1 +1,6 @@ -#include "../posix/main.c" \ No newline at end of file +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "../posix/main.c"