jvmtiClassFileReconstituter.cpp revision 3602:da91efe96a93
1/* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "precompiled.hpp" 26#include "classfile/symbolTable.hpp" 27#include "interpreter/bytecodeStream.hpp" 28#include "oops/fieldStreams.hpp" 29#include "prims/jvmtiClassFileReconstituter.hpp" 30#include "runtime/signature.hpp" 31#ifdef TARGET_ARCH_x86 32# include "bytes_x86.hpp" 33#endif 34#ifdef TARGET_ARCH_sparc 35# include "bytes_sparc.hpp" 36#endif 37#ifdef TARGET_ARCH_zero 38# include "bytes_zero.hpp" 39#endif 40#ifdef TARGET_ARCH_arm 41# include "bytes_arm.hpp" 42#endif 43#ifdef TARGET_ARCH_ppc 44# include "bytes_ppc.hpp" 45#endif 46// FIXME: add Deprecated, LVTT attributes 47// FIXME: fix Synthetic attribute 48// FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes() 49 50 51// Write the field information portion of ClassFile structure 52// JVMSpec| u2 fields_count; 53// JVMSpec| field_info fields[fields_count]; 54void JvmtiClassFileReconstituter::write_field_infos() { 55 HandleMark hm(thread()); 56 Array<AnnotationArray*>* fields_anno = ikh()->fields_annotations(); 57 58 // Compute the real number of Java fields 59 int java_fields = ikh()->java_fields_count(); 60 61 write_u2(java_fields); 62 for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) { 63 AccessFlags access_flags = fs.access_flags(); 64 int name_index = fs.name_index(); 65 int signature_index = fs.signature_index(); 66 int initial_value_index = fs.initval_index(); 67 guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field"); 68 // int offset = ikh()->field_offset( index ); 69 int generic_signature_index = fs.generic_signature_index(); 70 AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index()); 71 72 // JVMSpec| field_info { 73 // JVMSpec| u2 access_flags; 74 // JVMSpec| u2 name_index; 75 // JVMSpec| u2 descriptor_index; 76 // JVMSpec| u2 attributes_count; 77 // JVMSpec| attribute_info attributes[attributes_count]; 78 // JVMSpec| } 79 80 write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); 81 write_u2(name_index); 82 write_u2(signature_index); 83 int attr_count = 0; 84 if (initial_value_index != 0) { 85 ++attr_count; 86 } 87 if (access_flags.is_synthetic()) { 88 // ++attr_count; 89 } 90 if (generic_signature_index != 0) { 91 ++attr_count; 92 } 93 if (anno != NULL) { 94 ++attr_count; // has RuntimeVisibleAnnotations attribute 95 } 96 97 write_u2(attr_count); 98 99 if (initial_value_index != 0) { 100 write_attribute_name_index("ConstantValue"); 101 write_u4(2); //length always 2 102 write_u2(initial_value_index); 103 } 104 if (access_flags.is_synthetic()) { 105 // write_synthetic_attribute(); 106 } 107 if (generic_signature_index != 0) { 108 write_signature_attribute(generic_signature_index); 109 } 110 if (anno != NULL) { 111 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 112 } 113 } 114} 115 116// Write Code attribute 117// JVMSpec| Code_attribute { 118// JVMSpec| u2 attribute_name_index; 119// JVMSpec| u4 attribute_length; 120// JVMSpec| u2 max_stack; 121// JVMSpec| u2 max_locals; 122// JVMSpec| u4 code_length; 123// JVMSpec| u1 code[code_length]; 124// JVMSpec| u2 exception_table_length; 125// JVMSpec| { u2 start_pc; 126// JVMSpec| u2 end_pc; 127// JVMSpec| u2 handler_pc; 128// JVMSpec| u2 catch_type; 129// JVMSpec| } exception_table[exception_table_length]; 130// JVMSpec| u2 attributes_count; 131// JVMSpec| attribute_info attributes[attributes_count]; 132// JVMSpec| } 133void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) { 134 ConstMethod* const_method = method->constMethod(); 135 u2 line_num_cnt = 0; 136 int stackmap_len = 0; 137 int local_variable_table_length = 0; 138 139 // compute number and length of attributes 140 int attr_count = 0; 141 int attr_size = 0; 142 if (const_method->has_linenumber_table()) { 143 line_num_cnt = line_number_table_entries(method); 144 if (line_num_cnt != 0) { 145 ++attr_count; 146 // Compute the complete size of the line number table attribute: 147 // LineNumberTable_attribute { 148 // u2 attribute_name_index; 149 // u4 attribute_length; 150 // u2 line_number_table_length; 151 // { u2 start_pc; 152 // u2 line_number; 153 // } line_number_table[line_number_table_length]; 154 // } 155 attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2); 156 } 157 } 158 if (method->has_stackmap_table()) { 159 stackmap_len = method->stackmap_data()->length(); 160 if (stackmap_len != 0) { 161 ++attr_count; 162 // Compute the size of the stack map table attribute (VM stores raw): 163 // StackMapTable_attribute { 164 // u2 attribute_name_index; 165 // u4 attribute_length; 166 // u2 number_of_entries; 167 // stack_map_frame_entries[number_of_entries]; 168 // } 169 attr_size += 2 + 4 + stackmap_len; 170 } 171 } 172 if (method->has_localvariable_table()) { 173 local_variable_table_length = method->localvariable_table_length(); 174 ++attr_count; 175 if (local_variable_table_length != 0) { 176 // Compute the size of the local variable table attribute (VM stores raw): 177 // LocalVariableTable_attribute { 178 // u2 attribute_name_index; 179 // u4 attribute_length; 180 // u2 local_variable_table_length; 181 // { 182 // u2 start_pc; 183 // u2 length; 184 // u2 name_index; 185 // u2 descriptor_index; 186 // u2 index; 187 // } 188 attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2); 189 } 190 } 191 192 ExceptionTable exception_table(method()); 193 int exception_table_length = exception_table.length(); 194 int code_size = const_method->code_size(); 195 int size = 196 2+2+4 + // max_stack, max_locals, code_length 197 code_size + // code 198 2 + // exception_table_length 199 (2+2+2+2) * exception_table_length + // exception_table 200 2 + // attributes_count 201 attr_size; // attributes 202 203 write_attribute_name_index("Code"); 204 write_u4(size); 205 write_u2(method->max_stack()); 206 write_u2(method->max_locals()); 207 write_u4(code_size); 208 copy_bytecodes(method, (unsigned char*)writeable_address(code_size)); 209 write_u2(exception_table_length); 210 for (int index = 0; index < exception_table_length; index++) { 211 write_u2(exception_table.start_pc(index)); 212 write_u2(exception_table.end_pc(index)); 213 write_u2(exception_table.handler_pc(index)); 214 write_u2(exception_table.catch_type_index(index)); 215 } 216 write_u2(attr_count); 217 if (line_num_cnt != 0) { 218 write_line_number_table_attribute(method, line_num_cnt); 219 } 220 if (stackmap_len != 0) { 221 write_stackmap_table_attribute(method, stackmap_len); 222 } 223 if (local_variable_table_length != 0) { 224 write_local_variable_table_attribute(method, local_variable_table_length); 225 } 226} 227 228// Write Exceptions attribute 229// JVMSpec| Exceptions_attribute { 230// JVMSpec| u2 attribute_name_index; 231// JVMSpec| u4 attribute_length; 232// JVMSpec| u2 number_of_exceptions; 233// JVMSpec| u2 exception_index_table[number_of_exceptions]; 234// JVMSpec| } 235void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) { 236 CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start(); 237 int checked_exceptions_length = const_method->checked_exceptions_length(); 238 int size = 239 2 + // number_of_exceptions 240 2 * checked_exceptions_length; // exception_index_table 241 242 write_attribute_name_index("Exceptions"); 243 write_u4(size); 244 write_u2(checked_exceptions_length); 245 for (int index = 0; index < checked_exceptions_length; index++) { 246 write_u2(checked_exceptions[index].class_cp_index); 247 } 248} 249 250// Write SourceFile attribute 251// JVMSpec| SourceFile_attribute { 252// JVMSpec| u2 attribute_name_index; 253// JVMSpec| u4 attribute_length; 254// JVMSpec| u2 sourcefile_index; 255// JVMSpec| } 256void JvmtiClassFileReconstituter::write_source_file_attribute() { 257 assert(ikh()->source_file_name() != NULL, "caller must check"); 258 259 write_attribute_name_index("SourceFile"); 260 write_u4(2); // always length 2 261 write_u2(symbol_to_cpool_index(ikh()->source_file_name())); 262} 263 264// Write SourceDebugExtension attribute 265// JSR45| SourceDebugExtension_attribute { 266// JSR45| u2 attribute_name_index; 267// JSR45| u4 attribute_length; 268// JSR45| u1 debug_extension[attribute_length]; 269// JSR45| } 270void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() { 271 assert(ikh()->source_debug_extension() != NULL, "caller must check"); 272 273 write_attribute_name_index("SourceDebugExtension"); 274 int len = (int)strlen(ikh()->source_debug_extension()); 275 write_u4(len); 276 u1* ext = (u1*)ikh()->source_debug_extension(); 277 for (int i=0; i<len; i++) { 278 write_u1(ext[i]); 279 } 280} 281 282// Write (generic) Signature attribute 283// JVMSpec| Signature_attribute { 284// JVMSpec| u2 attribute_name_index; 285// JVMSpec| u4 attribute_length; 286// JVMSpec| u2 signature_index; 287// JVMSpec| } 288void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) { 289 write_attribute_name_index("Signature"); 290 write_u4(2); // always length 2 291 write_u2(generic_signature_index); 292} 293 294// Compute the number of entries in the InnerClasses attribute 295u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() { 296 InnerClassesIterator iter(ikh()); 297 return iter.length(); 298} 299 300// Write an annotation attribute. The VM stores them in raw form, so all we need 301// to do is add the attrubute name and fill in the length. 302// JSR202| *Annotations_attribute { 303// JSR202| u2 attribute_name_index; 304// JSR202| u4 attribute_length; 305// JSR202| ... 306// JSR202| } 307void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name, 308 AnnotationArray* annos) { 309 u4 length = annos->length(); 310 write_attribute_name_index(attr_name); 311 write_u4(length); 312 memcpy(writeable_address(length), annos->adr_at(0), length); 313} 314 315 316// Write InnerClasses attribute 317// JVMSpec| InnerClasses_attribute { 318// JVMSpec| u2 attribute_name_index; 319// JVMSpec| u4 attribute_length; 320// JVMSpec| u2 number_of_classes; 321// JVMSpec| { u2 inner_class_info_index; 322// JVMSpec| u2 outer_class_info_index; 323// JVMSpec| u2 inner_name_index; 324// JVMSpec| u2 inner_class_access_flags; 325// JVMSpec| } classes[number_of_classes]; 326// JVMSpec| } 327void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) { 328 InnerClassesIterator iter(ikh()); 329 guarantee(iter.length() != 0 && iter.length() == length, 330 "caller must check"); 331 u2 entry_count = length / InstanceKlass::inner_class_next_offset; 332 u4 size = 2 + entry_count * (2+2+2+2); 333 334 write_attribute_name_index("InnerClasses"); 335 write_u4(size); 336 write_u2(entry_count); 337 for (; !iter.done(); iter.next()) { 338 write_u2(iter.inner_class_info_index()); 339 write_u2(iter.outer_class_info_index()); 340 write_u2(iter.inner_name_index()); 341 write_u2(iter.inner_access_flags()); 342 } 343} 344 345// Write Synthetic attribute 346// JVMSpec| Synthetic_attribute { 347// JVMSpec| u2 attribute_name_index; 348// JVMSpec| u4 attribute_length; 349// JVMSpec| } 350void JvmtiClassFileReconstituter::write_synthetic_attribute() { 351 write_attribute_name_index("Synthetic"); 352 write_u4(0); //length always zero 353} 354 355// Compute size of LineNumberTable 356u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) { 357 // The line number table is compressed so we don't know how big it is until decompressed. 358 // Decompression is really fast so we just do it twice. 359 u2 num_entries = 0; 360 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 361 while (stream.read_pair()) { 362 num_entries++; 363 } 364 return num_entries; 365} 366 367// Write LineNumberTable attribute 368// JVMSpec| LineNumberTable_attribute { 369// JVMSpec| u2 attribute_name_index; 370// JVMSpec| u4 attribute_length; 371// JVMSpec| u2 line_number_table_length; 372// JVMSpec| { u2 start_pc; 373// JVMSpec| u2 line_number; 374// JVMSpec| } line_number_table[line_number_table_length]; 375// JVMSpec| } 376void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method, 377 u2 num_entries) { 378 379 write_attribute_name_index("LineNumberTable"); 380 write_u4(2 + num_entries * (2 + 2)); 381 write_u2(num_entries); 382 383 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 384 while (stream.read_pair()) { 385 write_u2(stream.bci()); 386 write_u2(stream.line()); 387 } 388} 389 390// Write LineNumberTable attribute 391// JVMSpec| LocalVariableTable_attribute { 392// JVMSpec| u2 attribute_name_index; 393// JVMSpec| u4 attribute_length; 394// JVMSpec| u2 local_variable_table_length; 395// JVMSpec| { u2 start_pc; 396// JVMSpec| u2 length; 397// JVMSpec| u2 name_index; 398// JVMSpec| u2 descriptor_index; 399// JVMSpec| u2 index; 400// JVMSpec| } local_variable_table[local_variable_table_length]; 401// JVMSpec| } 402void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) { 403 write_attribute_name_index("LocalVariableTable"); 404 write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2)); 405 write_u2(num_entries); 406 407 assert(method->localvariable_table_length() == num_entries, "just checking"); 408 409 LocalVariableTableElement *elem = method->localvariable_table_start(); 410 for (int j=0; j<method->localvariable_table_length(); j++) { 411 write_u2(elem->start_bci); 412 write_u2(elem->length); 413 write_u2(elem->name_cp_index); 414 write_u2(elem->descriptor_cp_index); 415 write_u2(elem->slot); 416 elem++; 417 } 418} 419 420// Write stack map table attribute 421// JSR-202| StackMapTable_attribute { 422// JSR-202| u2 attribute_name_index; 423// JSR-202| u4 attribute_length; 424// JSR-202| u2 number_of_entries; 425// JSR-202| stack_map_frame_entries[number_of_entries]; 426// JSR-202| } 427void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method, 428 int stackmap_len) { 429 430 write_attribute_name_index("StackMapTable"); 431 write_u4(stackmap_len); 432 memcpy( 433 writeable_address(stackmap_len), 434 (void*)(method->stackmap_data()->adr_at(0)), 435 stackmap_len); 436} 437 438// Write one method_info structure 439// JVMSpec| method_info { 440// JVMSpec| u2 access_flags; 441// JVMSpec| u2 name_index; 442// JVMSpec| u2 descriptor_index; 443// JVMSpec| u2 attributes_count; 444// JVMSpec| attribute_info attributes[attributes_count]; 445// JVMSpec| } 446void JvmtiClassFileReconstituter::write_method_info(methodHandle method) { 447 AccessFlags access_flags = method->access_flags(); 448 ConstMethod* const_method = method->constMethod(); 449 u2 generic_signature_index = const_method->generic_signature_index(); 450 AnnotationArray* anno = method->annotations(); 451 AnnotationArray* param_anno = method->parameter_annotations(); 452 AnnotationArray* default_anno = method->annotation_default(); 453 454 write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS); 455 write_u2(const_method->name_index()); 456 write_u2(const_method->signature_index()); 457 458 // write attributes in the same order javac does, so we can test with byte for 459 // byte comparison 460 int attr_count = 0; 461 if (const_method->code_size() != 0) { 462 ++attr_count; // has Code attribute 463 } 464 if (const_method->has_checked_exceptions()) { 465 ++attr_count; // has Exceptions attribute 466 } 467 if (default_anno != NULL) { 468 ++attr_count; // has AnnotationDefault attribute 469 } 470 // Deprecated attribute would go here 471 if (access_flags.is_synthetic()) { // FIXME 472 // ++attr_count; 473 } 474 if (generic_signature_index != 0) { 475 ++attr_count; 476 } 477 if (anno != NULL) { 478 ++attr_count; // has RuntimeVisibleAnnotations attribute 479 } 480 if (param_anno != NULL) { 481 ++attr_count; // has RuntimeVisibleParameterAnnotations attribute 482 } 483 484 write_u2(attr_count); 485 if (const_method->code_size() > 0) { 486 write_code_attribute(method); 487 } 488 if (const_method->has_checked_exceptions()) { 489 write_exceptions_attribute(const_method); 490 } 491 if (default_anno != NULL) { 492 write_annotations_attribute("AnnotationDefault", default_anno); 493 } 494 // Deprecated attribute would go here 495 if (access_flags.is_synthetic()) { 496 // write_synthetic_attribute(); 497 } 498 if (generic_signature_index != 0) { 499 write_signature_attribute(generic_signature_index); 500 } 501 if (anno != NULL) { 502 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 503 } 504 if (param_anno != NULL) { 505 write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno); 506 } 507} 508 509// Write the class attributes portion of ClassFile structure 510// JVMSpec| u2 attributes_count; 511// JVMSpec| attribute_info attributes[attributes_count]; 512void JvmtiClassFileReconstituter::write_class_attributes() { 513 u2 inner_classes_length = inner_classes_attribute_length(); 514 Symbol* generic_signature = ikh()->generic_signature(); 515 AnnotationArray* anno = ikh()->class_annotations(); 516 517 int attr_count = 0; 518 if (generic_signature != NULL) { 519 ++attr_count; 520 } 521 if (ikh()->source_file_name() != NULL) { 522 ++attr_count; 523 } 524 if (ikh()->source_debug_extension() != NULL) { 525 ++attr_count; 526 } 527 if (inner_classes_length > 0) { 528 ++attr_count; 529 } 530 if (anno != NULL) { 531 ++attr_count; // has RuntimeVisibleAnnotations attribute 532 } 533 534 write_u2(attr_count); 535 536 if (generic_signature != NULL) { 537 write_signature_attribute(symbol_to_cpool_index(generic_signature)); 538 } 539 if (ikh()->source_file_name() != NULL) { 540 write_source_file_attribute(); 541 } 542 if (ikh()->source_debug_extension() != NULL) { 543 write_source_debug_extension_attribute(); 544 } 545 if (inner_classes_length > 0) { 546 write_inner_classes_attribute(inner_classes_length); 547 } 548 if (anno != NULL) { 549 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 550 } 551} 552 553// Write the method information portion of ClassFile structure 554// JVMSpec| u2 methods_count; 555// JVMSpec| method_info methods[methods_count]; 556void JvmtiClassFileReconstituter::write_method_infos() { 557 HandleMark hm(thread()); 558 Array<Method*>* methods = ikh()->methods(); 559 int num_methods = methods->length(); 560 561 write_u2(num_methods); 562 if (JvmtiExport::can_maintain_original_method_order()) { 563 int index; 564 int original_index; 565 intArray method_order(num_methods, 0); 566 567 // invert the method order mapping 568 for (index = 0; index < num_methods; index++) { 569 original_index = ikh()->method_ordering()->at(index); 570 assert(original_index >= 0 && original_index < num_methods, 571 "invalid original method index"); 572 method_order.at_put(original_index, index); 573 } 574 575 // write in original order 576 for (original_index = 0; original_index < num_methods; original_index++) { 577 index = method_order.at(original_index); 578 methodHandle method(thread(), methods->at(index)); 579 write_method_info(method); 580 } 581 } else { 582 // method order not preserved just dump the method infos 583 for (int index = 0; index < num_methods; index++) { 584 methodHandle method(thread(), methods->at(index)); 585 write_method_info(method); 586 } 587 } 588} 589 590void JvmtiClassFileReconstituter::write_class_file_format() { 591 ReallocMark(); 592 593 // JVMSpec| ClassFile { 594 // JVMSpec| u4 magic; 595 write_u4(0xCAFEBABE); 596 597 // JVMSpec| u2 minor_version; 598 // JVMSpec| u2 major_version; 599 write_u2(ikh()->minor_version()); 600 u2 major = ikh()->major_version(); 601 write_u2(major); 602 603 // JVMSpec| u2 constant_pool_count; 604 // JVMSpec| cp_info constant_pool[constant_pool_count-1]; 605 write_u2(cpool()->length()); 606 copy_cpool_bytes(writeable_address(cpool_size())); 607 608 // JVMSpec| u2 access_flags; 609 write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS); 610 611 // JVMSpec| u2 this_class; 612 // JVMSpec| u2 super_class; 613 write_u2(class_symbol_to_cpool_index(ikh()->name())); 614 Klass* super_class = ikh()->super(); 615 write_u2(super_class == NULL? 0 : // zero for java.lang.Object 616 class_symbol_to_cpool_index(super_class->name())); 617 618 // JVMSpec| u2 interfaces_count; 619 // JVMSpec| u2 interfaces[interfaces_count]; 620 Array<Klass*>* interfaces = ikh()->local_interfaces(); 621 int num_interfaces = interfaces->length(); 622 write_u2(num_interfaces); 623 for (int index = 0; index < num_interfaces; index++) { 624 HandleMark hm(thread()); 625 instanceKlassHandle iikh(thread(), interfaces->at(index)); 626 write_u2(class_symbol_to_cpool_index(iikh->name())); 627 } 628 629 // JVMSpec| u2 fields_count; 630 // JVMSpec| field_info fields[fields_count]; 631 write_field_infos(); 632 633 // JVMSpec| u2 methods_count; 634 // JVMSpec| method_info methods[methods_count]; 635 write_method_infos(); 636 637 // JVMSpec| u2 attributes_count; 638 // JVMSpec| attribute_info attributes[attributes_count]; 639 // JVMSpec| } /* end ClassFile 8? 640 write_class_attributes(); 641} 642 643address JvmtiClassFileReconstituter::writeable_address(size_t size) { 644 size_t used_size = _buffer_ptr - _buffer; 645 if (size + used_size >= _buffer_size) { 646 // compute the new buffer size: must be at least twice as big as before 647 // plus whatever new is being used; then convert to nice clean block boundary 648 size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size 649 * initial_buffer_size; 650 651 // VM goes belly-up if the memory isn't available, so cannot do OOM processing 652 _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size); 653 _buffer_size = new_buffer_size; 654 _buffer_ptr = _buffer + used_size; 655 } 656 u1* ret_ptr = _buffer_ptr; 657 _buffer_ptr += size; 658 return ret_ptr; 659} 660 661void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) { 662 TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name)); 663 assert(sym != NULL, "attribute name symbol not found"); 664 u2 attr_name_index = symbol_to_cpool_index(sym); 665 assert(attr_name_index != 0, "attribute name symbol not in constant pool"); 666 write_u2(attr_name_index); 667} 668 669void JvmtiClassFileReconstituter::write_u1(u1 x) { 670 *writeable_address(1) = x; 671} 672 673void JvmtiClassFileReconstituter::write_u2(u2 x) { 674 Bytes::put_Java_u2(writeable_address(2), x); 675} 676 677void JvmtiClassFileReconstituter::write_u4(u4 x) { 678 Bytes::put_Java_u4(writeable_address(4), x); 679} 680 681void JvmtiClassFileReconstituter::write_u8(u8 x) { 682 Bytes::put_Java_u8(writeable_address(8), x); 683} 684 685void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh, 686 unsigned char* bytecodes) { 687 // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes 688 // and the breakpoint bytecode are converted to their original bytecodes. 689 690 BytecodeStream bs(mh); 691 692 unsigned char* p = bytecodes; 693 Bytecodes::Code code; 694 bool is_rewritten = InstanceKlass::cast(mh->method_holder())->is_rewritten(); 695 696 while ((code = bs.next()) >= 0) { 697 assert(Bytecodes::is_java_code(code), "sanity check"); 698 assert(code != Bytecodes::_breakpoint, "sanity check"); 699 700 // length of bytecode (mnemonic + operands) 701 address bcp = bs.bcp(); 702 int len = bs.instruction_size(); 703 assert(len > 0, "length must be > 0"); 704 705 // copy the bytecodes 706 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code); 707 if (len > 1) { 708 memcpy(p+1, bcp+1, len-1); 709 } 710 711 // During linking the get/put and invoke instructions are rewritten 712 // with an index into the constant pool cache. The original constant 713 // pool index must be returned to caller. Rewrite the index. 714 if (is_rewritten && len > 1) { 715 bool is_wide = false; 716 switch (code) { 717 case Bytecodes::_getstatic : // fall through 718 case Bytecodes::_putstatic : // fall through 719 case Bytecodes::_getfield : // fall through 720 case Bytecodes::_putfield : // fall through 721 case Bytecodes::_invokevirtual : // fall through 722 case Bytecodes::_invokespecial : // fall through 723 case Bytecodes::_invokestatic : // fall through 724 case Bytecodes::_invokedynamic : // fall through 725 case Bytecodes::_invokeinterface : { 726 assert(len == 3 || 727 (code == Bytecodes::_invokeinterface && len == 5) || 728 (code == Bytecodes::_invokedynamic && len == 5), 729 "sanity check"); 730 731 int cpci = Bytes::get_native_u2(bcp+1); 732 bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic); 733 ConstantPoolCacheEntry* entry; 734 if (is_invokedynamic) { 735 cpci = Bytes::get_native_u4(bcp+1); 736 entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci); 737 } else { 738 // cache cannot be pre-fetched since some classes won't have it yet 739 entry = mh->constants()->cache()->entry_at(cpci); 740 } 741 int i = entry->constant_pool_index(); 742 assert(i < mh->constants()->length(), "sanity check"); 743 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering 744 if (is_invokedynamic) *(p+3) = *(p+4) = 0; 745 break; 746 } 747 case Bytecodes::_ldc_w: 748 is_wide = true; // fall through 749 case Bytecodes::_ldc: { 750 if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) { 751 int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1)); 752 int i = mh->constants()->object_to_cp_index(cpci); 753 assert(i < mh->constants()->length(), "sanity check"); 754 if (is_wide) { 755 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering 756 } else { 757 *(p+1) = (u1)i; 758 } 759 } 760 break; 761 } 762 } 763 } 764 765 p += len; 766 } 767} 768