Detect and warn on egregious uses of At.Shift.BY, will error in future

This commit is contained in:
Mumfrey committed 2017-07-19 13:13:01 +01:00
1 parent 583cd5b0a5
commit a290e810ca
5 files changed
+140 -5

No files matched your search

@@ -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 <tt>warn</tt>. In later
* versions of Mixin this may be promoted to <tt>error</tt>.
*
* <p>Available values for this option are:</p>
*
* <dl>
* <dt>ignore</dt>
* <dd>Pre-0.7 behaviour, no action is taken when a violation is
* encountered</dd>
* <dt>warn</dt>
* <dd>Current behaviour, a <tt>WARN</tt>-level message is raised for
* violations</dd>
* <dt>error</dt>
* <dd>Violations throw an exception</dd>
* </dl>
*/
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 <E> enum type
* @return Option value
*/
public <E extends Enum<E>> E getOption(Option option, E defaultValue) {
return option.getEnumValue(defaultValue);
}
/**
* Set the obfuscation context
*
@@ -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 <tt>3</tt> 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
*/
@@ -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.<ShiftByViolationBehaviour>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().<AtCode>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.
@@ -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<MixinConfig>, IMixinConfig {
@SerializedName("injectionPoints")
List<String> injectionPoints;
@SerializedName("maxShiftBy")
int maxShiftBy = InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY;
}
/**
@@ -674,6 +678,18 @@ final class MixinConfig implements Comparable<MixinConfig>, 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 <tt>by</tt>.
*
* @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) {
@@ -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
*