diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
index 0fb5c7b2..e28d674a 100644
--- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
+++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
@@ -48,6 +48,7 @@ import org.spongepowered.asm.launch.Blackboard;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.mixin.extensibility.IEnvironmentTokenProvider;
+import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.throwables.MixinException;
import org.spongepowered.asm.mixin.transformer.MixinTransformer;
@@ -380,6 +381,26 @@ public final class MixinEnvironment implements ITokenProvider {
*/
DEFAULT_COMPATIBILITY_LEVEL(Option.ENVIRONMENT, Inherit.INDEPENDENT, "compatLevel"),
+ /**
+ * Behaviour when the maximum defined {@link At#by} value is exceeded in
+ * a mixin. Currently the behaviour is to warn. In later
+ * versions of Mixin this may be promoted to error.
+ *
+ *
Available values for this option are:
+ *
+ *
+ * - ignore
+ * - Pre-0.7 behaviour, no action is taken when a violation is
+ * encountered
+ * - warn
+ * - Current behaviour, a WARN-level message is raised for
+ * violations
+ * - error
+ * - Violations throw an exception
+ *
+ */
+ SHIFT_BY_VIOLATION_BEHAVIOUR(Option.ENVIRONMENT, Inherit.INDEPENDENT, "shiftByViolation", "warn"),
+
/**
* Behaviour for initialiser injections, current supported options are
* "default" and "safe"
@@ -465,7 +486,7 @@ public final class MixinEnvironment implements ITokenProvider {
}
private Option(String property, String defaultStringValue) {
- this(null, Inherit.INHERIT, property, false, defaultStringValue);
+ this(null, Inherit.INDEPENDENT, property, false, defaultStringValue);
}
private Option(Option parent, String property) {
@@ -1146,6 +1167,18 @@ public final class MixinEnvironment implements ITokenProvider {
return option.getStringValue();
}
+ /**
+ * Get the specified option from the current environment
+ *
+ * @param option Option to get
+ * @param defaultValue value to use if the user-defined value is invalid
+ * @param enum type
+ * @return Option value
+ */
+ public > E getOption(Option option, E defaultValue) {
+ return option.getEnumValue(defaultValue);
+ }
+
/**
* Set the obfuscation context
*
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/At.java b/src/main/java/org/spongepowered/asm/mixin/injection/At.java
index b637c813..47806e11 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/At.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/At.java
@@ -134,7 +134,11 @@ public @interface At {
/**
* If {@link #shift} is specified as {@link At.Shift#BY BY}, specifies the
- * number of opcodes to shift by (negative numbers are allowed).
+ * number of opcodes to shift by (negative numbers are allowed). Note that
+ * values above 3 should be avoided and in general either replaced
+ * with a custom injection point or with sliced injection points. The
+ * warning/error threshold is defined by the config (with a hard limit on
+ * value of {@link InjectionPoint#MAX_ALLOWED_SHIFT_BY})
*
* @return Amount of shift to apply for the {@link At.Shift#BY BY} shift
*/
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java
index a2fb677f..6082ec50 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java
@@ -43,13 +43,17 @@ import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.MethodNode;
+import org.spongepowered.asm.mixin.MixinEnvironment;
+import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.modify.AfterStoreLocal;
import org.spongepowered.asm.mixin.injection.modify.BeforeLoadLocal;
import org.spongepowered.asm.mixin.injection.points.*;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
+import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
import org.spongepowered.asm.util.Annotations;
+import org.spongepowered.asm.util.Bytecode;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
@@ -126,6 +130,40 @@ public abstract class InjectionPoint {
public static final Selector DEFAULT = Selector.FIRST;
}
+
+ /**
+ * Behaviour for when the defined allowed value of {@link At#by} is exceeded
+ */
+ enum ShiftByViolationBehaviour {
+
+ /**
+ * Shift-by violations are ignored
+ */
+ IGNORE,
+
+ /**
+ * Shift-by violations cause a warning message
+ */
+ WARN,
+
+ /**
+ * Shift-by violations throw an exception
+ */
+ ERROR
+
+ }
+
+ /**
+ * Initial limit on the value of {@link At#by} which triggers warning/error
+ * (based on environment)
+ */
+ public static final int DEFAULT_ALLOWED_SHIFT_BY = 0;
+
+ /**
+ * Hard limit on the value of {@link At#by} which triggers warning/error
+ * (based on environment)
+ */
+ public static final int MAX_ALLOWED_SHIFT_BY = 0;
/**
* Available injection point types
@@ -197,7 +235,7 @@ public abstract class InjectionPoint {
*/
@Override
public String toString() {
- return "InjectionPoint(" + this.getClass().getSimpleName() + ")";
+ return String.format("@At(\"%s\")", this.getAtCode());
}
/**
@@ -546,7 +584,7 @@ public abstract class InjectionPoint {
InjectionPointData data = new InjectionPointData(context, method, parent, at, args, target, slice, ordinal, opcode, id);
Class extends InjectionPoint> ipClass = findClass(context, data);
InjectionPoint point = InjectionPoint.create(context, data, ipClass);
- return InjectionPoint.shift(point, shift, by);
+ return InjectionPoint.shift(context, method, parent, point, shift, by);
}
@SuppressWarnings("unchecked")
@@ -587,13 +625,16 @@ public abstract class InjectionPoint {
return point;
}
- private static InjectionPoint shift(InjectionPoint point, At.Shift shift, int by) {
+ private static InjectionPoint shift(IMixinContext context, MethodNode method, AnnotationNode parent, InjectionPoint point,
+ At.Shift shift, int by) {
+
if (point != null) {
if (shift == At.Shift.BEFORE) {
return InjectionPoint.before(point);
} else if (shift == At.Shift.AFTER) {
return InjectionPoint.after(point);
} else if (shift == At.Shift.BY) {
+ InjectionPoint.validateByValue(context, method, parent, point, by);
return InjectionPoint.shift(point, by);
}
}
@@ -601,6 +642,38 @@ public abstract class InjectionPoint {
return point;
}
+ private static void validateByValue(IMixinContext context, MethodNode method, AnnotationNode parent, InjectionPoint point, int by) {
+ MixinEnvironment env = context.getMixin().getConfig().getEnvironment();
+ ShiftByViolationBehaviour err = env.getOption(Option.SHIFT_BY_VIOLATION_BEHAVIOUR, ShiftByViolationBehaviour.WARN);
+ if (err == ShiftByViolationBehaviour.IGNORE) {
+ return;
+ }
+
+ int allowed = InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY;
+ if (context instanceof MixinTargetContext) {
+ allowed = ((MixinTargetContext)context).getMaxShiftByValue();
+ }
+
+ if (by <= allowed) {
+ return;
+ }
+
+ String message = String.format("@%s(%s) Shift.BY=%d on %s::%s exceeds the maximum allowed value %d.", Bytecode.getSimpleName(parent), point,
+ by, context, method.name, allowed);
+
+ if (err == ShiftByViolationBehaviour.WARN) {
+ LogManager.getLogger("mixin").warn("{} Increase the value of maxShiftBy to suppress this warning.", message);
+ return;
+ }
+
+ throw new InvalidInjectionException(context, message);
+ }
+
+ private String getAtCode() {
+ AtCode code = this.getClass().getAnnotation(AtCode.class);
+ return code == null ? this.getClass().getName() : code.value();
+ }
+
/**
* Register an injection point class. The supplied class must be decorated
* with an {@link AtCode} annotation for registration purposes.
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java
index f9e59358..21d79eec 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java
@@ -49,6 +49,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
+import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.refmap.ReferenceMapper;
@@ -79,6 +80,9 @@ final class MixinConfig implements Comparable, IMixinConfig {
@SerializedName("injectionPoints")
List injectionPoints;
+ @SerializedName("maxShiftBy")
+ int maxShiftBy = InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY;
+
}
/**
@@ -674,6 +678,18 @@ final class MixinConfig implements Comparable, IMixinConfig {
public boolean requireOverwriteAnnotations() {
return this.overwriteOptions.requireOverwriteAnnotations;
}
+
+ /**
+ * Get the maximum allowed value of {@link At#by}. High values of shift can
+ * indicate very brittle injectors and in general should be replaced with
+ * slices. This value determines the warning/error threshold (behaviour
+ * determined by the environment) for the value of by.
+ *
+ * @return defined shift warning threshold for this config
+ */
+ public int getMaxShiftByValue() {
+ return Math.min(Math.max(this.injectorOptions.maxShiftBy, 0), InjectionPoint.MAX_ALLOWED_SHIFT_BY);
+ }
// AMS - temp
public boolean select(MixinEnvironment environment) {
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
index 9a8e8207..2c717496 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
@@ -391,6 +391,15 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
return this.mixin.getParent().getDefaultInjectorGroup();
}
+ /**
+ * Get the max shift "by" value for the parent config
+ *
+ * @return max shift by value
+ */
+ public int getMaxShiftByValue() {
+ return this.mixin.getParent().getMaxShiftByValue();
+ }
+
/**
* Get the injector groups for this target
*