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