1 /* 2 * Copyright (c) 2005, 2010, 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 "prims/jvmtiClassFileReconstituter.hpp" 29 #include "runtime/signature.hpp" 30 #ifdef TARGET_ARCH_x86 31 # include "bytes_x86.hpp" 32 #endif 33 #ifdef TARGET_ARCH_sparc 34 # include "bytes_sparc.hpp" 35 #endif 36 #ifdef TARGET_ARCH_zero 37 # include "bytes_zero.hpp" 38 #endif 39 // FIXME: add Deprecated, LVT, LVTT attributes 40 // FIXME: fix Synthetic attribute 41 // FIXME: per Serguei, add error return handling for constantPoolOopDesc::copy_cpool_bytes() 42 43 44 // Write the field information portion of ClassFile structure 45 // JVMSpec| u2 fields_count; 46 // JVMSpec| field_info fields[fields_count]; 47 void JvmtiClassFileReconstituter::write_field_infos() { 48 HandleMark hm(thread()); 49 typeArrayHandle fields(thread(), ikh()->fields()); 50 int fields_length = fields->length(); 51 int num_fields = fields_length / instanceKlass::next_offset; 52 objArrayHandle fields_anno(thread(), ikh()->fields_annotations()); 53 54 write_u2(num_fields); 55 for (int index = 0; index < fields_length; index += instanceKlass::next_offset) { 56 AccessFlags access_flags; 57 int flags = fields->ushort_at(index + instanceKlass::access_flags_offset); 58 access_flags.set_flags(flags); 59 int name_index = fields->ushort_at(index + instanceKlass::name_index_offset); 60 int signature_index = fields->ushort_at(index + instanceKlass::signature_index_offset); 61 int initial_value_index = fields->ushort_at(index + instanceKlass::initval_index_offset); 62 guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field"); 63 int offset = ikh()->offset_from_fields( index ); 64 int generic_signature_index = 65 fields->ushort_at(index + instanceKlass::generic_signature_offset); 66 typeArrayHandle anno(thread(), fields_anno.not_null() ? 67 (typeArrayOop)(fields_anno->obj_at(index / instanceKlass::next_offset)) : 68 (typeArrayOop)NULL); 69 70 // JVMSpec| field_info { 71 // JVMSpec| u2 access_flags; 72 // JVMSpec| u2 name_index; 73 // JVMSpec| u2 descriptor_index; 74 // JVMSpec| u2 attributes_count; 75 // JVMSpec| attribute_info attributes[attributes_count]; 76 // JVMSpec| } 77 78 write_u2(flags & JVM_RECOGNIZED_FIELD_MODIFIERS); 79 write_u2(name_index); 80 write_u2(signature_index); 81 int attr_count = 0; 82 if (initial_value_index != 0) { 83 ++attr_count; 84 } 85 if (access_flags.is_synthetic()) { 86 // ++attr_count; 87 } 88 if (generic_signature_index != 0) { 89 ++attr_count; 90 } 91 if (anno.not_null()) { 92 ++attr_count; // has RuntimeVisibleAnnotations attribute 93 } 94 95 write_u2(attr_count); 96 97 if (initial_value_index != 0) { 98 write_attribute_name_index("ConstantValue"); 99 write_u4(2); //length always 2 100 write_u2(initial_value_index); 101 } 102 if (access_flags.is_synthetic()) { 103 // write_synthetic_attribute(); 104 } 105 if (generic_signature_index != 0) { 106 write_signature_attribute(generic_signature_index); 107 } 108 if (anno.not_null()) { 109 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 110 } 111 } 112 } 113 114 // Write Code attribute 115 // JVMSpec| Code_attribute { 116 // JVMSpec| u2 attribute_name_index; 117 // JVMSpec| u4 attribute_length; 118 // JVMSpec| u2 max_stack; 119 // JVMSpec| u2 max_locals; 120 // JVMSpec| u4 code_length; 121 // JVMSpec| u1 code[code_length]; 122 // JVMSpec| u2 exception_table_length; 123 // JVMSpec| { u2 start_pc; 124 // JVMSpec| u2 end_pc; 125 // JVMSpec| u2 handler_pc; 126 // JVMSpec| u2 catch_type; 127 // JVMSpec| } exception_table[exception_table_length]; 128 // JVMSpec| u2 attributes_count; 129 // JVMSpec| attribute_info attributes[attributes_count]; 130 // JVMSpec| } 131 void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) { 132 constMethodHandle const_method(thread(), method->constMethod()); 133 u2 line_num_cnt = 0; 134 int stackmap_len = 0; 135 136 // compute number and length of attributes -- FIXME: for now no LVT 137 int attr_count = 0; 138 int attr_size = 0; 139 if (const_method->has_linenumber_table()) { 140 line_num_cnt = line_number_table_entries(method); 141 if (line_num_cnt != 0) { 142 ++attr_count; 143 // Compute the complete size of the line number table attribute: 144 // LineNumberTable_attribute { 145 // u2 attribute_name_index; 146 // u4 attribute_length; 147 // u2 line_number_table_length; 148 // { u2 start_pc; 149 // u2 line_number; 150 // } line_number_table[line_number_table_length]; 151 // } 152 attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2); 153 } 154 } 155 if (method->has_stackmap_table()) { 156 stackmap_len = method->stackmap_data()->length(); 157 if (stackmap_len != 0) { 158 ++attr_count; 159 // Compute the size of the stack map table attribute (VM stores raw): 160 // StackMapTable_attribute { 161 // u2 attribute_name_index; 162 // u4 attribute_length; 163 // u2 number_of_entries; 164 // stack_map_frame_entries[number_of_entries]; 165 // } 166 attr_size += 2 + 4 + stackmap_len; 167 } 168 } 169 170 typeArrayHandle exception_table(thread(), const_method->exception_table()); 171 int exception_table_length = exception_table->length(); 172 int exception_table_entries = exception_table_length / 4; 173 int code_size = const_method->code_size(); 174 int size = 175 2+2+4 + // max_stack, max_locals, code_length 176 code_size + // code 177 2 + // exception_table_length 178 (2+2+2+2) * exception_table_entries + // exception_table 179 2 + // attributes_count 180 attr_size; // attributes 181 182 write_attribute_name_index("Code"); 183 write_u4(size); 184 write_u2(method->max_stack()); 185 write_u2(method->max_locals()); 186 write_u4(code_size); 187 copy_bytecodes(method, (unsigned char*)writeable_address(code_size)); 188 write_u2(exception_table_entries); 189 for (int index = 0; index < exception_table_length; ) { 190 write_u2(exception_table->int_at(index++)); 191 write_u2(exception_table->int_at(index++)); 192 write_u2(exception_table->int_at(index++)); 193 write_u2(exception_table->int_at(index++)); 194 } 195 write_u2(attr_count); 196 if (line_num_cnt != 0) { 197 write_line_number_table_attribute(method, line_num_cnt); 198 } 199 if (stackmap_len != 0) { 200 write_stackmap_table_attribute(method, stackmap_len); 201 } 202 203 // FIXME: write LVT attribute 204 } 205 206 // Write Exceptions attribute 207 // JVMSpec| Exceptions_attribute { 208 // JVMSpec| u2 attribute_name_index; 209 // JVMSpec| u4 attribute_length; 210 // JVMSpec| u2 number_of_exceptions; 211 // JVMSpec| u2 exception_index_table[number_of_exceptions]; 212 // JVMSpec| } 213 void JvmtiClassFileReconstituter::write_exceptions_attribute(constMethodHandle const_method) { 214 CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start(); 215 int checked_exceptions_length = const_method->checked_exceptions_length(); 216 int size = 217 2 + // number_of_exceptions 218 2 * checked_exceptions_length; // exception_index_table 219 220 write_attribute_name_index("Exceptions"); 221 write_u4(size); 222 write_u2(checked_exceptions_length); 223 for (int index = 0; index < checked_exceptions_length; index++) { 224 write_u2(checked_exceptions[index].class_cp_index); 225 } 226 } 227 228 // Write SourceFile attribute 229 // JVMSpec| SourceFile_attribute { 230 // JVMSpec| u2 attribute_name_index; 231 // JVMSpec| u4 attribute_length; 232 // JVMSpec| u2 sourcefile_index; 233 // JVMSpec| } 234 void JvmtiClassFileReconstituter::write_source_file_attribute() { 235 assert(ikh()->source_file_name() != NULL, "caller must check"); 236 237 write_attribute_name_index("SourceFile"); 238 write_u4(2); // always length 2 239 write_u2(symbol_to_cpool_index(ikh()->source_file_name())); 240 } 241 242 // Write SourceDebugExtension attribute 243 // JSR45| SourceDebugExtension_attribute { 244 // JSR45| u2 attribute_name_index; 245 // JSR45| u4 attribute_length; 246 // JSR45| u2 sourcefile_index; 247 // JSR45| } 248 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() { 249 assert(ikh()->source_debug_extension() != NULL, "caller must check"); 250 251 write_attribute_name_index("SourceDebugExtension"); 252 write_u4(2); // always length 2 253 write_u2(symbol_to_cpool_index(ikh()->source_debug_extension())); 254 } 255 256 // Write (generic) Signature attribute 257 // JVMSpec| Signature_attribute { 258 // JVMSpec| u2 attribute_name_index; 259 // JVMSpec| u4 attribute_length; 260 // JVMSpec| u2 signature_index; 261 // JVMSpec| } 262 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) { 263 write_attribute_name_index("Signature"); 264 write_u4(2); // always length 2 265 write_u2(generic_signature_index); 266 } 267 268 // Compute the number of entries in the InnerClasses attribute 269 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() { 270 typeArrayOop inner_class_list = ikh()->inner_classes(); 271 return (inner_class_list == NULL) ? 0 : inner_class_list->length(); 272 } 273 274 // Write an annotation attribute. The VM stores them in raw form, so all we need 275 // to do is add the attrubute name and fill in the length. 276 // JSR202| *Annotations_attribute { 277 // JSR202| u2 attribute_name_index; 278 // JSR202| u4 attribute_length; 279 // JSR202| ... 280 // JSR202| } 281 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name, 282 typeArrayHandle annos) { 283 u4 length = annos->length(); 284 write_attribute_name_index(attr_name); 285 write_u4(length); 286 memcpy(writeable_address(length), annos->byte_at_addr(0), length); 287 } 288 289 290 // Write InnerClasses attribute 291 // JVMSpec| InnerClasses_attribute { 292 // JVMSpec| u2 attribute_name_index; 293 // JVMSpec| u4 attribute_length; 294 // JVMSpec| u2 number_of_classes; 295 // JVMSpec| { u2 inner_class_info_index; 296 // JVMSpec| u2 outer_class_info_index; 297 // JVMSpec| u2 inner_name_index; 298 // JVMSpec| u2 inner_class_access_flags; 299 // JVMSpec| } classes[number_of_classes]; 300 // JVMSpec| } 301 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) { 302 typeArrayOop inner_class_list = ikh()->inner_classes(); 303 guarantee(inner_class_list != NULL && inner_class_list->length() == length, 304 "caller must check"); 305 typeArrayHandle inner_class_list_h(thread(), inner_class_list); 306 assert (length % instanceKlass::inner_class_next_offset == 0, "just checking"); 307 u2 entry_count = length / instanceKlass::inner_class_next_offset; 308 u4 size = 2 + entry_count * (2+2+2+2); 309 310 write_attribute_name_index("InnerClasses"); 311 write_u4(size); 312 write_u2(entry_count); 313 for (int i = 0; i < length; i += instanceKlass::inner_class_next_offset) { 314 write_u2(inner_class_list_h->ushort_at( 315 i + instanceKlass::inner_class_inner_class_info_offset)); 316 write_u2(inner_class_list_h->ushort_at( 317 i + instanceKlass::inner_class_outer_class_info_offset)); 318 write_u2(inner_class_list_h->ushort_at( 319 i + instanceKlass::inner_class_inner_name_offset)); 320 write_u2(inner_class_list_h->ushort_at( 321 i + instanceKlass::inner_class_access_flags_offset)); 322 } 323 } 324 325 // Write Synthetic attribute 326 // JVMSpec| Synthetic_attribute { 327 // JVMSpec| u2 attribute_name_index; 328 // JVMSpec| u4 attribute_length; 329 // JVMSpec| } 330 void JvmtiClassFileReconstituter::write_synthetic_attribute() { 331 write_attribute_name_index("Synthetic"); 332 write_u4(0); //length always zero 333 } 334 335 // Compute size of LineNumberTable 336 u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) { 337 // The line number table is compressed so we don't know how big it is until decompressed. 338 // Decompression is really fast so we just do it twice. 339 u2 num_entries = 0; 340 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 341 while (stream.read_pair()) { 342 num_entries++; 343 } 344 return num_entries; 345 } 346 347 // Write LineNumberTable attribute 348 // JVMSpec| LineNumberTable_attribute { 349 // JVMSpec| u2 attribute_name_index; 350 // JVMSpec| u4 attribute_length; 351 // JVMSpec| u2 line_number_table_length; 352 // JVMSpec| { u2 start_pc; 353 // JVMSpec| u2 line_number; 354 // JVMSpec| } line_number_table[line_number_table_length]; 355 // JVMSpec| } 356 void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method, 357 u2 num_entries) { 358 359 write_attribute_name_index("LineNumberTable"); 360 write_u4(2 + num_entries * (2 + 2)); 361 write_u2(num_entries); 362 363 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 364 while (stream.read_pair()) { 365 write_u2(stream.bci()); 366 write_u2(stream.line()); 367 } 368 } 369 370 // Write stack map table attribute 371 // JSR-202| StackMapTable_attribute { 372 // JSR-202| u2 attribute_name_index; 373 // JSR-202| u4 attribute_length; 374 // JSR-202| u2 number_of_entries; 375 // JSR-202| stack_map_frame_entries[number_of_entries]; 376 // JSR-202| } 377 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method, 378 int stackmap_len) { 379 380 write_attribute_name_index("StackMapTable"); 381 write_u4(stackmap_len); 382 memcpy( 383 writeable_address(stackmap_len), 384 (void*)(method->stackmap_data()->byte_at_addr(0)), 385 stackmap_len); 386 } 387 388 // Write one method_info structure 389 // JVMSpec| method_info { 390 // JVMSpec| u2 access_flags; 391 // JVMSpec| u2 name_index; 392 // JVMSpec| u2 descriptor_index; 393 // JVMSpec| u2 attributes_count; 394 // JVMSpec| attribute_info attributes[attributes_count]; 395 // JVMSpec| } 396 void JvmtiClassFileReconstituter::write_method_info(methodHandle method) { 397 AccessFlags access_flags = method->access_flags(); 398 constMethodHandle const_method(thread(), method->constMethod()); 399 u2 generic_signature_index = const_method->generic_signature_index(); 400 typeArrayHandle anno(thread(), method->annotations()); 401 typeArrayHandle param_anno(thread(), method->parameter_annotations()); 402 typeArrayHandle default_anno(thread(), method->annotation_default()); 403 404 write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS); 405 write_u2(const_method->name_index()); 406 write_u2(const_method->signature_index()); 407 408 // write attributes in the same order javac does, so we can test with byte for 409 // byte comparison 410 int attr_count = 0; 411 if (const_method->code_size() != 0) { 412 ++attr_count; // has Code attribute 413 } 414 if (const_method->has_checked_exceptions()) { 415 ++attr_count; // has Exceptions attribute 416 } 417 if (default_anno.not_null()) { 418 ++attr_count; // has AnnotationDefault attribute 419 } 420 // Deprecated attribute would go here 421 if (access_flags.is_synthetic()) { // FIXME 422 // ++attr_count; 423 } 424 if (generic_signature_index != 0) { 425 ++attr_count; 426 } 427 if (anno.not_null()) { 428 ++attr_count; // has RuntimeVisibleAnnotations attribute 429 } 430 if (param_anno.not_null()) { 431 ++attr_count; // has RuntimeVisibleParameterAnnotations attribute 432 } 433 434 write_u2(attr_count); 435 if (const_method->code_size() > 0) { 436 write_code_attribute(method); 437 } 438 if (const_method->has_checked_exceptions()) { 439 write_exceptions_attribute(const_method); 440 } 441 if (default_anno.not_null()) { 442 write_annotations_attribute("AnnotationDefault", default_anno); 443 } 444 // Deprecated attribute would go here 445 if (access_flags.is_synthetic()) { 446 // write_synthetic_attribute(); 447 } 448 if (generic_signature_index != 0) { 449 write_signature_attribute(generic_signature_index); 450 } 451 if (anno.not_null()) { 452 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 453 } 454 if (param_anno.not_null()) { 455 write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno); 456 } 457 } 458 459 // Write the class attributes portion of ClassFile structure 460 // JVMSpec| u2 attributes_count; 461 // JVMSpec| attribute_info attributes[attributes_count]; 462 void JvmtiClassFileReconstituter::write_class_attributes() { 463 u2 inner_classes_length = inner_classes_attribute_length(); 464 symbolHandle generic_signature(thread(), ikh()->generic_signature()); 465 typeArrayHandle anno(thread(), ikh()->class_annotations()); 466 467 int attr_count = 0; 468 if (generic_signature() != NULL) { 469 ++attr_count; 470 } 471 if (ikh()->source_file_name() != NULL) { 472 ++attr_count; 473 } 474 if (ikh()->source_debug_extension() != NULL) { 475 ++attr_count; 476 } 477 if (inner_classes_length > 0) { 478 ++attr_count; 479 } 480 if (anno.not_null()) { 481 ++attr_count; // has RuntimeVisibleAnnotations attribute 482 } 483 484 write_u2(attr_count); 485 486 if (generic_signature() != NULL) { 487 write_signature_attribute(symbol_to_cpool_index(generic_signature())); 488 } 489 if (ikh()->source_file_name() != NULL) { 490 write_source_file_attribute(); 491 } 492 if (ikh()->source_debug_extension() != NULL) { 493 write_source_debug_extension_attribute(); 494 } 495 if (inner_classes_length > 0) { 496 write_inner_classes_attribute(inner_classes_length); 497 } 498 if (anno.not_null()) { 499 write_annotations_attribute("RuntimeVisibleAnnotations", anno); 500 } 501 } 502 503 // Write the method information portion of ClassFile structure 504 // JVMSpec| u2 methods_count; 505 // JVMSpec| method_info methods[methods_count]; 506 void JvmtiClassFileReconstituter::write_method_infos() { 507 HandleMark hm(thread()); 508 objArrayHandle methods(thread(), ikh()->methods()); 509 int num_methods = methods->length(); 510 511 write_u2(num_methods); 512 if (JvmtiExport::can_maintain_original_method_order()) { 513 int index; 514 int original_index; 515 int* method_order = NEW_RESOURCE_ARRAY(int, num_methods); 516 517 // invert the method order mapping 518 for (index = 0; index < num_methods; index++) { 519 original_index = ikh()->method_ordering()->int_at(index); 520 assert(original_index >= 0 && original_index < num_methods, 521 "invalid original method index"); 522 method_order[original_index] = index; 523 } 524 525 // write in original order 526 for (original_index = 0; original_index < num_methods; original_index++) { 527 index = method_order[original_index]; 528 methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index))); 529 write_method_info(method); 530 } 531 } else { 532 // method order not preserved just dump the method infos 533 for (int index = 0; index < num_methods; index++) { 534 methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index))); 535 write_method_info(method); 536 } 537 } 538 } 539 540 void JvmtiClassFileReconstituter::write_class_file_format() { 541 ReallocMark(); 542 543 // JVMSpec| ClassFile { 544 // JVMSpec| u4 magic; 545 write_u4(0xCAFEBABE); 546 547 // JVMSpec| u2 minor_version; 548 // JVMSpec| u2 major_version; 549 write_u2(ikh()->minor_version()); 550 u2 major = ikh()->major_version(); 551 write_u2(major); 552 553 // JVMSpec| u2 constant_pool_count; 554 // JVMSpec| cp_info constant_pool[constant_pool_count-1]; 555 write_u2(cpool()->length()); 556 copy_cpool_bytes(writeable_address(cpool_size())); 557 558 // JVMSpec| u2 access_flags; 559 write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS); 560 561 // JVMSpec| u2 this_class; 562 // JVMSpec| u2 super_class; 563 write_u2(class_symbol_to_cpool_index(ikh()->name())); 564 klassOop super_class = ikh()->super(); 565 write_u2(super_class == NULL? 0 : // zero for java.lang.Object 566 class_symbol_to_cpool_index(super_class->klass_part()->name())); 567 568 // JVMSpec| u2 interfaces_count; 569 // JVMSpec| u2 interfaces[interfaces_count]; 570 objArrayHandle interfaces(thread(), ikh()->local_interfaces()); 571 int num_interfaces = interfaces->length(); 572 write_u2(num_interfaces); 573 for (int index = 0; index < num_interfaces; index++) { 574 HandleMark hm(thread()); 575 instanceKlassHandle iikh(thread(), klassOop(interfaces->obj_at(index))); 576 write_u2(class_symbol_to_cpool_index(iikh->name())); 577 } 578 579 // JVMSpec| u2 fields_count; 580 // JVMSpec| field_info fields[fields_count]; 581 write_field_infos(); 582 583 // JVMSpec| u2 methods_count; 584 // JVMSpec| method_info methods[methods_count]; 585 write_method_infos(); 586 587 // JVMSpec| u2 attributes_count; 588 // JVMSpec| attribute_info attributes[attributes_count]; 589 // JVMSpec| } /* end ClassFile 8? 590 write_class_attributes(); 591 } 592 593 address JvmtiClassFileReconstituter::writeable_address(size_t size) { 594 size_t used_size = _buffer_ptr - _buffer; 595 if (size + used_size >= _buffer_size) { 596 // compute the new buffer size: must be at least twice as big as before 597 // plus whatever new is being used; then convert to nice clean block boundary 598 size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size 599 * initial_buffer_size; 600 601 // VM goes belly-up if the memory isn't available, so cannot do OOM processing 602 _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size); 603 _buffer_size = new_buffer_size; 604 _buffer_ptr = _buffer + used_size; 605 } 606 u1* ret_ptr = _buffer_ptr; 607 _buffer_ptr += size; 608 return ret_ptr; 609 } 610 611 void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) { 612 unsigned int hash_ignored; 613 symbolOop sym = SymbolTable::lookup_only(name, (int)strlen(name), hash_ignored); 614 assert(sym != NULL, "attribute name symbol not found"); 615 u2 attr_name_index = symbol_to_cpool_index(sym); 616 assert(attr_name_index != 0, "attribute name symbol not in constant pool"); 617 write_u2(attr_name_index); 618 } 619 620 void JvmtiClassFileReconstituter::write_u1(u1 x) { 621 *writeable_address(1) = x; 622 } 623 624 void JvmtiClassFileReconstituter::write_u2(u2 x) { 625 Bytes::put_Java_u2(writeable_address(2), x); 626 } 627 628 void JvmtiClassFileReconstituter::write_u4(u4 x) { 629 Bytes::put_Java_u4(writeable_address(4), x); 630 } 631 632 void JvmtiClassFileReconstituter::write_u8(u8 x) { 633 Bytes::put_Java_u8(writeable_address(8), x); 634 } 635 636 void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh, 637 unsigned char* bytecodes) { 638 // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes 639 // and the breakpoint bytecode are converted to their original bytecodes. 640 641 BytecodeStream bs(mh); 642 643 unsigned char* p = bytecodes; 644 Bytecodes::Code code; 645 bool is_rewritten = instanceKlass::cast(mh->method_holder())->is_rewritten(); 646 647 while ((code = bs.next()) >= 0) { 648 assert(Bytecodes::is_java_code(code), "sanity check"); 649 assert(code != Bytecodes::_breakpoint, "sanity check"); 650 651 // length of bytecode (mnemonic + operands) 652 address bcp = bs.bcp(); 653 int len = bs.instruction_size(); 654 assert(len > 0, "length must be > 0"); 655 656 // copy the bytecodes 657 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code); 658 if (len > 1) { 659 memcpy(p+1, bcp+1, len-1); 660 } 661 662 // During linking the get/put and invoke instructions are rewritten 663 // with an index into the constant pool cache. The original constant 664 // pool index must be returned to caller. Rewrite the index. 665 if (is_rewritten && len >= 3) { 666 switch (code) { 667 case Bytecodes::_getstatic : // fall through 668 case Bytecodes::_putstatic : // fall through 669 case Bytecodes::_getfield : // fall through 670 case Bytecodes::_putfield : // fall through 671 case Bytecodes::_invokevirtual : // fall through 672 case Bytecodes::_invokespecial : // fall through 673 case Bytecodes::_invokestatic : // fall through 674 case Bytecodes::_invokedynamic : // fall through 675 case Bytecodes::_invokeinterface : 676 assert(len == 3 || (code == Bytecodes::_invokeinterface && len ==5), 677 "sanity check"); 678 int cpci = Bytes::get_native_u2(bcp+1); 679 bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic); 680 if (is_invokedynamic) 681 cpci = Bytes::get_native_u4(bcp+1); 682 // cache cannot be pre-fetched since some classes won't have it yet 683 ConstantPoolCacheEntry* entry = 684 mh->constants()->cache()->main_entry_at(cpci); 685 int i = entry->constant_pool_index(); 686 assert(i < mh->constants()->length(), "sanity check"); 687 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering 688 if (is_invokedynamic) *(p+3) = *(p+4) = 0; 689 break; 690 } 691 } 692 693 p += len; 694 } 695 }