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