From dea3a3b6ef7c0aeadd242609ec15eb9523633f13 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 29 Apr 2015 15:51:58 +0100 Subject: [PATCH 1/4] Troubleshooting for LVT generation, emit lots of debug info during gen --- .../org/spongepowered/asm/util/Locals.java | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/util/Locals.java b/src/main/java/org/spongepowered/asm/util/Locals.java index d0603480..b177b3df 100644 --- a/src/main/java/org/spongepowered/asm/util/Locals.java +++ b/src/main/java/org/spongepowered/asm/util/Locals.java @@ -74,7 +74,7 @@ public class Locals { } } } - + /** *

Attempts to identify available locals at an arbitrary point in the * bytecode specified by node.

@@ -118,6 +118,8 @@ public class Locals { * specified location */ public static LocalVariableNode[] getLocalsAt(ClassNode classNode, MethodNode method, AbstractInsnNode node) { + System.err.printf("----------------------------------------------------\n"); + LocalVariableNode[] frame = new LocalVariableNode[method.maxLocals]; int local = 0, index = 0; @@ -131,11 +133,15 @@ public class Locals { frame[local] = new LocalVariableNode("arg" + index++, argType.toString(), null, null, null, local); local += argType.getSize(); } + + int initialFrameSize = local; + int opcode = 0; - for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + for (Iterator iter = method.instructions.iterator(); iter.hasNext(); opcode++) { AbstractInsnNode insn = iter.next(); if (insn instanceof FrameNode) { FrameNode frameNode = (FrameNode) insn; + System.err.printf("FRAME: Type=%s Size=%d\n", frameNode.type, frameNode.local.size()); // localPos tracks the location in the frame node's locals list, which doesn't leave space for TOP entries for (int localPos = 0, framePos = 0; framePos < frame.length; framePos++, localPos++) { @@ -144,14 +150,22 @@ public class Locals { if (localType instanceof String) { // String refers to a reference type frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos); + Locals.print(method.name, opcode, "F REF " + framePos, frame); } else if (localType instanceof Integer) { // Integer refers to a primitive type or other marker - boolean isMarkerType = localType == Opcodes.UNINITIALIZED_THIS || localType == Opcodes.TOP || localType == Opcodes.NULL; + boolean isMarkerType = localType == Opcodes.UNINITIALIZED_THIS || localType == Opcodes.NULL; boolean is32bitValue = localType == Opcodes.INTEGER || localType == Opcodes.FLOAT; boolean is64bitValue = localType == Opcodes.DOUBLE || localType == Opcodes.LONG; - if (isMarkerType) { + if (localType == Opcodes.TOP) { + if (framePos >= initialFrameSize) { +// frame[framePos] = null; + Locals.print(method.name, opcode, "F TOP " + framePos, frame); + } + } else if (isMarkerType) { frame[framePos] = null; + Locals.print(method.name, opcode, "F MRK[" + localType + "] " + framePos, frame); } else if (is32bitValue || is64bitValue) { frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos); + Locals.print(method.name, opcode, "F PRIMITIVE " + framePos, frame); if (is64bitValue) { framePos++; @@ -162,15 +176,20 @@ public class Locals { + " in " + classNode.name + "." + method.name + method.desc); } } else if (localType == null) { - frame[framePos] = null; + if (framePos >= initialFrameSize && frameNode.local.size() > 0) { + frame[framePos] = null; + } + Locals.print(method.name, opcode, "NULL " + framePos, frame); } else { throw new RuntimeException("Invalid value " + localType + " in locals array at position " + localPos + " in " + classNode.name + "." + method.name + method.desc); } } + System.err.printf("ENDFRAME\n"); } else if (insn instanceof VarInsnNode) { VarInsnNode varNode = (VarInsnNode) insn; frame[varNode.var] = Locals.getLocalVariableAt(classNode, method, node, varNode.var); + Locals.print(method.name, opcode, "VAR " + varNode.var, frame); } else if (insn == node) { break; } @@ -179,6 +198,20 @@ public class Locals { return frame; } + private static void print(String name, int opcode, String string, LocalVariableNode[] frame) { + System.err.printf("%-30s #%-4d %-20s [ ", name, opcode, string); + for (LocalVariableNode node : frame) { + String desc = node != null ? node.desc : "-"; + if (desc == null) { + desc = "?"; + } else { + desc = desc.substring(desc.lastIndexOf('/') + 1).replace(';', ' ').trim(); + } + System.err.printf("%s ", desc); + } + System.err.printf("]\n"); + } + /** * Attempts to locate the appropriate entry in the local variable table for * the specified local variable index at the location specified by node. @@ -282,7 +315,10 @@ public class Locals { LocalVariableNode[] localNodes = new LocalVariableNode[method.maxLocals]; // LocalVariableNodes for current frame BasicValue[] locals = new BasicValue[method.maxLocals]; // locals in previous frame, used to work out what changes between frames LabelNode[] labels = new LabelNode[methodSize]; // Labels to add to the method, for the markers - + String[] lastKnownType = new String[method.maxLocals]; + + System.err.printf(" -------->>\n"); + // Traverse the frames and work out when locals begin and end for (int i = 0; i < methodSize; i++) { Frame f = frames[i]; @@ -316,8 +352,12 @@ public class Locals { localNodes[j] = null; } - String desc = (local.getType() != null) ? local.getType().getDescriptor() : null; + String desc = (local.getType() != null) ? local.getType().getDescriptor() : lastKnownType[j]; + System.err.printf(" -------->> Generating %d with %s (%s)\n", j, desc, lastKnownType[j]); localNodes[j] = new LocalVariableNode("var" + j, desc, null, label, null, j); + if (desc != null) { + lastKnownType[j] = desc; + } } locals[j] = local; From 2955e1a359a0472b17983f691b326ce8afb54b0e Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 7 May 2015 16:36:14 +0100 Subject: [PATCH 2/4] Use information about original frames to improve LVT generation, ref #42 --- .../asm/mixin/transformer/ClassInfo.java | 69 +++++++++++++++++-- .../org/spongepowered/asm/util/Locals.java | 31 +++++++-- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index 6ae31bbd..4e7275f8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -35,9 +36,11 @@ import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.FrameNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type; @@ -51,7 +54,7 @@ import com.google.common.collect.ImmutableSet; * information needed to support more complex mixin behaviour such as detached * superclass and mixin inheritance. */ -class ClassInfo extends TreeInfo { +public class ClassInfo extends TreeInfo { /** *

To all intents and purposes, the "real" class hierarchy and the mixin @@ -68,7 +71,7 @@ class ClassInfo extends TreeInfo { * immediate match falls through to {@link #NONE} on the next step, which * prevents further traversals from occurring in the lookup.

*/ - static enum Traversal { + public static enum Traversal { /** * No traversals are allowed. @@ -109,6 +112,40 @@ class ClassInfo extends TreeInfo { } } + /** + * Information about frames in a method + */ + public static class FrameData { + + private static final String[] FRAMETYPES = { "NEW", "FULL", "APPEND", "CHOP", "SAME", "SAME1" }; + + public final int index; + + public final int type; + + public final int locals; + + FrameData(int index, int type, int locals) { + this.index = index; + this.type = type; + this.locals = locals; + } + + FrameData(int index, FrameNode frameNode) { + this.index = index; + this.type = frameNode.type; + this.locals = frameNode.local != null ? frameNode.local.size() : 0; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("FrameData[index=%d, type=%s, locals=%d]", this.index, FrameData.FRAMETYPES[this.type + 1], this.locals); + } + } + /** * Information about a member in this class */ @@ -236,10 +273,13 @@ class ClassInfo extends TreeInfo { /** * A method */ - class Method extends Member { + public class Method extends Member { + + private final List frames; public Method(Member member) { super(member); + this.frames = member instanceof Method ? ((Method)member).frames : null; } public Method(MethodNode method) { @@ -248,20 +288,39 @@ class ClassInfo extends TreeInfo { public Method(MethodNode method, boolean injected) { super(Type.METHOD, method.name, method.desc, method.access, injected); + this.frames = this.gatherFrames(method); } public Method(String name, String desc) { super(Type.METHOD, name, desc, Opcodes.ACC_PUBLIC, false); + this.frames = null; } public Method(String name, String desc, int access) { super(Type.METHOD, name, desc, access, false); + this.frames = null; } public Method(String name, String desc, int access, boolean injected) { super(Type.METHOD, name, desc, access, injected); + this.frames = null; } + private List gatherFrames(MethodNode method) { + List frames = new ArrayList(); + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof FrameNode) { + frames.add(new FrameData(method.instructions.indexOf(insn), (FrameNode)insn)); + } + } + return frames; + } + + public List getFrames() { + return this.frames; + } + @Override public ClassInfo getOwner() { return ClassInfo.this; @@ -629,7 +688,7 @@ class ClassInfo extends TreeInfo { /** * Class targets */ - protected List getTargets() { + List getTargets() { if (this.mixin != null) { List targets = new ArrayList(); targets.add(this); @@ -1269,7 +1328,7 @@ class ClassInfo extends TreeInfo { * @param classNode * @return */ - public static ClassInfo fromClassNode(ClassNode classNode) { + static ClassInfo fromClassNode(ClassNode classNode) { ClassInfo info = ClassInfo.cache.get(classNode.name); if (info == null) { info = new ClassInfo(classNode); diff --git a/src/main/java/org/spongepowered/asm/util/Locals.java b/src/main/java/org/spongepowered/asm/util/Locals.java index b177b3df..75336b82 100644 --- a/src/main/java/org/spongepowered/asm/util/Locals.java +++ b/src/main/java/org/spongepowered/asm/util/Locals.java @@ -44,6 +44,8 @@ import org.objectweb.asm.tree.analysis.Analyzer; import org.objectweb.asm.tree.analysis.AnalyzerException; import org.objectweb.asm.tree.analysis.BasicValue; import org.objectweb.asm.tree.analysis.Frame; +import org.spongepowered.asm.mixin.transformer.ClassInfo; +import org.spongepowered.asm.mixin.transformer.ClassInfo.FrameData; import org.spongepowered.asm.mixin.transformer.MixinVerifier; @@ -120,6 +122,8 @@ public class Locals { public static LocalVariableNode[] getLocalsAt(ClassNode classNode, MethodNode method, AbstractInsnNode node) { System.err.printf("----------------------------------------------------\n"); + List frames = ClassInfo.forName(classNode.name).findMethod(method).getFrames(); + LocalVariableNode[] frame = new LocalVariableNode[method.maxLocals]; int local = 0, index = 0; @@ -135,13 +139,16 @@ public class Locals { } int initialFrameSize = local; - int opcode = 0; + int opcode = 0, frameIndex = 0, numLocals = 0; for (Iterator iter = method.instructions.iterator(); iter.hasNext(); opcode++) { AbstractInsnNode insn = iter.next(); if (insn instanceof FrameNode) { - FrameNode frameNode = (FrameNode) insn; - System.err.printf("FRAME: Type=%s Size=%d\n", frameNode.type, frameNode.local.size()); + FrameNode frameNode = (FrameNode)insn; + FrameData frameData = frames.get(frameIndex++); + + numLocals = frameData.type == Opcodes.F_FULL ? Math.max(numLocals, frameNode.local.size()) : frameNode.local.size(); + System.err.printf("FRAME: Type=%s Size=%d FD=%s TOTAL=%d\n", frameNode.type, frameNode.local.size(), frameData, numLocals); // localPos tracks the location in the frame node's locals list, which doesn't leave space for TOP entries for (int localPos = 0, framePos = 0; framePos < frame.length; framePos++, localPos++) { @@ -176,7 +183,7 @@ public class Locals { + " in " + classNode.name + "." + method.name + method.desc); } } else if (localType == null) { - if (framePos >= initialFrameSize && frameNode.local.size() > 0) { + if (framePos >= initialFrameSize && framePos >= numLocals && numLocals > 0) { frame[framePos] = null; } Locals.print(method.name, opcode, "NULL " + framePos, frame); @@ -206,6 +213,22 @@ public class Locals { desc = "?"; } else { desc = desc.substring(desc.lastIndexOf('/') + 1).replace(';', ' ').trim(); + desc = desc.replace("ServerConfigurationManager", "SCM"); + desc = desc.replace("NetworkManager", "NM"); + desc = desc.replace("EntityPlayerMP", "EPMP"); + desc = desc.replace("GameProfile", "GP"); + desc = desc.replace("PlayerProfileCache", "PPC"); + desc = desc.replace("NBTTagCompound", "NBTTC"); + desc = desc.replace("WorldServer", "WS"); + desc = desc.replace("WorldInfo", "WI"); + desc = desc.replace("BlockPos", "BP"); + desc = desc.replace("NetHandlerPlayServer", "NHPS"); + desc = desc.replace("ChatComponentTranslation", "CCT"); + desc = desc.replace("Iterator", "IT"); + desc = desc.replace("PotionEffect", "PE"); + desc = desc.replace("World", "W"); + desc = desc.replace("EntityPlayer", "EP"); + desc = desc.replace("Entity", "E"); } System.err.printf("%s ", desc); } From c9f6d04f84d71163d4e0180bd883ace86486c0a5 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 8 May 2015 02:06:35 +0100 Subject: [PATCH 3/4] Clean up debugging code in Locals ready for merge --- .../org/spongepowered/asm/util/Locals.java | 60 +++---------------- 1 file changed, 8 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/util/Locals.java b/src/main/java/org/spongepowered/asm/util/Locals.java index 75336b82..c1e965e4 100644 --- a/src/main/java/org/spongepowered/asm/util/Locals.java +++ b/src/main/java/org/spongepowered/asm/util/Locals.java @@ -76,7 +76,7 @@ public class Locals { } } } - + /** *

Attempts to identify available locals at an arbitrary point in the * bytecode specified by node.

@@ -120,8 +120,6 @@ public class Locals { * specified location */ public static LocalVariableNode[] getLocalsAt(ClassNode classNode, MethodNode method, AbstractInsnNode node) { - System.err.printf("----------------------------------------------------\n"); - List frames = ClassInfo.forName(classNode.name).findMethod(method).getFrames(); LocalVariableNode[] frame = new LocalVariableNode[method.maxLocals]; @@ -139,16 +137,16 @@ public class Locals { } int initialFrameSize = local; - int opcode = 0, frameIndex = 0, numLocals = 0; + int frameIndex = -1, locals = 0; - for (Iterator iter = method.instructions.iterator(); iter.hasNext(); opcode++) { + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { AbstractInsnNode insn = iter.next(); if (insn instanceof FrameNode) { + frameIndex++; FrameNode frameNode = (FrameNode)insn; - FrameData frameData = frames.get(frameIndex++); + FrameData frameData = frameIndex < frames.size() ? frames.get(frameIndex) : null; - numLocals = frameData.type == Opcodes.F_FULL ? Math.max(numLocals, frameNode.local.size()) : frameNode.local.size(); - System.err.printf("FRAME: Type=%s Size=%d FD=%s TOTAL=%d\n", frameNode.type, frameNode.local.size(), frameData, numLocals); + locals = frameData != null && frameData.type == Opcodes.F_FULL ? Math.max(locals, frameNode.local.size()) : frameNode.local.size(); // localPos tracks the location in the frame node's locals list, which doesn't leave space for TOP entries for (int localPos = 0, framePos = 0; framePos < frame.length; framePos++, localPos++) { @@ -157,22 +155,16 @@ public class Locals { if (localType instanceof String) { // String refers to a reference type frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos); - Locals.print(method.name, opcode, "F REF " + framePos, frame); } else if (localType instanceof Integer) { // Integer refers to a primitive type or other marker boolean isMarkerType = localType == Opcodes.UNINITIALIZED_THIS || localType == Opcodes.NULL; boolean is32bitValue = localType == Opcodes.INTEGER || localType == Opcodes.FLOAT; boolean is64bitValue = localType == Opcodes.DOUBLE || localType == Opcodes.LONG; if (localType == Opcodes.TOP) { - if (framePos >= initialFrameSize) { -// frame[framePos] = null; - Locals.print(method.name, opcode, "F TOP " + framePos, frame); - } + // Do nothing, explicit TOP entries are pretty much always bogus, and real ones are handled below } else if (isMarkerType) { frame[framePos] = null; - Locals.print(method.name, opcode, "F MRK[" + localType + "] " + framePos, frame); } else if (is32bitValue || is64bitValue) { frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos); - Locals.print(method.name, opcode, "F PRIMITIVE " + framePos, frame); if (is64bitValue) { framePos++; @@ -183,20 +175,17 @@ public class Locals { + " in " + classNode.name + "." + method.name + method.desc); } } else if (localType == null) { - if (framePos >= initialFrameSize && framePos >= numLocals && numLocals > 0) { + if (framePos >= initialFrameSize && framePos >= locals && locals > 0) { frame[framePos] = null; } - Locals.print(method.name, opcode, "NULL " + framePos, frame); } else { throw new RuntimeException("Invalid value " + localType + " in locals array at position " + localPos + " in " + classNode.name + "." + method.name + method.desc); } } - System.err.printf("ENDFRAME\n"); } else if (insn instanceof VarInsnNode) { VarInsnNode varNode = (VarInsnNode) insn; frame[varNode.var] = Locals.getLocalVariableAt(classNode, method, node, varNode.var); - Locals.print(method.name, opcode, "VAR " + varNode.var, frame); } else if (insn == node) { break; } @@ -205,36 +194,6 @@ public class Locals { return frame; } - private static void print(String name, int opcode, String string, LocalVariableNode[] frame) { - System.err.printf("%-30s #%-4d %-20s [ ", name, opcode, string); - for (LocalVariableNode node : frame) { - String desc = node != null ? node.desc : "-"; - if (desc == null) { - desc = "?"; - } else { - desc = desc.substring(desc.lastIndexOf('/') + 1).replace(';', ' ').trim(); - desc = desc.replace("ServerConfigurationManager", "SCM"); - desc = desc.replace("NetworkManager", "NM"); - desc = desc.replace("EntityPlayerMP", "EPMP"); - desc = desc.replace("GameProfile", "GP"); - desc = desc.replace("PlayerProfileCache", "PPC"); - desc = desc.replace("NBTTagCompound", "NBTTC"); - desc = desc.replace("WorldServer", "WS"); - desc = desc.replace("WorldInfo", "WI"); - desc = desc.replace("BlockPos", "BP"); - desc = desc.replace("NetHandlerPlayServer", "NHPS"); - desc = desc.replace("ChatComponentTranslation", "CCT"); - desc = desc.replace("Iterator", "IT"); - desc = desc.replace("PotionEffect", "PE"); - desc = desc.replace("World", "W"); - desc = desc.replace("EntityPlayer", "EP"); - desc = desc.replace("Entity", "E"); - } - System.err.printf("%s ", desc); - } - System.err.printf("]\n"); - } - /** * Attempts to locate the appropriate entry in the local variable table for * the specified local variable index at the location specified by node. @@ -340,8 +299,6 @@ public class Locals { LabelNode[] labels = new LabelNode[methodSize]; // Labels to add to the method, for the markers String[] lastKnownType = new String[method.maxLocals]; - System.err.printf(" -------->>\n"); - // Traverse the frames and work out when locals begin and end for (int i = 0; i < methodSize; i++) { Frame f = frames[i]; @@ -376,7 +333,6 @@ public class Locals { } String desc = (local.getType() != null) ? local.getType().getDescriptor() : lastKnownType[j]; - System.err.printf(" -------->> Generating %d with %s (%s)\n", j, desc, lastKnownType[j]); localNodes[j] = new LocalVariableNode("var" + j, desc, null, label, null, j); if (desc != null) { lastKnownType[j] = desc; From becfae2c0b6ceb19eb16e87d6eaa87091166d909 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 8 May 2015 02:07:54 +0100 Subject: [PATCH 4/4] Version bump to 0.3.4 --- build.gradle | 2 +- src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 95aa6b4a..394ef3ba 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ defaultTasks 'licenseFormat', 'check', 'build' // Basic project information group = 'org.spongepowered' archivesBaseName = 'mixin' -version = '0.3.3-SNAPSHOT' +version = '0.3.4-SNAPSHOT' // Extended project information ext.projectName = 'Mixin' diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 9939c3c6..44fbe5c0 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.3.3"; + public static final String VERSION = "0.3.4"; /** * Blackboard key where the subsystem version will be stored to indicate