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