Fix for the fix for computing frames which now fixes #481 and fixes #482

This commit is contained in:
Mumfrey committed 2021-04-14 19:31:06 +01:00
1 parent 9109a19d5e
commit 6f5dce173b
5 files changed
+47 -12

No files matched your search

@@ -236,7 +236,7 @@ public class BeforeConstant extends InjectionPoint {
if (matchesInsn) {
this.log(" BeforeConstant found a matching constant{} at ordinal {}", this.matchByType != null ? " TYPE" : " value", ordinal);
if (this.ordinal == -1 || this.ordinal == ordinal) {
this.log(" BeforeConstant found {}", Bytecode.describeNode(insn).trim());
this.log(" BeforeConstant found {}", Bytecode.describeNode(insn, false));
nodes.add(insn);
found = true;
}
@@ -205,7 +205,7 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
*/
@Override
public String toString() {
return String.format("InjectionNode[%s]", Bytecode.describeNode(this.currentTarget).replaceAll("\\s+", " "));
return String.format("InjectionNode[%s]", Bytecode.describeNode(this.currentTarget, false));
}
}
@@ -248,7 +248,8 @@ public final class ClassInfo {
*/
@Override
public String toString() {
return String.format("FrameData[index=%d, type=%s, locals=%d]", this.index, FrameData.FRAMETYPES[this.type + 1], this.locals);
return String.format("FrameData[index=%d, type=%s, locals=%d size=%d]", this.index, FrameData.FRAMETYPES[this.type + 1], this.locals,
this.size);
}
}
@@ -413,15 +413,27 @@ public final class Bytecode {
* @return human-readable description of node
*/
public static String describeNode(AbstractInsnNode node) {
return Bytecode.describeNode(node, true);
}
/**
* Gets a description of the supplied node for debugging purposes
*
* @param node node to describe
* @param listFormat format the returned string so that returned nodes line
* up (node names aligned to 14 chars)
* @return human-readable description of node
*/
public static String describeNode(AbstractInsnNode node, boolean listFormat) {
if (node == null) {
return String.format(" %-14s ", "null");
return listFormat ? String.format(" %-14s ", "null") : "null";
}
if (node instanceof LabelNode) {
return String.format("[%s]", ((LabelNode)node).getLabel());
}
String out = String.format(" %-14s ", node.getClass().getSimpleName().replace("Node", ""));
String out = String.format(listFormat ? " %-14s " : "%s ", node.getClass().getSimpleName().replace("Node", ""));
if (node instanceof JumpInsnNode) {
out += String.format("[%s] [%s]", Bytecode.getOpcodeName(node), ((JumpInsnNode)node).label.getLabel());
} else if (node instanceof VarInsnNode) {
@@ -445,6 +457,8 @@ public final class Bytecode {
out += (((IntInsnNode)node).operand);
} else if (node instanceof FrameNode) {
out += String.format("[%s] ", Bytecode.getOpcodeName(((FrameNode)node).type, "H_INVOKEINTERFACE", -1));
} else if (node instanceof TypeInsnNode) {
out += String.format("[%s] %s", Bytecode.getOpcodeName(node), ((TypeInsnNode)node).desc);
} else {
out += String.format("[%s] ", Bytecode.getOpcodeName(node));
}
@@ -135,7 +135,11 @@ public final class Locals {
*/
public static LocalVariableNode[] getLocalsAt(ClassNode classNode, MethodNode method, AbstractInsnNode node) {
for (int i = 0; i < 3 && (node instanceof LabelNode || node instanceof LineNumberNode); i++) {
node = Locals.nextNode(method.instructions, node);
AbstractInsnNode nextNode = Locals.nextNode(method.instructions, node);
if (nextNode instanceof FrameNode) { // Do not ffwd over frames
break;
}
node = nextNode;
}
ClassInfo classInfo = ClassInfo.forName(classNode.name);
@@ -166,6 +170,7 @@ public final class Locals {
int frameSize = local;
int frameIndex = -1;
int lastFrameSize = local;
int knownFrameSize = local;
VarInsnNode storeInsn = null;
for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext();) {
@@ -173,9 +178,11 @@ public final class Locals {
if (storeInsn != null) {
LocalVariableNode storedLocal = Locals.getLocalVariableAt(classNode, method, insn, storeInsn.var);
frame[storeInsn.var] = storedLocal;
knownFrameSize = Math.max(knownFrameSize, storeInsn.var + 1);
if (storedLocal != null && storeInsn.var < method.maxLocals - 1 && storedLocal.desc != null
&& Type.getType(storedLocal.desc).getSize() == 2) {
frame[storeInsn.var + 1] = null; // TOP
knownFrameSize = Math.max(knownFrameSize, storeInsn.var + 2);
}
storeInsn = null;
}
@@ -187,12 +194,12 @@ public final class Locals {
break handleFrame;
}
int frameNodeSize = Locals.computeFrameSize(frameNode);
FrameData frameData = frameIndex < frames.size() ? frames.get(frameIndex) : null;
if (frameData != null) {
if (frameData.type == Opcodes.F_FULL) {
frameSize = Math.min(frameSize, frameData.locals);
lastFrameSize = frameSize;
knownFrameSize = lastFrameSize = frameSize = Math.min(frameNodeSize, frameData.size);
} else {
frameSize = Locals.getAdjustedFrameSize(frameSize, frameData);
}
@@ -200,17 +207,25 @@ public final class Locals {
frameSize = Locals.getAdjustedFrameSize(frameSize, frameNode);
}
if (frameNode.type == Opcodes.F_CHOP || frameNode.type == Opcodes.F_NEW) {
// Sanity check
if (frameSize < 0) {
throw new IllegalStateException(String.format("Locals entered an invalid state evaluating %s::%s%s at instruction %d (%s). "
+ "Initial frame size is %d, calculated a frame size of %d with %s", classNode.name, method.name, method.desc,
method.instructions.indexOf(insn), Bytecode.describeNode(insn, false), initialFrameSize, frameSize, frameData));
}
if ((frameData == null && (frameNode.type == Opcodes.F_CHOP || frameNode.type == Opcodes.F_NEW))
|| (frameData != null && frameData.type == Opcodes.F_CHOP)) {
for (int framePos = frameSize; framePos < frame.length; framePos++) {
frame[framePos] = null;
}
lastFrameSize = frameSize;
knownFrameSize = lastFrameSize = frameSize;
break handleFrame;
}
int framePos = frameNode.type == Opcodes.F_APPEND ? lastFrameSize : 0;
lastFrameSize = frameSize;
// localPos tracks the location in the frame node's locals list, which doesn't leave space for TOP entries
for (int localPos = 0; framePos < frame.length; framePos++, localPos++) {
// Get the local at the current position in the FrameNode's locals list
@@ -239,7 +254,11 @@ public final class Locals {
}
} else if (localType == null) {
if (framePos >= initialFrameSize && framePos >= frameSize && frameSize > 0) {
frame[framePos] = null;
if (framePos < knownFrameSize) {
frame[framePos] = Locals.getLocalVariableAt(classNode, method, insn, framePos);
} else {
frame[framePos] = null;
}
}
} else if (localType instanceof LabelNode) {
// Uninitialised
@@ -253,6 +272,7 @@ public final class Locals {
boolean isLoad = insn.getOpcode() >= Opcodes.ILOAD && insn.getOpcode() <= Opcodes.SALOAD;
if (isLoad) {
frame[varNode.var] = Locals.getLocalVariableAt(classNode, method, insn, varNode.var);
knownFrameSize = Math.max(knownFrameSize, varNode.var + Type.getType(frame[varNode.var].desc).getSize());
} else {
// Update the LVT for the opcode AFTER this one, since we always want to know
// the frame state BEFORE the *current* instruction to match the contract of