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