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