Skip to content

Commit

Permalink
fixes #19404 by protecting the memory we borrow from. this replaces c…
Browse files Browse the repository at this point in the history
…rashes with minor memory leaks which seems to be acceptable. In the longer run we need a better VM that didn't grow hacks over a decade.
  • Loading branch information
Araq committed Feb 11, 2022
1 parent 15f54de commit fecc8b8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ proc bailOut(c: PCtx; tos: PStackFrame) =
when not defined(nimComputedGoto):
{.pragma: computedGoto.}

proc ensureKind(n: var TFullReg, kind: TRegisterKind) =
if n.kind != kind:
n = TFullReg(kind: kind)
proc ensureKind(n: var TFullReg, k: TRegisterKind) {.inline.} =
if n.kind != k:
n = TFullReg(kind: k)

template ensureKind(k: untyped) {.dirty.} =
ensureKind(regs[ra], k)
Expand Down Expand Up @@ -521,6 +521,11 @@ template maybeHandlePtr(node2: PNode, reg: TFullReg, isAssign2: bool): bool =
when not defined(nimHasSinkInference):
{.pragma: nosinks.}

template takeAddress(reg, source) =
reg.nodeAddr = addr source
when defined(gcDestructors):
GC_ref source

proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
var pc = start
var tos = tos
Expand Down Expand Up @@ -679,7 +684,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
let idx = regs[rc].intVal.int
let src = if regs[rb].kind == rkNode: regs[rb].node else: regs[rb].nodeAddr[]
if src.kind notin {nkEmpty..nkTripleStrLit} and idx <% src.len:
regs[ra].nodeAddr = addr src.sons[idx]
takeAddress regs[ra], src.sons[idx]
else:
stackTrace(c, tos, pc, formatErrorIndexBound(idx, src.safeLen-1))
of opcLdStrIdx:
Expand Down Expand Up @@ -747,11 +752,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
of nkObjConstr:
let n = src.sons[rc + 1]
if n.kind == nkExprColonExpr:
regs[ra].nodeAddr = addr n.sons[1]
takeAddress regs[ra], n.sons[1]
else:
regs[ra].nodeAddr = addr src.sons[rc + 1]
takeAddress regs[ra], src.sons[rc + 1]
else:
regs[ra].nodeAddr = addr src.sons[rc]
takeAddress regs[ra], src.sons[rc]
of opcWrObj:
# a.b = c
decodeBC(rkNode)
Expand All @@ -778,7 +783,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
decodeB(rkNodeAddr)
case regs[rb].kind
of rkNode:
regs[ra].nodeAddr = addr(regs[rb].node)
takeAddress regs[ra], regs[rb].node
of rkNodeAddr: # bug #14339
regs[ra].nodeAddr = regs[rb].nodeAddr
else:
Expand Down

0 comments on commit fecc8b8

Please sign in to comment.