Skip to content

Commit

Permalink
stop assuming no-op frame_clear
Browse files Browse the repository at this point in the history
also, add a few comments and assertions.
  • Loading branch information
yamt committed Sep 26, 2023
1 parent 4cbdb91 commit 8f640e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ frame_enter(struct exec_context *ctx, struct instance *inst, uint32_t funcidx,
* frame->instance here.
*/
frame->callerpc = ptr2pc(ctx->instance->module, ctx->p);
} else {
/*
* Note: callerpc of the first frame is unused right now.
* Poison it with an invalid value to ensure no one is
* relying on the value.
*/
#if !defined(NDEBUG)
frame->callerpc = 0xdeadbeef;
#endif
}
frame->height = ctx->stack.lsize;
#if defined(TOYWASM_USE_SEPARATE_LOCALS)
Expand Down Expand Up @@ -292,6 +301,10 @@ frame_exit(struct exec_context *ctx)
if (ctx->frames.lsize > 0) {
const struct funcframe *pframe = &VEC_LASTELEM(ctx->frames);
set_current_frame(ctx, pframe, NULL);
/*
* Note: frame->callerpc belongs to the module of pframe,
* which we have just restored by the set_current_frame above.
*/
ctx->p = pc2ptr(ctx->instance->module, frame->callerpc);
}
assert(frame->labelidx <= ctx->labels.lsize);
Expand All @@ -300,7 +313,6 @@ frame_exit(struct exec_context *ctx)
assert(frame->localidx <= ctx->locals.lsize);
ctx->locals.lsize = frame->localidx;
#endif
frame_clear(frame);
}

static const struct jump *
Expand Down Expand Up @@ -454,6 +466,7 @@ do_return_call(struct exec_context *ctx, const struct funcinst *finst)
const struct funcframe *frame = &VEC_LASTELEM(ctx->frames);
uint32_t height = frame->height;
frame_exit(ctx);
frame_clear(frame);

const struct functype *ft = funcinst_functype(finst);
uint32_t arity = resulttype_cellsize(&ft->parameter);
Expand Down Expand Up @@ -743,6 +756,7 @@ do_branch(struct exec_context *ctx, uint32_t labelidx, bool goto_else)
frame_exit(ctx);
height = frame->height;
arity = frame->nresults;
frame_clear(frame);
} else {
if (branch_to_label(ctx, labelidx, goto_else, &height,
&arity)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/insn_impl_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ INSN_IMPL(end)
frame->height + frame->nresults);
#endif
rewind_stack(ectx, frame->height, frame->nresults);
LOAD_STACK_PTR;
frame_clear(frame);
LOAD_STACK_PTR; /* after rewind_stack */
if (__predict_false(ectx->frames.lsize == 0)) {
INSN_SUCCESS_RETURN;
}
Expand Down

0 comments on commit 8f640e5

Please sign in to comment.