Skip to content

Commit fcf4e0b

Browse files
yangzhixuanmbenz89
authored andcommitted
Improve readability of local variable names when reading dex
1 parent c4dd94f commit fcf4e0b

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/main/java/soot/dexpler/DexBody.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.File;
3333
import java.io.IOException;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.Collections;
3637
import java.util.HashMap;
3738
import java.util.HashSet;
@@ -53,7 +54,10 @@
5354
import org.jf.dexlib2.iface.TryBlock;
5455
import org.jf.dexlib2.iface.debug.DebugItem;
5556
import org.jf.dexlib2.iface.instruction.Instruction;
57+
import org.jf.dexlib2.immutable.debug.ImmutableEndLocal;
5658
import org.jf.dexlib2.immutable.debug.ImmutableLineNumber;
59+
import org.jf.dexlib2.immutable.debug.ImmutableRestartLocal;
60+
import org.jf.dexlib2.immutable.debug.ImmutableStartLocal;
5761
import org.jf.dexlib2.util.MethodUtil;
5862
import org.slf4j.Logger;
5963
import org.slf4j.LoggerFactory;
@@ -154,6 +158,31 @@ public class DexBody {
154158
protected final DexEntry<? extends DexFile> dexEntry;
155159
protected final Method method;
156160

161+
/**
162+
* An entry of debug information for a register from the dex file.
163+
*
164+
* @author Zhenghao Hu
165+
*/
166+
protected class RegDbgEntry {
167+
public int startAddress;
168+
public int endAddress;
169+
public int register;
170+
public String name;
171+
public Type type;
172+
public String signature;
173+
174+
public RegDbgEntry(int sa, int ea, int reg, String nam, String ty, String sig) {
175+
this.startAddress = sa;
176+
this.endAddress = ea;
177+
this.register = reg;
178+
this.name = nam;
179+
this.type = DexType.toSoot(ty);
180+
this.signature = sig;
181+
}
182+
}
183+
184+
private final Map<Integer, LinkedList<RegDbgEntry>> localDebugs;
185+
157186
// detect array/instructions overlapping obfuscation
158187
protected List<PseudoInstruction> pseudoInstructionData = new ArrayList<PseudoInstruction>();
159188

@@ -204,6 +233,8 @@ protected DexBody(DexEntry<? extends DexFile> dexFile, Method method, RefType de
204233

205234
instructions = new ArrayList<DexlibAbstractInstruction>();
206235
instructionAtAddress = new HashMap<Integer, DexlibAbstractInstruction>();
236+
localDebugs = new HashMap<Integer, LinkedList<RegDbgEntry>>();
237+
207238
registerLocals = new Local[numRegisters];
208239

209240
extractDexInstructions(code);
@@ -224,6 +255,43 @@ protected DexBody(DexEntry<? extends DexFile> dexFile, Method method, RefType de
224255
continue;
225256
}
226257
ins.setLineNumber(ln.getLineNumber());
258+
} else if (di instanceof ImmutableStartLocal
259+
|| di instanceof ImmutableRestartLocal) {
260+
LinkedList<RegDbgEntry> lds;
261+
int reg, codeAddr;
262+
String type, signature, name;
263+
if (di instanceof ImmutableStartLocal) {
264+
ImmutableStartLocal sl = (ImmutableStartLocal) di;
265+
reg = sl.getRegister();
266+
codeAddr = sl.getCodeAddress();
267+
name = sl.getName();
268+
type = sl.getType();
269+
signature = sl.getSignature();
270+
} else {
271+
ImmutableRestartLocal sl = (ImmutableRestartLocal) di;
272+
reg = sl.getRegister();
273+
codeAddr = sl.getCodeAddress();
274+
name = sl.getName();
275+
type = sl.getType();
276+
signature = sl.getSignature();
277+
}
278+
lds = localDebugs.get(reg);
279+
RegDbgEntry dbgEntry = new RegDbgEntry(codeAddr, -1/* endAddress */, reg, name, type, signature);
280+
if (lds == null) {
281+
localDebugs.put(reg,
282+
new LinkedList<RegDbgEntry>(Collections.singletonList(dbgEntry)));
283+
} else {
284+
lds.add(dbgEntry);
285+
}
286+
} else if (di instanceof ImmutableEndLocal) {
287+
ImmutableEndLocal el = (ImmutableEndLocal) di;
288+
LinkedList<RegDbgEntry> lds = localDebugs.get(el.getRegister());
289+
if (lds == null || lds.isEmpty()) {
290+
// Invalid debug info
291+
continue;
292+
} else {
293+
lds.getLast().endAddress = el.getCodeAddress();
294+
}
227295
}
228296
}
229297

@@ -420,6 +488,10 @@ public Body jimplify(Body b, SootMethod m) {
420488
int thisRegister = numRegisters - numParameterRegisters - 1;
421489

422490
Local thisLocal = jimple.newLocal("$u" + thisRegister, unknownType); // generateLocal(UnknownType.v());
491+
//thisLocal.setType(jBody.getMethod().getDeclaringClass().getType());
492+
if (localDebugs.containsKey(thisRegister)) {
493+
thisLocal.setName("this");
494+
}
423495
jBody.getLocals().add(thisLocal);
424496

425497
registerLocals[thisRegister] = thisLocal;
@@ -458,6 +530,9 @@ public Body jimplify(Body b, SootMethod m) {
458530
}
459531

460532
Local gen = jimple.newLocal(localName, localType);
533+
if (localDebugs.containsKey(parameterRegister)) {
534+
gen.setName(localDebugs.get(parameterRegister).getFirst().name);
535+
}
461536
jBody.getLocals().add(gen);
462537

463538
registerLocals[parameterRegister] = gen;
@@ -478,6 +553,9 @@ public Body jimplify(Body b, SootMethod m) {
478553
// may only use UnknownType here because the local may be reused with a different
479554
// type later (before splitting)
480555
Local g = jimple.newLocal("$u" + parameterRegister, unknownType);
556+
if (localDebugs.containsKey(parameterRegister)) {
557+
g.setName(localDebugs.get(parameterRegister).getFirst().name);
558+
}
481559
jBody.getLocals().add(g);
482560
registerLocals[parameterRegister] = g;
483561
}
@@ -489,6 +567,9 @@ public Body jimplify(Body b, SootMethod m) {
489567

490568
for (int i = 0; i < (numRegisters - numParameterRegisters - (isStatic ? 0 : 1)); i++) {
491569
registerLocals[i] = jimple.newLocal("$u" + i, unknownType);
570+
if (localDebugs.containsKey(i)) {
571+
registerLocals[i].setName(localDebugs.get(i).getFirst().name);
572+
}
492573
jBody.getLocals().add(registerLocals[i]);
493574
}
494575

0 commit comments

Comments
 (0)