From ed226eba6dab432ba47f3bb6e4a6b3b1dea64330 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 16 Jul 2014 22:32:09 +0200 Subject: [PATCH] added stack overflow prevention --- lib/nimbase.h | 3 ++- lib/system.nim | 3 ++- lib/system/excpt.nim | 29 ++++++++++++++++++----------- web/news.txt | 3 ++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/nimbase.h b/lib/nimbase.h index cfd33dca18..b430942270 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -328,7 +328,8 @@ struct TFrame { NCSTRING procname; NI line; NCSTRING filename; - NI len; + NI16 len; + NI16 calldepth; }; #define nimfr(proc, file) \ diff --git a/lib/system.nim b/lib/system.nim index 3c42385b78..2fb08563aa 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2001,7 +2001,8 @@ type procname*: cstring ## name of the proc that is currently executing line*: int ## line number of the proc that is currently executing filename*: cstring ## filename of the proc that is currently executing - len*: int ## length of the inspectable slots + len*: int16 ## length of the inspectable slots + calldepth*: int16 ## used for max call depth checking when defined(JS): proc add*(x: var string, y: cstring) {.asmNoStackFrame.} = diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index 63a61183f6..e1a5a958fe 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -1,7 +1,7 @@ # # # Nimrod's Runtime Library -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2014 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -44,16 +44,6 @@ var # a global variable for the root of all try blocks currException {.rtlThreadVar.}: ref E_Base -when defined(nimRequiresNimFrame): - proc nimFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = - s.prev = framePtr - framePtr = s -else: - proc pushFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = - # XXX only for backwards compatibility - s.prev = framePtr - framePtr = s - proc popFrame {.compilerRtl, inl.} = framePtr = framePtr.prev @@ -280,6 +270,23 @@ proc getStackTrace(e: ref E_Base): string = else: result = "" +when defined(nimRequiresNimFrame): + proc stackOverflow() {.noinline.} = + writeStackTrace() + showErrorMessage("Stack overflow\n") + quitOrDebug() + + proc nimFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = + s.calldepth = if framePtr == nil: 0 else: framePtr.calldepth+1 + s.prev = framePtr + framePtr = s + if s.calldepth == 2000: stackOverflow() +else: + proc pushFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = + # XXX only for backwards compatibility + s.prev = framePtr + framePtr = s + when defined(endb): var dbgAborting: bool # whether the debugger wants to abort diff --git a/web/news.txt b/web/news.txt index a8d525443f..a913c94ad7 100644 --- a/web/news.txt +++ b/web/news.txt @@ -16,7 +16,8 @@ News now considered for implicit early binding. - ``c2nim`` moved into its own repository and is now a Babel package. - ``pas2nim`` moved into its own repository and is now a Babel package. - + - ``system.$`` for floating point types now produces a human friendly string + representation. Library Additions -----------------