[BUG] qjs: SIGSEGV in find_own_property during backtrace construction
BUG description:
Running qjs under an 8MB memory limit on a JavaScript input causes a segmentation fault in find_own_property() while building an exception backtrace.
The crash occurs through JS_DefineProperty() and build_backtrace(), suggesting that an invalid object shape/property state is reached during error handling.
Steps to Reproduce:
The PoC attachment contains the input file that triggers the crash:
COMMAND LINE:
./qjs --memory-limit 8M Null_Pointer
Expected behavior
qjs should handle malformed or adversarial scripts by reporting a script error or exiting cleanly, rather than dereferencing invalid object property state during backtrace construction.
version:
QuickJS version 2026-06-04 Git branch: master commit: https://github.com/bellard/quickjs/commit/04be246001599f5995fa2f2d8c91a0f198d3f34c
OS and version:
Ubuntu 22.04.5 LTS arch: x86_64
Compiler:
gcc 11.4.0 clang 14.0.0
GDB output excerpt:
Program received signal SIGSEGV, Segmentation fault.
#0 find_own_property (
p=0x2398db58,
atom=57,
ppr=
Additional Information:
Crash type: SIGSEGV Crash location: find_own_property() Memory limit: 8M Fully reproducible.
Hit this same crash independently (embedding with JS_SetMemoryLimit) and root-caused it: it's a use-after-free of the pending exception object, not shape corruption.
Sequence (confirmed with a gdb breakpoint on free_object, master 04be246):
- Bytecode throws OOM;
rt->current_exceptionholds the only reference to the error object. - The exception path in
JS_CallInternal(quickjs.c:20668) passes it tobuild_backtrace()as a borrowedJSValueConst. - Inside
build_backtrace,JS_NewString()for the stack string hits the memory limit again:js_alloc_string → JS_ThrowOutOfMemory → JS_Throw, andJS_ThrowdoesJS_FreeValue(ctx, rt->current_exception)(quickjs.c:7358) — freeing the objectbuild_backtraceis still working on. JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, …)(quickjs.c:7615) then runs on the freed object → SIGSEGV infind_own_property(p->shapeis NULL; gdb showsfree_mark=1).
Deep JS recursion is needed only because it makes the backtrace string large enough that its own allocation is the one that fails. Depending on which allocation inside build_backtrace fails instead, the same condition also surfaces as a null uncaught exception rather than a crash.
Source-only reproducer (no PoC file needed) — a recursive-descent parser under a 4MB limit; the crash needs the second OOM to land on the final JS_NewString, so sweep an allocation-size knob:
for pad in $(seq 50 25 2000); do
./qjs --memory-limit 4194304 -e "
var depth=500, padn=$pad;
var input='';
for (var i=0;i<depth;i++) input += \"<a x='yyyyyyyyyy'>\";
input += 'hello';
for (var i=0;i<depth;i++) input += '</a>';
var pos=0;
function parseElem(){
var start=input.indexOf('>',pos); var tag=input.slice(pos,start+1); pos=start+1;
var node={tag:tag,attrs:tag.split(\"'\"),children:[],pad:'p'.repeat(padn)};
while(pos<input.length){
if(input[pos]==='<'){ if(input[pos+1]==='/'){pos=input.indexOf('>',pos)+1; return node;} node.children.push(parseElem()); }
else { var next=input.indexOf('<',pos); node.children.push(input.slice(pos,next)); pos=next; }
}
return node;
}
var trees=[]; for(;;){ pos=0; trees.push(parseElem()); }
" >/dev/null 2>&1
rc=$?; [ $rc -ge 128 ] && echo "signal $((rc-128)) at pad=$pad"
done
Segfaults deterministically at pad=1400 on my x86_64 box (gcc 12); also reproduced on ARM64.
fixed