Skip to content

Commit 703a20b

Browse files
committed
loader: sync frame_offset capacity with stack to prevent OOB
1 parent d7b6bc5 commit 703a20b

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

core/iwasm/interpreter/wasm_loader.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8542,6 +8542,24 @@ check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
85428542
return true;
85438543
}
85448544

8545+
static bool
8546+
ensure_frame_offset_capacity(WASMLoaderContext *ctx, uint32 min_cells,
8547+
char *error_buf, uint32 error_buf_size)
8548+
{
8549+
uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
8550+
while (ctx->frame_offset_bottom + min_cells > ctx->frame_offset_boundary) {
8551+
MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
8552+
ctx->frame_offset_size + 16);
8553+
ctx->frame_offset_size += 16;
8554+
ctx->frame_offset_boundary =
8555+
ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
8556+
ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
8557+
}
8558+
return true;
8559+
fail:
8560+
return false;
8561+
}
8562+
85458563
static void
85468564
free_label_patch_list(BranchBlock *frame_csp)
85478565
{
@@ -8843,6 +8861,11 @@ wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
88438861

88448862
if (!check_stack_push(ctx, type, error_buf, error_buf_size))
88458863
return false;
8864+
#if WASM_ENABLE_FAST_INTERP != 0
8865+
if (!ensure_frame_offset_capacity(ctx, ctx->stack_cell_num + type_cell_num,
8866+
error_buf, error_buf_size))
8867+
return false;
8868+
#endif
88468869

88478870
#if WASM_ENABLE_GC != 0
88488871
if (wasm_is_type_multi_byte_type(type)) {
@@ -9708,6 +9731,11 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
97089731
{
97099732
uint32 i = 0;
97109733
bool preserve_local;
9734+
#if WASM_ENABLE_FAST_INTERP != 0
9735+
if (!ensure_frame_offset_capacity(loader_ctx, loader_ctx->stack_cell_num,
9736+
error_buf, error_buf_size))
9737+
return false;
9738+
#endif
97119739

97129740
/* preserve locals before blocks to ensure that "tee/set_local" inside
97139741
blocks will not influence the value of these locals */
@@ -11060,6 +11088,9 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
1106011088
#if WASM_ENABLE_FAST_INTERP != 0
1106111089
loader_ctx->frame_offset =
1106211090
loader_ctx->frame_offset_bottom + stack_cell_num_old;
11091+
if (!ensure_frame_offset_capacity(loader_ctx, stack_cell_num_old,
11092+
error_buf, error_buf_size))
11093+
goto cleanup_and_return;
1106311094
total_size =
1106411095
(uint32)(sizeof(int16)
1106511096
* (frame_offset_old - frame_offset_after_popped));

0 commit comments

Comments
 (0)