From 7427f06dbb71bb5b6bacbcaa964880d34dcdab91 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 21 May 2015 17:45:19 +0100 Subject: [PATCH] Add AfterInvoke (INVOKE_ASSIGN) injection point type --- .../spongepowered/asm/mixin/injection/At.java | 10 +- .../asm/mixin/injection/InjectionPoint.java | 19 ++++ .../mixin/injection/points/AfterInvoke.java | 93 +++++++++++++++++++ .../mixin/injection/points/BeforeInvoke.java | 8 +- 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/points/AfterInvoke.java 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 e1d74aeb..8230e8bc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/At.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/At.java @@ -62,6 +62,8 @@ public @interface At { * {@link org.spongepowered.asm.mixin.injection.points.MethodHead HEAD}, * {@link org.spongepowered.asm.mixin.injection.points.BeforeReturn RETURN}, * {@link org.spongepowered.asm.mixin.injection.points.BeforeInvoke INVOKE}, + * {@link org.spongepowered.asm.mixin.injection.points.AfterInvoke + * INVOKE_ASSIGN}, * {@link org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess * FIELD}, * {@link org.spongepowered.asm.mixin.injection.points.BeforeNew NEW}, @@ -104,10 +106,10 @@ public @interface At { public String[] args() default { }; /** - * Target member used by INVOKE, INVOKE_STRING and FIELD. This must be - * specified as a fully-qualified member path including the class name - * and signature. Failing to fully-qualify the target member will result in - * an error at obfuscation time. + * Target member used by INVOKE, INVOKE_STRING, INVOKE_ASSIGN and FIELD. + * This must be specified as a fully-qualified member path including + * the class name and signature. Failing to fully-qualify the target member + * will result in an error at obfuscation time. * * @return target reference for supported InjectionPoint types */ 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 d794e397..f36fb21b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -34,6 +34,7 @@ import java.util.List; 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.mixin.injection.points.AfterInvoke; import org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess; import org.spongepowered.asm.mixin.injection.points.BeforeInvoke; import org.spongepowered.asm.mixin.injection.points.BeforeNew; @@ -91,6 +92,22 @@ public abstract class InjectionPoint { public String toString() { return "InjectionPoint(" + this.getClass().getSimpleName() + ")"; } + + /** + * Get the insn immediately following the specified insn, or return the same + * insn if the insn is the last insn in the list + * + * @param insns Insn list to fetch from + * @param insn Insn node + * @return Next insn or the same insn if last in the list + */ + protected static AbstractInsnNode nextNode(InsnList insns, AbstractInsnNode insn) { + int index = insns.indexOf(insn) + 1; + if (index > 0 && index < insns.size()) { + return insns.get(index); + } + return insn; + } /** * Composite injection point @@ -360,6 +377,8 @@ public abstract class InjectionPoint { point = new JumpInsnPoint(data); } else if (MethodHead.CODE.equals(at)) { point = new MethodHead(data); + } else if (AfterInvoke.CODE.equals(at)) { + point = new AfterInvoke(data); } else if (at.matches("^([A-Za-z_][A-Za-z0-9_]*\\.)+[A-Za-z_][A-Za-z0-9_]*$")) { try { @SuppressWarnings("unchecked") Class cls = (Class) Class.forName(at); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/AfterInvoke.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/AfterInvoke.java new file mode 100644 index 00000000..e3bd1b98 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/AfterInvoke.java @@ -0,0 +1,93 @@ +/* + * This file is part of Sponge, 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.injection.points; + +import java.util.Collection; + +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.InsnList; +import org.spongepowered.asm.lib.tree.MethodInsnNode; +import org.spongepowered.asm.lib.tree.VarInsnNode; +import org.spongepowered.asm.mixin.injection.InjectionPoint; +import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; + + +/** + *

This injection point searches for INVOKEVIRTUAL, INVOKESTATIC and + * INVOKESPECIAL opcodes matching its arguments and returns a list of insns + * after the matching instructions, with special handling for methods + * invocations which return a value and immediately assign it to a local + * variable. It accepts the following parameters from + * {@link org.spongepowered.asm.mixin.injection.At At}:

+ * + *
+ *
target
+ *
A + * {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo MemberInfo} + * which identifies the target method
+ *
ordinal
+ *
The ordinal position of the method invocation to match. For example if + * the method is invoked 3 times and you want to match the 3rd then you can + * specify an ordinal of 2 (ordinals are zero-indexed). The + * default value is -1 which supresses ordinal matching
+ *
+ * + *

Example:

+ *
+ *   @At(value = "INVOKE_ASSIGN", target="func_1234_a(III)J")
+ *
+ * + *

Note that unlike other standard injection points, this class matches the + * insn after the invocation, and after any local variable assignment. Use the + * {@link org.spongepowered.asm.mixin.injection.At#shift shift} specifier to + * adjust the matched opcode as necessary.

+ */ +public class AfterInvoke extends BeforeInvoke { + + @SuppressWarnings("hiding") + public static final String CODE = "INVOKE_ASSIGN"; + + public AfterInvoke(InjectionPointData data) { + super(data); + } + + @Override + protected boolean addInsn(InsnList insns, Collection nodes, AbstractInsnNode insn) { + MethodInsnNode methodNode = (MethodInsnNode)insn; + if (Type.getReturnType(methodNode.desc) == Type.VOID_TYPE) { + return false; + } + + insn = InjectionPoint.nextNode(insns, insn); + if (insn instanceof VarInsnNode && insn.getOpcode() >= Opcodes.ISTORE) { + insn = InjectionPoint.nextNode(insns, insn); + } + + nodes.add(insn); + return true; + } +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java index 4185df91..a07f68a4 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java @@ -137,8 +137,7 @@ public class BeforeInvoke extends InjectionPoint { this.logger.info("{} > > > found a matching insn at ordinal {}", this.className, ordinal); } - nodes.add(insn); - found = true; + found |= this.addInsn(insns, nodes, insn); if (this.ordinal == ordinal) { break; @@ -155,6 +154,11 @@ public class BeforeInvoke extends InjectionPoint { return found; } + protected boolean addInsn(InsnList insns, Collection nodes, AbstractInsnNode insn) { + nodes.add(insn); + return true; + } + protected boolean matchesInsn(AbstractInsnNode insn) { return insn instanceof MethodInsnNode; }