Permalink
Browse files

Attach a configurable timeout to expression evaluation

  • Loading branch information...
kenzierocks committed Nov 2, 2017
1 parent b951ba7 commit 56c83994cff6df462e06b6b2bfce5ea33a2b1800
@@ -104,6 +104,7 @@
public int navigationWand = ItemID.COMPASS;
public int navigationWandMaxDistance = 50;
public int scriptTimeout = 3000;
public int calculationTimeout = 100;
public Set<Integer> allowedDataCycleBlocks = new HashSet<Integer>();
public String saveDir = "schematics";
public String scriptsDir = "craftscripts";
@@ -19,6 +19,8 @@
package com.sk89q.worldedit.internal.expression;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.internal.expression.lexer.Lexer;
import com.sk89q.worldedit.internal.expression.lexer.tokens.Token;
import com.sk89q.worldedit.internal.expression.parser.Parser;
@@ -34,6 +36,13 @@
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Compiles and evaluates expressions.
@@ -69,6 +78,11 @@
public class Expression {
private static final ThreadLocal<Stack<Expression>> instance = new ThreadLocal<Stack<Expression>>();
private static final ExecutorService evalThread = Executors.newCachedThreadPool(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("worldedit-expression-eval-%d")
.build());
private final Map<String, RValue> variables = new HashMap<String, RValue>();
private final String[] variableNames;
@@ -115,9 +129,30 @@ public double evaluate(double... values) throws EvaluationException {
pushInstance();
try {
return root.getValue();
} catch (ReturnException e) {
return e.getValue();
Future<Double> result = evalThread.submit(new Callable<Double>() {
@Override
public Double call() throws Exception {
return root.getValue();
}
});
try {
return result.get(WorldEdit.getInstance().getConfiguration().calculationTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof ReturnException) {
return ((ReturnException) cause).getValue();
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
} catch (TimeoutException e) {
result.cancel(true);
throw new EvaluationException(-1, "Calculations exceeded time limit.");
}
} finally {
popInstance();
}
@@ -50,6 +50,9 @@ public double getValue() throws EvaluationException {
if (iterations > 256) {
throw new EvaluationException(getPosition(), "Loop exceeded 256 iterations.");
}
if (Thread.interrupted()) {
throw new EvaluationException(getPosition(), "Calculations exceeded time limit.");
}
++iterations;
try {
@@ -53,6 +53,9 @@ public double getValue() throws EvaluationException {
if (iterations > 256) {
throw new EvaluationException(getPosition(), "Loop exceeded 256 iterations.");
}
if (Thread.interrupted()) {
throw new EvaluationException(getPosition(), "Calculations exceeded time limit.");
}
++iterations;
try {
@@ -49,6 +49,9 @@ public double getValue() throws EvaluationException {
if (iterations > 256) {
throw new EvaluationException(getPosition(), "Loop exceeded 256 iterations.");
}
if (Thread.interrupted()) {
throw new EvaluationException(getPosition(), "Calculations exceeded time limit.");
}
++iterations;
try {
@@ -66,6 +69,9 @@ public double getValue() throws EvaluationException {
if (iterations > 256) {
throw new EvaluationException(getPosition(), "Loop exceeded 256 iterations.");
}
if (Thread.interrupted()) {
throw new EvaluationException(getPosition(), "Calculations exceeded time limit.");
}
++iterations;
try {
@@ -107,6 +107,7 @@ public void load() {
navigationWandMaxDistance = getInt("nav-wand-distance", navigationWandMaxDistance);
navigationUseGlass = getBool("nav-use-glass", navigationUseGlass);
scriptTimeout = getInt("scripting-timeout", scriptTimeout);
calculationTimeout = getInt("calculation-timeout", calculationTimeout);
saveDir = getString("schematic-save-dir", saveDir);
scriptsDir = getString("craftscript-dir", scriptsDir);
butcherDefaultRadius = getInt("butcher-default-radius", butcherDefaultRadius);
@@ -103,6 +103,8 @@ public void load() {
scriptTimeout = config.getInt("scripting.timeout", scriptTimeout);
scriptsDir = config.getString("scripting.dir", scriptsDir);
calculationTimeout = config.getInt("calculation.timeout", calculationTimeout);
saveDir = config.getString("saving.dir", saveDir);
allowSymlinks = config.getBoolean("files.allow-symbolic-links", false);

0 comments on commit 56c8399

Please sign in to comment.