diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinException.java b/src/main/java/org/spongepowered/asm/mixin/MixinException.java new file mode 100644 index 00000000..ef71da5d --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/MixinException.java @@ -0,0 +1,46 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin; + +public class MixinException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public MixinException() { + } + + public MixinException(String message) { + super(message); + } + + public MixinException(Throwable cause) { + super(cause); + } + + public MixinException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java index 4e62a3ff..8fa55f71 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -33,7 +33,9 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option; /** *

Specifies that this mixin method should redirect the specified - * {@link #method} call to the method decorated with this annotation.

+ * method call or field access to the method decorated with this annotation.

+ * + *

Method Redirect Mode

* *

The handler method signature must match the hooked method precisely * but prepended with an arg of the owning object's type to accept the @@ -70,6 +72,40 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option; *

public boolean barProxy(Foo someObject, int abc, int def,
  *   int someInt, String someString)
* + * + *

Field Access Redirect Mode

+ * + *

The handler method signature varies depending on whether the redirector is + * handling a field write (PUTFIELD, PUTSTATIC) or a + * field read (GETFIELD, GETSTATIC).

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Operation (OPCODE)Handler signature
Read static field (GETSTATIC)private FieldType getFieldValue()
Read instance field (GETFIELD)private FieldType getFieldValue(OwnerType + * owner)
Write static field (PUTSTATIC)private void setFieldValue(FieldType value)
Write instance field (PUTFIELD)private void getFieldValue(OwnerType + * owner, FieldType value)
+ * + *

Field redirectors do not support capture of enclosing method arguments. + *

*/ @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java index 65722dfd..e28d3785 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java @@ -80,7 +80,7 @@ public abstract class InvokeInjector extends Injector { + " in " + this); } - this.inject(target, (MethodInsnNode)node); + this.injectAtInvoke(target, (MethodInsnNode)node); } /** @@ -89,7 +89,7 @@ public abstract class InvokeInjector extends Injector { * @param target Target to inject into * @param node Discovered instruction node */ - protected abstract void inject(Target target, MethodInsnNode node); + protected abstract void injectAtInvoke(Target target, MethodInsnNode node); /** * @param args handler arguments diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java index 45fad1a4..b67b2afa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java @@ -86,7 +86,7 @@ public class ModifyArgInjector extends InvokeInjector { * Do the injection */ @Override - protected void inject(Target target, MethodInsnNode node) { + protected void injectAtInvoke(Target target, MethodInsnNode node) { Type[] args = Type.getArgumentTypes(node.desc); int argIndex = this.findArgIndex(target, args); InsnList insns = new InsnList(); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java index 91ec7da1..e253961a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java @@ -26,8 +26,12 @@ package org.spongepowered.asm.mixin.injection.invoke; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; +import org.spongepowered.asm.lib.tree.AbstractInsnNode; +import org.spongepowered.asm.lib.tree.FieldInsnNode; import org.spongepowered.asm.lib.tree.InsnList; +import org.spongepowered.asm.lib.tree.InsnNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; +import org.spongepowered.asm.lib.tree.VarInsnNode; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; @@ -38,11 +42,12 @@ import com.google.common.collect.ObjectArrays; import com.google.common.primitives.Ints; /** - *

A bytecode injector which allows a method call to be redirected to the - * annotated handler method. The handler method signature must match the hooked - * method precisely but prepended with an arg of the owning object's type - * to accept the object instance the method was going to be invoked on. For - * example when hooking the following call:

+ *

A bytecode injector which allows a method call or field access to be + * redirected to the annotated handler method. For method redirects, the handler + * method signature must match the hooked method precisely but prepended + * with an arg of the owning object's type to accept the object instance the + * method was going to be invoked on. For example when hooking the following + * call:

* *
  *   int abc = 0;
@@ -62,6 +67,9 @@ import com.google.common.primitives.Ints;
  * 

For obvious reasons this does not apply for static methods, for static * methods it is sufficient that the signature simply match the hooked method. *

+ * + *

For field redirections, see the details in {@link Redirect} for the + * required signature of the handler method.

*/ public class RedirectInjector extends InvokeInjector { @@ -71,12 +79,33 @@ public class RedirectInjector extends InvokeInjector { public RedirectInjector(InjectionInfo info) { super(info, "@Redirect"); } - - /** - * Do the injection + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector + * #inject(org.spongepowered.asm.mixin.injection.callback.Target, + * org.objectweb.asm.tree.AbstractInsnNode) */ @Override - protected void inject(Target target, MethodInsnNode node) { + protected void inject(Target target, AbstractInsnNode node) { + if (node instanceof MethodInsnNode) { + this.injectAtInvoke(target, (MethodInsnNode)node); + return; + } + + if (node instanceof FieldInsnNode) { + this.injectAtFieldAccess(target, (FieldInsnNode)node); + return; + } + + throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting an invalid insn in " + + target + " in " + this); + } + + /** + * Redirect a method invocation + */ + @Override + protected void injectAtInvoke(Target target, MethodInsnNode node) { boolean injectTargetParams = false; boolean targetIsStatic = node.getOpcode() == Opcodes.INVOKESTATIC; Type ownerType = Type.getType("L" + node.owner + ";"); @@ -90,8 +119,8 @@ public class RedirectInjector extends InvokeInjector { if (alternateDesc.equals(this.methodNode.desc)) { injectTargetParams = true; } else { - throw new InvalidInjectionException(this.info, "@Redirect handler method " + this.methodNode.name + " has an invalid signature " - + ", expected " + desc + " found " + this.methodNode.desc); + throw new InvalidInjectionException(this.info, this.annotationType + " handler method " + this + + " has an invalid signature, expected " + desc + " found " + this.methodNode.desc); } } @@ -112,4 +141,87 @@ public class RedirectInjector extends InvokeInjector { target.addToLocals(extraLocals); target.addToStack(extraStack); } + + /** + * Redirect a field get or set operation + */ + private void injectAtFieldAccess(Target target, FieldInsnNode node) { + boolean staticField = node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.PUTSTATIC; + Type ownerType = Type.getType("L" + node.owner + ";"); + Type fieldType = Type.getType(node.desc); + + InsnList insns = new InsnList(); + int extraStack = 0; + if (node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.GETFIELD) { + extraStack = this.injectAtGetField(insns, target, node, staticField, ownerType, fieldType); + } else if (node.getOpcode() == Opcodes.PUTSTATIC || node.getOpcode() == Opcodes.PUTFIELD) { + extraStack = this.injectAtPutField(insns, target, node, staticField, ownerType, fieldType); + } else { + throw new InvalidInjectionException(this.info, "Unspported opcode " + node.getOpcode() + " on FieldInsnNode for " + this.info); + } + + target.insns.insertBefore(node, insns); + target.insns.remove(node); + target.addToStack(extraStack); + } + + /** + * Inject opcodes to redirect a field getter. The injection will vary based + * on the staticness of the field and the handler thus there are four + * possible scenarios based on the possible combinations of static on the + * handler and the field itself. + */ + private int injectAtGetField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { + String handlerDesc = staticField ? ASMHelper.generateDescriptor(fieldType) : ASMHelper.generateDescriptor(fieldType, owner); + this.checkDescriptor(handlerDesc, "getter"); + + if (!this.isStatic) { + insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); + if (!staticField) { + insns.add(new InsnNode(Opcodes.SWAP)); + } + } + + this.invokeHandler(insns); + return this.isStatic ? 0 : 1; + } + + /** + * Inject opcodes to redirect a field setter. The injection will vary based + * on the staticness of the field and the handler thus there are four + * possible scenarios based on the possible combinations of static on the + * handler and the field itself. + */ + private int injectAtPutField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { + String handlerDesc = staticField ? ASMHelper.generateDescriptor(null, fieldType) : ASMHelper.generateDescriptor(null, owner, fieldType); + this.checkDescriptor(handlerDesc, "setter"); + + if (!this.isStatic) { + if (staticField) { + insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); + insns.add(new InsnNode(Opcodes.SWAP)); + } else { + int marshallVar = target.allocateLocals(fieldType.getSize()); + insns.add(new VarInsnNode(fieldType.getOpcode(Opcodes.ISTORE), marshallVar)); + insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); + insns.add(new InsnNode(Opcodes.SWAP)); + insns.add(new VarInsnNode(fieldType.getOpcode(Opcodes.ILOAD), marshallVar)); + } + } + + this.invokeHandler(insns); + return !this.isStatic && !staticField ? 1 : 0; + } + + /** + * Check that the handler descriptor matches the calculated descriptor for + * the field access being redirected. + */ + private void checkDescriptor(String handlerDesc, String type) { + if (!this.methodNode.desc.equals(handlerDesc)) { + throw new InvalidInjectionException(this.info, this.annotationType + " field " + type + " " + this + + " has an invalid signature. Expected " + handlerDesc + " but found " + this.methodNode.desc); + } + } + } diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index b22d18b3..0e6f10aa 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -898,4 +898,49 @@ public class ASMHelper { return labels; } + + /** + * @param obfType + * @param returnType + * @param args + */ + public static String generateDescriptor(/*int obfType, */Object returnType, Object... args) + { + StringBuilder sb = new StringBuilder().append('('); + + for (Object arg : args) + { + sb.append(ASMHelper.toDescriptor(/*obfType, */arg)); + } + + return sb.append(')').append(returnType != null ? ASMHelper.toDescriptor(/*obfType, */returnType) : "V").toString(); + } + + /** + * @param obfType + * @param arg + */ + private static String toDescriptor(/*int obfType, */Object arg) + { +// if (arg instanceof Obf) +// { +// return ((Obf)arg).getDescriptor(obfType); +// } +// else + if (arg instanceof String) + { + return (String)arg; + } + else if (arg instanceof Type) + { + return arg.toString(); + } + else if (arg instanceof Class) + { + return Type.getDescriptor((Class)arg).toString(); + } + + return arg == null ? "" : arg.toString(); + } + } diff --git a/src/main/java/org/spongepowered/asm/util/LVTGeneratorException.java b/src/main/java/org/spongepowered/asm/util/LVTGeneratorException.java new file mode 100644 index 00000000..f6ba9af5 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/LVTGeneratorException.java @@ -0,0 +1,41 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.util; + +import org.spongepowered.asm.mixin.MixinException; + +public class LVTGeneratorException extends MixinException { + + private static final long serialVersionUID = 1L; + + public LVTGeneratorException(String message) { + super(message); + } + + public LVTGeneratorException(String message, Throwable cause) { + super(message, cause); + } + +}