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