Path: blob/master/src/hotspot/share/classfile/classFileParser.cpp
64440 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "jvm.h"25#include "classfile/classFileParser.hpp"26#include "classfile/classFileStream.hpp"27#include "classfile/classLoader.hpp"28#include "classfile/classLoaderData.inline.hpp"29#include "classfile/classLoadInfo.hpp"30#include "classfile/defaultMethods.hpp"31#include "classfile/fieldLayoutBuilder.hpp"32#include "classfile/javaClasses.inline.hpp"33#include "classfile/moduleEntry.hpp"34#include "classfile/packageEntry.hpp"35#include "classfile/symbolTable.hpp"36#include "classfile/systemDictionary.hpp"37#include "classfile/verificationType.hpp"38#include "classfile/verifier.hpp"39#include "classfile/vmClasses.hpp"40#include "classfile/vmSymbols.hpp"41#include "logging/log.hpp"42#include "logging/logStream.hpp"43#include "memory/allocation.hpp"44#include "memory/metadataFactory.hpp"45#include "memory/oopFactory.hpp"46#include "memory/resourceArea.hpp"47#include "memory/universe.hpp"48#include "oops/annotations.hpp"49#include "oops/constantPool.inline.hpp"50#include "oops/fieldStreams.inline.hpp"51#include "oops/instanceKlass.inline.hpp"52#include "oops/instanceMirrorKlass.hpp"53#include "oops/klass.inline.hpp"54#include "oops/klassVtable.hpp"55#include "oops/metadata.hpp"56#include "oops/method.inline.hpp"57#include "oops/oop.inline.hpp"58#include "oops/recordComponent.hpp"59#include "oops/symbol.hpp"60#include "prims/jvmtiExport.hpp"61#include "prims/jvmtiThreadState.hpp"62#include "runtime/arguments.hpp"63#include "runtime/fieldDescriptor.inline.hpp"64#include "runtime/handles.inline.hpp"65#include "runtime/javaCalls.hpp"66#include "runtime/os.hpp"67#include "runtime/perfData.hpp"68#include "runtime/reflection.hpp"69#include "runtime/safepointVerifiers.hpp"70#include "runtime/signature.hpp"71#include "runtime/timer.hpp"72#include "services/classLoadingService.hpp"73#include "services/threadService.hpp"74#include "utilities/align.hpp"75#include "utilities/bitMap.inline.hpp"76#include "utilities/copy.hpp"77#include "utilities/formatBuffer.hpp"78#include "utilities/exceptions.hpp"79#include "utilities/globalDefinitions.hpp"80#include "utilities/growableArray.hpp"81#include "utilities/macros.hpp"82#include "utilities/ostream.hpp"83#include "utilities/resourceHash.hpp"84#include "utilities/utf8.hpp"8586#if INCLUDE_CDS87#include "classfile/systemDictionaryShared.hpp"88#endif89#if INCLUDE_JFR90#include "jfr/support/jfrTraceIdExtension.hpp"91#endif9293// We generally try to create the oops directly when parsing, rather than94// allocating temporary data structures and copying the bytes twice. A95// temporary area is only needed when parsing utf8 entries in the constant96// pool and when parsing line number tables.9798// We add assert in debug mode when class format is not checked.99100#define JAVA_CLASSFILE_MAGIC 0xCAFEBABE101#define JAVA_MIN_SUPPORTED_VERSION 45102#define JAVA_PREVIEW_MINOR_VERSION 65535103104// Used for two backward compatibility reasons:105// - to check for new additions to the class file format in JDK1.5106// - to check for bug fixes in the format checker in JDK1.5107#define JAVA_1_5_VERSION 49108109// Used for backward compatibility reasons:110// - to check for javac bug fixes that happened after 1.5111// - also used as the max version when running in jdk6112#define JAVA_6_VERSION 50113114// Used for backward compatibility reasons:115// - to disallow argument and require ACC_STATIC for <clinit> methods116#define JAVA_7_VERSION 51117118// Extension method support.119#define JAVA_8_VERSION 52120121#define JAVA_9_VERSION 53122123#define JAVA_10_VERSION 54124125#define JAVA_11_VERSION 55126127#define JAVA_12_VERSION 56128129#define JAVA_13_VERSION 57130131#define JAVA_14_VERSION 58132133#define JAVA_15_VERSION 59134135#define JAVA_16_VERSION 60136137#define JAVA_17_VERSION 61138139void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {140assert((bad_constant == JVM_CONSTANT_Module ||141bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,142"Unexpected bad constant pool entry");143if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;144}145146void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,147ConstantPool* cp,148const int length,149TRAPS) {150assert(stream != NULL, "invariant");151assert(cp != NULL, "invariant");152153// Use a local copy of ClassFileStream. It helps the C++ compiler to optimize154// this function (_current can be allocated in a register, with scalar155// replacement of aggregates). The _current pointer is copied back to156// stream() when this function returns. DON'T call another method within157// this method that uses stream().158const ClassFileStream cfs1 = *stream;159const ClassFileStream* const cfs = &cfs1;160161assert(cfs->allocated_on_stack(), "should be local");162debug_only(const u1* const old_current = stream->current();)163164// Used for batching symbol allocations.165const char* names[SymbolTable::symbol_alloc_batch_size];166int lengths[SymbolTable::symbol_alloc_batch_size];167int indices[SymbolTable::symbol_alloc_batch_size];168unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];169int names_count = 0;170171// parsing Index 0 is unused172for (int index = 1; index < length; index++) {173// Each of the following case guarantees one more byte in the stream174// for the following tag or the access_flags following constant pool,175// so we don't need bounds-check for reading tag.176const u1 tag = cfs->get_u1_fast();177switch (tag) {178case JVM_CONSTANT_Class : {179cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags180const u2 name_index = cfs->get_u2_fast();181cp->klass_index_at_put(index, name_index);182break;183}184case JVM_CONSTANT_Fieldref: {185cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags186const u2 class_index = cfs->get_u2_fast();187const u2 name_and_type_index = cfs->get_u2_fast();188cp->field_at_put(index, class_index, name_and_type_index);189break;190}191case JVM_CONSTANT_Methodref: {192cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags193const u2 class_index = cfs->get_u2_fast();194const u2 name_and_type_index = cfs->get_u2_fast();195cp->method_at_put(index, class_index, name_and_type_index);196break;197}198case JVM_CONSTANT_InterfaceMethodref: {199cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags200const u2 class_index = cfs->get_u2_fast();201const u2 name_and_type_index = cfs->get_u2_fast();202cp->interface_method_at_put(index, class_index, name_and_type_index);203break;204}205case JVM_CONSTANT_String : {206cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags207const u2 string_index = cfs->get_u2_fast();208cp->string_index_at_put(index, string_index);209break;210}211case JVM_CONSTANT_MethodHandle :212case JVM_CONSTANT_MethodType: {213if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {214classfile_parse_error(215"Class file version does not support constant tag %u in class file %s",216tag, THREAD);217return;218}219if (tag == JVM_CONSTANT_MethodHandle) {220cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags221const u1 ref_kind = cfs->get_u1_fast();222const u2 method_index = cfs->get_u2_fast();223cp->method_handle_index_at_put(index, ref_kind, method_index);224}225else if (tag == JVM_CONSTANT_MethodType) {226cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags227const u2 signature_index = cfs->get_u2_fast();228cp->method_type_index_at_put(index, signature_index);229}230else {231ShouldNotReachHere();232}233break;234}235case JVM_CONSTANT_Dynamic : {236if (_major_version < Verifier::DYNAMICCONSTANT_MAJOR_VERSION) {237classfile_parse_error(238"Class file version does not support constant tag %u in class file %s",239tag, THREAD);240return;241}242cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags243const u2 bootstrap_specifier_index = cfs->get_u2_fast();244const u2 name_and_type_index = cfs->get_u2_fast();245if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) {246_max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later247}248cp->dynamic_constant_at_put(index, bootstrap_specifier_index, name_and_type_index);249break;250}251case JVM_CONSTANT_InvokeDynamic : {252if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {253classfile_parse_error(254"Class file version does not support constant tag %u in class file %s",255tag, THREAD);256return;257}258cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags259const u2 bootstrap_specifier_index = cfs->get_u2_fast();260const u2 name_and_type_index = cfs->get_u2_fast();261if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) {262_max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later263}264cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index);265break;266}267case JVM_CONSTANT_Integer: {268cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags269const u4 bytes = cfs->get_u4_fast();270cp->int_at_put(index, (jint)bytes);271break;272}273case JVM_CONSTANT_Float: {274cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags275const u4 bytes = cfs->get_u4_fast();276cp->float_at_put(index, *(jfloat*)&bytes);277break;278}279case JVM_CONSTANT_Long: {280// A mangled type might cause you to overrun allocated memory281guarantee_property(index + 1 < length,282"Invalid constant pool entry %u in class file %s",283index,284CHECK);285cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags286const u8 bytes = cfs->get_u8_fast();287cp->long_at_put(index, bytes);288index++; // Skip entry following eigth-byte constant, see JVM book p. 98289break;290}291case JVM_CONSTANT_Double: {292// A mangled type might cause you to overrun allocated memory293guarantee_property(index+1 < length,294"Invalid constant pool entry %u in class file %s",295index,296CHECK);297cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags298const u8 bytes = cfs->get_u8_fast();299cp->double_at_put(index, *(jdouble*)&bytes);300index++; // Skip entry following eigth-byte constant, see JVM book p. 98301break;302}303case JVM_CONSTANT_NameAndType: {304cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags305const u2 name_index = cfs->get_u2_fast();306const u2 signature_index = cfs->get_u2_fast();307cp->name_and_type_at_put(index, name_index, signature_index);308break;309}310case JVM_CONSTANT_Utf8 : {311cfs->guarantee_more(2, CHECK); // utf8_length312u2 utf8_length = cfs->get_u2_fast();313const u1* utf8_buffer = cfs->current();314assert(utf8_buffer != NULL, "null utf8 buffer");315// Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.316cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags317cfs->skip_u1_fast(utf8_length);318319// Before storing the symbol, make sure it's legal320if (_need_verify) {321verify_legal_utf8(utf8_buffer, utf8_length, CHECK);322}323324unsigned int hash;325Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer,326utf8_length,327hash);328if (result == NULL) {329names[names_count] = (const char*)utf8_buffer;330lengths[names_count] = utf8_length;331indices[names_count] = index;332hashValues[names_count++] = hash;333if (names_count == SymbolTable::symbol_alloc_batch_size) {334SymbolTable::new_symbols(_loader_data,335constantPoolHandle(THREAD, cp),336names_count,337names,338lengths,339indices,340hashValues);341names_count = 0;342}343} else {344cp->symbol_at_put(index, result);345}346break;347}348case JVM_CONSTANT_Module:349case JVM_CONSTANT_Package: {350// Record that an error occurred in these two cases but keep parsing so351// that ACC_Module can be checked for in the access_flags. Need to352// throw NoClassDefFoundError in that case.353if (_major_version >= JAVA_9_VERSION) {354cfs->guarantee_more(3, CHECK);355cfs->get_u2_fast();356set_class_bad_constant_seen(tag);357break;358}359}360default: {361classfile_parse_error("Unknown constant tag %u in class file %s",362tag,363THREAD);364return;365}366} // end of switch(tag)367} // end of for368369// Allocate the remaining symbols370if (names_count > 0) {371SymbolTable::new_symbols(_loader_data,372constantPoolHandle(THREAD, cp),373names_count,374names,375lengths,376indices,377hashValues);378}379380// Copy _current pointer of local copy back to stream.381assert(stream->current() == old_current, "non-exclusive use of stream");382stream->set_current(cfs1.current());383384}385386static inline bool valid_cp_range(int index, int length) {387return (index > 0 && index < length);388}389390static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) {391assert(cp != NULL, "invariant");392if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) {393return cp->symbol_at(index);394}395return NULL;396}397398#ifdef ASSERT399PRAGMA_DIAG_PUSH400PRAGMA_FORMAT_NONLITERAL_IGNORED401void ClassFileParser::report_assert_property_failure(const char* msg, TRAPS) const {402ResourceMark rm(THREAD);403fatal(msg, _class_name->as_C_string());404}405406void ClassFileParser::report_assert_property_failure(const char* msg,407int index,408TRAPS) const {409ResourceMark rm(THREAD);410fatal(msg, index, _class_name->as_C_string());411}412PRAGMA_DIAG_POP413#endif414415void ClassFileParser::parse_constant_pool(const ClassFileStream* const stream,416ConstantPool* const cp,417const int length,418TRAPS) {419assert(cp != NULL, "invariant");420assert(stream != NULL, "invariant");421422// parsing constant pool entries423parse_constant_pool_entries(stream, cp, length, CHECK);424if (class_bad_constant_seen() != 0) {425// a bad CP entry has been detected previously so stop parsing and just return.426return;427}428429int index = 1; // declared outside of loops for portability430int num_klasses = 0;431432// first verification pass - validate cross references433// and fixup class and string constants434for (index = 1; index < length; index++) { // Index 0 is unused435const jbyte tag = cp->tag_at(index).value();436switch (tag) {437case JVM_CONSTANT_Class: {438ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present439break;440}441case JVM_CONSTANT_Fieldref:442// fall through443case JVM_CONSTANT_Methodref:444// fall through445case JVM_CONSTANT_InterfaceMethodref: {446if (!_need_verify) break;447const int klass_ref_index = cp->klass_ref_index_at(index);448const int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);449check_property(valid_klass_reference_at(klass_ref_index),450"Invalid constant pool index %u in class file %s",451klass_ref_index, CHECK);452check_property(valid_cp_range(name_and_type_ref_index, length) &&453cp->tag_at(name_and_type_ref_index).is_name_and_type(),454"Invalid constant pool index %u in class file %s",455name_and_type_ref_index, CHECK);456break;457}458case JVM_CONSTANT_String: {459ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present460break;461}462case JVM_CONSTANT_Integer:463break;464case JVM_CONSTANT_Float:465break;466case JVM_CONSTANT_Long:467case JVM_CONSTANT_Double: {468index++;469check_property(470(index < length && cp->tag_at(index).is_invalid()),471"Improper constant pool long/double index %u in class file %s",472index, CHECK);473break;474}475case JVM_CONSTANT_NameAndType: {476if (!_need_verify) break;477const int name_ref_index = cp->name_ref_index_at(index);478const int signature_ref_index = cp->signature_ref_index_at(index);479check_property(valid_symbol_at(name_ref_index),480"Invalid constant pool index %u in class file %s",481name_ref_index, CHECK);482check_property(valid_symbol_at(signature_ref_index),483"Invalid constant pool index %u in class file %s",484signature_ref_index, CHECK);485break;486}487case JVM_CONSTANT_Utf8:488break;489case JVM_CONSTANT_UnresolvedClass: // fall-through490case JVM_CONSTANT_UnresolvedClassInError: {491ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present492break;493}494case JVM_CONSTANT_ClassIndex: {495const int class_index = cp->klass_index_at(index);496check_property(valid_symbol_at(class_index),497"Invalid constant pool index %u in class file %s",498class_index, CHECK);499cp->unresolved_klass_at_put(index, class_index, num_klasses++);500break;501}502case JVM_CONSTANT_StringIndex: {503const int string_index = cp->string_index_at(index);504check_property(valid_symbol_at(string_index),505"Invalid constant pool index %u in class file %s",506string_index, CHECK);507Symbol* const sym = cp->symbol_at(string_index);508cp->unresolved_string_at_put(index, sym);509break;510}511case JVM_CONSTANT_MethodHandle: {512const int ref_index = cp->method_handle_index_at(index);513check_property(valid_cp_range(ref_index, length),514"Invalid constant pool index %u in class file %s",515ref_index, CHECK);516const constantTag tag = cp->tag_at(ref_index);517const int ref_kind = cp->method_handle_ref_kind_at(index);518519switch (ref_kind) {520case JVM_REF_getField:521case JVM_REF_getStatic:522case JVM_REF_putField:523case JVM_REF_putStatic: {524check_property(525tag.is_field(),526"Invalid constant pool index %u in class file %s (not a field)",527ref_index, CHECK);528break;529}530case JVM_REF_invokeVirtual:531case JVM_REF_newInvokeSpecial: {532check_property(533tag.is_method(),534"Invalid constant pool index %u in class file %s (not a method)",535ref_index, CHECK);536break;537}538case JVM_REF_invokeStatic:539case JVM_REF_invokeSpecial: {540check_property(541tag.is_method() ||542((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()),543"Invalid constant pool index %u in class file %s (not a method)",544ref_index, CHECK);545break;546}547case JVM_REF_invokeInterface: {548check_property(549tag.is_interface_method(),550"Invalid constant pool index %u in class file %s (not an interface method)",551ref_index, CHECK);552break;553}554default: {555classfile_parse_error(556"Bad method handle kind at constant pool index %u in class file %s",557index, THREAD);558return;559}560} // switch(refkind)561// Keep the ref_index unchanged. It will be indirected at link-time.562break;563} // case MethodHandle564case JVM_CONSTANT_MethodType: {565const int ref_index = cp->method_type_index_at(index);566check_property(valid_symbol_at(ref_index),567"Invalid constant pool index %u in class file %s",568ref_index, CHECK);569break;570}571case JVM_CONSTANT_Dynamic: {572const int name_and_type_ref_index =573cp->bootstrap_name_and_type_ref_index_at(index);574575check_property(valid_cp_range(name_and_type_ref_index, length) &&576cp->tag_at(name_and_type_ref_index).is_name_and_type(),577"Invalid constant pool index %u in class file %s",578name_and_type_ref_index, CHECK);579// bootstrap specifier index must be checked later,580// when BootstrapMethods attr is available581582// Mark the constant pool as having a CONSTANT_Dynamic_info structure583cp->set_has_dynamic_constant();584break;585}586case JVM_CONSTANT_InvokeDynamic: {587const int name_and_type_ref_index =588cp->bootstrap_name_and_type_ref_index_at(index);589590check_property(valid_cp_range(name_and_type_ref_index, length) &&591cp->tag_at(name_and_type_ref_index).is_name_and_type(),592"Invalid constant pool index %u in class file %s",593name_and_type_ref_index, CHECK);594// bootstrap specifier index must be checked later,595// when BootstrapMethods attr is available596break;597}598default: {599fatal("bad constant pool tag value %u", cp->tag_at(index).value());600ShouldNotReachHere();601break;602}603} // switch(tag)604} // end of for605606cp->allocate_resolved_klasses(_loader_data, num_klasses, CHECK);607608if (!_need_verify) {609return;610}611612// second verification pass - checks the strings are of the right format.613// but not yet to the other entries614for (index = 1; index < length; index++) {615const jbyte tag = cp->tag_at(index).value();616switch (tag) {617case JVM_CONSTANT_UnresolvedClass: {618const Symbol* const class_name = cp->klass_name_at(index);619// check the name620verify_legal_class_name(class_name, CHECK);621break;622}623case JVM_CONSTANT_NameAndType: {624if (_need_verify) {625const int sig_index = cp->signature_ref_index_at(index);626const int name_index = cp->name_ref_index_at(index);627const Symbol* const name = cp->symbol_at(name_index);628const Symbol* const sig = cp->symbol_at(sig_index);629guarantee_property(sig->utf8_length() != 0,630"Illegal zero length constant pool entry at %d in class %s",631sig_index, CHECK);632guarantee_property(name->utf8_length() != 0,633"Illegal zero length constant pool entry at %d in class %s",634name_index, CHECK);635636if (Signature::is_method(sig)) {637// Format check method name and signature638verify_legal_method_name(name, CHECK);639verify_legal_method_signature(name, sig, CHECK);640} else {641// Format check field name and signature642verify_legal_field_name(name, CHECK);643verify_legal_field_signature(name, sig, CHECK);644}645}646break;647}648case JVM_CONSTANT_Dynamic: {649const int name_and_type_ref_index =650cp->name_and_type_ref_index_at(index);651// already verified to be utf8652const int name_ref_index =653cp->name_ref_index_at(name_and_type_ref_index);654// already verified to be utf8655const int signature_ref_index =656cp->signature_ref_index_at(name_and_type_ref_index);657const Symbol* const name = cp->symbol_at(name_ref_index);658const Symbol* const signature = cp->symbol_at(signature_ref_index);659if (_need_verify) {660// CONSTANT_Dynamic's name and signature are verified above, when iterating NameAndType_info.661// Need only to be sure signature is the right type.662if (Signature::is_method(signature)) {663throwIllegalSignature("CONSTANT_Dynamic", name, signature, CHECK);664}665}666break;667}668case JVM_CONSTANT_InvokeDynamic:669case JVM_CONSTANT_Fieldref:670case JVM_CONSTANT_Methodref:671case JVM_CONSTANT_InterfaceMethodref: {672const int name_and_type_ref_index =673cp->name_and_type_ref_index_at(index);674// already verified to be utf8675const int name_ref_index =676cp->name_ref_index_at(name_and_type_ref_index);677// already verified to be utf8678const int signature_ref_index =679cp->signature_ref_index_at(name_and_type_ref_index);680const Symbol* const name = cp->symbol_at(name_ref_index);681const Symbol* const signature = cp->symbol_at(signature_ref_index);682if (tag == JVM_CONSTANT_Fieldref) {683if (_need_verify) {684// Field name and signature are verified above, when iterating NameAndType_info.685// Need only to be sure signature is non-zero length and the right type.686if (Signature::is_method(signature)) {687throwIllegalSignature("Field", name, signature, CHECK);688}689}690} else {691if (_need_verify) {692// Method name and signature are verified above, when iterating NameAndType_info.693// Need only to be sure signature is non-zero length and the right type.694if (!Signature::is_method(signature)) {695throwIllegalSignature("Method", name, signature, CHECK);696}697}698// 4509014: If a class method name begins with '<', it must be "<init>"699const unsigned int name_len = name->utf8_length();700if (tag == JVM_CONSTANT_Methodref &&701name_len != 0 &&702name->char_at(0) == JVM_SIGNATURE_SPECIAL &&703name != vmSymbols::object_initializer_name()) {704classfile_parse_error(705"Bad method name at constant pool index %u in class file %s",706name_ref_index, THREAD);707return;708}709}710break;711}712case JVM_CONSTANT_MethodHandle: {713const int ref_index = cp->method_handle_index_at(index);714const int ref_kind = cp->method_handle_ref_kind_at(index);715switch (ref_kind) {716case JVM_REF_invokeVirtual:717case JVM_REF_invokeStatic:718case JVM_REF_invokeSpecial:719case JVM_REF_newInvokeSpecial: {720const int name_and_type_ref_index =721cp->name_and_type_ref_index_at(ref_index);722const int name_ref_index =723cp->name_ref_index_at(name_and_type_ref_index);724const Symbol* const name = cp->symbol_at(name_ref_index);725if (ref_kind == JVM_REF_newInvokeSpecial) {726if (name != vmSymbols::object_initializer_name()) {727classfile_parse_error(728"Bad constructor name at constant pool index %u in class file %s",729name_ref_index, THREAD);730return;731}732} else {733if (name == vmSymbols::object_initializer_name()) {734classfile_parse_error(735"Bad method name at constant pool index %u in class file %s",736name_ref_index, THREAD);737return;738}739}740break;741}742// Other ref_kinds are already fully checked in previous pass.743} // switch(ref_kind)744break;745}746case JVM_CONSTANT_MethodType: {747const Symbol* const no_name = vmSymbols::type_name(); // place holder748const Symbol* const signature = cp->method_type_signature_at(index);749verify_legal_method_signature(no_name, signature, CHECK);750break;751}752case JVM_CONSTANT_Utf8: {753assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");754}755} // switch(tag)756} // end of for757}758759class NameSigHash: public ResourceObj {760public:761const Symbol* _name; // name762const Symbol* _sig; // signature763NameSigHash* _next; // Next entry in hash table764};765766static const int HASH_ROW_SIZE = 256;767768static unsigned int hash(const Symbol* name, const Symbol* sig) {769unsigned int raw_hash = 0;770raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2);771raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize;772773return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;774}775776777static void initialize_hashtable(NameSigHash** table) {778memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE);779}780// Return false if the name/sig combination is found in table.781// Return true if no duplicate is found. And name/sig is added as a new entry in table.782// The old format checker uses heap sort to find duplicates.783// NOTE: caller should guarantee that GC doesn't happen during the life cycle784// of table since we don't expect Symbol*'s to move.785static bool put_after_lookup(const Symbol* name, const Symbol* sig, NameSigHash** table) {786assert(name != NULL, "name in constant pool is NULL");787788// First lookup for duplicates789int index = hash(name, sig);790NameSigHash* entry = table[index];791while (entry != NULL) {792if (entry->_name == name && entry->_sig == sig) {793return false;794}795entry = entry->_next;796}797798// No duplicate is found, allocate a new entry and fill it.799entry = new NameSigHash();800entry->_name = name;801entry->_sig = sig;802803// Insert into hash table804entry->_next = table[index];805table[index] = entry;806807return true;808}809810// Side-effects: populates the _local_interfaces field811void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,812const int itfs_len,813ConstantPool* const cp,814bool* const has_nonstatic_concrete_methods,815TRAPS) {816assert(stream != NULL, "invariant");817assert(cp != NULL, "invariant");818assert(has_nonstatic_concrete_methods != NULL, "invariant");819820if (itfs_len == 0) {821_local_interfaces = Universe::the_empty_instance_klass_array();822} else {823assert(itfs_len > 0, "only called for len>0");824_local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK);825826int index;827for (index = 0; index < itfs_len; index++) {828const u2 interface_index = stream->get_u2(CHECK);829Klass* interf;830check_property(831valid_klass_reference_at(interface_index),832"Interface name has bad constant pool index %u in class file %s",833interface_index, CHECK);834if (cp->tag_at(interface_index).is_klass()) {835interf = cp->resolved_klass_at(interface_index);836} else {837Symbol* const unresolved_klass = cp->klass_name_at(interface_index);838839// Don't need to check legal name because it's checked when parsing constant pool.840// But need to make sure it's not an array type.841guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,842"Bad interface name in class file %s", CHECK);843844// Call resolve_super so class circularity is checked845interf = SystemDictionary::resolve_super_or_fail(846_class_name,847unresolved_klass,848Handle(THREAD, _loader_data->class_loader()),849_protection_domain,850false,851CHECK);852}853854if (!interf->is_interface()) {855THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),856err_msg("class %s can not implement %s, because it is not an interface (%s)",857_class_name->as_klass_external_name(),858interf->external_name(),859interf->class_in_module_of_loader()));860}861862if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {863*has_nonstatic_concrete_methods = true;864}865_local_interfaces->at_put(index, InstanceKlass::cast(interf));866}867868if (!_need_verify || itfs_len <= 1) {869return;870}871872// Check if there's any duplicates in interfaces873ResourceMark rm(THREAD);874NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,875NameSigHash*,876HASH_ROW_SIZE);877initialize_hashtable(interface_names);878bool dup = false;879const Symbol* name = NULL;880{881debug_only(NoSafepointVerifier nsv;)882for (index = 0; index < itfs_len; index++) {883const InstanceKlass* const k = _local_interfaces->at(index);884name = k->name();885// If no duplicates, add (name, NULL) in hashtable interface_names.886if (!put_after_lookup(name, NULL, interface_names)) {887dup = true;888break;889}890}891}892if (dup) {893classfile_parse_error("Duplicate interface name \"%s\" in class file %s",894name->as_C_string(), THREAD);895}896}897}898899void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,900int constantvalue_index,901int signature_index,902TRAPS) const {903// Make sure the constant pool entry is of a type appropriate to this field904guarantee_property(905(constantvalue_index > 0 &&906constantvalue_index < cp->length()),907"Bad initial value index %u in ConstantValue attribute in class file %s",908constantvalue_index, CHECK);909910const constantTag value_type = cp->tag_at(constantvalue_index);911switch(cp->basic_type_for_signature_at(signature_index)) {912case T_LONG: {913guarantee_property(value_type.is_long(),914"Inconsistent constant value type in class file %s",915CHECK);916break;917}918case T_FLOAT: {919guarantee_property(value_type.is_float(),920"Inconsistent constant value type in class file %s",921CHECK);922break;923}924case T_DOUBLE: {925guarantee_property(value_type.is_double(),926"Inconsistent constant value type in class file %s",927CHECK);928break;929}930case T_BYTE:931case T_CHAR:932case T_SHORT:933case T_BOOLEAN:934case T_INT: {935guarantee_property(value_type.is_int(),936"Inconsistent constant value type in class file %s",937CHECK);938break;939}940case T_OBJECT: {941guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")942&& value_type.is_string()),943"Bad string initial value in class file %s",944CHECK);945break;946}947default: {948classfile_parse_error("Unable to set initial value %u in class file %s",949constantvalue_index,950THREAD);951}952}953}954955class AnnotationCollector : public ResourceObj{956public:957enum Location { _in_field, _in_method, _in_class };958enum ID {959_unknown = 0,960_method_CallerSensitive,961_method_ForceInline,962_method_DontInline,963_method_InjectedProfile,964_method_LambdaForm_Compiled,965_method_Hidden,966_method_Scoped,967_method_IntrinsicCandidate,968_jdk_internal_vm_annotation_Contended,969_field_Stable,970_jdk_internal_vm_annotation_ReservedStackAccess,971_jdk_internal_ValueBased,972_annotation_LIMIT973};974const Location _location;975int _annotations_present;976u2 _contended_group;977978AnnotationCollector(Location location)979: _location(location), _annotations_present(0), _contended_group(0)980{981assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");982}983// If this annotation name has an ID, report it (or _none).984ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);985// Set the annotation name:986void set_annotation(ID id) {987assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");988_annotations_present |= nth_bit((int)id);989}990991void remove_annotation(ID id) {992assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");993_annotations_present &= ~nth_bit((int)id);994}995996// Report if the annotation is present.997bool has_any_annotations() const { return _annotations_present != 0; }998bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }9991000void set_contended_group(u2 group) { _contended_group = group; }1001u2 contended_group() const { return _contended_group; }10021003bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }10041005void set_stable(bool stable) { set_annotation(_field_Stable); }1006bool is_stable() const { return has_annotation(_field_Stable); }1007};10081009// This class also doubles as a holder for metadata cleanup.1010class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector {1011private:1012ClassLoaderData* _loader_data;1013AnnotationArray* _field_annotations;1014AnnotationArray* _field_type_annotations;1015public:1016FieldAnnotationCollector(ClassLoaderData* loader_data) :1017AnnotationCollector(_in_field),1018_loader_data(loader_data),1019_field_annotations(NULL),1020_field_type_annotations(NULL) {}1021~FieldAnnotationCollector();1022void apply_to(FieldInfo* f);1023AnnotationArray* field_annotations() { return _field_annotations; }1024AnnotationArray* field_type_annotations() { return _field_type_annotations; }10251026void set_field_annotations(AnnotationArray* a) { _field_annotations = a; }1027void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }1028};10291030class MethodAnnotationCollector : public AnnotationCollector{1031public:1032MethodAnnotationCollector() : AnnotationCollector(_in_method) { }1033void apply_to(const methodHandle& m);1034};10351036class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{1037public:1038ClassAnnotationCollector() : AnnotationCollector(_in_class) { }1039void apply_to(InstanceKlass* ik);1040};104110421043static int skip_annotation_value(const u1*, int, int); // fwd decl10441045// Safely increment index by val if does not pass limit1046#define SAFE_ADD(index, limit, val) \1047if (index >= limit - val) return limit; \1048index += val;10491050// Skip an annotation. Return >=limit if there is any problem.1051static int skip_annotation(const u1* buffer, int limit, int index) {1052assert(buffer != NULL, "invariant");1053// annotation := atype:u2 do(nmem:u2) {member:u2 value}1054// value := switch (tag:u1) { ... }1055SAFE_ADD(index, limit, 4); // skip atype and read nmem1056int nmem = Bytes::get_Java_u2((address)buffer + index - 2);1057while (--nmem >= 0 && index < limit) {1058SAFE_ADD(index, limit, 2); // skip member1059index = skip_annotation_value(buffer, limit, index);1060}1061return index;1062}10631064// Skip an annotation value. Return >=limit if there is any problem.1065static int skip_annotation_value(const u1* buffer, int limit, int index) {1066assert(buffer != NULL, "invariant");10671068// value := switch (tag:u1) {1069// case B, C, I, S, Z, D, F, J, c: con:u2;1070// case e: e_class:u2 e_name:u2;1071// case s: s_con:u2;1072// case [: do(nval:u2) {value};1073// case @: annotation;1074// case s: s_con:u2;1075// }1076SAFE_ADD(index, limit, 1); // read tag1077const u1 tag = buffer[index - 1];1078switch (tag) {1079case 'B':1080case 'C':1081case 'I':1082case 'S':1083case 'Z':1084case 'D':1085case 'F':1086case 'J':1087case 'c':1088case 's':1089SAFE_ADD(index, limit, 2); // skip con or s_con1090break;1091case 'e':1092SAFE_ADD(index, limit, 4); // skip e_class, e_name1093break;1094case '[':1095{1096SAFE_ADD(index, limit, 2); // read nval1097int nval = Bytes::get_Java_u2((address)buffer + index - 2);1098while (--nval >= 0 && index < limit) {1099index = skip_annotation_value(buffer, limit, index);1100}1101}1102break;1103case '@':1104index = skip_annotation(buffer, limit, index);1105break;1106default:1107return limit; // bad tag byte1108}1109return index;1110}11111112// Sift through annotations, looking for those significant to the VM:1113static void parse_annotations(const ConstantPool* const cp,1114const u1* buffer, int limit,1115AnnotationCollector* coll,1116ClassLoaderData* loader_data,1117const bool can_access_vm_annotations) {11181119assert(cp != NULL, "invariant");1120assert(buffer != NULL, "invariant");1121assert(coll != NULL, "invariant");1122assert(loader_data != NULL, "invariant");11231124// annotations := do(nann:u2) {annotation}1125int index = 2; // read nann1126if (index >= limit) return;1127int nann = Bytes::get_Java_u2((address)buffer + index - 2);1128enum { // initial annotation layout1129atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;'1130count_off = 2, // u2 such as 1 (one value)1131member_off = 4, // utf8 such as 'value'1132tag_off = 6, // u1 such as 'c' (type) or 'e' (enum)1133e_tag_val = 'e',1134e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;'1135e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME'1136e_size = 11, // end of 'e' annotation1137c_tag_val = 'c', // payload is type1138c_con_off = 7, // utf8 payload, such as 'I'1139c_size = 9, // end of 'c' annotation1140s_tag_val = 's', // payload is String1141s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;'1142s_size = 9,1143min_size = 6 // smallest possible size (zero members)1144};1145// Cannot add min_size to index in case of overflow MAX_INT1146while ((--nann) >= 0 && (index - 2 <= limit - min_size)) {1147int index0 = index;1148index = skip_annotation(buffer, limit, index);1149const u1* const abase = buffer + index0;1150const int atype = Bytes::get_Java_u2((address)abase + atype_off);1151const int count = Bytes::get_Java_u2((address)abase + count_off);1152const Symbol* const aname = check_symbol_at(cp, atype);1153if (aname == NULL) break; // invalid annotation name1154const Symbol* member = NULL;1155if (count >= 1) {1156const int member_index = Bytes::get_Java_u2((address)abase + member_off);1157member = check_symbol_at(cp, member_index);1158if (member == NULL) break; // invalid member name1159}11601161// Here is where parsing particular annotations will take place.1162AnnotationCollector::ID id = coll->annotation_index(loader_data, aname, can_access_vm_annotations);1163if (AnnotationCollector::_unknown == id) continue;1164coll->set_annotation(id);11651166if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) {1167// @Contended can optionally specify the contention group.1168//1169// Contended group defines the equivalence class over the fields:1170// the fields within the same contended group are not treated distinct.1171// The only exception is default group, which does not incur the1172// equivalence. Naturally, contention group for classes is meaningless.1173//1174// While the contention group is specified as String, annotation1175// values are already interned, and we might as well use the constant1176// pool index as the group tag.1177//1178u2 group_index = 0; // default contended group1179if (count == 11180&& s_size == (index - index0) // match size1181&& s_tag_val == *(abase + tag_off)1182&& member == vmSymbols::value_name()) {1183group_index = Bytes::get_Java_u2((address)abase + s_con_off);1184if (cp->symbol_at(group_index)->utf8_length() == 0) {1185group_index = 0; // default contended group1186}1187}1188coll->set_contended_group(group_index);1189}1190}1191}119211931194// Parse attributes for a field.1195void ClassFileParser::parse_field_attributes(const ClassFileStream* const cfs,1196u2 attributes_count,1197bool is_static, u2 signature_index,1198u2* const constantvalue_index_addr,1199bool* const is_synthetic_addr,1200u2* const generic_signature_index_addr,1201ClassFileParser::FieldAnnotationCollector* parsed_annotations,1202TRAPS) {1203assert(cfs != NULL, "invariant");1204assert(constantvalue_index_addr != NULL, "invariant");1205assert(is_synthetic_addr != NULL, "invariant");1206assert(generic_signature_index_addr != NULL, "invariant");1207assert(parsed_annotations != NULL, "invariant");1208assert(attributes_count > 0, "attributes_count should be greater than 0");12091210u2 constantvalue_index = 0;1211u2 generic_signature_index = 0;1212bool is_synthetic = false;1213const u1* runtime_visible_annotations = NULL;1214int runtime_visible_annotations_length = 0;1215const u1* runtime_invisible_annotations = NULL;1216int runtime_invisible_annotations_length = 0;1217const u1* runtime_visible_type_annotations = NULL;1218int runtime_visible_type_annotations_length = 0;1219const u1* runtime_invisible_type_annotations = NULL;1220int runtime_invisible_type_annotations_length = 0;1221bool runtime_invisible_annotations_exists = false;1222bool runtime_invisible_type_annotations_exists = false;1223const ConstantPool* const cp = _cp;12241225while (attributes_count--) {1226cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length1227const u2 attribute_name_index = cfs->get_u2_fast();1228const u4 attribute_length = cfs->get_u4_fast();1229check_property(valid_symbol_at(attribute_name_index),1230"Invalid field attribute index %u in class file %s",1231attribute_name_index,1232CHECK);12331234const Symbol* const attribute_name = cp->symbol_at(attribute_name_index);1235if (is_static && attribute_name == vmSymbols::tag_constant_value()) {1236// ignore if non-static1237if (constantvalue_index != 0) {1238classfile_parse_error("Duplicate ConstantValue attribute in class file %s", THREAD);1239return;1240}1241check_property(1242attribute_length == 2,1243"Invalid ConstantValue field attribute length %u in class file %s",1244attribute_length, CHECK);12451246constantvalue_index = cfs->get_u2(CHECK);1247if (_need_verify) {1248verify_constantvalue(cp, constantvalue_index, signature_index, CHECK);1249}1250} else if (attribute_name == vmSymbols::tag_synthetic()) {1251if (attribute_length != 0) {1252classfile_parse_error(1253"Invalid Synthetic field attribute length %u in class file %s",1254attribute_length, THREAD);1255return;1256}1257is_synthetic = true;1258} else if (attribute_name == vmSymbols::tag_deprecated()) { // 42761201259if (attribute_length != 0) {1260classfile_parse_error(1261"Invalid Deprecated field attribute length %u in class file %s",1262attribute_length, THREAD);1263return;1264}1265} else if (_major_version >= JAVA_1_5_VERSION) {1266if (attribute_name == vmSymbols::tag_signature()) {1267if (generic_signature_index != 0) {1268classfile_parse_error(1269"Multiple Signature attributes for field in class file %s", THREAD);1270return;1271}1272if (attribute_length != 2) {1273classfile_parse_error(1274"Wrong size %u for field's Signature attribute in class file %s",1275attribute_length, THREAD);1276return;1277}1278generic_signature_index = parse_generic_signature_attribute(cfs, CHECK);1279} else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {1280if (runtime_visible_annotations != NULL) {1281classfile_parse_error(1282"Multiple RuntimeVisibleAnnotations attributes for field in class file %s", THREAD);1283return;1284}1285runtime_visible_annotations_length = attribute_length;1286runtime_visible_annotations = cfs->current();1287assert(runtime_visible_annotations != NULL, "null visible annotations");1288cfs->guarantee_more(runtime_visible_annotations_length, CHECK);1289parse_annotations(cp,1290runtime_visible_annotations,1291runtime_visible_annotations_length,1292parsed_annotations,1293_loader_data,1294_can_access_vm_annotations);1295cfs->skip_u1_fast(runtime_visible_annotations_length);1296} else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {1297if (runtime_invisible_annotations_exists) {1298classfile_parse_error(1299"Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", THREAD);1300return;1301}1302runtime_invisible_annotations_exists = true;1303if (PreserveAllAnnotations) {1304runtime_invisible_annotations_length = attribute_length;1305runtime_invisible_annotations = cfs->current();1306assert(runtime_invisible_annotations != NULL, "null invisible annotations");1307}1308cfs->skip_u1(attribute_length, CHECK);1309} else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {1310if (runtime_visible_type_annotations != NULL) {1311classfile_parse_error(1312"Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", THREAD);1313return;1314}1315runtime_visible_type_annotations_length = attribute_length;1316runtime_visible_type_annotations = cfs->current();1317assert(runtime_visible_type_annotations != NULL, "null visible type annotations");1318cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);1319} else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {1320if (runtime_invisible_type_annotations_exists) {1321classfile_parse_error(1322"Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", THREAD);1323return;1324} else {1325runtime_invisible_type_annotations_exists = true;1326}1327if (PreserveAllAnnotations) {1328runtime_invisible_type_annotations_length = attribute_length;1329runtime_invisible_type_annotations = cfs->current();1330assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");1331}1332cfs->skip_u1(attribute_length, CHECK);1333} else {1334cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes1335}1336} else {1337cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes1338}1339}13401341*constantvalue_index_addr = constantvalue_index;1342*is_synthetic_addr = is_synthetic;1343*generic_signature_index_addr = generic_signature_index;1344AnnotationArray* a = assemble_annotations(runtime_visible_annotations,1345runtime_visible_annotations_length,1346runtime_invisible_annotations,1347runtime_invisible_annotations_length,1348CHECK);1349parsed_annotations->set_field_annotations(a);1350a = assemble_annotations(runtime_visible_type_annotations,1351runtime_visible_type_annotations_length,1352runtime_invisible_type_annotations,1353runtime_invisible_type_annotations_length,1354CHECK);1355parsed_annotations->set_field_type_annotations(a);1356return;1357}135813591360// Field allocation types. Used for computing field offsets.13611362enum FieldAllocationType {1363STATIC_OOP, // Oops1364STATIC_BYTE, // Boolean, Byte, char1365STATIC_SHORT, // shorts1366STATIC_WORD, // ints1367STATIC_DOUBLE, // aligned long or double1368NONSTATIC_OOP,1369NONSTATIC_BYTE,1370NONSTATIC_SHORT,1371NONSTATIC_WORD,1372NONSTATIC_DOUBLE,1373MAX_FIELD_ALLOCATION_TYPE,1374BAD_ALLOCATION_TYPE = -11375};13761377static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {1378BAD_ALLOCATION_TYPE, // 01379BAD_ALLOCATION_TYPE, // 11380BAD_ALLOCATION_TYPE, // 21381BAD_ALLOCATION_TYPE, // 31382NONSTATIC_BYTE , // T_BOOLEAN = 4,1383NONSTATIC_SHORT, // T_CHAR = 5,1384NONSTATIC_WORD, // T_FLOAT = 6,1385NONSTATIC_DOUBLE, // T_DOUBLE = 7,1386NONSTATIC_BYTE, // T_BYTE = 8,1387NONSTATIC_SHORT, // T_SHORT = 9,1388NONSTATIC_WORD, // T_INT = 10,1389NONSTATIC_DOUBLE, // T_LONG = 11,1390NONSTATIC_OOP, // T_OBJECT = 12,1391NONSTATIC_OOP, // T_ARRAY = 13,1392BAD_ALLOCATION_TYPE, // T_VOID = 14,1393BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,1394BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,1395BAD_ALLOCATION_TYPE, // T_METADATA = 17,1396BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,1397BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,1398BAD_ALLOCATION_TYPE, // 01399BAD_ALLOCATION_TYPE, // 11400BAD_ALLOCATION_TYPE, // 21401BAD_ALLOCATION_TYPE, // 31402STATIC_BYTE , // T_BOOLEAN = 4,1403STATIC_SHORT, // T_CHAR = 5,1404STATIC_WORD, // T_FLOAT = 6,1405STATIC_DOUBLE, // T_DOUBLE = 7,1406STATIC_BYTE, // T_BYTE = 8,1407STATIC_SHORT, // T_SHORT = 9,1408STATIC_WORD, // T_INT = 10,1409STATIC_DOUBLE, // T_LONG = 11,1410STATIC_OOP, // T_OBJECT = 12,1411STATIC_OOP, // T_ARRAY = 13,1412BAD_ALLOCATION_TYPE, // T_VOID = 14,1413BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,1414BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,1415BAD_ALLOCATION_TYPE, // T_METADATA = 17,1416BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,1417BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,1418};14191420static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {1421assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");1422FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];1423assert(result != BAD_ALLOCATION_TYPE, "bad type");1424return result;1425}14261427class ClassFileParser::FieldAllocationCount : public ResourceObj {1428public:1429u2 count[MAX_FIELD_ALLOCATION_TYPE];14301431FieldAllocationCount() {1432for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {1433count[i] = 0;1434}1435}14361437void update(bool is_static, BasicType type) {1438FieldAllocationType atype = basic_type_to_atype(is_static, type);1439if (atype != BAD_ALLOCATION_TYPE) {1440// Make sure there is no overflow with injected fields.1441assert(count[atype] < 0xFFFF, "More than 65535 fields");1442count[atype]++;1443}1444}1445};14461447// Side-effects: populates the _fields, _fields_annotations,1448// _fields_type_annotations fields1449void ClassFileParser::parse_fields(const ClassFileStream* const cfs,1450bool is_interface,1451FieldAllocationCount* const fac,1452ConstantPool* cp,1453const int cp_size,1454u2* const java_fields_count_ptr,1455TRAPS) {14561457assert(cfs != NULL, "invariant");1458assert(fac != NULL, "invariant");1459assert(cp != NULL, "invariant");1460assert(java_fields_count_ptr != NULL, "invariant");14611462assert(NULL == _fields, "invariant");1463assert(NULL == _fields_annotations, "invariant");1464assert(NULL == _fields_type_annotations, "invariant");14651466cfs->guarantee_more(2, CHECK); // length1467const u2 length = cfs->get_u2_fast();1468*java_fields_count_ptr = length;14691470int num_injected = 0;1471const InjectedField* const injected = JavaClasses::get_injected(_class_name,1472&num_injected);1473const int total_fields = length + num_injected;14741475// The field array starts with tuples of shorts1476// [access, name index, sig index, initial value index, byte offset].1477// A generic signature slot only exists for field with generic1478// signature attribute. And the access flag is set with1479// JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic1480// signature slots are at the end of the field array and after all1481// other fields data.1482//1483// f1: [access, name index, sig index, initial value index, low_offset, high_offset]1484// f2: [access, name index, sig index, initial value index, low_offset, high_offset]1485// ...1486// fn: [access, name index, sig index, initial value index, low_offset, high_offset]1487// [generic signature index]1488// [generic signature index]1489// ...1490//1491// Allocate a temporary resource array for field data. For each field,1492// a slot is reserved in the temporary array for the generic signature1493// index. After parsing all fields, the data are copied to a permanent1494// array and any unused slots will be discarded.1495ResourceMark rm(THREAD);1496u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,1497u2,1498total_fields * (FieldInfo::field_slots + 1));14991500// The generic signature slots start after all other fields' data.1501int generic_signature_slot = total_fields * FieldInfo::field_slots;1502int num_generic_signature = 0;1503for (int n = 0; n < length; n++) {1504// access_flags, name_index, descriptor_index, attributes_count1505cfs->guarantee_more(8, CHECK);15061507AccessFlags access_flags;1508const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;1509verify_legal_field_modifiers(flags, is_interface, CHECK);1510access_flags.set_flags(flags);15111512const u2 name_index = cfs->get_u2_fast();1513check_property(valid_symbol_at(name_index),1514"Invalid constant pool index %u for field name in class file %s",1515name_index, CHECK);1516const Symbol* const name = cp->symbol_at(name_index);1517verify_legal_field_name(name, CHECK);15181519const u2 signature_index = cfs->get_u2_fast();1520check_property(valid_symbol_at(signature_index),1521"Invalid constant pool index %u for field signature in class file %s",1522signature_index, CHECK);1523const Symbol* const sig = cp->symbol_at(signature_index);1524verify_legal_field_signature(name, sig, CHECK);15251526u2 constantvalue_index = 0;1527bool is_synthetic = false;1528u2 generic_signature_index = 0;1529const bool is_static = access_flags.is_static();1530FieldAnnotationCollector parsed_annotations(_loader_data);15311532const u2 attributes_count = cfs->get_u2_fast();1533if (attributes_count > 0) {1534parse_field_attributes(cfs,1535attributes_count,1536is_static,1537signature_index,1538&constantvalue_index,1539&is_synthetic,1540&generic_signature_index,1541&parsed_annotations,1542CHECK);15431544if (parsed_annotations.field_annotations() != NULL) {1545if (_fields_annotations == NULL) {1546_fields_annotations = MetadataFactory::new_array<AnnotationArray*>(1547_loader_data, length, NULL,1548CHECK);1549}1550_fields_annotations->at_put(n, parsed_annotations.field_annotations());1551parsed_annotations.set_field_annotations(NULL);1552}1553if (parsed_annotations.field_type_annotations() != NULL) {1554if (_fields_type_annotations == NULL) {1555_fields_type_annotations =1556MetadataFactory::new_array<AnnotationArray*>(_loader_data,1557length,1558NULL,1559CHECK);1560}1561_fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());1562parsed_annotations.set_field_type_annotations(NULL);1563}15641565if (is_synthetic) {1566access_flags.set_is_synthetic();1567}1568if (generic_signature_index != 0) {1569access_flags.set_field_has_generic_signature();1570fa[generic_signature_slot] = generic_signature_index;1571generic_signature_slot ++;1572num_generic_signature ++;1573}1574}15751576FieldInfo* const field = FieldInfo::from_field_array(fa, n);1577field->initialize(access_flags.as_short(),1578name_index,1579signature_index,1580constantvalue_index);1581const BasicType type = cp->basic_type_for_signature_at(signature_index);15821583// Update FieldAllocationCount for this kind of field1584fac->update(is_static, type);15851586// After field is initialized with type, we can augment it with aux info1587if (parsed_annotations.has_any_annotations()) {1588parsed_annotations.apply_to(field);1589if (field->is_contended()) {1590_has_contended_fields = true;1591}1592}1593}15941595int index = length;1596if (num_injected != 0) {1597for (int n = 0; n < num_injected; n++) {1598// Check for duplicates1599if (injected[n].may_be_java) {1600const Symbol* const name = injected[n].name();1601const Symbol* const signature = injected[n].signature();1602bool duplicate = false;1603for (int i = 0; i < length; i++) {1604const FieldInfo* const f = FieldInfo::from_field_array(fa, i);1605if (name == cp->symbol_at(f->name_index()) &&1606signature == cp->symbol_at(f->signature_index())) {1607// Symbol is desclared in Java so skip this one1608duplicate = true;1609break;1610}1611}1612if (duplicate) {1613// These will be removed from the field array at the end1614continue;1615}1616}16171618// Injected field1619FieldInfo* const field = FieldInfo::from_field_array(fa, index);1620field->initialize((u2)JVM_ACC_FIELD_INTERNAL,1621(u2)(injected[n].name_index),1622(u2)(injected[n].signature_index),16230);16241625const BasicType type = Signature::basic_type(injected[n].signature());16261627// Update FieldAllocationCount for this kind of field1628fac->update(false, type);1629index++;1630}1631}16321633assert(NULL == _fields, "invariant");16341635_fields =1636MetadataFactory::new_array<u2>(_loader_data,1637index * FieldInfo::field_slots + num_generic_signature,1638CHECK);1639// Sometimes injected fields already exist in the Java source so1640// the fields array could be too long. In that case the1641// fields array is trimed. Also unused slots that were reserved1642// for generic signature indexes are discarded.1643{1644int i = 0;1645for (; i < index * FieldInfo::field_slots; i++) {1646_fields->at_put(i, fa[i]);1647}1648for (int j = total_fields * FieldInfo::field_slots;1649j < generic_signature_slot; j++) {1650_fields->at_put(i++, fa[j]);1651}1652assert(_fields->length() == i, "");1653}16541655if (_need_verify && length > 1) {1656// Check duplicated fields1657ResourceMark rm(THREAD);1658NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(1659THREAD, NameSigHash*, HASH_ROW_SIZE);1660initialize_hashtable(names_and_sigs);1661bool dup = false;1662const Symbol* name = NULL;1663const Symbol* sig = NULL;1664{1665debug_only(NoSafepointVerifier nsv;)1666for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {1667name = fs.name();1668sig = fs.signature();1669// If no duplicates, add name/signature in hashtable names_and_sigs.1670if (!put_after_lookup(name, sig, names_and_sigs)) {1671dup = true;1672break;1673}1674}1675}1676if (dup) {1677classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",1678name->as_C_string(), sig->as_klass_external_name(), THREAD);1679}1680}1681}168216831684const ClassFileParser::unsafe_u2* ClassFileParser::parse_exception_table(const ClassFileStream* const cfs,1685u4 code_length,1686u4 exception_table_length,1687TRAPS) {1688assert(cfs != NULL, "invariant");16891690const unsafe_u2* const exception_table_start = cfs->current();1691assert(exception_table_start != NULL, "null exception table");16921693cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc,1694// end_pc,1695// handler_pc,1696// catch_type_index16971698// Will check legal target after parsing code array in verifier.1699if (_need_verify) {1700for (unsigned int i = 0; i < exception_table_length; i++) {1701const u2 start_pc = cfs->get_u2_fast();1702const u2 end_pc = cfs->get_u2_fast();1703const u2 handler_pc = cfs->get_u2_fast();1704const u2 catch_type_index = cfs->get_u2_fast();1705guarantee_property((start_pc < end_pc) && (end_pc <= code_length),1706"Illegal exception table range in class file %s",1707CHECK_NULL);1708guarantee_property(handler_pc < code_length,1709"Illegal exception table handler in class file %s",1710CHECK_NULL);1711if (catch_type_index != 0) {1712guarantee_property(valid_klass_reference_at(catch_type_index),1713"Catch type in exception table has bad constant type in class file %s", CHECK_NULL);1714}1715}1716} else {1717cfs->skip_u2_fast(exception_table_length * 4);1718}1719return exception_table_start;1720}17211722void ClassFileParser::parse_linenumber_table(u4 code_attribute_length,1723u4 code_length,1724CompressedLineNumberWriteStream**const write_stream,1725TRAPS) {17261727const ClassFileStream* const cfs = _stream;1728unsigned int num_entries = cfs->get_u2(CHECK);17291730// Each entry is a u2 start_pc, and a u2 line_number1731const unsigned int length_in_bytes = num_entries * (sizeof(u2) * 2);17321733// Verify line number attribute and table length1734check_property(1735code_attribute_length == sizeof(u2) + length_in_bytes,1736"LineNumberTable attribute has wrong length in class file %s", CHECK);17371738cfs->guarantee_more(length_in_bytes, CHECK);17391740if ((*write_stream) == NULL) {1741if (length_in_bytes > fixed_buffer_size) {1742(*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes);1743} else {1744(*write_stream) = new CompressedLineNumberWriteStream(1745_linenumbertable_buffer, fixed_buffer_size);1746}1747}17481749while (num_entries-- > 0) {1750const u2 bci = cfs->get_u2_fast(); // start_pc1751const u2 line = cfs->get_u2_fast(); // line_number1752guarantee_property(bci < code_length,1753"Invalid pc in LineNumberTable in class file %s", CHECK);1754(*write_stream)->write_pair(bci, line);1755}1756}175717581759class LVT_Hash : public AllStatic {1760public:17611762static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) {1763/*1764* 3-tuple start_bci/length/slot has to be unique key,1765* so the following comparison seems to be redundant:1766* && elem->name_cp_index == entry->_elem->name_cp_index1767*/1768return (e0.start_bci == e1.start_bci &&1769e0.length == e1.length &&1770e0.name_cp_index == e1.name_cp_index &&1771e0.slot == e1.slot);1772}17731774static unsigned int hash(LocalVariableTableElement const& e0) {1775unsigned int raw_hash = e0.start_bci;17761777raw_hash = e0.length + raw_hash * 37;1778raw_hash = e0.name_cp_index + raw_hash * 37;1779raw_hash = e0.slot + raw_hash * 37;17801781return raw_hash;1782}1783};178417851786// Class file LocalVariableTable elements.1787class Classfile_LVT_Element {1788public:1789u2 start_bci;1790u2 length;1791u2 name_cp_index;1792u2 descriptor_cp_index;1793u2 slot;1794};17951796static void copy_lvt_element(const Classfile_LVT_Element* const src,1797LocalVariableTableElement* const lvt) {1798lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci);1799lvt->length = Bytes::get_Java_u2((u1*) &src->length);1800lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index);1801lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index);1802lvt->signature_cp_index = 0;1803lvt->slot = Bytes::get_Java_u2((u1*) &src->slot);1804}18051806// Function is used to parse both attributes:1807// LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)1808const ClassFileParser::unsafe_u2* ClassFileParser::parse_localvariable_table(const ClassFileStream* cfs,1809u4 code_length,1810u2 max_locals,1811u4 code_attribute_length,1812u2* const localvariable_table_length,1813bool isLVTT,1814TRAPS) {1815const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";1816*localvariable_table_length = cfs->get_u2(CHECK_NULL);1817const unsigned int size =1818(*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2);18191820const ConstantPool* const cp = _cp;18211822// Verify local variable table attribute has right length1823if (_need_verify) {1824guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)),1825"%s has wrong length in class file %s", tbl_name, CHECK_NULL);1826}18271828const unsafe_u2* const localvariable_table_start = cfs->current();1829assert(localvariable_table_start != NULL, "null local variable table");1830if (!_need_verify) {1831cfs->skip_u2_fast(size);1832} else {1833cfs->guarantee_more(size * 2, CHECK_NULL);1834for(int i = 0; i < (*localvariable_table_length); i++) {1835const u2 start_pc = cfs->get_u2_fast();1836const u2 length = cfs->get_u2_fast();1837const u2 name_index = cfs->get_u2_fast();1838const u2 descriptor_index = cfs->get_u2_fast();1839const u2 index = cfs->get_u2_fast();1840// Assign to a u4 to avoid overflow1841const u4 end_pc = (u4)start_pc + (u4)length;18421843if (start_pc >= code_length) {1844classfile_parse_error(1845"Invalid start_pc %u in %s in class file %s",1846start_pc, tbl_name, THREAD);1847return NULL;1848}1849if (end_pc > code_length) {1850classfile_parse_error(1851"Invalid length %u in %s in class file %s",1852length, tbl_name, THREAD);1853return NULL;1854}1855const int cp_size = cp->length();1856guarantee_property(valid_symbol_at(name_index),1857"Name index %u in %s has bad constant type in class file %s",1858name_index, tbl_name, CHECK_NULL);1859guarantee_property(valid_symbol_at(descriptor_index),1860"Signature index %u in %s has bad constant type in class file %s",1861descriptor_index, tbl_name, CHECK_NULL);18621863const Symbol* const name = cp->symbol_at(name_index);1864const Symbol* const sig = cp->symbol_at(descriptor_index);1865verify_legal_field_name(name, CHECK_NULL);1866u2 extra_slot = 0;1867if (!isLVTT) {1868verify_legal_field_signature(name, sig, CHECK_NULL);18691870// 4894874: check special cases for double and long local variables1871if (sig == vmSymbols::type_signature(T_DOUBLE) ||1872sig == vmSymbols::type_signature(T_LONG)) {1873extra_slot = 1;1874}1875}1876guarantee_property((index + extra_slot) < max_locals,1877"Invalid index %u in %s in class file %s",1878index, tbl_name, CHECK_NULL);1879}1880}1881return localvariable_table_start;1882}18831884static const u1* parse_stackmap_table(const ClassFileStream* const cfs,1885u4 code_attribute_length,1886bool need_verify,1887TRAPS) {1888assert(cfs != NULL, "invariant");18891890if (0 == code_attribute_length) {1891return NULL;1892}18931894const u1* const stackmap_table_start = cfs->current();1895assert(stackmap_table_start != NULL, "null stackmap table");18961897// check code_attribute_length first1898cfs->skip_u1(code_attribute_length, CHECK_NULL);18991900if (!need_verify && !DumpSharedSpaces) {1901return NULL;1902}1903return stackmap_table_start;1904}19051906const ClassFileParser::unsafe_u2* ClassFileParser::parse_checked_exceptions(const ClassFileStream* const cfs,1907u2* const checked_exceptions_length,1908u4 method_attribute_length,1909TRAPS) {1910assert(cfs != NULL, "invariant");1911assert(checked_exceptions_length != NULL, "invariant");19121913cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length1914*checked_exceptions_length = cfs->get_u2_fast();1915const unsigned int size =1916(*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);1917const unsafe_u2* const checked_exceptions_start = cfs->current();1918assert(checked_exceptions_start != NULL, "null checked exceptions");1919if (!_need_verify) {1920cfs->skip_u2_fast(size);1921} else {1922// Verify each value in the checked exception table1923u2 checked_exception;1924const u2 len = *checked_exceptions_length;1925cfs->guarantee_more(2 * len, CHECK_NULL);1926for (int i = 0; i < len; i++) {1927checked_exception = cfs->get_u2_fast();1928check_property(1929valid_klass_reference_at(checked_exception),1930"Exception name has bad type at constant pool %u in class file %s",1931checked_exception, CHECK_NULL);1932}1933}1934// check exceptions attribute length1935if (_need_verify) {1936guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +1937sizeof(u2) * size),1938"Exceptions attribute has wrong length in class file %s", CHECK_NULL);1939}1940return checked_exceptions_start;1941}19421943void ClassFileParser::throwIllegalSignature(const char* type,1944const Symbol* name,1945const Symbol* sig,1946TRAPS) const {1947assert(name != NULL, "invariant");1948assert(sig != NULL, "invariant");19491950ResourceMark rm(THREAD);1951Exceptions::fthrow(THREAD_AND_LOCATION,1952vmSymbols::java_lang_ClassFormatError(),1953"%s \"%s\" in class %s has illegal signature \"%s\"", type,1954name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());1955}19561957AnnotationCollector::ID1958AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,1959const Symbol* name,1960const bool can_access_vm_annotations) {1961const vmSymbolID sid = vmSymbols::find_sid(name);1962// Privileged code can use all annotations. Other code silently drops some.1963const bool privileged = loader_data->is_boot_class_loader_data() ||1964loader_data->is_platform_class_loader_data() ||1965can_access_vm_annotations;1966switch (sid) {1967case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {1968if (_location != _in_method) break; // only allow for methods1969if (!privileged) break; // only allow in privileged code1970return _method_CallerSensitive;1971}1972case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {1973if (_location != _in_method) break; // only allow for methods1974if (!privileged) break; // only allow in privileged code1975return _method_ForceInline;1976}1977case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): {1978if (_location != _in_method) break; // only allow for methods1979if (!privileged) break; // only allow in privileged code1980return _method_DontInline;1981}1982case VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): {1983if (_location != _in_method) break; // only allow for methods1984if (!privileged) break; // only allow in privileged code1985return _method_InjectedProfile;1986}1987case VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): {1988if (_location != _in_method) break; // only allow for methods1989if (!privileged) break; // only allow in privileged code1990return _method_LambdaForm_Compiled;1991}1992case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Hidden_signature): {1993if (_location != _in_method) break; // only allow for methods1994if (!privileged) break; // only allow in privileged code1995return _method_Hidden;1996}1997case VM_SYMBOL_ENUM_NAME(jdk_internal_misc_Scoped_signature): {1998if (_location != _in_method) break; // only allow for methods1999if (!privileged) break; // only allow in privileged code2000return _method_Scoped;2001}2002case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_IntrinsicCandidate_signature): {2003if (_location != _in_method) break; // only allow for methods2004if (!privileged) break; // only allow in privileged code2005return _method_IntrinsicCandidate;2006}2007case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Stable_signature): {2008if (_location != _in_field) break; // only allow for fields2009if (!privileged) break; // only allow in privileged code2010return _field_Stable;2011}2012case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {2013if (_location != _in_field && _location != _in_class) {2014break; // only allow for fields and classes2015}2016if (!EnableContended || (RestrictContended && !privileged)) {2017break; // honor privileges2018}2019return _jdk_internal_vm_annotation_Contended;2020}2021case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {2022if (_location != _in_method) break; // only allow for methods2023if (RestrictReservedStack && !privileged) break; // honor privileges2024return _jdk_internal_vm_annotation_ReservedStackAccess;2025}2026case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {2027if (_location != _in_class) break; // only allow for classes2028if (!privileged) break; // only allow in priviledged code2029return _jdk_internal_ValueBased;2030}2031default: {2032break;2033}2034}2035return AnnotationCollector::_unknown;2036}20372038void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {2039if (is_contended())2040f->set_contended_group(contended_group());2041if (is_stable())2042f->set_stable(true);2043}20442045ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {2046// If there's an error deallocate metadata for field annotations2047MetadataFactory::free_array<u1>(_loader_data, _field_annotations);2048MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations);2049}20502051void MethodAnnotationCollector::apply_to(const methodHandle& m) {2052if (has_annotation(_method_CallerSensitive))2053m->set_caller_sensitive(true);2054if (has_annotation(_method_ForceInline))2055m->set_force_inline(true);2056if (has_annotation(_method_DontInline))2057m->set_dont_inline(true);2058if (has_annotation(_method_InjectedProfile))2059m->set_has_injected_profile(true);2060if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none)2061m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm);2062if (has_annotation(_method_Hidden))2063m->set_hidden(true);2064if (has_annotation(_method_Scoped))2065m->set_scoped(true);2066if (has_annotation(_method_IntrinsicCandidate) && !m->is_synthetic())2067m->set_intrinsic_candidate(true);2068if (has_annotation(_jdk_internal_vm_annotation_ReservedStackAccess))2069m->set_has_reserved_stack_access(true);2070}20712072void ClassFileParser::ClassAnnotationCollector::apply_to(InstanceKlass* ik) {2073assert(ik != NULL, "invariant");2074if (has_annotation(_jdk_internal_vm_annotation_Contended)) {2075ik->set_is_contended(is_contended());2076}2077if (has_annotation(_jdk_internal_ValueBased)) {2078ik->set_has_value_based_class_annotation();2079if (DiagnoseSyncOnValueBasedClasses) {2080ik->set_is_value_based();2081ik->set_prototype_header(markWord::prototype());2082}2083}2084}20852086#define MAX_ARGS_SIZE 2552087#define MAX_CODE_SIZE 655352088#define INITIAL_MAX_LVT_NUMBER 25620892090/* Copy class file LVT's/LVTT's into the HotSpot internal LVT.2091*2092* Rules for LVT's and LVTT's are:2093* - There can be any number of LVT's and LVTT's.2094* - If there are n LVT's, it is the same as if there was just2095* one LVT containing all the entries from the n LVT's.2096* - There may be no more than one LVT entry per local variable.2097* Two LVT entries are 'equal' if these fields are the same:2098* start_pc, length, name, slot2099* - There may be no more than one LVTT entry per each LVT entry.2100* Each LVTT entry has to match some LVT entry.2101* - HotSpot internal LVT keeps natural ordering of class file LVT entries.2102*/2103void ClassFileParser::copy_localvariable_table(const ConstMethod* cm,2104int lvt_cnt,2105u2* const localvariable_table_length,2106const unsafe_u2** const localvariable_table_start,2107int lvtt_cnt,2108u2* const localvariable_type_table_length,2109const unsafe_u2** const localvariable_type_table_start,2110TRAPS) {21112112ResourceMark rm(THREAD);21132114typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*,2115&LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable;21162117LVT_HashTable* const table = new LVT_HashTable();21182119// To fill LocalVariableTable in2120const Classfile_LVT_Element* cf_lvt;2121LocalVariableTableElement* lvt = cm->localvariable_table_start();21222123for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) {2124cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no];2125for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) {2126copy_lvt_element(&cf_lvt[idx], lvt);2127// If no duplicates, add LVT elem in hashtable.2128if (table->put(*lvt, lvt) == false2129&& _need_verify2130&& _major_version >= JAVA_1_5_VERSION) {2131classfile_parse_error("Duplicated LocalVariableTable attribute "2132"entry for '%s' in class file %s",2133_cp->symbol_at(lvt->name_cp_index)->as_utf8(),2134THREAD);2135return;2136}2137}2138}21392140// To merge LocalVariableTable and LocalVariableTypeTable2141const Classfile_LVT_Element* cf_lvtt;2142LocalVariableTableElement lvtt_elem;21432144for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) {2145cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no];2146for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) {2147copy_lvt_element(&cf_lvtt[idx], &lvtt_elem);2148LocalVariableTableElement** entry = table->get(lvtt_elem);2149if (entry == NULL) {2150if (_need_verify) {2151classfile_parse_error("LVTT entry for '%s' in class file %s "2152"does not match any LVT entry",2153_cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),2154THREAD);2155return;2156}2157} else if ((*entry)->signature_cp_index != 0 && _need_verify) {2158classfile_parse_error("Duplicated LocalVariableTypeTable attribute "2159"entry for '%s' in class file %s",2160_cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),2161THREAD);2162return;2163} else {2164// to add generic signatures into LocalVariableTable2165(*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index;2166}2167}2168}2169}217021712172void ClassFileParser::copy_method_annotations(ConstMethod* cm,2173const u1* runtime_visible_annotations,2174int runtime_visible_annotations_length,2175const u1* runtime_invisible_annotations,2176int runtime_invisible_annotations_length,2177const u1* runtime_visible_parameter_annotations,2178int runtime_visible_parameter_annotations_length,2179const u1* runtime_invisible_parameter_annotations,2180int runtime_invisible_parameter_annotations_length,2181const u1* runtime_visible_type_annotations,2182int runtime_visible_type_annotations_length,2183const u1* runtime_invisible_type_annotations,2184int runtime_invisible_type_annotations_length,2185const u1* annotation_default,2186int annotation_default_length,2187TRAPS) {21882189AnnotationArray* a;21902191if (runtime_visible_annotations_length +2192runtime_invisible_annotations_length > 0) {2193a = assemble_annotations(runtime_visible_annotations,2194runtime_visible_annotations_length,2195runtime_invisible_annotations,2196runtime_invisible_annotations_length,2197CHECK);2198cm->set_method_annotations(a);2199}22002201if (runtime_visible_parameter_annotations_length +2202runtime_invisible_parameter_annotations_length > 0) {2203a = assemble_annotations(runtime_visible_parameter_annotations,2204runtime_visible_parameter_annotations_length,2205runtime_invisible_parameter_annotations,2206runtime_invisible_parameter_annotations_length,2207CHECK);2208cm->set_parameter_annotations(a);2209}22102211if (annotation_default_length > 0) {2212a = assemble_annotations(annotation_default,2213annotation_default_length,2214NULL,22150,2216CHECK);2217cm->set_default_annotations(a);2218}22192220if (runtime_visible_type_annotations_length +2221runtime_invisible_type_annotations_length > 0) {2222a = assemble_annotations(runtime_visible_type_annotations,2223runtime_visible_type_annotations_length,2224runtime_invisible_type_annotations,2225runtime_invisible_type_annotations_length,2226CHECK);2227cm->set_type_annotations(a);2228}2229}223022312232// Note: the parse_method below is big and clunky because all parsing of the code and exceptions2233// attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the2234// Method* to save footprint, so we only know the size of the resulting Method* when the2235// entire method attribute is parsed.2236//2237// The promoted_flags parameter is used to pass relevant access_flags2238// from the method back up to the containing klass. These flag values2239// are added to klass's access_flags.22402241Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,2242bool is_interface,2243const ConstantPool* cp,2244AccessFlags* const promoted_flags,2245TRAPS) {2246assert(cfs != NULL, "invariant");2247assert(cp != NULL, "invariant");2248assert(promoted_flags != NULL, "invariant");22492250ResourceMark rm(THREAD);2251// Parse fixed parts:2252// access_flags, name_index, descriptor_index, attributes_count2253cfs->guarantee_more(8, CHECK_NULL);22542255int flags = cfs->get_u2_fast();2256const u2 name_index = cfs->get_u2_fast();2257const int cp_size = cp->length();2258check_property(2259valid_symbol_at(name_index),2260"Illegal constant pool index %u for method name in class file %s",2261name_index, CHECK_NULL);2262const Symbol* const name = cp->symbol_at(name_index);2263verify_legal_method_name(name, CHECK_NULL);22642265const u2 signature_index = cfs->get_u2_fast();2266guarantee_property(2267valid_symbol_at(signature_index),2268"Illegal constant pool index %u for method signature in class file %s",2269signature_index, CHECK_NULL);2270const Symbol* const signature = cp->symbol_at(signature_index);22712272if (name == vmSymbols::class_initializer_name()) {2273// We ignore the other access flags for a valid class initializer.2274// (JVM Spec 2nd ed., chapter 4.6)2275if (_major_version < 51) { // backward compatibility2276flags = JVM_ACC_STATIC;2277} else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {2278flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);2279} else {2280classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);2281return NULL;2282}2283} else {2284verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);2285}22862287if (name == vmSymbols::object_initializer_name() && is_interface) {2288classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);2289return NULL;2290}22912292int args_size = -1; // only used when _need_verify is true2293if (_need_verify) {2294args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +2295verify_legal_method_signature(name, signature, CHECK_NULL);2296if (args_size > MAX_ARGS_SIZE) {2297classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);2298return NULL;2299}2300}23012302AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);23032304// Default values for code and exceptions attribute elements2305u2 max_stack = 0;2306u2 max_locals = 0;2307u4 code_length = 0;2308const u1* code_start = 0;2309u2 exception_table_length = 0;2310const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements2311Array<int>* exception_handlers = Universe::the_empty_int_array();2312u2 checked_exceptions_length = 0;2313const unsafe_u2* checked_exceptions_start = NULL; // (potentially unaligned) pointer to array of u2 elements2314CompressedLineNumberWriteStream* linenumber_table = NULL;2315int linenumber_table_length = 0;2316int total_lvt_length = 0;2317u2 lvt_cnt = 0;2318u2 lvtt_cnt = 0;2319bool lvt_allocated = false;2320u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER;2321u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER;2322u2* localvariable_table_length = NULL;2323const unsafe_u2** localvariable_table_start = NULL; // (potentially unaligned) pointer to array of LVT attributes2324u2* localvariable_type_table_length = NULL;2325const unsafe_u2** localvariable_type_table_start = NULL; // (potentially unaligned) pointer to LVTT attributes2326int method_parameters_length = -1;2327const u1* method_parameters_data = NULL;2328bool method_parameters_seen = false;2329bool parsed_code_attribute = false;2330bool parsed_checked_exceptions_attribute = false;2331bool parsed_stackmap_attribute = false;2332// stackmap attribute - JDK1.52333const u1* stackmap_data = NULL;2334int stackmap_data_length = 0;2335u2 generic_signature_index = 0;2336MethodAnnotationCollector parsed_annotations;2337const u1* runtime_visible_annotations = NULL;2338int runtime_visible_annotations_length = 0;2339const u1* runtime_invisible_annotations = NULL;2340int runtime_invisible_annotations_length = 0;2341const u1* runtime_visible_parameter_annotations = NULL;2342int runtime_visible_parameter_annotations_length = 0;2343const u1* runtime_invisible_parameter_annotations = NULL;2344int runtime_invisible_parameter_annotations_length = 0;2345const u1* runtime_visible_type_annotations = NULL;2346int runtime_visible_type_annotations_length = 0;2347const u1* runtime_invisible_type_annotations = NULL;2348int runtime_invisible_type_annotations_length = 0;2349bool runtime_invisible_annotations_exists = false;2350bool runtime_invisible_type_annotations_exists = false;2351bool runtime_invisible_parameter_annotations_exists = false;2352const u1* annotation_default = NULL;2353int annotation_default_length = 0;23542355// Parse code and exceptions attribute2356u2 method_attributes_count = cfs->get_u2_fast();2357while (method_attributes_count--) {2358cfs->guarantee_more(6, CHECK_NULL); // method_attribute_name_index, method_attribute_length2359const u2 method_attribute_name_index = cfs->get_u2_fast();2360const u4 method_attribute_length = cfs->get_u4_fast();2361check_property(2362valid_symbol_at(method_attribute_name_index),2363"Invalid method attribute name index %u in class file %s",2364method_attribute_name_index, CHECK_NULL);23652366const Symbol* const method_attribute_name = cp->symbol_at(method_attribute_name_index);2367if (method_attribute_name == vmSymbols::tag_code()) {2368// Parse Code attribute2369if (_need_verify) {2370guarantee_property(2371!access_flags.is_native() && !access_flags.is_abstract(),2372"Code attribute in native or abstract methods in class file %s",2373CHECK_NULL);2374}2375if (parsed_code_attribute) {2376classfile_parse_error("Multiple Code attributes in class file %s",2377THREAD);2378return NULL;2379}2380parsed_code_attribute = true;23812382// Stack size, locals size, and code size2383cfs->guarantee_more(8, CHECK_NULL);2384max_stack = cfs->get_u2_fast();2385max_locals = cfs->get_u2_fast();2386code_length = cfs->get_u4_fast();2387if (_need_verify) {2388guarantee_property(args_size <= max_locals,2389"Arguments can't fit into locals in class file %s",2390CHECK_NULL);2391guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE,2392"Invalid method Code length %u in class file %s",2393code_length, CHECK_NULL);2394}2395// Code pointer2396code_start = cfs->current();2397assert(code_start != NULL, "null code start");2398cfs->guarantee_more(code_length, CHECK_NULL);2399cfs->skip_u1_fast(code_length);24002401// Exception handler table2402cfs->guarantee_more(2, CHECK_NULL); // exception_table_length2403exception_table_length = cfs->get_u2_fast();2404if (exception_table_length > 0) {2405exception_table_start = parse_exception_table(cfs,2406code_length,2407exception_table_length,2408CHECK_NULL);2409}24102411// Parse additional attributes in code attribute2412cfs->guarantee_more(2, CHECK_NULL); // code_attributes_count2413u2 code_attributes_count = cfs->get_u2_fast();24142415unsigned int calculated_attribute_length = 0;24162417calculated_attribute_length =2418sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);2419calculated_attribute_length +=2420code_length +2421sizeof(exception_table_length) +2422sizeof(code_attributes_count) +2423exception_table_length *2424( sizeof(u2) + // start_pc2425sizeof(u2) + // end_pc2426sizeof(u2) + // handler_pc2427sizeof(u2) ); // catch_type_index24282429while (code_attributes_count--) {2430cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length2431const u2 code_attribute_name_index = cfs->get_u2_fast();2432const u4 code_attribute_length = cfs->get_u4_fast();2433calculated_attribute_length += code_attribute_length +2434sizeof(code_attribute_name_index) +2435sizeof(code_attribute_length);2436check_property(valid_symbol_at(code_attribute_name_index),2437"Invalid code attribute name index %u in class file %s",2438code_attribute_name_index,2439CHECK_NULL);2440if (LoadLineNumberTables &&2441cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {2442// Parse and compress line number table2443parse_linenumber_table(code_attribute_length,2444code_length,2445&linenumber_table,2446CHECK_NULL);24472448} else if (LoadLocalVariableTables &&2449cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) {2450// Parse local variable table2451if (!lvt_allocated) {2452localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(2453THREAD, u2, INITIAL_MAX_LVT_NUMBER);2454localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(2455THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER);2456localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(2457THREAD, u2, INITIAL_MAX_LVT_NUMBER);2458localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(2459THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER);2460lvt_allocated = true;2461}2462if (lvt_cnt == max_lvt_cnt) {2463max_lvt_cnt <<= 1;2464localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt);2465localvariable_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt);2466}2467localvariable_table_start[lvt_cnt] =2468parse_localvariable_table(cfs,2469code_length,2470max_locals,2471code_attribute_length,2472&localvariable_table_length[lvt_cnt],2473false, // is not LVTT2474CHECK_NULL);2475total_lvt_length += localvariable_table_length[lvt_cnt];2476lvt_cnt++;2477} else if (LoadLocalVariableTypeTables &&2478_major_version >= JAVA_1_5_VERSION &&2479cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) {2480if (!lvt_allocated) {2481localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(2482THREAD, u2, INITIAL_MAX_LVT_NUMBER);2483localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(2484THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER);2485localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(2486THREAD, u2, INITIAL_MAX_LVT_NUMBER);2487localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(2488THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER);2489lvt_allocated = true;2490}2491// Parse local variable type table2492if (lvtt_cnt == max_lvtt_cnt) {2493max_lvtt_cnt <<= 1;2494localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt);2495localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt);2496}2497localvariable_type_table_start[lvtt_cnt] =2498parse_localvariable_table(cfs,2499code_length,2500max_locals,2501code_attribute_length,2502&localvariable_type_table_length[lvtt_cnt],2503true, // is LVTT2504CHECK_NULL);2505lvtt_cnt++;2506} else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&2507cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) {2508// Stack map is only needed by the new verifier in JDK1.5.2509if (parsed_stackmap_attribute) {2510classfile_parse_error("Multiple StackMapTable attributes in class file %s", THREAD);2511return NULL;2512}2513stackmap_data = parse_stackmap_table(cfs, code_attribute_length, _need_verify, CHECK_NULL);2514stackmap_data_length = code_attribute_length;2515parsed_stackmap_attribute = true;2516} else {2517// Skip unknown attributes2518cfs->skip_u1(code_attribute_length, CHECK_NULL);2519}2520}2521// check method attribute length2522if (_need_verify) {2523guarantee_property(method_attribute_length == calculated_attribute_length,2524"Code segment has wrong length in class file %s",2525CHECK_NULL);2526}2527} else if (method_attribute_name == vmSymbols::tag_exceptions()) {2528// Parse Exceptions attribute2529if (parsed_checked_exceptions_attribute) {2530classfile_parse_error("Multiple Exceptions attributes in class file %s",2531THREAD);2532return NULL;2533}2534parsed_checked_exceptions_attribute = true;2535checked_exceptions_start =2536parse_checked_exceptions(cfs,2537&checked_exceptions_length,2538method_attribute_length,2539CHECK_NULL);2540} else if (method_attribute_name == vmSymbols::tag_method_parameters()) {2541// reject multiple method parameters2542if (method_parameters_seen) {2543classfile_parse_error("Multiple MethodParameters attributes in class file %s",2544THREAD);2545return NULL;2546}2547method_parameters_seen = true;2548method_parameters_length = cfs->get_u1_fast();2549const u2 real_length = (method_parameters_length * 4u) + 1u;2550if (method_attribute_length != real_length) {2551classfile_parse_error(2552"Invalid MethodParameters method attribute length %u in class file",2553method_attribute_length, THREAD);2554return NULL;2555}2556method_parameters_data = cfs->current();2557cfs->skip_u2_fast(method_parameters_length);2558cfs->skip_u2_fast(method_parameters_length);2559// ignore this attribute if it cannot be reflected2560if (!vmClasses::Parameter_klass_loaded())2561method_parameters_length = -1;2562} else if (method_attribute_name == vmSymbols::tag_synthetic()) {2563if (method_attribute_length != 0) {2564classfile_parse_error(2565"Invalid Synthetic method attribute length %u in class file %s",2566method_attribute_length, THREAD);2567return NULL;2568}2569// Should we check that there hasn't already been a synthetic attribute?2570access_flags.set_is_synthetic();2571} else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 42761202572if (method_attribute_length != 0) {2573classfile_parse_error(2574"Invalid Deprecated method attribute length %u in class file %s",2575method_attribute_length, THREAD);2576return NULL;2577}2578} else if (_major_version >= JAVA_1_5_VERSION) {2579if (method_attribute_name == vmSymbols::tag_signature()) {2580if (generic_signature_index != 0) {2581classfile_parse_error(2582"Multiple Signature attributes for method in class file %s",2583THREAD);2584return NULL;2585}2586if (method_attribute_length != 2) {2587classfile_parse_error(2588"Invalid Signature attribute length %u in class file %s",2589method_attribute_length, THREAD);2590return NULL;2591}2592generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL);2593} else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {2594if (runtime_visible_annotations != NULL) {2595classfile_parse_error(2596"Multiple RuntimeVisibleAnnotations attributes for method in class file %s",2597THREAD);2598return NULL;2599}2600runtime_visible_annotations_length = method_attribute_length;2601runtime_visible_annotations = cfs->current();2602assert(runtime_visible_annotations != NULL, "null visible annotations");2603cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL);2604parse_annotations(cp,2605runtime_visible_annotations,2606runtime_visible_annotations_length,2607&parsed_annotations,2608_loader_data,2609_can_access_vm_annotations);2610cfs->skip_u1_fast(runtime_visible_annotations_length);2611} else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {2612if (runtime_invisible_annotations_exists) {2613classfile_parse_error(2614"Multiple RuntimeInvisibleAnnotations attributes for method in class file %s",2615THREAD);2616return NULL;2617}2618runtime_invisible_annotations_exists = true;2619if (PreserveAllAnnotations) {2620runtime_invisible_annotations_length = method_attribute_length;2621runtime_invisible_annotations = cfs->current();2622assert(runtime_invisible_annotations != NULL, "null invisible annotations");2623}2624cfs->skip_u1(method_attribute_length, CHECK_NULL);2625} else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {2626if (runtime_visible_parameter_annotations != NULL) {2627classfile_parse_error(2628"Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s",2629THREAD);2630return NULL;2631}2632runtime_visible_parameter_annotations_length = method_attribute_length;2633runtime_visible_parameter_annotations = cfs->current();2634assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations");2635cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_NULL);2636} else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) {2637if (runtime_invisible_parameter_annotations_exists) {2638classfile_parse_error(2639"Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s",2640THREAD);2641return NULL;2642}2643runtime_invisible_parameter_annotations_exists = true;2644if (PreserveAllAnnotations) {2645runtime_invisible_parameter_annotations_length = method_attribute_length;2646runtime_invisible_parameter_annotations = cfs->current();2647assert(runtime_invisible_parameter_annotations != NULL,2648"null invisible parameter annotations");2649}2650cfs->skip_u1(method_attribute_length, CHECK_NULL);2651} else if (method_attribute_name == vmSymbols::tag_annotation_default()) {2652if (annotation_default != NULL) {2653classfile_parse_error(2654"Multiple AnnotationDefault attributes for method in class file %s",2655THREAD);2656return NULL;2657}2658annotation_default_length = method_attribute_length;2659annotation_default = cfs->current();2660assert(annotation_default != NULL, "null annotation default");2661cfs->skip_u1(annotation_default_length, CHECK_NULL);2662} else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {2663if (runtime_visible_type_annotations != NULL) {2664classfile_parse_error(2665"Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s",2666THREAD);2667return NULL;2668}2669runtime_visible_type_annotations_length = method_attribute_length;2670runtime_visible_type_annotations = cfs->current();2671assert(runtime_visible_type_annotations != NULL, "null visible type annotations");2672// No need for the VM to parse Type annotations2673cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_NULL);2674} else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {2675if (runtime_invisible_type_annotations_exists) {2676classfile_parse_error(2677"Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s",2678THREAD);2679return NULL;2680} else {2681runtime_invisible_type_annotations_exists = true;2682}2683if (PreserveAllAnnotations) {2684runtime_invisible_type_annotations_length = method_attribute_length;2685runtime_invisible_type_annotations = cfs->current();2686assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");2687}2688cfs->skip_u1(method_attribute_length, CHECK_NULL);2689} else {2690// Skip unknown attributes2691cfs->skip_u1(method_attribute_length, CHECK_NULL);2692}2693} else {2694// Skip unknown attributes2695cfs->skip_u1(method_attribute_length, CHECK_NULL);2696}2697}26982699if (linenumber_table != NULL) {2700linenumber_table->write_terminator();2701linenumber_table_length = linenumber_table->position();2702}27032704// Make sure there's at least one Code attribute in non-native/non-abstract method2705if (_need_verify) {2706guarantee_property(access_flags.is_native() ||2707access_flags.is_abstract() ||2708parsed_code_attribute,2709"Absent Code attribute in method that is not native or abstract in class file %s",2710CHECK_NULL);2711}27122713// All sizing information for a Method* is finally available, now create it2714InlineTableSizes sizes(2715total_lvt_length,2716linenumber_table_length,2717exception_table_length,2718checked_exceptions_length,2719method_parameters_length,2720generic_signature_index,2721runtime_visible_annotations_length +2722runtime_invisible_annotations_length,2723runtime_visible_parameter_annotations_length +2724runtime_invisible_parameter_annotations_length,2725runtime_visible_type_annotations_length +2726runtime_invisible_type_annotations_length,2727annotation_default_length,27280);27292730Method* const m = Method::allocate(_loader_data,2731code_length,2732access_flags,2733&sizes,2734ConstMethod::NORMAL,2735CHECK_NULL);27362737ClassLoadingService::add_class_method_size(m->size()*wordSize);27382739// Fill in information from fixed part (access_flags already set)2740m->set_constants(_cp);2741m->set_name_index(name_index);2742m->set_signature_index(signature_index);2743m->compute_from_signature(cp->symbol_at(signature_index));2744assert(args_size < 0 || args_size == m->size_of_parameters(), "");27452746// Fill in code attribute information2747m->set_max_stack(max_stack);2748m->set_max_locals(max_locals);2749if (stackmap_data != NULL) {2750m->constMethod()->copy_stackmap_data(_loader_data,2751(u1*)stackmap_data,2752stackmap_data_length,2753CHECK_NULL);2754}27552756// Copy byte codes2757m->set_code((u1*)code_start);27582759// Copy line number table2760if (linenumber_table != NULL) {2761memcpy(m->compressed_linenumber_table(),2762linenumber_table->buffer(),2763linenumber_table_length);2764}27652766// Copy exception table2767if (exception_table_length > 0) {2768Copy::conjoint_swap_if_needed<Endian::JAVA>(exception_table_start,2769m->exception_table_start(),2770exception_table_length * sizeof(ExceptionTableElement),2771sizeof(u2));2772}27732774// Copy method parameters2775if (method_parameters_length > 0) {2776MethodParametersElement* elem = m->constMethod()->method_parameters_start();2777for (int i = 0; i < method_parameters_length; i++) {2778elem[i].name_cp_index = Bytes::get_Java_u2((address)method_parameters_data);2779method_parameters_data += 2;2780elem[i].flags = Bytes::get_Java_u2((address)method_parameters_data);2781method_parameters_data += 2;2782}2783}27842785// Copy checked exceptions2786if (checked_exceptions_length > 0) {2787Copy::conjoint_swap_if_needed<Endian::JAVA>(checked_exceptions_start,2788m->checked_exceptions_start(),2789checked_exceptions_length * sizeof(CheckedExceptionElement),2790sizeof(u2));2791}27922793// Copy class file LVT's/LVTT's into the HotSpot internal LVT.2794if (total_lvt_length > 0) {2795promoted_flags->set_has_localvariable_table();2796copy_localvariable_table(m->constMethod(),2797lvt_cnt,2798localvariable_table_length,2799localvariable_table_start,2800lvtt_cnt,2801localvariable_type_table_length,2802localvariable_type_table_start,2803CHECK_NULL);2804}28052806if (parsed_annotations.has_any_annotations())2807parsed_annotations.apply_to(methodHandle(THREAD, m));28082809if (is_hidden()) { // Mark methods in hidden classes as 'hidden'.2810m->set_hidden(true);2811}28122813// Copy annotations2814copy_method_annotations(m->constMethod(),2815runtime_visible_annotations,2816runtime_visible_annotations_length,2817runtime_invisible_annotations,2818runtime_invisible_annotations_length,2819runtime_visible_parameter_annotations,2820runtime_visible_parameter_annotations_length,2821runtime_invisible_parameter_annotations,2822runtime_invisible_parameter_annotations_length,2823runtime_visible_type_annotations,2824runtime_visible_type_annotations_length,2825runtime_invisible_type_annotations,2826runtime_invisible_type_annotations_length,2827annotation_default,2828annotation_default_length,2829CHECK_NULL);28302831if (name == vmSymbols::finalize_method_name() &&2832signature == vmSymbols::void_method_signature()) {2833if (m->is_empty_method()) {2834_has_empty_finalizer = true;2835} else {2836_has_finalizer = true;2837}2838}2839if (name == vmSymbols::object_initializer_name() &&2840signature == vmSymbols::void_method_signature() &&2841m->is_vanilla_constructor()) {2842_has_vanilla_constructor = true;2843}28442845NOT_PRODUCT(m->verify());2846return m;2847}284828492850// The promoted_flags parameter is used to pass relevant access_flags2851// from the methods back up to the containing klass. These flag values2852// are added to klass's access_flags.2853// Side-effects: populates the _methods field in the parser2854void ClassFileParser::parse_methods(const ClassFileStream* const cfs,2855bool is_interface,2856AccessFlags* promoted_flags,2857bool* has_final_method,2858bool* declares_nonstatic_concrete_methods,2859TRAPS) {2860assert(cfs != NULL, "invariant");2861assert(promoted_flags != NULL, "invariant");2862assert(has_final_method != NULL, "invariant");2863assert(declares_nonstatic_concrete_methods != NULL, "invariant");28642865assert(NULL == _methods, "invariant");28662867cfs->guarantee_more(2, CHECK); // length2868const u2 length = cfs->get_u2_fast();2869if (length == 0) {2870_methods = Universe::the_empty_method_array();2871} else {2872_methods = MetadataFactory::new_array<Method*>(_loader_data,2873length,2874NULL,2875CHECK);28762877for (int index = 0; index < length; index++) {2878Method* method = parse_method(cfs,2879is_interface,2880_cp,2881promoted_flags,2882CHECK);28832884if (method->is_final()) {2885*has_final_method = true;2886}2887// declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags2888// used for interface initialization, and default method inheritance analysis2889if (is_interface && !(*declares_nonstatic_concrete_methods)2890&& !method->is_abstract() && !method->is_static()) {2891*declares_nonstatic_concrete_methods = true;2892}2893_methods->at_put(index, method);2894}28952896if (_need_verify && length > 1) {2897// Check duplicated methods2898ResourceMark rm(THREAD);2899NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(2900THREAD, NameSigHash*, HASH_ROW_SIZE);2901initialize_hashtable(names_and_sigs);2902bool dup = false;2903const Symbol* name = NULL;2904const Symbol* sig = NULL;2905{2906debug_only(NoSafepointVerifier nsv;)2907for (int i = 0; i < length; i++) {2908const Method* const m = _methods->at(i);2909name = m->name();2910sig = m->signature();2911// If no duplicates, add name/signature in hashtable names_and_sigs.2912if (!put_after_lookup(name, sig, names_and_sigs)) {2913dup = true;2914break;2915}2916}2917}2918if (dup) {2919classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s",2920name->as_C_string(), sig->as_klass_external_name(), THREAD);2921}2922}2923}2924}29252926static const intArray* sort_methods(Array<Method*>* methods) {2927const int length = methods->length();2928// If JVMTI original method ordering or sharing is enabled we have to2929// remember the original class file ordering.2930// We temporarily use the vtable_index field in the Method* to store the2931// class file index, so we can read in after calling qsort.2932// Put the method ordering in the shared archive.2933if (JvmtiExport::can_maintain_original_method_order() || Arguments::is_dumping_archive()) {2934for (int index = 0; index < length; index++) {2935Method* const m = methods->at(index);2936assert(!m->valid_vtable_index(), "vtable index should not be set");2937m->set_vtable_index(index);2938}2939}2940// Sort method array by ascending method name (for faster lookups & vtable construction)2941// Note that the ordering is not alphabetical, see Symbol::fast_compare2942Method::sort_methods(methods);29432944intArray* method_ordering = NULL;2945// If JVMTI original method ordering or sharing is enabled construct int2946// array remembering the original ordering2947if (JvmtiExport::can_maintain_original_method_order() || Arguments::is_dumping_archive()) {2948method_ordering = new intArray(length, length, -1);2949for (int index = 0; index < length; index++) {2950Method* const m = methods->at(index);2951const int old_index = m->vtable_index();2952assert(old_index >= 0 && old_index < length, "invalid method index");2953method_ordering->at_put(index, old_index);2954m->set_vtable_index(Method::invalid_vtable_index);2955}2956}2957return method_ordering;2958}29592960// Parse generic_signature attribute for methods and fields2961u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs,2962TRAPS) {2963assert(cfs != NULL, "invariant");29642965cfs->guarantee_more(2, CHECK_0); // generic_signature_index2966const u2 generic_signature_index = cfs->get_u2_fast();2967check_property(2968valid_symbol_at(generic_signature_index),2969"Invalid Signature attribute at constant pool index %u in class file %s",2970generic_signature_index, CHECK_0);2971return generic_signature_index;2972}29732974void ClassFileParser::parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs,2975TRAPS) {29762977assert(cfs != NULL, "invariant");29782979cfs->guarantee_more(2, CHECK); // sourcefile_index2980const u2 sourcefile_index = cfs->get_u2_fast();2981check_property(2982valid_symbol_at(sourcefile_index),2983"Invalid SourceFile attribute at constant pool index %u in class file %s",2984sourcefile_index, CHECK);2985set_class_sourcefile_index(sourcefile_index);2986}29872988void ClassFileParser::parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,2989int length,2990TRAPS) {2991assert(cfs != NULL, "invariant");29922993const u1* const sde_buffer = cfs->current();2994assert(sde_buffer != NULL, "null sde buffer");29952996// Don't bother storing it if there is no way to retrieve it2997if (JvmtiExport::can_get_source_debug_extension()) {2998assert((length+1) > length, "Overflow checking");2999u1* const sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1);3000for (int i = 0; i < length; i++) {3001sde[i] = sde_buffer[i];3002}3003sde[length] = '\0';3004set_class_sde_buffer((const char*)sde, length);3005}3006// Got utf8 string, set stream position forward3007cfs->skip_u1(length, CHECK);3008}300930103011// Inner classes can be static, private or protected (classic VM does this)3012#define RECOGNIZED_INNER_CLASS_MODIFIERS ( JVM_RECOGNIZED_CLASS_MODIFIERS | \3013JVM_ACC_PRIVATE | \3014JVM_ACC_PROTECTED | \3015JVM_ACC_STATIC \3016)30173018// Find index of the InnerClasses entry for the specified inner_class_info_index.3019// Return -1 if none is found.3020static int inner_classes_find_index(const Array<u2>* inner_classes, int inner, const ConstantPool* cp, int length) {3021Symbol* cp_klass_name = cp->klass_name_at(inner);3022for (int idx = 0; idx < length; idx += InstanceKlass::inner_class_next_offset) {3023int idx_inner = inner_classes->at(idx + InstanceKlass::inner_class_inner_class_info_offset);3024if (cp->klass_name_at(idx_inner) == cp_klass_name) {3025return idx;3026}3027}3028return -1;3029}30303031// Return the outer_class_info_index for the InnerClasses entry containing the3032// specified inner_class_info_index. Return -1 if no InnerClasses entry is found.3033static int inner_classes_jump_to_outer(const Array<u2>* inner_classes, int inner, const ConstantPool* cp, int length) {3034if (inner == 0) return -1;3035int idx = inner_classes_find_index(inner_classes, inner, cp, length);3036if (idx == -1) return -1;3037int result = inner_classes->at(idx + InstanceKlass::inner_class_outer_class_info_offset);3038return result;3039}30403041// Return true if circularity is found, false if no circularity is found.3042// Use Floyd's cycle finding algorithm.3043static bool inner_classes_check_loop_through_outer(const Array<u2>* inner_classes, int idx, const ConstantPool* cp, int length) {3044int slow = inner_classes->at(idx + InstanceKlass::inner_class_inner_class_info_offset);3045int fast = inner_classes->at(idx + InstanceKlass::inner_class_outer_class_info_offset);30463047while (fast != -1 && fast != 0) {3048if (slow != 0 && (cp->klass_name_at(slow) == cp->klass_name_at(fast))) {3049return true; // found a circularity3050}3051fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length);3052if (fast == -1) return false;3053fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length);3054if (fast == -1) return false;3055slow = inner_classes_jump_to_outer(inner_classes, slow, cp, length);3056assert(slow != -1, "sanity check");3057}3058return false;3059}30603061// Loop through each InnerClasses entry checking for circularities and duplications3062// with other entries. If duplicate entries are found then throw CFE. Otherwise,3063// return true if a circularity or entries with duplicate inner_class_info_indexes3064// are found.3065bool ClassFileParser::check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS) {3066// Loop through each InnerClasses entry.3067for (int idx = 0; idx < length; idx += InstanceKlass::inner_class_next_offset) {3068// Return true if there are circular entries.3069if (inner_classes_check_loop_through_outer(_inner_classes, idx, cp, length)) {3070return true;3071}3072// Check if there are duplicate entries or entries with the same inner_class_info_index.3073for (int y = idx + InstanceKlass::inner_class_next_offset; y < length;3074y += InstanceKlass::inner_class_next_offset) {30753076// 4347400: make sure there's no duplicate entry in the classes array3077if (_major_version >= JAVA_1_5_VERSION) {3078guarantee_property((_inner_classes->at(idx) != _inner_classes->at(y) ||3079_inner_classes->at(idx+1) != _inner_classes->at(y+1) ||3080_inner_classes->at(idx+2) != _inner_classes->at(y+2) ||3081_inner_classes->at(idx+3) != _inner_classes->at(y+3)),3082"Duplicate entry in InnerClasses attribute in class file %s",3083CHECK_(true));3084}3085// Return true if there are two entries with the same inner_class_info_index.3086if (_inner_classes->at(y) == _inner_classes->at(idx)) {3087return true;3088}3089}3090}3091return false;3092}30933094// Return number of classes in the inner classes attribute table3095u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,3096const ConstantPool* cp,3097const u1* const inner_classes_attribute_start,3098bool parsed_enclosingmethod_attribute,3099u2 enclosing_method_class_index,3100u2 enclosing_method_method_index,3101TRAPS) {3102const u1* const current_mark = cfs->current();3103u2 length = 0;3104if (inner_classes_attribute_start != NULL) {3105cfs->set_current(inner_classes_attribute_start);3106cfs->guarantee_more(2, CHECK_0); // length3107length = cfs->get_u2_fast();3108}31093110// 4-tuples of shorts of inner classes data and 2 shorts of enclosing3111// method data:3112// [inner_class_info_index,3113// outer_class_info_index,3114// inner_name_index,3115// inner_class_access_flags,3116// ...3117// enclosing_method_class_index,3118// enclosing_method_method_index]3119const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);3120Array<u2>* inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3121_inner_classes = inner_classes;31223123int index = 0;3124cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u23125for (int n = 0; n < length; n++) {3126// Inner class index3127const u2 inner_class_info_index = cfs->get_u2_fast();3128check_property(3129valid_klass_reference_at(inner_class_info_index),3130"inner_class_info_index %u has bad constant type in class file %s",3131inner_class_info_index, CHECK_0);3132// Outer class index3133const u2 outer_class_info_index = cfs->get_u2_fast();3134check_property(3135outer_class_info_index == 0 ||3136valid_klass_reference_at(outer_class_info_index),3137"outer_class_info_index %u has bad constant type in class file %s",3138outer_class_info_index, CHECK_0);31393140if (outer_class_info_index != 0) {3141const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);3142char* bytes = (char*)outer_class_name->bytes();3143guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,3144"Outer class is an array class in class file %s", CHECK_0);3145}3146// Inner class name3147const u2 inner_name_index = cfs->get_u2_fast();3148check_property(3149inner_name_index == 0 || valid_symbol_at(inner_name_index),3150"inner_name_index %u has bad constant type in class file %s",3151inner_name_index, CHECK_0);3152if (_need_verify) {3153guarantee_property(inner_class_info_index != outer_class_info_index,3154"Class is both outer and inner class in class file %s", CHECK_0);3155}3156// Access flags3157jint flags;3158// JVM_ACC_MODULE is defined in JDK-9 and later.3159if (_major_version >= JAVA_9_VERSION) {3160flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);3161} else {3162flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;3163}3164if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {3165// Set abstract bit for old class files for backward compatibility3166flags |= JVM_ACC_ABSTRACT;3167}3168verify_legal_class_modifiers(flags, CHECK_0);3169AccessFlags inner_access_flags(flags);31703171inner_classes->at_put(index++, inner_class_info_index);3172inner_classes->at_put(index++, outer_class_info_index);3173inner_classes->at_put(index++, inner_name_index);3174inner_classes->at_put(index++, inner_access_flags.as_short());3175}31763177// Check for circular and duplicate entries.3178bool has_circularity = false;3179if (_need_verify) {3180has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);3181if (has_circularity) {3182// If circularity check failed then ignore InnerClasses attribute.3183MetadataFactory::free_array<u2>(_loader_data, _inner_classes);3184index = 0;3185if (parsed_enclosingmethod_attribute) {3186inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);3187_inner_classes = inner_classes;3188} else {3189_inner_classes = Universe::the_empty_short_array();3190}3191}3192}3193// Set EnclosingMethod class and method indexes.3194if (parsed_enclosingmethod_attribute) {3195inner_classes->at_put(index++, enclosing_method_class_index);3196inner_classes->at_put(index++, enclosing_method_method_index);3197}3198assert(index == size || has_circularity, "wrong size");31993200// Restore buffer's current position.3201cfs->set_current(current_mark);32023203return length;3204}32053206u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,3207const u1* const nest_members_attribute_start,3208TRAPS) {3209const u1* const current_mark = cfs->current();3210u2 length = 0;3211if (nest_members_attribute_start != NULL) {3212cfs->set_current(nest_members_attribute_start);3213cfs->guarantee_more(2, CHECK_0); // length3214length = cfs->get_u2_fast();3215}3216const int size = length;3217Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3218_nest_members = nest_members;32193220int index = 0;3221cfs->guarantee_more(2 * length, CHECK_0);3222for (int n = 0; n < length; n++) {3223const u2 class_info_index = cfs->get_u2_fast();3224check_property(3225valid_klass_reference_at(class_info_index),3226"Nest member class_info_index %u has bad constant type in class file %s",3227class_info_index, CHECK_0);3228nest_members->at_put(index++, class_info_index);3229}3230assert(index == size, "wrong size");32313232// Restore buffer's current position.3233cfs->set_current(current_mark);32343235return length;3236}32373238u2 ClassFileParser::parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs,3239const u1* const permitted_subclasses_attribute_start,3240TRAPS) {3241const u1* const current_mark = cfs->current();3242u2 length = 0;3243if (permitted_subclasses_attribute_start != NULL) {3244cfs->set_current(permitted_subclasses_attribute_start);3245cfs->guarantee_more(2, CHECK_0); // length3246length = cfs->get_u2_fast();3247}3248const int size = length;3249Array<u2>* const permitted_subclasses = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3250_permitted_subclasses = permitted_subclasses;32513252if (length > 0) {3253int index = 0;3254cfs->guarantee_more(2 * length, CHECK_0);3255for (int n = 0; n < length; n++) {3256const u2 class_info_index = cfs->get_u2_fast();3257check_property(3258valid_klass_reference_at(class_info_index),3259"Permitted subclass class_info_index %u has bad constant type in class file %s",3260class_info_index, CHECK_0);3261permitted_subclasses->at_put(index++, class_info_index);3262}3263assert(index == size, "wrong size");3264}32653266// Restore buffer's current position.3267cfs->set_current(current_mark);32683269return length;3270}32713272// Record {3273// u2 attribute_name_index;3274// u4 attribute_length;3275// u2 components_count;3276// component_info components[components_count];3277// }3278// component_info {3279// u2 name_index;3280// u2 descriptor_index3281// u2 attributes_count;3282// attribute_info_attributes[attributes_count];3283// }3284u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,3285const ConstantPool* cp,3286const u1* const record_attribute_start,3287TRAPS) {3288const u1* const current_mark = cfs->current();3289int components_count = 0;3290unsigned int calculate_attr_size = 0;3291if (record_attribute_start != NULL) {3292cfs->set_current(record_attribute_start);3293cfs->guarantee_more(2, CHECK_0); // num of components3294components_count = (int)cfs->get_u2_fast();3295calculate_attr_size = 2;3296}32973298Array<RecordComponent*>* const record_components =3299MetadataFactory::new_array<RecordComponent*>(_loader_data, components_count, NULL, CHECK_0);3300_record_components = record_components;33013302for (int x = 0; x < components_count; x++) {3303cfs->guarantee_more(6, CHECK_0); // name_index, descriptor_index, attributes_count33043305const u2 name_index = cfs->get_u2_fast();3306check_property(valid_symbol_at(name_index),3307"Invalid constant pool index %u for name in Record attribute in class file %s",3308name_index, CHECK_0);3309const Symbol* const name = cp->symbol_at(name_index);3310verify_legal_field_name(name, CHECK_0);33113312const u2 descriptor_index = cfs->get_u2_fast();3313check_property(valid_symbol_at(descriptor_index),3314"Invalid constant pool index %u for descriptor in Record attribute in class file %s",3315descriptor_index, CHECK_0);3316const Symbol* const descr = cp->symbol_at(descriptor_index);3317verify_legal_field_signature(name, descr, CHECK_0);33183319const u2 attributes_count = cfs->get_u2_fast();3320calculate_attr_size += 6;3321u2 generic_sig_index = 0;3322const u1* runtime_visible_annotations = NULL;3323int runtime_visible_annotations_length = 0;3324const u1* runtime_invisible_annotations = NULL;3325int runtime_invisible_annotations_length = 0;3326bool runtime_invisible_annotations_exists = false;3327const u1* runtime_visible_type_annotations = NULL;3328int runtime_visible_type_annotations_length = 0;3329const u1* runtime_invisible_type_annotations = NULL;3330int runtime_invisible_type_annotations_length = 0;3331bool runtime_invisible_type_annotations_exists = false;33323333// Expected attributes for record components are Signature, Runtime(In)VisibleAnnotations,3334// and Runtime(In)VisibleTypeAnnotations. Other attributes are ignored.3335for (int y = 0; y < attributes_count; y++) {3336cfs->guarantee_more(6, CHECK_0); // attribute_name_index, attribute_length3337const u2 attribute_name_index = cfs->get_u2_fast();3338const u4 attribute_length = cfs->get_u4_fast();3339calculate_attr_size += 6;3340check_property(3341valid_symbol_at(attribute_name_index),3342"Invalid Record attribute name index %u in class file %s",3343attribute_name_index, CHECK_0);33443345const Symbol* const attribute_name = cp->symbol_at(attribute_name_index);3346if (attribute_name == vmSymbols::tag_signature()) {3347if (generic_sig_index != 0) {3348classfile_parse_error(3349"Multiple Signature attributes for Record component in class file %s",3350THREAD);3351return 0;3352}3353if (attribute_length != 2) {3354classfile_parse_error(3355"Invalid Signature attribute length %u in Record component in class file %s",3356attribute_length, THREAD);3357return 0;3358}3359generic_sig_index = parse_generic_signature_attribute(cfs, CHECK_0);33603361} else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {3362if (runtime_visible_annotations != NULL) {3363classfile_parse_error(3364"Multiple RuntimeVisibleAnnotations attributes for Record component in class file %s", THREAD);3365return 0;3366}3367runtime_visible_annotations_length = attribute_length;3368runtime_visible_annotations = cfs->current();33693370assert(runtime_visible_annotations != NULL, "null record component visible annotation");3371cfs->guarantee_more(runtime_visible_annotations_length, CHECK_0);3372cfs->skip_u1_fast(runtime_visible_annotations_length);33733374} else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {3375if (runtime_invisible_annotations_exists) {3376classfile_parse_error(3377"Multiple RuntimeInvisibleAnnotations attributes for Record component in class file %s", THREAD);3378return 0;3379}3380runtime_invisible_annotations_exists = true;3381if (PreserveAllAnnotations) {3382runtime_invisible_annotations_length = attribute_length;3383runtime_invisible_annotations = cfs->current();3384assert(runtime_invisible_annotations != NULL, "null record component invisible annotation");3385}3386cfs->skip_u1(attribute_length, CHECK_0);33873388} else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {3389if (runtime_visible_type_annotations != NULL) {3390classfile_parse_error(3391"Multiple RuntimeVisibleTypeAnnotations attributes for Record component in class file %s", THREAD);3392return 0;3393}3394runtime_visible_type_annotations_length = attribute_length;3395runtime_visible_type_annotations = cfs->current();33963397assert(runtime_visible_type_annotations != NULL, "null record component visible type annotation");3398cfs->guarantee_more(runtime_visible_type_annotations_length, CHECK_0);3399cfs->skip_u1_fast(runtime_visible_type_annotations_length);34003401} else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {3402if (runtime_invisible_type_annotations_exists) {3403classfile_parse_error(3404"Multiple RuntimeInvisibleTypeAnnotations attributes for Record component in class file %s", THREAD);3405return 0;3406}3407runtime_invisible_type_annotations_exists = true;3408if (PreserveAllAnnotations) {3409runtime_invisible_type_annotations_length = attribute_length;3410runtime_invisible_type_annotations = cfs->current();3411assert(runtime_invisible_type_annotations != NULL, "null record component invisible type annotation");3412}3413cfs->skip_u1(attribute_length, CHECK_0);34143415} else {3416// Skip unknown attributes3417cfs->skip_u1(attribute_length, CHECK_0);3418}3419calculate_attr_size += attribute_length;3420} // End of attributes For loop34213422AnnotationArray* annotations = assemble_annotations(runtime_visible_annotations,3423runtime_visible_annotations_length,3424runtime_invisible_annotations,3425runtime_invisible_annotations_length,3426CHECK_0);3427AnnotationArray* type_annotations = assemble_annotations(runtime_visible_type_annotations,3428runtime_visible_type_annotations_length,3429runtime_invisible_type_annotations,3430runtime_invisible_type_annotations_length,3431CHECK_0);34323433RecordComponent* record_component =3434RecordComponent::allocate(_loader_data, name_index, descriptor_index,3435attributes_count, generic_sig_index,3436annotations, type_annotations, CHECK_0);3437record_components->at_put(x, record_component);3438} // End of component processing loop34393440// Restore buffer's current position.3441cfs->set_current(current_mark);3442return calculate_attr_size;3443}34443445void ClassFileParser::parse_classfile_synthetic_attribute() {3446set_class_synthetic_flag(true);3447}34483449void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {3450assert(cfs != NULL, "invariant");34513452const u2 signature_index = cfs->get_u2(CHECK);3453check_property(3454valid_symbol_at(signature_index),3455"Invalid constant pool index %u in Signature attribute in class file %s",3456signature_index, CHECK);3457set_class_generic_signature_index(signature_index);3458}34593460void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,3461ConstantPool* cp,3462u4 attribute_byte_length,3463TRAPS) {3464assert(cfs != NULL, "invariant");3465assert(cp != NULL, "invariant");34663467const u1* const current_start = cfs->current();34683469guarantee_property(attribute_byte_length >= sizeof(u2),3470"Invalid BootstrapMethods attribute length %u in class file %s",3471attribute_byte_length,3472CHECK);34733474cfs->guarantee_more(attribute_byte_length, CHECK);34753476const int attribute_array_length = cfs->get_u2_fast();34773478guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,3479"Short length on BootstrapMethods in class file %s",3480CHECK);348134823483// The attribute contains a counted array of counted tuples of shorts,3484// represending bootstrap specifiers:3485// length*{bootstrap_method_index, argument_count*{argument_index}}3486const int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2);3487// operand_count = number of shorts in attr, except for leading length34883489// The attribute is copied into a short[] array.3490// The array begins with a series of short[2] pairs, one for each tuple.3491const int index_size = (attribute_array_length * 2);34923493Array<u2>* const operands =3494MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK);34953496// Eagerly assign operands so they will be deallocated with the constant3497// pool if there is an error.3498cp->set_operands(operands);34993500int operand_fill_index = index_size;3501const int cp_size = cp->length();35023503for (int n = 0; n < attribute_array_length; n++) {3504// Store a 32-bit offset into the header of the operand array.3505ConstantPool::operand_offset_at_put(operands, n, operand_fill_index);35063507// Read a bootstrap specifier.3508cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc3509const u2 bootstrap_method_index = cfs->get_u2_fast();3510const u2 argument_count = cfs->get_u2_fast();3511check_property(3512valid_cp_range(bootstrap_method_index, cp_size) &&3513cp->tag_at(bootstrap_method_index).is_method_handle(),3514"bootstrap_method_index %u has bad constant type in class file %s",3515bootstrap_method_index,3516CHECK);35173518guarantee_property((operand_fill_index + 1 + argument_count) < operands->length(),3519"Invalid BootstrapMethods num_bootstrap_methods or num_bootstrap_arguments value in class file %s",3520CHECK);35213522operands->at_put(operand_fill_index++, bootstrap_method_index);3523operands->at_put(operand_fill_index++, argument_count);35243525cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]3526for (int j = 0; j < argument_count; j++) {3527const u2 argument_index = cfs->get_u2_fast();3528check_property(3529valid_cp_range(argument_index, cp_size) &&3530cp->tag_at(argument_index).is_loadable_constant(),3531"argument_index %u has bad constant type in class file %s",3532argument_index,3533CHECK);3534operands->at_put(operand_fill_index++, argument_index);3535}3536}3537guarantee_property(current_start + attribute_byte_length == cfs->current(),3538"Bad length on BootstrapMethods in class file %s",3539CHECK);3540}35413542void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,3543ConstantPool* cp,3544ClassFileParser::ClassAnnotationCollector* parsed_annotations,3545TRAPS) {3546assert(cfs != NULL, "invariant");3547assert(cp != NULL, "invariant");3548assert(parsed_annotations != NULL, "invariant");35493550// Set inner classes attribute to default sentinel3551_inner_classes = Universe::the_empty_short_array();3552// Set nest members attribute to default sentinel3553_nest_members = Universe::the_empty_short_array();3554// Set _permitted_subclasses attribute to default sentinel3555_permitted_subclasses = Universe::the_empty_short_array();3556cfs->guarantee_more(2, CHECK); // attributes_count3557u2 attributes_count = cfs->get_u2_fast();3558bool parsed_sourcefile_attribute = false;3559bool parsed_innerclasses_attribute = false;3560bool parsed_nest_members_attribute = false;3561bool parsed_permitted_subclasses_attribute = false;3562bool parsed_nest_host_attribute = false;3563bool parsed_record_attribute = false;3564bool parsed_enclosingmethod_attribute = false;3565bool parsed_bootstrap_methods_attribute = false;3566const u1* runtime_visible_annotations = NULL;3567int runtime_visible_annotations_length = 0;3568const u1* runtime_invisible_annotations = NULL;3569int runtime_invisible_annotations_length = 0;3570const u1* runtime_visible_type_annotations = NULL;3571int runtime_visible_type_annotations_length = 0;3572const u1* runtime_invisible_type_annotations = NULL;3573int runtime_invisible_type_annotations_length = 0;3574bool runtime_invisible_type_annotations_exists = false;3575bool runtime_invisible_annotations_exists = false;3576bool parsed_source_debug_ext_annotations_exist = false;3577const u1* inner_classes_attribute_start = NULL;3578u4 inner_classes_attribute_length = 0;3579u2 enclosing_method_class_index = 0;3580u2 enclosing_method_method_index = 0;3581const u1* nest_members_attribute_start = NULL;3582u4 nest_members_attribute_length = 0;3583const u1* record_attribute_start = NULL;3584u4 record_attribute_length = 0;3585const u1* permitted_subclasses_attribute_start = NULL;3586u4 permitted_subclasses_attribute_length = 0;35873588// Iterate over attributes3589while (attributes_count--) {3590cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length3591const u2 attribute_name_index = cfs->get_u2_fast();3592const u4 attribute_length = cfs->get_u4_fast();3593check_property(3594valid_symbol_at(attribute_name_index),3595"Attribute name has bad constant pool index %u in class file %s",3596attribute_name_index, CHECK);3597const Symbol* const tag = cp->symbol_at(attribute_name_index);3598if (tag == vmSymbols::tag_source_file()) {3599// Check for SourceFile tag3600if (_need_verify) {3601guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);3602}3603if (parsed_sourcefile_attribute) {3604classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);3605return;3606} else {3607parsed_sourcefile_attribute = true;3608}3609parse_classfile_sourcefile_attribute(cfs, CHECK);3610} else if (tag == vmSymbols::tag_source_debug_extension()) {3611// Check for SourceDebugExtension tag3612if (parsed_source_debug_ext_annotations_exist) {3613classfile_parse_error(3614"Multiple SourceDebugExtension attributes in class file %s", THREAD);3615return;3616}3617parsed_source_debug_ext_annotations_exist = true;3618parse_classfile_source_debug_extension_attribute(cfs, (int)attribute_length, CHECK);3619} else if (tag == vmSymbols::tag_inner_classes()) {3620// Check for InnerClasses tag3621if (parsed_innerclasses_attribute) {3622classfile_parse_error("Multiple InnerClasses attributes in class file %s", THREAD);3623return;3624} else {3625parsed_innerclasses_attribute = true;3626}3627inner_classes_attribute_start = cfs->current();3628inner_classes_attribute_length = attribute_length;3629cfs->skip_u1(inner_classes_attribute_length, CHECK);3630} else if (tag == vmSymbols::tag_synthetic()) {3631// Check for Synthetic tag3632// Shouldn't we check that the synthetic flags wasn't already set? - not required in spec3633if (attribute_length != 0) {3634classfile_parse_error(3635"Invalid Synthetic classfile attribute length %u in class file %s",3636attribute_length, THREAD);3637return;3638}3639parse_classfile_synthetic_attribute();3640} else if (tag == vmSymbols::tag_deprecated()) {3641// Check for Deprecated tag - 42761203642if (attribute_length != 0) {3643classfile_parse_error(3644"Invalid Deprecated classfile attribute length %u in class file %s",3645attribute_length, THREAD);3646return;3647}3648} else if (_major_version >= JAVA_1_5_VERSION) {3649if (tag == vmSymbols::tag_signature()) {3650if (_generic_signature_index != 0) {3651classfile_parse_error(3652"Multiple Signature attributes in class file %s", THREAD);3653return;3654}3655if (attribute_length != 2) {3656classfile_parse_error(3657"Wrong Signature attribute length %u in class file %s",3658attribute_length, THREAD);3659return;3660}3661parse_classfile_signature_attribute(cfs, CHECK);3662} else if (tag == vmSymbols::tag_runtime_visible_annotations()) {3663if (runtime_visible_annotations != NULL) {3664classfile_parse_error(3665"Multiple RuntimeVisibleAnnotations attributes in class file %s", THREAD);3666return;3667}3668runtime_visible_annotations_length = attribute_length;3669runtime_visible_annotations = cfs->current();3670assert(runtime_visible_annotations != NULL, "null visible annotations");3671cfs->guarantee_more(runtime_visible_annotations_length, CHECK);3672parse_annotations(cp,3673runtime_visible_annotations,3674runtime_visible_annotations_length,3675parsed_annotations,3676_loader_data,3677_can_access_vm_annotations);3678cfs->skip_u1_fast(runtime_visible_annotations_length);3679} else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {3680if (runtime_invisible_annotations_exists) {3681classfile_parse_error(3682"Multiple RuntimeInvisibleAnnotations attributes in class file %s", THREAD);3683return;3684}3685runtime_invisible_annotations_exists = true;3686if (PreserveAllAnnotations) {3687runtime_invisible_annotations_length = attribute_length;3688runtime_invisible_annotations = cfs->current();3689assert(runtime_invisible_annotations != NULL, "null invisible annotations");3690}3691cfs->skip_u1(attribute_length, CHECK);3692} else if (tag == vmSymbols::tag_enclosing_method()) {3693if (parsed_enclosingmethod_attribute) {3694classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", THREAD);3695return;3696} else {3697parsed_enclosingmethod_attribute = true;3698}3699guarantee_property(attribute_length == 4,3700"Wrong EnclosingMethod attribute length %u in class file %s",3701attribute_length, CHECK);3702cfs->guarantee_more(4, CHECK); // class_index, method_index3703enclosing_method_class_index = cfs->get_u2_fast();3704enclosing_method_method_index = cfs->get_u2_fast();3705if (enclosing_method_class_index == 0) {3706classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", THREAD);3707return;3708}3709// Validate the constant pool indices and types3710check_property(valid_klass_reference_at(enclosing_method_class_index),3711"Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);3712if (enclosing_method_method_index != 0 &&3713(!cp->is_within_bounds(enclosing_method_method_index) ||3714!cp->tag_at(enclosing_method_method_index).is_name_and_type())) {3715classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", THREAD);3716return;3717}3718} else if (tag == vmSymbols::tag_bootstrap_methods() &&3719_major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {3720if (parsed_bootstrap_methods_attribute) {3721classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", THREAD);3722return;3723}3724parsed_bootstrap_methods_attribute = true;3725parse_classfile_bootstrap_methods_attribute(cfs, cp, attribute_length, CHECK);3726} else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) {3727if (runtime_visible_type_annotations != NULL) {3728classfile_parse_error(3729"Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", THREAD);3730return;3731}3732runtime_visible_type_annotations_length = attribute_length;3733runtime_visible_type_annotations = cfs->current();3734assert(runtime_visible_type_annotations != NULL, "null visible type annotations");3735// No need for the VM to parse Type annotations3736cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);3737} else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {3738if (runtime_invisible_type_annotations_exists) {3739classfile_parse_error(3740"Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", THREAD);3741return;3742} else {3743runtime_invisible_type_annotations_exists = true;3744}3745if (PreserveAllAnnotations) {3746runtime_invisible_type_annotations_length = attribute_length;3747runtime_invisible_type_annotations = cfs->current();3748assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");3749}3750cfs->skip_u1(attribute_length, CHECK);3751} else if (_major_version >= JAVA_11_VERSION) {3752if (tag == vmSymbols::tag_nest_members()) {3753// Check for NestMembers tag3754if (parsed_nest_members_attribute) {3755classfile_parse_error("Multiple NestMembers attributes in class file %s", THREAD);3756return;3757} else {3758parsed_nest_members_attribute = true;3759}3760if (parsed_nest_host_attribute) {3761classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", THREAD);3762return;3763}3764nest_members_attribute_start = cfs->current();3765nest_members_attribute_length = attribute_length;3766cfs->skip_u1(nest_members_attribute_length, CHECK);3767} else if (tag == vmSymbols::tag_nest_host()) {3768if (parsed_nest_host_attribute) {3769classfile_parse_error("Multiple NestHost attributes in class file %s", THREAD);3770return;3771} else {3772parsed_nest_host_attribute = true;3773}3774if (parsed_nest_members_attribute) {3775classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", THREAD);3776return;3777}3778if (_need_verify) {3779guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);3780}3781cfs->guarantee_more(2, CHECK);3782u2 class_info_index = cfs->get_u2_fast();3783check_property(3784valid_klass_reference_at(class_info_index),3785"Nest-host class_info_index %u has bad constant type in class file %s",3786class_info_index, CHECK);3787_nest_host = class_info_index;37883789} else if (_major_version >= JAVA_16_VERSION) {3790if (tag == vmSymbols::tag_record()) {3791if (parsed_record_attribute) {3792classfile_parse_error("Multiple Record attributes in class file %s", THREAD);3793return;3794}3795parsed_record_attribute = true;3796record_attribute_start = cfs->current();3797record_attribute_length = attribute_length;3798} else if (_major_version >= JAVA_17_VERSION) {3799if (tag == vmSymbols::tag_permitted_subclasses()) {3800if (parsed_permitted_subclasses_attribute) {3801classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);3802return;3803}3804// Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.3805if (_access_flags.is_final()) {3806classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);3807return;3808}3809parsed_permitted_subclasses_attribute = true;3810permitted_subclasses_attribute_start = cfs->current();3811permitted_subclasses_attribute_length = attribute_length;3812}3813}3814// Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION3815cfs->skip_u1(attribute_length, CHECK);3816} else {3817// Unknown attribute3818cfs->skip_u1(attribute_length, CHECK);3819}3820} else {3821// Unknown attribute3822cfs->skip_u1(attribute_length, CHECK);3823}3824} else {3825// Unknown attribute3826cfs->skip_u1(attribute_length, CHECK);3827}3828}3829_class_annotations = assemble_annotations(runtime_visible_annotations,3830runtime_visible_annotations_length,3831runtime_invisible_annotations,3832runtime_invisible_annotations_length,3833CHECK);3834_class_type_annotations = assemble_annotations(runtime_visible_type_annotations,3835runtime_visible_type_annotations_length,3836runtime_invisible_type_annotations,3837runtime_invisible_type_annotations_length,3838CHECK);38393840if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {3841const u2 num_of_classes = parse_classfile_inner_classes_attribute(3842cfs,3843cp,3844inner_classes_attribute_start,3845parsed_innerclasses_attribute,3846enclosing_method_class_index,3847enclosing_method_method_index,3848CHECK);3849if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {3850guarantee_property(3851inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,3852"Wrong InnerClasses attribute length in class file %s", CHECK);3853}3854}38553856if (parsed_nest_members_attribute) {3857const u2 num_of_classes = parse_classfile_nest_members_attribute(3858cfs,3859nest_members_attribute_start,3860CHECK);3861if (_need_verify) {3862guarantee_property(3863nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,3864"Wrong NestMembers attribute length in class file %s", CHECK);3865}3866}38673868if (parsed_record_attribute) {3869const unsigned int calculated_attr_length = parse_classfile_record_attribute(3870cfs,3871cp,3872record_attribute_start,3873CHECK);3874if (_need_verify) {3875guarantee_property(record_attribute_length == calculated_attr_length,3876"Record attribute has wrong length in class file %s",3877CHECK);3878}3879}38803881if (parsed_permitted_subclasses_attribute) {3882const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(3883cfs,3884permitted_subclasses_attribute_start,3885CHECK);3886if (_need_verify) {3887guarantee_property(3888permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,3889"Wrong PermittedSubclasses attribute length in class file %s", CHECK);3890}3891}38923893if (_max_bootstrap_specifier_index >= 0) {3894guarantee_property(parsed_bootstrap_methods_attribute,3895"Missing BootstrapMethods attribute in class file %s", CHECK);3896}3897}38983899void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {3900assert(k != NULL, "invariant");39013902if (_synthetic_flag)3903k->set_is_synthetic();3904if (_sourcefile_index != 0) {3905k->set_source_file_name_index(_sourcefile_index);3906}3907if (_generic_signature_index != 0) {3908k->set_generic_signature_index(_generic_signature_index);3909}3910if (_sde_buffer != NULL) {3911k->set_source_debug_extension(_sde_buffer, _sde_length);3912}3913}39143915// Create the Annotations object that will3916// hold the annotations array for the Klass.3917void ClassFileParser::create_combined_annotations(TRAPS) {3918if (_class_annotations == NULL &&3919_class_type_annotations == NULL &&3920_fields_annotations == NULL &&3921_fields_type_annotations == NULL) {3922// Don't create the Annotations object unnecessarily.3923return;3924}39253926Annotations* const annotations = Annotations::allocate(_loader_data, CHECK);3927annotations->set_class_annotations(_class_annotations);3928annotations->set_class_type_annotations(_class_type_annotations);3929annotations->set_fields_annotations(_fields_annotations);3930annotations->set_fields_type_annotations(_fields_type_annotations);39313932// This is the Annotations object that will be3933// assigned to InstanceKlass being constructed.3934_combined_annotations = annotations;39353936// The annotations arrays below has been transfered the3937// _combined_annotations so these fields can now be cleared.3938_class_annotations = NULL;3939_class_type_annotations = NULL;3940_fields_annotations = NULL;3941_fields_type_annotations = NULL;3942}39433944// Transfer ownership of metadata allocated to the InstanceKlass.3945void ClassFileParser::apply_parsed_class_metadata(3946InstanceKlass* this_klass,3947int java_fields_count) {3948assert(this_klass != NULL, "invariant");39493950_cp->set_pool_holder(this_klass);3951this_klass->set_constants(_cp);3952this_klass->set_fields(_fields, java_fields_count);3953this_klass->set_methods(_methods);3954this_klass->set_inner_classes(_inner_classes);3955this_klass->set_nest_members(_nest_members);3956this_klass->set_nest_host_index(_nest_host);3957this_klass->set_annotations(_combined_annotations);3958this_klass->set_permitted_subclasses(_permitted_subclasses);3959this_klass->set_record_components(_record_components);3960// Delay the setting of _local_interfaces and _transitive_interfaces until after3961// initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could3962// be shared with _transitive_interfaces and _transitive_interfaces may be shared with3963// its _super. If an OOM occurs while loading the current klass, its _super field3964// may not have been set. When GC tries to free the klass, the _transitive_interfaces3965// may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent3966// dereferences to the deallocated _transitive_interfaces will result in a crash.39673968// Clear out these fields so they don't get deallocated by the destructor3969clear_class_metadata();3970}39713972AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,3973int runtime_visible_annotations_length,3974const u1* const runtime_invisible_annotations,3975int runtime_invisible_annotations_length,3976TRAPS) {3977AnnotationArray* annotations = NULL;3978if (runtime_visible_annotations != NULL ||3979runtime_invisible_annotations != NULL) {3980annotations = MetadataFactory::new_array<u1>(_loader_data,3981runtime_visible_annotations_length +3982runtime_invisible_annotations_length,3983CHECK_(annotations));3984if (runtime_visible_annotations != NULL) {3985for (int i = 0; i < runtime_visible_annotations_length; i++) {3986annotations->at_put(i, runtime_visible_annotations[i]);3987}3988}3989if (runtime_invisible_annotations != NULL) {3990for (int i = 0; i < runtime_invisible_annotations_length; i++) {3991int append = runtime_visible_annotations_length+i;3992annotations->at_put(append, runtime_invisible_annotations[i]);3993}3994}3995}3996return annotations;3997}39983999const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,4000const int super_class_index,4001const bool need_verify,4002TRAPS) {4003assert(cp != NULL, "invariant");4004const InstanceKlass* super_klass = NULL;40054006if (super_class_index == 0) {4007check_property(_class_name == vmSymbols::java_lang_Object(),4008"Invalid superclass index %u in class file %s",4009super_class_index,4010CHECK_NULL);4011} else {4012check_property(valid_klass_reference_at(super_class_index),4013"Invalid superclass index %u in class file %s",4014super_class_index,4015CHECK_NULL);4016// The class name should be legal because it is checked when parsing constant pool.4017// However, make sure it is not an array type.4018bool is_array = false;4019if (cp->tag_at(super_class_index).is_klass()) {4020super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));4021if (need_verify)4022is_array = super_klass->is_array_klass();4023} else if (need_verify) {4024is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);4025}4026if (need_verify) {4027guarantee_property(!is_array,4028"Bad superclass name in class file %s", CHECK_NULL);4029}4030}4031return super_klass;4032}40334034OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {4035_max_nonstatic_oop_maps = max_blocks;4036_nonstatic_oop_map_count = 0;4037if (max_blocks == 0) {4038_nonstatic_oop_maps = NULL;4039} else {4040_nonstatic_oop_maps =4041NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);4042memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);4043}4044}40454046OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {4047assert(_nonstatic_oop_map_count > 0, "Has no oop maps");4048return _nonstatic_oop_maps + (_nonstatic_oop_map_count - 1);4049}40504051// addition of super oop maps4052void OopMapBlocksBuilder::initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {4053assert(nof_blocks && _nonstatic_oop_map_count == 0 &&4054nof_blocks <= _max_nonstatic_oop_maps, "invariant");40554056memcpy(_nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);4057_nonstatic_oop_map_count += nof_blocks;4058}40594060// collection of oops4061void OopMapBlocksBuilder::add(int offset, int count) {4062if (_nonstatic_oop_map_count == 0) {4063_nonstatic_oop_map_count++;4064}4065OopMapBlock* nonstatic_oop_map = last_oop_map();4066if (nonstatic_oop_map->count() == 0) { // Unused map, set it up4067nonstatic_oop_map->set_offset(offset);4068nonstatic_oop_map->set_count(count);4069} else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add4070nonstatic_oop_map->increment_count(count);4071} else { // Need a new one...4072_nonstatic_oop_map_count++;4073assert(_nonstatic_oop_map_count <= _max_nonstatic_oop_maps, "range check");4074nonstatic_oop_map = last_oop_map();4075nonstatic_oop_map->set_offset(offset);4076nonstatic_oop_map->set_count(count);4077}4078}40794080// general purpose copy, e.g. into allocated instanceKlass4081void OopMapBlocksBuilder::copy(OopMapBlock* dst) {4082if (_nonstatic_oop_map_count != 0) {4083memcpy(dst, _nonstatic_oop_maps, sizeof(OopMapBlock) * _nonstatic_oop_map_count);4084}4085}40864087// Sort and compact adjacent blocks4088void OopMapBlocksBuilder::compact() {4089if (_nonstatic_oop_map_count <= 1) {4090return;4091}4092/*4093* Since field layout sneeks in oops before values, we will be able to condense4094* blocks. There is potential to compact between super, own refs and values4095* containing refs.4096*4097* Currently compaction is slightly limited due to values being 8 byte aligned.4098* This may well change: FixMe if it doesn't, the code below is fairly general purpose4099* and maybe it doesn't need to be.4100*/4101qsort(_nonstatic_oop_maps, _nonstatic_oop_map_count, sizeof(OopMapBlock),4102(_sort_Fn)OopMapBlock::compare_offset);4103if (_nonstatic_oop_map_count < 2) {4104return;4105}41064107// Make a temp copy, and iterate through and copy back into the original4108ResourceMark rm;4109OopMapBlock* oop_maps_copy =4110NEW_RESOURCE_ARRAY(OopMapBlock, _nonstatic_oop_map_count);4111OopMapBlock* oop_maps_copy_end = oop_maps_copy + _nonstatic_oop_map_count;4112copy(oop_maps_copy);4113OopMapBlock* nonstatic_oop_map = _nonstatic_oop_maps;4114unsigned int new_count = 1;4115oop_maps_copy++;4116while(oop_maps_copy < oop_maps_copy_end) {4117assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");4118if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {4119nonstatic_oop_map->increment_count(oop_maps_copy->count());4120} else {4121nonstatic_oop_map++;4122new_count++;4123nonstatic_oop_map->set_offset(oop_maps_copy->offset());4124nonstatic_oop_map->set_count(oop_maps_copy->count());4125}4126oop_maps_copy++;4127}4128assert(new_count <= _nonstatic_oop_map_count, "end up with more maps after compact() ?");4129_nonstatic_oop_map_count = new_count;4130}41314132void OopMapBlocksBuilder::print_on(outputStream* st) const {4133st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);4134if (_nonstatic_oop_map_count > 0) {4135OopMapBlock* map = _nonstatic_oop_maps;4136OopMapBlock* last_map = last_oop_map();4137assert(map <= last_map, "Last less than first");4138while (map <= last_map) {4139st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),4140map->offset() + map->offset_span() - heapOopSize, map->count());4141map++;4142}4143}4144}41454146void OopMapBlocksBuilder::print_value_on(outputStream* st) const {4147print_on(st);4148}41494150void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {4151assert(ik != NULL, "invariant");41524153const Klass* const super = ik->super();41544155// Check if this klass has an empty finalize method (i.e. one with return bytecode only),4156// in which case we don't have to register objects as finalizable4157if (!_has_empty_finalizer) {4158if (_has_finalizer ||4159(super != NULL && super->has_finalizer())) {4160ik->set_has_finalizer();4161}4162}41634164#ifdef ASSERT4165bool f = false;4166const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),4167vmSymbols::void_method_signature());4168if (m != NULL && !m->is_empty_method()) {4169f = true;4170}41714172// Spec doesn't prevent agent from redefinition of empty finalizer.4173// Despite the fact that it's generally bad idea and redefined finalizer4174// will not work as expected we shouldn't abort vm in this case4175if (!ik->has_redefined_this_or_super()) {4176assert(ik->has_finalizer() == f, "inconsistent has_finalizer");4177}4178#endif41794180// Check if this klass supports the java.lang.Cloneable interface4181if (vmClasses::Cloneable_klass_loaded()) {4182if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {4183ik->set_is_cloneable();4184}4185}41864187// Check if this klass has a vanilla default constructor4188if (super == NULL) {4189// java.lang.Object has empty default constructor4190ik->set_has_vanilla_constructor();4191} else {4192if (super->has_vanilla_constructor() &&4193_has_vanilla_constructor) {4194ik->set_has_vanilla_constructor();4195}4196#ifdef ASSERT4197bool v = false;4198if (super->has_vanilla_constructor()) {4199const Method* const constructor =4200ik->find_method(vmSymbols::object_initializer_name(),4201vmSymbols::void_method_signature());4202if (constructor != NULL && constructor->is_vanilla_constructor()) {4203v = true;4204}4205}4206assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");4207#endif4208}42094210// If it cannot be fast-path allocated, set a bit in the layout helper.4211// See documentation of InstanceKlass::can_be_fastpath_allocated().4212assert(ik->size_helper() > 0, "layout_helper is initialized");4213if ((!RegisterFinalizersAtInit && ik->has_finalizer())4214|| ik->is_abstract() || ik->is_interface()4215|| (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)4216|| ik->size_helper() >= FastAllocateSizeLimit) {4217// Forbid fast-path allocation.4218const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);4219ik->set_layout_helper(lh);4220}4221}42224223// utility methods for appending an array with check for duplicates42244225static void append_interfaces(GrowableArray<InstanceKlass*>* result,4226const Array<InstanceKlass*>* const ifs) {4227// iterate over new interfaces4228for (int i = 0; i < ifs->length(); i++) {4229InstanceKlass* const e = ifs->at(i);4230assert(e->is_klass() && e->is_interface(), "just checking");4231// add new interface4232result->append_if_missing(e);4233}4234}42354236static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,4237Array<InstanceKlass*>* local_ifs,4238ClassLoaderData* loader_data,4239TRAPS) {4240assert(local_ifs != NULL, "invariant");4241assert(loader_data != NULL, "invariant");42424243// Compute maximum size for transitive interfaces4244int max_transitive_size = 0;4245int super_size = 0;4246// Add superclass transitive interfaces size4247if (super != NULL) {4248super_size = super->transitive_interfaces()->length();4249max_transitive_size += super_size;4250}4251// Add local interfaces' super interfaces4252const int local_size = local_ifs->length();4253for (int i = 0; i < local_size; i++) {4254InstanceKlass* const l = local_ifs->at(i);4255max_transitive_size += l->transitive_interfaces()->length();4256}4257// Finally add local interfaces4258max_transitive_size += local_size;4259// Construct array4260if (max_transitive_size == 0) {4261// no interfaces, use canonicalized array4262return Universe::the_empty_instance_klass_array();4263} else if (max_transitive_size == super_size) {4264// no new local interfaces added, share superklass' transitive interface array4265return super->transitive_interfaces();4266} else if (max_transitive_size == local_size) {4267// only local interfaces added, share local interface array4268return local_ifs;4269} else {4270ResourceMark rm;4271GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);42724273// Copy down from superclass4274if (super != NULL) {4275append_interfaces(result, super->transitive_interfaces());4276}42774278// Copy down from local interfaces' superinterfaces4279for (int i = 0; i < local_size; i++) {4280InstanceKlass* const l = local_ifs->at(i);4281append_interfaces(result, l->transitive_interfaces());4282}4283// Finally add local interfaces4284append_interfaces(result, local_ifs);42854286// length will be less than the max_transitive_size if duplicates were removed4287const int length = result->length();4288assert(length <= max_transitive_size, "just checking");4289Array<InstanceKlass*>* const new_result =4290MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);4291for (int i = 0; i < length; i++) {4292InstanceKlass* const e = result->at(i);4293assert(e != NULL, "just checking");4294new_result->at_put(i, e);4295}4296return new_result;4297}4298}42994300void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {4301assert(this_klass != NULL, "invariant");4302const Klass* const super = this_klass->super();43034304if (super != NULL) {4305const InstanceKlass* super_ik = InstanceKlass::cast(super);43064307if (super->is_final()) {4308classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);4309return;4310}43114312if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {4313classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);4314return;4315}43164317// If the loader is not the boot loader then throw an exception if its4318// superclass is in package jdk.internal.reflect and its loader is not a4319// special reflection class loader4320if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {4321PackageEntry* super_package = super->package();4322if (super_package != NULL &&4323super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&4324!java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {4325ResourceMark rm(THREAD);4326Exceptions::fthrow(4327THREAD_AND_LOCATION,4328vmSymbols::java_lang_IllegalAccessError(),4329"class %s loaded by %s cannot access jdk/internal/reflect superclass %s",4330this_klass->external_name(),4331this_klass->class_loader_data()->loader_name_and_id(),4332super->external_name());4333return;4334}4335}43364337Reflection::VerifyClassAccessResults vca_result =4338Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);4339if (vca_result != Reflection::ACCESS_OK) {4340ResourceMark rm(THREAD);4341char* msg = Reflection::verify_class_access_msg(this_klass,4342InstanceKlass::cast(super),4343vca_result);4344if (msg == NULL) {4345bool same_module = (this_klass->module() == super->module());4346Exceptions::fthrow(4347THREAD_AND_LOCATION,4348vmSymbols::java_lang_IllegalAccessError(),4349"class %s cannot access its %ssuperclass %s (%s%s%s)",4350this_klass->external_name(),4351super->is_abstract() ? "abstract " : "",4352super->external_name(),4353(same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),4354(same_module) ? "" : "; ",4355(same_module) ? "" : super->class_in_module_of_loader());4356} else {4357// Add additional message content.4358Exceptions::fthrow(4359THREAD_AND_LOCATION,4360vmSymbols::java_lang_IllegalAccessError(),4361"superclass access check failed: %s",4362msg);4363}4364}4365}4366}436743684369void ClassFileParser::check_super_interface_access(const InstanceKlass* this_klass, TRAPS) {4370assert(this_klass != NULL, "invariant");4371const Array<InstanceKlass*>* const local_interfaces = this_klass->local_interfaces();4372const int lng = local_interfaces->length();4373for (int i = lng - 1; i >= 0; i--) {4374InstanceKlass* const k = local_interfaces->at(i);4375assert (k != NULL && k->is_interface(), "invalid interface");43764377if (k->is_sealed() && !k->has_as_permitted_subclass(this_klass)) {4378classfile_icce_error(this_klass->is_interface() ?4379"class %s cannot extend sealed interface %s" :4380"class %s cannot implement sealed interface %s",4381k, THREAD);4382return;4383}43844385Reflection::VerifyClassAccessResults vca_result =4386Reflection::verify_class_access(this_klass, k, false);4387if (vca_result != Reflection::ACCESS_OK) {4388ResourceMark rm(THREAD);4389char* msg = Reflection::verify_class_access_msg(this_klass,4390k,4391vca_result);4392if (msg == NULL) {4393bool same_module = (this_klass->module() == k->module());4394Exceptions::fthrow(4395THREAD_AND_LOCATION,4396vmSymbols::java_lang_IllegalAccessError(),4397"class %s cannot access its superinterface %s (%s%s%s)",4398this_klass->external_name(),4399k->external_name(),4400(same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(),4401(same_module) ? "" : "; ",4402(same_module) ? "" : k->class_in_module_of_loader());4403} else {4404// Add additional message content.4405Exceptions::fthrow(4406THREAD_AND_LOCATION,4407vmSymbols::java_lang_IllegalAccessError(),4408"superinterface check failed: %s",4409msg);4410}4411}4412}4413}441444154416static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) {4417assert(this_klass != NULL, "invariant");4418const Array<Method*>* const methods = this_klass->methods();4419const int num_methods = methods->length();44204421// go thru each method and check if it overrides a final method4422for (int index = 0; index < num_methods; index++) {4423const Method* const m = methods->at(index);44244425// skip private, static, and <init> methods4426if ((!m->is_private() && !m->is_static()) &&4427(m->name() != vmSymbols::object_initializer_name())) {44284429const Symbol* const name = m->name();4430const Symbol* const signature = m->signature();4431const Klass* k = this_klass->super();4432const Method* super_m = NULL;4433while (k != NULL) {4434// skip supers that don't have final methods.4435if (k->has_final_method()) {4436// lookup a matching method in the super class hierarchy4437super_m = InstanceKlass::cast(k)->lookup_method(name, signature);4438if (super_m == NULL) {4439break; // didn't find any match; get out4440}44414442if (super_m->is_final() && !super_m->is_static() &&4443!super_m->access_flags().is_private()) {4444// matching method in super is final, and not static or private4445bool can_access = Reflection::verify_member_access(this_klass,4446super_m->method_holder(),4447super_m->method_holder(),4448super_m->access_flags(),4449false, false, CHECK);4450if (can_access) {4451// this class can access super final method and therefore override4452ResourceMark rm(THREAD);4453THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),4454err_msg("class %s overrides final method %s.%s%s",4455this_klass->external_name(),4456super_m->method_holder()->external_name(),4457name->as_C_string(),4458signature->as_C_string()));4459}4460}44614462// continue to look from super_m's holder's super.4463k = super_m->method_holder()->super();4464continue;4465}44664467k = k->super();4468}4469}4470}4471}447244734474// assumes that this_klass is an interface4475static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) {4476assert(this_klass != NULL, "invariant");4477assert(this_klass->is_interface(), "not an interface");4478const Array<Method*>* methods = this_klass->methods();4479const int num_methods = methods->length();44804481for (int index = 0; index < num_methods; index++) {4482const Method* const m = methods->at(index);4483// if m is static and not the init method, throw a verify error4484if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {4485ResourceMark rm(THREAD);4486Exceptions::fthrow(4487THREAD_AND_LOCATION,4488vmSymbols::java_lang_VerifyError(),4489"Illegal static method %s in interface %s",4490m->name()->as_C_string(),4491this_klass->external_name()4492);4493return;4494}4495}4496}44974498// utility methods for format checking44994500void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {4501const bool is_module = (flags & JVM_ACC_MODULE) != 0;4502assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");4503if (is_module) {4504ResourceMark rm(THREAD);4505Exceptions::fthrow(4506THREAD_AND_LOCATION,4507vmSymbols::java_lang_NoClassDefFoundError(),4508"%s is not a class because access_flag ACC_MODULE is set",4509_class_name->as_C_string());4510return;4511}45124513if (!_need_verify) { return; }45144515const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;4516const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;4517const bool is_final = (flags & JVM_ACC_FINAL) != 0;4518const bool is_super = (flags & JVM_ACC_SUPER) != 0;4519const bool is_enum = (flags & JVM_ACC_ENUM) != 0;4520const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;4521const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;4522const bool major_gte_14 = _major_version >= JAVA_14_VERSION;45234524if ((is_abstract && is_final) ||4525(is_interface && !is_abstract) ||4526(is_interface && major_gte_1_5 && (is_super || is_enum)) ||4527(!is_interface && major_gte_1_5 && is_annotation)) {4528ResourceMark rm(THREAD);4529Exceptions::fthrow(4530THREAD_AND_LOCATION,4531vmSymbols::java_lang_ClassFormatError(),4532"Illegal class modifiers in class %s: 0x%X",4533_class_name->as_C_string(), flags4534);4535return;4536}4537}45384539static bool has_illegal_visibility(jint flags) {4540const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4541const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4542const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;45434544return ((is_public && is_protected) ||4545(is_public && is_private) ||4546(is_protected && is_private));4547}45484549// A legal major_version.minor_version must be one of the following:4550//4551// Major_version >= 45 and major_version < 56, any minor_version.4552// Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.4553// Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.4554//4555void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){4556ResourceMark rm(THREAD);4557const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;4558if (major < JAVA_MIN_SUPPORTED_VERSION) {4559classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid major version",4560class_name, major, minor, THREAD);4561return;4562}45634564if (major > max_version) {4565Exceptions::fthrow(4566THREAD_AND_LOCATION,4567vmSymbols::java_lang_UnsupportedClassVersionError(),4568"%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "4569"this version of the Java Runtime only recognizes class file versions up to %u.0",4570class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);4571return;4572}45734574if (major < JAVA_12_VERSION || minor == 0) {4575return;4576}45774578if (minor == JAVA_PREVIEW_MINOR_VERSION) {4579if (major != max_version) {4580Exceptions::fthrow(4581THREAD_AND_LOCATION,4582vmSymbols::java_lang_UnsupportedClassVersionError(),4583"%s (class file version %u.%u) was compiled with preview features that are unsupported. "4584"This version of the Java Runtime only recognizes preview features for class file version %u.%u",4585class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);4586return;4587}45884589if (!Arguments::enable_preview()) {4590classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",4591class_name, major, minor, THREAD);4592return;4593}45944595} else { // minor != JAVA_PREVIEW_MINOR_VERSION4596classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",4597class_name, major, minor, THREAD);4598}4599}46004601void ClassFileParser::verify_legal_field_modifiers(jint flags,4602bool is_interface,4603TRAPS) const {4604if (!_need_verify) { return; }46054606const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4607const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4608const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;4609const bool is_static = (flags & JVM_ACC_STATIC) != 0;4610const bool is_final = (flags & JVM_ACC_FINAL) != 0;4611const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;4612const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;4613const bool is_enum = (flags & JVM_ACC_ENUM) != 0;4614const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;46154616bool is_illegal = false;46174618if (is_interface) {4619if (!is_public || !is_static || !is_final || is_private ||4620is_protected || is_volatile || is_transient ||4621(major_gte_1_5 && is_enum)) {4622is_illegal = true;4623}4624} else { // not interface4625if (has_illegal_visibility(flags) || (is_final && is_volatile)) {4626is_illegal = true;4627}4628}46294630if (is_illegal) {4631ResourceMark rm(THREAD);4632Exceptions::fthrow(4633THREAD_AND_LOCATION,4634vmSymbols::java_lang_ClassFormatError(),4635"Illegal field modifiers in class %s: 0x%X",4636_class_name->as_C_string(), flags);4637return;4638}4639}46404641void ClassFileParser::verify_legal_method_modifiers(jint flags,4642bool is_interface,4643const Symbol* name,4644TRAPS) const {4645if (!_need_verify) { return; }46464647const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4648const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;4649const bool is_static = (flags & JVM_ACC_STATIC) != 0;4650const bool is_final = (flags & JVM_ACC_FINAL) != 0;4651const bool is_native = (flags & JVM_ACC_NATIVE) != 0;4652const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;4653const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;4654const bool is_strict = (flags & JVM_ACC_STRICT) != 0;4655const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;4656const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4657const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;4658const bool major_gte_8 = _major_version >= JAVA_8_VERSION;4659const bool major_gte_17 = _major_version >= JAVA_17_VERSION;4660const bool is_initializer = (name == vmSymbols::object_initializer_name());46614662bool is_illegal = false;46634664if (is_interface) {4665if (major_gte_8) {4666// Class file version is JAVA_8_VERSION or later Methods of4667// interfaces may set any of the flags except ACC_PROTECTED,4668// ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must4669// have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.4670if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */4671(is_native || is_protected || is_final || is_synchronized) ||4672// If a specific method of a class or interface has its4673// ACC_ABSTRACT flag set, it must not have any of its4674// ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,4675// ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to4676// check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as4677// those flags are illegal irrespective of ACC_ABSTRACT being set or not.4678(is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {4679is_illegal = true;4680}4681} else if (major_gte_1_5) {4682// Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)4683if (!is_public || is_private || is_protected || is_static || is_final ||4684is_synchronized || is_native || !is_abstract || is_strict) {4685is_illegal = true;4686}4687} else {4688// Class file version is pre-JAVA_1_5_VERSION4689if (!is_public || is_static || is_final || is_native || !is_abstract) {4690is_illegal = true;4691}4692}4693} else { // not interface4694if (has_illegal_visibility(flags)) {4695is_illegal = true;4696} else {4697if (is_initializer) {4698if (is_static || is_final || is_synchronized || is_native ||4699is_abstract || (major_gte_1_5 && is_bridge)) {4700is_illegal = true;4701}4702} else { // not initializer4703if (is_abstract) {4704if ((is_final || is_native || is_private || is_static ||4705(major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {4706is_illegal = true;4707}4708}4709}4710}4711}47124713if (is_illegal) {4714ResourceMark rm(THREAD);4715Exceptions::fthrow(4716THREAD_AND_LOCATION,4717vmSymbols::java_lang_ClassFormatError(),4718"Method %s in class %s has illegal modifiers: 0x%X",4719name->as_C_string(), _class_name->as_C_string(), flags);4720return;4721}4722}47234724void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,4725int length,4726TRAPS) const {4727assert(_need_verify, "only called when _need_verify is true");4728if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {4729classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);4730}4731}47324733// Unqualified names may not contain the characters '.', ';', '[', or '/'.4734// In class names, '/' separates unqualified names. This is verified in this function also.4735// Method names also may not contain the characters '<' or '>', unless <init>4736// or <clinit>. Note that method names may not be <init> or <clinit> in this4737// method. Because these names have been checked as special cases before4738// calling this method in verify_legal_method_name.4739//4740// This method is also called from the modular system APIs in modules.cpp4741// to verify the validity of module and package names.4742bool ClassFileParser::verify_unqualified_name(const char* name,4743unsigned int length,4744int type) {4745if (length == 0) return false; // Must have at least one char.4746for (const char* p = name; p != name + length; p++) {4747switch(*p) {4748case JVM_SIGNATURE_DOT:4749case JVM_SIGNATURE_ENDCLASS:4750case JVM_SIGNATURE_ARRAY:4751// do not permit '.', ';', or '['4752return false;4753case JVM_SIGNATURE_SLASH:4754// check for '//' or leading or trailing '/' which are not legal4755// unqualified name must not be empty4756if (type == ClassFileParser::LegalClass) {4757if (p == name || p+1 >= name+length ||4758*(p+1) == JVM_SIGNATURE_SLASH) {4759return false;4760}4761} else {4762return false; // do not permit '/' unless it's class name4763}4764break;4765case JVM_SIGNATURE_SPECIAL:4766case JVM_SIGNATURE_ENDSPECIAL:4767// do not permit '<' or '>' in method names4768if (type == ClassFileParser::LegalMethod) {4769return false;4770}4771}4772}4773return true;4774}47754776// Take pointer to a UTF8 byte string (not NUL-terminated).4777// Skip over the longest part of the string that could4778// be taken as a fieldname. Allow '/' if slash_ok is true.4779// Return a pointer to just past the fieldname.4780// Return NULL if no fieldname at all was found, or in the case of slash_ok4781// being true, we saw consecutive slashes (meaning we were looking for a4782// qualified path but found something that was badly-formed).4783static const char* skip_over_field_name(const char* const name,4784bool slash_ok,4785unsigned int length) {4786const char* p;4787jboolean last_is_slash = false;4788jboolean not_first_ch = false;47894790for (p = name; p != name + length; not_first_ch = true) {4791const char* old_p = p;4792jchar ch = *p;4793if (ch < 128) {4794p++;4795// quick check for ascii4796if ((ch >= 'a' && ch <= 'z') ||4797(ch >= 'A' && ch <= 'Z') ||4798(ch == '_' || ch == '$') ||4799(not_first_ch && ch >= '0' && ch <= '9')) {4800last_is_slash = false;4801continue;4802}4803if (slash_ok && ch == JVM_SIGNATURE_SLASH) {4804if (last_is_slash) {4805return NULL; // Don't permit consecutive slashes4806}4807last_is_slash = true;4808continue;4809}4810}4811else {4812jint unicode_ch;4813char* tmp_p = UTF8::next_character(p, &unicode_ch);4814p = tmp_p;4815last_is_slash = false;4816// Check if ch is Java identifier start or is Java identifier part4817// 4672820: call java.lang.Character methods directly without generating separate tables.4818EXCEPTION_MARK;4819// return value4820JavaValue result(T_BOOLEAN);4821// Set up the arguments to isJavaIdentifierStart or isJavaIdentifierPart4822JavaCallArguments args;4823args.push_int(unicode_ch);48244825if (not_first_ch) {4826// public static boolean isJavaIdentifierPart(char ch);4827JavaCalls::call_static(&result,4828vmClasses::Character_klass(),4829vmSymbols::isJavaIdentifierPart_name(),4830vmSymbols::int_bool_signature(),4831&args,4832THREAD);4833} else {4834// public static boolean isJavaIdentifierStart(char ch);4835JavaCalls::call_static(&result,4836vmClasses::Character_klass(),4837vmSymbols::isJavaIdentifierStart_name(),4838vmSymbols::int_bool_signature(),4839&args,4840THREAD);4841}4842if (HAS_PENDING_EXCEPTION) {4843CLEAR_PENDING_EXCEPTION;4844return NULL;4845}4846if(result.get_jboolean()) {4847continue;4848}4849}4850return (not_first_ch) ? old_p : NULL;4851}4852return (not_first_ch) ? p : NULL;4853}48544855// Take pointer to a UTF8 byte string (not NUL-terminated).4856// Skip over the longest part of the string that could4857// be taken as a field signature. Allow "void" if void_ok.4858// Return a pointer to just past the signature.4859// Return NULL if no legal signature is found.4860const char* ClassFileParser::skip_over_field_signature(const char* signature,4861bool void_ok,4862unsigned int length,4863TRAPS) const {4864unsigned int array_dim = 0;4865while (length > 0) {4866switch (signature[0]) {4867case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }4868case JVM_SIGNATURE_BOOLEAN:4869case JVM_SIGNATURE_BYTE:4870case JVM_SIGNATURE_CHAR:4871case JVM_SIGNATURE_SHORT:4872case JVM_SIGNATURE_INT:4873case JVM_SIGNATURE_FLOAT:4874case JVM_SIGNATURE_LONG:4875case JVM_SIGNATURE_DOUBLE:4876return signature + 1;4877case JVM_SIGNATURE_CLASS: {4878if (_major_version < JAVA_1_5_VERSION) {4879// Skip over the class name if one is there4880const char* const p = skip_over_field_name(signature + 1, true, --length);48814882// The next character better be a semicolon4883if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {4884return p + 1;4885}4886}4887else {4888// Skip leading 'L' and ignore first appearance of ';'4889signature++;4890const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);4891// Format check signature4892if (c != NULL) {4893int newlen = c - (char*) signature;4894bool legal = verify_unqualified_name(signature, newlen, LegalClass);4895if (!legal) {4896classfile_parse_error("Class name is empty or contains illegal character "4897"in descriptor in class file %s",4898THREAD);4899return NULL;4900}4901return signature + newlen + 1;4902}4903}4904return NULL;4905}4906case JVM_SIGNATURE_ARRAY:4907array_dim++;4908if (array_dim > 255) {4909// 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.4910classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", THREAD);4911return NULL;4912}4913// The rest of what's there better be a legal signature4914signature++;4915length--;4916void_ok = false;4917break;4918default:4919return NULL;4920}4921}4922return NULL;4923}49244925// Checks if name is a legal class name.4926void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {4927if (!_need_verify || _relax_verify) { return; }49284929assert(name->refcount() > 0, "symbol must be kept alive");4930char* bytes = (char*)name->bytes();4931unsigned int length = name->utf8_length();4932bool legal = false;49334934if (length > 0) {4935const char* p;4936if (bytes[0] == JVM_SIGNATURE_ARRAY) {4937p = skip_over_field_signature(bytes, false, length, CHECK);4938legal = (p != NULL) && ((p - bytes) == (int)length);4939} else if (_major_version < JAVA_1_5_VERSION) {4940if (bytes[0] != JVM_SIGNATURE_SPECIAL) {4941p = skip_over_field_name(bytes, true, length);4942legal = (p != NULL) && ((p - bytes) == (int)length);4943}4944} else {4945// 4900761: relax the constraints based on JSR202 spec4946// Class names may be drawn from the entire Unicode character set.4947// Identifiers between '/' must be unqualified names.4948// The utf8 string has been verified when parsing cpool entries.4949legal = verify_unqualified_name(bytes, length, LegalClass);4950}4951}4952if (!legal) {4953ResourceMark rm(THREAD);4954assert(_class_name != NULL, "invariant");4955Exceptions::fthrow(4956THREAD_AND_LOCATION,4957vmSymbols::java_lang_ClassFormatError(),4958"Illegal class name \"%.*s\" in class file %s", length, bytes,4959_class_name->as_C_string()4960);4961return;4962}4963}49644965// Checks if name is a legal field name.4966void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const {4967if (!_need_verify || _relax_verify) { return; }49684969char* bytes = (char*)name->bytes();4970unsigned int length = name->utf8_length();4971bool legal = false;49724973if (length > 0) {4974if (_major_version < JAVA_1_5_VERSION) {4975if (bytes[0] != JVM_SIGNATURE_SPECIAL) {4976const char* p = skip_over_field_name(bytes, false, length);4977legal = (p != NULL) && ((p - bytes) == (int)length);4978}4979} else {4980// 4881221: relax the constraints based on JSR202 spec4981legal = verify_unqualified_name(bytes, length, LegalField);4982}4983}49844985if (!legal) {4986ResourceMark rm(THREAD);4987assert(_class_name != NULL, "invariant");4988Exceptions::fthrow(4989THREAD_AND_LOCATION,4990vmSymbols::java_lang_ClassFormatError(),4991"Illegal field name \"%.*s\" in class %s", length, bytes,4992_class_name->as_C_string()4993);4994return;4995}4996}49974998// Checks if name is a legal method name.4999void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {5000if (!_need_verify || _relax_verify) { return; }50015002assert(name != NULL, "method name is null");5003char* bytes = (char*)name->bytes();5004unsigned int length = name->utf8_length();5005bool legal = false;50065007if (length > 0) {5008if (bytes[0] == JVM_SIGNATURE_SPECIAL) {5009if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {5010legal = true;5011}5012} else if (_major_version < JAVA_1_5_VERSION) {5013const char* p;5014p = skip_over_field_name(bytes, false, length);5015legal = (p != NULL) && ((p - bytes) == (int)length);5016} else {5017// 4881221: relax the constraints based on JSR202 spec5018legal = verify_unqualified_name(bytes, length, LegalMethod);5019}5020}50215022if (!legal) {5023ResourceMark rm(THREAD);5024assert(_class_name != NULL, "invariant");5025Exceptions::fthrow(5026THREAD_AND_LOCATION,5027vmSymbols::java_lang_ClassFormatError(),5028"Illegal method name \"%.*s\" in class %s", length, bytes,5029_class_name->as_C_string()5030);5031return;5032}5033}503450355036// Checks if signature is a legal field signature.5037void ClassFileParser::verify_legal_field_signature(const Symbol* name,5038const Symbol* signature,5039TRAPS) const {5040if (!_need_verify) { return; }50415042const char* const bytes = (const char* const)signature->bytes();5043const unsigned int length = signature->utf8_length();5044const char* const p = skip_over_field_signature(bytes, false, length, CHECK);50455046if (p == NULL || (p - bytes) != (int)length) {5047throwIllegalSignature("Field", name, signature, CHECK);5048}5049}50505051// Checks if signature is a legal method signature.5052// Returns number of parameters5053int ClassFileParser::verify_legal_method_signature(const Symbol* name,5054const Symbol* signature,5055TRAPS) const {5056if (!_need_verify) {5057// make sure caller's args_size will be less than 0 even for non-static5058// method so it will be recomputed in compute_size_of_parameters().5059return -2;5060}50615062// Class initializers cannot have args for class format version >= 51.5063if (name == vmSymbols::class_initializer_name() &&5064signature != vmSymbols::void_method_signature() &&5065_major_version >= JAVA_7_VERSION) {5066throwIllegalSignature("Method", name, signature, CHECK_0);5067return 0;5068}50695070unsigned int args_size = 0;5071const char* p = (const char*)signature->bytes();5072unsigned int length = signature->utf8_length();5073const char* nextp;50745075// The first character must be a '('5076if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {5077length--;5078// Skip over legal field signatures5079nextp = skip_over_field_signature(p, false, length, CHECK_0);5080while ((length > 0) && (nextp != NULL)) {5081args_size++;5082if (p[0] == 'J' || p[0] == 'D') {5083args_size++;5084}5085length -= nextp - p;5086p = nextp;5087nextp = skip_over_field_signature(p, false, length, CHECK_0);5088}5089// The first non-signature thing better be a ')'5090if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {5091length--;5092if (name->utf8_length() > 0 && name->char_at(0) == JVM_SIGNATURE_SPECIAL) {5093// All internal methods must return void5094if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {5095return args_size;5096}5097} else {5098// Now we better just have a return value5099nextp = skip_over_field_signature(p, true, length, CHECK_0);5100if (nextp && ((int)length == (nextp - p))) {5101return args_size;5102}5103}5104}5105}5106// Report error5107throwIllegalSignature("Method", name, signature, CHECK_0);5108return 0;5109}51105111int ClassFileParser::static_field_size() const {5112assert(_field_info != NULL, "invariant");5113return _field_info->_static_field_size;5114}51155116int ClassFileParser::total_oop_map_count() const {5117assert(_field_info != NULL, "invariant");5118return _field_info->oop_map_blocks->_nonstatic_oop_map_count;5119}51205121jint ClassFileParser::layout_size() const {5122assert(_field_info != NULL, "invariant");5123return _field_info->_instance_size;5124}51255126static void check_methods_for_intrinsics(const InstanceKlass* ik,5127const Array<Method*>* methods) {5128assert(ik != NULL, "invariant");5129assert(methods != NULL, "invariant");51305131// Set up Method*::intrinsic_id as soon as we know the names of methods.5132// (We used to do this lazily, but now we query it in Rewriter,5133// which is eagerly done for every method, so we might as well do it now,5134// when everything is fresh in memory.)5135const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);51365137if (klass_id != vmSymbolID::NO_SID) {5138for (int j = 0; j < methods->length(); ++j) {5139Method* method = methods->at(j);5140method->init_intrinsic_id(klass_id);51415142if (CheckIntrinsics) {5143// Check if an intrinsic is defined for method 'method',5144// but the method is not annotated with @IntrinsicCandidate.5145if (method->intrinsic_id() != vmIntrinsics::_none &&5146!method->intrinsic_candidate()) {5147tty->print("Compiler intrinsic is defined for method [%s], "5148"but the method is not annotated with @IntrinsicCandidate.%s",5149method->name_and_sig_as_C_string(),5150NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.")5151);5152tty->cr();5153DEBUG_ONLY(vm_exit(1));5154}5155// Check is the method 'method' is annotated with @IntrinsicCandidate,5156// but there is no intrinsic available for it.5157if (method->intrinsic_candidate() &&5158method->intrinsic_id() == vmIntrinsics::_none) {5159tty->print("Method [%s] is annotated with @IntrinsicCandidate, "5160"but no compiler intrinsic is defined for the method.%s",5161method->name_and_sig_as_C_string(),5162NOT_DEBUG("") DEBUG_ONLY(" Exiting.")5163);5164tty->cr();5165DEBUG_ONLY(vm_exit(1));5166}5167}5168} // end for51695170#ifdef ASSERT5171if (CheckIntrinsics) {5172// Check for orphan methods in the current class. A method m5173// of a class C is orphan if an intrinsic is defined for method m,5174// but class C does not declare m.5175// The check is potentially expensive, therefore it is available5176// only in debug builds.51775178for (auto id : EnumRange<vmIntrinsicID>{}) {5179if (vmIntrinsics::_compiledLambdaForm == id) {5180// The _compiledLamdbdaForm intrinsic is a special marker for bytecode5181// generated for the JVM from a LambdaForm and therefore no method5182// is defined for it.5183continue;5184}5185if (vmIntrinsics::_blackhole == id) {5186// The _blackhole intrinsic is a special marker. No explicit method5187// is defined for it.5188continue;5189}51905191if (vmIntrinsics::class_for(id) == klass_id) {5192// Check if the current class contains a method with the same5193// name, flags, signature.5194bool match = false;5195for (int j = 0; j < methods->length(); ++j) {5196const Method* method = methods->at(j);5197if (method->intrinsic_id() == id) {5198match = true;5199break;5200}5201}52025203if (!match) {5204char buf[1000];5205tty->print("Compiler intrinsic is defined for method [%s], "5206"but the method is not available in class [%s].%s",5207vmIntrinsics::short_name_as_C_string(id, buf, sizeof(buf)),5208ik->name()->as_C_string(),5209NOT_DEBUG("") DEBUG_ONLY(" Exiting.")5210);5211tty->cr();5212DEBUG_ONLY(vm_exit(1));5213}5214}5215} // end for5216} // CheckIntrinsics5217#endif // ASSERT5218}5219}52205221InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,5222const ClassInstanceInfo& cl_inst_info,5223TRAPS) {5224if (_klass != NULL) {5225return _klass;5226}52275228InstanceKlass* const ik =5229InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);52305231if (is_hidden()) {5232mangle_hidden_class_name(ik);5233}52345235fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);52365237assert(_klass == ik, "invariant");52385239return ik;5240}52415242void ClassFileParser::fill_instance_klass(InstanceKlass* ik,5243bool changed_by_loadhook,5244const ClassInstanceInfo& cl_inst_info,5245TRAPS) {5246assert(ik != NULL, "invariant");52475248// Set name and CLD before adding to CLD5249ik->set_class_loader_data(_loader_data);5250ik->set_name(_class_name);52515252// Add all classes to our internal class loader list here,5253// including classes in the bootstrap (NULL) class loader.5254const bool publicize = !is_internal();52555256_loader_data->add_class(ik, publicize);52575258set_klass_to_deallocate(ik);52595260assert(_field_info != NULL, "invariant");5261assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");5262assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,5263"sanity");52645265assert(ik->is_instance_klass(), "sanity");5266assert(ik->size_helper() == _field_info->_instance_size, "sanity");52675268// Fill in information already parsed5269ik->set_should_verify_class(_need_verify);52705271// Not yet: supers are done below to support the new subtype-checking fields5272ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);5273ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);5274assert(_fac != NULL, "invariant");5275ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);52765277// this transfers ownership of a lot of arrays from5278// the parser onto the InstanceKlass*5279apply_parsed_class_metadata(ik, _java_fields_count);52805281// can only set dynamic nest-host after static nest information is set5282if (cl_inst_info.dynamic_nest_host() != NULL) {5283ik->set_nest_host(cl_inst_info.dynamic_nest_host());5284}52855286// note that is not safe to use the fields in the parser from this point on5287assert(NULL == _cp, "invariant");5288assert(NULL == _fields, "invariant");5289assert(NULL == _methods, "invariant");5290assert(NULL == _inner_classes, "invariant");5291assert(NULL == _nest_members, "invariant");5292assert(NULL == _combined_annotations, "invariant");5293assert(NULL == _record_components, "invariant");5294assert(NULL == _permitted_subclasses, "invariant");52955296if (_has_final_method) {5297ik->set_has_final_method();5298}52995300ik->copy_method_ordering(_method_ordering, CHECK);5301// The InstanceKlass::_methods_jmethod_ids cache5302// is managed on the assumption that the initial cache5303// size is equal to the number of methods in the class. If5304// that changes, then InstanceKlass::idnum_can_increment()5305// has to be changed accordingly.5306ik->set_initial_method_idnum(ik->methods()->length());53075308ik->set_this_class_index(_this_class_index);53095310if (_is_hidden) {5311// _this_class_index is a CONSTANT_Class entry that refers to this5312// hidden class itself. If this class needs to refer to its own methods5313// or fields, it would use a CONSTANT_MethodRef, etc, which would reference5314// _this_class_index. However, because this class is hidden (it's5315// not stored in SystemDictionary), _this_class_index cannot be resolved5316// with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.5317// Therefore, we must eagerly resolve _this_class_index now.5318ik->constants()->klass_at_put(_this_class_index, ik);5319}53205321ik->set_minor_version(_minor_version);5322ik->set_major_version(_major_version);5323ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);5324ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);53255326if (_is_hidden) {5327ik->set_is_hidden();5328}53295330// Set PackageEntry for this_klass5331oop cl = ik->class_loader();5332Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));5333ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());5334ik->set_package(cld, NULL, CHECK);53355336const Array<Method*>* const methods = ik->methods();5337assert(methods != NULL, "invariant");5338const int methods_len = methods->length();53395340check_methods_for_intrinsics(ik, methods);53415342// Fill in field values obtained by parse_classfile_attributes5343if (_parsed_annotations->has_any_annotations()) {5344_parsed_annotations->apply_to(ik);5345}53465347apply_parsed_class_attributes(ik);53485349// Miranda methods5350if ((_num_miranda_methods > 0) ||5351// if this class introduced new miranda methods or5352(_super_klass != NULL && _super_klass->has_miranda_methods())5353// super class exists and this class inherited miranda methods5354) {5355ik->set_has_miranda_methods(); // then set a flag5356}53575358// Fill in information needed to compute superclasses.5359ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);5360ik->set_transitive_interfaces(_transitive_interfaces);5361ik->set_local_interfaces(_local_interfaces);5362_transitive_interfaces = NULL;5363_local_interfaces = NULL;53645365// Initialize itable offset tables5366klassItable::setup_itable_offset_table(ik);53675368// Compute transitive closure of interfaces this class implements5369// Do final class setup5370OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;5371if (oop_map_blocks->_nonstatic_oop_map_count > 0) {5372oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());5373}53745375if (_has_contended_fields || _parsed_annotations->is_contended() ||5376( _super_klass != NULL && _super_klass->has_contended_annotations())) {5377ik->set_has_contended_annotations(true);5378}53795380// Fill in has_finalizer, has_vanilla_constructor, and layout_helper5381set_precomputed_flags(ik);53825383// check if this class can access its super class5384check_super_class_access(ik, CHECK);53855386// check if this class can access its superinterfaces5387check_super_interface_access(ik, CHECK);53885389// check if this class overrides any final method5390check_final_method_override(ik, CHECK);53915392// reject static interface methods prior to Java 85393if (ik->is_interface() && _major_version < JAVA_8_VERSION) {5394check_illegal_static_method(ik, CHECK);5395}53965397// Obtain this_klass' module entry5398ModuleEntry* module_entry = ik->module();5399assert(module_entry != NULL, "module_entry should always be set");54005401// Obtain java.lang.Module5402Handle module_handle(THREAD, module_entry->module());54035404// Allocate mirror and initialize static fields5405// The create_mirror() call will also call compute_modifiers()5406java_lang_Class::create_mirror(ik,5407Handle(THREAD, _loader_data->class_loader()),5408module_handle,5409_protection_domain,5410cl_inst_info.class_data(),5411CHECK);54125413assert(_all_mirandas != NULL, "invariant");54145415// Generate any default methods - default methods are public interface methods5416// that have a default implementation. This is new with Java 8.5417if (_has_nonstatic_concrete_methods) {5418DefaultMethods::generate_default_methods(ik,5419_all_mirandas,5420CHECK);5421}54225423// Add read edges to the unnamed modules of the bootstrap and app class loaders.5424if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&5425!module_entry->has_default_read_edges()) {5426if (!module_entry->set_has_default_read_edges()) {5427// We won a potential race5428JvmtiExport::add_default_read_edges(module_handle, THREAD);5429}5430}54315432ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);54335434if (!is_internal()) {5435ik->print_class_load_logging(_loader_data, module_entry, _stream);54365437if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&5438ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&5439log_is_enabled(Info, class, preview)) {5440ResourceMark rm;5441log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",5442ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);5443}54445445if (log_is_enabled(Debug, class, resolve)) {5446ResourceMark rm;5447// print out the superclass.5448const char * from = ik->external_name();5449if (ik->java_super() != NULL) {5450log_debug(class, resolve)("%s %s (super)",5451from,5452ik->java_super()->external_name());5453}5454// print out each of the interface classes referred to by this class.5455const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces();5456if (local_interfaces != NULL) {5457const int length = local_interfaces->length();5458for (int i = 0; i < length; i++) {5459const InstanceKlass* const k = local_interfaces->at(i);5460const char * to = k->external_name();5461log_debug(class, resolve)("%s %s (interface)", from, to);5462}5463}5464}5465}54665467JFR_ONLY(INIT_ID(ik);)54685469// If we reach here, all is well.5470// Now remove the InstanceKlass* from the _klass_to_deallocate field5471// in order for it to not be destroyed in the ClassFileParser destructor.5472set_klass_to_deallocate(NULL);54735474// it's official5475set_klass(ik);54765477debug_only(ik->verify();)5478}54795480void ClassFileParser::update_class_name(Symbol* new_class_name) {5481// Decrement the refcount in the old name, since we're clobbering it.5482_class_name->decrement_refcount();54835484_class_name = new_class_name;5485// Increment the refcount of the new name.5486// Now the ClassFileParser owns this name and will decrement in5487// the destructor.5488_class_name->increment_refcount();5489}54905491static bool relax_format_check_for(ClassLoaderData* loader_data) {5492bool trusted = loader_data->is_boot_class_loader_data() ||5493loader_data->is_platform_class_loader_data();5494bool need_verify =5495// verifyAll5496(BytecodeVerificationLocal && BytecodeVerificationRemote) ||5497// verifyRemote5498(!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);5499return !need_verify;5500}55015502ClassFileParser::ClassFileParser(ClassFileStream* stream,5503Symbol* name,5504ClassLoaderData* loader_data,5505const ClassLoadInfo* cl_info,5506Publicity pub_level,5507TRAPS) :5508_stream(stream),5509_class_name(NULL),5510_loader_data(loader_data),5511_is_hidden(cl_info->is_hidden()),5512_can_access_vm_annotations(cl_info->can_access_vm_annotations()),5513_orig_cp_size(0),5514_super_klass(),5515_cp(NULL),5516_fields(NULL),5517_methods(NULL),5518_inner_classes(NULL),5519_nest_members(NULL),5520_nest_host(0),5521_permitted_subclasses(NULL),5522_record_components(NULL),5523_local_interfaces(NULL),5524_transitive_interfaces(NULL),5525_combined_annotations(NULL),5526_class_annotations(NULL),5527_class_type_annotations(NULL),5528_fields_annotations(NULL),5529_fields_type_annotations(NULL),5530_klass(NULL),5531_klass_to_deallocate(NULL),5532_parsed_annotations(NULL),5533_fac(NULL),5534_field_info(NULL),5535_method_ordering(NULL),5536_all_mirandas(NULL),5537_vtable_size(0),5538_itable_size(0),5539_num_miranda_methods(0),5540_rt(REF_NONE),5541_protection_domain(cl_info->protection_domain()),5542_access_flags(),5543_pub_level(pub_level),5544_bad_constant_seen(0),5545_synthetic_flag(false),5546_sde_length(false),5547_sde_buffer(NULL),5548_sourcefile_index(0),5549_generic_signature_index(0),5550_major_version(0),5551_minor_version(0),5552_this_class_index(0),5553_super_class_index(0),5554_itfs_len(0),5555_java_fields_count(0),5556_need_verify(false),5557_relax_verify(false),5558_has_nonstatic_concrete_methods(false),5559_declares_nonstatic_concrete_methods(false),5560_has_final_method(false),5561_has_contended_fields(false),5562_has_finalizer(false),5563_has_empty_finalizer(false),5564_has_vanilla_constructor(false),5565_max_bootstrap_specifier_index(-1) {55665567_class_name = name != NULL ? name : vmSymbols::unknown_class_name();5568_class_name->increment_refcount();55695570assert(_loader_data != NULL, "invariant");5571assert(stream != NULL, "invariant");5572assert(_stream != NULL, "invariant");5573assert(_stream->buffer() == _stream->current(), "invariant");5574assert(_class_name != NULL, "invariant");5575assert(0 == _access_flags.as_int(), "invariant");55765577// Figure out whether we can skip format checking (matching classic VM behavior)5578if (DumpSharedSpaces) {5579// verify == true means it's a 'remote' class (i.e., non-boot class)5580// Verification decision is based on BytecodeVerificationRemote flag5581// for those classes.5582_need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :5583BytecodeVerificationLocal;5584}5585else {5586_need_verify = Verifier::should_verify_for(_loader_data->class_loader(),5587stream->need_verify());5588}55895590// synch back verification state to stream5591stream->set_verify(_need_verify);55925593// Check if verification needs to be relaxed for this class file5594// Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)5595_relax_verify = relax_format_check_for(_loader_data);55965597parse_stream(stream, CHECK);55985599post_process_parsed_stream(stream, _cp, CHECK);5600}56015602void ClassFileParser::clear_class_metadata() {5603// metadata created before the instance klass is created. Must be5604// deallocated if classfile parsing returns an error.5605_cp = NULL;5606_fields = NULL;5607_methods = NULL;5608_inner_classes = NULL;5609_nest_members = NULL;5610_permitted_subclasses = NULL;5611_combined_annotations = NULL;5612_class_annotations = _class_type_annotations = NULL;5613_fields_annotations = _fields_type_annotations = NULL;5614_record_components = NULL;5615}56165617// Destructor to clean up5618ClassFileParser::~ClassFileParser() {5619_class_name->decrement_refcount();56205621if (_cp != NULL) {5622MetadataFactory::free_metadata(_loader_data, _cp);5623}5624if (_fields != NULL) {5625MetadataFactory::free_array<u2>(_loader_data, _fields);5626}56275628if (_methods != NULL) {5629// Free methods5630InstanceKlass::deallocate_methods(_loader_data, _methods);5631}56325633// beware of the Universe::empty_blah_array!!5634if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {5635MetadataFactory::free_array<u2>(_loader_data, _inner_classes);5636}56375638if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {5639MetadataFactory::free_array<u2>(_loader_data, _nest_members);5640}56415642if (_record_components != NULL) {5643InstanceKlass::deallocate_record_components(_loader_data, _record_components);5644}56455646if (_permitted_subclasses != NULL && _permitted_subclasses != Universe::the_empty_short_array()) {5647MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);5648}56495650// Free interfaces5651InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,5652_local_interfaces, _transitive_interfaces);56535654if (_combined_annotations != NULL) {5655// After all annotations arrays have been created, they are installed into the5656// Annotations object that will be assigned to the InstanceKlass being created.56575658// Deallocate the Annotations object and the installed annotations arrays.5659_combined_annotations->deallocate_contents(_loader_data);56605661// If the _combined_annotations pointer is non-NULL,5662// then the other annotations fields should have been cleared.5663assert(_class_annotations == NULL, "Should have been cleared");5664assert(_class_type_annotations == NULL, "Should have been cleared");5665assert(_fields_annotations == NULL, "Should have been cleared");5666assert(_fields_type_annotations == NULL, "Should have been cleared");5667} else {5668// If the annotations arrays were not installed into the Annotations object,5669// then they have to be deallocated explicitly.5670MetadataFactory::free_array<u1>(_loader_data, _class_annotations);5671MetadataFactory::free_array<u1>(_loader_data, _class_type_annotations);5672Annotations::free_contents(_loader_data, _fields_annotations);5673Annotations::free_contents(_loader_data, _fields_type_annotations);5674}56755676clear_class_metadata();5677_transitive_interfaces = NULL;5678_local_interfaces = NULL;56795680// deallocate the klass if already created. Don't directly deallocate, but add5681// to the deallocate list so that the klass is removed from the CLD::_klasses list5682// at a safepoint.5683if (_klass_to_deallocate != NULL) {5684_loader_data->add_to_deallocate_list(_klass_to_deallocate);5685}5686}56875688void ClassFileParser::parse_stream(const ClassFileStream* const stream,5689TRAPS) {56905691assert(stream != NULL, "invariant");5692assert(_class_name != NULL, "invariant");56935694// BEGIN STREAM PARSING5695stream->guarantee_more(8, CHECK); // magic, major, minor5696// Magic value5697const u4 magic = stream->get_u4_fast();5698guarantee_property(magic == JAVA_CLASSFILE_MAGIC,5699"Incompatible magic value %u in class file %s",5700magic, CHECK);57015702// Version numbers5703_minor_version = stream->get_u2_fast();5704_major_version = stream->get_u2_fast();57055706// Check version numbers - we check this even with verifier off5707verify_class_version(_major_version, _minor_version, _class_name, CHECK);57085709stream->guarantee_more(3, CHECK); // length, first cp tag5710u2 cp_size = stream->get_u2_fast();57115712guarantee_property(5713cp_size >= 1, "Illegal constant pool size %u in class file %s",5714cp_size, CHECK);57155716_orig_cp_size = cp_size;5717if (is_hidden()) { // Add a slot for hidden class name.5718cp_size++;5719}57205721_cp = ConstantPool::allocate(_loader_data,5722cp_size,5723CHECK);57245725ConstantPool* const cp = _cp;57265727parse_constant_pool(stream, cp, _orig_cp_size, CHECK);57285729assert(cp_size == (const u2)cp->length(), "invariant");57305731// ACCESS FLAGS5732stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len57335734// Access flags5735jint flags;5736// JVM_ACC_MODULE is defined in JDK-9 and later.5737if (_major_version >= JAVA_9_VERSION) {5738flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);5739} else {5740flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;5741}57425743if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {5744// Set abstract bit for old class files for backward compatibility5745flags |= JVM_ACC_ABSTRACT;5746}57475748verify_legal_class_modifiers(flags, CHECK);57495750short bad_constant = class_bad_constant_seen();5751if (bad_constant != 0) {5752// Do not throw CFE until after the access_flags are checked because if5753// ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.5754classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);5755return;5756}57575758_access_flags.set_flags(flags);57595760// This class and superclass5761_this_class_index = stream->get_u2_fast();5762check_property(5763valid_cp_range(_this_class_index, cp_size) &&5764cp->tag_at(_this_class_index).is_unresolved_klass(),5765"Invalid this class index %u in constant pool in class file %s",5766_this_class_index, CHECK);57675768Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);5769assert(class_name_in_cp != NULL, "class_name can't be null");57705771// Don't need to check whether this class name is legal or not.5772// It has been checked when constant pool is parsed.5773// However, make sure it is not an array type.5774if (_need_verify) {5775guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,5776"Bad class name in class file %s",5777CHECK);5778}57795780#ifdef ASSERT5781// Basic sanity checks5782if (_is_hidden) {5783assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");5784}5785#endif57865787// Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.57885789if (_is_hidden) {5790assert(_class_name != NULL, "Unexpected null _class_name");5791#ifdef ASSERT5792if (_need_verify) {5793verify_legal_class_name(_class_name, CHECK);5794}5795#endif57965797} else {5798// Check if name in class file matches given name5799if (_class_name != class_name_in_cp) {5800if (_class_name != vmSymbols::unknown_class_name()) {5801ResourceMark rm(THREAD);5802Exceptions::fthrow(THREAD_AND_LOCATION,5803vmSymbols::java_lang_NoClassDefFoundError(),5804"%s (wrong name: %s)",5805class_name_in_cp->as_C_string(),5806_class_name->as_C_string()5807);5808return;5809} else {5810// The class name was not known by the caller so we set it from5811// the value in the CP.5812update_class_name(class_name_in_cp);5813}5814// else nothing to do: the expected class name matches what is in the CP5815}5816}58175818// Verification prevents us from creating names with dots in them, this5819// asserts that that's the case.5820assert(is_internal_format(_class_name), "external class name format used internally");58215822if (!is_internal()) {5823LogTarget(Debug, class, preorder) lt;5824if (lt.is_enabled()){5825ResourceMark rm(THREAD);5826LogStream ls(lt);5827ls.print("%s", _class_name->as_klass_external_name());5828if (stream->source() != NULL) {5829ls.print(" source: %s", stream->source());5830}5831ls.cr();5832}5833}58345835// SUPERKLASS5836_super_class_index = stream->get_u2_fast();5837_super_klass = parse_super_class(cp,5838_super_class_index,5839_need_verify,5840CHECK);58415842// Interfaces5843_itfs_len = stream->get_u2_fast();5844parse_interfaces(stream,5845_itfs_len,5846cp,5847&_has_nonstatic_concrete_methods,5848CHECK);58495850assert(_local_interfaces != NULL, "invariant");58515852// Fields (offsets are filled in later)5853_fac = new FieldAllocationCount();5854parse_fields(stream,5855_access_flags.is_interface(),5856_fac,5857cp,5858cp_size,5859&_java_fields_count,5860CHECK);58615862assert(_fields != NULL, "invariant");58635864// Methods5865AccessFlags promoted_flags;5866parse_methods(stream,5867_access_flags.is_interface(),5868&promoted_flags,5869&_has_final_method,5870&_declares_nonstatic_concrete_methods,5871CHECK);58725873assert(_methods != NULL, "invariant");58745875// promote flags from parse_methods() to the klass' flags5876_access_flags.add_promoted_flags(promoted_flags.as_int());58775878if (_declares_nonstatic_concrete_methods) {5879_has_nonstatic_concrete_methods = true;5880}58815882// Additional attributes/annotations5883_parsed_annotations = new ClassAnnotationCollector();5884parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);58855886assert(_inner_classes != NULL, "invariant");58875888// Finalize the Annotations metadata object,5889// now that all annotation arrays have been created.5890create_combined_annotations(CHECK);58915892// Make sure this is the end of class file stream5893guarantee_property(stream->at_eos(),5894"Extra bytes at the end of class file %s",5895CHECK);58965897// all bytes in stream read and parsed5898}58995900void ClassFileParser::mangle_hidden_class_name(InstanceKlass* const ik) {5901ResourceMark rm;5902// Construct hidden name from _class_name, "+", and &ik. Note that we can't5903// use a '/' because that confuses finding the class's package. Also, can't5904// use an illegal char such as ';' because that causes serialization issues5905// and issues with hidden classes that create their own hidden classes.5906char addr_buf[20];5907if (DumpSharedSpaces) {5908// We want stable names for the archived hidden classes (only for static5909// archive for now). Spaces under default_SharedBaseAddress() will be5910// occupied by the archive at run time, so we know that no dynamically5911// loaded InstanceKlass will be placed under there.5912static volatile size_t counter = 0;5913Atomic::cmpxchg(&counter, (size_t)0, Arguments::default_SharedBaseAddress()); // initialize it5914size_t new_id = Atomic::add(&counter, (size_t)1);5915jio_snprintf(addr_buf, 20, SIZE_FORMAT_HEX, new_id);5916} else {5917jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(ik));5918}5919size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf);5920char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len);5921jio_snprintf(new_name, new_name_len, "%s+%s",5922_class_name->as_C_string(), addr_buf);5923update_class_name(SymbolTable::new_symbol(new_name));59245925// Add a Utf8 entry containing the hidden name.5926assert(_class_name != NULL, "Unexpected null _class_name");5927int hidden_index = _orig_cp_size; // this is an extra slot we added5928_cp->symbol_at_put(hidden_index, _class_name);59295930// Update this_class_index's slot in the constant pool with the new Utf8 entry.5931// We have to update the resolved_klass_index and the name_index together5932// so extract the existing resolved_klass_index first.5933CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);5934int resolved_klass_index = cp_klass_slot.resolved_klass_index();5935_cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);5936assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,5937"Bad name_index");5938}59395940void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,5941ConstantPool* cp,5942TRAPS) {5943assert(stream != NULL, "invariant");5944assert(stream->at_eos(), "invariant");5945assert(cp != NULL, "invariant");5946assert(_loader_data != NULL, "invariant");59475948if (_class_name == vmSymbols::java_lang_Object()) {5949check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),5950"java.lang.Object cannot implement an interface in class file %s",5951CHECK);5952}5953// We check super class after class file is parsed and format is checked5954if (_super_class_index > 0 && NULL == _super_klass) {5955Symbol* const super_class_name = cp->klass_name_at(_super_class_index);5956if (_access_flags.is_interface()) {5957// Before attempting to resolve the superclass, check for class format5958// errors not checked yet.5959guarantee_property(super_class_name == vmSymbols::java_lang_Object(),5960"Interfaces must have java.lang.Object as superclass in class file %s",5961CHECK);5962}5963Handle loader(THREAD, _loader_data->class_loader());5964_super_klass = (const InstanceKlass*)5965SystemDictionary::resolve_super_or_fail(_class_name,5966super_class_name,5967loader,5968_protection_domain,5969true,5970CHECK);5971}59725973if (_super_klass != NULL) {5974if (_super_klass->has_nonstatic_concrete_methods()) {5975_has_nonstatic_concrete_methods = true;5976}59775978if (_super_klass->is_interface()) {5979classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);5980return;5981}5982}59835984// Compute the transitive list of all unique interfaces implemented by this class5985_transitive_interfaces =5986compute_transitive_interfaces(_super_klass,5987_local_interfaces,5988_loader_data,5989CHECK);59905991assert(_transitive_interfaces != NULL, "invariant");59925993// sort methods5994_method_ordering = sort_methods(_methods);59955996_all_mirandas = new GrowableArray<Method*>(20);59975998Handle loader(THREAD, _loader_data->class_loader());5999klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,6000&_num_miranda_methods,6001_all_mirandas,6002_super_klass,6003_methods,6004_access_flags,6005_major_version,6006loader,6007_class_name,6008_local_interfaces);60096010// Size of Java itable (in words)6011_itable_size = _access_flags.is_interface() ? 0 :6012klassItable::compute_itable_size(_transitive_interfaces);60136014assert(_fac != NULL, "invariant");6015assert(_parsed_annotations != NULL, "invariant");60166017_field_info = new FieldLayoutInfo();6018FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,6019_parsed_annotations->is_contended(), _field_info);6020lb.build_layout();60216022// Compute reference typ6023_rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();60246025}60266027void ClassFileParser::set_klass(InstanceKlass* klass) {60286029#ifdef ASSERT6030if (klass != NULL) {6031assert(NULL == _klass, "leaking?");6032}6033#endif60346035_klass = klass;6036}60376038void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {60396040#ifdef ASSERT6041if (klass != NULL) {6042assert(NULL == _klass_to_deallocate, "leaking?");6043}6044#endif60456046_klass_to_deallocate = klass;6047}60486049// Caller responsible for ResourceMark6050// clone stream with rewound position6051const ClassFileStream* ClassFileParser::clone_stream() const {6052assert(_stream != NULL, "invariant");60536054return _stream->clone();6055}6056// ----------------------------------------------------------------------------6057// debugging60586059#ifdef ASSERT60606061// return true if class_name contains no '.' (internal format is '/')6062bool ClassFileParser::is_internal_format(Symbol* class_name) {6063if (class_name != NULL) {6064ResourceMark rm;6065char* name = class_name->as_C_string();6066return strchr(name, JVM_SIGNATURE_DOT) == NULL;6067} else {6068return true;6069}6070}60716072#endif607360746075