mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-21 03:22:17 +00:00
Add require and expect metrics to injectors, allows injector validation
This commit is contained in:
1 parent
34402ab33c
commit
99c07dcfc9
7 files changed
+180
-11
No files matched your search
@@ -36,12 +36,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.launchwrapper.IClassNameTransformer;
|
||||
import net.minecraft.launchwrapper.IClassTransformer;
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -53,6 +47,7 @@ import org.apache.logging.log4j.core.helpers.Booleans;
|
||||
import org.spongepowered.asm.launch.MixinBootstrap;
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.mixin.extensibility.IEnvironmentTokenProvider;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTransformer;
|
||||
import org.spongepowered.asm.util.ITokenProvider;
|
||||
import org.spongepowered.asm.util.JavaVersion;
|
||||
@@ -61,6 +56,12 @@ import org.spongepowered.asm.util.PrettyPrinter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import net.minecraft.launchwrapper.IClassNameTransformer;
|
||||
import net.minecraft.launchwrapper.IClassTransformer;
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
|
||||
/**
|
||||
* The mixin environment manages global state information for the mixin
|
||||
@@ -283,6 +284,12 @@ public class MixinEnvironment implements ITokenProvider {
|
||||
*/
|
||||
DEBUG_VERBOSE(Option.DEBUG_ALL, "verbose"),
|
||||
|
||||
/**
|
||||
* Elevates failed injections to an error condition, see
|
||||
* {@link Inject#expect} for details
|
||||
*/
|
||||
DEBUG_INJECTORS(Option.DEBUG_ALL, "countInjections"),
|
||||
|
||||
/**
|
||||
* Dumps the bytecode for the target class to disk when mixin
|
||||
* application fails
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
@@ -128,4 +129,38 @@ public @interface Inject {
|
||||
* obfuscation mappings for this annotation
|
||||
*/
|
||||
public boolean remap() default true;
|
||||
|
||||
/**
|
||||
* In general, injectors are intended to "fail soft" in that a failure to
|
||||
* locate the injection point in the target method is not considered an
|
||||
* error condition. Another transformer may have changed the method
|
||||
* structure or any number of reasons may cause an injection to fail. This
|
||||
* also makes it possible to define several injections to achieve the same
|
||||
* task given <em>expected</em> mutation of the target class and the
|
||||
* injectors which fail are simply ignored.
|
||||
*
|
||||
* <p>However, this behaviour is not always desirable. For example, if your
|
||||
* application depends on a particular injection succeeding you may wish to
|
||||
* detect the injection failure as an error condition. This argument is thus
|
||||
* provided to allow you to stipulate a <b>minimum</b> number of successful
|
||||
* injections for this callback handler. If the number of injections
|
||||
* specified is not achieved then an {@link InjectionError} is thrown at
|
||||
* application time. Use this option with care.</p>
|
||||
*
|
||||
* @return Minimum required number of injected callbacks, default 0
|
||||
*/
|
||||
public int require() default 0;
|
||||
|
||||
/**
|
||||
* Like {@link #require()} but only enabled if the
|
||||
* {@link Option#DEBUG_INJECTORS mixin.debug.countInjections} option is set
|
||||
* to <tt>true</tt> and defaults to 1. Use this option during debugging to
|
||||
* perform simple checking of your injectors. Causes the injector to throw
|
||||
* a {@link InvalidInjectionException} if the expected number of injections
|
||||
* is not realised.
|
||||
*
|
||||
* @return Minimum number of <em>expected</em> callbacks, default 1
|
||||
*/
|
||||
public int expect() default 1;
|
||||
|
||||
}
|
||||
@@ -29,6 +29,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
|
||||
/**
|
||||
* Specifies that this mixin method should inject an argument modifier to itself
|
||||
* in the target method(s) identified by {@link #method}.
|
||||
@@ -86,4 +88,38 @@ public @interface ModifyArg {
|
||||
* obfuscation mappings for this annotation
|
||||
*/
|
||||
public boolean remap() default true;
|
||||
|
||||
/**
|
||||
* In general, injectors are intended to "fail soft" in that a failure to
|
||||
* locate the injection point in the target method is not considered an
|
||||
* error condition. Another transformer may have changed the method
|
||||
* structure or any number of reasons may cause an injection to fail. This
|
||||
* also makes it possible to define several injections to achieve the same
|
||||
* task given <em>expected</em> mutation of the target class and the
|
||||
* injectors which fail are simply ignored.
|
||||
*
|
||||
* <p>However, this behaviour is not always desirable. For example, if your
|
||||
* application depends on a particular injection succeeding you may wish to
|
||||
* detect the injection failure as an error condition. This argument is thus
|
||||
* provided to allow you to stipulate a <b>minimum</b> number of successful
|
||||
* injections for this callback handler. If the number of injections
|
||||
* specified is not achieved then an {@link InjectionError} is thrown at
|
||||
* application time. Use this option with care.</p>
|
||||
*
|
||||
* @return Minimum required number of injected callbacks, default 0
|
||||
*/
|
||||
public int require() default 0;
|
||||
|
||||
/**
|
||||
* Like {@link #require()} but only enabled if the
|
||||
* {@link Option#DEBUG_INJECTORS mixin.debug.countInjections} option is set
|
||||
* to <tt>true</tt> and defaults to 1. Use this option during debugging to
|
||||
* perform simple checking of your injectors. Causes the injector to throw
|
||||
* a {@link InvalidInjectionException} if the expected number of injections
|
||||
* is not realised.
|
||||
*
|
||||
* @return Minimum number of <em>expected</em> callbacks, default 1
|
||||
*/
|
||||
public int expect() default 1;
|
||||
|
||||
}
|
||||
@@ -29,6 +29,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
|
||||
/**
|
||||
* <p>Specifies that this mixin method should redirect the specified
|
||||
* {@link #method} call to the method decorated with this annotation.</p>
|
||||
@@ -108,4 +110,38 @@ public @interface Redirect {
|
||||
* obfuscation mappings for this annotation
|
||||
*/
|
||||
public boolean remap() default true;
|
||||
|
||||
/**
|
||||
* In general, injectors are intended to "fail soft" in that a failure to
|
||||
* locate the injection point in the target method is not considered an
|
||||
* error condition. Another transformer may have changed the method
|
||||
* structure or any number of reasons may cause an injection to fail. This
|
||||
* also makes it possible to define several injections to achieve the same
|
||||
* task given <em>expected</em> mutation of the target class and the
|
||||
* injectors which fail are simply ignored.
|
||||
*
|
||||
* <p>However, this behaviour is not always desirable. For example, if your
|
||||
* application depends on a particular injection succeeding you may wish to
|
||||
* detect the injection failure as an error condition. This argument is thus
|
||||
* provided to allow you to stipulate a <b>minimum</b> number of successful
|
||||
* injections for this callback handler. If the number of injections
|
||||
* specified is not achieved then an {@link InjectionError} is thrown at
|
||||
* application time. Use this option with care.</p>
|
||||
*
|
||||
* @return Minimum required number of injected callbacks, default 0
|
||||
*/
|
||||
public int require() default 0;
|
||||
|
||||
/**
|
||||
* Like {@link #require()} but only enabled if the
|
||||
* {@link Option#DEBUG_INJECTORS mixin.debug.countInjections} option is set
|
||||
* to <tt>true</tt> and defaults to 1. Use this option during debugging to
|
||||
* perform simple checking of your injectors. Causes the injector to throw
|
||||
* a {@link InvalidInjectionException} if the expected number of injections
|
||||
* is not realised.
|
||||
*
|
||||
* @return Minimum number of <em>expected</em> callbacks, default 1
|
||||
*/
|
||||
public int expect() default 1;
|
||||
|
||||
}
|
||||
@@ -580,7 +580,7 @@ public class CallbackInjector extends Injector {
|
||||
}
|
||||
|
||||
// Call the callback!
|
||||
this.invokeMethod(callback, callbackMethod);
|
||||
this.invokeHandler(callback, callbackMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -148,13 +148,21 @@ public abstract class Injector {
|
||||
* @param insns Instruction list to inject into
|
||||
*/
|
||||
protected void invokeHandler(InsnList insns) {
|
||||
this.invokeMethod(insns, this.methodNode);
|
||||
this.invokeHandler(insns, this.methodNode);
|
||||
}
|
||||
|
||||
protected void invokeMethod(InsnList insns, MethodNode methodNode) {
|
||||
boolean isPrivate = (methodNode.access & Opcodes.ACC_PRIVATE) != 0;
|
||||
/**
|
||||
* Invoke a handler method
|
||||
*
|
||||
* @param insns Instruction list to inject into
|
||||
* @param handler Actual method to invoke (may be different if using a
|
||||
* surrogate)
|
||||
*/
|
||||
protected void invokeHandler(InsnList insns, MethodNode handler) {
|
||||
boolean isPrivate = (handler.access & Opcodes.ACC_PRIVATE) != 0;
|
||||
int invokeOpcode = this.isStatic ? Opcodes.INVOKESTATIC : isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL;
|
||||
insns.add(new MethodInsnNode(invokeOpcode, this.classNode.name, methodNode.name, methodNode.desc, false));
|
||||
insns.add(new MethodInsnNode(invokeOpcode, this.classNode.name, handler.name, handler.desc, false));
|
||||
this.info.addCallbackInvocation(handler);
|
||||
}
|
||||
|
||||
protected static String printArgs(Type[] args) {
|
||||
|
||||
@@ -34,7 +34,10 @@ import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.tree.AnnotationNode;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionError;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionPoint;
|
||||
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||
@@ -96,6 +99,21 @@ public abstract class InjectionInfo {
|
||||
*/
|
||||
private final List<MethodNode> injectedMethods = new ArrayList<MethodNode>(0);
|
||||
|
||||
/**
|
||||
* Number of callbacks we expect to inject into targets
|
||||
*/
|
||||
private int expectedCallbackCount = 1;
|
||||
|
||||
/**
|
||||
* Number of callbacks we require injected
|
||||
*/
|
||||
private int requiredCallbackCount = 0;
|
||||
|
||||
/**
|
||||
* Actual number of injected callbacks
|
||||
*/
|
||||
private int injectedCallbackCount = 0;
|
||||
|
||||
/**
|
||||
* ctor
|
||||
*
|
||||
@@ -159,6 +177,16 @@ public abstract class InjectionInfo {
|
||||
}
|
||||
}
|
||||
|
||||
Integer expect = ASMHelper.<Integer>getAnnotationValue(this.annotation, "expect");
|
||||
if (expect != null) {
|
||||
this.expectedCallbackCount = expect.intValue();
|
||||
}
|
||||
|
||||
Integer require = ASMHelper.<Integer>getAnnotationValue(this.annotation, "require");
|
||||
if (require != null) {
|
||||
this.requiredCallbackCount = require.intValue();
|
||||
}
|
||||
|
||||
this.injector = this.initInjector(this.annotation);
|
||||
}
|
||||
|
||||
@@ -192,6 +220,16 @@ public abstract class InjectionInfo {
|
||||
for (MethodNode method : this.injectedMethods) {
|
||||
this.classNode.methods.add(method);
|
||||
}
|
||||
|
||||
if ((MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_INJECTORS) && this.injectedCallbackCount < this.expectedCallbackCount)) {
|
||||
throw new InvalidInjectionException(this,
|
||||
String.format("Injection validation failed: Callback method %s%s in %s expected %d invocation(s) but %d succeeded",
|
||||
this.method.name, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount));
|
||||
} else if (this.injectedCallbackCount < this.requiredCallbackCount) {
|
||||
throw new InjectionError(
|
||||
String.format("Critical injection failure: Callback method %s%s in %s failed injection check, (%d/%d) succeeded",
|
||||
this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,6 +292,15 @@ public abstract class InjectionInfo {
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify method, called by injector when adding a callback into a target
|
||||
*
|
||||
* @param handler callback handler being invoked
|
||||
*/
|
||||
public void addCallbackInvocation(MethodNode handler) {
|
||||
this.injectedCallbackCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds methods in the target class which match searchFor
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user