Skip to content

Commit b462bae

Browse files
timotheecourAraq
authored andcommitted
VM: allow overriding MaxLoopIterations without rebuilding nim (#13233)
1 parent f12bea1 commit b462bae

File tree

6 files changed

+13
-10
lines changed

6 files changed

+13
-10
lines changed

compiler/commands.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
678678
setTarget(conf.target, conf.target.targetOS, cpu)
679679
of "run", "r":
680680
processOnOffSwitchG(conf, {optRun}, arg, pass, info)
681+
of "maxloopiterationsvm":
682+
expectArg(conf, switch, arg, pass, info)
683+
conf.maxLoopIterationsVM = parseInt(arg)
681684
of "errormax":
682685
expectArg(conf, switch, arg, pass, info)
683686
# Note: `nim check` (etc) can overwrite this.

compiler/options.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ type
235235
hintCounter*: int
236236
warnCounter*: int
237237
errorMax*: int
238+
maxLoopIterationsVM*: int ## VM: max iterations of all loops
238239
configVars*: StringTableRef
239240
symbols*: StringTableRef ## We need to use a StringTableRef here as defined
240241
## symbols are always guaranteed to be style
@@ -380,7 +381,8 @@ proc newConfigRef*(): ConfigRef =
380381
ccompilerpath: "",
381382
toCompile: @[],
382383
arguments: "",
383-
suggestMaxResults: 10_000
384+
suggestMaxResults: 10_000,
385+
maxLoopIterationsVM: 10_000_000,
384386
)
385387
setTargetFromSystem(result.target)
386388
# enable colors by default on terminals

compiler/vm.nim

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ proc compile(c: PCtx, s: PSym): int =
486486
template handleJmpBack() {.dirty.} =
487487
if c.loopIterations <= 0:
488488
if allowInfiniteLoops in c.features:
489-
c.loopIterations = MaxLoopIterations
489+
c.loopIterations = c.config.maxLoopIterationsVM
490490
else:
491491
msgWriteln(c.config, "stack trace: (most recent call last)")
492492
stackTraceAux(c, tos, pc)
493-
globalError(c.config, c.debug[pc], errTooManyIterations)
493+
globalError(c.config, c.debug[pc], errTooManyIterations % $c.config.maxLoopIterationsVM)
494494
dec(c.loopIterations)
495495

496496
proc recSetFlagIsRef(arg: PNode) =
@@ -513,8 +513,7 @@ const
513513
errConstantDivisionByZero = "division by zero"
514514
errIllegalConvFromXtoY = "illegal conversion from '$1' to '$2'"
515515
errTooManyIterations = "interpretation requires too many iterations; " &
516-
"if you are sure this is not a bug in your code edit " &
517-
"compiler/vmdef.MaxLoopIterations and rebuild the compiler"
516+
"if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: $1)"
518517
errFieldXNotFound = "node lacks field: "
519518

520519
proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =

compiler/vmdef.nim

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const
2323

2424
byteExcess* = 128 # we use excess-K for immediates
2525

26-
MaxLoopIterations* = 10_000_000 # max iterations of all loops
27-
2826
# Calculate register shifts, masks and ranges
2927

3028
const
@@ -259,14 +257,14 @@ type
259257
proc newCtx*(module: PSym; cache: IdentCache; g: ModuleGraph): PCtx =
260258
PCtx(code: @[], debug: @[],
261259
globals: newNode(nkStmtListExpr), constants: newNode(nkStmtList), types: @[],
262-
prc: PProc(blocks: @[]), module: module, loopIterations: MaxLoopIterations,
260+
prc: PProc(blocks: @[]), module: module, loopIterations: g.config.maxLoopIterationsVM,
263261
comesFromHeuristic: unknownLineInfo, callbacks: @[], errorFlag: "",
264262
cache: cache, config: g.config, graph: g)
265263

266264
proc refresh*(c: PCtx, module: PSym) =
267265
c.module = module
268266
c.prc = PProc(blocks: @[])
269-
c.loopIterations = MaxLoopIterations
267+
c.loopIterations = c.config.maxLoopIterationsVM
270268

271269
proc registerCallback*(c: PCtx; name: string; callback: VmCallback): int {.discardable.} =
272270
result = c.callbacks.len

doc/advopt.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Advanced options:
125125
--incremental:on|off only recompile the changed modules (experimental!)
126126
--verbosity:0|1|2|3 set Nim's verbosity level (1 is default)
127127
--errorMax:N stop compilation after N errors; 0 means unlimited
128+
--maxLoopIterationsVM:N set max iterations for all VM loops
128129
--experimental:$1
129130
enable experimental language feature
130131
--legacy:$2

tests/vm/tmaxloopiterations.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
discard """
2-
errormsg: "interpretation requires too many iterations; if you are sure this is not a bug in your code edit compiler/vmdef.MaxLoopIterations and rebuild the compiler"
2+
errormsg: "interpretation requires too many iterations; if you are sure this is not a bug in your code"
33
"""
44

55
# issue #9829

0 commit comments

Comments
 (0)