@@ -9572,6 +9572,16 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
95729572
95739573 /* preserve locals before blocks to ensure that "tee/set_local" inside
95749574 blocks will not influence the value of these locals */
9575+ uint32 frame_offset_cell =
9576+ (uint32)(loader_ctx->frame_offset - loader_ctx->frame_offset_bottom);
9577+ uint32 frame_ref_cell =
9578+ (uint32)(loader_ctx->frame_ref - loader_ctx->frame_ref_bottom);
9579+ if (frame_offset_cell < loader_ctx->stack_cell_num
9580+ || frame_ref_cell < loader_ctx->stack_cell_num) {
9581+ set_error_buf(error_buf, error_buf_size, "stack cell num error");
9582+ return false;
9583+ }
9584+
95759585 while (i < loader_ctx->stack_cell_num) {
95769586 int16 cur_offset = loader_ctx->frame_offset_bottom[i];
95779587 uint8 cur_type = loader_ctx->frame_ref_bottom[i];
@@ -11928,13 +11938,19 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1192811938 break;
1192911939 }
1193011940
11941+ uint8 *frame_ref_before_pop = loader_ctx->frame_ref;
1193111942 POP_TYPE(
1193211943 wasm_type->types[wasm_type->param_count - i - 1]);
1193311944#if WASM_ENABLE_FAST_INTERP != 0
1193411945 /* decrease the frame_offset pointer accordingly to keep
11935- * consistent with frame_ref stack */
11936- cell_num = wasm_value_type_cell_num(
11937- wasm_type->types[wasm_type->param_count - i - 1]);
11946+ * consistent with frame_ref stack. Use the actual
11947+ * popped cell count instead of
11948+ * wasm_value_type_cell_num() because when the stack top
11949+ * is VALUE_TYPE_ANY, wasm_loader_pop_frame_ref always
11950+ * pops exactly 1 cell regardless of the expected type
11951+ */
11952+ cell_num = (uint32)(frame_ref_before_pop
11953+ - loader_ctx->frame_ref);
1193811954 loader_ctx->frame_offset -= cell_num;
1193911955
1194011956 if (loader_ctx->frame_offset
0 commit comments