Catch OutOfMemory and StackOverflow errors in Painless since it's safe

to do so.
This commit is contained in:
Jack Conradson 2016-08-10 18:09:45 -07:00
parent 82fc86553c
commit f38d64039b
3 changed files with 23 additions and 5 deletions

View File

@ -127,9 +127,9 @@ public final class PainlessScriptEngineService extends AbstractComponent impleme
if (value != null) {
compilerSettings.setPicky(Boolean.parseBoolean(value));
}
value = copy.remove(CompilerSettings.INITIAL_CALL_SITE_DEPTH);
if (value != null) {
compilerSettings.setInitialCallSiteDepth(Integer.parseInt(value));
}
@ -162,7 +162,8 @@ public final class PainlessScriptEngineService extends AbstractComponent impleme
return Compiler.compile(loader, scriptName == null ? INLINE_NAME : scriptName, scriptSource, compilerSettings);
}
}, COMPILATION_CONTEXT);
} catch (Exception e) {
// Note that it is safe to catch any of the following errors since Painless is stateless.
} catch (OutOfMemoryError | StackOverflowError | Exception e) {
throw convertToScriptException(scriptName == null ? scriptSource : scriptName, scriptSource, e);
}
}

View File

@ -119,8 +119,9 @@ final class ScriptImpl implements ExecutableScript, LeafSearchScript {
public Object run() {
try {
return executable.execute(variables, scorer, doc, aggregationValue);
} catch (PainlessError | BootstrapMethodError | IllegalAccessError | Exception t) {
throw convertToScriptException(t);
// Note that it is safe to catch any of the following errors since Painless is stateless.
} catch (PainlessError | BootstrapMethodError | OutOfMemoryError | StackOverflowError | Exception e) {
throw convertToScriptException(e);
}
}

View File

@ -19,6 +19,9 @@
package org.elasticsearch.painless;
import org.apache.lucene.util.Constants;
import org.elasticsearch.script.ScriptException;
import java.lang.invoke.WrongMethodTypeException;
import java.util.Arrays;
import java.util.Collections;
@ -218,4 +221,17 @@ public class WhenThingsGoWrongTests extends ScriptTestCase {
exec("BitSet bs = new BitSet(); bs.and(2);");
});
}
public void testOutOfMemoryError() {
assumeTrue("test only happens to work for sure on oracle jre", Constants.JAVA_VENDOR.startsWith("Oracle"));
expectScriptThrows(OutOfMemoryError.class, () -> {
exec("int[] x = new int[Integer.MAX_VALUE - 1];");
});
}
public void testStackOverflowError() {
expectScriptThrows(StackOverflowError.class, () -> {
exec("void recurse(int x, int y) {recurse(x, y)} recurse(1, 2);");
});
}
}