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 KlassHandle 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 = KlassHandle(THREAD, 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 const Klass* const k = 835 SystemDictionary::resolve_super_or_fail(_class_name, 836 unresolved_klass, 837 _loader_data->class_loader(), 838 _protection_domain, 839 false, 840 CHECK); 841 interf = KlassHandle(THREAD, k); 842 } 843 844 if (!interf()->is_interface()) { 845 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 846 "Implementing class"); 847 } 848 849 if (InstanceKlass::cast(interf())->has_nonstatic_concrete_methods()) { 850 *has_nonstatic_concrete_methods = true; 851 } 852 _local_interfaces->at_put(index, interf()); 853 } 854 855 if (!_need_verify || itfs_len <= 1) { 856 return; 857 } 858 859 // Check if there's any duplicates in interfaces 860 ResourceMark rm(THREAD); 861 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 862 NameSigHash*, 863 HASH_ROW_SIZE); 864 initialize_hashtable(interface_names); 865 bool dup = false; 866 { 867 debug_only(NoSafepointVerifier nsv;) 868 for (index = 0; index < itfs_len; index++) { 869 const Klass* const k = _local_interfaces->at(index); 870 const Symbol* const 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 in class file %s", CHECK); 880 } 881 } 882 } 883 884 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp, 885 int constantvalue_index, 886 int signature_index, 887 TRAPS) const { 888 // Make sure the constant pool entry is of a type appropriate to this field 889 guarantee_property( 890 (constantvalue_index > 0 && 891 constantvalue_index < cp->length()), 892 "Bad initial value index %u in ConstantValue attribute in class file %s", 893 constantvalue_index, CHECK); 894 895 const constantTag value_type = cp->tag_at(constantvalue_index); 896 switch(cp->basic_type_for_signature_at(signature_index)) { 897 case T_LONG: { 898 guarantee_property(value_type.is_long(), 899 "Inconsistent constant value type in class file %s", 900 CHECK); 901 break; 902 } 903 case T_FLOAT: { 904 guarantee_property(value_type.is_float(), 905 "Inconsistent constant value type in class file %s", 906 CHECK); 907 break; 908 } 909 case T_DOUBLE: { 910 guarantee_property(value_type.is_double(), 911 "Inconsistent constant value type in class file %s", 912 CHECK); 913 break; 914 } 915 case T_BYTE: 916 case T_CHAR: 917 case T_SHORT: 918 case T_BOOLEAN: 919 case T_INT: { 920 guarantee_property(value_type.is_int(), 921 "Inconsistent constant value type in class file %s", 922 CHECK); 923 break; 924 } 925 case T_OBJECT: { 926 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") 927 && value_type.is_string()), 928 "Bad string initial value in class file %s", 929 CHECK); 930 break; 931 } 932 default: { 933 classfile_parse_error("Unable to set initial value %u in class file %s", 934 constantvalue_index, 935 CHECK); 936 } 937 } 938 } 939 940 class AnnotationCollector : public ResourceObj{ 941 public: 942 enum Location { _in_field, _in_method, _in_class }; 943 enum ID { 944 _unknown = 0, 945 _method_CallerSensitive, 946 _method_ForceInline, 947 _method_DontInline, 948 _method_InjectedProfile, 949 _method_LambdaForm_Compiled, 950 _method_LambdaForm_Hidden, 951 _method_HotSpotIntrinsicCandidate, 952 _jdk_internal_vm_annotation_Contended, 953 _field_Stable, 954 _jdk_internal_vm_annotation_ReservedStackAccess, 955 _annotation_LIMIT 956 }; 957 const Location _location; 958 int _annotations_present; 959 u2 _contended_group; 960 961 AnnotationCollector(Location location) 962 : _location(location), _annotations_present(0) 963 { 964 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, ""); 965 } 966 // If this annotation name has an ID, report it (or _none). 967 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name); 968 // Set the annotation name: 969 void set_annotation(ID id) { 970 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 971 _annotations_present |= nth_bit((int)id); 972 } 973 974 void remove_annotation(ID id) { 975 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 976 _annotations_present &= ~nth_bit((int)id); 977 } 978 979 // Report if the annotation is present. 980 bool has_any_annotations() const { return _annotations_present != 0; } 981 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; } 982 983 void set_contended_group(u2 group) { _contended_group = group; } 984 u2 contended_group() const { return _contended_group; } 985 986 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); } 987 988 void set_stable(bool stable) { set_annotation(_field_Stable); } 989 bool is_stable() const { return has_annotation(_field_Stable); } 990 }; 991 992 // This class also doubles as a holder for metadata cleanup. 993 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector { 994 private: 995 ClassLoaderData* _loader_data; 996 AnnotationArray* _field_annotations; 997 AnnotationArray* _field_type_annotations; 998 public: 999 FieldAnnotationCollector(ClassLoaderData* loader_data) : 1000 AnnotationCollector(_in_field), 1001 _loader_data(loader_data), 1002 _field_annotations(NULL), 1003 _field_type_annotations(NULL) {} 1004 ~FieldAnnotationCollector(); 1005 void apply_to(FieldInfo* f); 1006 AnnotationArray* field_annotations() { return _field_annotations; } 1007 AnnotationArray* field_type_annotations() { return _field_type_annotations; } 1008 1009 void set_field_annotations(AnnotationArray* a) { _field_annotations = a; } 1010 void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; } 1011 }; 1012 1013 class MethodAnnotationCollector : public AnnotationCollector{ 1014 public: 1015 MethodAnnotationCollector() : AnnotationCollector(_in_method) { } 1016 void apply_to(methodHandle m); 1017 }; 1018 1019 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{ 1020 public: 1021 ClassAnnotationCollector() : AnnotationCollector(_in_class) { } 1022 void apply_to(InstanceKlass* ik); 1023 }; 1024 1025 1026 static int skip_annotation_value(const u1*, int, int); // fwd decl 1027 1028 // Safely increment index by val if does not pass limit 1029 #define SAFE_ADD(index, limit, val) \ 1030 if (index >= limit - val) return limit; \ 1031 index += val; 1032 1033 // Skip an annotation. Return >=limit if there is any problem. 1034 static int skip_annotation(const u1* buffer, int limit, int index) { 1035 assert(buffer != NULL, "invariant"); 1036 // annotation := atype:u2 do(nmem:u2) {member:u2 value} 1037 // value := switch (tag:u1) { ... } 1038 SAFE_ADD(index, limit, 4); // skip atype and read nmem 1039 int nmem = Bytes::get_Java_u2((address)buffer + index - 2); 1040 while (--nmem >= 0 && index < limit) { 1041 SAFE_ADD(index, limit, 2); // skip member 1042 index = skip_annotation_value(buffer, limit, index); 1043 } 1044 return index; 1045 } 1046 1047 // Skip an annotation value. Return >=limit if there is any problem. 1048 static int skip_annotation_value(const u1* buffer, int limit, int index) { 1049 assert(buffer != NULL, "invariant"); 1050 1051 // value := switch (tag:u1) { 1052 // case B, C, I, S, Z, D, F, J, c: con:u2; 1053 // case e: e_class:u2 e_name:u2; 1054 // case s: s_con:u2; 1055 // case [: do(nval:u2) {value}; 1056 // case @: annotation; 1057 // case s: s_con:u2; 1058 // } 1059 SAFE_ADD(index, limit, 1); // read tag 1060 const u1 tag = buffer[index - 1]; 1061 switch (tag) { 1062 case 'B': 1063 case 'C': 1064 case 'I': 1065 case 'S': 1066 case 'Z': 1067 case 'D': 1068 case 'F': 1069 case 'J': 1070 case 'c': 1071 case 's': 1072 SAFE_ADD(index, limit, 2); // skip con or s_con 1073 break; 1074 case 'e': 1075 SAFE_ADD(index, limit, 4); // skip e_class, e_name 1076 break; 1077 case '[': 1078 { 1079 SAFE_ADD(index, limit, 2); // read nval 1080 int nval = Bytes::get_Java_u2((address)buffer + index - 2); 1081 while (--nval >= 0 && index < limit) { 1082 index = skip_annotation_value(buffer, limit, index); 1083 } 1084 } 1085 break; 1086 case '@': 1087 index = skip_annotation(buffer, limit, index); 1088 break; 1089 default: 1090 return limit; // bad tag byte 1091 } 1092 return index; 1093 } 1094 1095 // Sift through annotations, looking for those significant to the VM: 1096 static void parse_annotations(const ConstantPool* const cp, 1097 const u1* buffer, int limit, 1098 AnnotationCollector* coll, 1099 ClassLoaderData* loader_data, 1100 TRAPS) { 1101 1102 assert(cp != NULL, "invariant"); 1103 assert(buffer != NULL, "invariant"); 1104 assert(coll != NULL, "invariant"); 1105 assert(loader_data != NULL, "invariant"); 1106 1107 // annotations := do(nann:u2) {annotation} 1108 int index = 2; // read nann 1109 if (index >= limit) return; 1110 int nann = Bytes::get_Java_u2((address)buffer + index - 2); 1111 enum { // initial annotation layout 1112 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' 1113 count_off = 2, // u2 such as 1 (one value) 1114 member_off = 4, // utf8 such as 'value' 1115 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) 1116 e_tag_val = 'e', 1117 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' 1118 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' 1119 e_size = 11, // end of 'e' annotation 1120 c_tag_val = 'c', // payload is type 1121 c_con_off = 7, // utf8 payload, such as 'I' 1122 c_size = 9, // end of 'c' annotation 1123 s_tag_val = 's', // payload is String 1124 s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' 1125 s_size = 9, 1126 min_size = 6 // smallest possible size (zero members) 1127 }; 1128 // Cannot add min_size to index in case of overflow MAX_INT 1129 while ((--nann) >= 0 && (index - 2 <= limit - min_size)) { 1130 int index0 = index; 1131 index = skip_annotation(buffer, limit, index); 1132 const u1* const abase = buffer + index0; 1133 const int atype = Bytes::get_Java_u2((address)abase + atype_off); 1134 const int count = Bytes::get_Java_u2((address)abase + count_off); 1135 const Symbol* const aname = check_symbol_at(cp, atype); 1136 if (aname == NULL) break; // invalid annotation name 1137 const Symbol* member = NULL; 1138 if (count >= 1) { 1139 const int member_index = Bytes::get_Java_u2((address)abase + member_off); 1140 member = check_symbol_at(cp, member_index); 1141 if (member == NULL) break; // invalid member name 1142 } 1143 1144 // Here is where parsing particular annotations will take place. 1145 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname); 1146 if (AnnotationCollector::_unknown == id) continue; 1147 coll->set_annotation(id); 1148 1149 if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) { 1150 // @Contended can optionally specify the contention group. 1151 // 1152 // Contended group defines the equivalence class over the fields: 1153 // the fields within the same contended group are not treated distinct. 1154 // The only exception is default group, which does not incur the 1155 // equivalence. Naturally, contention group for classes is meaningless. 1156 // 1157 // While the contention group is specified as String, annotation 1158 // values are already interned, and we might as well use the constant 1159 // pool index as the group tag. 1160 // 1161 u2 group_index = 0; // default contended group 1162 if (count == 1 1163 && s_size == (index - index0) // match size 1164 && s_tag_val == *(abase + tag_off) 1165 && member == vmSymbols::value_name()) { 1166 group_index = Bytes::get_Java_u2((address)abase + s_con_off); 1167 if (cp->symbol_at(group_index)->utf8_length() == 0) { 1168 group_index = 0; // default contended group 1169 } 1170 } 1171 coll->set_contended_group(group_index); 1172 } 1173 } 1174 } 1175 1176 1177 // Parse attributes for a field. 1178 void ClassFileParser::parse_field_attributes(const ClassFileStream* const cfs, 1179 u2 attributes_count, 1180 bool is_static, u2 signature_index, 1181 u2* const constantvalue_index_addr, 1182 bool* const is_synthetic_addr, 1183 u2* const generic_signature_index_addr, 1184 ClassFileParser::FieldAnnotationCollector* parsed_annotations, 1185 TRAPS) { 1186 assert(cfs != NULL, "invariant"); 1187 assert(constantvalue_index_addr != NULL, "invariant"); 1188 assert(is_synthetic_addr != NULL, "invariant"); 1189 assert(generic_signature_index_addr != NULL, "invariant"); 1190 assert(parsed_annotations != NULL, "invariant"); 1191 assert(attributes_count > 0, "attributes_count should be greater than 0"); 1192 1193 u2 constantvalue_index = 0; 1194 u2 generic_signature_index = 0; 1195 bool is_synthetic = false; 1196 const u1* runtime_visible_annotations = NULL; 1197 int runtime_visible_annotations_length = 0; 1198 const u1* runtime_invisible_annotations = NULL; 1199 int runtime_invisible_annotations_length = 0; 1200 const u1* runtime_visible_type_annotations = NULL; 1201 int runtime_visible_type_annotations_length = 0; 1202 const u1* runtime_invisible_type_annotations = NULL; 1203 int runtime_invisible_type_annotations_length = 0; 1204 bool runtime_invisible_annotations_exists = false; 1205 bool runtime_invisible_type_annotations_exists = false; 1206 const ConstantPool* const cp = _cp; 1207 1208 while (attributes_count--) { 1209 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 1210 const u2 attribute_name_index = cfs->get_u2_fast(); 1211 const u4 attribute_length = cfs->get_u4_fast(); 1212 check_property(valid_symbol_at(attribute_name_index), 1213 "Invalid field attribute index %u in class file %s", 1214 attribute_name_index, 1215 CHECK); 1216 1217 const Symbol* const attribute_name = cp->symbol_at(attribute_name_index); 1218 if (is_static && attribute_name == vmSymbols::tag_constant_value()) { 1219 // ignore if non-static 1220 if (constantvalue_index != 0) { 1221 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK); 1222 } 1223 check_property( 1224 attribute_length == 2, 1225 "Invalid ConstantValue field attribute length %u in class file %s", 1226 attribute_length, CHECK); 1227 1228 constantvalue_index = cfs->get_u2(CHECK); 1229 if (_need_verify) { 1230 verify_constantvalue(cp, constantvalue_index, signature_index, CHECK); 1231 } 1232 } else if (attribute_name == vmSymbols::tag_synthetic()) { 1233 if (attribute_length != 0) { 1234 classfile_parse_error( 1235 "Invalid Synthetic field attribute length %u in class file %s", 1236 attribute_length, CHECK); 1237 } 1238 is_synthetic = true; 1239 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120 1240 if (attribute_length != 0) { 1241 classfile_parse_error( 1242 "Invalid Deprecated field attribute length %u in class file %s", 1243 attribute_length, CHECK); 1244 } 1245 } else if (_major_version >= JAVA_1_5_VERSION) { 1246 if (attribute_name == vmSymbols::tag_signature()) { 1247 if (attribute_length != 2) { 1248 classfile_parse_error( 1249 "Wrong size %u for field's Signature attribute in class file %s", 1250 attribute_length, CHECK); 1251 } 1252 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK); 1253 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 1254 if (runtime_visible_annotations != NULL) { 1255 classfile_parse_error( 1256 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK); 1257 } 1258 runtime_visible_annotations_length = attribute_length; 1259 runtime_visible_annotations = cfs->get_u1_buffer(); 1260 assert(runtime_visible_annotations != NULL, "null visible annotations"); 1261 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 1262 parse_annotations(cp, 1263 runtime_visible_annotations, 1264 runtime_visible_annotations_length, 1265 parsed_annotations, 1266 _loader_data, 1267 CHECK); 1268 cfs->skip_u1_fast(runtime_visible_annotations_length); 1269 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 1270 if (runtime_invisible_annotations_exists) { 1271 classfile_parse_error( 1272 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK); 1273 } 1274 runtime_invisible_annotations_exists = true; 1275 if (PreserveAllAnnotations) { 1276 runtime_invisible_annotations_length = attribute_length; 1277 runtime_invisible_annotations = cfs->get_u1_buffer(); 1278 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 1279 } 1280 cfs->skip_u1(attribute_length, CHECK); 1281 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 1282 if (runtime_visible_type_annotations != NULL) { 1283 classfile_parse_error( 1284 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK); 1285 } 1286 runtime_visible_type_annotations_length = attribute_length; 1287 runtime_visible_type_annotations = cfs->get_u1_buffer(); 1288 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 1289 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 1290 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 1291 if (runtime_invisible_type_annotations_exists) { 1292 classfile_parse_error( 1293 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", CHECK); 1294 } else { 1295 runtime_invisible_type_annotations_exists = true; 1296 } 1297 if (PreserveAllAnnotations) { 1298 runtime_invisible_type_annotations_length = attribute_length; 1299 runtime_invisible_type_annotations = cfs->get_u1_buffer(); 1300 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 1301 } 1302 cfs->skip_u1(attribute_length, CHECK); 1303 } else { 1304 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1305 } 1306 } else { 1307 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1308 } 1309 } 1310 1311 *constantvalue_index_addr = constantvalue_index; 1312 *is_synthetic_addr = is_synthetic; 1313 *generic_signature_index_addr = generic_signature_index; 1314 AnnotationArray* a = assemble_annotations(runtime_visible_annotations, 1315 runtime_visible_annotations_length, 1316 runtime_invisible_annotations, 1317 runtime_invisible_annotations_length, 1318 CHECK); 1319 parsed_annotations->set_field_annotations(a); 1320 a = assemble_annotations(runtime_visible_type_annotations, 1321 runtime_visible_type_annotations_length, 1322 runtime_invisible_type_annotations, 1323 runtime_invisible_type_annotations_length, 1324 CHECK); 1325 parsed_annotations->set_field_type_annotations(a); 1326 return; 1327 } 1328 1329 1330 // Field allocation types. Used for computing field offsets. 1331 1332 enum FieldAllocationType { 1333 STATIC_OOP, // Oops 1334 STATIC_BYTE, // Boolean, Byte, char 1335 STATIC_SHORT, // shorts 1336 STATIC_WORD, // ints 1337 STATIC_DOUBLE, // aligned long or double 1338 NONSTATIC_OOP, 1339 NONSTATIC_BYTE, 1340 NONSTATIC_SHORT, 1341 NONSTATIC_WORD, 1342 NONSTATIC_DOUBLE, 1343 MAX_FIELD_ALLOCATION_TYPE, 1344 BAD_ALLOCATION_TYPE = -1 1345 }; 1346 1347 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = { 1348 BAD_ALLOCATION_TYPE, // 0 1349 BAD_ALLOCATION_TYPE, // 1 1350 BAD_ALLOCATION_TYPE, // 2 1351 BAD_ALLOCATION_TYPE, // 3 1352 NONSTATIC_BYTE , // T_BOOLEAN = 4, 1353 NONSTATIC_SHORT, // T_CHAR = 5, 1354 NONSTATIC_WORD, // T_FLOAT = 6, 1355 NONSTATIC_DOUBLE, // T_DOUBLE = 7, 1356 NONSTATIC_BYTE, // T_BYTE = 8, 1357 NONSTATIC_SHORT, // T_SHORT = 9, 1358 NONSTATIC_WORD, // T_INT = 10, 1359 NONSTATIC_DOUBLE, // T_LONG = 11, 1360 NONSTATIC_OOP, // T_OBJECT = 12, 1361 NONSTATIC_OOP, // T_ARRAY = 13, 1362 BAD_ALLOCATION_TYPE, // T_VOID = 14, 1363 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, 1364 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, 1365 BAD_ALLOCATION_TYPE, // T_METADATA = 17, 1366 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, 1367 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, 1368 BAD_ALLOCATION_TYPE, // 0 1369 BAD_ALLOCATION_TYPE, // 1 1370 BAD_ALLOCATION_TYPE, // 2 1371 BAD_ALLOCATION_TYPE, // 3 1372 STATIC_BYTE , // T_BOOLEAN = 4, 1373 STATIC_SHORT, // T_CHAR = 5, 1374 STATIC_WORD, // T_FLOAT = 6, 1375 STATIC_DOUBLE, // T_DOUBLE = 7, 1376 STATIC_BYTE, // T_BYTE = 8, 1377 STATIC_SHORT, // T_SHORT = 9, 1378 STATIC_WORD, // T_INT = 10, 1379 STATIC_DOUBLE, // T_LONG = 11, 1380 STATIC_OOP, // T_OBJECT = 12, 1381 STATIC_OOP, // T_ARRAY = 13, 1382 BAD_ALLOCATION_TYPE, // T_VOID = 14, 1383 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, 1384 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, 1385 BAD_ALLOCATION_TYPE, // T_METADATA = 17, 1386 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, 1387 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, 1388 }; 1389 1390 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) { 1391 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values"); 1392 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)]; 1393 assert(result != BAD_ALLOCATION_TYPE, "bad type"); 1394 return result; 1395 } 1396 1397 class ClassFileParser::FieldAllocationCount : public ResourceObj { 1398 public: 1399 u2 count[MAX_FIELD_ALLOCATION_TYPE]; 1400 1401 FieldAllocationCount() { 1402 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) { 1403 count[i] = 0; 1404 } 1405 } 1406 1407 FieldAllocationType update(bool is_static, BasicType type) { 1408 FieldAllocationType atype = basic_type_to_atype(is_static, type); 1409 if (atype != BAD_ALLOCATION_TYPE) { 1410 // Make sure there is no overflow with injected fields. 1411 assert(count[atype] < 0xFFFF, "More than 65535 fields"); 1412 count[atype]++; 1413 } 1414 return atype; 1415 } 1416 }; 1417 1418 // Side-effects: populates the _fields, _fields_annotations, 1419 // _fields_type_annotations fields 1420 void ClassFileParser::parse_fields(const ClassFileStream* const cfs, 1421 bool is_interface, 1422 FieldAllocationCount* const fac, 1423 ConstantPool* cp, 1424 const int cp_size, 1425 u2* const java_fields_count_ptr, 1426 TRAPS) { 1427 1428 assert(cfs != NULL, "invariant"); 1429 assert(fac != NULL, "invariant"); 1430 assert(cp != NULL, "invariant"); 1431 assert(java_fields_count_ptr != NULL, "invariant"); 1432 1433 assert(NULL == _fields, "invariant"); 1434 assert(NULL == _fields_annotations, "invariant"); 1435 assert(NULL == _fields_type_annotations, "invariant"); 1436 1437 cfs->guarantee_more(2, CHECK); // length 1438 const u2 length = cfs->get_u2_fast(); 1439 *java_fields_count_ptr = length; 1440 1441 int num_injected = 0; 1442 const InjectedField* const injected = JavaClasses::get_injected(_class_name, 1443 &num_injected); 1444 const int total_fields = length + num_injected; 1445 1446 // The field array starts with tuples of shorts 1447 // [access, name index, sig index, initial value index, byte offset]. 1448 // A generic signature slot only exists for field with generic 1449 // signature attribute. And the access flag is set with 1450 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic 1451 // signature slots are at the end of the field array and after all 1452 // other fields data. 1453 // 1454 // f1: [access, name index, sig index, initial value index, low_offset, high_offset] 1455 // f2: [access, name index, sig index, initial value index, low_offset, high_offset] 1456 // ... 1457 // fn: [access, name index, sig index, initial value index, low_offset, high_offset] 1458 // [generic signature index] 1459 // [generic signature index] 1460 // ... 1461 // 1462 // Allocate a temporary resource array for field data. For each field, 1463 // a slot is reserved in the temporary array for the generic signature 1464 // index. After parsing all fields, the data are copied to a permanent 1465 // array and any unused slots will be discarded. 1466 ResourceMark rm(THREAD); 1467 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 1468 u2, 1469 total_fields * (FieldInfo::field_slots + 1)); 1470 1471 // The generic signature slots start after all other fields' data. 1472 int generic_signature_slot = total_fields * FieldInfo::field_slots; 1473 int num_generic_signature = 0; 1474 for (int n = 0; n < length; n++) { 1475 // access_flags, name_index, descriptor_index, attributes_count 1476 cfs->guarantee_more(8, CHECK); 1477 1478 AccessFlags access_flags; 1479 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS; 1480 verify_legal_field_modifiers(flags, is_interface, CHECK); 1481 access_flags.set_flags(flags); 1482 1483 const u2 name_index = cfs->get_u2_fast(); 1484 check_property(valid_symbol_at(name_index), 1485 "Invalid constant pool index %u for field name in class file %s", 1486 name_index, CHECK); 1487 const Symbol* const name = cp->symbol_at(name_index); 1488 verify_legal_field_name(name, CHECK); 1489 1490 const u2 signature_index = cfs->get_u2_fast(); 1491 check_property(valid_symbol_at(signature_index), 1492 "Invalid constant pool index %u for field signature in class file %s", 1493 signature_index, CHECK); 1494 const Symbol* const sig = cp->symbol_at(signature_index); 1495 verify_legal_field_signature(name, sig, CHECK); 1496 1497 u2 constantvalue_index = 0; 1498 bool is_synthetic = false; 1499 u2 generic_signature_index = 0; 1500 const bool is_static = access_flags.is_static(); 1501 FieldAnnotationCollector parsed_annotations(_loader_data); 1502 1503 const u2 attributes_count = cfs->get_u2_fast(); 1504 if (attributes_count > 0) { 1505 parse_field_attributes(cfs, 1506 attributes_count, 1507 is_static, 1508 signature_index, 1509 &constantvalue_index, 1510 &is_synthetic, 1511 &generic_signature_index, 1512 &parsed_annotations, 1513 CHECK); 1514 1515 if (parsed_annotations.field_annotations() != NULL) { 1516 if (_fields_annotations == NULL) { 1517 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>( 1518 _loader_data, length, NULL, 1519 CHECK); 1520 } 1521 _fields_annotations->at_put(n, parsed_annotations.field_annotations()); 1522 parsed_annotations.set_field_annotations(NULL); 1523 } 1524 if (parsed_annotations.field_type_annotations() != NULL) { 1525 if (_fields_type_annotations == NULL) { 1526 _fields_type_annotations = 1527 MetadataFactory::new_array<AnnotationArray*>(_loader_data, 1528 length, 1529 NULL, 1530 CHECK); 1531 } 1532 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations()); 1533 parsed_annotations.set_field_type_annotations(NULL); 1534 } 1535 1536 if (is_synthetic) { 1537 access_flags.set_is_synthetic(); 1538 } 1539 if (generic_signature_index != 0) { 1540 access_flags.set_field_has_generic_signature(); 1541 fa[generic_signature_slot] = generic_signature_index; 1542 generic_signature_slot ++; 1543 num_generic_signature ++; 1544 } 1545 } 1546 1547 FieldInfo* const field = FieldInfo::from_field_array(fa, n); 1548 field->initialize(access_flags.as_short(), 1549 name_index, 1550 signature_index, 1551 constantvalue_index); 1552 const BasicType type = cp->basic_type_for_signature_at(signature_index); 1553 1554 // Remember how many oops we encountered and compute allocation type 1555 const FieldAllocationType atype = fac->update(is_static, type); 1556 field->set_allocation_type(atype); 1557 1558 // After field is initialized with type, we can augment it with aux info 1559 if (parsed_annotations.has_any_annotations()) 1560 parsed_annotations.apply_to(field); 1561 } 1562 1563 int index = length; 1564 if (num_injected != 0) { 1565 for (int n = 0; n < num_injected; n++) { 1566 // Check for duplicates 1567 if (injected[n].may_be_java) { 1568 const Symbol* const name = injected[n].name(); 1569 const Symbol* const signature = injected[n].signature(); 1570 bool duplicate = false; 1571 for (int i = 0; i < length; i++) { 1572 const FieldInfo* const f = FieldInfo::from_field_array(fa, i); 1573 if (name == cp->symbol_at(f->name_index()) && 1574 signature == cp->symbol_at(f->signature_index())) { 1575 // Symbol is desclared in Java so skip this one 1576 duplicate = true; 1577 break; 1578 } 1579 } 1580 if (duplicate) { 1581 // These will be removed from the field array at the end 1582 continue; 1583 } 1584 } 1585 1586 // Injected field 1587 FieldInfo* const field = FieldInfo::from_field_array(fa, index); 1588 field->initialize(JVM_ACC_FIELD_INTERNAL, 1589 injected[n].name_index, 1590 injected[n].signature_index, 1591 0); 1592 1593 const BasicType type = FieldType::basic_type(injected[n].signature()); 1594 1595 // Remember how many oops we encountered and compute allocation type 1596 const FieldAllocationType atype = fac->update(false, type); 1597 field->set_allocation_type(atype); 1598 index++; 1599 } 1600 } 1601 1602 assert(NULL == _fields, "invariant"); 1603 1604 _fields = 1605 MetadataFactory::new_array<u2>(_loader_data, 1606 index * FieldInfo::field_slots + num_generic_signature, 1607 CHECK); 1608 // Sometimes injected fields already exist in the Java source so 1609 // the fields array could be too long. In that case the 1610 // fields array is trimed. Also unused slots that were reserved 1611 // for generic signature indexes are discarded. 1612 { 1613 int i = 0; 1614 for (; i < index * FieldInfo::field_slots; i++) { 1615 _fields->at_put(i, fa[i]); 1616 } 1617 for (int j = total_fields * FieldInfo::field_slots; 1618 j < generic_signature_slot; j++) { 1619 _fields->at_put(i++, fa[j]); 1620 } 1621 assert(_fields->length() == i, ""); 1622 } 1623 1624 if (_need_verify && length > 1) { 1625 // Check duplicated fields 1626 ResourceMark rm(THREAD); 1627 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 1628 THREAD, NameSigHash*, HASH_ROW_SIZE); 1629 initialize_hashtable(names_and_sigs); 1630 bool dup = false; 1631 { 1632 debug_only(NoSafepointVerifier nsv;) 1633 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 1634 const Symbol* const name = fs.name(); 1635 const Symbol* const sig = fs.signature(); 1636 // If no duplicates, add name/signature in hashtable names_and_sigs. 1637 if (!put_after_lookup(name, sig, names_and_sigs)) { 1638 dup = true; 1639 break; 1640 } 1641 } 1642 } 1643 if (dup) { 1644 classfile_parse_error("Duplicate field name&signature in class file %s", 1645 CHECK); 1646 } 1647 } 1648 } 1649 1650 1651 static void copy_u2_with_conversion(u2* dest, const u2* src, int length) { 1652 while (length-- > 0) { 1653 *dest++ = Bytes::get_Java_u2((u1*) (src++)); 1654 } 1655 } 1656 1657 const u2* ClassFileParser::parse_exception_table(const ClassFileStream* const cfs, 1658 u4 code_length, 1659 u4 exception_table_length, 1660 TRAPS) { 1661 assert(cfs != NULL, "invariant"); 1662 1663 const u2* const exception_table_start = cfs->get_u2_buffer(); 1664 assert(exception_table_start != NULL, "null exception table"); 1665 1666 cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, 1667 // end_pc, 1668 // handler_pc, 1669 // catch_type_index 1670 1671 // Will check legal target after parsing code array in verifier. 1672 if (_need_verify) { 1673 for (unsigned int i = 0; i < exception_table_length; i++) { 1674 const u2 start_pc = cfs->get_u2_fast(); 1675 const u2 end_pc = cfs->get_u2_fast(); 1676 const u2 handler_pc = cfs->get_u2_fast(); 1677 const u2 catch_type_index = cfs->get_u2_fast(); 1678 guarantee_property((start_pc < end_pc) && (end_pc <= code_length), 1679 "Illegal exception table range in class file %s", 1680 CHECK_NULL); 1681 guarantee_property(handler_pc < code_length, 1682 "Illegal exception table handler in class file %s", 1683 CHECK_NULL); 1684 if (catch_type_index != 0) { 1685 guarantee_property(valid_klass_reference_at(catch_type_index), 1686 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); 1687 } 1688 } 1689 } else { 1690 cfs->skip_u2_fast(exception_table_length * 4); 1691 } 1692 return exception_table_start; 1693 } 1694 1695 void ClassFileParser::parse_linenumber_table(u4 code_attribute_length, 1696 u4 code_length, 1697 CompressedLineNumberWriteStream**const write_stream, 1698 TRAPS) { 1699 1700 const ClassFileStream* const cfs = _stream; 1701 unsigned int num_entries = cfs->get_u2(CHECK); 1702 1703 // Each entry is a u2 start_pc, and a u2 line_number 1704 const unsigned int length_in_bytes = num_entries * (sizeof(u2) * 2); 1705 1706 // Verify line number attribute and table length 1707 check_property( 1708 code_attribute_length == sizeof(u2) + length_in_bytes, 1709 "LineNumberTable attribute has wrong length in class file %s", CHECK); 1710 1711 cfs->guarantee_more(length_in_bytes, CHECK); 1712 1713 if ((*write_stream) == NULL) { 1714 if (length_in_bytes > fixed_buffer_size) { 1715 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes); 1716 } else { 1717 (*write_stream) = new CompressedLineNumberWriteStream( 1718 _linenumbertable_buffer, fixed_buffer_size); 1719 } 1720 } 1721 1722 while (num_entries-- > 0) { 1723 const u2 bci = cfs->get_u2_fast(); // start_pc 1724 const u2 line = cfs->get_u2_fast(); // line_number 1725 guarantee_property(bci < code_length, 1726 "Invalid pc in LineNumberTable in class file %s", CHECK); 1727 (*write_stream)->write_pair(bci, line); 1728 } 1729 } 1730 1731 1732 class LVT_Hash : public AllStatic { 1733 public: 1734 1735 static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) { 1736 /* 1737 * 3-tuple start_bci/length/slot has to be unique key, 1738 * so the following comparison seems to be redundant: 1739 * && elem->name_cp_index == entry->_elem->name_cp_index 1740 */ 1741 return (e0.start_bci == e1.start_bci && 1742 e0.length == e1.length && 1743 e0.name_cp_index == e1.name_cp_index && 1744 e0.slot == e1.slot); 1745 } 1746 1747 static unsigned int hash(LocalVariableTableElement const& e0) { 1748 unsigned int raw_hash = e0.start_bci; 1749 1750 raw_hash = e0.length + raw_hash * 37; 1751 raw_hash = e0.name_cp_index + raw_hash * 37; 1752 raw_hash = e0.slot + raw_hash * 37; 1753 1754 return raw_hash; 1755 } 1756 }; 1757 1758 1759 // Class file LocalVariableTable elements. 1760 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC { 1761 public: 1762 u2 start_bci; 1763 u2 length; 1764 u2 name_cp_index; 1765 u2 descriptor_cp_index; 1766 u2 slot; 1767 }; 1768 1769 static void copy_lvt_element(const Classfile_LVT_Element* const src, 1770 LocalVariableTableElement* const lvt) { 1771 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci); 1772 lvt->length = Bytes::get_Java_u2((u1*) &src->length); 1773 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index); 1774 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index); 1775 lvt->signature_cp_index = 0; 1776 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot); 1777 } 1778 1779 // Function is used to parse both attributes: 1780 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) 1781 const u2* ClassFileParser::parse_localvariable_table(const ClassFileStream* cfs, 1782 u4 code_length, 1783 u2 max_locals, 1784 u4 code_attribute_length, 1785 u2* const localvariable_table_length, 1786 bool isLVTT, 1787 TRAPS) { 1788 const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; 1789 *localvariable_table_length = cfs->get_u2(CHECK_NULL); 1790 const unsigned int size = 1791 (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2); 1792 1793 const ConstantPool* const cp = _cp; 1794 1795 // Verify local variable table attribute has right length 1796 if (_need_verify) { 1797 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)), 1798 "%s has wrong length in class file %s", tbl_name, CHECK_NULL); 1799 } 1800 1801 const u2* const localvariable_table_start = cfs->get_u2_buffer(); 1802 assert(localvariable_table_start != NULL, "null local variable table"); 1803 if (!_need_verify) { 1804 cfs->skip_u2_fast(size); 1805 } else { 1806 cfs->guarantee_more(size * 2, CHECK_NULL); 1807 for(int i = 0; i < (*localvariable_table_length); i++) { 1808 const u2 start_pc = cfs->get_u2_fast(); 1809 const u2 length = cfs->get_u2_fast(); 1810 const u2 name_index = cfs->get_u2_fast(); 1811 const u2 descriptor_index = cfs->get_u2_fast(); 1812 const u2 index = cfs->get_u2_fast(); 1813 // Assign to a u4 to avoid overflow 1814 const u4 end_pc = (u4)start_pc + (u4)length; 1815 1816 if (start_pc >= code_length) { 1817 classfile_parse_error( 1818 "Invalid start_pc %u in %s in class file %s", 1819 start_pc, tbl_name, CHECK_NULL); 1820 } 1821 if (end_pc > code_length) { 1822 classfile_parse_error( 1823 "Invalid length %u in %s in class file %s", 1824 length, tbl_name, CHECK_NULL); 1825 } 1826 const int cp_size = cp->length(); 1827 guarantee_property(valid_symbol_at(name_index), 1828 "Name index %u in %s has bad constant type in class file %s", 1829 name_index, tbl_name, CHECK_NULL); 1830 guarantee_property(valid_symbol_at(descriptor_index), 1831 "Signature index %u in %s has bad constant type in class file %s", 1832 descriptor_index, tbl_name, CHECK_NULL); 1833 1834 const Symbol* const name = cp->symbol_at(name_index); 1835 const Symbol* const sig = cp->symbol_at(descriptor_index); 1836 verify_legal_field_name(name, CHECK_NULL); 1837 u2 extra_slot = 0; 1838 if (!isLVTT) { 1839 verify_legal_field_signature(name, sig, CHECK_NULL); 1840 1841 // 4894874: check special cases for double and long local variables 1842 if (sig == vmSymbols::type_signature(T_DOUBLE) || 1843 sig == vmSymbols::type_signature(T_LONG)) { 1844 extra_slot = 1; 1845 } 1846 } 1847 guarantee_property((index + extra_slot) < max_locals, 1848 "Invalid index %u in %s in class file %s", 1849 index, tbl_name, CHECK_NULL); 1850 } 1851 } 1852 return localvariable_table_start; 1853 } 1854 1855 1856 void ClassFileParser::parse_type_array(u2 array_length, 1857 u4 code_length, 1858 u4* const u1_index, 1859 u4* const u2_index, 1860 u1* const u1_array, 1861 u2* const u2_array, 1862 TRAPS) { 1863 const ClassFileStream* const cfs = _stream; 1864 u2 index = 0; // index in the array with long/double occupying two slots 1865 u4 i1 = *u1_index; 1866 u4 i2 = *u2_index + 1; 1867 for(int i = 0; i < array_length; i++) { 1868 const u1 tag = u1_array[i1++] = cfs->get_u1(CHECK); 1869 index++; 1870 if (tag == ITEM_Long || tag == ITEM_Double) { 1871 index++; 1872 } else if (tag == ITEM_Object) { 1873 const u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK); 1874 guarantee_property(valid_klass_reference_at(class_index), 1875 "Bad class index %u in StackMap in class file %s", 1876 class_index, CHECK); 1877 } else if (tag == ITEM_Uninitialized) { 1878 const u2 offset = u2_array[i2++] = cfs->get_u2(CHECK); 1879 guarantee_property( 1880 offset < code_length, 1881 "Bad uninitialized type offset %u in StackMap in class file %s", 1882 offset, CHECK); 1883 } else { 1884 guarantee_property( 1885 tag <= (u1)ITEM_Uninitialized, 1886 "Unknown variable type %u in StackMap in class file %s", 1887 tag, CHECK); 1888 } 1889 } 1890 u2_array[*u2_index] = index; 1891 *u1_index = i1; 1892 *u2_index = i2; 1893 } 1894 1895 static const u1* parse_stackmap_table(const ClassFileStream* const cfs, 1896 u4 code_attribute_length, 1897 bool need_verify, 1898 TRAPS) { 1899 assert(cfs != NULL, "invariant"); 1900 1901 if (0 == code_attribute_length) { 1902 return NULL; 1903 } 1904 1905 const u1* const stackmap_table_start = cfs->get_u1_buffer(); 1906 assert(stackmap_table_start != NULL, "null stackmap table"); 1907 1908 // check code_attribute_length first 1909 cfs->skip_u1(code_attribute_length, CHECK_NULL); 1910 1911 if (!need_verify && !DumpSharedSpaces) { 1912 return NULL; 1913 } 1914 return stackmap_table_start; 1915 } 1916 1917 const u2* ClassFileParser::parse_checked_exceptions(const ClassFileStream* const cfs, 1918 u2* const checked_exceptions_length, 1919 u4 method_attribute_length, 1920 TRAPS) { 1921 assert(cfs != NULL, "invariant"); 1922 assert(checked_exceptions_length != NULL, "invariant"); 1923 1924 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length 1925 *checked_exceptions_length = cfs->get_u2_fast(); 1926 const unsigned int size = 1927 (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); 1928 const u2* const checked_exceptions_start = cfs->get_u2_buffer(); 1929 assert(checked_exceptions_start != NULL, "null checked exceptions"); 1930 if (!_need_verify) { 1931 cfs->skip_u2_fast(size); 1932 } else { 1933 // Verify each value in the checked exception table 1934 u2 checked_exception; 1935 const u2 len = *checked_exceptions_length; 1936 cfs->guarantee_more(2 * len, CHECK_NULL); 1937 for (int i = 0; i < len; i++) { 1938 checked_exception = cfs->get_u2_fast(); 1939 check_property( 1940 valid_klass_reference_at(checked_exception), 1941 "Exception name has bad type at constant pool %u in class file %s", 1942 checked_exception, CHECK_NULL); 1943 } 1944 } 1945 // check exceptions attribute length 1946 if (_need_verify) { 1947 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) + 1948 sizeof(u2) * size), 1949 "Exceptions attribute has wrong length in class file %s", CHECK_NULL); 1950 } 1951 return checked_exceptions_start; 1952 } 1953 1954 void ClassFileParser::throwIllegalSignature(const char* type, 1955 const Symbol* name, 1956 const Symbol* sig, 1957 TRAPS) const { 1958 assert(name != NULL, "invariant"); 1959 assert(sig != NULL, "invariant"); 1960 1961 ResourceMark rm(THREAD); 1962 Exceptions::fthrow(THREAD_AND_LOCATION, 1963 vmSymbols::java_lang_ClassFormatError(), 1964 "%s \"%s\" in class %s has illegal signature \"%s\"", type, 1965 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); 1966 } 1967 1968 AnnotationCollector::ID 1969 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data, 1970 const Symbol* name) { 1971 const vmSymbols::SID sid = vmSymbols::find_sid(name); 1972 // Privileged code can use all annotations. Other code silently drops some. 1973 const bool privileged = loader_data->is_the_null_class_loader_data() || 1974 loader_data->is_platform_class_loader_data() || 1975 loader_data->is_anonymous(); 1976 switch (sid) { 1977 case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): { 1978 if (_location != _in_method) break; // only allow for methods 1979 if (!privileged) break; // only allow in privileged code 1980 return _method_CallerSensitive; 1981 } 1982 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): { 1983 if (_location != _in_method) break; // only allow for methods 1984 if (!privileged) break; // only allow in privileged code 1985 return _method_ForceInline; 1986 } 1987 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): { 1988 if (_location != _in_method) break; // only allow for methods 1989 if (!privileged) break; // only allow in privileged code 1990 return _method_DontInline; 1991 } 1992 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): { 1993 if (_location != _in_method) break; // only allow for methods 1994 if (!privileged) break; // only allow in privileged code 1995 return _method_InjectedProfile; 1996 } 1997 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): { 1998 if (_location != _in_method) break; // only allow for methods 1999 if (!privileged) break; // only allow in privileged code 2000 return _method_LambdaForm_Compiled; 2001 } 2002 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Hidden_signature): { 2003 if (_location != _in_method) break; // only allow for methods 2004 if (!privileged) break; // only allow in privileged code 2005 return _method_LambdaForm_Hidden; 2006 } 2007 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_HotSpotIntrinsicCandidate_signature): { 2008 if (_location != _in_method) break; // only allow for methods 2009 if (!privileged) break; // only allow in privileged code 2010 return _method_HotSpotIntrinsicCandidate; 2011 } 2012 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Stable_signature): { 2013 if (_location != _in_field) break; // only allow for fields 2014 if (!privileged) break; // only allow in privileged code 2015 return _field_Stable; 2016 } 2017 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): { 2018 if (_location != _in_field && _location != _in_class) { 2019 break; // only allow for fields and classes 2020 } 2021 if (!EnableContended || (RestrictContended && !privileged)) { 2022 break; // honor privileges 2023 } 2024 return _jdk_internal_vm_annotation_Contended; 2025 } 2026 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): { 2027 if (_location != _in_method) break; // only allow for methods 2028 if (RestrictReservedStack && !privileged) break; // honor privileges 2029 return _jdk_internal_vm_annotation_ReservedStackAccess; 2030 } 2031 default: { 2032 break; 2033 } 2034 } 2035 return AnnotationCollector::_unknown; 2036 } 2037 2038 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { 2039 if (is_contended()) 2040 f->set_contended_group(contended_group()); 2041 if (is_stable()) 2042 f->set_stable(true); 2043 } 2044 2045 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() { 2046 // If there's an error deallocate metadata for field annotations 2047 MetadataFactory::free_array<u1>(_loader_data, _field_annotations); 2048 MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations); 2049 } 2050 2051 void MethodAnnotationCollector::apply_to(methodHandle m) { 2052 if (has_annotation(_method_CallerSensitive)) 2053 m->set_caller_sensitive(true); 2054 if (has_annotation(_method_ForceInline)) 2055 m->set_force_inline(true); 2056 if (has_annotation(_method_DontInline)) 2057 m->set_dont_inline(true); 2058 if (has_annotation(_method_InjectedProfile)) 2059 m->set_has_injected_profile(true); 2060 if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none) 2061 m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm); 2062 if (has_annotation(_method_LambdaForm_Hidden)) 2063 m->set_hidden(true); 2064 if (has_annotation(_method_HotSpotIntrinsicCandidate) && !m->is_synthetic()) 2065 m->set_intrinsic_candidate(true); 2066 if (has_annotation(_jdk_internal_vm_annotation_ReservedStackAccess)) 2067 m->set_has_reserved_stack_access(true); 2068 } 2069 2070 void ClassFileParser::ClassAnnotationCollector::apply_to(InstanceKlass* ik) { 2071 assert(ik != NULL, "invariant"); 2072 ik->set_is_contended(is_contended()); 2073 } 2074 2075 #define MAX_ARGS_SIZE 255 2076 #define MAX_CODE_SIZE 65535 2077 #define INITIAL_MAX_LVT_NUMBER 256 2078 2079 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2080 * 2081 * Rules for LVT's and LVTT's are: 2082 * - There can be any number of LVT's and LVTT's. 2083 * - If there are n LVT's, it is the same as if there was just 2084 * one LVT containing all the entries from the n LVT's. 2085 * - There may be no more than one LVT entry per local variable. 2086 * Two LVT entries are 'equal' if these fields are the same: 2087 * start_pc, length, name, slot 2088 * - There may be no more than one LVTT entry per each LVT entry. 2089 * Each LVTT entry has to match some LVT entry. 2090 * - HotSpot internal LVT keeps natural ordering of class file LVT entries. 2091 */ 2092 void ClassFileParser::copy_localvariable_table(const ConstMethod* cm, 2093 int lvt_cnt, 2094 u2* const localvariable_table_length, 2095 const u2**const localvariable_table_start, 2096 int lvtt_cnt, 2097 u2* const localvariable_type_table_length, 2098 const u2**const localvariable_type_table_start, 2099 TRAPS) { 2100 2101 ResourceMark rm(THREAD); 2102 2103 typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*, 2104 &LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable; 2105 2106 LVT_HashTable* const table = new LVT_HashTable(); 2107 2108 // To fill LocalVariableTable in 2109 const Classfile_LVT_Element* cf_lvt; 2110 LocalVariableTableElement* lvt = cm->localvariable_table_start(); 2111 2112 for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { 2113 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; 2114 for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { 2115 copy_lvt_element(&cf_lvt[idx], lvt); 2116 // If no duplicates, add LVT elem in hashtable. 2117 if (table->put(*lvt, lvt) == false 2118 && _need_verify 2119 && _major_version >= JAVA_1_5_VERSION) { 2120 classfile_parse_error("Duplicated LocalVariableTable attribute " 2121 "entry for '%s' in class file %s", 2122 _cp->symbol_at(lvt->name_cp_index)->as_utf8(), 2123 CHECK); 2124 } 2125 } 2126 } 2127 2128 // To merge LocalVariableTable and LocalVariableTypeTable 2129 const Classfile_LVT_Element* cf_lvtt; 2130 LocalVariableTableElement lvtt_elem; 2131 2132 for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { 2133 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; 2134 for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { 2135 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); 2136 LocalVariableTableElement** entry = table->get(lvtt_elem); 2137 if (entry == NULL) { 2138 if (_need_verify) { 2139 classfile_parse_error("LVTT entry for '%s' in class file %s " 2140 "does not match any LVT entry", 2141 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2142 CHECK); 2143 } 2144 } else if ((*entry)->signature_cp_index != 0 && _need_verify) { 2145 classfile_parse_error("Duplicated LocalVariableTypeTable attribute " 2146 "entry for '%s' in class file %s", 2147 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2148 CHECK); 2149 } else { 2150 // to add generic signatures into LocalVariableTable 2151 (*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index; 2152 } 2153 } 2154 } 2155 } 2156 2157 2158 void ClassFileParser::copy_method_annotations(ConstMethod* cm, 2159 const u1* runtime_visible_annotations, 2160 int runtime_visible_annotations_length, 2161 const u1* runtime_invisible_annotations, 2162 int runtime_invisible_annotations_length, 2163 const u1* runtime_visible_parameter_annotations, 2164 int runtime_visible_parameter_annotations_length, 2165 const u1* runtime_invisible_parameter_annotations, 2166 int runtime_invisible_parameter_annotations_length, 2167 const u1* runtime_visible_type_annotations, 2168 int runtime_visible_type_annotations_length, 2169 const u1* runtime_invisible_type_annotations, 2170 int runtime_invisible_type_annotations_length, 2171 const u1* annotation_default, 2172 int annotation_default_length, 2173 TRAPS) { 2174 2175 AnnotationArray* a; 2176 2177 if (runtime_visible_annotations_length + 2178 runtime_invisible_annotations_length > 0) { 2179 a = assemble_annotations(runtime_visible_annotations, 2180 runtime_visible_annotations_length, 2181 runtime_invisible_annotations, 2182 runtime_invisible_annotations_length, 2183 CHECK); 2184 cm->set_method_annotations(a); 2185 } 2186 2187 if (runtime_visible_parameter_annotations_length + 2188 runtime_invisible_parameter_annotations_length > 0) { 2189 a = assemble_annotations(runtime_visible_parameter_annotations, 2190 runtime_visible_parameter_annotations_length, 2191 runtime_invisible_parameter_annotations, 2192 runtime_invisible_parameter_annotations_length, 2193 CHECK); 2194 cm->set_parameter_annotations(a); 2195 } 2196 2197 if (annotation_default_length > 0) { 2198 a = assemble_annotations(annotation_default, 2199 annotation_default_length, 2200 NULL, 2201 0, 2202 CHECK); 2203 cm->set_default_annotations(a); 2204 } 2205 2206 if (runtime_visible_type_annotations_length + 2207 runtime_invisible_type_annotations_length > 0) { 2208 a = assemble_annotations(runtime_visible_type_annotations, 2209 runtime_visible_type_annotations_length, 2210 runtime_invisible_type_annotations, 2211 runtime_invisible_type_annotations_length, 2212 CHECK); 2213 cm->set_type_annotations(a); 2214 } 2215 } 2216 2217 2218 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions 2219 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the 2220 // Method* to save footprint, so we only know the size of the resulting Method* when the 2221 // entire method attribute is parsed. 2222 // 2223 // The promoted_flags parameter is used to pass relevant access_flags 2224 // from the method back up to the containing klass. These flag values 2225 // are added to klass's access_flags. 2226 2227 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs, 2228 bool is_interface, 2229 const ConstantPool* cp, 2230 AccessFlags* const promoted_flags, 2231 TRAPS) { 2232 assert(cfs != NULL, "invariant"); 2233 assert(cp != NULL, "invariant"); 2234 assert(promoted_flags != NULL, "invariant"); 2235 2236 ResourceMark rm(THREAD); 2237 // Parse fixed parts: 2238 // access_flags, name_index, descriptor_index, attributes_count 2239 cfs->guarantee_more(8, CHECK_NULL); 2240 2241 int flags = cfs->get_u2_fast(); 2242 const u2 name_index = cfs->get_u2_fast(); 2243 const int cp_size = cp->length(); 2244 check_property( 2245 valid_symbol_at(name_index), 2246 "Illegal constant pool index %u for method name in class file %s", 2247 name_index, CHECK_NULL); 2248 const Symbol* const name = cp->symbol_at(name_index); 2249 verify_legal_method_name(name, CHECK_NULL); 2250 2251 const u2 signature_index = cfs->get_u2_fast(); 2252 guarantee_property( 2253 valid_symbol_at(signature_index), 2254 "Illegal constant pool index %u for method signature in class file %s", 2255 signature_index, CHECK_NULL); 2256 const Symbol* const signature = cp->symbol_at(signature_index); 2257 2258 if (name == vmSymbols::class_initializer_name()) { 2259 // We ignore the other access flags for a valid class initializer. 2260 // (JVM Spec 2nd ed., chapter 4.6) 2261 if (_major_version < 51) { // backward compatibility 2262 flags = JVM_ACC_STATIC; 2263 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) { 2264 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT; 2265 } else { 2266 classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL); 2267 } 2268 } else { 2269 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL); 2270 } 2271 2272 if (name == vmSymbols::object_initializer_name() && is_interface) { 2273 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL); 2274 } 2275 2276 int args_size = -1; // only used when _need_verify is true 2277 if (_need_verify) { 2278 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) + 2279 verify_legal_method_signature(name, signature, CHECK_NULL); 2280 if (args_size > MAX_ARGS_SIZE) { 2281 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL); 2282 } 2283 } 2284 2285 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS); 2286 2287 // Default values for code and exceptions attribute elements 2288 u2 max_stack = 0; 2289 u2 max_locals = 0; 2290 u4 code_length = 0; 2291 const u1* code_start = 0; 2292 u2 exception_table_length = 0; 2293 const u2* exception_table_start = NULL; 2294 Array<int>* exception_handlers = Universe::the_empty_int_array(); 2295 u2 checked_exceptions_length = 0; 2296 const u2* checked_exceptions_start = NULL; 2297 CompressedLineNumberWriteStream* linenumber_table = NULL; 2298 int linenumber_table_length = 0; 2299 int total_lvt_length = 0; 2300 u2 lvt_cnt = 0; 2301 u2 lvtt_cnt = 0; 2302 bool lvt_allocated = false; 2303 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER; 2304 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER; 2305 u2* localvariable_table_length = NULL; 2306 const u2** localvariable_table_start = NULL; 2307 u2* localvariable_type_table_length = NULL; 2308 const u2** localvariable_type_table_start = NULL; 2309 int method_parameters_length = -1; 2310 const u1* method_parameters_data = NULL; 2311 bool method_parameters_seen = false; 2312 bool parsed_code_attribute = false; 2313 bool parsed_checked_exceptions_attribute = false; 2314 bool parsed_stackmap_attribute = false; 2315 // stackmap attribute - JDK1.5 2316 const u1* stackmap_data = NULL; 2317 int stackmap_data_length = 0; 2318 u2 generic_signature_index = 0; 2319 MethodAnnotationCollector parsed_annotations; 2320 const u1* runtime_visible_annotations = NULL; 2321 int runtime_visible_annotations_length = 0; 2322 const u1* runtime_invisible_annotations = NULL; 2323 int runtime_invisible_annotations_length = 0; 2324 const u1* runtime_visible_parameter_annotations = NULL; 2325 int runtime_visible_parameter_annotations_length = 0; 2326 const u1* runtime_invisible_parameter_annotations = NULL; 2327 int runtime_invisible_parameter_annotations_length = 0; 2328 const u1* runtime_visible_type_annotations = NULL; 2329 int runtime_visible_type_annotations_length = 0; 2330 const u1* runtime_invisible_type_annotations = NULL; 2331 int runtime_invisible_type_annotations_length = 0; 2332 bool runtime_invisible_annotations_exists = false; 2333 bool runtime_invisible_type_annotations_exists = false; 2334 bool runtime_invisible_parameter_annotations_exists = false; 2335 const u1* annotation_default = NULL; 2336 int annotation_default_length = 0; 2337 2338 // Parse code and exceptions attribute 2339 u2 method_attributes_count = cfs->get_u2_fast(); 2340 while (method_attributes_count--) { 2341 cfs->guarantee_more(6, CHECK_NULL); // method_attribute_name_index, method_attribute_length 2342 const u2 method_attribute_name_index = cfs->get_u2_fast(); 2343 const u4 method_attribute_length = cfs->get_u4_fast(); 2344 check_property( 2345 valid_symbol_at(method_attribute_name_index), 2346 "Invalid method attribute name index %u in class file %s", 2347 method_attribute_name_index, CHECK_NULL); 2348 2349 const Symbol* const method_attribute_name = cp->symbol_at(method_attribute_name_index); 2350 if (method_attribute_name == vmSymbols::tag_code()) { 2351 // Parse Code attribute 2352 if (_need_verify) { 2353 guarantee_property( 2354 !access_flags.is_native() && !access_flags.is_abstract(), 2355 "Code attribute in native or abstract methods in class file %s", 2356 CHECK_NULL); 2357 } 2358 if (parsed_code_attribute) { 2359 classfile_parse_error("Multiple Code attributes in class file %s", 2360 CHECK_NULL); 2361 } 2362 parsed_code_attribute = true; 2363 2364 // Stack size, locals size, and code size 2365 if (_major_version == 45 && _minor_version <= 2) { 2366 cfs->guarantee_more(4, CHECK_NULL); 2367 max_stack = cfs->get_u1_fast(); 2368 max_locals = cfs->get_u1_fast(); 2369 code_length = cfs->get_u2_fast(); 2370 } else { 2371 cfs->guarantee_more(8, CHECK_NULL); 2372 max_stack = cfs->get_u2_fast(); 2373 max_locals = cfs->get_u2_fast(); 2374 code_length = cfs->get_u4_fast(); 2375 } 2376 if (_need_verify) { 2377 guarantee_property(args_size <= max_locals, 2378 "Arguments can't fit into locals in class file %s", 2379 CHECK_NULL); 2380 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE, 2381 "Invalid method Code length %u in class file %s", 2382 code_length, CHECK_NULL); 2383 } 2384 // Code pointer 2385 code_start = cfs->get_u1_buffer(); 2386 assert(code_start != NULL, "null code start"); 2387 cfs->guarantee_more(code_length, CHECK_NULL); 2388 cfs->skip_u1_fast(code_length); 2389 2390 // Exception handler table 2391 cfs->guarantee_more(2, CHECK_NULL); // exception_table_length 2392 exception_table_length = cfs->get_u2_fast(); 2393 if (exception_table_length > 0) { 2394 exception_table_start = parse_exception_table(cfs, 2395 code_length, 2396 exception_table_length, 2397 CHECK_NULL); 2398 } 2399 2400 // Parse additional attributes in code attribute 2401 cfs->guarantee_more(2, CHECK_NULL); // code_attributes_count 2402 u2 code_attributes_count = cfs->get_u2_fast(); 2403 2404 unsigned int calculated_attribute_length = 0; 2405 2406 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) { 2407 calculated_attribute_length = 2408 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length); 2409 } else { 2410 // max_stack, locals and length are smaller in pre-version 45.2 classes 2411 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2); 2412 } 2413 calculated_attribute_length += 2414 code_length + 2415 sizeof(exception_table_length) + 2416 sizeof(code_attributes_count) + 2417 exception_table_length * 2418 ( sizeof(u2) + // start_pc 2419 sizeof(u2) + // end_pc 2420 sizeof(u2) + // handler_pc 2421 sizeof(u2) ); // catch_type_index 2422 2423 while (code_attributes_count--) { 2424 cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length 2425 const u2 code_attribute_name_index = cfs->get_u2_fast(); 2426 const u4 code_attribute_length = cfs->get_u4_fast(); 2427 calculated_attribute_length += code_attribute_length + 2428 sizeof(code_attribute_name_index) + 2429 sizeof(code_attribute_length); 2430 check_property(valid_symbol_at(code_attribute_name_index), 2431 "Invalid code attribute name index %u in class file %s", 2432 code_attribute_name_index, 2433 CHECK_NULL); 2434 if (LoadLineNumberTables && 2435 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { 2436 // Parse and compress line number table 2437 parse_linenumber_table(code_attribute_length, 2438 code_length, 2439 &linenumber_table, 2440 CHECK_NULL); 2441 2442 } else if (LoadLocalVariableTables && 2443 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { 2444 // Parse local variable table 2445 if (!lvt_allocated) { 2446 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2447 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2448 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2449 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2450 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2451 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2452 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2453 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2454 lvt_allocated = true; 2455 } 2456 if (lvt_cnt == max_lvt_cnt) { 2457 max_lvt_cnt <<= 1; 2458 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt); 2459 localvariable_table_start = REALLOC_RESOURCE_ARRAY(const u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt); 2460 } 2461 localvariable_table_start[lvt_cnt] = 2462 parse_localvariable_table(cfs, 2463 code_length, 2464 max_locals, 2465 code_attribute_length, 2466 &localvariable_table_length[lvt_cnt], 2467 false, // is not LVTT 2468 CHECK_NULL); 2469 total_lvt_length += localvariable_table_length[lvt_cnt]; 2470 lvt_cnt++; 2471 } else if (LoadLocalVariableTypeTables && 2472 _major_version >= JAVA_1_5_VERSION && 2473 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { 2474 if (!lvt_allocated) { 2475 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2476 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2477 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2478 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2479 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2480 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2481 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2482 THREAD, const u2*, INITIAL_MAX_LVT_NUMBER); 2483 lvt_allocated = true; 2484 } 2485 // Parse local variable type table 2486 if (lvtt_cnt == max_lvtt_cnt) { 2487 max_lvtt_cnt <<= 1; 2488 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt); 2489 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(const u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt); 2490 } 2491 localvariable_type_table_start[lvtt_cnt] = 2492 parse_localvariable_table(cfs, 2493 code_length, 2494 max_locals, 2495 code_attribute_length, 2496 &localvariable_type_table_length[lvtt_cnt], 2497 true, // is LVTT 2498 CHECK_NULL); 2499 lvtt_cnt++; 2500 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && 2501 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { 2502 // Stack map is only needed by the new verifier in JDK1.5. 2503 if (parsed_stackmap_attribute) { 2504 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_NULL); 2505 } 2506 stackmap_data = parse_stackmap_table(cfs, code_attribute_length, _need_verify, CHECK_NULL); 2507 stackmap_data_length = code_attribute_length; 2508 parsed_stackmap_attribute = true; 2509 } else { 2510 // Skip unknown attributes 2511 cfs->skip_u1(code_attribute_length, CHECK_NULL); 2512 } 2513 } 2514 // check method attribute length 2515 if (_need_verify) { 2516 guarantee_property(method_attribute_length == calculated_attribute_length, 2517 "Code segment has wrong length in class file %s", 2518 CHECK_NULL); 2519 } 2520 } else if (method_attribute_name == vmSymbols::tag_exceptions()) { 2521 // Parse Exceptions attribute 2522 if (parsed_checked_exceptions_attribute) { 2523 classfile_parse_error("Multiple Exceptions attributes in class file %s", 2524 CHECK_NULL); 2525 } 2526 parsed_checked_exceptions_attribute = true; 2527 checked_exceptions_start = 2528 parse_checked_exceptions(cfs, 2529 &checked_exceptions_length, 2530 method_attribute_length, 2531 CHECK_NULL); 2532 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { 2533 // reject multiple method parameters 2534 if (method_parameters_seen) { 2535 classfile_parse_error("Multiple MethodParameters attributes in class file %s", 2536 CHECK_NULL); 2537 } 2538 method_parameters_seen = true; 2539 method_parameters_length = cfs->get_u1_fast(); 2540 const u2 real_length = (method_parameters_length * 4u) + 1u; 2541 if (method_attribute_length != real_length) { 2542 classfile_parse_error( 2543 "Invalid MethodParameters method attribute length %u in class file", 2544 method_attribute_length, CHECK_NULL); 2545 } 2546 method_parameters_data = cfs->get_u1_buffer(); 2547 cfs->skip_u2_fast(method_parameters_length); 2548 cfs->skip_u2_fast(method_parameters_length); 2549 // ignore this attribute if it cannot be reflected 2550 if (!SystemDictionary::Parameter_klass_loaded()) 2551 method_parameters_length = -1; 2552 } else if (method_attribute_name == vmSymbols::tag_synthetic()) { 2553 if (method_attribute_length != 0) { 2554 classfile_parse_error( 2555 "Invalid Synthetic method attribute length %u in class file %s", 2556 method_attribute_length, CHECK_NULL); 2557 } 2558 // Should we check that there hasn't already been a synthetic attribute? 2559 access_flags.set_is_synthetic(); 2560 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120 2561 if (method_attribute_length != 0) { 2562 classfile_parse_error( 2563 "Invalid Deprecated method attribute length %u in class file %s", 2564 method_attribute_length, CHECK_NULL); 2565 } 2566 } else if (_major_version >= JAVA_1_5_VERSION) { 2567 if (method_attribute_name == vmSymbols::tag_signature()) { 2568 if (method_attribute_length != 2) { 2569 classfile_parse_error( 2570 "Invalid Signature attribute length %u in class file %s", 2571 method_attribute_length, CHECK_NULL); 2572 } 2573 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL); 2574 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2575 if (runtime_visible_annotations != NULL) { 2576 classfile_parse_error( 2577 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", 2578 CHECK_NULL); 2579 } 2580 runtime_visible_annotations_length = method_attribute_length; 2581 runtime_visible_annotations = cfs->get_u1_buffer(); 2582 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2583 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL); 2584 parse_annotations(cp, 2585 runtime_visible_annotations, 2586 runtime_visible_annotations_length, 2587 &parsed_annotations, 2588 _loader_data, 2589 CHECK_NULL); 2590 cfs->skip_u1_fast(runtime_visible_annotations_length); 2591 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2592 if (runtime_invisible_annotations_exists) { 2593 classfile_parse_error( 2594 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", 2595 CHECK_NULL); 2596 } 2597 runtime_invisible_annotations_exists = true; 2598 if (PreserveAllAnnotations) { 2599 runtime_invisible_annotations_length = method_attribute_length; 2600 runtime_invisible_annotations = cfs->get_u1_buffer(); 2601 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 2602 } 2603 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2604 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) { 2605 if (runtime_visible_parameter_annotations != NULL) { 2606 classfile_parse_error( 2607 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", 2608 CHECK_NULL); 2609 } 2610 runtime_visible_parameter_annotations_length = method_attribute_length; 2611 runtime_visible_parameter_annotations = cfs->get_u1_buffer(); 2612 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations"); 2613 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_NULL); 2614 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) { 2615 if (runtime_invisible_parameter_annotations_exists) { 2616 classfile_parse_error( 2617 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", 2618 CHECK_NULL); 2619 } 2620 runtime_invisible_parameter_annotations_exists = true; 2621 if (PreserveAllAnnotations) { 2622 runtime_invisible_parameter_annotations_length = method_attribute_length; 2623 runtime_invisible_parameter_annotations = cfs->get_u1_buffer(); 2624 assert(runtime_invisible_parameter_annotations != NULL, 2625 "null invisible parameter annotations"); 2626 } 2627 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2628 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) { 2629 if (annotation_default != NULL) { 2630 classfile_parse_error( 2631 "Multiple AnnotationDefault attributes for method in class file %s", 2632 CHECK_NULL); 2633 } 2634 annotation_default_length = method_attribute_length; 2635 annotation_default = cfs->get_u1_buffer(); 2636 assert(annotation_default != NULL, "null annotation default"); 2637 cfs->skip_u1(annotation_default_length, CHECK_NULL); 2638 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 2639 if (runtime_visible_type_annotations != NULL) { 2640 classfile_parse_error( 2641 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s", 2642 CHECK_NULL); 2643 } 2644 runtime_visible_type_annotations_length = method_attribute_length; 2645 runtime_visible_type_annotations = cfs->get_u1_buffer(); 2646 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 2647 // No need for the VM to parse Type annotations 2648 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_NULL); 2649 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 2650 if (runtime_invisible_type_annotations_exists) { 2651 classfile_parse_error( 2652 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s", 2653 CHECK_NULL); 2654 } else { 2655 runtime_invisible_type_annotations_exists = true; 2656 } 2657 if (PreserveAllAnnotations) { 2658 runtime_invisible_type_annotations_length = method_attribute_length; 2659 runtime_invisible_type_annotations = cfs->get_u1_buffer(); 2660 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 2661 } 2662 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2663 } else { 2664 // Skip unknown attributes 2665 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2666 } 2667 } else { 2668 // Skip unknown attributes 2669 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2670 } 2671 } 2672 2673 if (linenumber_table != NULL) { 2674 linenumber_table->write_terminator(); 2675 linenumber_table_length = linenumber_table->position(); 2676 } 2677 2678 // Make sure there's at least one Code attribute in non-native/non-abstract method 2679 if (_need_verify) { 2680 guarantee_property(access_flags.is_native() || 2681 access_flags.is_abstract() || 2682 parsed_code_attribute, 2683 "Absent Code attribute in method that is not native or abstract in class file %s", 2684 CHECK_NULL); 2685 } 2686 2687 // All sizing information for a Method* is finally available, now create it 2688 InlineTableSizes sizes( 2689 total_lvt_length, 2690 linenumber_table_length, 2691 exception_table_length, 2692 checked_exceptions_length, 2693 method_parameters_length, 2694 generic_signature_index, 2695 runtime_visible_annotations_length + 2696 runtime_invisible_annotations_length, 2697 runtime_visible_parameter_annotations_length + 2698 runtime_invisible_parameter_annotations_length, 2699 runtime_visible_type_annotations_length + 2700 runtime_invisible_type_annotations_length, 2701 annotation_default_length, 2702 0); 2703 2704 Method* const m = Method::allocate(_loader_data, 2705 code_length, 2706 access_flags, 2707 &sizes, 2708 ConstMethod::NORMAL, 2709 CHECK_NULL); 2710 2711 ClassLoadingService::add_class_method_size(m->size()*wordSize); 2712 2713 // Fill in information from fixed part (access_flags already set) 2714 m->set_constants(_cp); 2715 m->set_name_index(name_index); 2716 m->set_signature_index(signature_index); 2717 2718 ResultTypeFinder rtf(cp->symbol_at(signature_index)); 2719 m->constMethod()->set_result_type(rtf.type()); 2720 2721 if (args_size >= 0) { 2722 m->set_size_of_parameters(args_size); 2723 } else { 2724 m->compute_size_of_parameters(THREAD); 2725 } 2726 #ifdef ASSERT 2727 if (args_size >= 0) { 2728 m->compute_size_of_parameters(THREAD); 2729 assert(args_size == m->size_of_parameters(), ""); 2730 } 2731 #endif 2732 2733 // Fill in code attribute information 2734 m->set_max_stack(max_stack); 2735 m->set_max_locals(max_locals); 2736 if (stackmap_data != NULL) { 2737 m->constMethod()->copy_stackmap_data(_loader_data, 2738 (u1*)stackmap_data, 2739 stackmap_data_length, 2740 CHECK_NULL); 2741 } 2742 2743 // Copy byte codes 2744 m->set_code((u1*)code_start); 2745 2746 // Copy line number table 2747 if (linenumber_table != NULL) { 2748 memcpy(m->compressed_linenumber_table(), 2749 linenumber_table->buffer(), 2750 linenumber_table_length); 2751 } 2752 2753 // Copy exception table 2754 if (exception_table_length > 0) { 2755 int size = 2756 exception_table_length * sizeof(ExceptionTableElement) / sizeof(u2); 2757 copy_u2_with_conversion((u2*) m->exception_table_start(), 2758 exception_table_start, size); 2759 } 2760 2761 // Copy method parameters 2762 if (method_parameters_length > 0) { 2763 MethodParametersElement* elem = m->constMethod()->method_parameters_start(); 2764 for (int i = 0; i < method_parameters_length; i++) { 2765 elem[i].name_cp_index = Bytes::get_Java_u2((address)method_parameters_data); 2766 method_parameters_data += 2; 2767 elem[i].flags = Bytes::get_Java_u2((address)method_parameters_data); 2768 method_parameters_data += 2; 2769 } 2770 } 2771 2772 // Copy checked exceptions 2773 if (checked_exceptions_length > 0) { 2774 const int size = 2775 checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2); 2776 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), 2777 checked_exceptions_start, 2778 size); 2779 } 2780 2781 // Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2782 if (total_lvt_length > 0) { 2783 promoted_flags->set_has_localvariable_table(); 2784 copy_localvariable_table(m->constMethod(), 2785 lvt_cnt, 2786 localvariable_table_length, 2787 localvariable_table_start, 2788 lvtt_cnt, 2789 localvariable_type_table_length, 2790 localvariable_type_table_start, 2791 CHECK_NULL); 2792 } 2793 2794 if (parsed_annotations.has_any_annotations()) 2795 parsed_annotations.apply_to(m); 2796 2797 // Copy annotations 2798 copy_method_annotations(m->constMethod(), 2799 runtime_visible_annotations, 2800 runtime_visible_annotations_length, 2801 runtime_invisible_annotations, 2802 runtime_invisible_annotations_length, 2803 runtime_visible_parameter_annotations, 2804 runtime_visible_parameter_annotations_length, 2805 runtime_invisible_parameter_annotations, 2806 runtime_invisible_parameter_annotations_length, 2807 runtime_visible_type_annotations, 2808 runtime_visible_type_annotations_length, 2809 runtime_invisible_type_annotations, 2810 runtime_invisible_type_annotations_length, 2811 annotation_default, 2812 annotation_default_length, 2813 CHECK_NULL); 2814 2815 if (name == vmSymbols::finalize_method_name() && 2816 signature == vmSymbols::void_method_signature()) { 2817 if (m->is_empty_method()) { 2818 _has_empty_finalizer = true; 2819 } else { 2820 _has_finalizer = true; 2821 } 2822 } 2823 if (name == vmSymbols::object_initializer_name() && 2824 signature == vmSymbols::void_method_signature() && 2825 m->is_vanilla_constructor()) { 2826 _has_vanilla_constructor = true; 2827 } 2828 2829 NOT_PRODUCT(m->verify()); 2830 return m; 2831 } 2832 2833 2834 // The promoted_flags parameter is used to pass relevant access_flags 2835 // from the methods back up to the containing klass. These flag values 2836 // are added to klass's access_flags. 2837 // Side-effects: populates the _methods field in the parser 2838 void ClassFileParser::parse_methods(const ClassFileStream* const cfs, 2839 bool is_interface, 2840 AccessFlags* promoted_flags, 2841 bool* has_final_method, 2842 bool* declares_nonstatic_concrete_methods, 2843 TRAPS) { 2844 assert(cfs != NULL, "invariant"); 2845 assert(promoted_flags != NULL, "invariant"); 2846 assert(has_final_method != NULL, "invariant"); 2847 assert(declares_nonstatic_concrete_methods != NULL, "invariant"); 2848 2849 assert(NULL == _methods, "invariant"); 2850 2851 cfs->guarantee_more(2, CHECK); // length 2852 const u2 length = cfs->get_u2_fast(); 2853 if (length == 0) { 2854 _methods = Universe::the_empty_method_array(); 2855 } else { 2856 _methods = MetadataFactory::new_array<Method*>(_loader_data, 2857 length, 2858 NULL, 2859 CHECK); 2860 2861 HandleMark hm(THREAD); 2862 for (int index = 0; index < length; index++) { 2863 Method* method = parse_method(cfs, 2864 is_interface, 2865 _cp, 2866 promoted_flags, 2867 CHECK); 2868 2869 if (method->is_final()) { 2870 *has_final_method = true; 2871 } 2872 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags 2873 // used for interface initialization, and default method inheritance analysis 2874 if (is_interface && !(*declares_nonstatic_concrete_methods) 2875 && !method->is_abstract() && !method->is_static()) { 2876 *declares_nonstatic_concrete_methods = true; 2877 } 2878 _methods->at_put(index, method); 2879 } 2880 2881 if (_need_verify && length > 1) { 2882 // Check duplicated methods 2883 ResourceMark rm(THREAD); 2884 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 2885 THREAD, NameSigHash*, HASH_ROW_SIZE); 2886 initialize_hashtable(names_and_sigs); 2887 bool dup = false; 2888 { 2889 debug_only(NoSafepointVerifier nsv;) 2890 for (int i = 0; i < length; i++) { 2891 const Method* const m = _methods->at(i); 2892 // If no duplicates, add name/signature in hashtable names_and_sigs. 2893 if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) { 2894 dup = true; 2895 break; 2896 } 2897 } 2898 } 2899 if (dup) { 2900 classfile_parse_error("Duplicate method name&signature in class file %s", 2901 CHECK); 2902 } 2903 } 2904 } 2905 } 2906 2907 static const intArray* sort_methods(Array<Method*>* methods) { 2908 const int length = methods->length(); 2909 // If JVMTI original method ordering or sharing is enabled we have to 2910 // remember the original class file ordering. 2911 // We temporarily use the vtable_index field in the Method* to store the 2912 // class file index, so we can read in after calling qsort. 2913 // Put the method ordering in the shared archive. 2914 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 2915 for (int index = 0; index < length; index++) { 2916 Method* const m = methods->at(index); 2917 assert(!m->valid_vtable_index(), "vtable index should not be set"); 2918 m->set_vtable_index(index); 2919 } 2920 } 2921 // Sort method array by ascending method name (for faster lookups & vtable construction) 2922 // Note that the ordering is not alphabetical, see Symbol::fast_compare 2923 Method::sort_methods(methods); 2924 2925 intArray* method_ordering = NULL; 2926 // If JVMTI original method ordering or sharing is enabled construct int 2927 // array remembering the original ordering 2928 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 2929 method_ordering = new intArray(length, length, -1); 2930 for (int index = 0; index < length; index++) { 2931 Method* const m = methods->at(index); 2932 const int old_index = m->vtable_index(); 2933 assert(old_index >= 0 && old_index < length, "invalid method index"); 2934 method_ordering->at_put(index, old_index); 2935 m->set_vtable_index(Method::invalid_vtable_index); 2936 } 2937 } 2938 return method_ordering; 2939 } 2940 2941 // Parse generic_signature attribute for methods and fields 2942 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs, 2943 TRAPS) { 2944 assert(cfs != NULL, "invariant"); 2945 2946 cfs->guarantee_more(2, CHECK_0); // generic_signature_index 2947 const u2 generic_signature_index = cfs->get_u2_fast(); 2948 check_property( 2949 valid_symbol_at(generic_signature_index), 2950 "Invalid Signature attribute at constant pool index %u in class file %s", 2951 generic_signature_index, CHECK_0); 2952 return generic_signature_index; 2953 } 2954 2955 void ClassFileParser::parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, 2956 TRAPS) { 2957 2958 assert(cfs != NULL, "invariant"); 2959 2960 cfs->guarantee_more(2, CHECK); // sourcefile_index 2961 const u2 sourcefile_index = cfs->get_u2_fast(); 2962 check_property( 2963 valid_symbol_at(sourcefile_index), 2964 "Invalid SourceFile attribute at constant pool index %u in class file %s", 2965 sourcefile_index, CHECK); 2966 set_class_sourcefile_index(sourcefile_index); 2967 } 2968 2969 void ClassFileParser::parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 2970 int length, 2971 TRAPS) { 2972 assert(cfs != NULL, "invariant"); 2973 2974 const u1* const sde_buffer = cfs->get_u1_buffer(); 2975 assert(sde_buffer != NULL, "null sde buffer"); 2976 2977 // Don't bother storing it if there is no way to retrieve it 2978 if (JvmtiExport::can_get_source_debug_extension()) { 2979 assert((length+1) > length, "Overflow checking"); 2980 u1* const sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); 2981 for (int i = 0; i < length; i++) { 2982 sde[i] = sde_buffer[i]; 2983 } 2984 sde[length] = '\0'; 2985 set_class_sde_buffer((const char*)sde, length); 2986 } 2987 // Got utf8 string, set stream position forward 2988 cfs->skip_u1(length, CHECK); 2989 } 2990 2991 2992 // Inner classes can be static, private or protected (classic VM does this) 2993 #define RECOGNIZED_INNER_CLASS_MODIFIERS ( JVM_RECOGNIZED_CLASS_MODIFIERS | \ 2994 JVM_ACC_PRIVATE | \ 2995 JVM_ACC_PROTECTED | \ 2996 JVM_ACC_STATIC \ 2997 ) 2998 2999 // Return number of classes in the inner classes attribute table 3000 u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 3001 const u1* const inner_classes_attribute_start, 3002 bool parsed_enclosingmethod_attribute, 3003 u2 enclosing_method_class_index, 3004 u2 enclosing_method_method_index, 3005 TRAPS) { 3006 const u1* const current_mark = cfs->current(); 3007 u2 length = 0; 3008 if (inner_classes_attribute_start != NULL) { 3009 cfs->set_current(inner_classes_attribute_start); 3010 cfs->guarantee_more(2, CHECK_0); // length 3011 length = cfs->get_u2_fast(); 3012 } 3013 3014 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing 3015 // method data: 3016 // [inner_class_info_index, 3017 // outer_class_info_index, 3018 // inner_name_index, 3019 // inner_class_access_flags, 3020 // ... 3021 // enclosing_method_class_index, 3022 // enclosing_method_method_index] 3023 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); 3024 Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3025 _inner_classes = inner_classes; 3026 3027 int index = 0; 3028 const int cp_size = _cp->length(); 3029 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 3030 for (int n = 0; n < length; n++) { 3031 // Inner class index 3032 const u2 inner_class_info_index = cfs->get_u2_fast(); 3033 check_property( 3034 valid_klass_reference_at(inner_class_info_index), 3035 "inner_class_info_index %u has bad constant type in class file %s", 3036 inner_class_info_index, CHECK_0); 3037 // Outer class index 3038 const u2 outer_class_info_index = cfs->get_u2_fast(); 3039 check_property( 3040 outer_class_info_index == 0 || 3041 valid_klass_reference_at(outer_class_info_index), 3042 "outer_class_info_index %u has bad constant type in class file %s", 3043 outer_class_info_index, CHECK_0); 3044 // Inner class name 3045 const u2 inner_name_index = cfs->get_u2_fast(); 3046 check_property( 3047 inner_name_index == 0 || valid_symbol_at(inner_name_index), 3048 "inner_name_index %u has bad constant type in class file %s", 3049 inner_name_index, CHECK_0); 3050 if (_need_verify) { 3051 guarantee_property(inner_class_info_index != outer_class_info_index, 3052 "Class is both outer and inner class in class file %s", CHECK_0); 3053 } 3054 // Access flags 3055 jint flags; 3056 // JVM_ACC_MODULE is defined in JDK-9 and later. 3057 if (_major_version >= JAVA_9_VERSION) { 3058 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE); 3059 } else { 3060 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS; 3061 } 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, super, false); 4392 if (vca_result != Reflection::ACCESS_OK) { 4393 ResourceMark rm(THREAD); 4394 char* msg = Reflection::verify_class_access_msg(this_klass, super, vca_result); 4395 if (msg == NULL) { 4396 Exceptions::fthrow( 4397 THREAD_AND_LOCATION, 4398 vmSymbols::java_lang_IllegalAccessError(), 4399 "class %s cannot access its superclass %s", 4400 this_klass->external_name(), 4401 super->external_name()); 4402 } else { 4403 // Add additional message content. 4404 Exceptions::fthrow( 4405 THREAD_AND_LOCATION, 4406 vmSymbols::java_lang_IllegalAccessError(), 4407 "superclass access check failed: %s", 4408 msg); 4409 } 4410 } 4411 } 4412 } 4413 4414 4415 static void check_super_interface_access(const InstanceKlass* this_klass, TRAPS) { 4416 assert(this_klass != NULL, "invariant"); 4417 const Array<Klass*>* const local_interfaces = this_klass->local_interfaces(); 4418 const int lng = local_interfaces->length(); 4419 for (int i = lng - 1; i >= 0; i--) { 4420 Klass* const k = local_interfaces->at(i); 4421 assert (k != NULL && k->is_interface(), "invalid interface"); 4422 Reflection::VerifyClassAccessResults vca_result = 4423 Reflection::verify_class_access(this_klass, k, false); 4424 if (vca_result != Reflection::ACCESS_OK) { 4425 ResourceMark rm(THREAD); 4426 char* msg = Reflection::verify_class_access_msg(this_klass, k, vca_result); 4427 if (msg == NULL) { 4428 Exceptions::fthrow( 4429 THREAD_AND_LOCATION, 4430 vmSymbols::java_lang_IllegalAccessError(), 4431 "class %s cannot access its superinterface %s", 4432 this_klass->external_name(), 4433 k->external_name()); 4434 } else { 4435 // Add additional message content. 4436 Exceptions::fthrow( 4437 THREAD_AND_LOCATION, 4438 vmSymbols::java_lang_IllegalAccessError(), 4439 "superinterface check failed: %s", 4440 msg); 4441 } 4442 } 4443 } 4444 } 4445 4446 4447 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) { 4448 assert(this_klass != NULL, "invariant"); 4449 const Array<Method*>* const methods = this_klass->methods(); 4450 const int num_methods = methods->length(); 4451 4452 // go thru each method and check if it overrides a final method 4453 for (int index = 0; index < num_methods; index++) { 4454 const Method* const m = methods->at(index); 4455 4456 // skip private, static, and <init> methods 4457 if ((!m->is_private() && !m->is_static()) && 4458 (m->name() != vmSymbols::object_initializer_name())) { 4459 4460 const Symbol* const name = m->name(); 4461 const Symbol* const signature = m->signature(); 4462 const Klass* k = this_klass->super(); 4463 const Method* super_m = NULL; 4464 while (k != NULL) { 4465 // skip supers that don't have final methods. 4466 if (k->has_final_method()) { 4467 // lookup a matching method in the super class hierarchy 4468 super_m = InstanceKlass::cast(k)->lookup_method(name, signature); 4469 if (super_m == NULL) { 4470 break; // didn't find any match; get out 4471 } 4472 4473 if (super_m->is_final() && !super_m->is_static() && 4474 // matching method in super is final, and not static 4475 (Reflection::verify_field_access(this_klass, 4476 super_m->method_holder(), 4477 super_m->method_holder(), 4478 super_m->access_flags(), false)) 4479 // this class can access super final method and therefore override 4480 ) { 4481 ResourceMark rm(THREAD); 4482 Exceptions::fthrow( 4483 THREAD_AND_LOCATION, 4484 vmSymbols::java_lang_VerifyError(), 4485 "class %s overrides final method %s.%s%s", 4486 this_klass->external_name(), 4487 super_m->method_holder()->external_name(), 4488 name->as_C_string(), 4489 signature->as_C_string() 4490 ); 4491 return; 4492 } 4493 4494 // continue to look from super_m's holder's super. 4495 k = super_m->method_holder()->super(); 4496 continue; 4497 } 4498 4499 k = k->super(); 4500 } 4501 } 4502 } 4503 } 4504 4505 4506 // assumes that this_klass is an interface 4507 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) { 4508 assert(this_klass != NULL, "invariant"); 4509 assert(this_klass->is_interface(), "not an interface"); 4510 const Array<Method*>* methods = this_klass->methods(); 4511 const int num_methods = methods->length(); 4512 4513 for (int index = 0; index < num_methods; index++) { 4514 const Method* const m = methods->at(index); 4515 // if m is static and not the init method, throw a verify error 4516 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) { 4517 ResourceMark rm(THREAD); 4518 Exceptions::fthrow( 4519 THREAD_AND_LOCATION, 4520 vmSymbols::java_lang_VerifyError(), 4521 "Illegal static method %s in interface %s", 4522 m->name()->as_C_string(), 4523 this_klass->external_name() 4524 ); 4525 return; 4526 } 4527 } 4528 } 4529 4530 // utility methods for format checking 4531 4532 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const { 4533 const bool is_module = (flags & JVM_ACC_MODULE) != 0; 4534 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set"); 4535 if (is_module) { 4536 ResourceMark rm(THREAD); 4537 Exceptions::fthrow( 4538 THREAD_AND_LOCATION, 4539 vmSymbols::java_lang_NoClassDefFoundError(), 4540 "%s is not a class because access_flag ACC_MODULE is set", 4541 _class_name->as_C_string()); 4542 return; 4543 } 4544 4545 if (!_need_verify) { return; } 4546 4547 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0; 4548 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 4549 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4550 const bool is_super = (flags & JVM_ACC_SUPER) != 0; 4551 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 4552 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0; 4553 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4554 4555 if ((is_abstract && is_final) || 4556 (is_interface && !is_abstract) || 4557 (is_interface && major_gte_15 && (is_super || is_enum)) || 4558 (!is_interface && major_gte_15 && is_annotation)) { 4559 ResourceMark rm(THREAD); 4560 Exceptions::fthrow( 4561 THREAD_AND_LOCATION, 4562 vmSymbols::java_lang_ClassFormatError(), 4563 "Illegal class modifiers in class %s: 0x%X", 4564 _class_name->as_C_string(), flags 4565 ); 4566 return; 4567 } 4568 } 4569 4570 static bool has_illegal_visibility(jint flags) { 4571 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4572 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4573 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4574 4575 return ((is_public && is_protected) || 4576 (is_public && is_private) || 4577 (is_protected && is_private)); 4578 } 4579 4580 static bool is_supported_version(u2 major, u2 minor){ 4581 const u2 max_version = JAVA_MAX_SUPPORTED_VERSION; 4582 return (major >= JAVA_MIN_SUPPORTED_VERSION) && 4583 (major <= max_version) && 4584 ((major != max_version) || 4585 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION)); 4586 } 4587 4588 void ClassFileParser::verify_legal_field_modifiers(jint flags, 4589 bool is_interface, 4590 TRAPS) const { 4591 if (!_need_verify) { return; } 4592 4593 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4594 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4595 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4596 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 4597 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4598 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0; 4599 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0; 4600 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 4601 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4602 4603 bool is_illegal = false; 4604 4605 if (is_interface) { 4606 if (!is_public || !is_static || !is_final || is_private || 4607 is_protected || is_volatile || is_transient || 4608 (major_gte_15 && is_enum)) { 4609 is_illegal = true; 4610 } 4611 } else { // not interface 4612 if (has_illegal_visibility(flags) || (is_final && is_volatile)) { 4613 is_illegal = true; 4614 } 4615 } 4616 4617 if (is_illegal) { 4618 ResourceMark rm(THREAD); 4619 Exceptions::fthrow( 4620 THREAD_AND_LOCATION, 4621 vmSymbols::java_lang_ClassFormatError(), 4622 "Illegal field modifiers in class %s: 0x%X", 4623 _class_name->as_C_string(), flags); 4624 return; 4625 } 4626 } 4627 4628 void ClassFileParser::verify_legal_method_modifiers(jint flags, 4629 bool is_interface, 4630 const Symbol* name, 4631 TRAPS) const { 4632 if (!_need_verify) { return; } 4633 4634 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4635 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4636 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 4637 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4638 const bool is_native = (flags & JVM_ACC_NATIVE) != 0; 4639 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 4640 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0; 4641 const bool is_strict = (flags & JVM_ACC_STRICT) != 0; 4642 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0; 4643 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4644 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 4645 const bool major_gte_8 = _major_version >= JAVA_8_VERSION; 4646 const bool is_initializer = (name == vmSymbols::object_initializer_name()); 4647 4648 bool is_illegal = false; 4649 4650 if (is_interface) { 4651 if (major_gte_8) { 4652 // Class file version is JAVA_8_VERSION or later Methods of 4653 // interfaces may set any of the flags except ACC_PROTECTED, 4654 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must 4655 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. 4656 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */ 4657 (is_native || is_protected || is_final || is_synchronized) || 4658 // If a specific method of a class or interface has its 4659 // ACC_ABSTRACT flag set, it must not have any of its 4660 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, 4661 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to 4662 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as 4663 // those flags are illegal irrespective of ACC_ABSTRACT being set or not. 4664 (is_abstract && (is_private || is_static || is_strict))) { 4665 is_illegal = true; 4666 } 4667 } else if (major_gte_15) { 4668 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION) 4669 if (!is_public || is_private || is_protected || is_static || is_final || 4670 is_synchronized || is_native || !is_abstract || is_strict) { 4671 is_illegal = true; 4672 } 4673 } else { 4674 // Class file version is pre-JAVA_1_5_VERSION 4675 if (!is_public || is_static || is_final || is_native || !is_abstract) { 4676 is_illegal = true; 4677 } 4678 } 4679 } else { // not interface 4680 if (has_illegal_visibility(flags)) { 4681 is_illegal = true; 4682 } else { 4683 if (is_initializer) { 4684 if (is_static || is_final || is_synchronized || is_native || 4685 is_abstract || (major_gte_15 && is_bridge)) { 4686 is_illegal = true; 4687 } 4688 } else { // not initializer 4689 if (is_abstract) { 4690 if ((is_final || is_native || is_private || is_static || 4691 (major_gte_15 && (is_synchronized || is_strict)))) { 4692 is_illegal = true; 4693 } 4694 } 4695 } 4696 } 4697 } 4698 4699 if (is_illegal) { 4700 ResourceMark rm(THREAD); 4701 Exceptions::fthrow( 4702 THREAD_AND_LOCATION, 4703 vmSymbols::java_lang_ClassFormatError(), 4704 "Method %s in class %s has illegal modifiers: 0x%X", 4705 name->as_C_string(), _class_name->as_C_string(), flags); 4706 return; 4707 } 4708 } 4709 4710 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, 4711 int length, 4712 TRAPS) const { 4713 assert(_need_verify, "only called when _need_verify is true"); 4714 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) { 4715 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); 4716 } 4717 } 4718 4719 // Unqualified names may not contain the characters '.', ';', '[', or '/'. 4720 // In class names, '/' separates unqualified names. This is verified in this function also. 4721 // Method names also may not contain the characters '<' or '>', unless <init> 4722 // or <clinit>. Note that method names may not be <init> or <clinit> in this 4723 // method. Because these names have been checked as special cases before 4724 // calling this method in verify_legal_method_name. 4725 // 4726 // This method is also called from the modular system APIs in modules.cpp 4727 // to verify the validity of module and package names. 4728 bool ClassFileParser::verify_unqualified_name(const char* name, 4729 unsigned int length, 4730 int type) { 4731 for (const char* p = name; p != name + length;) { 4732 jchar ch = *p; 4733 if (ch < 128) { 4734 if (ch == '.' || ch == ';' || ch == '[' ) { 4735 return false; // do not permit '.', ';', or '[' 4736 } 4737 if (ch == '/') { 4738 // check for '//' or leading or trailing '/' which are not legal 4739 // unqualified name must not be empty 4740 if (type == ClassFileParser::LegalClass) { 4741 if (p == name || p+1 >= name+length || *(p+1) == '/') { 4742 return false; 4743 } 4744 } else { 4745 return false; // do not permit '/' unless it's class name 4746 } 4747 } 4748 if (type == ClassFileParser::LegalMethod && (ch == '<' || ch == '>')) { 4749 return false; // do not permit '<' or '>' in method names 4750 } 4751 p++; 4752 } else { 4753 char* tmp_p = UTF8::next(p, &ch); 4754 p = tmp_p; 4755 } 4756 } 4757 return true; 4758 } 4759 4760 // Take pointer to a string. Skip over the longest part of the string that could 4761 // be taken as a fieldname. Allow '/' if slash_ok is true. 4762 // Return a pointer to just past the fieldname. 4763 // Return NULL if no fieldname at all was found, or in the case of slash_ok 4764 // being true, we saw consecutive slashes (meaning we were looking for a 4765 // qualified path but found something that was badly-formed). 4766 static const char* skip_over_field_name(const char* name, 4767 bool slash_ok, 4768 unsigned int length) { 4769 const char* p; 4770 jboolean last_is_slash = false; 4771 jboolean not_first_ch = false; 4772 4773 for (p = name; p != name + length; not_first_ch = true) { 4774 const char* old_p = p; 4775 jchar ch = *p; 4776 if (ch < 128) { 4777 p++; 4778 // quick check for ascii 4779 if ((ch >= 'a' && ch <= 'z') || 4780 (ch >= 'A' && ch <= 'Z') || 4781 (ch == '_' || ch == '$') || 4782 (not_first_ch && ch >= '0' && ch <= '9')) { 4783 last_is_slash = false; 4784 continue; 4785 } 4786 if (slash_ok && ch == '/') { 4787 if (last_is_slash) { 4788 return NULL; // Don't permit consecutive slashes 4789 } 4790 last_is_slash = true; 4791 continue; 4792 } 4793 } 4794 else { 4795 jint unicode_ch; 4796 char* tmp_p = UTF8::next_character(p, &unicode_ch); 4797 p = tmp_p; 4798 last_is_slash = false; 4799 // Check if ch is Java identifier start or is Java identifier part 4800 // 4672820: call java.lang.Character methods directly without generating separate tables. 4801 EXCEPTION_MARK; 4802 4803 // return value 4804 JavaValue result(T_BOOLEAN); 4805 // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart 4806 JavaCallArguments args; 4807 args.push_int(unicode_ch); 4808 4809 // public static boolean isJavaIdentifierStart(char ch); 4810 JavaCalls::call_static(&result, 4811 SystemDictionary::Character_klass(), 4812 vmSymbols::isJavaIdentifierStart_name(), 4813 vmSymbols::int_bool_signature(), 4814 &args, 4815 THREAD); 4816 4817 if (HAS_PENDING_EXCEPTION) { 4818 CLEAR_PENDING_EXCEPTION; 4819 return 0; 4820 } 4821 if (result.get_jboolean()) { 4822 continue; 4823 } 4824 4825 if (not_first_ch) { 4826 // public static boolean isJavaIdentifierPart(char ch); 4827 JavaCalls::call_static(&result, 4828 SystemDictionary::Character_klass(), 4829 vmSymbols::isJavaIdentifierPart_name(), 4830 vmSymbols::int_bool_signature(), 4831 &args, 4832 THREAD); 4833 4834 if (HAS_PENDING_EXCEPTION) { 4835 CLEAR_PENDING_EXCEPTION; 4836 return 0; 4837 } 4838 4839 if (result.get_jboolean()) { 4840 continue; 4841 } 4842 } 4843 } 4844 return (not_first_ch) ? old_p : NULL; 4845 } 4846 return (not_first_ch) ? p : NULL; 4847 } 4848 4849 // Take pointer to a string. Skip over the longest part of the string that could 4850 // be taken as a field signature. Allow "void" if void_ok. 4851 // Return a pointer to just past the signature. 4852 // Return NULL if no legal signature is found. 4853 const char* ClassFileParser::skip_over_field_signature(const char* signature, 4854 bool void_ok, 4855 unsigned int length, 4856 TRAPS) const { 4857 unsigned int array_dim = 0; 4858 while (length > 0) { 4859 switch (signature[0]) { 4860 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; } 4861 case JVM_SIGNATURE_BOOLEAN: 4862 case JVM_SIGNATURE_BYTE: 4863 case JVM_SIGNATURE_CHAR: 4864 case JVM_SIGNATURE_SHORT: 4865 case JVM_SIGNATURE_INT: 4866 case JVM_SIGNATURE_FLOAT: 4867 case JVM_SIGNATURE_LONG: 4868 case JVM_SIGNATURE_DOUBLE: 4869 return signature + 1; 4870 case JVM_SIGNATURE_CLASS: { 4871 if (_major_version < JAVA_1_5_VERSION) { 4872 // Skip over the class name if one is there 4873 const char* const p = skip_over_field_name(signature + 1, true, --length); 4874 4875 // The next character better be a semicolon 4876 if (p && (p - signature) > 1 && p[0] == ';') { 4877 return p + 1; 4878 } 4879 } 4880 else { 4881 // Skip leading 'L' and ignore first appearance of ';' 4882 length--; 4883 signature++; 4884 char* c = strchr((char*) signature, ';'); 4885 // Format check signature 4886 if (c != NULL) { 4887 ResourceMark rm(THREAD); 4888 int newlen = c - (char*) signature; 4889 char* sig = NEW_RESOURCE_ARRAY(char, newlen + 1); 4890 strncpy(sig, signature, newlen); 4891 sig[newlen] = '\0'; 4892 4893 bool legal = verify_unqualified_name(sig, newlen, LegalClass); 4894 if (!legal) { 4895 classfile_parse_error("Class name contains illegal character " 4896 "in descriptor in class file %s", 4897 CHECK_0); 4898 return NULL; 4899 } 4900 return signature + newlen + 1; 4901 } 4902 } 4903 return NULL; 4904 } 4905 case JVM_SIGNATURE_ARRAY: 4906 array_dim++; 4907 if (array_dim > 255) { 4908 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions. 4909 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0); 4910 } 4911 // The rest of what's there better be a legal signature 4912 signature++; 4913 length--; 4914 void_ok = false; 4915 break; 4916 default: 4917 return NULL; 4918 } 4919 } 4920 return NULL; 4921 } 4922 4923 // Checks if name is a legal class name. 4924 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const { 4925 if (!_need_verify || _relax_verify) { return; } 4926 4927 char buf[fixed_buffer_size]; 4928 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 4929 unsigned int length = name->utf8_length(); 4930 bool legal = false; 4931 4932 if (length > 0) { 4933 const char* p; 4934 if (bytes[0] == JVM_SIGNATURE_ARRAY) { 4935 p = skip_over_field_signature(bytes, false, length, CHECK); 4936 legal = (p != NULL) && ((p - bytes) == (int)length); 4937 } else if (_major_version < JAVA_1_5_VERSION) { 4938 if (bytes[0] != '<') { 4939 p = skip_over_field_name(bytes, true, length); 4940 legal = (p != NULL) && ((p - bytes) == (int)length); 4941 } 4942 } else { 4943 // 4900761: relax the constraints based on JSR202 spec 4944 // Class names may be drawn from the entire Unicode character set. 4945 // Identifiers between '/' must be unqualified names. 4946 // The utf8 string has been verified when parsing cpool entries. 4947 legal = verify_unqualified_name(bytes, length, LegalClass); 4948 } 4949 } 4950 if (!legal) { 4951 ResourceMark rm(THREAD); 4952 assert(_class_name != NULL, "invariant"); 4953 Exceptions::fthrow( 4954 THREAD_AND_LOCATION, 4955 vmSymbols::java_lang_ClassFormatError(), 4956 "Illegal class name \"%s\" in class file %s", bytes, 4957 _class_name->as_C_string() 4958 ); 4959 return; 4960 } 4961 } 4962 4963 // Checks if name is a legal field name. 4964 void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const { 4965 if (!_need_verify || _relax_verify) { return; } 4966 4967 char buf[fixed_buffer_size]; 4968 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 4969 unsigned int length = name->utf8_length(); 4970 bool legal = false; 4971 4972 if (length > 0) { 4973 if (_major_version < JAVA_1_5_VERSION) { 4974 if (bytes[0] != '<') { 4975 const char* p = skip_over_field_name(bytes, false, length); 4976 legal = (p != NULL) && ((p - bytes) == (int)length); 4977 } 4978 } else { 4979 // 4881221: relax the constraints based on JSR202 spec 4980 legal = verify_unqualified_name(bytes, length, LegalField); 4981 } 4982 } 4983 4984 if (!legal) { 4985 ResourceMark rm(THREAD); 4986 assert(_class_name != NULL, "invariant"); 4987 Exceptions::fthrow( 4988 THREAD_AND_LOCATION, 4989 vmSymbols::java_lang_ClassFormatError(), 4990 "Illegal field name \"%s\" in class %s", bytes, 4991 _class_name->as_C_string() 4992 ); 4993 return; 4994 } 4995 } 4996 4997 // Checks if name is a legal method name. 4998 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const { 4999 if (!_need_verify || _relax_verify) { return; } 5000 5001 assert(name != NULL, "method name is null"); 5002 char buf[fixed_buffer_size]; 5003 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 5004 unsigned int length = name->utf8_length(); 5005 bool legal = false; 5006 5007 if (length > 0) { 5008 if (bytes[0] == '<') { 5009 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) { 5010 legal = true; 5011 } 5012 } else if (_major_version < JAVA_1_5_VERSION) { 5013 const char* p; 5014 p = skip_over_field_name(bytes, false, length); 5015 legal = (p != NULL) && ((p - bytes) == (int)length); 5016 } else { 5017 // 4881221: relax the constraints based on JSR202 spec 5018 legal = verify_unqualified_name(bytes, length, LegalMethod); 5019 } 5020 } 5021 5022 if (!legal) { 5023 ResourceMark rm(THREAD); 5024 assert(_class_name != NULL, "invariant"); 5025 Exceptions::fthrow( 5026 THREAD_AND_LOCATION, 5027 vmSymbols::java_lang_ClassFormatError(), 5028 "Illegal method name \"%s\" in class %s", bytes, 5029 _class_name->as_C_string() 5030 ); 5031 return; 5032 } 5033 } 5034 5035 5036 // Checks if signature is a legal field signature. 5037 void ClassFileParser::verify_legal_field_signature(const Symbol* name, 5038 const Symbol* signature, 5039 TRAPS) const { 5040 if (!_need_verify) { return; } 5041 5042 char buf[fixed_buffer_size]; 5043 const char* const bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 5044 const unsigned int length = signature->utf8_length(); 5045 const char* const p = skip_over_field_signature(bytes, false, length, CHECK); 5046 5047 if (p == NULL || (p - bytes) != (int)length) { 5048 throwIllegalSignature("Field", name, signature, CHECK); 5049 } 5050 } 5051 5052 // Checks if signature is a legal method signature. 5053 // Returns number of parameters 5054 int ClassFileParser::verify_legal_method_signature(const Symbol* name, 5055 const Symbol* signature, 5056 TRAPS) const { 5057 if (!_need_verify) { 5058 // make sure caller's args_size will be less than 0 even for non-static 5059 // method so it will be recomputed in compute_size_of_parameters(). 5060 return -2; 5061 } 5062 5063 // Class initializers cannot have args for class format version >= 51. 5064 if (name == vmSymbols::class_initializer_name() && 5065 signature != vmSymbols::void_method_signature() && 5066 _major_version >= JAVA_7_VERSION) { 5067 throwIllegalSignature("Method", name, signature, CHECK_0); 5068 return 0; 5069 } 5070 5071 unsigned int args_size = 0; 5072 char buf[fixed_buffer_size]; 5073 const char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); 5074 unsigned int length = signature->utf8_length(); 5075 const char* nextp; 5076 5077 // The first character must be a '(' 5078 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) { 5079 length--; 5080 // Skip over legal field signatures 5081 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5082 while ((length > 0) && (nextp != NULL)) { 5083 args_size++; 5084 if (p[0] == 'J' || p[0] == 'D') { 5085 args_size++; 5086 } 5087 length -= nextp - p; 5088 p = nextp; 5089 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5090 } 5091 // The first non-signature thing better be a ')' 5092 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) { 5093 length--; 5094 if (name->utf8_length() > 0 && name->byte_at(0) == '<') { 5095 // All internal methods must return void 5096 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) { 5097 return args_size; 5098 } 5099 } else { 5100 // Now we better just have a return value 5101 nextp = skip_over_field_signature(p, true, length, CHECK_0); 5102 if (nextp && ((int)length == (nextp - p))) { 5103 return args_size; 5104 } 5105 } 5106 } 5107 } 5108 // Report error 5109 throwIllegalSignature("Method", name, signature, CHECK_0); 5110 return 0; 5111 } 5112 5113 int ClassFileParser::static_field_size() const { 5114 assert(_field_info != NULL, "invariant"); 5115 return _field_info->static_field_size; 5116 } 5117 5118 int ClassFileParser::total_oop_map_count() const { 5119 assert(_field_info != NULL, "invariant"); 5120 return _field_info->total_oop_map_count; 5121 } 5122 5123 jint ClassFileParser::layout_size() const { 5124 assert(_field_info != NULL, "invariant"); 5125 return _field_info->instance_size; 5126 } 5127 5128 static void check_methods_for_intrinsics(const InstanceKlass* ik, 5129 const Array<Method*>* methods) { 5130 assert(ik != NULL, "invariant"); 5131 assert(methods != NULL, "invariant"); 5132 5133 // Set up Method*::intrinsic_id as soon as we know the names of methods. 5134 // (We used to do this lazily, but now we query it in Rewriter, 5135 // which is eagerly done for every method, so we might as well do it now, 5136 // when everything is fresh in memory.) 5137 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik); 5138 5139 if (klass_id != vmSymbols::NO_SID) { 5140 for (int j = 0; j < methods->length(); ++j) { 5141 Method* method = methods->at(j); 5142 method->init_intrinsic_id(); 5143 5144 if (CheckIntrinsics) { 5145 // Check if an intrinsic is defined for method 'method', 5146 // but the method is not annotated with @HotSpotIntrinsicCandidate. 5147 if (method->intrinsic_id() != vmIntrinsics::_none && 5148 !method->intrinsic_candidate()) { 5149 tty->print("Compiler intrinsic is defined for method [%s], " 5150 "but the method is not annotated with @HotSpotIntrinsicCandidate.%s", 5151 method->name_and_sig_as_C_string(), 5152 NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.") 5153 ); 5154 tty->cr(); 5155 DEBUG_ONLY(vm_exit(1)); 5156 } 5157 // Check is the method 'method' is annotated with @HotSpotIntrinsicCandidate, 5158 // but there is no intrinsic available for it. 5159 if (method->intrinsic_candidate() && 5160 method->intrinsic_id() == vmIntrinsics::_none) { 5161 tty->print("Method [%s] is annotated with @HotSpotIntrinsicCandidate, " 5162 "but no compiler intrinsic is defined for the method.%s", 5163 method->name_and_sig_as_C_string(), 5164 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5165 ); 5166 tty->cr(); 5167 DEBUG_ONLY(vm_exit(1)); 5168 } 5169 } 5170 } // end for 5171 5172 #ifdef ASSERT 5173 if (CheckIntrinsics) { 5174 // Check for orphan methods in the current class. A method m 5175 // of a class C is orphan if an intrinsic is defined for method m, 5176 // but class C does not declare m. 5177 // The check is potentially expensive, therefore it is available 5178 // only in debug builds. 5179 5180 for (int id = vmIntrinsics::FIRST_ID; id < (int)vmIntrinsics::ID_LIMIT; ++id) { 5181 if (vmIntrinsics::_compiledLambdaForm == id) { 5182 // The _compiledLamdbdaForm intrinsic is a special marker for bytecode 5183 // generated for the JVM from a LambdaForm and therefore no method 5184 // is defined for it. 5185 continue; 5186 } 5187 5188 if (vmIntrinsics::class_for(vmIntrinsics::ID_from(id)) == klass_id) { 5189 // Check if the current class contains a method with the same 5190 // name, flags, signature. 5191 bool match = false; 5192 for (int j = 0; j < methods->length(); ++j) { 5193 const Method* method = methods->at(j); 5194 if (method->intrinsic_id() == id) { 5195 match = true; 5196 break; 5197 } 5198 } 5199 5200 if (!match) { 5201 char buf[1000]; 5202 tty->print("Compiler intrinsic is defined for method [%s], " 5203 "but the method is not available in class [%s].%s", 5204 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id), 5205 buf, sizeof(buf)), 5206 ik->name()->as_C_string(), 5207 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5208 ); 5209 tty->cr(); 5210 DEBUG_ONLY(vm_exit(1)); 5211 } 5212 } 5213 } // end for 5214 } // CheckIntrinsics 5215 #endif // ASSERT 5216 } 5217 } 5218 5219 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) { 5220 if (_klass != NULL) { 5221 return _klass; 5222 } 5223 5224 InstanceKlass* const ik = 5225 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL); 5226 5227 fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL); 5228 5229 assert(_klass == ik, "invariant"); 5230 5231 ik->set_has_passed_fingerprint_check(false); 5232 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) { 5233 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik); 5234 if (aot_fp != 0 && aot_fp == _stream->compute_fingerprint()) { 5235 // This class matches with a class saved in an AOT library 5236 ik->set_has_passed_fingerprint_check(true); 5237 } else { 5238 ResourceMark rm; 5239 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, 5240 ik->external_name(), aot_fp, _stream->compute_fingerprint()); 5241 } 5242 } 5243 5244 return ik; 5245 } 5246 5247 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) { 5248 assert(ik != NULL, "invariant"); 5249 5250 set_klass_to_deallocate(ik); 5251 5252 assert(_field_info != NULL, "invariant"); 5253 assert(ik->static_field_size() == _field_info->static_field_size, "sanity"); 5254 assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count, 5255 "sanity"); 5256 5257 assert(ik->is_instance_klass(), "sanity"); 5258 assert(ik->size_helper() == _field_info->instance_size, "sanity"); 5259 5260 // Fill in information already parsed 5261 ik->set_should_verify_class(_need_verify); 5262 5263 // Not yet: supers are done below to support the new subtype-checking fields 5264 ik->set_class_loader_data(_loader_data); 5265 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size); 5266 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields); 5267 assert(_fac != NULL, "invariant"); 5268 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]); 5269 5270 // this transfers ownership of a lot of arrays from 5271 // the parser onto the InstanceKlass* 5272 apply_parsed_class_metadata(ik, _java_fields_count, CHECK); 5273 5274 // note that is not safe to use the fields in the parser from this point on 5275 assert(NULL == _cp, "invariant"); 5276 assert(NULL == _fields, "invariant"); 5277 assert(NULL == _methods, "invariant"); 5278 assert(NULL == _inner_classes, "invariant"); 5279 assert(NULL == _local_interfaces, "invariant"); 5280 assert(NULL == _transitive_interfaces, "invariant"); 5281 assert(NULL == _combined_annotations, "invariant"); 5282 5283 if (_has_final_method) { 5284 ik->set_has_final_method(); 5285 } 5286 5287 ik->copy_method_ordering(_method_ordering, CHECK); 5288 // The InstanceKlass::_methods_jmethod_ids cache 5289 // is managed on the assumption that the initial cache 5290 // size is equal to the number of methods in the class. If 5291 // that changes, then InstanceKlass::idnum_can_increment() 5292 // has to be changed accordingly. 5293 ik->set_initial_method_idnum(ik->methods()->length()); 5294 5295 ik->set_name(_class_name); 5296 5297 if (is_anonymous()) { 5298 // I am well known to myself 5299 ik->constants()->klass_at_put(_this_class_index, ik); // eagerly resolve 5300 } 5301 5302 ik->set_minor_version(_minor_version); 5303 ik->set_major_version(_major_version); 5304 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods); 5305 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods); 5306 5307 if (_host_klass != NULL) { 5308 assert (ik->is_anonymous(), "should be the same"); 5309 ik->set_host_klass(_host_klass); 5310 } 5311 5312 // Set PackageEntry for this_klass 5313 oop cl = ik->class_loader(); 5314 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl)); 5315 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh()); 5316 ik->set_package(cld, CHECK); 5317 5318 const Array<Method*>* const methods = ik->methods(); 5319 assert(methods != NULL, "invariant"); 5320 const int methods_len = methods->length(); 5321 5322 check_methods_for_intrinsics(ik, methods); 5323 5324 // Fill in field values obtained by parse_classfile_attributes 5325 if (_parsed_annotations->has_any_annotations()) { 5326 _parsed_annotations->apply_to(ik); 5327 } 5328 5329 apply_parsed_class_attributes(ik); 5330 5331 // Miranda methods 5332 if ((_num_miranda_methods > 0) || 5333 // if this class introduced new miranda methods or 5334 (_super_klass != NULL && _super_klass->has_miranda_methods()) 5335 // super class exists and this class inherited miranda methods 5336 ) { 5337 ik->set_has_miranda_methods(); // then set a flag 5338 } 5339 5340 // Fill in information needed to compute superclasses. 5341 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), CHECK); 5342 5343 // Initialize itable offset tables 5344 klassItable::setup_itable_offset_table(ik); 5345 5346 // Compute transitive closure of interfaces this class implements 5347 // Do final class setup 5348 fill_oop_maps(ik, 5349 _field_info->nonstatic_oop_map_count, 5350 _field_info->nonstatic_oop_offsets, 5351 _field_info->nonstatic_oop_counts); 5352 5353 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper 5354 set_precomputed_flags(ik); 5355 5356 // check if this class can access its super class 5357 check_super_class_access(ik, CHECK); 5358 5359 // check if this class can access its superinterfaces 5360 check_super_interface_access(ik, CHECK); 5361 5362 // check if this class overrides any final method 5363 check_final_method_override(ik, CHECK); 5364 5365 // reject static interface methods prior to Java 8 5366 if (ik->is_interface() && _major_version < JAVA_8_VERSION) { 5367 check_illegal_static_method(ik, CHECK); 5368 } 5369 5370 // Obtain this_klass' module entry 5371 ModuleEntry* module_entry = ik->module(); 5372 assert(module_entry != NULL, "module_entry should always be set"); 5373 5374 // Obtain java.lang.reflect.Module 5375 Handle module_handle(THREAD, JNIHandles::resolve(module_entry->module())); 5376 5377 // Allocate mirror and initialize static fields 5378 // The create_mirror() call will also call compute_modifiers() 5379 java_lang_Class::create_mirror(ik, 5380 _loader_data->class_loader(), 5381 module_handle, 5382 _protection_domain, 5383 CHECK); 5384 5385 assert(_all_mirandas != NULL, "invariant"); 5386 5387 // Generate any default methods - default methods are public interface methods 5388 // that have a default implementation. This is new with Java 8. 5389 if (_has_nonstatic_concrete_methods) { 5390 DefaultMethods::generate_default_methods(ik, 5391 _all_mirandas, 5392 CHECK); 5393 } 5394 5395 // Add read edges to the unnamed modules of the bootstrap and app class loaders. 5396 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() && 5397 !module_entry->has_default_read_edges()) { 5398 if (!module_entry->set_has_default_read_edges()) { 5399 // We won a potential race 5400 JvmtiExport::add_default_read_edges(module_handle, THREAD); 5401 } 5402 } 5403 5404 // Update the loader_data graph. 5405 record_defined_class_dependencies(ik, CHECK); 5406 5407 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */); 5408 5409 if (!is_internal()) { 5410 if (log_is_enabled(Info, class, load)) { 5411 ResourceMark rm; 5412 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string(); 5413 5414 if (log_is_enabled(Info, class, load)) { 5415 ik->print_loading_log(LogLevel::Info, _loader_data, module_name, _stream); 5416 } 5417 // No 'else' here as logging levels are not mutually exclusive 5418 if (log_is_enabled(Debug, class, load)) { 5419 ik->print_loading_log(LogLevel::Debug, _loader_data, module_name, _stream); 5420 } 5421 } 5422 5423 if (log_is_enabled(Debug, class, resolve)) { 5424 ResourceMark rm; 5425 // print out the superclass. 5426 const char * from = ik->external_name(); 5427 if (ik->java_super() != NULL) { 5428 log_debug(class, resolve)("%s %s (super)", 5429 from, 5430 ik->java_super()->external_name()); 5431 } 5432 // print out each of the interface classes referred to by this class. 5433 const Array<Klass*>* const local_interfaces = ik->local_interfaces(); 5434 if (local_interfaces != NULL) { 5435 const int length = local_interfaces->length(); 5436 for (int i = 0; i < length; i++) { 5437 const Klass* const k = local_interfaces->at(i); 5438 const char * to = k->external_name(); 5439 log_debug(class, resolve)("%s %s (interface)", from, to); 5440 } 5441 } 5442 } 5443 } 5444 5445 TRACE_INIT_ID(ik); 5446 5447 // If we reach here, all is well. 5448 // Now remove the InstanceKlass* from the _klass_to_deallocate field 5449 // in order for it to not be destroyed in the ClassFileParser destructor. 5450 set_klass_to_deallocate(NULL); 5451 5452 // it's official 5453 set_klass(ik); 5454 5455 debug_only(ik->verify();) 5456 } 5457 5458 // For an anonymous class that is in the unnamed package, move it to its host class's 5459 // package by prepending its host class's package name to its class name and setting 5460 // its _class_name field. 5461 void ClassFileParser::prepend_host_package_name(const InstanceKlass* host_klass, TRAPS) { 5462 ResourceMark rm(THREAD); 5463 assert(strrchr(_class_name->as_C_string(), '/') == NULL, 5464 "Anonymous class should not be in a package"); 5465 const char* host_pkg_name = 5466 ClassLoader::package_from_name(host_klass->name()->as_C_string(), NULL); 5467 5468 if (host_pkg_name != NULL) { 5469 size_t host_pkg_len = strlen(host_pkg_name); 5470 int class_name_len = _class_name->utf8_length(); 5471 char* new_anon_name = 5472 NEW_RESOURCE_ARRAY(char, host_pkg_len + 1 + class_name_len); 5473 // Copy host package name and trailing /. 5474 strncpy(new_anon_name, host_pkg_name, host_pkg_len); 5475 new_anon_name[host_pkg_len] = '/'; 5476 // Append anonymous class name. The anonymous class name can contain odd 5477 // characters. So, do a strncpy instead of using sprintf("%s..."). 5478 strncpy(new_anon_name + host_pkg_len + 1, (char *)_class_name->base(), class_name_len); 5479 5480 // Create a symbol and update the anonymous class name. 5481 _class_name = SymbolTable::new_symbol(new_anon_name, 5482 (int)host_pkg_len + 1 + class_name_len, 5483 CHECK); 5484 } 5485 } 5486 5487 // If the host class and the anonymous class are in the same package then do 5488 // nothing. If the anonymous class is in the unnamed package then move it to its 5489 // host's package. If the classes are in different packages then throw an IAE 5490 // exception. 5491 void ClassFileParser::fix_anonymous_class_name(TRAPS) { 5492 assert(_host_klass != NULL, "Expected an anonymous class"); 5493 5494 const jbyte* anon_last_slash = UTF8::strrchr(_class_name->base(), 5495 _class_name->utf8_length(), '/'); 5496 if (anon_last_slash == NULL) { // Unnamed package 5497 prepend_host_package_name(_host_klass, CHECK); 5498 } else { 5499 if (!InstanceKlass::is_same_class_package(_host_klass->class_loader(), 5500 _host_klass->name(), 5501 _host_klass->class_loader(), 5502 _class_name)) { 5503 ResourceMark rm(THREAD); 5504 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 5505 err_msg("Host class %s and anonymous class %s are in different packages", 5506 _host_klass->name()->as_C_string(), _class_name->as_C_string())); 5507 } 5508 } 5509 } 5510 5511 static bool relax_format_check_for(ClassLoaderData* loader_data) { 5512 bool trusted = (loader_data->is_the_null_class_loader_data() || 5513 SystemDictionary::is_platform_class_loader(loader_data->class_loader())); 5514 bool need_verify = 5515 // verifyAll 5516 (BytecodeVerificationLocal && BytecodeVerificationRemote) || 5517 // verifyRemote 5518 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted); 5519 return !need_verify; 5520 } 5521 5522 ClassFileParser::ClassFileParser(ClassFileStream* stream, 5523 Symbol* name, 5524 ClassLoaderData* loader_data, 5525 Handle protection_domain, 5526 const InstanceKlass* host_klass, 5527 GrowableArray<Handle>* cp_patches, 5528 Publicity pub_level, 5529 TRAPS) : 5530 _stream(stream), 5531 _requested_name(name), 5532 _loader_data(loader_data), 5533 _host_klass(host_klass), 5534 _cp_patches(cp_patches), 5535 _super_klass(), 5536 _cp(NULL), 5537 _fields(NULL), 5538 _methods(NULL), 5539 _inner_classes(NULL), 5540 _local_interfaces(NULL), 5541 _transitive_interfaces(NULL), 5542 _combined_annotations(NULL), 5543 _annotations(NULL), 5544 _type_annotations(NULL), 5545 _fields_annotations(NULL), 5546 _fields_type_annotations(NULL), 5547 _klass(NULL), 5548 _klass_to_deallocate(NULL), 5549 _parsed_annotations(NULL), 5550 _fac(NULL), 5551 _field_info(NULL), 5552 _method_ordering(NULL), 5553 _all_mirandas(NULL), 5554 _vtable_size(0), 5555 _itable_size(0), 5556 _num_miranda_methods(0), 5557 _rt(REF_NONE), 5558 _protection_domain(protection_domain), 5559 _access_flags(), 5560 _pub_level(pub_level), 5561 _synthetic_flag(false), 5562 _sde_length(false), 5563 _sde_buffer(NULL), 5564 _sourcefile_index(0), 5565 _generic_signature_index(0), 5566 _major_version(0), 5567 _minor_version(0), 5568 _this_class_index(0), 5569 _super_class_index(0), 5570 _itfs_len(0), 5571 _java_fields_count(0), 5572 _need_verify(false), 5573 _relax_verify(false), 5574 _has_nonstatic_concrete_methods(false), 5575 _declares_nonstatic_concrete_methods(false), 5576 _has_final_method(false), 5577 _has_finalizer(false), 5578 _has_empty_finalizer(false), 5579 _has_vanilla_constructor(false), 5580 _max_bootstrap_specifier_index(-1) { 5581 5582 _class_name = name != NULL ? name : vmSymbols::unknown_class_name(); 5583 5584 assert(THREAD->is_Java_thread(), "invariant"); 5585 assert(_loader_data != NULL, "invariant"); 5586 assert(stream != NULL, "invariant"); 5587 assert(_stream != NULL, "invariant"); 5588 assert(_stream->buffer() == _stream->current(), "invariant"); 5589 assert(_class_name != NULL, "invariant"); 5590 assert(0 == _access_flags.as_int(), "invariant"); 5591 5592 // Figure out whether we can skip format checking (matching classic VM behavior) 5593 if (DumpSharedSpaces) { 5594 // verify == true means it's a 'remote' class (i.e., non-boot class) 5595 // Verification decision is based on BytecodeVerificationRemote flag 5596 // for those classes. 5597 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote : 5598 BytecodeVerificationLocal; 5599 } 5600 else { 5601 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(), 5602 stream->need_verify()); 5603 } 5604 5605 // synch back verification state to stream 5606 stream->set_verify(_need_verify); 5607 5608 // Check if verification needs to be relaxed for this class file 5609 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) 5610 _relax_verify = relax_format_check_for(_loader_data); 5611 5612 parse_stream(stream, CHECK); 5613 5614 post_process_parsed_stream(stream, _cp, CHECK); 5615 } 5616 5617 void ClassFileParser::clear_class_metadata() { 5618 // metadata created before the instance klass is created. Must be 5619 // deallocated if classfile parsing returns an error. 5620 _cp = NULL; 5621 _fields = NULL; 5622 _methods = NULL; 5623 _inner_classes = NULL; 5624 _local_interfaces = NULL; 5625 _transitive_interfaces = NULL; 5626 _combined_annotations = NULL; 5627 _annotations = _type_annotations = NULL; 5628 _fields_annotations = _fields_type_annotations = NULL; 5629 } 5630 5631 // Destructor to clean up 5632 ClassFileParser::~ClassFileParser() { 5633 if (_cp != NULL) { 5634 MetadataFactory::free_metadata(_loader_data, _cp); 5635 } 5636 if (_fields != NULL) { 5637 MetadataFactory::free_array<u2>(_loader_data, _fields); 5638 } 5639 5640 if (_methods != NULL) { 5641 // Free methods 5642 InstanceKlass::deallocate_methods(_loader_data, _methods); 5643 } 5644 5645 // beware of the Universe::empty_blah_array!! 5646 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) { 5647 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 5648 } 5649 5650 // Free interfaces 5651 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass, 5652 _local_interfaces, _transitive_interfaces); 5653 5654 if (_combined_annotations != NULL) { 5655 // After all annotations arrays have been created, they are installed into the 5656 // Annotations object that will be assigned to the InstanceKlass being created. 5657 5658 // Deallocate the Annotations object and the installed annotations arrays. 5659 _combined_annotations->deallocate_contents(_loader_data); 5660 5661 // If the _combined_annotations pointer is non-NULL, 5662 // then the other annotations fields should have been cleared. 5663 assert(_annotations == NULL, "Should have been cleared"); 5664 assert(_type_annotations == NULL, "Should have been cleared"); 5665 assert(_fields_annotations == NULL, "Should have been cleared"); 5666 assert(_fields_type_annotations == NULL, "Should have been cleared"); 5667 } else { 5668 // If the annotations arrays were not installed into the Annotations object, 5669 // then they have to be deallocated explicitly. 5670 MetadataFactory::free_array<u1>(_loader_data, _annotations); 5671 MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 5672 Annotations::free_contents(_loader_data, _fields_annotations); 5673 Annotations::free_contents(_loader_data, _fields_type_annotations); 5674 } 5675 5676 clear_class_metadata(); 5677 5678 // deallocate the klass if already created. Don't directly deallocate, but add 5679 // to the deallocate list so that the klass is removed from the CLD::_klasses list 5680 // at a safepoint. 5681 if (_klass_to_deallocate != NULL) { 5682 _loader_data->add_to_deallocate_list(_klass_to_deallocate); 5683 } 5684 } 5685 5686 void ClassFileParser::parse_stream(const ClassFileStream* const stream, 5687 TRAPS) { 5688 5689 assert(stream != NULL, "invariant"); 5690 assert(_class_name != NULL, "invariant"); 5691 5692 // BEGIN STREAM PARSING 5693 stream->guarantee_more(8, CHECK); // magic, major, minor 5694 // Magic value 5695 const u4 magic = stream->get_u4_fast(); 5696 guarantee_property(magic == JAVA_CLASSFILE_MAGIC, 5697 "Incompatible magic value %u in class file %s", 5698 magic, CHECK); 5699 5700 // Version numbers 5701 _minor_version = stream->get_u2_fast(); 5702 _major_version = stream->get_u2_fast(); 5703 5704 if (DumpSharedSpaces && _major_version < JAVA_1_5_VERSION) { 5705 ResourceMark rm; 5706 warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s", 5707 _major_version, _minor_version, _class_name->as_C_string()); 5708 Exceptions::fthrow( 5709 THREAD_AND_LOCATION, 5710 vmSymbols::java_lang_UnsupportedClassVersionError(), 5711 "Unsupported major.minor version for dump time %u.%u", 5712 _major_version, 5713 _minor_version); 5714 } 5715 5716 // Check version numbers - we check this even with verifier off 5717 if (!is_supported_version(_major_version, _minor_version)) { 5718 ResourceMark rm(THREAD); 5719 Exceptions::fthrow( 5720 THREAD_AND_LOCATION, 5721 vmSymbols::java_lang_UnsupportedClassVersionError(), 5722 "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), " 5723 "this version of the Java Runtime only recognizes class file versions up to %u.%u", 5724 _class_name->as_C_string(), 5725 _major_version, 5726 _minor_version, 5727 JAVA_MAX_SUPPORTED_VERSION, 5728 JAVA_MAX_SUPPORTED_MINOR_VERSION); 5729 return; 5730 } 5731 5732 stream->guarantee_more(3, CHECK); // length, first cp tag 5733 const u2 cp_size = stream->get_u2_fast(); 5734 5735 guarantee_property( 5736 cp_size >= 1, "Illegal constant pool size %u in class file %s", 5737 cp_size, CHECK); 5738 5739 _cp = ConstantPool::allocate(_loader_data, 5740 cp_size, 5741 CHECK); 5742 5743 ConstantPool* const cp = _cp; 5744 5745 parse_constant_pool(stream, cp, cp_size, CHECK); 5746 5747 assert(cp_size == (const u2)cp->length(), "invariant"); 5748 5749 // ACCESS FLAGS 5750 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 5751 5752 // Access flags 5753 jint flags; 5754 // JVM_ACC_MODULE is defined in JDK-9 and later. 5755 if (_major_version >= JAVA_9_VERSION) { 5756 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE); 5757 } else { 5758 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS; 5759 } 5760 5761 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 5762 // Set abstract bit for old class files for backward compatibility 5763 flags |= JVM_ACC_ABSTRACT; 5764 } 5765 5766 verify_legal_class_modifiers(flags, CHECK); 5767 5768 _access_flags.set_flags(flags); 5769 5770 5771 // This class and superclass 5772 _this_class_index = stream->get_u2_fast(); 5773 check_property( 5774 valid_cp_range(_this_class_index, cp_size) && 5775 cp->tag_at(_this_class_index).is_unresolved_klass(), 5776 "Invalid this class index %u in constant pool in class file %s", 5777 _this_class_index, CHECK); 5778 5779 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index); 5780 assert(class_name_in_cp != NULL, "class_name can't be null"); 5781 5782 // Update _class_name which could be null previously 5783 // to reflect the name in the constant pool 5784 _class_name = class_name_in_cp; 5785 5786 // Don't need to check whether this class name is legal or not. 5787 // It has been checked when constant pool is parsed. 5788 // However, make sure it is not an array type. 5789 if (_need_verify) { 5790 guarantee_property(_class_name->byte_at(0) != JVM_SIGNATURE_ARRAY, 5791 "Bad class name in class file %s", 5792 CHECK); 5793 } 5794 5795 // Checks if name in class file matches requested name 5796 if (_requested_name != NULL && _requested_name != _class_name) { 5797 ResourceMark rm(THREAD); 5798 Exceptions::fthrow( 5799 THREAD_AND_LOCATION, 5800 vmSymbols::java_lang_NoClassDefFoundError(), 5801 "%s (wrong name: %s)", 5802 _class_name->as_C_string(), 5803 _requested_name != NULL ? _requested_name->as_C_string() : "NoName" 5804 ); 5805 return; 5806 } 5807 5808 // if this is an anonymous class fix up its name if it's in the unnamed 5809 // package. Otherwise, throw IAE if it is in a different package than 5810 // its host class. 5811 if (_host_klass != NULL) { 5812 fix_anonymous_class_name(CHECK); 5813 } 5814 5815 // Verification prevents us from creating names with dots in them, this 5816 // asserts that that's the case. 5817 assert(is_internal_format(_class_name), "external class name format used internally"); 5818 5819 if (!is_internal()) { 5820 if (log_is_enabled(Debug, class, preorder)){ 5821 ResourceMark rm(THREAD); 5822 outputStream* log = Log(class, preorder)::debug_stream(); 5823 log->print("%s", _class_name->as_klass_external_name()); 5824 if (stream->source() != NULL) { 5825 log->print(" source: %s", stream->source()); 5826 } 5827 log->cr(); 5828 } 5829 5830 #if INCLUDE_CDS 5831 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) { 5832 // Only dump the classes that can be stored into CDS archive. 5833 // Anonymous classes such as generated LambdaForm classes are also not included. 5834 if (SystemDictionaryShared::is_sharing_possible(_loader_data) && 5835 _host_klass == NULL) { 5836 oop class_loader = _loader_data->class_loader(); 5837 ResourceMark rm(THREAD); 5838 // For the boot and platform class loaders, check if the class is not found in the 5839 // java runtime image. Additional check for the boot class loader is if the class 5840 // is not found in the boot loader's appended entries. This indicates that the class 5841 // is not useable during run time, such as the ones found in the --patch-module entries, 5842 // so it should not be included in the classlist file. 5843 if (((class_loader == NULL && !ClassLoader::contains_append_entry(stream->source())) || 5844 SystemDictionary::is_platform_class_loader(class_loader)) && 5845 !ClassLoader::is_jrt(stream->source())) { 5846 tty->print_cr("skip writing class %s from source %s to classlist file", 5847 _class_name->as_C_string(), stream->source()); 5848 } else { 5849 classlist_file->print_cr("%s", _class_name->as_C_string()); 5850 classlist_file->flush(); 5851 } 5852 } 5853 } 5854 #endif 5855 } 5856 5857 // SUPERKLASS 5858 _super_class_index = stream->get_u2_fast(); 5859 _super_klass = parse_super_class(cp, 5860 _super_class_index, 5861 _need_verify, 5862 CHECK); 5863 5864 // Interfaces 5865 _itfs_len = stream->get_u2_fast(); 5866 parse_interfaces(stream, 5867 _itfs_len, 5868 cp, 5869 &_has_nonstatic_concrete_methods, 5870 CHECK); 5871 5872 assert(_local_interfaces != NULL, "invariant"); 5873 5874 // Fields (offsets are filled in later) 5875 _fac = new FieldAllocationCount(); 5876 parse_fields(stream, 5877 _access_flags.is_interface(), 5878 _fac, 5879 cp, 5880 cp_size, 5881 &_java_fields_count, 5882 CHECK); 5883 5884 assert(_fields != NULL, "invariant"); 5885 5886 // Methods 5887 AccessFlags promoted_flags; 5888 parse_methods(stream, 5889 _access_flags.is_interface(), 5890 &promoted_flags, 5891 &_has_final_method, 5892 &_declares_nonstatic_concrete_methods, 5893 CHECK); 5894 5895 assert(_methods != NULL, "invariant"); 5896 5897 // promote flags from parse_methods() to the klass' flags 5898 _access_flags.add_promoted_flags(promoted_flags.as_int()); 5899 5900 if (_declares_nonstatic_concrete_methods) { 5901 _has_nonstatic_concrete_methods = true; 5902 } 5903 5904 // Additional attributes/annotations 5905 _parsed_annotations = new ClassAnnotationCollector(); 5906 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK); 5907 5908 assert(_inner_classes != NULL, "invariant"); 5909 5910 // Finalize the Annotations metadata object, 5911 // now that all annotation arrays have been created. 5912 create_combined_annotations(CHECK); 5913 5914 // Make sure this is the end of class file stream 5915 guarantee_property(stream->at_eos(), 5916 "Extra bytes at the end of class file %s", 5917 CHECK); 5918 5919 // all bytes in stream read and parsed 5920 } 5921 5922 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream, 5923 ConstantPool* cp, 5924 TRAPS) { 5925 assert(stream != NULL, "invariant"); 5926 assert(stream->at_eos(), "invariant"); 5927 assert(cp != NULL, "invariant"); 5928 assert(_loader_data != NULL, "invariant"); 5929 5930 if (_class_name == vmSymbols::java_lang_Object()) { 5931 check_property(_local_interfaces == Universe::the_empty_klass_array(), 5932 "java.lang.Object cannot implement an interface in class file %s", 5933 CHECK); 5934 } 5935 // We check super class after class file is parsed and format is checked 5936 if (_super_class_index > 0 && NULL ==_super_klass) { 5937 Symbol* const super_class_name = cp->klass_name_at(_super_class_index); 5938 if (_access_flags.is_interface()) { 5939 // Before attempting to resolve the superclass, check for class format 5940 // errors not checked yet. 5941 guarantee_property(super_class_name == vmSymbols::java_lang_Object(), 5942 "Interfaces must have java.lang.Object as superclass in class file %s", 5943 CHECK); 5944 } 5945 _super_klass = (const InstanceKlass*) 5946 SystemDictionary::resolve_super_or_fail(_class_name, 5947 super_class_name, 5948 _loader_data->class_loader(), 5949 _protection_domain, 5950 true, 5951 CHECK); 5952 } 5953 5954 if (_super_klass != NULL) { 5955 if (_super_klass->has_nonstatic_concrete_methods()) { 5956 _has_nonstatic_concrete_methods = true; 5957 } 5958 5959 if (_super_klass->is_interface()) { 5960 ResourceMark rm(THREAD); 5961 Exceptions::fthrow( 5962 THREAD_AND_LOCATION, 5963 vmSymbols::java_lang_IncompatibleClassChangeError(), 5964 "class %s has interface %s as super class", 5965 _class_name->as_klass_external_name(), 5966 _super_klass->external_name() 5967 ); 5968 return; 5969 } 5970 // Make sure super class is not final 5971 if (_super_klass->is_final()) { 5972 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class"); 5973 } 5974 } 5975 5976 // Compute the transitive list of all unique interfaces implemented by this class 5977 _transitive_interfaces = 5978 compute_transitive_interfaces(_super_klass, 5979 _local_interfaces, 5980 _loader_data, 5981 CHECK); 5982 5983 assert(_transitive_interfaces != NULL, "invariant"); 5984 5985 // sort methods 5986 _method_ordering = sort_methods(_methods); 5987 5988 _all_mirandas = new GrowableArray<Method*>(20); 5989 5990 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size, 5991 &_num_miranda_methods, 5992 _all_mirandas, 5993 _super_klass, 5994 _methods, 5995 _access_flags, 5996 _major_version, 5997 _loader_data->class_loader(), 5998 _class_name, 5999 _local_interfaces, 6000 CHECK); 6001 6002 // Size of Java itable (in words) 6003 _itable_size = _access_flags.is_interface() ? 0 : 6004 klassItable::compute_itable_size(_transitive_interfaces); 6005 6006 assert(_fac != NULL, "invariant"); 6007 assert(_parsed_annotations != NULL, "invariant"); 6008 6009 _field_info = new FieldLayoutInfo(); 6010 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK); 6011 6012 // Compute reference typ 6013 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type(); 6014 6015 } 6016 6017 void ClassFileParser::set_klass(InstanceKlass* klass) { 6018 6019 #ifdef ASSERT 6020 if (klass != NULL) { 6021 assert(NULL == _klass, "leaking?"); 6022 } 6023 #endif 6024 6025 _klass = klass; 6026 } 6027 6028 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) { 6029 6030 #ifdef ASSERT 6031 if (klass != NULL) { 6032 assert(NULL == _klass_to_deallocate, "leaking?"); 6033 } 6034 #endif 6035 6036 _klass_to_deallocate = klass; 6037 } 6038 6039 // Caller responsible for ResourceMark 6040 // clone stream with rewound position 6041 const ClassFileStream* ClassFileParser::clone_stream() const { 6042 assert(_stream != NULL, "invariant"); 6043 6044 return _stream->clone(); 6045 } 6046 // ---------------------------------------------------------------------------- 6047 // debugging 6048 6049 #ifdef ASSERT 6050 6051 // return true if class_name contains no '.' (internal format is '/') 6052 bool ClassFileParser::is_internal_format(Symbol* class_name) { 6053 if (class_name != NULL) { 6054 ResourceMark rm; 6055 char* name = class_name->as_C_string(); 6056 return strchr(name, '.') == NULL; 6057 } else { 6058 return true; 6059 } 6060 } 6061 6062 #endif