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