Reserve one pointer size for fast-interp code_compiled_size (#1382)

Reserve one pointer size for fast-interp code_compiled_size: if the last opcode of
current function is to be dropped (e.g. OP_DROP), the peak memory usage will
be larger than the final code_compiled_size, we record the peak size to ensure
there won't be invalid memory access during the second traversing.
This commit is contained in:
Xu Jun 2022-08-15 11:33:20 +08:00 committed by GitHub
parent 6798637891
commit 3b641b17d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 16 deletions

View File

@ -4533,6 +4533,10 @@ typedef struct WASMLoaderContext {
uint8 *p_code_compiled;
uint8 *p_code_compiled_end;
uint32 code_compiled_size;
/* If the last opcode will be dropped, the peak memory usage will be larger
* than the final code_compiled_size, we record the peak size to ensure
* there will not be invalid memory access during second traverse */
uint32 code_compiled_peak_size;
#endif
} WASMLoaderContext;
@ -5077,9 +5081,10 @@ static bool
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
{
if (!(ctx->p_code_compiled =
loader_malloc(ctx->code_compiled_size, NULL, 0)))
loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
return false;
ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size;
ctx->p_code_compiled_end =
ctx->p_code_compiled + ctx->code_compiled_peak_size;
/* clean up frame ref */
memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
@ -5104,6 +5109,15 @@ wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
return true;
}
static void
increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
{
ctx->code_compiled_size += size;
if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
ctx->code_compiled_peak_size = ctx->code_compiled_size;
}
}
static void
wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
{
@ -5122,7 +5136,7 @@ wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += size;
increase_compiled_code_space(ctx, size);
}
}
@ -5140,7 +5154,7 @@ wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(uint32);
increase_compiled_code_space(ctx, sizeof(uint32));
}
}
@ -5158,7 +5172,7 @@ wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(int16);
increase_compiled_code_space(ctx, sizeof(uint16));
}
}
@ -5174,9 +5188,9 @@ wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
#endif
}
else {
ctx->code_compiled_size += sizeof(uint8);
increase_compiled_code_space(ctx, sizeof(uint8));
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
ctx->code_compiled_size++;
increase_compiled_code_space(ctx, sizeof(uint8));
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
}
@ -5196,7 +5210,7 @@ wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(void *);
increase_compiled_code_space(ctx, sizeof(void *));
}
}

View File

@ -3038,6 +3038,10 @@ typedef struct WASMLoaderContext {
uint8 *p_code_compiled;
uint8 *p_code_compiled_end;
uint32 code_compiled_size;
/* If the last opcode will be dropped, the peak memory usage will be larger
* than the final code_compiled_size, we record the peak size to ensure
* there will not be invalid memory access during second traverse */
uint32 code_compiled_peak_size;
#endif
} WASMLoaderContext;
@ -3530,9 +3534,10 @@ static bool
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
{
if (!(ctx->p_code_compiled =
loader_malloc(ctx->code_compiled_size, NULL, 0)))
loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
return false;
ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size;
ctx->p_code_compiled_end =
ctx->p_code_compiled + ctx->code_compiled_peak_size;
/* clean up frame ref */
memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
@ -3557,6 +3562,15 @@ wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
return true;
}
static void
increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
{
ctx->code_compiled_size += size;
if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
ctx->code_compiled_peak_size = ctx->code_compiled_size;
}
}
static void
wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
{
@ -3575,7 +3589,7 @@ wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += size;
increase_compiled_code_space(ctx, size);
}
}
@ -3593,7 +3607,7 @@ wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(uint32);
increase_compiled_code_space(ctx, sizeof(uint32));
}
}
@ -3611,7 +3625,7 @@ wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(int16);
increase_compiled_code_space(ctx, sizeof(uint16));
}
}
@ -3627,9 +3641,9 @@ wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
#endif
}
else {
ctx->code_compiled_size += sizeof(uint8);
increase_compiled_code_space(ctx, sizeof(uint8));
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
ctx->code_compiled_size++;
increase_compiled_code_space(ctx, sizeof(uint8));
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
}
@ -3649,7 +3663,7 @@ wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
bh_assert((ctx->code_compiled_size & 1) == 0);
#endif
ctx->code_compiled_size += sizeof(void *);
increase_compiled_code_space(ctx, sizeof(void *));
}
}