Skip to content

Commit 2167098

Browse files
committed
Fix few integer overflowing
- fix(interpreter): correct offset calculations in wasm_loader_get_const_offset function - fix(mem-alloc): update offset calculation in gc_migrate for memory migration
1 parent 5d8fe5d commit 2167098

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9693,8 +9693,8 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
96939693
*offset = 0;
96949694
return true;
96959695
}
9696-
*offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
9697-
+ (uint32)(i64_const - ctx->i64_consts) * 2;
9696+
*offset = (int32)(i64_const - ctx->i64_consts) * 2
9697+
- (int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
96989698
}
96999699
else if (type == VALUE_TYPE_V128) {
97009700
V128 key = *(V128 *)value, *v128_const;
@@ -9704,9 +9704,10 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
97049704
*offset = 0;
97059705
return true;
97069706
}
9707-
*offset = -(uint32)(ctx->v128_const_num)
9708-
+ (uint32)(v128_const - ctx->v128_consts);
9707+
*offset = (int32)(v128_const - ctx->v128_consts)
9708+
- (int32)(ctx->v128_const_num);
97099709
}
9710+
97109711
else {
97119712
int32 key = *(int32 *)value, *i32_const;
97129713
i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
@@ -9715,8 +9716,9 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
97159716
*offset = 0;
97169717
return true;
97179718
}
9718-
*offset = -(uint32)(ctx->i32_const_num)
9719-
+ (uint32)(i32_const - ctx->i32_consts);
9719+
9720+
*offset = (int32)(i32_const - ctx->i32_consts)
9721+
- (int32)(ctx->i32_const_num);
97209722
}
97219723

97229724
return true;

core/shared/mem-alloc/ems/ems_kfc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
218218
gc_heap_t *heap = (gc_heap_t *)handle;
219219
char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
220220
char *pool_buf_end = pool_buf_new + pool_buf_size;
221-
intptr_t offset = (uint8 *)base_addr_new - (uint8 *)heap->base_addr;
221+
intptr_t offset = 0;
222222
hmu_t *cur = NULL, *end = NULL;
223223
hmu_tree_node_t *tree_node;
224224
uint8 **p_left, **p_right, **p_parent;
@@ -236,6 +236,13 @@ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
236236
return GC_ERROR;
237237
}
238238

239+
if ((uintptr_t)base_addr_new > (uintptr_t)heap->base_addr) {
240+
offset = (uintptr_t)base_addr_new - (uintptr_t)heap->base_addr;
241+
}
242+
else {
243+
offset = (uintptr_t)heap->base_addr - (uintptr_t)base_addr_new;
244+
}
245+
239246
if (offset == 0)
240247
return 0;
241248

0 commit comments

Comments
 (0)