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