Skip to content

Commit 4b18068

Browse files
committed
fix: Remove opaque predicate field assignments
From openrs2 upstream, author sophxm: The previous implementation removes flow obstructor accesses, but not assignments (which occur occasionally in older client builds).
1 parent 4d03151 commit 4b18068

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/main/java/rs/lostcity/deob/bytecode/transform/openrs2/OpaquePredicateTransformer.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ public class OpaquePredicateTransformer extends Transformer {
2424
"""
2525
);
2626
private final InsnMatcher OPAQUE_PREDICATE_MATCHER = InsnMatcher.compile("(GETSTATIC | ILOAD) (IFEQ | IFNE)");
27+
private final InsnMatcher PUT_MATCHER = InsnMatcher.compile("ICONST PUTSTATIC");
2728
private final InsnMatcher STORE_MATCHER = InsnMatcher.compile("GETSTATIC ISTORE");
2829

2930
private final Set<String> flowObfuscators = new HashSet<>();
31+
private int assignments = 0;
3032
private int opaquePredicates = 0;
31-
private int stores = 0;
3233

3334
@Override
3435
public void preTransform(List<ClassNode> classes) {
3536
flowObfuscators.clear();
3637
opaquePredicates = 0;
37-
stores = 0;
38+
assignments = 0;
3839

3940
for (ClassNode clazz : classes) {
4041
for (MethodNode method : clazz.methods) {
@@ -100,8 +101,13 @@ private boolean isOpaquePredicate(MethodNode method, List<AbstractInsnNode> matc
100101
return false;
101102
}
102103

104+
private boolean isRedundantPut(List<AbstractInsnNode> match) {
105+
FieldInsnNode putstatic = (FieldInsnNode) match.get(1);
106+
return isFlowObstructor(putstatic);
107+
}
108+
103109
private boolean isRedundantStore(List<AbstractInsnNode> match) {
104-
FieldInsnNode getstatic = (FieldInsnNode) match.getFirst();
110+
FieldInsnNode getstatic = (FieldInsnNode) match.get(0);
105111
return isFlowObstructor(getstatic);
106112
}
107113

@@ -126,10 +132,19 @@ public boolean transformCode(List<ClassNode> classes, ClassNode clazz, MethodNod
126132
}
127133
}
128134

135+
// remove redundant stores
129136
for (List<AbstractInsnNode> match : this.STORE_MATCHER.match(method.instructions)) {
130137
if (isRedundantStore(match)) {
131138
match.forEach(method.instructions::remove);
132-
stores++;
139+
assignments++;
140+
}
141+
}
142+
143+
// remove redundant field assignments
144+
for (List<AbstractInsnNode> match : this.PUT_MATCHER.match(method.instructions)) {
145+
if (isRedundantPut(match)) {
146+
match.forEach(method.instructions::remove);
147+
assignments++;
133148
}
134149
}
135150

@@ -138,6 +153,6 @@ public boolean transformCode(List<ClassNode> classes, ClassNode clazz, MethodNod
138153

139154
@Override
140155
public void postTransform(List<ClassNode> classes) {
141-
System.out.println("Removed " + opaquePredicates + " opaque predicates and " + stores + " redundant stores");
156+
System.out.println("Removed " + opaquePredicates + " opaque predicates and " + assignments + " redundant assignments");
142157
}
143158
}

0 commit comments

Comments
 (0)