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