Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM: allow overriding MaxLoopIterations without rebuilding nim #13233

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
VM: allow overriding MaxLoopIterations without rebuilding nim
timotheecour committed Jan 22, 2020

Verified

This commit was signed with the committer’s verified signature.
panh99 Heng Pan
commit d3e6bcdd05177897adebf73f21959444946b4216
3 changes: 3 additions & 0 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
@@ -678,6 +678,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
setTarget(conf.target, conf.target.targetOS, cpu)
of "run", "r":
processOnOffSwitchG(conf, {optRun}, arg, pass, info)
of "maxloopiterationsvm":
expectArg(conf, switch, arg, pass, info)
conf.maxLoopIterationsVM = parseInt(arg)
of "errormax":
expectArg(conf, switch, arg, pass, info)
# Note: `nim check` (etc) can overwrite this.
4 changes: 3 additions & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
@@ -235,6 +235,7 @@ type
hintCounter*: int
warnCounter*: int
errorMax*: int
maxLoopIterationsVM*: int ## VM: max iterations of all loops
configVars*: StringTableRef
symbols*: StringTableRef ## We need to use a StringTableRef here as defined
## symbols are always guaranteed to be style
@@ -380,7 +381,8 @@ proc newConfigRef*(): ConfigRef =
ccompilerpath: "",
toCompile: @[],
arguments: "",
suggestMaxResults: 10_000
suggestMaxResults: 10_000,
maxLoopIterationsVM: 10_000_000,
)
setTargetFromSystem(result.target)
# enable colors by default on terminals
7 changes: 3 additions & 4 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
@@ -486,11 +486,11 @@ proc compile(c: PCtx, s: PSym): int =
template handleJmpBack() {.dirty.} =
if c.loopIterations <= 0:
if allowInfiniteLoops in c.features:
c.loopIterations = MaxLoopIterations
c.loopIterations = c.config.maxLoopIterationsVM
else:
msgWriteln(c.config, "stack trace: (most recent call last)")
stackTraceAux(c, tos, pc)
globalError(c.config, c.debug[pc], errTooManyIterations)
globalError(c.config, c.debug[pc], errTooManyIterations % $c.config.maxLoopIterationsVM)
dec(c.loopIterations)

proc recSetFlagIsRef(arg: PNode) =
@@ -513,8 +513,7 @@ const
errConstantDivisionByZero = "division by zero"
errIllegalConvFromXtoY = "illegal conversion from '$1' to '$2'"
errTooManyIterations = "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"
"if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: $1)"
errFieldXNotFound = "node lacks field: "

proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
6 changes: 2 additions & 4 deletions compiler/vmdef.nim
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@ const

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

MaxLoopIterations* = 10_000_000 # max iterations of all loops

# Calculate register shifts, masks and ranges

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

proc refresh*(c: PCtx, module: PSym) =
c.module = module
c.prc = PProc(blocks: @[])
c.loopIterations = MaxLoopIterations
c.loopIterations = c.config.maxLoopIterationsVM

proc registerCallback*(c: PCtx; name: string; callback: VmCallback): int {.discardable.} =
result = c.callbacks.len
1 change: 1 addition & 0 deletions doc/advopt.txt
Original file line number Diff line number Diff line change
@@ -125,6 +125,7 @@ Advanced options:
--incremental:on|off only recompile the changed modules (experimental!)
--verbosity:0|1|2|3 set Nim's verbosity level (1 is default)
--errorMax:N stop compilation after N errors; 0 means unlimited
--maxLoopIterationsVM:N set max iterations for all VM loops
--experimental:$1
enable experimental language feature
--legacy:$2
2 changes: 1 addition & 1 deletion tests/vm/tmaxloopiterations.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
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"
errormsg: "interpretation requires too many iterations; if you are sure this is not a bug in your code"
"""

# issue #9829