@@ -9720,6 +9720,16 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
97209720
97219721 /* preserve locals before blocks to ensure that "tee/set_local" inside
97229722 blocks will not influence the value of these locals */
9723+ uint32 frame_offset_cell =
9724+ (uint32)(loader_ctx->frame_offset - loader_ctx->frame_offset_bottom);
9725+ uint32 frame_ref_cell =
9726+ (uint32)(loader_ctx->frame_ref - loader_ctx->frame_ref_bottom);
9727+ if (frame_offset_cell < loader_ctx->stack_cell_num
9728+ || frame_ref_cell < loader_ctx->stack_cell_num) {
9729+ set_error_buf(error_buf, error_buf_size, "stack cell num error");
9730+ return false;
9731+ }
9732+
97239733 while (i < loader_ctx->stack_cell_num) {
97249734 int16 cur_offset = loader_ctx->frame_offset_bottom[i];
97259735 uint8 cur_type = loader_ctx->frame_ref_bottom[i];
@@ -12106,13 +12116,19 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1210612116 }
1210712117#endif
1210812118
12119+ uint8 *frame_ref_before_pop = loader_ctx->frame_ref;
1210912120 POP_TYPE(
1211012121 wasm_type->types[wasm_type->param_count - i - 1]);
1211112122#if WASM_ENABLE_FAST_INTERP != 0
1211212123 /* decrease the frame_offset pointer accordingly to keep
12113- * consistent with frame_ref stack */
12114- cell_num = wasm_value_type_cell_num(
12115- wasm_type->types[wasm_type->param_count - i - 1]);
12124+ * consistent with frame_ref stack. Use the actual
12125+ * popped cell count instead of
12126+ * wasm_value_type_cell_num() because when the stack top
12127+ * is VALUE_TYPE_ANY, wasm_loader_pop_frame_ref always
12128+ * pops exactly 1 cell regardless of the expected type
12129+ */
12130+ cell_num = (uint32)(frame_ref_before_pop
12131+ - loader_ctx->frame_ref);
1211612132 loader_ctx->frame_offset -= cell_num;
1211712133
1211812134 if (loader_ctx->frame_offset
0 commit comments