diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 6de40b74..7c8e636b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -39,6 +39,7 @@ import org.spongepowered.asm.mixin.injection.Surrogate; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.throwables.InjectionError; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; @@ -143,7 +144,7 @@ public class CallbackInjector extends Injector { * then set the MAXS value on the target method to its original value * plus the larger of the two values. */ - int ctor, invoke; + Extension ctor, invoke; /** * Marshal var is the local where we marshal the utility references we @@ -196,8 +197,14 @@ public class CallbackInjector extends Injector { this.desc = target.getCallbackDescriptor(this.localTypes, target.arguments); this.descl = target.getCallbackDescriptor(true, this.localTypes, target.arguments, this.frameSize, this.extraArgs); // this.typeCasts = new Type[this.frameSize + this.extraArgs]; + + this.invoke = target.extendStack(); + this.ctor = target.extendStack(); - this.invoke = target.arguments.length + (this.canCaptureLocals ? this.localTypes.length - this.frameSize : 0); + this.invoke.add(target.arguments.length); + if (this.canCaptureLocals) { + this.invoke.add(this.localTypes.length - this.frameSize); + } } /** @@ -241,8 +248,12 @@ public class CallbackInjector extends Injector { } else { this.add(insn); } - this.ctor += (ctorStack ? 1 : 0); - this.invoke += (invokeStack ? 1 : 0); + if (ctorStack) { + this.ctor.add(); + } + if (invokeStack) { + this.invoke.add(); + } } /** @@ -251,7 +262,8 @@ public class CallbackInjector extends Injector { */ void inject() { this.target.insertBefore(this.node, this); - this.target.addToStack(Math.max(this.invoke, this.ctor)); + this.invoke.apply(); + this.ctor.apply(); } boolean checkDescriptor(String desc) { 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 6ea389f0..95a046b9 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 @@ -34,6 +34,7 @@ import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.Bytecode; @@ -110,33 +111,33 @@ public class ModifyArgInjector extends InvokeInjector { Type[] args = Type.getArgumentTypes(methodNode.desc); int argIndex = this.findArgIndex(target, args); InsnList insns = new InsnList(); - int extraLocals = 0; + Extension extraLocals = target.extendLocals(); if (this.singleArgMode) { - extraLocals = this.injectSingleArgHandler(target, args, argIndex, insns); + this.injectSingleArgHandler(target, extraLocals, args, argIndex, insns); } else { - extraLocals = this.injectMultiArgHandler(target, args, argIndex, insns); + this.injectMultiArgHandler(target, extraLocals, args, argIndex, insns); } target.insns.insertBefore(methodNode, insns); - target.addToLocals(extraLocals); - target.addToStack(2 - (extraLocals - 1)); + target.extendStack().set(2 - (extraLocals.get() - 1)).apply(); + extraLocals.apply(); } /** * Inject handler opcodes for a single arg handler */ - private int injectSingleArgHandler(Target target, Type[] args, int argIndex, InsnList insns) { + private void injectSingleArgHandler(Target target, Extension extraLocals, Type[] args, int argIndex, InsnList insns) { int[] argMap = this.storeArgs(target, args, insns, argIndex); this.invokeHandlerWithArgs(args, insns, argMap, argIndex, argIndex + 1); this.pushArgs(args, insns, argMap, argIndex + 1, args.length); - return (argMap[argMap.length - 1] - target.getMaxLocals()) + args[args.length - 1].getSize(); + extraLocals.add((argMap[argMap.length - 1] - target.getMaxLocals()) + args[args.length - 1].getSize()); } /** * Inject handler opcodes for a multi arg handler */ - private int injectMultiArgHandler(Target target, Type[] args, int argIndex, InsnList insns) { + private void injectMultiArgHandler(Target target, Extension extraLocals, Type[] args, int argIndex, InsnList insns) { if (!Arrays.equals(args, this.methodArgs)) { throw new InvalidInjectionException(this.info, "@ModifyArg method " + this + " targets a method with an invalid signature " + Bytecode.getDescriptor(args) + ", expected " + Bytecode.getDescriptor(this.methodArgs)); @@ -146,7 +147,7 @@ public class ModifyArgInjector extends InvokeInjector { this.pushArgs(args, insns, argMap, 0, argIndex); this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length); this.pushArgs(args, insns, argMap, argIndex + 1, args.length); - return (argMap[argMap.length - 1] - target.getMaxLocals()) + args[args.length - 1].getSize(); + extraLocals.add((argMap[argMap.length - 1] - target.getMaxLocals()) + args[args.length - 1].getSize()); } protected int findArgIndex(Target target, Type[] args) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java index bd20fe66..b722d806 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java @@ -34,6 +34,7 @@ import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel; import org.spongepowered.asm.mixin.injection.invoke.arg.ArgsClassGenerator; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.Bytecode; @@ -87,18 +88,19 @@ public class ModifyArgsInjector extends InvokeInjector { boolean withArgs = this.verifyTarget(target); InsnList insns = new InsnList(); - target.addToStack(1); + Extension extraStack = target.extendStack().add(1); this.packArgs(insns, clArgs, targetMethod); if (withArgs) { - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); Bytecode.loadArgs(target.arguments, insns, target.isStatic ? 0 : 1); } this.invokeHandler(insns); this.unpackArgs(insns, clArgs, args); + extraStack.apply(); target.insns.insertBefore(targetMethod, insns); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java index 270b2090..35732fb2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java @@ -40,13 +40,15 @@ import org.spongepowered.asm.mixin.injection.invoke.util.InsnFinder; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.SignaturePrinter; /** - * A bytecode injector which allows a specific + * A bytecode injector which allows a specific constant value to be identified + * and replaced with a callback */ public class ModifyConstantInjector extends RedirectInjector { @@ -103,12 +105,13 @@ public class ModifyConstantInjector extends RedirectInjector { + Bytecode.getOpcodeName(opcode) + " in " + target + " in " + this); } + Extension extraStack = target.extendStack(); final InsnList insns = new InsnList(); insns.add(new InsnNode(Opcodes.ICONST_0)); - AbstractInsnNode invoke = this.invokeConstantHandler(Type.getType("I"), target, insns, insns); + AbstractInsnNode invoke = this.invokeConstantHandler(Type.getType("I"), target, extraStack, insns, insns); insns.add(new JumpInsnNode(opcode + ModifyConstantInjector.OPCODE_OFFSET, jumpNode.label)); + extraStack.add(1).apply(); target.replaceNode(jumpNode, invoke, insns); - target.addToStack(1); } private void injectConstantModifier(Target target, AbstractInsnNode constNode) { @@ -118,24 +121,26 @@ public class ModifyConstantInjector extends RedirectInjector { this.checkNarrowing(target, constNode, constantType); } + Extension extraStack = target.extendStack(); final InsnList before = new InsnList(); final InsnList after = new InsnList(); - AbstractInsnNode invoke = this.invokeConstantHandler(constantType, target, before, after); + AbstractInsnNode invoke = this.invokeConstantHandler(constantType, target, extraStack, before, after); + extraStack.apply(); target.wrapNode(constNode, invoke, before, after); } - private AbstractInsnNode invokeConstantHandler(Type constantType, Target target, InsnList before, InsnList after) { + private AbstractInsnNode invokeConstantHandler(Type constantType, Target target, Extension extraStack, InsnList before, InsnList after) { final String handlerDesc = Bytecode.generateDescriptor(constantType, constantType); final boolean withArgs = this.checkDescriptor(handlerDesc, target, "getter"); if (!this.isStatic) { before.insert(new VarInsnNode(Opcodes.ALOAD, 0)); - target.addToStack(1); + extraStack.add(); } if (withArgs) { this.pushArgs(target.arguments, after, target.getArgIndices(), 0, target.arguments.length); - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); } return this.invokeHandler(after); 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 01934366..d7e1c347 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 @@ -43,6 +43,7 @@ import org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess; import org.spongepowered.asm.mixin.injection.points.BeforeNew; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.Annotations; @@ -311,19 +312,19 @@ public class RedirectInjector extends InvokeInjector { this.validateParams(invoke); InsnList insns = new InsnList(); - int extraLocals = Bytecode.getArgsSize(invoke.locals) + 1; - int extraStack = 1; // Normally only need 1 extra stack pos to store target ref + Extension extraLocals = target.extendLocals().add(invoke.locals).add(1); + Extension extraStack = target.extendStack().add(1); // Normally only need 1 extra stack pos to store target ref int[] argMap = this.storeArgs(target, invoke.locals, insns, 0); if (invoke.captureTargetArgs) { int argSize = Bytecode.getArgsSize(target.arguments); - extraLocals += argSize; - extraStack += argSize; + extraLocals.add(argSize); + extraStack.add(argSize); argMap = Ints.concat(argMap, target.getArgIndices()); } AbstractInsnNode insn = this.invokeHandlerWithArgs(this.methodArgs, insns, argMap); target.replaceNode(invoke.node, insn, insns); - target.addToLocals(extraLocals); - target.addToStack(extraStack); + extraLocals.apply(); + extraStack.apply(); } /** @@ -465,7 +466,7 @@ public class RedirectInjector extends InvokeInjector { * the target method signature * @param type description of access type for use in error messages */ - public void injectArrayRedirect(Target target, FieldInsnNode fieldNode, AbstractInsnNode varNode, boolean withArgs, String type) { + private void injectArrayRedirect(Target target, FieldInsnNode fieldNode, AbstractInsnNode varNode, boolean withArgs, String type) { if (varNode == null) { String advice = ""; throw new InvalidInjectionException(this.info, String.format( @@ -473,16 +474,19 @@ public class RedirectInjector extends InvokeInjector { this.annotationType, this, type, target, advice)); } + Extension extraStack = target.extendStack(); + if (!this.isStatic) { target.insns.insertBefore(fieldNode, new VarInsnNode(Opcodes.ALOAD, 0)); - target.addToStack(1); + extraStack.add(); } InsnList invokeInsns = new InsnList(); if (withArgs) { this.pushArgs(target.arguments, invokeInsns, target.getArgIndices(), 0, target.arguments.length); - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); } + extraStack.apply(); target.replaceNode(varNode, this.invokeHandler(invokeInsns), invokeInsns); } @@ -495,7 +499,7 @@ public class RedirectInjector extends InvokeInjector { * @param ownerType type of the field owner * @param fieldType field type */ - public void injectAtScalarField(Target target, final FieldInsnNode fieldNode, int opCode, Type ownerType, Type fieldType) { + private void injectAtScalarField(Target target, final FieldInsnNode fieldNode, int opCode, Type ownerType, Type fieldType) { AbstractInsnNode invoke = null; InsnList insns = new InsnList(); if (opCode == Opcodes.GETSTATIC || opCode == Opcodes.GETFIELD) { @@ -519,7 +523,10 @@ public class RedirectInjector extends InvokeInjector { final String handlerDesc = staticField ? Bytecode.generateDescriptor(fieldType) : Bytecode.generateDescriptor(fieldType, owner); final boolean withArgs = this.checkDescriptor(handlerDesc, target, "getter"); + Extension extraStack = target.extendStack(); + if (!this.isStatic) { + extraStack.add(); insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); if (!staticField) { insns.add(new InsnNode(Opcodes.SWAP)); @@ -528,10 +535,10 @@ public class RedirectInjector extends InvokeInjector { if (withArgs) { this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); } - target.addToStack(this.isStatic ? 0 : 1); + extraStack.apply(); return this.invokeHandler(insns); } @@ -545,11 +552,14 @@ public class RedirectInjector extends InvokeInjector { String handlerDesc = staticField ? Bytecode.generateDescriptor(null, fieldType) : Bytecode.generateDescriptor(null, owner, fieldType); boolean withArgs = this.checkDescriptor(handlerDesc, target, "setter"); + Extension extraStack = target.extendStack(); + if (!this.isStatic) { if (staticField) { insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); insns.add(new InsnNode(Opcodes.SWAP)); } else { + extraStack.add(); int marshallVar = target.allocateLocals(fieldType.getSize()); insns.add(new VarInsnNode(fieldType.getOpcode(Opcodes.ISTORE), marshallVar)); insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); @@ -560,10 +570,10 @@ public class RedirectInjector extends InvokeInjector { if (withArgs) { this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); } - target.addToStack(!this.isStatic && !staticField ? 1 : 0); + extraStack.apply(); return this.invokeHandler(insns); } @@ -635,13 +645,15 @@ public class RedirectInjector extends InvokeInjector { target.replaceNode(newNode, new VarInsnNode(Opcodes.ALOAD, 0)); } + Extension extraStack = target.extendStack(); InsnList insns = new InsnList(); if (withArgs) { this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); - target.addToStack(Bytecode.getArgsSize(target.arguments)); + extraStack.add(target.arguments); } this.invokeHandler(insns); + extraStack.apply(); if (isAssigned) { // Do a null-check following the redirect to ensure that the handler @@ -653,12 +665,13 @@ public class RedirectInjector extends InvokeInjector { this.throwException(insns, "java/lang/NullPointerException", String.format("%s constructor handler %s returned null for %s", this.annotationType, this, newNode.desc.replace('/', '.'))); insns.add(nullCheckSucceeded); - target.addToStack(1); + extraStack.add(); } else { // Result is not assigned, so just pop it from the operand stack insns.add(new InsnNode(Opcodes.POP)); } + extraStack.apply(); target.replaceNode(initNode, insns); meta.injected++; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java index affc51bb..1678b293 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java @@ -38,6 +38,7 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.injection.struct.Target.Extension; import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.mixin.refmap.IMixinContext; @@ -154,11 +155,13 @@ public class ModifyVariableInjector extends Injector { throw new InvalidInjectionException(this.info, "Variable modifier " + this + " has an invalid signature, expected " + handlerDesc + " but found " + this.methodNode.desc); } + + Extension extraStack = target.extendStack(); try { int local = this.discriminator.findLocal(context); if (local > -1) { - this.inject(context, local); + this.inject(context, extraStack, local); } } catch (InvalidImplicitDiscriminatorException ex) { if (this.discriminator.printLVT()) { @@ -168,8 +171,8 @@ public class ModifyVariableInjector extends Injector { throw new InvalidInjectionException(this.info, "Implicit variable modifier injection failed in " + this, ex); } + extraStack.apply(); target.insns.insertBefore(context.node, context.insns); - target.addToStack(this.isStatic ? 1 : 2); } /** @@ -199,14 +202,17 @@ public class ModifyVariableInjector extends Injector { * Perform the injection * * @param context target context + * @param extraStack stack extension * @param local local variable to capture */ - private void inject(final Context context, final int local) { + private void inject(final Context context, Extension extraStack, final int local) { if (!this.isStatic) { context.insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); + extraStack.add(); } context.insns.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ILOAD), local)); + extraStack.add(); this.invokeHandler(context.insns); context.insns.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ISTORE), local)); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java index 58eb9d6a..aa1b27e9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java @@ -51,6 +51,111 @@ import org.spongepowered.asm.util.Constants; * rather than passing a bunch of values around. */ public class Target implements Comparable, Iterable { + + /** + * A stateful extension of stack or locals space for a target method which + * can be applied. + * + *

Stack and locals extensions are generally localised to a particular + * injection. This means they don't necessarily need to accumulate, it's + * generally sufficient to expand the method's original stack or + * locals size by the extension amount, with the largest extension + * determining the final stack size of the method.

+ * + *

Before this mechanism, injectors generally needed to keep track of + * their extension sizes via arbitrary integers, then apply them in one go. + * However some injector logic made the flow of these integers hard to track + * and lead to insidious bugs when values which should be combined were + * instead applied sequentially. The minor performance trade-off of using + * extensions to track these operations is covered by the increase in + * robustness.

+ */ + public class Extension { + + /** + * True if this is a locals extension, false for stack. + */ + private final boolean locals; + + /** + * The current 'depth' of this extension. Discrete operations maintain + * their own size value. When an extension is applied this value resets + * to zero so that the same extension can be used for multiple + * operations. + */ + private int size; + + Extension(boolean locals) { + this.locals = locals; + } + + /** + * Add 1 to this extension + * + * @return fluent interface + */ + public Extension add() { + this.size++; + return this; + } + + /** + * Add the specified size to this extension + * + * @param size size to add + * @return fluent interface + */ + public Extension add(int size) { + this.size += size; + return this; + } + + /** + * Add the size of the supplied type array to this extension. The size + * of the supplied args will first be computed, then the result will be + * added to the extension. + * + * @param types Types to add + * @return fluent interface + */ + public Extension add(Type[] types) { + return this.add(Bytecode.getArgsSize(types)); + } + + /** + * Set the size of this extension + * + * @param size Size to set + * @return fluent interface + */ + public Extension set(int size) { + this.size = size; + return this; + } + + /** + * Get the current size of this extension + * + * @return fluent interface + */ + public int get() { + return this.size; + } + + /** + * Apply this extension to the target. This reduces the current size to + * zero so that a new operation can begin if required. + */ + public void apply() { + if (this.locals) { + Target.this.extendLocalsBy(this.size); + } else { + Target.this.extendStackBy(this.size); + } + this.size = 0; + } + + } /** * Target class node @@ -211,6 +316,12 @@ public class Target implements Comparable, Iterable { /** * Allocate a new local variable for the method * + *

Note that allocating a local differs from extending + * locals (see {@link #extendLocals()}) in that the allocation immediately + * increases the target max locals and returns the new index of the local + * just allocated. Extending locals is used for temporary localised + * extensions to the locals table for the purposes of injectors.

+ * * @return the allocated local index */ public int allocateLocal() { @@ -219,7 +330,13 @@ public class Target implements Comparable, Iterable { /** * Allocate a number of new local variables for this method, returns the - * first local variable index of the allocated range + * first local variable index of the allocated range. + * + *

Note that allocating locals differs from extending + * locals (see {@link #extendLocals()}) in that the allocation immediately + * increases the target max locals and returns the new index of the local + * just allocated. Extending locals is used for temporary localised + * extensions to the locals table for the purposes of injectors.

* * @param locals number of locals to allocate * @return the first local variable index of the allocated range @@ -229,13 +346,37 @@ public class Target implements Comparable, Iterable { this.method.maxLocals += locals; return nextLocal; } + + /** + * Creates a new locals extension. The returned object can be used to create + * extensions to the method locals for an injector to use. Extensions are + * not designed to overlap, and the largest extension for a method will + * determine the amount by which the locals table is extended. + * + * @return extension handle + */ + public Extension extendLocals() { + return new Extension(true); + } + + /** + * Creates a new stack extension. The returned object can be used to create + * extensions to the method stack for an injector to use. Extensions are + * not designed to overlap, and the largest extension for a method will + * determine the amount by which the stack is extended. + * + * @return extension handle + */ + public Extension extendStack() { + return new Extension(false); + } /** * Allocate a number of new local variables for this method * * @param locals number of locals to allocate */ - public void addToLocals(int locals) { + void extendLocalsBy(int locals) { this.setMaxLocals(this.maxLocals + locals); } @@ -245,7 +386,7 @@ public class Target implements Comparable, Iterable { * * @param maxLocals max locals value to set */ - public void setMaxLocals(int maxLocals) { + private void setMaxLocals(int maxLocals) { if (maxLocals > this.method.maxLocals) { this.method.maxLocals = maxLocals; } @@ -256,7 +397,7 @@ public class Target implements Comparable, Iterable { * * @param stack number of stack entries to allocate */ - public void addToStack(int stack) { + void extendStackBy(int stack) { this.setMaxStack(this.maxStack + stack); } @@ -266,7 +407,7 @@ public class Target implements Comparable, Iterable { * * @param maxStack max stack value to set */ - public void setMaxStack(int maxStack) { + private void setMaxStack(int maxStack) { if (maxStack > this.method.maxStack) { this.method.maxStack = maxStack; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 2169e6db..a69662c4 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.launch.MixinInitialisationError; import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnList; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel; import org.spongepowered.asm.mixin.MixinEnvironment.Option; @@ -508,23 +509,43 @@ final class MixinConfig implements Comparable, IMixinConfig { return fallbackEnvironment; } - @SuppressWarnings("unchecked") private void initInjectionPoints() { if (this.injectorOptions.injectionPoints == null) { return; } - for (String injectionPoint : this.injectorOptions.injectionPoints) { + for (String injectionPointClassName : this.injectorOptions.injectionPoints) { + this.initInjectionPoint(injectionPointClassName); + } + } + + @SuppressWarnings("unchecked") + public void initInjectionPoint(String className) { + try { + Class injectionPointClass = null; try { - Class injectionPointClass = this.service.getClassProvider().findClass(injectionPoint, true); - if (InjectionPoint.class.isAssignableFrom(injectionPointClass)) { - InjectionPoint.register((Class)injectionPointClass); - } else { - this.logger.error("Unable to register injection point {} for {}, class must extend InjectionPoint", injectionPointClass, this); - } - } catch (Throwable th) { - this.logger.catching(th); + injectionPointClass = this.service.getClassProvider().findClass(className, true); + } catch (ClassNotFoundException cnfe) { + this.logger.error("Unable to register injection point {} for {}, the specified class was not found", className, this, cnfe); + return; } + + if (!InjectionPoint.class.isAssignableFrom(injectionPointClass)) { + this.logger.error("Unable to register injection point {} for {}, class must extend InjectionPoint", className, this); + return; + } + + try { + injectionPointClass.getDeclaredMethod("find", String.class, InsnList.class, Collection.class); + } catch (NoSuchMethodException cnfe) { + this.logger.error("Unable to register injection point {} for {}, the class is not compatible with this version of Mixin", + className, this, cnfe); + return; + } + + InjectionPoint.register((Class)injectionPointClass); + } catch (Throwable th) { + this.logger.catching(th); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index 458d0ccd..99bd42dd 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -887,7 +887,7 @@ class MixinInfo implements Comparable, IMixinInfo { for (String targetRef : inTargets) { String targetName = targetRef.replace('/', '.'); if (MixinInfo.classLoaderUtil.isClassLoaded(targetName) && !this.isReloading()) { - String message = String.format("Critical problem: %s target %s was already transformed.", this, targetName); + String message = String.format("Critical problem: %s target %s was loaded too early.", this, targetName); if (this.parent.isRequired()) { throw new MixinTargetAlreadyLoadedException(this, message, targetName); } diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index 67104255..091ff60a 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -388,7 +388,7 @@ public final class Bytecode { * @param method Method to print */ public static void printMethod(MethodNode method) { - System.err.printf("%s%s\n", method.name, method.desc); + System.err.printf("%s%s maxStack=%d maxLocals=%d\n", method.name, method.desc, method.maxStack, method.maxLocals); int index = 0; for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { System.err.printf("%-4d ", index++); diff --git a/src/main/java/org/spongepowered/asm/util/asm/ASM.java b/src/main/java/org/spongepowered/asm/util/asm/ASM.java index 0f13353c..4ce4cfcc 100644 --- a/src/main/java/org/spongepowered/asm/util/asm/ASM.java +++ b/src/main/java/org/spongepowered/asm/util/asm/ASM.java @@ -47,7 +47,7 @@ public final class ASM { /** * Get the ASM API version as a string (mostly for debugging and the banner) * - * @return + * @return ASM API version as string */ public static String getApiVersionString() { String suffix = "";