-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldPatcher.java
More file actions
259 lines (246 loc) · 9.53 KB
/
Copy pathHelloWorldPatcher.java
File metadata and controls
259 lines (246 loc) · 9.53 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Handle;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.org.objectweb.asm.util.*;
import jdk.internal.org.objectweb.asm.tree.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.lang.reflect.*;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.*;
import java.nio.file.FileSystems;
/**
* Class that patches a java class file taken as an argument,
* replacing all JSR and RET instructions with a valid equivalent
*/
public class HelloWorldPatcher extends ClassVisitor {
private static HashSet<String> targetMethods = new HashSet<String>(); // Set containing each method with the desired opcode
private static byte[] bytecode;
private static ClassNode cn;
public static void main(String[] args) {
ClassReader cr;
Path filePath;
if (args[0]==null) {
System.out.println("Must pass in a class file!");
System.exit(-1);
}
else {
System.out.println("Patching " + args[0] + ".class......");
}
try {
filePath = FileSystems.getDefault().getPath(args[0]+".class");
} catch (Exception e) {
throw new Error("File not found", e);
}
try (FileInputStream fis = new FileInputStream(filePath.toFile())) {
bytecode = readStream(fis, true);
targetMethods = new HashSet<String>();
cr = new ClassReader(bytecode);
viewByteCode(cr, bytecode, "JSR");
/*int offset = cr.header;
int magic = cr.readInt(0);
int minor_version = cr.readUnsignedShort(4);
int major_version = cr.readUnsignedShort(6);
System.out.printf("Major: %d, Minor: %d, Magic: %x\n", major_version, minor_version, magic);*/
cn = replaceOpcodes(cr, bytecode);
} catch (IOException e) {
throw new Error("Error reading file", e);
}
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
//cn.accept(new InvokeDynamicPatcher(Opcodes.ASM5, cw), cr.EXPAND_FRAMES);
cn.accept(cw);
try {
Files.write(filePath, cw.toByteArray(),
StandardOpenOption.WRITE);
} catch (IOException e) {
throw new Error(e);
}
}
public HelloWorldPatcher(int api, ClassWriter cw) {
super(api, cw);
}
// Convert inputstream of class file into byte array
private static byte[] readStream(final InputStream inputStream, final boolean close)
throws IOException {
if (inputStream == null) {
throw new IOException("Class not found");
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] data = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, bytesRead);
}
outputStream.flush();
return outputStream.toByteArray();
} finally {
if (close) {
inputStream.close();
}
}
}
// got this code from: https://www.programcreek.com/java-api-examples/?project_name=brutusin%2Finstrumentation#
// Reads the bytecode of a class and prints it
public static void viewByteCode(ClassReader cr, byte[] bytecode, String opcode) throws IOException { // Originally bytes[] bytecode
System.out.println();
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
final List<MethodNode> mns = cn.methods;
Printer printer = new Textifier();
TraceMethodVisitor mp = new TraceMethodVisitor(printer);
for (MethodNode mn : mns) {
InsnList inList = mn.instructions;
System.out.println(mn.name);
for (int i = 0; i < inList.size(); i++) {
inList.get(i).accept(mp);
StringWriter sw = new StringWriter();
printer.print(new PrintWriter(sw));
printer.getText().clear();
System.out.print(sw.toString());
if (sw.toString().contains(opcode)) {
//System.out.println("Found Opcode: "+ opcode);
targetMethods.add(mn.name);
}
}
}
System.out.println();
}
// Builds map for cloning instructions
public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
if (insn.getType() == 8) {
labelMap.put((LabelNode) insn, new LabelNode());
}
}
return labelMap;
}
/*
* Replaces JST and RET opcodes in the class file
* bytecode: byte array containing the contents of the class file
* cr: ClassReader
* Returns ClassNode with altered instruction list
*/
public static ClassNode replaceOpcodes(ClassReader cr, byte[] bytecode) throws IOException {
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
List<MethodNode> mns = cn.methods;
System.out.println("Class name: " + cn.name + "\nMethods: " + mns.size());
boolean mustExpand = false; // Flag for expanding bytecode when JSRs and RETs overlap
for (MethodNode mn : mns) {
if (targetMethods.contains(mn.name)) {
InsnList inList = mn.instructions;
InsnList newInst = new InsnList();
System.out.println("Method name: " + mn.name + " Instructions: " + inList.size());
// Return label for RET
LabelNode retLb = null;
// Map for cloning instructions
Map<LabelNode, LabelNode> cloneMap = cloneLabels(inList);
// Maps a RET instruction to the label it must return to once converted to GOTO instruction
HashMap<AbstractInsnNode, LabelNode> retLabelMap = new HashMap<AbstractInsnNode, LabelNode>();
// Set of ASTORE instructions that must be removed
HashSet<VarInsnNode> astoreToRemove = new HashSet<VarInsnNode>();
for (int i = 0; i < inList.size(); i++) {
mustExpand = false;
if (inList.get(i).getOpcode() == Opcodes.JSR) {
System.out.println("Replacing JSR...");
LabelNode lb = ((JumpInsnNode)inList.get(i)).label;
// Start from the target label and find the next RET instruction
HashSet<VarInsnNode> astores = new HashSet<VarInsnNode>(); // List of all ASTORE instructions in subroutine
for(int j = inList.indexOf(lb); j < inList.size(); j++) {
if (inList.get(j).getOpcode() == Opcodes.RET) {
if (retLabelMap.containsKey(inList.get(j))) {
System.out.println("Another JSR points to this RET!");
mustExpand = true;
}
else {
retLb = new LabelNode(new Label());
retLabelMap.put(inList.get(j), retLb);
}
// Once RET is found, find and remove associated ASTORE
for (VarInsnNode n : astores) {
if (n.var == ((VarInsnNode)inList.get(j)).var) {
// Mark the matching ASTORE to be ignored
astoreToRemove.add((VarInsnNode)inList.get(j));
}
}
break;
}
else if (inList.get(j).getOpcode() == Opcodes.ASTORE) {
astores.add((VarInsnNode)inList.get(j));
}
}
if (mustExpand) {
System.out.println("Expanding code...");
for (AbstractInsnNode n = inList.get(inList.indexOf(lb)+1); n.getOpcode() != Opcodes.RET; n=n.getNext()) {
newInst.add(n.clone(cloneMap));
}
}
else {
newInst.add(new JumpInsnNode(Opcodes.GOTO, lb));
newInst.add(retLb);
}
}
else if (inList.get(i).getOpcode() == Opcodes.RET) {
System.out.println("Replacing RET...");
// Replace RET with GOTO which jumps to the label corresponding to its associated JSR
newInst.add(new JumpInsnNode(Opcodes.GOTO, retLabelMap.get(inList.get(i))));
}
else if (inList.get(i).getOpcode() == Opcodes.ASTORE) {
if (astoreToRemove.contains(inList.get(i))) {
System.out.println("ASTORE removed");
}
}
else {
newInst.add(inList.get(i));
}
}
inList.clear();
inList.add(newInst);
inList.resetLabels(); // Don't know if this is necessary
}
}
return cn;
}
}