1 /* 2 * Copyright (c) 1997, 2017, 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 #include "precompiled.hpp" 25 #include "aot/aotLoader.hpp" 26 #include "classfile/classFileParser.hpp" 27 #include "classfile/classFileStream.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/defaultMethods.hpp" 31 #include "classfile/dictionary.hpp" 32 #include "classfile/javaClasses.inline.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/symbolTable.hpp" 35 #include "classfile/systemDictionary.hpp" 36 #include "classfile/verificationType.hpp" 37 #include "classfile/verifier.hpp" 38 #include "classfile/vmSymbols.hpp" 39 #include "gc/shared/gcLocker.hpp" 40 #include "logging/log.hpp" 41 #include "memory/allocation.hpp" 42 #include "memory/metadataFactory.hpp" 43 #include "memory/oopFactory.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.inline.hpp" 46 #include "oops/annotations.hpp" 47 #include "oops/fieldStreams.hpp" 48 #include "oops/instanceKlass.hpp" 49 #include "oops/instanceMirrorKlass.hpp" 50 #include "oops/klass.inline.hpp" 51 #include "oops/klassVtable.hpp" 52 #include "oops/metadata.hpp" 53 #include "oops/method.hpp" 54 #include "oops/oop.inline.hpp" 55 #include "oops/symbol.hpp" 56 #include "prims/jvm.h" 57 #include "prims/jvmtiExport.hpp" 58 #include "prims/jvmtiThreadState.hpp" 59 #include "runtime/javaCalls.hpp" 60 #include "runtime/perfData.hpp" 61 #include "runtime/reflection.hpp" 62 #include "runtime/signature.hpp" 63 #include "runtime/timer.hpp" 64 #include "services/classLoadingService.hpp" 65 #include "services/threadService.hpp" 66 #include "trace/traceMacros.hpp" 67 #include "utilities/array.hpp" 68 #include "utilities/exceptions.hpp" 69 #include "utilities/globalDefinitions.hpp" 70 #include "utilities/macros.hpp" 71 #include "utilities/ostream.hpp" 72 #include "utilities/resourceHash.hpp" 73 #if INCLUDE_CDS 74 #include "classfile/systemDictionaryShared.hpp" 75 #endif 76 77 // We generally try to create the oops directly when parsing, rather than 78 // allocating temporary data structures and copying the bytes twice. A 79 // temporary area is only needed when parsing utf8 entries in the constant 80 // pool and when parsing line number tables. 81 82 // We add assert in debug mode when class format is not checked. 83 84 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE 85 #define JAVA_MIN_SUPPORTED_VERSION 45 86 #define JAVA_MAX_SUPPORTED_VERSION 53 87 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0 88 89 // Used for two backward compatibility reasons: 90 // - to check for new additions to the class file format in JDK1.5 91 // - to check for bug fixes in the format checker in JDK1.5 92 #define JAVA_1_5_VERSION 49 93 94 // Used for backward compatibility reasons: 95 // - to check for javac bug fixes that happened after 1.5 96 // - also used as the max version when running in jdk6 97 #define JAVA_6_VERSION 50 98 99 // Used for backward compatibility reasons: 100 // - to disallow argument and require ACC_STATIC for <clinit> methods 101 #define JAVA_7_VERSION 51 102 103 // Extension method support. 104 #define JAVA_8_VERSION 52 105 106 #define JAVA_9_VERSION 53 107 108 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream, 109 ConstantPool* cp, 110 const int length, 111 TRAPS) { 112 assert(stream != NULL, "invariant"); 113 assert(cp != NULL, "invariant"); 114 115 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize 116 // this function (_current can be allocated in a register, with scalar 117 // replacement of aggregates). The _current pointer is copied back to 118 // stream() when this function returns. DON'T call another method within 119 // this method that uses stream(). 120 const ClassFileStream cfs1 = *stream; 121 const ClassFileStream* const cfs = &cfs1; 122 123 assert(cfs->allocated_on_stack(), "should be local"); 124 debug_only(const u1* const old_current = stream->current();) 125 126 // Used for batching symbol allocations. 127 const char* names[SymbolTable::symbol_alloc_batch_size]; 128 int lengths[SymbolTable::symbol_alloc_batch_size]; 129 int indices[SymbolTable::symbol_alloc_batch_size]; 130 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size]; 131 int names_count = 0; 132 133 // parsing Index 0 is unused 134 for (int index = 1; index < length; index++) { 135 // Each of the following case guarantees one more byte in the stream 136 // for the following tag or the access_flags following constant pool, 137 // so we don't need bounds-check for reading tag. 138 const u1 tag = cfs->get_u1_fast(); 139 switch (tag) { 140 case JVM_CONSTANT_Class : { 141 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags 142 const u2 name_index = cfs->get_u2_fast(); 143 cp->klass_index_at_put(index, name_index); 144 break; 145 } 146 case JVM_CONSTANT_Fieldref: { 147 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 148 const u2 class_index = cfs->get_u2_fast(); 149 const u2 name_and_type_index = cfs->get_u2_fast(); 150 cp->field_at_put(index, class_index, name_and_type_index); 151 break; 152 } 153 case JVM_CONSTANT_Methodref: { 154 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 155 const u2 class_index = cfs->get_u2_fast(); 156 const u2 name_and_type_index = cfs->get_u2_fast(); 157 cp->method_at_put(index, class_index, name_and_type_index); 158 break; 159 } 160 case JVM_CONSTANT_InterfaceMethodref: { 161 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 162 const u2 class_index = cfs->get_u2_fast(); 163 const u2 name_and_type_index = cfs->get_u2_fast(); 164 cp->interface_method_at_put(index, class_index, name_and_type_index); 165 break; 166 } 167 case JVM_CONSTANT_String : { 168 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags 169 const u2 string_index = cfs->get_u2_fast(); 170 cp->string_index_at_put(index, string_index); 171 break; 172 } 173 case JVM_CONSTANT_MethodHandle : 174 case JVM_CONSTANT_MethodType: { 175 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 176 classfile_parse_error( 177 "Class file version does not support constant tag %u in class file %s", 178 tag, CHECK); 179 } 180 if (tag == JVM_CONSTANT_MethodHandle) { 181 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags 182 const u1 ref_kind = cfs->get_u1_fast(); 183 const u2 method_index = cfs->get_u2_fast(); 184 cp->method_handle_index_at_put(index, ref_kind, method_index); 185 } 186 else if (tag == JVM_CONSTANT_MethodType) { 187 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags 188 const u2 signature_index = cfs->get_u2_fast(); 189 cp->method_type_index_at_put(index, signature_index); 190 } 191 else { 192 ShouldNotReachHere(); 193 } 194 break; 195 } 196 case JVM_CONSTANT_InvokeDynamic : { 197 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 198 classfile_parse_error( 199 "Class file version does not support constant tag %u in class file %s", 200 tag, CHECK); 201 } 202 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 203 const u2 bootstrap_specifier_index = cfs->get_u2_fast(); 204 const u2 name_and_type_index = cfs->get_u2_fast(); 205 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) { 206 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 207 } 208 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index); 209 break; 210 } 211 case JVM_CONSTANT_Integer: { 212 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 213 const u4 bytes = cfs->get_u4_fast(); 214 cp->int_at_put(index, (jint)bytes); 215 break; 216 } 217 case JVM_CONSTANT_Float: { 218 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 219 const u4 bytes = cfs->get_u4_fast(); 220 cp->float_at_put(index, *(jfloat*)&bytes); 221 break; 222 } 223 case JVM_CONSTANT_Long: { 224 // A mangled type might cause you to overrun allocated memory 225 guarantee_property(index + 1 < length, 226 "Invalid constant pool entry %u in class file %s", 227 index, 228 CHECK); 229 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 230 const u8 bytes = cfs->get_u8_fast(); 231 cp->long_at_put(index, bytes); 232 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 233 break; 234 } 235 case JVM_CONSTANT_Double: { 236 // A mangled type might cause you to overrun allocated memory 237 guarantee_property(index+1 < length, 238 "Invalid constant pool entry %u in class file %s", 239 index, 240 CHECK); 241 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 242 const u8 bytes = cfs->get_u8_fast(); 243 cp->double_at_put(index, *(jdouble*)&bytes); 244 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 245 break; 246 } 247 case JVM_CONSTANT_NameAndType: { 248 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags 249 const u2 name_index = cfs->get_u2_fast(); 250 const u2 signature_index = cfs->get_u2_fast(); 251 cp->name_and_type_at_put(index, name_index, signature_index); 252 break; 253 } 254 case JVM_CONSTANT_Utf8 : { 255 cfs->guarantee_more(2, CHECK); // utf8_length 256 u2 utf8_length = cfs->get_u2_fast(); 257 const u1* utf8_buffer = cfs->get_u1_buffer(); 258 assert(utf8_buffer != NULL, "null utf8 buffer"); 259 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward. 260 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags 261 cfs->skip_u1_fast(utf8_length); 262 263 // Before storing the symbol, make sure it's legal 264 if (_need_verify) { 265 verify_legal_utf8(utf8_buffer, utf8_length, CHECK); 266 } 267 268 if (has_cp_patch_at(index)) { 269 Handle patch = clear_cp_patch_at(index); 270 guarantee_property(java_lang_String::is_instance(patch()), 271 "Illegal utf8 patch at %d in class file %s", 272 index, 273 CHECK); 274 const char* const str = java_lang_String::as_utf8_string(patch()); 275 // (could use java_lang_String::as_symbol instead, but might as well batch them) 276 utf8_buffer = (const u1*) str; 277 utf8_length = (int) strlen(str); 278 } 279 280 unsigned int hash; 281 Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer, 282 utf8_length, 283 hash); 284 if (result == NULL) { 285 names[names_count] = (const char*)utf8_buffer; 286 lengths[names_count] = utf8_length; 287 indices[names_count] = index; 288 hashValues[names_count++] = hash; 289 if (names_count == SymbolTable::symbol_alloc_batch_size) { 290 SymbolTable::new_symbols(_loader_data, 291 cp, 292 names_count, 293 names, 294 lengths, 295 indices, 296 hashValues, 297 CHECK); 298 names_count = 0; 299 } 300 } else { 301 cp->symbol_at_put(index, result); 302 } 303 break; 304 } 305 default: { 306 classfile_parse_error("Unknown constant tag %u in class file %s", 307 tag, 308 CHECK); 309 break; 310 } 311 } // end of switch(tag) 312 } // end of for 313 314 // Allocate the remaining symbols 315 if (names_count > 0) { 316 SymbolTable::new_symbols(_loader_data, 317 cp, 318 names_count, 319 names, 320 lengths, 321 indices, 322 hashValues, 323 CHECK); 324 } 325 326 // Copy _current pointer of local copy back to stream. 327 assert(stream->current() == old_current, "non-exclusive use of stream"); 328 stream->set_current(cfs1.current()); 329 330 } 331 332 static inline bool valid_cp_range(int index, int length) { 333 return (index > 0 && index < length); 334 } 335 336 static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) { 337 assert(cp != NULL, "invariant"); 338 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) { 339 return cp->symbol_at(index); 340 } 341 return NULL; 342 } 343 344 #ifdef ASSERT 345 PRAGMA_DIAG_PUSH 346 PRAGMA_FORMAT_NONLITERAL_IGNORED 347 void ClassFileParser::report_assert_property_failure(const char* msg, TRAPS) const { 348 ResourceMark rm(THREAD); 349 fatal(msg, _class_name->as_C_string()); 350 } 351 352 void ClassFileParser::report_assert_property_failure(const char* msg, 353 int index, 354 TRAPS) const { 355 ResourceMark rm(THREAD); 356 fatal(msg, index, _class_name->as_C_string()); 357 } 358 PRAGMA_DIAG_POP 359 #endif 360 361 void ClassFileParser::parse_constant_pool(const ClassFileStream* const stream, 362 ConstantPool* const cp, 363 const int length, 364 TRAPS) { 365 assert(cp != NULL, "invariant"); 366 assert(stream != NULL, "invariant"); 367 368 // parsing constant pool entries 369 parse_constant_pool_entries(stream, cp, length, CHECK); 370 371 int index = 1; // declared outside of loops for portability 372 373 // first verification pass - validate cross references 374 // and fixup class and string constants 375 for (index = 1; index < length; index++) { // Index 0 is unused 376 const jbyte tag = cp->tag_at(index).value(); 377 switch (tag) { 378 case JVM_CONSTANT_Class: { 379 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 380 break; 381 } 382 case JVM_CONSTANT_Fieldref: 383 // fall through 384 case JVM_CONSTANT_Methodref: 385 // fall through 386 case JVM_CONSTANT_InterfaceMethodref: { 387 if (!_need_verify) break; 388 const int klass_ref_index = cp->klass_ref_index_at(index); 389 const int name_and_type_ref_index = cp->name_and_type_ref_index_at(index); 390 check_property(valid_klass_reference_at(klass_ref_index), 391 "Invalid constant pool index %u in class file %s", 392 klass_ref_index, CHECK); 393 check_property(valid_cp_range(name_and_type_ref_index, length) && 394 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 395 "Invalid constant pool index %u in class file %s", 396 name_and_type_ref_index, CHECK); 397 break; 398 } 399 case JVM_CONSTANT_String: { 400 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present 401 break; 402 } 403 case JVM_CONSTANT_Integer: 404 break; 405 case JVM_CONSTANT_Float: 406 break; 407 case JVM_CONSTANT_Long: 408 case JVM_CONSTANT_Double: { 409 index++; 410 check_property( 411 (index < length && cp->tag_at(index).is_invalid()), 412 "Improper constant pool long/double index %u in class file %s", 413 index, CHECK); 414 break; 415 } 416 case JVM_CONSTANT_NameAndType: { 417 if (!_need_verify) break; 418 const int name_ref_index = cp->name_ref_index_at(index); 419 const int signature_ref_index = cp->signature_ref_index_at(index); 420 check_property(valid_symbol_at(name_ref_index), 421 "Invalid constant pool index %u in class file %s", 422 name_ref_index, CHECK); 423 check_property(valid_symbol_at(signature_ref_index), 424 "Invalid constant pool index %u in class file %s", 425 signature_ref_index, CHECK); 426 break; 427 } 428 case JVM_CONSTANT_Utf8: 429 break; 430 case JVM_CONSTANT_UnresolvedClass: // fall-through 431 case JVM_CONSTANT_UnresolvedClassInError: { 432 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 433 break; 434 } 435 case JVM_CONSTANT_ClassIndex: { 436 const int class_index = cp->klass_index_at(index); 437 check_property(valid_symbol_at(class_index), 438 "Invalid constant pool index %u in class file %s", 439 class_index, CHECK); 440 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index)); 441 break; 442 } 443 case JVM_CONSTANT_StringIndex: { 444 const int string_index = cp->string_index_at(index); 445 check_property(valid_symbol_at(string_index), 446 "Invalid constant pool index %u in class file %s", 447 string_index, CHECK); 448 Symbol* const sym = cp->symbol_at(string_index); 449 cp->unresolved_string_at_put(index, sym); 450 break; 451 } 452 case JVM_CONSTANT_MethodHandle: { 453 const int ref_index = cp->method_handle_index_at(index); 454 check_property(valid_cp_range(ref_index, length), 455 "Invalid constant pool index %u in class file %s", 456 ref_index, CHECK); 457 const constantTag tag = cp->tag_at(ref_index); 458 const int ref_kind = cp->method_handle_ref_kind_at(index); 459 460 switch (ref_kind) { 461 case JVM_REF_getField: 462 case JVM_REF_getStatic: 463 case JVM_REF_putField: 464 case JVM_REF_putStatic: { 465 check_property( 466 tag.is_field(), 467 "Invalid constant pool index %u in class file %s (not a field)", 468 ref_index, CHECK); 469 break; 470 } 471 case JVM_REF_invokeVirtual: 472 case JVM_REF_newInvokeSpecial: { 473 check_property( 474 tag.is_method(), 475 "Invalid constant pool index %u in class file %s (not a method)", 476 ref_index, CHECK); 477 break; 478 } 479 case JVM_REF_invokeStatic: 480 case JVM_REF_invokeSpecial: { 481 check_property( 482 tag.is_method() || 483 ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()), 484 "Invalid constant pool index %u in class file %s (not a method)", 485 ref_index, CHECK); 486 break; 487 } 488 case JVM_REF_invokeInterface: { 489 check_property( 490 tag.is_interface_method(), 491 "Invalid constant pool index %u in class file %s (not an interface method)", 492 ref_index, CHECK); 493 break; 494 } 495 default: { 496 classfile_parse_error( 497 "Bad method handle kind at constant pool index %u in class file %s", 498 index, CHECK); 499 } 500 } // switch(refkind) 501 // Keep the ref_index unchanged. It will be indirected at link-time. 502 break; 503 } // case MethodHandle 504 case JVM_CONSTANT_MethodType: { 505 const int ref_index = cp->method_type_index_at(index); 506 check_property(valid_symbol_at(ref_index), 507 "Invalid constant pool index %u in class file %s", 508 ref_index, CHECK); 509 break; 510 } 511 case JVM_CONSTANT_InvokeDynamic: { 512 const int name_and_type_ref_index = 513 cp->invoke_dynamic_name_and_type_ref_index_at(index); 514 515 check_property(valid_cp_range(name_and_type_ref_index, length) && 516 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 517 "Invalid constant pool index %u in class file %s", 518 name_and_type_ref_index, CHECK); 519 // bootstrap specifier index must be checked later, 520 // when BootstrapMethods attr is available 521 break; 522 } 523 default: { 524 fatal("bad constant pool tag value %u", cp->tag_at(index).value()); 525 ShouldNotReachHere(); 526 break; 527 } 528 } // switch(tag) 529 } // end of for 530 531 if (_cp_patches != NULL) { 532 // need to treat this_class specially... 533 int this_class_index; 534 { 535 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 536 const u1* const mark = stream->current(); 537 stream->skip_u2_fast(1); // skip flags 538 this_class_index = stream->get_u2_fast(); 539 stream->set_current(mark); // revert to mark 540 } 541 542 for (index = 1; index < length; index++) { // Index 0 is unused 543 if (has_cp_patch_at(index)) { 544 guarantee_property(index != this_class_index, 545 "Illegal constant pool patch to self at %d in class file %s", 546 index, CHECK); 547 patch_constant_pool(cp, index, cp_patch_at(index), CHECK); 548 } 549 } 550 } 551 552 if (!_need_verify) { 553 return; 554 } 555 556 // second verification pass - checks the strings are of the right format. 557 // but not yet to the other entries 558 for (index = 1; index < length; index++) { 559 const jbyte tag = cp->tag_at(index).value(); 560 switch (tag) { 561 case JVM_CONSTANT_UnresolvedClass: { 562 const Symbol* const class_name = cp->klass_name_at(index); 563 // check the name, even if _cp_patches will overwrite it 564 verify_legal_class_name(class_name, CHECK); 565 break; 566 } 567 case JVM_CONSTANT_NameAndType: { 568 if (_need_verify) { 569 const int sig_index = cp->signature_ref_index_at(index); 570 const int name_index = cp->name_ref_index_at(index); 571 const Symbol* const name = cp->symbol_at(name_index); 572 const Symbol* const sig = cp->symbol_at(sig_index); 573 guarantee_property(sig->utf8_length() != 0, 574 "Illegal zero length constant pool entry at %d in class %s", 575 sig_index, CHECK); 576 guarantee_property(name->utf8_length() != 0, 577 "Illegal zero length constant pool entry at %d in class %s", 578 name_index, CHECK); 579 580 if (sig->byte_at(0) == JVM_SIGNATURE_FUNC) { 581 // Format check method name and signature 582 verify_legal_method_name(name, CHECK); 583 verify_legal_method_signature(name, sig, CHECK); 584 } else { 585 // Format check field name and signature 586 verify_legal_field_name(name, CHECK); 587 verify_legal_field_signature(name, sig, CHECK); 588 } 589 } 590 break; 591 } 592 case JVM_CONSTANT_InvokeDynamic: 593 case JVM_CONSTANT_Fieldref: 594 case JVM_CONSTANT_Methodref: 595 case JVM_CONSTANT_InterfaceMethodref: { 596 const int name_and_type_ref_index = 597 cp->name_and_type_ref_index_at(index); 598 // already verified to be utf8 599 const int name_ref_index = 600 cp->name_ref_index_at(name_and_type_ref_index); 601 // already verified to be utf8 602 const int signature_ref_index = 603 cp->signature_ref_index_at(name_and_type_ref_index); 604 const Symbol* const name = cp->symbol_at(name_ref_index); 605 const Symbol* const signature = cp->symbol_at(signature_ref_index); 606 if (tag == JVM_CONSTANT_Fieldref) { 607 if (_need_verify) { 608 // Field name and signature are verified above, when iterating NameAndType_info. 609 // Need only to be sure signature is non-zero length and the right type. 610 if (signature->utf8_length() == 0 || 611 signature->byte_at(0) == JVM_SIGNATURE_FUNC) { 612 throwIllegalSignature("Field", name, signature, CHECK); 613 } 614 } 615 } else { 616 if (_need_verify) { 617 // Method name and signature are verified above, when iterating NameAndType_info. 618 // Need only to be sure signature is non-zero length and the right type. 619 if (signature->utf8_length() == 0 || 620 signature->byte_at(0) != JVM_SIGNATURE_FUNC) { 621 throwIllegalSignature("Method", name, signature, CHECK); 622 } 623 } 624 // 4509014: If a class method name begins with '<', it must be "<init>" 625 const unsigned int name_len = name->utf8_length(); 626 if (tag == JVM_CONSTANT_Methodref && 627 name_len != 0 && 628 name->byte_at(0) == '<' && 629 name != vmSymbols::object_initializer_name()) { 630 classfile_parse_error( 631 "Bad method name at constant pool index %u in class file %s", 632 name_ref_index, CHECK); 633 } 634 } 635 break; 636 } 637 case JVM_CONSTANT_MethodHandle: { 638 const int ref_index = cp->method_handle_index_at(index); 639 const int ref_kind = cp->method_handle_ref_kind_at(index); 640 switch (ref_kind) { 641 case JVM_REF_invokeVirtual: 642 case JVM_REF_invokeStatic: 643 case JVM_REF_invokeSpecial: 644 case JVM_REF_newInvokeSpecial: { 645 const int name_and_type_ref_index = 646 cp->name_and_type_ref_index_at(ref_index); 647 const int name_ref_index = 648 cp->name_ref_index_at(name_and_type_ref_index); 649 const Symbol* const name = cp->symbol_at(name_ref_index); 650 if (ref_kind == JVM_REF_newInvokeSpecial) { 651 if (name != vmSymbols::object_initializer_name()) { 652 classfile_parse_error( 653 "Bad constructor name at constant pool index %u in class file %s", 654 name_ref_index, CHECK); 655 } 656 } else { 657 if (name == vmSymbols::object_initializer_name()) { 658 classfile_parse_error( 659 "Bad method name at constant pool index %u in class file %s", 660 name_ref_index, CHECK); 661 } 662 } 663 break; 664 } 665 // Other ref_kinds are already fully checked in previous pass. 666 } // switch(ref_kind) 667 break; 668 } 669 case JVM_CONSTANT_MethodType: { 670 const Symbol* const no_name = vmSymbols::type_name(); // place holder 671 const Symbol* const signature = cp->method_type_signature_at(index); 672 verify_legal_method_signature(no_name, signature, CHECK); 673 break; 674 } 675 case JVM_CONSTANT_Utf8: { 676 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted"); 677 } 678 } // switch(tag) 679 } // end of for 680 } 681 682 void ClassFileParser::patch_constant_pool(ConstantPool* cp, 683 int index, 684 Handle patch, 685 TRAPS) { 686 assert(cp != NULL, "invariant"); 687 688 BasicType patch_type = T_VOID; 689 690 switch (cp->tag_at(index).value()) { 691 692 case JVM_CONSTANT_UnresolvedClass: { 693 // Patching a class means pre-resolving it. 694 // The name in the constant pool is ignored. 695 if (java_lang_Class::is_instance(patch())) { 696 guarantee_property(!java_lang_Class::is_primitive(patch()), 697 "Illegal class patch at %d in class file %s", 698 index, CHECK); 699 cp->klass_at_put(index, java_lang_Class::as_Klass(patch())); 700 } else { 701 guarantee_property(java_lang_String::is_instance(patch()), 702 "Illegal class patch at %d in class file %s", 703 index, CHECK); 704 Symbol* const name = java_lang_String::as_symbol(patch(), CHECK); 705 cp->unresolved_klass_at_put(index, name); 706 } 707 break; 708 } 709 710 case JVM_CONSTANT_String: { 711 // skip this patch and don't clear it. Needs the oop array for resolved 712 // references to be created first. 713 return; 714 } 715 case JVM_CONSTANT_Integer: patch_type = T_INT; goto patch_prim; 716 case JVM_CONSTANT_Float: patch_type = T_FLOAT; goto patch_prim; 717 case JVM_CONSTANT_Long: patch_type = T_LONG; goto patch_prim; 718 case JVM_CONSTANT_Double: patch_type = T_DOUBLE; goto patch_prim; 719 patch_prim: 720 { 721 jvalue value; 722 BasicType value_type = java_lang_boxing_object::get_value(patch(), &value); 723 guarantee_property(value_type == patch_type, 724 "Illegal primitive patch at %d in class file %s", 725 index, CHECK); 726 switch (value_type) { 727 case T_INT: cp->int_at_put(index, value.i); break; 728 case T_FLOAT: cp->float_at_put(index, value.f); break; 729 case T_LONG: cp->long_at_put(index, value.j); break; 730 case T_DOUBLE: cp->double_at_put(index, value.d); break; 731 default: assert(false, ""); 732 } 733 } // end patch_prim label 734 break; 735 736 default: { 737 // %%% TODO: put method handles into CONSTANT_InterfaceMethodref, etc. 738 guarantee_property(!has_cp_patch_at(index), 739 "Illegal unexpected patch at %d in class file %s", 740 index, CHECK); 741 return; 742 } 743 } // end of switch(tag) 744 745 // On fall-through, mark the patch as used. 746 clear_cp_patch_at(index); 747 } 748 class NameSigHash: public ResourceObj { 749 public: 750 const Symbol* _name; // name 751 const Symbol* _sig; // signature 752 NameSigHash* _next; // Next entry in hash table 753 }; 754 755 static const int HASH_ROW_SIZE = 256; 756 757 static unsigned int hash(const Symbol* name, const Symbol* sig) { 758 unsigned int raw_hash = 0; 759 raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2); 760 raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize; 761 762 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE; 763 } 764 765 766 static void initialize_hashtable(NameSigHash** table) { 767 memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE); 768 } 769 // Return false if the name/sig combination is found in table. 770 // Return true if no duplicate is found. And name/sig is added as a new entry in table. 771 // The old format checker uses heap sort to find duplicates. 772 // NOTE: caller should guarantee that GC doesn't happen during the life cycle 773 // of table since we don't expect Symbol*'s to move. 774 static bool put_after_lookup(const Symbol* name, const Symbol* sig, NameSigHash** table) { 775 assert(name != NULL, "name in constant pool is NULL"); 776 777 // First lookup for duplicates 778 int index = hash(name, sig); 779 NameSigHash* entry = table[index]; 780 while (entry != NULL) { 781 if (entry->_name == name && entry->_sig == sig) { 782 return false; 783 } 784 entry = entry->_next; 785 } 786 787 // No duplicate is found, allocate a new entry and fill it. 788 entry = new NameSigHash(); 789 entry->_name = name; 790 entry->_sig = sig; 791 792 // Insert into hash table 793 entry->_next = table[index]; 794 table[index] = entry; 795 796 return true; 797 } 798 799 // Side-effects: populates the _local_interfaces field 800 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream, 801 const int itfs_len, 802 ConstantPool* const cp, 803 bool* const has_nonstatic_concrete_methods, 804 TRAPS) { 805 assert(stream != NULL, "invariant"); 806 assert(cp != NULL, "invariant"); 807 assert(has_nonstatic_concrete_methods != NULL, "invariant"); 808 809 if (itfs_len == 0) { 810 _local_interfaces = Universe::the_empty_klass_array(); 811 } else { 812 assert(itfs_len > 0, "only called for len>0"); 813 _local_interfaces = MetadataFactory::new_array<Klass*>(_loader_data, itfs_len, NULL, CHECK); 814 815 int index; 816 for (index = 0; index < itfs_len; index++) { 817 const u2 interface_index = stream->get_u2(CHECK); 818 Klass* interf; 819 check_property( 820 valid_klass_reference_at(interface_index), 821 "Interface name has bad constant pool index %u in class file %s", 822 interface_index, CHECK); 823 if (cp->tag_at(interface_index).is_klass()) { 824 interf = cp->resolved_klass_at(interface_index); 825 } else { 826 Symbol* const unresolved_klass = cp->klass_name_at(interface_index); 827 828 // Don't need to check legal name because it's checked when parsing constant pool. 829 // But need to make sure it's not an array type. 830 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY, 831 "Bad interface name in class file %s", CHECK); 832 833 // Call resolve_super so classcircularity is checked 834 interf = SystemDictionary::resolve_super_or_fail( 835 _class_name, 836 unresolved_klass, 837 Handle(THREAD, _loader_data->class_loader()), 838 _protection_domain, 839 false, 840 CHECK); 841 } 842 843 if (!interf->is_interface()) { 844 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 845 "Implementing class"); 846 } 847 848 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) { 849 *has_nonstatic_concrete_methods = true; 850 } 851 _local_interfaces->at_put(index, interf); 852 } 853 854 if (!_need_verify || itfs_len <= 1) { 855 return; 856 } 857 858 // Check if there's any duplicates in interfaces 859 ResourceMark rm(THREAD); 860 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 861 NameSigHash*, 862 HASH_ROW_SIZE); 863 initialize_hashtable(interface_names); 864 bool dup = false; 865 const Symbol* name = NULL; 866 { 867 debug_only(NoSafepointVerifier nsv;) 868 for (index = 0; index < itfs_len; index++) { 869 const Klass* const k = _local_interfaces->at(index); 870 name = InstanceKlass::cast(k)->name(); 871 // If no duplicates, add (name, NULL) in hashtable interface_names. 872 if (!put_after_lookup(name, NULL, interface_names)) { 873 dup = true; 874 break; 875 } 876 } 877 } 878 if (dup) { 879 classfile_parse_error("Duplicate interface name \"%s\" in class file %s", 880 name->as_C_string(), CHECK); 881 } 882 } 883 } 884 885 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp, 886 int constantvalue_index, 887 int signature_index, 888 TRAPS) const { 889 // Make sure the constant pool entry is of a type appropriate to this field 890 guarantee_property( 891 (constantvalue_index > 0 && 892 constantvalue_index < cp->length()), 893 "Bad initial value index %u in ConstantValue attribute in class file %s", 894 constantvalue_index, CHECK); 895 896 const constantTag value_type = cp->tag_at(constantvalue_index); 897 switch(cp->basic_type_for_signature_at(signature_index)) { 898 case T_LONG: { 899 guarantee_property(value_type.is_long(), 900 "Inconsistent constant value type in class file %s", 901 CHECK); 902 break; 903 } 904 case T_FLOAT: { 905 guarantee_property(value_type.is_float(), 906 "Inconsistent constant value type in class file %s", 907 CHECK); 908 break; 909 } 910 case T_DOUBLE: { 911 guarantee_property(value_type.is_double(), 912 "Inconsistent constant value type in class file %s", 913 CHECK); 914 break; 915 } 916 case T_BYTE: 917 case T_CHAR: 918 case T_SHORT: 919 case T_BOOLEAN: 920 case T_INT: { 921 guarantee_property(value_type.is_int(), 922 "Inconsistent constant value type in class file %s", 923 CHECK); 924 break; 925 } 926 case T_OBJECT: { 927 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") 928 && value_type.is_string()), 929 "Bad string initial value in class file %s", 930 CHECK); 931 break; 932 } 933 default: { 934 classfile_parse_error("Unable to set initial value %u in class file %s", 935 constantvalue_index, 936 CHECK); 937 } 938 } 939 } 940 941 class AnnotationCollector : public ResourceObj{ 942 public: 943 enum Location { _in_field, _in_method, _in_class }; 944 enum ID { 945 _unknown = 0, 946 _method_CallerSensitive, 947 _method_ForceInline, 948 _method_DontInline, 949 _method_InjectedProfile, 950 _method_LambdaForm_Compiled, 951 _method_LambdaForm_Hidden, 952 _method_HotSpotIntrinsicCandidate, 953 _jdk_internal_vm_annotation_Contended, 954 _field_Stable, 955 _jdk_internal_vm_annotation_ReservedStackAccess, 956 _annotation_LIMIT 957 }; 958 const Location _location; 959 int _annotations_present; 960 u2 _contended_group; 961 962 AnnotationCollector(Location location) 963 : _location(location), _annotations_present(0) 964 { 965 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, ""); 966 } 967 // If this annotation name has an ID, report it (or _none). 968 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name); 969 // Set the annotation name: 970 void set_annotation(ID id) { 971 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 972 _annotations_present |= nth_bit((int)id); 973 } 974 975 void remove_annotation(ID id) { 976 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 977 _annotations_present &= ~nth_bit((int)id); 978 } 979 980 // Report if the annotation is present. 981 bool has_any_annotations() const { return _annotations_present != 0; } 982 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; } 983 984 void set_contended_group(u2 group) { _contended_group = group; } 985 u2 contended_group() const { return _contended_group; } 986 987 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); } 988 989 void set_stable(bool stable) { set_annotation(_field_Stable); } 990 bool is_stable() const { return has_annotation(_field_Stable); } 991 }; 992 993 // This class also doubles as a holder for metadata cleanup. 994 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector { 995 private: 996 ClassLoaderData* _loader_data; 997 AnnotationArray* _field_annotations; 998 AnnotationArray* _field_type_annotations; 999 public: 1000 FieldAnnotationCollector(ClassLoaderData* loader_data) : 1001 AnnotationCollector(_in_field), 1002 _loader_data(loader_data), 1003 _field_annotations(NULL), 1004 _field_type_annotations(NULL) {} 1005 ~FieldAnnotationCollector(); 1006 void apply_to(FieldInfo* f); 1007 AnnotationArray* field_annotations() { return _field_annotations; } 1008 AnnotationArray* field_type_annotations() { return _field_type_annotations; } 1009 1010 void set_field_annotations(AnnotationArray* a) { _field_annotations = a; } 1011 void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; } 1012 }; 1013 1014 class MethodAnnotationCollector : public AnnotationCollector{ 1015 public: 1016 MethodAnnotationCollector() : AnnotationCollector(_in_method) { } 1017 void apply_to(methodHandle m); 1018 }; 1019 1020 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{ 1021 public: 1022 ClassAnnotationCollector() : AnnotationCollector(_in_class) { } 1023 void apply_to(InstanceKlass* ik); 1024 }; 1025 1026 1027 static int skip_annotation_value(const u1*, int, int); // fwd decl 1028 1029 // Safely increment index by val if does not pass limit 1030 #define SAFE_ADD(index, limit, val) \ 1031 if (index >= limit - val) return limit; \ 1032 index += val; 1033 1034 // Skip an annotation. Return >=limit if there is any problem. 1035 static int skip_annotation(const u1* buffer, int limit, int index) { 1036 assert(buffer != NULL, "invariant"); 1037 // annotation := atype:u2 do(nmem:u2) {member:u2 value} 1038 // value := switch (tag:u1) { ... } 1039 SAFE_ADD(index, limit, 4); // skip atype and read nmem 1040 int nmem = Bytes::get_Java_u2((address)buffer + index - 2); 1041 while (--nmem >= 0 && index < limit) { 1042 SAFE_ADD(index, limit, 2); // skip member 1043 index = skip_annotation_value(buffer, limit, index); 1044 } 1045 return index; 1046 } 1047 1048 // Skip an annotation value. Return >=limit if there is any problem. 1049 static int skip_annotation_value(const u1* buffer, int limit, int index) { 1050 assert(buffer != NULL, "invariant"); 1051 1052 // value := switch (tag:u1) { 1053 // case B, C, I, S, Z, D, F, J, c: con:u2; 1054 // case e: e_class:u2 e_name:u2; 1055 // case s: s_con:u2; 1056 // case [: do(nval:u2) {value}; 1057 // case @: annotation; 1058 // case s: s_con:u2; 1059 // } 1060 SAFE_ADD(index, limit, 1); // read tag 1061 const u1 tag = buffer[index - 1]; 1062 switch (tag) { 1063 case 'B': 1064 case 'C': 1065 case 'I': 1066 case 'S': 1067 case 'Z': 1068 case 'D': 1069 case 'F': 1070 case 'J': 1071 case 'c': 1072 case 's': 1073 SAFE_ADD(index, limit, 2); // skip con or s_con 1074 break; 1075 case 'e': 1076 SAFE_ADD(index, limit, 4); // skip e_class, e_name 1077 break; 1078 case '[': 1079 { 1080 SAFE_ADD(index, limit, 2); // read nval 1081 int nval = Bytes::get_Java_u2((address)buffer + index - 2); 1082 while (--nval >= 0 && index < limit) { 1083 index = skip_annotation_value(buffer, limit, index); 1084 } 1085 } 1086 break; 1087 case '@': 1088 index = skip_annotation(buffer, limit, index); 1089 break; 1090 default: 1091 return limit; // bad tag byte 1092 } 1093 return index; 1094 } 1095 1096 // Sift through annotations, looking for those significant to the VM: 1097 static void parse_annotations(const ConstantPool* const cp, 1098 const u1* buffer, int limit, 1099 AnnotationCollector* coll, 1100 ClassLoaderData* loader_data, 1101 TRAPS) { 1102 1103 assert(cp != NULL, "invariant"); 1104 assert(buffer != NULL, "invariant"); 1105 assert(coll != NULL, "invariant"); 1106 assert(loader_data != NULL, "invariant"); 1107 1108 // annotations := do(nann:u2) {annotation} 1109 int index = 2; // read nann 1110 if (index >= limit) return; 1111 int nann = Bytes::get_Java_u2((address)buffer + index - 2); 1112 enum { // initial annotation layout 1113 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' 1114 count_off = 2, // u2 such as 1 (one value) 1115 member_off = 4, // utf8 such as 'value' 1116 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) 1117 e_tag_val = 'e', 1118 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' 1119 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' 1120 e_size = 11, // end of 'e' annotation 1121 c_tag_val = 'c', // payload is type 1122 c_con_off = 7, // utf8 payload, such as 'I' 1123 c_size = 9, // end of 'c' annotation 1124 s_tag_val = 's', // payload is String 1125 s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' 1126 s_size = 9, 1127 min_size = 6 // smallest possible size (zero members) 1128 }; 1129 // Cannot add min_size to index in case of overflow MAX_INT 1130 while ((--nann) >= 0 && (index - 2 <= limit - min_size)) { 1131 int index0 = index; 1132 index = skip_annotation(buffer, limit, index); 1133 const u1* const abase = buffer + index0; 1134 const int atype = Bytes::get_Java_u2((address)abase + atype_off); 1135 const int count = Bytes::get_Java_u2((address)abase + count_off); 1136 const Symbol* const aname = check_symbol_at(cp, atype); 1137 if (aname == NULL) break; // invalid annotation name 1138 const Symbol* member = NULL; 1139 if (count >= 1) { 1140 const int member_index = Bytes::get_Java_u2((address)abase + member_off); 1141 member = check_symbol_at(cp, member_index); 1142 if (member == NULL) break; // invalid member name 1143 } 1144 1145 // Here is where parsing particular annotations will take place. 1146 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname); 1147 if (AnnotationCollector::_unknown == id) continue; 1148 coll->set_annotation(id); 1149 1150 if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) { 1151 // @Contended can optionally specify the contention group. 1152 // 1153 // Contended group defines the equivalence class over the fields: 1154 // the fields within the same contended group are not treated distinct. 1155 // The only exception is default group, which does not incur the 1156 // equivalence. Naturally, contention group for classes is meaningless. 1157 // 1158 // While the contention group is specified as String, annotation 1159 // values are already interned, and we might as well use the constant 1160 // pool index as the group tag. 1161 // 1162 u2 group_index = 0; // default contended group 1163 if (count == 1 1164 && s_size == (index - index0) // match size 1165 && s_tag_val == *(abase + tag_off) 1166 && member == vmSymbols::value_name()) { 1167 group_index = Bytes::get_Java_u2((address)abase + s_con_off); 1168 if (cp->symbol_at(group_index)->utf8_length() == 0) { 1169 group_index = 0; // default contended group 1170 } 1171 } 1172 coll->set_contended_group(group_index); 1173 } 1174 } 1175 } 1176 1177 1178 // Parse attributes for a field. 1179 void ClassFileParser::parse_field_attributes(const ClassFileStream* const cfs, 1180 u2 attributes_count, 1181 bool is_static, u2 signature_index, 1182 u2* const constantvalue_index_addr, 1183 bool* const is_synthetic_addr, 1184 u2* const generic_signature_index_addr, 1185 ClassFileParser::FieldAnnotationCollector* parsed_annotations, 1186 TRAPS) { 1187 assert(cfs != NULL, "invariant"); 1188 assert(constantvalue_index_addr != NULL, "invariant"); 1189 assert(is_synthetic_addr != NULL, "invariant"); 1190 assert(generic_signature_index_addr != NULL, "invariant"); 1191 assert(parsed_annotations != NULL, "invariant"); 1192 assert(attributes_count > 0, "attributes_count should be greater than 0"); 1193 1194 u2 constantvalue_index = 0; 1195 u2 generic_signature_index = 0; 1196 bool is_synthetic = false; 1197 const u1* runtime_visible_annotations = NULL; 1198 int runtime_visible_annotations_length = 0; 1199 const u1* runtime_invisible_annotations = NULL; 1200 int runtime_invisible_annotations_length = 0; 1201 const u1* runtime_visible_type_annotations = NULL; 1202 int runtime_visible_type_annotations_length = 0; 1203 const u1* runtime_invisible_type_annotations = NULL; 1204 int runtime_invisible_type_annotations_length = 0; 1205 bool runtime_invisible_annotations_exists = false; 1206 bool runtime_invisible_type_annotations_exists = false; 1207 const ConstantPool* const cp = _cp; 1208 1209 while (attributes_count--) { 1210 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 1211 const u2 attribute_name_index = cfs->get_u2_fast(); 1212 const u4 attribute_length = cfs->get_u4_fast(); 1213 check_property(valid_symbol_at(attribute_name_index), 1214 "Invalid field attribute index %u in class file %s", 1215 attribute_name_index, 1216 CHECK); 1217 1218 const Symbol* const attribute_name = cp->symbol_at(attribute_name_index); 1219 if (is_static && attribute_name == vmSymbols::tag_constant_value()) { 1220 // ignore if non-static 1221 if (constantvalue_index != 0) { 1222 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK); 1223 } 1224 check_property( 1225 attribute_length == 2, 1226 "Invalid ConstantValue field attribute length %u in class file %s", 1227 attribute_length, CHECK); 1228 1229 constantvalue_index = cfs->get_u2(CHECK); 1230 if (_need_verify) { 1231 verify_constantvalue(cp, constantvalue_index, signature_index, CHECK); 1232 } 1233 } else if (attribute_name == vmSymbols::tag_synthetic()) { 1234 if (attribute_length != 0) { 1235 classfile_parse_error( 1236 "Invalid Synthetic field attribute length %u in class file %s", 1237 attribute_length, CHECK); 1238 } 1239 is_synthetic = true; 1240 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120 1241 if (attribute_length != 0) { 1242 classfile_parse_error( 1243 "Invalid Deprecated field attribute length %u in class file %s", 1244 attribute_length, CHECK); 1245 } 1246 } else if (_major_version >= JAVA_1_5_VERSION) { 1247 if (attribute_name == vmSymbols::tag_signature()) { 1248 if (attribute_length != 2) { 1249 classfile_parse_error( 1250 "Wrong size %u for field's Signature attribute in class file %s", 1251 attribute_length, CHECK); 1252 } 1253 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK); 1254 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 1255 if (runtime_visible_annotations != NULL) { 1256 classfile_parse_error( 1257 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK); 1258 } 1259 runtime_visible_annotations_length = attribute_length; 1260 runtime_visible_annotations = cfs->get_u1_buffer(); 1261 assert(runtime_visible_annotations != NULL, "null visible annotations"); 1262 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 1263 parse_annotations(cp, 1264 runtime_visible_annotations, 1265 runtime_visible_annotations_length, 1266 parsed_annotations, 1267 _loader_data, 1268 CHECK); 1269 cfs->skip_u1_fast(runtime_visible_annotations_length); 1270 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 1271 if (runtime_invisible_annotations_exists) { 1272 classfile_parse_error( 1273 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK); 1274 } 1275 runtime_invisible_annotations_exists = true; 1276 if (PreserveAllAnnotations) { 1277 runtime_invisible_annotations_length = attribute_length; 1278 runtime_invisible_annotations = cfs->get_u1_buffer(); 1279 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 1280 } 1281 cfs->skip_u1(attribute_length, CHECK); 1282 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 1283 if (runtime_visible_type_annotations != NULL) { 1284 classfile_parse_error( 1285 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK); 1286 } 1287 runtime_visible_type_annotations_length = attribute_length; 1288 runtime_visible_type_annotations = cfs->get_u1_buffer(); 1289 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 1290 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 1291 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 1292 if (runtime_invisible_type_annotations_exists) { 1293 classfile_parse_error( 1294 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", CHECK); 1295 } else { 1296 runtime_invisible_type_annotations_exists = true; 1297 } 1298 if (PreserveAllAnnotations) { 1299 runtime_invisible_type_annotations_length = attribute_length; 1300 runtime_invisible_type_annotations = cfs->get_u1_buffer(); 1301 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 1302 } 1303 cfs->skip_u1(attribute_length, CHECK); 1304 } else { 1305 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1306 } 1307 } else { 1308 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1309 } 1310 } 1311 1312 *constantvalue_index_addr = constantvalue_index; 1313 *is_synthetic_addr = is_synthetic; 1314 *generic_signature_index_addr = generic_signature_index; 1315 AnnotationArray* a = assemble_annotations(runtime_visible_annotations, 1316 runtime_visible_annotations_length, 1317 runtime_invisible_annotations, 1318 runtime_invisible_annotations_length, 1319 CHECK); 1320 parsed_annotations->set_field_annotations(a); 1321 a = assemble_annotations(runtime_visible_type_annotations, 1322 runtime_visible_type_annotations_length, 1323 runtime_invisible_type_annotations, 1324 runtime_invisible_type_annotations_length, 1325 CHECK); 1326 parsed_annotations->set_field_type_annotations(a); 1327 return; 1328 } 1329 1330 1331 // Field allocation types. Used for computing field offsets. 1332 1333 enum FieldAllocationType { 1334 STATIC_OOP, // Oops 1335 STATIC_BYTE, // Boolean, Byte, char 1336 STATIC_SHORT, // shorts 1337 STATIC_WORD, // ints 1338 STATIC_DOUBLE, // aligned long or double 1339 NONSTATIC_OOP, 1340 NONSTATIC_BYTE, 1341 NONSTATIC_SHORT, 1342 NONSTATIC_WORD, 1343 NONSTATIC_DOUBLE, 1344 MAX_FIELD_ALLOCATION_TYPE, 1345 BAD_ALLOCATION_TYPE = -1 1346 }; 1347 1348 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = { 1349 BAD_ALLOCATION_TYPE, // 0 1350 BAD_ALLOCATION_TYPE, // 1 1351 BAD_ALLOCATION_TYPE, // 2 1352 BAD_ALLOCATION_TYPE, // 3 1353 NONSTATIC_BYTE , // T_BOOLEAN = 4, 1354 NONSTATIC_SHORT, // T_CHAR = 5, 1355 NONSTATIC_WORD, // T_FLOAT = 6, 1356 NONSTATIC_DOUBLE, // T_DOUBLE = 7, 1357 NONSTATIC_BYTE, // T_BYTE = 8, 1358 NONSTATIC_SHORT, // T_SHORT = 9, 1359 NONSTATIC_WORD, // T_INT = 10, 1360 NONSTATIC_DOUBLE, // T_LONG = 11, 1361 NONSTATIC_OOP, // T_OBJECT = 12, 1362 NONSTATIC_OOP, // T_ARRAY = 13, 1363 BAD_ALLOCATION_TYPE, // T_VOID = 14, 1364 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, 1365 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, 1366 BAD_ALLOCATION_TYPE, // T_METADATA = 17, 1367 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, 1368 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, 1369 BAD_ALLOCATION_TYPE, // 0 1370 BAD_ALLOCATION_TYPE, // 1 1371 BAD_ALLOCATION_TYPE, // 2 1372 BAD_ALLOCATION_TYPE, // 3 1373 STATIC_BYTE , // T_BOOLEAN = 4, 1374 STATIC_SHORT, // T_CHAR = 5, 1375 STATIC_WORD, // T_FLOAT = 6, 1376 STATIC_DOUBLE, // T_DOUBLE = 7, 1377 STATIC_BYTE, // T_BYTE = 8, 1378 STATIC_SHORT, // T_SHORT = 9, 1379 STATIC_WORD, // T_INT = 10, 1380 STATIC_DOUBLE, // T_LONG = 11, 1381 STATIC_OOP, // T_OBJECT = 12, 1382 STATIC_OOP, // T_ARRAY = 13, 1383 BAD_ALLOCATION_TYPE, // T_VOID = 14, 1384 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, 1385 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, 1386 BAD_ALLOCATION_TYPE, // T_METADATA = 17, 1387 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, 1388 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, 1389 }; 1390 1391 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) { 1392 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values"); 1393 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)]; 1394 assert(result != BAD_ALLOCATION_TYPE, "bad type"); 1395 return result; 1396 } 1397 1398 class ClassFileParser::FieldAllocationCount : public ResourceObj { 1399 public: 1400 u2 count[MAX_FIELD_ALLOCATION_TYPE]; 1401 1402 FieldAllocationCount() { 1403 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) { 1404 count[i] = 0; 1405 } 1406 } 1407 1408 FieldAllocationType update(bool is_static, BasicType type) { 1409 FieldAllocationType atype = basic_type_to_atype(is_static, type); 1410 if (atype != BAD_ALLOCATION_TYPE) { 1411 // Make sure there is no overflow with injected fields. 1412 assert(count[atype] < 0xFFFF, "More than 65535 fields"); 1413 count[atype]++; 1414 } 1415 return atype; 1416 } 1417 }; 1418 1419 // Side-effects: populates the _fields, _fields_annotations, 1420 // _fields_type_annotations fields 1421 void ClassFileParser::parse_fields(const ClassFileStream* const cfs, 1422 bool is_interface, 1423 FieldAllocationCount* const fac, 1424 ConstantPool* cp, 1425 const int cp_size, 1426 u2* const java_fields_count_ptr, 1427 TRAPS) { 1428 1429 assert(cfs != NULL, "invariant"); 1430 assert(fac != NULL, "invariant"); 1431 assert(cp != NULL, "invariant"); 1432 assert(java_fields_count_ptr != NULL, "invariant"); 1433 1434 assert(NULL == _fields, "invariant"); 1435 assert(NULL == _fields_annotations, "invariant"); 1436 assert(NULL == _fields_type_annotations, "invariant"); 1437 1438 cfs->guarantee_more(2, CHECK); // length 1439 const u2 length = cfs->get_u2_fast(); 1440 *java_fields_count_ptr = length; 1441 1442 int num_injected = 0; 1443 const InjectedField* const injected = JavaClasses::get_injected(_class_name, 1444 &num_injected); 1445 const int total_fields = length + num_injected; 1446 1447 // The field array starts with tuples of shorts 1448 // [access, name index, sig index, initial value index, byte offset]. 1449 // A generic signature slot only exists for field with generic 1450 // signature attribute. And the access flag is set with 1451 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic 1452 // signature slots are at the end of the field array and after all 1453 // other fields data. 1454 // 1455 // f1: [access, name index, sig index, initial value index, low_offset, high_offset] 1456 // f2: [access, name index, sig index, initial value index, low_offset, high_offset] 1457 // ... 1458 // fn: [access, name index, sig index, initial value index, low_offset, high_offset] 1459 // [generic signature index] 1460 // [generic signature index] 1461 // ... 1462 // 1463 // Allocate a temporary resource array for field data. For each field, 1464 // a slot is reserved in the temporary array for the generic signature 1465 // index. After parsing all fields, the data are copied to a permanent 1466 // array and any unused slots will be discarded. 1467 ResourceMark rm(THREAD); 1468 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 1469 u2, 1470 total_fields * (FieldInfo::field_slots + 1)); 1471 1472 // The generic signature slots start after all other fields' data. 1473 int generic_signature_slot = total_fields * FieldInfo::field_slots; 1474 int num_generic_signature = 0; 1475 for (int n = 0; n < length; n++) { 1476 // access_flags, name_index, descriptor_index, attributes_count 1477 cfs->guarantee_more(8, CHECK); 1478 1479 AccessFlags access_flags; 1480 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS; 1481 verify_legal_field_modifiers(flags, is_interface, CHECK); 1482 access_flags.set_flags(flags); 1483 1484 const u2 name_index = cfs->get_u2_fast(); 1485 check_property(valid_symbol_at(name_index), 1486 "Invalid constant pool index %u for field name in class file %s", 1487 name_index, CHECK); 1488 const Symbol* const name = cp->symbol_at(name_index); 1489 verify_legal_field_name(name, CHECK); 1490 1491 const u2 signature_index = cfs->get_u2_fast(); 1492 check_property(valid_symbol_at(signature_index), 1493 "Invalid constant pool index %u for field signature in class file %s", 1494 signature_index, CHECK); 1495 const Symbol* const sig = cp->symbol_at(signature_index); 1496 verify_legal_field_signature(name, sig, CHECK); 1497 1498 u2 constantvalue_index = 0; 1499 bool is_synthetic = false; 1500 u2 generic_signature_index = 0; 1501 const bool is_static = access_flags.is_static(); 1502 FieldAnnotationCollector parsed_annotations(_loader_data); 1503 1504 const u2 attributes_count = cfs->get_u2_fast(); 1505 if (attributes_count > 0) { 1506 parse_field_attributes(cfs, 1507 attributes_count, 1508 is_static, 1509 signature_index, 1510 &constantvalue_index, 1511 &is_synthetic, 1512 &generic_signature_index, 1513 &parsed_annotations, 1514 CHECK); 1515 1516 if (parsed_annotations.field_annotations() != NULL) { 1517 if (_fields_annotations == NULL) { 1518 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>( 1519 _loader_data, length, NULL, 1520 CHECK); 1521 } 1522 _fields_annotations->at_put(n, parsed_annotations.field_annotations()); 1523 parsed_annotations.set_field_annotations(NULL); 1524 } 1525 if (parsed_annotations.field_type_annotations() != NULL) { 1526 if (_fields_type_annotations == NULL) { 1527 _fields_type_annotations = 1528 MetadataFactory::new_array<AnnotationArray*>(_loader_data, 1529 length, 1530 NULL, 1531 CHECK); 1532 } 1533 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations()); 1534 parsed_annotations.set_field_type_annotations(NULL); 1535 } 1536 1537 if (is_synthetic) { 1538 access_flags.set_is_synthetic(); 1539 } 1540 if (generic_signature_index != 0) { 1541 access_flags.set_field_has_generic_signature(); 1542 fa[generic_signature_slot] = generic_signature_index; 1543 generic_signature_slot ++; 1544 num_generic_signature ++; 1545 } 1546 } 1547 1548 FieldInfo* const field = FieldInfo::from_field_array(fa, n); 1549 field->initialize(access_flags.as_short(), 1550 name_index, 1551 signature_index, 1552 constantvalue_index); 1553 const BasicType type = cp->basic_type_for_signature_at(signature_index); 1554 1555 // Remember how many oops we encountered and compute allocation type 1556 const FieldAllocationType atype = fac->update(is_static, type); 1557 field->set_allocation_type(atype); 1558 1559 // After field is initialized with type, we can augment it with aux info 1560 if (parsed_annotations.has_any_annotations()) 1561 parsed_annotations.apply_to(field); 1562 } 1563 1564 int index = length; 1565 if (num_injected != 0) { 1566 for (int n = 0; n < num_injected; n++) { 1567 // Check for duplicates 1568 if (injected[n].may_be_java) { 1569 const Symbol* const name = injected[n].name(); 1570 const Symbol* const signature = injected[n].signature(); 1571 bool duplicate = false; 1572 for (int i = 0; i < length; i++) { 1573 const FieldInfo* const f = FieldInfo::from_field_array(fa, i); 1574 if (name == cp->symbol_at(f->name_index()) && 1575 signature == cp->symbol_at(f->signature_index())) { 1576 // Symbol is desclared in Java so skip this one 1577 duplicate = true; 1578 break; 1579 } 1580 } 1581 if (duplicate) { 1582 // These will be removed from the field array at the end 1583 continue; 1584 } 1585 } 1586 1587 // Injected field 1588 FieldInfo* const field = FieldInfo::from_field_array(fa, index); 1589 field->initialize(JVM_ACC_FIELD_INTERNAL, 1590 injected[n].name_index, 1591 injected[n].signature_index, 1592 0); 1593 1594 const BasicType type = FieldType::basic_type(injected[n].signature()); 1595 1596 // Remember how many oops we encountered and compute allocation type 1597 const FieldAllocationType atype = fac->update(false, type); 1598 field->set_allocation_type(atype); 1599 index++; 1600 } 1601 } 1602 1603 assert(NULL == _fields, "invariant"); 1604 1605 _fields = 1606 MetadataFactory::new_array<u2>(_loader_data, 1607 index * FieldInfo::field_slots + num_generic_signature, 1608 CHECK); 1609 // Sometimes injected fields already exist in the Java source so 1610 // the fields array could be too long. In that case the 1611 // fields array is trimed. Also unused slots that were reserved 1612 // for generic signature indexes are discarded. 1613 { 1614 int i = 0; 1615 for (; i < index * FieldInfo::field_slots; i++) { 1616 _fields->at_put(i, fa[i]); 1617 } 1618 for (int j = total_fields * FieldInfo::field_slots; 1619 j < generic_signature_slot; j++) { 1620 _fields->at_put(i++, fa[j]); 1621 } 1622 assert(_fields->length() == i, ""); 1623 } 1624 1625 if (_need_verify && length > 1) { 1626 // Check duplicated fields 1627 ResourceMark rm(THREAD); 1628 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 1629 THREAD, NameSigHash*, HASH_ROW_SIZE); 1630 initialize_hashtable(names_and_sigs); 1631 bool dup = false; 1632 const Symbol* name = NULL; 1633 const Symbol* sig = NULL; 1634 { 1635 debug_only(NoSafepointVerifier nsv;) 1636 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 1637 name = fs.name(); 1638 sig = fs.signature(); 1639 // If no duplicates, add name/signature in hashtable names_and_sigs. 1640 if (!put_after_lookup(name, sig, names_and_sigs)) { 1641 dup = true; 1642 break; 1643 } 1644 } 1645 } 1646 if (dup) { 1647 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s", 1648 name->as_C_string(), sig->as_klass_external_name(), CHECK); 1649 } 1650 } 1651 } 1652 1653 1654 static void copy_u2_with_conversion(u2* dest, const u2* src, int length) { 1655 while (length-- > 0) { 1656 *dest++ = Bytes::get_Java_u2((u1*) (src++)); 1657 } 1658 } 1659 1660 const u2* ClassFileParser::parse_exception_table(const ClassFileStream* const cfs, 1661 u4 code_length, 1662 u4 exception_table_length, 1663 TRAPS) { 1664 assert(cfs != NULL, "invariant"); 1665 1666 const u2* const exception_table_start = cfs->get_u2_buffer(); 1667 assert(exception_table_start != NULL, "null exception table"); 1668 1669 cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, 1670 // end_pc, 1671 // handler_pc, 1672 // catch_type_index 1673 1674 // Will check legal target after parsing code array in verifier. 1675 if (_need_verify) { 1676 for (unsigned int i = 0; i < exception_table_length; i++) { 1677 const u2 start_pc = cfs->get_u2_fast(); 1678 const u2 end_pc = cfs->get_u2_fast(); 1679 const u2 handler_pc = cfs->get_u2_fast(); 1680 const u2 catch_type_index = cfs->get_u2_fast(); 1681 guarantee_property((start_pc < end_pc) && (end_pc <= code_length), 1682 "Illegal exception table range in class file %s", 1683 CHECK_NULL); 1684 guarantee_property(handler_pc < code_length, 1685 "Illegal exception table handler in class file %s", 1686 CHECK_NULL); 1687 if (catch_type_index != 0) { 1688 guarantee_property(valid_klass_reference_at(catch_type_index), 1689 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); 1690 } 1691 } 1692 } else { 1693 cfs->skip_u2_fast(exception_table_length * 4); 1694 } 1695 return exception_table_start; 1696 } 1697 1698 void ClassFileParser::parse_linenumber_table(u4 code_attribute_length, 1699 u4 code_length, 1700 CompressedLineNumberWriteStream**const write_stream, 1701 TRAPS) { 1702 1703 const ClassFileStream* const cfs = _stream; 1704 unsigned int num_entries = cfs->get_u2(CHECK); 1705 1706 // Each entry is a u2 start_pc, and a u2 line_number 1707 const unsigned int length_in_bytes = num_entries * (sizeof(u2) * 2); 1708 1709 // Verify line number attribute and table length 1710 check_property( 1711 code_attribute_length == sizeof(u2) + length_in_bytes, 1712 "LineNumberTable attribute has wrong length in class file %s", CHECK); 1713 1714 cfs->guarantee_more(length_in_bytes, CHECK); 1715 1716 if ((*write_stream) == NULL) { 1717 if (length_in_bytes > fixed_buffer_size) { 1718 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes); 1719 } else { 1720 (*write_stream) = new CompressedLineNumberWriteStream( 1721 _linenumbertable_buffer, fixed_buffer_size); 1722 } 1723 } 1724 1725 while (num_entries-- > 0) { 1726 const u2 bci = cfs->get_u2_fast(); // start_pc 1727 const u2 line = cfs->get_u2_fast(); // line_number 1728 guarantee_property(bci < code_length, 1729 "Invalid pc in LineNumberTable in class file %s", CHECK); 1730 (*write_stream)->write_pair(bci, line); 1731 } 1732 } 1733 1734 1735 class LVT_Hash : public AllStatic { 1736 public: 1737 1738 static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) { 1739 /* 1740 * 3-tuple start_bci/length/slot has to be unique key, 1741 * so the following comparison seems to be redundant: 1742 * && elem->name_cp_index == entry->_elem->name_cp_index 1743 */ 1744 return (e0.start_bci == e1.start_bci && 1745 e0.length == e1.length && 1746 e0.name_cp_index == e1.name_cp_index && 1747 e0.slot == e1.slot); 1748 } 1749 1750 static unsigned int hash(LocalVariableTableElement const& e0) { 1751 unsigned int raw_hash = e0.start_bci; 1752 1753 raw_hash = e0.length + raw_hash * 37; 1754 raw_hash = e0.name_cp_index + raw_hash * 37; 1755 raw_hash = e0.slot + raw_hash * 37; 1756 1757 return raw_hash; 1758 } 1759 }; 1760 1761 1762 // Class file LocalVariableTable elements. 1763 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC { 1764 public: 1765 u2 start_bci; 1766 u2 length; 1767 u2 name_cp_index; 1768 u2 descriptor_cp_index; 1769 u2 slot; 1770 }; 1771 1772 static void copy_lvt_element(const Classfile_LVT_Element* const src, 1773 LocalVariableTableElement* const lvt) { 1774 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci); 1775 lvt->length = Bytes::get_Java_u2((u1*) &src->length); 1776 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index); 1777 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index); 1778 lvt->signature_cp_index = 0; 1779 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot); 1780 } 1781 1782 // Function is used to parse both attributes: 1783 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) 1784 const u2* ClassFileParser::parse_localvariable_table(const ClassFileStream* cfs, 1785 u4 code_length, 1786 u2 max_locals, 1787 u4 code_attribute_length, 1788 u2* const localvariable_table_length, 1789 bool isLVTT, 1790 TRAPS) { 1791 const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; 1792 *localvariable_table_length = cfs->get_u2(CHECK_NULL); 1793 const unsigned int size = 1794 (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2); 1795 1796 const ConstantPool* const cp = _cp; 1797 1798 // Verify local variable table attribute has right length 1799 if (_need_verify) { 1800 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)), 1801 "%s has wrong length in class file %s", tbl_name, CHECK_NULL); 1802 } 1803 1804 const u2* const localvariable_table_start = cfs->get_u2_buffer(); 1805 assert(localvariable_table_start != NULL, "null local variable table"); 1806 if (!_need_verify) { 1807 cfs->skip_u2_fast(size); 1808 } else { 1809 cfs->guarantee_more(size * 2, CHECK_NULL); 1810 for(int i = 0; i < (*localvariable_table_length); i++) { 1811 const u2 start_pc = cfs->get_u2_fast(); 1812 const u2 length = cfs->get_u2_fast(); 1813 const u2 name_index = cfs->get_u2_fast(); 1814 const u2 descriptor_index = cfs->get_u2_fast(); 1815 const u2 index = cfs->get_u2_fast(); 1816 // Assign to a u4 to avoid overflow 1817 const u4 end_pc = (u4)start_pc + (u4)length; 1818 1819 if (start_pc >= code_length) { 1820 classfile_parse_error( 1821 "Invalid start_pc %u in %s in class file %s", 1822 start_pc, tbl_name, CHECK_NULL); 1823 } 1824 if (end_pc > code_length) { 1825 classfile_parse_error( 1826 "Invalid length %u in %s in class file %s", 1827 length, tbl_name, CHECK_NULL); 1828 } 1829 const int cp_size = cp->length(); 1830 guarantee_property(valid_symbol_at(name_index), 1831 "Name index %u in %s has bad constant type in class file %s", 1832 name_index, tbl_name, CHECK_NULL); 1833 guarantee_property(valid_symbol_at(descriptor_index), 1834 "Signature index %u in %s has bad constant type in class file %s", 1835 descriptor_index, tbl_name, CHECK_NULL); 1836 1837 const Symbol* const name = cp->symbol_at(name_index); 1838 const Symbol* const sig = cp->symbol_at(descriptor_index); 1839 verify_legal_field_name(name, CHECK_NULL); 1840 u2 extra_slot = 0; 1841 if (!isLVTT) { 1842 verify_legal_field_signature(name, sig, CHECK_NULL); 1843 1844 // 4894874: check special cases for double and long local variables 1845 if (sig == vmSymbols::type_signature(T_DOUBLE) || 1846 sig == vmSymbols::type_signature(T_LONG)) { 1847 extra_slot = 1; 1848 } 1849 } 1850 guarantee_property((index + extra_slot) < max_locals, 1851 "Invalid index %u in %s in class file %s", 1852 index, tbl_name, CHECK_NULL); 1853 } 1854 } 1855 return localvariable_table_start; 1856 } 1857 1858 1859 void ClassFileParser::parse_type_array(u2 array_length, 1860 u4 code_length, 1861 u4* const u1_index, 1862 u4* const u2_index, 1863 u1* const u1_array, 1864 u2* const u2_array, 1865 TRAPS) { 1866 const ClassFileStream* const cfs = _stream; 1867 u2 index = 0; // index in the array with long/double occupying two slots 1868 u4 i1 = *u1_index; 1869 u4 i2 = *u2_index + 1; 1870 for(int i = 0; i < array_length; i++) { 1871 const u1 tag = u1_array[i1++] = cfs->get_u1(CHECK); 1872 index++; 1873 if (tag == ITEM_Long || tag == ITEM_Double) { 1874 index++; 1875 } else if (tag == ITEM_Object) { 1876 const u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK); 1877 guarantee_property(valid_klass_reference_at(class_index), 1878 "Bad class index %u in StackMap in class file %s", 1879 class_index, CHECK); 1880 } else if (tag == ITEM_Uninitialized) { 1881 const u2 offset = u2_array[i2++] = cfs->get_u2(CHECK); 1882 guarantee_property( 1883 offset < code_length, 1884 "Bad uninitialized type offset %u in StackMap in class file %s", 1885 offset, CHECK); 1886 } else { 1887 guarantee_property( 1888 tag <= (u1)ITEM_Uninitialized, 1889 "Unknown variable type %u in StackMap in class file %s", 1890 tag, CHECK); 1891 } 1892 } 1893 u2_array[*u2_index] = index; 1894 *u1_index = i1; 1895 *u2_index = i2; 1896 } 1897 1898 static const u1* parse_stackmap_table(const ClassFileStream* const cfs, 1899 u4 code_attribute_length, 1900 bool need_verify, 1901 TRAPS) { 1902 assert(cfs != NULL, "invariant"); 1903 1904 if (0 == code_attribute_length) { 1905 return NULL; 1906 } 1907 1908 const u1* const stackmap_table_start = cfs->get_u1_buffer(); 1909 assert(stackmap_table_start != NULL, "null stackmap table"); 1910 1911 // check code_attribute_length first 1912 cfs->skip_u1(code_attribute_length, CHECK_NULL); 1913 1914 if (!need_verify && !DumpSharedSpaces) { 1915 return NULL; 1916 } 1917 return stackmap_table_start; 1918 } 1919 1920 const u2* ClassFileParser::parse_checked_exceptions(const ClassFileStream* const cfs, 1921 u2* const checked_exceptions_length, 1922 u4 method_attribute_length, 1923 TRAPS) { 1924 assert(cfs != NULL, "invariant"); 1925 assert(checked_exceptions_length != NULL, "invariant"); 1926 1927 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length 1928 *checked_exceptions_length = cfs->get_u2_fast(); 1929 const unsigned int size = 1930 (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); 1931 const u2* const checked_exceptions_start = cfs->get_u2_buffer(); 1932 assert(checked_exceptions_start != NULL, "null checked exceptions"); 1933 if (!_need_verify) { 1934 cfs->skip_u2_fast(size); 1935 } else { 1936 // Verify each value in the checked exception table 1937 u2 checked_exception; 1938 const u2 len = *checked_exceptions_length; 1939 cfs->guarantee_more(2 * len, CHECK_NULL); 1940 for (int i = 0; i < len; i++) { 1941 checked_exception = cfs->get_u2_fast(); 1942 check_property( 1943 valid_klass_reference_at(checked_exception), 1944 "Exception name has bad type at constant pool %u in class file %s", 1945 checked_exception, CHECK_NULL); 1946 } 1947 } 1948 // check exceptions attribute length 1949 if (_need_verify) { 1950 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) + 1951 sizeof(u2) * size), 1952 "Exceptions attribute has wrong length in class file %s", CHECK_NULL); 1953 } 1954 return checked_exceptions_start; 1955 } 1956 1957 void ClassFileParser::throwIllegalSignature(const char* type, 1958 const Symbol* name, 1959 const Symbol* sig, 1960 TRAPS) const { 1961 assert(name != NULL, "invariant"); 1962 assert(sig != NULL, "invariant"); 1963 1964 ResourceMark rm(THREAD); 1965 Exceptions::fthrow(THREAD_AND_LOCATION, 1966 vmSymbols::java_lang_ClassFormatError(), 1967 "%s \"%s\" in class %s has illegal signature \"%s\"", type, 1968 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); 1969 } 1970 1971 AnnotationCollector::ID 1972 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data, 1973 const Symbol* name) { 1974 const vmSymbols::SID sid = vmSymbols::find_sid(name); 1975 // Privileged code can use all annotations. Other code silently drops some. 1976 const bool privileged = loader_data->is_the_null_class_loader_data() || 1977 loader_data->is_platform_class_loader_data() || 1978 loader_data->is_anonymous(); 1979 switch (sid) { 1980 case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): { 1981 if (_location != _in_method) break; // only allow for methods 1982 if (!privileged) break; // only allow in privileged code 1983 return _method_CallerSensitive; 1984 } 1985 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): { 1986 if (_location != _in_method) break; // only allow for methods 1987 if (!privileged) break; // only allow in privileged code 1988 return _method_ForceInline; 1989 } 1990 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): { 1991 if (_location != _in_method) break; // only allow for methods 1992 if (!privileged) break; // only allow in privileged code 1993 return _method_DontInline; 1994 } 1995 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): { 1996 if (_location != _in_method) break; // only allow for methods 1997 if (!privileged) break; // only allow in privileged code 1998 return _method_InjectedProfile; 1999 } 2000 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): { 2001 if (_location != _in_method) break; // only allow for methods 2002 if (!privileged) break; // only allow in privileged code 2003 return _method_LambdaForm_Compiled; 2004 } 2005 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Hidden_signature): { 2006 if (_location != _in_method) break; // only allow for methods 2007 if (!privileged) break; // only allow in privileged code 2008 return _method_LambdaForm_Hidden; 2009 } 2010 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_HotSpotIntrinsicCandidate_signature): { 2011 if (_location != _in_method) break; // only allow for methods 2012 if (!privileged) break; // only allow in privileged code 2013 return _method_HotSpotIntrinsicCandidate; 2014 } 2015 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Stable_signature): { 2016 if (_location != _in_field) break; // only allow for fields 2017 if (!privileged) break; // only allow in privileged code 2018 return _field_Stable; 2019 } 2020 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): { 2021 if (_location != _in_field && _location != _in_class) { 2022 break; // only allow for fields and classes 2023 } 2024 if (!EnableContended || (RestrictContended && !privileged)) { 2025 break; // honor privileges 2026 } 2027 return _jdk_internal_vm_annotation_Contended; 2028 } 2029 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): { 2030 if (_location != _in_method) break; // only allow for methods 2031 if (RestrictReservedStack && !privileged) break; // honor privileges 2032 return _jdk_internal_vm_annotation_ReservedStackAccess; 2033 } 2034 default: { 2035 break; 2036 } 2037 } 2038 return AnnotationCollector::_unknown; 2039 } 2040 2041 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { 2042 if (is_contended()) 2043 f->set_contended_group(contended_group()); 2044 if (is_stable()) 2045 f->set_stable(true); 2046 } 2047 2048 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() { 2049 // If there's an error deallocate metadata for field annotations 2050 MetadataFactory::free_array<u1>(_loader_data, _field_annotations); 2051 MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations); 2052 } 2053 2054 void MethodAnnotationCollector::apply_to(methodHandle m) { 2055 if (has_annotation(_method_CallerSensitive)) 2056 m->set_caller_sensitive(true); 2057 if (has_annotation(_method_ForceInline)) 2058 m->set_force_inline(true); 2059 if (has_annotation(_method_DontInline)) 2060 m->set_dont_inline(true); 2061 if (has_annotation(_method_InjectedProfile)) 2062 m->set_has_injected_profile(true); 2063 if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none) 2064 m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm); 2065 if (has_annotation(_method_LambdaForm_Hidden)) 2066 m->set_hidden(true); 2067 if (has_annotation(_method_HotSpotIntrinsicCandidate) && !m->is_synthetic()) 2068 m->set_intrinsic_candidate(true); 2069 if (has_annotation(_jdk_internal_vm_annotation_ReservedStackAccess)) 2070 m->set_has_reserved_stack_access(true); 2071 } 2072 2073 void ClassFileParser::ClassAnnotationCollector::apply_to(InstanceKlass* ik) { 2074 assert(ik != NULL, "invariant"); 2075 ik->set_is_contended(is_contended()); 2076 } 2077 2078 #define MAX_ARGS_SIZE 255 2079 #define MAX_CODE_SIZE 65535 2080 #define INITIAL_MAX_LVT_NUMBER 256 2081 2082 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2083 * 2084 * Rules for LVT's and LVTT's are: 2085 * - There can be any number of LVT's and LVTT's. 2086 * - If there are n LVT's, it is the same as if there was just 2087 * one LVT containing all the entries from the n LVT's. 2088 * - There may be no more than one LVT entry per local variable. 2089 * Two LVT entries are 'equal' if these fields are the same: 2090 * start_pc, length, name, slot 2091 * - There may be no more than one LVTT entry per each LVT entry. 2092 * Each LVTT entry has to match some LVT entry. 2093 * - HotSpot internal LVT keeps natural ordering of class file LVT entries. 2094 */ 2095 void ClassFileParser::copy_localvariable_table(const ConstMethod* cm, 2096 int lvt_cnt, 2097 u2* const localvariable_table_length, 2098 const u2**const localvariable_table_start, 2099 int lvtt_cnt, 2100 u2* const localvariable_type_table_length, 2101 const u2**const localvariable_type_table_start, 2102 TRAPS) { 2103 2104 ResourceMark rm(THREAD); 2105 2106 typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*, 2107 &LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable; 2108 2109 LVT_HashTable* const table = new LVT_HashTable(); 2110 2111 // To fill LocalVariableTable in 2112 const Classfile_LVT_Element* cf_lvt; 2113 LocalVariableTableElement* lvt = cm->localvariable_table_start(); 2114 2115 for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { 2116 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; 2117 for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { 2118 copy_lvt_element(&cf_lvt[idx], lvt); 2119 // If no duplicates, add LVT elem in hashtable. 2120 if (table->put(*lvt, lvt) == false 2121 && _need_verify 2122 && _major_version >= JAVA_1_5_VERSION) { 2123 classfile_parse_error("Duplicated LocalVariableTable attribute " 2124 "entry for '%s' in class file %s", 2125 _cp->symbol_at(lvt->name_cp_index)->as_utf8(), 2126 CHECK); 2127 } 2128 } 2129 } 2130 2131 // To merge LocalVariableTable and LocalVariableTypeTable 2132 const Classfile_LVT_Element* cf_lvtt; 2133 LocalVariableTableElement lvtt_elem; 2134 2135 for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { 2136 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; 2137 for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { 2138 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); 2139 LocalVariableTableElement** entry = table->get(lvtt_elem); 2140 if (entry == NULL) { 2141 if (_need_verify) { 2142 classfile_parse_error("LVTT entry for '%s' in class file %s " 2143 "does not match any LVT entry", 2144 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2145 CHECK); 2146 } 2147 } else if ((*entry)->signature_cp_index != 0 && _need_verify) { 2148 classfile_parse_error("Duplicated LocalVariableTypeTable attribute " 2149 "entry for '%s' in class file %s", 2150 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2151 CHECK); 2152 } else { 2153 // to add generic signatures into LocalVariableTable 2154 (*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index; 2155 } 2156 } 2157 } 2158 } 2159 2160 2161 void ClassFileParser::copy_method_annotations(ConstMethod* cm, 2162 const u1* runtime_visible_annotations, 2163 int runtime_visible_annotations_length, 2164 const u1* runtime_invisible_annotations, 2165 int runtime_invisible_annotations_length, 2166 const u1* runtime_visible_parameter_annotations, 2167 int runtime_visible_parameter_annotations_length, 2168 const u1* runtime_invisible_parameter_annotations, 2169 int runtime_invisible_parameter_annotations_length, 2170 const u1* runtime_visible_type_annotations, 2171 int runtime_visible_type_annotations_length, 2172 const u1* runtime_invisible_type_annotations, 2173 int runtime_invisible_type_annotations_length, 2174 const u1* annotation_default, 2175 int annotation_default_length, 2176 TRAPS) { 2177 2178 AnnotationArray* a; 2179 2180 if (runtime_visible_annotations_length + 2181 runtime_invisible_annotations_length > 0) { 2182 a = assemble_annotations(runtime_visible_annotations, 2183 runtime_visible_annotations_length, 2184 runtime_invisible_annotations, 2185 runtime_invisible_annotations_length, 2186 CHECK); 2187 cm->set_method_annotations(a); 2188 } 2189 2190 if (runtime_visible_parameter_annotations_length + 2191 runtime_invisible_parameter_annotations_length > 0) { 2192 a = assemble_annotations(runtime_visible_parameter_annotations, 2193 runtime_visible_parameter_annotations_length, 2194 runtime_invisible_parameter_annotations, 2195 runtime_invisible_parameter_annotations_length, 2196 CHECK); 2197 cm->set_parameter_annotations(a); 2198 } 2199 2200 if (annotation_default_length > 0) { 2201 a = assemble_annotations(annotation_default, 2202 annotation_default_length, 2203 NULL, 2204 0, 2205 CHECK); 2206 cm->set_default_annotations(a); 2207 } 2208 2209 if (runtime_visible_type_annotations_length + 2210 runtime_invisible_type_annotations_length > 0) { 2211 a = assemble_annotations(runtime_visible_type_annotations, 2212 runtime_visible_type_annotations_length, 2213 runtime_invisible_type_annotations, 2214 runtime_invisible_type_annotations_length, 2215 CHECK); 2216 cm->set_type_annotations(a); 2217 } 2218 } 2219 2220 2221 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions 2222 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the 2223 // Method* to save footprint, so we only know the size of the resulting Method* when the 2224 // entire method attribute is parsed. 2225 // 2226 // The promoted_flags parameter is used to pass relevant access_flags 2227 // from the method back up to the containing klass. These flag values 2228 // are added to klass's access_flags. 2229 2230 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs, 2231 bool is_interface, 2232 const ConstantPool* cp, 2233 AccessFlags* const promoted_flags, 2234 TRAPS) { 2235 assert(cfs != NULL, "invariant"); 2236 assert(cp != NULL, "invariant"); 2237 assert(promoted_flags != NULL, "invariant"); 2238 2239 ResourceMark rm(THREAD); 2240 // Parse fixed parts: 2241 // access_flags, name_index, descriptor_index, attributes_count 2242 cfs->guarantee_more(8, CHECK_NULL); 2243 2244 int flags = cfs->get_u2_fast(); 2245 const u2 name_index = cfs->get_u2_fast(); 2246 const int cp_size = cp->length(); 2247 check_property( 2248 valid_symbol_at(name_index), 2249 "Illegal constant pool index %u for method name in class file %s", 2250 name_index, CHECK_NULL); 2251 const Symbol* const name = cp->symbol_at(name_index); 2252 verify_legal_method_name(name, CHECK_NULL); 2253 2254 const u2 signature_index = cfs->get_u2_fast(); 2255 guarantee_property( 2256 valid_symbol_at(signature_index), 2257 "Illegal constant pool index %u for method signature in class file %s", 2258 signature_index, CHECK_NULL); 2259 const Symbol* const signature = cp->symbol_at(signature_index); 2260 2261 if (name == vmSymbols::class_initializer_name()) { 2262 // We ignore the other access flags for a valid class initializer. 2263 // (JVM Spec 2nd ed., chapter 4.6) 2264 if (_major_version < 51) { // backward compatibility 2265 flags = JVM_ACC_STATIC; 2266 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) { 2267 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT; 2268 } else { 2269 classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL); 2270 } 2271 } else { 2272 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL); 2273 } 2274 2275 if (name == vmSymbols::object_initializer_name() && is_interface) { 2276 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL); 2277 } 2278 2279 int args_size = -1; // only used when _need_verify is true 2280 if (_need_verify) { 2281 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) + 2282 verify_legal_method_signature(name, signature, CHECK_NULL); 2283 if (args_size > MAX_ARGS_SIZE) { 2284 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL); 2285 } 2286 } 2287 2288 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS); 2289 2290 // Default values for code and exceptions attribute elements 2291 u2 max_stack = 0; 2292 u2 max_locals = 0; 2293 u4 code_length = 0; 2294 const u1* code_start = 0; 2295 u2 exception_table_length = 0; 2296 const u2* exception_table_start = NULL; 2297 Array<int>* exception_handlers = Universe::the_empty_int_array(); 2298 u2 checked_exceptions_length = 0; 2299 const u2* checked_exceptions_start = NULL; 2300 CompressedLineNumberWriteStream* linenumber_table = NULL; 2301 int linenumber_table_length = 0; 2302 int total_lvt_length = 0; 2303 u2 lvt_cnt = 0; 2304 u2 lvtt_cnt = 0; 2305 bool lvt_allocated = false; 2306 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER; 2307 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER; 2308 u2* localvariable_table_length = NULL; 2309 const u2** localvariable_table_start = NULL; 2310 u2* localvariable_type_table_length = NULL; 2311 const u2** localvariable_type_table_start = NULL; 2312 int method_parameters_length = -1; 2313 const u1* method_parameters_data = NULL; 2314 bool method_parameters_seen = false; 2315 bool parsed_code_attribute = false; 2316 bool parsed_checked_exceptions_attribute = false; 2317 bool parsed_stackmap_attribute = false; 2318 // stackmap attribute - JDK1.5 2319 const u1* stackmap_data = NULL; 2320 int stackmap_data_length = 0; 2321 u2 generic_signature_index = 0; 2322 MethodAnnotationCollector parsed_annotations; 2323 const u1* runtime_visible_annotations = NULL; 2324 int runtime_visible_annotations_length = 0; 2325 const u1* runtime_invisible_annotations = NULL; 2326 int runtime_invisible_annotations_length = 0; 2327 const u1* runtime_visible_parameter_annotations = NULL; 2328 int runtime_visible_parameter_annotations_length = 0; 2329 const u1* runtime_invisible_parameter_annotations = NULL; 2330 int runtime_invisible_parameter_annotations_length = 0; 2331 const u1* runtime_visible_type_annotations = NULL; 2332 int runtime_visible_type_annotations_length = 0; 2333 const u1* runtime_invisible_type_annotations = NULL; 2334 int runtime_invisible_type_annotations_length = 0; 2335 bool runtime_invisible_annotations_exists = false; 2336 bool runtime_invisible_type_annotations_exists = false; 2337 bool runtime_invisible_parameter_annotations_exists = false; 2338 const u1* annotation_default = NULL; 2339 int annotation_default_length = 0; 2340 2341 // Parse code and exceptions attribute 2342 u2 method_attributes_count = cfs->get_u2_fast(); 2343 while (method_attributes_count--) { 2344 cfs->guarantee_more(6, CHECK_NULL); // method_attribute_name_index, method_attribute_length 2345 const u2 method_attribute_name_index = cfs->get_u2_fast(); 2346 const u4 method_attribute_length = cfs->get_u4_fast(); 2347 check_property( 2348 valid_symbol_at(method_attribute_name_index), 2349 "Invalid method attribute name index %u in class file %s", 2350 method_attribute_name_index, CHECK_NULL); 2351 2352 const Symbol* const method_attribute_name = cp->symbol_at(method_attribute_name_index); 2353 if (method_attribute_name == vmSymbols::tag_code()) { 2354 // Parse Code attribute 2355 if (_need_verify) { 2356 guarantee_property( 2357 !access_flags.is_native() && !access_flags.is_abstract(), 2358 "Code attribute in native or abstract methods in class file %s", 2359 CHECK_NULL); 2360 } 2361 if (parsed_code_attribute) { 2362 classfile_parse_error("Multiple Code attributes in class file %s", 2363 CHECK_NULL); 2364 } 2365 parsed_code_attribute = true; 2366 2367 // Stack size, locals size, and code size 2368 if (_major_version == 45 && _minor_version <= 2) { 2369 cfs->guarantee_more(4, CHECK_NULL); 2370 max_stack = cfs->get_u1_fast(); 2371 max_locals = cfs->get_u1_fast(); 2372 code_length = cfs->get_u2_fast(); 2373 } else { 2374 cfs->guarantee_more(8, CHECK_NULL); 2375 max_stack = cfs->get_u2_fast(); 2376 max_locals = cfs->get_u2_fast(); 2377 code_length = cfs->get_u4_fast(); 2378 } 2379 if (_need_verify) { 2380 guarantee_property(args_size <= max_locals, 2381 "Arguments can't fit into locals in class file %s", 2382 CHECK_NULL); 2383 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE, 2384 "Invalid method Code length %u in class file %s", 2385 code_length, CHECK_NULL); 2386 } 2387 // Code pointer 2388 code_start = cfs->get_u1_buffer(); 2389 assert(code_start != NULL, "null code start"); 2390 cfs->guarantee_more(code_length, CHECK_NULL); 2391 cfs->skip_u1_fast(code_length); 2392 2393 // Exception handler table 2394 cfs->guarantee_more(2, CHECK_NULL); // exception_table_length 2395 exception_table_length = cfs->get_u2_fast(); 2396 if (exception_table_length > 0) { 2397 exception_table_start = parse_exception_table(cfs, 2398 code_length, 2399 exception_table_length, 2400 CHECK_NULL); 2401 } 2402 2403 // Parse additional attributes in code attribute 2404 cfs->guarantee_more(2, CHECK_NULL); // code_attributes_count 2405 u2 code_attributes_count = cfs->get_u2_fast(); 2406 2407 unsigned int calculated_attribute_length = 0; 2408 2409 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) { 2410 calculated_attribute_length = 2411 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length); 2412 } else { 2413 // max_stack, locals and length are smaller in pre-version 45.2 classes 2414 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2); 2415 } 2416 calculated_attribute_length += 2417 code_length + 2418 sizeof(exception_table_length) + 2419 sizeof(code_attributes_count) + 2420 exception_table_length * 2421 ( sizeof(u2) + // start_pc 2422 sizeof(u2) + // end_pc 2423 sizeof(u2) + // handler_pc 2424 sizeof(u2) ); // catch_type_index 2425 2426 while (code_attributes_count--) { 2427 cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length 2428 const u2 code_attribute_name_index = cfs->get_u2_fast(); 2429 const u4 code_attribute_length = cfs->get_u4_fast(); 2430 calculated_attribute_length += code_attribute_length + 2431 sizeof(code_attribute_name_index) + 2432 sizeof(code_attribute_length); 2433 check_property(valid_symbol_at(code_attribute_name_index), 2434 "Invalid code attribute name index %u in class file %s", 2435 code_attribute_name_index, 2436 CHECK_NULL); 2437 if (LoadLineNumberTables && 2438 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { 2439 // Parse and compress line number table 2440 parse_linenumber_table(code_attribute_length, 2441 code_length, 2442 &linenumber_table, 2443 CHECK_NULL); 2444 2445 } else if (LoadLocalVariableTables && 2446 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { 2447 // Parse local variable table 2448 if (!lvt_allocated) { 2449 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2450 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2451 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2452 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2453 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2454 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2455 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2456 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2457 lvt_allocated = true; 2458 } 2459 if (lvt_cnt == max_lvt_cnt) { 2460 max_lvt_cnt <<= 1; 2461 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt); 2462 localvariable_table_start = REALLOC_RESOURCE_ARRAY(const u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt); 2463 } 2464 localvariable_table_start[lvt_cnt] = 2465 parse_localvariable_table(cfs, 2466 code_length, 2467 max_locals, 2468 code_attribute_length, 2469 &localvariable_table_length[lvt_cnt], 2470 false, // is not LVTT 2471 CHECK_NULL); 2472 total_lvt_length += localvariable_table_length[lvt_cnt]; 2473 lvt_cnt++; 2474 } else if (LoadLocalVariableTypeTables && 2475 _major_version >= JAVA_1_5_VERSION && 2476 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { 2477 if (!lvt_allocated) { 2478 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2479 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2480 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2481 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2482 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2483 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2484 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2485 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2486 lvt_allocated = true; 2487 } 2488 // Parse local variable type table 2489 if (lvtt_cnt == max_lvtt_cnt) { 2490 max_lvtt_cnt <<= 1; 2491 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt); 2492 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(const u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt); 2493 } 2494 localvariable_type_table_start[lvtt_cnt] = 2495 parse_localvariable_table(cfs, 2496 code_length, 2497 max_locals, 2498 code_attribute_length, 2499 &localvariable_type_table_length[lvtt_cnt], 2500 true, // is LVTT 2501 CHECK_NULL); 2502 lvtt_cnt++; 2503 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && 2504 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { 2505 // Stack map is only needed by the new verifier in JDK1.5. 2506 if (parsed_stackmap_attribute) { 2507 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_NULL); 2508 } 2509 stackmap_data = parse_stackmap_table(cfs, code_attribute_length, _need_verify, CHECK_NULL); 2510 stackmap_data_length = code_attribute_length; 2511 parsed_stackmap_attribute = true; 2512 } else { 2513 // Skip unknown attributes 2514 cfs->skip_u1(code_attribute_length, CHECK_NULL); 2515 } 2516 } 2517 // check method attribute length 2518 if (_need_verify) { 2519 guarantee_property(method_attribute_length == calculated_attribute_length, 2520 "Code segment has wrong length in class file %s", 2521 CHECK_NULL); 2522 } 2523 } else if (method_attribute_name == vmSymbols::tag_exceptions()) { 2524 // Parse Exceptions attribute 2525 if (parsed_checked_exceptions_attribute) { 2526 classfile_parse_error("Multiple Exceptions attributes in class file %s", 2527 CHECK_NULL); 2528 } 2529 parsed_checked_exceptions_attribute = true; 2530 checked_exceptions_start = 2531 parse_checked_exceptions(cfs, 2532 &checked_exceptions_length, 2533 method_attribute_length, 2534 CHECK_NULL); 2535 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { 2536 // reject multiple method parameters 2537 if (method_parameters_seen) { 2538 classfile_parse_error("Multiple MethodParameters attributes in class file %s", 2539 CHECK_NULL); 2540 } 2541 method_parameters_seen = true; 2542 method_parameters_length = cfs->get_u1_fast(); 2543 const u2 real_length = (method_parameters_length * 4u) + 1u; 2544 if (method_attribute_length != real_length) { 2545 classfile_parse_error( 2546 "Invalid MethodParameters method attribute length %u in class file", 2547 method_attribute_length, CHECK_NULL); 2548 } 2549 method_parameters_data = cfs->get_u1_buffer(); 2550 cfs->skip_u2_fast(method_parameters_length); 2551 cfs->skip_u2_fast(method_parameters_length); 2552 // ignore this attribute if it cannot be reflected 2553 if (!SystemDictionary::Parameter_klass_loaded()) 2554 method_parameters_length = -1; 2555 } else if (method_attribute_name == vmSymbols::tag_synthetic()) { 2556 if (method_attribute_length != 0) { 2557 classfile_parse_error( 2558 "Invalid Synthetic method attribute length %u in class file %s", 2559 method_attribute_length, CHECK_NULL); 2560 } 2561 // Should we check that there hasn't already been a synthetic attribute? 2562 access_flags.set_is_synthetic(); 2563 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120 2564 if (method_attribute_length != 0) { 2565 classfile_parse_error( 2566 "Invalid Deprecated method attribute length %u in class file %s", 2567 method_attribute_length, CHECK_NULL); 2568 } 2569 } else if (_major_version >= JAVA_1_5_VERSION) { 2570 if (method_attribute_name == vmSymbols::tag_signature()) { 2571 if (method_attribute_length != 2) { 2572 classfile_parse_error( 2573 "Invalid Signature attribute length %u in class file %s", 2574 method_attribute_length, CHECK_NULL); 2575 } 2576 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL); 2577 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2578 if (runtime_visible_annotations != NULL) { 2579 classfile_parse_error( 2580 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", 2581 CHECK_NULL); 2582 } 2583 runtime_visible_annotations_length = method_attribute_length; 2584 runtime_visible_annotations = cfs->get_u1_buffer(); 2585 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2586 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL); 2587 parse_annotations(cp, 2588 runtime_visible_annotations, 2589 runtime_visible_annotations_length, 2590 &parsed_annotations, 2591 _loader_data, 2592 CHECK_NULL); 2593 cfs->skip_u1_fast(runtime_visible_annotations_length); 2594 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2595 if (runtime_invisible_annotations_exists) { 2596 classfile_parse_error( 2597 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", 2598 CHECK_NULL); 2599 } 2600 runtime_invisible_annotations_exists = true; 2601 if (PreserveAllAnnotations) { 2602 runtime_invisible_annotations_length = method_attribute_length; 2603 runtime_invisible_annotations = cfs->get_u1_buffer(); 2604 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 2605 } 2606 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2607 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) { 2608 if (runtime_visible_parameter_annotations != NULL) { 2609 classfile_parse_error( 2610 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", 2611 CHECK_NULL); 2612 } 2613 runtime_visible_parameter_annotations_length = method_attribute_length; 2614 runtime_visible_parameter_annotations = cfs->get_u1_buffer(); 2615 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations"); 2616 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_NULL); 2617 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) { 2618 if (runtime_invisible_parameter_annotations_exists) { 2619 classfile_parse_error( 2620 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", 2621 CHECK_NULL); 2622 } 2623 runtime_invisible_parameter_annotations_exists = true; 2624 if (PreserveAllAnnotations) { 2625 runtime_invisible_parameter_annotations_length = method_attribute_length; 2626 runtime_invisible_parameter_annotations = cfs->get_u1_buffer(); 2627 assert(runtime_invisible_parameter_annotations != NULL, 2628 "null invisible parameter annotations"); 2629 } 2630 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2631 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) { 2632 if (annotation_default != NULL) { 2633 classfile_parse_error( 2634 "Multiple AnnotationDefault attributes for method in class file %s", 2635 CHECK_NULL); 2636 } 2637 annotation_default_length = method_attribute_length; 2638 annotation_default = cfs->get_u1_buffer(); 2639 assert(annotation_default != NULL, "null annotation default"); 2640 cfs->skip_u1(annotation_default_length, CHECK_NULL); 2641 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 2642 if (runtime_visible_type_annotations != NULL) { 2643 classfile_parse_error( 2644 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s", 2645 CHECK_NULL); 2646 } 2647 runtime_visible_type_annotations_length = method_attribute_length; 2648 runtime_visible_type_annotations = cfs->get_u1_buffer(); 2649 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 2650 // No need for the VM to parse Type annotations 2651 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_NULL); 2652 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 2653 if (runtime_invisible_type_annotations_exists) { 2654 classfile_parse_error( 2655 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s", 2656 CHECK_NULL); 2657 } else { 2658 runtime_invisible_type_annotations_exists = true; 2659 } 2660 if (PreserveAllAnnotations) { 2661 runtime_invisible_type_annotations_length = method_attribute_length; 2662 runtime_invisible_type_annotations = cfs->get_u1_buffer(); 2663 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 2664 } 2665 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2666 } else { 2667 // Skip unknown attributes 2668 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2669 } 2670 } else { 2671 // Skip unknown attributes 2672 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2673 } 2674 } 2675 2676 if (linenumber_table != NULL) { 2677 linenumber_table->write_terminator(); 2678 linenumber_table_length = linenumber_table->position(); 2679 } 2680 2681 // Make sure there's at least one Code attribute in non-native/non-abstract method 2682 if (_need_verify) { 2683 guarantee_property(access_flags.is_native() || 2684 access_flags.is_abstract() || 2685 parsed_code_attribute, 2686 "Absent Code attribute in method that is not native or abstract in class file %s", 2687 CHECK_NULL); 2688 } 2689 2690 // All sizing information for a Method* is finally available, now create it 2691 InlineTableSizes sizes( 2692 total_lvt_length, 2693 linenumber_table_length, 2694 exception_table_length, 2695 checked_exceptions_length, 2696 method_parameters_length, 2697 generic_signature_index, 2698 runtime_visible_annotations_length + 2699 runtime_invisible_annotations_length, 2700 runtime_visible_parameter_annotations_length + 2701 runtime_invisible_parameter_annotations_length, 2702 runtime_visible_type_annotations_length + 2703 runtime_invisible_type_annotations_length, 2704 annotation_default_length, 2705 0); 2706 2707 Method* const m = Method::allocate(_loader_data, 2708 code_length, 2709 access_flags, 2710 &sizes, 2711 ConstMethod::NORMAL, 2712 CHECK_NULL); 2713 2714 ClassLoadingService::add_class_method_size(m->size()*wordSize); 2715 2716 // Fill in information from fixed part (access_flags already set) 2717 m->set_constants(_cp); 2718 m->set_name_index(name_index); 2719 m->set_signature_index(signature_index); 2720 2721 ResultTypeFinder rtf(cp->symbol_at(signature_index)); 2722 m->constMethod()->set_result_type(rtf.type()); 2723 2724 if (args_size >= 0) { 2725 m->set_size_of_parameters(args_size); 2726 } else { 2727 m->compute_size_of_parameters(THREAD); 2728 } 2729 #ifdef ASSERT 2730 if (args_size >= 0) { 2731 m->compute_size_of_parameters(THREAD); 2732 assert(args_size == m->size_of_parameters(), ""); 2733 } 2734 #endif 2735 2736 // Fill in code attribute information 2737 m->set_max_stack(max_stack); 2738 m->set_max_locals(max_locals); 2739 if (stackmap_data != NULL) { 2740 m->constMethod()->copy_stackmap_data(_loader_data, 2741 (u1*)stackmap_data, 2742 stackmap_data_length, 2743 CHECK_NULL); 2744 } 2745 2746 // Copy byte codes 2747 m->set_code((u1*)code_start); 2748 2749 // Copy line number table 2750 if (linenumber_table != NULL) { 2751 memcpy(m->compressed_linenumber_table(), 2752 linenumber_table->buffer(), 2753 linenumber_table_length); 2754 } 2755 2756 // Copy exception table 2757 if (exception_table_length > 0) { 2758 int size = 2759 exception_table_length * sizeof(ExceptionTableElement) / sizeof(u2); 2760 copy_u2_with_conversion((u2*) m->exception_table_start(), 2761 exception_table_start, size); 2762 } 2763 2764 // Copy method parameters 2765 if (method_parameters_length > 0) { 2766 MethodParametersElement* elem = m->constMethod()->method_parameters_start(); 2767 for (int i = 0; i < method_parameters_length; i++) { 2768 elem[i].name_cp_index = Bytes::get_Java_u2((address)method_parameters_data); 2769 method_parameters_data += 2; 2770 elem[i].flags = Bytes::get_Java_u2((address)method_parameters_data); 2771 method_parameters_data += 2; 2772 } 2773 } 2774 2775 // Copy checked exceptions 2776 if (checked_exceptions_length > 0) { 2777 const int size = 2778 checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2); 2779 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), 2780 checked_exceptions_start, 2781 size); 2782 } 2783 2784 // Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2785 if (total_lvt_length > 0) { 2786 promoted_flags->set_has_localvariable_table(); 2787 copy_localvariable_table(m->constMethod(), 2788 lvt_cnt, 2789 localvariable_table_length, 2790 localvariable_table_start, 2791 lvtt_cnt, 2792 localvariable_type_table_length, 2793 localvariable_type_table_start, 2794 CHECK_NULL); 2795 } 2796 2797 if (parsed_annotations.has_any_annotations()) 2798 parsed_annotations.apply_to(m); 2799 2800 // Copy annotations 2801 copy_method_annotations(m->constMethod(), 2802 runtime_visible_annotations, 2803 runtime_visible_annotations_length, 2804 runtime_invisible_annotations, 2805 runtime_invisible_annotations_length, 2806 runtime_visible_parameter_annotations, 2807 runtime_visible_parameter_annotations_length, 2808 runtime_invisible_parameter_annotations, 2809 runtime_invisible_parameter_annotations_length, 2810 runtime_visible_type_annotations, 2811 runtime_visible_type_annotations_length, 2812 runtime_invisible_type_annotations, 2813 runtime_invisible_type_annotations_length, 2814 annotation_default, 2815 annotation_default_length, 2816 CHECK_NULL); 2817 2818 if (name == vmSymbols::finalize_method_name() && 2819 signature == vmSymbols::void_method_signature()) { 2820 if (m->is_empty_method()) { 2821 _has_empty_finalizer = true; 2822 } else { 2823 _has_finalizer = true; 2824 } 2825 } 2826 if (name == vmSymbols::object_initializer_name() && 2827 signature == vmSymbols::void_method_signature() && 2828 m->is_vanilla_constructor()) { 2829 _has_vanilla_constructor = true; 2830 } 2831 2832 NOT_PRODUCT(m->verify()); 2833 return m; 2834 } 2835 2836 2837 // The promoted_flags parameter is used to pass relevant access_flags 2838 // from the methods back up to the containing klass. These flag values 2839 // are added to klass's access_flags. 2840 // Side-effects: populates the _methods field in the parser 2841 void ClassFileParser::parse_methods(const ClassFileStream* const cfs, 2842 bool is_interface, 2843 AccessFlags* promoted_flags, 2844 bool* has_final_method, 2845 bool* declares_nonstatic_concrete_methods, 2846 TRAPS) { 2847 assert(cfs != NULL, "invariant"); 2848 assert(promoted_flags != NULL, "invariant"); 2849 assert(has_final_method != NULL, "invariant"); 2850 assert(declares_nonstatic_concrete_methods != NULL, "invariant"); 2851 2852 assert(NULL == _methods, "invariant"); 2853 2854 cfs->guarantee_more(2, CHECK); // length 2855 const u2 length = cfs->get_u2_fast(); 2856 if (length == 0) { 2857 _methods = Universe::the_empty_method_array(); 2858 } else { 2859 _methods = MetadataFactory::new_array<Method*>(_loader_data, 2860 length, 2861 NULL, 2862 CHECK); 2863 2864 for (int index = 0; index < length; index++) { 2865 Method* method = parse_method(cfs, 2866 is_interface, 2867 _cp, 2868 promoted_flags, 2869 CHECK); 2870 2871 if (method->is_final()) { 2872 *has_final_method = true; 2873 } 2874 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags 2875 // used for interface initialization, and default method inheritance analysis 2876 if (is_interface && !(*declares_nonstatic_concrete_methods) 2877 && !method->is_abstract() && !method->is_static()) { 2878 *declares_nonstatic_concrete_methods = true; 2879 } 2880 _methods->at_put(index, method); 2881 } 2882 2883 if (_need_verify && length > 1) { 2884 // Check duplicated methods 2885 ResourceMark rm(THREAD); 2886 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 2887 THREAD, NameSigHash*, HASH_ROW_SIZE); 2888 initialize_hashtable(names_and_sigs); 2889 bool dup = false; 2890 const Symbol* name = NULL; 2891 const Symbol* sig = NULL; 2892 { 2893 debug_only(NoSafepointVerifier nsv;) 2894 for (int i = 0; i < length; i++) { 2895 const Method* const m = _methods->at(i); 2896 name = m->name(); 2897 sig = m->signature(); 2898 // If no duplicates, add name/signature in hashtable names_and_sigs. 2899 if (!put_after_lookup(name, sig, names_and_sigs)) { 2900 dup = true; 2901 break; 2902 } 2903 } 2904 } 2905 if (dup) { 2906 classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s", 2907 name->as_C_string(), sig->as_klass_external_name(), CHECK); 2908 } 2909 } 2910 } 2911 } 2912 2913 static const intArray* sort_methods(Array<Method*>* methods) { 2914 const int length = methods->length(); 2915 // If JVMTI original method ordering or sharing is enabled we have to 2916 // remember the original class file ordering. 2917 // We temporarily use the vtable_index field in the Method* to store the 2918 // class file index, so we can read in after calling qsort. 2919 // Put the method ordering in the shared archive. 2920 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 2921 for (int index = 0; index < length; index++) { 2922 Method* const m = methods->at(index); 2923 assert(!m->valid_vtable_index(), "vtable index should not be set"); 2924 m->set_vtable_index(index); 2925 } 2926 } 2927 // Sort method array by ascending method name (for faster lookups & vtable construction) 2928 // Note that the ordering is not alphabetical, see Symbol::fast_compare 2929 Method::sort_methods(methods); 2930 2931 intArray* method_ordering = NULL; 2932 // If JVMTI original method ordering or sharing is enabled construct int 2933 // array remembering the original ordering 2934 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 2935 method_ordering = new intArray(length, length, -1); 2936 for (int index = 0; index < length; index++) { 2937 Method* const m = methods->at(index); 2938 const int old_index = m->vtable_index(); 2939 assert(old_index >= 0 && old_index < length, "invalid method index"); 2940 method_ordering->at_put(index, old_index); 2941 m->set_vtable_index(Method::invalid_vtable_index); 2942 } 2943 } 2944 return method_ordering; 2945 } 2946 2947 // Parse generic_signature attribute for methods and fields 2948 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs, 2949 TRAPS) { 2950 assert(cfs != NULL, "invariant"); 2951 2952 cfs->guarantee_more(2, CHECK_0); // generic_signature_index 2953 const u2 generic_signature_index = cfs->get_u2_fast(); 2954 check_property( 2955 valid_symbol_at(generic_signature_index), 2956 "Invalid Signature attribute at constant pool index %u in class file %s", 2957 generic_signature_index, CHECK_0); 2958 return generic_signature_index; 2959 } 2960 2961 void ClassFileParser::parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, 2962 TRAPS) { 2963 2964 assert(cfs != NULL, "invariant"); 2965 2966 cfs->guarantee_more(2, CHECK); // sourcefile_index 2967 const u2 sourcefile_index = cfs->get_u2_fast(); 2968 check_property( 2969 valid_symbol_at(sourcefile_index), 2970 "Invalid SourceFile attribute at constant pool index %u in class file %s", 2971 sourcefile_index, CHECK); 2972 set_class_sourcefile_index(sourcefile_index); 2973 } 2974 2975 void ClassFileParser::parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 2976 int length, 2977 TRAPS) { 2978 assert(cfs != NULL, "invariant"); 2979 2980 const u1* const sde_buffer = cfs->get_u1_buffer(); 2981 assert(sde_buffer != NULL, "null sde buffer"); 2982 2983 // Don't bother storing it if there is no way to retrieve it 2984 if (JvmtiExport::can_get_source_debug_extension()) { 2985 assert((length+1) > length, "Overflow checking"); 2986 u1* const sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); 2987 for (int i = 0; i < length; i++) { 2988 sde[i] = sde_buffer[i]; 2989 } 2990 sde[length] = '\0'; 2991 set_class_sde_buffer((const char*)sde, length); 2992 } 2993 // Got utf8 string, set stream position forward 2994 cfs->skip_u1(length, CHECK); 2995 } 2996 2997 2998 // Inner classes can be static, private or protected (classic VM does this) 2999 #define RECOGNIZED_INNER_CLASS_MODIFIERS ( JVM_RECOGNIZED_CLASS_MODIFIERS | \ 3000 JVM_ACC_PRIVATE | \ 3001 JVM_ACC_PROTECTED | \ 3002 JVM_ACC_STATIC \ 3003 ) 3004 3005 // Return number of classes in the inner classes attribute table 3006 u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 3007 const u1* const inner_classes_attribute_start, 3008 bool parsed_enclosingmethod_attribute, 3009 u2 enclosing_method_class_index, 3010 u2 enclosing_method_method_index, 3011 TRAPS) { 3012 const u1* const current_mark = cfs->current(); 3013 u2 length = 0; 3014 if (inner_classes_attribute_start != NULL) { 3015 cfs->set_current(inner_classes_attribute_start); 3016 cfs->guarantee_more(2, CHECK_0); // length 3017 length = cfs->get_u2_fast(); 3018 } 3019 3020 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing 3021 // method data: 3022 // [inner_class_info_index, 3023 // outer_class_info_index, 3024 // inner_name_index, 3025 // inner_class_access_flags, 3026 // ... 3027 // enclosing_method_class_index, 3028 // enclosing_method_method_index] 3029 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); 3030 Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3031 _inner_classes = inner_classes; 3032 3033 int index = 0; 3034 const int cp_size = _cp->length(); 3035 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 3036 for (int n = 0; n < length; n++) { 3037 // Inner class index 3038 const u2 inner_class_info_index = cfs->get_u2_fast(); 3039 check_property( 3040 valid_klass_reference_at(inner_class_info_index), 3041 "inner_class_info_index %u has bad constant type in class file %s", 3042 inner_class_info_index, CHECK_0); 3043 // Outer class index 3044 const u2 outer_class_info_index = cfs->get_u2_fast(); 3045 check_property( 3046 outer_class_info_index == 0 || 3047 valid_klass_reference_at(outer_class_info_index), 3048 "outer_class_info_index %u has bad constant type in class file %s", 3049 outer_class_info_index, CHECK_0); 3050 // Inner class name 3051 const u2 inner_name_index = cfs->get_u2_fast(); 3052 check_property( 3053 inner_name_index == 0 || valid_symbol_at(inner_name_index), 3054 "inner_name_index %u has bad constant type in class file %s", 3055 inner_name_index, CHECK_0); 3056 if (_need_verify) { 3057 guarantee_property(inner_class_info_index != outer_class_info_index, 3058 "Class is both outer and inner class in class file %s", CHECK_0); 3059 } 3060 // Access flags 3061 jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS; 3062 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 3063 // Set abstract bit for old class files for backward compatibility 3064 flags |= JVM_ACC_ABSTRACT; 3065 } 3066 verify_legal_class_modifiers(flags, CHECK_0); 3067 AccessFlags inner_access_flags(flags); 3068 3069 inner_classes->at_put(index++, inner_class_info_index); 3070 inner_classes->at_put(index++, outer_class_info_index); 3071 inner_classes->at_put(index++, inner_name_index); 3072 inner_classes->at_put(index++, inner_access_flags.as_short()); 3073 } 3074 3075 // 4347400: make sure there's no duplicate entry in the classes array 3076 if (_need_verify && _major_version >= JAVA_1_5_VERSION) { 3077 for(int i = 0; i < length * 4; i += 4) { 3078 for(int j = i + 4; j < length * 4; j += 4) { 3079 guarantee_property((inner_classes->at(i) != inner_classes->at(j) || 3080 inner_classes->at(i+1) != inner_classes->at(j+1) || 3081 inner_classes->at(i+2) != inner_classes->at(j+2) || 3082 inner_classes->at(i+3) != inner_classes->at(j+3)), 3083 "Duplicate entry in InnerClasses in class file %s", 3084 CHECK_0); 3085 } 3086 } 3087 } 3088 3089 // Set EnclosingMethod class and method indexes. 3090 if (parsed_enclosingmethod_attribute) { 3091 inner_classes->at_put(index++, enclosing_method_class_index); 3092 inner_classes->at_put(index++, enclosing_method_method_index); 3093 } 3094 assert(index == size, "wrong size"); 3095 3096 // Restore buffer's current position. 3097 cfs->set_current(current_mark); 3098 3099 return length; 3100 } 3101 3102 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) { 3103 set_class_synthetic_flag(true); 3104 } 3105 3106 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) { 3107 assert(cfs != NULL, "invariant"); 3108 3109 const u2 signature_index = cfs->get_u2(CHECK); 3110 check_property( 3111 valid_symbol_at(signature_index), 3112 "Invalid constant pool index %u in Signature attribute in class file %s", 3113 signature_index, CHECK); 3114 set_class_generic_signature_index(signature_index); 3115 } 3116 3117 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 3118 ConstantPool* cp, 3119 u4 attribute_byte_length, 3120 TRAPS) { 3121 assert(cfs != NULL, "invariant"); 3122 assert(cp != NULL, "invariant"); 3123 3124 const u1* const current_start = cfs->current(); 3125 3126 guarantee_property(attribute_byte_length >= sizeof(u2), 3127 "Invalid BootstrapMethods attribute length %u in class file %s", 3128 attribute_byte_length, 3129 CHECK); 3130 3131 cfs->guarantee_more(attribute_byte_length, CHECK); 3132 3133 const int attribute_array_length = cfs->get_u2_fast(); 3134 3135 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length, 3136 "Short length on BootstrapMethods in class file %s", 3137 CHECK); 3138 3139 3140 // The attribute contains a counted array of counted tuples of shorts, 3141 // represending bootstrap specifiers: 3142 // length*{bootstrap_method_index, argument_count*{argument_index}} 3143 const int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2); 3144 // operand_count = number of shorts in attr, except for leading length 3145 3146 // The attribute is copied into a short[] array. 3147 // The array begins with a series of short[2] pairs, one for each tuple. 3148 const int index_size = (attribute_array_length * 2); 3149 3150 Array<u2>* const operands = 3151 MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK); 3152 3153 // Eagerly assign operands so they will be deallocated with the constant 3154 // pool if there is an error. 3155 cp->set_operands(operands); 3156 3157 int operand_fill_index = index_size; 3158 const int cp_size = cp->length(); 3159 3160 for (int n = 0; n < attribute_array_length; n++) { 3161 // Store a 32-bit offset into the header of the operand array. 3162 ConstantPool::operand_offset_at_put(operands, n, operand_fill_index); 3163 3164 // Read a bootstrap specifier. 3165 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc 3166 const u2 bootstrap_method_index = cfs->get_u2_fast(); 3167 const u2 argument_count = cfs->get_u2_fast(); 3168 check_property( 3169 valid_cp_range(bootstrap_method_index, cp_size) && 3170 cp->tag_at(bootstrap_method_index).is_method_handle(), 3171 "bootstrap_method_index %u has bad constant type in class file %s", 3172 bootstrap_method_index, 3173 CHECK); 3174 3175 guarantee_property((operand_fill_index + 1 + argument_count) < operands->length(), 3176 "Invalid BootstrapMethods num_bootstrap_methods or num_bootstrap_arguments value in class file %s", 3177 CHECK); 3178 3179 operands->at_put(operand_fill_index++, bootstrap_method_index); 3180 operands->at_put(operand_fill_index++, argument_count); 3181 3182 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc] 3183 for (int j = 0; j < argument_count; j++) { 3184 const u2 argument_index = cfs->get_u2_fast(); 3185 check_property( 3186 valid_cp_range(argument_index, cp_size) && 3187 cp->tag_at(argument_index).is_loadable_constant(), 3188 "argument_index %u has bad constant type in class file %s", 3189 argument_index, 3190 CHECK); 3191 operands->at_put(operand_fill_index++, argument_index); 3192 } 3193 } 3194 guarantee_property(current_start + attribute_byte_length == cfs->current(), 3195 "Bad length on BootstrapMethods in class file %s", 3196 CHECK); 3197 } 3198 3199 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs, 3200 ConstantPool* cp, 3201 ClassFileParser::ClassAnnotationCollector* parsed_annotations, 3202 TRAPS) { 3203 assert(cfs != NULL, "invariant"); 3204 assert(cp != NULL, "invariant"); 3205 assert(parsed_annotations != NULL, "invariant"); 3206 3207 // Set inner classes attribute to default sentinel 3208 _inner_classes = Universe::the_empty_short_array(); 3209 cfs->guarantee_more(2, CHECK); // attributes_count 3210 u2 attributes_count = cfs->get_u2_fast(); 3211 bool parsed_sourcefile_attribute = false; 3212 bool parsed_innerclasses_attribute = false; 3213 bool parsed_enclosingmethod_attribute = false; 3214 bool parsed_bootstrap_methods_attribute = false; 3215 const u1* runtime_visible_annotations = NULL; 3216 int runtime_visible_annotations_length = 0; 3217 const u1* runtime_invisible_annotations = NULL; 3218 int runtime_invisible_annotations_length = 0; 3219 const u1* runtime_visible_type_annotations = NULL; 3220 int runtime_visible_type_annotations_length = 0; 3221 const u1* runtime_invisible_type_annotations = NULL; 3222 int runtime_invisible_type_annotations_length = 0; 3223 bool runtime_invisible_type_annotations_exists = false; 3224 bool runtime_invisible_annotations_exists = false; 3225 bool parsed_source_debug_ext_annotations_exist = false; 3226 const u1* inner_classes_attribute_start = NULL; 3227 u4 inner_classes_attribute_length = 0; 3228 u2 enclosing_method_class_index = 0; 3229 u2 enclosing_method_method_index = 0; 3230 // Iterate over attributes 3231 while (attributes_count--) { 3232 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 3233 const u2 attribute_name_index = cfs->get_u2_fast(); 3234 const u4 attribute_length = cfs->get_u4_fast(); 3235 check_property( 3236 valid_symbol_at(attribute_name_index), 3237 "Attribute name has bad constant pool index %u in class file %s", 3238 attribute_name_index, CHECK); 3239 const Symbol* const tag = cp->symbol_at(attribute_name_index); 3240 if (tag == vmSymbols::tag_source_file()) { 3241 // Check for SourceFile tag 3242 if (_need_verify) { 3243 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK); 3244 } 3245 if (parsed_sourcefile_attribute) { 3246 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK); 3247 } else { 3248 parsed_sourcefile_attribute = true; 3249 } 3250 parse_classfile_sourcefile_attribute(cfs, CHECK); 3251 } else if (tag == vmSymbols::tag_source_debug_extension()) { 3252 // Check for SourceDebugExtension tag 3253 if (parsed_source_debug_ext_annotations_exist) { 3254 classfile_parse_error( 3255 "Multiple SourceDebugExtension attributes in class file %s", CHECK); 3256 } 3257 parsed_source_debug_ext_annotations_exist = true; 3258 parse_classfile_source_debug_extension_attribute(cfs, (int)attribute_length, CHECK); 3259 } else if (tag == vmSymbols::tag_inner_classes()) { 3260 // Check for InnerClasses tag 3261 if (parsed_innerclasses_attribute) { 3262 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK); 3263 } else { 3264 parsed_innerclasses_attribute = true; 3265 } 3266 inner_classes_attribute_start = cfs->get_u1_buffer(); 3267 inner_classes_attribute_length = attribute_length; 3268 cfs->skip_u1(inner_classes_attribute_length, CHECK); 3269 } else if (tag == vmSymbols::tag_synthetic()) { 3270 // Check for Synthetic tag 3271 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec 3272 if (attribute_length != 0) { 3273 classfile_parse_error( 3274 "Invalid Synthetic classfile attribute length %u in class file %s", 3275 attribute_length, CHECK); 3276 } 3277 parse_classfile_synthetic_attribute(CHECK); 3278 } else if (tag == vmSymbols::tag_deprecated()) { 3279 // Check for Deprecatd tag - 4276120 3280 if (attribute_length != 0) { 3281 classfile_parse_error( 3282 "Invalid Deprecated classfile attribute length %u in class file %s", 3283 attribute_length, CHECK); 3284 } 3285 } else if (_major_version >= JAVA_1_5_VERSION) { 3286 if (tag == vmSymbols::tag_signature()) { 3287 if (attribute_length != 2) { 3288 classfile_parse_error( 3289 "Wrong Signature attribute length %u in class file %s", 3290 attribute_length, CHECK); 3291 } 3292 parse_classfile_signature_attribute(cfs, CHECK); 3293 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { 3294 if (runtime_visible_annotations != NULL) { 3295 classfile_parse_error( 3296 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK); 3297 } 3298 runtime_visible_annotations_length = attribute_length; 3299 runtime_visible_annotations = cfs->get_u1_buffer(); 3300 assert(runtime_visible_annotations != NULL, "null visible annotations"); 3301 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 3302 parse_annotations(cp, 3303 runtime_visible_annotations, 3304 runtime_visible_annotations_length, 3305 parsed_annotations, 3306 _loader_data, 3307 CHECK); 3308 cfs->skip_u1_fast(runtime_visible_annotations_length); 3309 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) { 3310 if (runtime_invisible_annotations_exists) { 3311 classfile_parse_error( 3312 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK); 3313 } 3314 runtime_invisible_annotations_exists = true; 3315 if (PreserveAllAnnotations) { 3316 runtime_invisible_annotations_length = attribute_length; 3317 runtime_invisible_annotations = cfs->get_u1_buffer(); 3318 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 3319 } 3320 cfs->skip_u1(attribute_length, CHECK); 3321 } else if (tag == vmSymbols::tag_enclosing_method()) { 3322 if (parsed_enclosingmethod_attribute) { 3323 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK); 3324 } else { 3325 parsed_enclosingmethod_attribute = true; 3326 } 3327 guarantee_property(attribute_length == 4, 3328 "Wrong EnclosingMethod attribute length %u in class file %s", 3329 attribute_length, CHECK); 3330 cfs->guarantee_more(4, CHECK); // class_index, method_index 3331 enclosing_method_class_index = cfs->get_u2_fast(); 3332 enclosing_method_method_index = cfs->get_u2_fast(); 3333 if (enclosing_method_class_index == 0) { 3334 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK); 3335 } 3336 // Validate the constant pool indices and types 3337 check_property(valid_klass_reference_at(enclosing_method_class_index), 3338 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK); 3339 if (enclosing_method_method_index != 0 && 3340 (!cp->is_within_bounds(enclosing_method_method_index) || 3341 !cp->tag_at(enclosing_method_method_index).is_name_and_type())) { 3342 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK); 3343 } 3344 } else if (tag == vmSymbols::tag_bootstrap_methods() && 3345 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 3346 if (parsed_bootstrap_methods_attribute) { 3347 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK); 3348 } 3349 parsed_bootstrap_methods_attribute = true; 3350 parse_classfile_bootstrap_methods_attribute(cfs, cp, attribute_length, CHECK); 3351 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) { 3352 if (runtime_visible_type_annotations != NULL) { 3353 classfile_parse_error( 3354 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK); 3355 } 3356 runtime_visible_type_annotations_length = attribute_length; 3357 runtime_visible_type_annotations = cfs->get_u1_buffer(); 3358 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 3359 // No need for the VM to parse Type annotations 3360 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 3361 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) { 3362 if (runtime_invisible_type_annotations_exists) { 3363 classfile_parse_error( 3364 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK); 3365 } else { 3366 runtime_invisible_type_annotations_exists = true; 3367 } 3368 if (PreserveAllAnnotations) { 3369 runtime_invisible_type_annotations_length = attribute_length; 3370 runtime_invisible_type_annotations = cfs->get_u1_buffer(); 3371 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 3372 } 3373 cfs->skip_u1(attribute_length, CHECK); 3374 } else { 3375 // Unknown attribute 3376 cfs->skip_u1(attribute_length, CHECK); 3377 } 3378 } else { 3379 // Unknown attribute 3380 cfs->skip_u1(attribute_length, CHECK); 3381 } 3382 } 3383 _annotations = assemble_annotations(runtime_visible_annotations, 3384 runtime_visible_annotations_length, 3385 runtime_invisible_annotations, 3386 runtime_invisible_annotations_length, 3387 CHECK); 3388 _type_annotations = assemble_annotations(runtime_visible_type_annotations, 3389 runtime_visible_type_annotations_length, 3390 runtime_invisible_type_annotations, 3391 runtime_invisible_type_annotations_length, 3392 CHECK); 3393 3394 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 3395 const u2 num_of_classes = parse_classfile_inner_classes_attribute( 3396 cfs, 3397 inner_classes_attribute_start, 3398 parsed_innerclasses_attribute, 3399 enclosing_method_class_index, 3400 enclosing_method_method_index, 3401 CHECK); 3402 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { 3403 guarantee_property( 3404 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 3405 "Wrong InnerClasses attribute length in class file %s", CHECK); 3406 } 3407 } 3408 3409 if (_max_bootstrap_specifier_index >= 0) { 3410 guarantee_property(parsed_bootstrap_methods_attribute, 3411 "Missing BootstrapMethods attribute in class file %s", CHECK); 3412 } 3413 } 3414 3415 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) { 3416 assert(k != NULL, "invariant"); 3417 3418 if (_synthetic_flag) 3419 k->set_is_synthetic(); 3420 if (_sourcefile_index != 0) { 3421 k->set_source_file_name_index(_sourcefile_index); 3422 } 3423 if (_generic_signature_index != 0) { 3424 k->set_generic_signature_index(_generic_signature_index); 3425 } 3426 if (_sde_buffer != NULL) { 3427 k->set_source_debug_extension(_sde_buffer, _sde_length); 3428 } 3429 } 3430 3431 // Create the Annotations object that will 3432 // hold the annotations array for the Klass. 3433 void ClassFileParser::create_combined_annotations(TRAPS) { 3434 if (_annotations == NULL && 3435 _type_annotations == NULL && 3436 _fields_annotations == NULL && 3437 _fields_type_annotations == NULL) { 3438 // Don't create the Annotations object unnecessarily. 3439 return; 3440 } 3441 3442 Annotations* const annotations = Annotations::allocate(_loader_data, CHECK); 3443 annotations->set_class_annotations(_annotations); 3444 annotations->set_class_type_annotations(_type_annotations); 3445 annotations->set_fields_annotations(_fields_annotations); 3446 annotations->set_fields_type_annotations(_fields_type_annotations); 3447 3448 // This is the Annotations object that will be 3449 // assigned to InstanceKlass being constructed. 3450 _combined_annotations = annotations; 3451 3452 // The annotations arrays below has been transfered the 3453 // _combined_annotations so these fields can now be cleared. 3454 _annotations = NULL; 3455 _type_annotations = NULL; 3456 _fields_annotations = NULL; 3457 _fields_type_annotations = NULL; 3458 } 3459 3460 // Transfer ownership of metadata allocated to the InstanceKlass. 3461 void ClassFileParser::apply_parsed_class_metadata( 3462 InstanceKlass* this_klass, 3463 int java_fields_count, TRAPS) { 3464 assert(this_klass != NULL, "invariant"); 3465 3466 _cp->set_pool_holder(this_klass); 3467 this_klass->set_constants(_cp); 3468 this_klass->set_fields(_fields, java_fields_count); 3469 this_klass->set_methods(_methods); 3470 this_klass->set_inner_classes(_inner_classes); 3471 this_klass->set_local_interfaces(_local_interfaces); 3472 this_klass->set_transitive_interfaces(_transitive_interfaces); 3473 this_klass->set_annotations(_combined_annotations); 3474 3475 // Clear out these fields so they don't get deallocated by the destructor 3476 clear_class_metadata(); 3477 } 3478 3479 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations, 3480 int runtime_visible_annotations_length, 3481 const u1* const runtime_invisible_annotations, 3482 int runtime_invisible_annotations_length, 3483 TRAPS) { 3484 AnnotationArray* annotations = NULL; 3485 if (runtime_visible_annotations != NULL || 3486 runtime_invisible_annotations != NULL) { 3487 annotations = MetadataFactory::new_array<u1>(_loader_data, 3488 runtime_visible_annotations_length + 3489 runtime_invisible_annotations_length, 3490 CHECK_(annotations)); 3491 if (runtime_visible_annotations != NULL) { 3492 for (int i = 0; i < runtime_visible_annotations_length; i++) { 3493 annotations->at_put(i, runtime_visible_annotations[i]); 3494 } 3495 } 3496 if (runtime_invisible_annotations != NULL) { 3497 for (int i = 0; i < runtime_invisible_annotations_length; i++) { 3498 int append = runtime_visible_annotations_length+i; 3499 annotations->at_put(append, runtime_invisible_annotations[i]); 3500 } 3501 } 3502 } 3503 return annotations; 3504 } 3505 3506 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp, 3507 const int super_class_index, 3508 const bool need_verify, 3509 TRAPS) { 3510 assert(cp != NULL, "invariant"); 3511 const InstanceKlass* super_klass = NULL; 3512 3513 if (super_class_index == 0) { 3514 check_property(_class_name == vmSymbols::java_lang_Object(), 3515 "Invalid superclass index %u in class file %s", 3516 super_class_index, 3517 CHECK_NULL); 3518 } else { 3519 check_property(valid_klass_reference_at(super_class_index), 3520 "Invalid superclass index %u in class file %s", 3521 super_class_index, 3522 CHECK_NULL); 3523 // The class name should be legal because it is checked when parsing constant pool. 3524 // However, make sure it is not an array type. 3525 bool is_array = false; 3526 if (cp->tag_at(super_class_index).is_klass()) { 3527 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index)); 3528 if (need_verify) 3529 is_array = super_klass->is_array_klass(); 3530 } else if (need_verify) { 3531 is_array = (cp->klass_name_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY); 3532 } 3533 if (need_verify) { 3534 guarantee_property(!is_array, 3535 "Bad superclass name in class file %s", CHECK_NULL); 3536 } 3537 } 3538 return super_klass; 3539 } 3540 3541 static unsigned int compute_oop_map_count(const InstanceKlass* super, 3542 unsigned int nonstatic_oop_map_count, 3543 int first_nonstatic_oop_offset) { 3544 3545 unsigned int map_count = 3546 NULL == super ? 0 : super->nonstatic_oop_map_count(); 3547 if (nonstatic_oop_map_count > 0) { 3548 // We have oops to add to map 3549 if (map_count == 0) { 3550 map_count = nonstatic_oop_map_count; 3551 } 3552 else { 3553 // Check whether we should add a new map block or whether the last one can 3554 // be extended 3555 const OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps(); 3556 const OopMapBlock* const last_map = first_map + map_count - 1; 3557 3558 const int next_offset = last_map->offset() + last_map->count() * heapOopSize; 3559 if (next_offset == first_nonstatic_oop_offset) { 3560 // There is no gap bettwen superklass's last oop field and first 3561 // local oop field, merge maps. 3562 nonstatic_oop_map_count -= 1; 3563 } 3564 else { 3565 // Superklass didn't end with a oop field, add extra maps 3566 assert(next_offset < first_nonstatic_oop_offset, "just checking"); 3567 } 3568 map_count += nonstatic_oop_map_count; 3569 } 3570 } 3571 return map_count; 3572 } 3573 3574 #ifndef PRODUCT 3575 static void print_field_layout(const Symbol* name, 3576 Array<u2>* fields, 3577 constantPoolHandle cp, 3578 int instance_size, 3579 int instance_fields_start, 3580 int instance_fields_end, 3581 int static_fields_end) { 3582 3583 assert(name != NULL, "invariant"); 3584 3585 tty->print("%s: field layout\n", name->as_klass_external_name()); 3586 tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---"); 3587 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 3588 if (!fs.access_flags().is_static()) { 3589 tty->print(" @%3d \"%s\" %s\n", 3590 fs.offset(), 3591 fs.name()->as_klass_external_name(), 3592 fs.signature()->as_klass_external_name()); 3593 } 3594 } 3595 tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---"); 3596 tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---"); 3597 tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---"); 3598 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 3599 if (fs.access_flags().is_static()) { 3600 tty->print(" @%3d \"%s\" %s\n", 3601 fs.offset(), 3602 fs.name()->as_klass_external_name(), 3603 fs.signature()->as_klass_external_name()); 3604 } 3605 } 3606 tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---"); 3607 tty->print("\n"); 3608 } 3609 #endif 3610 3611 // Values needed for oopmap and InstanceKlass creation 3612 class ClassFileParser::FieldLayoutInfo : public ResourceObj { 3613 public: 3614 int* nonstatic_oop_offsets; 3615 unsigned int* nonstatic_oop_counts; 3616 unsigned int nonstatic_oop_map_count; 3617 unsigned int total_oop_map_count; 3618 int instance_size; 3619 int nonstatic_field_size; 3620 int static_field_size; 3621 bool has_nonstatic_fields; 3622 }; 3623 3624 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring! 3625 void ClassFileParser::layout_fields(ConstantPool* cp, 3626 const FieldAllocationCount* fac, 3627 const ClassAnnotationCollector* parsed_annotations, 3628 FieldLayoutInfo* info, 3629 TRAPS) { 3630 3631 assert(cp != NULL, "invariant"); 3632 3633 // Field size and offset computation 3634 int nonstatic_field_size = _super_klass == NULL ? 0 : 3635 _super_klass->nonstatic_field_size(); 3636 3637 // Count the contended fields by type. 3638 // 3639 // We ignore static fields, because @Contended is not supported for them. 3640 // The layout code below will also ignore the static fields. 3641 int nonstatic_contended_count = 0; 3642 FieldAllocationCount fac_contended; 3643 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 3644 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 3645 if (fs.is_contended()) { 3646 fac_contended.count[atype]++; 3647 if (!fs.access_flags().is_static()) { 3648 nonstatic_contended_count++; 3649 } 3650 } 3651 } 3652 3653 3654 // Calculate the starting byte offsets 3655 int next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields(); 3656 int next_static_double_offset = next_static_oop_offset + 3657 ((fac->count[STATIC_OOP]) * heapOopSize); 3658 if ( fac->count[STATIC_DOUBLE] && 3659 (Universe::field_type_should_be_aligned(T_DOUBLE) || 3660 Universe::field_type_should_be_aligned(T_LONG)) ) { 3661 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong); 3662 } 3663 3664 int next_static_word_offset = next_static_double_offset + 3665 ((fac->count[STATIC_DOUBLE]) * BytesPerLong); 3666 int next_static_short_offset = next_static_word_offset + 3667 ((fac->count[STATIC_WORD]) * BytesPerInt); 3668 int next_static_byte_offset = next_static_short_offset + 3669 ((fac->count[STATIC_SHORT]) * BytesPerShort); 3670 3671 int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() + 3672 nonstatic_field_size * heapOopSize; 3673 3674 int next_nonstatic_field_offset = nonstatic_fields_start; 3675 3676 const bool is_contended_class = parsed_annotations->is_contended(); 3677 3678 // Class is contended, pad before all the fields 3679 if (is_contended_class) { 3680 next_nonstatic_field_offset += ContendedPaddingWidth; 3681 } 3682 3683 // Compute the non-contended fields count. 3684 // The packing code below relies on these counts to determine if some field 3685 // can be squeezed into the alignment gap. Contended fields are obviously 3686 // exempt from that. 3687 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE]; 3688 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD]; 3689 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT]; 3690 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE]; 3691 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP]; 3692 3693 // Total non-static fields count, including every contended field 3694 unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] + 3695 fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] + 3696 fac->count[NONSTATIC_OOP]; 3697 3698 const bool super_has_nonstatic_fields = 3699 (_super_klass != NULL && _super_klass->has_nonstatic_fields()); 3700 const bool has_nonstatic_fields = 3701 super_has_nonstatic_fields || (nonstatic_fields_count != 0); 3702 3703 3704 // Prepare list of oops for oop map generation. 3705 // 3706 // "offset" and "count" lists are describing the set of contiguous oop 3707 // regions. offset[i] is the start of the i-th region, which then has 3708 // count[i] oops following. Before we know how many regions are required, 3709 // we pessimistically allocate the maps to fit all the oops into the 3710 // distinct regions. 3711 // 3712 // TODO: We add +1 to always allocate non-zero resource arrays; we need 3713 // to figure out if we still need to do this. 3714 unsigned int nonstatic_oop_map_count = 0; 3715 unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1; 3716 3717 int* nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD( 3718 THREAD, int, max_nonstatic_oop_maps); 3719 unsigned int* const nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD( 3720 THREAD, unsigned int, max_nonstatic_oop_maps); 3721 3722 int first_nonstatic_oop_offset = 0; // will be set for first oop field 3723 3724 bool compact_fields = CompactFields; 3725 int allocation_style = FieldsAllocationStyle; 3726 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range? 3727 assert(false, "0 <= FieldsAllocationStyle <= 2"); 3728 allocation_style = 1; // Optimistic 3729 } 3730 3731 // The next classes have predefined hard-coded fields offsets 3732 // (see in JavaClasses::compute_hard_coded_offsets()). 3733 // Use default fields allocation order for them. 3734 if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL && 3735 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() || 3736 _class_name == vmSymbols::java_lang_Class() || 3737 _class_name == vmSymbols::java_lang_ClassLoader() || 3738 _class_name == vmSymbols::java_lang_ref_Reference() || 3739 _class_name == vmSymbols::java_lang_ref_SoftReference() || 3740 _class_name == vmSymbols::java_lang_StackTraceElement() || 3741 _class_name == vmSymbols::java_lang_String() || 3742 _class_name == vmSymbols::java_lang_Throwable() || 3743 _class_name == vmSymbols::java_lang_Boolean() || 3744 _class_name == vmSymbols::java_lang_Character() || 3745 _class_name == vmSymbols::java_lang_Float() || 3746 _class_name == vmSymbols::java_lang_Double() || 3747 _class_name == vmSymbols::java_lang_Byte() || 3748 _class_name == vmSymbols::java_lang_Short() || 3749 _class_name == vmSymbols::java_lang_Integer() || 3750 _class_name == vmSymbols::java_lang_Long())) { 3751 allocation_style = 0; // Allocate oops first 3752 compact_fields = false; // Don't compact fields 3753 } 3754 3755 int next_nonstatic_oop_offset = 0; 3756 int next_nonstatic_double_offset = 0; 3757 3758 // Rearrange fields for a given allocation style 3759 if( allocation_style == 0 ) { 3760 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields 3761 next_nonstatic_oop_offset = next_nonstatic_field_offset; 3762 next_nonstatic_double_offset = next_nonstatic_oop_offset + 3763 (nonstatic_oop_count * heapOopSize); 3764 } else if( allocation_style == 1 ) { 3765 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields 3766 next_nonstatic_double_offset = next_nonstatic_field_offset; 3767 } else if( allocation_style == 2 ) { 3768 // Fields allocation: oops fields in super and sub classes are together. 3769 if( nonstatic_field_size > 0 && _super_klass != NULL && 3770 _super_klass->nonstatic_oop_map_size() > 0 ) { 3771 const unsigned int map_count = _super_klass->nonstatic_oop_map_count(); 3772 const OopMapBlock* const first_map = _super_klass->start_of_nonstatic_oop_maps(); 3773 const OopMapBlock* const last_map = first_map + map_count - 1; 3774 const int next_offset = last_map->offset() + (last_map->count() * heapOopSize); 3775 if (next_offset == next_nonstatic_field_offset) { 3776 allocation_style = 0; // allocate oops first 3777 next_nonstatic_oop_offset = next_nonstatic_field_offset; 3778 next_nonstatic_double_offset = next_nonstatic_oop_offset + 3779 (nonstatic_oop_count * heapOopSize); 3780 } 3781 } 3782 if( allocation_style == 2 ) { 3783 allocation_style = 1; // allocate oops last 3784 next_nonstatic_double_offset = next_nonstatic_field_offset; 3785 } 3786 } else { 3787 ShouldNotReachHere(); 3788 } 3789 3790 int nonstatic_oop_space_count = 0; 3791 int nonstatic_word_space_count = 0; 3792 int nonstatic_short_space_count = 0; 3793 int nonstatic_byte_space_count = 0; 3794 int nonstatic_oop_space_offset = 0; 3795 int nonstatic_word_space_offset = 0; 3796 int nonstatic_short_space_offset = 0; 3797 int nonstatic_byte_space_offset = 0; 3798 3799 // Try to squeeze some of the fields into the gaps due to 3800 // long/double alignment. 3801 if (nonstatic_double_count > 0) { 3802 int offset = next_nonstatic_double_offset; 3803 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong); 3804 if (compact_fields && offset != next_nonstatic_double_offset) { 3805 // Allocate available fields into the gap before double field. 3806 int length = next_nonstatic_double_offset - offset; 3807 assert(length == BytesPerInt, ""); 3808 nonstatic_word_space_offset = offset; 3809 if (nonstatic_word_count > 0) { 3810 nonstatic_word_count -= 1; 3811 nonstatic_word_space_count = 1; // Only one will fit 3812 length -= BytesPerInt; 3813 offset += BytesPerInt; 3814 } 3815 nonstatic_short_space_offset = offset; 3816 while (length >= BytesPerShort && nonstatic_short_count > 0) { 3817 nonstatic_short_count -= 1; 3818 nonstatic_short_space_count += 1; 3819 length -= BytesPerShort; 3820 offset += BytesPerShort; 3821 } 3822 nonstatic_byte_space_offset = offset; 3823 while (length > 0 && nonstatic_byte_count > 0) { 3824 nonstatic_byte_count -= 1; 3825 nonstatic_byte_space_count += 1; 3826 length -= 1; 3827 } 3828 // Allocate oop field in the gap if there are no other fields for that. 3829 nonstatic_oop_space_offset = offset; 3830 if (length >= heapOopSize && nonstatic_oop_count > 0 && 3831 allocation_style != 0) { // when oop fields not first 3832 nonstatic_oop_count -= 1; 3833 nonstatic_oop_space_count = 1; // Only one will fit 3834 length -= heapOopSize; 3835 offset += heapOopSize; 3836 } 3837 } 3838 } 3839 3840 int next_nonstatic_word_offset = next_nonstatic_double_offset + 3841 (nonstatic_double_count * BytesPerLong); 3842 int next_nonstatic_short_offset = next_nonstatic_word_offset + 3843 (nonstatic_word_count * BytesPerInt); 3844 int next_nonstatic_byte_offset = next_nonstatic_short_offset + 3845 (nonstatic_short_count * BytesPerShort); 3846 int next_nonstatic_padded_offset = next_nonstatic_byte_offset + 3847 nonstatic_byte_count; 3848 3849 // let oops jump before padding with this allocation style 3850 if( allocation_style == 1 ) { 3851 next_nonstatic_oop_offset = next_nonstatic_padded_offset; 3852 if( nonstatic_oop_count > 0 ) { 3853 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize); 3854 } 3855 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize); 3856 } 3857 3858 // Iterate over fields again and compute correct offsets. 3859 // The field allocation type was temporarily stored in the offset slot. 3860 // oop fields are located before non-oop fields (static and non-static). 3861 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 3862 3863 // skip already laid out fields 3864 if (fs.is_offset_set()) continue; 3865 3866 // contended instance fields are handled below 3867 if (fs.is_contended() && !fs.access_flags().is_static()) continue; 3868 3869 int real_offset = 0; 3870 const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type(); 3871 3872 // pack the rest of the fields 3873 switch (atype) { 3874 case STATIC_OOP: 3875 real_offset = next_static_oop_offset; 3876 next_static_oop_offset += heapOopSize; 3877 break; 3878 case STATIC_BYTE: 3879 real_offset = next_static_byte_offset; 3880 next_static_byte_offset += 1; 3881 break; 3882 case STATIC_SHORT: 3883 real_offset = next_static_short_offset; 3884 next_static_short_offset += BytesPerShort; 3885 break; 3886 case STATIC_WORD: 3887 real_offset = next_static_word_offset; 3888 next_static_word_offset += BytesPerInt; 3889 break; 3890 case STATIC_DOUBLE: 3891 real_offset = next_static_double_offset; 3892 next_static_double_offset += BytesPerLong; 3893 break; 3894 case NONSTATIC_OOP: 3895 if( nonstatic_oop_space_count > 0 ) { 3896 real_offset = nonstatic_oop_space_offset; 3897 nonstatic_oop_space_offset += heapOopSize; 3898 nonstatic_oop_space_count -= 1; 3899 } else { 3900 real_offset = next_nonstatic_oop_offset; 3901 next_nonstatic_oop_offset += heapOopSize; 3902 } 3903 3904 // Record this oop in the oop maps 3905 if( nonstatic_oop_map_count > 0 && 3906 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == 3907 real_offset - 3908 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * 3909 heapOopSize ) { 3910 // This oop is adjacent to the previous one, add to current oop map 3911 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check"); 3912 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; 3913 } else { 3914 // This oop is not adjacent to the previous one, create new oop map 3915 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); 3916 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; 3917 nonstatic_oop_counts [nonstatic_oop_map_count] = 1; 3918 nonstatic_oop_map_count += 1; 3919 if( first_nonstatic_oop_offset == 0 ) { // Undefined 3920 first_nonstatic_oop_offset = real_offset; 3921 } 3922 } 3923 break; 3924 case NONSTATIC_BYTE: 3925 if( nonstatic_byte_space_count > 0 ) { 3926 real_offset = nonstatic_byte_space_offset; 3927 nonstatic_byte_space_offset += 1; 3928 nonstatic_byte_space_count -= 1; 3929 } else { 3930 real_offset = next_nonstatic_byte_offset; 3931 next_nonstatic_byte_offset += 1; 3932 } 3933 break; 3934 case NONSTATIC_SHORT: 3935 if( nonstatic_short_space_count > 0 ) { 3936 real_offset = nonstatic_short_space_offset; 3937 nonstatic_short_space_offset += BytesPerShort; 3938 nonstatic_short_space_count -= 1; 3939 } else { 3940 real_offset = next_nonstatic_short_offset; 3941 next_nonstatic_short_offset += BytesPerShort; 3942 } 3943 break; 3944 case NONSTATIC_WORD: 3945 if( nonstatic_word_space_count > 0 ) { 3946 real_offset = nonstatic_word_space_offset; 3947 nonstatic_word_space_offset += BytesPerInt; 3948 nonstatic_word_space_count -= 1; 3949 } else { 3950 real_offset = next_nonstatic_word_offset; 3951 next_nonstatic_word_offset += BytesPerInt; 3952 } 3953 break; 3954 case NONSTATIC_DOUBLE: 3955 real_offset = next_nonstatic_double_offset; 3956 next_nonstatic_double_offset += BytesPerLong; 3957 break; 3958 default: 3959 ShouldNotReachHere(); 3960 } 3961 fs.set_offset(real_offset); 3962 } 3963 3964 3965 // Handle the contended cases. 3966 // 3967 // Each contended field should not intersect the cache line with another contended field. 3968 // In the absence of alignment information, we end up with pessimistically separating 3969 // the fields with full-width padding. 3970 // 3971 // Additionally, this should not break alignment for the fields, so we round the alignment up 3972 // for each field. 3973 if (nonstatic_contended_count > 0) { 3974 3975 // if there is at least one contended field, we need to have pre-padding for them 3976 next_nonstatic_padded_offset += ContendedPaddingWidth; 3977 3978 // collect all contended groups 3979 ResourceBitMap bm(cp->size()); 3980 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 3981 // skip already laid out fields 3982 if (fs.is_offset_set()) continue; 3983 3984 if (fs.is_contended()) { 3985 bm.set_bit(fs.contended_group()); 3986 } 3987 } 3988 3989 int current_group = -1; 3990 while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) { 3991 3992 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 3993 3994 // skip already laid out fields 3995 if (fs.is_offset_set()) continue; 3996 3997 // skip non-contended fields and fields from different group 3998 if (!fs.is_contended() || (fs.contended_group() != current_group)) continue; 3999 4000 // handle statics below 4001 if (fs.access_flags().is_static()) continue; 4002 4003 int real_offset = 0; 4004 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 4005 4006 switch (atype) { 4007 case NONSTATIC_BYTE: 4008 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, 1); 4009 real_offset = next_nonstatic_padded_offset; 4010 next_nonstatic_padded_offset += 1; 4011 break; 4012 4013 case NONSTATIC_SHORT: 4014 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerShort); 4015 real_offset = next_nonstatic_padded_offset; 4016 next_nonstatic_padded_offset += BytesPerShort; 4017 break; 4018 4019 case NONSTATIC_WORD: 4020 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerInt); 4021 real_offset = next_nonstatic_padded_offset; 4022 next_nonstatic_padded_offset += BytesPerInt; 4023 break; 4024 4025 case NONSTATIC_DOUBLE: 4026 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerLong); 4027 real_offset = next_nonstatic_padded_offset; 4028 next_nonstatic_padded_offset += BytesPerLong; 4029 break; 4030 4031 case NONSTATIC_OOP: 4032 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, heapOopSize); 4033 real_offset = next_nonstatic_padded_offset; 4034 next_nonstatic_padded_offset += heapOopSize; 4035 4036 // Record this oop in the oop maps 4037 if( nonstatic_oop_map_count > 0 && 4038 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == 4039 real_offset - 4040 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * 4041 heapOopSize ) { 4042 // This oop is adjacent to the previous one, add to current oop map 4043 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check"); 4044 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; 4045 } else { 4046 // This oop is not adjacent to the previous one, create new oop map 4047 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); 4048 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; 4049 nonstatic_oop_counts [nonstatic_oop_map_count] = 1; 4050 nonstatic_oop_map_count += 1; 4051 if( first_nonstatic_oop_offset == 0 ) { // Undefined 4052 first_nonstatic_oop_offset = real_offset; 4053 } 4054 } 4055 break; 4056 4057 default: 4058 ShouldNotReachHere(); 4059 } 4060 4061 if (fs.contended_group() == 0) { 4062 // Contended group defines the equivalence class over the fields: 4063 // the fields within the same contended group are not inter-padded. 4064 // The only exception is default group, which does not incur the 4065 // equivalence, and so requires intra-padding. 4066 next_nonstatic_padded_offset += ContendedPaddingWidth; 4067 } 4068 4069 fs.set_offset(real_offset); 4070 } // for 4071 4072 // Start laying out the next group. 4073 // Note that this will effectively pad the last group in the back; 4074 // this is expected to alleviate memory contention effects for 4075 // subclass fields and/or adjacent object. 4076 // If this was the default group, the padding is already in place. 4077 if (current_group != 0) { 4078 next_nonstatic_padded_offset += ContendedPaddingWidth; 4079 } 4080 } 4081 4082 // handle static fields 4083 } 4084 4085 // Entire class is contended, pad in the back. 4086 // This helps to alleviate memory contention effects for subclass fields 4087 // and/or adjacent object. 4088 if (is_contended_class) { 4089 next_nonstatic_padded_offset += ContendedPaddingWidth; 4090 } 4091 4092 int notaligned_nonstatic_fields_end = next_nonstatic_padded_offset; 4093 4094 int nonstatic_fields_end = align_size_up(notaligned_nonstatic_fields_end, heapOopSize); 4095 int instance_end = align_size_up(notaligned_nonstatic_fields_end, wordSize); 4096 int static_fields_end = align_size_up(next_static_byte_offset, wordSize); 4097 4098 int static_field_size = (static_fields_end - 4099 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 4100 nonstatic_field_size = nonstatic_field_size + 4101 (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize; 4102 4103 int instance_size = align_object_size(instance_end / wordSize); 4104 4105 assert(instance_size == align_object_size(align_size_up( 4106 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), 4107 wordSize) / wordSize), "consistent layout helper value"); 4108 4109 // Invariant: nonstatic_field end/start should only change if there are 4110 // nonstatic fields in the class, or if the class is contended. We compare 4111 // against the non-aligned value, so that end alignment will not fail the 4112 // assert without actually having the fields. 4113 assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) || 4114 is_contended_class || 4115 (nonstatic_fields_count > 0), "double-check nonstatic start/end"); 4116 4117 // Number of non-static oop map blocks allocated at end of klass. 4118 const unsigned int total_oop_map_count = 4119 compute_oop_map_count(_super_klass, nonstatic_oop_map_count, 4120 first_nonstatic_oop_offset); 4121 4122 #ifndef PRODUCT 4123 if (PrintFieldLayout) { 4124 print_field_layout(_class_name, 4125 _fields, 4126 cp, 4127 instance_size, 4128 nonstatic_fields_start, 4129 nonstatic_fields_end, 4130 static_fields_end); 4131 } 4132 4133 #endif 4134 // Pass back information needed for InstanceKlass creation 4135 info->nonstatic_oop_offsets = nonstatic_oop_offsets; 4136 info->nonstatic_oop_counts = nonstatic_oop_counts; 4137 info->nonstatic_oop_map_count = nonstatic_oop_map_count; 4138 info->total_oop_map_count = total_oop_map_count; 4139 info->instance_size = instance_size; 4140 info->static_field_size = static_field_size; 4141 info->nonstatic_field_size = nonstatic_field_size; 4142 info->has_nonstatic_fields = has_nonstatic_fields; 4143 } 4144 4145 static void fill_oop_maps(const InstanceKlass* k, 4146 unsigned int nonstatic_oop_map_count, 4147 const int* nonstatic_oop_offsets, 4148 const unsigned int* nonstatic_oop_counts) { 4149 4150 assert(k != NULL, "invariant"); 4151 4152 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps(); 4153 const InstanceKlass* const super = k->superklass(); 4154 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0; 4155 if (super_count > 0) { 4156 // Copy maps from superklass 4157 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps(); 4158 for (unsigned int i = 0; i < super_count; ++i) { 4159 *this_oop_map++ = *super_oop_map++; 4160 } 4161 } 4162 4163 if (nonstatic_oop_map_count > 0) { 4164 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) { 4165 // The counts differ because there is no gap between superklass's last oop 4166 // field and the first local oop field. Extend the last oop map copied 4167 // from the superklass instead of creating new one. 4168 nonstatic_oop_map_count--; 4169 nonstatic_oop_offsets++; 4170 this_oop_map--; 4171 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++); 4172 this_oop_map++; 4173 } 4174 4175 // Add new map blocks, fill them 4176 while (nonstatic_oop_map_count-- > 0) { 4177 this_oop_map->set_offset(*nonstatic_oop_offsets++); 4178 this_oop_map->set_count(*nonstatic_oop_counts++); 4179 this_oop_map++; 4180 } 4181 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() == 4182 this_oop_map, "sanity"); 4183 } 4184 } 4185 4186 4187 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) { 4188 assert(ik != NULL, "invariant"); 4189 4190 const Klass* const super = ik->super(); 4191 4192 // Check if this klass has an empty finalize method (i.e. one with return bytecode only), 4193 // in which case we don't have to register objects as finalizable 4194 if (!_has_empty_finalizer) { 4195 if (_has_finalizer || 4196 (super != NULL && super->has_finalizer())) { 4197 ik->set_has_finalizer(); 4198 } 4199 } 4200 4201 #ifdef ASSERT 4202 bool f = false; 4203 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(), 4204 vmSymbols::void_method_signature()); 4205 if (m != NULL && !m->is_empty_method()) { 4206 f = true; 4207 } 4208 4209 // Spec doesn't prevent agent from redefinition of empty finalizer. 4210 // Despite the fact that it's generally bad idea and redefined finalizer 4211 // will not work as expected we shouldn't abort vm in this case 4212 if (!ik->has_redefined_this_or_super()) { 4213 assert(ik->has_finalizer() == f, "inconsistent has_finalizer"); 4214 } 4215 #endif 4216 4217 // Check if this klass supports the java.lang.Cloneable interface 4218 if (SystemDictionary::Cloneable_klass_loaded()) { 4219 if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) { 4220 ik->set_is_cloneable(); 4221 } 4222 } 4223 4224 // Check if this klass has a vanilla default constructor 4225 if (super == NULL) { 4226 // java.lang.Object has empty default constructor 4227 ik->set_has_vanilla_constructor(); 4228 } else { 4229 if (super->has_vanilla_constructor() && 4230 _has_vanilla_constructor) { 4231 ik->set_has_vanilla_constructor(); 4232 } 4233 #ifdef ASSERT 4234 bool v = false; 4235 if (super->has_vanilla_constructor()) { 4236 const Method* const constructor = 4237 ik->find_method(vmSymbols::object_initializer_name(), 4238 vmSymbols::void_method_signature()); 4239 if (constructor != NULL && constructor->is_vanilla_constructor()) { 4240 v = true; 4241 } 4242 } 4243 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor"); 4244 #endif 4245 } 4246 4247 // If it cannot be fast-path allocated, set a bit in the layout helper. 4248 // See documentation of InstanceKlass::can_be_fastpath_allocated(). 4249 assert(ik->size_helper() > 0, "layout_helper is initialized"); 4250 if ((!RegisterFinalizersAtInit && ik->has_finalizer()) 4251 || ik->is_abstract() || ik->is_interface() 4252 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL) 4253 || ik->size_helper() >= FastAllocateSizeLimit) { 4254 // Forbid fast-path allocation. 4255 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true); 4256 ik->set_layout_helper(lh); 4257 } 4258 } 4259 4260 // Attach super classes and interface classes to class loader data 4261 static void record_defined_class_dependencies(const InstanceKlass* defined_klass, 4262 TRAPS) { 4263 assert(defined_klass != NULL, "invariant"); 4264 4265 ClassLoaderData* const defining_loader_data = defined_klass->class_loader_data(); 4266 if (defining_loader_data->is_the_null_class_loader_data()) { 4267 // Dependencies to null class loader data are implicit. 4268 return; 4269 } else { 4270 // add super class dependency 4271 Klass* const super = defined_klass->super(); 4272 if (super != NULL) { 4273 defining_loader_data->record_dependency(super, CHECK); 4274 } 4275 4276 // add super interface dependencies 4277 const Array<Klass*>* const local_interfaces = defined_klass->local_interfaces(); 4278 if (local_interfaces != NULL) { 4279 const int length = local_interfaces->length(); 4280 for (int i = 0; i < length; i++) { 4281 defining_loader_data->record_dependency(local_interfaces->at(i), CHECK); 4282 } 4283 } 4284 } 4285 } 4286 4287 // utility methods for appending an array with check for duplicates 4288 4289 static void append_interfaces(GrowableArray<Klass*>* result, 4290 const Array<Klass*>* const ifs) { 4291 // iterate over new interfaces 4292 for (int i = 0; i < ifs->length(); i++) { 4293 Klass* const e = ifs->at(i); 4294 assert(e->is_klass() && InstanceKlass::cast(e)->is_interface(), "just checking"); 4295 // add new interface 4296 result->append_if_missing(e); 4297 } 4298 } 4299 4300 static Array<Klass*>* compute_transitive_interfaces(const InstanceKlass* super, 4301 Array<Klass*>* local_ifs, 4302 ClassLoaderData* loader_data, 4303 TRAPS) { 4304 assert(local_ifs != NULL, "invariant"); 4305 assert(loader_data != NULL, "invariant"); 4306 4307 // Compute maximum size for transitive interfaces 4308 int max_transitive_size = 0; 4309 int super_size = 0; 4310 // Add superclass transitive interfaces size 4311 if (super != NULL) { 4312 super_size = super->transitive_interfaces()->length(); 4313 max_transitive_size += super_size; 4314 } 4315 // Add local interfaces' super interfaces 4316 const int local_size = local_ifs->length(); 4317 for (int i = 0; i < local_size; i++) { 4318 Klass* const l = local_ifs->at(i); 4319 max_transitive_size += InstanceKlass::cast(l)->transitive_interfaces()->length(); 4320 } 4321 // Finally add local interfaces 4322 max_transitive_size += local_size; 4323 // Construct array 4324 if (max_transitive_size == 0) { 4325 // no interfaces, use canonicalized array 4326 return Universe::the_empty_klass_array(); 4327 } else if (max_transitive_size == super_size) { 4328 // no new local interfaces added, share superklass' transitive interface array 4329 return super->transitive_interfaces(); 4330 } else if (max_transitive_size == local_size) { 4331 // only local interfaces added, share local interface array 4332 return local_ifs; 4333 } else { 4334 ResourceMark rm; 4335 GrowableArray<Klass*>* const result = new GrowableArray<Klass*>(max_transitive_size); 4336 4337 // Copy down from superclass 4338 if (super != NULL) { 4339 append_interfaces(result, super->transitive_interfaces()); 4340 } 4341 4342 // Copy down from local interfaces' superinterfaces 4343 for (int i = 0; i < local_size; i++) { 4344 Klass* const l = local_ifs->at(i); 4345 append_interfaces(result, InstanceKlass::cast(l)->transitive_interfaces()); 4346 } 4347 // Finally add local interfaces 4348 append_interfaces(result, local_ifs); 4349 4350 // length will be less than the max_transitive_size if duplicates were removed 4351 const int length = result->length(); 4352 assert(length <= max_transitive_size, "just checking"); 4353 Array<Klass*>* const new_result = 4354 MetadataFactory::new_array<Klass*>(loader_data, length, CHECK_NULL); 4355 for (int i = 0; i < length; i++) { 4356 Klass* const e = result->at(i); 4357 assert(e != NULL, "just checking"); 4358 new_result->at_put(i, e); 4359 } 4360 return new_result; 4361 } 4362 } 4363 4364 static void check_super_class_access(const InstanceKlass* this_klass, TRAPS) { 4365 assert(this_klass != NULL, "invariant"); 4366 const Klass* const super = this_klass->super(); 4367 if (super != NULL) { 4368 4369 // If the loader is not the boot loader then throw an exception if its 4370 // superclass is in package jdk.internal.reflect and its loader is not a 4371 // special reflection class loader 4372 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) { 4373 assert(super->is_instance_klass(), "super is not instance klass"); 4374 PackageEntry* super_package = super->package(); 4375 if (super_package != NULL && 4376 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 && 4377 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) { 4378 ResourceMark rm(THREAD); 4379 Exceptions::fthrow( 4380 THREAD_AND_LOCATION, 4381 vmSymbols::java_lang_IllegalAccessError(), 4382 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s", 4383 this_klass->external_name(), 4384 this_klass->class_loader_data()->loader_name(), 4385 super->external_name()); 4386 return; 4387 } 4388 } 4389 4390 Reflection::VerifyClassAccessResults vca_result = 4391 Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false); 4392 if (vca_result != Reflection::ACCESS_OK) { 4393 ResourceMark rm(THREAD); 4394 char* msg = Reflection::verify_class_access_msg(this_klass, 4395 InstanceKlass::cast(super), 4396 vca_result); 4397 if (msg == NULL) { 4398 Exceptions::fthrow( 4399 THREAD_AND_LOCATION, 4400 vmSymbols::java_lang_IllegalAccessError(), 4401 "class %s cannot access its superclass %s", 4402 this_klass->external_name(), 4403 super->external_name()); 4404 } else { 4405 // Add additional message content. 4406 Exceptions::fthrow( 4407 THREAD_AND_LOCATION, 4408 vmSymbols::java_lang_IllegalAccessError(), 4409 "superclass access check failed: %s", 4410 msg); 4411 } 4412 } 4413 } 4414 } 4415 4416 4417 static void check_super_interface_access(const InstanceKlass* this_klass, TRAPS) { 4418 assert(this_klass != NULL, "invariant"); 4419 const Array<Klass*>* const local_interfaces = this_klass->local_interfaces(); 4420 const int lng = local_interfaces->length(); 4421 for (int i = lng - 1; i >= 0; i--) { 4422 Klass* const k = local_interfaces->at(i); 4423 assert (k != NULL && k->is_interface(), "invalid interface"); 4424 Reflection::VerifyClassAccessResults vca_result = 4425 Reflection::verify_class_access(this_klass, InstanceKlass::cast(k), false); 4426 if (vca_result != Reflection::ACCESS_OK) { 4427 ResourceMark rm(THREAD); 4428 char* msg = Reflection::verify_class_access_msg(this_klass, 4429 InstanceKlass::cast(k), 4430 vca_result); 4431 if (msg == NULL) { 4432 Exceptions::fthrow( 4433 THREAD_AND_LOCATION, 4434 vmSymbols::java_lang_IllegalAccessError(), 4435 "class %s cannot access its superinterface %s", 4436 this_klass->external_name(), 4437 k->external_name()); 4438 } else { 4439 // Add additional message content. 4440 Exceptions::fthrow( 4441 THREAD_AND_LOCATION, 4442 vmSymbols::java_lang_IllegalAccessError(), 4443 "superinterface check failed: %s", 4444 msg); 4445 } 4446 } 4447 } 4448 } 4449 4450 4451 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) { 4452 assert(this_klass != NULL, "invariant"); 4453 const Array<Method*>* const methods = this_klass->methods(); 4454 const int num_methods = methods->length(); 4455 4456 // go thru each method and check if it overrides a final method 4457 for (int index = 0; index < num_methods; index++) { 4458 const Method* const m = methods->at(index); 4459 4460 // skip private, static, and <init> methods 4461 if ((!m->is_private() && !m->is_static()) && 4462 (m->name() != vmSymbols::object_initializer_name())) { 4463 4464 const Symbol* const name = m->name(); 4465 const Symbol* const signature = m->signature(); 4466 const Klass* k = this_klass->super(); 4467 const Method* super_m = NULL; 4468 while (k != NULL) { 4469 // skip supers that don't have final methods. 4470 if (k->has_final_method()) { 4471 // lookup a matching method in the super class hierarchy 4472 super_m = InstanceKlass::cast(k)->lookup_method(name, signature); 4473 if (super_m == NULL) { 4474 break; // didn't find any match; get out 4475 } 4476 4477 if (super_m->is_final() && !super_m->is_static() && 4478 // matching method in super is final, and not static 4479 (Reflection::verify_field_access(this_klass, 4480 super_m->method_holder(), 4481 super_m->method_holder(), 4482 super_m->access_flags(), false)) 4483 // this class can access super final method and therefore override 4484 ) { 4485 ResourceMark rm(THREAD); 4486 Exceptions::fthrow( 4487 THREAD_AND_LOCATION, 4488 vmSymbols::java_lang_VerifyError(), 4489 "class %s overrides final method %s.%s%s", 4490 this_klass->external_name(), 4491 super_m->method_holder()->external_name(), 4492 name->as_C_string(), 4493 signature->as_C_string() 4494 ); 4495 return; 4496 } 4497 4498 // continue to look from super_m's holder's super. 4499 k = super_m->method_holder()->super(); 4500 continue; 4501 } 4502 4503 k = k->super(); 4504 } 4505 } 4506 } 4507 } 4508 4509 4510 // assumes that this_klass is an interface 4511 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) { 4512 assert(this_klass != NULL, "invariant"); 4513 assert(this_klass->is_interface(), "not an interface"); 4514 const Array<Method*>* methods = this_klass->methods(); 4515 const int num_methods = methods->length(); 4516 4517 for (int index = 0; index < num_methods; index++) { 4518 const Method* const m = methods->at(index); 4519 // if m is static and not the init method, throw a verify error 4520 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) { 4521 ResourceMark rm(THREAD); 4522 Exceptions::fthrow( 4523 THREAD_AND_LOCATION, 4524 vmSymbols::java_lang_VerifyError(), 4525 "Illegal static method %s in interface %s", 4526 m->name()->as_C_string(), 4527 this_klass->external_name() 4528 ); 4529 return; 4530 } 4531 } 4532 } 4533 4534 // utility methods for format checking 4535 4536 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const { 4537 if (!_need_verify) { return; } 4538 4539 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0; 4540 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 4541 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4542 const bool is_super = (flags & JVM_ACC_SUPER) != 0; 4543 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 4544 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0; 4545 const bool is_module_info= (flags & JVM_ACC_MODULE) != 0; 4546 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4547 4548 if ((is_abstract && is_final) || 4549 (is_interface && !is_abstract) || 4550 (is_interface && major_gte_15 && (is_super || is_enum)) || 4551 (!is_interface && major_gte_15 && is_annotation) || 4552 is_module_info) { 4553 ResourceMark rm(THREAD); 4554 Exceptions::fthrow( 4555 THREAD_AND_LOCATION, 4556 vmSymbols::java_lang_ClassFormatError(), 4557 "Illegal class modifiers in class %s: 0x%X", 4558 _class_name->as_C_string(), flags 4559 ); 4560 return; 4561 } 4562 } 4563 4564 static bool has_illegal_visibility(jint flags) { 4565 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4566 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4567 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4568 4569 return ((is_public && is_protected) || 4570 (is_public && is_private) || 4571 (is_protected && is_private)); 4572 } 4573 4574 static bool is_supported_version(u2 major, u2 minor){ 4575 const u2 max_version = JAVA_MAX_SUPPORTED_VERSION; 4576 return (major >= JAVA_MIN_SUPPORTED_VERSION) && 4577 (major <= max_version) && 4578 ((major != max_version) || 4579 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION)); 4580 } 4581 4582 void ClassFileParser::verify_legal_field_modifiers(jint flags, 4583 bool is_interface, 4584 TRAPS) const { 4585 if (!_need_verify) { return; } 4586 4587 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4588 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4589 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4590 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 4591 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4592 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0; 4593 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0; 4594 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 4595 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4596 4597 bool is_illegal = false; 4598 4599 if (is_interface) { 4600 if (!is_public || !is_static || !is_final || is_private || 4601 is_protected || is_volatile || is_transient || 4602 (major_gte_15 && is_enum)) { 4603 is_illegal = true; 4604 } 4605 } else { // not interface 4606 if (has_illegal_visibility(flags) || (is_final && is_volatile)) { 4607 is_illegal = true; 4608 } 4609 } 4610 4611 if (is_illegal) { 4612 ResourceMark rm(THREAD); 4613 Exceptions::fthrow( 4614 THREAD_AND_LOCATION, 4615 vmSymbols::java_lang_ClassFormatError(), 4616 "Illegal field modifiers in class %s: 0x%X", 4617 _class_name->as_C_string(), flags); 4618 return; 4619 } 4620 } 4621 4622 void ClassFileParser::verify_legal_method_modifiers(jint flags, 4623 bool is_interface, 4624 const Symbol* name, 4625 TRAPS) const { 4626 if (!_need_verify) { return; } 4627 4628 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4629 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4630 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 4631 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4632 const bool is_native = (flags & JVM_ACC_NATIVE) != 0; 4633 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 4634 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0; 4635 const bool is_strict = (flags & JVM_ACC_STRICT) != 0; 4636 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0; 4637 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4638 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4639 const bool major_gte_8 = _major_version >= JAVA_8_VERSION; 4640 const bool is_initializer = (name == vmSymbols::object_initializer_name()); 4641 4642 bool is_illegal = false; 4643 4644 if (is_interface) { 4645 if (major_gte_8) { 4646 // Class file version is JAVA_8_VERSION or later Methods of 4647 // interfaces may set any of the flags except ACC_PROTECTED, 4648 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must 4649 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. 4650 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */ 4651 (is_native || is_protected || is_final || is_synchronized) || 4652 // If a specific method of a class or interface has its 4653 // ACC_ABSTRACT flag set, it must not have any of its 4654 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, 4655 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to 4656 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as 4657 // those flags are illegal irrespective of ACC_ABSTRACT being set or not. 4658 (is_abstract && (is_private || is_static || is_strict))) { 4659 is_illegal = true; 4660 } 4661 } else if (major_gte_15) { 4662 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION) 4663 if (!is_public || is_private || is_protected || is_static || is_final || 4664 is_synchronized || is_native || !is_abstract || is_strict) { 4665 is_illegal = true; 4666 } 4667 } else { 4668 // Class file version is pre-JAVA_1_5_VERSION 4669 if (!is_public || is_static || is_final || is_native || !is_abstract) { 4670 is_illegal = true; 4671 } 4672 } 4673 } else { // not interface 4674 if (has_illegal_visibility(flags)) { 4675 is_illegal = true; 4676 } else { 4677 if (is_initializer) { 4678 if (is_static || is_final || is_synchronized || is_native || 4679 is_abstract || (major_gte_15 && is_bridge)) { 4680 is_illegal = true; 4681 } 4682 } else { // not initializer 4683 if (is_abstract) { 4684 if ((is_final || is_native || is_private || is_static || 4685 (major_gte_15 && (is_synchronized || is_strict)))) { 4686 is_illegal = true; 4687 } 4688 } 4689 } 4690 } 4691 } 4692 4693 if (is_illegal) { 4694 ResourceMark rm(THREAD); 4695 Exceptions::fthrow( 4696 THREAD_AND_LOCATION, 4697 vmSymbols::java_lang_ClassFormatError(), 4698 "Method %s in class %s has illegal modifiers: 0x%X", 4699 name->as_C_string(), _class_name->as_C_string(), flags); 4700 return; 4701 } 4702 } 4703 4704 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, 4705 int length, 4706 TRAPS) const { 4707 assert(_need_verify, "only called when _need_verify is true"); 4708 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) { 4709 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); 4710 } 4711 } 4712 4713 // Unqualified names may not contain the characters '.', ';', '[', or '/'. 4714 // In class names, '/' separates unqualified names. This is verified in this function also. 4715 // Method names also may not contain the characters '<' or '>', unless <init> 4716 // or <clinit>. Note that method names may not be <init> or <clinit> in this 4717 // method. Because these names have been checked as special cases before 4718 // calling this method in verify_legal_method_name. 4719 // 4720 // This method is also called from the modular system APIs in modules.cpp 4721 // to verify the validity of module and package names. 4722 bool ClassFileParser::verify_unqualified_name(const char* name, 4723 unsigned int length, 4724 int type) { 4725 for (const char* p = name; p != name + length;) { 4726 jchar ch = *p; 4727 if (ch < 128) { 4728 if (ch == '.' || ch == ';' || ch == '[' ) { 4729 return false; // do not permit '.', ';', or '[' 4730 } 4731 if (ch == '/') { 4732 // check for '//' or leading or trailing '/' which are not legal 4733 // unqualified name must not be empty 4734 if (type == ClassFileParser::LegalClass) { 4735 if (p == name || p+1 >= name+length || *(p+1) == '/') { 4736 return false; 4737 } 4738 } else { 4739 return false; // do not permit '/' unless it's class name 4740 } 4741 } 4742 if (type == ClassFileParser::LegalMethod && (ch == '<' || ch == '>')) { 4743 return false; // do not permit '<' or '>' in method names 4744 } 4745 p++; 4746 } else { 4747 char* tmp_p = UTF8::next(p, &ch); 4748 p = tmp_p; 4749 } 4750 } 4751 return true; 4752 } 4753 4754 // Take pointer to a string. Skip over the longest part of the string that could 4755 // be taken as a fieldname. Allow '/' if slash_ok is true. 4756 // Return a pointer to just past the fieldname. 4757 // Return NULL if no fieldname at all was found, or in the case of slash_ok 4758 // being true, we saw consecutive slashes (meaning we were looking for a 4759 // qualified path but found something that was badly-formed). 4760 static const char* skip_over_field_name(const char* name, 4761 bool slash_ok, 4762 unsigned int length) { 4763 const char* p; 4764 jboolean last_is_slash = false; 4765 jboolean not_first_ch = false; 4766 4767 for (p = name; p != name + length; not_first_ch = true) { 4768 const char* old_p = p; 4769 jchar ch = *p; 4770 if (ch < 128) { 4771 p++; 4772 // quick check for ascii 4773 if ((ch >= 'a' && ch <= 'z') || 4774 (ch >= 'A' && ch <= 'Z') || 4775 (ch == '_' || ch == '$') || 4776 (not_first_ch && ch >= '0' && ch <= '9')) { 4777 last_is_slash = false; 4778 continue; 4779 } 4780 if (slash_ok && ch == '/') { 4781 if (last_is_slash) { 4782 return NULL; // Don't permit consecutive slashes 4783 } 4784 last_is_slash = true; 4785 continue; 4786 } 4787 } 4788 else { 4789 jint unicode_ch; 4790 char* tmp_p = UTF8::next_character(p, &unicode_ch); 4791 p = tmp_p; 4792 last_is_slash = false; 4793 // Check if ch is Java identifier start or is Java identifier part 4794 // 4672820: call java.lang.Character methods directly without generating separate tables. 4795 EXCEPTION_MARK; 4796 4797 // return value 4798 JavaValue result(T_BOOLEAN); 4799 // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart 4800 JavaCallArguments args; 4801 args.push_int(unicode_ch); 4802 4803 // public static boolean isJavaIdentifierStart(char ch); 4804 JavaCalls::call_static(&result, 4805 SystemDictionary::Character_klass(), 4806 vmSymbols::isJavaIdentifierStart_name(), 4807 vmSymbols::int_bool_signature(), 4808 &args, 4809 THREAD); 4810 4811 if (HAS_PENDING_EXCEPTION) { 4812 CLEAR_PENDING_EXCEPTION; 4813 return 0; 4814 } 4815 if (result.get_jboolean()) { 4816 continue; 4817 } 4818 4819 if (not_first_ch) { 4820 // public static boolean isJavaIdentifierPart(char ch); 4821 JavaCalls::call_static(&result, 4822 SystemDictionary::Character_klass(), 4823 vmSymbols::isJavaIdentifierPart_name(), 4824 vmSymbols::int_bool_signature(), 4825 &args, 4826 THREAD); 4827 4828 if (HAS_PENDING_EXCEPTION) { 4829 CLEAR_PENDING_EXCEPTION; 4830 return 0; 4831 } 4832 4833 if (result.get_jboolean()) { 4834 continue; 4835 } 4836 } 4837 } 4838 return (not_first_ch) ? old_p : NULL; 4839 } 4840 return (not_first_ch) ? p : NULL; 4841 } 4842 4843 // Take pointer to a string. Skip over the longest part of the string that could 4844 // be taken as a field signature. Allow "void" if void_ok. 4845 // Return a pointer to just past the signature. 4846 // Return NULL if no legal signature is found. 4847 const char* ClassFileParser::skip_over_field_signature(const char* signature, 4848 bool void_ok, 4849 unsigned int length, 4850 TRAPS) const { 4851 unsigned int array_dim = 0; 4852 while (length > 0) { 4853 switch (signature[0]) { 4854 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; } 4855 case JVM_SIGNATURE_BOOLEAN: 4856 case JVM_SIGNATURE_BYTE: 4857 case JVM_SIGNATURE_CHAR: 4858 case JVM_SIGNATURE_SHORT: 4859 case JVM_SIGNATURE_INT: 4860 case JVM_SIGNATURE_FLOAT: 4861 case JVM_SIGNATURE_LONG: 4862 case JVM_SIGNATURE_DOUBLE: 4863 return signature + 1; 4864 case JVM_SIGNATURE_CLASS: { 4865 if (_major_version < JAVA_1_5_VERSION) { 4866 // Skip over the class name if one is there 4867 const char* const p = skip_over_field_name(signature + 1, true, --length); 4868 4869 // The next character better be a semicolon 4870 if (p && (p - signature) > 1 && p[0] == ';') { 4871 return p + 1; 4872 } 4873 } 4874 else { 4875 // Skip leading 'L' and ignore first appearance of ';' 4876 length--; 4877 signature++; 4878 char* c = strchr((char*) signature, ';'); 4879 // Format check signature 4880 if (c != NULL) { 4881 ResourceMark rm(THREAD); 4882 int newlen = c - (char*) signature; 4883 char* sig = NEW_RESOURCE_ARRAY(char, newlen + 1); 4884 strncpy(sig, signature, newlen); 4885 sig[newlen] = '\0'; 4886 4887 bool legal = verify_unqualified_name(sig, newlen, LegalClass); 4888 if (!legal) { 4889 classfile_parse_error("Class name contains illegal character " 4890 "in descriptor in class file %s", 4891 CHECK_0); 4892 return NULL; 4893 } 4894 return signature + newlen + 1; 4895 } 4896 } 4897 return NULL; 4898 } 4899 case JVM_SIGNATURE_ARRAY: 4900 array_dim++; 4901 if (array_dim > 255) { 4902 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions. 4903 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0); 4904 } 4905 // The rest of what's there better be a legal signature 4906 signature++; 4907 length--; 4908 void_ok = false; 4909 break; 4910 default: 4911 return NULL; 4912 } 4913 } 4914 return NULL; 4915 } 4916 4917 // Checks if name is a legal class name. 4918 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const { 4919 if (!_need_verify || _relax_verify) { return; } 4920 4921 char buf[fixed_buffer_size]; 4922 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 4923 unsigned int length = name->utf8_length(); 4924 bool legal = false; 4925 4926 if (length > 0) { 4927 const char* p; 4928 if (bytes[0] == JVM_SIGNATURE_ARRAY) { 4929 p = skip_over_field_signature(bytes, false, length, CHECK); 4930 legal = (p != NULL) && ((p - bytes) == (int)length); 4931 } else if (_major_version < JAVA_1_5_VERSION) { 4932 if (bytes[0] != '<') { 4933 p = skip_over_field_name(bytes, true, length); 4934 legal = (p != NULL) && ((p - bytes) == (int)length); 4935 } 4936 } else { 4937 // 4900761: relax the constraints based on JSR202 spec 4938 // Class names may be drawn from the entire Unicode character set. 4939 // Identifiers between '/' must be unqualified names. 4940 // The utf8 string has been verified when parsing cpool entries. 4941 legal = verify_unqualified_name(bytes, length, LegalClass); 4942 } 4943 } 4944 if (!legal) { 4945 ResourceMark rm(THREAD); 4946 assert(_class_name != NULL, "invariant"); 4947 Exceptions::fthrow( 4948 THREAD_AND_LOCATION, 4949 vmSymbols::java_lang_ClassFormatError(), 4950 "Illegal class name \"%s\" in class file %s", bytes, 4951 _class_name->as_C_string() 4952 ); 4953 return; 4954 } 4955 } 4956 4957 // Checks if name is a legal field name. 4958 void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const { 4959 if (!_need_verify || _relax_verify) { return; } 4960 4961 char buf[fixed_buffer_size]; 4962 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 4963 unsigned int length = name->utf8_length(); 4964 bool legal = false; 4965 4966 if (length > 0) { 4967 if (_major_version < JAVA_1_5_VERSION) { 4968 if (bytes[0] != '<') { 4969 const char* p = skip_over_field_name(bytes, false, length); 4970 legal = (p != NULL) && ((p - bytes) == (int)length); 4971 } 4972 } else { 4973 // 4881221: relax the constraints based on JSR202 spec 4974 legal = verify_unqualified_name(bytes, length, LegalField); 4975 } 4976 } 4977 4978 if (!legal) { 4979 ResourceMark rm(THREAD); 4980 assert(_class_name != NULL, "invariant"); 4981 Exceptions::fthrow( 4982 THREAD_AND_LOCATION, 4983 vmSymbols::java_lang_ClassFormatError(), 4984 "Illegal field name \"%s\" in class %s", bytes, 4985 _class_name->as_C_string() 4986 ); 4987 return; 4988 } 4989 } 4990 4991 // Checks if name is a legal method name. 4992 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const { 4993 if (!_need_verify || _relax_verify) { return; } 4994 4995 assert(name != NULL, "method name is null"); 4996 char buf[fixed_buffer_size]; 4997 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 4998 unsigned int length = name->utf8_length(); 4999 bool legal = false; 5000 5001 if (length > 0) { 5002 if (bytes[0] == '<') { 5003 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) { 5004 legal = true; 5005 } 5006 } else if (_major_version < JAVA_1_5_VERSION) { 5007 const char* p; 5008 p = skip_over_field_name(bytes, false, length); 5009 legal = (p != NULL) && ((p - bytes) == (int)length); 5010 } else { 5011 // 4881221: relax the constraints based on JSR202 spec 5012 legal = verify_unqualified_name(bytes, length, LegalMethod); 5013 } 5014 } 5015 5016 if (!legal) { 5017 ResourceMark rm(THREAD); 5018 assert(_class_name != NULL, "invariant"); 5019 Exceptions::fthrow( 5020 THREAD_AND_LOCATION, 5021 vmSymbols::java_lang_ClassFormatError(), 5022 "Illegal method name \"%s\" in class %s", bytes, 5023 _class_name->as_C_string() 5024 ); 5025 return; 5026 } 5027 } 5028 5029 5030 // Checks if signature is a legal field signature. 5031 void ClassFileParser::verify_legal_field_signature(const Symbol* name, 5032 const Symbol* signature, 5033 TRAPS) const { 5034 if (!_need_verify) { return; } 5035 5036 char buf[fixed_buffer_size]; 5037 const char* const bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 5038 const unsigned int length = signature->utf8_length(); 5039 const char* const p = skip_over_field_signature(bytes, false, length, CHECK); 5040 5041 if (p == NULL || (p - bytes) != (int)length) { 5042 throwIllegalSignature("Field", name, signature, CHECK); 5043 } 5044 } 5045 5046 // Checks if signature is a legal method signature. 5047 // Returns number of parameters 5048 int ClassFileParser::verify_legal_method_signature(const Symbol* name, 5049 const Symbol* signature, 5050 TRAPS) const { 5051 if (!_need_verify) { 5052 // make sure caller's args_size will be less than 0 even for non-static 5053 // method so it will be recomputed in compute_size_of_parameters(). 5054 return -2; 5055 } 5056 5057 // Class initializers cannot have args for class format version >= 51. 5058 if (name == vmSymbols::class_initializer_name() && 5059 signature != vmSymbols::void_method_signature() && 5060 _major_version >= JAVA_7_VERSION) { 5061 throwIllegalSignature("Method", name, signature, CHECK_0); 5062 return 0; 5063 } 5064 5065 unsigned int args_size = 0; 5066 char buf[fixed_buffer_size]; 5067 const char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 5068 unsigned int length = signature->utf8_length(); 5069 const char* nextp; 5070 5071 // The first character must be a '(' 5072 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) { 5073 length--; 5074 // Skip over legal field signatures 5075 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5076 while ((length > 0) && (nextp != NULL)) { 5077 args_size++; 5078 if (p[0] == 'J' || p[0] == 'D') { 5079 args_size++; 5080 } 5081 length -= nextp - p; 5082 p = nextp; 5083 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5084 } 5085 // The first non-signature thing better be a ')' 5086 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) { 5087 length--; 5088 if (name->utf8_length() > 0 && name->byte_at(0) == '<') { 5089 // All internal methods must return void 5090 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) { 5091 return args_size; 5092 } 5093 } else { 5094 // Now we better just have a return value 5095 nextp = skip_over_field_signature(p, true, length, CHECK_0); 5096 if (nextp && ((int)length == (nextp - p))) { 5097 return args_size; 5098 } 5099 } 5100 } 5101 } 5102 // Report error 5103 throwIllegalSignature("Method", name, signature, CHECK_0); 5104 return 0; 5105 } 5106 5107 int ClassFileParser::static_field_size() const { 5108 assert(_field_info != NULL, "invariant"); 5109 return _field_info->static_field_size; 5110 } 5111 5112 int ClassFileParser::total_oop_map_count() const { 5113 assert(_field_info != NULL, "invariant"); 5114 return _field_info->total_oop_map_count; 5115 } 5116 5117 jint ClassFileParser::layout_size() const { 5118 assert(_field_info != NULL, "invariant"); 5119 return _field_info->instance_size; 5120 } 5121 5122 static void check_methods_for_intrinsics(const InstanceKlass* ik, 5123 const Array<Method*>* methods) { 5124 assert(ik != NULL, "invariant"); 5125 assert(methods != NULL, "invariant"); 5126 5127 // Set up Method*::intrinsic_id as soon as we know the names of methods. 5128 // (We used to do this lazily, but now we query it in Rewriter, 5129 // which is eagerly done for every method, so we might as well do it now, 5130 // when everything is fresh in memory.) 5131 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik); 5132 5133 if (klass_id != vmSymbols::NO_SID) { 5134 for (int j = 0; j < methods->length(); ++j) { 5135 Method* method = methods->at(j); 5136 method->init_intrinsic_id(); 5137 5138 if (CheckIntrinsics) { 5139 // Check if an intrinsic is defined for method 'method', 5140 // but the method is not annotated with @HotSpotIntrinsicCandidate. 5141 if (method->intrinsic_id() != vmIntrinsics::_none && 5142 !method->intrinsic_candidate()) { 5143 tty->print("Compiler intrinsic is defined for method [%s], " 5144 "but the method is not annotated with @HotSpotIntrinsicCandidate.%s", 5145 method->name_and_sig_as_C_string(), 5146 NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.") 5147 ); 5148 tty->cr(); 5149 DEBUG_ONLY(vm_exit(1)); 5150 } 5151 // Check is the method 'method' is annotated with @HotSpotIntrinsicCandidate, 5152 // but there is no intrinsic available for it. 5153 if (method->intrinsic_candidate() && 5154 method->intrinsic_id() == vmIntrinsics::_none) { 5155 tty->print("Method [%s] is annotated with @HotSpotIntrinsicCandidate, " 5156 "but no compiler intrinsic is defined for the method.%s", 5157 method->name_and_sig_as_C_string(), 5158 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5159 ); 5160 tty->cr(); 5161 DEBUG_ONLY(vm_exit(1)); 5162 } 5163 } 5164 } // end for 5165 5166 #ifdef ASSERT 5167 if (CheckIntrinsics) { 5168 // Check for orphan methods in the current class. A method m 5169 // of a class C is orphan if an intrinsic is defined for method m, 5170 // but class C does not declare m. 5171 // The check is potentially expensive, therefore it is available 5172 // only in debug builds. 5173 5174 for (int id = vmIntrinsics::FIRST_ID; id < (int)vmIntrinsics::ID_LIMIT; ++id) { 5175 if (vmIntrinsics::_compiledLambdaForm == id) { 5176 // The _compiledLamdbdaForm intrinsic is a special marker for bytecode 5177 // generated for the JVM from a LambdaForm and therefore no method 5178 // is defined for it. 5179 continue; 5180 } 5181 5182 if (vmIntrinsics::class_for(vmIntrinsics::ID_from(id)) == klass_id) { 5183 // Check if the current class contains a method with the same 5184 // name, flags, signature. 5185 bool match = false; 5186 for (int j = 0; j < methods->length(); ++j) { 5187 const Method* method = methods->at(j); 5188 if (method->intrinsic_id() == id) { 5189 match = true; 5190 break; 5191 } 5192 } 5193 5194 if (!match) { 5195 char buf[1000]; 5196 tty->print("Compiler intrinsic is defined for method [%s], " 5197 "but the method is not available in class [%s].%s", 5198 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id), 5199 buf, sizeof(buf)), 5200 ik->name()->as_C_string(), 5201 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5202 ); 5203 tty->cr(); 5204 DEBUG_ONLY(vm_exit(1)); 5205 } 5206 } 5207 } // end for 5208 } // CheckIntrinsics 5209 #endif // ASSERT 5210 } 5211 } 5212 5213 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) { 5214 if (_klass != NULL) { 5215 return _klass; 5216 } 5217 5218 InstanceKlass* const ik = 5219 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL); 5220 5221 fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL); 5222 5223 assert(_klass == ik, "invariant"); 5224 5225 ik->set_has_passed_fingerprint_check(false); 5226 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) { 5227 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik); 5228 if (aot_fp != 0 && aot_fp == _stream->compute_fingerprint()) { 5229 // This class matches with a class saved in an AOT library 5230 ik->set_has_passed_fingerprint_check(true); 5231 } else { 5232 ResourceMark rm; 5233 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, 5234 ik->external_name(), aot_fp, _stream->compute_fingerprint()); 5235 } 5236 } 5237 5238 return ik; 5239 } 5240 5241 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) { 5242 assert(ik != NULL, "invariant"); 5243 5244 set_klass_to_deallocate(ik); 5245 5246 assert(_field_info != NULL, "invariant"); 5247 assert(ik->static_field_size() == _field_info->static_field_size, "sanity"); 5248 assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count, 5249 "sanity"); 5250 5251 assert(ik->is_instance_klass(), "sanity"); 5252 assert(ik->size_helper() == _field_info->instance_size, "sanity"); 5253 5254 // Fill in information already parsed 5255 ik->set_should_verify_class(_need_verify); 5256 5257 // Not yet: supers are done below to support the new subtype-checking fields 5258 ik->set_class_loader_data(_loader_data); 5259 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size); 5260 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields); 5261 assert(_fac != NULL, "invariant"); 5262 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]); 5263 5264 // this transfers ownership of a lot of arrays from 5265 // the parser onto the InstanceKlass* 5266 apply_parsed_class_metadata(ik, _java_fields_count, CHECK); 5267 5268 // note that is not safe to use the fields in the parser from this point on 5269 assert(NULL == _cp, "invariant"); 5270 assert(NULL == _fields, "invariant"); 5271 assert(NULL == _methods, "invariant"); 5272 assert(NULL == _inner_classes, "invariant"); 5273 assert(NULL == _local_interfaces, "invariant"); 5274 assert(NULL == _transitive_interfaces, "invariant"); 5275 assert(NULL == _combined_annotations, "invariant"); 5276 5277 if (_has_final_method) { 5278 ik->set_has_final_method(); 5279 } 5280 5281 ik->copy_method_ordering(_method_ordering, CHECK); 5282 // The InstanceKlass::_methods_jmethod_ids cache 5283 // is managed on the assumption that the initial cache 5284 // size is equal to the number of methods in the class. If 5285 // that changes, then InstanceKlass::idnum_can_increment() 5286 // has to be changed accordingly. 5287 ik->set_initial_method_idnum(ik->methods()->length()); 5288 5289 ik->set_name(_class_name); 5290 5291 if (is_anonymous()) { 5292 // I am well known to myself 5293 ik->constants()->klass_at_put(_this_class_index, ik); // eagerly resolve 5294 } 5295 5296 ik->set_minor_version(_minor_version); 5297 ik->set_major_version(_major_version); 5298 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods); 5299 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods); 5300 5301 if (_host_klass != NULL) { 5302 assert (ik->is_anonymous(), "should be the same"); 5303 ik->set_host_klass(_host_klass); 5304 } 5305 5306 // Set PackageEntry for this_klass 5307 oop cl = ik->class_loader(); 5308 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl)); 5309 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh()); 5310 ik->set_package(cld, CHECK); 5311 5312 const Array<Method*>* const methods = ik->methods(); 5313 assert(methods != NULL, "invariant"); 5314 const int methods_len = methods->length(); 5315 5316 check_methods_for_intrinsics(ik, methods); 5317 5318 // Fill in field values obtained by parse_classfile_attributes 5319 if (_parsed_annotations->has_any_annotations()) { 5320 _parsed_annotations->apply_to(ik); 5321 } 5322 5323 apply_parsed_class_attributes(ik); 5324 5325 // Miranda methods 5326 if ((_num_miranda_methods > 0) || 5327 // if this class introduced new miranda methods or 5328 (_super_klass != NULL && _super_klass->has_miranda_methods()) 5329 // super class exists and this class inherited miranda methods 5330 ) { 5331 ik->set_has_miranda_methods(); // then set a flag 5332 } 5333 5334 // Fill in information needed to compute superclasses. 5335 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), CHECK); 5336 5337 // Initialize itable offset tables 5338 klassItable::setup_itable_offset_table(ik); 5339 5340 // Compute transitive closure of interfaces this class implements 5341 // Do final class setup 5342 fill_oop_maps(ik, 5343 _field_info->nonstatic_oop_map_count, 5344 _field_info->nonstatic_oop_offsets, 5345 _field_info->nonstatic_oop_counts); 5346 5347 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper 5348 set_precomputed_flags(ik); 5349 5350 // check if this class can access its super class 5351 check_super_class_access(ik, CHECK); 5352 5353 // check if this class can access its superinterfaces 5354 check_super_interface_access(ik, CHECK); 5355 5356 // check if this class overrides any final method 5357 check_final_method_override(ik, CHECK); 5358 5359 // reject static interface methods prior to Java 8 5360 if (ik->is_interface() && _major_version < JAVA_8_VERSION) { 5361 check_illegal_static_method(ik, CHECK); 5362 } 5363 5364 // Obtain this_klass' module entry 5365 ModuleEntry* module_entry = ik->module(); 5366 assert(module_entry != NULL, "module_entry should always be set"); 5367 5368 // Obtain java.lang.reflect.Module 5369 Handle module_handle(THREAD, JNIHandles::resolve(module_entry->module())); 5370 5371 // Allocate mirror and initialize static fields 5372 // The create_mirror() call will also call compute_modifiers() 5373 java_lang_Class::create_mirror(ik, 5374 Handle(THREAD, _loader_data->class_loader()), 5375 module_handle, 5376 _protection_domain, 5377 CHECK); 5378 5379 assert(_all_mirandas != NULL, "invariant"); 5380 5381 // Generate any default methods - default methods are public interface methods 5382 // that have a default implementation. This is new with Java 8. 5383 if (_has_nonstatic_concrete_methods) { 5384 DefaultMethods::generate_default_methods(ik, 5385 _all_mirandas, 5386 CHECK); 5387 } 5388 5389 // Add read edges to the unnamed modules of the bootstrap and app class loaders. 5390 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() && 5391 !module_entry->has_default_read_edges()) { 5392 if (!module_entry->set_has_default_read_edges()) { 5393 // We won a potential race 5394 JvmtiExport::add_default_read_edges(module_handle, THREAD); 5395 } 5396 } 5397 5398 // Update the loader_data graph. 5399 record_defined_class_dependencies(ik, CHECK); 5400 5401 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */); 5402 5403 if (!is_internal()) { 5404 if (log_is_enabled(Info, class, load)) { 5405 ResourceMark rm; 5406 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string(); 5407 5408 if (log_is_enabled(Info, class, load)) { 5409 ik->print_loading_log(LogLevel::Info, _loader_data, module_name, _stream); 5410 } 5411 // No 'else' here as logging levels are not mutually exclusive 5412 if (log_is_enabled(Debug, class, load)) { 5413 ik->print_loading_log(LogLevel::Debug, _loader_data, module_name, _stream); 5414 } 5415 } 5416 5417 if (log_is_enabled(Debug, class, resolve)) { 5418 ResourceMark rm; 5419 // print out the superclass. 5420 const char * from = ik->external_name(); 5421 if (ik->java_super() != NULL) { 5422 log_debug(class, resolve)("%s %s (super)", 5423 from, 5424 ik->java_super()->external_name()); 5425 } 5426 // print out each of the interface classes referred to by this class. 5427 const Array<Klass*>* const local_interfaces = ik->local_interfaces(); 5428 if (local_interfaces != NULL) { 5429 const int length = local_interfaces->length(); 5430 for (int i = 0; i < length; i++) { 5431 const Klass* const k = local_interfaces->at(i); 5432 const char * to = k->external_name(); 5433 log_debug(class, resolve)("%s %s (interface)", from, to); 5434 } 5435 } 5436 } 5437 } 5438 5439 TRACE_INIT_ID(ik); 5440 5441 // If we reach here, all is well. 5442 // Now remove the InstanceKlass* from the _klass_to_deallocate field 5443 // in order for it to not be destroyed in the ClassFileParser destructor. 5444 set_klass_to_deallocate(NULL); 5445 5446 // it's official 5447 set_klass(ik); 5448 5449 debug_only(ik->verify();) 5450 } 5451 5452 // For an anonymous class that is in the unnamed package, move it to its host class's 5453 // package by prepending its host class's package name to its class name and setting 5454 // its _class_name field. 5455 void ClassFileParser::prepend_host_package_name(const InstanceKlass* host_klass, TRAPS) { 5456 ResourceMark rm(THREAD); 5457 assert(strrchr(_class_name->as_C_string(), '/') == NULL, 5458 "Anonymous class should not be in a package"); 5459 const char* host_pkg_name = 5460 ClassLoader::package_from_name(host_klass->name()->as_C_string(), NULL); 5461 5462 if (host_pkg_name != NULL) { 5463 size_t host_pkg_len = strlen(host_pkg_name); 5464 int class_name_len = _class_name->utf8_length(); 5465 char* new_anon_name = 5466 NEW_RESOURCE_ARRAY(char, host_pkg_len + 1 + class_name_len); 5467 // Copy host package name and trailing /. 5468 strncpy(new_anon_name, host_pkg_name, host_pkg_len); 5469 new_anon_name[host_pkg_len] = '/'; 5470 // Append anonymous class name. The anonymous class name can contain odd 5471 // characters. So, do a strncpy instead of using sprintf("%s..."). 5472 strncpy(new_anon_name + host_pkg_len + 1, (char *)_class_name->base(), class_name_len); 5473 5474 // Create a symbol and update the anonymous class name. 5475 _class_name = SymbolTable::new_symbol(new_anon_name, 5476 (int)host_pkg_len + 1 + class_name_len, 5477 CHECK); 5478 } 5479 } 5480 5481 // If the host class and the anonymous class are in the same package then do 5482 // nothing. If the anonymous class is in the unnamed package then move it to its 5483 // host's package. If the classes are in different packages then throw an IAE 5484 // exception. 5485 void ClassFileParser::fix_anonymous_class_name(TRAPS) { 5486 assert(_host_klass != NULL, "Expected an anonymous class"); 5487 5488 const jbyte* anon_last_slash = UTF8::strrchr(_class_name->base(), 5489 _class_name->utf8_length(), '/'); 5490 if (anon_last_slash == NULL) { // Unnamed package 5491 prepend_host_package_name(_host_klass, CHECK); 5492 } else { 5493 if (!InstanceKlass::is_same_class_package(_host_klass->class_loader(), 5494 _host_klass->name(), 5495 _host_klass->class_loader(), 5496 _class_name)) { 5497 ResourceMark rm(THREAD); 5498 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 5499 err_msg("Host class %s and anonymous class %s are in different packages", 5500 _host_klass->name()->as_C_string(), _class_name->as_C_string())); 5501 } 5502 } 5503 } 5504 5505 static bool relax_format_check_for(ClassLoaderData* loader_data) { 5506 bool trusted = (loader_data->is_the_null_class_loader_data() || 5507 SystemDictionary::is_platform_class_loader(loader_data->class_loader())); 5508 bool need_verify = 5509 // verifyAll 5510 (BytecodeVerificationLocal && BytecodeVerificationRemote) || 5511 // verifyRemote 5512 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted); 5513 return !need_verify; 5514 } 5515 5516 ClassFileParser::ClassFileParser(ClassFileStream* stream, 5517 Symbol* name, 5518 ClassLoaderData* loader_data, 5519 Handle protection_domain, 5520 const InstanceKlass* host_klass, 5521 GrowableArray<Handle>* cp_patches, 5522 Publicity pub_level, 5523 TRAPS) : 5524 _stream(stream), 5525 _requested_name(name), 5526 _loader_data(loader_data), 5527 _host_klass(host_klass), 5528 _cp_patches(cp_patches), 5529 _super_klass(), 5530 _cp(NULL), 5531 _fields(NULL), 5532 _methods(NULL), 5533 _inner_classes(NULL), 5534 _local_interfaces(NULL), 5535 _transitive_interfaces(NULL), 5536 _combined_annotations(NULL), 5537 _annotations(NULL), 5538 _type_annotations(NULL), 5539 _fields_annotations(NULL), 5540 _fields_type_annotations(NULL), 5541 _klass(NULL), 5542 _klass_to_deallocate(NULL), 5543 _parsed_annotations(NULL), 5544 _fac(NULL), 5545 _field_info(NULL), 5546 _method_ordering(NULL), 5547 _all_mirandas(NULL), 5548 _vtable_size(0), 5549 _itable_size(0), 5550 _num_miranda_methods(0), 5551 _rt(REF_NONE), 5552 _protection_domain(protection_domain), 5553 _access_flags(), 5554 _pub_level(pub_level), 5555 _synthetic_flag(false), 5556 _sde_length(false), 5557 _sde_buffer(NULL), 5558 _sourcefile_index(0), 5559 _generic_signature_index(0), 5560 _major_version(0), 5561 _minor_version(0), 5562 _this_class_index(0), 5563 _super_class_index(0), 5564 _itfs_len(0), 5565 _java_fields_count(0), 5566 _need_verify(false), 5567 _relax_verify(false), 5568 _has_nonstatic_concrete_methods(false), 5569 _declares_nonstatic_concrete_methods(false), 5570 _has_final_method(false), 5571 _has_finalizer(false), 5572 _has_empty_finalizer(false), 5573 _has_vanilla_constructor(false), 5574 _max_bootstrap_specifier_index(-1) { 5575 5576 _class_name = name != NULL ? name : vmSymbols::unknown_class_name(); 5577 5578 assert(THREAD->is_Java_thread(), "invariant"); 5579 assert(_loader_data != NULL, "invariant"); 5580 assert(stream != NULL, "invariant"); 5581 assert(_stream != NULL, "invariant"); 5582 assert(_stream->buffer() == _stream->current(), "invariant"); 5583 assert(_class_name != NULL, "invariant"); 5584 assert(0 == _access_flags.as_int(), "invariant"); 5585 5586 // Figure out whether we can skip format checking (matching classic VM behavior) 5587 if (DumpSharedSpaces) { 5588 // verify == true means it's a 'remote' class (i.e., non-boot class) 5589 // Verification decision is based on BytecodeVerificationRemote flag 5590 // for those classes. 5591 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote : 5592 BytecodeVerificationLocal; 5593 } 5594 else { 5595 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(), 5596 stream->need_verify()); 5597 } 5598 5599 // synch back verification state to stream 5600 stream->set_verify(_need_verify); 5601 5602 // Check if verification needs to be relaxed for this class file 5603 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) 5604 _relax_verify = relax_format_check_for(_loader_data); 5605 5606 parse_stream(stream, CHECK); 5607 5608 post_process_parsed_stream(stream, _cp, CHECK); 5609 } 5610 5611 void ClassFileParser::clear_class_metadata() { 5612 // metadata created before the instance klass is created. Must be 5613 // deallocated if classfile parsing returns an error. 5614 _cp = NULL; 5615 _fields = NULL; 5616 _methods = NULL; 5617 _inner_classes = NULL; 5618 _local_interfaces = NULL; 5619 _transitive_interfaces = NULL; 5620 _combined_annotations = NULL; 5621 _annotations = _type_annotations = NULL; 5622 _fields_annotations = _fields_type_annotations = NULL; 5623 } 5624 5625 // Destructor to clean up 5626 ClassFileParser::~ClassFileParser() { 5627 if (_cp != NULL) { 5628 MetadataFactory::free_metadata(_loader_data, _cp); 5629 } 5630 if (_fields != NULL) { 5631 MetadataFactory::free_array<u2>(_loader_data, _fields); 5632 } 5633 5634 if (_methods != NULL) { 5635 // Free methods 5636 InstanceKlass::deallocate_methods(_loader_data, _methods); 5637 } 5638 5639 // beware of the Universe::empty_blah_array!! 5640 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) { 5641 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 5642 } 5643 5644 // Free interfaces 5645 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass, 5646 _local_interfaces, _transitive_interfaces); 5647 5648 if (_combined_annotations != NULL) { 5649 // After all annotations arrays have been created, they are installed into the 5650 // Annotations object that will be assigned to the InstanceKlass being created. 5651 5652 // Deallocate the Annotations object and the installed annotations arrays. 5653 _combined_annotations->deallocate_contents(_loader_data); 5654 5655 // If the _combined_annotations pointer is non-NULL, 5656 // then the other annotations fields should have been cleared. 5657 assert(_annotations == NULL, "Should have been cleared"); 5658 assert(_type_annotations == NULL, "Should have been cleared"); 5659 assert(_fields_annotations == NULL, "Should have been cleared"); 5660 assert(_fields_type_annotations == NULL, "Should have been cleared"); 5661 } else { 5662 // If the annotations arrays were not installed into the Annotations object, 5663 // then they have to be deallocated explicitly. 5664 MetadataFactory::free_array<u1>(_loader_data, _annotations); 5665 MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 5666 Annotations::free_contents(_loader_data, _fields_annotations); 5667 Annotations::free_contents(_loader_data, _fields_type_annotations); 5668 } 5669 5670 clear_class_metadata(); 5671 5672 // deallocate the klass if already created. Don't directly deallocate, but add 5673 // to the deallocate list so that the klass is removed from the CLD::_klasses list 5674 // at a safepoint. 5675 if (_klass_to_deallocate != NULL) { 5676 _loader_data->add_to_deallocate_list(_klass_to_deallocate); 5677 } 5678 } 5679 5680 void ClassFileParser::parse_stream(const ClassFileStream* const stream, 5681 TRAPS) { 5682 5683 assert(stream != NULL, "invariant"); 5684 assert(_class_name != NULL, "invariant"); 5685 5686 // BEGIN STREAM PARSING 5687 stream->guarantee_more(8, CHECK); // magic, major, minor 5688 // Magic value 5689 const u4 magic = stream->get_u4_fast(); 5690 guarantee_property(magic == JAVA_CLASSFILE_MAGIC, 5691 "Incompatible magic value %u in class file %s", 5692 magic, CHECK); 5693 5694 // Version numbers 5695 _minor_version = stream->get_u2_fast(); 5696 _major_version = stream->get_u2_fast(); 5697 5698 if (DumpSharedSpaces && _major_version < JAVA_1_5_VERSION) { 5699 ResourceMark rm; 5700 warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s", 5701 _major_version, _minor_version, _class_name->as_C_string()); 5702 Exceptions::fthrow( 5703 THREAD_AND_LOCATION, 5704 vmSymbols::java_lang_UnsupportedClassVersionError(), 5705 "Unsupported major.minor version for dump time %u.%u", 5706 _major_version, 5707 _minor_version); 5708 } 5709 5710 // Check version numbers - we check this even with verifier off 5711 if (!is_supported_version(_major_version, _minor_version)) { 5712 ResourceMark rm(THREAD); 5713 Exceptions::fthrow( 5714 THREAD_AND_LOCATION, 5715 vmSymbols::java_lang_UnsupportedClassVersionError(), 5716 "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), " 5717 "this version of the Java Runtime only recognizes class file versions up to %u.%u", 5718 _class_name->as_C_string(), 5719 _major_version, 5720 _minor_version, 5721 JAVA_MAX_SUPPORTED_VERSION, 5722 JAVA_MAX_SUPPORTED_MINOR_VERSION); 5723 return; 5724 } 5725 5726 stream->guarantee_more(3, CHECK); // length, first cp tag 5727 const u2 cp_size = stream->get_u2_fast(); 5728 5729 guarantee_property( 5730 cp_size >= 1, "Illegal constant pool size %u in class file %s", 5731 cp_size, CHECK); 5732 5733 _cp = ConstantPool::allocate(_loader_data, 5734 cp_size, 5735 CHECK); 5736 5737 ConstantPool* const cp = _cp; 5738 5739 parse_constant_pool(stream, cp, cp_size, CHECK); 5740 5741 assert(cp_size == (const u2)cp->length(), "invariant"); 5742 5743 // ACCESS FLAGS 5744 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 5745 5746 // Access flags 5747 jint flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS; 5748 5749 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 5750 // Set abstract bit for old class files for backward compatibility 5751 flags |= JVM_ACC_ABSTRACT; 5752 } 5753 5754 _access_flags.set_flags(flags); 5755 5756 verify_legal_class_modifiers((jint)_access_flags.as_int(), CHECK); 5757 5758 // This class and superclass 5759 _this_class_index = stream->get_u2_fast(); 5760 check_property( 5761 valid_cp_range(_this_class_index, cp_size) && 5762 cp->tag_at(_this_class_index).is_unresolved_klass(), 5763 "Invalid this class index %u in constant pool in class file %s", 5764 _this_class_index, CHECK); 5765 5766 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index); 5767 assert(class_name_in_cp != NULL, "class_name can't be null"); 5768 5769 // Update _class_name which could be null previously 5770 // to reflect the name in the constant pool 5771 _class_name = class_name_in_cp; 5772 5773 // Don't need to check whether this class name is legal or not. 5774 // It has been checked when constant pool is parsed. 5775 // However, make sure it is not an array type. 5776 if (_need_verify) { 5777 guarantee_property(_class_name->byte_at(0) != JVM_SIGNATURE_ARRAY, 5778 "Bad class name in class file %s", 5779 CHECK); 5780 } 5781 5782 // Checks if name in class file matches requested name 5783 if (_requested_name != NULL && _requested_name != _class_name) { 5784 ResourceMark rm(THREAD); 5785 Exceptions::fthrow( 5786 THREAD_AND_LOCATION, 5787 vmSymbols::java_lang_NoClassDefFoundError(), 5788 "%s (wrong name: %s)", 5789 _class_name->as_C_string(), 5790 _requested_name != NULL ? _requested_name->as_C_string() : "NoName" 5791 ); 5792 return; 5793 } 5794 5795 // if this is an anonymous class fix up its name if it's in the unnamed 5796 // package. Otherwise, throw IAE if it is in a different package than 5797 // its host class. 5798 if (_host_klass != NULL) { 5799 fix_anonymous_class_name(CHECK); 5800 } 5801 5802 // Verification prevents us from creating names with dots in them, this 5803 // asserts that that's the case. 5804 assert(is_internal_format(_class_name), "external class name format used internally"); 5805 5806 if (!is_internal()) { 5807 if (log_is_enabled(Debug, class, preorder)){ 5808 ResourceMark rm(THREAD); 5809 outputStream* log = Log(class, preorder)::debug_stream(); 5810 log->print("%s", _class_name->as_klass_external_name()); 5811 if (stream->source() != NULL) { 5812 log->print(" source: %s", stream->source()); 5813 } 5814 log->cr(); 5815 } 5816 5817 #if INCLUDE_CDS 5818 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) { 5819 // Only dump the classes that can be stored into CDS archive. 5820 // Anonymous classes such as generated LambdaForm classes are also not included. 5821 if (SystemDictionaryShared::is_sharing_possible(_loader_data) && 5822 _host_klass == NULL) { 5823 oop class_loader = _loader_data->class_loader(); 5824 ResourceMark rm(THREAD); 5825 // For the boot and platform class loaders, check if the class is not found in the 5826 // java runtime image. Additional check for the boot class loader is if the class 5827 // is not found in the boot loader's appended entries. This indicates that the class 5828 // is not useable during run time, such as the ones found in the --patch-module entries, 5829 // so it should not be included in the classlist file. 5830 if (((class_loader == NULL && !ClassLoader::contains_append_entry(stream->source())) || 5831 SystemDictionary::is_platform_class_loader(class_loader)) && 5832 !ClassLoader::is_jrt(stream->source())) { 5833 tty->print_cr("skip writing class %s from source %s to classlist file", 5834 _class_name->as_C_string(), stream->source()); 5835 } else { 5836 classlist_file->print_cr("%s", _class_name->as_C_string()); 5837 classlist_file->flush(); 5838 } 5839 } 5840 } 5841 #endif 5842 } 5843 5844 // SUPERKLASS 5845 _super_class_index = stream->get_u2_fast(); 5846 _super_klass = parse_super_class(cp, 5847 _super_class_index, 5848 _need_verify, 5849 CHECK); 5850 5851 // Interfaces 5852 _itfs_len = stream->get_u2_fast(); 5853 parse_interfaces(stream, 5854 _itfs_len, 5855 cp, 5856 &_has_nonstatic_concrete_methods, 5857 CHECK); 5858 5859 assert(_local_interfaces != NULL, "invariant"); 5860 5861 // Fields (offsets are filled in later) 5862 _fac = new FieldAllocationCount(); 5863 parse_fields(stream, 5864 _access_flags.is_interface(), 5865 _fac, 5866 cp, 5867 cp_size, 5868 &_java_fields_count, 5869 CHECK); 5870 5871 assert(_fields != NULL, "invariant"); 5872 5873 // Methods 5874 AccessFlags promoted_flags; 5875 parse_methods(stream, 5876 _access_flags.is_interface(), 5877 &promoted_flags, 5878 &_has_final_method, 5879 &_declares_nonstatic_concrete_methods, 5880 CHECK); 5881 5882 assert(_methods != NULL, "invariant"); 5883 5884 // promote flags from parse_methods() to the klass' flags 5885 _access_flags.add_promoted_flags(promoted_flags.as_int()); 5886 5887 if (_declares_nonstatic_concrete_methods) { 5888 _has_nonstatic_concrete_methods = true; 5889 } 5890 5891 // Additional attributes/annotations 5892 _parsed_annotations = new ClassAnnotationCollector(); 5893 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK); 5894 5895 assert(_inner_classes != NULL, "invariant"); 5896 5897 // Finalize the Annotations metadata object, 5898 // now that all annotation arrays have been created. 5899 create_combined_annotations(CHECK); 5900 5901 // Make sure this is the end of class file stream 5902 guarantee_property(stream->at_eos(), 5903 "Extra bytes at the end of class file %s", 5904 CHECK); 5905 5906 // all bytes in stream read and parsed 5907 } 5908 5909 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream, 5910 ConstantPool* cp, 5911 TRAPS) { 5912 assert(stream != NULL, "invariant"); 5913 assert(stream->at_eos(), "invariant"); 5914 assert(cp != NULL, "invariant"); 5915 assert(_loader_data != NULL, "invariant"); 5916 5917 if (_class_name == vmSymbols::java_lang_Object()) { 5918 check_property(_local_interfaces == Universe::the_empty_klass_array(), 5919 "java.lang.Object cannot implement an interface in class file %s", 5920 CHECK); 5921 } 5922 // We check super class after class file is parsed and format is checked 5923 if (_super_class_index > 0 && NULL ==_super_klass) { 5924 Symbol* const super_class_name = cp->klass_name_at(_super_class_index); 5925 if (_access_flags.is_interface()) { 5926 // Before attempting to resolve the superclass, check for class format 5927 // errors not checked yet. 5928 guarantee_property(super_class_name == vmSymbols::java_lang_Object(), 5929 "Interfaces must have java.lang.Object as superclass in class file %s", 5930 CHECK); 5931 } 5932 Handle loader(THREAD, _loader_data->class_loader()); 5933 _super_klass = (const InstanceKlass*) 5934 SystemDictionary::resolve_super_or_fail(_class_name, 5935 super_class_name, 5936 loader, 5937 _protection_domain, 5938 true, 5939 CHECK); 5940 } 5941 5942 if (_super_klass != NULL) { 5943 if (_super_klass->has_nonstatic_concrete_methods()) { 5944 _has_nonstatic_concrete_methods = true; 5945 } 5946 5947 if (_super_klass->is_interface()) { 5948 ResourceMark rm(THREAD); 5949 Exceptions::fthrow( 5950 THREAD_AND_LOCATION, 5951 vmSymbols::java_lang_IncompatibleClassChangeError(), 5952 "class %s has interface %s as super class", 5953 _class_name->as_klass_external_name(), 5954 _super_klass->external_name() 5955 ); 5956 return; 5957 } 5958 // Make sure super class is not final 5959 if (_super_klass->is_final()) { 5960 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class"); 5961 } 5962 } 5963 5964 // Compute the transitive list of all unique interfaces implemented by this class 5965 _transitive_interfaces = 5966 compute_transitive_interfaces(_super_klass, 5967 _local_interfaces, 5968 _loader_data, 5969 CHECK); 5970 5971 assert(_transitive_interfaces != NULL, "invariant"); 5972 5973 // sort methods 5974 _method_ordering = sort_methods(_methods); 5975 5976 _all_mirandas = new GrowableArray<Method*>(20); 5977 5978 Handle loader(THREAD, _loader_data->class_loader()); 5979 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size, 5980 &_num_miranda_methods, 5981 _all_mirandas, 5982 _super_klass, 5983 _methods, 5984 _access_flags, 5985 _major_version, 5986 loader, 5987 _class_name, 5988 _local_interfaces, 5989 CHECK); 5990 5991 // Size of Java itable (in words) 5992 _itable_size = _access_flags.is_interface() ? 0 : 5993 klassItable::compute_itable_size(_transitive_interfaces); 5994 5995 assert(_fac != NULL, "invariant"); 5996 assert(_parsed_annotations != NULL, "invariant"); 5997 5998 _field_info = new FieldLayoutInfo(); 5999 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK); 6000 6001 // Compute reference typ 6002 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type(); 6003 6004 } 6005 6006 void ClassFileParser::set_klass(InstanceKlass* klass) { 6007 6008 #ifdef ASSERT 6009 if (klass != NULL) { 6010 assert(NULL == _klass, "leaking?"); 6011 } 6012 #endif 6013 6014 _klass = klass; 6015 } 6016 6017 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) { 6018 6019 #ifdef ASSERT 6020 if (klass != NULL) { 6021 assert(NULL == _klass_to_deallocate, "leaking?"); 6022 } 6023 #endif 6024 6025 _klass_to_deallocate = klass; 6026 } 6027 6028 // Caller responsible for ResourceMark 6029 // clone stream with rewound position 6030 const ClassFileStream* ClassFileParser::clone_stream() const { 6031 assert(_stream != NULL, "invariant"); 6032 6033 return _stream->clone(); 6034 } 6035 // ---------------------------------------------------------------------------- 6036 // debugging 6037 6038 #ifdef ASSERT 6039 6040 // return true if class_name contains no '.' (internal format is '/') 6041 bool ClassFileParser::is_internal_format(Symbol* class_name) { 6042 if (class_name != NULL) { 6043 ResourceMark rm; 6044 char* name = class_name->as_C_string(); 6045 return strchr(name, '.') == NULL; 6046 } else { 6047 return true; 6048 } 6049 } 6050 6051 #endif --- EOF ---