Detect static mismatch and visibility reduction of overwrite and shadows

fixes #167
This commit is contained in:
Mumfrey committed 2017-02-20 15:20:35 +00:00
1 parent 1752418584
commit 433ab64dfd
2 files changed
+150 -10

No files matched your search

@@ -51,8 +51,9 @@ import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode;
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinMethodNode;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Bytecode.Visibility;
import org.spongepowered.asm.util.Constants;
/**
@@ -312,6 +313,7 @@ class MixinPreProcessorStandard {
this.checkMixinNotUnique(mixinMethod, type);
}
String description = "@" + Bytecode.getSimpleName(type);
Method method = this.getSpecialMethod(mixinMethod, type);
MethodNode target = context.findMethod(mixinMethod, annotation);
if (target == null) {
@@ -320,14 +322,28 @@ class MixinPreProcessorStandard {
}
target = context.findRemappedMethod(mixinMethod);
if (target == null) {
throw new InvalidMixinException(this.mixin, "@" + Bytecode.getSimpleName(type) + " method " + mixinMethod.name
throw new InvalidMixinException(this.mixin, description + " method " + mixinMethod.name + " in " + this.mixin
+ " was not located in the target class");
}
mixinMethod.name = method.renameTo(target.name);
}
if (Constants.CTOR.equals(target.name)) {
throw new InvalidMixinException(this.mixin, "Nice try! " + mixinMethod.name + " cannot alias a constructor!");
throw new InvalidMixinException(this.mixin, "Nice try! " + mixinMethod.name + " in " + this.mixin + " cannot alias a constructor!");
}
if (!Bytecode.compareFlags(mixinMethod, target, Opcodes.ACC_STATIC)) {
throw new InvalidMixinException(this.mixin, "STATIC modifier of " + description + " method " + mixinMethod.name + " in " + this.mixin
+ " does not match the target");
}
if (overwrite) {
Visibility visTarget = Bytecode.getVisibility(target);
Visibility visMethod = Bytecode.getVisibility(mixinMethod);
if (visMethod.ordinal() < visTarget.ordinal()) {
throw new InvalidMixinException(this.mixin, visMethod + " " + description + " method " + mixinMethod.name + " in " + this.mixin
+ " cannot reduce visibiliy of " + visTarget + " target method");
}
}
if (!target.name.equals(mixinMethod.name)) {
@@ -343,21 +359,21 @@ class MixinPreProcessorStandard {
protected Method getSpecialMethod(MixinMethodNode mixinMethod, Class<? extends Annotation> type) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
this.checkMethodNotUnique(type, method);
this.checkMethodNotUnique(method, type);
return method;
}
protected void checkMethodNotUnique(Class<? extends Annotation> type, Method method) {
protected void checkMethodNotUnique(Method method, Class<? extends Annotation> type) {
if (method.isUnique()) {
String annotation = "@" + Bytecode.getSimpleName(type);
throw new InvalidMixinException(this.mixin, annotation + " method " + method.getName() + " cannot be @Unique");
String description = "@" + Bytecode.getSimpleName(type);
throw new InvalidMixinException(this.mixin, description + " method " + method.getName() + " cannot be @Unique");
}
}
protected void checkMixinNotUnique(MixinMethodNode mixinMethod, Class<? extends Annotation> type) {
if (this.mixin.isUnique()) {
String annotation = "@" + Bytecode.getSimpleName(type);
throw new InvalidMixinException(this.mixin, annotation + " method " + mixinMethod.name + " found in a @Unique mixin");
String description = "@" + Bytecode.getSimpleName(type);
throw new InvalidMixinException(this.mixin, description + " method " + mixinMethod.name + " found in a @Unique mixin");
}
}
@@ -434,6 +450,11 @@ class MixinPreProcessorStandard {
mixinField.name = field.renameTo(target.name);
}
if (!Bytecode.compareFlags(mixinField, target, Opcodes.ACC_STATIC)) {
throw new InvalidMixinException(this.mixin, "STATIC modifier of @Shadow field " + mixinField.name + " in " + this.mixin
+ " does not match the target");
}
if (field.isUnique()) {
if ((mixinField.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) != 0) {
String uniqueName = context.getUniqueName(mixinField);
@@ -48,7 +48,39 @@ import com.google.common.primitives.Ints;
* Utility methods for working with bytecode via ASM
*/
public final class Bytecode {
/**
* Ordinal member visibility level. This is used to represent visibility of
* a member in a formal way from lowest to highest. The
* {@link Bytecode#getVisibility} methods can be used to convert access
* flags to this enum. The value returned from {@link #ordinal} can then be
* used to determine whether a visibility level is <i>higher</i> or <i>lower
* </i> than any other given visibility level.
*/
public enum Visibility {
/**
* Members decorated with {@link Opcodes#ACC_PRIVATE}
*/
PRIVATE,
/**
* Members decorated with {@link Opcodes#ACC_PROTECTED}
*/
PROTECTED,
/**
* Members not decorated with any access flags
*/
PACKAGE,
/**
* Members decorated with {@link Opcodes#ACC_PUBLIC}
*/
PUBLIC
}
/**
* Integer constant opcodes
*/
@@ -596,6 +628,93 @@ public final class Bytecode {
return (field.access & flag) == flag;
}
/**
* Check whether the status of the specified flag matches on both of the
* supplied arguments.
*
* @param m1 First method
* @param m2 Second method
* @param flag flag to compare
* @return True if the flag is set to the same value on both members
*/
public static boolean compareFlags(MethodNode m1, MethodNode m2, int flag) {
return Bytecode.hasFlag(m1, flag) == Bytecode.hasFlag(m2, flag);
}
/**
* Check whether the status of the specified flag matches on both of the
* supplied arguments.
*
* @param f1 First field
* @param f2 Second field
* @param flag flag to compare
* @return True if the flag is set to the same value on both members
*/
public static boolean compareFlags(FieldNode f1, FieldNode f2, int flag) {
return Bytecode.hasFlag(f1, flag) == Bytecode.hasFlag(f2, flag);
}
/**
* Returns the <i>ordinal visibility</i> of the supplied argument where a
* higher value equals higher "visibility":
*
* <ol start="0">
* <li>{@link #Visibility.PRIVATE}</li>
* <li>{@link #Visibility.PROTECTED}</li>
* <li>{@link #Visibility.PACKAGE}</li>
* <li>{@link #Visibility.PUBLIC}</li>
* </ol>
*
* @param method method to get visibility for
* @return visibility level
*/
public static Visibility getVisibility(MethodNode method) {
return Bytecode.getVisibility(method.access & 0x7);
}
/**
* Returns the <i>ordinal visibility</i> of the supplied argument where a
* higher value equals higher "visibility":
*
* <ol start="0">
* <li>{@link Visibility#PRIVATE}</li>
* <li>{@link Visibility#PROTECTED}</li>
* <li>{@link Visibility#PACKAGE}</li>
* <li>{@link Visibility#PUBLIC}</li>
* </ol>
*
* @param field field to get visibility for
* @return visibility level
*/
public static Visibility getVisibility(FieldNode field) {
return Bytecode.getVisibility(field.access & 0x7);
}
/**
* Returns the <i>ordinal visibility</i> of the supplied argument where a
* higher value equals higher "visibility":
*
* <ol start="0">
* <li>{@link Visibility#PRIVATE}</li>
* <li>{@link Visibility#PROTECTED}</li>
* <li>{@link Visibility#PACKAGE}</li>
* <li>{@link Visibility#PUBLIC}</li>
* </ol>
*
* @param flags access flags of member
* @return visibility level
*/
private static Visibility getVisibility(int flags) {
if ((flags & Opcodes.ACC_PROTECTED) != 0) {
return Bytecode.Visibility.PROTECTED;
} else if ((flags & Opcodes.ACC_PRIVATE) != 0) {
return Bytecode.Visibility.PRIVATE;
} else if ((flags & Opcodes.ACC_PUBLIC) != 0) {
return Bytecode.Visibility.PUBLIC;
}
return Bytecode.Visibility.PACKAGE;
}
/**
* Compute the largest line number found in the specified class
*