-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathETProxy.java
More file actions
636 lines (556 loc) · 22.2 KB
/
ETProxy.java
File metadata and controls
636 lines (556 loc) · 22.2 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import java.lang.Runtime;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Arrays;
import java.lang.Thread;
import java.io.PrintWriter;
import java.util.concurrent.locks.ReentrantLock;
public class ETProxy {
private static boolean atMain = false;
// Thread local boolean w/ default value false
private static final InstrumentFlag inInstrumentMethod = new InstrumentFlag();
private static final int BUFMAX = 1000;
private static long[] timestampBuffer = new long[BUFMAX];
private static long[] threadIDBuffer = new long[BUFMAX];
// TRACING EVENTS
// Method entry = 1, method exit = 2, object allocation = 3
// object array allocation = 4
// 2D array allocation = 6, put field = 7
// WITNESSES
// get field = 8
private static int[] eventTypeBuffer = new int[BUFMAX];
private static int[] firstBuffer = new int[BUFMAX];
private static int[] secondBuffer = new int[BUFMAX];
private static int[] thirdBuffer = new int[BUFMAX];
private static int[] fourthBuffer = new int[BUFMAX];
private static int[] fifthBuffer = new int[BUFMAX];
private static AtomicInteger ptr = new AtomicInteger();
private static ReentrantLock mx = new ReentrantLock();
private static PrintWriter pw;
// TODO: public static class ShutdownRunnable implements Runnable {
// TODO: // public int[] eventTypeBuffer = ETProxy.eventTypeBuffer;
// TODO: // public int[] firstBuffer = ETProxy.firstBuffer;
// TODO: // public int[] secondBuffer = ETProxy.secondBuffer;
// TODO: // public int[] thirdBuffer = ETProxy.thirdBuffer;
// TODO: // public int[] fourthBuffer = ETProxy.fourthBuffer;
// TODO: // public int[] fifthBuffer = ETProxy.fifthBuffer;
// TODO: // public AtomicInteger ptr = ETProxy.ptr;
// TODO: public void run() {
// TODO: ETProxy.flushBuffer();
// TODO: }
// TODO: };
// TODO: private static Thread shutdownHook = new Thread(new ShutdownRunnable());
static {
try {
pw = new PrintWriter("trace");
} catch (Exception e) {
System.err.println("FNF");
}
}
// static {
// try {
// System.loadLibrary("objectsize");
// System.err.println("object size library loaded");
// } catch (Exception e) {
// System.err.println(e.getMessage());
// System.err.println("Error: can't load libobjectsize.so");
// }
// }
// I hope no one ever creates a 2 gigabyte object
private static native int getObjectSize(Object obj);
// shh.. pretend I didn't do anything bad
// private static Unsafe unsafe = getUnsafe();
// private static Unsafe getUnsafe() {
// try {
// Field f = Unsafe.class.getDeclaredField("theUnsafe");
// f.setAccessible(true);
// return (Unsafe) f.get(null);
// } catch (Exception e) {
// return null;
// }
// }
// See: https://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/
// private static long getObjectSize(Object object) {
// System.err.println(object);
// return unsafe.getAddress(_normalize(unsafe.getInt(object, 4L)) + 12L);
// }
// private static long _normalize(int value) {
// if(value >= 0) return value;
// return (~0L >>> 32) & value;
// }
// Adapted from JNIF test code
private static byte[] getResourceEx(String className, ClassLoader loader) {
java.io.InputStream is;
try {
is = loader.getResourceAsStream(className + ".class");
} catch (Throwable e) {
// System.err.println("Error: getResource for " + className);
// System.err.println("Error: loader: " + loader);
// e.printStackTrace();
return null;
}
java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();
try {
byte[] buffer = new byte[0xFFFF];
for (int len; (len = is.read(buffer)) != -1;) {
os.write(buffer, 0, len);
}
os.flush();
return os.toByteArray();
} catch (Throwable e) {
// System.err.println("Error: getResource for " + className);
// System.err.println("Error: loader: " + loader);
// System.err.println("Error: is: " + is);
// e.printStackTrace();
return null;
}
}
public static byte[] getResource(String className, ClassLoader loader) {
byte[] res = null;
// System.err.println(className + " requested");
if (loader != null) {
res = getResourceEx(className, loader);
}
if (res == null) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
res = getResourceEx(className, cl);
}
return res;
}
// TODO: What was this for?
// public static native void _onMain();
public static void onEntry(int methodID, Object receiver)
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
synchronized(ptr) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr = ptr.getAndIncrement();
firstBuffer[currPtr] = methodID;
secondBuffer[currPtr] = (receiver == null) ? 0 : System.identityHashCode(receiver);
eventTypeBuffer[currPtr] = 1;
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
flushBuffer();
}
}
}
// System.out.println("Method ID: " + methodID);
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void onExit(int methodID)
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
synchronized(ptr) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr = ptr.getAndIncrement();
firstBuffer[currPtr] = methodID;
eventTypeBuffer[currPtr] = 2;
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
flushBuffer();
}
}
}
} finally {
mx.unlock();
}
// System.out.println("Method ID: " + methodID);
inInstrumentMethod.set(false);
}
public static void onObjectAlloc(Object allocdObject, int allocdClassID, int allocSiteID)
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
synchronized(ptr) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr = ptr.getAndIncrement();
firstBuffer[currPtr] = System.identityHashCode(allocdObject);
eventTypeBuffer[currPtr] = 3;
secondBuffer[currPtr] = allocdClassID;
thirdBuffer[currPtr] = allocSiteID;
// I hope no one ever wants a 2 gigabyte (shallow size!) object
// some problem here...
// System.err.println("Class ID: " + allocdClassID);
fourthBuffer[currPtr] = (int) getObjectSize(allocdObject);
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
flushBuffer();
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void onPutField(Object tgtObject, Object srcObject, int fieldID)
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
synchronized(ptr) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr = ptr.getAndIncrement();
firstBuffer[currPtr] = System.identityHashCode(tgtObject);
eventTypeBuffer[currPtr] = 7;
secondBuffer[currPtr] = fieldID;
timestampBuffer[currPtr] = timestamp;
thirdBuffer[currPtr] = System.identityHashCode(srcObject);
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
flushBuffer();
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
// TODO: Why is this commented out?
//
// public static void onPrimitiveArrayAlloc(int atype, int size, Object allocdArray)
// {
// long timestamp = System.nanoTime();
// if (inInstrumentMethod.get()) {
// return;
// } else {
// inInstrumentMethod.set(true);
// }
// while (true) {
// if (ptr.get() < BUFMAX) {
// // wait on ptr to prevent overflow
// int currPtr = ptr.getAndIncrement();
// firstBuffer[currPtr] = System.identityHashCode(allocdArray);
// eventTypeBuffer[currPtr] = 5;
// secondBuffer[currPtr] = atype;
// thirdBuffer[currPtr] = size;
// timestampBuffer[currPtr] = timestamp;
// threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
// break;
// } else {
// synchronized(ptr) {
// if (ptr.get() >= BUFMAX) {
// flushBuffer();
// }
// }
// }
// }
// inInstrumentMethod.set(false);
// }
public static void onArrayAlloc( int allocdClassID,
int size,
Object allocdArray,
int allocSiteID )
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
synchronized(ptr) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr = ptr.getAndIncrement();
firstBuffer[currPtr] = System.identityHashCode(allocdArray);
eventTypeBuffer[currPtr] = 4;
secondBuffer[currPtr] = allocdClassID;
thirdBuffer[currPtr] = size;
fourthBuffer[currPtr] = allocSiteID;
fifthBuffer[currPtr] = getObjectSize(allocdArray);
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
flushBuffer();
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void onMultiArrayAlloc( int dims,
int allocdClassID,
Object[] allocdArray,
int allocSiteID )
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr;
synchronized(ptr) {
currPtr = ptr.getAndIncrement();
}
firstBuffer[currPtr] = System.identityHashCode(allocdArray);
eventTypeBuffer[currPtr] = 6;
secondBuffer[currPtr] = allocdClassID;
thirdBuffer[currPtr] = allocdArray.length;
fourthBuffer[currPtr] = allocSiteID;
fifthBuffer[currPtr] = getObjectSize(allocdArray);
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
if (dims > 2) {
for (int i = 0; i < allocdArray.length; ++i) {
onMultiArrayAlloc(dims - 1, allocdClassID, (Object[]) allocdArray[i], allocSiteID);
}
} else { // dims == 2
for (int i = 0; i < allocdArray.length; ++i) {
onArrayAlloc(allocdClassID, 0, allocdArray[i], allocSiteID);
}
}
break;
} else {
synchronized(ptr) {
if (ptr.get() >= BUFMAX) {
flushBuffer();
}
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void witnessObjectAlive( Object aliveObject,
int classID )
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr;
synchronized(ptr) {
currPtr = ptr.getAndIncrement();
}
// System.err.println("Seems like problem is here...");
// System.err.println("Class being instrumented here (ID): " + classID);
firstBuffer[currPtr] = System.identityHashCode(aliveObject);
// System.err.println("gets here");
eventTypeBuffer[currPtr] = 8;
secondBuffer[currPtr] = classID;
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
synchronized(ptr) {
if (ptr.get() >= BUFMAX) {
flushBuffer();
}
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void onInvokeMethod(Object allocdObject, int allocdClassID, int allocSiteID)
{
if (!atMain) {
return;
}
long timestamp = System.nanoTime();
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
mx.lock();
try {
while (true) {
if (ptr.get() < BUFMAX) {
// wait on ptr to prevent overflow
int currPtr;
synchronized(ptr) {
currPtr = ptr.getAndIncrement();
}
firstBuffer[currPtr] = System.identityHashCode(allocdObject);
eventTypeBuffer[currPtr] = 3;
secondBuffer[currPtr] = allocdClassID;
thirdBuffer[currPtr] = allocSiteID;
// I hope no one ever wants a 2 gigabyte (shallow size!) object
// some problem here...
// System.err.println("Class ID: " + allocdClassID);
fourthBuffer[currPtr] = (int) getObjectSize(allocdObject);
timestampBuffer[currPtr] = timestamp;
threadIDBuffer[currPtr] = System.identityHashCode(Thread.currentThread());
break;
} else {
synchronized(ptr) {
if (ptr.get() >= BUFMAX) {
flushBuffer();
}
}
}
}
} finally {
mx.unlock();
}
inInstrumentMethod.set(false);
}
public static void flushBuffer()
{
mx.lock();
try {
for (int i = 0; i < BUFMAX; i++) {
switch(eventTypeBuffer[i]) {
case 1: // method entry
// M <method-id> <receiver-object-id> <thread-id>
pw.println( "M " +
firstBuffer[i] + " " +
secondBuffer[i] + " " +
threadIDBuffer[i] );
break;
case 2: // method exit
// E <method-id> <thread-id>
pw.println( "E " +
firstBuffer[i] + " " +
threadIDBuffer[i] );
break;
case 3: // object allocation
// N <object-id> <size> <type-id> <site-id> <length (0)> <thread-id>
// 1st buffer = object ID (hash)
// 2nd buffer = class ID
// 3rd buffer = allocation site (method ID)
pw.println( "N " +
firstBuffer[i] + " " +
fourthBuffer[i] + " " +
secondBuffer[i] + " " +
thirdBuffer[i] + " "
+ 0 + " "
+ threadIDBuffer[i] );
break;
case 4: // object array allocation
case 5: // primitive array allocation
// 5 now removed so nothing should come out of it
// A <object-id> <size> <type-id> <site-id> <length> <thread-id>
pw.println( "A " +
firstBuffer[i] + " " +
fifthBuffer[i] + " " +
secondBuffer[i] + " " +
fourthBuffer[i] + " " +
thirdBuffer[i] + " " +
threadIDBuffer[i] );
break;
case 6: // 2D array allocation
// TODO: Conflicting documention: 2018-1112
// 6, arrayHash, arrayClassID, size1, size2, timestamp
// A <object-id> <size> <type-id> <site-id> <length> <thread-id>
pw.println( "A " +
firstBuffer[i] + " " +
fifthBuffer[i] + " " +
secondBuffer[i] + " " +
fourthBuffer[i] + " " +
thirdBuffer[i] + " " +
threadIDBuffer[i] );
break;
case 7: // object update
// TODO: Conflicting documention: 2018-1112
// 7, targetObjectHash, fieldID, srcObjectHash, timestamp
// U <old-tgt-obj-id> <obj-id> <new-tgt-obj-id> <field-id> <thread-id>
pw.println( "U " +
firstBuffer[i] + " " +
secondBuffer[i] + " " +
thirdBuffer[i] + " " +
threadIDBuffer[i] );
break;
case 8: // witness with get field
// 8, aliveObjectHash, classID, timestamp
pw.println( "W" + " " +
firstBuffer[i] + " " +
secondBuffer[i] + " " +
threadIDBuffer[i] );
break;
}
}
ptr.set(0);
} finally {
mx.unlock();
}
}
}