Bugzilla #312702: Feature for limiting call stack depth in interpeter

This commit is contained in:
szegedia%freemail.hu 2005-11-04 13:10:56 +00:00
Родитель 9dd7459c1b
Коммит 6074b83bc6
2 изменённых файлов: 55 добавлений и 1 удалений

Просмотреть файл

@ -272,6 +272,7 @@ public class Context
{
setLanguageVersion(VERSION_DEFAULT);
optimizationLevel = codegenClass != null ? 0 : -1;
maximumInterpreterStackDepth = Integer.MAX_VALUE;
}
/**
@ -1755,7 +1756,7 @@ public class Context
{
return optimizationLevel;
}
/**
* Set the current optimization level.
* <p>
@ -1800,6 +1801,54 @@ public class Context
"Optimization level outside [-1..9]: "+optimizationLevel);
}
/**
* Returns the maximum stack depth (in terms of number of call frames)
* allowed in a single invocation of interpreter. If the set depth would be
* exceeded, the interpreter will throw an EvaluatorException in the script.
* Defaults to Integer.MAX_VALUE. The setting only has effect for
* interpreted functions (those compiled with optimization level set to -1).
* As the interpreter doesn't use the Java stack but rather manages its own
* stack in the heap memory, a runaway recursion in interpreted code would
* eventually consume all available memory and cause OutOfMemoryError
* instead of a StackOverflowError limited to only a single thread. This
* setting helps prevent such situations.
*
* @return The current maximum interpreter stack depth.
*/
public final int getMaximumInterpreterStackDepth()
{
return maximumInterpreterStackDepth;
}
/**
* Sets the maximum stack depth (in terms of number of call frames)
* allowed in a single invocation of interpreter. If the set depth would be
* exceeded, the interpreter will throw an EvaluatorException in the script.
* Defaults to Integer.MAX_VALUE. The setting only has effect for
* interpreted functions (those compiled with optimization level set to -1).
* As the interpreter doesn't use the Java stack but rather manages its own
* stack in the heap memory, a runaway recursion in interpreted code would
* eventually consume all available memory and cause OutOfMemoryError
* instead of a StackOverflowError limited to only a single thread. This
* setting helps prevent such situations.
*
* @param max the new maximum interpreter stack depth
* @throws IllegalStateException if this context's optimization level is not
* -1
* @throws IllegalArgumentException if the new depth is not at least 1
*/
public final void setMaximumInterpreterStackDepth(int max)
{
if(sealed) onSealedMutation();
if(optimizationLevel != -1) {
throw new IllegalStateException("Cannot set maximumInterpreterStackDepth when optimizationLevel != -1");
}
if(max < 1) {
throw new IllegalArgumentException("Cannot set maximumInterpreterStackDepth to less than 1");
}
maximumInterpreterStackDepth = max;
}
/**
* Set the security controller for this context.
* <p> SecurityController may only be set if it is currently null
@ -2412,6 +2461,7 @@ public class Context
boolean compileFunctionsWithDynamicScopeFlag;
boolean useDynamicScope;
private int optimizationLevel;
private int maximumInterpreterStackDepth;
private WrapFactory wrapFactory;
Debugger debugger;
private Object debuggerData;

Просмотреть файл

@ -3787,6 +3787,10 @@ switch (op) {
frame.parentFrame = parentFrame;
frame.frameIndex = (parentFrame == null)
? 0 : parentFrame.frameIndex + 1;
if(frame.frameIndex > cx.getMaximumInterpreterStackDepth())
{
throw Context.reportRuntimeError("Exceeded maximum stack depth");
}
frame.frozen = false;
frame.fnOrScript = fnOrScript;