added stack overflow prevention

This commit is contained in:
Araq 2014-07-16 22:32:09 +02:00
commit ed226eba6d
4 changed files with 24 additions and 14 deletions

View file

@ -328,7 +328,8 @@ struct TFrame {
NCSTRING procname; NCSTRING procname;
NI line; NI line;
NCSTRING filename; NCSTRING filename;
NI len; NI16 len;
NI16 calldepth;
}; };
#define nimfr(proc, file) \ #define nimfr(proc, file) \

View file

@ -2001,7 +2001,8 @@ type
procname*: cstring ## name of the proc that is currently executing procname*: cstring ## name of the proc that is currently executing
line*: int ## line number 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 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): when defined(JS):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} = proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =

View file

@ -1,7 +1,7 @@
# #
# #
# Nimrod's Runtime Library # Nimrod's Runtime Library
# (c) Copyright 2012 Andreas Rumpf # (c) Copyright 2014 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -44,16 +44,6 @@ var
# a global variable for the root of all try blocks # a global variable for the root of all try blocks
currException {.rtlThreadVar.}: ref E_Base 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.} = proc popFrame {.compilerRtl, inl.} =
framePtr = framePtr.prev framePtr = framePtr.prev
@ -280,6 +270,23 @@ proc getStackTrace(e: ref E_Base): string =
else: else:
result = "" 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): when defined(endb):
var var
dbgAborting: bool # whether the debugger wants to abort dbgAborting: bool # whether the debugger wants to abort

View file

@ -16,7 +16,8 @@ News
now considered for implicit early binding. now considered for implicit early binding.
- ``c2nim`` moved into its own repository and is now a Babel package. - ``c2nim`` moved into its own repository and is now a Babel package.
- ``pas2nim`` 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 Library Additions
----------------- -----------------