|
| 1 | +package uwu.narumi.deobfuscator.core.other.impl.clean.peephole; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Nullable; |
| 4 | +import org.objectweb.asm.tree.AbstractInsnNode; |
| 5 | +import org.objectweb.asm.tree.MethodNode; |
| 6 | +import org.objectweb.asm.tree.analysis.Frame; |
| 7 | +import org.objectweb.asm.tree.analysis.OriginalSourceValue; |
| 8 | +import uwu.narumi.deobfuscator.api.asm.InsnContext; |
| 9 | +import uwu.narumi.deobfuscator.api.asm.MethodContext; |
| 10 | +import uwu.narumi.deobfuscator.api.transformer.Transformer; |
| 11 | + |
| 12 | +/** |
| 13 | + * This transformer expands DUP-related instructions (DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2) and SWAP |
| 14 | + * by replacing them with clones of the instructions that produced the values on the stack. |
| 15 | + * This simplification is often effective after constant propagation or other optimizations have made |
| 16 | + * the producers constant values (e.g., LDC, BIPUSH). |
| 17 | + * |
| 18 | + * <p>Example 1: |
| 19 | + * |
| 20 | + * <p>Input: |
| 21 | + * <pre> |
| 22 | + * bipush 10 |
| 23 | + * dup |
| 24 | + * invokestatic foo (I)V |
| 25 | + * invokestatic bar (I)V |
| 26 | + * </pre> |
| 27 | + * |
| 28 | + * <p>Output: |
| 29 | + * <pre> |
| 30 | + * bipush 10 |
| 31 | + * bipush 10 // cloned value |
| 32 | + * invokestatic foo (I)V |
| 33 | + * invokestatic bar (I)V |
| 34 | + * </pre> |
| 35 | + * |
| 36 | + * Example 2 (DUP_X2 - Form 1: value1, value2, value3 -> value1, value3, value2, value1): |
| 37 | + * |
| 38 | + * <p>Input: |
| 39 | + * <pre> |
| 40 | + * bipush 10 // value3 |
| 41 | + * bipush 20 // value2 |
| 42 | + * bipush 30 // value1 |
| 43 | + * dup_x2 |
| 44 | + * invokestatic someMethod(III)V // Consumes value1, value2, value3 |
| 45 | + * invokestatic anotherMethod(I)V // Consumes original value1 |
| 46 | + * </pre> |
| 47 | + * |
| 48 | + * <p>Output: |
| 49 | + * <pre> |
| 50 | + * bipush 10 // value3 |
| 51 | + * bipush 20 // value2 |
| 52 | + * bipush 30 // value1 |
| 53 | + * pop |
| 54 | + * pop |
| 55 | + * pop |
| 56 | + * bipush 30 // cloned value1 |
| 57 | + * bipush 10 // cloned value3 |
| 58 | + * bipush 20 // cloned value2 |
| 59 | + * bipush 30 // cloned value1 |
| 60 | + * invokestatic someMethod(III)V |
| 61 | + * invokestatic anotherMethod(I)V |
| 62 | + * </pre> |
| 63 | + * |
| 64 | + */ |
| 65 | +public class ExpandDupsTransformer extends Transformer { |
| 66 | + @Override |
| 67 | + protected void transform() throws Exception { |
| 68 | + scopedClasses().forEach(classWrapper -> classWrapper.methods().forEach(methodNode -> { |
| 69 | + MethodContext methodContext = MethodContext.of(classWrapper, methodNode); |
| 70 | + |
| 71 | + for (AbstractInsnNode insn : methodNode.instructions.toArray()) { |
| 72 | + // Try to expand DUP-related instructions |
| 73 | + boolean expanded = expandDup(methodContext, insn); |
| 74 | + |
| 75 | + if (expanded) { |
| 76 | + methodNode.instructions.remove(insn); |
| 77 | + markChange(); |
| 78 | + } |
| 79 | + } |
| 80 | + })); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Expands DUP-related instructions in a method's bytecode. |
| 85 | + * |
| 86 | + * @return Is expanded |
| 87 | + */ |
| 88 | + // This is a mess XD |
| 89 | + private boolean expandDup(MethodContext methodContext, AbstractInsnNode insn) { |
| 90 | + MethodNode methodNode = methodContext.methodNode(); |
| 91 | + InsnContext insnContext = methodContext.at(insn); |
| 92 | + Frame<OriginalSourceValue> frame = insnContext.frame(); |
| 93 | + if (frame == null) return false; |
| 94 | + |
| 95 | + if (insn.getOpcode() == DUP) { |
| 96 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 97 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 98 | + if (producer1 == null) return false; |
| 99 | + |
| 100 | + // Expand |
| 101 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 102 | + |
| 103 | + return true; |
| 104 | + } else if (insn.getOpcode() == DUP_X1) { |
| 105 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 106 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 107 | + if (producer1 == null) return false; |
| 108 | + |
| 109 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 110 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 111 | + if (producer2 == null) return false; |
| 112 | + |
| 113 | + insnContext.placePops(); |
| 114 | + |
| 115 | + // Expand |
| 116 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 117 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 118 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 119 | + |
| 120 | + return true; |
| 121 | + } else if (insn.getOpcode() == DUP_X2) { |
| 122 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 123 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 124 | + if (producer1 == null) return false; |
| 125 | + |
| 126 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 127 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 128 | + if (producer2 == null) return false; |
| 129 | + |
| 130 | + // Forms |
| 131 | + if (value1.getSize() == 1 && value2.getSize() == 2) { |
| 132 | + insnContext.placePops(); |
| 133 | + |
| 134 | + // Expand |
| 135 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 136 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 137 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 138 | + |
| 139 | + return true; |
| 140 | + } else { |
| 141 | + OriginalSourceValue value3 = frame.getStack(frame.getStackSize() - 3); |
| 142 | + AbstractInsnNode producer3 = checkAndGetProducer(value3); |
| 143 | + if (producer3 == null) return false; |
| 144 | + |
| 145 | + if (value1.getSize() == 1 && value2.getSize() == 1 && value3.getSize() == 1) { |
| 146 | + insnContext.placePops(); |
| 147 | + |
| 148 | + // Expand |
| 149 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 150 | + methodNode.instructions.insert(insn, producer3.clone(null)); |
| 151 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 152 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 153 | + |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + } else if (insn.getOpcode() == DUP2) { |
| 158 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 159 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 160 | + if (producer1 == null) return false; |
| 161 | + |
| 162 | + // Forms |
| 163 | + if (value1.getSize() == 2) { |
| 164 | + // Expand |
| 165 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 166 | + |
| 167 | + return true; |
| 168 | + } else if (value1.getSize() == 1) { |
| 169 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 170 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 171 | + if (producer2 == null) return false; |
| 172 | + |
| 173 | + // Expand |
| 174 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 175 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 176 | + |
| 177 | + return true; |
| 178 | + } |
| 179 | + } else if (insn.getOpcode() == DUP2_X1) { |
| 180 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 181 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 182 | + if (producer1 == null) return false; |
| 183 | + |
| 184 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 185 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 186 | + if (producer2 == null) return false; |
| 187 | + |
| 188 | + // Forms |
| 189 | + if (value1.getSize() == 2 && value2.getSize() == 1) { |
| 190 | + insnContext.placePops(); |
| 191 | + |
| 192 | + // Expand |
| 193 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 194 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 195 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 196 | + |
| 197 | + return true; |
| 198 | + } else { |
| 199 | + OriginalSourceValue value3 = frame.getStack(frame.getStackSize() - 3); |
| 200 | + AbstractInsnNode producer3 = checkAndGetProducer(value3); |
| 201 | + if (producer3 == null) return false; |
| 202 | + |
| 203 | + if (value1.getSize() == 1 && value2.getSize() == 1 && value3.getSize() == 1) { |
| 204 | + insnContext.placePops(); |
| 205 | + |
| 206 | + // Expand |
| 207 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 208 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 209 | + methodNode.instructions.insert(insn, producer3.clone(null)); |
| 210 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 211 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 212 | + |
| 213 | + return true; |
| 214 | + } |
| 215 | + } |
| 216 | + } else if (insn.getOpcode() == DUP2_X2) { |
| 217 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 218 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 219 | + if (producer1 == null) return false; |
| 220 | + |
| 221 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 222 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 223 | + if (producer2 == null) return false; |
| 224 | + |
| 225 | + // Forms |
| 226 | + if (value1.getSize() == 2 && value2.getSize() == 2) { |
| 227 | + insnContext.placePops(); |
| 228 | + |
| 229 | + // Expand |
| 230 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 231 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 232 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 233 | + |
| 234 | + return true; |
| 235 | + } else { |
| 236 | + OriginalSourceValue value3 = frame.getStack(frame.getStackSize() - 3); |
| 237 | + AbstractInsnNode producer3 = checkAndGetProducer(value3); |
| 238 | + if (producer3 == null) return false; |
| 239 | + |
| 240 | + if (value1.getSize() == 1 && value2.getSize() == 1 && value3.getSize() == 2) { |
| 241 | + insnContext.placePops(); |
| 242 | + |
| 243 | + // Expand |
| 244 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 245 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 246 | + methodNode.instructions.insert(insn, producer3.clone(null)); |
| 247 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 248 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 249 | + |
| 250 | + return true; |
| 251 | + } else if (value1.getSize() == 2 && value2.getSize() == 1 && value3.getSize() == 1) { |
| 252 | + insnContext.placePops(); |
| 253 | + |
| 254 | + // Expand |
| 255 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 256 | + methodNode.instructions.insert(insn, producer3.clone(null)); |
| 257 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 258 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 259 | + |
| 260 | + return true; |
| 261 | + } else { |
| 262 | + OriginalSourceValue value4 = frame.getStack(frame.getStackSize() - 4); |
| 263 | + AbstractInsnNode producer4 = checkAndGetProducer(value4); |
| 264 | + if (producer4 == null) return false; |
| 265 | + |
| 266 | + if (value1.getSize() == 1 && value2.getSize() == 2 && value3.getSize() == 1 && value4.getSize() == 1) { |
| 267 | + insnContext.placePops(); |
| 268 | + |
| 269 | + // Expand |
| 270 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 271 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 272 | + methodNode.instructions.insert(insn, producer4.clone(null)); |
| 273 | + methodNode.instructions.insert(insn, producer3.clone(null)); |
| 274 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 275 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 276 | + |
| 277 | + return true; |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } else if (insn.getOpcode() == SWAP) { |
| 282 | + OriginalSourceValue value1 = frame.getStack(frame.getStackSize() - 1); |
| 283 | + AbstractInsnNode producer1 = checkAndGetProducer(value1); |
| 284 | + if (producer1 == null) return false; |
| 285 | + |
| 286 | + OriginalSourceValue value2 = frame.getStack(frame.getStackSize() - 2); |
| 287 | + AbstractInsnNode producer2 = checkAndGetProducer(value2); |
| 288 | + if (producer2 == null) return false; |
| 289 | + |
| 290 | + insnContext.placePops(); |
| 291 | + |
| 292 | + // Swap |
| 293 | + methodNode.instructions.insert(insn, producer2.clone(null)); |
| 294 | + methodNode.instructions.insert(insn, producer1.clone(null)); |
| 295 | + |
| 296 | + return true; |
| 297 | + } |
| 298 | + |
| 299 | + return false; |
| 300 | + } |
| 301 | + |
| 302 | + @Nullable |
| 303 | + private AbstractInsnNode checkAndGetProducer(OriginalSourceValue sourceValue) { |
| 304 | + if (sourceValue.originalSource.isOneWayProduced() && sourceValue.originalSource.getProducer().isConstant()) { |
| 305 | + return sourceValue.originalSource.getProducer(); |
| 306 | + } |
| 307 | + return null; |
| 308 | + } |
| 309 | +} |
0 commit comments