Fix descriptor filtering for NEW injection point, closes #515

This commit is contained in:
Mumfrey committed 2024-05-12 23:39:21 +01:00
1 parent f0d2cd21df
commit 2e5a4b0512
7 files changed
+62 -25

No files matched your search

@@ -32,7 +32,6 @@ import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.logging.Level;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
@@ -27,7 +27,6 @@ package org.spongepowered.tools.agent;
import java.util.HashMap;
import java.util.Map;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.logging.Level;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
@@ -136,6 +136,8 @@ public class RedirectInjector extends InvokeInjector {
public static final String KEY = "ctor";
String desc = null;
boolean wildcard = false;
int injected = 0;
@@ -281,8 +283,10 @@ public class RedirectInjector extends InvokeInjector {
for (InjectionPoint ip : nominators) {
if (ip instanceof BeforeNew) {
ctorData = this.getCtorRedirect((BeforeNew)ip);
ctorData.wildcard = !((BeforeNew)ip).hasDescriptor();
BeforeNew beforeNew = (BeforeNew)ip;
ctorData = this.getCtorRedirect(beforeNew);
ctorData.wildcard = !beforeNew.hasDescriptor();
ctorData.desc = beforeNew.getDescriptor();
} else if (ip instanceof BeforeFieldAccess) {
BeforeFieldAccess bfa = (BeforeFieldAccess)ip;
fuzz = bfa.getFuzzFactor();
@@ -614,7 +618,7 @@ public class RedirectInjector extends InvokeInjector {
final TypeInsnNode newNode = (TypeInsnNode)node.getCurrentTarget();
final AbstractInsnNode dupNode = target.get(target.indexOf(newNode) + 1);
final MethodInsnNode initNode = target.findInitNodeFor(newNode);
final MethodInsnNode initNode = target.findInitNodeFor(newNode, meta.desc);
if (initNode == null) {
meta.throwOrCollect(new InvalidInjectionException(this.info, String.format("%s ctor invocation was not found in %s",
@@ -127,6 +127,13 @@ public class BeforeNew extends InjectionPoint {
public boolean hasDescriptor() {
return this.desc != null;
}
/**
* Gets the descriptor from the injection point, can return null
*/
public String getDescriptor() {
return this.desc;
}
@SuppressWarnings("unchecked")
@Override
@@ -152,7 +159,7 @@ public class BeforeNew extends InjectionPoint {
if (this.desc != null) {
for (TypeInsnNode newNode : newNodes) {
if (this.findCtor(insns, newNode)) {
if (BeforeNew.findInitNodeFor(insns, newNode, this.desc) != null) {
nodes.add(newNode);
found = true;
}
@@ -162,18 +169,21 @@ public class BeforeNew extends InjectionPoint {
return found;
}
protected boolean findCtor(InsnList insns, TypeInsnNode newNode) {
public static MethodInsnNode findInitNodeFor(InsnList insns, TypeInsnNode newNode, String desc) {
int indexOf = insns.indexOf(newNode);
int depth = 0;
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;
if (Constants.CTOR.equals(methodNode.name) && --depth == 0) {
return methodNode.owner.equals(newNode.desc) && (desc == null || methodNode.desc.equals(desc)) ? methodNode : null;
}
} else if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
depth++;
}
}
return false;
return null;
}
private boolean matchesOwner(TypeInsnNode insn) {
@@ -39,6 +39,7 @@ import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.points.BeforeNew;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.util.Bytecode;
@@ -636,20 +637,11 @@ public class Target implements Comparable<Target>, Iterable<AbstractInsnNode> {
* <tt>NEW</tt> insn
*
* @param newNode NEW insn
* @param desc Descriptor to match
* @return INVOKESPECIAL opcode of ctor, or null if not found
*/
public MethodInsnNode findInitNodeFor(TypeInsnNode newNode) {
int start = this.indexOf(newNode);
for (Iterator<AbstractInsnNode> iter = this.insns.iterator(start); 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)) {
return methodNode;
}
}
}
return null;
public MethodInsnNode findInitNodeFor(TypeInsnNode newNode, String desc) {
return BeforeNew.findInitNodeFor(this.insns, newNode, desc);
}
/**
@@ -76,21 +76,46 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
*/
static class InjectorOptions {
/**
* Specifies the default value for <tt>require</tt> to be used when no
* explicit value is defined on the injector. Setting this value to 1
* essentially makes all injectors in the config automatically required.
* Individual injectors can still be marked optional by explicitly
* setting their <tt>require</tt> value to 0.
*/
@SerializedName("defaultRequire")
int defaultRequireValue = 0;
/**
* Specifies the name for injector groups which have no explicit group
* name defined. It is recommended to set this value when grouping
* injectors to support global injector groupings in the future.
*/
@SerializedName("defaultGroup")
String defaultGroup = "default";
/**
* The namespace for custom injection points and dynamic selectors
*/
@SerializedName("namespace")
String namespace;
/**
* List of fully-qualified custom injection point classes to register
*/
@SerializedName("injectionPoints")
List<String> injectionPoints;
/**
* List of fully-qualified dynamic selector classes to register
*/
@SerializedName("dynamicSelectors")
List<String> dynamicSelectors;
/**
* Allows the max Shift.By value to adjusted from the environment
* default, max value is 5
*/
@SerializedName("maxShiftBy")
int maxShiftBy = InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY;
@@ -113,9 +138,18 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
*/
static class OverwriteOptions {
/**
* Flag which specifies whether an overwrite with lower visibility than
* its target is allowed to be applied, the visibility will be upgraded
* if the target method is nonprivate but the merged method is private.
*/
@SerializedName("conformVisibility")
boolean conformAccessModifiers;
/**
* Changes the default always-overwrite behaviour of mixins to
* explicitly require {@link Overwrite} annotations on overwrite methods
*/
@SerializedName("requireAnnotations")
boolean requireOverwriteAnnotations;
@@ -1052,7 +1086,7 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
}
/**
* Get whether visibility levelfor overwritten methods should be conformed
* Get whether visibility level for overwritten methods should be conformed
* to the target class
*
* @return true if conform is enabled
@@ -53,8 +53,7 @@ public class MixinTransformationService extends MixinTransformationServiceAbstra
@Override
public List<Resource> completeScan(final IModuleLayerManager layerManager) {
try {
if (this.detectVirtualJar(layerManager))
{
if (this.detectVirtualJar(layerManager)) {
try {
return ImmutableList.<Resource>of(this.createVirtualJar(layerManager));
} catch (URISyntaxException e) {