Skip to content

Commit b2850b4

Browse files
committed
fix #497: Stop VM execution after thread kills itself
1 parent 5bfc4f3 commit b2850b4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,14 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
781781
DISPATCH(); // this avoids freeing registers after "stopping" a warp script
782782
}
783783
FREE_REGS(ret);
784+
785+
if (atEnd) {
786+
if (regCount > 0)
787+
std::cout << "warning: VM: " << regCount << " registers were leaked by the script; this is most likely a bug in the VM or in the compiler" << std::endl;
788+
789+
return pos;
790+
}
791+
784792
DISPATCH();
785793
}
786794

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,3 +1773,24 @@ TEST(VirtualMachineTest, NoCrashWhenReadingProcedureArgsAfterStopping)
17731773
ASSERT_EQ(vm.registerCount(), 0);
17741774
ASSERT_TRUE(vm.atEnd());
17751775
}
1776+
1777+
unsigned int killTest(VirtualMachine *vm)
1778+
{
1779+
vm->kill();
1780+
return 1;
1781+
}
1782+
1783+
TEST(VirtualMachineTest, StopAfterKilled)
1784+
{
1785+
// Regtest for #497
1786+
static unsigned int bytecode[] = { OP_START, OP_NULL, OP_EXEC, 0, OP_NULL, OP_NULL, OP_HALT };
1787+
static BlockFunc functions[] = { &killTest };
1788+
1789+
VirtualMachine vm;
1790+
vm.setBytecode(bytecode);
1791+
vm.setFunctions(functions);
1792+
1793+
vm.run();
1794+
ASSERT_TRUE(vm.atEnd());
1795+
ASSERT_EQ(vm.registerCount(), 0);
1796+
}

0 commit comments

Comments
 (0)