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