Skip to content

Commit 7cdfc9f

Browse files
authored
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.
1 parent 62fb3c9 commit 7cdfc9f

File tree

14 files changed

+84
-108
lines changed

14 files changed

+84
-108
lines changed

core/iwasm/aot/arch/aot_reloc_aarch64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ get_plt_table_size()
111111
}
112112

113113
#define SIGN_EXTEND_TO_INT64(val, bits, val_ext) do { \
114-
int64 m = ((int64)1 << (bits - 1)); \
115-
val_ext = ((int64)val ^ m) - m; \
114+
int64 m = (int64)((uint64)1 << (bits - 1)); \
115+
val_ext = ((int64)val ^ m) - m; \
116116
} while (0)
117117

118118
#define Page(expr) ((expr) & ~0xFFF)

core/iwasm/common/wasm_c_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,14 @@ wasm_store_delete(wasm_store_t *store)
434434
for (i = 0; i != store_count; ++i) {
435435
wasm_store_t *tmp;
436436

437-
if (!bh_vector_get((Vector *)singleton_engine->stores,
438-
(uint32)i, &tmp)) {
437+
if (!bh_vector_get((Vector *)singleton_engine->stores, (uint32)i,
438+
&tmp)) {
439439
break;
440440
}
441441

442442
if (tmp == store) {
443-
bh_vector_remove((Vector *)singleton_engine->stores,
444-
(uint32)i, NULL);
443+
bh_vector_remove((Vector *)singleton_engine->stores, (uint32)i,
444+
NULL);
445445
break;
446446
}
447447
}
@@ -1935,8 +1935,8 @@ wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary)
19351935
return false;
19361936
}
19371937

1938-
if ((module_rt = wasm_runtime_load((uint8 *)binary->data, (uint32)binary->size,
1939-
error_buf, 128))) {
1938+
if ((module_rt = wasm_runtime_load(
1939+
(uint8 *)binary->data, (uint32)binary->size, error_buf, 128))) {
19401940
wasm_runtime_unload(module_rt);
19411941
return true;
19421942
}
@@ -3415,7 +3415,7 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
34153415
if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
34163416
AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
34173417
AOTTableInstance *table_aot =
3418-
(AOTTableInstance*)inst_aot->tables.ptr + table->table_idx_rt;
3418+
(AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt;
34193419
if (index >= table_aot->cur_size) {
34203420
return NULL;
34213421
}

core/iwasm/common/wasm_c_api_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct wasm_memorytype_t {
6868

6969
struct wasm_externtype_t {
7070
uint32 extern_kind;
71+
/* reservered space */
7172
uint8 data[1];
7273
};
7374

@@ -199,7 +200,8 @@ struct wasm_extern_t {
199200
wasm_name_t *module_name;
200201
wasm_name_t *name;
201202
wasm_externkind_t kind;
202-
uint8 data[4];
203+
/* reservered space */
204+
uint8 data[1];
203205
};
204206

205207
struct wasm_instance_t {

core/iwasm/compilation/aot.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ aot_create_comp_data(WASMModule *module)
461461
}
462462
else {
463463
j = i - module->import_table_count;
464-
comp_data->tables[i].elem_type = module->tables[i].elem_type;
465-
comp_data->tables[i].table_flags = module->tables[i].flags;
466-
comp_data->tables[i].table_init_size = module->tables[i].init_size;
467-
comp_data->tables[i].table_max_size = module->tables[i].max_size;
468-
comp_data->tables[i].possible_grow = module->tables[i].possible_grow;
464+
comp_data->tables[i].elem_type = module->tables[j].elem_type;
465+
comp_data->tables[i].table_flags = module->tables[j].flags;
466+
comp_data->tables[i].table_init_size = module->tables[j].init_size;
467+
comp_data->tables[i].table_max_size = module->tables[j].max_size;
468+
comp_data->tables[i].possible_grow = module->tables[j].possible_grow;
469469
}
470470
}
471471
}

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ typedef float64 CellType_F64;
3939
goto out_of_bounds; \
4040
} while (0)
4141

