Repository navigation
Description
JS_SetInterruptHandler() is the documented API an embedder uses to bound the execution time of untrusted scripts. However, the C element-scan loops of several Array.prototype builtins never poll it. Calling one of them on a plain array-like object with a huge (but legal) length runs for minutes without the handler ever firing — while the exact same amount of work written as a JS loop is aborted within milliseconds by the same handler. (The official qjs CLI installs no interrupt handler, so this is not reproducible from the CLI.)
Repro code
Save the following as repro.js:
Array.prototype.indexOf.call({ length: 4294967295 }, 99);The input is a single line of valid JavaScript; the object has no elements at all — 4294967295 is the maximum legal array length.
For comparison, save the following as control.js — the same number of iterations as a plain JS loop:
var n = 0; for (var i = 0; i < 4294967295; i++) { n += i & 1; }Reproduced with the official fuzz_eval harness (the OSS-Fuzz QuickJS JS-source target, attached as fuzz_eval.c): it evaluates the input via JS_Eval with the std/os modules loaded and JS_SetInterruptHandler() installed to abort after 100 polls. Build options:
clang -O1 -g -D_GNU_SOURCE -fno-omit-frame-pointer -fsanitize=fuzzer,undefined \
-I. fuzz_eval.c quickjs.c quickjs-libc.c libregexp.c libunicode.c dtoa.c \
-o fuzz_eval -lm -lpthread -ldl
Replay the inputs:
$ timeout -s KILL 10s ./fuzz_eval repro.js # hangs; killed by timeout (rc=137)
$ timeout -s KILL 10s ./fuzz_eval control.js # interrupted at once; exits 0
With repro.js the interrupt handler never fires inside the builtin and the target hangs until killed. With control.js the same handler aborts execution in well under a second (InternalError: interrupted).
Expected behavior
With an interrupt handler installed, the builtin's scan loops should be interruptible like every other execution path: js_poll_interrupts() should be consulted inside the element-scan loops (as it is on interpreter backward branches and in other long-running C loops, e.g. the proxy/property loop at quickjs.c ~8880, "must check for timeout to avoid infinite loop").
Actual behavior / root cause
The scan loops contain no interrupt polling, so once the engine is inside them the handler never fires and JS_Eval cannot return until the full length has been walked (~4.29e9 property lookups; minutes in a release build). Affected builtins (line numbers at the commit below):
| builtin | function | uninterruptible loops |
|---|---|---|
Array.prototype.indexOf |
js_array_indexOf (quickjs.c:43803) |
quickjs.c:43822, quickjs.c:43828 |
Array.prototype.includes |
js_array_includes (quickjs.c:43754) |
quickjs.c:43775, quickjs.c:43782 |
Array.prototype.lastIndexOf |
js_array_lastIndexOf (quickjs.c:43851) |
same pattern |
Array.prototype.every / some / forEach / map |
js_array_every (quickjs.c:43475) |
C loop with JS_GetPropertyInt64 per index; the per-element JS_Call only happens for present properties, so on holey array-likes nothing is polled |
find/findIndex are not affected (they invoke the callback per index, and JS_Call polls the interrupt at entry). A Proxy target is also not affected for the same reason — which is exactly why plain array-like objects are the worst case: the per-index property lookup takes the fast shape-lookup path and never enters a poll point.
Suggested fix
Poll js_poll_interrupts(ctx) inside these loops (e.g. once per N iterations) and bail out with the interrupted exception when it reports, mirroring what other long-running C paths already do.
Version
- QuickJS version/release: quickjs-ng
v0.16.1dev (master) - Git commit:
82626eacc81825340736af5a6f363f5ebdf8aede - Operating system: Ubuntu 24.04 x86_64 (clang 18)
Activity
bnoordhuis commentedon Aug 18, 2026
The documentation doesn't make claims about running untrusted code. JS_SetInterruptHandler exists to catch things like scripts with infinite loops; the docs hint obliquely at that: "[..] used by the command line interpreter to implement a Ctrl-C handler"
I don't think it's a bad idea to add js_poll_interrupts calls at loop edges, it's just that there are tons of built-ins that then need updating.
edit: for at least some of the Array prototype methods, interrupt checks fall out naturally because they call into JS, so maybe it's not so bad.
Add interrupt checks to Array prototype methods