]> git.kaiwu.me - njs.git/commitdiff
VM: fix call stack sharing in cloned VMs
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 12 Nov 2026 04:04:23 +0000 (20:04 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 12 Nov 2026 22:29:31 +0000 (14:29 -0800)
Previously, njs_vm_clone() copied the call stack pointers of the VM
being cloned.  A VM created with the "init" option keeps its initial
frame, so every clone allocated frames in the spare space of that
frame instead of its own memory.  When two clones of the same VM ran at
the same time, they overwrote each other's frames.  In nginx, this
happened when a parent handler evaluated a js_set variable of a
subrequest: the subrequest clone overwrote the frames of the running
parent clone, which crashed after returning from the native call.

Now a cloned VM starts with an empty call stack and allocates its own
initial frame.

The issue was introduced in 16f42c87 (0.8.8).

src/njs_vm.c

index 6bd459f19cc7c6e8ee71364c0b5ea1067e5b3c0e..ed98c4674ed1aa42460b72dd995d8acec85241a6 100644 (file)
@@ -442,6 +442,9 @@ njs_vm_clone(njs_vm_t *vm, njs_external_ptr_t external)
     nvm->trace.data = nvm;
     nvm->external = external;
 
+    nvm->active_frame = NULL;
+    nvm->top_frame = NULL;
+
     nvm->shared_atom_count = vm->atom_id_generator;
 
     njs_flathsh_init(&nvm->atom_hash);