Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/prims/jvmtiClassFileReconstituter.cpp
32285 views
1
/*
2
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "classfile/symbolTable.hpp"
27
#include "interpreter/bytecodeStream.hpp"
28
#include "oops/fieldStreams.hpp"
29
#include "prims/jvmtiClassFileReconstituter.hpp"
30
#include "runtime/signature.hpp"
31
#ifdef TARGET_ARCH_x86
32
# include "bytes_x86.hpp"
33
#endif
34
#ifdef TARGET_ARCH_aarch32
35
# include "bytes_aarch32.hpp"
36
#endif
37
#ifdef TARGET_ARCH_aarch64
38
# include "bytes_aarch64.hpp"
39
#endif
40
#ifdef TARGET_ARCH_sparc
41
# include "bytes_sparc.hpp"
42
#endif
43
#ifdef TARGET_ARCH_zero
44
# include "bytes_zero.hpp"
45
#endif
46
#ifdef TARGET_ARCH_arm
47
# include "bytes_arm.hpp"
48
#endif
49
#ifdef TARGET_ARCH_ppc
50
# include "bytes_ppc.hpp"
51
#endif
52
// FIXME: add Deprecated attribute
53
// FIXME: fix Synthetic attribute
54
// FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
55
56
57
// Write the field information portion of ClassFile structure
58
// JVMSpec| u2 fields_count;
59
// JVMSpec| field_info fields[fields_count];
60
void JvmtiClassFileReconstituter::write_field_infos() {
61
HandleMark hm(thread());
62
Array<AnnotationArray*>* fields_anno = ikh()->fields_annotations();
63
Array<AnnotationArray*>* fields_type_anno = ikh()->fields_type_annotations();
64
65
// Compute the real number of Java fields
66
int java_fields = ikh()->java_fields_count();
67
68
write_u2(java_fields);
69
for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) {
70
AccessFlags access_flags = fs.access_flags();
71
int name_index = fs.name_index();
72
int signature_index = fs.signature_index();
73
int initial_value_index = fs.initval_index();
74
guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
75
// int offset = ikh()->field_offset( index );
76
int generic_signature_index = fs.generic_signature_index();
77
AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index());
78
AnnotationArray* type_anno = fields_type_anno == NULL ? NULL : fields_type_anno->at(fs.index());
79
80
// JVMSpec| field_info {
81
// JVMSpec| u2 access_flags;
82
// JVMSpec| u2 name_index;
83
// JVMSpec| u2 descriptor_index;
84
// JVMSpec| u2 attributes_count;
85
// JVMSpec| attribute_info attributes[attributes_count];
86
// JVMSpec| }
87
88
write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
89
write_u2(name_index);
90
write_u2(signature_index);
91
int attr_count = 0;
92
if (initial_value_index != 0) {
93
++attr_count;
94
}
95
if (access_flags.is_synthetic()) {
96
// ++attr_count;
97
}
98
if (generic_signature_index != 0) {
99
++attr_count;
100
}
101
if (anno != NULL) {
102
++attr_count; // has RuntimeVisibleAnnotations attribute
103
}
104
if (type_anno != NULL) {
105
++attr_count; // has RuntimeVisibleTypeAnnotations attribute
106
}
107
108
write_u2(attr_count);
109
110
if (initial_value_index != 0) {
111
write_attribute_name_index("ConstantValue");
112
write_u4(2); //length always 2
113
write_u2(initial_value_index);
114
}
115
if (access_flags.is_synthetic()) {
116
// write_synthetic_attribute();
117
}
118
if (generic_signature_index != 0) {
119
write_signature_attribute(generic_signature_index);
120
}
121
if (anno != NULL) {
122
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
123
}
124
if (type_anno != NULL) {
125
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
126
}
127
}
128
}
129
130
// Write Code attribute
131
// JVMSpec| Code_attribute {
132
// JVMSpec| u2 attribute_name_index;
133
// JVMSpec| u4 attribute_length;
134
// JVMSpec| u2 max_stack;
135
// JVMSpec| u2 max_locals;
136
// JVMSpec| u4 code_length;
137
// JVMSpec| u1 code[code_length];
138
// JVMSpec| u2 exception_table_length;
139
// JVMSpec| { u2 start_pc;
140
// JVMSpec| u2 end_pc;
141
// JVMSpec| u2 handler_pc;
142
// JVMSpec| u2 catch_type;
143
// JVMSpec| } exception_table[exception_table_length];
144
// JVMSpec| u2 attributes_count;
145
// JVMSpec| attribute_info attributes[attributes_count];
146
// JVMSpec| }
147
void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
148
ConstMethod* const_method = method->constMethod();
149
u2 line_num_cnt = 0;
150
int stackmap_len = 0;
151
int local_variable_table_length = 0;
152
int local_variable_type_table_length = 0;
153
154
// compute number and length of attributes
155
int attr_count = 0;
156
int attr_size = 0;
157
if (const_method->has_linenumber_table()) {
158
line_num_cnt = line_number_table_entries(method);
159
if (line_num_cnt != 0) {
160
++attr_count;
161
// Compute the complete size of the line number table attribute:
162
// LineNumberTable_attribute {
163
// u2 attribute_name_index;
164
// u4 attribute_length;
165
// u2 line_number_table_length;
166
// { u2 start_pc;
167
// u2 line_number;
168
// } line_number_table[line_number_table_length];
169
// }
170
attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
171
}
172
}
173
if (method->has_stackmap_table()) {
174
stackmap_len = method->stackmap_data()->length();
175
if (stackmap_len != 0) {
176
++attr_count;
177
// Compute the size of the stack map table attribute (VM stores raw):
178
// StackMapTable_attribute {
179
// u2 attribute_name_index;
180
// u4 attribute_length;
181
// u2 number_of_entries;
182
// stack_map_frame_entries[number_of_entries];
183
// }
184
attr_size += 2 + 4 + stackmap_len;
185
}
186
}
187
if (method->has_localvariable_table()) {
188
local_variable_table_length = method->localvariable_table_length();
189
if (local_variable_table_length != 0) {
190
++attr_count;
191
// Compute the size of the local variable table attribute (VM stores raw):
192
// LocalVariableTable_attribute {
193
// u2 attribute_name_index;
194
// u4 attribute_length;
195
// u2 local_variable_table_length;
196
// {
197
// u2 start_pc;
198
// u2 length;
199
// u2 name_index;
200
// u2 descriptor_index;
201
// u2 index;
202
// }
203
attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2);
204
205
// Local variables with generic signatures must have LVTT entries
206
LocalVariableTableElement *elem = method->localvariable_table_start();
207
for (int idx = 0; idx < local_variable_table_length; idx++) {
208
if (elem[idx].signature_cp_index != 0) {
209
local_variable_type_table_length++;
210
}
211
}
212
213
if (local_variable_type_table_length != 0) {
214
++attr_count;
215
// Compute the size of the local variable type table attribute (VM stores raw):
216
// LocalVariableTypeTable_attribute {
217
// u2 attribute_name_index;
218
// u4 attribute_length;
219
// u2 local_variable_type_table_length;
220
// {
221
// u2 start_pc;
222
// u2 length;
223
// u2 name_index;
224
// u2 signature_index;
225
// u2 index;
226
// }
227
attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2);
228
}
229
}
230
}
231
232
ExceptionTable exception_table(method());
233
int exception_table_length = exception_table.length();
234
int code_size = const_method->code_size();
235
int size =
236
2+2+4 + // max_stack, max_locals, code_length
237
code_size + // code
238
2 + // exception_table_length
239
(2+2+2+2) * exception_table_length + // exception_table
240
2 + // attributes_count
241
attr_size; // attributes
242
243
write_attribute_name_index("Code");
244
write_u4(size);
245
write_u2(method->verifier_max_stack());
246
write_u2(method->max_locals());
247
write_u4(code_size);
248
copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
249
write_u2(exception_table_length);
250
for (int index = 0; index < exception_table_length; index++) {
251
write_u2(exception_table.start_pc(index));
252
write_u2(exception_table.end_pc(index));
253
write_u2(exception_table.handler_pc(index));
254
write_u2(exception_table.catch_type_index(index));
255
}
256
write_u2(attr_count);
257
if (line_num_cnt != 0) {
258
write_line_number_table_attribute(method, line_num_cnt);
259
}
260
if (stackmap_len != 0) {
261
write_stackmap_table_attribute(method, stackmap_len);
262
}
263
if (local_variable_table_length != 0) {
264
write_local_variable_table_attribute(method, local_variable_table_length);
265
}
266
if (local_variable_type_table_length != 0) {
267
write_local_variable_type_table_attribute(method, local_variable_type_table_length);
268
}
269
}
270
271
// Write Exceptions attribute
272
// JVMSpec| Exceptions_attribute {
273
// JVMSpec| u2 attribute_name_index;
274
// JVMSpec| u4 attribute_length;
275
// JVMSpec| u2 number_of_exceptions;
276
// JVMSpec| u2 exception_index_table[number_of_exceptions];
277
// JVMSpec| }
278
void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) {
279
CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
280
int checked_exceptions_length = const_method->checked_exceptions_length();
281
int size =
282
2 + // number_of_exceptions
283
2 * checked_exceptions_length; // exception_index_table
284
285
write_attribute_name_index("Exceptions");
286
write_u4(size);
287
write_u2(checked_exceptions_length);
288
for (int index = 0; index < checked_exceptions_length; index++) {
289
write_u2(checked_exceptions[index].class_cp_index);
290
}
291
}
292
293
// Write SourceFile attribute
294
// JVMSpec| SourceFile_attribute {
295
// JVMSpec| u2 attribute_name_index;
296
// JVMSpec| u4 attribute_length;
297
// JVMSpec| u2 sourcefile_index;
298
// JVMSpec| }
299
void JvmtiClassFileReconstituter::write_source_file_attribute() {
300
assert(ikh()->source_file_name() != NULL, "caller must check");
301
302
write_attribute_name_index("SourceFile");
303
write_u4(2); // always length 2
304
write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
305
}
306
307
// Write SourceDebugExtension attribute
308
// JSR45| SourceDebugExtension_attribute {
309
// JSR45| u2 attribute_name_index;
310
// JSR45| u4 attribute_length;
311
// JSR45| u1 debug_extension[attribute_length];
312
// JSR45| }
313
void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
314
assert(ikh()->source_debug_extension() != NULL, "caller must check");
315
316
write_attribute_name_index("SourceDebugExtension");
317
int len = (int)strlen(ikh()->source_debug_extension());
318
write_u4(len);
319
u1* ext = (u1*)ikh()->source_debug_extension();
320
for (int i=0; i<len; i++) {
321
write_u1(ext[i]);
322
}
323
}
324
325
// Write (generic) Signature attribute
326
// JVMSpec| Signature_attribute {
327
// JVMSpec| u2 attribute_name_index;
328
// JVMSpec| u4 attribute_length;
329
// JVMSpec| u2 signature_index;
330
// JVMSpec| }
331
void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
332
write_attribute_name_index("Signature");
333
write_u4(2); // always length 2
334
write_u2(generic_signature_index);
335
}
336
337
// Compute the number of entries in the InnerClasses attribute
338
u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
339
InnerClassesIterator iter(ikh());
340
return iter.length();
341
}
342
343
// Write an annotation attribute. The VM stores them in raw form, so all we need
344
// to do is add the attrubute name and fill in the length.
345
// JSR202| *Annotations_attribute {
346
// JSR202| u2 attribute_name_index;
347
// JSR202| u4 attribute_length;
348
// JSR202| ...
349
// JSR202| }
350
void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
351
AnnotationArray* annos) {
352
u4 length = annos->length();
353
write_attribute_name_index(attr_name);
354
write_u4(length);
355
memcpy(writeable_address(length), annos->adr_at(0), length);
356
}
357
358
// BootstrapMethods_attribute {
359
// u2 attribute_name_index;
360
// u4 attribute_length;
361
// u2 num_bootstrap_methods;
362
// { u2 bootstrap_method_ref;
363
// u2 num_bootstrap_arguments;
364
// u2 bootstrap_arguments[num_bootstrap_arguments];
365
// } bootstrap_methods[num_bootstrap_methods];
366
// }
367
void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() {
368
Array<u2>* operands = cpool()->operands();
369
write_attribute_name_index("BootstrapMethods");
370
int num_bootstrap_methods = ConstantPool::operand_array_length(operands);
371
372
// calculate length of attribute
373
int length = sizeof(u2); // num_bootstrap_methods
374
for (int n = 0; n < num_bootstrap_methods; n++) {
375
u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
376
length += sizeof(u2); // bootstrap_method_ref
377
length += sizeof(u2); // num_bootstrap_arguments
378
length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments]
379
}
380
write_u4(length);
381
382
// write attribute
383
write_u2(num_bootstrap_methods);
384
for (int n = 0; n < num_bootstrap_methods; n++) {
385
u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n);
386
u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
387
write_u2(bootstrap_method_ref);
388
write_u2(num_bootstrap_arguments);
389
for (int arg = 0; arg < num_bootstrap_arguments; arg++) {
390
u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg);
391
write_u2(bootstrap_argument);
392
}
393
}
394
}
395
396
397
// Write InnerClasses attribute
398
// JVMSpec| InnerClasses_attribute {
399
// JVMSpec| u2 attribute_name_index;
400
// JVMSpec| u4 attribute_length;
401
// JVMSpec| u2 number_of_classes;
402
// JVMSpec| { u2 inner_class_info_index;
403
// JVMSpec| u2 outer_class_info_index;
404
// JVMSpec| u2 inner_name_index;
405
// JVMSpec| u2 inner_class_access_flags;
406
// JVMSpec| } classes[number_of_classes];
407
// JVMSpec| }
408
void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
409
InnerClassesIterator iter(ikh());
410
guarantee(iter.length() != 0 && iter.length() == length,
411
"caller must check");
412
u2 entry_count = length / InstanceKlass::inner_class_next_offset;
413
u4 size = 2 + entry_count * (2+2+2+2);
414
415
write_attribute_name_index("InnerClasses");
416
write_u4(size);
417
write_u2(entry_count);
418
for (; !iter.done(); iter.next()) {
419
write_u2(iter.inner_class_info_index());
420
write_u2(iter.outer_class_info_index());
421
write_u2(iter.inner_name_index());
422
write_u2(iter.inner_access_flags());
423
}
424
}
425
426
// Write Synthetic attribute
427
// JVMSpec| Synthetic_attribute {
428
// JVMSpec| u2 attribute_name_index;
429
// JVMSpec| u4 attribute_length;
430
// JVMSpec| }
431
void JvmtiClassFileReconstituter::write_synthetic_attribute() {
432
write_attribute_name_index("Synthetic");
433
write_u4(0); //length always zero
434
}
435
436
// Compute size of LineNumberTable
437
u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
438
// The line number table is compressed so we don't know how big it is until decompressed.
439
// Decompression is really fast so we just do it twice.
440
u2 num_entries = 0;
441
CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
442
while (stream.read_pair()) {
443
num_entries++;
444
}
445
return num_entries;
446
}
447
448
// Write LineNumberTable attribute
449
// JVMSpec| LineNumberTable_attribute {
450
// JVMSpec| u2 attribute_name_index;
451
// JVMSpec| u4 attribute_length;
452
// JVMSpec| u2 line_number_table_length;
453
// JVMSpec| { u2 start_pc;
454
// JVMSpec| u2 line_number;
455
// JVMSpec| } line_number_table[line_number_table_length];
456
// JVMSpec| }
457
void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
458
u2 num_entries) {
459
460
write_attribute_name_index("LineNumberTable");
461
write_u4(2 + num_entries * (2 + 2));
462
write_u2(num_entries);
463
464
CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
465
while (stream.read_pair()) {
466
write_u2(stream.bci());
467
write_u2(stream.line());
468
}
469
}
470
471
// Write LocalVariableTable attribute
472
// JVMSpec| LocalVariableTable_attribute {
473
// JVMSpec| u2 attribute_name_index;
474
// JVMSpec| u4 attribute_length;
475
// JVMSpec| u2 local_variable_table_length;
476
// JVMSpec| { u2 start_pc;
477
// JVMSpec| u2 length;
478
// JVMSpec| u2 name_index;
479
// JVMSpec| u2 descriptor_index;
480
// JVMSpec| u2 index;
481
// JVMSpec| } local_variable_table[local_variable_table_length];
482
// JVMSpec| }
483
void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) {
484
write_attribute_name_index("LocalVariableTable");
485
write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
486
write_u2(num_entries);
487
488
assert(method->localvariable_table_length() == num_entries, "just checking");
489
490
LocalVariableTableElement *elem = method->localvariable_table_start();
491
for (int j=0; j<method->localvariable_table_length(); j++) {
492
write_u2(elem->start_bci);
493
write_u2(elem->length);
494
write_u2(elem->name_cp_index);
495
write_u2(elem->descriptor_cp_index);
496
write_u2(elem->slot);
497
elem++;
498
}
499
}
500
501
// Write LocalVariableTypeTable attribute
502
// JVMSpec| LocalVariableTypeTable_attribute {
503
// JVMSpec| u2 attribute_name_index;
504
// JVMSpec| u4 attribute_length;
505
// JVMSpec| u2 local_variable_type_table_length;
506
// JVMSpec| { u2 start_pc;
507
// JVMSpec| u2 length;
508
// JVMSpec| u2 name_index;
509
// JVMSpec| u2 signature_index;
510
// JVMSpec| u2 index;
511
// JVMSpec| } local_variable_type_table[local_variable_type_table_length];
512
// JVMSpec| }
513
void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) {
514
write_attribute_name_index("LocalVariableTypeTable");
515
write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
516
write_u2(num_entries);
517
518
LocalVariableTableElement *elem = method->localvariable_table_start();
519
for (int j=0; j<method->localvariable_table_length(); j++) {
520
if (elem->signature_cp_index > 0) {
521
// Local variable has a generic signature - write LVTT attribute entry
522
write_u2(elem->start_bci);
523
write_u2(elem->length);
524
write_u2(elem->name_cp_index);
525
write_u2(elem->signature_cp_index);
526
write_u2(elem->slot);
527
num_entries--;
528
}
529
elem++;
530
}
531
assert(num_entries == 0, "just checking");
532
}
533
534
// Write stack map table attribute
535
// JSR-202| StackMapTable_attribute {
536
// JSR-202| u2 attribute_name_index;
537
// JSR-202| u4 attribute_length;
538
// JSR-202| u2 number_of_entries;
539
// JSR-202| stack_map_frame_entries[number_of_entries];
540
// JSR-202| }
541
void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
542
int stackmap_len) {
543
544
write_attribute_name_index("StackMapTable");
545
write_u4(stackmap_len);
546
memcpy(
547
writeable_address(stackmap_len),
548
(void*)(method->stackmap_data()->adr_at(0)),
549
stackmap_len);
550
}
551
552
// Write one method_info structure
553
// JVMSpec| method_info {
554
// JVMSpec| u2 access_flags;
555
// JVMSpec| u2 name_index;
556
// JVMSpec| u2 descriptor_index;
557
// JVMSpec| u2 attributes_count;
558
// JVMSpec| attribute_info attributes[attributes_count];
559
// JVMSpec| }
560
void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
561
AccessFlags access_flags = method->access_flags();
562
ConstMethod* const_method = method->constMethod();
563
u2 generic_signature_index = const_method->generic_signature_index();
564
AnnotationArray* anno = method->annotations();
565
AnnotationArray* param_anno = method->parameter_annotations();
566
AnnotationArray* default_anno = method->annotation_default();
567
AnnotationArray* type_anno = method->type_annotations();
568
569
// skip generated default interface methods
570
if (method->is_overpass()) {
571
return;
572
}
573
574
write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
575
write_u2(const_method->name_index());
576
write_u2(const_method->signature_index());
577
578
// write attributes in the same order javac does, so we can test with byte for
579
// byte comparison
580
int attr_count = 0;
581
if (const_method->code_size() != 0) {
582
++attr_count; // has Code attribute
583
}
584
if (const_method->has_checked_exceptions()) {
585
++attr_count; // has Exceptions attribute
586
}
587
if (default_anno != NULL) {
588
++attr_count; // has AnnotationDefault attribute
589
}
590
// Deprecated attribute would go here
591
if (access_flags.is_synthetic()) { // FIXME
592
// ++attr_count;
593
}
594
if (generic_signature_index != 0) {
595
++attr_count;
596
}
597
if (anno != NULL) {
598
++attr_count; // has RuntimeVisibleAnnotations attribute
599
}
600
if (param_anno != NULL) {
601
++attr_count; // has RuntimeVisibleParameterAnnotations attribute
602
}
603
if (type_anno != NULL) {
604
++attr_count; // has RuntimeVisibleTypeAnnotations attribute
605
}
606
607
write_u2(attr_count);
608
if (const_method->code_size() > 0) {
609
write_code_attribute(method);
610
}
611
if (const_method->has_checked_exceptions()) {
612
write_exceptions_attribute(const_method);
613
}
614
if (default_anno != NULL) {
615
write_annotations_attribute("AnnotationDefault", default_anno);
616
}
617
// Deprecated attribute would go here
618
if (access_flags.is_synthetic()) {
619
// write_synthetic_attribute();
620
}
621
if (generic_signature_index != 0) {
622
write_signature_attribute(generic_signature_index);
623
}
624
if (anno != NULL) {
625
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
626
}
627
if (param_anno != NULL) {
628
write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
629
}
630
if (type_anno != NULL) {
631
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
632
}
633
}
634
635
// Write the class attributes portion of ClassFile structure
636
// JVMSpec| u2 attributes_count;
637
// JVMSpec| attribute_info attributes[attributes_count];
638
void JvmtiClassFileReconstituter::write_class_attributes() {
639
u2 inner_classes_length = inner_classes_attribute_length();
640
Symbol* generic_signature = ikh()->generic_signature();
641
AnnotationArray* anno = ikh()->class_annotations();
642
AnnotationArray* type_anno = ikh()->class_type_annotations();
643
644
int attr_count = 0;
645
if (generic_signature != NULL) {
646
++attr_count;
647
}
648
if (ikh()->source_file_name() != NULL) {
649
++attr_count;
650
}
651
if (ikh()->source_debug_extension() != NULL) {
652
++attr_count;
653
}
654
if (inner_classes_length > 0) {
655
++attr_count;
656
}
657
if (anno != NULL) {
658
++attr_count; // has RuntimeVisibleAnnotations attribute
659
}
660
if (type_anno != NULL) {
661
++attr_count; // has RuntimeVisibleTypeAnnotations attribute
662
}
663
if (cpool()->operands() != NULL) {
664
++attr_count;
665
}
666
667
write_u2(attr_count);
668
669
if (generic_signature != NULL) {
670
write_signature_attribute(symbol_to_cpool_index(generic_signature));
671
}
672
if (ikh()->source_file_name() != NULL) {
673
write_source_file_attribute();
674
}
675
if (ikh()->source_debug_extension() != NULL) {
676
write_source_debug_extension_attribute();
677
}
678
if (inner_classes_length > 0) {
679
write_inner_classes_attribute(inner_classes_length);
680
}
681
if (anno != NULL) {
682
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
683
}
684
if (type_anno != NULL) {
685
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
686
}
687
if (cpool()->operands() != NULL) {
688
write_bootstrapmethod_attribute();
689
}
690
}
691
692
// Write the method information portion of ClassFile structure
693
// JVMSpec| u2 methods_count;
694
// JVMSpec| method_info methods[methods_count];
695
void JvmtiClassFileReconstituter::write_method_infos() {
696
HandleMark hm(thread());
697
Array<Method*>* methods = ikh()->methods();
698
int num_methods = methods->length();
699
int num_overpass = 0;
700
701
// count the generated default interface methods
702
// these will not be re-created by write_method_info
703
// and should not be included in the total count
704
for (int index = 0; index < num_methods; index++) {
705
Method* method = methods->at(index);
706
if (method->is_overpass()) {
707
num_overpass++;
708
}
709
}
710
711
write_u2(num_methods - num_overpass);
712
if (JvmtiExport::can_maintain_original_method_order()) {
713
int index;
714
int original_index;
715
intArray method_order(num_methods, 0);
716
717
// invert the method order mapping
718
for (index = 0; index < num_methods; index++) {
719
original_index = ikh()->method_ordering()->at(index);
720
assert(original_index >= 0 && original_index < num_methods,
721
"invalid original method index");
722
method_order.at_put(original_index, index);
723
}
724
725
// write in original order
726
for (original_index = 0; original_index < num_methods; original_index++) {
727
index = method_order.at(original_index);
728
methodHandle method(thread(), methods->at(index));
729
write_method_info(method);
730
}
731
} else {
732
// method order not preserved just dump the method infos
733
for (int index = 0; index < num_methods; index++) {
734
methodHandle method(thread(), methods->at(index));
735
write_method_info(method);
736
}
737
}
738
}
739
740
void JvmtiClassFileReconstituter::write_class_file_format() {
741
ReallocMark();
742
743
// JVMSpec| ClassFile {
744
// JVMSpec| u4 magic;
745
write_u4(0xCAFEBABE);
746
747
// JVMSpec| u2 minor_version;
748
// JVMSpec| u2 major_version;
749
write_u2(ikh()->minor_version());
750
u2 major = ikh()->major_version();
751
write_u2(major);
752
753
// JVMSpec| u2 constant_pool_count;
754
// JVMSpec| cp_info constant_pool[constant_pool_count-1];
755
write_u2(cpool()->length());
756
copy_cpool_bytes(writeable_address(cpool_size()));
757
758
// JVMSpec| u2 access_flags;
759
write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
760
761
// JVMSpec| u2 this_class;
762
// JVMSpec| u2 super_class;
763
write_u2(class_symbol_to_cpool_index(ikh()->name()));
764
Klass* super_class = ikh()->super();
765
write_u2(super_class == NULL? 0 : // zero for java.lang.Object
766
class_symbol_to_cpool_index(super_class->name()));
767
768
// JVMSpec| u2 interfaces_count;
769
// JVMSpec| u2 interfaces[interfaces_count];
770
Array<Klass*>* interfaces = ikh()->local_interfaces();
771
int num_interfaces = interfaces->length();
772
write_u2(num_interfaces);
773
for (int index = 0; index < num_interfaces; index++) {
774
HandleMark hm(thread());
775
instanceKlassHandle iikh(thread(), interfaces->at(index));
776
write_u2(class_symbol_to_cpool_index(iikh->name()));
777
}
778
779
// JVMSpec| u2 fields_count;
780
// JVMSpec| field_info fields[fields_count];
781
write_field_infos();
782
783
// JVMSpec| u2 methods_count;
784
// JVMSpec| method_info methods[methods_count];
785
write_method_infos();
786
787
// JVMSpec| u2 attributes_count;
788
// JVMSpec| attribute_info attributes[attributes_count];
789
// JVMSpec| } /* end ClassFile 8?
790
write_class_attributes();
791
}
792
793
address JvmtiClassFileReconstituter::writeable_address(size_t size) {
794
size_t used_size = _buffer_ptr - _buffer;
795
if (size + used_size >= _buffer_size) {
796
// compute the new buffer size: must be at least twice as big as before
797
// plus whatever new is being used; then convert to nice clean block boundary
798
size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
799
* initial_buffer_size;
800
801
// VM goes belly-up if the memory isn't available, so cannot do OOM processing
802
_buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
803
_buffer_size = new_buffer_size;
804
_buffer_ptr = _buffer + used_size;
805
}
806
u1* ret_ptr = _buffer_ptr;
807
_buffer_ptr += size;
808
return ret_ptr;
809
}
810
811
void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
812
TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name));
813
assert(sym != NULL, "attribute name symbol not found");
814
u2 attr_name_index = symbol_to_cpool_index(sym);
815
assert(attr_name_index != 0, "attribute name symbol not in constant pool");
816
write_u2(attr_name_index);
817
}
818
819
void JvmtiClassFileReconstituter::write_u1(u1 x) {
820
*writeable_address(1) = x;
821
}
822
823
void JvmtiClassFileReconstituter::write_u2(u2 x) {
824
Bytes::put_Java_u2(writeable_address(2), x);
825
}
826
827
void JvmtiClassFileReconstituter::write_u4(u4 x) {
828
Bytes::put_Java_u4(writeable_address(4), x);
829
}
830
831
void JvmtiClassFileReconstituter::write_u8(u8 x) {
832
Bytes::put_Java_u8(writeable_address(8), x);
833
}
834
835
void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
836
unsigned char* bytecodes) {
837
// use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
838
// and the breakpoint bytecode are converted to their original bytecodes.
839
840
BytecodeStream bs(mh);
841
842
unsigned char* p = bytecodes;
843
Bytecodes::Code code;
844
bool is_rewritten = mh->method_holder()->is_rewritten();
845
846
while ((code = bs.next()) >= 0) {
847
assert(Bytecodes::is_java_code(code), "sanity check");
848
assert(code != Bytecodes::_breakpoint, "sanity check");
849
850
// length of bytecode (mnemonic + operands)
851
address bcp = bs.bcp();
852
int len = bs.instruction_size();
853
assert(len > 0, "length must be > 0");
854
855
// copy the bytecodes
856
*p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
857
if (len > 1) {
858
memcpy(p+1, bcp+1, len-1);
859
}
860
861
// During linking the get/put and invoke instructions are rewritten
862
// with an index into the constant pool cache. The original constant
863
// pool index must be returned to caller. Rewrite the index.
864
if (is_rewritten && len > 1) {
865
bool is_wide = false;
866
switch (code) {
867
case Bytecodes::_getstatic : // fall through
868
case Bytecodes::_putstatic : // fall through
869
case Bytecodes::_getfield : // fall through
870
case Bytecodes::_putfield : // fall through
871
case Bytecodes::_invokevirtual : // fall through
872
case Bytecodes::_invokespecial : // fall through
873
case Bytecodes::_invokestatic : // fall through
874
case Bytecodes::_invokedynamic : // fall through
875
case Bytecodes::_invokeinterface : {
876
assert(len == 3 ||
877
(code == Bytecodes::_invokeinterface && len == 5) ||
878
(code == Bytecodes::_invokedynamic && len == 5),
879
"sanity check");
880
881
int cpci = Bytes::get_native_u2(bcp+1);
882
bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic);
883
ConstantPoolCacheEntry* entry;
884
if (is_invokedynamic) {
885
cpci = Bytes::get_native_u4(bcp+1);
886
entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
887
} else {
888
// cache cannot be pre-fetched since some classes won't have it yet
889
entry = mh->constants()->cache()->entry_at(cpci);
890
}
891
int i = entry->constant_pool_index();
892
assert(i < mh->constants()->length(), "sanity check");
893
Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering
894
if (is_invokedynamic) *(p+3) = *(p+4) = 0;
895
break;
896
}
897
case Bytecodes::_ldc_w:
898
is_wide = true; // fall through
899
case Bytecodes::_ldc: {
900
if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) {
901
int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1));
902
int i = mh->constants()->object_to_cp_index(cpci);
903
assert(i < mh->constants()->length(), "sanity check");
904
if (is_wide) {
905
Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering
906
} else {
907
*(p+1) = (u1)i;
908
}
909
}
910
break;
911
}
912
}
913
}
914
915
p += len;
916
}
917
}
918
919