-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstrumentBytecode.cc
More file actions
694 lines (553 loc) · 25.1 KB
/
InstrumentBytecode.cc
File metadata and controls
694 lines (553 loc) · 25.1 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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include "InstrumentBytecode.h"
// #include <ClassHierarchy.hpp>
#include <cassert>
#include <iostream>
#include <utility>
using namespace std;
using namespace jnif;
using namespace jnif::model;
ClassHierarchy classHierarchy;
class ClassNotLoadedException {
public:
ClassNotLoadedException(const string &className) :
className(className) {}
string className;
};
class ClassPath: public IClassPath {
public:
ClassPath(JNIEnv* jni, jobject loader) :
thisJni(jni), loader(loader)
{
if (loader != nullptr) {
#ifdef DEBUG
cerr << "We're in live phase" << endl;
#endif
inLivePhase = true;
}
}
string getCommonSuperClass(const string& className1, const string& className2) {
if (!inLivePhase) {
return "java/lang/Object";
}
try {
loadClassIfNotLoaded(className1);
loadClassIfNotLoaded(className2);
string sup = className1;
const string& sub = className2;
while (!isAssignableFrom(sub, sup)) {
loadClassIfNotLoaded(sup);
sup = classHierarchy.getSuperClass(sup);
if (sup == "0") {
return "java/lang/Object";
}
}
return sup;
} catch (const ClassNotLoadedException& e) {
// cerr << "Not loaded: " << e.className << endl;
string res = "java/lang/Object";
return res;
}
}
bool isAssignableFrom(const string& sub, const string& sup) {
string cls = sub;
while (cls != "0") {
if (cls == sup) {
return true;
}
loadClassIfNotLoaded(cls);
cls = classHierarchy.getSuperClass(cls);
}
return false;
}
static void initProxyClass(JNIEnv* theJni) {
if (proxyClass == NULL) {
proxyClass = theJni->FindClass("ETProxy");
if (!proxyClass) {
cerr << "ETProxy not found" << endl;
}
assert(proxyClass);
getResourceId = theJni->GetStaticMethodID(proxyClass, "getResource",
"(Ljava/lang/String;Ljava/lang/ClassLoader;)[B");
assert(getResourceId);
proxyClass = (jclass) theJni->NewGlobalRef(proxyClass);
assert(proxyClass);
}
}
private:
void loadClassIfNotLoaded(const string& className) {
if (!classHierarchy.isDefined(className)) {
loadClassAsResource(className);
}
}
void loadClassAsResource(const string& className) {
// cerr << "in class: " << className << endl;
ClassPath::initProxyClass(thisJni);
jstring targetName = thisJni->NewStringUTF(className.c_str());
jobject res = thisJni->CallStaticObjectMethod(proxyClass, getResourceId,
targetName, loader);
if (res == NULL) {
throw ClassNotLoadedException(className);
}
jsize len = thisJni->GetArrayLength((jarray) res);
u1* bytes = (u1*) thisJni->GetByteArrayElements((jbyteArray) res, NULL);
parser::ClassFileParser cf(bytes, len);
thisJni->ReleaseByteArrayElements((jbyteArray) res, (jbyte*) bytes,
JNI_ABORT);
thisJni->DeleteLocalRef(res);
thisJni->DeleteLocalRef(targetName);
classHierarchy.addClass(cf);
}
//const char* className;
JNIEnv* thisJni;
jobject loader;
static jclass proxyClass;
static jmethodID getResourceId;
bool inLivePhase = false;
};
jclass ClassPath::proxyClass = NULL;
jmethodID ClassPath::getResourceId = NULL;
int lastNewSiteID = 0;
static void instrumentMethodEntry( ClassFile &cf,
Method &method,
ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod,
ConstPool::Index &witnessMethod );
static void instrumentMethodExit( ClassFile &cf,
Method &method,
ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod );
static void instrumentObjectAlloc( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void instrumentObjectArrayAlloc( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void instrumentPrimitiveArrayAlloc( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void instrumentMultiArrayAlloc( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void instrumentPutField( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void witnessGetField( ClassFile &cf,
Method &method,
ConstPool::Index &instrMethod );
static void instrumentMethodInvoke( ClassFile &cf,
Method &method,
ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod );
void instrumentClass( jvmtiEnv *jvmti,
JNIEnv *currJni,
jobject loader,
u1 *class_data,
jint class_data_len,
jint *new_class_data_len,
u1 **new_class_data )
{
static int methodIDCounter = 1;
#ifdef DEBUG
cerr << "Attempting to parse class file" << endl;
#endif
parser::ClassFileParser cf(class_data, class_data_len);
#ifdef DEBUG
cerr << "Parsing " << cf.getThisClassName() << " was successful" << endl;
#endif
classHierarchy.addClass(cf);
classTable.mapOrFind(string(cf.getThisClassName()));
ConstPool::Index instrumentFlagClass = cf.addClass("InstrumentFlag");
ConstPool::Index proxyClass = cf.addClass("ETProxy");
// public static void onEntry(int methodID, Object receiver);
ConstPool::Index onEntryMethod = cf.addMethodRef(proxyClass, "onEntry", "(ILjava/lang/Object;)V");
// public static void onExit(int methodID);
ConstPool::Index onExitMethod = cf.addMethodRef(proxyClass, "onExit", "(I)V");
// public static native void onMain();
// ConstPool::Index onMainMethod = cf.addMethodRef(proxyClass, "_onMain", "()V");
// public static void onObjectAlloc(Object allocdObject, int allocdClassID, int allocSiteID);
ConstPool::Index onAllocMethod = cf.addMethodRef(proxyClass, "onObjectAlloc", "(Ljava/lang/Object;II)V");
// pitfall: allocdArray is not always Object[]!
// public static void onObjectArrayAlloc(int allocdClassID, int size, Object allocdArray, int allocSiteID);
ConstPool::Index onAllocArrayMethod = cf.addMethodRef(proxyClass, "onArrayAlloc", "(IILjava/lang/Object;I)V");
// ... but allocdArray is always Object[] here
// public static void onMultiArrayAlloc(int dims, int allocdClassID, Object[] allocdArray, int allocSiteID);
ConstPool::Index onAllocMultiArrayMethod = cf.addMethodRef(proxyClass, "onMultiArrayAlloc", "(IILjava/lang/Object;I)V");
// public static void onPutField(Object tgtObject, Object srcObject, int fieldID);
ConstPool::Index onPutFieldMethod = cf.addMethodRef(proxyClass, "onPutField","(Ljava/lang/Object;Ljava/lang/Object;I)V");
// public static void witnessObjectAlive(Object aliveObject, int classID);
ConstPool::Index witnessObjectAliveMethod = cf.addMethodRef(proxyClass, "witnessObjectAlive", "(Ljava/lang/Object;I)V");
#ifdef DEBUG
cerr << "Adding method ref was successful" << endl;
#endif
for (Method &method : cf.methods) {
// Record the method
methodTable.insert(make_pair(methodIDCounter, make_pair(string(cf.getThisClassName()),
method.getName())));
int methodID = methodIDCounter;
methodIDCounter++;
if (method.hasCode()) {
ConstPool::Index methodIDIndex = cf.addInteger(methodID);
instrumentMethodEntry(cf, method, methodIDIndex, onEntryMethod, witnessObjectAliveMethod);
instrumentMethodExit(cf, method, methodIDIndex, onExitMethod);
instrumentObjectAlloc(cf, method, onAllocMethod);
instrumentObjectArrayAlloc(cf, method, onAllocArrayMethod);
instrumentPrimitiveArrayAlloc(cf, method, onAllocArrayMethod);
instrumentMultiArrayAlloc(cf, method, onAllocMultiArrayMethod);
instrumentPutField(cf, method, onPutFieldMethod);
witnessGetField(cf, method, witnessObjectAliveMethod);
instrumentMethodInvoke(cf, method, methodIDIndex, onEntryMethod);
}
}
#ifdef DEBUG
cerr << "Added instrumentation calls" << endl;
#endif
// assert(currJni);
// assert(loader);
ClassPath cp(currJni, loader);
#ifdef DEBUG
cerr << "Created ClassPath object" << endl;
#endif
try {
cf.computeFrames(&cp);
#ifdef DEBUG
cerr << "Computing frames was successful" << endl;
#endif
} catch (Exception &e) {
cerr << "JNIF Exception!: " << e.message << endl;
} catch (exception &e) {
cerr << "Exception!: " << e.what() << endl;
}
*new_class_data_len = (jint) cf.computeSize();
jvmtiError error = jvmti->Allocate((jlong) *new_class_data_len, new_class_data);
assert(!error);
try {
cf.write(*new_class_data, *new_class_data_len);
} catch (Exception &e) {
cerr << "JNIF Exception!: " << e.message << endl;
}
#ifdef DEBUG
cerr << "Wrote new bytecode back" << endl;
#endif
}
inline static void instrumentPutField(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::putfield) {
ConstPool::Index targetFieldRefIndex = inst->field()->fieldRefIndex;
string targetClassName, targetFieldName, fieldDesc;
cf.getFieldRef(targetFieldRefIndex, &targetClassName, &targetFieldName, &fieldDesc);
// Only worry about objects for now
if (fieldDesc[0] == 'L') {
// Foo.bar becomes Foo/bar
string fieldString = targetClassName + "/" + targetFieldName;
int targetFieldID = fieldTable.mapOrFind(fieldString);
// Stack: ... | tgtObjectRef | srcObjectRef
instList.addZero(Opcode::dup2, inst);
// Stack: ... | tgtObjectRef | srcObjectRef | tgtObjectRef | srcObjectRef
ConstPool::Index fieldIDIndex = cf.addInteger(targetFieldID);
instList.addLdc(Opcode::ldc_w, fieldIDIndex, inst);
// Stack: ... | tgtObjectRef | srcObjectRef | tgtObjectRef | srcObjectRef | fieldID
instList.addInvoke(Opcode::invokestatic, instrMethod, inst);
// Stack: ... | tgtObjectRef | srcObjectRef
// Stack preserved
}
}
}
}
inline static void instrumentMethodEntry( ClassFile &cf,
Method &method,
ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod,
ConstPool::Index &witnessMethod )
{
InstList &instList = method.instList();
Inst *ptr = *instList.begin();
// doesn't quite work yet, because...circular reasoning
// if (method.isMain()) {
// instList.addInvoke(Opcode::invokestatic, onMainMethod, ptr);
// }
if (method.isStatic() || method.isInit()) {
// Stack: ...
instList.addLdc(Opcode::ldc_w, methodIDIndex, ptr);
// Stack: ... | methodIDIndex
instList.addZero(Opcode::aconst_null, ptr);
// Stack: ... | methodIDIndex | null
instList.addInvoke(Opcode::invokestatic, instrMethod, ptr);
// Stack: ...
// Stack preserved
} else {
// Stack: ...
instList.addLdc(Opcode::ldc_w, methodIDIndex, ptr);
// Stack: ... | methodIDIndex
instList.addZero(Opcode::aload_0, ptr);
// Stack: ... | methodIDIndex | thisObject
instList.addInvoke(Opcode::invokestatic, instrMethod, ptr);
// Stack: ...
// Stack preserved
}
/* Can't instrument certain built-in classes
also, one can't instrument in an initializer
because an uninitialized thing isn't an object */
if (!method.isStatic()
&& !method.isInit()
&& string(method.getName()) != "<clinit>"
// && string(cf.getThisClassName()).find("java") != 0
// && string(cf.getThisClassName()).find("sun") != 0
) {
// Stack: ...
instList.addZero(Opcode::aload_0, ptr);
// Stack: ... | thisObject
// cerr << "Debug: " << cf.getThisClassName() << endl;
int classID = classTable.mapOrFind(string(cf.getThisClassName()));
ConstPool::Index classIDIndex = cf.addInteger(classID);
instList.addLdc(Opcode::ldc_w, classIDIndex, ptr);
// Stack: ... | thisObject | thisClassID
instList.addInvoke(Opcode::invokestatic, witnessMethod, ptr);
}
}
inline static void instrumentMethodExit(ClassFile &cf, Method &method, ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->isExit()) {
instList.addLdc(Opcode::ldc_w, methodIDIndex, inst);
instList.addInvoke(Opcode::invokestatic, instrMethod, inst);
}
}
}
static void instrumentObjectAlloc(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
/* Very tricky due to a few reasons
* #1: an uninitialized object is NOT a java.lang.Object so can't do anything with it
* #2: an object isn't initialized the moment <init> is called
*/
// TODO: DELETE?
// if (method.isInit()) {
// InstList &instList = method.instList();
// Inst *p = *instList.begin();
// instList.addZero(Opcode::aload_0, p);
// // Stack: ... | thisObject
// // cerr << "Debug: " << cf.getThisClassName() << endl;
// int classID = classTable.mapOrFind(string(cf.getThisClassName()));
// ConstPool::Index classIDIndex = cf.addInteger(classID);
// instList.addLdc(Opcode::ldc_w, classIDIndex, p);
// // Stack: ... | thisObject | classIDIndex
// ConstPool::Index allocSiteIndex = cf.addInteger(++lastNewSiteID);
// instList.addLdc(Opcode::ldc_w, allocSiteIndex, p);
// // Stack: ... | thisObject | classIDIndex | allocSiteID
// instList.addInvoke(Opcode::invokestatic, instrMethod, p);
// // Stack: ...
// // Stack preserved
// }
// END TODO.
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::NEW) {
// Stack: ...
Inst *ptr = inst->next;
// Stack: ... | objRef
instList.addZero(Opcode::dup, ptr);
// Stack: ... | objRef | objRef
u4 localVar = (method.codeAttr())->maxLocals;
// DEBUG: cerr << "method: " << method.getName() << "; max locals: " << localVar << endl;
instList.addVar(Opcode::astore, localVar, ptr);
// Stack: ... | objRef
// new local var: objRef
(method.codeAttr())->maxLocals++;
ConstPool::Index allocSiteIndex = cf.addInteger(++lastNewSiteID);
// TODO: ConstPool::Index allocSiteIndex = cf.addInteger(inst->_offset);
ConstPool::Index allocatedClassIndex = inst->type()->classIndex;
int classID = classTable.mapOrFind(string(cf.getClassName(allocatedClassIndex)));
ConstPool::Index classIDIndex = cf.addInteger(classID);
while (true) {
if (ptr->opcode != Opcode::invokespecial) {
ptr = ptr->next;
} else {
InvokeInst *pinv = ptr->invoke();
ConstPool::Index invokedMethodIndex = pinv->methodRefIndex;
string clsName, methodName, methodDesc;
cf.getMethodRef(invokedMethodIndex, &clsName, &methodName, &methodDesc);
ptr = ptr->next;
if (methodName == "<init>") {
break;
}
}
}
instList.addVar(Opcode::aload, localVar, ptr);
// Stack: ... | objRef
instList.addLdc(Opcode::ldc_w, classIDIndex, ptr);
instList.addLdc(Opcode::ldc_w, allocSiteIndex, ptr);
// Stack: ... | objRef | classID | allocSiteID
instList.addInvoke(Opcode::invokestatic, instrMethod, ptr);
// Stack: ... | objRef
// Stack preserved
}
}
}
inline static void instrumentObjectArrayAlloc(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::anewarray) {
// cerr << "object array alloc" << endl;
// Stack: ... | size
ConstPool::Index allocatedClassIndex = inst->type()->classIndex;
// cerr << "allocd class: " << string(cf.getClassName(allocatedClassIndex)) << endl;
int classID = classTable.mapOrFind(string(cf.getClassName(allocatedClassIndex)));
ConstPool::Index classIDIndex = cf.addInteger(classID);
// cerr << "check 1" << endl;
instList.addLdc(Opcode::ldc_w, classIDIndex, inst);
// Stack: ... | size | classID
instList.addZero(Opcode::swap, inst);
// Stack: ... | classID | size
instList.addZero(Opcode::dup, inst);
// Stack: ... | classID | size | size
Inst *p = inst->next;
// Stack: ... | classID | size | arrayRef
// cerr << "check 2" << endl;
instList.addZero(Opcode::dup_x2, p);
// Stack: ... | arrayRef | classID | size | arrayRef
ConstPool::Index allocSiteIndex = cf.addInteger(inst->_offset);
instList.addLdc(Opcode::ldc_w, allocSiteIndex, p);
// Stack: ... | arrayRef | classID | size | arrayRef | allocSiteID
instList.addInvoke(Opcode::invokestatic, instrMethod, p);
// cerr << "check 3" << endl;
// Stack: ... | arrayRef
// Stack preserved
}
}
}
inline static void instrumentPrimitiveArrayAlloc(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::newarray) {
// cerr << "prim array alloc" << endl;
// Stack: ... | size
// this actually depends on JNIF implementation specifics
// potentially unstable across JNIF versions
u1 pseudoClassID = (inst->newarray()->atype) - 3;
// cerr << "class id: " << (int)pseudoClassID << endl;
instList.addBiPush(pseudoClassID, inst);
// Stack: ... | size | atype
instList.addZero(Opcode::swap, inst);
// Stack: ... | atype | size
instList.addZero(Opcode::dup, inst);
// Stack: ... | atype | size | size
Inst *p = inst->next;
// Stack: ... | atype | size | arrayRef
instList.addZero(Opcode::dup_x2, p);
// Stack: ... | arrayRef | atype | size | arrayRef
ConstPool::Index allocSiteIndex = cf.addInteger(inst->_offset);
instList.addLdc(Opcode::ldc_w, allocSiteIndex, p);
// Stack: ... | arrayRef | atype | size | arrayRef | allocSiteID
// Yes, arrays are objects!
instList.addInvoke(Opcode::invokestatic, instrMethod, p);
// Stack: ... | arrayRef
// Stack preserved
}
}
}
inline static void instrumentMultiArrayAlloc(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::multianewarray) {
// cerr << "multi array alloc" << endl;
// cerr << "current class: " << cf.getThisClassName() << endl;
// Stack: ... | size1 | size2 | ... | sizeN
ConstPool::Index allocatedClassIndex = inst->multiarray()->classIndex;
// cerr << "alloc'd class: " << string(cf.getClassName(allocatedClassIndex)) << endl;
int classID = classTable.mapOrFind(string(cf.getClassName(allocatedClassIndex)));
ConstPool::Index classIDIndex = cf.addInteger(classID);
// I hope no one ever wants an array of more than 255 dimensions, really
u1 dims = inst->multiarray()->dims;
// only care about outermost size, because multi-array is just
// a bunch of nested arrays
// Stack: ... | sizeN-1 | sizeN
Inst *p = inst->next;
// Stack: ... | arrayRef
instList.addZero(Opcode::dup, p);
// Stack: ... | arrayRef | arrayRef
instList.addBiPush(dims, p);
// Stack: ... | arrayRef | arrayRef | dims
instList.addLdc(Opcode::ldc_w, classIDIndex, p);
// Stack: ... | arrayRef | arrayRef | dims | arrayClassID
instList.addZero(Opcode::dup2_x1, p);
// Stack: ... | arrayRef | dims | arrayClassID | arrayRef | dims | arrayClassID
instList.addZero(Opcode::pop2, p);
// Stack: ... | arrayRef | dims | arrayClassID | arrayRef
ConstPool::Index allocSiteIndex = cf.addInteger(inst->_offset);
instList.addLdc(Opcode::ldc_w, allocSiteIndex, p);
// Stack: ... | arrayRef | dims | arrayClassID | arrayRef | allocSiteID
instList.addInvoke(Opcode::invokestatic, instrMethod, p);
// Stack: ... | arrayRef
}
}
}
inline static void witnessGetField(ClassFile &cf, Method &method, ConstPool::Index &instrMethod)
{
InstList &instList = method.instList();
for (Inst *inst : instList) {
if (inst->opcode == Opcode::getfield) {
ConstPool::Index fieldRefIndex = inst->field()->fieldRefIndex;
string className, fieldName, fieldDesc;
cf.getFieldRef(fieldRefIndex, &className, &fieldName, &fieldDesc);
// Stack: ... | objectRef
instList.addZero(Opcode::dup, inst);
// Stack: ... | objectRef | objectRef
int aliveClassID = classTable.mapOrFind(className);
ConstPool::Index aliveClassIDIndex = cf.addInteger(aliveClassID);
instList.addLdc(Opcode::ldc_w, aliveClassIDIndex, inst);
// Stack: ... | objectRef | objectRef | aliveClassID
instList.addInvoke(Opcode::invokestatic, instrMethod, inst);
}
}
}
static string to_string(Inst &inst)
{
switch (inst.kind) {
case KIND_ZERO: { return "ZERO"; }
case KIND_BIPUSH: { return "BIPUSH"; }
case KIND_SIPUSH: { return "SIPUSH"; }
case KIND_LDC: { return "LDC"; }
case KIND_VAR: { return "VAR"; }
case KIND_IINC: { return "IINC"; }
case KIND_JUMP: { return "JUMP"; }
case KIND_TABLESWITCH: { return "TABLESWITCH"; }
case KIND_LOOKUPSWITCH: { return "LOOKUPSWITCH"; }
case KIND_FIELD: { return "FIELD"; }
case KIND_INVOKE: { return "INVOKE"; }
case KIND_INVOKEINTERFACE: { return "INVOKEINTERFACE"; }
case KIND_INVOKEDYNAMIC: { return "INVOKEDYNAMIC"; }
case KIND_TYPE: { return "TYPE"; }
case KIND_NEWARRAY: { return "NEWARRAY"; }
case KIND_MULTIARRAY: { return "MULTIARRAY"; }
case KIND_PARSE4TODO: { return "PARSE4TODO"; }
case KIND_RESERVED: { return "RESERVED"; }
case KIND_LABEL: { return "LABEL"; }
case KIND_FRAME: { return "FRAME"; }
default:
assert(false);
}
}
inline static void instrumentMethodInvoke( ClassFile &cf,
Method &method,
ConstPool::Index &methodIDIndex,
ConstPool::Index &instrMethod )
{
InstList &instList = method.instList();
Inst *ptr = *instList.begin();
for (Inst *inst : instList) {
cout << " - " << to_string(*inst);
if (inst->isInvoke()) {
ConstPool::Index invokedMethodIndex = (inst->invoke())->methodRefIndex;
string class_name, method_name, methodDesc;
cf.getMethodRef(invokedMethodIndex, &class_name, &method_name, &methodDesc);
cout << " : " << invokedMethodIndex << " => " << class_name << " # " << method_name;
}
cout << endl;
}
}