Skip to content

Commit 2f6142e

Browse files
committed
transfomer improvements and fixes
1 parent cff6eb3 commit 2f6142e

6 files changed

Lines changed: 45 additions & 117 deletions

File tree

src/main/java/me/katze225/Obfuscator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private List<ITransformer> buildTransformers() {
101101
transformers.add(new BooleansTransformer());
102102
}
103103
if (settings.isEnabledFlow()) {
104-
transformers.add(new FlowTransformer());
104+
transformers.add(new ExpressionTransformer());
105105
}
106106
if (settings.isEnabledDispatcher()) {
107107
transformers.add(new DispatcherTransformer());

src/main/java/me/katze225/transformer/ITransformer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface ITransformer extends Opcodes {
99
void modify(ClassNode classNode);
10-
void setPrefix(String prefix);
10+
default void setPrefix(String prefix) {};
1111
Random RANDOM = new Random();
1212
}

src/main/java/me/katze225/transformer/impl/FlowTransformer.java renamed to src/main/java/me/katze225/transformer/impl/ExpressionTransformer.java

Lines changed: 36 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
import org.objectweb.asm.tree.*;
77

88

9-
public class FlowTransformer implements ITransformer {
10-
private String prefix;
9+
public class ExpressionTransformer implements ITransformer {
1110

12-
@Override
13-
public void setPrefix(String prefix) {
14-
this.prefix = prefix;
15-
}
1611
private static final int LARGE_METHOD_LIMIT = 4000;
1712

1813
private static boolean isMethodTooLarge(MethodNode method) {
@@ -51,6 +46,34 @@ public void modify(ClassNode classNode) {
5146
}
5247

5348
if (isIfComparisonInsn(insnNode)) {
49+
if (!isClinit) {
50+
AbstractInsnNode prev = previousMeaningful(insnNode);
51+
if (prev != null && isIntConstant(prev)) {
52+
int constantValue = getIntValue(prev);
53+
InsnList replacement = createComplexIntExpression(constantValue);
54+
AbstractInsnNode prevPrev = previousMeaningful(prev);
55+
method.instructions.insertBefore(prev, replacement);
56+
method.instructions.remove(prev);
57+
58+
if (prevPrev != null && isIntConstant(prevPrev)) {
59+
int constantValue2 = getIntValue(prevPrev);
60+
InsnList replacement2 = createComplexIntExpression(constantValue2);
61+
method.instructions.insertBefore(prevPrev, replacement2);
62+
method.instructions.remove(prevPrev);
63+
}
64+
} else if (prev != null) {
65+
AbstractInsnNode prevPrev = previousMeaningful(prev);
66+
if (prevPrev != null && isIntConstant(prevPrev)) {
67+
int constantValue2 = getIntValue(prevPrev);
68+
InsnList replacement2 = createComplexIntExpression(constantValue2);
69+
method.instructions.insertBefore(prevPrev, replacement2);
70+
method.instructions.remove(prevPrev);
71+
}
72+
}
73+
}
74+
}
75+
76+
if (isIfZeroComparisonInsn(insnNode)) {
5477
if (!isClinit) {
5578
AbstractInsnNode prev = previousMeaningful(insnNode);
5679
if (prev != null && isIntConstant(prev)) {
@@ -73,7 +96,7 @@ public void modify(ClassNode classNode) {
7396
AbstractInsnNode prev = previousMeaningful(insnNode);
7497
if (prev != null && isIntConstant(prev)) {
7598
int constantValue = getIntValue(prev);
76-
if (constantValue > 0 && constantValue <= 30000) {
99+
if (constantValue > 0) {
77100
InsnList replacement = createComplexIntExpression(constantValue);
78101
method.instructions.insertBefore(prev, replacement);
79102
method.instructions.remove(prev);
@@ -159,110 +182,22 @@ private boolean isMethodInvocation(AbstractInsnNode node) {
159182
|| op == INVOKESTATIC || op == INVOKEINTERFACE;
160183
}
161184

162-
private boolean isNullCheckInsn(AbstractInsnNode node) {
163-
if (node == null) {
164-
return false;
165-
}
166-
int op = node.getOpcode();
167-
return op == IFNULL || op == IFNONNULL;
168-
}
169-
170-
private boolean isObjectComparisonInsn(AbstractInsnNode node) {
185+
private boolean isIfComparisonInsn(AbstractInsnNode node) {
171186
if (node == null) {
172187
return false;
173188
}
174189
int op = node.getOpcode();
175-
return op == IF_ACMPEQ || op == IF_ACMPNE;
176-
}
177-
178-
private void invertCondition(MethodNode method, AbstractInsnNode jumpInsn) {
179-
JumpInsnNode jump = (JumpInsnNode) jumpInsn;
180-
LabelNode originalTarget = jump.label;
181-
182-
if (!hasCodeBetween(jumpInsn, originalTarget)) {
183-
return;
184-
}
185-
186-
LabelNode newElseLabel = new LabelNode();
187-
int invertedOp = getInvertedIntComparison(jump.getOpcode());
188-
JumpInsnNode newJump = new JumpInsnNode(invertedOp, newElseLabel);
189-
190-
method.instructions.set(jump, newJump);
191-
method.instructions.insert(newJump, newElseLabel);
192-
method.instructions.insert(newElseLabel, new JumpInsnNode(GOTO, originalTarget));
193-
}
194-
195-
private void invertNullCheck(MethodNode method, AbstractInsnNode jumpInsn) {
196-
JumpInsnNode jump = (JumpInsnNode) jumpInsn;
197-
LabelNode originalTarget = jump.label;
198-
199-
if (!hasCodeBetween(jumpInsn, originalTarget)) {
200-
return;
201-
}
202-
203-
LabelNode newElseLabel = new LabelNode();
204-
int invertedOp = jump.getOpcode() == IFNULL ? IFNONNULL : IFNULL;
205-
JumpInsnNode newJump = new JumpInsnNode(invertedOp, newElseLabel);
206-
207-
method.instructions.set(jump, newJump);
208-
method.instructions.insert(newJump, newElseLabel);
209-
method.instructions.insert(newElseLabel, new JumpInsnNode(GOTO, originalTarget));
210-
}
211-
212-
private void invertObjectComparison(MethodNode method, AbstractInsnNode jumpInsn) {
213-
JumpInsnNode jump = (JumpInsnNode) jumpInsn;
214-
LabelNode originalTarget = jump.label;
215-
216-
if (!hasCodeBetween(jumpInsn, originalTarget)) {
217-
return;
218-
}
219-
220-
LabelNode newElseLabel = new LabelNode();
221-
int invertedOp = jump.getOpcode() == IF_ACMPEQ ? IF_ACMPNE : IF_ACMPEQ;
222-
JumpInsnNode newJump = new JumpInsnNode(invertedOp, newElseLabel);
223-
224-
method.instructions.set(jump, newJump);
225-
method.instructions.insert(newJump, newElseLabel);
226-
method.instructions.insert(newElseLabel, new JumpInsnNode(GOTO, originalTarget));
227-
}
228-
229-
private boolean hasCodeBetween(AbstractInsnNode start, LabelNode target) {
230-
AbstractInsnNode current = start.getNext();
231-
while (current != null && current != target) {
232-
if (current.getOpcode() >= 0 && !(current instanceof LabelNode)
233-
&& !(current instanceof LineNumberNode) && !(current instanceof FrameNode)) {
234-
return true;
235-
}
236-
current = current.getNext();
237-
}
238-
return false;
239-
}
240-
241-
private int getInvertedIntComparison(int opcode) {
242-
switch (opcode) {
243-
case IF_ICMPEQ: return IF_ICMPNE;
244-
case IF_ICMPNE: return IF_ICMPEQ;
245-
case IF_ICMPLT: return IF_ICMPGE;
246-
case IF_ICMPGE: return IF_ICMPLT;
247-
case IF_ICMPGT: return IF_ICMPLE;
248-
case IF_ICMPLE: return IF_ICMPGT;
249-
case IFEQ: return IFNE;
250-
case IFNE: return IFEQ;
251-
case IFLT: return IFGE;
252-
case IFGE: return IFLT;
253-
case IFGT: return IFLE;
254-
case IFLE: return IFGT;
255-
default: return opcode;
256-
}
190+
return op == IF_ICMPEQ || op == IF_ICMPNE || op == IF_ICMPLT
191+
|| op == IF_ICMPGE || op == IF_ICMPGT || op == IF_ICMPLE;
257192
}
258193

259-
private boolean isIfComparisonInsn(AbstractInsnNode node) {
194+
private boolean isIfZeroComparisonInsn(AbstractInsnNode node) {
260195
if (node == null) {
261196
return false;
262197
}
263198
int op = node.getOpcode();
264-
return op == IF_ICMPEQ || op == IF_ICMPNE || op == IF_ICMPLT
265-
|| op == IF_ICMPGE || op == IF_ICMPGT || op == IF_ICMPLE;
199+
return op == IFEQ || op == IFNE || op == IFLT
200+
|| op == IFLE || op == IFGT || op == IFGE;
266201
}
267202

268203
private boolean isIntConstant(AbstractInsnNode node) {

src/main/java/me/katze225/transformer/impl/ShuffleTransformer.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
import java.util.Collections;
77

88
public class ShuffleTransformer implements ITransformer {
9-
private String prefix;
10-
11-
@Override
12-
public void setPrefix(String prefix) {
13-
this.prefix = prefix;
14-
}
159

1610
@Override
1711
public void modify(ClassNode classNode) {

src/main/java/me/katze225/utility/BytecodeUtility.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static <T extends AbstractInsnNode> void forEach(InsnList instructions,
2828
AbstractInsnNode[] array = instructions.toArray();
2929
for (AbstractInsnNode node : array) {
3030
if (node.getClass() == type) {
31-
consumer.accept((T) node);
31+
consumer.accept(type.cast(node));
3232
}
3333
}
3434
}

src/main/java/me/katze225/utility/NumberUtility.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88

99
public class NumberUtility {
1010
public static int getInt(AbstractInsnNode ain) {
11-
int op;
12-
if((op = ain.getOpcode()) == ICONST_0) {
11+
if(ain.getOpcode() == ICONST_0) {
1312
return 0;
1413
} else
15-
if((op = ain.getOpcode()) == ICONST_1) {
14+
if(ain.getOpcode() == ICONST_1) {
1615
return 1;
1716
} else
18-
if((op = ain.getOpcode()) == ICONST_2) {
17+
if(ain.getOpcode() == ICONST_2) {
1918
return 2;
2019
} else
21-
if((op = ain.getOpcode()) == ICONST_3) {
20+
if(ain.getOpcode() == ICONST_3) {
2221
return 3;
2322
} else
24-
if((op = ain.getOpcode()) == ICONST_4) {
23+
if(ain.getOpcode() == ICONST_4) {
2524
return 4;
2625
} else
27-
if((op = ain.getOpcode()) == ICONST_5) {
26+
if(ain.getOpcode() == ICONST_5) {
2827
return 5;
2928
} else if(ain instanceof IntInsnNode) {
3029
return ((IntInsnNode)ain).operand;

0 commit comments

Comments
 (0)