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