Path: blob/master/src/hotspot/share/classfile/classFileParser.cpp
40949 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);3046while (fast != -1 && fast != 0) {3047if (slow != 0 && (cp->klass_name_at(slow) == cp->klass_name_at(fast))) {3048return true; // found a circularity3049}3050fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length);3051if (fast == -1) return false;3052fast = inner_classes_jump_to_outer(inner_classes, fast, cp, length);3053if (fast == -1) return false;3054slow = inner_classes_jump_to_outer(inner_classes, slow, cp, length);3055assert(slow != -1, "sanity check");3056}3057return false;3058}30593060// Loop through each InnerClasses entry checking for circularities and duplications3061// with other entries. If duplicate entries are found then throw CFE. Otherwise,3062// return true if a circularity or entries with duplicate inner_class_info_indexes3063// are found.3064bool ClassFileParser::check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS) {3065// Loop through each InnerClasses entry.3066for (int idx = 0; idx < length; idx += InstanceKlass::inner_class_next_offset) {3067// Return true if there are circular entries.3068if (inner_classes_check_loop_through_outer(_inner_classes, idx, cp, length)) {3069return true;3070}3071// Check if there are duplicate entries or entries with the same inner_class_info_index.3072for (int y = idx + InstanceKlass::inner_class_next_offset; y < length;3073y += InstanceKlass::inner_class_next_offset) {30743075// To maintain compatibility, throw an exception if duplicate inner classes3076// entries are found.3077guarantee_property((_inner_classes->at(idx) != _inner_classes->at(y) ||3078_inner_classes->at(idx+1) != _inner_classes->at(y+1) ||3079_inner_classes->at(idx+2) != _inner_classes->at(y+2) ||3080_inner_classes->at(idx+3) != _inner_classes->at(y+3)),3081"Duplicate entry in InnerClasses attribute in class file %s",3082CHECK_(true));3083// Return true if there are two entries with the same inner_class_info_index.3084if (_inner_classes->at(y) == _inner_classes->at(idx)) {3085return true;3086}3087}3088}3089return false;3090}30913092// Return number of classes in the inner classes attribute table3093u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,3094const ConstantPool* cp,3095const u1* const inner_classes_attribute_start,3096bool parsed_enclosingmethod_attribute,3097u2 enclosing_method_class_index,3098u2 enclosing_method_method_index,3099TRAPS) {3100const u1* const current_mark = cfs->current();3101u2 length = 0;3102if (inner_classes_attribute_start != NULL) {3103cfs->set_current(inner_classes_attribute_start);3104cfs->guarantee_more(2, CHECK_0); // length3105length = cfs->get_u2_fast();3106}31073108// 4-tuples of shorts of inner classes data and 2 shorts of enclosing3109// method data:3110// [inner_class_info_index,3111// outer_class_info_index,3112// inner_name_index,3113// inner_class_access_flags,3114// ...3115// enclosing_method_class_index,3116// enclosing_method_method_index]3117const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);3118Array<u2>* inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3119_inner_classes = inner_classes;31203121int index = 0;3122cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u23123for (int n = 0; n < length; n++) {3124// Inner class index3125const u2 inner_class_info_index = cfs->get_u2_fast();3126check_property(3127valid_klass_reference_at(inner_class_info_index),3128"inner_class_info_index %u has bad constant type in class file %s",3129inner_class_info_index, CHECK_0);3130// Outer class index3131const u2 outer_class_info_index = cfs->get_u2_fast();3132check_property(3133outer_class_info_index == 0 ||3134valid_klass_reference_at(outer_class_info_index),3135"outer_class_info_index %u has bad constant type in class file %s",3136outer_class_info_index, CHECK_0);3137// Inner class name3138const u2 inner_name_index = cfs->get_u2_fast();3139check_property(3140inner_name_index == 0 || valid_symbol_at(inner_name_index),3141"inner_name_index %u has bad constant type in class file %s",3142inner_name_index, CHECK_0);3143if (_need_verify) {3144guarantee_property(inner_class_info_index != outer_class_info_index,3145"Class is both outer and inner class in class file %s", CHECK_0);3146}3147// Access flags3148jint flags;3149// JVM_ACC_MODULE is defined in JDK-9 and later.3150if (_major_version >= JAVA_9_VERSION) {3151flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);3152} else {3153flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;3154}3155if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {3156// Set abstract bit for old class files for backward compatibility3157flags |= JVM_ACC_ABSTRACT;3158}3159verify_legal_class_modifiers(flags, CHECK_0);3160AccessFlags inner_access_flags(flags);31613162inner_classes->at_put(index++, inner_class_info_index);3163inner_classes->at_put(index++, outer_class_info_index);3164inner_classes->at_put(index++, inner_name_index);3165inner_classes->at_put(index++, inner_access_flags.as_short());3166}31673168// 4347400: make sure there's no duplicate entry in the classes array3169// Also, check for circular entries.3170bool has_circularity = false;3171if (_need_verify && _major_version >= JAVA_1_5_VERSION) {3172has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);3173if (has_circularity) {3174// If circularity check failed then ignore InnerClasses attribute.3175MetadataFactory::free_array<u2>(_loader_data, _inner_classes);3176index = 0;3177if (parsed_enclosingmethod_attribute) {3178inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);3179_inner_classes = inner_classes;3180} else {3181_inner_classes = Universe::the_empty_short_array();3182}3183}3184}3185// Set EnclosingMethod class and method indexes.3186if (parsed_enclosingmethod_attribute) {3187inner_classes->at_put(index++, enclosing_method_class_index);3188inner_classes->at_put(index++, enclosing_method_method_index);3189}3190assert(index == size || has_circularity, "wrong size");31913192// Restore buffer's current position.3193cfs->set_current(current_mark);31943195return length;3196}31973198u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,3199const u1* const nest_members_attribute_start,3200TRAPS) {3201const u1* const current_mark = cfs->current();3202u2 length = 0;3203if (nest_members_attribute_start != NULL) {3204cfs->set_current(nest_members_attribute_start);3205cfs->guarantee_more(2, CHECK_0); // length3206length = cfs->get_u2_fast();3207}3208const int size = length;3209Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3210_nest_members = nest_members;32113212int index = 0;3213cfs->guarantee_more(2 * length, CHECK_0);3214for (int n = 0; n < length; n++) {3215const u2 class_info_index = cfs->get_u2_fast();3216check_property(3217valid_klass_reference_at(class_info_index),3218"Nest member class_info_index %u has bad constant type in class file %s",3219class_info_index, CHECK_0);3220nest_members->at_put(index++, class_info_index);3221}3222assert(index == size, "wrong size");32233224// Restore buffer's current position.3225cfs->set_current(current_mark);32263227return length;3228}32293230u2 ClassFileParser::parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs,3231const u1* const permitted_subclasses_attribute_start,3232TRAPS) {3233const u1* const current_mark = cfs->current();3234u2 length = 0;3235if (permitted_subclasses_attribute_start != NULL) {3236cfs->set_current(permitted_subclasses_attribute_start);3237cfs->guarantee_more(2, CHECK_0); // length3238length = cfs->get_u2_fast();3239}3240const int size = length;3241Array<u2>* const permitted_subclasses = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);3242_permitted_subclasses = permitted_subclasses;32433244if (length > 0) {3245int index = 0;3246cfs->guarantee_more(2 * length, CHECK_0);3247for (int n = 0; n < length; n++) {3248const u2 class_info_index = cfs->get_u2_fast();3249check_property(3250valid_klass_reference_at(class_info_index),3251"Permitted subclass class_info_index %u has bad constant type in class file %s",3252class_info_index, CHECK_0);3253permitted_subclasses->at_put(index++, class_info_index);3254}3255assert(index == size, "wrong size");3256}32573258// Restore buffer's current position.3259cfs->set_current(current_mark);32603261return length;3262}32633264// Record {3265// u2 attribute_name_index;3266// u4 attribute_length;3267// u2 components_count;3268// component_info components[components_count];3269// }3270// component_info {3271// u2 name_index;3272// u2 descriptor_index3273// u2 attributes_count;3274// attribute_info_attributes[attributes_count];3275// }3276u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,3277const ConstantPool* cp,3278const u1* const record_attribute_start,3279TRAPS) {3280const u1* const current_mark = cfs->current();3281int components_count = 0;3282unsigned int calculate_attr_size = 0;3283if (record_attribute_start != NULL) {3284cfs->set_current(record_attribute_start);3285cfs->guarantee_more(2, CHECK_0); // num of components3286components_count = (int)cfs->get_u2_fast();3287calculate_attr_size = 2;3288}32893290Array<RecordComponent*>* const record_components =3291MetadataFactory::new_array<RecordComponent*>(_loader_data, components_count, NULL, CHECK_0);3292_record_components = record_components;32933294for (int x = 0; x < components_count; x++) {3295cfs->guarantee_more(6, CHECK_0); // name_index, descriptor_index, attributes_count32963297const u2 name_index = cfs->get_u2_fast();3298check_property(valid_symbol_at(name_index),3299"Invalid constant pool index %u for name in Record attribute in class file %s",3300name_index, CHECK_0);3301const Symbol* const name = cp->symbol_at(name_index);3302verify_legal_field_name(name, CHECK_0);33033304const u2 descriptor_index = cfs->get_u2_fast();3305check_property(valid_symbol_at(descriptor_index),3306"Invalid constant pool index %u for descriptor in Record attribute in class file %s",3307descriptor_index, CHECK_0);3308const Symbol* const descr = cp->symbol_at(descriptor_index);3309verify_legal_field_signature(name, descr, CHECK_0);33103311const u2 attributes_count = cfs->get_u2_fast();3312calculate_attr_size += 6;3313u2 generic_sig_index = 0;3314const u1* runtime_visible_annotations = NULL;3315int runtime_visible_annotations_length = 0;3316const u1* runtime_invisible_annotations = NULL;3317int runtime_invisible_annotations_length = 0;3318bool runtime_invisible_annotations_exists = false;3319const u1* runtime_visible_type_annotations = NULL;3320int runtime_visible_type_annotations_length = 0;3321const u1* runtime_invisible_type_annotations = NULL;3322int runtime_invisible_type_annotations_length = 0;3323bool runtime_invisible_type_annotations_exists = false;33243325// Expected attributes for record components are Signature, Runtime(In)VisibleAnnotations,3326// and Runtime(In)VisibleTypeAnnotations. Other attributes are ignored.3327for (int y = 0; y < attributes_count; y++) {3328cfs->guarantee_more(6, CHECK_0); // attribute_name_index, attribute_length3329const u2 attribute_name_index = cfs->get_u2_fast();3330const u4 attribute_length = cfs->get_u4_fast();3331calculate_attr_size += 6;3332check_property(3333valid_symbol_at(attribute_name_index),3334"Invalid Record attribute name index %u in class file %s",3335attribute_name_index, CHECK_0);33363337const Symbol* const attribute_name = cp->symbol_at(attribute_name_index);3338if (attribute_name == vmSymbols::tag_signature()) {3339if (generic_sig_index != 0) {3340classfile_parse_error(3341"Multiple Signature attributes for Record component in class file %s",3342THREAD);3343return 0;3344}3345if (attribute_length != 2) {3346classfile_parse_error(3347"Invalid Signature attribute length %u in Record component in class file %s",3348attribute_length, THREAD);3349return 0;3350}3351generic_sig_index = parse_generic_signature_attribute(cfs, CHECK_0);33523353} else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {3354if (runtime_visible_annotations != NULL) {3355classfile_parse_error(3356"Multiple RuntimeVisibleAnnotations attributes for Record component in class file %s", THREAD);3357return 0;3358}3359runtime_visible_annotations_length = attribute_length;3360runtime_visible_annotations = cfs->current();33613362assert(runtime_visible_annotations != NULL, "null record component visible annotation");3363cfs->guarantee_more(runtime_visible_annotations_length, CHECK_0);3364cfs->skip_u1_fast(runtime_visible_annotations_length);33653366} else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {3367if (runtime_invisible_annotations_exists) {3368classfile_parse_error(3369"Multiple RuntimeInvisibleAnnotations attributes for Record component in class file %s", THREAD);3370return 0;3371}3372runtime_invisible_annotations_exists = true;3373if (PreserveAllAnnotations) {3374runtime_invisible_annotations_length = attribute_length;3375runtime_invisible_annotations = cfs->current();3376assert(runtime_invisible_annotations != NULL, "null record component invisible annotation");3377}3378cfs->skip_u1(attribute_length, CHECK_0);33793380} else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {3381if (runtime_visible_type_annotations != NULL) {3382classfile_parse_error(3383"Multiple RuntimeVisibleTypeAnnotations attributes for Record component in class file %s", THREAD);3384return 0;3385}3386runtime_visible_type_annotations_length = attribute_length;3387runtime_visible_type_annotations = cfs->current();33883389assert(runtime_visible_type_annotations != NULL, "null record component visible type annotation");3390cfs->guarantee_more(runtime_visible_type_annotations_length, CHECK_0);3391cfs->skip_u1_fast(runtime_visible_type_annotations_length);33923393} else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {3394if (runtime_invisible_type_annotations_exists) {3395classfile_parse_error(3396"Multiple RuntimeInvisibleTypeAnnotations attributes for Record component in class file %s", THREAD);3397return 0;3398}3399runtime_invisible_type_annotations_exists = true;3400if (PreserveAllAnnotations) {3401runtime_invisible_type_annotations_length = attribute_length;3402runtime_invisible_type_annotations = cfs->current();3403assert(runtime_invisible_type_annotations != NULL, "null record component invisible type annotation");3404}3405cfs->skip_u1(attribute_length, CHECK_0);34063407} else {3408// Skip unknown attributes3409cfs->skip_u1(attribute_length, CHECK_0);3410}3411calculate_attr_size += attribute_length;3412} // End of attributes For loop34133414AnnotationArray* annotations = assemble_annotations(runtime_visible_annotations,3415runtime_visible_annotations_length,3416runtime_invisible_annotations,3417runtime_invisible_annotations_length,3418CHECK_0);3419AnnotationArray* type_annotations = assemble_annotations(runtime_visible_type_annotations,3420runtime_visible_type_annotations_length,3421runtime_invisible_type_annotations,3422runtime_invisible_type_annotations_length,3423CHECK_0);34243425RecordComponent* record_component =3426RecordComponent::allocate(_loader_data, name_index, descriptor_index,3427attributes_count, generic_sig_index,3428annotations, type_annotations, CHECK_0);3429record_components->at_put(x, record_component);3430} // End of component processing loop34313432// Restore buffer's current position.3433cfs->set_current(current_mark);3434return calculate_attr_size;3435}34363437void ClassFileParser::parse_classfile_synthetic_attribute() {3438set_class_synthetic_flag(true);3439}34403441void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {3442assert(cfs != NULL, "invariant");34433444const u2 signature_index = cfs->get_u2(CHECK);3445check_property(3446valid_symbol_at(signature_index),3447"Invalid constant pool index %u in Signature attribute in class file %s",3448signature_index, CHECK);3449set_class_generic_signature_index(signature_index);3450}34513452void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,3453ConstantPool* cp,3454u4 attribute_byte_length,3455TRAPS) {3456assert(cfs != NULL, "invariant");3457assert(cp != NULL, "invariant");34583459const u1* const current_start = cfs->current();34603461guarantee_property(attribute_byte_length >= sizeof(u2),3462"Invalid BootstrapMethods attribute length %u in class file %s",3463attribute_byte_length,3464CHECK);34653466cfs->guarantee_more(attribute_byte_length, CHECK);34673468const int attribute_array_length = cfs->get_u2_fast();34693470guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,3471"Short length on BootstrapMethods in class file %s",3472CHECK);347334743475// The attribute contains a counted array of counted tuples of shorts,3476// represending bootstrap specifiers:3477// length*{bootstrap_method_index, argument_count*{argument_index}}3478const int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2);3479// operand_count = number of shorts in attr, except for leading length34803481// The attribute is copied into a short[] array.3482// The array begins with a series of short[2] pairs, one for each tuple.3483const int index_size = (attribute_array_length * 2);34843485Array<u2>* const operands =3486MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK);34873488// Eagerly assign operands so they will be deallocated with the constant3489// pool if there is an error.3490cp->set_operands(operands);34913492int operand_fill_index = index_size;3493const int cp_size = cp->length();34943495for (int n = 0; n < attribute_array_length; n++) {3496// Store a 32-bit offset into the header of the operand array.3497ConstantPool::operand_offset_at_put(operands, n, operand_fill_index);34983499// Read a bootstrap specifier.3500cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc3501const u2 bootstrap_method_index = cfs->get_u2_fast();3502const u2 argument_count = cfs->get_u2_fast();3503check_property(3504valid_cp_range(bootstrap_method_index, cp_size) &&3505cp->tag_at(bootstrap_method_index).is_method_handle(),3506"bootstrap_method_index %u has bad constant type in class file %s",3507bootstrap_method_index,3508CHECK);35093510guarantee_property((operand_fill_index + 1 + argument_count) < operands->length(),3511"Invalid BootstrapMethods num_bootstrap_methods or num_bootstrap_arguments value in class file %s",3512CHECK);35133514operands->at_put(operand_fill_index++, bootstrap_method_index);3515operands->at_put(operand_fill_index++, argument_count);35163517cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]3518for (int j = 0; j < argument_count; j++) {3519const u2 argument_index = cfs->get_u2_fast();3520check_property(3521valid_cp_range(argument_index, cp_size) &&3522cp->tag_at(argument_index).is_loadable_constant(),3523"argument_index %u has bad constant type in class file %s",3524argument_index,3525CHECK);3526operands->at_put(operand_fill_index++, argument_index);3527}3528}3529guarantee_property(current_start + attribute_byte_length == cfs->current(),3530"Bad length on BootstrapMethods in class file %s",3531CHECK);3532}35333534void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,3535ConstantPool* cp,3536ClassFileParser::ClassAnnotationCollector* parsed_annotations,3537TRAPS) {3538assert(cfs != NULL, "invariant");3539assert(cp != NULL, "invariant");3540assert(parsed_annotations != NULL, "invariant");35413542// Set inner classes attribute to default sentinel3543_inner_classes = Universe::the_empty_short_array();3544// Set nest members attribute to default sentinel3545_nest_members = Universe::the_empty_short_array();3546// Set _permitted_subclasses attribute to default sentinel3547_permitted_subclasses = Universe::the_empty_short_array();3548cfs->guarantee_more(2, CHECK); // attributes_count3549u2 attributes_count = cfs->get_u2_fast();3550bool parsed_sourcefile_attribute = false;3551bool parsed_innerclasses_attribute = false;3552bool parsed_nest_members_attribute = false;3553bool parsed_permitted_subclasses_attribute = false;3554bool parsed_nest_host_attribute = false;3555bool parsed_record_attribute = false;3556bool parsed_enclosingmethod_attribute = false;3557bool parsed_bootstrap_methods_attribute = false;3558const u1* runtime_visible_annotations = NULL;3559int runtime_visible_annotations_length = 0;3560const u1* runtime_invisible_annotations = NULL;3561int runtime_invisible_annotations_length = 0;3562const u1* runtime_visible_type_annotations = NULL;3563int runtime_visible_type_annotations_length = 0;3564const u1* runtime_invisible_type_annotations = NULL;3565int runtime_invisible_type_annotations_length = 0;3566bool runtime_invisible_type_annotations_exists = false;3567bool runtime_invisible_annotations_exists = false;3568bool parsed_source_debug_ext_annotations_exist = false;3569const u1* inner_classes_attribute_start = NULL;3570u4 inner_classes_attribute_length = 0;3571u2 enclosing_method_class_index = 0;3572u2 enclosing_method_method_index = 0;3573const u1* nest_members_attribute_start = NULL;3574u4 nest_members_attribute_length = 0;3575const u1* record_attribute_start = NULL;3576u4 record_attribute_length = 0;3577const u1* permitted_subclasses_attribute_start = NULL;3578u4 permitted_subclasses_attribute_length = 0;35793580// Iterate over attributes3581while (attributes_count--) {3582cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length3583const u2 attribute_name_index = cfs->get_u2_fast();3584const u4 attribute_length = cfs->get_u4_fast();3585check_property(3586valid_symbol_at(attribute_name_index),3587"Attribute name has bad constant pool index %u in class file %s",3588attribute_name_index, CHECK);3589const Symbol* const tag = cp->symbol_at(attribute_name_index);3590if (tag == vmSymbols::tag_source_file()) {3591// Check for SourceFile tag3592if (_need_verify) {3593guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);3594}3595if (parsed_sourcefile_attribute) {3596classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);3597return;3598} else {3599parsed_sourcefile_attribute = true;3600}3601parse_classfile_sourcefile_attribute(cfs, CHECK);3602} else if (tag == vmSymbols::tag_source_debug_extension()) {3603// Check for SourceDebugExtension tag3604if (parsed_source_debug_ext_annotations_exist) {3605classfile_parse_error(3606"Multiple SourceDebugExtension attributes in class file %s", THREAD);3607return;3608}3609parsed_source_debug_ext_annotations_exist = true;3610parse_classfile_source_debug_extension_attribute(cfs, (int)attribute_length, CHECK);3611} else if (tag == vmSymbols::tag_inner_classes()) {3612// Check for InnerClasses tag3613if (parsed_innerclasses_attribute) {3614classfile_parse_error("Multiple InnerClasses attributes in class file %s", THREAD);3615return;3616} else {3617parsed_innerclasses_attribute = true;3618}3619inner_classes_attribute_start = cfs->current();3620inner_classes_attribute_length = attribute_length;3621cfs->skip_u1(inner_classes_attribute_length, CHECK);3622} else if (tag == vmSymbols::tag_synthetic()) {3623// Check for Synthetic tag3624// Shouldn't we check that the synthetic flags wasn't already set? - not required in spec3625if (attribute_length != 0) {3626classfile_parse_error(3627"Invalid Synthetic classfile attribute length %u in class file %s",3628attribute_length, THREAD);3629return;3630}3631parse_classfile_synthetic_attribute();3632} else if (tag == vmSymbols::tag_deprecated()) {3633// Check for Deprecated tag - 42761203634if (attribute_length != 0) {3635classfile_parse_error(3636"Invalid Deprecated classfile attribute length %u in class file %s",3637attribute_length, THREAD);3638return;3639}3640} else if (_major_version >= JAVA_1_5_VERSION) {3641if (tag == vmSymbols::tag_signature()) {3642if (_generic_signature_index != 0) {3643classfile_parse_error(3644"Multiple Signature attributes in class file %s", THREAD);3645return;3646}3647if (attribute_length != 2) {3648classfile_parse_error(3649"Wrong Signature attribute length %u in class file %s",3650attribute_length, THREAD);3651return;3652}3653parse_classfile_signature_attribute(cfs, CHECK);3654} else if (tag == vmSymbols::tag_runtime_visible_annotations()) {3655if (runtime_visible_annotations != NULL) {3656classfile_parse_error(3657"Multiple RuntimeVisibleAnnotations attributes in class file %s", THREAD);3658return;3659}3660runtime_visible_annotations_length = attribute_length;3661runtime_visible_annotations = cfs->current();3662assert(runtime_visible_annotations != NULL, "null visible annotations");3663cfs->guarantee_more(runtime_visible_annotations_length, CHECK);3664parse_annotations(cp,3665runtime_visible_annotations,3666runtime_visible_annotations_length,3667parsed_annotations,3668_loader_data,3669_can_access_vm_annotations);3670cfs->skip_u1_fast(runtime_visible_annotations_length);3671} else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {3672if (runtime_invisible_annotations_exists) {3673classfile_parse_error(3674"Multiple RuntimeInvisibleAnnotations attributes in class file %s", THREAD);3675return;3676}3677runtime_invisible_annotations_exists = true;3678if (PreserveAllAnnotations) {3679runtime_invisible_annotations_length = attribute_length;3680runtime_invisible_annotations = cfs->current();3681assert(runtime_invisible_annotations != NULL, "null invisible annotations");3682}3683cfs->skip_u1(attribute_length, CHECK);3684} else if (tag == vmSymbols::tag_enclosing_method()) {3685if (parsed_enclosingmethod_attribute) {3686classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", THREAD);3687return;3688} else {3689parsed_enclosingmethod_attribute = true;3690}3691guarantee_property(attribute_length == 4,3692"Wrong EnclosingMethod attribute length %u in class file %s",3693attribute_length, CHECK);3694cfs->guarantee_more(4, CHECK); // class_index, method_index3695enclosing_method_class_index = cfs->get_u2_fast();3696enclosing_method_method_index = cfs->get_u2_fast();3697if (enclosing_method_class_index == 0) {3698classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", THREAD);3699return;3700}3701// Validate the constant pool indices and types3702check_property(valid_klass_reference_at(enclosing_method_class_index),3703"Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);3704if (enclosing_method_method_index != 0 &&3705(!cp->is_within_bounds(enclosing_method_method_index) ||3706!cp->tag_at(enclosing_method_method_index).is_name_and_type())) {3707classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", THREAD);3708return;3709}3710} else if (tag == vmSymbols::tag_bootstrap_methods() &&3711_major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {3712if (parsed_bootstrap_methods_attribute) {3713classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", THREAD);3714return;3715}3716parsed_bootstrap_methods_attribute = true;3717parse_classfile_bootstrap_methods_attribute(cfs, cp, attribute_length, CHECK);3718} else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) {3719if (runtime_visible_type_annotations != NULL) {3720classfile_parse_error(3721"Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", THREAD);3722return;3723}3724runtime_visible_type_annotations_length = attribute_length;3725runtime_visible_type_annotations = cfs->current();3726assert(runtime_visible_type_annotations != NULL, "null visible type annotations");3727// No need for the VM to parse Type annotations3728cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);3729} else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {3730if (runtime_invisible_type_annotations_exists) {3731classfile_parse_error(3732"Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", THREAD);3733return;3734} else {3735runtime_invisible_type_annotations_exists = true;3736}3737if (PreserveAllAnnotations) {3738runtime_invisible_type_annotations_length = attribute_length;3739runtime_invisible_type_annotations = cfs->current();3740assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");3741}3742cfs->skip_u1(attribute_length, CHECK);3743} else if (_major_version >= JAVA_11_VERSION) {3744if (tag == vmSymbols::tag_nest_members()) {3745// Check for NestMembers tag3746if (parsed_nest_members_attribute) {3747classfile_parse_error("Multiple NestMembers attributes in class file %s", THREAD);3748return;3749} else {3750parsed_nest_members_attribute = true;3751}3752if (parsed_nest_host_attribute) {3753classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", THREAD);3754return;3755}3756nest_members_attribute_start = cfs->current();3757nest_members_attribute_length = attribute_length;3758cfs->skip_u1(nest_members_attribute_length, CHECK);3759} else if (tag == vmSymbols::tag_nest_host()) {3760if (parsed_nest_host_attribute) {3761classfile_parse_error("Multiple NestHost attributes in class file %s", THREAD);3762return;3763} else {3764parsed_nest_host_attribute = true;3765}3766if (parsed_nest_members_attribute) {3767classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", THREAD);3768return;3769}3770if (_need_verify) {3771guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);3772}3773cfs->guarantee_more(2, CHECK);3774u2 class_info_index = cfs->get_u2_fast();3775check_property(3776valid_klass_reference_at(class_info_index),3777"Nest-host class_info_index %u has bad constant type in class file %s",3778class_info_index, CHECK);3779_nest_host = class_info_index;37803781} else if (_major_version >= JAVA_16_VERSION) {3782if (tag == vmSymbols::tag_record()) {3783if (parsed_record_attribute) {3784classfile_parse_error("Multiple Record attributes in class file %s", THREAD);3785return;3786}3787parsed_record_attribute = true;3788record_attribute_start = cfs->current();3789record_attribute_length = attribute_length;3790} else if (_major_version >= JAVA_17_VERSION) {3791if (tag == vmSymbols::tag_permitted_subclasses()) {3792if (parsed_permitted_subclasses_attribute) {3793classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);3794return;3795}3796// Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.3797if (_access_flags.is_final()) {3798classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);3799return;3800}3801parsed_permitted_subclasses_attribute = true;3802permitted_subclasses_attribute_start = cfs->current();3803permitted_subclasses_attribute_length = attribute_length;3804}3805}3806// Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION3807cfs->skip_u1(attribute_length, CHECK);3808} else {3809// Unknown attribute3810cfs->skip_u1(attribute_length, CHECK);3811}3812} else {3813// Unknown attribute3814cfs->skip_u1(attribute_length, CHECK);3815}3816} else {3817// Unknown attribute3818cfs->skip_u1(attribute_length, CHECK);3819}3820}3821_class_annotations = assemble_annotations(runtime_visible_annotations,3822runtime_visible_annotations_length,3823runtime_invisible_annotations,3824runtime_invisible_annotations_length,3825CHECK);3826_class_type_annotations = assemble_annotations(runtime_visible_type_annotations,3827runtime_visible_type_annotations_length,3828runtime_invisible_type_annotations,3829runtime_invisible_type_annotations_length,3830CHECK);38313832if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {3833const u2 num_of_classes = parse_classfile_inner_classes_attribute(3834cfs,3835cp,3836inner_classes_attribute_start,3837parsed_innerclasses_attribute,3838enclosing_method_class_index,3839enclosing_method_method_index,3840CHECK);3841if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {3842guarantee_property(3843inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,3844"Wrong InnerClasses attribute length in class file %s", CHECK);3845}3846}38473848if (parsed_nest_members_attribute) {3849const u2 num_of_classes = parse_classfile_nest_members_attribute(3850cfs,3851nest_members_attribute_start,3852CHECK);3853if (_need_verify) {3854guarantee_property(3855nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,3856"Wrong NestMembers attribute length in class file %s", CHECK);3857}3858}38593860if (parsed_record_attribute) {3861const unsigned int calculated_attr_length = parse_classfile_record_attribute(3862cfs,3863cp,3864record_attribute_start,3865CHECK);3866if (_need_verify) {3867guarantee_property(record_attribute_length == calculated_attr_length,3868"Record attribute has wrong length in class file %s",3869CHECK);3870}3871}38723873if (parsed_permitted_subclasses_attribute) {3874const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(3875cfs,3876permitted_subclasses_attribute_start,3877CHECK);3878if (_need_verify) {3879guarantee_property(3880permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,3881"Wrong PermittedSubclasses attribute length in class file %s", CHECK);3882}3883}38843885if (_max_bootstrap_specifier_index >= 0) {3886guarantee_property(parsed_bootstrap_methods_attribute,3887"Missing BootstrapMethods attribute in class file %s", CHECK);3888}3889}38903891void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {3892assert(k != NULL, "invariant");38933894if (_synthetic_flag)3895k->set_is_synthetic();3896if (_sourcefile_index != 0) {3897k->set_source_file_name_index(_sourcefile_index);3898}3899if (_generic_signature_index != 0) {3900k->set_generic_signature_index(_generic_signature_index);3901}3902if (_sde_buffer != NULL) {3903k->set_source_debug_extension(_sde_buffer, _sde_length);3904}3905}39063907// Create the Annotations object that will3908// hold the annotations array for the Klass.3909void ClassFileParser::create_combined_annotations(TRAPS) {3910if (_class_annotations == NULL &&3911_class_type_annotations == NULL &&3912_fields_annotations == NULL &&3913_fields_type_annotations == NULL) {3914// Don't create the Annotations object unnecessarily.3915return;3916}39173918Annotations* const annotations = Annotations::allocate(_loader_data, CHECK);3919annotations->set_class_annotations(_class_annotations);3920annotations->set_class_type_annotations(_class_type_annotations);3921annotations->set_fields_annotations(_fields_annotations);3922annotations->set_fields_type_annotations(_fields_type_annotations);39233924// This is the Annotations object that will be3925// assigned to InstanceKlass being constructed.3926_combined_annotations = annotations;39273928// The annotations arrays below has been transfered the3929// _combined_annotations so these fields can now be cleared.3930_class_annotations = NULL;3931_class_type_annotations = NULL;3932_fields_annotations = NULL;3933_fields_type_annotations = NULL;3934}39353936// Transfer ownership of metadata allocated to the InstanceKlass.3937void ClassFileParser::apply_parsed_class_metadata(3938InstanceKlass* this_klass,3939int java_fields_count) {3940assert(this_klass != NULL, "invariant");39413942_cp->set_pool_holder(this_klass);3943this_klass->set_constants(_cp);3944this_klass->set_fields(_fields, java_fields_count);3945this_klass->set_methods(_methods);3946this_klass->set_inner_classes(_inner_classes);3947this_klass->set_nest_members(_nest_members);3948this_klass->set_nest_host_index(_nest_host);3949this_klass->set_annotations(_combined_annotations);3950this_klass->set_permitted_subclasses(_permitted_subclasses);3951this_klass->set_record_components(_record_components);3952// Delay the setting of _local_interfaces and _transitive_interfaces until after3953// initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could3954// be shared with _transitive_interfaces and _transitive_interfaces may be shared with3955// its _super. If an OOM occurs while loading the current klass, its _super field3956// may not have been set. When GC tries to free the klass, the _transitive_interfaces3957// may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent3958// dereferences to the deallocated _transitive_interfaces will result in a crash.39593960// Clear out these fields so they don't get deallocated by the destructor3961clear_class_metadata();3962}39633964AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,3965int runtime_visible_annotations_length,3966const u1* const runtime_invisible_annotations,3967int runtime_invisible_annotations_length,3968TRAPS) {3969AnnotationArray* annotations = NULL;3970if (runtime_visible_annotations != NULL ||3971runtime_invisible_annotations != NULL) {3972annotations = MetadataFactory::new_array<u1>(_loader_data,3973runtime_visible_annotations_length +3974runtime_invisible_annotations_length,3975CHECK_(annotations));3976if (runtime_visible_annotations != NULL) {3977for (int i = 0; i < runtime_visible_annotations_length; i++) {3978annotations->at_put(i, runtime_visible_annotations[i]);3979}3980}3981if (runtime_invisible_annotations != NULL) {3982for (int i = 0; i < runtime_invisible_annotations_length; i++) {3983int append = runtime_visible_annotations_length+i;3984annotations->at_put(append, runtime_invisible_annotations[i]);3985}3986}3987}3988return annotations;3989}39903991const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,3992const int super_class_index,3993const bool need_verify,3994TRAPS) {3995assert(cp != NULL, "invariant");3996const InstanceKlass* super_klass = NULL;39973998if (super_class_index == 0) {3999check_property(_class_name == vmSymbols::java_lang_Object(),4000"Invalid superclass index %u in class file %s",4001super_class_index,4002CHECK_NULL);4003} else {4004check_property(valid_klass_reference_at(super_class_index),4005"Invalid superclass index %u in class file %s",4006super_class_index,4007CHECK_NULL);4008// The class name should be legal because it is checked when parsing constant pool.4009// However, make sure it is not an array type.4010bool is_array = false;4011if (cp->tag_at(super_class_index).is_klass()) {4012super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));4013if (need_verify)4014is_array = super_klass->is_array_klass();4015} else if (need_verify) {4016is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);4017}4018if (need_verify) {4019guarantee_property(!is_array,4020"Bad superclass name in class file %s", CHECK_NULL);4021}4022}4023return super_klass;4024}40254026OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {4027_max_nonstatic_oop_maps = max_blocks;4028_nonstatic_oop_map_count = 0;4029if (max_blocks == 0) {4030_nonstatic_oop_maps = NULL;4031} else {4032_nonstatic_oop_maps =4033NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);4034memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);4035}4036}40374038OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {4039assert(_nonstatic_oop_map_count > 0, "Has no oop maps");4040return _nonstatic_oop_maps + (_nonstatic_oop_map_count - 1);4041}40424043// addition of super oop maps4044void OopMapBlocksBuilder::initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {4045assert(nof_blocks && _nonstatic_oop_map_count == 0 &&4046nof_blocks <= _max_nonstatic_oop_maps, "invariant");40474048memcpy(_nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);4049_nonstatic_oop_map_count += nof_blocks;4050}40514052// collection of oops4053void OopMapBlocksBuilder::add(int offset, int count) {4054if (_nonstatic_oop_map_count == 0) {4055_nonstatic_oop_map_count++;4056}4057OopMapBlock* nonstatic_oop_map = last_oop_map();4058if (nonstatic_oop_map->count() == 0) { // Unused map, set it up4059nonstatic_oop_map->set_offset(offset);4060nonstatic_oop_map->set_count(count);4061} else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add4062nonstatic_oop_map->increment_count(count);4063} else { // Need a new one...4064_nonstatic_oop_map_count++;4065assert(_nonstatic_oop_map_count <= _max_nonstatic_oop_maps, "range check");4066nonstatic_oop_map = last_oop_map();4067nonstatic_oop_map->set_offset(offset);4068nonstatic_oop_map->set_count(count);4069}4070}40714072// general purpose copy, e.g. into allocated instanceKlass4073void OopMapBlocksBuilder::copy(OopMapBlock* dst) {4074if (_nonstatic_oop_map_count != 0) {4075memcpy(dst, _nonstatic_oop_maps, sizeof(OopMapBlock) * _nonstatic_oop_map_count);4076}4077}40784079// Sort and compact adjacent blocks4080void OopMapBlocksBuilder::compact() {4081if (_nonstatic_oop_map_count <= 1) {4082return;4083}4084/*4085* Since field layout sneeks in oops before values, we will be able to condense4086* blocks. There is potential to compact between super, own refs and values4087* containing refs.4088*4089* Currently compaction is slightly limited due to values being 8 byte aligned.4090* This may well change: FixMe if it doesn't, the code below is fairly general purpose4091* and maybe it doesn't need to be.4092*/4093qsort(_nonstatic_oop_maps, _nonstatic_oop_map_count, sizeof(OopMapBlock),4094(_sort_Fn)OopMapBlock::compare_offset);4095if (_nonstatic_oop_map_count < 2) {4096return;4097}40984099// Make a temp copy, and iterate through and copy back into the original4100ResourceMark rm;4101OopMapBlock* oop_maps_copy =4102NEW_RESOURCE_ARRAY(OopMapBlock, _nonstatic_oop_map_count);4103OopMapBlock* oop_maps_copy_end = oop_maps_copy + _nonstatic_oop_map_count;4104copy(oop_maps_copy);4105OopMapBlock* nonstatic_oop_map = _nonstatic_oop_maps;4106unsigned int new_count = 1;4107oop_maps_copy++;4108while(oop_maps_copy < oop_maps_copy_end) {4109assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");4110if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {4111nonstatic_oop_map->increment_count(oop_maps_copy->count());4112} else {4113nonstatic_oop_map++;4114new_count++;4115nonstatic_oop_map->set_offset(oop_maps_copy->offset());4116nonstatic_oop_map->set_count(oop_maps_copy->count());4117}4118oop_maps_copy++;4119}4120assert(new_count <= _nonstatic_oop_map_count, "end up with more maps after compact() ?");4121_nonstatic_oop_map_count = new_count;4122}41234124void OopMapBlocksBuilder::print_on(outputStream* st) const {4125st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);4126if (_nonstatic_oop_map_count > 0) {4127OopMapBlock* map = _nonstatic_oop_maps;4128OopMapBlock* last_map = last_oop_map();4129assert(map <= last_map, "Last less than first");4130while (map <= last_map) {4131st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),4132map->offset() + map->offset_span() - heapOopSize, map->count());4133map++;4134}4135}4136}41374138void OopMapBlocksBuilder::print_value_on(outputStream* st) const {4139print_on(st);4140}41414142void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {4143assert(ik != NULL, "invariant");41444145const Klass* const super = ik->super();41464147// Check if this klass has an empty finalize method (i.e. one with return bytecode only),4148// in which case we don't have to register objects as finalizable4149if (!_has_empty_finalizer) {4150if (_has_finalizer ||4151(super != NULL && super->has_finalizer())) {4152ik->set_has_finalizer();4153}4154}41554156#ifdef ASSERT4157bool f = false;4158const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),4159vmSymbols::void_method_signature());4160if (m != NULL && !m->is_empty_method()) {4161f = true;4162}41634164// Spec doesn't prevent agent from redefinition of empty finalizer.4165// Despite the fact that it's generally bad idea and redefined finalizer4166// will not work as expected we shouldn't abort vm in this case4167if (!ik->has_redefined_this_or_super()) {4168assert(ik->has_finalizer() == f, "inconsistent has_finalizer");4169}4170#endif41714172// Check if this klass supports the java.lang.Cloneable interface4173if (vmClasses::Cloneable_klass_loaded()) {4174if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {4175ik->set_is_cloneable();4176}4177}41784179// Check if this klass has a vanilla default constructor4180if (super == NULL) {4181// java.lang.Object has empty default constructor4182ik->set_has_vanilla_constructor();4183} else {4184if (super->has_vanilla_constructor() &&4185_has_vanilla_constructor) {4186ik->set_has_vanilla_constructor();4187}4188#ifdef ASSERT4189bool v = false;4190if (super->has_vanilla_constructor()) {4191const Method* const constructor =4192ik->find_method(vmSymbols::object_initializer_name(),4193vmSymbols::void_method_signature());4194if (constructor != NULL && constructor->is_vanilla_constructor()) {4195v = true;4196}4197}4198assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");4199#endif4200}42014202// If it cannot be fast-path allocated, set a bit in the layout helper.4203// See documentation of InstanceKlass::can_be_fastpath_allocated().4204assert(ik->size_helper() > 0, "layout_helper is initialized");4205if ((!RegisterFinalizersAtInit && ik->has_finalizer())4206|| ik->is_abstract() || ik->is_interface()4207|| (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)4208|| ik->size_helper() >= FastAllocateSizeLimit) {4209// Forbid fast-path allocation.4210const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);4211ik->set_layout_helper(lh);4212}4213}42144215// utility methods for appending an array with check for duplicates42164217static void append_interfaces(GrowableArray<InstanceKlass*>* result,4218const Array<InstanceKlass*>* const ifs) {4219// iterate over new interfaces4220for (int i = 0; i < ifs->length(); i++) {4221InstanceKlass* const e = ifs->at(i);4222assert(e->is_klass() && e->is_interface(), "just checking");4223// add new interface4224result->append_if_missing(e);4225}4226}42274228static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,4229Array<InstanceKlass*>* local_ifs,4230ClassLoaderData* loader_data,4231TRAPS) {4232assert(local_ifs != NULL, "invariant");4233assert(loader_data != NULL, "invariant");42344235// Compute maximum size for transitive interfaces4236int max_transitive_size = 0;4237int super_size = 0;4238// Add superclass transitive interfaces size4239if (super != NULL) {4240super_size = super->transitive_interfaces()->length();4241max_transitive_size += super_size;4242}4243// Add local interfaces' super interfaces4244const int local_size = local_ifs->length();4245for (int i = 0; i < local_size; i++) {4246InstanceKlass* const l = local_ifs->at(i);4247max_transitive_size += l->transitive_interfaces()->length();4248}4249// Finally add local interfaces4250max_transitive_size += local_size;4251// Construct array4252if (max_transitive_size == 0) {4253// no interfaces, use canonicalized array4254return Universe::the_empty_instance_klass_array();4255} else if (max_transitive_size == super_size) {4256// no new local interfaces added, share superklass' transitive interface array4257return super->transitive_interfaces();4258} else if (max_transitive_size == local_size) {4259// only local interfaces added, share local interface array4260return local_ifs;4261} else {4262ResourceMark rm;4263GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);42644265// Copy down from superclass4266if (super != NULL) {4267append_interfaces(result, super->transitive_interfaces());4268}42694270// Copy down from local interfaces' superinterfaces4271for (int i = 0; i < local_size; i++) {4272InstanceKlass* const l = local_ifs->at(i);4273append_interfaces(result, l->transitive_interfaces());4274}4275// Finally add local interfaces4276append_interfaces(result, local_ifs);42774278// length will be less than the max_transitive_size if duplicates were removed4279const int length = result->length();4280assert(length <= max_transitive_size, "just checking");4281Array<InstanceKlass*>* const new_result =4282MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);4283for (int i = 0; i < length; i++) {4284InstanceKlass* const e = result->at(i);4285assert(e != NULL, "just checking");4286new_result->at_put(i, e);4287}4288return new_result;4289}4290}42914292void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {4293assert(this_klass != NULL, "invariant");4294const Klass* const super = this_klass->super();42954296if (super != NULL) {4297const InstanceKlass* super_ik = InstanceKlass::cast(super);42984299if (super->is_final()) {4300classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);4301return;4302}43034304if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {4305classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);4306return;4307}43084309// If the loader is not the boot loader then throw an exception if its4310// superclass is in package jdk.internal.reflect and its loader is not a4311// special reflection class loader4312if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {4313PackageEntry* super_package = super->package();4314if (super_package != NULL &&4315super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&4316!java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {4317ResourceMark rm(THREAD);4318Exceptions::fthrow(4319THREAD_AND_LOCATION,4320vmSymbols::java_lang_IllegalAccessError(),4321"class %s loaded by %s cannot access jdk/internal/reflect superclass %s",4322this_klass->external_name(),4323this_klass->class_loader_data()->loader_name_and_id(),4324super->external_name());4325return;4326}4327}43284329Reflection::VerifyClassAccessResults vca_result =4330Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);4331if (vca_result != Reflection::ACCESS_OK) {4332ResourceMark rm(THREAD);4333char* msg = Reflection::verify_class_access_msg(this_klass,4334InstanceKlass::cast(super),4335vca_result);4336if (msg == NULL) {4337bool same_module = (this_klass->module() == super->module());4338Exceptions::fthrow(4339THREAD_AND_LOCATION,4340vmSymbols::java_lang_IllegalAccessError(),4341"class %s cannot access its %ssuperclass %s (%s%s%s)",4342this_klass->external_name(),4343super->is_abstract() ? "abstract " : "",4344super->external_name(),4345(same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),4346(same_module) ? "" : "; ",4347(same_module) ? "" : super->class_in_module_of_loader());4348} else {4349// Add additional message content.4350Exceptions::fthrow(4351THREAD_AND_LOCATION,4352vmSymbols::java_lang_IllegalAccessError(),4353"superclass access check failed: %s",4354msg);4355}4356}4357}4358}435943604361void ClassFileParser::check_super_interface_access(const InstanceKlass* this_klass, TRAPS) {4362assert(this_klass != NULL, "invariant");4363const Array<InstanceKlass*>* const local_interfaces = this_klass->local_interfaces();4364const int lng = local_interfaces->length();4365for (int i = lng - 1; i >= 0; i--) {4366InstanceKlass* const k = local_interfaces->at(i);4367assert (k != NULL && k->is_interface(), "invalid interface");43684369if (k->is_sealed() && !k->has_as_permitted_subclass(this_klass)) {4370classfile_icce_error(this_klass->is_interface() ?4371"class %s cannot extend sealed interface %s" :4372"class %s cannot implement sealed interface %s",4373k, THREAD);4374return;4375}43764377Reflection::VerifyClassAccessResults vca_result =4378Reflection::verify_class_access(this_klass, k, false);4379if (vca_result != Reflection::ACCESS_OK) {4380ResourceMark rm(THREAD);4381char* msg = Reflection::verify_class_access_msg(this_klass,4382k,4383vca_result);4384if (msg == NULL) {4385bool same_module = (this_klass->module() == k->module());4386Exceptions::fthrow(4387THREAD_AND_LOCATION,4388vmSymbols::java_lang_IllegalAccessError(),4389"class %s cannot access its superinterface %s (%s%s%s)",4390this_klass->external_name(),4391k->external_name(),4392(same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(),4393(same_module) ? "" : "; ",4394(same_module) ? "" : k->class_in_module_of_loader());4395} else {4396// Add additional message content.4397Exceptions::fthrow(4398THREAD_AND_LOCATION,4399vmSymbols::java_lang_IllegalAccessError(),4400"superinterface check failed: %s",4401msg);4402}4403}4404}4405}440644074408static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) {4409assert(this_klass != NULL, "invariant");4410const Array<Method*>* const methods = this_klass->methods();4411const int num_methods = methods->length();44124413// go thru each method and check if it overrides a final method4414for (int index = 0; index < num_methods; index++) {4415const Method* const m = methods->at(index);44164417// skip private, static, and <init> methods4418if ((!m->is_private() && !m->is_static()) &&4419(m->name() != vmSymbols::object_initializer_name())) {44204421const Symbol* const name = m->name();4422const Symbol* const signature = m->signature();4423const Klass* k = this_klass->super();4424const Method* super_m = NULL;4425while (k != NULL) {4426// skip supers that don't have final methods.4427if (k->has_final_method()) {4428// lookup a matching method in the super class hierarchy4429super_m = InstanceKlass::cast(k)->lookup_method(name, signature);4430if (super_m == NULL) {4431break; // didn't find any match; get out4432}44334434if (super_m->is_final() && !super_m->is_static() &&4435!super_m->access_flags().is_private()) {4436// matching method in super is final, and not static or private4437bool can_access = Reflection::verify_member_access(this_klass,4438super_m->method_holder(),4439super_m->method_holder(),4440super_m->access_flags(),4441false, false, CHECK);4442if (can_access) {4443// this class can access super final method and therefore override4444ResourceMark rm(THREAD);4445THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),4446err_msg("class %s overrides final method %s.%s%s",4447this_klass->external_name(),4448super_m->method_holder()->external_name(),4449name->as_C_string(),4450signature->as_C_string()));4451}4452}44534454// continue to look from super_m's holder's super.4455k = super_m->method_holder()->super();4456continue;4457}44584459k = k->super();4460}4461}4462}4463}446444654466// assumes that this_klass is an interface4467static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) {4468assert(this_klass != NULL, "invariant");4469assert(this_klass->is_interface(), "not an interface");4470const Array<Method*>* methods = this_klass->methods();4471const int num_methods = methods->length();44724473for (int index = 0; index < num_methods; index++) {4474const Method* const m = methods->at(index);4475// if m is static and not the init method, throw a verify error4476if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {4477ResourceMark rm(THREAD);4478Exceptions::fthrow(4479THREAD_AND_LOCATION,4480vmSymbols::java_lang_VerifyError(),4481"Illegal static method %s in interface %s",4482m->name()->as_C_string(),4483this_klass->external_name()4484);4485return;4486}4487}4488}44894490// utility methods for format checking44914492void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {4493const bool is_module = (flags & JVM_ACC_MODULE) != 0;4494assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");4495if (is_module) {4496ResourceMark rm(THREAD);4497Exceptions::fthrow(4498THREAD_AND_LOCATION,4499vmSymbols::java_lang_NoClassDefFoundError(),4500"%s is not a class because access_flag ACC_MODULE is set",4501_class_name->as_C_string());4502return;4503}45044505if (!_need_verify) { return; }45064507const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;4508const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;4509const bool is_final = (flags & JVM_ACC_FINAL) != 0;4510const bool is_super = (flags & JVM_ACC_SUPER) != 0;4511const bool is_enum = (flags & JVM_ACC_ENUM) != 0;4512const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;4513const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;4514const bool major_gte_14 = _major_version >= JAVA_14_VERSION;45154516if ((is_abstract && is_final) ||4517(is_interface && !is_abstract) ||4518(is_interface && major_gte_1_5 && (is_super || is_enum)) ||4519(!is_interface && major_gte_1_5 && is_annotation)) {4520ResourceMark rm(THREAD);4521Exceptions::fthrow(4522THREAD_AND_LOCATION,4523vmSymbols::java_lang_ClassFormatError(),4524"Illegal class modifiers in class %s: 0x%X",4525_class_name->as_C_string(), flags4526);4527return;4528}4529}45304531static bool has_illegal_visibility(jint flags) {4532const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4533const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4534const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;45354536return ((is_public && is_protected) ||4537(is_public && is_private) ||4538(is_protected && is_private));4539}45404541// A legal major_version.minor_version must be one of the following:4542//4543// Major_version >= 45 and major_version < 56, any minor_version.4544// Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.4545// Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.4546//4547void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){4548ResourceMark rm(THREAD);4549const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;4550if (major < JAVA_MIN_SUPPORTED_VERSION) {4551classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid major version",4552class_name, major, minor, THREAD);4553return;4554}45554556if (major > max_version) {4557Exceptions::fthrow(4558THREAD_AND_LOCATION,4559vmSymbols::java_lang_UnsupportedClassVersionError(),4560"%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "4561"this version of the Java Runtime only recognizes class file versions up to %u.0",4562class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);4563return;4564}45654566if (major < JAVA_12_VERSION || minor == 0) {4567return;4568}45694570if (minor == JAVA_PREVIEW_MINOR_VERSION) {4571if (major != max_version) {4572Exceptions::fthrow(4573THREAD_AND_LOCATION,4574vmSymbols::java_lang_UnsupportedClassVersionError(),4575"%s (class file version %u.%u) was compiled with preview features that are unsupported. "4576"This version of the Java Runtime only recognizes preview features for class file version %u.%u",4577class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);4578return;4579}45804581if (!Arguments::enable_preview()) {4582classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",4583class_name, major, minor, THREAD);4584return;4585}45864587} else { // minor != JAVA_PREVIEW_MINOR_VERSION4588classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",4589class_name, major, minor, THREAD);4590}4591}45924593void ClassFileParser::verify_legal_field_modifiers(jint flags,4594bool is_interface,4595TRAPS) const {4596if (!_need_verify) { return; }45974598const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4599const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4600const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;4601const bool is_static = (flags & JVM_ACC_STATIC) != 0;4602const bool is_final = (flags & JVM_ACC_FINAL) != 0;4603const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;4604const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;4605const bool is_enum = (flags & JVM_ACC_ENUM) != 0;4606const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;46074608bool is_illegal = false;46094610if (is_interface) {4611if (!is_public || !is_static || !is_final || is_private ||4612is_protected || is_volatile || is_transient ||4613(major_gte_1_5 && is_enum)) {4614is_illegal = true;4615}4616} else { // not interface4617if (has_illegal_visibility(flags) || (is_final && is_volatile)) {4618is_illegal = true;4619}4620}46214622if (is_illegal) {4623ResourceMark rm(THREAD);4624Exceptions::fthrow(4625THREAD_AND_LOCATION,4626vmSymbols::java_lang_ClassFormatError(),4627"Illegal field modifiers in class %s: 0x%X",4628_class_name->as_C_string(), flags);4629return;4630}4631}46324633void ClassFileParser::verify_legal_method_modifiers(jint flags,4634bool is_interface,4635const Symbol* name,4636TRAPS) const {4637if (!_need_verify) { return; }46384639const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;4640const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;4641const bool is_static = (flags & JVM_ACC_STATIC) != 0;4642const bool is_final = (flags & JVM_ACC_FINAL) != 0;4643const bool is_native = (flags & JVM_ACC_NATIVE) != 0;4644const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;4645const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;4646const bool is_strict = (flags & JVM_ACC_STRICT) != 0;4647const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;4648const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;4649const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;4650const bool major_gte_8 = _major_version >= JAVA_8_VERSION;4651const bool major_gte_17 = _major_version >= JAVA_17_VERSION;4652const bool is_initializer = (name == vmSymbols::object_initializer_name());46534654bool is_illegal = false;46554656if (is_interface) {4657if (major_gte_8) {4658// Class file version is JAVA_8_VERSION or later Methods of4659// interfaces may set any of the flags except ACC_PROTECTED,4660// ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must4661// have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.4662if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */4663(is_native || is_protected || is_final || is_synchronized) ||4664// If a specific method of a class or interface has its4665// ACC_ABSTRACT flag set, it must not have any of its4666// ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,4667// ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to4668// check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as4669// those flags are illegal irrespective of ACC_ABSTRACT being set or not.4670(is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {4671is_illegal = true;4672}4673} else if (major_gte_1_5) {4674// Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)4675if (!is_public || is_private || is_protected || is_static || is_final ||4676is_synchronized || is_native || !is_abstract || is_strict) {4677is_illegal = true;4678}4679} else {4680// Class file version is pre-JAVA_1_5_VERSION4681if (!is_public || is_static || is_final || is_native || !is_abstract) {4682is_illegal = true;4683}4684}4685} else { // not interface4686if (has_illegal_visibility(flags)) {4687is_illegal = true;4688} else {4689if (is_initializer) {4690if (is_static || is_final || is_synchronized || is_native ||4691is_abstract || (major_gte_1_5 && is_bridge)) {4692is_illegal = true;4693}4694} else { // not initializer4695if (is_abstract) {4696if ((is_final || is_native || is_private || is_static ||4697(major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {4698is_illegal = true;4699}4700}4701}4702}4703}47044705if (is_illegal) {4706ResourceMark rm(THREAD);4707Exceptions::fthrow(4708THREAD_AND_LOCATION,4709vmSymbols::java_lang_ClassFormatError(),4710"Method %s in class %s has illegal modifiers: 0x%X",4711name->as_C_string(), _class_name->as_C_string(), flags);4712return;4713}4714}47154716void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,4717int length,4718TRAPS) const {4719assert(_need_verify, "only called when _need_verify is true");4720if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {4721classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);4722}4723}47244725// Unqualified names may not contain the characters '.', ';', '[', or '/'.4726// In class names, '/' separates unqualified names. This is verified in this function also.4727// Method names also may not contain the characters '<' or '>', unless <init>4728// or <clinit>. Note that method names may not be <init> or <clinit> in this4729// method. Because these names have been checked as special cases before4730// calling this method in verify_legal_method_name.4731//4732// This method is also called from the modular system APIs in modules.cpp4733// to verify the validity of module and package names.4734bool ClassFileParser::verify_unqualified_name(const char* name,4735unsigned int length,4736int type) {4737if (length == 0) return false; // Must have at least one char.4738for (const char* p = name; p != name + length; p++) {4739switch(*p) {4740case JVM_SIGNATURE_DOT:4741case JVM_SIGNATURE_ENDCLASS:4742case JVM_SIGNATURE_ARRAY:4743// do not permit '.', ';', or '['4744return false;4745case JVM_SIGNATURE_SLASH:4746// check for '//' or leading or trailing '/' which are not legal4747// unqualified name must not be empty4748if (type == ClassFileParser::LegalClass) {4749if (p == name || p+1 >= name+length ||4750*(p+1) == JVM_SIGNATURE_SLASH) {4751return false;4752}4753} else {4754return false; // do not permit '/' unless it's class name4755}4756break;4757case JVM_SIGNATURE_SPECIAL:4758case JVM_SIGNATURE_ENDSPECIAL:4759// do not permit '<' or '>' in method names4760if (type == ClassFileParser::LegalMethod) {4761return false;4762}4763}4764}4765return true;4766}47674768// Take pointer to a UTF8 byte string (not NUL-terminated).4769// Skip over the longest part of the string that could4770// be taken as a fieldname. Allow '/' if slash_ok is true.4771// Return a pointer to just past the fieldname.4772// Return NULL if no fieldname at all was found, or in the case of slash_ok4773// being true, we saw consecutive slashes (meaning we were looking for a4774// qualified path but found something that was badly-formed).4775static const char* skip_over_field_name(const char* const name,4776bool slash_ok,4777unsigned int length) {4778const char* p;4779jboolean last_is_slash = false;4780jboolean not_first_ch = false;47814782for (p = name; p != name + length; not_first_ch = true) {4783const char* old_p = p;4784jchar ch = *p;4785if (ch < 128) {4786p++;4787// quick check for ascii4788if ((ch >= 'a' && ch <= 'z') ||4789(ch >= 'A' && ch <= 'Z') ||4790(ch == '_' || ch == '$') ||4791(not_first_ch && ch >= '0' && ch <= '9')) {4792last_is_slash = false;4793continue;4794}4795if (slash_ok && ch == JVM_SIGNATURE_SLASH) {4796if (last_is_slash) {4797return NULL; // Don't permit consecutive slashes4798}4799last_is_slash = true;4800continue;4801}4802}4803else {4804jint unicode_ch;4805char* tmp_p = UTF8::next_character(p, &unicode_ch);4806p = tmp_p;4807last_is_slash = false;4808// Check if ch is Java identifier start or is Java identifier part4809// 4672820: call java.lang.Character methods directly without generating separate tables.4810EXCEPTION_MARK;4811// return value4812JavaValue result(T_BOOLEAN);4813// Set up the arguments to isJavaIdentifierStart or isJavaIdentifierPart4814JavaCallArguments args;4815args.push_int(unicode_ch);48164817if (not_first_ch) {4818// public static boolean isJavaIdentifierPart(char ch);4819JavaCalls::call_static(&result,4820vmClasses::Character_klass(),4821vmSymbols::isJavaIdentifierPart_name(),4822vmSymbols::int_bool_signature(),4823&args,4824THREAD);4825} else {4826// public static boolean isJavaIdentifierStart(char ch);4827JavaCalls::call_static(&result,4828vmClasses::Character_klass(),4829vmSymbols::isJavaIdentifierStart_name(),4830vmSymbols::int_bool_signature(),4831&args,4832THREAD);4833}4834if (HAS_PENDING_EXCEPTION) {4835CLEAR_PENDING_EXCEPTION;4836return NULL;4837}4838if(result.get_jboolean()) {4839continue;4840}4841}4842return (not_first_ch) ? old_p : NULL;4843}4844return (not_first_ch) ? p : NULL;4845}48464847// Take pointer to a UTF8 byte string (not NUL-terminated).4848// Skip over the longest part of the string that could4849// be taken as a field signature. Allow "void" if void_ok.4850// Return a pointer to just past the signature.4851// Return NULL if no legal signature is found.4852const char* ClassFileParser::skip_over_field_signature(const char* signature,4853bool void_ok,4854unsigned int length,4855TRAPS) const {4856unsigned int array_dim = 0;4857while (length > 0) {4858switch (signature[0]) {4859case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }4860case JVM_SIGNATURE_BOOLEAN:4861case JVM_SIGNATURE_BYTE:4862case JVM_SIGNATURE_CHAR:4863case JVM_SIGNATURE_SHORT:4864case JVM_SIGNATURE_INT:4865case JVM_SIGNATURE_FLOAT:4866case JVM_SIGNATURE_LONG:4867case JVM_SIGNATURE_DOUBLE:4868return signature + 1;4869case JVM_SIGNATURE_CLASS: {4870if (_major_version < JAVA_1_5_VERSION) {4871// Skip over the class name if one is there4872const char* const p = skip_over_field_name(signature + 1, true, --length);48734874// The next character better be a semicolon4875if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {4876return p + 1;4877}4878}4879else {4880// Skip leading 'L' and ignore first appearance of ';'4881signature++;4882const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);4883// Format check signature4884if (c != NULL) {4885int newlen = c - (char*) signature;4886bool legal = verify_unqualified_name(signature, newlen, LegalClass);4887if (!legal) {4888classfile_parse_error("Class name is empty or contains illegal character "4889"in descriptor in class file %s",4890THREAD);4891return NULL;4892}4893return signature + newlen + 1;4894}4895}4896return NULL;4897}4898case JVM_SIGNATURE_ARRAY:4899array_dim++;4900if (array_dim > 255) {4901// 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.4902classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", THREAD);4903return NULL;4904}4905// The rest of what's there better be a legal signature4906signature++;4907length--;4908void_ok = false;4909break;4910default:4911return NULL;4912}4913}4914return NULL;4915}49164917// Checks if name is a legal class name.4918void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {4919if (!_need_verify || _relax_verify) { return; }49204921assert(name->refcount() > 0, "symbol must be kept alive");4922char* bytes = (char*)name->bytes();4923unsigned int length = name->utf8_length();4924bool legal = false;49254926if (length > 0) {4927const char* p;4928if (bytes[0] == JVM_SIGNATURE_ARRAY) {4929p = skip_over_field_signature(bytes, false, length, CHECK);4930legal = (p != NULL) && ((p - bytes) == (int)length);4931} else if (_major_version < JAVA_1_5_VERSION) {4932if (bytes[0] != JVM_SIGNATURE_SPECIAL) {4933p = skip_over_field_name(bytes, true, length);4934legal = (p != NULL) && ((p - bytes) == (int)length);4935}4936} else {4937// 4900761: relax the constraints based on JSR202 spec4938// Class names may be drawn from the entire Unicode character set.4939// Identifiers between '/' must be unqualified names.4940// The utf8 string has been verified when parsing cpool entries.4941legal = verify_unqualified_name(bytes, length, LegalClass);4942}4943}4944if (!legal) {4945ResourceMark rm(THREAD);4946assert(_class_name != NULL, "invariant");4947Exceptions::fthrow(4948THREAD_AND_LOCATION,4949vmSymbols::java_lang_ClassFormatError(),4950"Illegal class name \"%.*s\" in class file %s", length, bytes,4951_class_name->as_C_string()4952);4953return;4954}4955}49564957// Checks if name is a legal field name.4958void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const {4959if (!_need_verify || _relax_verify) { return; }49604961char* bytes = (char*)name->bytes();4962unsigned int length = name->utf8_length();4963bool legal = false;49644965if (length > 0) {4966if (_major_version < JAVA_1_5_VERSION) {4967if (bytes[0] != JVM_SIGNATURE_SPECIAL) {4968const char* p = skip_over_field_name(bytes, false, length);4969legal = (p != NULL) && ((p - bytes) == (int)length);4970}4971} else {4972// 4881221: relax the constraints based on JSR202 spec4973legal = verify_unqualified_name(bytes, length, LegalField);4974}4975}49764977if (!legal) {4978ResourceMark rm(THREAD);4979assert(_class_name != NULL, "invariant");4980Exceptions::fthrow(4981THREAD_AND_LOCATION,4982vmSymbols::java_lang_ClassFormatError(),4983"Illegal field name \"%.*s\" in class %s", length, bytes,4984_class_name->as_C_string()4985);4986return;4987}4988}49894990// Checks if name is a legal method name.4991void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {4992if (!_need_verify || _relax_verify) { return; }49934994assert(name != NULL, "method name is null");4995char* bytes = (char*)name->bytes();4996unsigned int length = name->utf8_length();4997bool legal = false;49984999if (length > 0) {5000if (bytes[0] == JVM_SIGNATURE_SPECIAL) {5001if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {5002legal = true;5003}5004} else if (_major_version < JAVA_1_5_VERSION) {5005const char* p;5006p = skip_over_field_name(bytes, false, length);5007legal = (p != NULL) && ((p - bytes) == (int)length);5008} else {5009// 4881221: relax the constraints based on JSR202 spec5010legal = verify_unqualified_name(bytes, length, LegalMethod);5011}5012}50135014if (!legal) {5015ResourceMark rm(THREAD);5016assert(_class_name != NULL, "invariant");5017Exceptions::fthrow(5018THREAD_AND_LOCATION,5019vmSymbols::java_lang_ClassFormatError(),5020"Illegal method name \"%.*s\" in class %s", length, bytes,5021_class_name->as_C_string()5022);5023return;5024}5025}502650275028// Checks if signature is a legal field signature.5029void ClassFileParser::verify_legal_field_signature(const Symbol* name,5030const Symbol* signature,5031TRAPS) const {5032if (!_need_verify) { return; }50335034const char* const bytes = (const char* const)signature->bytes();5035const unsigned int length = signature->utf8_length();5036const char* const p = skip_over_field_signature(bytes, false, length, CHECK);50375038if (p == NULL || (p - bytes) != (int)length) {5039throwIllegalSignature("Field", name, signature, CHECK);5040}5041}50425043// Checks if signature is a legal method signature.5044// Returns number of parameters5045int ClassFileParser::verify_legal_method_signature(const Symbol* name,5046const Symbol* signature,5047TRAPS) const {5048if (!_need_verify) {5049// make sure caller's args_size will be less than 0 even for non-static5050// method so it will be recomputed in compute_size_of_parameters().5051return -2;5052}50535054// Class initializers cannot have args for class format version >= 51.5055if (name == vmSymbols::class_initializer_name() &&5056signature != vmSymbols::void_method_signature() &&5057_major_version >= JAVA_7_VERSION) {5058throwIllegalSignature("Method", name, signature, CHECK_0);5059return 0;5060}50615062unsigned int args_size = 0;5063const char* p = (const char*)signature->bytes();5064unsigned int length = signature->utf8_length();5065const char* nextp;50665067// The first character must be a '('5068if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {5069length--;5070// Skip over legal field signatures5071nextp = skip_over_field_signature(p, false, length, CHECK_0);5072while ((length > 0) && (nextp != NULL)) {5073args_size++;5074if (p[0] == 'J' || p[0] == 'D') {5075args_size++;5076}5077length -= nextp - p;5078p = nextp;5079nextp = skip_over_field_signature(p, false, length, CHECK_0);5080}5081// The first non-signature thing better be a ')'5082if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {5083length--;5084if (name->utf8_length() > 0 && name->char_at(0) == JVM_SIGNATURE_SPECIAL) {5085// All internal methods must return void5086if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {5087return args_size;5088}5089} else {5090// Now we better just have a return value5091nextp = skip_over_field_signature(p, true, length, CHECK_0);5092if (nextp && ((int)length == (nextp - p))) {5093return args_size;5094}5095}5096}5097}5098// Report error5099throwIllegalSignature("Method", name, signature, CHECK_0);5100return 0;5101}51025103int ClassFileParser::static_field_size() const {5104assert(_field_info != NULL, "invariant");5105return _field_info->_static_field_size;5106}51075108int ClassFileParser::total_oop_map_count() const {5109assert(_field_info != NULL, "invariant");5110return _field_info->oop_map_blocks->_nonstatic_oop_map_count;5111}51125113jint ClassFileParser::layout_size() const {5114assert(_field_info != NULL, "invariant");5115return _field_info->_instance_size;5116}51175118static void check_methods_for_intrinsics(const InstanceKlass* ik,5119const Array<Method*>* methods) {5120assert(ik != NULL, "invariant");5121assert(methods != NULL, "invariant");51225123// Set up Method*::intrinsic_id as soon as we know the names of methods.5124// (We used to do this lazily, but now we query it in Rewriter,5125// which is eagerly done for every method, so we might as well do it now,5126// when everything is fresh in memory.)5127const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);51285129if (klass_id != vmSymbolID::NO_SID) {5130for (int j = 0; j < methods->length(); ++j) {5131Method* method = methods->at(j);5132method->init_intrinsic_id(klass_id);51335134if (CheckIntrinsics) {5135// Check if an intrinsic is defined for method 'method',5136// but the method is not annotated with @IntrinsicCandidate.5137if (method->intrinsic_id() != vmIntrinsics::_none &&5138!method->intrinsic_candidate()) {5139tty->print("Compiler intrinsic is defined for method [%s], "5140"but the method is not annotated with @IntrinsicCandidate.%s",5141method->name_and_sig_as_C_string(),5142NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.")5143);5144tty->cr();5145DEBUG_ONLY(vm_exit(1));5146}5147// Check is the method 'method' is annotated with @IntrinsicCandidate,5148// but there is no intrinsic available for it.5149if (method->intrinsic_candidate() &&5150method->intrinsic_id() == vmIntrinsics::_none) {5151tty->print("Method [%s] is annotated with @IntrinsicCandidate, "5152"but no compiler intrinsic is defined for the method.%s",5153method->name_and_sig_as_C_string(),5154NOT_DEBUG("") DEBUG_ONLY(" Exiting.")5155);5156tty->cr();5157DEBUG_ONLY(vm_exit(1));5158}5159}5160} // end for51615162#ifdef ASSERT5163if (CheckIntrinsics) {5164// Check for orphan methods in the current class. A method m5165// of a class C is orphan if an intrinsic is defined for method m,5166// but class C does not declare m.5167// The check is potentially expensive, therefore it is available5168// only in debug builds.51695170for (auto id : EnumRange<vmIntrinsicID>{}) {5171if (vmIntrinsics::_compiledLambdaForm == id) {5172// The _compiledLamdbdaForm intrinsic is a special marker for bytecode5173// generated for the JVM from a LambdaForm and therefore no method5174// is defined for it.5175continue;5176}5177if (vmIntrinsics::_blackhole == id) {5178// The _blackhole intrinsic is a special marker. No explicit method5179// is defined for it.5180continue;5181}51825183if (vmIntrinsics::class_for(id) == klass_id) {5184// Check if the current class contains a method with the same5185// name, flags, signature.5186bool match = false;5187for (int j = 0; j < methods->length(); ++j) {5188const Method* method = methods->at(j);5189if (method->intrinsic_id() == id) {5190match = true;5191break;5192}5193}51945195if (!match) {5196char buf[1000];5197tty->print("Compiler intrinsic is defined for method [%s], "5198"but the method is not available in class [%s].%s",5199vmIntrinsics::short_name_as_C_string(id, buf, sizeof(buf)),5200ik->name()->as_C_string(),5201NOT_DEBUG("") DEBUG_ONLY(" Exiting.")5202);5203tty->cr();5204DEBUG_ONLY(vm_exit(1));5205}5206}5207} // end for5208} // CheckIntrinsics5209#endif // ASSERT5210}5211}52125213InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,5214const ClassInstanceInfo& cl_inst_info,5215TRAPS) {5216if (_klass != NULL) {5217return _klass;5218}52195220InstanceKlass* const ik =5221InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);52225223if (is_hidden()) {5224mangle_hidden_class_name(ik);5225}52265227fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);52285229assert(_klass == ik, "invariant");52305231return ik;5232}52335234void ClassFileParser::fill_instance_klass(InstanceKlass* ik,5235bool changed_by_loadhook,5236const ClassInstanceInfo& cl_inst_info,5237TRAPS) {5238assert(ik != NULL, "invariant");52395240// Set name and CLD before adding to CLD5241ik->set_class_loader_data(_loader_data);5242ik->set_name(_class_name);52435244// Add all classes to our internal class loader list here,5245// including classes in the bootstrap (NULL) class loader.5246const bool publicize = !is_internal();52475248_loader_data->add_class(ik, publicize);52495250set_klass_to_deallocate(ik);52515252assert(_field_info != NULL, "invariant");5253assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");5254assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,5255"sanity");52565257assert(ik->is_instance_klass(), "sanity");5258assert(ik->size_helper() == _field_info->_instance_size, "sanity");52595260// Fill in information already parsed5261ik->set_should_verify_class(_need_verify);52625263// Not yet: supers are done below to support the new subtype-checking fields5264ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);5265ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);5266assert(_fac != NULL, "invariant");5267ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);52685269// this transfers ownership of a lot of arrays from5270// the parser onto the InstanceKlass*5271apply_parsed_class_metadata(ik, _java_fields_count);52725273// can only set dynamic nest-host after static nest information is set5274if (cl_inst_info.dynamic_nest_host() != NULL) {5275ik->set_nest_host(cl_inst_info.dynamic_nest_host());5276}52775278// note that is not safe to use the fields in the parser from this point on5279assert(NULL == _cp, "invariant");5280assert(NULL == _fields, "invariant");5281assert(NULL == _methods, "invariant");5282assert(NULL == _inner_classes, "invariant");5283assert(NULL == _nest_members, "invariant");5284assert(NULL == _combined_annotations, "invariant");5285assert(NULL == _record_components, "invariant");5286assert(NULL == _permitted_subclasses, "invariant");52875288if (_has_final_method) {5289ik->set_has_final_method();5290}52915292ik->copy_method_ordering(_method_ordering, CHECK);5293// The InstanceKlass::_methods_jmethod_ids cache5294// is managed on the assumption that the initial cache5295// size is equal to the number of methods in the class. If5296// that changes, then InstanceKlass::idnum_can_increment()5297// has to be changed accordingly.5298ik->set_initial_method_idnum(ik->methods()->length());52995300ik->set_this_class_index(_this_class_index);53015302if (_is_hidden) {5303// _this_class_index is a CONSTANT_Class entry that refers to this5304// hidden class itself. If this class needs to refer to its own methods5305// or fields, it would use a CONSTANT_MethodRef, etc, which would reference5306// _this_class_index. However, because this class is hidden (it's5307// not stored in SystemDictionary), _this_class_index cannot be resolved5308// with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.5309// Therefore, we must eagerly resolve _this_class_index now.5310ik->constants()->klass_at_put(_this_class_index, ik);5311}53125313ik->set_minor_version(_minor_version);5314ik->set_major_version(_major_version);5315ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);5316ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);53175318if (_is_hidden) {5319ik->set_is_hidden();5320}53215322// Set PackageEntry for this_klass5323oop cl = ik->class_loader();5324Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));5325ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());5326ik->set_package(cld, NULL, CHECK);53275328const Array<Method*>* const methods = ik->methods();5329assert(methods != NULL, "invariant");5330const int methods_len = methods->length();53315332check_methods_for_intrinsics(ik, methods);53335334// Fill in field values obtained by parse_classfile_attributes5335if (_parsed_annotations->has_any_annotations()) {5336_parsed_annotations->apply_to(ik);5337}53385339apply_parsed_class_attributes(ik);53405341// Miranda methods5342if ((_num_miranda_methods > 0) ||5343// if this class introduced new miranda methods or5344(_super_klass != NULL && _super_klass->has_miranda_methods())5345// super class exists and this class inherited miranda methods5346) {5347ik->set_has_miranda_methods(); // then set a flag5348}53495350// Fill in information needed to compute superclasses.5351ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);5352ik->set_transitive_interfaces(_transitive_interfaces);5353ik->set_local_interfaces(_local_interfaces);5354_transitive_interfaces = NULL;5355_local_interfaces = NULL;53565357// Initialize itable offset tables5358klassItable::setup_itable_offset_table(ik);53595360// Compute transitive closure of interfaces this class implements5361// Do final class setup5362OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;5363if (oop_map_blocks->_nonstatic_oop_map_count > 0) {5364oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());5365}53665367if (_has_contended_fields || _parsed_annotations->is_contended() ||5368( _super_klass != NULL && _super_klass->has_contended_annotations())) {5369ik->set_has_contended_annotations(true);5370}53715372// Fill in has_finalizer, has_vanilla_constructor, and layout_helper5373set_precomputed_flags(ik);53745375// check if this class can access its super class5376check_super_class_access(ik, CHECK);53775378// check if this class can access its superinterfaces5379check_super_interface_access(ik, CHECK);53805381// check if this class overrides any final method5382check_final_method_override(ik, CHECK);53835384// reject static interface methods prior to Java 85385if (ik->is_interface() && _major_version < JAVA_8_VERSION) {5386check_illegal_static_method(ik, CHECK);5387}53885389// Obtain this_klass' module entry5390ModuleEntry* module_entry = ik->module();5391assert(module_entry != NULL, "module_entry should always be set");53925393// Obtain java.lang.Module5394Handle module_handle(THREAD, module_entry->module());53955396// Allocate mirror and initialize static fields5397// The create_mirror() call will also call compute_modifiers()5398java_lang_Class::create_mirror(ik,5399Handle(THREAD, _loader_data->class_loader()),5400module_handle,5401_protection_domain,5402cl_inst_info.class_data(),5403CHECK);54045405assert(_all_mirandas != NULL, "invariant");54065407// Generate any default methods - default methods are public interface methods5408// that have a default implementation. This is new with Java 8.5409if (_has_nonstatic_concrete_methods) {5410DefaultMethods::generate_default_methods(ik,5411_all_mirandas,5412CHECK);5413}54145415// Add read edges to the unnamed modules of the bootstrap and app class loaders.5416if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&5417!module_entry->has_default_read_edges()) {5418if (!module_entry->set_has_default_read_edges()) {5419// We won a potential race5420JvmtiExport::add_default_read_edges(module_handle, THREAD);5421}5422}54235424ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);54255426if (!is_internal()) {5427ik->print_class_load_logging(_loader_data, module_entry, _stream);54285429if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&5430ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&5431log_is_enabled(Info, class, preview)) {5432ResourceMark rm;5433log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",5434ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);5435}54365437if (log_is_enabled(Debug, class, resolve)) {5438ResourceMark rm;5439// print out the superclass.5440const char * from = ik->external_name();5441if (ik->java_super() != NULL) {5442log_debug(class, resolve)("%s %s (super)",5443from,5444ik->java_super()->external_name());5445}5446// print out each of the interface classes referred to by this class.5447const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces();5448if (local_interfaces != NULL) {5449const int length = local_interfaces->length();5450for (int i = 0; i < length; i++) {5451const InstanceKlass* const k = local_interfaces->at(i);5452const char * to = k->external_name();5453log_debug(class, resolve)("%s %s (interface)", from, to);5454}5455}5456}5457}54585459JFR_ONLY(INIT_ID(ik);)54605461// If we reach here, all is well.5462// Now remove the InstanceKlass* from the _klass_to_deallocate field5463// in order for it to not be destroyed in the ClassFileParser destructor.5464set_klass_to_deallocate(NULL);54655466// it's official5467set_klass(ik);54685469debug_only(ik->verify();)5470}54715472void ClassFileParser::update_class_name(Symbol* new_class_name) {5473// Decrement the refcount in the old name, since we're clobbering it.5474_class_name->decrement_refcount();54755476_class_name = new_class_name;5477// Increment the refcount of the new name.5478// Now the ClassFileParser owns this name and will decrement in5479// the destructor.5480_class_name->increment_refcount();5481}54825483static bool relax_format_check_for(ClassLoaderData* loader_data) {5484bool trusted = loader_data->is_boot_class_loader_data() ||5485loader_data->is_platform_class_loader_data();5486bool need_verify =5487// verifyAll5488(BytecodeVerificationLocal && BytecodeVerificationRemote) ||5489// verifyRemote5490(!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);5491return !need_verify;5492}54935494ClassFileParser::ClassFileParser(ClassFileStream* stream,5495Symbol* name,5496ClassLoaderData* loader_data,5497const ClassLoadInfo* cl_info,5498Publicity pub_level,5499TRAPS) :5500_stream(stream),5501_class_name(NULL),5502_loader_data(loader_data),5503_is_hidden(cl_info->is_hidden()),5504_can_access_vm_annotations(cl_info->can_access_vm_annotations()),5505_orig_cp_size(0),5506_super_klass(),5507_cp(NULL),5508_fields(NULL),5509_methods(NULL),5510_inner_classes(NULL),5511_nest_members(NULL),5512_nest_host(0),5513_permitted_subclasses(NULL),5514_record_components(NULL),5515_local_interfaces(NULL),5516_transitive_interfaces(NULL),5517_combined_annotations(NULL),5518_class_annotations(NULL),5519_class_type_annotations(NULL),5520_fields_annotations(NULL),5521_fields_type_annotations(NULL),5522_klass(NULL),5523_klass_to_deallocate(NULL),5524_parsed_annotations(NULL),5525_fac(NULL),5526_field_info(NULL),5527_method_ordering(NULL),5528_all_mirandas(NULL),5529_vtable_size(0),5530_itable_size(0),5531_num_miranda_methods(0),5532_rt(REF_NONE),5533_protection_domain(cl_info->protection_domain()),5534_access_flags(),5535_pub_level(pub_level),5536_bad_constant_seen(0),5537_synthetic_flag(false),5538_sde_length(false),5539_sde_buffer(NULL),5540_sourcefile_index(0),5541_generic_signature_index(0),5542_major_version(0),5543_minor_version(0),5544_this_class_index(0),5545_super_class_index(0),5546_itfs_len(0),5547_java_fields_count(0),5548_need_verify(false),5549_relax_verify(false),5550_has_nonstatic_concrete_methods(false),5551_declares_nonstatic_concrete_methods(false),5552_has_final_method(false),5553_has_contended_fields(false),5554_has_finalizer(false),5555_has_empty_finalizer(false),5556_has_vanilla_constructor(false),5557_max_bootstrap_specifier_index(-1) {55585559_class_name = name != NULL ? name : vmSymbols::unknown_class_name();5560_class_name->increment_refcount();55615562assert(_loader_data != NULL, "invariant");5563assert(stream != NULL, "invariant");5564assert(_stream != NULL, "invariant");5565assert(_stream->buffer() == _stream->current(), "invariant");5566assert(_class_name != NULL, "invariant");5567assert(0 == _access_flags.as_int(), "invariant");55685569// Figure out whether we can skip format checking (matching classic VM behavior)5570if (DumpSharedSpaces) {5571// verify == true means it's a 'remote' class (i.e., non-boot class)5572// Verification decision is based on BytecodeVerificationRemote flag5573// for those classes.5574_need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :5575BytecodeVerificationLocal;5576}5577else {5578_need_verify = Verifier::should_verify_for(_loader_data->class_loader(),5579stream->need_verify());5580}55815582// synch back verification state to stream5583stream->set_verify(_need_verify);55845585// Check if verification needs to be relaxed for this class file5586// Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)5587_relax_verify = relax_format_check_for(_loader_data);55885589parse_stream(stream, CHECK);55905591post_process_parsed_stream(stream, _cp, CHECK);5592}55935594void ClassFileParser::clear_class_metadata() {5595// metadata created before the instance klass is created. Must be5596// deallocated if classfile parsing returns an error.5597_cp = NULL;5598_fields = NULL;5599_methods = NULL;5600_inner_classes = NULL;5601_nest_members = NULL;5602_permitted_subclasses = NULL;5603_combined_annotations = NULL;5604_class_annotations = _class_type_annotations = NULL;5605_fields_annotations = _fields_type_annotations = NULL;5606_record_components = NULL;5607}56085609// Destructor to clean up5610ClassFileParser::~ClassFileParser() {5611_class_name->decrement_refcount();56125613if (_cp != NULL) {5614MetadataFactory::free_metadata(_loader_data, _cp);5615}5616if (_fields != NULL) {5617MetadataFactory::free_array<u2>(_loader_data, _fields);5618}56195620if (_methods != NULL) {5621// Free methods5622InstanceKlass::deallocate_methods(_loader_data, _methods);5623}56245625// beware of the Universe::empty_blah_array!!5626if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {5627MetadataFactory::free_array<u2>(_loader_data, _inner_classes);5628}56295630if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {5631MetadataFactory::free_array<u2>(_loader_data, _nest_members);5632}56335634if (_record_components != NULL) {5635InstanceKlass::deallocate_record_components(_loader_data, _record_components);5636}56375638if (_permitted_subclasses != NULL && _permitted_subclasses != Universe::the_empty_short_array()) {5639MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);5640}56415642// Free interfaces5643InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,5644_local_interfaces, _transitive_interfaces);56455646if (_combined_annotations != NULL) {5647// After all annotations arrays have been created, they are installed into the5648// Annotations object that will be assigned to the InstanceKlass being created.56495650// Deallocate the Annotations object and the installed annotations arrays.5651_combined_annotations->deallocate_contents(_loader_data);56525653// If the _combined_annotations pointer is non-NULL,5654// then the other annotations fields should have been cleared.5655assert(_class_annotations == NULL, "Should have been cleared");5656assert(_class_type_annotations == NULL, "Should have been cleared");5657assert(_fields_annotations == NULL, "Should have been cleared");5658assert(_fields_type_annotations == NULL, "Should have been cleared");5659} else {5660// If the annotations arrays were not installed into the Annotations object,5661// then they have to be deallocated explicitly.5662MetadataFactory::free_array<u1>(_loader_data, _class_annotations);5663MetadataFactory::free_array<u1>(_loader_data, _class_type_annotations);5664Annotations::free_contents(_loader_data, _fields_annotations);5665Annotations::free_contents(_loader_data, _fields_type_annotations);5666}56675668clear_class_metadata();5669_transitive_interfaces = NULL;5670_local_interfaces = NULL;56715672// deallocate the klass if already created. Don't directly deallocate, but add5673// to the deallocate list so that the klass is removed from the CLD::_klasses list5674// at a safepoint.5675if (_klass_to_deallocate != NULL) {5676_loader_data->add_to_deallocate_list(_klass_to_deallocate);5677}5678}56795680void ClassFileParser::parse_stream(const ClassFileStream* const stream,5681TRAPS) {56825683assert(stream != NULL, "invariant");5684assert(_class_name != NULL, "invariant");56855686// BEGIN STREAM PARSING5687stream->guarantee_more(8, CHECK); // magic, major, minor5688// Magic value5689const u4 magic = stream->get_u4_fast();5690guarantee_property(magic == JAVA_CLASSFILE_MAGIC,5691"Incompatible magic value %u in class file %s",5692magic, CHECK);56935694// Version numbers5695_minor_version = stream->get_u2_fast();5696_major_version = stream->get_u2_fast();56975698// Check version numbers - we check this even with verifier off5699verify_class_version(_major_version, _minor_version, _class_name, CHECK);57005701stream->guarantee_more(3, CHECK); // length, first cp tag5702u2 cp_size = stream->get_u2_fast();57035704guarantee_property(5705cp_size >= 1, "Illegal constant pool size %u in class file %s",5706cp_size, CHECK);57075708_orig_cp_size = cp_size;5709if (is_hidden()) { // Add a slot for hidden class name.5710cp_size++;5711}57125713_cp = ConstantPool::allocate(_loader_data,5714cp_size,5715CHECK);57165717ConstantPool* const cp = _cp;57185719parse_constant_pool(stream, cp, _orig_cp_size, CHECK);57205721assert(cp_size == (const u2)cp->length(), "invariant");57225723// ACCESS FLAGS5724stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len57255726// Access flags5727jint flags;5728// JVM_ACC_MODULE is defined in JDK-9 and later.5729if (_major_version >= JAVA_9_VERSION) {5730flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);5731} else {5732flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;5733}57345735if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {5736// Set abstract bit for old class files for backward compatibility5737flags |= JVM_ACC_ABSTRACT;5738}57395740verify_legal_class_modifiers(flags, CHECK);57415742short bad_constant = class_bad_constant_seen();5743if (bad_constant != 0) {5744// Do not throw CFE until after the access_flags are checked because if5745// ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.5746classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);5747return;5748}57495750_access_flags.set_flags(flags);57515752// This class and superclass5753_this_class_index = stream->get_u2_fast();5754check_property(5755valid_cp_range(_this_class_index, cp_size) &&5756cp->tag_at(_this_class_index).is_unresolved_klass(),5757"Invalid this class index %u in constant pool in class file %s",5758_this_class_index, CHECK);57595760Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);5761assert(class_name_in_cp != NULL, "class_name can't be null");57625763// Don't need to check whether this class name is legal or not.5764// It has been checked when constant pool is parsed.5765// However, make sure it is not an array type.5766if (_need_verify) {5767guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,5768"Bad class name in class file %s",5769CHECK);5770}57715772#ifdef ASSERT5773// Basic sanity checks5774if (_is_hidden) {5775assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");5776}5777#endif57785779// Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.57805781if (_is_hidden) {5782assert(_class_name != NULL, "Unexpected null _class_name");5783#ifdef ASSERT5784if (_need_verify) {5785verify_legal_class_name(_class_name, CHECK);5786}5787#endif57885789} else {5790// Check if name in class file matches given name5791if (_class_name != class_name_in_cp) {5792if (_class_name != vmSymbols::unknown_class_name()) {5793ResourceMark rm(THREAD);5794Exceptions::fthrow(THREAD_AND_LOCATION,5795vmSymbols::java_lang_NoClassDefFoundError(),5796"%s (wrong name: %s)",5797class_name_in_cp->as_C_string(),5798_class_name->as_C_string()5799);5800return;5801} else {5802// The class name was not known by the caller so we set it from5803// the value in the CP.5804update_class_name(class_name_in_cp);5805}5806// else nothing to do: the expected class name matches what is in the CP5807}5808}58095810// Verification prevents us from creating names with dots in them, this5811// asserts that that's the case.5812assert(is_internal_format(_class_name), "external class name format used internally");58135814if (!is_internal()) {5815LogTarget(Debug, class, preorder) lt;5816if (lt.is_enabled()){5817ResourceMark rm(THREAD);5818LogStream ls(lt);5819ls.print("%s", _class_name->as_klass_external_name());5820if (stream->source() != NULL) {5821ls.print(" source: %s", stream->source());5822}5823ls.cr();5824}5825}58265827// SUPERKLASS5828_super_class_index = stream->get_u2_fast();5829_super_klass = parse_super_class(cp,5830_super_class_index,5831_need_verify,5832CHECK);58335834// Interfaces5835_itfs_len = stream->get_u2_fast();5836parse_interfaces(stream,5837_itfs_len,5838cp,5839&_has_nonstatic_concrete_methods,5840CHECK);58415842assert(_local_interfaces != NULL, "invariant");58435844// Fields (offsets are filled in later)5845_fac = new FieldAllocationCount();5846parse_fields(stream,5847_access_flags.is_interface(),5848_fac,5849cp,5850cp_size,5851&_java_fields_count,5852CHECK);58535854assert(_fields != NULL, "invariant");58555856// Methods5857AccessFlags promoted_flags;5858parse_methods(stream,5859_access_flags.is_interface(),5860&promoted_flags,5861&_has_final_method,5862&_declares_nonstatic_concrete_methods,5863CHECK);58645865assert(_methods != NULL, "invariant");58665867// promote flags from parse_methods() to the klass' flags5868_access_flags.add_promoted_flags(promoted_flags.as_int());58695870if (_declares_nonstatic_concrete_methods) {5871_has_nonstatic_concrete_methods = true;5872}58735874// Additional attributes/annotations5875_parsed_annotations = new ClassAnnotationCollector();5876parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);58775878assert(_inner_classes != NULL, "invariant");58795880// Finalize the Annotations metadata object,5881// now that all annotation arrays have been created.5882create_combined_annotations(CHECK);58835884// Make sure this is the end of class file stream5885guarantee_property(stream->at_eos(),5886"Extra bytes at the end of class file %s",5887CHECK);58885889// all bytes in stream read and parsed5890}58915892void ClassFileParser::mangle_hidden_class_name(InstanceKlass* const ik) {5893ResourceMark rm;5894// Construct hidden name from _class_name, "+", and &ik. Note that we can't5895// use a '/' because that confuses finding the class's package. Also, can't5896// use an illegal char such as ';' because that causes serialization issues5897// and issues with hidden classes that create their own hidden classes.5898char addr_buf[20];5899if (DumpSharedSpaces) {5900// We want stable names for the archived hidden classes (only for static5901// archive for now). Spaces under default_SharedBaseAddress() will be5902// occupied by the archive at run time, so we know that no dynamically5903// loaded InstanceKlass will be placed under there.5904static volatile size_t counter = 0;5905Atomic::cmpxchg(&counter, (size_t)0, Arguments::default_SharedBaseAddress()); // initialize it5906size_t new_id = Atomic::add(&counter, (size_t)1);5907jio_snprintf(addr_buf, 20, SIZE_FORMAT_HEX, new_id);5908} else {5909jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(ik));5910}5911size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf);5912char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len);5913jio_snprintf(new_name, new_name_len, "%s+%s",5914_class_name->as_C_string(), addr_buf);5915update_class_name(SymbolTable::new_symbol(new_name));59165917// Add a Utf8 entry containing the hidden name.5918assert(_class_name != NULL, "Unexpected null _class_name");5919int hidden_index = _orig_cp_size; // this is an extra slot we added5920_cp->symbol_at_put(hidden_index, _class_name);59215922// Update this_class_index's slot in the constant pool with the new Utf8 entry.5923// We have to update the resolved_klass_index and the name_index together5924// so extract the existing resolved_klass_index first.5925CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);5926int resolved_klass_index = cp_klass_slot.resolved_klass_index();5927_cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);5928assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,5929"Bad name_index");5930}59315932void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,5933ConstantPool* cp,5934TRAPS) {5935assert(stream != NULL, "invariant");5936assert(stream->at_eos(), "invariant");5937assert(cp != NULL, "invariant");5938assert(_loader_data != NULL, "invariant");59395940if (_class_name == vmSymbols::java_lang_Object()) {5941check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),5942"java.lang.Object cannot implement an interface in class file %s",5943CHECK);5944}5945// We check super class after class file is parsed and format is checked5946if (_super_class_index > 0 && NULL == _super_klass) {5947Symbol* const super_class_name = cp->klass_name_at(_super_class_index);5948if (_access_flags.is_interface()) {5949// Before attempting to resolve the superclass, check for class format5950// errors not checked yet.5951guarantee_property(super_class_name == vmSymbols::java_lang_Object(),5952"Interfaces must have java.lang.Object as superclass in class file %s",5953CHECK);5954}5955Handle loader(THREAD, _loader_data->class_loader());5956_super_klass = (const InstanceKlass*)5957SystemDictionary::resolve_super_or_fail(_class_name,5958super_class_name,5959loader,5960_protection_domain,5961true,5962CHECK);5963}59645965if (_super_klass != NULL) {5966if (_super_klass->has_nonstatic_concrete_methods()) {5967_has_nonstatic_concrete_methods = true;5968}59695970if (_super_klass->is_interface()) {5971classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);5972return;5973}5974}59755976// Compute the transitive list of all unique interfaces implemented by this class5977_transitive_interfaces =5978compute_transitive_interfaces(_super_klass,5979_local_interfaces,5980_loader_data,5981CHECK);59825983assert(_transitive_interfaces != NULL, "invariant");59845985// sort methods5986_method_ordering = sort_methods(_methods);59875988_all_mirandas = new GrowableArray<Method*>(20);59895990Handle loader(THREAD, _loader_data->class_loader());5991klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,5992&_num_miranda_methods,5993_all_mirandas,5994_super_klass,5995_methods,5996_access_flags,5997_major_version,5998loader,5999_class_name,6000_local_interfaces);60016002// Size of Java itable (in words)6003_itable_size = _access_flags.is_interface() ? 0 :6004klassItable::compute_itable_size(_transitive_interfaces);60056006assert(_fac != NULL, "invariant");6007assert(_parsed_annotations != NULL, "invariant");60086009_field_info = new FieldLayoutInfo();6010FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,6011_parsed_annotations->is_contended(), _field_info);6012lb.build_layout();60136014// Compute reference typ6015_rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();60166017}60186019void ClassFileParser::set_klass(InstanceKlass* klass) {60206021#ifdef ASSERT6022if (klass != NULL) {6023assert(NULL == _klass, "leaking?");6024}6025#endif60266027_klass = klass;6028}60296030void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {60316032#ifdef ASSERT6033if (klass != NULL) {6034assert(NULL == _klass_to_deallocate, "leaking?");6035}6036#endif60376038_klass_to_deallocate = klass;6039}60406041// Caller responsible for ResourceMark6042// clone stream with rewound position6043const ClassFileStream* ClassFileParser::clone_stream() const {6044assert(_stream != NULL, "invariant");60456046return _stream->clone();6047}6048// ----------------------------------------------------------------------------6049// debugging60506051#ifdef ASSERT60526053// return true if class_name contains no '.' (internal format is '/')6054bool ClassFileParser::is_internal_format(Symbol* class_name) {6055if (class_name != NULL) {6056ResourceMark rm;6057char* name = class_name->as_C_string();6058return strchr(name, JVM_SIGNATURE_DOT) == NULL;6059} else {6060return true;6061}6062}60636064#endif606560666067