Support explicit descriptors for ctor redirect, closes #157

This commit is contained in:
Mumfrey committed 2017-01-02 00:16:00 +00:00
1 parent ab6ab96539
commit 53df42cae5
9 files changed
+377 -103

No files matched your search

@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.6.3";
public static final String VERSION = "0.6.4";
// Consts
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
@@ -68,7 +68,7 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
/**
* Injector decorations on this node
*/
private Map<Object, Object> decorations;
private Map<String, Object> decorations;
/**
* Create a new node wrapper for the specified target node
@@ -144,9 +144,9 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
* @param key meta key
* @param value meta value
*/
public <K, V> InjectionNode decorate(K key, V value) {
public <V> InjectionNode decorate(String key, V value) {
if (this.decorations == null) {
this.decorations = new HashMap<Object, Object>();
this.decorations = new HashMap<String, Object>();
}
this.decorations.put(key, value);
return this;
@@ -169,7 +169,7 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
* @return decoration value or null if absent
*/
@SuppressWarnings("unchecked")
public <K, V> V getDecoration(K key) {
public <V> V getDecoration(String key) {
return (V) (this.decorations == null ? null : this.decorations.get(key));
}
@@ -186,7 +186,7 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
*/
@Override
public String toString() {
return String.format("InjectionNode[%s]", ASMHelper.getNodeDescriptionForDebug(this.currentTarget));
return String.format("InjectionNode[%s]", ASMHelper.getNodeDescriptionForDebug(this.currentTarget).replaceAll("\\s+", " "));
}
}
@@ -26,8 +26,10 @@ package org.spongepowered.asm.mixin.injection.code;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
@@ -57,6 +59,20 @@ import com.google.common.base.Joiner;
*/
public abstract class Injector {
/**
* A nominated target node
*/
class TargetNode {
final AbstractInsnNode node;
final Set<InjectionPoint> nominators = new HashSet<InjectionPoint>();
TargetNode(AbstractInsnNode node) {
this.node = node;
}
}
/**
* Log more things
*/
@@ -133,13 +149,13 @@ public abstract class Injector {
this.sanityCheck(target, injectionPoints);
List<InjectionNode> myNodes = new ArrayList<InjectionNode>();
for (AbstractInsnNode node : this.findTargetNodes(target.method, injectionPoints)) {
this.addTargetNode(target, myNodes, node);
for (TargetNode node : this.findTargetNodes(target.method, injectionPoints)) {
this.addTargetNode(target, myNodes, node.node, node.nominators);
}
return myNodes;
}
protected void addTargetNode(Target target, List<InjectionNode> myNodes, AbstractInsnNode node) {
protected void addTargetNode(Target target, List<InjectionNode> myNodes, AbstractInsnNode node, Set<InjectionPoint> nominators) {
myNodes.add(target.injectionNodes.add(node));
}
@@ -153,6 +169,10 @@ public abstract class Injector {
}
this.inject(target, node);
}
for (InjectionNode node : nodes) {
this.postInject(target, node);
}
}
/**
@@ -164,8 +184,8 @@ public abstract class Injector {
* annotations on the callback method
* @return Target insn nodes in the target method
*/
protected Set<AbstractInsnNode> findTargetNodes(MethodNode into, List<InjectionPoint> injectionPoints) {
Set<AbstractInsnNode> targetNodes = new HashSet<AbstractInsnNode>();
private Collection<TargetNode> findTargetNodes(MethodNode into, List<InjectionPoint> injectionPoints) {
Map<AbstractInsnNode, TargetNode> targetNodes = new HashMap<AbstractInsnNode, TargetNode>();
// Defensive objects, so that injectionPoint instances can't modify our working copies
ReadOnlyInsnList insns = new ReadOnlyInsnList(into.instructions);
@@ -174,12 +194,19 @@ public abstract class Injector {
for (InjectionPoint injectionPoint : injectionPoints) {
nodes.clear();
if (this.findTargetNodes(into, injectionPoint, insns, nodes)) {
targetNodes.addAll(nodes);
for (AbstractInsnNode node : nodes) {
TargetNode target = targetNodes.get(node);
if (target == null) {
target = new TargetNode(node);
targetNodes.put(node, target);
}
target.nominators.add(injectionPoint);
}
}
}
insns.dispose();
return targetNodes;
return targetNodes.values();
}
protected boolean findTargetNodes(MethodNode into, InjectionPoint injectionPoint, InsnList insns, Collection<AbstractInsnNode> nodes) {
@@ -194,6 +221,10 @@ public abstract class Injector {
protected abstract void inject(Target target, InjectionNode node);
protected void postInject(Target target, InjectionNode node) {
// stub
}
/**
* Invoke the handler method
*
@@ -24,16 +24,29 @@
*/
package org.spongepowered.asm.mixin.injection.invoke;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.*;
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.JumpInsnNode;
import org.spongepowered.asm.lib.tree.LabelNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.lib.tree.TypeInsnNode;
import org.spongepowered.asm.lib.tree.VarInsnNode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.points.BeforeNew;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
@@ -80,6 +93,9 @@ import com.google.common.primitives.Ints;
*/
public class RedirectInjector extends InvokeInjector {
private static final String KEY_NOMINATORS = "nominators";
private static final String KEY_WILD = "wildcard";
/**
* Meta decoration object for redirector target nodes
*/
@@ -108,8 +124,21 @@ public class RedirectInjector extends InvokeInjector {
}
/**
* Meta decoration for wildcard ctor redirects
*/
class ConstructorRedirectData {
public static final String KEY = "ctor";
public int injected = 0;
}
protected Meta meta;
private Map<BeforeNew, ConstructorRedirectData> ctorRedirectors = new HashMap<BeforeNew, ConstructorRedirectData>();
/**
* @param info Injection info
*/
@@ -135,8 +164,9 @@ public class RedirectInjector extends InvokeInjector {
}
@Override
protected void addTargetNode(Target target, List<InjectionNode> myNodes, AbstractInsnNode insn) {
protected void addTargetNode(Target target, List<InjectionNode> myNodes, AbstractInsnNode insn, Set<InjectionPoint> nominators) {
InjectionNode node = target.injectionNodes.get(insn);
ConstructorRedirectData ctorData = null;
if (node != null ) {
Meta other = node.getDecoration(Meta.KEY);
@@ -153,7 +183,29 @@ public class RedirectInjector extends InvokeInjector {
}
}
myNodes.add(target.injectionNodes.add(insn).decorate(Meta.KEY, this.meta));
for (InjectionPoint ip : nominators) {
if (ip instanceof BeforeNew && !((BeforeNew)ip).hasDescriptor()) {
ctorData = this.getCtorRedirect((BeforeNew)ip);
}
}
InjectionNode targetNode = target.injectionNodes.add(insn);
targetNode.decorate(Meta.KEY, this.meta);
targetNode.decorate(RedirectInjector.KEY_NOMINATORS, nominators);
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
targetNode.decorate(RedirectInjector.KEY_WILD, Boolean.valueOf(ctorData != null));
targetNode.decorate(ConstructorRedirectData.KEY, ctorData);
}
myNodes.add(targetNode);
}
private ConstructorRedirectData getCtorRedirect(BeforeNew ip) {
ConstructorRedirectData ctorRedirect = this.ctorRedirectors.get(ip);
if (ctorRedirect == null) {
ctorRedirect = new ConstructorRedirectData();
this.ctorRedirectors.put(ip, ctorRedirect);
}
return ctorRedirect;
}
@Override
@@ -200,6 +252,18 @@ public class RedirectInjector extends InvokeInjector {
return true;
}
@Override
protected void postInject(Target target, InjectionNode node) {
super.postInject(target, node);
if (node.getOriginalTarget() instanceof TypeInsnNode && node.getOriginalTarget().getOpcode() == Opcodes.NEW) {
ConstructorRedirectData meta = node.<ConstructorRedirectData>getDecoration(ConstructorRedirectData.KEY);
boolean wildcard = node.<Boolean>getDecoration(RedirectInjector.KEY_WILD).booleanValue();
if (wildcard && meta.injected == 0) {
throw new InvalidInjectionException(this.info, this.annotationType + " ctor invocation was not found in " + target);
}
}
}
/**
* Redirect a method invocation
*/
@@ -341,18 +405,32 @@ public class RedirectInjector extends InvokeInjector {
}
protected void injectAtConstructor(Target target, InjectionNode node) {
ConstructorRedirectData meta = node.<ConstructorRedirectData>getDecoration(ConstructorRedirectData.KEY);
boolean wildcard = node.<Boolean>getDecoration(RedirectInjector.KEY_WILD).booleanValue();
final TypeInsnNode newNode = (TypeInsnNode)node.getCurrentTarget();
final AbstractInsnNode dupNode = target.get(target.indexOf(newNode) + 1);
final MethodInsnNode initNode = this.findInitNode(target, newNode);
if (initNode == null) {
throw new InvalidInjectionException(this.info, this.annotationType + " ctor invocation was not found in " + target);
if (!wildcard) {
throw new InvalidInjectionException(this.info, this.annotationType + " ctor invocation was not found in " + target);
}
return;
}
// True if the result of the object construction is being assigned
boolean isAssigned = dupNode.getOpcode() == Opcodes.DUP;
String desc = initNode.desc.replace(")V", ")L" + newNode.desc + ";");
boolean withArgs = this.checkDescriptor(desc, target, "constructor");
boolean withArgs = false;
try {
withArgs = this.checkDescriptor(desc, target, "constructor");
} catch (InvalidInjectionException ex) {
if (!wildcard) {
throw ex;
}
return;
}
if (isAssigned) {
target.removeNode(dupNode);
@@ -389,6 +467,7 @@ public class RedirectInjector extends InvokeInjector {
}
target.replaceNode(initNode, insns);
meta.injected++;
}
protected MethodInsnNode findInitNode(Target target, TypeInsnNode newNode) {
@@ -24,15 +24,20 @@
*/
package org.spongepowered.asm.mixin.injection.points;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import org.spongepowered.asm.lib.Opcodes;
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.TypeInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.util.Constants;
import com.google.common.base.Strings;
@@ -43,7 +48,7 @@ import com.google.common.base.Strings;
* {@link org.spongepowered.asm.mixin.injection.At At}:</p>
*
* <dl>
* <dt><em>named argument</em> class</dt>
* <dt><em>named argument</em> class (or specify using <tt>target</tt></dt>
* <dd>The value of the NEW node to look for, the fully-qualified class name
* </dd>
* <dt>ordinal</dt>
@@ -51,15 +56,28 @@ import com.google.common.base.Strings;
* opcode appears 3 times in the method and you want to match the 3rd then you
* can specify an <em>ordinal</em> of <b>2</b> (ordinals are zero-indexed).
* The default value is <b>-1</b> which supresses ordinal matching</dd>
* <dt>target</dt>
* <dd>Target class can also be specified in <tt>target</tt> which also
* supports specifying the exact signature of the constructor to target. In
* this case the <em>target type</em> is specified as the return type of the
* constructor (in place of the usual <tt>V</tt> (void)) and no owner or name
* should be specified (they are ignored).</dd>
* </dl>
*
* <p>Examples:</p>
* <blockquote><pre>
* // Find all NEW opcodes for <tt>String</tt>
* &#064;At(value = "NEW", args = "class=java/lang/String")</pre>
* </blockquote>
* <blockquote><pre>
* // Find all NEW opcodes for <tt>String</tt>
* &#064;At(value = "NEW", target = "java/lang/String"</pre>
* </blockquote>
* <blockquote><pre>
* // Find all NEW opcodes for <tt>String</tt> which are constructed using the
* // ctor which takes an array of <tt>char</tt>
* &#064;At(value = "NEW", target = "([C)Ljava/lang/String;"</pre>
* </blockquote>
*
* <p>Note that like all standard injection points, this class matches the insn
* itself, putting the injection point immediately <em>before</em> the access in
@@ -73,7 +91,12 @@ public class BeforeNew extends InjectionPoint {
/**
* Class name we're looking for
*/
private final String className;
private final String target;
/**
* Ctor descriptor we're looking for
*/
private final String desc;
/**
* Ordinal value
@@ -82,32 +105,65 @@ public class BeforeNew extends InjectionPoint {
public BeforeNew(InjectionPointData data) {
this.ordinal = data.getOrdinal();
this.className = Strings.emptyToNull(data.get("class", data.get("target", "")).replace('.', '/'));
String target = Strings.emptyToNull(data.get("class", data.get("target", "")).replace('.', '/'));
MemberInfo member = MemberInfo.parseAndValidate(target, data.getMixin());
this.target = member.toCtorType();
this.desc = member.toCtorDesc();
}
public boolean hasDescriptor() {
return this.desc != null;
}
@SuppressWarnings("unchecked")
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes) {
boolean found = false;
int ordinal = 0;
Collection<TypeInsnNode> newNodes = new ArrayList<TypeInsnNode>();
Collection<AbstractInsnNode> candidates = (Collection<AbstractInsnNode>) (this.desc != null ? newNodes : nodes);
ListIterator<AbstractInsnNode> iter = insns.iterator();
while (iter.hasNext()) {
AbstractInsnNode insn = iter.next();
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW && this.matchesOwner((TypeInsnNode) insn)) {
if (this.ordinal == -1 || this.ordinal == ordinal) {
nodes.add(insn);
found = true;
candidates.add(insn);
found = this.desc == null;
}
ordinal++;
}
}
if (this.desc != null) {
for (TypeInsnNode newNode : newNodes) {
if (this.findCtor(insns, newNode)) {
nodes.add(newNode);
found = true;
}
}
}
return found;
}
protected boolean findCtor(InsnList insns, TypeInsnNode newNode) {
int indexOf = insns.indexOf(newNode);
for (Iterator<AbstractInsnNode> iter = insns.iterator(indexOf); iter.hasNext();) {
AbstractInsnNode insn = iter.next();
if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) {
MethodInsnNode methodNode = (MethodInsnNode)insn;
if (Constants.CTOR.equals(methodNode.name) && methodNode.owner.equals(newNode.desc) && methodNode.desc.equals(this.desc)) {
return true;
}
}
}
return false;
}
private boolean matchesOwner(TypeInsnNode insn) {
return this.className == null || this.className.equals(insn.desc);
return this.target == null || this.target.equals(insn.desc);
}
}
@@ -36,6 +36,8 @@ import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.asm.util.SignaturePrinter;
import com.google.common.base.Strings;
/**
* <p>Information bundle about a member (method or field) parsed from a String
* token in another annotation, this is used where target members need to be
@@ -102,6 +104,11 @@ public class MemberInfo {
* Force this member to report as a field
*/
private final boolean forceField;
/**
* The actual String value passed into the {@link #parse} method
*/
private final String unparsed;
/**
* ctor
@@ -149,6 +156,19 @@ public class MemberInfo {
* @param matchAll True to match all matching members, not just the first
*/
public MemberInfo(String name, String owner, String desc, boolean matchAll) {
this(name, owner, desc, matchAll, null);
}
/**
* ctor
*
* @param name Member name, must not be null
* @param owner Member owner, can be null otherwise must be in internal form
* without L;
* @param desc Member descriptor, can be null
* @param matchAll True to match all matching members, not just the first
*/
public MemberInfo(String name, String owner, String desc, boolean matchAll, String unparsed) {
if (owner != null && owner.contains(".")) {
throw new IllegalArgumentException("Attempt to instance a MemberInfo with an invalid owner format");
}
@@ -158,6 +178,7 @@ public class MemberInfo {
this.desc = desc;
this.matchAll = matchAll;
this.forceField = false;
this.unparsed = unparsed;
}
/**
@@ -169,6 +190,7 @@ public class MemberInfo {
public MemberInfo(AbstractInsnNode insn) {
this.matchAll = false;
this.forceField = false;
this.unparsed = null;
if (insn instanceof MethodInsnNode) {
MethodInsnNode methodNode = (MethodInsnNode) insn;
@@ -196,6 +218,7 @@ public class MemberInfo {
this.desc = mapping.getDesc();
this.matchAll = false;
this.forceField = mapping.getType() == IMapping.Type.FIELD;
this.unparsed = null;
}
/**
@@ -209,6 +232,7 @@ public class MemberInfo {
this.desc = method.getDesc();
this.matchAll = remapped.matchAll;
this.forceField = false;
this.unparsed = null;
}
/**
@@ -223,6 +247,7 @@ public class MemberInfo {
this.desc = original.desc;
this.matchAll = original.matchAll;
this.forceField = original.forceField;
this.unparsed = null;
}
/* (non-Javadoc)
@@ -265,6 +290,47 @@ public class MemberInfo {
return new SignaturePrinter(this).setFullyQualified(true).toDescriptor();
}
public String toCtorType() {
if (this.unparsed == null) {
return null;
}
String returnType = this.getReturnType();
if (returnType != null) {
return returnType;
}
if (this.owner != null) {
return this.owner;
}
if (this.name != null && this.desc == null) {
return this.name;
}
return this.desc != null ? this.desc : this.unparsed;
}
public String toCtorDesc() {
if (this.desc != null && this.desc.startsWith("(") && this.desc.indexOf(')') > -1) {
return this.desc.substring(0, this.desc.indexOf(')') + 1) + "V";
}
return null;
}
public String getReturnType() {
if (this.desc == null || this.desc.indexOf(')') == -1 || this.desc.indexOf('(') != 0 ) {
return null;
}
String returnType = this.desc.substring(this.desc.indexOf(')') + 1);
if (returnType.startsWith("L") && returnType.endsWith(";")) {
return returnType.substring(1, returnType.length() - 1);
}
return returnType;
}
public IMapping<?> asMapping() {
return this.isField() ? this.asFieldMapping() : this.asMethodMapping();
}
@@ -503,16 +569,15 @@ public class MemberInfo {
/**
* Parse a MemberInfo from a string
*
* @param name String to parse MemberInfo from
* @param input String to parse MemberInfo from
* @param refMapper Reference mapper to use
* @param mixinClass Mixin class to use for remapping
* @return parsed MemberInfo
*/
private static MemberInfo parse(String name, ReferenceMapper refMapper, String mixinClass) {
private static MemberInfo parse(String input, ReferenceMapper refMapper, String mixinClass) {
String desc = null;
String owner = null;
name = name.replaceAll("\\s", "");
String name = Strings.nullToEmpty(input).replaceAll("\\s", "");
if (refMapper != null) {
name = refMapper.remap(mixinClass, name);
@@ -538,6 +603,11 @@ public class MemberInfo {
name = name.substring(0, colonPos);
}
if ((name.indexOf('/') > -1 || name.indexOf('.') > -1) && owner == null) {
owner = name;
name = "";
}
boolean matchAll = name.endsWith("*");
if (matchAll) {
name = name.substring(0, name.length() - 1);
@@ -547,10 +617,15 @@ public class MemberInfo {
name = null;
}
return new MemberInfo(name, owner, desc, matchAll);
return new MemberInfo(name, owner, desc, matchAll, input);
}
public static MemberInfo fromMapping(IMapping<?> mapping) {
return new MemberInfo(mapping);
}
public static void main(String[] args) {
MemberInfo m = MemberInfo.parseAndValidate(null); //"(Lfoo;III)Lfoo/bar/qcv;");
System.err.printf(">> [%s] [%s]\n", m.toCtorType(), m.toCtorDesc());
}
}