42-
#define CHECK_ATOMIC_MEMORY_ACCESS() do { \
43-
if (((uintptr_t)maddr & ((1 << align) - 1)) != 0) \
44-
goto unaligned_atomic; \
42+
#define CHECK_ATOMIC_MEMORY_ACCESS() do { \
43+
if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0)\
44+
goto unaligned_atomic; \
4545
} while (0)
4646

4747
static inline uint32
@@ -189,7 +189,7 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
189189
}
190190
if (sign && (shift < maxbits) && (byte & 0x40)) {
191191
/* Sign extend */
192-
result |= - ((uint64)1 << shift);
192+
result |= (~((uint64)0)) << shift;
193193
}
194194
*p_offset = offset;
195195
return result;
@@ -2060,31 +2060,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
20602060

20612061
HANDLE_OP (WASM_OP_I32_SHL):
20622062
{
2063-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2064-
DEF_OP_NUMERIC(uint32, uint32, I32, <<);
2065-
#else
20662063
DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
2067-
#endif
20682064
HANDLE_OP_END ();
20692065
}
20702066

20712067
HANDLE_OP (WASM_OP_I32_SHR_S):
20722068
{
2073-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2074-
DEF_OP_NUMERIC(int32, uint32, I32, >>);
2075-
#else
20762069
DEF_OP_NUMERIC2(int32, uint32, I32, >>);
2077-
#endif
20782070
HANDLE_OP_END ();
20792071
}
20802072

20812073
HANDLE_OP (WASM_OP_I32_SHR_U):
20822074
{
2083-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2084-
DEF_OP_NUMERIC(uint32, uint32, I32, >>);
2085-
#else
20862075
DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
2087-
#endif
20882076
HANDLE_OP_END ();
20892077
}
20902078

@@ -2211,31 +2199,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22112199

22122200
HANDLE_OP (WASM_OP_I64_SHL):
22132201
{
2214-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2215-
DEF_OP_NUMERIC_64(uint64, uint64, I64, <<);
2216-
#else
22172202
DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
2218-
#endif
22192203
HANDLE_OP_END ();
22202204
}
22212205

22222206
HANDLE_OP (WASM_OP_I64_SHR_S):
22232207
{
2224-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2225-
DEF_OP_NUMERIC_64(int64, uint64, I64, >>);
2226-
#else
22272208
DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
2228-
#endif
22292209
HANDLE_OP_END ();
22302210
}
22312211

22322212
HANDLE_OP (WASM_OP_I64_SHR_U):
22332213
{
2234-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2235-
DEF_OP_NUMERIC_64(uint64, uint64, I64, >>);
2236-
#else
22372214
DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
2238-
#endif
22392215
HANDLE_OP_END ();
22402216
}
22412217

@@ -2266,12 +2242,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22662242

22672243
HANDLE_OP (WASM_OP_F32_NEG):
22682244
{
2269-
int32 i32 = (int32)frame_sp[-1];
2270-
int32 sign_bit = i32 & (1 << 31);
2245+
uint32 u32 = frame_sp[-1];
2246+
uint32 sign_bit = u32 & ((uint32)1 << 31);
22712247
if (sign_bit)
2272-
frame_sp[-1] = i32 & ~(1 << 31);
2248+
frame_sp[-1] = u32 & ~((uint32)1 << 31);
22732249
else
2274-
frame_sp[-1] = (uint32)(i32 | (1 << 31));
2250+
frame_sp[-1] = u32 | ((uint32)1 << 31);
22752251
HANDLE_OP_END ();
22762252
}
22772253

@@ -2360,12 +2336,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
23602336

