From eec61f16c95f42411ed831474e22ef63f094dc67 Mon Sep 17 00:00:00 2001 From: tequ Date: Wed, 22 Jan 2025 07:39:20 +0900 Subject: [PATCH] JSHooks Instruction Count (#423) --- src/quickjs/quickjs.c | 9 ++++++++- src/quickjs/quickjs.h | 2 ++ src/ripple/app/hook/applyHook.h | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/quickjs/quickjs.c b/src/quickjs/quickjs.c index 773d29e4b4..33faa19bc5 100644 --- a/src/quickjs/quickjs.c +++ b/src/quickjs/quickjs.c @@ -304,6 +304,7 @@ struct JSRuntime { uint32_t operator_count; #endif void *user_opaque; + int64_t instruction_count; int64_t instruction_limit; }; @@ -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); @@ -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. */ @@ -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, diff --git a/src/quickjs/quickjs.h b/src/quickjs/quickjs.h index 779fbda104..92726cfb46 100644 --- a/src/quickjs/quickjs.h +++ b/src/quickjs/quickjs.h @@ -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 diff --git a/src/ripple/app/hook/applyHook.h b/src/ripple/app/hook/applyHook.h index 2e5252b347..8a6014d303 100644 --- a/src/ripple/app/hook/applyHook.h +++ b/src/ripple/app/hook/applyHook.h @@ -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