From 98b3b7cb3b371550cebca0563d5384f3c626cb57 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Fri, 5 Feb 2021 14:58:42 +0100 Subject: [PATCH] Implement try-catch-finally injection --- src/dev/w1zzrd/asm/Combine.java | 15 ++++++++++----- src/dev/w1zzrd/asm/GraftSource.java | 4 +--- src/dev/w1zzrd/asm/analysis/FrameState.java | 13 +++++-------- src/dev/w1zzrd/asm/signature/TypeSignature.java | 4 +--- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/dev/w1zzrd/asm/Combine.java b/src/dev/w1zzrd/asm/Combine.java index b5484e5..57788e4 100644 --- a/src/dev/w1zzrd/asm/Combine.java +++ b/src/dev/w1zzrd/asm/Combine.java @@ -11,8 +11,6 @@ import jdk.internal.org.objectweb.asm.Handle; import jdk.internal.org.objectweb.asm.Opcodes; import jdk.internal.org.objectweb.asm.Type; import jdk.internal.org.objectweb.asm.tree.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; @@ -134,6 +132,10 @@ public class Combine { // Recompute maximum stack size resolution.node.maxStack = Math.max(resolution.node.maxStack, extension.maxStack); + // Merge try-catch blocks + resolution.node.tryCatchBlocks.addAll(extension.tryCatchBlocks); + // Exception list not merged to maintain original signature + finishGrafting(extension, source); } @@ -155,6 +157,9 @@ public class Combine { for (int i = 0; i < sig.getArgCount(); ++i) adjustArgument(target, getVarAt(target.localVariables, i), true, false); + target.tryCatchBlocks.addAll(extension.tryCatchBlocks); + // Exception list not merged to maintain original signature + finishGrafting(extension, source); } @@ -750,7 +755,7 @@ public class Combine { * @return The located method node * @throws MethodNodeResolutionException If no method node matching the given description could be found */ - protected final @NotNull MethodNode checkMethodExists(String name, MethodSignature descriptor) { + protected final MethodNode checkMethodExists(String name, MethodSignature descriptor) { final MethodNode target = findMethodNode(name, descriptor); if (target == null) @@ -768,7 +773,7 @@ public class Combine { * @param desc Descriptor of the method node to find * @return A matching {@link MethodNode} if one exists, else null */ - protected @Nullable MethodNode findMethodNode(String name, MethodSignature desc) { + protected MethodNode findMethodNode(String name, MethodSignature desc) { return target.methods .stream() .filter(it -> it.name.equals(name) && new MethodSignature(it.desc).equals(desc)) @@ -838,7 +843,7 @@ public class Combine { } - protected static @Nullable LabelNode findLabelBeforeReturn(AbstractInsnNode start, INodeTraversal traverse) { + protected static LabelNode findLabelBeforeReturn(AbstractInsnNode start, INodeTraversal traverse) { for (AbstractInsnNode cur = start; cur != null; cur = traverse.traverse(cur)) if (cur instanceof LabelNode) // Traversal hit label return (LabelNode) cur; diff --git a/src/dev/w1zzrd/asm/GraftSource.java b/src/dev/w1zzrd/asm/GraftSource.java index b613f2e..4eb3214 100644 --- a/src/dev/w1zzrd/asm/GraftSource.java +++ b/src/dev/w1zzrd/asm/GraftSource.java @@ -6,8 +6,6 @@ import jdk.internal.org.objectweb.asm.tree.AnnotationNode; import jdk.internal.org.objectweb.asm.tree.ClassNode; import jdk.internal.org.objectweb.asm.tree.FieldNode; import jdk.internal.org.objectweb.asm.tree.MethodNode; -import org.jetbrains.annotations.Nullable; - import java.util.*; import java.util.stream.Collectors; @@ -91,7 +89,7 @@ public final class GraftSource { return getInjectedMethod(name, desc) != null; } - public @Nullable MethodNode getInjectedMethod(String name, String desc) { + public MethodNode getInjectedMethod(String name, String desc) { return methodAnnotations .entrySet() .stream() diff --git a/src/dev/w1zzrd/asm/analysis/FrameState.java b/src/dev/w1zzrd/asm/analysis/FrameState.java index 4f68a92..857294d 100644 --- a/src/dev/w1zzrd/asm/analysis/FrameState.java +++ b/src/dev/w1zzrd/asm/analysis/FrameState.java @@ -7,9 +7,6 @@ import jdk.internal.org.objectweb.asm.Label; import jdk.internal.org.objectweb.asm.Opcodes; import jdk.internal.org.objectweb.asm.Type; import jdk.internal.org.objectweb.asm.tree.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -192,7 +189,7 @@ public class FrameState { * @param node {@link LabelNode} to find jumps referencing * @return A jump instruction node earlier in the instruction list or null if none could be found */ - private static @Nullable JumpInsnNode findEarliestJump(LabelNode node) { + private static JumpInsnNode findEarliestJump(LabelNode node) { JumpInsnNode jump = null; // Traverse backward until we hit the beginning of the list @@ -643,7 +640,7 @@ public class FrameState { private static List getOpsByComplexity( boolean complexPush, boolean complexPop, - @Nullable Predicate insnP + Predicate insnP ) { ArrayList opcodes = new ArrayList<>(); @@ -687,9 +684,9 @@ public class FrameState { * @return Negative values for instructions previous to the current instruction, positive values for instructions * after the current instruction. Null if instruction could not be found */ - private static @Nullable Integer relativeIndexOf( - @NotNull AbstractInsnNode current, - @NotNull AbstractInsnNode find + private static Integer relativeIndexOf( + AbstractInsnNode current, + AbstractInsnNode find ) { // Check backward int idx = 0; diff --git a/src/dev/w1zzrd/asm/signature/TypeSignature.java b/src/dev/w1zzrd/asm/signature/TypeSignature.java index e8ece33..89dc1c7 100644 --- a/src/dev/w1zzrd/asm/signature/TypeSignature.java +++ b/src/dev/w1zzrd/asm/signature/TypeSignature.java @@ -2,8 +2,6 @@ package dev.w1zzrd.asm.signature; import dev.w1zzrd.asm.exception.SignatureInstanceMismatchException; import dev.w1zzrd.asm.exception.TypeSignatureParseException; -import org.jetbrains.annotations.Nullable; - import java.util.Arrays; import java.util.Objects; @@ -86,7 +84,7 @@ public class TypeSignature { * @param primitive Primitive type internal name (V, J or D for Top types) * @param isTop Whether or not this is a Top type (only valid for 64-bit types J and D or as delimiter type V) */ - public TypeSignature(@Nullable Character primitive, boolean isTop) { + public TypeSignature(Character primitive, boolean isTop) { if (primitive != null) { switch (Character.toUpperCase(primitive)) { case 'J':