From e2a3b30b8bb617e5813364a863048202b7905494 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Tue, 27 Jun 2017 13:33:54 +0100 Subject: [PATCH] Enforce staticness of pre-super ModifyArg invocations --- .../injection/invoke/InvokeInjector.java | 24 ++++++++++++++++ .../injection/invoke/ModifyArgInjector.java | 6 ++++ .../injection/invoke/ModifyArgsInjector.java | 6 ++++ .../injection/invoke/RedirectInjector.java | 28 ++----------------- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java index c53355f7..926b8d50 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java @@ -77,6 +77,30 @@ public abstract class InvokeInjector extends Injector { } } + /** + * The normal staticness check is not location-aware, in that it merely + * enforces static modifiers of handlers to match their targets. For + * injecting into constructors however (which are ostensibly instance + * methods) calls which are injected before the call to + * super() cannot access this and must therefore be declared + * as static. + * + * @param target Target method + * @param node Injection location + */ + protected void checkTargetForNode(Target target, InjectionNode node) { + if (!target.isCtor) { + return; + } + + MethodInsnNode superCall = target.findSuperInitNode(); + int superCallIndex = target.indexOf(superCall); + int targetIndex = target.indexOf(node.getCurrentTarget()); + if (targetIndex <= superCallIndex && !this.isStatic) { + throw new InvalidInjectionException(this.info, "Pre-super " + this.annotationType + " invocation must be static in " + this); + } + } + /* (non-Javadoc) * @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector * #inject(org.spongepowered.asm.mixin.injection.callback.Target, 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 8480851d..f00bb5bc 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 @@ -94,6 +94,12 @@ public class ModifyArgInjector extends InvokeInjector { } } + @Override + protected void inject(Target target, InjectionNode node) { + this.checkTargetForNode(target, node); + super.inject(target, node); + } + /** * Do the injection */ 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 aa938647..64d5364a 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 @@ -62,6 +62,12 @@ public class ModifyArgsInjector extends InvokeInjector { } } + @Override + protected void inject(Target target, InjectionNode node) { + this.checkTargetForNode(target, node); + super.inject(target, node); + } + /** * Do the injection */ 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 b184aa75..0c581934 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 @@ -226,12 +226,14 @@ public class RedirectInjector extends InvokeInjector { if (node.getCurrentTarget() instanceof MethodInsnNode) { this.checkTargetForNode(target, node); + super.checkTarget(target); this.injectAtInvoke(target, node); return; } if (node.getCurrentTarget() instanceof FieldInsnNode) { this.checkTargetForNode(target, node); + super.checkTarget(target); this.injectAtFieldAccess(target, node); return; } @@ -248,32 +250,6 @@ public class RedirectInjector extends InvokeInjector { + target + " in " + this); } - /** - * The normal staticness check is not location-aware, in that it merely - * enforces static modifiers of handlers to match their targets. For - * injecting into constructors however (which are ostensibly instance - * methods) calls which are redirected before the call to - * super() cannot access this and must therefore be declared - * as static. - * - * @param target Target method - * @param node Injection location - */ - private void checkTargetForNode(Target target, InjectionNode node) { - if (target.isCtor) { - MethodInsnNode superCall = target.findSuperInitNode(); - int superCallIndex = target.indexOf(superCall); - int targetIndex = target.indexOf(node.getCurrentTarget()); - if (targetIndex < superCallIndex) { - if (!this.isStatic) { - throw new InvalidInjectionException(this.info, "@Redirect of pre-super invocation must be static in " + this); - } - return; - } - } - super.checkTarget(target); - } - protected boolean preInject(InjectionNode node) { Meta other = node.getDecoration(Meta.KEY); if (other.getOwner() != this) {