From b0cd24bd63e62481973d1da0864b627060ea5cc0 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Tue, 13 Jul 2021 19:28:04 +0100 Subject: [PATCH] Use more accurate coordinates for ModifyVariable context decorations Fixes #511 --- .../modify/LocalVariableDiscriminator.java | 23 ++++++++---- .../modify/ModifyVariableInjector.java | 35 +++++++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java index 544e5184..3ee7c0f9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java @@ -65,17 +65,17 @@ public class LocalVariableDiscriminator { /** * Ordinal value of this local variable type */ - int ord = 0; + private int ord = -1; /** * Local variable name */ - String name; + final String name; /** * Local variable type */ - Type type; + final Type type; public Local(String name, Type type) { this.name = name; @@ -87,6 +87,17 @@ public class LocalVariableDiscriminator { return String.format("Local[ordinal=%d, name=%s, type=%s]", this.ord, this.name, this.type); } + void setOrdinal(int ordinal) { + if (this.ord > -1 && this.ord != ordinal) { + throw new IllegalStateException("Attempted to reset ordinal for computed local"); + } + this.ord = ordinal; + } + + int getOrdinal() { + return this.ord; + } + } /** @@ -166,7 +177,7 @@ public class LocalVariableDiscriminator { if (this.locals[l] != null) { ordinal = ordinalMap.get(this.locals[l].type); ordinalMap.put(this.locals[l].type, ordinal = Integer.valueOf(ordinal == null ? 0 : ordinal.intValue() + 1)); - this.locals[l].ord = ordinal.intValue(); + this.locals[l].setOrdinal(ordinal.intValue()); } } } @@ -189,7 +200,7 @@ public class LocalVariableDiscriminator { if (local != null) { Type localType = local.type; String localName = local.name; - int ordinal = local.ord; + int ordinal = local.getOrdinal(); String candidate = this.returnType.equals(localType) ? "YES" : "-"; printer.add("[%3d] [%3d] %30s %-50s %s", l, ordinal, SignaturePrinter.getTypeName(localType, false), localName, candidate); } else if (l > 0) { @@ -366,7 +377,7 @@ public class LocalVariableDiscriminator { continue; } if (this.ordinal > -1) { - if (this.ordinal == local.ord) { + if (this.ordinal == local.getOrdinal()) { return index; } continue; 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 147d9076..2e3b281b 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 @@ -93,11 +93,6 @@ public class ModifyVariableInjector extends Injector { abstract boolean find(InjectionInfo info, InsnList insns, Collection nodes, Target target); } - - /** - * Decorator key for Context decoration - */ - private static final String LOCALS_CONTEXT_KEY = "localcontext"; /** * True to consider only method args @@ -142,10 +137,27 @@ public class ModifyVariableInjector extends Injector { } } + /** + * Generate a key which uniquely identifies the combination of return type, + * frame type and target injection node so that injectors targetting the + * same instruction still get unique contexts. + * + * @param target Target method + * @param node Target node + * @return Key for storing/retrieving the injector context decoration + */ + protected String getTargetNodeKey(Target target, InjectionNode node) { + return String.format("localcontext(%s,%s,#%s)", this.returnType, this.discriminator.isArgsOnly() ? "argsOnly" : "fullFrame", node.getId()); + } + @Override protected void preInject(Target target, InjectionNode node) { + String key = this.getTargetNodeKey(target, node); + if (node.hasDecoration(key)) { + return; // already have a suitable context + } Context context = new Context(this.returnType, this.discriminator.isArgsOnly(), target, node.getCurrentTarget()); - node.decorate(ModifyVariableInjector.LOCALS_CONTEXT_KEY, context); + node.decorate(key, context); } /** @@ -157,13 +169,22 @@ public class ModifyVariableInjector extends Injector { throw new InvalidInjectionException(this.info, "Variable modifier target for " + this + " was removed by another injector"); } - Context context = node.getDecoration(ModifyVariableInjector.LOCALS_CONTEXT_KEY); + Context context = node.getDecoration(this.getTargetNodeKey(target, node)); if (context == null) { throw new InjectionError(String.format( "%s injector target is missing CONTEXT decoration for %s. PreInjection failure or illegal internal state change", this.annotationType, this.info)); } + // If the context is being reused (because two identical injectors are targetting this node) + // then the insns SHOULD have been drained by the previous insertBefore. If the list hasn't + // been cleared for some reason then something probably went wrong during the previous inject + if (context.insns.size() > 0) { + throw new InjectionError(String.format( + "%s injector target has contaminated CONTEXT decoration for %s. Check for previous errors.", + this.annotationType, this.info)); + } + if (this.discriminator.printLVT()) { this.printLocals(target, context); }