Skip to content

Commit

Permalink
JSHooks Instruction Count (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
tequdev authored Jan 21, 2025
1 parent 1bed7f1 commit eec61f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ struct JSRuntime {
uint32_t operator_count;
#endif
void *user_opaque;
int64_t instruction_count;
int64_t instruction_limit;
};

Expand Down Expand Up @@ -1637,6 +1638,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque, uint32_t in
}
rt->malloc_state = ms;
rt->malloc_gc_threshold = 256 * 1024;
rt->instruction_count = 0;
rt->instruction_limit = instructionLimit;

bf_context_init(&rt->bf_ctx, js_bf_realloc, rt);
Expand Down Expand Up @@ -6878,7 +6880,7 @@ static no_inline __exception int __js_poll_interrupts(JSContext *ctx)

static inline __exception int js_poll_interrupts(JSContext *ctx)
{
if (unlikely(--ctx->rt->instruction_limit <= 0))
if (unlikely(ctx->rt->instruction_limit <= ++ctx->rt->instruction_count))
{
/* XXX: should set a specific flag to avoid catching */
/* RHTODO: investigate if this is user-catchable. */
Expand All @@ -6893,6 +6895,11 @@ static inline __exception int js_poll_interrupts(JSContext *ctx)
}
}

int64_t JS_GetInstructionCount(JSContext *ctx)
{
return ctx->rt->instruction_count;
}

/* return -1 (exception) or TRUE/FALSE */
static int JS_SetPrototypeInternal(JSContext *ctx, JSValueConst obj,
JSValueConst proto_val,
Expand Down
2 changes: 2 additions & 0 deletions src/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,8 @@ int js_get_length64(JSContext *ctx, int64_t *pres,

JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst obj, int64_t idx);

int64_t JS_GetInstructionCount(JSContext *ctx);

#undef js_unlikely
#undef js_force_inline

Expand Down
4 changes: 4 additions & 0 deletions src/ripple/app/hook/applyHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,10 @@ class HookExecutorJS : public HookExecutorBase
{
JLOG(j.warn()) << "HookInfo[" << HC_ACC() << "]: JSVM Exited with ROLLBACK";
}
JLOG(j.warn()) << "HookInfo[" << HC_ACC()
<< "]: Instruction Count: "
<< JS_GetInstructionCount(ctx);
hookCtx.result.instructionCount = JS_GetInstructionCount(ctx);
}
/*
// RHTODO: place jsvm_error exit type logic appropriately
Expand Down

0 comments on commit eec61f1

Please sign in to comment.