Fix issues reported by gcc -fsanitize flag (#678)

And refine some coding styles, fix JIT compiler data wasm table create issue, add license header for some files.
This commit is contained in:
Wenyong Huang 2021-07-30 15:21:17 +08:00 committed by GitHub
parent 62fb3c9a89
commit 7cdfc9fe11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 84 additions and 108 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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 {

View File

@ -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;
}
}
}

View File

@ -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 @@ label_pop_csp_n:
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 @@ label_pop_csp_n:
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 @@ label_pop_csp_n:
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 @@ label_pop_csp_n:
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 ();
}

View File

@ -1989,31 +1989,19 @@ recover_br_info:
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 @@ recover_br_info:
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 @@ recover_br_info:
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 @@ recover_br_info:
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 ();
}

View File

@ -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 */

View File

@ -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 */

View File

@ -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

View File

@ -1 +1,6 @@
#include "../posix/main.c"
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "../posix/main.c"

View File

@ -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 ()

View File

@ -1 +1,6 @@
#include "../posix/main.c"
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "../posix/main.c"

View File

@ -1 +1,6 @@
#include "../posix/main.c"
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "../posix/main.c"

View File

@ -1 +1,6 @@
#include "../posix/main.c"
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "../posix/main.c"