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 #define JAVA_26_VERSION 70 158 159 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) { 160 assert((bad_constant == JVM_CONSTANT_Module || 161 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION, 162 "Unexpected bad constant pool entry"); 163 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant; 164 } 165 166 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream, 167 ConstantPool* cp, 168 const int length, 169 TRAPS) { 170 assert(stream != nullptr, "invariant"); 171 assert(cp != nullptr, "invariant"); 172 173 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize 174 // this function (_current can be allocated in a register, with scalar 175 // replacement of aggregates). The _current pointer is copied back to 176 // stream() when this function returns. DON'T call another method within 177 // this method that uses stream(). 178 const ClassFileStream cfs1 = *stream; 179 const ClassFileStream* const cfs = &cfs1; 180 181 DEBUG_ONLY(const u1* const old_current = stream->current();) 182 183 // Used for batching symbol allocations. 184 const char* names[SymbolTable::symbol_alloc_batch_size]; 185 int lengths[SymbolTable::symbol_alloc_batch_size]; 186 int indices[SymbolTable::symbol_alloc_batch_size]; 187 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size]; 188 int names_count = 0; 189 190 // parsing Index 0 is unused 191 for (int index = 1; index < length; index++) { 192 // Each of the following case guarantees one more byte in the stream 193 // for the following tag or the access_flags following constant pool, 194 // so we don't need bounds-check for reading tag. 195 const u1 tag = cfs->get_u1_fast(); 196 switch (tag) { 197 case JVM_CONSTANT_Class : { 198 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags 199 const u2 name_index = cfs->get_u2_fast(); 200 cp->klass_index_at_put(index, name_index); 201 break; 202 } 203 case JVM_CONSTANT_Fieldref: { 204 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 205 const u2 class_index = cfs->get_u2_fast(); 206 const u2 name_and_type_index = cfs->get_u2_fast(); 207 cp->field_at_put(index, class_index, name_and_type_index); 208 break; 209 } 210 case JVM_CONSTANT_Methodref: { 211 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 212 const u2 class_index = cfs->get_u2_fast(); 213 const u2 name_and_type_index = cfs->get_u2_fast(); 214 cp->method_at_put(index, class_index, name_and_type_index); 215 break; 216 } 217 case JVM_CONSTANT_InterfaceMethodref: { 218 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 219 const u2 class_index = cfs->get_u2_fast(); 220 const u2 name_and_type_index = cfs->get_u2_fast(); 221 cp->interface_method_at_put(index, class_index, name_and_type_index); 222 break; 223 } 224 case JVM_CONSTANT_String : { 225 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags 226 const u2 string_index = cfs->get_u2_fast(); 227 cp->string_index_at_put(index, string_index); 228 break; 229 } 230 case JVM_CONSTANT_MethodHandle : 231 case JVM_CONSTANT_MethodType: { 232 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 233 classfile_parse_error( 234 "Class file version does not support constant tag %u in class file %s", 235 tag, THREAD); 236 return; 237 } 238 if (tag == JVM_CONSTANT_MethodHandle) { 239 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags 240 const u1 ref_kind = cfs->get_u1_fast(); 241 const u2 method_index = cfs->get_u2_fast(); 242 cp->method_handle_index_at_put(index, ref_kind, method_index); 243 } 244 else if (tag == JVM_CONSTANT_MethodType) { 245 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags 246 const u2 signature_index = cfs->get_u2_fast(); 247 cp->method_type_index_at_put(index, signature_index); 248 } 249 else { 250 ShouldNotReachHere(); 251 } 252 break; 253 } 254 case JVM_CONSTANT_Dynamic : { 255 if (_major_version < Verifier::DYNAMICCONSTANT_MAJOR_VERSION) { 256 classfile_parse_error( 257 "Class file version does not support constant tag %u in class file %s", 258 tag, THREAD); 259 return; 260 } 261 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 262 const u2 bootstrap_specifier_index = cfs->get_u2_fast(); 263 const u2 name_and_type_index = cfs->get_u2_fast(); 264 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) { 265 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 266 } 267 cp->dynamic_constant_at_put(index, bootstrap_specifier_index, name_and_type_index); 268 break; 269 } 270 case JVM_CONSTANT_InvokeDynamic : { 271 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 272 classfile_parse_error( 273 "Class file version does not support constant tag %u in class file %s", 274 tag, THREAD); 275 return; 276 } 277 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 278 const u2 bootstrap_specifier_index = cfs->get_u2_fast(); 279 const u2 name_and_type_index = cfs->get_u2_fast(); 280 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) { 281 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 282 } 283 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index); 284 break; 285 } 286 case JVM_CONSTANT_Integer: { 287 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 288 const u4 bytes = cfs->get_u4_fast(); 289 cp->int_at_put(index, (jint)bytes); 290 break; 291 } 292 case JVM_CONSTANT_Float: { 293 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 294 const u4 bytes = cfs->get_u4_fast(); 295 cp->float_at_put(index, *(jfloat*)&bytes); 296 break; 297 } 298 case JVM_CONSTANT_Long: { 299 // A mangled type might cause you to overrun allocated memory 300 guarantee_property(index + 1 < length, 301 "Invalid constant pool entry %u in class file %s", 302 index, 303 CHECK); 304 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 305 const u8 bytes = cfs->get_u8_fast(); 306 cp->long_at_put(index, bytes); 307 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 308 break; 309 } 310 case JVM_CONSTANT_Double: { 311 // A mangled type might cause you to overrun allocated memory 312 guarantee_property(index+1 < length, 313 "Invalid constant pool entry %u in class file %s", 314 index, 315 CHECK); 316 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 317 const u8 bytes = cfs->get_u8_fast(); 318 cp->double_at_put(index, *(jdouble*)&bytes); 319 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 320 break; 321 } 322 case JVM_CONSTANT_NameAndType: { 323 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags 324 const u2 name_index = cfs->get_u2_fast(); 325 const u2 signature_index = cfs->get_u2_fast(); 326 cp->name_and_type_at_put(index, name_index, signature_index); 327 break; 328 } 329 case JVM_CONSTANT_Utf8 : { 330 cfs->guarantee_more(2, CHECK); // utf8_length 331 u2 utf8_length = cfs->get_u2_fast(); 332 const u1* utf8_buffer = cfs->current(); 333 assert(utf8_buffer != nullptr, "null utf8 buffer"); 334 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward. 335 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags 336 cfs->skip_u1_fast(utf8_length); 337 338 // Before storing the symbol, make sure it's legal 339 if (_need_verify) { 340 verify_legal_utf8(utf8_buffer, utf8_length, CHECK); 341 } 342 343 unsigned int hash; 344 Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer, 345 utf8_length, 346 hash); 347 if (result == nullptr) { 348 names[names_count] = (const char*)utf8_buffer; 349 lengths[names_count] = utf8_length; 350 indices[names_count] = index; 351 hashValues[names_count++] = hash; 352 if (names_count == SymbolTable::symbol_alloc_batch_size) { 353 SymbolTable::new_symbols(_loader_data, 354 constantPoolHandle(THREAD, cp), 355 names_count, 356 names, 357 lengths, 358 indices, 359 hashValues); 360 names_count = 0; 361 } 362 } else { 363 cp->symbol_at_put(index, result); 364 } 365 break; 366 } 367 case JVM_CONSTANT_Module: 368 case JVM_CONSTANT_Package: { 369 // Record that an error occurred in these two cases but keep parsing so 370 // that ACC_Module can be checked for in the access_flags. Need to 371 // throw NoClassDefFoundError in that case. 372 if (_major_version >= JAVA_9_VERSION) { 373 cfs->guarantee_more(3, CHECK); 374 cfs->get_u2_fast(); 375 set_class_bad_constant_seen(tag); 376 break; 377 } 378 } 379 default: { 380 classfile_parse_error("Unknown constant tag %u in class file %s", 381 tag, 382 THREAD); 383 return; 384 } 385 } // end of switch(tag) 386 } // end of for 387 388 // Allocate the remaining symbols 389 if (names_count > 0) { 390 SymbolTable::new_symbols(_loader_data, 391 constantPoolHandle(THREAD, cp), 392 names_count, 393 names, 394 lengths, 395 indices, 396 hashValues); 397 } 398 399 // Copy _current pointer of local copy back to stream. 400 assert(stream->current() == old_current, "non-exclusive use of stream"); 401 stream->set_current(cfs1.current()); 402 403 } 404 405 static inline bool valid_cp_range(int index, int length) { 406 return (index > 0 && index < length); 407 } 408 409 static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) { 410 assert(cp != nullptr, "invariant"); 411 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) { 412 return cp->symbol_at(index); 413 } 414 return nullptr; 415 } 416 417 void ClassFileParser::parse_constant_pool(const ClassFileStream* const stream, 418 ConstantPool* const cp, 419 const int length, 420 TRAPS) { 421 assert(cp != nullptr, "invariant"); 422 assert(stream != nullptr, "invariant"); 423 424 // parsing constant pool entries 425 parse_constant_pool_entries(stream, cp, length, CHECK); 426 if (class_bad_constant_seen() != 0) { 427 // a bad CP entry has been detected previously so stop parsing and just return. 428 return; 429 } 430 431 int index = 1; // declared outside of loops for portability 432 int num_klasses = 0; 433 434 // first verification pass - validate cross references 435 // and fixup class and string constants 436 for (index = 1; index < length; index++) { // Index 0 is unused 437 const jbyte tag = cp->tag_at(index).value(); 438 switch (tag) { 439 case JVM_CONSTANT_Class: { 440 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 441 break; 442 } 443 case JVM_CONSTANT_Fieldref: 444 // fall through 445 case JVM_CONSTANT_Methodref: 446 // fall through 447 case JVM_CONSTANT_InterfaceMethodref: { 448 if (!_need_verify) break; 449 const int klass_ref_index = cp->uncached_klass_ref_index_at(index); 450 const int name_and_type_ref_index = cp->uncached_name_and_type_ref_index_at(index); 451 guarantee_property(valid_klass_reference_at(klass_ref_index), 452 "Invalid constant pool index %u in class file %s", 453 klass_ref_index, CHECK); 454 guarantee_property(valid_cp_range(name_and_type_ref_index, length) && 455 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 456 "Invalid constant pool index %u in class file %s", 457 name_and_type_ref_index, CHECK); 458 break; 459 } 460 case JVM_CONSTANT_String: { 461 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present 462 break; 463 } 464 case JVM_CONSTANT_Integer: 465 break; 466 case JVM_CONSTANT_Float: 467 break; 468 case JVM_CONSTANT_Long: 469 case JVM_CONSTANT_Double: { 470 index++; 471 guarantee_property( 472 (index < length && cp->tag_at(index).is_invalid()), 473 "Improper constant pool long/double index %u in class file %s", 474 index, CHECK); 475 break; 476 } 477 case JVM_CONSTANT_NameAndType: { 478 if (!_need_verify) break; 479 const int name_ref_index = cp->name_ref_index_at(index); 480 const int signature_ref_index = cp->signature_ref_index_at(index); 481 guarantee_property(valid_symbol_at(name_ref_index), 482 "Invalid constant pool index %u in class file %s", 483 name_ref_index, CHECK); 484 guarantee_property(valid_symbol_at(signature_ref_index), 485 "Invalid constant pool index %u in class file %s", 486 signature_ref_index, CHECK); 487 break; 488 } 489 case JVM_CONSTANT_Utf8: 490 break; 491 case JVM_CONSTANT_UnresolvedClass: // fall-through 492 case JVM_CONSTANT_UnresolvedClassInError: { 493 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 494 break; 495 } 496 case JVM_CONSTANT_ClassIndex: { 497 const int class_index = cp->klass_index_at(index); 498 guarantee_property(valid_symbol_at(class_index), 499 "Invalid constant pool index %u in class file %s", 500 class_index, CHECK); 501 cp->unresolved_klass_at_put(index, class_index, num_klasses++); 502 break; 503 } 504 case JVM_CONSTANT_StringIndex: { 505 const int string_index = cp->string_index_at(index); 506 guarantee_property(valid_symbol_at(string_index), 507 "Invalid constant pool index %u in class file %s", 508 string_index, CHECK); 509 Symbol* const sym = cp->symbol_at(string_index); 510 cp->unresolved_string_at_put(index, sym); 511 break; 512 } 513 case JVM_CONSTANT_MethodHandle: { 514 const int ref_index = cp->method_handle_index_at(index); 515 guarantee_property(valid_cp_range(ref_index, length), 516 "Invalid constant pool index %u in class file %s", 517 ref_index, CHECK); 518 const constantTag tag = cp->tag_at(ref_index); 519 const int ref_kind = cp->method_handle_ref_kind_at(index); 520 521 switch (ref_kind) { 522 case JVM_REF_getField: 523 case JVM_REF_getStatic: 524 case JVM_REF_putField: 525 case JVM_REF_putStatic: { 526 guarantee_property( 527 tag.is_field(), 528 "Invalid constant pool index %u in class file %s (not a field)", 529 ref_index, CHECK); 530 break; 531 } 532 case JVM_REF_invokeVirtual: 533 case JVM_REF_newInvokeSpecial: { 534 guarantee_property( 535 tag.is_method(), 536 "Invalid constant pool index %u in class file %s (not a method)", 537 ref_index, CHECK); 538 break; 539 } 540 case JVM_REF_invokeStatic: 541 case JVM_REF_invokeSpecial: { 542 guarantee_property( 543 tag.is_method() || 544 ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()), 545 "Invalid constant pool index %u in class file %s (not a method)", 546 ref_index, CHECK); 547 break; 548 } 549 case JVM_REF_invokeInterface: { 550 guarantee_property( 551 tag.is_interface_method(), 552 "Invalid constant pool index %u in class file %s (not an interface method)", 553 ref_index, CHECK); 554 break; 555 } 556 default: { 557 classfile_parse_error( 558 "Bad method handle kind at constant pool index %u in class file %s", 559 index, THREAD); 560 return; 561 } 562 } // switch(refkind) 563 // Keep the ref_index unchanged. It will be indirected at link-time. 564 break; 565 } // case MethodHandle 566 case JVM_CONSTANT_MethodType: { 567 const int ref_index = cp->method_type_index_at(index); 568 guarantee_property(valid_symbol_at(ref_index), 569 "Invalid constant pool index %u in class file %s", 570 ref_index, CHECK); 571 break; 572 } 573 case JVM_CONSTANT_Dynamic: { 574 const int name_and_type_ref_index = 575 cp->bootstrap_name_and_type_ref_index_at(index); 576 577 guarantee_property(valid_cp_range(name_and_type_ref_index, length) && 578 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 579 "Invalid constant pool index %u in class file %s", 580 name_and_type_ref_index, CHECK); 581 // bootstrap specifier index must be checked later, 582 // when BootstrapMethods attr is available 583 584 // Mark the constant pool as having a CONSTANT_Dynamic_info structure 585 cp->set_has_dynamic_constant(); 586 break; 587 } 588 case JVM_CONSTANT_InvokeDynamic: { 589 const int name_and_type_ref_index = 590 cp->bootstrap_name_and_type_ref_index_at(index); 591 592 guarantee_property(valid_cp_range(name_and_type_ref_index, length) && 593 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 594 "Invalid constant pool index %u in class file %s", 595 name_and_type_ref_index, CHECK); 596 // bootstrap specifier index must be checked later, 597 // when BootstrapMethods attr is available 598 break; 599 } 600 default: { 601 fatal("bad constant pool tag value %u", cp->tag_at(index).value()); 602 ShouldNotReachHere(); 603 break; 604 } 605 } // switch(tag) 606 } // end of for 607 608 cp->allocate_resolved_klasses(_loader_data, num_klasses, CHECK); 609 610 if (!_need_verify) { 611 return; 612 } 613 614 // second verification pass - checks the strings are of the right format. 615 // but not yet to the other entries 616 for (index = 1; index < length; index++) { 617 const jbyte tag = cp->tag_at(index).value(); 618 switch (tag) { 619 case JVM_CONSTANT_UnresolvedClass: { 620 const Symbol* const class_name = cp->klass_name_at(index); 621 // check the name 622 verify_legal_class_name(class_name, CHECK); 623 break; 624 } 625 case JVM_CONSTANT_NameAndType: { 626 if (_need_verify) { 627 const int sig_index = cp->signature_ref_index_at(index); 628 const int name_index = cp->name_ref_index_at(index); 629 const Symbol* const name = cp->symbol_at(name_index); 630 const Symbol* const sig = cp->symbol_at(sig_index); 631 guarantee_property(sig->utf8_length() != 0, 632 "Illegal zero length constant pool entry at %d in class %s", 633 sig_index, CHECK); 634 guarantee_property(name->utf8_length() != 0, 635 "Illegal zero length constant pool entry at %d in class %s", 636 name_index, CHECK); 637 638 if (Signature::is_method(sig)) { 639 // Format check method name and signature 640 verify_legal_method_name(name, CHECK); 641 verify_legal_method_signature(name, sig, CHECK); 642 } else { 643 // Format check field name and signature 644 verify_legal_field_name(name, CHECK); 645 verify_legal_field_signature(name, sig, CHECK); 646 } 647 } 648 break; 649 } 650 case JVM_CONSTANT_Dynamic: { 651 const int name_and_type_ref_index = 652 cp->uncached_name_and_type_ref_index_at(index); 653 // already verified to be utf8 654 const int name_ref_index = 655 cp->name_ref_index_at(name_and_type_ref_index); 656 // already verified to be utf8 657 const int signature_ref_index = 658 cp->signature_ref_index_at(name_and_type_ref_index); 659 const Symbol* const name = cp->symbol_at(name_ref_index); 660 const Symbol* const signature = cp->symbol_at(signature_ref_index); 661 if (_need_verify) { 662 // CONSTANT_Dynamic's name and signature are verified above, when iterating NameAndType_info. 663 // Need only to be sure signature is the right type. 664 if (Signature::is_method(signature)) { 665 throwIllegalSignature("CONSTANT_Dynamic", name, signature, CHECK); 666 } 667 } 668 break; 669 } 670 case JVM_CONSTANT_InvokeDynamic: 671 case JVM_CONSTANT_Fieldref: 672 case JVM_CONSTANT_Methodref: 673 case JVM_CONSTANT_InterfaceMethodref: { 674 const int name_and_type_ref_index = 675 cp->uncached_name_and_type_ref_index_at(index); 676 // already verified to be utf8 677 const int name_ref_index = 678 cp->name_ref_index_at(name_and_type_ref_index); 679 // already verified to be utf8 680 const int signature_ref_index = 681 cp->signature_ref_index_at(name_and_type_ref_index); 682 const Symbol* const name = cp->symbol_at(name_ref_index); 683 const Symbol* const signature = cp->symbol_at(signature_ref_index); 684 if (tag == JVM_CONSTANT_Fieldref) { 685 if (_need_verify) { 686 // Field name and signature are verified above, when iterating NameAndType_info. 687 // Need only to be sure signature is non-zero length and the right type. 688 if (Signature::is_method(signature)) { 689 throwIllegalSignature("Field", name, signature, CHECK); 690 } 691 } 692 } else { 693 if (_need_verify) { 694 // Method name and signature are individually verified above, when iterating 695 // NameAndType_info. Need to check here that signature is non-zero length and 696 // the right type. 697 if (!Signature::is_method(signature)) { 698 throwIllegalSignature("Method", name, signature, CHECK); 699 } 700 } 701 // If a class method name begins with '<', it must be "<init>" and have void signature. 702 const unsigned int name_len = name->utf8_length(); 703 if (tag == JVM_CONSTANT_Methodref && name_len != 0 && 704 name->char_at(0) == JVM_SIGNATURE_SPECIAL) { 705 if (name != vmSymbols::object_initializer_name()) { 706 classfile_parse_error( 707 "Bad method name at constant pool index %u in class file %s", 708 name_ref_index, THREAD); 709 return; 710 } else if (!Signature::is_void_method(signature)) { // must have void signature. 711 throwIllegalSignature("Method", name, signature, CHECK); 712 } 713 } 714 } 715 break; 716 } 717 case JVM_CONSTANT_MethodHandle: { 718 const int ref_index = cp->method_handle_index_at(index); 719 const int ref_kind = cp->method_handle_ref_kind_at(index); 720 switch (ref_kind) { 721 case JVM_REF_invokeVirtual: 722 case JVM_REF_invokeStatic: 723 case JVM_REF_invokeSpecial: 724 case JVM_REF_newInvokeSpecial: { 725 const int name_and_type_ref_index = 726 cp->uncached_name_and_type_ref_index_at(ref_index); 727 const int name_ref_index = 728 cp->name_ref_index_at(name_and_type_ref_index); 729 const Symbol* const name = cp->symbol_at(name_ref_index); 730 if (ref_kind == JVM_REF_newInvokeSpecial) { 731 if (name != vmSymbols::object_initializer_name()) { 732 classfile_parse_error( 733 "Bad constructor name at constant pool index %u in class file %s", 734 name_ref_index, THREAD); 735 return; 736 } 737 } else { 738 if (name == vmSymbols::object_initializer_name()) { 739 classfile_parse_error( 740 "Bad method name at constant pool index %u in class file %s", 741 name_ref_index, THREAD); 742 return; 743 } 744 } 745 break; 746 } 747 // Other ref_kinds are already fully checked in previous pass. 748 } // switch(ref_kind) 749 break; 750 } 751 case JVM_CONSTANT_MethodType: { 752 const Symbol* const no_name = vmSymbols::type_name(); // place holder 753 const Symbol* const signature = cp->method_type_signature_at(index); 754 verify_legal_method_signature(no_name, signature, CHECK); 755 break; 756 } 757 case JVM_CONSTANT_Utf8: { 758 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted"); 759 } 760 } // switch(tag) 761 } // end of for 762 } 763 764 class NameSigHash: public ResourceObj { 765 public: 766 const Symbol* _name; // name 767 const Symbol* _sig; // signature 768 769 static const int HASH_ROW_SIZE = 256; 770 771 NameSigHash(Symbol* name, Symbol* sig) : 772 _name(name), 773 _sig(sig) {} 774 775 static unsigned int hash(NameSigHash const& namesig) { 776 return namesig._name->identity_hash() ^ namesig._sig->identity_hash(); 777 } 778 779 static bool equals(NameSigHash const& e0, NameSigHash const& e1) { 780 return (e0._name == e1._name) && 781 (e0._sig == e1._sig); 782 } 783 }; 784 785 using NameSigHashtable = ResourceHashtable<NameSigHash, int, 786 NameSigHash::HASH_ROW_SIZE, 787 AnyObj::RESOURCE_AREA, mtInternal, 788 &NameSigHash::hash, &NameSigHash::equals>; 789 790 // Side-effects: populates the _local_interfaces field 791 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream, 792 const int itfs_len, 793 ConstantPool* const cp, 794 bool* const has_nonstatic_concrete_methods, 795 TRAPS) { 796 assert(stream != nullptr, "invariant"); 797 assert(cp != nullptr, "invariant"); 798 assert(has_nonstatic_concrete_methods != nullptr, "invariant"); 799 800 if (itfs_len == 0) { 801 _local_interfaces = Universe::the_empty_instance_klass_array(); 802 } else { 803 assert(itfs_len > 0, "only called for len>0"); 804 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK); 805 806 int index; 807 for (index = 0; index < itfs_len; index++) { 808 const u2 interface_index = stream->get_u2(CHECK); 809 Klass* interf; 810 guarantee_property( 811 valid_klass_reference_at(interface_index), 812 "Interface name has bad constant pool index %u in class file %s", 813 interface_index, CHECK); 814 if (cp->tag_at(interface_index).is_klass()) { 815 interf = cp->resolved_klass_at(interface_index); 816 } else { 817 Symbol* const unresolved_klass = cp->klass_name_at(interface_index); 818 819 // Don't need to check legal name because it's checked when parsing constant pool. 820 // But need to make sure it's not an array type. 821 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY, 822 "Bad interface name in class file %s", CHECK); 823 824 // Call resolve on the interface class name with class circularity checking 825 interf = SystemDictionary::resolve_super_or_fail(_class_name, 826 unresolved_klass, 827 Handle(THREAD, _loader_data->class_loader()), 828 false, CHECK); 829 } 830 831 if (!interf->is_interface()) { 832 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 833 err_msg("class %s can not implement %s, because it is not an interface (%s)", 834 _class_name->as_klass_external_name(), 835 interf->external_name(), 836 interf->class_in_module_of_loader())); 837 } 838 839 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) { 840 *has_nonstatic_concrete_methods = true; 841 } 842 _local_interfaces->at_put(index, InstanceKlass::cast(interf)); 843 } 844 845 if (!_need_verify || itfs_len <= 1) { 846 return; 847 } 848 849 // Check if there's any duplicates in interfaces 850 ResourceMark rm(THREAD); 851 // Set containing interface names 852 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>(); 853 for (index = 0; index < itfs_len; index++) { 854 const InstanceKlass* const k = _local_interfaces->at(index); 855 Symbol* interface_name = k->name(); 856 // If no duplicates, add (name, nullptr) in hashtable interface_names. 857 if (!interface_names->put(interface_name, 0)) { 858 classfile_parse_error("Duplicate interface name \"%s\" in class file %s", 859 interface_name->as_C_string(), THREAD); 860 return; 861 } 862 } 863 } 864 } 865 866 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp, 867 int constantvalue_index, 868 int signature_index, 869 TRAPS) const { 870 // Make sure the constant pool entry is of a type appropriate to this field 871 guarantee_property( 872 (constantvalue_index > 0 && 873 constantvalue_index < cp->length()), 874 "Bad initial value index %u in ConstantValue attribute in class file %s", 875 constantvalue_index, CHECK); 876 877 const constantTag value_type = cp->tag_at(constantvalue_index); 878 switch(cp->basic_type_for_signature_at(signature_index)) { 879 case T_LONG: { 880 guarantee_property(value_type.is_long(), 881 "Inconsistent constant value type in class file %s", 882 CHECK); 883 break; 884 } 885 case T_FLOAT: { 886 guarantee_property(value_type.is_float(), 887 "Inconsistent constant value type in class file %s", 888 CHECK); 889 break; 890 } 891 case T_DOUBLE: { 892 guarantee_property(value_type.is_double(), 893 "Inconsistent constant value type in class file %s", 894 CHECK); 895 break; 896 } 897 case T_BYTE: 898 case T_CHAR: 899 case T_SHORT: 900 case T_BOOLEAN: 901 case T_INT: { 902 guarantee_property(value_type.is_int(), 903 "Inconsistent constant value type in class file %s", 904 CHECK); 905 break; 906 } 907 case T_OBJECT: { 908 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") 909 && value_type.is_string()), 910 "Bad string initial value in class file %s", 911 CHECK); 912 break; 913 } 914 default: { 915 classfile_parse_error("Unable to set initial value %u in class file %s", 916 constantvalue_index, 917 THREAD); 918 } 919 } 920 } 921 922 class AnnotationCollector : public ResourceObj{ 923 public: 924 enum Location { _in_field, _in_method, _in_class }; 925 enum ID { 926 _unknown = 0, 927 _method_CallerSensitive, 928 _method_ForceInline, 929 _method_DontInline, 930 _method_ChangesCurrentThread, 931 _method_JvmtiHideEvents, 932 _method_JvmtiMountTransition, 933 _method_InjectedProfile, 934 _method_LambdaForm_Compiled, 935 _method_Hidden, 936 _method_Scoped, 937 _method_IntrinsicCandidate, 938 _jdk_internal_vm_annotation_Contended, 939 _field_Stable, 940 _jdk_internal_vm_annotation_ReservedStackAccess, 941 _jdk_internal_ValueBased, 942 _java_lang_Deprecated, 943 _java_lang_Deprecated_for_removal, 944 _annotation_LIMIT 945 }; 946 const Location _location; 947 int _annotations_present; 948 u2 _contended_group; 949 950 AnnotationCollector(Location location) 951 : _location(location), _annotations_present(0), _contended_group(0) 952 { 953 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, ""); 954 } 955 // If this annotation name has an ID, report it (or _none). 956 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations); 957 // Set the annotation name: 958 void set_annotation(ID id) { 959 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 960 _annotations_present |= (int)nth_bit((int)id); 961 } 962 963 void remove_annotation(ID id) { 964 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 965 _annotations_present &= (int)~nth_bit((int)id); 966 } 967 968 // Report if the annotation is present. 969 bool has_any_annotations() const { return _annotations_present != 0; } 970 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; } 971 972 void set_contended_group(u2 group) { _contended_group = group; } 973 u2 contended_group() const { return _contended_group; } 974 975 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); } 976 977 void set_stable(bool stable) { set_annotation(_field_Stable); } 978 bool is_stable() const { return has_annotation(_field_Stable); } 979 }; 980 981 // This class also doubles as a holder for metadata cleanup. 982 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector { 983 private: 984 ClassLoaderData* _loader_data; 985 AnnotationArray* _field_annotations; 986 AnnotationArray* _field_type_annotations; 987 public: 988 FieldAnnotationCollector(ClassLoaderData* loader_data) : 989 AnnotationCollector(_in_field), 990 _loader_data(loader_data), 991 _field_annotations(nullptr), 992 _field_type_annotations(nullptr) {} 993 ~FieldAnnotationCollector(); 994 void apply_to(FieldInfo* f); 995 AnnotationArray* field_annotations() { return _field_annotations; } 996 AnnotationArray* field_type_annotations() { return _field_type_annotations; } 997 998 void set_field_annotations(AnnotationArray* a) { _field_annotations = a; } 999 void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; } 1000 }; 1001 1002 class MethodAnnotationCollector : public AnnotationCollector{ 1003 public: 1004 MethodAnnotationCollector() : AnnotationCollector(_in_method) { } 1005 void apply_to(const methodHandle& m); 1006 }; 1007 1008 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{ 1009 public: 1010 ClassAnnotationCollector() : AnnotationCollector(_in_class) { } 1011 void apply_to(InstanceKlass* ik); 1012 }; 1013 1014 1015 static int skip_annotation_value(const u1*, int, int); // fwd decl 1016 1017 // Safely increment index by val if does not pass limit 1018 #define SAFE_ADD(index, limit, val) \ 1019 if (index >= limit - val) return limit; \ 1020 index += val; 1021 1022 // Skip an annotation. Return >=limit if there is any problem. 1023 static int skip_annotation(const u1* buffer, int limit, int index) { 1024 assert(buffer != nullptr, "invariant"); 1025 // annotation := atype:u2 do(nmem:u2) {member:u2 value} 1026 // value := switch (tag:u1) { ... } 1027 SAFE_ADD(index, limit, 4); // skip atype and read nmem 1028 int nmem = Bytes::get_Java_u2((address)buffer + index - 2); 1029 while (--nmem >= 0 && index < limit) { 1030 SAFE_ADD(index, limit, 2); // skip member 1031 index = skip_annotation_value(buffer, limit, index); 1032 } 1033 return index; 1034 } 1035 1036 // Skip an annotation value. Return >=limit if there is any problem. 1037 static int skip_annotation_value(const u1* buffer, int limit, int index) { 1038 assert(buffer != nullptr, "invariant"); 1039 1040 // value := switch (tag:u1) { 1041 // case B, C, I, S, Z, D, F, J, c: con:u2; 1042 // case e: e_class:u2 e_name:u2; 1043 // case s: s_con:u2; 1044 // case [: do(nval:u2) {value}; 1045 // case @: annotation; 1046 // case s: s_con:u2; 1047 // } 1048 SAFE_ADD(index, limit, 1); // read tag 1049 const u1 tag = buffer[index - 1]; 1050 switch (tag) { 1051 case 'B': 1052 case 'C': 1053 case 'I': 1054 case 'S': 1055 case 'Z': 1056 case 'D': 1057 case 'F': 1058 case 'J': 1059 case 'c': 1060 case 's': 1061 SAFE_ADD(index, limit, 2); // skip con or s_con 1062 break; 1063 case 'e': 1064 SAFE_ADD(index, limit, 4); // skip e_class, e_name 1065 break; 1066 case '[': 1067 { 1068 SAFE_ADD(index, limit, 2); // read nval 1069 int nval = Bytes::get_Java_u2((address)buffer + index - 2); 1070 while (--nval >= 0 && index < limit) { 1071 index = skip_annotation_value(buffer, limit, index); 1072 } 1073 } 1074 break; 1075 case '@': 1076 index = skip_annotation(buffer, limit, index); 1077 break; 1078 default: 1079 return limit; // bad tag byte 1080 } 1081 return index; 1082 } 1083 1084 // Sift through annotations, looking for those significant to the VM: 1085 static void parse_annotations(const ConstantPool* const cp, 1086 const u1* buffer, int limit, 1087 AnnotationCollector* coll, 1088 ClassLoaderData* loader_data, 1089 const bool can_access_vm_annotations) { 1090 1091 assert(cp != nullptr, "invariant"); 1092 assert(buffer != nullptr, "invariant"); 1093 assert(coll != nullptr, "invariant"); 1094 assert(loader_data != nullptr, "invariant"); 1095 1096 // annotations := do(nann:u2) {annotation} 1097 int index = 2; // read nann 1098 if (index >= limit) return; 1099 int nann = Bytes::get_Java_u2((address)buffer + index - 2); 1100 enum { // initial annotation layout 1101 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' 1102 count_off = 2, // u2 such as 1 (one value) 1103 member_off = 4, // utf8 such as 'value' 1104 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) 1105 e_tag_val = 'e', 1106 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' 1107 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' 1108 e_size = 11, // end of 'e' annotation 1109 c_tag_val = 'c', // payload is type 1110 c_con_off = 7, // utf8 payload, such as 'I' 1111 c_size = 9, // end of 'c' annotation 1112 s_tag_val = 's', // payload is String 1113 s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' 1114 s_size = 9, 1115 b_tag_val = 'Z', // payload is boolean 1116 min_size = 6 // smallest possible size (zero members) 1117 }; 1118 // Cannot add min_size to index in case of overflow MAX_INT 1119 while ((--nann) >= 0 && (index - 2 <= limit - min_size)) { 1120 int index0 = index; 1121 index = skip_annotation(buffer, limit, index); 1122 const u1* const abase = buffer + index0; 1123 const int atype = Bytes::get_Java_u2((address)abase + atype_off); 1124 const int count = Bytes::get_Java_u2((address)abase + count_off); 1125 const Symbol* const aname = check_symbol_at(cp, atype); 1126 if (aname == nullptr) break; // invalid annotation name 1127 const Symbol* member = nullptr; 1128 if (count >= 1) { 1129 const int member_index = Bytes::get_Java_u2((address)abase + member_off); 1130 member = check_symbol_at(cp, member_index); 1131 if (member == nullptr) break; // invalid member name 1132 } 1133 1134 // Here is where parsing particular annotations will take place. 1135 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname, can_access_vm_annotations); 1136 if (AnnotationCollector::_unknown == id) continue; 1137 coll->set_annotation(id); 1138 if (AnnotationCollector::_java_lang_Deprecated == id) { 1139 // @Deprecated can specify forRemoval=true, which we need 1140 // to record for JFR to use. If the annotation is not well-formed 1141 // then we may not be able to determine that. 1142 const u1* offset = abase + member_off; 1143 // There are only 2 members in @Deprecated. 1144 int n_members = MIN2(count, 2); 1145 for (int i = 0; i < n_members; ++i) { 1146 int member_index = Bytes::get_Java_u2((address)offset); 1147 offset += 2; 1148 member = check_symbol_at(cp, member_index); 1149 if (member == vmSymbols::since() && 1150 (*((address)offset) == s_tag_val)) { 1151 // Found `since` first so skip over it 1152 offset += 3; 1153 } 1154 else if (member == vmSymbols::for_removal() && 1155 (*((address)offset) == b_tag_val)) { 1156 const u2 boolean_value_index = Bytes::get_Java_u2((address)offset + 1); 1157 // No guarantee the entry is valid so check it refers to an int in the CP. 1158 if (cp->is_within_bounds(boolean_value_index) && 1159 cp->tag_at(boolean_value_index).is_int() && 1160 cp->int_at(boolean_value_index) == 1) { 1161 // forRemoval == true 1162 coll->set_annotation(AnnotationCollector::_java_lang_Deprecated_for_removal); 1163 } 1164 break; // no need to check further 1165 } 1166 else { 1167 // This @Deprecated annotation is malformed so we don't try to 1168 // determine whether forRemoval is set. 1169 break; 1170 } 1171 } 1172 continue; // proceed to next annotation 1173 } 1174 1175 if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) { 1176 // @Contended can optionally specify the contention group. 1177 // 1178 // Contended group defines the equivalence class over the fields: 1179 // the fields within the same contended group are not treated distinct. 1180 // The only exception is default group, which does not incur the 1181 // equivalence. Naturally, contention group for classes is meaningless. 1182 // 1183 // While the contention group is specified as String, annotation 1184 // values are already interned, and we might as well use the constant 1185 // pool index as the group tag. 1186 // 1187 u2 group_index = 0; // default contended group 1188 if (count == 1 1189 && s_size == (index - index0) // match size 1190 && s_tag_val == *(abase + tag_off) 1191 && member == vmSymbols::value_name()) { 1192 group_index = Bytes::get_Java_u2((address)abase + s_con_off); 1193 // No guarantee the group_index is valid so check it refers to a 1194 // symbol in the CP. 1195 if (cp->is_within_bounds(group_index) && 1196 cp->tag_at(group_index).is_utf8()) { 1197 // Seems valid, so check for empty string and reset 1198 if (cp->symbol_at(group_index)->utf8_length() == 0) { 1199 group_index = 0; // default contended group 1200 } 1201 } else { 1202 // Not valid so use the default 1203 group_index = 0; 1204 } 1205 } 1206 coll->set_contended_group(group_index); 1207 continue; // proceed to next annotation 1208 } 1209 } 1210 } 1211 1212 1213 // Parse attributes for a field. 1214 void ClassFileParser::parse_field_attributes(const ClassFileStream* const cfs, 1215 u2 attributes_count, 1216 bool is_static, u2 signature_index, 1217 u2* const constantvalue_index_addr, 1218 bool* const is_synthetic_addr, 1219 u2* const generic_signature_index_addr, 1220 ClassFileParser::FieldAnnotationCollector* parsed_annotations, 1221 TRAPS) { 1222 assert(cfs != nullptr, "invariant"); 1223 assert(constantvalue_index_addr != nullptr, "invariant"); 1224 assert(is_synthetic_addr != nullptr, "invariant"); 1225 assert(generic_signature_index_addr != nullptr, "invariant"); 1226 assert(parsed_annotations != nullptr, "invariant"); 1227 assert(attributes_count > 0, "attributes_count should be greater than 0"); 1228 1229 u2 constantvalue_index = 0; 1230 u2 generic_signature_index = 0; 1231 bool is_synthetic = false; 1232 const u1* runtime_visible_annotations = nullptr; 1233 int runtime_visible_annotations_length = 0; 1234 const u1* runtime_visible_type_annotations = nullptr; 1235 int runtime_visible_type_annotations_length = 0; 1236 bool runtime_invisible_annotations_exists = false; 1237 bool runtime_invisible_type_annotations_exists = false; 1238 const ConstantPool* const cp = _cp; 1239 1240 while (attributes_count--) { 1241 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 1242 const u2 attribute_name_index = cfs->get_u2_fast(); 1243 const u4 attribute_length = cfs->get_u4_fast(); 1244 guarantee_property(valid_symbol_at(attribute_name_index), 1245 "Invalid field attribute index %u in class file %s", 1246 attribute_name_index, 1247 CHECK); 1248 1249 const Symbol* const attribute_name = cp->symbol_at(attribute_name_index); 1250 if (is_static && attribute_name == vmSymbols::tag_constant_value()) { 1251 // ignore if non-static 1252 if (constantvalue_index != 0) { 1253 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", THREAD); 1254 return; 1255 } 1256 guarantee_property( 1257 attribute_length == 2, 1258 "Invalid ConstantValue field attribute length %u in class file %s", 1259 attribute_length, CHECK); 1260 1261 constantvalue_index = cfs->get_u2(CHECK); 1262 if (_need_verify) { 1263 verify_constantvalue(cp, constantvalue_index, signature_index, CHECK); 1264 } 1265 } else if (attribute_name == vmSymbols::tag_synthetic()) { 1266 if (attribute_length != 0) { 1267 classfile_parse_error( 1268 "Invalid Synthetic field attribute length %u in class file %s", 1269 attribute_length, THREAD); 1270 return; 1271 } 1272 is_synthetic = true; 1273 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120 1274 if (attribute_length != 0) { 1275 classfile_parse_error( 1276 "Invalid Deprecated field attribute length %u in class file %s", 1277 attribute_length, THREAD); 1278 return; 1279 } 1280 } else if (_major_version >= JAVA_1_5_VERSION) { 1281 if (attribute_name == vmSymbols::tag_signature()) { 1282 if (generic_signature_index != 0) { 1283 classfile_parse_error( 1284 "Multiple Signature attributes for field in class file %s", THREAD); 1285 return; 1286 } 1287 if (attribute_length != 2) { 1288 classfile_parse_error( 1289 "Wrong size %u for field's Signature attribute in class file %s", 1290 attribute_length, THREAD); 1291 return; 1292 } 1293 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK); 1294 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 1295 if (runtime_visible_annotations != nullptr) { 1296 classfile_parse_error( 1297 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", THREAD); 1298 return; 1299 } 1300 runtime_visible_annotations_length = attribute_length; 1301 runtime_visible_annotations = cfs->current(); 1302 assert(runtime_visible_annotations != nullptr, "null visible annotations"); 1303 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 1304 parse_annotations(cp, 1305 runtime_visible_annotations, 1306 runtime_visible_annotations_length, 1307 parsed_annotations, 1308 _loader_data, 1309 _can_access_vm_annotations); 1310 cfs->skip_u1_fast(runtime_visible_annotations_length); 1311 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 1312 if (runtime_invisible_annotations_exists) { 1313 classfile_parse_error( 1314 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", THREAD); 1315 return; 1316 } 1317 runtime_invisible_annotations_exists = true; 1318 cfs->skip_u1(attribute_length, CHECK); 1319 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 1320 if (runtime_visible_type_annotations != nullptr) { 1321 classfile_parse_error( 1322 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", THREAD); 1323 return; 1324 } 1325 runtime_visible_type_annotations_length = attribute_length; 1326 runtime_visible_type_annotations = cfs->current(); 1327 assert(runtime_visible_type_annotations != nullptr, "null visible type annotations"); 1328 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 1329 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 1330 if (runtime_invisible_type_annotations_exists) { 1331 classfile_parse_error( 1332 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", THREAD); 1333 return; 1334 } else { 1335 runtime_invisible_type_annotations_exists = true; 1336 } 1337 cfs->skip_u1(attribute_length, CHECK); 1338 } else { 1339 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1340 } 1341 } else { 1342 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1343 } 1344 } 1345 1346 *constantvalue_index_addr = constantvalue_index; 1347 *is_synthetic_addr = is_synthetic; 1348 *generic_signature_index_addr = generic_signature_index; 1349 AnnotationArray* a = allocate_annotations(runtime_visible_annotations, 1350 runtime_visible_annotations_length, 1351 CHECK); 1352 parsed_annotations->set_field_annotations(a); 1353 a = allocate_annotations(runtime_visible_type_annotations, 1354 runtime_visible_type_annotations_length, 1355 CHECK); 1356 parsed_annotations->set_field_type_annotations(a); 1357 return; 1358 } 1359 1360 1361 // Side-effects: populates the _fields, _fields_annotations, 1362 // _fields_type_annotations fields 1363 void ClassFileParser::parse_fields(const ClassFileStream* const cfs, 1364 bool is_interface, 1365 ConstantPool* cp, 1366 const int cp_size, 1367 u2* const java_fields_count_ptr, 1368 TRAPS) { 1369 1370 assert(cfs != nullptr, "invariant"); 1371 assert(cp != nullptr, "invariant"); 1372 assert(java_fields_count_ptr != nullptr, "invariant"); 1373 1374 assert(nullptr == _fields_annotations, "invariant"); 1375 assert(nullptr == _fields_type_annotations, "invariant"); 1376 1377 cfs->guarantee_more(2, CHECK); // length 1378 const u2 length = cfs->get_u2_fast(); 1379 *java_fields_count_ptr = length; 1380 1381 int num_injected = 0; 1382 const InjectedField* const injected = JavaClasses::get_injected(_class_name, 1383 &num_injected); 1384 const int total_fields = length + num_injected; 1385 1386 // Allocate a temporary resource array to collect field data. 1387 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream. 1388 _temp_field_info = new GrowableArray<FieldInfo>(total_fields); 1389 1390 ResourceMark rm(THREAD); 1391 for (int n = 0; n < length; n++) { 1392 // access_flags, name_index, descriptor_index, attributes_count 1393 cfs->guarantee_more(8, CHECK); 1394 1395 AccessFlags access_flags; 1396 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS; 1397 verify_legal_field_modifiers(flags, is_interface, CHECK); 1398 access_flags.set_flags(flags); 1399 FieldInfo::FieldFlags fieldFlags(0); 1400 1401 const u2 name_index = cfs->get_u2_fast(); 1402 guarantee_property(valid_symbol_at(name_index), 1403 "Invalid constant pool index %u for field name in class file %s", 1404 name_index, CHECK); 1405 const Symbol* const name = cp->symbol_at(name_index); 1406 verify_legal_field_name(name, CHECK); 1407 1408 const u2 signature_index = cfs->get_u2_fast(); 1409 guarantee_property(valid_symbol_at(signature_index), 1410 "Invalid constant pool index %u for field signature in class file %s", 1411 signature_index, CHECK); 1412 const Symbol* const sig = cp->symbol_at(signature_index); 1413 verify_legal_field_signature(name, sig, CHECK); 1414 1415 u2 constantvalue_index = 0; 1416 bool is_synthetic = false; 1417 u2 generic_signature_index = 0; 1418 const bool is_static = access_flags.is_static(); 1419 FieldAnnotationCollector parsed_annotations(_loader_data); 1420 1421 const u2 attributes_count = cfs->get_u2_fast(); 1422 if (attributes_count > 0) { 1423 parse_field_attributes(cfs, 1424 attributes_count, 1425 is_static, 1426 signature_index, 1427 &constantvalue_index, 1428 &is_synthetic, 1429 &generic_signature_index, 1430 &parsed_annotations, 1431 CHECK); 1432 1433 if (parsed_annotations.field_annotations() != nullptr) { 1434 if (_fields_annotations == nullptr) { 1435 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>( 1436 _loader_data, length, nullptr, 1437 CHECK); 1438 } 1439 _fields_annotations->at_put(n, parsed_annotations.field_annotations()); 1440 parsed_annotations.set_field_annotations(nullptr); 1441 } 1442 if (parsed_annotations.field_type_annotations() != nullptr) { 1443 if (_fields_type_annotations == nullptr) { 1444 _fields_type_annotations = 1445 MetadataFactory::new_array<AnnotationArray*>(_loader_data, 1446 length, 1447 nullptr, 1448 CHECK); 1449 } 1450 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations()); 1451 parsed_annotations.set_field_type_annotations(nullptr); 1452 } 1453 1454 if (is_synthetic) { 1455 access_flags.set_is_synthetic(); 1456 } 1457 if (generic_signature_index != 0) { 1458 fieldFlags.update_generic(true); 1459 } 1460 } 1461 1462 const BasicType type = cp->basic_type_for_signature_at(signature_index); 1463 1464 // Update number of static oop fields. 1465 if (is_static && is_reference_type(type)) { 1466 _static_oop_count++; 1467 } 1468 1469 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags); 1470 fi.set_index(n); 1471 if (fieldFlags.is_generic()) { 1472 fi.set_generic_signature_index(generic_signature_index); 1473 } 1474 parsed_annotations.apply_to(&fi); 1475 if (fi.field_flags().is_contended()) { 1476 _has_contended_fields = true; 1477 } 1478 _temp_field_info->append(fi); 1479 } 1480 assert(_temp_field_info->length() == length, "Must be"); 1481 1482 int index = length; 1483 if (num_injected != 0) { 1484 for (int n = 0; n < num_injected; n++) { 1485 // Check for duplicates 1486 if (injected[n].may_be_java) { 1487 const Symbol* const name = injected[n].name(); 1488 const Symbol* const signature = injected[n].signature(); 1489 bool duplicate = false; 1490 for (int i = 0; i < length; i++) { 1491 const FieldInfo* const f = _temp_field_info->adr_at(i); 1492 if (name == cp->symbol_at(f->name_index()) && 1493 signature == cp->symbol_at(f->signature_index())) { 1494 // Symbol is desclared in Java so skip this one 1495 duplicate = true; 1496 break; 1497 } 1498 } 1499 if (duplicate) { 1500 // These will be removed from the field array at the end 1501 continue; 1502 } 1503 } 1504 1505 // Injected field 1506 FieldInfo::FieldFlags fflags(0); 1507 fflags.update_injected(true); 1508 AccessFlags aflags; 1509 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags); 1510 fi.set_index(index); 1511 _temp_field_info->append(fi); 1512 index++; 1513 } 1514 } 1515 1516 assert(_temp_field_info->length() == index, "Must be"); 1517 1518 if (_need_verify && length > 1) { 1519 // Check duplicated fields 1520 ResourceMark rm(THREAD); 1521 // Set containing name-signature pairs 1522 NameSigHashtable* names_and_sigs = new NameSigHashtable(); 1523 for (int i = 0; i < _temp_field_info->length(); i++) { 1524 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp), 1525 _temp_field_info->adr_at(i)->signature(_cp)); 1526 // If no duplicates, add name/signature in hashtable names_and_sigs. 1527 if(!names_and_sigs->put(name_and_sig, 0)) { 1528 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s", 1529 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD); 1530 return; 1531 } 1532 } 1533 } 1534 } 1535 1536 1537 const ClassFileParser::unsafe_u2* ClassFileParser::parse_exception_table(const ClassFileStream* const cfs, 1538 u4 code_length, 1539 u4 exception_table_length, 1540 TRAPS) { 1541 assert(cfs != nullptr, "invariant"); 1542 1543 const unsafe_u2* const exception_table_start = cfs->current(); 1544 assert(exception_table_start != nullptr, "null exception table"); 1545 1546 cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, 1547 // end_pc, 1548 // handler_pc, 1549 // catch_type_index 1550 1551 // Will check legal target after parsing code array in verifier. 1552 if (_need_verify) { 1553 for (unsigned int i = 0; i < exception_table_length; i++) { 1554 const u2 start_pc = cfs->get_u2_fast(); 1555 const u2 end_pc = cfs->get_u2_fast(); 1556 const u2 handler_pc = cfs->get_u2_fast(); 1557 const u2 catch_type_index = cfs->get_u2_fast(); 1558 guarantee_property((start_pc < end_pc) && (end_pc <= code_length), 1559 "Illegal exception table range in class file %s", 1560 CHECK_NULL); 1561 guarantee_property(handler_pc < code_length, 1562 "Illegal exception table handler in class file %s", 1563 CHECK_NULL); 1564 if (catch_type_index != 0) { 1565 guarantee_property(valid_klass_reference_at(catch_type_index), 1566 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); 1567 } 1568 } 1569 } else { 1570 cfs->skip_u2_fast(exception_table_length * 4); 1571 } 1572 return exception_table_start; 1573 } 1574 1575 void ClassFileParser::parse_linenumber_table(u4 code_attribute_length, 1576 u4 code_length, 1577 CompressedLineNumberWriteStream**const write_stream, 1578 TRAPS) { 1579 1580 const ClassFileStream* const cfs = _stream; 1581 unsigned int num_entries = cfs->get_u2(CHECK); 1582 1583 // Each entry is a u2 start_pc, and a u2 line_number 1584 const unsigned int length_in_bytes = num_entries * (sizeof(u2) * 2); 1585 1586 // Verify line number attribute and table length 1587 guarantee_property( 1588 code_attribute_length == sizeof(u2) + length_in_bytes, 1589 "LineNumberTable attribute has wrong length in class file %s", CHECK); 1590 1591 cfs->guarantee_more(length_in_bytes, CHECK); 1592 1593 if ((*write_stream) == nullptr) { 1594 if (length_in_bytes > fixed_buffer_size) { 1595 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes); 1596 } else { 1597 (*write_stream) = new CompressedLineNumberWriteStream( 1598 _linenumbertable_buffer, fixed_buffer_size); 1599 } 1600 } 1601 1602 while (num_entries-- > 0) { 1603 const u2 bci = cfs->get_u2_fast(); // start_pc 1604 const u2 line = cfs->get_u2_fast(); // line_number 1605 guarantee_property(bci < code_length, 1606 "Invalid pc in LineNumberTable in class file %s", CHECK); 1607 (*write_stream)->write_pair(bci, line); 1608 } 1609 } 1610 1611 1612 class LVT_Hash : public AllStatic { 1613 public: 1614 1615 static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) { 1616 /* 1617 * 3-tuple start_bci/length/slot has to be unique key, 1618 * so the following comparison seems to be redundant: 1619 * && elem->name_cp_index == entry->_elem->name_cp_index 1620 */ 1621 return (e0.start_bci == e1.start_bci && 1622 e0.length == e1.length && 1623 e0.name_cp_index == e1.name_cp_index && 1624 e0.slot == e1.slot); 1625 } 1626 1627 static unsigned int hash(LocalVariableTableElement const& e0) { 1628 unsigned int raw_hash = e0.start_bci; 1629 1630 raw_hash = e0.length + raw_hash * 37; 1631 raw_hash = e0.name_cp_index + raw_hash * 37; 1632 raw_hash = e0.slot + raw_hash * 37; 1633 1634 return raw_hash; 1635 } 1636 }; 1637 1638 1639 // Class file LocalVariableTable elements. 1640 class Classfile_LVT_Element { 1641 public: 1642 u2 start_bci; 1643 u2 length; 1644 u2 name_cp_index; 1645 u2 descriptor_cp_index; 1646 u2 slot; 1647 }; 1648 1649 static void copy_lvt_element(const Classfile_LVT_Element* const src, 1650 LocalVariableTableElement* const lvt) { 1651 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci); 1652 lvt->length = Bytes::get_Java_u2((u1*) &src->length); 1653 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index); 1654 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index); 1655 lvt->signature_cp_index = 0; 1656 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot); 1657 } 1658 1659 // Function is used to parse both attributes: 1660 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) 1661 const ClassFileParser::unsafe_u2* ClassFileParser::parse_localvariable_table(const ClassFileStream* cfs, 1662 u4 code_length, 1663 u2 max_locals, 1664 u4 code_attribute_length, 1665 u2* const localvariable_table_length, 1666 bool isLVTT, 1667 TRAPS) { 1668 const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; 1669 *localvariable_table_length = cfs->get_u2(CHECK_NULL); 1670 const unsigned int size = checked_cast<unsigned>( 1671 (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2)); 1672 1673 const ConstantPool* const cp = _cp; 1674 1675 // Verify local variable table attribute has right length 1676 if (_need_verify) { 1677 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)), 1678 "%s has wrong length in class file %s", tbl_name, CHECK_NULL); 1679 } 1680 1681 const unsafe_u2* const localvariable_table_start = cfs->current(); 1682 assert(localvariable_table_start != nullptr, "null local variable table"); 1683 if (!_need_verify) { 1684 cfs->skip_u2_fast(size); 1685 } else { 1686 cfs->guarantee_more(size * 2, CHECK_NULL); 1687 for(int i = 0; i < (*localvariable_table_length); i++) { 1688 const u2 start_pc = cfs->get_u2_fast(); 1689 const u2 length = cfs->get_u2_fast(); 1690 const u2 name_index = cfs->get_u2_fast(); 1691 const u2 descriptor_index = cfs->get_u2_fast(); 1692 const u2 index = cfs->get_u2_fast(); 1693 // Assign to a u4 to avoid overflow 1694 const u4 end_pc = (u4)start_pc + (u4)length; 1695 1696 if (start_pc >= code_length) { 1697 classfile_parse_error( 1698 "Invalid start_pc %u in %s in class file %s", 1699 start_pc, tbl_name, THREAD); 1700 return nullptr; 1701 } 1702 if (end_pc > code_length) { 1703 classfile_parse_error( 1704 "Invalid length %u in %s in class file %s", 1705 length, tbl_name, THREAD); 1706 return nullptr; 1707 } 1708 const int cp_size = cp->length(); 1709 guarantee_property(valid_symbol_at(name_index), 1710 "Name index %u in %s has bad constant type in class file %s", 1711 name_index, tbl_name, CHECK_NULL); 1712 guarantee_property(valid_symbol_at(descriptor_index), 1713 "Signature index %u in %s has bad constant type in class file %s", 1714 descriptor_index, tbl_name, CHECK_NULL); 1715 1716 const Symbol* const name = cp->symbol_at(name_index); 1717 const Symbol* const sig = cp->symbol_at(descriptor_index); 1718 verify_legal_field_name(name, CHECK_NULL); 1719 u2 extra_slot = 0; 1720 if (!isLVTT) { 1721 verify_legal_field_signature(name, sig, CHECK_NULL); 1722 1723 // 4894874: check special cases for double and long local variables 1724 if (sig == vmSymbols::type_signature(T_DOUBLE) || 1725 sig == vmSymbols::type_signature(T_LONG)) { 1726 extra_slot = 1; 1727 } 1728 } 1729 guarantee_property((index + extra_slot) < max_locals, 1730 "Invalid index %u in %s in class file %s", 1731 index, tbl_name, CHECK_NULL); 1732 } 1733 } 1734 return localvariable_table_start; 1735 } 1736 1737 static const u1* parse_stackmap_table(const ClassFileStream* const cfs, 1738 u4 code_attribute_length, 1739 TRAPS) { 1740 assert(cfs != nullptr, "invariant"); 1741 1742 if (0 == code_attribute_length) { 1743 return nullptr; 1744 } 1745 1746 const u1* const stackmap_table_start = cfs->current(); 1747 assert(stackmap_table_start != nullptr, "null stackmap table"); 1748 1749 // check code_attribute_length 1750 cfs->skip_u1(code_attribute_length, CHECK_NULL); 1751 1752 return stackmap_table_start; 1753 } 1754 1755 const ClassFileParser::unsafe_u2* ClassFileParser::parse_checked_exceptions(const ClassFileStream* const cfs, 1756 u2* const checked_exceptions_length, 1757 u4 method_attribute_length, 1758 TRAPS) { 1759 assert(cfs != nullptr, "invariant"); 1760 assert(checked_exceptions_length != nullptr, "invariant"); 1761 1762 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length 1763 *checked_exceptions_length = cfs->get_u2_fast(); 1764 const unsigned int size = 1765 (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); 1766 const unsafe_u2* const checked_exceptions_start = cfs->current(); 1767 assert(checked_exceptions_start != nullptr, "null checked exceptions"); 1768 if (!_need_verify) { 1769 cfs->skip_u2_fast(size); 1770 } else { 1771 // Verify each value in the checked exception table 1772 u2 checked_exception; 1773 const u2 len = *checked_exceptions_length; 1774 cfs->guarantee_more(2 * len, CHECK_NULL); 1775 for (int i = 0; i < len; i++) { 1776 checked_exception = cfs->get_u2_fast(); 1777 guarantee_property( 1778 valid_klass_reference_at(checked_exception), 1779 "Exception name has bad type at constant pool %u in class file %s", 1780 checked_exception, CHECK_NULL); 1781 } 1782 } 1783 // check exceptions attribute length 1784 if (_need_verify) { 1785 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) + 1786 sizeof(u2) * size), 1787 "Exceptions attribute has wrong length in class file %s", CHECK_NULL); 1788 } 1789 return checked_exceptions_start; 1790 } 1791 1792 void ClassFileParser::throwIllegalSignature(const char* type, 1793 const Symbol* name, 1794 const Symbol* sig, 1795 TRAPS) const { 1796 assert(name != nullptr, "invariant"); 1797 assert(sig != nullptr, "invariant"); 1798 1799 ResourceMark rm(THREAD); 1800 // Names are all known to be < 64k so we know this formatted message is not excessively large. 1801 Exceptions::fthrow(THREAD_AND_LOCATION, 1802 vmSymbols::java_lang_ClassFormatError(), 1803 "%s \"%s\" in class %s has illegal signature \"%s\"", type, 1804 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); 1805 } 1806 1807 AnnotationCollector::ID 1808 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data, 1809 const Symbol* name, 1810 const bool can_access_vm_annotations) { 1811 const vmSymbolID sid = vmSymbols::find_sid(name); 1812 // Privileged code can use all annotations. Other code silently drops some. 1813 const bool privileged = loader_data->is_boot_class_loader_data() || 1814 loader_data->is_platform_class_loader_data() || 1815 can_access_vm_annotations; 1816 switch (sid) { 1817 case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): { 1818 if (_location != _in_method) break; // only allow for methods 1819 if (!privileged) break; // only allow in privileged code 1820 return _method_CallerSensitive; 1821 } 1822 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): { 1823 if (_location != _in_method) break; // only allow for methods 1824 if (!privileged) break; // only allow in privileged code 1825 return _method_ForceInline; 1826 } 1827 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): { 1828 if (_location != _in_method) break; // only allow for methods 1829 if (!privileged) break; // only allow in privileged code 1830 return _method_DontInline; 1831 } 1832 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ChangesCurrentThread_signature): { 1833 if (_location != _in_method) break; // only allow for methods 1834 if (!privileged) break; // only allow in privileged code 1835 return _method_ChangesCurrentThread; 1836 } 1837 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_JvmtiHideEvents_signature): { 1838 if (_location != _in_method) break; // only allow for methods 1839 if (!privileged) break; // only allow in privileged code 1840 return _method_JvmtiHideEvents; 1841 } 1842 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_JvmtiMountTransition_signature): { 1843 if (_location != _in_method) break; // only allow for methods 1844 if (!privileged) break; // only allow in privileged code 1845 return _method_JvmtiMountTransition; 1846 } 1847 case VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): { 1848 if (_location != _in_method) break; // only allow for methods 1849 if (!privileged) break; // only allow in privileged code 1850 return _method_InjectedProfile; 1851 } 1852 case VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): { 1853 if (_location != _in_method) break; // only allow for methods 1854 if (!privileged) break; // only allow in privileged code 1855 return _method_LambdaForm_Compiled; 1856 } 1857 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Hidden_signature): { 1858 if (_location != _in_method) break; // only allow for methods 1859 if (!privileged) break; // only allow in privileged code 1860 return _method_Hidden; 1861 } 1862 case VM_SYMBOL_ENUM_NAME(jdk_internal_misc_Scoped_signature): { 1863 if (_location != _in_method) break; // only allow for methods 1864 if (!privileged) break; // only allow in privileged code 1865 return _method_Scoped; 1866 } 1867 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_IntrinsicCandidate_signature): { 1868 if (_location != _in_method) break; // only allow for methods 1869 if (!privileged) break; // only allow in privileged code 1870 return _method_IntrinsicCandidate; 1871 } 1872 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Stable_signature): { 1873 if (_location != _in_field) break; // only allow for fields 1874 if (!privileged) break; // only allow in privileged code 1875 return _field_Stable; 1876 } 1877 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): { 1878 if (_location != _in_field && _location != _in_class) { 1879 break; // only allow for fields and classes 1880 } 1881 if (!EnableContended || (RestrictContended && !privileged)) { 1882 break; // honor privileges 1883 } 1884 return _jdk_internal_vm_annotation_Contended; 1885 } 1886 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): { 1887 if (_location != _in_method) break; // only allow for methods 1888 if (RestrictReservedStack && !privileged) break; // honor privileges 1889 return _jdk_internal_vm_annotation_ReservedStackAccess; 1890 } 1891 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): { 1892 if (_location != _in_class) break; // only allow for classes 1893 if (!privileged) break; // only allow in privileged code 1894 return _jdk_internal_ValueBased; 1895 } 1896 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): { 1897 return _java_lang_Deprecated; 1898 } 1899 default: { 1900 break; 1901 } 1902 } 1903 return AnnotationCollector::_unknown; 1904 } 1905 1906 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { 1907 if (is_contended()) 1908 // Setting the contended group also sets the contended bit in field flags 1909 f->set_contended_group(contended_group()); 1910 if (is_stable()) 1911 (f->field_flags_addr())->update_stable(true); 1912 } 1913 1914 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() { 1915 // If there's an error deallocate metadata for field annotations 1916 MetadataFactory::free_array<u1>(_loader_data, _field_annotations); 1917 MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations); 1918 } 1919 1920 void MethodAnnotationCollector::apply_to(const methodHandle& m) { 1921 if (has_annotation(_method_CallerSensitive)) 1922 m->set_caller_sensitive(); 1923 if (has_annotation(_method_ForceInline)) 1924 m->set_force_inline(); 1925 if (has_annotation(_method_DontInline)) 1926 m->set_dont_inline(); 1927 if (has_annotation(_method_ChangesCurrentThread)) 1928 m->set_changes_current_thread(); 1929 if (has_annotation(_method_JvmtiHideEvents)) 1930 m->set_jvmti_hide_events(); 1931 if (has_annotation(_method_JvmtiMountTransition)) 1932 m->set_jvmti_mount_transition(); 1933 if (has_annotation(_method_InjectedProfile)) 1934 m->set_has_injected_profile(); 1935 if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none) 1936 m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm); 1937 if (has_annotation(_method_Hidden)) 1938 m->set_is_hidden(); 1939 if (has_annotation(_method_Scoped)) 1940 m->set_scoped(); 1941 if (has_annotation(_method_IntrinsicCandidate) && !m->is_synthetic()) 1942 m->set_intrinsic_candidate(); 1943 if (has_annotation(_jdk_internal_vm_annotation_ReservedStackAccess)) 1944 m->set_has_reserved_stack_access(); 1945 if (has_annotation(_java_lang_Deprecated)) 1946 m->set_deprecated(); 1947 if (has_annotation(_java_lang_Deprecated_for_removal)) 1948 m->set_deprecated_for_removal(); 1949 } 1950 1951 void ClassFileParser::ClassAnnotationCollector::apply_to(InstanceKlass* ik) { 1952 assert(ik != nullptr, "invariant"); 1953 if (has_annotation(_jdk_internal_vm_annotation_Contended)) { 1954 ik->set_is_contended(is_contended()); 1955 } 1956 if (has_annotation(_jdk_internal_ValueBased)) { 1957 ik->set_has_value_based_class_annotation(); 1958 if (DiagnoseSyncOnValueBasedClasses) { 1959 ik->set_is_value_based(); 1960 } 1961 } 1962 if (has_annotation(_java_lang_Deprecated)) { 1963 Array<Method*>* methods = ik->methods(); 1964 int length = ik->methods()->length(); 1965 for (int i = 0; i < length; i++) { 1966 Method* m = methods->at(i); 1967 m->set_deprecated(); 1968 } 1969 } 1970 if (has_annotation(_java_lang_Deprecated_for_removal)) { 1971 Array<Method*>* methods = ik->methods(); 1972 int length = ik->methods()->length(); 1973 for (int i = 0; i < length; i++) { 1974 Method* m = methods->at(i); 1975 m->set_deprecated_for_removal(); 1976 } 1977 } 1978 } 1979 1980 #define MAX_ARGS_SIZE 255 1981 #define MAX_CODE_SIZE 65535 1982 #define INITIAL_MAX_LVT_NUMBER 256 1983 1984 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. 1985 * 1986 * Rules for LVT's and LVTT's are: 1987 * - There can be any number of LVT's and LVTT's. 1988 * - If there are n LVT's, it is the same as if there was just 1989 * one LVT containing all the entries from the n LVT's. 1990 * - There may be no more than one LVT entry per local variable. 1991 * Two LVT entries are 'equal' if these fields are the same: 1992 * start_pc, length, name, slot 1993 * - There may be no more than one LVTT entry per each LVT entry. 1994 * Each LVTT entry has to match some LVT entry. 1995 * - HotSpot internal LVT keeps natural ordering of class file LVT entries. 1996 */ 1997 void ClassFileParser::copy_localvariable_table(const ConstMethod* cm, 1998 int lvt_cnt, 1999 u2* const localvariable_table_length, 2000 const unsafe_u2** const localvariable_table_start, 2001 int lvtt_cnt, 2002 u2* const localvariable_type_table_length, 2003 const unsafe_u2** const localvariable_type_table_start, 2004 TRAPS) { 2005 2006 ResourceMark rm(THREAD); 2007 2008 typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*, 2009 256, AnyObj::RESOURCE_AREA, mtInternal, 2010 &LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable; 2011 2012 LVT_HashTable* const table = new LVT_HashTable(); 2013 2014 // To fill LocalVariableTable in 2015 const Classfile_LVT_Element* cf_lvt; 2016 LocalVariableTableElement* lvt = cm->localvariable_table_start(); 2017 2018 for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { 2019 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; 2020 for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { 2021 copy_lvt_element(&cf_lvt[idx], lvt); 2022 // If no duplicates, add LVT elem in hashtable. 2023 if (table->put(*lvt, lvt) == false 2024 && _need_verify 2025 && _major_version >= JAVA_1_5_VERSION) { 2026 classfile_parse_error("Duplicated LocalVariableTable attribute " 2027 "entry for '%s' in class file %s", 2028 _cp->symbol_at(lvt->name_cp_index)->as_utf8(), 2029 THREAD); 2030 return; 2031 } 2032 } 2033 } 2034 2035 // To merge LocalVariableTable and LocalVariableTypeTable 2036 const Classfile_LVT_Element* cf_lvtt; 2037 LocalVariableTableElement lvtt_elem; 2038 2039 for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { 2040 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; 2041 for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { 2042 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); 2043 LocalVariableTableElement** entry = table->get(lvtt_elem); 2044 if (entry == nullptr) { 2045 if (_need_verify) { 2046 classfile_parse_error("LVTT entry for '%s' in class file %s " 2047 "does not match any LVT entry", 2048 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2049 THREAD); 2050 return; 2051 } 2052 } else if ((*entry)->signature_cp_index != 0 && _need_verify) { 2053 classfile_parse_error("Duplicated LocalVariableTypeTable attribute " 2054 "entry for '%s' in class file %s", 2055 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2056 THREAD); 2057 return; 2058 } else { 2059 // to add generic signatures into LocalVariableTable 2060 (*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index; 2061 } 2062 } 2063 } 2064 } 2065 2066 2067 void ClassFileParser::copy_method_annotations(ConstMethod* cm, 2068 const u1* runtime_visible_annotations, 2069 int runtime_visible_annotations_length, 2070 const u1* runtime_visible_parameter_annotations, 2071 int runtime_visible_parameter_annotations_length, 2072 const u1* runtime_visible_type_annotations, 2073 int runtime_visible_type_annotations_length, 2074 const u1* annotation_default, 2075 int annotation_default_length, 2076 TRAPS) { 2077 2078 AnnotationArray* a; 2079 2080 if (runtime_visible_annotations_length > 0) { 2081 a = allocate_annotations(runtime_visible_annotations, 2082 runtime_visible_annotations_length, 2083 CHECK); 2084 cm->set_method_annotations(a); 2085 } 2086 2087 if (runtime_visible_parameter_annotations_length > 0) { 2088 a = allocate_annotations(runtime_visible_parameter_annotations, 2089 runtime_visible_parameter_annotations_length, 2090 CHECK); 2091 cm->set_parameter_annotations(a); 2092 } 2093 2094 if (annotation_default_length > 0) { 2095 a = allocate_annotations(annotation_default, 2096 annotation_default_length, 2097 CHECK); 2098 cm->set_default_annotations(a); 2099 } 2100 2101 if (runtime_visible_type_annotations_length > 0) { 2102 a = allocate_annotations(runtime_visible_type_annotations, 2103 runtime_visible_type_annotations_length, 2104 CHECK); 2105 cm->set_type_annotations(a); 2106 } 2107 } 2108 2109 2110 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions 2111 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the 2112 // Method* to save footprint, so we only know the size of the resulting Method* when the 2113 // entire method attribute is parsed. 2114 // 2115 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass. 2116 2117 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs, 2118 bool is_interface, 2119 const ConstantPool* cp, 2120 bool* const has_localvariable_table, 2121 TRAPS) { 2122 assert(cfs != nullptr, "invariant"); 2123 assert(cp != nullptr, "invariant"); 2124 assert(has_localvariable_table != nullptr, "invariant"); 2125 2126 ResourceMark rm(THREAD); 2127 // Parse fixed parts: 2128 // access_flags, name_index, descriptor_index, attributes_count 2129 cfs->guarantee_more(8, CHECK_NULL); 2130 2131 u2 flags = cfs->get_u2_fast(); 2132 const u2 name_index = cfs->get_u2_fast(); 2133 const int cp_size = cp->length(); 2134 guarantee_property( 2135 valid_symbol_at(name_index), 2136 "Illegal constant pool index %u for method name in class file %s", 2137 name_index, CHECK_NULL); 2138 const Symbol* const name = cp->symbol_at(name_index); 2139 verify_legal_method_name(name, CHECK_NULL); 2140 2141 const u2 signature_index = cfs->get_u2_fast(); 2142 guarantee_property( 2143 valid_symbol_at(signature_index), 2144 "Illegal constant pool index %u for method signature in class file %s", 2145 signature_index, CHECK_NULL); 2146 const Symbol* const signature = cp->symbol_at(signature_index); 2147 2148 if (name == vmSymbols::class_initializer_name()) { 2149 // We ignore the other access flags for a valid class initializer. 2150 // (JVM Spec 2nd ed., chapter 4.6) 2151 if (_major_version < 51) { // backward compatibility 2152 flags = JVM_ACC_STATIC; 2153 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) { 2154 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0); 2155 } else { 2156 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD); 2157 return nullptr; 2158 } 2159 } else { 2160 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL); 2161 } 2162 2163 if (name == vmSymbols::object_initializer_name() && is_interface) { 2164 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD); 2165 return nullptr; 2166 } 2167 2168 int args_size = -1; // only used when _need_verify is true 2169 if (_need_verify) { 2170 verify_legal_name_with_signature(name, signature, CHECK_NULL); 2171 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) + 2172 verify_legal_method_signature(name, signature, CHECK_NULL); 2173 if (args_size > MAX_ARGS_SIZE) { 2174 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD); 2175 return nullptr; 2176 } 2177 } 2178 2179 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS); 2180 2181 // Default values for code and exceptions attribute elements 2182 u2 max_stack = 0; 2183 u2 max_locals = 0; 2184 u4 code_length = 0; 2185 const u1* code_start = nullptr; 2186 u2 exception_table_length = 0; 2187 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements 2188 Array<int>* exception_handlers = Universe::the_empty_int_array(); 2189 u2 checked_exceptions_length = 0; 2190 const unsafe_u2* checked_exceptions_start = nullptr; // (potentially unaligned) pointer to array of u2 elements 2191 CompressedLineNumberWriteStream* linenumber_table = nullptr; 2192 int linenumber_table_length = 0; 2193 int total_lvt_length = 0; 2194 u2 lvt_cnt = 0; 2195 u2 lvtt_cnt = 0; 2196 bool lvt_allocated = false; 2197 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER; 2198 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER; 2199 u2* localvariable_table_length = nullptr; 2200 const unsafe_u2** localvariable_table_start = nullptr; // (potentially unaligned) pointer to array of LVT attributes 2201 u2* localvariable_type_table_length = nullptr; 2202 const unsafe_u2** localvariable_type_table_start = nullptr; // (potentially unaligned) pointer to LVTT attributes 2203 int method_parameters_length = -1; 2204 const u1* method_parameters_data = nullptr; 2205 bool method_parameters_seen = false; 2206 bool parsed_code_attribute = false; 2207 bool parsed_checked_exceptions_attribute = false; 2208 bool parsed_stackmap_attribute = false; 2209 // stackmap attribute - JDK1.5 2210 const u1* stackmap_data = nullptr; 2211 int stackmap_data_length = 0; 2212 u2 generic_signature_index = 0; 2213 MethodAnnotationCollector parsed_annotations; 2214 const u1* runtime_visible_annotations = nullptr; 2215 int runtime_visible_annotations_length = 0; 2216 const u1* runtime_visible_parameter_annotations = nullptr; 2217 int runtime_visible_parameter_annotations_length = 0; 2218 const u1* runtime_visible_type_annotations = nullptr; 2219 int runtime_visible_type_annotations_length = 0; 2220 bool runtime_invisible_annotations_exists = false; 2221 bool runtime_invisible_type_annotations_exists = false; 2222 bool runtime_invisible_parameter_annotations_exists = false; 2223 const u1* annotation_default = nullptr; 2224 int annotation_default_length = 0; 2225 2226 // Parse code and exceptions attribute 2227 u2 method_attributes_count = cfs->get_u2_fast(); 2228 while (method_attributes_count--) { 2229 cfs->guarantee_more(6, CHECK_NULL); // method_attribute_name_index, method_attribute_length 2230 const u2 method_attribute_name_index = cfs->get_u2_fast(); 2231 const u4 method_attribute_length = cfs->get_u4_fast(); 2232 guarantee_property( 2233 valid_symbol_at(method_attribute_name_index), 2234 "Invalid method attribute name index %u in class file %s", 2235 method_attribute_name_index, CHECK_NULL); 2236 2237 const Symbol* const method_attribute_name = cp->symbol_at(method_attribute_name_index); 2238 if (method_attribute_name == vmSymbols::tag_code()) { 2239 // Parse Code attribute 2240 if (_need_verify) { 2241 guarantee_property( 2242 !access_flags.is_native() && !access_flags.is_abstract(), 2243 "Code attribute in native or abstract methods in class file %s", 2244 CHECK_NULL); 2245 } 2246 if (parsed_code_attribute) { 2247 classfile_parse_error("Multiple Code attributes in class file %s", 2248 THREAD); 2249 return nullptr; 2250 } 2251 parsed_code_attribute = true; 2252 2253 // Stack size, locals size, and code size 2254 cfs->guarantee_more(8, CHECK_NULL); 2255 max_stack = cfs->get_u2_fast(); 2256 max_locals = cfs->get_u2_fast(); 2257 code_length = cfs->get_u4_fast(); 2258 if (_need_verify) { 2259 guarantee_property(args_size <= max_locals, 2260 "Arguments can't fit into locals in class file %s", 2261 CHECK_NULL); 2262 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE, 2263 "Invalid method Code length %u in class file %s", 2264 code_length, CHECK_NULL); 2265 } 2266 // Code pointer 2267 code_start = cfs->current(); 2268 assert(code_start != nullptr, "null code start"); 2269 cfs->guarantee_more(code_length, CHECK_NULL); 2270 cfs->skip_u1_fast(code_length); 2271 2272 // Exception handler table 2273 cfs->guarantee_more(2, CHECK_NULL); // exception_table_length 2274 exception_table_length = cfs->get_u2_fast(); 2275 if (exception_table_length > 0) { 2276 exception_table_start = parse_exception_table(cfs, 2277 code_length, 2278 exception_table_length, 2279 CHECK_NULL); 2280 } 2281 2282 // Parse additional attributes in code attribute 2283 cfs->guarantee_more(2, CHECK_NULL); // code_attributes_count 2284 u2 code_attributes_count = cfs->get_u2_fast(); 2285 2286 unsigned int calculated_attribute_length = 0; 2287 2288 calculated_attribute_length = 2289 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length); 2290 calculated_attribute_length += checked_cast<unsigned int>( 2291 code_length + 2292 sizeof(exception_table_length) + 2293 sizeof(code_attributes_count) + 2294 exception_table_length * 2295 ( sizeof(u2) + // start_pc 2296 sizeof(u2) + // end_pc 2297 sizeof(u2) + // handler_pc 2298 sizeof(u2) )); // catch_type_index 2299 2300 while (code_attributes_count--) { 2301 cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length 2302 const u2 code_attribute_name_index = cfs->get_u2_fast(); 2303 const u4 code_attribute_length = cfs->get_u4_fast(); 2304 calculated_attribute_length += code_attribute_length + 2305 (unsigned)sizeof(code_attribute_name_index) + 2306 (unsigned)sizeof(code_attribute_length); 2307 guarantee_property(valid_symbol_at(code_attribute_name_index), 2308 "Invalid code attribute name index %u in class file %s", 2309 code_attribute_name_index, 2310 CHECK_NULL); 2311 if (LoadLineNumberTables && 2312 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { 2313 // Parse and compress line number table 2314 parse_linenumber_table(code_attribute_length, 2315 code_length, 2316 &linenumber_table, 2317 CHECK_NULL); 2318 2319 } else if (LoadLocalVariableTables && 2320 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { 2321 // Parse local variable table 2322 if (!lvt_allocated) { 2323 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2324 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2325 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2326 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2327 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2328 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2329 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2330 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2331 lvt_allocated = true; 2332 } 2333 if (lvt_cnt == max_lvt_cnt) { 2334 max_lvt_cnt <<= 1; 2335 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt); 2336 localvariable_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt); 2337 } 2338 localvariable_table_start[lvt_cnt] = 2339 parse_localvariable_table(cfs, 2340 code_length, 2341 max_locals, 2342 code_attribute_length, 2343 &localvariable_table_length[lvt_cnt], 2344 false, // is not LVTT 2345 CHECK_NULL); 2346 total_lvt_length += localvariable_table_length[lvt_cnt]; 2347 lvt_cnt++; 2348 } else if (LoadLocalVariableTypeTables && 2349 _major_version >= JAVA_1_5_VERSION && 2350 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { 2351 if (!lvt_allocated) { 2352 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2353 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2354 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2355 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2356 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2357 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2358 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2359 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2360 lvt_allocated = true; 2361 } 2362 // Parse local variable type table 2363 if (lvtt_cnt == max_lvtt_cnt) { 2364 max_lvtt_cnt <<= 1; 2365 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt); 2366 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt); 2367 } 2368 localvariable_type_table_start[lvtt_cnt] = 2369 parse_localvariable_table(cfs, 2370 code_length, 2371 max_locals, 2372 code_attribute_length, 2373 &localvariable_type_table_length[lvtt_cnt], 2374 true, // is LVTT 2375 CHECK_NULL); 2376 lvtt_cnt++; 2377 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && 2378 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { 2379 // Stack map is only needed by the new verifier in JDK1.5. 2380 if (parsed_stackmap_attribute) { 2381 classfile_parse_error("Multiple StackMapTable attributes in class file %s", THREAD); 2382 return nullptr; 2383 } 2384 stackmap_data = parse_stackmap_table(cfs, code_attribute_length, CHECK_NULL); 2385 stackmap_data_length = code_attribute_length; 2386 parsed_stackmap_attribute = true; 2387 } else { 2388 // Skip unknown attributes 2389 cfs->skip_u1(code_attribute_length, CHECK_NULL); 2390 } 2391 } 2392 // check method attribute length 2393 if (_need_verify) { 2394 guarantee_property(method_attribute_length == calculated_attribute_length, 2395 "Code segment has wrong length in class file %s", 2396 CHECK_NULL); 2397 } 2398 } else if (method_attribute_name == vmSymbols::tag_exceptions()) { 2399 // Parse Exceptions attribute 2400 if (parsed_checked_exceptions_attribute) { 2401 classfile_parse_error("Multiple Exceptions attributes in class file %s", 2402 THREAD); 2403 return nullptr; 2404 } 2405 parsed_checked_exceptions_attribute = true; 2406 checked_exceptions_start = 2407 parse_checked_exceptions(cfs, 2408 &checked_exceptions_length, 2409 method_attribute_length, 2410 CHECK_NULL); 2411 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { 2412 // reject multiple method parameters 2413 if (method_parameters_seen) { 2414 classfile_parse_error("Multiple MethodParameters attributes in class file %s", 2415 THREAD); 2416 return nullptr; 2417 } 2418 method_parameters_seen = true; 2419 method_parameters_length = cfs->get_u1_fast(); 2420 const u4 real_length = (method_parameters_length * 4u) + 1u; 2421 if (method_attribute_length != real_length) { 2422 classfile_parse_error( 2423 "Invalid MethodParameters method attribute length %u in class file", 2424 method_attribute_length, THREAD); 2425 return nullptr; 2426 } 2427 method_parameters_data = cfs->current(); 2428 cfs->skip_u2_fast(method_parameters_length); 2429 cfs->skip_u2_fast(method_parameters_length); 2430 // ignore this attribute if it cannot be reflected 2431 if (!vmClasses::Parameter_klass_loaded()) 2432 method_parameters_length = -1; 2433 } else if (method_attribute_name == vmSymbols::tag_synthetic()) { 2434 if (method_attribute_length != 0) { 2435 classfile_parse_error( 2436 "Invalid Synthetic method attribute length %u in class file %s", 2437 method_attribute_length, THREAD); 2438 return nullptr; 2439 } 2440 // Should we check that there hasn't already been a synthetic attribute? 2441 access_flags.set_is_synthetic(); 2442 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120 2443 if (method_attribute_length != 0) { 2444 classfile_parse_error( 2445 "Invalid Deprecated method attribute length %u in class file %s", 2446 method_attribute_length, THREAD); 2447 return nullptr; 2448 } 2449 } else if (_major_version >= JAVA_1_5_VERSION) { 2450 if (method_attribute_name == vmSymbols::tag_signature()) { 2451 if (generic_signature_index != 0) { 2452 classfile_parse_error( 2453 "Multiple Signature attributes for method in class file %s", 2454 THREAD); 2455 return nullptr; 2456 } 2457 if (method_attribute_length != 2) { 2458 classfile_parse_error( 2459 "Invalid Signature attribute length %u in class file %s", 2460 method_attribute_length, THREAD); 2461 return nullptr; 2462 } 2463 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL); 2464 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2465 if (runtime_visible_annotations != nullptr) { 2466 classfile_parse_error( 2467 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", 2468 THREAD); 2469 return nullptr; 2470 } 2471 runtime_visible_annotations_length = method_attribute_length; 2472 runtime_visible_annotations = cfs->current(); 2473 assert(runtime_visible_annotations != nullptr, "null visible annotations"); 2474 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL); 2475 parse_annotations(cp, 2476 runtime_visible_annotations, 2477 runtime_visible_annotations_length, 2478 &parsed_annotations, 2479 _loader_data, 2480 _can_access_vm_annotations); 2481 cfs->skip_u1_fast(runtime_visible_annotations_length); 2482 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2483 if (runtime_invisible_annotations_exists) { 2484 classfile_parse_error( 2485 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", 2486 THREAD); 2487 return nullptr; 2488 } 2489 runtime_invisible_annotations_exists = true; 2490 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2491 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) { 2492 if (runtime_visible_parameter_annotations != nullptr) { 2493 classfile_parse_error( 2494 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", 2495 THREAD); 2496 return nullptr; 2497 } 2498 runtime_visible_parameter_annotations_length = method_attribute_length; 2499 runtime_visible_parameter_annotations = cfs->current(); 2500 assert(runtime_visible_parameter_annotations != nullptr, "null visible parameter annotations"); 2501 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_NULL); 2502 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) { 2503 if (runtime_invisible_parameter_annotations_exists) { 2504 classfile_parse_error( 2505 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", 2506 THREAD); 2507 return nullptr; 2508 } 2509 runtime_invisible_parameter_annotations_exists = true; 2510 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2511 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) { 2512 if (annotation_default != nullptr) { 2513 classfile_parse_error( 2514 "Multiple AnnotationDefault attributes for method in class file %s", 2515 THREAD); 2516 return nullptr; 2517 } 2518 annotation_default_length = method_attribute_length; 2519 annotation_default = cfs->current(); 2520 assert(annotation_default != nullptr, "null annotation default"); 2521 cfs->skip_u1(annotation_default_length, CHECK_NULL); 2522 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 2523 if (runtime_visible_type_annotations != nullptr) { 2524 classfile_parse_error( 2525 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s", 2526 THREAD); 2527 return nullptr; 2528 } 2529 runtime_visible_type_annotations_length = method_attribute_length; 2530 runtime_visible_type_annotations = cfs->current(); 2531 assert(runtime_visible_type_annotations != nullptr, "null visible type annotations"); 2532 // No need for the VM to parse Type annotations 2533 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_NULL); 2534 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 2535 if (runtime_invisible_type_annotations_exists) { 2536 classfile_parse_error( 2537 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s", 2538 THREAD); 2539 return nullptr; 2540 } 2541 runtime_invisible_type_annotations_exists = true; 2542 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2543 } else { 2544 // Skip unknown attributes 2545 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2546 } 2547 } else { 2548 // Skip unknown attributes 2549 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2550 } 2551 } 2552 2553 if (linenumber_table != nullptr) { 2554 linenumber_table->write_terminator(); 2555 linenumber_table_length = linenumber_table->position(); 2556 } 2557 2558 // Make sure there's at least one Code attribute in non-native/non-abstract method 2559 if (_need_verify) { 2560 guarantee_property(access_flags.is_native() || 2561 access_flags.is_abstract() || 2562 parsed_code_attribute, 2563 "Absent Code attribute in method that is not native or abstract in class file %s", 2564 CHECK_NULL); 2565 } 2566 2567 // All sizing information for a Method* is finally available, now create it 2568 InlineTableSizes sizes( 2569 total_lvt_length, 2570 linenumber_table_length, 2571 exception_table_length, 2572 checked_exceptions_length, 2573 method_parameters_length, 2574 generic_signature_index, 2575 runtime_visible_annotations_length, 2576 runtime_visible_parameter_annotations_length, 2577 runtime_visible_type_annotations_length, 2578 annotation_default_length, 2579 0); 2580 2581 Method* const m = Method::allocate(_loader_data, 2582 code_length, 2583 access_flags, 2584 &sizes, 2585 ConstMethod::NORMAL, 2586 _cp->symbol_at(name_index), 2587 CHECK_NULL); 2588 2589 ClassLoadingService::add_class_method_size(m->size()*wordSize); 2590 2591 // Fill in information from fixed part (access_flags already set) 2592 m->set_constants(_cp); 2593 m->set_name_index(name_index); 2594 m->set_signature_index(signature_index); 2595 m->constMethod()->compute_from_signature(cp->symbol_at(signature_index), access_flags.is_static()); 2596 assert(args_size < 0 || args_size == m->size_of_parameters(), ""); 2597 2598 // Fill in code attribute information 2599 m->set_max_stack(max_stack); 2600 m->set_max_locals(max_locals); 2601 if (stackmap_data != nullptr) { 2602 m->constMethod()->copy_stackmap_data(_loader_data, 2603 (u1*)stackmap_data, 2604 stackmap_data_length, 2605 CHECK_NULL); 2606 } 2607 2608 // Copy byte codes 2609 m->set_code((u1*)code_start); 2610 2611 // Copy line number table 2612 if (linenumber_table != nullptr) { 2613 memcpy(m->compressed_linenumber_table(), 2614 linenumber_table->buffer(), 2615 linenumber_table_length); 2616 } 2617 2618 // Copy exception table 2619 if (exception_table_length > 0) { 2620 Copy::conjoint_swap_if_needed<Endian::JAVA>(exception_table_start, 2621 m->exception_table_start(), 2622 exception_table_length * sizeof(ExceptionTableElement), 2623 sizeof(u2)); 2624 } 2625 2626 // Copy method parameters 2627 if (method_parameters_length > 0) { 2628 MethodParametersElement* elem = m->constMethod()->method_parameters_start(); 2629 for (int i = 0; i < method_parameters_length; i++) { 2630 elem[i].name_cp_index = Bytes::get_Java_u2((address)method_parameters_data); 2631 method_parameters_data += 2; 2632 elem[i].flags = Bytes::get_Java_u2((address)method_parameters_data); 2633 method_parameters_data += 2; 2634 } 2635 } 2636 2637 // Copy checked exceptions 2638 if (checked_exceptions_length > 0) { 2639 Copy::conjoint_swap_if_needed<Endian::JAVA>(checked_exceptions_start, 2640 m->checked_exceptions_start(), 2641 checked_exceptions_length * sizeof(CheckedExceptionElement), 2642 sizeof(u2)); 2643 } 2644 2645 // Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2646 if (total_lvt_length > 0) { 2647 *has_localvariable_table = true; 2648 copy_localvariable_table(m->constMethod(), 2649 lvt_cnt, 2650 localvariable_table_length, 2651 localvariable_table_start, 2652 lvtt_cnt, 2653 localvariable_type_table_length, 2654 localvariable_type_table_start, 2655 CHECK_NULL); 2656 } 2657 2658 if (parsed_annotations.has_any_annotations()) 2659 parsed_annotations.apply_to(methodHandle(THREAD, m)); 2660 2661 if (is_hidden()) { // Mark methods in hidden classes as 'hidden'. 2662 m->set_is_hidden(); 2663 } 2664 2665 // Copy annotations 2666 copy_method_annotations(m->constMethod(), 2667 runtime_visible_annotations, 2668 runtime_visible_annotations_length, 2669 runtime_visible_parameter_annotations, 2670 runtime_visible_parameter_annotations_length, 2671 runtime_visible_type_annotations, 2672 runtime_visible_type_annotations_length, 2673 annotation_default, 2674 annotation_default_length, 2675 CHECK_NULL); 2676 2677 if (InstanceKlass::is_finalization_enabled() && 2678 name == vmSymbols::finalize_method_name() && 2679 signature == vmSymbols::void_method_signature()) { 2680 if (m->is_empty_method()) { 2681 _has_empty_finalizer = true; 2682 } else { 2683 _has_finalizer = true; 2684 } 2685 } 2686 2687 NOT_PRODUCT(m->verify()); 2688 return m; 2689 } 2690 2691 2692 // Side-effects: populates the _methods field in the parser 2693 void ClassFileParser::parse_methods(const ClassFileStream* const cfs, 2694 bool is_interface, 2695 bool* const has_localvariable_table, 2696 bool* has_final_method, 2697 bool* declares_nonstatic_concrete_methods, 2698 TRAPS) { 2699 assert(cfs != nullptr, "invariant"); 2700 assert(has_localvariable_table != nullptr, "invariant"); 2701 assert(has_final_method != nullptr, "invariant"); 2702 assert(declares_nonstatic_concrete_methods != nullptr, "invariant"); 2703 2704 assert(nullptr == _methods, "invariant"); 2705 2706 cfs->guarantee_more(2, CHECK); // length 2707 const u2 length = cfs->get_u2_fast(); 2708 if (length == 0) { 2709 _methods = Universe::the_empty_method_array(); 2710 } else { 2711 _methods = MetadataFactory::new_array<Method*>(_loader_data, 2712 length, 2713 nullptr, 2714 CHECK); 2715 2716 for (int index = 0; index < length; index++) { 2717 Method* method = parse_method(cfs, 2718 is_interface, 2719 _cp, 2720 has_localvariable_table, 2721 CHECK); 2722 2723 if (method->is_final()) { 2724 *has_final_method = true; 2725 } 2726 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags 2727 // used for interface initialization, and default method inheritance analysis 2728 if (is_interface && !(*declares_nonstatic_concrete_methods) 2729 && !method->is_abstract() && !method->is_static()) { 2730 *declares_nonstatic_concrete_methods = true; 2731 } 2732 _methods->at_put(index, method); 2733 } 2734 2735 if (_need_verify && length > 1) { 2736 // Check duplicated methods 2737 ResourceMark rm(THREAD); 2738 // Set containing name-signature pairs 2739 NameSigHashtable* names_and_sigs = new NameSigHashtable(); 2740 for (int i = 0; i < length; i++) { 2741 const Method* const m = _methods->at(i); 2742 NameSigHash name_and_sig(m->name(), m->signature()); 2743 // If no duplicates, add name/signature in hashtable names_and_sigs. 2744 if(!names_and_sigs->put(name_and_sig, 0)) { 2745 classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s", 2746 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD); 2747 return; 2748 } 2749 } 2750 } 2751 } 2752 } 2753 2754 static const intArray* sort_methods(Array<Method*>* methods) { 2755 const int length = methods->length(); 2756 // If JVMTI original method ordering or sharing is enabled we have to 2757 // remember the original class file ordering. 2758 // We temporarily use the vtable_index field in the Method* to store the 2759 // class file index, so we can read in after calling qsort. 2760 // Put the method ordering in the shared archive. 2761 if (JvmtiExport::can_maintain_original_method_order() || CDSConfig::is_dumping_archive()) { 2762 for (int index = 0; index < length; index++) { 2763 Method* const m = methods->at(index); 2764 assert(!m->valid_vtable_index(), "vtable index should not be set"); 2765 m->set_vtable_index(index); 2766 } 2767 } 2768 // Sort method array by ascending method name (for faster lookups & vtable construction) 2769 // Note that the ordering is not alphabetical, see Symbol::fast_compare 2770 Method::sort_methods(methods); 2771 2772 intArray* method_ordering = nullptr; 2773 // If JVMTI original method ordering or sharing is enabled construct int 2774 // array remembering the original ordering 2775 if (JvmtiExport::can_maintain_original_method_order() || CDSConfig::is_dumping_archive()) { 2776 method_ordering = new intArray(length, length, -1); 2777 for (int index = 0; index < length; index++) { 2778 Method* const m = methods->at(index); 2779 const int old_index = m->vtable_index(); 2780 assert(old_index >= 0 && old_index < length, "invalid method index"); 2781 method_ordering->at_put(index, old_index); 2782 m->set_vtable_index(Method::invalid_vtable_index); 2783 } 2784 } 2785 return method_ordering; 2786 } 2787 2788 // Parse generic_signature attribute for methods and fields 2789 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs, 2790 TRAPS) { 2791 assert(cfs != nullptr, "invariant"); 2792 2793 cfs->guarantee_more(2, CHECK_0); // generic_signature_index 2794 const u2 generic_signature_index = cfs->get_u2_fast(); 2795 guarantee_property( 2796 valid_symbol_at(generic_signature_index), 2797 "Invalid Signature attribute at constant pool index %u in class file %s", 2798 generic_signature_index, CHECK_0); 2799 return generic_signature_index; 2800 } 2801 2802 void ClassFileParser::parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, 2803 TRAPS) { 2804 2805 assert(cfs != nullptr, "invariant"); 2806 2807 cfs->guarantee_more(2, CHECK); // sourcefile_index 2808 const u2 sourcefile_index = cfs->get_u2_fast(); 2809 guarantee_property( 2810 valid_symbol_at(sourcefile_index), 2811 "Invalid SourceFile attribute at constant pool index %u in class file %s", 2812 sourcefile_index, CHECK); 2813 set_class_sourcefile_index(sourcefile_index); 2814 } 2815 2816 void ClassFileParser::parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 2817 int length, 2818 TRAPS) { 2819 assert(cfs != nullptr, "invariant"); 2820 2821 const u1* const sde_buffer = cfs->current(); 2822 assert(sde_buffer != nullptr, "null sde buffer"); 2823 2824 // Don't bother storing it if there is no way to retrieve it 2825 if (JvmtiExport::can_get_source_debug_extension()) { 2826 assert((length+1) > length, "Overflow checking"); 2827 u1* const sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); 2828 for (int i = 0; i < length; i++) { 2829 sde[i] = sde_buffer[i]; 2830 } 2831 sde[length] = '\0'; 2832 set_class_sde_buffer((const char*)sde, length); 2833 } 2834 // Got utf8 string, set stream position forward 2835 cfs->skip_u1(length, CHECK); 2836 } 2837 2838 2839 // Inner classes can be static, private or protected (classic VM does this) 2840 #define RECOGNIZED_INNER_CLASS_MODIFIERS ( JVM_RECOGNIZED_CLASS_MODIFIERS | \ 2841 JVM_ACC_PRIVATE | \ 2842 JVM_ACC_PROTECTED | \ 2843 JVM_ACC_STATIC \ 2844 ) 2845 2846 // Find index of the InnerClasses entry for the specified inner_class_info_index. 2847 // Return -1 if none is found. 2848 static int inner_classes_find_index(const Array<u2>* inner_classes, int inner, const ConstantPool* cp, int length) { 2849 Symbol* cp_klass_name = cp->klass_name_at(inner); 2850 for (int idx = 0; idx < length; idx += InstanceKlass::inner_class_next_offset) { 2851 int idx_inner = inner_classes->at(idx + InstanceKlass::inner_class_inner_class_info_offset); 2852 if (cp->klass_name_at(idx_inner) == cp_klass_name) { 2853 return idx; 2854 } 2855 } 2856 return -1; 2857 } 2858 2859 // Return the outer_class_info_index for the InnerClasses entry containing the 2860 // specified inner_class_info_index. Return -1 if no InnerClasses entry is found. 2861 static int inner_classes_jump_to_outer(const Array<u2>* inner_classes, int inner, const ConstantPool* cp, int length) { 2862 if (inner == 0) return -1; 2863 int idx = inner_classes_find_index(inner_classes, inner, cp, length); 2864 if (idx == -1) return -1; 2865 int result = inner_classes->at(idx + InstanceKlass::inner_class_outer_class_info_offset); 2866 return result; 2867 } 2868 2869 // Return true if circularity is found, false if no circularity is found. 2870 // Use Floyd's cycle finding algorithm. 2871 static bool inner_classes_check_loop_through_outer(const Array<u2>* inner_classes, int idx, const ConstantPool* cp, int length) { 2872 int slow = inner_classes->at(idx + InstanceKlass::inner_class_inner_class_info_offset); 2873 int fast = inner_classes->at(idx + InstanceKlass::inner_class_outer_class_info_offset); 2874 2875 while (fast != -1 && fast != 0) { 2876 if (slow != 0 && (cp->klass_name_at(slow) == cp->klass_name_at(fast))) { 2877 return true; // found a circularity 2878 } 2879 fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length); 2880 if (fast == -1) return false; 2881 fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length); 2882 if (fast == -1) return false; 2883 slow = inner_classes_jump_to_outer(inner_classes, slow, cp, length); 2884 assert(slow != -1, "sanity check"); 2885 } 2886 return false; 2887 } 2888 2889 // Loop through each InnerClasses entry checking for circularities and duplications 2890 // with other entries. If duplicate entries are found then throw CFE. Otherwise, 2891 // return true if a circularity or entries with duplicate inner_class_info_indexes 2892 // are found. 2893 bool ClassFileParser::check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS) { 2894 // Loop through each InnerClasses entry. 2895 for (int idx = 0; idx < length; idx += InstanceKlass::inner_class_next_offset) { 2896 // Return true if there are circular entries. 2897 if (inner_classes_check_loop_through_outer(_inner_classes, idx, cp, length)) { 2898 return true; 2899 } 2900 // Check if there are duplicate entries or entries with the same inner_class_info_index. 2901 for (int y = idx + InstanceKlass::inner_class_next_offset; y < length; 2902 y += InstanceKlass::inner_class_next_offset) { 2903 2904 // 4347400: make sure there's no duplicate entry in the classes array 2905 if (_major_version >= JAVA_1_5_VERSION) { 2906 guarantee_property((_inner_classes->at(idx) != _inner_classes->at(y) || 2907 _inner_classes->at(idx+1) != _inner_classes->at(y+1) || 2908 _inner_classes->at(idx+2) != _inner_classes->at(y+2) || 2909 _inner_classes->at(idx+3) != _inner_classes->at(y+3)), 2910 "Duplicate entry in InnerClasses attribute in class file %s", 2911 CHECK_(true)); 2912 } 2913 // Return true if there are two entries with the same inner_class_info_index. 2914 if (_inner_classes->at(y) == _inner_classes->at(idx)) { 2915 return true; 2916 } 2917 } 2918 } 2919 return false; 2920 } 2921 2922 // Return number of classes in the inner classes attribute table 2923 u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 2924 const ConstantPool* cp, 2925 const u1* const inner_classes_attribute_start, 2926 bool parsed_enclosingmethod_attribute, 2927 u2 enclosing_method_class_index, 2928 u2 enclosing_method_method_index, 2929 TRAPS) { 2930 const u1* const current_mark = cfs->current(); 2931 u2 length = 0; 2932 if (inner_classes_attribute_start != nullptr) { 2933 cfs->set_current(inner_classes_attribute_start); 2934 cfs->guarantee_more(2, CHECK_0); // length 2935 length = cfs->get_u2_fast(); 2936 } 2937 2938 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing 2939 // method data: 2940 // [inner_class_info_index, 2941 // outer_class_info_index, 2942 // inner_name_index, 2943 // inner_class_access_flags, 2944 // ... 2945 // enclosing_method_class_index, 2946 // enclosing_method_method_index] 2947 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); 2948 Array<u2>* inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 2949 _inner_classes = inner_classes; 2950 2951 int index = 0; 2952 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 2953 for (int n = 0; n < length; n++) { 2954 // Inner class index 2955 const u2 inner_class_info_index = cfs->get_u2_fast(); 2956 guarantee_property( 2957 valid_klass_reference_at(inner_class_info_index), 2958 "inner_class_info_index %u has bad constant type in class file %s", 2959 inner_class_info_index, CHECK_0); 2960 // Outer class index 2961 const u2 outer_class_info_index = cfs->get_u2_fast(); 2962 guarantee_property( 2963 outer_class_info_index == 0 || 2964 valid_klass_reference_at(outer_class_info_index), 2965 "outer_class_info_index %u has bad constant type in class file %s", 2966 outer_class_info_index, CHECK_0); 2967 2968 if (outer_class_info_index != 0) { 2969 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index); 2970 char* bytes = (char*)outer_class_name->bytes(); 2971 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY, 2972 "Outer class is an array class in class file %s", CHECK_0); 2973 } 2974 // Inner class name 2975 const u2 inner_name_index = cfs->get_u2_fast(); 2976 guarantee_property( 2977 inner_name_index == 0 || valid_symbol_at(inner_name_index), 2978 "inner_name_index %u has bad constant type in class file %s", 2979 inner_name_index, CHECK_0); 2980 if (_need_verify) { 2981 guarantee_property(inner_class_info_index != outer_class_info_index, 2982 "Class is both outer and inner class in class file %s", CHECK_0); 2983 } 2984 // Access flags 2985 u2 flags; 2986 // JVM_ACC_MODULE is defined in JDK-9 and later. 2987 if (_major_version >= JAVA_9_VERSION) { 2988 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE); 2989 } else { 2990 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS; 2991 } 2992 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 2993 // Set abstract bit for old class files for backward compatibility 2994 flags |= JVM_ACC_ABSTRACT; 2995 } 2996 verify_legal_class_modifiers(flags, CHECK_0); 2997 AccessFlags inner_access_flags(flags); 2998 2999 inner_classes->at_put(index++, inner_class_info_index); 3000 inner_classes->at_put(index++, outer_class_info_index); 3001 inner_classes->at_put(index++, inner_name_index); 3002 inner_classes->at_put(index++, inner_access_flags.as_unsigned_short()); 3003 } 3004 3005 // Check for circular and duplicate entries. 3006 bool has_circularity = false; 3007 if (_need_verify) { 3008 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0); 3009 if (has_circularity) { 3010 // If circularity check failed then ignore InnerClasses attribute. 3011 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 3012 index = 0; 3013 if (parsed_enclosingmethod_attribute) { 3014 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0); 3015 _inner_classes = inner_classes; 3016 } else { 3017 _inner_classes = Universe::the_empty_short_array(); 3018 } 3019 } 3020 } 3021 // Set EnclosingMethod class and method indexes. 3022 if (parsed_enclosingmethod_attribute) { 3023 inner_classes->at_put(index++, enclosing_method_class_index); 3024 inner_classes->at_put(index++, enclosing_method_method_index); 3025 } 3026 assert(index == size || has_circularity, "wrong size"); 3027 3028 // Restore buffer's current position. 3029 cfs->set_current(current_mark); 3030 3031 return length; 3032 } 3033 3034 u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 3035 const u1* const nest_members_attribute_start, 3036 TRAPS) { 3037 const u1* const current_mark = cfs->current(); 3038 u2 length = 0; 3039 if (nest_members_attribute_start != nullptr) { 3040 cfs->set_current(nest_members_attribute_start); 3041 cfs->guarantee_more(2, CHECK_0); // length 3042 length = cfs->get_u2_fast(); 3043 } 3044 const int size = length; 3045 Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3046 _nest_members = nest_members; 3047 3048 int index = 0; 3049 cfs->guarantee_more(2 * length, CHECK_0); 3050 for (int n = 0; n < length; n++) { 3051 const u2 class_info_index = cfs->get_u2_fast(); 3052 guarantee_property( 3053 valid_klass_reference_at(class_info_index), 3054 "Nest member class_info_index %u has bad constant type in class file %s", 3055 class_info_index, CHECK_0); 3056 nest_members->at_put(index++, class_info_index); 3057 } 3058 assert(index == size, "wrong size"); 3059 3060 // Restore buffer's current position. 3061 cfs->set_current(current_mark); 3062 3063 return length; 3064 } 3065 3066 u2 ClassFileParser::parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs, 3067 const u1* const permitted_subclasses_attribute_start, 3068 TRAPS) { 3069 const u1* const current_mark = cfs->current(); 3070 u2 length = 0; 3071 if (permitted_subclasses_attribute_start != nullptr) { 3072 cfs->set_current(permitted_subclasses_attribute_start); 3073 cfs->guarantee_more(2, CHECK_0); // length 3074 length = cfs->get_u2_fast(); 3075 } 3076 const int size = length; 3077 Array<u2>* const permitted_subclasses = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3078 _permitted_subclasses = permitted_subclasses; 3079 3080 if (length > 0) { 3081 int index = 0; 3082 cfs->guarantee_more(2 * length, CHECK_0); 3083 for (int n = 0; n < length; n++) { 3084 const u2 class_info_index = cfs->get_u2_fast(); 3085 guarantee_property( 3086 valid_klass_reference_at(class_info_index), 3087 "Permitted subclass class_info_index %u has bad constant type in class file %s", 3088 class_info_index, CHECK_0); 3089 permitted_subclasses->at_put(index++, class_info_index); 3090 } 3091 assert(index == size, "wrong size"); 3092 } 3093 3094 // Restore buffer's current position. 3095 cfs->set_current(current_mark); 3096 3097 return length; 3098 } 3099 3100 // Record { 3101 // u2 attribute_name_index; 3102 // u4 attribute_length; 3103 // u2 components_count; 3104 // component_info components[components_count]; 3105 // } 3106 // component_info { 3107 // u2 name_index; 3108 // u2 descriptor_index 3109 // u2 attributes_count; 3110 // attribute_info_attributes[attributes_count]; 3111 // } 3112 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs, 3113 const ConstantPool* cp, 3114 const u1* const record_attribute_start, 3115 TRAPS) { 3116 const u1* const current_mark = cfs->current(); 3117 int components_count = 0; 3118 unsigned int calculate_attr_size = 0; 3119 if (record_attribute_start != nullptr) { 3120 cfs->set_current(record_attribute_start); 3121 cfs->guarantee_more(2, CHECK_0); // num of components 3122 components_count = (int)cfs->get_u2_fast(); 3123 calculate_attr_size = 2; 3124 } 3125 3126 Array<RecordComponent*>* const record_components = 3127 MetadataFactory::new_array<RecordComponent*>(_loader_data, components_count, nullptr, CHECK_0); 3128 _record_components = record_components; 3129 3130 for (int x = 0; x < components_count; x++) { 3131 cfs->guarantee_more(6, CHECK_0); // name_index, descriptor_index, attributes_count 3132 3133 const u2 name_index = cfs->get_u2_fast(); 3134 guarantee_property(valid_symbol_at(name_index), 3135 "Invalid constant pool index %u for name in Record attribute in class file %s", 3136 name_index, CHECK_0); 3137 const Symbol* const name = cp->symbol_at(name_index); 3138 verify_legal_field_name(name, CHECK_0); 3139 3140 const u2 descriptor_index = cfs->get_u2_fast(); 3141 guarantee_property(valid_symbol_at(descriptor_index), 3142 "Invalid constant pool index %u for descriptor in Record attribute in class file %s", 3143 descriptor_index, CHECK_0); 3144 const Symbol* const descr = cp->symbol_at(descriptor_index); 3145 verify_legal_field_signature(name, descr, CHECK_0); 3146 3147 const u2 attributes_count = cfs->get_u2_fast(); 3148 calculate_attr_size += 6; 3149 u2 generic_sig_index = 0; 3150 const u1* runtime_visible_annotations = nullptr; 3151 int runtime_visible_annotations_length = 0; 3152 bool runtime_invisible_annotations_exists = false; 3153 const u1* runtime_visible_type_annotations = nullptr; 3154 int runtime_visible_type_annotations_length = 0; 3155 bool runtime_invisible_type_annotations_exists = false; 3156 3157 // Expected attributes for record components are Signature, Runtime(In)VisibleAnnotations, 3158 // and Runtime(In)VisibleTypeAnnotations. Other attributes are ignored. 3159 for (int y = 0; y < attributes_count; y++) { 3160 cfs->guarantee_more(6, CHECK_0); // attribute_name_index, attribute_length 3161 const u2 attribute_name_index = cfs->get_u2_fast(); 3162 const u4 attribute_length = cfs->get_u4_fast(); 3163 calculate_attr_size += 6; 3164 guarantee_property( 3165 valid_symbol_at(attribute_name_index), 3166 "Invalid Record attribute name index %u in class file %s", 3167 attribute_name_index, CHECK_0); 3168 3169 const Symbol* const attribute_name = cp->symbol_at(attribute_name_index); 3170 if (attribute_name == vmSymbols::tag_signature()) { 3171 if (generic_sig_index != 0) { 3172 classfile_parse_error( 3173 "Multiple Signature attributes for Record component in class file %s", 3174 THREAD); 3175 return 0; 3176 } 3177 if (attribute_length != 2) { 3178 classfile_parse_error( 3179 "Invalid Signature attribute length %u in Record component in class file %s", 3180 attribute_length, THREAD); 3181 return 0; 3182 } 3183 generic_sig_index = parse_generic_signature_attribute(cfs, CHECK_0); 3184 3185 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 3186 if (runtime_visible_annotations != nullptr) { 3187 classfile_parse_error( 3188 "Multiple RuntimeVisibleAnnotations attributes for Record component in class file %s", THREAD); 3189 return 0; 3190 } 3191 runtime_visible_annotations_length = attribute_length; 3192 runtime_visible_annotations = cfs->current(); 3193 3194 assert(runtime_visible_annotations != nullptr, "null record component visible annotation"); 3195 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_0); 3196 cfs->skip_u1_fast(runtime_visible_annotations_length); 3197 3198 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 3199 if (runtime_invisible_annotations_exists) { 3200 classfile_parse_error( 3201 "Multiple RuntimeInvisibleAnnotations attributes for Record component in class file %s", THREAD); 3202 return 0; 3203 } 3204 runtime_invisible_annotations_exists = true; 3205 cfs->skip_u1(attribute_length, CHECK_0); 3206 3207 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 3208 if (runtime_visible_type_annotations != nullptr) { 3209 classfile_parse_error( 3210 "Multiple RuntimeVisibleTypeAnnotations attributes for Record component in class file %s", THREAD); 3211 return 0; 3212 } 3213 runtime_visible_type_annotations_length = attribute_length; 3214 runtime_visible_type_annotations = cfs->current(); 3215 3216 assert(runtime_visible_type_annotations != nullptr, "null record component visible type annotation"); 3217 cfs->guarantee_more(runtime_visible_type_annotations_length, CHECK_0); 3218 cfs->skip_u1_fast(runtime_visible_type_annotations_length); 3219 3220 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 3221 if (runtime_invisible_type_annotations_exists) { 3222 classfile_parse_error( 3223 "Multiple RuntimeInvisibleTypeAnnotations attributes for Record component in class file %s", THREAD); 3224 return 0; 3225 } 3226 runtime_invisible_type_annotations_exists = true; 3227 cfs->skip_u1(attribute_length, CHECK_0); 3228 3229 } else { 3230 // Skip unknown attributes 3231 cfs->skip_u1(attribute_length, CHECK_0); 3232 } 3233 calculate_attr_size += attribute_length; 3234 } // End of attributes For loop 3235 3236 AnnotationArray* annotations = allocate_annotations(runtime_visible_annotations, 3237 runtime_visible_annotations_length, 3238 CHECK_0); 3239 AnnotationArray* type_annotations = allocate_annotations(runtime_visible_type_annotations, 3240 runtime_visible_type_annotations_length, 3241 CHECK_0); 3242 3243 RecordComponent* record_component = 3244 RecordComponent::allocate(_loader_data, name_index, descriptor_index, generic_sig_index, 3245 annotations, type_annotations, CHECK_0); 3246 record_components->at_put(x, record_component); 3247 } // End of component processing loop 3248 3249 // Restore buffer's current position. 3250 cfs->set_current(current_mark); 3251 return calculate_attr_size; 3252 } 3253 3254 void ClassFileParser::parse_classfile_synthetic_attribute() { 3255 set_class_synthetic_flag(true); 3256 } 3257 3258 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) { 3259 assert(cfs != nullptr, "invariant"); 3260 3261 const u2 signature_index = cfs->get_u2(CHECK); 3262 guarantee_property( 3263 valid_symbol_at(signature_index), 3264 "Invalid constant pool index %u in Signature attribute in class file %s", 3265 signature_index, CHECK); 3266 set_class_generic_signature_index(signature_index); 3267 } 3268 3269 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 3270 ConstantPool* cp, 3271 u4 attribute_byte_length, 3272 TRAPS) { 3273 assert(cfs != nullptr, "invariant"); 3274 assert(cp != nullptr, "invariant"); 3275 3276 const u1* const current_start = cfs->current(); 3277 3278 guarantee_property(attribute_byte_length >= sizeof(u2), 3279 "Invalid BootstrapMethods attribute length %u in class file %s", 3280 attribute_byte_length, 3281 CHECK); 3282 3283 cfs->guarantee_more(attribute_byte_length, CHECK); 3284 3285 const int attribute_array_length = cfs->get_u2_fast(); 3286 3287 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length, 3288 "Short length on BootstrapMethods in class file %s", 3289 CHECK); 3290 3291 3292 // The attribute contains a counted array of counted tuples of shorts, 3293 // represending bootstrap specifiers: 3294 // length*{bootstrap_method_index, argument_count*{argument_index}} 3295 const unsigned int operand_count = (attribute_byte_length - (unsigned)sizeof(u2)) / (unsigned)sizeof(u2); 3296 // operand_count = number of shorts in attr, except for leading length 3297 3298 // The attribute is copied into a short[] array. 3299 // The array begins with a series of short[2] pairs, one for each tuple. 3300 const int index_size = (attribute_array_length * 2); 3301 3302 Array<u2>* const operands = 3303 MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK); 3304 3305 // Eagerly assign operands so they will be deallocated with the constant 3306 // pool if there is an error. 3307 cp->set_operands(operands); 3308 3309 int operand_fill_index = index_size; 3310 const int cp_size = cp->length(); 3311 3312 for (int n = 0; n < attribute_array_length; n++) { 3313 // Store a 32-bit offset into the header of the operand array. 3314 ConstantPool::operand_offset_at_put(operands, n, operand_fill_index); 3315 3316 // Read a bootstrap specifier. 3317 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc 3318 const u2 bootstrap_method_index = cfs->get_u2_fast(); 3319 const u2 argument_count = cfs->get_u2_fast(); 3320 guarantee_property( 3321 valid_cp_range(bootstrap_method_index, cp_size) && 3322 cp->tag_at(bootstrap_method_index).is_method_handle(), 3323 "bootstrap_method_index %u has bad constant type in class file %s", 3324 bootstrap_method_index, 3325 CHECK); 3326 3327 guarantee_property((operand_fill_index + 1 + argument_count) < operands->length(), 3328 "Invalid BootstrapMethods num_bootstrap_methods or num_bootstrap_arguments value in class file %s", 3329 CHECK); 3330 3331 operands->at_put(operand_fill_index++, bootstrap_method_index); 3332 operands->at_put(operand_fill_index++, argument_count); 3333 3334 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc] 3335 for (int j = 0; j < argument_count; j++) { 3336 const u2 argument_index = cfs->get_u2_fast(); 3337 guarantee_property( 3338 valid_cp_range(argument_index, cp_size) && 3339 cp->tag_at(argument_index).is_loadable_constant(), 3340 "argument_index %u has bad constant type in class file %s", 3341 argument_index, 3342 CHECK); 3343 operands->at_put(operand_fill_index++, argument_index); 3344 } 3345 } 3346 guarantee_property(current_start + attribute_byte_length == cfs->current(), 3347 "Bad length on BootstrapMethods in class file %s", 3348 CHECK); 3349 } 3350 3351 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs, 3352 ConstantPool* cp, 3353 ClassFileParser::ClassAnnotationCollector* parsed_annotations, 3354 TRAPS) { 3355 assert(cfs != nullptr, "invariant"); 3356 assert(cp != nullptr, "invariant"); 3357 assert(parsed_annotations != nullptr, "invariant"); 3358 3359 // Set inner classes attribute to default sentinel 3360 _inner_classes = Universe::the_empty_short_array(); 3361 // Set nest members attribute to default sentinel 3362 _nest_members = Universe::the_empty_short_array(); 3363 // Set _permitted_subclasses attribute to default sentinel 3364 _permitted_subclasses = Universe::the_empty_short_array(); 3365 cfs->guarantee_more(2, CHECK); // attributes_count 3366 u2 attributes_count = cfs->get_u2_fast(); 3367 bool parsed_sourcefile_attribute = false; 3368 bool parsed_innerclasses_attribute = false; 3369 bool parsed_nest_members_attribute = false; 3370 bool parsed_permitted_subclasses_attribute = false; 3371 bool parsed_nest_host_attribute = false; 3372 bool parsed_record_attribute = false; 3373 bool parsed_enclosingmethod_attribute = false; 3374 bool parsed_bootstrap_methods_attribute = false; 3375 const u1* runtime_visible_annotations = nullptr; 3376 int runtime_visible_annotations_length = 0; 3377 const u1* runtime_visible_type_annotations = nullptr; 3378 int runtime_visible_type_annotations_length = 0; 3379 bool runtime_invisible_type_annotations_exists = false; 3380 bool runtime_invisible_annotations_exists = false; 3381 bool parsed_source_debug_ext_annotations_exist = false; 3382 const u1* inner_classes_attribute_start = nullptr; 3383 u4 inner_classes_attribute_length = 0; 3384 u2 enclosing_method_class_index = 0; 3385 u2 enclosing_method_method_index = 0; 3386 const u1* nest_members_attribute_start = nullptr; 3387 u4 nest_members_attribute_length = 0; 3388 const u1* record_attribute_start = nullptr; 3389 u4 record_attribute_length = 0; 3390 const u1* permitted_subclasses_attribute_start = nullptr; 3391 u4 permitted_subclasses_attribute_length = 0; 3392 3393 // Iterate over attributes 3394 while (attributes_count--) { 3395 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 3396 const u2 attribute_name_index = cfs->get_u2_fast(); 3397 const u4 attribute_length = cfs->get_u4_fast(); 3398 guarantee_property( 3399 valid_symbol_at(attribute_name_index), 3400 "Attribute name has bad constant pool index %u in class file %s", 3401 attribute_name_index, CHECK); 3402 const Symbol* const tag = cp->symbol_at(attribute_name_index); 3403 if (tag == vmSymbols::tag_source_file()) { 3404 // Check for SourceFile tag 3405 if (_need_verify) { 3406 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK); 3407 } 3408 if (parsed_sourcefile_attribute) { 3409 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD); 3410 return; 3411 } else { 3412 parsed_sourcefile_attribute = true; 3413 } 3414 parse_classfile_sourcefile_attribute(cfs, CHECK); 3415 } else if (tag == vmSymbols::tag_source_debug_extension()) { 3416 // Check for SourceDebugExtension tag 3417 if (parsed_source_debug_ext_annotations_exist) { 3418 classfile_parse_error( 3419 "Multiple SourceDebugExtension attributes in class file %s", THREAD); 3420 return; 3421 } 3422 parsed_source_debug_ext_annotations_exist = true; 3423 parse_classfile_source_debug_extension_attribute(cfs, (int)attribute_length, CHECK); 3424 } else if (tag == vmSymbols::tag_inner_classes()) { 3425 // Check for InnerClasses tag 3426 if (parsed_innerclasses_attribute) { 3427 classfile_parse_error("Multiple InnerClasses attributes in class file %s", THREAD); 3428 return; 3429 } else { 3430 parsed_innerclasses_attribute = true; 3431 } 3432 inner_classes_attribute_start = cfs->current(); 3433 inner_classes_attribute_length = attribute_length; 3434 cfs->skip_u1(inner_classes_attribute_length, CHECK); 3435 } else if (tag == vmSymbols::tag_synthetic()) { 3436 // Check for Synthetic tag 3437 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec 3438 if (attribute_length != 0) { 3439 classfile_parse_error( 3440 "Invalid Synthetic classfile attribute length %u in class file %s", 3441 attribute_length, THREAD); 3442 return; 3443 } 3444 parse_classfile_synthetic_attribute(); 3445 } else if (tag == vmSymbols::tag_deprecated()) { 3446 // Check for Deprecated tag - 4276120 3447 if (attribute_length != 0) { 3448 classfile_parse_error( 3449 "Invalid Deprecated classfile attribute length %u in class file %s", 3450 attribute_length, THREAD); 3451 return; 3452 } 3453 } else if (_major_version >= JAVA_1_5_VERSION) { 3454 if (tag == vmSymbols::tag_signature()) { 3455 if (_generic_signature_index != 0) { 3456 classfile_parse_error( 3457 "Multiple Signature attributes in class file %s", THREAD); 3458 return; 3459 } 3460 if (attribute_length != 2) { 3461 classfile_parse_error( 3462 "Wrong Signature attribute length %u in class file %s", 3463 attribute_length, THREAD); 3464 return; 3465 } 3466 parse_classfile_signature_attribute(cfs, CHECK); 3467 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { 3468 if (runtime_visible_annotations != nullptr) { 3469 classfile_parse_error( 3470 "Multiple RuntimeVisibleAnnotations attributes in class file %s", THREAD); 3471 return; 3472 } 3473 runtime_visible_annotations_length = attribute_length; 3474 runtime_visible_annotations = cfs->current(); 3475 assert(runtime_visible_annotations != nullptr, "null visible annotations"); 3476 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 3477 parse_annotations(cp, 3478 runtime_visible_annotations, 3479 runtime_visible_annotations_length, 3480 parsed_annotations, 3481 _loader_data, 3482 _can_access_vm_annotations); 3483 cfs->skip_u1_fast(runtime_visible_annotations_length); 3484 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) { 3485 if (runtime_invisible_annotations_exists) { 3486 classfile_parse_error( 3487 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", THREAD); 3488 return; 3489 } 3490 runtime_invisible_annotations_exists = true; 3491 cfs->skip_u1(attribute_length, CHECK); 3492 } else if (tag == vmSymbols::tag_enclosing_method()) { 3493 if (parsed_enclosingmethod_attribute) { 3494 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", THREAD); 3495 return; 3496 } else { 3497 parsed_enclosingmethod_attribute = true; 3498 } 3499 guarantee_property(attribute_length == 4, 3500 "Wrong EnclosingMethod attribute length %u in class file %s", 3501 attribute_length, CHECK); 3502 cfs->guarantee_more(4, CHECK); // class_index, method_index 3503 enclosing_method_class_index = cfs->get_u2_fast(); 3504 enclosing_method_method_index = cfs->get_u2_fast(); 3505 if (enclosing_method_class_index == 0) { 3506 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", THREAD); 3507 return; 3508 } 3509 // Validate the constant pool indices and types 3510 guarantee_property(valid_klass_reference_at(enclosing_method_class_index), 3511 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK); 3512 if (enclosing_method_method_index != 0 && 3513 (!cp->is_within_bounds(enclosing_method_method_index) || 3514 !cp->tag_at(enclosing_method_method_index).is_name_and_type())) { 3515 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", THREAD); 3516 return; 3517 } 3518 } else if (tag == vmSymbols::tag_bootstrap_methods() && 3519 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 3520 if (parsed_bootstrap_methods_attribute) { 3521 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", THREAD); 3522 return; 3523 } 3524 parsed_bootstrap_methods_attribute = true; 3525 parse_classfile_bootstrap_methods_attribute(cfs, cp, attribute_length, CHECK); 3526 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) { 3527 if (runtime_visible_type_annotations != nullptr) { 3528 classfile_parse_error( 3529 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", THREAD); 3530 return; 3531 } 3532 runtime_visible_type_annotations_length = attribute_length; 3533 runtime_visible_type_annotations = cfs->current(); 3534 assert(runtime_visible_type_annotations != nullptr, "null visible type annotations"); 3535 // No need for the VM to parse Type annotations 3536 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 3537 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) { 3538 if (runtime_invisible_type_annotations_exists) { 3539 classfile_parse_error( 3540 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", THREAD); 3541 return; 3542 } 3543 runtime_invisible_type_annotations_exists = true; 3544 cfs->skip_u1(attribute_length, CHECK); 3545 } else if (_major_version >= JAVA_11_VERSION) { 3546 if (tag == vmSymbols::tag_nest_members()) { 3547 // Check for NestMembers tag 3548 if (parsed_nest_members_attribute) { 3549 classfile_parse_error("Multiple NestMembers attributes in class file %s", THREAD); 3550 return; 3551 } else { 3552 parsed_nest_members_attribute = true; 3553 } 3554 if (parsed_nest_host_attribute) { 3555 classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", THREAD); 3556 return; 3557 } 3558 nest_members_attribute_start = cfs->current(); 3559 nest_members_attribute_length = attribute_length; 3560 cfs->skip_u1(nest_members_attribute_length, CHECK); 3561 } else if (tag == vmSymbols::tag_nest_host()) { 3562 if (parsed_nest_host_attribute) { 3563 classfile_parse_error("Multiple NestHost attributes in class file %s", THREAD); 3564 return; 3565 } else { 3566 parsed_nest_host_attribute = true; 3567 } 3568 if (parsed_nest_members_attribute) { 3569 classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", THREAD); 3570 return; 3571 } 3572 if (_need_verify) { 3573 guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK); 3574 } 3575 cfs->guarantee_more(2, CHECK); 3576 u2 class_info_index = cfs->get_u2_fast(); 3577 guarantee_property( 3578 valid_klass_reference_at(class_info_index), 3579 "Nest-host class_info_index %u has bad constant type in class file %s", 3580 class_info_index, CHECK); 3581 _nest_host = class_info_index; 3582 3583 } else if (_major_version >= JAVA_16_VERSION) { 3584 if (tag == vmSymbols::tag_record()) { 3585 if (parsed_record_attribute) { 3586 classfile_parse_error("Multiple Record attributes in class file %s", THREAD); 3587 return; 3588 } 3589 parsed_record_attribute = true; 3590 record_attribute_start = cfs->current(); 3591 record_attribute_length = attribute_length; 3592 } else if (_major_version >= JAVA_17_VERSION) { 3593 if (tag == vmSymbols::tag_permitted_subclasses()) { 3594 if (parsed_permitted_subclasses_attribute) { 3595 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK); 3596 return; 3597 } 3598 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute. 3599 if (_access_flags.is_final()) { 3600 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK); 3601 return; 3602 } 3603 parsed_permitted_subclasses_attribute = true; 3604 permitted_subclasses_attribute_start = cfs->current(); 3605 permitted_subclasses_attribute_length = attribute_length; 3606 } 3607 } 3608 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION 3609 cfs->skip_u1(attribute_length, CHECK); 3610 } else { 3611 // Unknown attribute 3612 cfs->skip_u1(attribute_length, CHECK); 3613 } 3614 } else { 3615 // Unknown attribute 3616 cfs->skip_u1(attribute_length, CHECK); 3617 } 3618 } else { 3619 // Unknown attribute 3620 cfs->skip_u1(attribute_length, CHECK); 3621 } 3622 } 3623 _class_annotations = allocate_annotations(runtime_visible_annotations, 3624 runtime_visible_annotations_length, 3625 CHECK); 3626 _class_type_annotations = allocate_annotations(runtime_visible_type_annotations, 3627 runtime_visible_type_annotations_length, 3628 CHECK); 3629 3630 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 3631 const u2 num_of_classes = parse_classfile_inner_classes_attribute( 3632 cfs, 3633 cp, 3634 inner_classes_attribute_start, 3635 parsed_innerclasses_attribute, 3636 enclosing_method_class_index, 3637 enclosing_method_method_index, 3638 CHECK); 3639 if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) { 3640 guarantee_property( 3641 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 3642 "Wrong InnerClasses attribute length in class file %s", CHECK); 3643 } 3644 } 3645 3646 if (parsed_nest_members_attribute) { 3647 const u2 num_of_classes = parse_classfile_nest_members_attribute( 3648 cfs, 3649 nest_members_attribute_start, 3650 CHECK); 3651 if (_need_verify) { 3652 guarantee_property( 3653 nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes, 3654 "Wrong NestMembers attribute length in class file %s", CHECK); 3655 } 3656 } 3657 3658 if (parsed_record_attribute) { 3659 const unsigned int calculated_attr_length = parse_classfile_record_attribute( 3660 cfs, 3661 cp, 3662 record_attribute_start, 3663 CHECK); 3664 if (_need_verify) { 3665 guarantee_property(record_attribute_length == calculated_attr_length, 3666 "Record attribute has wrong length in class file %s", 3667 CHECK); 3668 } 3669 } 3670 3671 if (parsed_permitted_subclasses_attribute) { 3672 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute( 3673 cfs, 3674 permitted_subclasses_attribute_start, 3675 CHECK); 3676 if (_need_verify) { 3677 guarantee_property( 3678 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses, 3679 "Wrong PermittedSubclasses attribute length in class file %s", CHECK); 3680 } 3681 } 3682 3683 if (_max_bootstrap_specifier_index >= 0) { 3684 guarantee_property(parsed_bootstrap_methods_attribute, 3685 "Missing BootstrapMethods attribute in class file %s", CHECK); 3686 } 3687 } 3688 3689 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) { 3690 assert(k != nullptr, "invariant"); 3691 3692 if (_synthetic_flag) 3693 k->set_is_synthetic(); 3694 if (_sourcefile_index != 0) { 3695 k->set_source_file_name_index(_sourcefile_index); 3696 } 3697 if (_generic_signature_index != 0) { 3698 k->set_generic_signature_index(_generic_signature_index); 3699 } 3700 if (_sde_buffer != nullptr) { 3701 k->set_source_debug_extension(_sde_buffer, _sde_length); 3702 } 3703 } 3704 3705 // Create the Annotations object that will 3706 // hold the annotations array for the Klass. 3707 void ClassFileParser::create_combined_annotations(TRAPS) { 3708 if (_class_annotations == nullptr && 3709 _class_type_annotations == nullptr && 3710 _fields_annotations == nullptr && 3711 _fields_type_annotations == nullptr) { 3712 // Don't create the Annotations object unnecessarily. 3713 return; 3714 } 3715 3716 Annotations* const annotations = Annotations::allocate(_loader_data, CHECK); 3717 annotations->set_class_annotations(_class_annotations); 3718 annotations->set_class_type_annotations(_class_type_annotations); 3719 annotations->set_fields_annotations(_fields_annotations); 3720 annotations->set_fields_type_annotations(_fields_type_annotations); 3721 3722 // This is the Annotations object that will be 3723 // assigned to InstanceKlass being constructed. 3724 _combined_annotations = annotations; 3725 3726 // The annotations arrays below has been transferred the 3727 // _combined_annotations so these fields can now be cleared. 3728 _class_annotations = nullptr; 3729 _class_type_annotations = nullptr; 3730 _fields_annotations = nullptr; 3731 _fields_type_annotations = nullptr; 3732 } 3733 3734 // Transfer ownership of metadata allocated to the InstanceKlass. 3735 void ClassFileParser::apply_parsed_class_metadata( 3736 InstanceKlass* this_klass, 3737 int java_fields_count) { 3738 assert(this_klass != nullptr, "invariant"); 3739 3740 _cp->set_pool_holder(this_klass); 3741 this_klass->set_constants(_cp); 3742 this_klass->set_fieldinfo_stream(_fieldinfo_stream); 3743 this_klass->set_fieldinfo_search_table(_fieldinfo_search_table); 3744 this_klass->set_fields_status(_fields_status); 3745 this_klass->set_methods(_methods); 3746 this_klass->set_inner_classes(_inner_classes); 3747 this_klass->set_nest_members(_nest_members); 3748 this_klass->set_nest_host_index(_nest_host); 3749 this_klass->set_annotations(_combined_annotations); 3750 this_klass->set_permitted_subclasses(_permitted_subclasses); 3751 this_klass->set_record_components(_record_components); 3752 3753 DEBUG_ONLY(FieldInfoStream::validate_search_table(_cp, _fieldinfo_stream, _fieldinfo_search_table)); 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 static void check_methods_for_intrinsics(const InstanceKlass* ik, 4900 const Array<Method*>* methods) { 4901 assert(ik != nullptr, "invariant"); 4902 assert(methods != nullptr, "invariant"); 4903 4904 // Set up Method*::intrinsic_id as soon as we know the names of methods. 4905 // (We used to do this lazily, but now we query it in Rewriter, 4906 // which is eagerly done for every method, so we might as well do it now, 4907 // when everything is fresh in memory.) 4908 const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik); 4909 4910 if (klass_id != vmSymbolID::NO_SID) { 4911 for (int j = 0; j < methods->length(); ++j) { 4912 Method* method = methods->at(j); 4913 method->init_intrinsic_id(klass_id); 4914 4915 if (CheckIntrinsics) { 4916 // Check if an intrinsic is defined for method 'method', 4917 // but the method is not annotated with @IntrinsicCandidate. 4918 if (method->intrinsic_id() != vmIntrinsics::_none && 4919 !method->intrinsic_candidate()) { 4920 tty->print("Compiler intrinsic is defined for method [%s], " 4921 "but the method is not annotated with @IntrinsicCandidate.%s", 4922 method->name_and_sig_as_C_string(), 4923 NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.") 4924 ); 4925 tty->cr(); 4926 DEBUG_ONLY(vm_exit(1)); 4927 } 4928 // Check is the method 'method' is annotated with @IntrinsicCandidate, 4929 // but there is no intrinsic available for it. 4930 if (method->intrinsic_candidate() && 4931 method->intrinsic_id() == vmIntrinsics::_none) { 4932 tty->print("Method [%s] is annotated with @IntrinsicCandidate, " 4933 "but no compiler intrinsic is defined for the method.%s", 4934 method->name_and_sig_as_C_string(), 4935 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 4936 ); 4937 tty->cr(); 4938 DEBUG_ONLY(vm_exit(1)); 4939 } 4940 } 4941 } // end for 4942 4943 #ifdef ASSERT 4944 if (CheckIntrinsics) { 4945 // Check for orphan methods in the current class. A method m 4946 // of a class C is orphan if an intrinsic is defined for method m, 4947 // but class C does not declare m. 4948 // The check is potentially expensive, therefore it is available 4949 // only in debug builds. 4950 4951 for (auto id : EnumRange<vmIntrinsicID>{}) { 4952 if (vmIntrinsics::_compiledLambdaForm == id) { 4953 // The _compiledLamdbdaForm intrinsic is a special marker for bytecode 4954 // generated for the JVM from a LambdaForm and therefore no method 4955 // is defined for it. 4956 continue; 4957 } 4958 if (vmIntrinsics::_blackhole == id) { 4959 // The _blackhole intrinsic is a special marker. No explicit method 4960 // is defined for it. 4961 continue; 4962 } 4963 4964 if (vmIntrinsics::class_for(id) == klass_id) { 4965 // Check if the current class contains a method with the same 4966 // name, flags, signature. 4967 bool match = false; 4968 for (int j = 0; j < methods->length(); ++j) { 4969 const Method* method = methods->at(j); 4970 if (method->intrinsic_id() == id) { 4971 match = true; 4972 break; 4973 } 4974 } 4975 4976 if (!match) { 4977 char buf[1000]; 4978 tty->print("Compiler intrinsic is defined for method [%s], " 4979 "but the method is not available in class [%s].%s", 4980 vmIntrinsics::short_name_as_C_string(id, buf, sizeof(buf)), 4981 ik->name()->as_C_string(), 4982 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 4983 ); 4984 tty->cr(); 4985 DEBUG_ONLY(vm_exit(1)); 4986 } 4987 } 4988 } // end for 4989 } // CheckIntrinsics 4990 #endif // ASSERT 4991 } 4992 } 4993 4994 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, 4995 const ClassInstanceInfo& cl_inst_info, 4996 TRAPS) { 4997 if (_klass != nullptr) { 4998 return _klass; 4999 } 5000 5001 InstanceKlass* const ik = 5002 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL); 5003 5004 if (is_hidden()) { 5005 mangle_hidden_class_name(ik); 5006 } 5007 5008 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL); 5009 5010 assert(_klass == ik, "invariant"); 5011 5012 return ik; 5013 } 5014 5015 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, 5016 bool changed_by_loadhook, 5017 const ClassInstanceInfo& cl_inst_info, 5018 TRAPS) { 5019 assert(ik != nullptr, "invariant"); 5020 5021 // Set name and CLD before adding to CLD 5022 ik->set_class_loader_data(_loader_data); 5023 ik->set_class_loader_type(); 5024 ik->set_name(_class_name); 5025 5026 // Add all classes to our internal class loader list here, 5027 // including classes in the bootstrap (null) class loader. 5028 const bool publicize = !is_internal(); 5029 5030 _loader_data->add_class(ik, publicize); 5031 5032 set_klass_to_deallocate(ik); 5033 5034 assert(_field_info != nullptr, "invariant"); 5035 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity"); 5036 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count, 5037 "sanity"); 5038 5039 assert(ik->is_instance_klass(), "sanity"); 5040 assert(ik->size_helper() == _field_info->_instance_size, "sanity"); 5041 5042 // Fill in information already parsed 5043 ik->set_should_verify_class(_need_verify); 5044 5045 // Not yet: supers are done below to support the new subtype-checking fields 5046 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size); 5047 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields); 5048 ik->set_static_oop_field_count(_static_oop_count); 5049 5050 // this transfers ownership of a lot of arrays from 5051 // the parser onto the InstanceKlass* 5052 apply_parsed_class_metadata(ik, _java_fields_count); 5053 5054 // can only set dynamic nest-host after static nest information is set 5055 if (cl_inst_info.dynamic_nest_host() != nullptr) { 5056 ik->set_nest_host(cl_inst_info.dynamic_nest_host()); 5057 } 5058 5059 // note that is not safe to use the fields in the parser from this point on 5060 assert(nullptr == _cp, "invariant"); 5061 assert(nullptr == _fieldinfo_stream, "invariant"); 5062 assert(nullptr == _fieldinfo_search_table, "invariant"); 5063 assert(nullptr == _fields_status, "invariant"); 5064 assert(nullptr == _methods, "invariant"); 5065 assert(nullptr == _inner_classes, "invariant"); 5066 assert(nullptr == _nest_members, "invariant"); 5067 assert(nullptr == _combined_annotations, "invariant"); 5068 assert(nullptr == _record_components, "invariant"); 5069 assert(nullptr == _permitted_subclasses, "invariant"); 5070 5071 if (_has_localvariable_table) { 5072 ik->set_has_localvariable_table(true); 5073 } 5074 5075 if (_has_final_method) { 5076 ik->set_has_final_method(); 5077 } 5078 5079 ik->copy_method_ordering(_method_ordering, CHECK); 5080 // The InstanceKlass::_methods_jmethod_ids cache 5081 // is managed on the assumption that the initial cache 5082 // size is equal to the number of methods in the class. If 5083 // that changes, then InstanceKlass::idnum_can_increment() 5084 // has to be changed accordingly. 5085 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length())); 5086 5087 ik->set_this_class_index(_this_class_index); 5088 5089 if (_is_hidden) { 5090 // _this_class_index is a CONSTANT_Class entry that refers to this 5091 // hidden class itself. If this class needs to refer to its own methods 5092 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference 5093 // _this_class_index. However, because this class is hidden (it's 5094 // not stored in SystemDictionary), _this_class_index cannot be resolved 5095 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup. 5096 // Therefore, we must eagerly resolve _this_class_index now. 5097 ik->constants()->klass_at_put(_this_class_index, ik); 5098 } 5099 5100 ik->set_minor_version(_minor_version); 5101 ik->set_major_version(_major_version); 5102 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods); 5103 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods); 5104 5105 assert(!_is_hidden || ik->is_hidden(), "must be set already"); 5106 5107 // Set PackageEntry for this_klass 5108 oop cl = ik->class_loader(); 5109 Handle clh = Handle(THREAD, cl); 5110 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh()); 5111 ik->set_package(cld, nullptr, CHECK); 5112 5113 const Array<Method*>* const methods = ik->methods(); 5114 assert(methods != nullptr, "invariant"); 5115 const int methods_len = methods->length(); 5116 5117 check_methods_for_intrinsics(ik, methods); 5118 5119 // Fill in field values obtained by parse_classfile_attributes 5120 if (_parsed_annotations->has_any_annotations()) { 5121 _parsed_annotations->apply_to(ik); 5122 } 5123 5124 apply_parsed_class_attributes(ik); 5125 5126 // Miranda methods 5127 if ((_num_miranda_methods > 0) || 5128 // if this class introduced new miranda methods or 5129 (_super_klass != nullptr && _super_klass->has_miranda_methods()) 5130 // super class exists and this class inherited miranda methods 5131 ) { 5132 ik->set_has_miranda_methods(); // then set a flag 5133 } 5134 5135 // Fill in information needed to compute superclasses. 5136 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK); 5137 ik->set_transitive_interfaces(_transitive_interfaces); 5138 ik->set_local_interfaces(_local_interfaces); 5139 _transitive_interfaces = nullptr; 5140 _local_interfaces = nullptr; 5141 5142 // Initialize itable offset tables 5143 klassItable::setup_itable_offset_table(ik); 5144 5145 // Compute transitive closure of interfaces this class implements 5146 // Do final class setup 5147 OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks; 5148 if (oop_map_blocks->_nonstatic_oop_map_count > 0) { 5149 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps()); 5150 } 5151 5152 if (_has_contended_fields || _parsed_annotations->is_contended() || 5153 ( _super_klass != nullptr && _super_klass->has_contended_annotations())) { 5154 ik->set_has_contended_annotations(true); 5155 } 5156 5157 // Fill in has_finalizer and layout_helper 5158 set_precomputed_flags(ik); 5159 5160 // check if this class can access its super class 5161 check_super_class_access(ik, CHECK); 5162 5163 // check if this class can access its superinterfaces 5164 check_super_interface_access(ik, CHECK); 5165 5166 // check if this class overrides any final method 5167 check_final_method_override(ik, CHECK); 5168 5169 // reject static interface methods prior to Java 8 5170 if (ik->is_interface() && _major_version < JAVA_8_VERSION) { 5171 check_illegal_static_method(ik, CHECK); 5172 } 5173 5174 // Obtain this_klass' module entry 5175 ModuleEntry* module_entry = ik->module(); 5176 assert(module_entry != nullptr, "module_entry should always be set"); 5177 5178 // Obtain java.lang.Module 5179 Handle module_handle(THREAD, module_entry->module_oop()); 5180 5181 // Allocate mirror and initialize static fields 5182 java_lang_Class::create_mirror(ik, 5183 Handle(THREAD, _loader_data->class_loader()), 5184 module_handle, 5185 _protection_domain, 5186 cl_inst_info.class_data(), 5187 CHECK); 5188 5189 assert(_all_mirandas != nullptr, "invariant"); 5190 5191 // Generate any default methods - default methods are public interface methods 5192 // that have a default implementation. This is new with Java 8. 5193 if (_has_nonstatic_concrete_methods) { 5194 DefaultMethods::generate_default_methods(ik, 5195 _all_mirandas, 5196 CHECK); 5197 } 5198 5199 // Add read edges to the unnamed modules of the bootstrap and app class loaders. 5200 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() && 5201 !module_entry->has_default_read_edges()) { 5202 if (!module_entry->set_has_default_read_edges()) { 5203 // We won a potential race 5204 JvmtiExport::add_default_read_edges(module_handle, THREAD); 5205 } 5206 } 5207 5208 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */); 5209 5210 if (!is_internal()) { 5211 ik->print_class_load_logging(_loader_data, module_entry, _stream); 5212 5213 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION && 5214 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION && 5215 log_is_enabled(Info, class, preview)) { 5216 ResourceMark rm; 5217 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)", 5218 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION); 5219 } 5220 5221 if (log_is_enabled(Debug, class, resolve)) { 5222 ResourceMark rm; 5223 // print out the superclass. 5224 const char * from = ik->external_name(); 5225 if (ik->java_super() != nullptr) { 5226 log_debug(class, resolve)("%s %s (super)", 5227 from, 5228 ik->java_super()->external_name()); 5229 } 5230 // print out each of the interface classes referred to by this class. 5231 const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces(); 5232 if (local_interfaces != nullptr) { 5233 const int length = local_interfaces->length(); 5234 for (int i = 0; i < length; i++) { 5235 const InstanceKlass* const k = local_interfaces->at(i); 5236 const char * to = k->external_name(); 5237 log_debug(class, resolve)("%s %s (interface)", from, to); 5238 } 5239 } 5240 } 5241 } 5242 5243 JFR_ONLY(INIT_ID(ik);) 5244 5245 // If we reach here, all is well. 5246 // Now remove the InstanceKlass* from the _klass_to_deallocate field 5247 // in order for it to not be destroyed in the ClassFileParser destructor. 5248 set_klass_to_deallocate(nullptr); 5249 5250 // it's official 5251 set_klass(ik); 5252 5253 DEBUG_ONLY(ik->verify();) 5254 } 5255 5256 void ClassFileParser::update_class_name(Symbol* new_class_name) { 5257 // Decrement the refcount in the old name, since we're clobbering it. 5258 _class_name->decrement_refcount(); 5259 5260 _class_name = new_class_name; 5261 // Increment the refcount of the new name. 5262 // Now the ClassFileParser owns this name and will decrement in 5263 // the destructor. 5264 _class_name->increment_refcount(); 5265 } 5266 5267 ClassFileParser::ClassFileParser(ClassFileStream* stream, 5268 Symbol* name, 5269 ClassLoaderData* loader_data, 5270 const ClassLoadInfo* cl_info, 5271 Publicity pub_level, 5272 TRAPS) : 5273 _stream(stream), 5274 _class_name(nullptr), 5275 _loader_data(loader_data), 5276 _is_hidden(cl_info->is_hidden()), 5277 _can_access_vm_annotations(cl_info->can_access_vm_annotations()), 5278 _orig_cp_size(0), 5279 _static_oop_count(0), 5280 _super_klass(), 5281 _cp(nullptr), 5282 _fieldinfo_stream(nullptr), 5283 _fieldinfo_search_table(nullptr), 5284 _fields_status(nullptr), 5285 _methods(nullptr), 5286 _inner_classes(nullptr), 5287 _nest_members(nullptr), 5288 _nest_host(0), 5289 _permitted_subclasses(nullptr), 5290 _record_components(nullptr), 5291 _local_interfaces(nullptr), 5292 _transitive_interfaces(nullptr), 5293 _combined_annotations(nullptr), 5294 _class_annotations(nullptr), 5295 _class_type_annotations(nullptr), 5296 _fields_annotations(nullptr), 5297 _fields_type_annotations(nullptr), 5298 _klass(nullptr), 5299 _klass_to_deallocate(nullptr), 5300 _parsed_annotations(nullptr), 5301 _field_info(nullptr), 5302 _temp_field_info(nullptr), 5303 _method_ordering(nullptr), 5304 _all_mirandas(nullptr), 5305 _vtable_size(0), 5306 _itable_size(0), 5307 _num_miranda_methods(0), 5308 _protection_domain(cl_info->protection_domain()), 5309 _access_flags(), 5310 _pub_level(pub_level), 5311 _bad_constant_seen(0), 5312 _synthetic_flag(false), 5313 _sde_length(false), 5314 _sde_buffer(nullptr), 5315 _sourcefile_index(0), 5316 _generic_signature_index(0), 5317 _major_version(0), 5318 _minor_version(0), 5319 _this_class_index(0), 5320 _super_class_index(0), 5321 _itfs_len(0), 5322 _java_fields_count(0), 5323 _need_verify(false), 5324 _has_nonstatic_concrete_methods(false), 5325 _declares_nonstatic_concrete_methods(false), 5326 _has_localvariable_table(false), 5327 _has_final_method(false), 5328 _has_contended_fields(false), 5329 _has_finalizer(false), 5330 _has_empty_finalizer(false), 5331 _max_bootstrap_specifier_index(-1) { 5332 5333 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name(); 5334 _class_name->increment_refcount(); 5335 5336 assert(_loader_data != nullptr, "invariant"); 5337 assert(stream != nullptr, "invariant"); 5338 assert(_stream != nullptr, "invariant"); 5339 assert(_stream->buffer() == _stream->current(), "invariant"); 5340 assert(_class_name != nullptr, "invariant"); 5341 assert(0 == _access_flags.as_unsigned_short(), "invariant"); 5342 5343 // Figure out whether we can skip format checking (matching classic VM behavior) 5344 // Always verify CFLH bytes from the user agents. 5345 _need_verify = stream->from_class_file_load_hook() ? true : 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 _fieldinfo_search_table = nullptr; 5361 _fields_status = nullptr; 5362 _methods = nullptr; 5363 _inner_classes = nullptr; 5364 _nest_members = nullptr; 5365 _permitted_subclasses = nullptr; 5366 _combined_annotations = nullptr; 5367 _class_annotations = _class_type_annotations = nullptr; 5368 _fields_annotations = _fields_type_annotations = nullptr; 5369 _record_components = nullptr; 5370 } 5371 5372 // Destructor to clean up 5373 ClassFileParser::~ClassFileParser() { 5374 _class_name->decrement_refcount(); 5375 5376 if (_cp != nullptr) { 5377 MetadataFactory::free_metadata(_loader_data, _cp); 5378 } 5379 5380 if (_fieldinfo_stream != nullptr) { 5381 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream); 5382 } 5383 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_search_table); 5384 5385 if (_fields_status != nullptr) { 5386 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status); 5387 } 5388 5389 if (_methods != nullptr) { 5390 // Free methods 5391 InstanceKlass::deallocate_methods(_loader_data, _methods); 5392 } 5393 5394 // beware of the Universe::empty_blah_array!! 5395 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) { 5396 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 5397 } 5398 5399 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) { 5400 MetadataFactory::free_array<u2>(_loader_data, _nest_members); 5401 } 5402 5403 if (_record_components != nullptr) { 5404 InstanceKlass::deallocate_record_components(_loader_data, _record_components); 5405 } 5406 5407 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) { 5408 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses); 5409 } 5410 5411 // Free interfaces 5412 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass, 5413 _local_interfaces, _transitive_interfaces); 5414 5415 if (_combined_annotations != nullptr) { 5416 // After all annotations arrays have been created, they are installed into the 5417 // Annotations object that will be assigned to the InstanceKlass being created. 5418 5419 // Deallocate the Annotations object and the installed annotations arrays. 5420 _combined_annotations->deallocate_contents(_loader_data); 5421 5422 // If the _combined_annotations pointer is non-null, 5423 // then the other annotations fields should have been cleared. 5424 assert(_class_annotations == nullptr, "Should have been cleared"); 5425 assert(_class_type_annotations == nullptr, "Should have been cleared"); 5426 assert(_fields_annotations == nullptr, "Should have been cleared"); 5427 assert(_fields_type_annotations == nullptr, "Should have been cleared"); 5428 } else { 5429 // If the annotations arrays were not installed into the Annotations object, 5430 // then they have to be deallocated explicitly. 5431 MetadataFactory::free_array<u1>(_loader_data, _class_annotations); 5432 MetadataFactory::free_array<u1>(_loader_data, _class_type_annotations); 5433 Annotations::free_contents(_loader_data, _fields_annotations); 5434 Annotations::free_contents(_loader_data, _fields_type_annotations); 5435 } 5436 5437 clear_class_metadata(); 5438 _transitive_interfaces = nullptr; 5439 _local_interfaces = nullptr; 5440 5441 // deallocate the klass if already created. Don't directly deallocate, but add 5442 // to the deallocate list so that the klass is removed from the CLD::_klasses list 5443 // at a safepoint. 5444 if (_klass_to_deallocate != nullptr) { 5445 _loader_data->add_to_deallocate_list(_klass_to_deallocate); 5446 } 5447 } 5448 5449 void ClassFileParser::parse_stream(const ClassFileStream* const stream, 5450 TRAPS) { 5451 5452 assert(stream != nullptr, "invariant"); 5453 assert(_class_name != nullptr, "invariant"); 5454 5455 // BEGIN STREAM PARSING 5456 stream->guarantee_more(8, CHECK); // magic, major, minor 5457 // Magic value 5458 const u4 magic = stream->get_u4_fast(); 5459 guarantee_property(magic == JAVA_CLASSFILE_MAGIC, 5460 "Incompatible magic value %u in class file %s", 5461 magic, CHECK); 5462 5463 // Version numbers 5464 _minor_version = stream->get_u2_fast(); 5465 _major_version = stream->get_u2_fast(); 5466 5467 // Check version numbers - we check this even with verifier off 5468 verify_class_version(_major_version, _minor_version, _class_name, CHECK); 5469 5470 stream->guarantee_more(3, CHECK); // length, first cp tag 5471 u2 cp_size = stream->get_u2_fast(); 5472 5473 guarantee_property( 5474 cp_size >= 1, "Illegal constant pool size %u in class file %s", 5475 cp_size, CHECK); 5476 5477 _orig_cp_size = cp_size; 5478 if (is_hidden()) { // Add a slot for hidden class name. 5479 cp_size++; 5480 } 5481 5482 _cp = ConstantPool::allocate(_loader_data, 5483 cp_size, 5484 CHECK); 5485 5486 ConstantPool* const cp = _cp; 5487 5488 parse_constant_pool(stream, cp, _orig_cp_size, CHECK); 5489 5490 assert(cp_size == (u2)cp->length(), "invariant"); 5491 5492 // ACCESS FLAGS 5493 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 5494 5495 // Access flags 5496 u2 flags; 5497 // JVM_ACC_MODULE is defined in JDK-9 and later. 5498 if (_major_version >= JAVA_9_VERSION) { 5499 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE); 5500 } else { 5501 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS; 5502 } 5503 5504 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 5505 // Set abstract bit for old class files for backward compatibility 5506 flags |= JVM_ACC_ABSTRACT; 5507 } 5508 5509 verify_legal_class_modifiers(flags, CHECK); 5510 5511 short bad_constant = class_bad_constant_seen(); 5512 if (bad_constant != 0) { 5513 // Do not throw CFE until after the access_flags are checked because if 5514 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE. 5515 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD); 5516 return; 5517 } 5518 5519 _access_flags.set_flags(flags); 5520 5521 // This class and superclass 5522 _this_class_index = stream->get_u2_fast(); 5523 guarantee_property( 5524 valid_cp_range(_this_class_index, cp_size) && 5525 cp->tag_at(_this_class_index).is_unresolved_klass(), 5526 "Invalid this class index %u in constant pool in class file %s", 5527 _this_class_index, CHECK); 5528 5529 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index); 5530 assert(class_name_in_cp != nullptr, "class_name can't be null"); 5531 5532 // Don't need to check whether this class name is legal or not. 5533 // It has been checked when constant pool is parsed. 5534 // However, make sure it is not an array type. 5535 if (_need_verify) { 5536 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY, 5537 "Bad class name in class file %s", 5538 CHECK); 5539 } 5540 5541 #ifdef ASSERT 5542 // Basic sanity checks 5543 if (_is_hidden) { 5544 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name"); 5545 } 5546 #endif 5547 5548 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class. 5549 5550 if (_is_hidden) { 5551 assert(_class_name != nullptr, "Unexpected null _class_name"); 5552 #ifdef ASSERT 5553 if (_need_verify) { 5554 verify_legal_class_name(_class_name, CHECK); 5555 } 5556 #endif 5557 5558 } else { 5559 // Check if name in class file matches given name 5560 if (_class_name != class_name_in_cp) { 5561 if (_class_name != vmSymbols::unknown_class_name()) { 5562 ResourceMark rm(THREAD); 5563 // Names are all known to be < 64k so we know this formatted message is not excessively large. 5564 Exceptions::fthrow(THREAD_AND_LOCATION, 5565 vmSymbols::java_lang_NoClassDefFoundError(), 5566 "%s (wrong name: %s)", 5567 _class_name->as_C_string(), 5568 class_name_in_cp->as_C_string() 5569 ); 5570 return; 5571 } else { 5572 // The class name was not known by the caller so we set it from 5573 // the value in the CP. 5574 update_class_name(class_name_in_cp); 5575 } 5576 // else nothing to do: the expected class name matches what is in the CP 5577 } 5578 } 5579 5580 // Verification prevents us from creating names with dots in them, this 5581 // asserts that that's the case. 5582 assert(is_internal_format(_class_name), "external class name format used internally"); 5583 5584 if (!is_internal()) { 5585 LogTarget(Debug, class, preorder) lt; 5586 if (lt.is_enabled()){ 5587 ResourceMark rm(THREAD); 5588 LogStream ls(lt); 5589 ls.print("%s", _class_name->as_klass_external_name()); 5590 if (stream->source() != nullptr) { 5591 ls.print(" source: %s", stream->source()); 5592 } 5593 ls.cr(); 5594 } 5595 } 5596 5597 // SUPERKLASS 5598 _super_class_index = stream->get_u2_fast(); 5599 _super_klass = parse_super_class(cp, 5600 _super_class_index, 5601 _need_verify, 5602 CHECK); 5603 5604 // Interfaces 5605 _itfs_len = stream->get_u2_fast(); 5606 parse_interfaces(stream, 5607 _itfs_len, 5608 cp, 5609 &_has_nonstatic_concrete_methods, 5610 CHECK); 5611 5612 assert(_local_interfaces != nullptr, "invariant"); 5613 5614 // Fields (offsets are filled in later) 5615 parse_fields(stream, 5616 _access_flags.is_interface(), 5617 cp, 5618 cp_size, 5619 &_java_fields_count, 5620 CHECK); 5621 5622 assert(_temp_field_info != nullptr, "invariant"); 5623 5624 // Methods 5625 parse_methods(stream, 5626 _access_flags.is_interface(), 5627 &_has_localvariable_table, 5628 &_has_final_method, 5629 &_declares_nonstatic_concrete_methods, 5630 CHECK); 5631 5632 assert(_methods != nullptr, "invariant"); 5633 5634 if (_declares_nonstatic_concrete_methods) { 5635 _has_nonstatic_concrete_methods = true; 5636 } 5637 5638 // Additional attributes/annotations 5639 _parsed_annotations = new ClassAnnotationCollector(); 5640 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK); 5641 5642 assert(_inner_classes != nullptr, "invariant"); 5643 5644 // Finalize the Annotations metadata object, 5645 // now that all annotation arrays have been created. 5646 create_combined_annotations(CHECK); 5647 5648 // Make sure this is the end of class file stream 5649 guarantee_property(stream->at_eos(), 5650 "Extra bytes at the end of class file %s", 5651 CHECK); 5652 5653 // all bytes in stream read and parsed 5654 } 5655 5656 void ClassFileParser::mangle_hidden_class_name(InstanceKlass* const ik) { 5657 ResourceMark rm; 5658 // Construct hidden name from _class_name, "+", and &ik. Note that we can't 5659 // use a '/' because that confuses finding the class's package. Also, can't 5660 // use an illegal char such as ';' because that causes serialization issues 5661 // and issues with hidden classes that create their own hidden classes. 5662 char addr_buf[20]; 5663 if (CDSConfig::is_dumping_static_archive()) { 5664 // We want stable names for the archived hidden classes (only for static 5665 // archive for now). Spaces under default_SharedBaseAddress() will be 5666 // occupied by the archive at run time, so we know that no dynamically 5667 // loaded InstanceKlass will be placed under there. 5668 static volatile size_t counter = 0; 5669 Atomic::cmpxchg(&counter, (size_t)0, Arguments::default_SharedBaseAddress()); // initialize it 5670 size_t new_id = Atomic::add(&counter, (size_t)1); 5671 jio_snprintf(addr_buf, 20, "0x%zx", new_id); 5672 } else { 5673 jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(ik)); 5674 } 5675 size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf); 5676 char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len); 5677 jio_snprintf(new_name, new_name_len, "%s+%s", 5678 _class_name->as_C_string(), addr_buf); 5679 update_class_name(SymbolTable::new_symbol(new_name)); 5680 5681 // Add a Utf8 entry containing the hidden name. 5682 assert(_class_name != nullptr, "Unexpected null _class_name"); 5683 int hidden_index = _orig_cp_size; // this is an extra slot we added 5684 _cp->symbol_at_put(hidden_index, _class_name); 5685 5686 // Update this_class_index's slot in the constant pool with the new Utf8 entry. 5687 // We have to update the resolved_klass_index and the name_index together 5688 // so extract the existing resolved_klass_index first. 5689 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index); 5690 int resolved_klass_index = cp_klass_slot.resolved_klass_index(); 5691 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index); 5692 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size, 5693 "Bad name_index"); 5694 } 5695 5696 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream, 5697 ConstantPool* cp, 5698 TRAPS) { 5699 assert(stream != nullptr, "invariant"); 5700 assert(stream->at_eos(), "invariant"); 5701 assert(cp != nullptr, "invariant"); 5702 assert(_loader_data != nullptr, "invariant"); 5703 5704 if (_class_name == vmSymbols::java_lang_Object()) { 5705 guarantee_property(_local_interfaces == Universe::the_empty_instance_klass_array(), 5706 "java.lang.Object cannot implement an interface in class file %s", 5707 CHECK); 5708 } 5709 // We check super class after class file is parsed and format is checked 5710 if (_super_class_index > 0 && nullptr == _super_klass) { 5711 Symbol* const super_class_name = cp->klass_name_at(_super_class_index); 5712 if (_access_flags.is_interface()) { 5713 // Before attempting to resolve the superclass, check for class format 5714 // errors not checked yet. 5715 guarantee_property(super_class_name == vmSymbols::java_lang_Object(), 5716 "Interfaces must have java.lang.Object as superclass in class file %s", 5717 CHECK); 5718 } 5719 Handle loader(THREAD, _loader_data->class_loader()); 5720 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) { 5721 _super_klass = vmClasses::Object_klass(); 5722 } else { 5723 _super_klass = (const InstanceKlass*) 5724 SystemDictionary::resolve_super_or_fail(_class_name, 5725 super_class_name, 5726 loader, 5727 true, 5728 CHECK); 5729 } 5730 } 5731 5732 if (_super_klass != nullptr) { 5733 if (_super_klass->has_nonstatic_concrete_methods()) { 5734 _has_nonstatic_concrete_methods = true; 5735 } 5736 5737 if (_super_klass->is_interface()) { 5738 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD); 5739 return; 5740 } 5741 } 5742 5743 // Compute the transitive list of all unique interfaces implemented by this class 5744 _transitive_interfaces = 5745 compute_transitive_interfaces(_super_klass, 5746 _local_interfaces, 5747 _loader_data, 5748 CHECK); 5749 5750 assert(_transitive_interfaces != nullptr, "invariant"); 5751 5752 // sort methods 5753 _method_ordering = sort_methods(_methods); 5754 5755 _all_mirandas = new GrowableArray<Method*>(20); 5756 5757 Handle loader(THREAD, _loader_data->class_loader()); 5758 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size, 5759 &_num_miranda_methods, 5760 _all_mirandas, 5761 _super_klass, 5762 _methods, 5763 _access_flags, 5764 _major_version, 5765 loader, 5766 _class_name, 5767 _local_interfaces); 5768 5769 // Size of Java itable (in words) 5770 _itable_size = _access_flags.is_interface() ? 0 : 5771 klassItable::compute_itable_size(_transitive_interfaces); 5772 5773 assert(_parsed_annotations != nullptr, "invariant"); 5774 5775 _field_info = new FieldLayoutInfo(); 5776 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info, 5777 _parsed_annotations->is_contended(), _field_info); 5778 lb.build_layout(); 5779 5780 int injected_fields_count = _temp_field_info->length() - _java_fields_count; 5781 _fieldinfo_stream = 5782 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count, 5783 injected_fields_count, loader_data(), CHECK); 5784 _fieldinfo_search_table = FieldInfoStream::create_search_table(_cp, _fieldinfo_stream, _loader_data, CHECK); 5785 _fields_status = 5786 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(), 5787 FieldStatus(0), CHECK); 5788 } 5789 5790 void ClassFileParser::set_klass(InstanceKlass* klass) { 5791 5792 #ifdef ASSERT 5793 if (klass != nullptr) { 5794 assert(nullptr == _klass, "leaking?"); 5795 } 5796 #endif 5797 5798 _klass = klass; 5799 } 5800 5801 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) { 5802 5803 #ifdef ASSERT 5804 if (klass != nullptr) { 5805 assert(nullptr == _klass_to_deallocate, "leaking?"); 5806 } 5807 #endif 5808 5809 _klass_to_deallocate = klass; 5810 } 5811 5812 // Caller responsible for ResourceMark 5813 // clone stream with rewound position 5814 const ClassFileStream* ClassFileParser::clone_stream() const { 5815 assert(_stream != nullptr, "invariant"); 5816 5817 return _stream->clone(); 5818 } 5819 5820 ReferenceType ClassFileParser::super_reference_type() const { 5821 return _super_klass == nullptr ? REF_NONE : _super_klass->reference_type(); 5822 } 5823 5824 bool ClassFileParser::is_instance_ref_klass() const { 5825 // Only the subclasses of j.l.r.Reference are InstanceRefKlass. 5826 // j.l.r.Reference itself is InstanceKlass because InstanceRefKlass denotes a 5827 // klass requiring special treatment in ref-processing. The abstract 5828 // j.l.r.Reference cannot be instantiated so doesn't partake in 5829 // ref-processing. 5830 return is_java_lang_ref_Reference_subclass(); 5831 } 5832 5833 bool ClassFileParser::is_java_lang_ref_Reference_subclass() const { 5834 if (_super_klass == nullptr) { 5835 return false; 5836 } 5837 5838 if (_super_klass->name() == vmSymbols::java_lang_ref_Reference()) { 5839 // Direct subclass of j.l.r.Reference: Soft|Weak|Final|Phantom 5840 return true; 5841 } 5842 5843 return _super_klass->reference_type() != REF_NONE; 5844 } 5845 5846 // Returns true if the future Klass will need to be addressable with a narrow Klass ID. 5847 bool ClassFileParser::klass_needs_narrow_id() const { 5848 // Classes that are never instantiated need no narrow Klass Id, since the 5849 // only point of having a narrow id is to put it into an object header. Keeping 5850 // never instantiated classes out of class space lessens the class space pressure. 5851 // For more details, see JDK-8338526. 5852 return !is_interface() && !is_abstract(); 5853 } 5854 5855 // ---------------------------------------------------------------------------- 5856 // debugging 5857 5858 #ifdef ASSERT 5859 5860 // return true if class_name contains no '.' (internal format is '/') 5861 bool ClassFileParser::is_internal_format(Symbol* class_name) { 5862 if (class_name != nullptr) { 5863 ResourceMark rm; 5864 char* name = class_name->as_C_string(); 5865 return strchr(name, JVM_SIGNATURE_DOT) == nullptr; 5866 } else { 5867 return true; 5868 } 5869 } 5870 5871 #endif