Skip to content

Commit

Permalink
Improved stack overflow handling, fixes #709
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbattle committed Nov 30, 2019
1 parent bdedb54 commit 3ead045
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 50 deletions.
21 changes: 19 additions & 2 deletions core/interpreter/src/main/java/org/overture/interpreter/VDMPP.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,16 @@ protected ExitStatus interpret(List<File> filenames, String defaultName)
} catch (ContextException e)
{
println("Initialization: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}

dumpLogs();
return ExitStatus.EXIT_ERRORS;
} catch (Exception e)
Expand Down Expand Up @@ -348,7 +357,15 @@ protected ExitStatus interpret(List<File> filenames, String defaultName)
} catch (ContextException e)
{
println("Execution: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}
} catch (Exception e)
{
println("Execution: " + e);
Expand Down
21 changes: 19 additions & 2 deletions core/interpreter/src/main/java/org/overture/interpreter/VDMSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ protected ExitStatus interpret(List<File> filenames, String defaultName)
} catch (ContextException e)
{
println("Initialization: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}

return ExitStatus.EXIT_ERRORS;
} catch (Exception e)
{
Expand All @@ -309,7 +318,15 @@ protected ExitStatus interpret(List<File> filenames, String defaultName)
} catch (ContextException e)
{
println("Execution: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}
} catch (Exception e)
{
println("Execution: " + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ public Set<ContextException> initialize(AModuleModules m,
initialContext.putList(af.createPDefinitionAssistant().getNamedValues(d, initialContext));
} catch (ContextException e)
{
if (e.isStackOverflow()) // Stack overflow returns immediately
{
trouble.clear();
trouble.add(e);
return trouble;
}

trouble.add(e); // Carry on...
}
}
Expand All @@ -172,6 +179,13 @@ public Set<ContextException> initialize(AModuleModules m,
initialContext.putList(af.createPDefinitionAssistant().getNamedValues(d, initialContext));
} catch (ContextException e)
{
if (e.isStackOverflow()) // Stack overflow returns immediately
{
trouble.clear();
trouble.add(e);
return trouble;
}

trouble.add(e); // Carry on...
}
}
Expand All @@ -186,6 +200,13 @@ public Set<ContextException> initialize(AModuleModules m,
}
} catch (ContextException e)
{
if (e.isStackOverflow()) // Stack overflow returns immediately
{
trouble.clear();
trouble.add(e);
return trouble;
}

trouble.add(e); // Carry on...
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public void initialize(RootContext ctxt, ModuleList modules, DBGPReader dbgp)
if (e != null)
{
problems.addAll(e);

if (e.size() == 1 && e.iterator().next().isStackOverflow())
{
retries = 0;
break;
}
}
}
} while (--retries > 0 && !problems.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,16 @@ public static void main(String[] args)
} catch (ContextException e)
{
System.out.println("Initialization: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}

RTLogger.dump(true);
System.exit(3);
} catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,16 @@ public static void main(String[] args)
} catch (ContextException e)
{
System.err.println("Initialization: " + e);
e.ctxt.printStackTrace(Console.out, true);

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}
else
{
e.ctxt.printStackTrace(Console.out, true);
}

RTLogger.dump(true);
System.exit(3);
} catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected String format(String indent, Context what)
* @param out
*/

private static final int FRAMES_LIMIT = 100;
private static final int FRAMES_LIMIT = 50;

public void printStackFrames(PrintWriter out)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public String toString()
{
return getMessage();
}

public boolean isStackOverflow()
{
return number == 4174;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ private void runCmd()
{
setException(e);
suspendOthers();

if (e.isStackOverflow())
{
e.ctxt.printStackFrames(Console.out);
}

DebuggerReader.stopped(e.ctxt, e.location);
} catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,6 @@ public String toString()
return type.toString();
}

private static Integer stackUnwind = null;
private static final int UNWIND_COUNT = 20; // Needed by printStackFrames

private ContextException stackOverflow(StackOverflowError e, Context ctxt)
{
if (stackUnwind == null) // First time
{
stackUnwind = new Integer(UNWIND_COUNT);
}
else if (--stackUnwind <= 0)
{
Console.out.printf("Stack overflow %s\n", location);
ctxt.printStackFrames(Console.out);
return new ContextException(4174, "Stack overflow", location, ctxt);
}

throw e; // Unwind further to make space for printStackFrames
}

public Value eval(ILexLocation from, Value arg, Context ctxt)
throws AnalysisException
{
Expand All @@ -299,7 +280,7 @@ public Value eval(ILexLocation from, Value arg, Context ctxt)
}
catch (StackOverflowError e)
{
throw stackOverflow(e, ctxt);
throw new ContextException(4174, "Stack overflow", location, ctxt);
}
}

Expand All @@ -312,7 +293,7 @@ public Value eval(ILexLocation from, ValueList argValues, Context ctxt)
}
catch (StackOverflowError e)
{
throw stackOverflow(e, ctxt);
throw new ContextException(4174, "Stack overflow", location, ctxt);
}
}

Expand All @@ -337,8 +318,6 @@ public void setClass(SClassDefinition classdef)
public Value eval(ILexLocation from, ValueList argValues, Context ctxt,
Context sctxt) throws AnalysisException
{
stackUnwind = null;

if (uninstantiated)
{
abort(3033, "Polymorphic function has not been instantiated: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,6 @@ public void prepareGuard(ObjectContext ctxt)
}
}

private static Integer stackUnwind = null;
private static final int UNWIND_COUNT = 20; // Needed by printStackFrames

private ContextException stackOverflow(ILexLocation location, StackOverflowError e, Context ctxt)
{
if (stackUnwind == null) // First time
{
stackUnwind = new Integer(UNWIND_COUNT);
}
else if (--stackUnwind <= 0)
{
Console.out.printf("Stack overflow %s\n", location);
ctxt.printStackFrames(Console.out);
return new ContextException(4174, "Stack overflow", location, ctxt);
}

throw e; // Unwind further to make space for printStackFrames
}

public Value eval(ILexLocation from, ValueList argValues, Context ctxt)
throws AnalysisException
{
Expand All @@ -299,7 +280,7 @@ public Value eval(ILexLocation from, ValueList argValues, Context ctxt)
}
catch (StackOverflowError e)
{
throw stackOverflow(from, e, ctxt);
throw new ContextException(4174, "Stack overflow", from, ctxt);
}
}

Expand Down

0 comments on commit 3ead045

Please sign in to comment.