23612337
HANDLE_OP (WASM_OP_F64_NEG):
23622338
{
2363-
int64 i64 = GET_I64_FROM_ADDR(frame_sp - 2);
2364-
int64 sign_bit = i64 & (((int64)1) << 63);
2339+
uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2);
2340+
uint64 sign_bit = u64 & (((uint64)1) << 63);
23652341
if (sign_bit)
2366-
PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 & ~(((uint64)1) << 63)));
2342+
PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63)));
23672343
else
2368-
PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 | (((uint64)1) << 63)));
2344+
PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63)));
23692345
HANDLE_OP_END ();
23702346
}
23712347

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,31 +1989,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
19891989

19901990
HANDLE_OP (WASM_OP_I32_SHL):
19911991
{
1992-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
1993-
DEF_OP_NUMERIC(uint32, uint32, I32, <<);
1994-
#else
19951992
DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
1996-
#endif
19971993
HANDLE_OP_END ();
19981994
}
19991995

20001996
HANDLE_OP (WASM_OP_I32_SHR_S):
20011997
{
2002-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2003-
DEF_OP_NUMERIC(int32, uint32, I32, >>);
2004-
#else
20051998
DEF_OP_NUMERIC2(int32, uint32, I32, >>);
2006-
#endif
20071999
HANDLE_OP_END ();
20082000
}
20092001

20102002
HANDLE_OP (WASM_OP_I32_SHR_U):
20112003
{
2012-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2013-
DEF_OP_NUMERIC(uint32, uint32, I32, >>);
2014-
#else
20152004
DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
2016-
#endif
20172005
HANDLE_OP_END ();
20182006
}
20192007

@@ -2140,31 +2128,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
21402128

21412129
HANDLE_OP (WASM_OP_I64_SHL):
21422130
{
2143-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2144-
DEF_OP_NUMERIC_64(uint64, uint64, I64, <<);
2145-
#else
21462131
DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
2147-
#endif
21482132
HANDLE_OP_END ();
21492133
}
21502134

21512135
HANDLE_OP (WASM_OP_I64_SHR_S):
21522136
{
2153-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2154-
DEF_OP_NUMERIC_64(int64, uint64, I64, >>);
2155-
#else
21562137
DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
2157-
#endif
21582138
HANDLE_OP_END ();
21592139
}
21602140

21612141
HANDLE_OP (WASM_OP_I64_SHR_U):
21622142
{
2163-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
2164-
DEF_OP_NUMERIC_64(uint64, uint64, I64, >>);
2165-
#else
21662143
DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
2167-
#endif
21682144
HANDLE_OP_END ();
21692145
}
21702146

@@ -2195,13 +2171,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
21952171

21962172
HANDLE_OP (WASM_OP_F32_NEG):
21972173
{
2198-
int32 i32 = (int32)frame_lp[GET_OFFSET()];
2174+
uint32 u32 = frame_lp[GET_OFFSET()];
2175+
uint32 sign_bit = u32 & ((uint32)1 << 31);
21992176
addr_ret = GET_OFFSET();
2200-
int32 sign_bit = i32 & (1 << 31);
22012177
if (sign_bit)
2202-
frame_lp[addr_ret] = i32 & ~(1 << 31);
2178+
frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
22032179
else
2204-
frame_lp[addr_ret] = (uint32)(i32 | (1 << 31));
2180+
frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
22052181
HANDLE_OP_END ();
22062182
}
22072183

@@ -2290,14 +2266,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22902266

22912267
HANDLE_OP (WASM_OP_F64_NEG):
22922268
{
2293-
int64 i64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
2294-
int64 sign_bit = i64 & (((int64)1) << 63);
2269+
uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
2270+
uint64 sign_bit = u64 & (((uint64)1) << 63);
22952271
if (sign_bit)
22962272
PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
2297-
((uint64)i64 & ~(((uint64)1) << 63)));
2273+
(u64 & ~(((uint64)1) << 63)));
22982274
else
22992275
PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
2300-
((uint64)i64 | (((uint64)1) << 63)));
2276+
(u64 | (((uint64)1) << 63)));
23012277
HANDLE_OP_END ();
23022278
}
23032279

