Skip to content

Add script cancellation support#1119

Open
tpetsas wants to merge 3 commits into
frida:mainfrom
tpetsas:feature/cancellable-scripts
Open

Add script cancellation support#1119
tpetsas wants to merge 3 commits into
frida:mainfrom
tpetsas:feature/cancellable-scripts

Conversation

@tpetsas

@tpetsas tpetsas commented Jun 12, 2026

Copy link
Copy Markdown

Add gum_script_cancel() API that allows graceful termination of a running script from another thread.

Implementation details:

  • QuickJS: Uses the engine's interrupt handler (JS_SetInterruptHandler()) to poll a per-script cancellation flag (is_cancelled) protected by a recursive mutex.
  • V8: We use Isolate::TerminateExecution() directly and skip exceptions via a try-catch block.

Key changes summary:

  • Add cancel vfunc to GumScriptInterface
  • Protect dispose/unload state transitions with a per-script GRecMutex
  • Move cancellation_mutex cleanup from destroy_context() to finalize() to prevent use-after-clear during dispose
  • Auto-dispose the script if cancelled during load
  • Handle interrupted/terminated exceptions in entrypoint execution
  • Include 3 unit tests (run both on QJS and V8)

Limitations:

Only interrupts JavaScript execution. If a script is blocked in native code (e.g., a NativeFunction call or a blocking syscall), cancellation will take effect once control returns to the JS engine.


To quickly test only the added unit tests, do the following:
QJS:

> build/tests/gum-tests -p '/GumJS/Script/script_can_be_cancelled#QJS' -p '/GumJS/Script/script_cancel_is_idempotent#QJS' -p '/GumJS/Script/script_cancel_during_load_should_auto_dispose#QJS'

V8:

> build/tests/gum-tests -p '/GumJS/Script/script_can_be_cancelled#V8' -p '/GumJS/Script/script_cancel_is_idempotent#V8' -p '/GumJS/Script/script_cancel_during_load_should_auto_dispose#V8'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant