Add and test Anchor.Sequence to match instruction patterns, skipping labels and line numbers. Document sequence matching behavior and integrate new matchers (constant, opcode, invoke).
This commit is contained in:
1 parent
c9e41e2c91
commit
1a53f833e3
2 files changed
+254
No files matched your search
@@ -1,12 +1,19 @@
|
|||||||
package dev.overlord.zomboidforge.patcher;
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
import org.objectweb.asm.tree.ClassNode;
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.IntInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
import org.objectweb.asm.tree.MethodInsnNode;
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
import org.objectweb.asm.tree.MethodNode;
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
import org.objectweb.asm.util.Printer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The landmark in the target class that a {@link ClassPatch} depends on. A patch can only
|
* The landmark in the target class that a {@link ClassPatch} depends on. A patch can only
|
||||||
@@ -71,4 +78,134 @@ public sealed interface Anchor
|
|||||||
&& call.desc.equals(this.desc);
|
&& call.desc.equals(this.desc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consecutive instructions inside the method {@code in}, one per element of {@code pattern}.
|
||||||
|
* Labels, line numbers and stack map frames between them are skipped, so a jump target or a
|
||||||
|
* new source line inside the sequence does not break a match. Overlapping matches count
|
||||||
|
* separately.
|
||||||
|
*/
|
||||||
|
record Sequence(Method in, List<Instruction> pattern) implements Anchor
|
||||||
|
{
|
||||||
|
public Sequence
|
||||||
|
{
|
||||||
|
if (pattern.isEmpty()) throw new IllegalArgumentException("A sequence anchor needs at least one instruction.");
|
||||||
|
|
||||||
|
pattern = List.copyOf(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sequence(Method in, Instruction... pattern) { this(in, List.of(pattern)); }
|
||||||
|
|
||||||
|
/** The matched instructions, one per element of {@link #pattern()}, in the same order. */
|
||||||
|
public List<AbstractInsnNode> resolve(ClassNode node)
|
||||||
|
{
|
||||||
|
List<AbstractInsnNode> instructions = Sequence.realInstructions(this.in.resolve(node));
|
||||||
|
List<List<AbstractInsnNode>> matches = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int start = 0; start + this.pattern.size() <= instructions.size(); start++) {
|
||||||
|
List<AbstractInsnNode> candidate = instructions.subList(start, start + this.pattern.size());
|
||||||
|
if (this.matches(candidate)) matches.add(candidate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches.size() != 1) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Expected exactly one sequence " + this.pattern + " in '" + node.name + "." + this.in.name() + this.in.desc()
|
||||||
|
+ "', found " + matches.size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return List.copyOf(matches.getFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(ClassNode node) { this.resolve(node); }
|
||||||
|
|
||||||
|
private boolean matches(List<AbstractInsnNode> candidate)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < candidate.size(); i++) {
|
||||||
|
if (!this.pattern.get(i).matches(candidate.get(i))) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The instructions of {@code method} without labels, line numbers and frames (opcode -1). */
|
||||||
|
private static List<AbstractInsnNode> realInstructions(MethodNode method)
|
||||||
|
{
|
||||||
|
List<AbstractInsnNode> instructions = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions) {
|
||||||
|
if (instruction.getOpcode() != -1) instructions.add(instruction);
|
||||||
|
}
|
||||||
|
|
||||||
|
return instructions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One element of a {@link Sequence}: a test for a single instruction. */
|
||||||
|
sealed interface Instruction
|
||||||
|
{
|
||||||
|
boolean matches(AbstractInsnNode instruction);
|
||||||
|
|
||||||
|
/** Any instruction with this opcode, e.g. {@code Opcodes.FSTORE} for a store to any local variable. */
|
||||||
|
record Opcode(int opcode) implements Instruction
|
||||||
|
{
|
||||||
|
public Opcode
|
||||||
|
{
|
||||||
|
if (opcode < 0 || opcode >= Printer.OPCODES.length) throw new IllegalArgumentException("Not an opcode: " + opcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(AbstractInsnNode instruction) { return instruction.getOpcode() == this.opcode; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() { return Printer.OPCODES[this.opcode].toLowerCase(Locale.ROOT); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A constant push of {@code value}: {@code ldc}, or the shorter forms used for small
|
||||||
|
* values ({@code iconst_<n>}, {@code lconst_<n>}, {@code fconst_<n>}, {@code dconst_<n>},
|
||||||
|
* {@code bipush}, {@code sipush}). The boxed type has to match: {@code 0} (an
|
||||||
|
* {@code Integer}) does not match {@code fconst_0}. A class literal is an
|
||||||
|
* {@link org.objectweb.asm.Type}.
|
||||||
|
*/
|
||||||
|
record Constant(Object value) implements Instruction
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean matches(AbstractInsnNode instruction) { return this.value.equals(Constant.valueOf(instruction)); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() { return "constant " + this.value; }
|
||||||
|
|
||||||
|
private static @Nullable Object valueOf(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
int opcode = instruction.getOpcode();
|
||||||
|
|
||||||
|
if (instruction instanceof LdcInsnNode ldc) return ldc.cst;
|
||||||
|
if (opcode == Opcodes.BIPUSH || opcode == Opcodes.SIPUSH) return ((IntInsnNode) instruction).operand;
|
||||||
|
if (opcode >= Opcodes.ICONST_M1 && opcode <= Opcodes.ICONST_5) return opcode - Opcodes.ICONST_0;
|
||||||
|
if (opcode >= Opcodes.LCONST_0 && opcode <= Opcodes.LCONST_1) return (long) (opcode - Opcodes.LCONST_0);
|
||||||
|
if (opcode >= Opcodes.FCONST_0 && opcode <= Opcodes.FCONST_2) return (float) (opcode - Opcodes.FCONST_0);
|
||||||
|
if (opcode >= Opcodes.DCONST_0 && opcode <= Opcodes.DCONST_1) return (double) (opcode - Opcodes.DCONST_0);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A static, virtual, special or interface call to {@code owner.name desc}. */
|
||||||
|
record Invoke(String owner, String name, String desc) implements Instruction
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean matches(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof MethodInsnNode call
|
||||||
|
&& call.owner.equals(this.owner)
|
||||||
|
&& call.name.equals(this.name)
|
||||||
|
&& call.desc.equals(this.desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() { return "invoke '" + this.owner + "." + this.name + this.desc + "'"; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,32 @@ package dev.overlord.zomboidforge.patcher;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.objectweb.asm.tree.ClassNode;
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.InsnList;
|
||||||
|
import org.objectweb.asm.tree.InsnNode;
|
||||||
|
import org.objectweb.asm.tree.IntInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LabelNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LineNumberNode;
|
||||||
import org.objectweb.asm.tree.MethodInsnNode;
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
import org.objectweb.asm.tree.MethodNode;
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
import org.objectweb.asm.tree.VarInsnNode;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.BIPUSH;
|
||||||
|
import static org.objectweb.asm.Opcodes.FCONST_0;
|
||||||
|
import static org.objectweb.asm.Opcodes.FSTORE;
|
||||||
|
import static org.objectweb.asm.Opcodes.ICONST_M1;
|
||||||
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.LCONST_1;
|
||||||
import static org.objectweb.asm.Opcodes.RETURN;
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
|
||||||
class AnchorTest
|
class AnchorTest
|
||||||
@@ -18,6 +35,12 @@ class AnchorTest
|
|||||||
private static final Anchor.Method RUN = new Anchor.Method("run", "()V");
|
private static final Anchor.Method RUN = new Anchor.Method("run", "()V");
|
||||||
private static final Anchor.Call CALL = new Anchor.Call(RUN, "t/Dep", "go", "()V");
|
private static final Anchor.Call CALL = new Anchor.Call(RUN, "t/Dep", "go", "()V");
|
||||||
|
|
||||||
|
private static final Anchor.Sequence FORCED_STORE = new Anchor.Sequence(
|
||||||
|
RUN,
|
||||||
|
new Anchor.Instruction.Constant(1000000.0F),
|
||||||
|
new Anchor.Instruction.Opcode(FSTORE)
|
||||||
|
);
|
||||||
|
|
||||||
private static ClassNode classWithRun(int callsToGo)
|
private static ClassNode classWithRun(int callsToGo)
|
||||||
{
|
{
|
||||||
ClassNode node = new ClassNode();
|
ClassNode node = new ClassNode();
|
||||||
@@ -75,4 +98,98 @@ class AnchorTest
|
|||||||
|
|
||||||
assertDoesNotThrow(() -> AnchorTest.CALL.verify(node));
|
assertDoesNotThrow(() -> AnchorTest.CALL.verify(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@code run()V} with {@code ldc 1000000.0F; <label>; <line number>; fstore 5}, {@code stores} times. */
|
||||||
|
private static ClassNode classWithForcedStores(int stores)
|
||||||
|
{
|
||||||
|
InsnList code = new InsnList();
|
||||||
|
|
||||||
|
for (int i = 0; i < stores; i++) {
|
||||||
|
LabelNode label = new LabelNode();
|
||||||
|
|
||||||
|
code.add(new LdcInsnNode(1000000.0F));
|
||||||
|
code.add(label);
|
||||||
|
code.add(new LineNumberNode(100 + i, label));
|
||||||
|
code.add(new VarInsnNode(FSTORE, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
return AnchorTest.classWithRun(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassNode classWithRun(InsnList code)
|
||||||
|
{
|
||||||
|
ClassNode node = new ClassNode();
|
||||||
|
node.name = "t/Target";
|
||||||
|
|
||||||
|
MethodNode run = new MethodNode(ACC_STATIC, "run", "()V", null, null);
|
||||||
|
run.instructions.add(code);
|
||||||
|
run.visitInsn(RETURN);
|
||||||
|
|
||||||
|
node.methods.add(run);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sequenceAnchorSkipsLabelsAndLineNumbers()
|
||||||
|
{
|
||||||
|
ClassNode node = AnchorTest.classWithForcedStores(1);
|
||||||
|
List<?> match = AnchorTest.FORCED_STORE.resolve(node);
|
||||||
|
|
||||||
|
assertEquals(2, match.size());
|
||||||
|
assertSame(node.methods.getFirst().instructions.get(0), match.get(0));
|
||||||
|
assertSame(node.methods.getFirst().instructions.get(3), match.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sequenceAnchorMatchesExactlyOnce()
|
||||||
|
{
|
||||||
|
assertEquals(
|
||||||
|
"Expected exactly one sequence [constant 1000000.0, fstore] in 't/Target.run()V', found 0",
|
||||||
|
assertThrows(IllegalStateException.class, () -> AnchorTest.FORCED_STORE.verify(AnchorTest.classWithForcedStores(0))).getMessage()
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
assertThrows(IllegalStateException.class, () -> AnchorTest.FORCED_STORE.verify(AnchorTest.classWithForcedStores(2)))
|
||||||
|
.getMessage().endsWith("found 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sequenceAnchorNeedsAnInstruction()
|
||||||
|
{
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new Anchor.Sequence(AnchorTest.RUN));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void constantMatchesShortFormsOfTheSameType()
|
||||||
|
{
|
||||||
|
assertTrue(new Anchor.Instruction.Constant(0.0F).matches(new InsnNode(FCONST_0)));
|
||||||
|
assertTrue(new Anchor.Instruction.Constant(-1).matches(new InsnNode(ICONST_M1)));
|
||||||
|
assertTrue(new Anchor.Instruction.Constant(1L).matches(new InsnNode(LCONST_1)));
|
||||||
|
assertTrue(new Anchor.Instruction.Constant(100).matches(new IntInsnNode(BIPUSH, 100)));
|
||||||
|
assertTrue(new Anchor.Instruction.Constant("text").matches(new LdcInsnNode("text")));
|
||||||
|
|
||||||
|
assertFalse(new Anchor.Instruction.Constant(0).matches(new InsnNode(FCONST_0)));
|
||||||
|
assertFalse(new Anchor.Instruction.Constant(100L).matches(new IntInsnNode(BIPUSH, 100)));
|
||||||
|
assertFalse(new Anchor.Instruction.Constant(0.0F).matches(new InsnNode(RETURN)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void invokeMatchesOwnerNameAndDescriptor()
|
||||||
|
{
|
||||||
|
Anchor.Instruction.Invoke go = new Anchor.Instruction.Invoke("t/Dep", "go", "()V");
|
||||||
|
|
||||||
|
assertTrue(go.matches(new MethodInsnNode(INVOKESTATIC, "t/Dep", "go", "()V", false)));
|
||||||
|
assertTrue(go.matches(new MethodInsnNode(INVOKEVIRTUAL, "t/Dep", "go", "()V", false)));
|
||||||
|
|
||||||
|
assertFalse(go.matches(new MethodInsnNode(INVOKESTATIC, "t/Other", "go", "()V", false)));
|
||||||
|
assertFalse(go.matches(new MethodInsnNode(INVOKESTATIC, "t/Dep", "go", "(I)V", false)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void opcodeRejectsValuesOutsideTheInstructionSet()
|
||||||
|
{
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new Anchor.Instruction.Opcode(-1));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new Anchor.Instruction.Opcode(200));
|
||||||
|
assertEquals("fstore", new Anchor.Instruction.Opcode(FSTORE).toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user