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