Skip to content

Commit

Permalink
fix TOYWASM_USE_SEPARATE_VALIDATE=OFF
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed May 7, 2024
1 parent cbcc1c2 commit fa09d19
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,28 @@ read_op(const uint8_t **pp, const uint8_t *ep,
return ret;
}

static int
check_const_instruction(const struct instruction_desc *desc,
struct validation_context *vctx)
{
if (vctx->const_expr && (desc->flags & INSN_FLAG_CONST) == 0) {
return validation_failure(vctx,
"instruction \"%s\" not "
"allowed in a const expr",
desc->name);
}
return 0;
}

#if defined(TOYWASM_USE_SEPARATE_VALIDATE)
int
fetch_validate_next_insn(const uint8_t *p, const uint8_t *ep,
struct validation_context *vctx)
{
xassert(ep != NULL);
const struct instruction_desc *desc;
#if defined(TOYWASM_ENABLE_TRACING_INSN)
uint32_t pc;
pc = ptr2pc(vctx->module, p);
uint32_t pc = ptr2pc(vctx->module, p);
#endif
int ret;

Expand All @@ -86,11 +99,8 @@ fetch_validate_next_insn(const uint8_t *p, const uint8_t *ep,
goto fail;
}
xlog_trace_insn("inst %06" PRIx32 " %s", pc, desc->name);
if (vctx->const_expr && (desc->flags & INSN_FLAG_CONST) == 0) {
ret = validation_failure(vctx,
"instruction \"%s\" not "
"allowed in a const expr",
desc->name);
ret = check_const_instruction(desc, vctx);
if (ret != 0) {
goto fail;
}
#if defined(TOYWASM_USE_TAILCALL)
Expand All @@ -100,6 +110,34 @@ fetch_validate_next_insn(const uint8_t *p, const uint8_t *ep,
fail:
return ret;
}
#else
int
fetch_process_next_insn(const uint8_t **pp, const uint8_t *ep,
struct context *ctx)
{
xassert(ep != NULL);
struct validation_context *vctx = ctx->validation;

const struct instruction_desc *desc;
#if defined(TOYWASM_ENABLE_TRACING_INSN)
uint32_t pc = ptr2pc(vctx->module, p);
#endif
int ret;

ret = read_op(pp, ep, &desc);
if (ret != 0) {
goto fail;
}
xlog_trace_insn("inst %06" PRIx32 " %s", pc, desc->name);
ret = check_const_instruction(desc, vctx);
if (ret != 0) {
goto fail;
}
return desc->process(pp, ep, ctx);
fail:
return ret;
}
#endif /* defined(TOYWASM_USE_SEPARATE_VALIDATE) */

static int
read_expr_common(const uint8_t **pp, const uint8_t *ep, struct expr *expr,
Expand Down Expand Up @@ -161,13 +199,26 @@ read_expr_common(const uint8_t **pp, const uint8_t *ep, struct expr *expr,
goto fail;
}

#if !defined(TOYWASM_USE_SEPARATE_VALIDATE)
struct context ctx0;
struct context *ctx = &ctx0;
ctx->validation = vctx;
ctx->exec = NULL;
#endif
while (true) {
#if defined(TOYWASM_USE_SEPARATE_VALIDATE)
ret = fetch_validate_next_insn(p, ep, vctx);
if (ret != 0) {
goto fail;
}
assert(vctx->p > p);
p = vctx->p;
#else
ret = fetch_process_next_insn(&p, ep, ctx);
if (ret != 0) {
goto fail;
}
#endif
assert(p <= ep);
if (vctx->cframes.lsize == 0) {
break;
Expand Down

0 comments on commit fa09d19

Please sign in to comment.