-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInstrumentationTest.java
More file actions
151 lines (135 loc) · 6.93 KB
/
Copy pathInstrumentationTest.java
File metadata and controls
151 lines (135 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import com.google.common.collect.Lists;
import org.jf.dexlib2.DexFileFactory;
import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.builder.BuilderInstruction;
import org.jf.dexlib2.builder.MutableMethodImplementation;
import org.jf.dexlib2.builder.instruction.BuilderInstruction11x;
import org.jf.dexlib2.builder.instruction.BuilderInstruction21c;
import org.jf.dexlib2.builder.instruction.BuilderInstruction35c;
import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.MethodImplementation;
import org.jf.dexlib2.iface.instruction.Instruction;
import org.jf.dexlib2.iface.instruction.NarrowLiteralInstruction;
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction;
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction;
import org.jf.dexlib2.immutable.ImmutableClassDef;
import org.jf.dexlib2.immutable.ImmutableMethod;
import org.jf.dexlib2.immutable.reference.ImmutableMethodReference;
import org.jf.dexlib2.immutable.reference.ImmutableStringReference;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class InstrumentationTest {
public static void main(String[] args) throws IOException {
File srcFile = new File(args[0]);
DexFile dexFile = DexFileFactory.loadDexFile(srcFile, 15);
final List<ClassDef> classes = Lists.newArrayList();
for (ClassDef classDef: dexFile.getClasses()) {
List<Method> methods = Lists.newArrayList();
boolean modifiedMethod = false;
for (Method method: classDef.getMethods()) {
MethodImplementation implementation = method.getImplementation();
if (implementation != null) { // && methodNeedsModification(implementation)) {
modifiedMethod = true;
methods.add(new ImmutableMethod(
method.getDefiningClass(),
method.getName(),
method.getParameters(),
method.getReturnType(),
method.getAccessFlags(),
method.getAnnotations(),
modifyMethod(implementation)));
} else {
methods.add(method);
}
}
if (!modifiedMethod) {
classes.add(classDef);
} else {
classes.add(new ImmutableClassDef(
classDef.getType(),
classDef.getAccessFlags(),
classDef.getSuperclass(),
classDef.getInterfaces(),
classDef.getSourceFile(),
classDef.getAnnotations(),
classDef.getFields(),
methods));
}
}
DexFileFactory.writeDexFile(args[1], new DexFile() {
@Nonnull @Override public Set<? extends ClassDef> getClasses() {
return new AbstractSet<ClassDef>() {
@Nonnull @Override public Iterator<ClassDef> iterator() {
return classes.iterator();
}
@Override public int size() {
return classes.size();
}
};
}
});
}
private static boolean methodNeedsModification(@Nonnull MethodImplementation implementation) {
for (Instruction instruction: implementation.getInstructions()) {
System.out.println("opcode: " + Integer.toHexString(instruction.getOpcode().value) + "\tclass name: " + instruction.getClass().getName());
if (instruction instanceof TwoRegisterInstruction) {
TwoRegisterInstruction tri = (TwoRegisterInstruction) instruction;
System.out.println("vA: " + tri.getRegisterA() + "\t\tvB: " + tri.getRegisterB());
}
else if (instruction instanceof OneRegisterInstruction) {
OneRegisterInstruction one = (OneRegisterInstruction) instruction;
System.out.println("vA: " + one.getRegisterA());
}
if (instruction instanceof NarrowLiteralInstruction) {
// a rough heuristic for detecting resource ids
if ((((NarrowLiteralInstruction)instruction).getNarrowLiteral() >> 24) == 0x7f) {
return true;
}
}
}
return false;
}
private static MethodImplementation modifyMethod(@Nonnull MethodImplementation implementation) {
MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
boolean insAdded = false;
for (int i=0; i<instructions.size(); i++) {
Instruction instruction = instructions.get(i);
if (insAdded == false && instruction.getOpcode() == Opcode.INVOKE_STATIC) {
System.out.println("modify");
int register = ((BuilderInstruction35c)instruction).getRegisterC();
mutableImplementation.addInstruction(i++,
new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, register, 0, 0, 0, 0,
new ImmutableMethodReference("Ljava/io/PrintStream;", "print",
Lists.newArrayList("Ljava/lang/String;"), "V")));
insAdded = true;
}
if (instruction instanceof NarrowLiteralInstruction) {
// a rough heuristic for detecting resource ids
if ((((NarrowLiteralInstruction)instruction).getNarrowLiteral() >> 24) == 0x7f) {
int register = ((OneRegisterInstruction)instruction).getRegisterA();
// const-string vN, "R.id.blah"
mutableImplementation.replaceInstruction(i++,
new BuilderInstruction21c(Opcode.CONST_STRING, register,
new ImmutableStringReference("R.id.blah")));
// invoke-static {vN} Lmy/utility/class;->methodToGetId(Ljava/lang/String;)I
mutableImplementation.addInstruction(i++,
new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, register, 0, 0, 0, 0,
new ImmutableMethodReference("Lmy/utility/class;", "methodToGetId",
Lists.newArrayList("Ljava/lang/String;"), "I")));
// move-result vN
mutableImplementation.addInstruction(i++,
new BuilderInstruction11x(Opcode.MOVE_RESULT, register));
}
}
}
return mutableImplementation;
}
}