diff --git a/patcher/src/main/java/dev/overlord/zomboidforge/patcher/Anchor.java b/patcher/src/main/java/dev/overlord/zomboidforge/patcher/Anchor.java index 6bad840..b24d152 100644 --- a/patcher/src/main/java/dev/overlord/zomboidforge/patcher/Anchor.java +++ b/patcher/src/main/java/dev/overlord/zomboidforge/patcher/Anchor.java @@ -1,12 +1,19 @@ 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.ClassNode; +import org.objectweb.asm.tree.IntInsnNode; +import org.objectweb.asm.tree.LdcInsnNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.util.Printer; import java.util.ArrayList; import java.util.List; +import java.util.Locale; /** * 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); } } + + /** + * 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 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 resolve(ClassNode node) + { + List instructions = Sequence.realInstructions(this.in.resolve(node)); + List> matches = new ArrayList<>(); + + for (int start = 0; start + this.pattern.size() <= instructions.size(); start++) { + List 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 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 realInstructions(MethodNode method) + { + List 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_}, {@code lconst_}, {@code fconst_}, {@code dconst_}, + * {@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 + "'"; } + } + } } diff --git a/patcher/src/test/java/dev/overlord/zomboidforge/patcher/AnchorTest.java b/patcher/src/test/java/dev/overlord/zomboidforge/patcher/AnchorTest.java index ca30463..bdcb240 100644 --- a/patcher/src/test/java/dev/overlord/zomboidforge/patcher/AnchorTest.java +++ b/patcher/src/test/java/dev/overlord/zomboidforge/patcher/AnchorTest.java @@ -2,15 +2,32 @@ package dev.overlord.zomboidforge.patcher; import org.junit.jupiter.api.Test; 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.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.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.assertTrue; 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.INVOKEVIRTUAL; +import static org.objectweb.asm.Opcodes.LCONST_1; import static org.objectweb.asm.Opcodes.RETURN; class AnchorTest @@ -18,6 +35,12 @@ class AnchorTest 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.Sequence FORCED_STORE = new Anchor.Sequence( + RUN, + new Anchor.Instruction.Constant(1000000.0F), + new Anchor.Instruction.Opcode(FSTORE) + ); + private static ClassNode classWithRun(int callsToGo) { ClassNode node = new ClassNode(); @@ -75,4 +98,98 @@ class AnchorTest assertDoesNotThrow(() -> AnchorTest.CALL.verify(node)); } + + /** {@code run()V} with {@code ldc 1000000.0F;