-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava_crw_demo.c
executable file
·2503 lines (2168 loc) · 76.1 KB
/
java_crw_demo.c
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @(#)java_crw_demo.c 1.39 06/01/27
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/* Class reader writer (java_crw_demo) for instrumenting bytecodes */
/*
* As long as the callbacks allow for it and the class number is unique,
* this code is completely re-entrant and any number of classfile
* injections can happen at the same time.
*
* The current logic requires a unique number for this class instance
* or (jclass,jobject loader) pair, this is done via the ClassIndex
* in hprof, which is passed in as the 'unsigned cnum' to java_crw_demo().
* It's up to the user of this interface if it wants to use this
* feature.
*
* Example Usage: See file test_crw.c.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Get Java and class file and bytecode information. */
#include <jni.h>
#include "classfile_constants.h"
/* Include our own interface for cross check */
#include "java_crw_demo.h"
/* Macros over error functions to capture line numbers */
#define CRW_FATAL(ci, message) fatal_error(ci, message, __FILE__, __LINE__)
#if defined(DEBUG) || !defined(NDEBUG)
#define CRW_ASSERT(ci, cond) \
((cond)?(void)0:assert_error(ci, #cond, __FILE__, __LINE__))
#else
#define CRW_ASSERT(ci, cond)
#endif
#define CRW_ASSERT_MI(mi) CRW_ASSERT((mi)?(mi)->ci:NULL,(mi)!=NULL)
#define CRW_ASSERT_CI(ci) CRW_ASSERT(ci, ( (ci) != NULL && \
(ci)->input_position <= (ci)->input_len && \
(ci)->output_position <= (ci)->output_len) )
/* Typedefs for various integral numbers, just for code clarity */
typedef unsigned ClassOpcode; /* One opcode */
typedef unsigned char ByteCode; /* One byte from bytecodes */
typedef int ByteOffset; /* Byte offset */
typedef int ClassConstant; /* Constant pool kind */
typedef long CrwPosition; /* Position in class image */
typedef unsigned short CrwCpoolIndex; /* Index into constant pool */
/* Misc support macros */
/* Given the position of an opcode, find the next 4byte boundary position */
#define NEXT_4BYTE_BOUNDARY(opcode_pos) (((opcode_pos)+4) & (~3))
#define LARGEST_INJECTION (12*3) /* 3 injections at same site */
#define MAXIMUM_NEW_CPOOL_ENTRIES 64 /* don't add more than 32 entries */
/* Constant Pool Entry (internal table that mirrors pool in file image) */
typedef struct {
const char * ptr; /* Pointer to any string */
unsigned short len; /* Length of string */
unsigned int index1; /* 1st 16 bit index or 32bit value. */
unsigned int index2; /* 2nd 16 bit index or 32bit value. */
ClassConstant tag; /* Tag or kind of entry. */
} CrwConstantPoolEntry;
struct MethodImage;
/* Class file image storage structure */
typedef struct CrwClassImage {
/* Unique class number for this class */
unsigned number;
/* Name of class, given or gotten out of class image */
const char * name;
/* Input and Output class images tracking */
const unsigned char * input;
unsigned char * output;
CrwPosition input_len;
CrwPosition output_len;
CrwPosition input_position;
CrwPosition output_position;
/* Mirrored constant pool */
CrwConstantPoolEntry * cpool;
CrwCpoolIndex cpool_max_elements; /* Max count */
CrwCpoolIndex cpool_count_plus_one;
/* Input flags about class (e.g. is it a system class) */
int system_class;
/* Class access flags gotten from file. */
unsigned access_flags;
/* Names of classes and methods. */
char* tclass_name; /* Name of class that has tracker methods. */
char* tclass_sig; /* Signature of class */
char* call_name; /* Method name to call at offset 0 */
char* call_sig; /* Signature of this method */
char* return_name; /* Method name to call before any return */
char* return_sig; /* Signature of this method */
char* obj_init_name; /* Method name to call in Object <init> */
char* obj_init_sig; /* Signature of this method */
char* newarray_name; /* Method name to call after newarray opcodes */
char* newarray_sig; /* Signature of this method */
/* Constant pool index values for new entries */
CrwCpoolIndex tracker_class_index;
CrwCpoolIndex object_init_tracker_index;
CrwCpoolIndex newarray_tracker_index;
CrwCpoolIndex call_tracker_index;
CrwCpoolIndex return_tracker_index;
CrwCpoolIndex class_number_index; /* Class number in pool */
/* Count of injections made into this class */
int injection_count;
/* This class must be the java.lang.Object class */
jboolean is_object_class;
/* This class must be the java.lang.Thread class */
jboolean is_thread_class;
/* Callback functions */
FatalErrorHandler fatal_error_handler;
MethodNumberRegister mnum_callback;
/* Table of method names and descr's */
int method_count;
const char ** method_name;
const char ** method_descr;
struct MethodImage * current_mi;
} CrwClassImage;
/* Injection bytecodes (holds injected bytecodes for each code position) */
typedef struct {
ByteCode * code;
ByteOffset len;
} Injection;
/* Method transformation data (allocated/freed as each method is processed) */
typedef struct MethodImage {
/* Back reference to Class image data. */
CrwClassImage * ci;
/* Unique method number for this class. */
unsigned number;
/* Method name and descr */
const char * name;
const char * descr;
/* Map of input bytecode offsets to output bytecode offsets */
ByteOffset * map;
/* Bytecode injections for each input bytecode offset */
Injection * injections;
/* Widening setting for each input bytecode offset */
signed char * widening;
/* Length of original input bytecodes, and new bytecodes. */
ByteOffset code_len;
ByteOffset new_code_len;
/* Location in input where bytecodes are located. */
CrwPosition start_of_input_bytecodes;
/* Original max_stack and new max stack */
unsigned max_stack;
unsigned new_max_stack;
jboolean object_init_method;
jboolean skip_call_return_sites;
/* Method access flags gotten from file. */
unsigned access_flags;
} MethodImage;
/* ----------------------------------------------------------------- */
/* General support functions (memory and error handling) */
static void
fatal_error(CrwClassImage *ci, const char *message, const char *file, int line)
{
if ( ci != NULL && ci->fatal_error_handler != NULL ) {
(*ci->fatal_error_handler)(message, file, line);
} else {
/* Normal operation should NEVER reach here */
/* NO CRW FATAL ERROR HANDLER! */
(void)fprintf(stderr, "CRW: %s [%s:%d]\n", message, file, line);
abort();
}
}
#if defined(DEBUG) || !defined(NDEBUG)
static void
assert_error(CrwClassImage *ci, const char *condition,
const char *file, int line)
{
char buf[512];
MethodImage *mi;
ByteOffset byte_code_offset;
mi = ci->current_mi;
if ( mi != NULL ) {
byte_code_offset = (ByteOffset)(mi->ci->input_position - mi->start_of_input_bytecodes);
} else {
byte_code_offset=-1;
}
(void)sprintf(buf,
"CRW ASSERTION FAILURE: %s (%s:%s:%d)",
condition,
ci->name==0?"?":ci->name,
mi->name==0?"?":mi->name,
byte_code_offset);
fatal_error(ci, buf, file, line);
}
#endif
static void *
allocate(CrwClassImage *ci, int nbytes)
{
void * ptr;
if ( nbytes <= 0 ) {
CRW_FATAL(ci, "Cannot allocate <= 0 bytes");
}
ptr = malloc(nbytes);
if ( ptr == NULL ) {
CRW_FATAL(ci, "Ran out of malloc memory");
}
return ptr;
}
static void *
reallocate(CrwClassImage *ci, void *optr, int nbytes)
{
void * ptr;
if ( optr == NULL ) {
CRW_FATAL(ci, "Cannot deallocate NULL");
}
if ( nbytes <= 0 ) {
CRW_FATAL(ci, "Cannot reallocate <= 0 bytes");
}
ptr = realloc(optr, nbytes);
if ( ptr == NULL ) {
CRW_FATAL(ci, "Ran out of malloc memory");
}
return ptr;
}
static void *
allocate_clean(CrwClassImage *ci, int nbytes)
{
void * ptr;
if ( nbytes <= 0 ) {
CRW_FATAL(ci, "Cannot allocate <= 0 bytes");
}
ptr = calloc(nbytes, 1);
if ( ptr == NULL ) {
CRW_FATAL(ci, "Ran out of malloc memory");
}
return ptr;
}
static const char *
duplicate(CrwClassImage *ci, const char *str, int len)
{
char *copy;
copy = (char*)allocate(ci, len+1);
(void)memcpy(copy, str, len);
copy[len] = 0;
return (const char *)copy;
}
static void
deallocate(CrwClassImage *ci, void *ptr)
{
if ( ptr == NULL ) {
CRW_FATAL(ci, "Cannot deallocate NULL");
}
(void)free(ptr);
}
/* ----------------------------------------------------------------- */
/* Functions for reading/writing bytes to/from the class images */
static unsigned
readU1(CrwClassImage *ci)
{
CRW_ASSERT_CI(ci);
return ((unsigned)(ci->input[ci->input_position++])) & 0xFF;
}
static unsigned
readU2(CrwClassImage *ci)
{
unsigned res;
res = readU1(ci);
return (res << 8) + readU1(ci);
}
static signed short
readS2(CrwClassImage *ci)
{
unsigned res;
res = readU1(ci);
return ((res << 8) + readU1(ci)) & 0xFFFF;
}
static unsigned
readU4(CrwClassImage *ci)
{
unsigned res;
res = readU2(ci);
return (res << 16) + readU2(ci);
}
static void
writeU1(CrwClassImage *ci, unsigned val) /* Only writes out lower 8 bits */
{
CRW_ASSERT_CI(ci);
if ( ci->output != NULL ) {
ci->output[ci->output_position++] = val & 0xFF;
}
}
static void
writeU2(CrwClassImage *ci, unsigned val)
{
writeU1(ci, val >> 8);
writeU1(ci, val);
}
static void
writeU4(CrwClassImage *ci, unsigned val)
{
writeU2(ci, val >> 16);
writeU2(ci, val);
}
static unsigned
copyU1(CrwClassImage *ci)
{
unsigned value;
value = readU1(ci);
writeU1(ci, value);
return value;
}
static unsigned
copyU2(CrwClassImage *ci)
{
unsigned value;
value = readU2(ci);
writeU2(ci, value);
return value;
}
static unsigned
copyU4(CrwClassImage *ci)
{
unsigned value;
value = readU4(ci);
writeU4(ci, value);
return value;
}
static void
copy(CrwClassImage *ci, unsigned count)
{
CRW_ASSERT_CI(ci);
if ( ci->output != NULL ) {
(void)memcpy(ci->output+ci->output_position,
ci->input+ci->input_position, count);
ci->output_position += count;
}
ci->input_position += count;
CRW_ASSERT_CI(ci);
}
static void
skip(CrwClassImage *ci, unsigned count)
{
CRW_ASSERT_CI(ci);
ci->input_position += count;
}
static void
read_bytes(CrwClassImage *ci, void *bytes, unsigned count)
{
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, bytes!=NULL);
(void)memcpy(bytes, ci->input+ci->input_position, count);
ci->input_position += count;
}
static void
write_bytes(CrwClassImage *ci, void *bytes, unsigned count)
{
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, bytes!=NULL);
if ( ci->output != NULL ) {
(void)memcpy(ci->output+ci->output_position, bytes, count);
ci->output_position += count;
}
}
static void
random_writeU2(CrwClassImage *ci, CrwPosition pos, unsigned val)
{
CrwPosition save_position;
CRW_ASSERT_CI(ci);
save_position = ci->output_position;
ci->output_position = pos;
writeU2(ci, val);
ci->output_position = save_position;
}
static void
random_writeU4(CrwClassImage *ci, CrwPosition pos, unsigned val)
{
CrwPosition save_position;
CRW_ASSERT_CI(ci);
save_position = ci->output_position;
ci->output_position = pos;
writeU4(ci, val);
ci->output_position = save_position;
}
/* ----------------------------------------------------------------- */
/* Constant Pool handling functions. */
static void
fillin_cpool_entry(CrwClassImage *ci, CrwCpoolIndex i,
ClassConstant tag,
unsigned int index1, unsigned int index2,
const char *ptr, int len)
{
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, i > 0 && i < ci->cpool_count_plus_one);
ci->cpool[i].tag = tag;
ci->cpool[i].index1 = index1;
ci->cpool[i].index2 = index2;
ci->cpool[i].ptr = ptr;
ci->cpool[i].len = (unsigned short)len;
}
static CrwCpoolIndex
add_new_cpool_entry(CrwClassImage *ci, ClassConstant tag,
unsigned int index1, unsigned int index2,
const char *str, int len)
{
CrwCpoolIndex i;
char *utf8 = NULL;
CRW_ASSERT_CI(ci);
i = ci->cpool_count_plus_one++;
/* NOTE: This implementation does not automatically expand the
* constant pool table beyond the expected number needed
* to handle this particular CrwTrackerInterface injections.
* See MAXIMUM_NEW_CPOOL_ENTRIES
*/
CRW_ASSERT(ci, ci->cpool_count_plus_one < ci->cpool_max_elements );
writeU1(ci, tag);
switch (tag) {
case JVM_CONSTANT_Class:
writeU2(ci, index1);
break;
case JVM_CONSTANT_String:
writeU2(ci, index1);
break;
case JVM_CONSTANT_Fieldref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_InterfaceMethodref:
case JVM_CONSTANT_Integer:
case JVM_CONSTANT_Float:
case JVM_CONSTANT_NameAndType:
writeU2(ci, index1);
writeU2(ci, index2);
break;
case JVM_CONSTANT_Long:
case JVM_CONSTANT_Double:
writeU4(ci, index1);
writeU4(ci, index2);
ci->cpool_count_plus_one++;
CRW_ASSERT(ci, ci->cpool_count_plus_one < ci->cpool_max_elements );
break;
case JVM_CONSTANT_Utf8:
CRW_ASSERT(ci, len==(len & 0xFFFF));
writeU2(ci, len);
write_bytes(ci, (void*)str, len);
utf8 = (char*)duplicate(ci, str, len);
break;
default:
CRW_FATAL(ci, "Unknown constant");
break;
}
fillin_cpool_entry(ci, i, tag, index1, index2, (const char *)utf8, len);
CRW_ASSERT(ci, i > 0 && i < ci->cpool_count_plus_one);
return i;
}
static CrwCpoolIndex
add_new_class_cpool_entry(CrwClassImage *ci, const char *class_name)
{
CrwCpoolIndex name_index;
CrwCpoolIndex class_index;
int len;
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, class_name!=NULL);
len = (int)strlen(class_name);
name_index = add_new_cpool_entry(ci, JVM_CONSTANT_Utf8, len, 0,
class_name, len);
class_index = add_new_cpool_entry(ci, JVM_CONSTANT_Class, name_index, 0,
NULL, 0);
return class_index;
}
static CrwCpoolIndex
add_new_method_cpool_entry(CrwClassImage *ci, CrwCpoolIndex class_index,
const char *name, const char *descr)
{
CrwCpoolIndex name_index;
CrwCpoolIndex descr_index;
CrwCpoolIndex name_type_index;
int len;
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, name!=NULL);
CRW_ASSERT(ci, descr!=NULL);
len = (int)strlen(name);
name_index =
add_new_cpool_entry(ci, JVM_CONSTANT_Utf8, len, 0, name, len);
len = (int)strlen(descr);
descr_index =
add_new_cpool_entry(ci, JVM_CONSTANT_Utf8, len, 0, descr, len);
name_type_index =
add_new_cpool_entry(ci, JVM_CONSTANT_NameAndType,
name_index, descr_index, NULL, 0);
return add_new_cpool_entry(ci, JVM_CONSTANT_Methodref,
class_index, name_type_index, NULL, 0);
}
static CrwConstantPoolEntry
cpool_entry(CrwClassImage *ci, CrwCpoolIndex c_index)
{
CRW_ASSERT_CI(ci);
CRW_ASSERT(ci, c_index > 0 && c_index < ci->cpool_count_plus_one);
return ci->cpool[c_index];
}
static void
cpool_setup(CrwClassImage *ci)
{
CrwCpoolIndex i;
CrwPosition cpool_output_position;
int count_plus_one;
CRW_ASSERT_CI(ci);
cpool_output_position = ci->output_position;
count_plus_one = copyU2(ci);
CRW_ASSERT(ci, count_plus_one>1);
ci->cpool_max_elements = count_plus_one+MAXIMUM_NEW_CPOOL_ENTRIES;
ci->cpool = (CrwConstantPoolEntry*)allocate_clean(ci,
(int)((ci->cpool_max_elements)*sizeof(CrwConstantPoolEntry)));
ci->cpool_count_plus_one = (CrwCpoolIndex)count_plus_one;
/* Index zero not in class file */
for (i = 1; i < count_plus_one; ++i) {
CrwCpoolIndex ipos;
ClassConstant tag;
unsigned int index1;
unsigned int index2;
unsigned len;
char * utf8;
ipos = i;
index1 = 0;
index2 = 0;
len = 0;
utf8 = NULL;
tag = copyU1(ci);
switch (tag) {
case JVM_CONSTANT_Class:
index1 = copyU2(ci);
break;
case JVM_CONSTANT_String:
index1 = copyU2(ci);
break;
case JVM_CONSTANT_Fieldref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_InterfaceMethodref:
case JVM_CONSTANT_Integer:
case JVM_CONSTANT_Float:
case JVM_CONSTANT_NameAndType:
index1 = copyU2(ci);
index2 = copyU2(ci);
break;
case JVM_CONSTANT_Long:
case JVM_CONSTANT_Double:
index1 = copyU4(ci);
index2 = copyU4(ci);
++i; /* // these take two CP entries - duh! */
break;
case JVM_CONSTANT_Utf8:
len = copyU2(ci);
index1 = (unsigned short)len;
utf8 = (char*)allocate(ci, len+1);
read_bytes(ci, (void*)utf8, len);
utf8[len] = 0;
write_bytes(ci, (void*)utf8, len);
break;
default:
CRW_FATAL(ci, "Unknown constant");
break;
}
fillin_cpool_entry(ci, ipos, tag, index1, index2, (const char *)utf8, len);
}
if (ci->call_name != NULL || ci->return_name != NULL) {
if ( ci->number != (ci->number & 0x7FFF) ) {
ci->class_number_index =
add_new_cpool_entry(ci, JVM_CONSTANT_Integer,
(ci->number>>16) & 0xFFFF, ci->number & 0xFFFF, NULL, 0);
}
}
if ( ci->tclass_name != NULL ) {
ci->tracker_class_index =
add_new_class_cpool_entry(ci, ci->tclass_name);
}
if (ci->obj_init_name != NULL) {
ci->object_init_tracker_index = add_new_method_cpool_entry(ci,
ci->tracker_class_index,
ci->obj_init_name,
ci->obj_init_sig);
}
if (ci->newarray_name != NULL) {
ci->newarray_tracker_index = add_new_method_cpool_entry(ci,
ci->tracker_class_index,
ci->newarray_name,
ci->newarray_sig);
}
if (ci->call_name != NULL) {
ci->call_tracker_index = add_new_method_cpool_entry(ci,
ci->tracker_class_index,
ci->call_name,
ci->call_sig);
}
if (ci->return_name != NULL) {
ci->return_tracker_index = add_new_method_cpool_entry(ci,
ci->tracker_class_index,
ci->return_name,
ci->return_sig);
}
random_writeU2(ci, cpool_output_position, ci->cpool_count_plus_one);
}
/* ----------------------------------------------------------------- */
/* Functions that create the bytecodes to inject */
static ByteOffset
push_pool_constant_bytecodes(ByteCode *bytecodes, CrwCpoolIndex index)
{
ByteOffset nbytes = 0;
if ( index == (index&0x7F) ) {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_ldc;
} else {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_ldc_w;
bytecodes[nbytes++] = (ByteCode)((index >> 8) & 0xFF);
}
bytecodes[nbytes++] = (ByteCode)(index & 0xFF);
return nbytes;
}
static ByteOffset
push_short_constant_bytecodes(ByteCode *bytecodes, unsigned number)
{
ByteOffset nbytes = 0;
if ( number <= 5 ) {
bytecodes[nbytes++] = (ByteCode)(JVM_OPC_iconst_0+number);
} else if ( number == (number&0x7F) ) {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_bipush;
bytecodes[nbytes++] = (ByteCode)(number & 0xFF);
} else {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_sipush;
bytecodes[nbytes++] = (ByteCode)((number >> 8) & 0xFF);
bytecodes[nbytes++] = (ByteCode)(number & 0xFF);
}
return nbytes;
}
static ByteOffset
injection_template(MethodImage *mi, ByteCode *bytecodes, ByteOffset max_nbytes,
CrwCpoolIndex method_index)
{
CrwClassImage * ci;
ByteOffset nbytes = 0;
unsigned max_stack;
int add_dup;
int add_aload;
int push_cnum;
int push_mnum;
ci = mi->ci;
CRW_ASSERT(ci, bytecodes!=NULL);
if ( method_index == 0 ) {
return 0;
}
if ( method_index == ci->newarray_tracker_index) {
max_stack = mi->max_stack + 1;
add_dup = JNI_TRUE;
add_aload = JNI_FALSE;
push_cnum = JNI_FALSE;
push_mnum = JNI_FALSE;
} else if ( method_index == ci->object_init_tracker_index) {
max_stack = mi->max_stack + 1;
add_dup = JNI_FALSE;
add_aload = JNI_TRUE;
push_cnum = JNI_FALSE;
push_mnum = JNI_FALSE;
} else {
max_stack = mi->max_stack + 2;
add_dup = JNI_FALSE;
add_aload = JNI_FALSE;
push_cnum = JNI_TRUE;
push_mnum = JNI_TRUE;
}
if ( add_dup ) {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_dup;
}
if ( add_aload ) {
bytecodes[nbytes++] = (ByteCode)JVM_OPC_aload_0;
}
if ( push_cnum ) {
if ( ci->number == (ci->number & 0x7FFF) ) {
nbytes += push_short_constant_bytecodes(bytecodes+nbytes,
ci->number);
} else {
CRW_ASSERT(ci, ci->class_number_index!=0);
nbytes += push_pool_constant_bytecodes(bytecodes+nbytes,
ci->class_number_index);
}
}
if ( push_mnum ) {
nbytes += push_short_constant_bytecodes(bytecodes+nbytes,
mi->number);
}
bytecodes[nbytes++] = (ByteCode)JVM_OPC_invokestatic;
bytecodes[nbytes++] = (ByteCode)(method_index >> 8);
bytecodes[nbytes++] = (ByteCode)method_index;
bytecodes[nbytes] = 0;
CRW_ASSERT(ci, nbytes<max_nbytes);
/* Make sure the new max_stack is appropriate */
if ( max_stack > mi->new_max_stack ) {
mi->new_max_stack = max_stack;
}
return nbytes;
}
/* Called to create injection code at entry to a method */
static ByteOffset
entry_injection_code(MethodImage *mi, ByteCode *bytecodes, ByteOffset len)
{
CrwClassImage * ci;
ByteOffset nbytes = 0;
CRW_ASSERT_MI(mi);
ci = mi->ci;
if ( mi->object_init_method ) {
nbytes = injection_template(mi,
bytecodes, len, ci->object_init_tracker_index);
}
if ( !mi->skip_call_return_sites ) {
nbytes += injection_template(mi,
bytecodes+nbytes, len-nbytes, ci->call_tracker_index);
}
return nbytes;
}
/* Called to create injection code before an opcode */
static ByteOffset
before_injection_code(MethodImage *mi, ClassOpcode opcode,
ByteCode *bytecodes, ByteOffset len)
{
ByteOffset nbytes = 0;
CRW_ASSERT_MI(mi);
switch ( opcode ) {
case JVM_OPC_return:
case JVM_OPC_ireturn:
case JVM_OPC_lreturn:
case JVM_OPC_freturn:
case JVM_OPC_dreturn:
case JVM_OPC_areturn:
if ( !mi->skip_call_return_sites ) {
nbytes = injection_template(mi,
bytecodes, len, mi->ci->return_tracker_index);
}
break;
default:
break;
}
return nbytes;
}
/* Called to create injection code after an opcode */
static ByteOffset
after_injection_code(MethodImage *mi, ClassOpcode opcode,
ByteCode *bytecodes, ByteOffset len)
{
CrwClassImage* ci;
ByteOffset nbytes;
ci = mi->ci;
nbytes = 0;
CRW_ASSERT_MI(mi);
switch ( opcode ) {
case JVM_OPC_new:
/* Can't inject here cannot pass around uninitialized object */
break;
case JVM_OPC_newarray:
case JVM_OPC_anewarray:
case JVM_OPC_multianewarray:
nbytes = injection_template(mi,
bytecodes, len, ci->newarray_tracker_index);
break;
default:
break;
}
return nbytes;
}
/* Actually inject the bytecodes */
static void
inject_bytecodes(MethodImage *mi, ByteOffset at,
ByteCode *bytecodes, ByteOffset len)
{
Injection injection;
CrwClassImage *ci;
ci = mi->ci;
CRW_ASSERT_MI(mi);
CRW_ASSERT(ci, at <= mi->code_len);
injection = mi->injections[at];
CRW_ASSERT(ci, len <= LARGEST_INJECTION/2);
CRW_ASSERT(ci, injection.len+len <= LARGEST_INJECTION);
/* Either start an injection area or concatenate to what is there */
if ( injection.code == NULL ) {
CRW_ASSERT(ci, injection.len==0);
injection.code = (ByteCode *)allocate_clean(ci, LARGEST_INJECTION+1);
}
(void)memcpy(injection.code+injection.len, bytecodes, len);
injection.len += len;
injection.code[injection.len] = 0;
mi->injections[at] = injection;
ci->injection_count++;
}
/* ----------------------------------------------------------------- */
/* Method handling functions */
static MethodImage *
method_init(CrwClassImage *ci, unsigned mnum, ByteOffset code_len)
{
MethodImage * mi;
ByteOffset i;
mi = (MethodImage*)allocate_clean(ci, (int)sizeof(MethodImage));
mi->ci = ci;
mi->name = ci->method_name[mnum];
mi->descr = ci->method_descr[mnum];
mi->code_len = code_len;
mi->map = (ByteOffset*)allocate_clean(ci,
(int)((code_len+1)*sizeof(ByteOffset)));
for(i=0; i<=code_len; i++) {
mi->map[i] = i;
}
mi->widening = (signed char*)allocate_clean(ci, code_len+1);
mi->injections = (Injection *)allocate_clean(ci,
(int)((code_len+1)*sizeof(Injection)));
mi->number = mnum;
ci->current_mi = mi;
return mi;
}
static void
method_term(MethodImage *mi)
{
CrwClassImage *ci;
ci = mi->ci;
CRW_ASSERT_MI(mi);
if ( mi->map != NULL ) {
deallocate(ci, (void*)mi->map);
mi->map = NULL;
}
if ( mi->widening != NULL ) {
deallocate(ci, (void*)mi->widening);
mi->widening = NULL;
}
if ( mi->injections != NULL ) {
ByteOffset i;
for(i=0; i<= mi->code_len; i++) {
if ( mi->injections[i].code != NULL ) {
deallocate(ci, (void*)mi->injections[i].code);
mi->injections[i].code = NULL;