Skip to content

Commit

Permalink
rename InstructionContext to just InsnContext
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Sep 25, 2024
1 parent 9859d2b commit 92777a8
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
/**
* Instruction context. Holds all information relevant to the current instruction.
*/
public class InstructionContext {
public class InsnContext {
private final AbstractInsnNode insn;
private final MethodContext methodContext;

InstructionContext(AbstractInsnNode insn, MethodContext methodContext) {
InsnContext(AbstractInsnNode insn, MethodContext methodContext) {
this.insn = insn;
this.methodContext = methodContext;
}

public InstructionContext of(AbstractInsnNode insn) {
return new InstructionContext(insn, this.methodContext);
public InsnContext of(AbstractInsnNode insn) {
return new InsnContext(insn, this.methodContext);
}

public Frame<OriginalSourceValue> frame() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public MethodNode methodNode() {
return frames;
}

public InstructionContext newInsnContext(AbstractInsnNode insn) {
return new InstructionContext(insn, this);
public InsnContext newInsnContext(AbstractInsnNode insn) {
return new InsnContext(insn, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package uwu.narumi.deobfuscator.api.asm.matcher;

import org.objectweb.asm.tree.AbstractInsnNode;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;

import java.util.ArrayList;
Expand All @@ -25,7 +25,7 @@ public abstract class Match {
* @param insnContext Instruction context
* @return If matches
*/
public boolean matches(InstructionContext insnContext) {
public boolean matches(InsnContext insnContext) {
return this.matchResult(insnContext) != null;
}

Expand All @@ -36,7 +36,7 @@ public boolean matches(InstructionContext insnContext) {
* @param currentMatchContext Match context to merge into
* @return If matches
*/
public boolean matchAndMerge(InstructionContext insnContext, MatchContext currentMatchContext) {
public boolean matchAndMerge(InsnContext insnContext, MatchContext currentMatchContext) {
MatchContext result = this.matchResult(insnContext);
if (result != null) {
currentMatchContext.merge(result);
Expand All @@ -54,7 +54,7 @@ public List<MatchContext> findAllMatches(MethodContext methodContext) {
List<MatchContext> allMatches = new ArrayList<>();

for (AbstractInsnNode insn : methodContext.methodNode().instructions) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);
MatchContext match = this.matchResult(insnContext);
if (match != null) {
allMatches.add(match);
Expand All @@ -67,7 +67,7 @@ public List<MatchContext> findAllMatches(MethodContext methodContext) {
/**
* @return {@link MatchContext} if matches or {@code null} if it does not match
*/
public MatchContext matchResult(InstructionContext insnContext) {
public MatchContext matchResult(InsnContext insnContext) {
// Create MatchContext
MatchContext context = MatchContext.of(insnContext);

Expand All @@ -91,7 +91,7 @@ public MatchContext matchResult(InstructionContext insnContext) {
}

/**
* @see #matches(InstructionContext)
* @see #matches(InsnContext)
*/
protected abstract boolean test(MatchContext context);

Expand Down Expand Up @@ -149,6 +149,6 @@ public interface Transformation {
* @param context Current instruction context
* @return If changed
*/
boolean transform(InstructionContext context);
boolean transform(InsnContext context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.analysis.Frame;
import org.objectweb.asm.tree.analysis.OriginalSourceValue;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -15,17 +15,17 @@
* Immutable match context. After matching process, the context is frozen by {@link MatchContext#freeze()}
*/
public class MatchContext {
private final InstructionContext insnContext;
private final InsnContext insnContext;
private final Map<String, MatchContext> captures;
private final List<AbstractInsnNode> collectedInsns;

private MatchContext(InstructionContext insnContext, Map<String, MatchContext> captures, List<AbstractInsnNode> collectedInsns) {
private MatchContext(InsnContext insnContext, Map<String, MatchContext> captures, List<AbstractInsnNode> collectedInsns) {
this.insnContext = insnContext;
this.captures = captures;
this.collectedInsns = collectedInsns;
}

public static MatchContext of(InstructionContext insnContext) {
public static MatchContext of(InsnContext insnContext) {
return new MatchContext(insnContext, new HashMap<>(), new ArrayList<>());
}

Expand All @@ -36,7 +36,7 @@ public MatchContext freeze() {
/**
* Merges other {@link MatchContext} into this {@link MatchContext}.
*
* @see Match#matchAndMerge(InstructionContext, MatchContext)
* @see Match#matchAndMerge(InsnContext, MatchContext)
*/
void merge(MatchContext other) {
this.captures.putAll(other.captures);
Expand All @@ -49,14 +49,14 @@ void merge(MatchContext other) {
}

/**
* @see InstructionContext#insn()
* @see InsnContext#insn()
*/
public AbstractInsnNode insn() {
return this.insnContext.insn();
}

/**
* @see InstructionContext#frame()
* @see InsnContext#frame()
*/
public Frame<OriginalSourceValue> frame() {
return this.insnContext.frame();
Expand All @@ -65,7 +65,7 @@ public Frame<OriginalSourceValue> frame() {
/**
* Instruction context
*/
public InstructionContext insnContext() {
public InsnContext insnContext() {
return insnContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.objectweb.asm.tree.FrameNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LineNumberNode;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.matcher.Match;
import uwu.narumi.deobfuscator.api.asm.matcher.MatchContext;

Expand Down Expand Up @@ -73,7 +73,7 @@ protected boolean test(MatchContext context) {
return false;
}

InstructionContext currentInsnContext = context.insnContext().of(currentInsn);
InsnContext currentInsnContext = context.insnContext().of(currentInsn);
if (this.skipMatches.stream().anyMatch(match -> match.matches(currentInsnContext))) {
// Skip instruction
currentInsn = currentInsn.getNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;
import uwu.narumi.deobfuscator.api.context.Context;

Expand All @@ -14,7 +14,7 @@
import java.util.stream.Stream;

/**
* Framed instructions stream that gives {@link InstructionContext} and computing all frames for you. Also, this class
* Framed instructions stream that gives {@link InsnContext} and computing all frames for you. Also, this class
* iterate over classes and methods ASYNC, and instructions SYNC. This can really speed up computing frames for methods.
*/
public class FramedInstructionsStream {
Expand Down Expand Up @@ -52,7 +52,7 @@ public FramedInstructionsStream editInstructionsStream(Function<Stream<AbstractI
return this;
}

public void forEach(Consumer<InstructionContext> consumer) {
public void forEach(Consumer<InsnContext> consumer) {
// Iterate over classes in parallel
this.classesStreamModifier.apply(this.context.classes(this.scope).parallelStream())
// Iterate over methods in parallel
Expand All @@ -67,7 +67,7 @@ public void forEach(Consumer<InstructionContext> consumer) {
// Iterate over instructions SYNC
instructionsStreamModifier.apply(Arrays.stream(methodNode.instructions.toArray()))
.forEach(insn -> {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);
// Check if frame exists
if (insnContext.frame() == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.objectweb.asm.tree.analysis.Frame;
import org.objectweb.asm.tree.analysis.OriginalSourceValue;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;
import uwu.narumi.deobfuscator.api.context.Context;
import uwu.narumi.deobfuscator.api.transformer.Transformer;
Expand All @@ -26,7 +26,7 @@ protected void transform(ClassWrapper scope, Context context) throws Exception {
// Find all local variables in use
for (AbstractInsnNode insn : methodNode.instructions.toArray()) {
if ((insn instanceof VarInsnNode && !insn.isVarStore()) || insn instanceof IincInsnNode) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);

Frame<OriginalSourceValue> frame = insnContext.frame();
if (frame == null) return;
Expand All @@ -52,7 +52,7 @@ protected void transform(ClassWrapper scope, Context context) throws Exception {
for (AbstractInsnNode insn : methodNode.instructions.toArray()) {
if (insn instanceof VarInsnNode varInsnNode && insn.isVarStore()) {
if (!varStoresInUse.contains(varInsnNode)) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);

// Pop the value from the stack
insnContext.pop(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.analysis.OriginalSourceValue;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.context.Context;
import uwu.narumi.deobfuscator.api.helper.FramedInstructionsStream;
import uwu.narumi.deobfuscator.api.transformer.Transformer;
Expand Down Expand Up @@ -39,7 +39,7 @@ protected void transform(ClassWrapper scope, Context context) throws Exception {
* @param insnContext Instructon context
* @return If removed
*/
private boolean tryRemovePop(InstructionContext insnContext) {
private boolean tryRemovePop(InsnContext insnContext) {
AbstractInsnNode insn = insnContext.insn();
OriginalSourceValue firstValue = insnContext.frame().getStack(insnContext.frame().getStackSize() - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,35 @@
// TODO: Will probably shit itself
public class TryCatchRepairTransformer extends Transformer {

private boolean changed = false;

@Override
protected void transform(ClassWrapper scope, Context context) throws Exception {
context.classes(scope).stream()
.flatMap(classWrapper -> classWrapper.methods().stream())
.forEach(
methodNode -> {
this.changed |= methodNode.tryCatchBlocks.removeIf(
tbce -> {
if (tbce.start.equals(tbce.end)
|| tbce.start.equals(tbce.handler)
|| tbce.end.equals(tbce.handler)) return true;

LabelNode start = tbce.start;
LabelNode handler = tbce.handler;
LabelNode end = tbce.end;

if (methodNode.instructions.indexOf(start) == -1
|| methodNode.instructions.indexOf(handler) == -1
|| methodNode.instructions.indexOf(end) == -1) return true;
else if (end.getNext() != null
&& end.getNext().getNext() != null
&& end.getNext().getOpcode() == ACONST_NULL
&& end.getNext().getNext().getOpcode() == ATHROW) return true;
else
return methodNode.instructions.indexOf(start)
>= methodNode.instructions.indexOf(handler)
|| methodNode.instructions.indexOf(start)
>= methodNode.instructions.indexOf(end)
|| methodNode.instructions.indexOf(handler)
<= methodNode.instructions.indexOf(end);
});

this.changed |= methodNode.exceptions.removeIf(
exception ->
methodNode.tryCatchBlocks.stream()
.noneMatch(tbce -> tbce.type != null && tbce.type.equals(exception)));
});

if (changed) {
this.markChange();
}
context.classes(scope).forEach(classWrapper -> classWrapper.methods().forEach(methodNode -> {
methodNode.tryCatchBlocks.removeIf(tryCatchBlock -> {
LabelNode start = tryCatchBlock.start;
LabelNode handler = tryCatchBlock.handler;
LabelNode end = tryCatchBlock.end;

if (start.equals(end) || start.equals(handler) || end.equals(handler)) {
// Try-catch has overlapping labels. Remove it.
markChange();
return true;
}

// Check if try-catch labels exist
if (methodNode.instructions.indexOf(start) == -1 || methodNode.instructions.indexOf(handler) == -1 || methodNode.instructions.indexOf(end) == -1) {
return true;
}

// Check if try-catch labels are in the correct order
return methodNode.instructions.indexOf(start) >= methodNode.instructions.indexOf(handler)
|| methodNode.instructions.indexOf(start) >= methodNode.instructions.indexOf(end)
|| methodNode.instructions.indexOf(handler) <= methodNode.instructions.indexOf(end);
});

// Remove exceptions that are already caught by try-catch blocks
methodNode.exceptions.removeIf(exception ->
methodNode.tryCatchBlocks.stream().noneMatch(tryCatch -> tryCatch.type != null && tryCatch.type.equals(exception))
);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;
import uwu.narumi.deobfuscator.api.asm.matcher.Match;
import uwu.narumi.deobfuscator.api.asm.matcher.MatchContext;
Expand Down Expand Up @@ -148,7 +148,7 @@ private void decryptEncryptedLongs(Context context, ClassWrapper classWrapper) {
MethodContext methodContext = MethodContext.framed(classWrapper, clinit);

for (AbstractInsnNode insn : clinit.instructions) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);
if (insnContext.frame() == null) return;

MatchContext result = DECRYPT_LONG_MATCHER.matchResult(insnContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.VarInsnNode;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;
import uwu.narumi.deobfuscator.api.asm.matcher.Match;
import uwu.narumi.deobfuscator.api.asm.matcher.MatchContext;
Expand Down Expand Up @@ -123,7 +123,7 @@ private List<Type> decomposeObjectArrayToTypes(MethodContext methodContext, Map<

// Find all casts from that Object array
for (AbstractInsnNode insn : methodContext.methodNode().instructions.toArray()) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);

MatchContext matchContext = OBJECT_ARRAY_VAR_USAGE.matchResult(insnContext);
if (matchContext == null) continue;
Expand Down Expand Up @@ -170,7 +170,7 @@ private List<Type> decomposeObjectArrayToTypes(MethodContext methodContext, Map<
private boolean removeObjectArrayAccess(MethodContext methodContext) {
// Remove all object array accesses
for (AbstractInsnNode insn : methodContext.methodNode().instructions.toArray()) {
InstructionContext insnContext = methodContext.newInsnContext(insn);
InsnContext insnContext = methodContext.newInsnContext(insn);

MatchContext matchContext = OBJECT_ARRAY_POP.matchResult(insnContext);
if (matchContext == null) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.objectweb.asm.tree.MethodInsnNode;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.asm.InstructionContext;
import uwu.narumi.deobfuscator.api.asm.InsnContext;
import uwu.narumi.deobfuscator.api.asm.MethodContext;
import uwu.narumi.deobfuscator.api.asm.MethodRef;
import uwu.narumi.deobfuscator.api.asm.matcher.Match;
Expand Down Expand Up @@ -73,7 +73,7 @@ protected void transform(ClassWrapper scope, Context context) throws Exception {
MethodContext framelessContext = MethodContext.frameless(classWrapper, methodNode);

methodNode.tryCatchBlocks.removeIf(tryBlock -> {
InstructionContext start = framelessContext.newInsnContext(tryBlock.handler.getNext());
InsnContext start = framelessContext.newInsnContext(tryBlock.handler.getNext());
MatchContext result = INVOKE_AND_RETURN.matchResult(start);
if (result != null) {
MethodRef methodRef = MethodRef.of((MethodInsnNode) result.insn());
Expand Down

0 comments on commit 92777a8

Please sign in to comment.