forked from btraceio/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstrumentorTestBase.java
More file actions
321 lines (273 loc) · 11 KB
/
InstrumentorTestBase.java
File metadata and controls
321 lines (273 loc) · 11 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2009, 2014, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 support;
import com.sun.btrace.SharedSettings;
import static org.junit.Assert.*;
import com.sun.btrace.org.objectweb.asm.ClassReader;
import com.sun.btrace.org.objectweb.asm.ClassWriter;
import com.sun.btrace.org.objectweb.asm.tree.ClassNode;
import com.sun.btrace.runtime.BTraceProbe;
import com.sun.btrace.runtime.BTraceProbeFactory;
import com.sun.btrace.runtime.InstrumentUtils;
import com.sun.btrace.runtime.Instrumentor;
import com.sun.btrace.util.MethodID;
import org.junit.After;
import org.junit.Before;
import org.objectweb.asm.util.CheckClassAdapter;
import org.objectweb.asm.util.TraceClassVisitor;
import sun.misc.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author Jaroslav Bachorik
*/
public abstract class InstrumentorTestBase {
private static final boolean DEBUG = true;
private static Unsafe unsafe;
static {
try {
Field unsafeFld = AtomicInteger.class.getDeclaredField("unsafe");
unsafeFld.setAccessible(true);
unsafe = (Unsafe)unsafeFld.get(null);
} catch (Exception e) {
}
}
protected byte[] originalBC;
protected byte[] transformedBC;
protected byte[] originalTrace;
protected byte[] transformedTrace;
private ClassLoader cl;
private final SharedSettings settings = SharedSettings.GLOBAL;
private final BTraceProbeFactory factory = new BTraceProbeFactory(settings);
@After
public void tearDown() {
cleanup();
}
@Before
public void startup() {
try {
originalBC = null;
transformedBC = null;
originalTrace = null;
transformedTrace = null;
resetClassLoader();
Field lastFld = MethodID.class.getDeclaredField("lastMehodId");
Field mapFld = MethodID.class.getDeclaredField("methodIds");
lastFld.setAccessible(true);
mapFld.setAccessible(true);
AtomicInteger last = (AtomicInteger)lastFld.get(null);
Map map = (Map)mapFld.get(null);
last.set(1);
map.clear();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
protected final void resetClassLoader() {
cl = new ClassLoader(InstrumentorTestBase.class.getClassLoader()) {};
}
protected void cleanup() {
originalBC = null;
transformedBC = null;
originalTrace = null;
transformedTrace = null;
}
protected void load() {
String clzName = new ClassReader(transformedBC).getClassName().replace('.', '/');
String traceName = new ClassReader(transformedTrace).getClassName().replace('.', '/');
unsafe.defineClass(clzName, transformedBC, 0, transformedBC.length, cl, null);
unsafe.defineClass(traceName, transformedTrace, 0, transformedTrace.length, cl, null);
}
protected void checkTransformation(String expected) throws IOException {
org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(transformedBC);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
CheckClassAdapter.verify(cr, false, pw);
if (sw.toString().contains("AnalyzerException")) {
System.err.println(sw.toString());
fail();
}
String diff = diff();
if (DEBUG) {
System.err.println(diff);
}
assertEquals(expected, diff.substring(0, diff.length() > expected.length() ? expected.length() : diff.length()));
}
protected void checkTrace(String expected) throws IOException {
org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(transformedTrace);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
CheckClassAdapter.verify(cr, false, pw);
if (sw.toString().contains("AnalyzerException")) {
System.err.println(sw.toString());
fail();
}
System.err.println("=== HERE");
System.err.println(asmify(transformedTrace));
System.err.println("========");
String diff = diffTrace();
if (DEBUG) {
System.err.println(diff);
}
assertEquals(expected, diff.substring(0, diff.length() > expected.length() ? expected.length() : diff.length()));
}
protected void transform(String traceName) throws IOException {
transform(traceName, false);
}
protected void transform(String traceName, boolean unsafe) throws IOException {
settings.setUnsafe(unsafe);
BTraceProbe btrace = loadTrace(traceName, unsafe);
transformedBC = InstrumentUtils.instrument(originalBC, btrace);
if (transformedBC != null) {
load();
} else {
fail("Failed instrumenting class");
}
System.err.println("==== " + traceName);
}
protected String asmify(byte[] bytecode) {
StringWriter sw = new StringWriter();
TraceClassVisitor acv = new TraceClassVisitor(new PrintWriter(sw));
new org.objectweb.asm.ClassReader(bytecode).accept(acv, ClassReader.SKIP_FRAMES);
return sw.toString();
}
protected String asmify(ClassNode cn) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return asmify(cw.toByteArray());
}
private String diffTrace() throws IOException {
String origCode = asmify(originalTrace);
String transCode = asmify(transformedTrace);
return diff(transCode, origCode);
}
private String diff() throws IOException {
String origCode = asmify(originalBC);
String transCode = asmify(transformedBC);
return diff(transCode, origCode);
}
private String diff(String modified, String orig) throws IOException {
StringBuilder sb = new StringBuilder();
String[] modArr = modified.split("\\n");
String[] orgArr = orig.split("\\n");
// number of lines of each file
int modLen = modArr.length;
int origLen = orgArr.length;
// opt[i][j] = length of LCS of x[i..M] and y[j..N]
int[][] opt = new int[modLen + 1][origLen + 1];
// compute length of LCS and all subproblems via dynamic programming
for (int i = modLen - 1; i >= 0; i--) {
for (int j = origLen - 1; j >= 0; j--) {
if (modArr[i].equals(orgArr[j])) {
opt[i][j] = opt[i + 1][j + 1] + 1;
} else {
opt[i][j] = Math.max(opt[i + 1][j], opt[i][j + 1]);
}
}
}
// recover LCS itself and print out non-matching lines to standard output
int modIndex = 0;
int origIndex = 0;
while (modIndex < modLen && origIndex < origLen) {
if (modArr[modIndex].equals(orgArr[origIndex])) {
modIndex++;
origIndex++;
} else if (opt[modIndex + 1][origIndex] >= opt[modIndex][origIndex + 1]) {
sb.append(modArr[modIndex++].trim()).append('\n');
} else {
origIndex++;
}
}
// dump out one remainder of one string if the other is exhausted
while (modIndex < modLen || origIndex < origLen) {
if (modIndex == modLen) {
origIndex++;
} else if (origIndex == origLen) {
sb.append(orgArr[modIndex++].trim()).append('\n');
}
}
return sb.toString().trim();
}
protected BTraceProbe loadTrace(String name) throws IOException {
return loadTrace(name, false);
}
protected BTraceProbe loadTrace(String name, boolean unsafe) throws IOException {
originalTrace = loadFile("traces/" + name + ".class");
if (DEBUG) {
System.err.println("=== Loaded Trace: " + name + "\n");
System.err.println(asmify(originalTrace));
}
BTraceProbe bcn = factory.createProbe(originalTrace);
// Trace t = new Trace(bcn.getBytecode(), bcn.getOnMethods(), verifier.getReachableCalls(), bcn.name);
transformedTrace = extractBytecode(bcn);
if (DEBUG) {
// writer = InstrumentUtils.newClassWriter();
// InstrumentUtils.accept(new ClassReader(originalTrace), new Preprocessor1(writer));
System.err.println("=== Preprocessed Trace ===");
// System.err.println(asmify(writer.toByteArray()));
System.err.println(asmify(transformedTrace));
}
return bcn;
}
protected byte[] loadTargetClass(String name) throws IOException {
originalBC = loadFile("resources/" + name + ".class");
return originalBC;
}
private byte[] loadFile(String path) throws IOException {
InputStream is = ClassLoader.getSystemResourceAsStream(path);
try {
return loadFile(is);
} finally {
if (is == null) {
System.err.println("Unable to load file: " + path);
} else {
is.close();
}
}
}
private byte[] loadFile(InputStream is) throws IOException {
byte[] result = new byte[0];
byte[] buffer = new byte[1024];
int read = -1;
while ((read = is.read(buffer)) > 0) {
byte[] newresult = new byte[result.length + read];
System.arraycopy(result, 0, newresult, 0, result.length);
System.arraycopy(buffer, 0, newresult, result.length, read);
result = newresult;
}
return result;
}
private byte[] extractBytecode(BTraceProbe bp) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
bp.accept(cw);
return cw.toByteArray();
}
}