-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreverifier.java
More file actions
269 lines (256 loc) · 9.67 KB
/
Copy pathPreverifier.java
File metadata and controls
269 lines (256 loc) · 9.67 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
260
261
262
263
264
265
266
267
268
269
/*
* 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.
*/
package jdk.internal.vm;
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.io.File;
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;
import java.nio.file.LinkOption;
/**
* Patches a java class file taken as an argument, replacing all JSR and RET instructions with a valid equivalent
* JSR and RET instructions exist in pairs, usually with an ASTORE_X at the top of the subroutine containing
* the RET. These instructions are deprecated and must be replaced with valid bytecodes such as GOTO.
*/
public class Preverifier extends ClassVisitor {
//private static HashSet<String> targetMethods = new HashSet<String>(); // Set containing each method with the desired opcode
private static byte[] bytecode; // Contents of the class file
private static ClassNode cn;
private static String fileName;
// public static void main(String[] args) {
// patch(args);
// }
/**
* Reads class file, locates all JSR/RET instructions, and writes new class file
* with new valid instructions
*/
public static byte[] patch(String [] args) {
ClassReader cr;
Path filePath;
if (args.length == 0 || args[0] == null) {
System.out.println("Must pass in a class file");
System.exit(1);
}
else {
if (args[0].contains("/")) {
fileName = args[0].substring(args[0].lastIndexOf("/"));
}
else {
fileName = args[0];
}
System.out.println("Patching " + fileName + ".class......");
}
try {
filePath = Path.of(args[0]+".class");
bytecode = Files.readAllBytes(filePath);
cr = new ClassReader(bytecode);
cn = replaceOpcodes(cr, bytecode);
} catch (Exception e) {
System.out.println(fileName+".class");
throw new Error("File not found", e);
}
//ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
// try {
// Path tmpDir;
// if (!Files.exists(Path.of("/tmp/preverifier/"))) {
// tmpDir = Files.createDirectory(Path.of("/tmp/preverifier/"));
// }
// else {
// tmpDir = Path.of("/tmp/preverifier/");
// }
// Path tmpFile = Path.of(tmpDir.toString() + fileName + ".class");
// Files.write(tmpFile, cw.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
// } catch (IOException e) {
// throw new Error("Cannot write file", e);
// }
return cw.toByteArray();
}
/**
* Constructor
*/
public Preverifier(int api, ClassWriter cw) {
super(api, cw);
}
/**
* Builds map for cloning instructions
*/
public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
HashMap<LabelNode, LabelNode> labelMap = new HashMap<>();
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 {
// Create classnode to view methods and instructions
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
List<MethodNode> mns = cn.methods;
boolean mustExpand = false; // Flag for expanding bytecode when JSRs and RETs overlap
System.out.println("Class name: " + cn.name + "\nMethods: " + mns.size());
for (MethodNode mn : mns) {
// for (TryCatchBlockNode s : mn.tryCatchBlocks) {
// System.out.println(s);
// }
InsnList inList = mn.instructions;
// New list of instructions that should replace the previous list
InsnList newInst = new InsnList();
// 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<>();
// Set of ASTORE instructions that must be removed
HashSet<VarInsnNode> astoreToRemove = new HashSet<>();
boolean hasJSR = false;
System.out.println("Method name: " + mn.name + " Instructions: " + inList.size());
for (int i = 0; i < inList.size(); i++) {
mustExpand = false;
// JSR instructions are replaced with GOTO to the same label
// A new label is added after the new GOTO that the associated RET will return to
if (inList.get(i).getOpcode() == Opcodes.JSR || inList.get(i).getOpcode() == 201) {
hasJSR = true;
boolean hasRet = false;
System.out.println("Replacing JSR...");
// Extract the operator from JSR
LabelNode lb = ((JumpInsnNode)inList.get(i)).label;
// List of all ASTORE instructions in subroutine
HashSet<VarInsnNode> astores = new HashSet<>();
// Start from the target label and find the next RET instruction
for(int j = inList.indexOf(lb); j < inList.size(); j++) {
if (inList.get(j).getOpcode() == Opcodes.RET) {
hasRet = true;
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 associated ASTORE
for (VarInsnNode n : astores) {
if (n.var == ((VarInsnNode)inList.get(j)).var) {
astoreToRemove.add(n);
}
}
break;
}
// Gather all the ASTORE instructions so you don't have to iterate over the whole subroutine again
else if (inList.get(j).getOpcode() == Opcodes.ASTORE) {
astores.add((VarInsnNode)inList.get(j));
}
}
if (!hasRet) {
throw new Error("Verifier Error. JSR has no matching RET");
}
if (mustExpand) {
System.out.println("Expanding code...");
for (AbstractInsnNode n = inList.get(inList.indexOf(lb)+2); n.getOpcode() != Opcodes.RET; n=n.getNext()) {
// If there is a JSR in the code to be copied, replace it with the subroutine it points to
if (n.getOpcode() == Opcodes.JSR) {
System.out.println("Replacing nested JSR");
LabelNode nestedLb = ((JumpInsnNode)n).label;
for (AbstractInsnNode m = inList.get(inList.indexOf(nestedLb)+2); m.getOpcode() != Opcodes.RET; m=m.getNext()) {
System.out.println("Insn: " + m.getOpcode());
newInst.add(m.clone(cloneMap));
}
}
else {
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
if (!retLabelMap.containsKey(inList.get(i))) {
throw new Error("Verifier Error. RET has no matching 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));
}
}
else {
newInst.add(inList.get(i));
}
}
if (astoreToRemove.isEmpty() && hasJSR) {
throw new Error("Verifier Error");
}
// Replace instructions in the method
inList.clear();
inList.add(newInst);
inList.resetLabels(); // Don't know if this is necessary
}
return cn;
}
}