core/iwasm/interpreter/wasm_loader.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end,
121121
}
122122
else if (sign && maxbits == 32) {
123123
if (shift < maxbits) {
124-
/* Sign extend */
125-
result = (((int32)result) << (maxbits - shift))
126-
>> (maxbits - shift);
124+
/* Sign extend, second highest bit is the sign bit */
125+
if ((uint8)byte & 0x40)
126+
result |= (~((uint64)0)) << shift;
127127
}
128128
else {
129129
/* 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,
136136
}
137137
else if (sign && maxbits == 64) {
138138
if (shift < maxbits) {
139-
/* Sign extend */
140-
result = (((int64)result) << (maxbits - shift))
141-
>> (maxbits - shift);
139+
/* Sign extend, second highest bit is the sign bit */
140+
if ((uint8)byte & 0x40)
141+
result |= (~((uint64)0)) << shift;
142142
}
143143
else {
144144
/* The top bits should be a sign-extension of the sign bit */

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ read_leb(uint8 **p_buf, const uint8 *buf_end,
8989
}
9090
else if (sign && maxbits == 32) {
9191
if (shift < maxbits) {
92-
/* Sign extend */
93-
result = (((int32)result) << (maxbits - shift))
94-
>> (maxbits - shift);
92+
/* Sign extend, second highest bit is the sign bit */
93+
if ((uint8)byte & 0x40)
94+
result |= (~((uint64)0)) << shift;
9595
}
9696
else {
9797
/* 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,
105105
}
106106
else if (sign && maxbits == 64) {
107107
if (shift < maxbits) {
108-
/* Sign extend */
109-
result = (((int64)result) << (maxbits - shift))
110-
>> (maxbits - shift);
108+
/* Sign extend, second highest bit is the sign bit */
109+
if ((uint8)byte & 0x40)
110+
result |= (~((uint64)0)) << shift;
111111
}
112112
else {
113113
/* The top bits should be a sign-extension of the sign bit */

core/shared/mem-alloc/ems/ems_gc_internal.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ hmu_verify(void *vheap, hmu_t *hmu);
8888
* hmu bit operation
8989
*/
9090

91-
#define SETBIT(v, offset) (v) |= (1 << (offset))
92-
#define GETBIT(v, offset) ((v) & (1 << (offset)) ? 1 : 0)
93-
#define CLRBIT(v, offset) (v) &= (uint32)(~(1 << (offset)))
91+
#define SETBIT(v, offset) (v) |= ((uint32)1 << (offset))
92+
#define GETBIT(v, offset) ((v) & ((uint32)1 << (offset)) ? 1 : 0)
93+
#define CLRBIT(v, offset) (v) &= (~((uint32)1 << (offset)))
9494

9595
#define SETBITS(v, offset, size, value) do { \
96-
(v) &= (uint32)(~(((1 << size) - 1) << offset));\
97-
(v) |= (uint32)(value << offset); \
96+
(v) &= ~((((uint32)1 << size) - 1) << offset); \
97+
(v) |= ((uint32)value << offset); \
9898
} while(0)
99-
#define CLRBITS(v, offset, size) (v) &= ~(((1 << size) - 1) << offset)
100-
#define GETBITS(v, offset, size) (((v) & ((uint32)(((1 << size) - 1) << offset))) >> offset)
99+
#define CLRBITS(v, offset, size) (v) &= ~((((uint32)1 << size) - 1) << offset)
100+
#define GETBITS(v, offset, size) (((v) & (((((uint32)1 << size) - 1) << offset))) >> offset)
101101

102102
/**
103103
* gc object layout definition

product-mini/platforms/darwin/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
#include "../posix/main.c"
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "../posix/main.c"

0 commit comments

Comments
 (0)