duke@435: /* acorn@4422: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/classFileParser.hpp" stefank@2314: #include "classfile/classLoader.hpp" coleenp@4037: #include "classfile/classLoaderData.hpp" coleenp@4037: #include "classfile/classLoaderData.inline.hpp" kamg@4245: #include "classfile/defaultMethods.hpp" kamg@4245: #include "classfile/genericSignatures.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/verificationType.hpp" stefank@2314: #include "classfile/verifier.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/gcLocker.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" stefank@2314: #include "memory/oopFactory.hpp" sla@5237: #include "memory/referenceType.hpp" stefank@2314: #include "memory/universe.inline.hpp" coleenp@4037: #include "oops/constantPool.hpp" never@3137: #include "oops/fieldStreams.hpp" stefank@2314: #include "oops/instanceKlass.hpp" never@2658: #include "oops/instanceMirrorKlass.hpp" stefank@2314: #include "oops/klass.inline.hpp" stefank@2314: #include "oops/klassVtable.hpp" coleenp@4037: #include "oops/method.hpp" coleenp@2497: #include "oops/symbol.hpp" coleenp@4037: #include "prims/jvm.h" stefank@2314: #include "prims/jvmtiExport.hpp" dcubed@3360: #include "prims/jvmtiThreadState.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/perfData.hpp" stefank@2314: #include "runtime/reflection.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #include "runtime/timer.hpp" stefank@2314: #include "services/classLoadingService.hpp" stefank@2314: #include "services/threadService.hpp" coleenp@4037: #include "utilities/array.hpp" coleenp@4431: #include "utilities/globalDefinitions.hpp" duke@435: kamg@1941: // We generally try to create the oops directly when parsing, rather than kamg@1941: // allocating temporary data structures and copying the bytes twice. A kamg@1941: // temporary area is only needed when parsing utf8 entries in the constant kamg@1941: // pool and when parsing line number tables. duke@435: duke@435: // We add assert in debug mode when class format is not checked. duke@435: duke@435: #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE duke@435: #define JAVA_MIN_SUPPORTED_VERSION 45 kamg@4179: #define JAVA_MAX_SUPPORTED_VERSION 52 duke@435: #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0 duke@435: duke@435: // Used for two backward compatibility reasons: duke@435: // - to check for new additions to the class file format in JDK1.5 duke@435: // - to check for bug fixes in the format checker in JDK1.5 duke@435: #define JAVA_1_5_VERSION 49 duke@435: duke@435: // Used for backward compatibility reasons: duke@435: // - to check for javac bug fixes that happened after 1.5 kamg@611: // - also used as the max version when running in jdk6 duke@435: #define JAVA_6_VERSION 50 duke@435: kamg@1941: // Used for backward compatibility reasons: kamg@1941: // - to check NameAndType_info signatures more aggressively kamg@1941: #define JAVA_7_VERSION 51 kamg@1941: kamg@4245: // Extension method support. kamg@4245: #define JAVA_8_VERSION 52 kamg@4245: coleenp@4719: void ClassFileParser::parse_constant_pool_entries(int length, TRAPS) { duke@435: // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize duke@435: // this function (_current can be allocated in a register, with scalar duke@435: // replacement of aggregates). The _current pointer is copied back to duke@435: // stream() when this function returns. DON'T call another method within duke@435: // this method that uses stream(). duke@435: ClassFileStream* cfs0 = stream(); duke@435: ClassFileStream cfs1 = *cfs0; duke@435: ClassFileStream* cfs = &cfs1; duke@435: #ifdef ASSERT kvn@2040: assert(cfs->allocated_on_stack(),"should be local"); duke@435: u1* old_current = cfs0->current(); duke@435: #endif coleenp@4719: Handle class_loader(THREAD, _loader_data->class_loader()); duke@435: duke@435: // Used for batching symbol allocations. duke@435: const char* names[SymbolTable::symbol_alloc_batch_size]; duke@435: int lengths[SymbolTable::symbol_alloc_batch_size]; duke@435: int indices[SymbolTable::symbol_alloc_batch_size]; duke@435: unsigned int hashValues[SymbolTable::symbol_alloc_batch_size]; duke@435: int names_count = 0; duke@435: duke@435: // parsing Index 0 is unused duke@435: for (int index = 1; index < length; index++) { duke@435: // Each of the following case guarantees one more byte in the stream duke@435: // for the following tag or the access_flags following constant pool, duke@435: // so we don't need bounds-check for reading tag. duke@435: u1 tag = cfs->get_u1_fast(); duke@435: switch (tag) { duke@435: case JVM_CONSTANT_Class : duke@435: { duke@435: cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags duke@435: u2 name_index = cfs->get_u2_fast(); coleenp@4719: _cp->klass_index_at_put(index, name_index); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_Fieldref : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags duke@435: u2 class_index = cfs->get_u2_fast(); duke@435: u2 name_and_type_index = cfs->get_u2_fast(); coleenp@4719: _cp->field_at_put(index, class_index, name_and_type_index); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_Methodref : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags duke@435: u2 class_index = cfs->get_u2_fast(); duke@435: u2 name_and_type_index = cfs->get_u2_fast(); coleenp@4719: _cp->method_at_put(index, class_index, name_and_type_index); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_InterfaceMethodref : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags duke@435: u2 class_index = cfs->get_u2_fast(); duke@435: u2 name_and_type_index = cfs->get_u2_fast(); coleenp@4719: _cp->interface_method_at_put(index, class_index, name_and_type_index); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_String : duke@435: { duke@435: cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags duke@435: u2 string_index = cfs->get_u2_fast(); coleenp@4719: _cp->string_index_at_put(index, string_index); duke@435: } duke@435: break; jrose@1957: case JVM_CONSTANT_MethodHandle : jrose@1957: case JVM_CONSTANT_MethodType : jrose@2638: if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { jrose@1957: classfile_parse_error( jrose@2638: "Class file version does not support constant tag %u in class file %s", jrose@2638: tag, CHECK); jrose@2638: } twisti@2698: if (!EnableInvokeDynamic) { jrose@2638: classfile_parse_error( jrose@2638: "This JVM does not support constant tag %u in class file %s", jrose@1957: tag, CHECK); jrose@1957: } jrose@1957: if (tag == JVM_CONSTANT_MethodHandle) { jrose@1957: cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags jrose@1957: u1 ref_kind = cfs->get_u1_fast(); jrose@1957: u2 method_index = cfs->get_u2_fast(); coleenp@4719: _cp->method_handle_index_at_put(index, ref_kind, method_index); jrose@1957: } else if (tag == JVM_CONSTANT_MethodType) { jrose@1957: cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags jrose@1957: u2 signature_index = cfs->get_u2_fast(); coleenp@4719: _cp->method_type_index_at_put(index, signature_index); jrose@1957: } else { jrose@1957: ShouldNotReachHere(); jrose@1957: } jrose@1957: break; jrose@2015: case JVM_CONSTANT_InvokeDynamic : jrose@2015: { jrose@2638: if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { jrose@2015: classfile_parse_error( jrose@2638: "Class file version does not support constant tag %u in class file %s", jrose@2638: tag, CHECK); jrose@2638: } jrose@2638: if (!EnableInvokeDynamic) { jrose@2638: classfile_parse_error( jrose@2638: "This JVM does not support constant tag %u in class file %s", jrose@2015: tag, CHECK); jrose@2015: } jrose@2353: cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags jrose@2353: u2 bootstrap_specifier_index = cfs->get_u2_fast(); jrose@2353: u2 name_and_type_index = cfs->get_u2_fast(); jrose@2353: if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) jrose@2353: _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later coleenp@4719: _cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index); jrose@2015: } jrose@2015: break; duke@435: case JVM_CONSTANT_Integer : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags duke@435: u4 bytes = cfs->get_u4_fast(); coleenp@4719: _cp->int_at_put(index, (jint) bytes); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_Float : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags duke@435: u4 bytes = cfs->get_u4_fast(); coleenp@4719: _cp->float_at_put(index, *(jfloat*)&bytes); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_Long : duke@435: // A mangled type might cause you to overrun allocated memory duke@435: guarantee_property(index+1 < length, duke@435: "Invalid constant pool entry %u in class file %s", duke@435: index, CHECK); duke@435: { duke@435: cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags duke@435: u8 bytes = cfs->get_u8_fast(); coleenp@4719: _cp->long_at_put(index, bytes); duke@435: } duke@435: index++; // Skip entry following eigth-byte constant, see JVM book p. 98 duke@435: break; duke@435: case JVM_CONSTANT_Double : duke@435: // A mangled type might cause you to overrun allocated memory duke@435: guarantee_property(index+1 < length, duke@435: "Invalid constant pool entry %u in class file %s", duke@435: index, CHECK); duke@435: { duke@435: cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags duke@435: u8 bytes = cfs->get_u8_fast(); coleenp@4719: _cp->double_at_put(index, *(jdouble*)&bytes); duke@435: } duke@435: index++; // Skip entry following eigth-byte constant, see JVM book p. 98 duke@435: break; duke@435: case JVM_CONSTANT_NameAndType : duke@435: { duke@435: cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags duke@435: u2 name_index = cfs->get_u2_fast(); duke@435: u2 signature_index = cfs->get_u2_fast(); coleenp@4719: _cp->name_and_type_at_put(index, name_index, signature_index); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_Utf8 : duke@435: { duke@435: cfs->guarantee_more(2, CHECK); // utf8_length duke@435: u2 utf8_length = cfs->get_u2_fast(); duke@435: u1* utf8_buffer = cfs->get_u1_buffer(); duke@435: assert(utf8_buffer != NULL, "null utf8 buffer"); duke@435: // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward. duke@435: cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags duke@435: cfs->skip_u1_fast(utf8_length); jrose@866: duke@435: // Before storing the symbol, make sure it's legal duke@435: if (_need_verify) { duke@435: verify_legal_utf8((unsigned char*)utf8_buffer, utf8_length, CHECK); duke@435: } duke@435: twisti@2698: if (EnableInvokeDynamic && has_cp_patch_at(index)) { jrose@866: Handle patch = clear_cp_patch_at(index); jrose@866: guarantee_property(java_lang_String::is_instance(patch()), jrose@866: "Illegal utf8 patch at %d in class file %s", jrose@866: index, CHECK); jrose@866: char* str = java_lang_String::as_utf8_string(patch()); jrose@866: // (could use java_lang_String::as_symbol instead, but might as well batch them) jrose@866: utf8_buffer = (u1*) str; jrose@866: utf8_length = (int) strlen(str); jrose@866: } jrose@866: duke@435: unsigned int hash; coleenp@2497: Symbol* result = SymbolTable::lookup_only((char*)utf8_buffer, utf8_length, hash); duke@435: if (result == NULL) { duke@435: names[names_count] = (char*)utf8_buffer; duke@435: lengths[names_count] = utf8_length; duke@435: indices[names_count] = index; duke@435: hashValues[names_count++] = hash; duke@435: if (names_count == SymbolTable::symbol_alloc_batch_size) { coleenp@4719: SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK); duke@435: names_count = 0; duke@435: } duke@435: } else { coleenp@4719: _cp->symbol_at_put(index, result); duke@435: } duke@435: } duke@435: break; duke@435: default: duke@435: classfile_parse_error( duke@435: "Unknown constant tag %u in class file %s", tag, CHECK); duke@435: break; duke@435: } duke@435: } duke@435: duke@435: // Allocate the remaining symbols duke@435: if (names_count > 0) { coleenp@4719: SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK); duke@435: } duke@435: duke@435: // Copy _current pointer of local copy back to stream(). duke@435: #ifdef ASSERT duke@435: assert(cfs0->current() == old_current, "non-exclusive use of stream()"); duke@435: #endif duke@435: cfs0->set_current(cfs1.current()); duke@435: } duke@435: duke@435: bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); } duke@435: jrose@3926: inline Symbol* check_symbol_at(constantPoolHandle cp, int index) { jrose@3926: if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) jrose@3926: return cp->symbol_at(index); jrose@3926: else jrose@3926: return NULL; jrose@3926: } jrose@3926: coleenp@4719: constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: constantPoolHandle nullHandle; duke@435: duke@435: cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag duke@435: u2 length = cfs->get_u2_fast(); duke@435: guarantee_property( duke@435: length >= 1, "Illegal constant pool size %u in class file %s", duke@435: length, CHECK_(nullHandle)); coleenp@4719: ConstantPool* constant_pool = ConstantPool::allocate(_loader_data, length, coleenp@4719: CHECK_(nullHandle)); coleenp@4719: _cp = constant_pool; // save in case of errors duke@435: constantPoolHandle cp (THREAD, constant_pool); duke@435: duke@435: // parsing constant pool entries coleenp@4719: parse_constant_pool_entries(length, CHECK_(nullHandle)); duke@435: duke@435: int index = 1; // declared outside of loops for portability duke@435: duke@435: // first verification pass - validate cross references and fixup class and string constants duke@435: for (index = 1; index < length; index++) { // Index 0 is unused jrose@2353: jbyte tag = cp->tag_at(index).value(); jrose@2353: switch (tag) { duke@435: case JVM_CONSTANT_Class : duke@435: ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present duke@435: break; duke@435: case JVM_CONSTANT_Fieldref : duke@435: // fall through duke@435: case JVM_CONSTANT_Methodref : duke@435: // fall through duke@435: case JVM_CONSTANT_InterfaceMethodref : { duke@435: if (!_need_verify) break; duke@435: int klass_ref_index = cp->klass_ref_index_at(index); duke@435: int name_and_type_ref_index = cp->name_and_type_ref_index_at(index); coleenp@4719: check_property(valid_klass_reference_at(klass_ref_index), duke@435: "Invalid constant pool index %u in class file %s", duke@435: klass_ref_index, duke@435: CHECK_(nullHandle)); duke@435: check_property(valid_cp_range(name_and_type_ref_index, length) && duke@435: cp->tag_at(name_and_type_ref_index).is_name_and_type(), duke@435: "Invalid constant pool index %u in class file %s", duke@435: name_and_type_ref_index, duke@435: CHECK_(nullHandle)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_String : duke@435: ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present duke@435: break; duke@435: case JVM_CONSTANT_Integer : duke@435: break; duke@435: case JVM_CONSTANT_Float : duke@435: break; duke@435: case JVM_CONSTANT_Long : duke@435: case JVM_CONSTANT_Double : duke@435: index++; duke@435: check_property( duke@435: (index < length && cp->tag_at(index).is_invalid()), duke@435: "Improper constant pool long/double index %u in class file %s", duke@435: index, CHECK_(nullHandle)); duke@435: break; duke@435: case JVM_CONSTANT_NameAndType : { duke@435: if (!_need_verify) break; duke@435: int name_ref_index = cp->name_ref_index_at(index); duke@435: int signature_ref_index = cp->signature_ref_index_at(index); coleenp@4719: check_property(valid_symbol_at(name_ref_index), coleenp@4719: "Invalid constant pool index %u in class file %s", coleenp@4719: name_ref_index, CHECK_(nullHandle)); coleenp@4719: check_property(valid_symbol_at(signature_ref_index), coleenp@4719: "Invalid constant pool index %u in class file %s", coleenp@4719: signature_ref_index, CHECK_(nullHandle)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Utf8 : duke@435: break; duke@435: case JVM_CONSTANT_UnresolvedClass : // fall-through duke@435: case JVM_CONSTANT_UnresolvedClassInError: duke@435: ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present duke@435: break; duke@435: case JVM_CONSTANT_ClassIndex : duke@435: { duke@435: int class_index = cp->klass_index_at(index); coleenp@4719: check_property(valid_symbol_at(class_index), coleenp@4719: "Invalid constant pool index %u in class file %s", coleenp@4719: class_index, CHECK_(nullHandle)); duke@435: cp->unresolved_klass_at_put(index, cp->symbol_at(class_index)); duke@435: } duke@435: break; duke@435: case JVM_CONSTANT_StringIndex : duke@435: { duke@435: int string_index = cp->string_index_at(index); coleenp@4719: check_property(valid_symbol_at(string_index), coleenp@4719: "Invalid constant pool index %u in class file %s", coleenp@4719: string_index, CHECK_(nullHandle)); coleenp@2497: Symbol* sym = cp->symbol_at(string_index); duke@435: cp->unresolved_string_at_put(index, sym); duke@435: } duke@435: break; jrose@1957: case JVM_CONSTANT_MethodHandle : jrose@1957: { jrose@1957: int ref_index = cp->method_handle_index_at(index); jrose@1957: check_property( jrose@1957: valid_cp_range(ref_index, length) && twisti@2698: EnableInvokeDynamic, jrose@1957: "Invalid constant pool index %u in class file %s", jrose@1957: ref_index, CHECK_(nullHandle)); jrose@1957: constantTag tag = cp->tag_at(ref_index); jrose@1957: int ref_kind = cp->method_handle_ref_kind_at(index); jrose@1957: switch (ref_kind) { jrose@1957: case JVM_REF_getField: jrose@1957: case JVM_REF_getStatic: jrose@1957: case JVM_REF_putField: jrose@1957: case JVM_REF_putStatic: jrose@1957: check_property( jrose@1957: tag.is_field(), jrose@1957: "Invalid constant pool index %u in class file %s (not a field)", jrose@1957: ref_index, CHECK_(nullHandle)); jrose@1957: break; jrose@1957: case JVM_REF_invokeVirtual: jrose@1957: case JVM_REF_newInvokeSpecial: jrose@1957: check_property( jrose@1957: tag.is_method(), jrose@1957: "Invalid constant pool index %u in class file %s (not a method)", jrose@1957: ref_index, CHECK_(nullHandle)); jrose@1957: break; bharadwaj@4960: case JVM_REF_invokeStatic: bharadwaj@4960: case JVM_REF_invokeSpecial: bharadwaj@5173: check_property(tag.is_method() || bharadwaj@5173: ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()), bharadwaj@4960: "Invalid constant pool index %u in class file %s (not a method)", bharadwaj@4960: ref_index, CHECK_(nullHandle)); bharadwaj@4960: break; jrose@1957: case JVM_REF_invokeInterface: jrose@1957: check_property( jrose@1957: tag.is_interface_method(), jrose@1957: "Invalid constant pool index %u in class file %s (not an interface method)", jrose@1957: ref_index, CHECK_(nullHandle)); jrose@1957: break; jrose@1957: default: jrose@1957: classfile_parse_error( jrose@1957: "Bad method handle kind at constant pool index %u in class file %s", jrose@1957: index, CHECK_(nullHandle)); jrose@1957: } jrose@1957: // Keep the ref_index unchanged. It will be indirected at link-time. jrose@1957: } jrose@1957: break; jrose@1957: case JVM_CONSTANT_MethodType : jrose@1957: { jrose@1957: int ref_index = cp->method_type_index_at(index); coleenp@4719: check_property(valid_symbol_at(ref_index) && EnableInvokeDynamic, coleenp@4719: "Invalid constant pool index %u in class file %s", coleenp@4719: ref_index, CHECK_(nullHandle)); jrose@1957: } jrose@1957: break; jrose@2015: case JVM_CONSTANT_InvokeDynamic : jrose@2015: { jrose@2015: int name_and_type_ref_index = cp->invoke_dynamic_name_and_type_ref_index_at(index); jrose@2015: check_property(valid_cp_range(name_and_type_ref_index, length) && jrose@2015: cp->tag_at(name_and_type_ref_index).is_name_and_type(), jrose@2015: "Invalid constant pool index %u in class file %s", jrose@2015: name_and_type_ref_index, jrose@2015: CHECK_(nullHandle)); jrose@2353: // bootstrap specifier index must be checked later, when BootstrapMethods attr is available jrose@2015: break; jrose@2015: } duke@435: default: jcoomes@1845: fatal(err_msg("bad constant pool tag value %u", jcoomes@1845: cp->tag_at(index).value())); duke@435: ShouldNotReachHere(); duke@435: break; duke@435: } // end of switch duke@435: } // end of for duke@435: jrose@866: if (_cp_patches != NULL) { jrose@866: // need to treat this_class specially... twisti@2698: assert(EnableInvokeDynamic, ""); jrose@866: int this_class_index; jrose@866: { jrose@866: cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len jrose@866: u1* mark = cfs->current(); jrose@866: u2 flags = cfs->get_u2_fast(); jrose@866: this_class_index = cfs->get_u2_fast(); jrose@866: cfs->set_current(mark); // revert to mark jrose@866: } jrose@866: jrose@866: for (index = 1; index < length; index++) { // Index 0 is unused jrose@866: if (has_cp_patch_at(index)) { jrose@866: guarantee_property(index != this_class_index, jrose@866: "Illegal constant pool patch to self at %d in class file %s", jrose@866: index, CHECK_(nullHandle)); jrose@866: patch_constant_pool(cp, index, cp_patch_at(index), CHECK_(nullHandle)); jrose@866: } jrose@866: } jrose@866: } jrose@866: duke@435: if (!_need_verify) { duke@435: return cp; duke@435: } duke@435: duke@435: // second verification pass - checks the strings are of the right format. jrose@866: // but not yet to the other entries duke@435: for (index = 1; index < length; index++) { duke@435: jbyte tag = cp->tag_at(index).value(); duke@435: switch (tag) { duke@435: case JVM_CONSTANT_UnresolvedClass: { coleenp@2497: Symbol* class_name = cp->unresolved_klass_at(index); jrose@866: // check the name, even if _cp_patches will overwrite it duke@435: verify_legal_class_name(class_name, CHECK_(nullHandle)); duke@435: break; duke@435: } kamg@1941: case JVM_CONSTANT_NameAndType: { kamg@1941: if (_need_verify && _major_version >= JAVA_7_VERSION) { kamg@1941: int sig_index = cp->signature_ref_index_at(index); kamg@1941: int name_index = cp->name_ref_index_at(index); coleenp@2497: Symbol* name = cp->symbol_at(name_index); coleenp@2497: Symbol* sig = cp->symbol_at(sig_index); kamg@1941: if (sig->byte_at(0) == JVM_SIGNATURE_FUNC) { kamg@1941: verify_legal_method_signature(name, sig, CHECK_(nullHandle)); kamg@1941: } else { kamg@1941: verify_legal_field_signature(name, sig, CHECK_(nullHandle)); kamg@1941: } kamg@1941: } kamg@1941: break; kamg@1941: } jrose@2743: case JVM_CONSTANT_InvokeDynamic: duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_InterfaceMethodref: { duke@435: int name_and_type_ref_index = cp->name_and_type_ref_index_at(index); duke@435: // already verified to be utf8 duke@435: int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index); duke@435: // already verified to be utf8 duke@435: int signature_ref_index = cp->signature_ref_index_at(name_and_type_ref_index); coleenp@2497: Symbol* name = cp->symbol_at(name_ref_index); coleenp@2497: Symbol* signature = cp->symbol_at(signature_ref_index); duke@435: if (tag == JVM_CONSTANT_Fieldref) { duke@435: verify_legal_field_name(name, CHECK_(nullHandle)); kamg@1941: if (_need_verify && _major_version >= JAVA_7_VERSION) { kamg@1941: // Signature is verified above, when iterating NameAndType_info. kamg@1941: // Need only to be sure it's the right type. kamg@1941: if (signature->byte_at(0) == JVM_SIGNATURE_FUNC) { kamg@1941: throwIllegalSignature( kamg@1941: "Field", name, signature, CHECK_(nullHandle)); kamg@1941: } kamg@1941: } else { kamg@1941: verify_legal_field_signature(name, signature, CHECK_(nullHandle)); kamg@1941: } duke@435: } else { duke@435: verify_legal_method_name(name, CHECK_(nullHandle)); kamg@1941: if (_need_verify && _major_version >= JAVA_7_VERSION) { kamg@1941: // Signature is verified above, when iterating NameAndType_info. kamg@1941: // Need only to be sure it's the right type. kamg@1941: if (signature->byte_at(0) != JVM_SIGNATURE_FUNC) { kamg@1941: throwIllegalSignature( kamg@1941: "Method", name, signature, CHECK_(nullHandle)); kamg@1941: } kamg@1941: } else { kamg@1941: verify_legal_method_signature(name, signature, CHECK_(nullHandle)); kamg@1941: } duke@435: if (tag == JVM_CONSTANT_Methodref) { duke@435: // 4509014: If a class method name begins with '<', it must be "". coleenp@2497: assert(name != NULL, "method name in constant pool is null"); duke@435: unsigned int name_len = name->utf8_length(); duke@435: assert(name_len > 0, "bad method name"); // already verified as legal name duke@435: if (name->byte_at(0) == '<') { coleenp@2497: if (name != vmSymbols::object_initializer_name()) { duke@435: classfile_parse_error( duke@435: "Bad method name at constant pool index %u in class file %s", duke@435: name_ref_index, CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: break; duke@435: } jrose@1957: case JVM_CONSTANT_MethodHandle: { jrose@1957: int ref_index = cp->method_handle_index_at(index); jrose@1957: int ref_kind = cp->method_handle_ref_kind_at(index); jrose@1957: switch (ref_kind) { jrose@1957: case JVM_REF_invokeVirtual: jrose@1957: case JVM_REF_invokeStatic: jrose@1957: case JVM_REF_invokeSpecial: jrose@1957: case JVM_REF_newInvokeSpecial: jrose@1957: { jrose@1957: int name_and_type_ref_index = cp->name_and_type_ref_index_at(ref_index); jrose@1957: int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index); coleenp@2497: Symbol* name = cp->symbol_at(name_ref_index); jrose@1957: if (ref_kind == JVM_REF_newInvokeSpecial) { coleenp@2497: if (name != vmSymbols::object_initializer_name()) { jrose@1957: classfile_parse_error( jrose@1957: "Bad constructor name at constant pool index %u in class file %s", jrose@1957: name_ref_index, CHECK_(nullHandle)); jrose@1957: } jrose@1957: } else { coleenp@2497: if (name == vmSymbols::object_initializer_name()) { jrose@1957: classfile_parse_error( jrose@1957: "Bad method name at constant pool index %u in class file %s", jrose@1957: name_ref_index, CHECK_(nullHandle)); jrose@1957: } jrose@1957: } jrose@1957: } jrose@1957: break; jrose@1957: // Other ref_kinds are already fully checked in previous pass. jrose@1957: } jrose@1957: break; jrose@1957: } jrose@1957: case JVM_CONSTANT_MethodType: { coleenp@2497: Symbol* no_name = vmSymbols::type_name(); // place holder coleenp@2497: Symbol* signature = cp->method_type_signature_at(index); jrose@1957: verify_legal_method_signature(no_name, signature, CHECK_(nullHandle)); jrose@1957: break; jrose@1957: } coleenp@2497: case JVM_CONSTANT_Utf8: { coleenp@2497: assert(cp->symbol_at(index)->refcount() != 0, "count corrupted"); coleenp@2497: } duke@435: } // end of switch duke@435: } // end of for duke@435: duke@435: return cp; duke@435: } duke@435: duke@435: jrose@866: void ClassFileParser::patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS) { twisti@2698: assert(EnableInvokeDynamic, ""); jrose@866: BasicType patch_type = T_VOID; coleenp@4037: jrose@866: switch (cp->tag_at(index).value()) { jrose@866: jrose@866: case JVM_CONSTANT_UnresolvedClass : jrose@866: // Patching a class means pre-resolving it. jrose@866: // The name in the constant pool is ignored. jrose@1957: if (java_lang_Class::is_instance(patch())) { jrose@866: guarantee_property(!java_lang_Class::is_primitive(patch()), jrose@866: "Illegal class patch at %d in class file %s", jrose@866: index, CHECK); coleenp@4037: cp->klass_at_put(index, java_lang_Class::as_Klass(patch())); jrose@866: } else { jrose@866: guarantee_property(java_lang_String::is_instance(patch()), jrose@866: "Illegal class patch at %d in class file %s", jrose@866: index, CHECK); coleenp@2497: Symbol* name = java_lang_String::as_symbol(patch(), CHECK); coleenp@2497: cp->unresolved_klass_at_put(index, name); jrose@866: } jrose@866: break; jrose@866: coleenp@4037: case JVM_CONSTANT_String : coleenp@4037: // skip this patch and don't clear it. Needs the oop array for resolved coleenp@4037: // references to be created first. coleenp@4037: return; jrose@866: jrose@866: case JVM_CONSTANT_Integer : patch_type = T_INT; goto patch_prim; jrose@866: case JVM_CONSTANT_Float : patch_type = T_FLOAT; goto patch_prim; jrose@866: case JVM_CONSTANT_Long : patch_type = T_LONG; goto patch_prim; jrose@866: case JVM_CONSTANT_Double : patch_type = T_DOUBLE; goto patch_prim; jrose@866: patch_prim: jrose@866: { jrose@866: jvalue value; jrose@866: BasicType value_type = java_lang_boxing_object::get_value(patch(), &value); jrose@866: guarantee_property(value_type == patch_type, jrose@866: "Illegal primitive patch at %d in class file %s", jrose@866: index, CHECK); jrose@866: switch (value_type) { jrose@866: case T_INT: cp->int_at_put(index, value.i); break; jrose@866: case T_FLOAT: cp->float_at_put(index, value.f); break; jrose@866: case T_LONG: cp->long_at_put(index, value.j); break; jrose@866: case T_DOUBLE: cp->double_at_put(index, value.d); break; jrose@866: default: assert(false, ""); jrose@866: } jrose@866: } jrose@866: break; jrose@866: jrose@866: default: jrose@866: // %%% TODO: put method handles into CONSTANT_InterfaceMethodref, etc. jrose@866: guarantee_property(!has_cp_patch_at(index), jrose@866: "Illegal unexpected patch at %d in class file %s", jrose@866: index, CHECK); jrose@866: return; jrose@866: } jrose@866: jrose@866: // On fall-through, mark the patch as used. jrose@866: clear_cp_patch_at(index); jrose@866: } jrose@866: jrose@866: jrose@866: duke@435: class NameSigHash: public ResourceObj { duke@435: public: coleenp@2497: Symbol* _name; // name coleenp@2497: Symbol* _sig; // signature duke@435: NameSigHash* _next; // Next entry in hash table duke@435: }; duke@435: duke@435: duke@435: #define HASH_ROW_SIZE 256 duke@435: coleenp@2497: unsigned int hash(Symbol* name, Symbol* sig) { duke@435: unsigned int raw_hash = 0; duke@435: raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2); duke@435: raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize; duke@435: duke@435: return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE; duke@435: } duke@435: duke@435: duke@435: void initialize_hashtable(NameSigHash** table) { duke@435: memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE); duke@435: } duke@435: duke@435: // Return false if the name/sig combination is found in table. duke@435: // Return true if no duplicate is found. And name/sig is added as a new entry in table. duke@435: // The old format checker uses heap sort to find duplicates. duke@435: // NOTE: caller should guarantee that GC doesn't happen during the life cycle coleenp@2497: // of table since we don't expect Symbol*'s to move. coleenp@2497: bool put_after_lookup(Symbol* name, Symbol* sig, NameSigHash** table) { duke@435: assert(name != NULL, "name in constant pool is NULL"); duke@435: duke@435: // First lookup for duplicates duke@435: int index = hash(name, sig); duke@435: NameSigHash* entry = table[index]; duke@435: while (entry != NULL) { duke@435: if (entry->_name == name && entry->_sig == sig) { duke@435: return false; duke@435: } duke@435: entry = entry->_next; duke@435: } duke@435: duke@435: // No duplicate is found, allocate a new entry and fill it. duke@435: entry = new NameSigHash(); duke@435: entry->_name = name; duke@435: entry->_sig = sig; duke@435: duke@435: // Insert into hash table duke@435: entry->_next = table[index]; duke@435: table[index] = entry; duke@435: duke@435: return true; duke@435: } duke@435: duke@435: coleenp@4719: Array* ClassFileParser::parse_interfaces(int length, duke@435: Handle protection_domain, coleenp@2497: Symbol* class_name, kamg@4245: bool* has_default_methods, duke@435: TRAPS) { coleenp@4719: if (length == 0) { coleenp@4719: _local_interfaces = Universe::the_empty_klass_array(); coleenp@4719: } else { coleenp@4719: ClassFileStream* cfs = stream(); coleenp@4719: assert(length > 0, "only called for length>0"); coleenp@4719: _local_interfaces = MetadataFactory::new_array(_loader_data, length, NULL, CHECK_NULL); coleenp@4719: coleenp@4719: int index; coleenp@4719: for (index = 0; index < length; index++) { coleenp@4719: u2 interface_index = cfs->get_u2(CHECK_NULL); coleenp@4719: KlassHandle interf; coleenp@4719: check_property( coleenp@4719: valid_klass_reference_at(interface_index), coleenp@4719: "Interface name has bad constant pool index %u in class file %s", coleenp@4719: interface_index, CHECK_NULL); coleenp@4719: if (_cp->tag_at(interface_index).is_klass()) { coleenp@4719: interf = KlassHandle(THREAD, _cp->resolved_klass_at(interface_index)); coleenp@4719: } else { coleenp@4719: Symbol* unresolved_klass = _cp->klass_name_at(interface_index); coleenp@4719: coleenp@4719: // Don't need to check legal name because it's checked when parsing constant pool. coleenp@4719: // But need to make sure it's not an array type. coleenp@4719: guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY, coleenp@4719: "Bad interface name in class file %s", CHECK_NULL); coleenp@4719: Handle class_loader(THREAD, _loader_data->class_loader()); coleenp@4719: coleenp@4719: // Call resolve_super so classcircularity is checked coleenp@4719: Klass* k = SystemDictionary::resolve_super_or_fail(class_name, coleenp@4719: unresolved_klass, class_loader, protection_domain, coleenp@4719: false, CHECK_NULL); coleenp@4719: interf = KlassHandle(THREAD, k); coleenp@4719: } coleenp@4719: coleenp@4719: if (!interf()->is_interface()) { coleenp@4719: THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", NULL); coleenp@4719: } coleenp@4719: if (InstanceKlass::cast(interf())->has_default_methods()) { coleenp@4719: *has_default_methods = true; coleenp@4719: } coleenp@4719: _local_interfaces->at_put(index, interf()); jrose@866: } duke@435: coleenp@4719: if (!_need_verify || length <= 1) { coleenp@4719: return _local_interfaces; duke@435: } coleenp@4719: coleenp@4719: // Check if there's any duplicates in interfaces coleenp@4719: ResourceMark rm(THREAD); coleenp@4719: NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD( coleenp@4719: THREAD, NameSigHash*, HASH_ROW_SIZE); coleenp@4719: initialize_hashtable(interface_names); coleenp@4719: bool dup = false; coleenp@4719: { coleenp@4719: debug_only(No_Safepoint_Verifier nsv;) coleenp@4719: for (index = 0; index < length; index++) { coleenp@4719: Klass* k = _local_interfaces->at(index); coleenp@4719: Symbol* name = InstanceKlass::cast(k)->name(); coleenp@4719: // If no duplicates, add (name, NULL) in hashtable interface_names. coleenp@4719: if (!put_after_lookup(name, NULL, interface_names)) { coleenp@4719: dup = true; coleenp@4719: break; coleenp@4719: } duke@435: } duke@435: } coleenp@4719: if (dup) { coleenp@4719: classfile_parse_error("Duplicate interface name in class file %s", CHECK_NULL); coleenp@4719: } duke@435: } coleenp@4719: return _local_interfaces; duke@435: } duke@435: duke@435: coleenp@4719: void ClassFileParser::verify_constantvalue(int constantvalue_index, int signature_index, TRAPS) { duke@435: // Make sure the constant pool entry is of a type appropriate to this field duke@435: guarantee_property( duke@435: (constantvalue_index > 0 && coleenp@4719: constantvalue_index < _cp->length()), duke@435: "Bad initial value index %u in ConstantValue attribute in class file %s", duke@435: constantvalue_index, CHECK); coleenp@4719: constantTag value_type = _cp->tag_at(constantvalue_index); coleenp@4719: switch ( _cp->basic_type_for_signature_at(signature_index) ) { duke@435: case T_LONG: duke@435: guarantee_property(value_type.is_long(), "Inconsistent constant value type in class file %s", CHECK); duke@435: break; duke@435: case T_FLOAT: duke@435: guarantee_property(value_type.is_float(), "Inconsistent constant value type in class file %s", CHECK); duke@435: break; duke@435: case T_DOUBLE: duke@435: guarantee_property(value_type.is_double(), "Inconsistent constant value type in class file %s", CHECK); duke@435: break; duke@435: case T_BYTE: case T_CHAR: case T_SHORT: case T_BOOLEAN: case T_INT: duke@435: guarantee_property(value_type.is_int(), "Inconsistent constant value type in class file %s", CHECK); duke@435: break; duke@435: case T_OBJECT: coleenp@4719: guarantee_property((_cp->symbol_at(signature_index)->equals("Ljava/lang/String;") coleenp@4037: && value_type.is_string()), duke@435: "Bad string initial value in class file %s", CHECK); duke@435: break; duke@435: default: duke@435: classfile_parse_error( duke@435: "Unable to set initial value %u in class file %s", duke@435: constantvalue_index, CHECK); duke@435: } duke@435: } duke@435: duke@435: duke@435: // Parse attributes for a field. coleenp@4719: void ClassFileParser::parse_field_attributes(u2 attributes_count, duke@435: bool is_static, u2 signature_index, duke@435: u2* constantvalue_index_addr, duke@435: bool* is_synthetic_addr, duke@435: u2* generic_signature_index_addr, jrose@3926: ClassFileParser::FieldAnnotationCollector* parsed_annotations, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: assert(attributes_count > 0, "length should be greater than 0"); duke@435: u2 constantvalue_index = 0; duke@435: u2 generic_signature_index = 0; duke@435: bool is_synthetic = false; duke@435: u1* runtime_visible_annotations = NULL; duke@435: int runtime_visible_annotations_length = 0; duke@435: u1* runtime_invisible_annotations = NULL; duke@435: int runtime_invisible_annotations_length = 0; stefank@4393: u1* runtime_visible_type_annotations = NULL; stefank@4393: int runtime_visible_type_annotations_length = 0; stefank@4393: u1* runtime_invisible_type_annotations = NULL; stefank@4393: int runtime_invisible_type_annotations_length = 0; duke@435: while (attributes_count--) { duke@435: cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length duke@435: u2 attribute_name_index = cfs->get_u2_fast(); duke@435: u4 attribute_length = cfs->get_u4_fast(); coleenp@4719: check_property(valid_symbol_at(attribute_name_index), duke@435: "Invalid field attribute index %u in class file %s", duke@435: attribute_name_index, duke@435: CHECK); coleenp@4719: Symbol* attribute_name = _cp->symbol_at(attribute_name_index); duke@435: if (is_static && attribute_name == vmSymbols::tag_constant_value()) { duke@435: // ignore if non-static duke@435: if (constantvalue_index != 0) { duke@435: classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK); duke@435: } duke@435: check_property( duke@435: attribute_length == 2, duke@435: "Invalid ConstantValue field attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: constantvalue_index = cfs->get_u2(CHECK); duke@435: if (_need_verify) { coleenp@4719: verify_constantvalue(constantvalue_index, signature_index, CHECK); duke@435: } duke@435: } else if (attribute_name == vmSymbols::tag_synthetic()) { duke@435: if (attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Synthetic field attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: } duke@435: is_synthetic = true; duke@435: } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120 duke@435: if (attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Deprecated field attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: } duke@435: } else if (_major_version >= JAVA_1_5_VERSION) { duke@435: if (attribute_name == vmSymbols::tag_signature()) { duke@435: if (attribute_length != 2) { duke@435: classfile_parse_error( duke@435: "Wrong size %u for field's Signature attribute in class file %s", duke@435: attribute_length, CHECK); duke@435: } duke@435: generic_signature_index = cfs->get_u2(CHECK); duke@435: } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { duke@435: runtime_visible_annotations_length = attribute_length; duke@435: runtime_visible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_visible_annotations != NULL, "null visible annotations"); coleenp@4719: parse_annotations(runtime_visible_annotations, jwilhelm@4430: runtime_visible_annotations_length, jwilhelm@4430: parsed_annotations, jwilhelm@4430: CHECK); duke@435: cfs->skip_u1(runtime_visible_annotations_length, CHECK); duke@435: } else if (PreserveAllAnnotations && attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { duke@435: runtime_invisible_annotations_length = attribute_length; duke@435: runtime_invisible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_invisible_annotations != NULL, "null invisible annotations"); duke@435: cfs->skip_u1(runtime_invisible_annotations_length, CHECK); stefank@4393: } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { stefank@4393: runtime_visible_type_annotations_length = attribute_length; stefank@4393: runtime_visible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); stefank@4393: cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); stefank@4393: } else if (PreserveAllAnnotations && attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { stefank@4393: runtime_invisible_type_annotations_length = attribute_length; stefank@4393: runtime_invisible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); stefank@4393: cfs->skip_u1(runtime_invisible_type_annotations_length, CHECK); duke@435: } else { duke@435: cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes duke@435: } duke@435: } else { duke@435: cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes duke@435: } duke@435: } duke@435: duke@435: *constantvalue_index_addr = constantvalue_index; duke@435: *is_synthetic_addr = is_synthetic; duke@435: *generic_signature_index_addr = generic_signature_index; coleenp@4719: AnnotationArray* a = assemble_annotations(runtime_visible_annotations, duke@435: runtime_visible_annotations_length, duke@435: runtime_invisible_annotations, duke@435: runtime_invisible_annotations_length, duke@435: CHECK); coleenp@4719: parsed_annotations->set_field_annotations(a); coleenp@4719: a = assemble_annotations(runtime_visible_type_annotations, coleenp@4719: runtime_visible_type_annotations_length, coleenp@4719: runtime_invisible_type_annotations, coleenp@4719: runtime_invisible_type_annotations_length, coleenp@4719: CHECK); coleenp@4719: parsed_annotations->set_field_type_annotations(a); duke@435: return; duke@435: } duke@435: duke@435: duke@435: // Field allocation types. Used for computing field offsets. duke@435: duke@435: enum FieldAllocationType { duke@435: STATIC_OOP, // Oops duke@435: STATIC_BYTE, // Boolean, Byte, char duke@435: STATIC_SHORT, // shorts duke@435: STATIC_WORD, // ints never@3137: STATIC_DOUBLE, // aligned long or double duke@435: NONSTATIC_OOP, duke@435: NONSTATIC_BYTE, duke@435: NONSTATIC_SHORT, duke@435: NONSTATIC_WORD, duke@435: NONSTATIC_DOUBLE, never@3137: MAX_FIELD_ALLOCATION_TYPE, never@3137: BAD_ALLOCATION_TYPE = -1 duke@435: }; duke@435: never@3137: static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = { never@3137: BAD_ALLOCATION_TYPE, // 0 never@3137: BAD_ALLOCATION_TYPE, // 1 never@3137: BAD_ALLOCATION_TYPE, // 2 never@3137: BAD_ALLOCATION_TYPE, // 3 roland@4159: NONSTATIC_BYTE , // T_BOOLEAN = 4, roland@4159: NONSTATIC_SHORT, // T_CHAR = 5, roland@4159: NONSTATIC_WORD, // T_FLOAT = 6, roland@4159: NONSTATIC_DOUBLE, // T_DOUBLE = 7, roland@4159: NONSTATIC_BYTE, // T_BYTE = 8, roland@4159: NONSTATIC_SHORT, // T_SHORT = 9, roland@4159: NONSTATIC_WORD, // T_INT = 10, roland@4159: NONSTATIC_DOUBLE, // T_LONG = 11, roland@4159: NONSTATIC_OOP, // T_OBJECT = 12, roland@4159: NONSTATIC_OOP, // T_ARRAY = 13, roland@4159: BAD_ALLOCATION_TYPE, // T_VOID = 14, roland@4159: BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, roland@4159: BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, roland@4159: BAD_ALLOCATION_TYPE, // T_METADATA = 17, roland@4159: BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, roland@4159: BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, never@3137: BAD_ALLOCATION_TYPE, // 0 never@3137: BAD_ALLOCATION_TYPE, // 1 never@3137: BAD_ALLOCATION_TYPE, // 2 never@3137: BAD_ALLOCATION_TYPE, // 3 roland@4159: STATIC_BYTE , // T_BOOLEAN = 4, roland@4159: STATIC_SHORT, // T_CHAR = 5, roland@4159: STATIC_WORD, // T_FLOAT = 6, roland@4159: STATIC_DOUBLE, // T_DOUBLE = 7, roland@4159: STATIC_BYTE, // T_BYTE = 8, roland@4159: STATIC_SHORT, // T_SHORT = 9, roland@4159: STATIC_WORD, // T_INT = 10, roland@4159: STATIC_DOUBLE, // T_LONG = 11, roland@4159: STATIC_OOP, // T_OBJECT = 12, roland@4159: STATIC_OOP, // T_ARRAY = 13, roland@4159: BAD_ALLOCATION_TYPE, // T_VOID = 14, roland@4159: BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, roland@4159: BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16, roland@4159: BAD_ALLOCATION_TYPE, // T_METADATA = 17, roland@4159: BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18, roland@4159: BAD_ALLOCATION_TYPE, // T_CONFLICT = 19, duke@435: }; duke@435: never@3137: static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) { never@3137: assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values"); never@3137: FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)]; never@3137: assert(result != BAD_ALLOCATION_TYPE, "bad type"); never@3137: return result; never@3137: } never@3137: never@3137: class FieldAllocationCount: public ResourceObj { never@3137: public: jiangli@3373: u2 count[MAX_FIELD_ALLOCATION_TYPE]; never@3137: never@3137: FieldAllocationCount() { never@3137: for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) { never@3137: count[i] = 0; never@3137: } never@3137: } never@3137: never@3137: FieldAllocationType update(bool is_static, BasicType type) { never@3137: FieldAllocationType atype = basic_type_to_atype(is_static, type); jiangli@3373: // Make sure there is no overflow with injected fields. jiangli@3373: assert(count[atype] < 0xFFFF, "More than 65535 fields"); never@3137: count[atype]++; never@3137: return atype; never@3137: } never@3137: }; never@3137: coleenp@4719: Array* ClassFileParser::parse_fields(Symbol* class_name, coleenp@4142: bool is_interface, coleenp@4142: FieldAllocationCount *fac, coleenp@4142: u2* java_fields_count_ptr, TRAPS) { duke@435: ClassFileStream* cfs = stream(); coleenp@4037: cfs->guarantee_more(2, CHECK_NULL); // length duke@435: u2 length = cfs->get_u2_fast(); never@3137: *java_fields_count_ptr = length; never@3137: never@3137: int num_injected = 0; never@3137: InjectedField* injected = JavaClasses::get_injected(class_name, &num_injected); jiangli@3803: int total_fields = length + num_injected; jiangli@3803: jiangli@3803: // The field array starts with tuples of shorts jiangli@3803: // [access, name index, sig index, initial value index, byte offset]. jiangli@3803: // A generic signature slot only exists for field with generic jiangli@3803: // signature attribute. And the access flag is set with jiangli@3803: // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic jiangli@3803: // signature slots are at the end of the field array and after all jiangli@3803: // other fields data. jiangli@3803: // jiangli@3803: // f1: [access, name index, sig index, initial value index, low_offset, high_offset] jiangli@3803: // f2: [access, name index, sig index, initial value index, low_offset, high_offset] jiangli@3803: // ... jiangli@3803: // fn: [access, name index, sig index, initial value index, low_offset, high_offset] jiangli@3803: // [generic signature index] jiangli@3803: // [generic signature index] jiangli@3803: // ... jiangli@3803: // jiangli@3803: // Allocate a temporary resource array for field data. For each field, jiangli@3803: // a slot is reserved in the temporary array for the generic signature jiangli@3803: // index. After parsing all fields, the data are copied to a permanent jiangli@3803: // array and any unused slots will be discarded. jiangli@3803: ResourceMark rm(THREAD); jiangli@3803: u2* fa = NEW_RESOURCE_ARRAY_IN_THREAD( jiangli@3803: THREAD, u2, total_fields * (FieldInfo::field_slots + 1)); duke@435: jiangli@3803: // The generic signature slots start after all other fields' data. jiangli@3803: int generic_signature_slot = total_fields * FieldInfo::field_slots; jiangli@3803: int num_generic_signature = 0; duke@435: for (int n = 0; n < length; n++) { coleenp@4037: cfs->guarantee_more(8, CHECK_NULL); // access_flags, name_index, descriptor_index, attributes_count duke@435: duke@435: AccessFlags access_flags; duke@435: jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS; coleenp@4037: verify_legal_field_modifiers(flags, is_interface, CHECK_NULL); duke@435: access_flags.set_flags(flags); duke@435: duke@435: u2 name_index = cfs->get_u2_fast(); coleenp@4719: int cp_size = _cp->length(); coleenp@4719: check_property(valid_symbol_at(name_index), duke@435: "Invalid constant pool index %u for field name in class file %s", coleenp@4719: name_index, coleenp@4719: CHECK_NULL); coleenp@4719: Symbol* name = _cp->symbol_at(name_index); coleenp@4037: verify_legal_field_name(name, CHECK_NULL); duke@435: duke@435: u2 signature_index = cfs->get_u2_fast(); coleenp@4719: check_property(valid_symbol_at(signature_index), duke@435: "Invalid constant pool index %u for field signature in class file %s", coleenp@4037: signature_index, CHECK_NULL); coleenp@4719: Symbol* sig = _cp->symbol_at(signature_index); coleenp@4037: verify_legal_field_signature(name, sig, CHECK_NULL); duke@435: duke@435: u2 constantvalue_index = 0; duke@435: bool is_synthetic = false; duke@435: u2 generic_signature_index = 0; duke@435: bool is_static = access_flags.is_static(); coleenp@4719: FieldAnnotationCollector parsed_annotations(_loader_data); duke@435: duke@435: u2 attributes_count = cfs->get_u2_fast(); duke@435: if (attributes_count > 0) { coleenp@4719: parse_field_attributes(attributes_count, is_static, signature_index, duke@435: &constantvalue_index, &is_synthetic, coleenp@4719: &generic_signature_index, &parsed_annotations, coleenp@4037: CHECK_NULL); coleenp@4719: if (parsed_annotations.field_annotations() != NULL) { coleenp@4719: if (_fields_annotations == NULL) { coleenp@4719: _fields_annotations = MetadataFactory::new_array( coleenp@4719: _loader_data, length, NULL, coleenp@4037: CHECK_NULL); duke@435: } coleenp@4719: _fields_annotations->at_put(n, parsed_annotations.field_annotations()); coleenp@4719: parsed_annotations.set_field_annotations(NULL); duke@435: } coleenp@4719: if (parsed_annotations.field_type_annotations() != NULL) { coleenp@4719: if (_fields_type_annotations == NULL) { coleenp@4719: _fields_type_annotations = MetadataFactory::new_array( coleenp@4719: _loader_data, length, NULL, stefank@4393: CHECK_NULL); stefank@4393: } coleenp@4719: _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations()); coleenp@4719: parsed_annotations.set_field_type_annotations(NULL); stefank@4393: } coleenp@4719: duke@435: if (is_synthetic) { duke@435: access_flags.set_is_synthetic(); duke@435: } jiangli@3803: if (generic_signature_index != 0) { jiangli@3803: access_flags.set_field_has_generic_signature(); jiangli@3803: fa[generic_signature_slot] = generic_signature_index; jiangli@3803: generic_signature_slot ++; jiangli@3803: num_generic_signature ++; jiangli@3803: } duke@435: } duke@435: jiangli@3803: FieldInfo* field = FieldInfo::from_field_array(fa, n); never@3137: field->initialize(access_flags.as_short(), never@3137: name_index, never@3137: signature_index, jwilhelm@4430: constantvalue_index); coleenp@4719: BasicType type = _cp->basic_type_for_signature_at(signature_index); jwilhelm@4430: jwilhelm@4430: // Remember how many oops we encountered and compute allocation type jwilhelm@4430: FieldAllocationType atype = fac->update(is_static, type); jwilhelm@4430: field->set_allocation_type(atype); jwilhelm@4430: jwilhelm@4430: // After field is initialized with type, we can augment it with aux info jrose@3926: if (parsed_annotations.has_any_annotations()) jrose@3926: parsed_annotations.apply_to(field); never@3137: } never@3137: jiangli@3803: int index = length; never@3137: if (num_injected != 0) { never@3137: for (int n = 0; n < num_injected; n++) { never@3137: // Check for duplicates never@3137: if (injected[n].may_be_java) { never@3137: Symbol* name = injected[n].name(); never@3137: Symbol* signature = injected[n].signature(); never@3137: bool duplicate = false; never@3137: for (int i = 0; i < length; i++) { jiangli@3803: FieldInfo* f = FieldInfo::from_field_array(fa, i); coleenp@4719: if (name == _cp->symbol_at(f->name_index()) && coleenp@4719: signature == _cp->symbol_at(f->signature_index())) { never@3137: // Symbol is desclared in Java so skip this one never@3137: duplicate = true; never@3137: break; never@3137: } never@3137: } never@3137: if (duplicate) { never@3137: // These will be removed from the field array at the end never@3137: continue; never@3137: } never@3137: } never@3137: never@3137: // Injected field jiangli@3803: FieldInfo* field = FieldInfo::from_field_array(fa, index); never@3137: field->initialize(JVM_ACC_FIELD_INTERNAL, never@3137: injected[n].name_index, never@3137: injected[n].signature_index, never@3137: 0); never@3137: never@3137: BasicType type = FieldType::basic_type(injected[n].signature()); never@3137: never@3137: // Remember how many oops we encountered and compute allocation type never@3137: FieldAllocationType atype = fac->update(false, type); jwilhelm@4430: field->set_allocation_type(atype); never@3137: index++; never@3137: } jiangli@3803: } jiangli@3803: jiangli@3803: // Now copy the fields' data from the temporary resource array. jiangli@3803: // Sometimes injected fields already exist in the Java source so jiangli@3803: // the fields array could be too long. In that case the jiangli@3803: // fields array is trimed. Also unused slots that were reserved jiangli@3803: // for generic signature indexes are discarded. coleenp@4037: Array* fields = MetadataFactory::new_array( coleenp@4719: _loader_data, index * FieldInfo::field_slots + num_generic_signature, coleenp@4037: CHECK_NULL); coleenp@4719: _fields = fields; // save in case of error jiangli@3803: { jiangli@3803: int i = 0; jiangli@3803: for (; i < index * FieldInfo::field_slots; i++) { coleenp@4037: fields->at_put(i, fa[i]); never@3137: } jiangli@3803: for (int j = total_fields * FieldInfo::field_slots; jiangli@3803: j < generic_signature_slot; j++) { coleenp@4037: fields->at_put(i++, fa[j]); jiangli@3803: } coleenp@4037: assert(i == fields->length(), ""); duke@435: } duke@435: duke@435: if (_need_verify && length > 1) { duke@435: // Check duplicated fields duke@435: ResourceMark rm(THREAD); duke@435: NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, NameSigHash*, HASH_ROW_SIZE); duke@435: initialize_hashtable(names_and_sigs); duke@435: bool dup = false; duke@435: { duke@435: debug_only(No_Safepoint_Verifier nsv;) coleenp@4719: for (AllFieldStream fs(fields, _cp); !fs.done(); fs.next()) { never@3137: Symbol* name = fs.name(); never@3137: Symbol* sig = fs.signature(); duke@435: // If no duplicates, add name/signature in hashtable names_and_sigs. duke@435: if (!put_after_lookup(name, sig, names_and_sigs)) { duke@435: dup = true; duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: if (dup) { duke@435: classfile_parse_error("Duplicate field name&signature in class file %s", coleenp@4037: CHECK_NULL); duke@435: } duke@435: } duke@435: duke@435: return fields; duke@435: } duke@435: duke@435: duke@435: static void copy_u2_with_conversion(u2* dest, u2* src, int length) { duke@435: while (length-- > 0) { duke@435: *dest++ = Bytes::get_Java_u2((u1*) (src++)); duke@435: } duke@435: } duke@435: duke@435: coleenp@4719: u2* ClassFileParser::parse_exception_table(u4 code_length, jiangli@3917: u4 exception_table_length, jiangli@3917: TRAPS) { duke@435: ClassFileStream* cfs = stream(); jiangli@3917: jiangli@3917: u2* exception_table_start = cfs->get_u2_buffer(); jiangli@3917: assert(exception_table_start != NULL, "null exception table"); jiangli@3917: cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, end_pc, handler_pc, catch_type_index jiangli@3917: // Will check legal target after parsing code array in verifier. jiangli@3917: if (_need_verify) { jiangli@3917: for (unsigned int i = 0; i < exception_table_length; i++) { jiangli@3917: u2 start_pc = cfs->get_u2_fast(); jiangli@3917: u2 end_pc = cfs->get_u2_fast(); jiangli@3917: u2 handler_pc = cfs->get_u2_fast(); jiangli@3917: u2 catch_type_index = cfs->get_u2_fast(); duke@435: guarantee_property((start_pc < end_pc) && (end_pc <= code_length), jiangli@3917: "Illegal exception table range in class file %s", jiangli@3917: CHECK_NULL); duke@435: guarantee_property(handler_pc < code_length, jiangli@3917: "Illegal exception table handler in class file %s", jiangli@3917: CHECK_NULL); duke@435: if (catch_type_index != 0) { coleenp@4719: guarantee_property(valid_klass_reference_at(catch_type_index), jiangli@3917: "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); duke@435: } duke@435: } jiangli@3917: } else { jiangli@3917: cfs->skip_u2_fast(exception_table_length * 4); duke@435: } jiangli@3917: return exception_table_start; duke@435: } duke@435: duke@435: void ClassFileParser::parse_linenumber_table( duke@435: u4 code_attribute_length, u4 code_length, duke@435: CompressedLineNumberWriteStream** write_stream, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: unsigned int num_entries = cfs->get_u2(CHECK); duke@435: duke@435: // Each entry is a u2 start_pc, and a u2 line_number duke@435: unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2)); duke@435: duke@435: // Verify line number attribute and table length duke@435: check_property( duke@435: code_attribute_length == sizeof(u2) + length_in_bytes, duke@435: "LineNumberTable attribute has wrong length in class file %s", CHECK); duke@435: duke@435: cfs->guarantee_more(length_in_bytes, CHECK); duke@435: duke@435: if ((*write_stream) == NULL) { duke@435: if (length_in_bytes > fixed_buffer_size) { duke@435: (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes); duke@435: } else { duke@435: (*write_stream) = new CompressedLineNumberWriteStream( duke@435: linenumbertable_buffer, fixed_buffer_size); duke@435: } duke@435: } duke@435: duke@435: while (num_entries-- > 0) { duke@435: u2 bci = cfs->get_u2_fast(); // start_pc duke@435: u2 line = cfs->get_u2_fast(); // line_number duke@435: guarantee_property(bci < code_length, duke@435: "Invalid pc in LineNumberTable in class file %s", CHECK); duke@435: (*write_stream)->write_pair(bci, line); duke@435: } duke@435: } duke@435: duke@435: duke@435: // Class file LocalVariableTable elements. duke@435: class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC { duke@435: public: duke@435: u2 start_bci; duke@435: u2 length; duke@435: u2 name_cp_index; duke@435: u2 descriptor_cp_index; duke@435: u2 slot; duke@435: }; duke@435: duke@435: zgu@3900: class LVT_Hash: public CHeapObj { duke@435: public: duke@435: LocalVariableTableElement *_elem; // element duke@435: LVT_Hash* _next; // Next entry in hash table duke@435: }; duke@435: duke@435: unsigned int hash(LocalVariableTableElement *elem) { duke@435: unsigned int raw_hash = elem->start_bci; duke@435: duke@435: raw_hash = elem->length + raw_hash * 37; duke@435: raw_hash = elem->name_cp_index + raw_hash * 37; duke@435: raw_hash = elem->slot + raw_hash * 37; duke@435: duke@435: return raw_hash % HASH_ROW_SIZE; duke@435: } duke@435: duke@435: void initialize_hashtable(LVT_Hash** table) { duke@435: for (int i = 0; i < HASH_ROW_SIZE; i++) { duke@435: table[i] = NULL; duke@435: } duke@435: } duke@435: duke@435: void clear_hashtable(LVT_Hash** table) { duke@435: for (int i = 0; i < HASH_ROW_SIZE; i++) { duke@435: LVT_Hash* current = table[i]; duke@435: LVT_Hash* next; duke@435: while (current != NULL) { duke@435: next = current->_next; duke@435: current->_next = NULL; duke@435: delete(current); duke@435: current = next; duke@435: } duke@435: table[i] = NULL; duke@435: } duke@435: } duke@435: duke@435: LVT_Hash* LVT_lookup(LocalVariableTableElement *elem, int index, LVT_Hash** table) { duke@435: LVT_Hash* entry = table[index]; duke@435: duke@435: /* duke@435: * 3-tuple start_bci/length/slot has to be unique key, duke@435: * so the following comparison seems to be redundant: duke@435: * && elem->name_cp_index == entry->_elem->name_cp_index duke@435: */ duke@435: while (entry != NULL) { duke@435: if (elem->start_bci == entry->_elem->start_bci duke@435: && elem->length == entry->_elem->length duke@435: && elem->name_cp_index == entry->_elem->name_cp_index duke@435: && elem->slot == entry->_elem->slot duke@435: ) { duke@435: return entry; duke@435: } duke@435: entry = entry->_next; duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // Return false if the local variable is found in table. duke@435: // Return true if no duplicate is found. duke@435: // And local variable is added as a new entry in table. duke@435: bool LVT_put_after_lookup(LocalVariableTableElement *elem, LVT_Hash** table) { duke@435: // First lookup for duplicates duke@435: int index = hash(elem); duke@435: LVT_Hash* entry = LVT_lookup(elem, index, table); duke@435: duke@435: if (entry != NULL) { duke@435: return false; duke@435: } duke@435: // No duplicate is found, allocate a new entry and fill it. duke@435: if ((entry = new LVT_Hash()) == NULL) { duke@435: return false; duke@435: } duke@435: entry->_elem = elem; duke@435: duke@435: // Insert into hash table duke@435: entry->_next = table[index]; duke@435: table[index] = entry; duke@435: duke@435: return true; duke@435: } duke@435: duke@435: void copy_lvt_element(Classfile_LVT_Element *src, LocalVariableTableElement *lvt) { duke@435: lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci); duke@435: lvt->length = Bytes::get_Java_u2((u1*) &src->length); duke@435: lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index); duke@435: lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index); duke@435: lvt->signature_cp_index = 0; duke@435: lvt->slot = Bytes::get_Java_u2((u1*) &src->slot); duke@435: } duke@435: duke@435: // Function is used to parse both attributes: duke@435: // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) duke@435: u2* ClassFileParser::parse_localvariable_table(u4 code_length, duke@435: u2 max_locals, duke@435: u4 code_attribute_length, duke@435: u2* localvariable_table_length, duke@435: bool isLVTT, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; duke@435: *localvariable_table_length = cfs->get_u2(CHECK_NULL); duke@435: unsigned int size = (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2); duke@435: // Verify local variable table attribute has right length duke@435: if (_need_verify) { duke@435: guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)), duke@435: "%s has wrong length in class file %s", tbl_name, CHECK_NULL); duke@435: } duke@435: u2* localvariable_table_start = cfs->get_u2_buffer(); duke@435: assert(localvariable_table_start != NULL, "null local variable table"); duke@435: if (!_need_verify) { duke@435: cfs->skip_u2_fast(size); duke@435: } else { duke@435: cfs->guarantee_more(size * 2, CHECK_NULL); duke@435: for(int i = 0; i < (*localvariable_table_length); i++) { duke@435: u2 start_pc = cfs->get_u2_fast(); duke@435: u2 length = cfs->get_u2_fast(); duke@435: u2 name_index = cfs->get_u2_fast(); duke@435: u2 descriptor_index = cfs->get_u2_fast(); duke@435: u2 index = cfs->get_u2_fast(); duke@435: // Assign to a u4 to avoid overflow duke@435: u4 end_pc = (u4)start_pc + (u4)length; duke@435: duke@435: if (start_pc >= code_length) { duke@435: classfile_parse_error( duke@435: "Invalid start_pc %u in %s in class file %s", duke@435: start_pc, tbl_name, CHECK_NULL); duke@435: } duke@435: if (end_pc > code_length) { duke@435: classfile_parse_error( duke@435: "Invalid length %u in %s in class file %s", duke@435: length, tbl_name, CHECK_NULL); duke@435: } coleenp@4719: int cp_size = _cp->length(); coleenp@4719: guarantee_property(valid_symbol_at(name_index), duke@435: "Name index %u in %s has bad constant type in class file %s", duke@435: name_index, tbl_name, CHECK_NULL); coleenp@4719: guarantee_property(valid_symbol_at(descriptor_index), duke@435: "Signature index %u in %s has bad constant type in class file %s", duke@435: descriptor_index, tbl_name, CHECK_NULL); duke@435: coleenp@4719: Symbol* name = _cp->symbol_at(name_index); coleenp@4719: Symbol* sig = _cp->symbol_at(descriptor_index); duke@435: verify_legal_field_name(name, CHECK_NULL); duke@435: u2 extra_slot = 0; duke@435: if (!isLVTT) { duke@435: verify_legal_field_signature(name, sig, CHECK_NULL); duke@435: duke@435: // 4894874: check special cases for double and long local variables coleenp@2497: if (sig == vmSymbols::type_signature(T_DOUBLE) || coleenp@2497: sig == vmSymbols::type_signature(T_LONG)) { duke@435: extra_slot = 1; duke@435: } duke@435: } duke@435: guarantee_property((index + extra_slot) < max_locals, duke@435: "Invalid index %u in %s in class file %s", duke@435: index, tbl_name, CHECK_NULL); duke@435: } duke@435: } duke@435: return localvariable_table_start; duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index, coleenp@4719: u1* u1_array, u2* u2_array, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: u2 index = 0; // index in the array with long/double occupying two slots duke@435: u4 i1 = *u1_index; duke@435: u4 i2 = *u2_index + 1; duke@435: for(int i = 0; i < array_length; i++) { duke@435: u1 tag = u1_array[i1++] = cfs->get_u1(CHECK); duke@435: index++; duke@435: if (tag == ITEM_Long || tag == ITEM_Double) { duke@435: index++; duke@435: } else if (tag == ITEM_Object) { duke@435: u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK); coleenp@4719: guarantee_property(valid_klass_reference_at(class_index), duke@435: "Bad class index %u in StackMap in class file %s", duke@435: class_index, CHECK); duke@435: } else if (tag == ITEM_Uninitialized) { duke@435: u2 offset = u2_array[i2++] = cfs->get_u2(CHECK); duke@435: guarantee_property( duke@435: offset < code_length, duke@435: "Bad uninitialized type offset %u in StackMap in class file %s", duke@435: offset, CHECK); duke@435: } else { duke@435: guarantee_property( duke@435: tag <= (u1)ITEM_Uninitialized, duke@435: "Unknown variable type %u in StackMap in class file %s", duke@435: tag, CHECK); duke@435: } duke@435: } duke@435: u2_array[*u2_index] = index; duke@435: *u1_index = i1; duke@435: *u2_index = i2; duke@435: } duke@435: coleenp@4719: u1* ClassFileParser::parse_stackmap_table( duke@435: u4 code_attribute_length, TRAPS) { duke@435: if (code_attribute_length == 0) duke@435: return NULL; duke@435: duke@435: ClassFileStream* cfs = stream(); duke@435: u1* stackmap_table_start = cfs->get_u1_buffer(); duke@435: assert(stackmap_table_start != NULL, "null stackmap table"); duke@435: duke@435: // check code_attribute_length first duke@435: stream()->skip_u1(code_attribute_length, CHECK_NULL); duke@435: duke@435: if (!_need_verify && !DumpSharedSpaces) { duke@435: return NULL; duke@435: } coleenp@4719: return stackmap_table_start; duke@435: } duke@435: duke@435: u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length, duke@435: u4 method_attribute_length, coleenp@4719: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length duke@435: *checked_exceptions_length = cfs->get_u2_fast(); duke@435: unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); duke@435: u2* checked_exceptions_start = cfs->get_u2_buffer(); duke@435: assert(checked_exceptions_start != NULL, "null checked exceptions"); duke@435: if (!_need_verify) { duke@435: cfs->skip_u2_fast(size); duke@435: } else { duke@435: // Verify each value in the checked exception table duke@435: u2 checked_exception; duke@435: u2 len = *checked_exceptions_length; duke@435: cfs->guarantee_more(2 * len, CHECK_NULL); duke@435: for (int i = 0; i < len; i++) { duke@435: checked_exception = cfs->get_u2_fast(); duke@435: check_property( coleenp@4719: valid_klass_reference_at(checked_exception), duke@435: "Exception name has bad type at constant pool %u in class file %s", duke@435: checked_exception, CHECK_NULL); duke@435: } duke@435: } duke@435: // check exceptions attribute length duke@435: if (_need_verify) { duke@435: guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) + duke@435: sizeof(u2) * size), duke@435: "Exceptions attribute has wrong length in class file %s", CHECK_NULL); duke@435: } duke@435: return checked_exceptions_start; duke@435: } duke@435: kamg@1941: void ClassFileParser::throwIllegalSignature( coleenp@2497: const char* type, Symbol* name, Symbol* sig, TRAPS) { kamg@1941: ResourceMark rm(THREAD); kamg@1941: Exceptions::fthrow(THREAD_AND_LOCATION, kamg@1941: vmSymbols::java_lang_ClassFormatError(), kamg@1941: "%s \"%s\" in class %s has illegal signature \"%s\"", type, kamg@1941: name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); kamg@1941: } duke@435: jrose@3926: // Skip an annotation. Return >=limit if there is any problem. jrose@3926: int ClassFileParser::skip_annotation(u1* buffer, int limit, int index) { jrose@3926: // annotation := atype:u2 do(nmem:u2) {member:u2 value} jrose@3926: // value := switch (tag:u1) { ... } jrose@3926: index += 2; // skip atype jrose@3926: if ((index += 2) >= limit) return limit; // read nmem jrose@3926: int nmem = Bytes::get_Java_u2(buffer+index-2); jrose@3926: while (--nmem >= 0 && index < limit) { jrose@3926: index += 2; // skip member jrose@3926: index = skip_annotation_value(buffer, limit, index); jrose@3926: } jrose@3926: return index; jrose@3926: } jrose@3926: jrose@3926: // Skip an annotation value. Return >=limit if there is any problem. jrose@3926: int ClassFileParser::skip_annotation_value(u1* buffer, int limit, int index) { jrose@3926: // value := switch (tag:u1) { jrose@3926: // case B, C, I, S, Z, D, F, J, c: con:u2; jrose@3926: // case e: e_class:u2 e_name:u2; jrose@3926: // case s: s_con:u2; jrose@3926: // case [: do(nval:u2) {value}; jrose@3926: // case @: annotation; jrose@3926: // case s: s_con:u2; jrose@3926: // } jrose@3926: if ((index += 1) >= limit) return limit; // read tag jrose@3926: u1 tag = buffer[index-1]; jrose@3926: switch (tag) { jrose@3926: case 'B': case 'C': case 'I': case 'S': case 'Z': jrose@3926: case 'D': case 'F': case 'J': case 'c': case 's': jrose@3926: index += 2; // skip con or s_con jrose@3926: break; jrose@3926: case 'e': jrose@3926: index += 4; // skip e_class, e_name jrose@3926: break; jrose@3926: case '[': jrose@3926: { jrose@3926: if ((index += 2) >= limit) return limit; // read nval jrose@3926: int nval = Bytes::get_Java_u2(buffer+index-2); jrose@3926: while (--nval >= 0 && index < limit) { jrose@3926: index = skip_annotation_value(buffer, limit, index); jrose@3926: } jrose@3926: } jrose@3926: break; jrose@3926: case '@': jrose@3926: index = skip_annotation(buffer, limit, index); jrose@3926: break; jrose@3926: default: jrose@3926: assert(false, "annotation tag"); jrose@3926: return limit; // bad tag byte jrose@3926: } jrose@3926: return index; jrose@3926: } jrose@3926: jrose@3926: // Sift through annotations, looking for those significant to the VM: coleenp@4719: void ClassFileParser::parse_annotations(u1* buffer, int limit, jrose@3926: ClassFileParser::AnnotationCollector* coll, jrose@3926: TRAPS) { jrose@3926: // annotations := do(nann:u2) {annotation} jrose@3926: int index = 0; jrose@3926: if ((index += 2) >= limit) return; // read nann jrose@3926: int nann = Bytes::get_Java_u2(buffer+index-2); jrose@3926: enum { // initial annotation layout jrose@3926: atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' jrose@3926: count_off = 2, // u2 such as 1 (one value) jrose@3926: member_off = 4, // utf8 such as 'value' jrose@3926: tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) jrose@3926: e_tag_val = 'e', jrose@3926: e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' jrose@3926: e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' jrose@3926: e_size = 11, // end of 'e' annotation jwilhelm@4430: c_tag_val = 'c', // payload is type jwilhelm@4430: c_con_off = 7, // utf8 payload, such as 'I' jrose@3926: c_size = 9, // end of 'c' annotation jwilhelm@4430: s_tag_val = 's', // payload is String jwilhelm@4430: s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' jwilhelm@4430: s_size = 9, jrose@3926: min_size = 6 // smallest possible size (zero members) jrose@3926: }; jrose@3926: while ((--nann) >= 0 && (index-2 + min_size <= limit)) { jrose@3926: int index0 = index; jrose@3926: index = skip_annotation(buffer, limit, index); jrose@3926: u1* abase = buffer + index0; jrose@3926: int atype = Bytes::get_Java_u2(abase + atype_off); jrose@3926: int count = Bytes::get_Java_u2(abase + count_off); coleenp@4719: Symbol* aname = check_symbol_at(_cp, atype); jrose@3926: if (aname == NULL) break; // invalid annotation name jrose@3926: Symbol* member = NULL; jrose@3926: if (count >= 1) { jrose@3926: int member_index = Bytes::get_Java_u2(abase + member_off); coleenp@4719: member = check_symbol_at(_cp, member_index); jrose@3926: if (member == NULL) break; // invalid member name jrose@3926: } jrose@3926: jrose@3926: // Here is where parsing particular annotations will take place. coleenp@4719: AnnotationCollector::ID id = coll->annotation_index(_loader_data, aname); jrose@3926: if (id == AnnotationCollector::_unknown) continue; jrose@3926: coll->set_annotation(id); jwilhelm@4430: jwilhelm@4430: if (id == AnnotationCollector::_sun_misc_Contended) { shade@5137: // @Contended can optionally specify the contention group. shade@5137: // shade@5137: // Contended group defines the equivalence class over the fields: shade@5137: // the fields within the same contended group are not treated distinct. shade@5137: // The only exception is default group, which does not incur the shade@5137: // equivalence. Naturally, contention group for classes is meaningless. shade@5137: // shade@5137: // While the contention group is specified as String, annotation shade@5137: // values are already interned, and we might as well use the constant shade@5137: // pool index as the group tag. shade@5137: // shade@5137: u2 group_index = 0; // default contended group jwilhelm@4430: if (count == 1 jwilhelm@4430: && s_size == (index - index0) // match size jwilhelm@4430: && s_tag_val == *(abase + tag_off) jwilhelm@4430: && member == vmSymbols::value_name()) { shade@5137: group_index = Bytes::get_Java_u2(abase + s_con_off); shade@5137: if (_cp->symbol_at(group_index)->utf8_length() == 0) { shade@5137: group_index = 0; // default contended group shade@5137: } jwilhelm@4430: } shade@5137: coll->set_contended_group(group_index); jwilhelm@4430: } jrose@3926: } jrose@3926: } jrose@3926: jwilhelm@4430: ClassFileParser::AnnotationCollector::ID jwilhelm@4430: ClassFileParser::AnnotationCollector::annotation_index(ClassLoaderData* loader_data, jwilhelm@4430: Symbol* name) { jrose@3926: vmSymbols::SID sid = vmSymbols::find_sid(name); kmo@4481: // Privileged code can use all annotations. Other code silently drops some. twisti@4866: const bool privileged = loader_data->is_the_null_class_loader_data() || twisti@4866: loader_data->is_ext_class_loader_data() || twisti@4866: loader_data->is_anonymous(); jrose@3926: switch (sid) { twisti@4866: case vmSymbols::VM_SYMBOL_ENUM_NAME(sun_reflect_CallerSensitive_signature): twisti@4866: if (_location != _in_method) break; // only allow for methods twisti@4866: if (!privileged) break; // only allow in privileged code twisti@4866: return _method_CallerSensitive; jrose@3926: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_ForceInline_signature): jrose@3926: if (_location != _in_method) break; // only allow for methods jwilhelm@4430: if (!privileged) break; // only allow in privileged code jrose@3926: return _method_ForceInline; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_DontInline_signature): twisti@3969: if (_location != _in_method) break; // only allow for methods jwilhelm@4430: if (!privileged) break; // only allow in privileged code twisti@3969: return _method_DontInline; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): twisti@3969: if (_location != _in_method) break; // only allow for methods jwilhelm@4430: if (!privileged) break; // only allow in privileged code twisti@3969: return _method_LambdaForm_Compiled; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Hidden_signature): twisti@3969: if (_location != _in_method) break; // only allow for methods jwilhelm@4430: if (!privileged) break; // only allow in privileged code twisti@3969: return _method_LambdaForm_Hidden; jwilhelm@4430: case vmSymbols::VM_SYMBOL_ENUM_NAME(sun_misc_Contended_signature): jwilhelm@4430: if (_location != _in_field && _location != _in_class) break; // only allow for fields and classes jwilhelm@4430: if (!EnableContended || (RestrictContended && !privileged)) break; // honor privileges jwilhelm@4430: return _sun_misc_Contended; jrose@3926: default: break; jrose@3926: } jrose@3926: return AnnotationCollector::_unknown; jrose@3926: } jrose@3926: jrose@3926: void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { jwilhelm@4430: if (is_contended()) jwilhelm@4430: f->set_contended_group(contended_group()); jrose@3926: } jrose@3926: coleenp@4719: ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() { coleenp@4719: // If there's an error deallocate metadata for field annotations coleenp@4719: MetadataFactory::free_array(_loader_data, _field_annotations); coleenp@4719: MetadataFactory::free_array(_loader_data, _field_type_annotations); coleenp@4719: } coleenp@4719: jrose@3926: void ClassFileParser::MethodAnnotationCollector::apply_to(methodHandle m) { twisti@4866: if (has_annotation(_method_CallerSensitive)) twisti@4866: m->set_caller_sensitive(true); jrose@3926: if (has_annotation(_method_ForceInline)) jrose@3926: m->set_force_inline(true); twisti@3969: if (has_annotation(_method_DontInline)) twisti@3969: m->set_dont_inline(true); twisti@3969: if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none) twisti@3969: m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm); twisti@3969: if (has_annotation(_method_LambdaForm_Hidden)) twisti@3969: m->set_hidden(true); jrose@3926: } jrose@3926: jrose@3926: void ClassFileParser::ClassAnnotationCollector::apply_to(instanceKlassHandle k) { jwilhelm@4430: k->set_is_contended(is_contended()); jrose@3926: } jrose@3926: jrose@3926: duke@435: #define MAX_ARGS_SIZE 255 duke@435: #define MAX_CODE_SIZE 65535 duke@435: #define INITIAL_MAX_LVT_NUMBER 256 duke@435: coleenp@4572: /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. coleenp@4572: * coleenp@4572: * Rules for LVT's and LVTT's are: coleenp@4572: * - There can be any number of LVT's and LVTT's. coleenp@4572: * - If there are n LVT's, it is the same as if there was just coleenp@4572: * one LVT containing all the entries from the n LVT's. coleenp@4572: * - There may be no more than one LVT entry per local variable. coleenp@4572: * Two LVT entries are 'equal' if these fields are the same: coleenp@4572: * start_pc, length, name, slot coleenp@4572: * - There may be no more than one LVTT entry per each LVT entry. coleenp@4572: * Each LVTT entry has to match some LVT entry. coleenp@4572: * - HotSpot internal LVT keeps natural ordering of class file LVT entries. coleenp@4572: */ coleenp@4572: void ClassFileParser::copy_localvariable_table(ConstMethod* cm, coleenp@4572: int lvt_cnt, coleenp@4572: u2* localvariable_table_length, coleenp@4572: u2** localvariable_table_start, coleenp@4572: int lvtt_cnt, coleenp@4572: u2* localvariable_type_table_length, coleenp@4572: u2** localvariable_type_table_start, coleenp@4572: TRAPS) { coleenp@4572: coleenp@4572: LVT_Hash** lvt_Hash = NEW_RESOURCE_ARRAY(LVT_Hash*, HASH_ROW_SIZE); coleenp@4572: initialize_hashtable(lvt_Hash); coleenp@4572: coleenp@4572: // To fill LocalVariableTable in coleenp@4572: Classfile_LVT_Element* cf_lvt; coleenp@4572: LocalVariableTableElement* lvt = cm->localvariable_table_start(); coleenp@4572: coleenp@4572: for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { coleenp@4572: cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; coleenp@4572: for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { coleenp@4572: copy_lvt_element(&cf_lvt[idx], lvt); coleenp@4572: // If no duplicates, add LVT elem in hashtable lvt_Hash. coleenp@4572: if (LVT_put_after_lookup(lvt, lvt_Hash) == false coleenp@4572: && _need_verify coleenp@4572: && _major_version >= JAVA_1_5_VERSION) { coleenp@4572: clear_hashtable(lvt_Hash); coleenp@4572: classfile_parse_error("Duplicated LocalVariableTable attribute " coleenp@4572: "entry for '%s' in class file %s", coleenp@4719: _cp->symbol_at(lvt->name_cp_index)->as_utf8(), coleenp@4572: CHECK); coleenp@4572: } coleenp@4572: } coleenp@4572: } coleenp@4572: coleenp@4572: // To merge LocalVariableTable and LocalVariableTypeTable coleenp@4572: Classfile_LVT_Element* cf_lvtt; coleenp@4572: LocalVariableTableElement lvtt_elem; coleenp@4572: coleenp@4572: for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { coleenp@4572: cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; coleenp@4572: for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { coleenp@4572: copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); coleenp@4572: int index = hash(&lvtt_elem); coleenp@4572: LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash); coleenp@4572: if (entry == NULL) { coleenp@4572: if (_need_verify) { coleenp@4572: clear_hashtable(lvt_Hash); coleenp@4572: classfile_parse_error("LVTT entry for '%s' in class file %s " coleenp@4572: "does not match any LVT entry", coleenp@4719: _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), coleenp@4572: CHECK); coleenp@4572: } coleenp@4572: } else if (entry->_elem->signature_cp_index != 0 && _need_verify) { coleenp@4572: clear_hashtable(lvt_Hash); coleenp@4572: classfile_parse_error("Duplicated LocalVariableTypeTable attribute " coleenp@4572: "entry for '%s' in class file %s", coleenp@4719: _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), coleenp@4572: CHECK); coleenp@4572: } else { coleenp@4572: // to add generic signatures into LocalVariableTable coleenp@4572: entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index; coleenp@4572: } coleenp@4572: } coleenp@4572: } coleenp@4572: clear_hashtable(lvt_Hash); coleenp@4572: } coleenp@4572: coleenp@4572: coleenp@4719: void ClassFileParser::copy_method_annotations(ConstMethod* cm, coleenp@4572: u1* runtime_visible_annotations, coleenp@4572: int runtime_visible_annotations_length, coleenp@4572: u1* runtime_invisible_annotations, coleenp@4572: int runtime_invisible_annotations_length, coleenp@4572: u1* runtime_visible_parameter_annotations, coleenp@4572: int runtime_visible_parameter_annotations_length, coleenp@4572: u1* runtime_invisible_parameter_annotations, coleenp@4572: int runtime_invisible_parameter_annotations_length, coleenp@4572: u1* runtime_visible_type_annotations, coleenp@4572: int runtime_visible_type_annotations_length, coleenp@4572: u1* runtime_invisible_type_annotations, coleenp@4572: int runtime_invisible_type_annotations_length, coleenp@4572: u1* annotation_default, coleenp@4572: int annotation_default_length, coleenp@4572: TRAPS) { coleenp@4572: coleenp@4572: AnnotationArray* a; coleenp@4572: coleenp@4572: if (runtime_visible_annotations_length + coleenp@4572: runtime_invisible_annotations_length > 0) { coleenp@4719: a = assemble_annotations(runtime_visible_annotations, coleenp@4572: runtime_visible_annotations_length, coleenp@4572: runtime_invisible_annotations, coleenp@4572: runtime_invisible_annotations_length, coleenp@4572: CHECK); coleenp@4572: cm->set_method_annotations(a); coleenp@4572: } coleenp@4572: coleenp@4572: if (runtime_visible_parameter_annotations_length + coleenp@4572: runtime_invisible_parameter_annotations_length > 0) { coleenp@4719: a = assemble_annotations(runtime_visible_parameter_annotations, coleenp@4572: runtime_visible_parameter_annotations_length, coleenp@4572: runtime_invisible_parameter_annotations, coleenp@4572: runtime_invisible_parameter_annotations_length, coleenp@4572: CHECK); coleenp@4572: cm->set_parameter_annotations(a); coleenp@4572: } coleenp@4572: coleenp@4572: if (annotation_default_length > 0) { coleenp@4719: a = assemble_annotations(annotation_default, coleenp@4572: annotation_default_length, coleenp@4572: NULL, coleenp@4572: 0, coleenp@4572: CHECK); coleenp@4572: cm->set_default_annotations(a); coleenp@4572: } coleenp@4572: coleenp@4572: if (runtime_visible_type_annotations_length + coleenp@4572: runtime_invisible_type_annotations_length > 0) { coleenp@4719: a = assemble_annotations(runtime_visible_type_annotations, coleenp@4572: runtime_visible_type_annotations_length, coleenp@4572: runtime_invisible_type_annotations, coleenp@4572: runtime_invisible_type_annotations_length, coleenp@4572: CHECK); coleenp@4572: cm->set_type_annotations(a); coleenp@4572: } coleenp@4572: } coleenp@4572: coleenp@4572: duke@435: // Note: the parse_method below is big and clunky because all parsing of the code and exceptions jrose@3926: // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the coleenp@4037: // Method* to save footprint, so we only know the size of the resulting Method* when the duke@435: // entire method attribute is parsed. duke@435: // duke@435: // The promoted_flags parameter is used to pass relevant access_flags duke@435: // from the method back up to the containing klass. These flag values duke@435: // are added to klass's access_flags. duke@435: coleenp@4719: methodHandle ClassFileParser::parse_method(bool is_interface, duke@435: AccessFlags *promoted_flags, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: methodHandle nullHandle; duke@435: ResourceMark rm(THREAD); duke@435: // Parse fixed parts duke@435: cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count duke@435: duke@435: int flags = cfs->get_u2_fast(); duke@435: u2 name_index = cfs->get_u2_fast(); coleenp@4719: int cp_size = _cp->length(); duke@435: check_property( coleenp@4719: valid_symbol_at(name_index), duke@435: "Illegal constant pool index %u for method name in class file %s", duke@435: name_index, CHECK_(nullHandle)); coleenp@4719: Symbol* name = _cp->symbol_at(name_index); duke@435: verify_legal_method_name(name, CHECK_(nullHandle)); duke@435: duke@435: u2 signature_index = cfs->get_u2_fast(); duke@435: guarantee_property( coleenp@4719: valid_symbol_at(signature_index), duke@435: "Illegal constant pool index %u for method signature in class file %s", duke@435: signature_index, CHECK_(nullHandle)); coleenp@4719: Symbol* signature = _cp->symbol_at(signature_index); duke@435: duke@435: AccessFlags access_flags; duke@435: if (name == vmSymbols::class_initializer_name()) { kamg@2616: // We ignore the other access flags for a valid class initializer. kamg@2616: // (JVM Spec 2nd ed., chapter 4.6) kamg@2616: if (_major_version < 51) { // backward compatibility kamg@2616: flags = JVM_ACC_STATIC; kamg@2616: } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) { kamg@2616: flags &= JVM_ACC_STATIC | JVM_ACC_STRICT; kamg@2616: } duke@435: } else { duke@435: verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle)); duke@435: } duke@435: duke@435: int args_size = -1; // only used when _need_verify is true duke@435: if (_need_verify) { duke@435: args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) + duke@435: verify_legal_method_signature(name, signature, CHECK_(nullHandle)); duke@435: if (args_size > MAX_ARGS_SIZE) { duke@435: classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: duke@435: access_flags.set_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS); duke@435: duke@435: // Default values for code and exceptions attribute elements duke@435: u2 max_stack = 0; duke@435: u2 max_locals = 0; duke@435: u4 code_length = 0; duke@435: u1* code_start = 0; duke@435: u2 exception_table_length = 0; jiangli@3917: u2* exception_table_start = NULL; coleenp@4037: Array* exception_handlers = Universe::the_empty_int_array(); duke@435: u2 checked_exceptions_length = 0; duke@435: u2* checked_exceptions_start = NULL; duke@435: CompressedLineNumberWriteStream* linenumber_table = NULL; duke@435: int linenumber_table_length = 0; duke@435: int total_lvt_length = 0; duke@435: u2 lvt_cnt = 0; duke@435: u2 lvtt_cnt = 0; duke@435: bool lvt_allocated = false; duke@435: u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER; duke@435: u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER; duke@435: u2* localvariable_table_length; duke@435: u2** localvariable_table_start; duke@435: u2* localvariable_type_table_length; duke@435: u2** localvariable_type_table_start; coleenp@4398: u2 method_parameters_length = 0; coleenp@4398: u1* method_parameters_data = NULL; emc@4524: bool method_parameters_seen = false; duke@435: bool parsed_code_attribute = false; duke@435: bool parsed_checked_exceptions_attribute = false; duke@435: bool parsed_stackmap_attribute = false; duke@435: // stackmap attribute - JDK1.5 coleenp@4719: u1* stackmap_data = NULL; coleenp@4719: int stackmap_data_length = 0; duke@435: u2 generic_signature_index = 0; jrose@3926: MethodAnnotationCollector parsed_annotations; duke@435: u1* runtime_visible_annotations = NULL; duke@435: int runtime_visible_annotations_length = 0; duke@435: u1* runtime_invisible_annotations = NULL; duke@435: int runtime_invisible_annotations_length = 0; duke@435: u1* runtime_visible_parameter_annotations = NULL; duke@435: int runtime_visible_parameter_annotations_length = 0; duke@435: u1* runtime_invisible_parameter_annotations = NULL; duke@435: int runtime_invisible_parameter_annotations_length = 0; stefank@4393: u1* runtime_visible_type_annotations = NULL; stefank@4393: int runtime_visible_type_annotations_length = 0; stefank@4393: u1* runtime_invisible_type_annotations = NULL; stefank@4393: int runtime_invisible_type_annotations_length = 0; duke@435: u1* annotation_default = NULL; duke@435: int annotation_default_length = 0; duke@435: duke@435: // Parse code and exceptions attribute duke@435: u2 method_attributes_count = cfs->get_u2_fast(); duke@435: while (method_attributes_count--) { duke@435: cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length duke@435: u2 method_attribute_name_index = cfs->get_u2_fast(); duke@435: u4 method_attribute_length = cfs->get_u4_fast(); duke@435: check_property( coleenp@4719: valid_symbol_at(method_attribute_name_index), duke@435: "Invalid method attribute name index %u in class file %s", duke@435: method_attribute_name_index, CHECK_(nullHandle)); duke@435: coleenp@4719: Symbol* method_attribute_name = _cp->symbol_at(method_attribute_name_index); duke@435: if (method_attribute_name == vmSymbols::tag_code()) { duke@435: // Parse Code attribute duke@435: if (_need_verify) { kamg@4245: guarantee_property( kamg@4245: !access_flags.is_native() && !access_flags.is_abstract(), duke@435: "Code attribute in native or abstract methods in class file %s", duke@435: CHECK_(nullHandle)); duke@435: } duke@435: if (parsed_code_attribute) { duke@435: classfile_parse_error("Multiple Code attributes in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: parsed_code_attribute = true; duke@435: duke@435: // Stack size, locals size, and code size duke@435: if (_major_version == 45 && _minor_version <= 2) { duke@435: cfs->guarantee_more(4, CHECK_(nullHandle)); duke@435: max_stack = cfs->get_u1_fast(); duke@435: max_locals = cfs->get_u1_fast(); duke@435: code_length = cfs->get_u2_fast(); duke@435: } else { duke@435: cfs->guarantee_more(8, CHECK_(nullHandle)); duke@435: max_stack = cfs->get_u2_fast(); duke@435: max_locals = cfs->get_u2_fast(); duke@435: code_length = cfs->get_u4_fast(); duke@435: } duke@435: if (_need_verify) { duke@435: guarantee_property(args_size <= max_locals, duke@435: "Arguments can't fit into locals in class file %s", CHECK_(nullHandle)); duke@435: guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE, duke@435: "Invalid method Code length %u in class file %s", duke@435: code_length, CHECK_(nullHandle)); duke@435: } duke@435: // Code pointer duke@435: code_start = cfs->get_u1_buffer(); duke@435: assert(code_start != NULL, "null code start"); duke@435: cfs->guarantee_more(code_length, CHECK_(nullHandle)); duke@435: cfs->skip_u1_fast(code_length); duke@435: duke@435: // Exception handler table duke@435: cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length duke@435: exception_table_length = cfs->get_u2_fast(); duke@435: if (exception_table_length > 0) { jiangli@3917: exception_table_start = coleenp@4719: parse_exception_table(code_length, exception_table_length, CHECK_(nullHandle)); duke@435: } duke@435: duke@435: // Parse additional attributes in code attribute duke@435: cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count duke@435: u2 code_attributes_count = cfs->get_u2_fast(); kamg@527: kamg@527: unsigned int calculated_attribute_length = 0; kamg@527: kamg@527: if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) { kamg@527: calculated_attribute_length = kamg@527: sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length); kamg@527: } else { kamg@527: // max_stack, locals and length are smaller in pre-version 45.2 classes kamg@527: calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2); kamg@527: } kamg@527: calculated_attribute_length += kamg@527: code_length + kamg@527: sizeof(exception_table_length) + kamg@527: sizeof(code_attributes_count) + kamg@527: exception_table_length * kamg@527: ( sizeof(u2) + // start_pc kamg@527: sizeof(u2) + // end_pc kamg@527: sizeof(u2) + // handler_pc kamg@527: sizeof(u2) ); // catch_type_index duke@435: duke@435: while (code_attributes_count--) { duke@435: cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length duke@435: u2 code_attribute_name_index = cfs->get_u2_fast(); duke@435: u4 code_attribute_length = cfs->get_u4_fast(); duke@435: calculated_attribute_length += code_attribute_length + duke@435: sizeof(code_attribute_name_index) + duke@435: sizeof(code_attribute_length); coleenp@4719: check_property(valid_symbol_at(code_attribute_name_index), duke@435: "Invalid code attribute name index %u in class file %s", duke@435: code_attribute_name_index, duke@435: CHECK_(nullHandle)); duke@435: if (LoadLineNumberTables && coleenp@4719: _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { duke@435: // Parse and compress line number table duke@435: parse_linenumber_table(code_attribute_length, code_length, duke@435: &linenumber_table, CHECK_(nullHandle)); duke@435: duke@435: } else if (LoadLocalVariableTables && coleenp@4719: _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { duke@435: // Parse local variable table duke@435: if (!lvt_allocated) { duke@435: localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2*, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2*, INITIAL_MAX_LVT_NUMBER); duke@435: lvt_allocated = true; duke@435: } duke@435: if (lvt_cnt == max_lvt_cnt) { duke@435: max_lvt_cnt <<= 1; duke@435: REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt); duke@435: REALLOC_RESOURCE_ARRAY(u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt); duke@435: } duke@435: localvariable_table_start[lvt_cnt] = duke@435: parse_localvariable_table(code_length, duke@435: max_locals, duke@435: code_attribute_length, duke@435: &localvariable_table_length[lvt_cnt], duke@435: false, // is not LVTT duke@435: CHECK_(nullHandle)); duke@435: total_lvt_length += localvariable_table_length[lvt_cnt]; duke@435: lvt_cnt++; duke@435: } else if (LoadLocalVariableTypeTables && duke@435: _major_version >= JAVA_1_5_VERSION && coleenp@4719: _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { duke@435: if (!lvt_allocated) { duke@435: localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2*, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2, INITIAL_MAX_LVT_NUMBER); duke@435: localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, u2*, INITIAL_MAX_LVT_NUMBER); duke@435: lvt_allocated = true; duke@435: } duke@435: // Parse local variable type table duke@435: if (lvtt_cnt == max_lvtt_cnt) { duke@435: max_lvtt_cnt <<= 1; duke@435: REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt); duke@435: REALLOC_RESOURCE_ARRAY(u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt); duke@435: } duke@435: localvariable_type_table_start[lvtt_cnt] = duke@435: parse_localvariable_table(code_length, duke@435: max_locals, duke@435: code_attribute_length, duke@435: &localvariable_type_table_length[lvtt_cnt], duke@435: true, // is LVTT duke@435: CHECK_(nullHandle)); duke@435: lvtt_cnt++; hseigel@4819: } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && coleenp@4719: _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { duke@435: // Stack map is only needed by the new verifier in JDK1.5. duke@435: if (parsed_stackmap_attribute) { duke@435: classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle)); duke@435: } coleenp@4719: stackmap_data = parse_stackmap_table(code_attribute_length, CHECK_(nullHandle)); coleenp@4719: stackmap_data_length = code_attribute_length; duke@435: parsed_stackmap_attribute = true; duke@435: } else { duke@435: // Skip unknown attributes duke@435: cfs->skip_u1(code_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: // check method attribute length duke@435: if (_need_verify) { duke@435: guarantee_property(method_attribute_length == calculated_attribute_length, duke@435: "Code segment has wrong length in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: } else if (method_attribute_name == vmSymbols::tag_exceptions()) { duke@435: // Parse Exceptions attribute duke@435: if (parsed_checked_exceptions_attribute) { duke@435: classfile_parse_error("Multiple Exceptions attributes in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: parsed_checked_exceptions_attribute = true; duke@435: checked_exceptions_start = duke@435: parse_checked_exceptions(&checked_exceptions_length, duke@435: method_attribute_length, coleenp@4719: CHECK_(nullHandle)); coleenp@4398: } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { emc@4524: // reject multiple method parameters emc@4524: if (method_parameters_seen) { emc@4524: classfile_parse_error("Multiple MethodParameters attributes in class file %s", CHECK_(nullHandle)); emc@4524: } emc@4524: method_parameters_seen = true; coleenp@4398: method_parameters_length = cfs->get_u1_fast(); emc@4986: if (method_attribute_length != (method_parameters_length * 4u) + 1u) { emc@4524: classfile_parse_error( emc@4524: "Invalid MethodParameters method attribute length %u in class file", emc@4524: method_attribute_length, CHECK_(nullHandle)); emc@4524: } coleenp@4398: method_parameters_data = cfs->get_u1_buffer(); coleenp@4398: cfs->skip_u2_fast(method_parameters_length); emc@4986: cfs->skip_u2_fast(method_parameters_length); coleenp@4398: // ignore this attribute if it cannot be reflected coleenp@4398: if (!SystemDictionary::Parameter_klass_loaded()) coleenp@4398: method_parameters_length = 0; duke@435: } else if (method_attribute_name == vmSymbols::tag_synthetic()) { duke@435: if (method_attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Synthetic method attribute length %u in class file %s", duke@435: method_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: // Should we check that there hasn't already been a synthetic attribute? duke@435: access_flags.set_is_synthetic(); duke@435: } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120 duke@435: if (method_attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Deprecated method attribute length %u in class file %s", duke@435: method_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: } else if (_major_version >= JAVA_1_5_VERSION) { duke@435: if (method_attribute_name == vmSymbols::tag_signature()) { duke@435: if (method_attribute_length != 2) { duke@435: classfile_parse_error( duke@435: "Invalid Signature attribute length %u in class file %s", duke@435: method_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: cfs->guarantee_more(2, CHECK_(nullHandle)); // generic_signature_index duke@435: generic_signature_index = cfs->get_u2_fast(); duke@435: } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { duke@435: runtime_visible_annotations_length = method_attribute_length; duke@435: runtime_visible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_visible_annotations != NULL, "null visible annotations"); coleenp@4719: parse_annotations(runtime_visible_annotations, coleenp@4719: runtime_visible_annotations_length, &parsed_annotations, kamg@4245: CHECK_(nullHandle)); duke@435: cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle)); duke@435: } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { duke@435: runtime_invisible_annotations_length = method_attribute_length; duke@435: runtime_invisible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_invisible_annotations != NULL, "null invisible annotations"); duke@435: cfs->skip_u1(runtime_invisible_annotations_length, CHECK_(nullHandle)); duke@435: } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) { duke@435: runtime_visible_parameter_annotations_length = method_attribute_length; duke@435: runtime_visible_parameter_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations"); duke@435: cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_(nullHandle)); duke@435: } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) { duke@435: runtime_invisible_parameter_annotations_length = method_attribute_length; duke@435: runtime_invisible_parameter_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_invisible_parameter_annotations != NULL, "null invisible parameter annotations"); duke@435: cfs->skip_u1(runtime_invisible_parameter_annotations_length, CHECK_(nullHandle)); duke@435: } else if (method_attribute_name == vmSymbols::tag_annotation_default()) { duke@435: annotation_default_length = method_attribute_length; duke@435: annotation_default = cfs->get_u1_buffer(); duke@435: assert(annotation_default != NULL, "null annotation default"); duke@435: cfs->skip_u1(annotation_default_length, CHECK_(nullHandle)); stefank@4393: } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { stefank@4393: runtime_visible_type_annotations_length = method_attribute_length; stefank@4393: runtime_visible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); stefank@4393: // No need for the VM to parse Type annotations stefank@4393: cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_(nullHandle)); stefank@4393: } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { stefank@4393: runtime_invisible_type_annotations_length = method_attribute_length; stefank@4393: runtime_invisible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); stefank@4393: cfs->skip_u1(runtime_invisible_type_annotations_length, CHECK_(nullHandle)); duke@435: } else { duke@435: // Skip unknown attributes duke@435: cfs->skip_u1(method_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: } else { duke@435: // Skip unknown attributes duke@435: cfs->skip_u1(method_attribute_length, CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: duke@435: if (linenumber_table != NULL) { duke@435: linenumber_table->write_terminator(); duke@435: linenumber_table_length = linenumber_table->position(); duke@435: } duke@435: duke@435: // Make sure there's at least one Code attribute in non-native/non-abstract method duke@435: if (_need_verify) { duke@435: guarantee_property(access_flags.is_native() || access_flags.is_abstract() || parsed_code_attribute, duke@435: "Absent Code attribute in method that is not native or abstract in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: coleenp@4037: // All sizing information for a Method* is finally available, now create it coleenp@4572: InlineTableSizes sizes( coleenp@4572: total_lvt_length, coleenp@4572: linenumber_table_length, coleenp@4572: exception_table_length, coleenp@4572: checked_exceptions_length, coleenp@4572: method_parameters_length, coleenp@4572: generic_signature_index, coleenp@4572: runtime_visible_annotations_length + coleenp@4572: runtime_invisible_annotations_length, coleenp@4572: runtime_visible_parameter_annotations_length + coleenp@4572: runtime_invisible_parameter_annotations_length, coleenp@4572: runtime_visible_type_annotations_length + coleenp@4572: runtime_invisible_type_annotations_length, coleenp@4572: annotation_default_length, coleenp@4572: 0); coleenp@4572: kamg@4245: Method* m = Method::allocate( coleenp@4719: _loader_data, code_length, access_flags, &sizes, coleenp@4398: ConstMethod::NORMAL, CHECK_(nullHandle)); coleenp@4037: coleenp@4037: ClassLoadingService::add_class_method_size(m->size()*HeapWordSize); duke@435: duke@435: // Fill in information from fixed part (access_flags already set) coleenp@4719: m->set_constants(_cp); duke@435: m->set_name_index(name_index); duke@435: m->set_signature_index(signature_index); duke@435: #ifdef CC_INTERP duke@435: // hmm is there a gc issue here?? coleenp@4719: ResultTypeFinder rtf(_cp->symbol_at(signature_index)); duke@435: m->set_result_index(rtf.type()); duke@435: #endif duke@435: duke@435: if (args_size >= 0) { duke@435: m->set_size_of_parameters(args_size); duke@435: } else { duke@435: m->compute_size_of_parameters(THREAD); duke@435: } duke@435: #ifdef ASSERT duke@435: if (args_size >= 0) { duke@435: m->compute_size_of_parameters(THREAD); duke@435: assert(args_size == m->size_of_parameters(), ""); duke@435: } duke@435: #endif duke@435: duke@435: // Fill in code attribute information duke@435: m->set_max_stack(max_stack); duke@435: m->set_max_locals(max_locals); coleenp@4719: if (stackmap_data != NULL) { coleenp@4719: m->constMethod()->copy_stackmap_data(_loader_data, stackmap_data, coleenp@4719: stackmap_data_length, CHECK_NULL); coleenp@4719: } duke@435: duke@435: // Copy byte codes twisti@1573: m->set_code(code_start); duke@435: duke@435: // Copy line number table duke@435: if (linenumber_table != NULL) { duke@435: memcpy(m->compressed_linenumber_table(), duke@435: linenumber_table->buffer(), linenumber_table_length); duke@435: } duke@435: jiangli@3917: // Copy exception table jiangli@3917: if (exception_table_length > 0) { jiangli@3917: int size = jiangli@3917: exception_table_length * sizeof(ExceptionTableElement) / sizeof(u2); jiangli@3917: copy_u2_with_conversion((u2*) m->exception_table_start(), jiangli@3917: exception_table_start, size); jiangli@3917: } jiangli@3917: coleenp@4398: // Copy method parameters coleenp@4398: if (method_parameters_length > 0) { coleenp@4398: MethodParametersElement* elem = m->constMethod()->method_parameters_start(); emc@4524: for (int i = 0; i < method_parameters_length; i++) { emc@4524: elem[i].name_cp_index = Bytes::get_Java_u2(method_parameters_data); coleenp@4398: method_parameters_data += 2; emc@4986: elem[i].flags = Bytes::get_Java_u2(method_parameters_data); emc@4986: method_parameters_data += 2; coleenp@4398: } coleenp@4398: } coleenp@4398: duke@435: // Copy checked exceptions duke@435: if (checked_exceptions_length > 0) { duke@435: int size = checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2); duke@435: copy_u2_with_conversion((u2*) m->checked_exceptions_start(), checked_exceptions_start, size); duke@435: } duke@435: coleenp@4572: // Copy class file LVT's/LVTT's into the HotSpot internal LVT. duke@435: if (total_lvt_length > 0) { duke@435: promoted_flags->set_has_localvariable_table(); coleenp@4572: copy_localvariable_table(m->constMethod(), lvt_cnt, coleenp@4572: localvariable_table_length, coleenp@4572: localvariable_table_start, coleenp@4572: lvtt_cnt, coleenp@4572: localvariable_type_table_length, coleenp@4572: localvariable_type_table_start, CHECK_NULL); duke@435: } duke@435: jrose@3926: if (parsed_annotations.has_any_annotations()) jrose@3926: parsed_annotations.apply_to(m); coleenp@4572: coleenp@4572: // Copy annotations coleenp@4719: copy_method_annotations(m->constMethod(), coleenp@4572: runtime_visible_annotations, coleenp@4572: runtime_visible_annotations_length, coleenp@4572: runtime_invisible_annotations, coleenp@4572: runtime_invisible_annotations_length, coleenp@4572: runtime_visible_parameter_annotations, coleenp@4572: runtime_visible_parameter_annotations_length, coleenp@4572: runtime_invisible_parameter_annotations, coleenp@4572: runtime_invisible_parameter_annotations_length, coleenp@4572: runtime_visible_type_annotations, coleenp@4572: runtime_visible_type_annotations_length, coleenp@4572: runtime_invisible_type_annotations, coleenp@4572: runtime_invisible_type_annotations_length, coleenp@4572: annotation_default, coleenp@4572: annotation_default_length, coleenp@4572: CHECK_NULL); duke@435: coleenp@2497: if (name == vmSymbols::finalize_method_name() && coleenp@2497: signature == vmSymbols::void_method_signature()) { duke@435: if (m->is_empty_method()) { duke@435: _has_empty_finalizer = true; duke@435: } else { duke@435: _has_finalizer = true; duke@435: } duke@435: } coleenp@2497: if (name == vmSymbols::object_initializer_name() && coleenp@2497: signature == vmSymbols::void_method_signature() && duke@435: m->is_vanilla_constructor()) { duke@435: _has_vanilla_constructor = true; duke@435: } duke@435: coleenp@4572: NOT_PRODUCT(m->verify()); duke@435: return m; duke@435: } duke@435: duke@435: duke@435: // The promoted_flags parameter is used to pass relevant access_flags duke@435: // from the methods back up to the containing klass. These flag values duke@435: // are added to klass's access_flags. duke@435: coleenp@4719: Array* ClassFileParser::parse_methods(bool is_interface, coleenp@4142: AccessFlags* promoted_flags, coleenp@4142: bool* has_final_method, kamg@4245: bool* has_default_methods, coleenp@4142: TRAPS) { duke@435: ClassFileStream* cfs = stream(); coleenp@4037: cfs->guarantee_more(2, CHECK_NULL); // length duke@435: u2 length = cfs->get_u2_fast(); duke@435: if (length == 0) { coleenp@4719: _methods = Universe::the_empty_method_array(); duke@435: } else { coleenp@4719: _methods = MetadataFactory::new_array(_loader_data, length, NULL, CHECK_NULL); coleenp@4037: duke@435: HandleMark hm(THREAD); duke@435: for (int index = 0; index < length; index++) { coleenp@4719: methodHandle method = parse_method(is_interface, duke@435: promoted_flags, coleenp@4037: CHECK_NULL); coleenp@4037: duke@435: if (method->is_final()) { duke@435: *has_final_method = true; duke@435: } kamg@4245: if (is_interface && !method->is_abstract() && !method->is_static()) { kamg@4245: // default method kamg@4245: *has_default_methods = true; kamg@4245: } coleenp@4719: _methods->at_put(index, method()); duke@435: } stefank@4393: duke@435: if (_need_verify && length > 1) { duke@435: // Check duplicated methods duke@435: ResourceMark rm(THREAD); duke@435: NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, NameSigHash*, HASH_ROW_SIZE); duke@435: initialize_hashtable(names_and_sigs); duke@435: bool dup = false; duke@435: { duke@435: debug_only(No_Safepoint_Verifier nsv;) duke@435: for (int i = 0; i < length; i++) { coleenp@4719: Method* m = _methods->at(i); duke@435: // If no duplicates, add name/signature in hashtable names_and_sigs. duke@435: if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) { duke@435: dup = true; duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: if (dup) { duke@435: classfile_parse_error("Duplicate method name&signature in class file %s", coleenp@4037: CHECK_NULL); duke@435: } duke@435: } duke@435: } coleenp@4719: return _methods; duke@435: } duke@435: duke@435: coleenp@4719: intArray* ClassFileParser::sort_methods(Array* methods) { coleenp@4037: int length = methods->length(); coleenp@2777: // If JVMTI original method ordering or sharing is enabled we have to duke@435: // remember the original class file ordering. coleenp@4037: // We temporarily use the vtable_index field in the Method* to store the duke@435: // class file index, so we can read in after calling qsort. coleenp@2777: // Put the method ordering in the shared archive. coleenp@2777: if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { duke@435: for (int index = 0; index < length; index++) { coleenp@4037: Method* m = methods->at(index); duke@435: assert(!m->valid_vtable_index(), "vtable index should not be set"); duke@435: m->set_vtable_index(index); duke@435: } duke@435: } duke@435: // Sort method array by ascending method name (for faster lookups & vtable construction) coleenp@2497: // Note that the ordering is not alphabetical, see Symbol::fast_compare coleenp@4572: Method::sort_methods(methods); duke@435: coleenp@4719: intArray* method_ordering = NULL; coleenp@2777: // If JVMTI original method ordering or sharing is enabled construct int coleenp@2777: // array remembering the original ordering coleenp@2777: if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { coleenp@4719: method_ordering = new intArray(length); duke@435: for (int index = 0; index < length; index++) { coleenp@4037: Method* m = methods->at(index); duke@435: int old_index = m->vtable_index(); duke@435: assert(old_index >= 0 && old_index < length, "invalid method index"); coleenp@4037: method_ordering->at_put(index, old_index); coleenp@4037: m->set_vtable_index(Method::invalid_vtable_index); duke@435: } duke@435: } coleenp@4719: return method_ordering; duke@435: } duke@435: duke@435: coleenp@4719: void ClassFileParser::parse_classfile_sourcefile_attribute(TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: cfs->guarantee_more(2, CHECK); // sourcefile_index duke@435: u2 sourcefile_index = cfs->get_u2_fast(); duke@435: check_property( coleenp@4719: valid_symbol_at(sourcefile_index), duke@435: "Invalid SourceFile attribute at constant pool index %u in class file %s", duke@435: sourcefile_index, CHECK); coleenp@4719: set_class_sourcefile(_cp->symbol_at(sourcefile_index)); duke@435: } duke@435: duke@435: duke@435: coleenp@4719: void ClassFileParser::parse_classfile_source_debug_extension_attribute(int length, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: u1* sde_buffer = cfs->get_u1_buffer(); duke@435: assert(sde_buffer != NULL, "null sde buffer"); duke@435: duke@435: // Don't bother storing it if there is no way to retrieve it duke@435: if (JvmtiExport::can_get_source_debug_extension()) { kvn@3930: assert((length+1) > length, "Overflow checking"); kvn@3930: u1* sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); kvn@3930: for (int i = 0; i < length; i++) { kvn@3930: sde[i] = sde_buffer[i]; kvn@3930: } kvn@3930: sde[length] = '\0'; kvn@3930: set_class_sde_buffer((char*)sde, length); duke@435: } duke@435: // Got utf8 string, set stream position forward duke@435: cfs->skip_u1(length, CHECK); duke@435: } duke@435: duke@435: duke@435: // Inner classes can be static, private or protected (classic VM does this) duke@435: #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC) duke@435: duke@435: // Return number of classes in the inner classes attribute table coleenp@4719: u2 ClassFileParser::parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start, jiangli@3670: bool parsed_enclosingmethod_attribute, jiangli@3670: u2 enclosing_method_class_index, jiangli@3670: u2 enclosing_method_method_index, jrose@3926: TRAPS) { duke@435: ClassFileStream* cfs = stream(); jiangli@3670: u1* current_mark = cfs->current(); jiangli@3670: u2 length = 0; jiangli@3670: if (inner_classes_attribute_start != NULL) { jiangli@3670: cfs->set_current(inner_classes_attribute_start); jiangli@3670: cfs->guarantee_more(2, CHECK_0); // length jiangli@3670: length = cfs->get_u2_fast(); jiangli@3670: } jiangli@3670: jiangli@3670: // 4-tuples of shorts of inner classes data and 2 shorts of enclosing jiangli@3670: // method data: jiangli@3670: // [inner_class_info_index, jiangli@3670: // outer_class_info_index, jiangli@3670: // inner_name_index, jiangli@3670: // inner_class_access_flags, jiangli@3670: // ... jiangli@3670: // enclosing_method_class_index, jiangli@3670: // enclosing_method_method_index] jiangli@3670: int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); coleenp@4719: Array* inner_classes = MetadataFactory::new_array(_loader_data, size, CHECK_0); coleenp@4719: _inner_classes = inner_classes; coleenp@4719: duke@435: int index = 0; coleenp@4719: int cp_size = _cp->length(); duke@435: cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 duke@435: for (int n = 0; n < length; n++) { duke@435: // Inner class index duke@435: u2 inner_class_info_index = cfs->get_u2_fast(); duke@435: check_property( duke@435: inner_class_info_index == 0 || coleenp@4719: valid_klass_reference_at(inner_class_info_index), duke@435: "inner_class_info_index %u has bad constant type in class file %s", duke@435: inner_class_info_index, CHECK_0); duke@435: // Outer class index duke@435: u2 outer_class_info_index = cfs->get_u2_fast(); duke@435: check_property( duke@435: outer_class_info_index == 0 || coleenp@4719: valid_klass_reference_at(outer_class_info_index), duke@435: "outer_class_info_index %u has bad constant type in class file %s", duke@435: outer_class_info_index, CHECK_0); duke@435: // Inner class name duke@435: u2 inner_name_index = cfs->get_u2_fast(); duke@435: check_property( coleenp@4719: inner_name_index == 0 || valid_symbol_at(inner_name_index), duke@435: "inner_name_index %u has bad constant type in class file %s", duke@435: inner_name_index, CHECK_0); duke@435: if (_need_verify) { duke@435: guarantee_property(inner_class_info_index != outer_class_info_index, duke@435: "Class is both outer and inner class in class file %s", CHECK_0); duke@435: } duke@435: // Access flags duke@435: AccessFlags inner_access_flags; duke@435: jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS; duke@435: if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { duke@435: // Set abstract bit for old class files for backward compatibility duke@435: flags |= JVM_ACC_ABSTRACT; duke@435: } duke@435: verify_legal_class_modifiers(flags, CHECK_0); duke@435: inner_access_flags.set_flags(flags); duke@435: coleenp@4037: inner_classes->at_put(index++, inner_class_info_index); coleenp@4037: inner_classes->at_put(index++, outer_class_info_index); coleenp@4037: inner_classes->at_put(index++, inner_name_index); coleenp@4037: inner_classes->at_put(index++, inner_access_flags.as_short()); duke@435: } duke@435: duke@435: // 4347400: make sure there's no duplicate entry in the classes array duke@435: if (_need_verify && _major_version >= JAVA_1_5_VERSION) { jiangli@3670: for(int i = 0; i < length * 4; i += 4) { jiangli@3670: for(int j = i + 4; j < length * 4; j += 4) { coleenp@4037: guarantee_property((inner_classes->at(i) != inner_classes->at(j) || coleenp@4037: inner_classes->at(i+1) != inner_classes->at(j+1) || coleenp@4037: inner_classes->at(i+2) != inner_classes->at(j+2) || coleenp@4037: inner_classes->at(i+3) != inner_classes->at(j+3)), duke@435: "Duplicate entry in InnerClasses in class file %s", duke@435: CHECK_0); duke@435: } duke@435: } duke@435: } duke@435: jiangli@3670: // Set EnclosingMethod class and method indexes. jiangli@3670: if (parsed_enclosingmethod_attribute) { coleenp@4037: inner_classes->at_put(index++, enclosing_method_class_index); coleenp@4037: inner_classes->at_put(index++, enclosing_method_method_index); jiangli@3670: } jiangli@3670: assert(index == size, "wrong size"); jiangli@3670: jiangli@3670: // Restore buffer's current position. jiangli@3670: cfs->set_current(current_mark); jiangli@3670: duke@435: return length; duke@435: } duke@435: coleenp@4719: void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) { jrose@3926: set_class_synthetic_flag(true); duke@435: } duke@435: coleenp@4719: void ClassFileParser::parse_classfile_signature_attribute(TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: u2 signature_index = cfs->get_u2(CHECK); duke@435: check_property( coleenp@4719: valid_symbol_at(signature_index), duke@435: "Invalid constant pool index %u in Signature attribute in class file %s", duke@435: signature_index, CHECK); coleenp@4719: set_class_generic_signature(_cp->symbol_at(signature_index)); duke@435: } duke@435: coleenp@4719: void ClassFileParser::parse_classfile_bootstrap_methods_attribute(u4 attribute_byte_length, TRAPS) { jrose@2353: ClassFileStream* cfs = stream(); jrose@2353: u1* current_start = cfs->current(); jrose@2353: jrose@2353: cfs->guarantee_more(2, CHECK); // length jrose@2353: int attribute_array_length = cfs->get_u2_fast(); jrose@2353: jrose@2353: guarantee_property(_max_bootstrap_specifier_index < attribute_array_length, jrose@2353: "Short length on BootstrapMethods in class file %s", jrose@2353: CHECK); jrose@2353: jrose@2353: // The attribute contains a counted array of counted tuples of shorts, jrose@2353: // represending bootstrap specifiers: jrose@2353: // length*{bootstrap_method_index, argument_count*{argument_index}} jrose@2353: int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2); jrose@2353: // operand_count = number of shorts in attr, except for leading length jrose@2353: jrose@2353: // The attribute is copied into a short[] array. jrose@2353: // The array begins with a series of short[2] pairs, one for each tuple. jrose@2353: int index_size = (attribute_array_length * 2); jrose@2353: coleenp@4719: Array* operands = MetadataFactory::new_array(_loader_data, index_size + operand_count, CHECK); coleenp@4719: coleenp@4719: // Eagerly assign operands so they will be deallocated with the constant coleenp@4719: // pool if there is an error. coleenp@4719: _cp->set_operands(operands); jrose@2353: jrose@2353: int operand_fill_index = index_size; coleenp@4719: int cp_size = _cp->length(); jrose@2353: jrose@2353: for (int n = 0; n < attribute_array_length; n++) { jrose@2353: // Store a 32-bit offset into the header of the operand array. coleenp@4037: ConstantPool::operand_offset_at_put(operands, n, operand_fill_index); jrose@2353: jrose@2353: // Read a bootstrap specifier. jrose@2353: cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc jrose@2353: u2 bootstrap_method_index = cfs->get_u2_fast(); jrose@2353: u2 argument_count = cfs->get_u2_fast(); jrose@2353: check_property( jrose@2353: valid_cp_range(bootstrap_method_index, cp_size) && coleenp@4719: _cp->tag_at(bootstrap_method_index).is_method_handle(), jrose@2353: "bootstrap_method_index %u has bad constant type in class file %s", twisti@2408: bootstrap_method_index, jrose@2353: CHECK); coleenp@4037: operands->at_put(operand_fill_index++, bootstrap_method_index); coleenp@4037: operands->at_put(operand_fill_index++, argument_count); jrose@2353: jrose@2353: cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc] jrose@2353: for (int j = 0; j < argument_count; j++) { twisti@2408: u2 argument_index = cfs->get_u2_fast(); jrose@2353: check_property( twisti@2408: valid_cp_range(argument_index, cp_size) && coleenp@4719: _cp->tag_at(argument_index).is_loadable_constant(), jrose@2353: "argument_index %u has bad constant type in class file %s", twisti@2408: argument_index, jrose@2353: CHECK); coleenp@4037: operands->at_put(operand_fill_index++, argument_index); jrose@2353: } jrose@2353: } jrose@2353: coleenp@4037: assert(operand_fill_index == operands->length(), "exact fill"); coleenp@4037: assert(ConstantPool::operand_array_length(operands) == attribute_array_length, "correct decode"); jrose@2353: jrose@2353: u1* current_end = cfs->current(); jrose@2353: guarantee_property(current_end == current_start + attribute_byte_length, jrose@2353: "Bad length on BootstrapMethods in class file %s", jrose@2353: CHECK); jrose@2353: } jrose@2353: coleenp@4719: void ClassFileParser::parse_classfile_attributes(ClassFileParser::ClassAnnotationCollector* parsed_annotations, jrose@3926: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: // Set inner classes attribute to default sentinel coleenp@4719: _inner_classes = Universe::the_empty_short_array(); duke@435: cfs->guarantee_more(2, CHECK); // attributes_count duke@435: u2 attributes_count = cfs->get_u2_fast(); duke@435: bool parsed_sourcefile_attribute = false; duke@435: bool parsed_innerclasses_attribute = false; duke@435: bool parsed_enclosingmethod_attribute = false; jrose@2353: bool parsed_bootstrap_methods_attribute = false; duke@435: u1* runtime_visible_annotations = NULL; duke@435: int runtime_visible_annotations_length = 0; duke@435: u1* runtime_invisible_annotations = NULL; duke@435: int runtime_invisible_annotations_length = 0; stefank@4393: u1* runtime_visible_type_annotations = NULL; stefank@4393: int runtime_visible_type_annotations_length = 0; stefank@4393: u1* runtime_invisible_type_annotations = NULL; stefank@4393: int runtime_invisible_type_annotations_length = 0; jiangli@3670: u1* inner_classes_attribute_start = NULL; jiangli@3670: u4 inner_classes_attribute_length = 0; jiangli@3670: u2 enclosing_method_class_index = 0; jiangli@3670: u2 enclosing_method_method_index = 0; duke@435: // Iterate over attributes duke@435: while (attributes_count--) { duke@435: cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length duke@435: u2 attribute_name_index = cfs->get_u2_fast(); duke@435: u4 attribute_length = cfs->get_u4_fast(); duke@435: check_property( coleenp@4719: valid_symbol_at(attribute_name_index), duke@435: "Attribute name has bad constant pool index %u in class file %s", duke@435: attribute_name_index, CHECK); coleenp@4719: Symbol* tag = _cp->symbol_at(attribute_name_index); duke@435: if (tag == vmSymbols::tag_source_file()) { duke@435: // Check for SourceFile tag duke@435: if (_need_verify) { duke@435: guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK); duke@435: } duke@435: if (parsed_sourcefile_attribute) { duke@435: classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK); duke@435: } else { duke@435: parsed_sourcefile_attribute = true; duke@435: } coleenp@4719: parse_classfile_sourcefile_attribute(CHECK); duke@435: } else if (tag == vmSymbols::tag_source_debug_extension()) { duke@435: // Check for SourceDebugExtension tag coleenp@4719: parse_classfile_source_debug_extension_attribute((int)attribute_length, CHECK); duke@435: } else if (tag == vmSymbols::tag_inner_classes()) { duke@435: // Check for InnerClasses tag duke@435: if (parsed_innerclasses_attribute) { duke@435: classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK); duke@435: } else { duke@435: parsed_innerclasses_attribute = true; duke@435: } jiangli@3670: inner_classes_attribute_start = cfs->get_u1_buffer(); jiangli@3670: inner_classes_attribute_length = attribute_length; jiangli@3670: cfs->skip_u1(inner_classes_attribute_length, CHECK); duke@435: } else if (tag == vmSymbols::tag_synthetic()) { duke@435: // Check for Synthetic tag duke@435: // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec duke@435: if (attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Synthetic classfile attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: } coleenp@4719: parse_classfile_synthetic_attribute(CHECK); duke@435: } else if (tag == vmSymbols::tag_deprecated()) { duke@435: // Check for Deprecatd tag - 4276120 duke@435: if (attribute_length != 0) { duke@435: classfile_parse_error( duke@435: "Invalid Deprecated classfile attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: } duke@435: } else if (_major_version >= JAVA_1_5_VERSION) { duke@435: if (tag == vmSymbols::tag_signature()) { duke@435: if (attribute_length != 2) { duke@435: classfile_parse_error( duke@435: "Wrong Signature attribute length %u in class file %s", duke@435: attribute_length, CHECK); duke@435: } coleenp@4719: parse_classfile_signature_attribute(CHECK); duke@435: } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { duke@435: runtime_visible_annotations_length = attribute_length; duke@435: runtime_visible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_visible_annotations != NULL, "null visible annotations"); coleenp@4719: parse_annotations(runtime_visible_annotations, jrose@3926: runtime_visible_annotations_length, jrose@3926: parsed_annotations, jrose@3926: CHECK); duke@435: cfs->skip_u1(runtime_visible_annotations_length, CHECK); duke@435: } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) { duke@435: runtime_invisible_annotations_length = attribute_length; duke@435: runtime_invisible_annotations = cfs->get_u1_buffer(); duke@435: assert(runtime_invisible_annotations != NULL, "null invisible annotations"); duke@435: cfs->skip_u1(runtime_invisible_annotations_length, CHECK); duke@435: } else if (tag == vmSymbols::tag_enclosing_method()) { duke@435: if (parsed_enclosingmethod_attribute) { duke@435: classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK); duke@435: } else { duke@435: parsed_enclosingmethod_attribute = true; duke@435: } duke@435: cfs->guarantee_more(4, CHECK); // class_index, method_index jiangli@3670: enclosing_method_class_index = cfs->get_u2_fast(); jiangli@3670: enclosing_method_method_index = cfs->get_u2_fast(); jiangli@3670: if (enclosing_method_class_index == 0) { duke@435: classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK); duke@435: } duke@435: // Validate the constant pool indices and types coleenp@4719: check_property(valid_klass_reference_at(enclosing_method_class_index), coleenp@4719: "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK); jiangli@3670: if (enclosing_method_method_index != 0 && coleenp@4719: (!_cp->is_within_bounds(enclosing_method_method_index) || coleenp@4719: !_cp->tag_at(enclosing_method_method_index).is_name_and_type())) { duke@435: classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK); duke@435: } jrose@2353: } else if (tag == vmSymbols::tag_bootstrap_methods() && jrose@2353: _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { jrose@2353: if (parsed_bootstrap_methods_attribute) jrose@2353: classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK); jrose@2353: parsed_bootstrap_methods_attribute = true; coleenp@4719: parse_classfile_bootstrap_methods_attribute(attribute_length, CHECK); stefank@4393: } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) { stefank@4393: runtime_visible_type_annotations_length = attribute_length; stefank@4393: runtime_visible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); stefank@4393: // No need for the VM to parse Type annotations stefank@4393: cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); stefank@4393: } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_type_annotations()) { stefank@4393: runtime_invisible_type_annotations_length = attribute_length; stefank@4393: runtime_invisible_type_annotations = cfs->get_u1_buffer(); stefank@4393: assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); stefank@4393: cfs->skip_u1(runtime_invisible_type_annotations_length, CHECK); duke@435: } else { duke@435: // Unknown attribute duke@435: cfs->skip_u1(attribute_length, CHECK); duke@435: } duke@435: } else { duke@435: // Unknown attribute duke@435: cfs->skip_u1(attribute_length, CHECK); duke@435: } duke@435: } coleenp@4719: _annotations = assemble_annotations(runtime_visible_annotations, coleenp@4719: runtime_visible_annotations_length, coleenp@4719: runtime_invisible_annotations, coleenp@4719: runtime_invisible_annotations_length, coleenp@4719: CHECK); coleenp@4719: _type_annotations = assemble_annotations(runtime_visible_type_annotations, coleenp@4719: runtime_visible_type_annotations_length, coleenp@4719: runtime_invisible_type_annotations, coleenp@4719: runtime_invisible_type_annotations_length, coleenp@4719: CHECK); jrose@2353: jiangli@3670: if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { jiangli@3670: u2 num_of_classes = parse_classfile_inner_classes_attribute( jiangli@3670: inner_classes_attribute_start, jiangli@3670: parsed_innerclasses_attribute, jiangli@3670: enclosing_method_class_index, jiangli@3670: enclosing_method_method_index, coleenp@4719: CHECK); jiangli@3670: if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { jiangli@3670: guarantee_property( jiangli@3670: inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, jiangli@3670: "Wrong InnerClasses attribute length in class file %s", CHECK); jiangli@3670: } jiangli@3670: } jiangli@3670: jrose@2353: if (_max_bootstrap_specifier_index >= 0) { jrose@2353: guarantee_property(parsed_bootstrap_methods_attribute, jrose@2353: "Missing BootstrapMethods attribute in class file %s", CHECK); jrose@2353: } duke@435: } duke@435: jrose@3926: void ClassFileParser::apply_parsed_class_attributes(instanceKlassHandle k) { jrose@3926: if (_synthetic_flag) jrose@3926: k->set_is_synthetic(); jrose@3926: if (_sourcefile != NULL) { jrose@3926: _sourcefile->increment_refcount(); jrose@3926: k->set_source_file_name(_sourcefile); jrose@3926: } jrose@3926: if (_generic_signature != NULL) { jrose@3926: _generic_signature->increment_refcount(); jrose@3926: k->set_generic_signature(_generic_signature); jrose@3926: } kvn@3930: if (_sde_buffer != NULL) { kvn@3930: k->set_source_debug_extension(_sde_buffer, _sde_length); kvn@3930: } jrose@3926: } duke@435: coleenp@4719: // Transfer ownership of metadata allocated to the InstanceKlass. coleenp@4719: void ClassFileParser::apply_parsed_class_metadata( coleenp@4719: instanceKlassHandle this_klass, coleenp@4719: int java_fields_count, TRAPS) { coleenp@4719: // Assign annotations if needed coleenp@4719: if (_annotations != NULL || _type_annotations != NULL || coleenp@4719: _fields_annotations != NULL || _fields_type_annotations != NULL) { coleenp@4719: Annotations* annotations = Annotations::allocate(_loader_data, CHECK); coleenp@4719: annotations->set_class_annotations(_annotations); coleenp@4719: annotations->set_class_type_annotations(_type_annotations); coleenp@4719: annotations->set_fields_annotations(_fields_annotations); coleenp@4719: annotations->set_fields_type_annotations(_fields_type_annotations); coleenp@4719: this_klass->set_annotations(annotations); coleenp@4719: } coleenp@4719: coleenp@4719: _cp->set_pool_holder(this_klass()); coleenp@4719: this_klass->set_constants(_cp); coleenp@4719: this_klass->set_fields(_fields, java_fields_count); coleenp@4719: this_klass->set_methods(_methods); coleenp@4719: this_klass->set_inner_classes(_inner_classes); coleenp@4719: this_klass->set_local_interfaces(_local_interfaces); coleenp@4719: this_klass->set_transitive_interfaces(_transitive_interfaces); coleenp@4719: coleenp@4719: // Clear out these fields so they don't get deallocated by the destructor coleenp@4719: clear_class_metadata(); coleenp@4719: } coleenp@4719: coleenp@4719: AnnotationArray* ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, coleenp@4142: int runtime_visible_annotations_length, coleenp@4142: u1* runtime_invisible_annotations, coleenp@4142: int runtime_invisible_annotations_length, TRAPS) { coleenp@4037: AnnotationArray* annotations = NULL; duke@435: if (runtime_visible_annotations != NULL || duke@435: runtime_invisible_annotations != NULL) { coleenp@4719: annotations = MetadataFactory::new_array(_loader_data, coleenp@4037: runtime_visible_annotations_length + coleenp@4037: runtime_invisible_annotations_length, coleenp@4037: CHECK_(annotations)); duke@435: if (runtime_visible_annotations != NULL) { coleenp@4037: for (int i = 0; i < runtime_visible_annotations_length; i++) { coleenp@4037: annotations->at_put(i, runtime_visible_annotations[i]); coleenp@4037: } duke@435: } duke@435: if (runtime_invisible_annotations != NULL) { coleenp@4037: for (int i = 0; i < runtime_invisible_annotations_length; i++) { coleenp@4037: int append = runtime_visible_annotations_length+i; coleenp@4037: annotations->at_put(append, runtime_invisible_annotations[i]); coleenp@4037: } duke@435: } duke@435: } duke@435: return annotations; duke@435: } duke@435: duke@435: roland@5106: #ifdef ASSERT kamg@4245: static void parseAndPrintGenericSignatures( kamg@4245: instanceKlassHandle this_klass, TRAPS) { kamg@4245: assert(ParseAllGenericSignatures == true, "Shouldn't call otherwise"); kamg@4245: ResourceMark rm; kamg@4245: kamg@4245: if (this_klass->generic_signature() != NULL) { kamg@4245: using namespace generic; kamg@4245: ClassDescriptor* spec = ClassDescriptor::parse_generic_signature(this_klass(), CHECK); kamg@4245: kamg@4245: tty->print_cr("Parsing %s", this_klass->generic_signature()->as_C_string()); kamg@4245: spec->print_on(tty); kamg@4245: kamg@4245: for (int i = 0; i < this_klass->methods()->length(); ++i) { kamg@4245: Method* m = this_klass->methods()->at(i); kamg@4245: MethodDescriptor* method_spec = MethodDescriptor::parse_generic_signature(m, spec); kamg@4245: Symbol* sig = m->generic_signature(); kamg@4245: if (sig == NULL) { kamg@4245: sig = m->signature(); kamg@4245: } kamg@4245: tty->print_cr("Parsing %s", sig->as_C_string()); kamg@4245: method_spec->print_on(tty); kamg@4245: } kamg@4245: } kamg@4245: } roland@5106: #endif // def ASSERT kamg@4245: kamg@4245: coleenp@4719: instanceKlassHandle ClassFileParser::parse_super_class(int super_class_index, coleenp@4719: TRAPS) { coleenp@4719: instanceKlassHandle super_klass; coleenp@4719: if (super_class_index == 0) { coleenp@4719: check_property(_class_name == vmSymbols::java_lang_Object(), coleenp@4719: "Invalid superclass index %u in class file %s", coleenp@4719: super_class_index, coleenp@4719: CHECK_NULL); coleenp@4719: } else { coleenp@4719: check_property(valid_klass_reference_at(super_class_index), coleenp@4719: "Invalid superclass index %u in class file %s", coleenp@4719: super_class_index, coleenp@4719: CHECK_NULL); coleenp@4719: // The class name should be legal because it is checked when parsing constant pool. coleenp@4719: // However, make sure it is not an array type. coleenp@4719: bool is_array = false; coleenp@4719: if (_cp->tag_at(super_class_index).is_klass()) { coleenp@4719: super_klass = instanceKlassHandle(THREAD, _cp->resolved_klass_at(super_class_index)); coleenp@4719: if (_need_verify) coleenp@4719: is_array = super_klass->oop_is_array(); coleenp@4719: } else if (_need_verify) { coleenp@4719: is_array = (_cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY); coleenp@4719: } coleenp@4719: if (_need_verify) { coleenp@4719: guarantee_property(!is_array, coleenp@4719: "Bad superclass name in class file %s", CHECK_NULL); coleenp@4719: } coleenp@4719: } coleenp@4719: return super_klass; coleenp@4719: } coleenp@4719: coleenp@4719: coleenp@4719: // Values needed for oopmap and InstanceKlass creation coleenp@4719: class FieldLayoutInfo : public StackObj { coleenp@4719: public: coleenp@4719: int* nonstatic_oop_offsets; coleenp@4719: unsigned int* nonstatic_oop_counts; coleenp@4719: unsigned int nonstatic_oop_map_count; coleenp@4719: unsigned int total_oop_map_count; coleenp@4719: int instance_size; coleenp@4719: int nonstatic_field_size; coleenp@4719: int static_field_size; coleenp@4719: bool has_nonstatic_fields; coleenp@4719: }; coleenp@4719: coleenp@4719: // Layout fields and fill in FieldLayoutInfo. Could use more refactoring! coleenp@4719: void ClassFileParser::layout_fields(Handle class_loader, coleenp@4719: FieldAllocationCount* fac, coleenp@4719: ClassAnnotationCollector* parsed_annotations, coleenp@4719: FieldLayoutInfo* info, coleenp@4719: TRAPS) { coleenp@4719: coleenp@4719: // Field size and offset computation coleenp@4719: int nonstatic_field_size = _super_klass() == NULL ? 0 : _super_klass()->nonstatic_field_size(); coleenp@4719: int next_static_oop_offset; coleenp@4719: int next_static_double_offset; coleenp@4719: int next_static_word_offset; coleenp@4719: int next_static_short_offset; coleenp@4719: int next_static_byte_offset; coleenp@4719: int next_nonstatic_oop_offset; coleenp@4719: int next_nonstatic_double_offset; coleenp@4719: int next_nonstatic_word_offset; coleenp@4719: int next_nonstatic_short_offset; coleenp@4719: int next_nonstatic_byte_offset; coleenp@4719: int first_nonstatic_oop_offset; coleenp@4719: int next_nonstatic_field_offset; coleenp@4719: int next_nonstatic_padded_offset; coleenp@4719: coleenp@4719: // Count the contended fields by type. shade@5146: // shade@5146: // We ignore static fields, because @Contended is not supported for them. shade@5146: // The layout code below will also ignore the static fields. coleenp@4719: int nonstatic_contended_count = 0; coleenp@4719: FieldAllocationCount fac_contended; coleenp@4719: for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) { coleenp@4719: FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); coleenp@4719: if (fs.is_contended()) { coleenp@4719: fac_contended.count[atype]++; coleenp@4719: if (!fs.access_flags().is_static()) { coleenp@4719: nonstatic_contended_count++; coleenp@4719: } coleenp@4719: } coleenp@4719: } coleenp@4719: coleenp@4719: coleenp@4719: // Calculate the starting byte offsets coleenp@4719: next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields(); coleenp@4719: next_static_double_offset = next_static_oop_offset + coleenp@4719: ((fac->count[STATIC_OOP]) * heapOopSize); coleenp@4719: if ( fac->count[STATIC_DOUBLE] && coleenp@4719: (Universe::field_type_should_be_aligned(T_DOUBLE) || coleenp@4719: Universe::field_type_should_be_aligned(T_LONG)) ) { coleenp@4719: next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong); coleenp@4719: } coleenp@4719: coleenp@4719: next_static_word_offset = next_static_double_offset + coleenp@4719: ((fac->count[STATIC_DOUBLE]) * BytesPerLong); coleenp@4719: next_static_short_offset = next_static_word_offset + coleenp@4719: ((fac->count[STATIC_WORD]) * BytesPerInt); coleenp@4719: next_static_byte_offset = next_static_short_offset + coleenp@4719: ((fac->count[STATIC_SHORT]) * BytesPerShort); coleenp@4719: shade@5146: int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() + shade@5146: nonstatic_field_size * heapOopSize; shade@5146: shade@5146: next_nonstatic_field_offset = nonstatic_fields_start; shade@5146: shade@5181: bool is_contended_class = parsed_annotations->is_contended(); shade@5181: shade@5146: // Class is contended, pad before all the fields shade@5181: if (is_contended_class) { shade@5146: next_nonstatic_field_offset += ContendedPaddingWidth; coleenp@4719: } coleenp@4719: shade@5181: // Compute the non-contended fields count. shade@5181: // The packing code below relies on these counts to determine if some field shade@5181: // can be squeezed into the alignment gap. Contended fields are obviously shade@5181: // exempt from that. coleenp@4719: unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE]; coleenp@4719: unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD]; coleenp@4719: unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT]; coleenp@4719: unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE]; coleenp@4719: unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP]; coleenp@4719: shade@5181: // Total non-static fields count, including every contended field shade@5181: unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] + shade@5181: fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] + shade@5181: fac->count[NONSTATIC_OOP]; shade@5181: coleenp@4719: bool super_has_nonstatic_fields = coleenp@4719: (_super_klass() != NULL && _super_klass->has_nonstatic_fields()); shade@5181: bool has_nonstatic_fields = super_has_nonstatic_fields || (nonstatic_fields_count != 0); coleenp@4719: coleenp@4719: coleenp@4719: // Prepare list of oops for oop map generation. shade@5181: // shade@5181: // "offset" and "count" lists are describing the set of contiguous oop shade@5181: // regions. offset[i] is the start of the i-th region, which then has shade@5181: // count[i] oops following. Before we know how many regions are required, shade@5181: // we pessimistically allocate the maps to fit all the oops into the shade@5181: // distinct regions. shade@5181: // shade@5181: // TODO: We add +1 to always allocate non-zero resource arrays; we need shade@5181: // to figure out if we still need to do this. coleenp@4719: int* nonstatic_oop_offsets; coleenp@4719: unsigned int* nonstatic_oop_counts; coleenp@4719: unsigned int nonstatic_oop_map_count = 0; shade@5181: unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1; coleenp@4719: coleenp@4719: nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD( shade@5181: THREAD, int, max_nonstatic_oop_maps); coleenp@4719: nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD( shade@5181: THREAD, unsigned int, max_nonstatic_oop_maps); coleenp@4719: coleenp@4719: first_nonstatic_oop_offset = 0; // will be set for first oop field coleenp@4719: coleenp@4719: bool compact_fields = CompactFields; coleenp@4719: int allocation_style = FieldsAllocationStyle; coleenp@4719: if( allocation_style < 0 || allocation_style > 2 ) { // Out of range? coleenp@4719: assert(false, "0 <= FieldsAllocationStyle <= 2"); coleenp@4719: allocation_style = 1; // Optimistic coleenp@4719: } coleenp@4719: coleenp@4719: // The next classes have predefined hard-coded fields offsets coleenp@4719: // (see in JavaClasses::compute_hard_coded_offsets()). coleenp@4719: // Use default fields allocation order for them. coleenp@4719: if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() && coleenp@4719: (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() || coleenp@4719: _class_name == vmSymbols::java_lang_Class() || coleenp@4719: _class_name == vmSymbols::java_lang_ClassLoader() || coleenp@4719: _class_name == vmSymbols::java_lang_ref_Reference() || coleenp@4719: _class_name == vmSymbols::java_lang_ref_SoftReference() || coleenp@4719: _class_name == vmSymbols::java_lang_StackTraceElement() || coleenp@4719: _class_name == vmSymbols::java_lang_String() || coleenp@4719: _class_name == vmSymbols::java_lang_Throwable() || coleenp@4719: _class_name == vmSymbols::java_lang_Boolean() || coleenp@4719: _class_name == vmSymbols::java_lang_Character() || coleenp@4719: _class_name == vmSymbols::java_lang_Float() || coleenp@4719: _class_name == vmSymbols::java_lang_Double() || coleenp@4719: _class_name == vmSymbols::java_lang_Byte() || coleenp@4719: _class_name == vmSymbols::java_lang_Short() || coleenp@4719: _class_name == vmSymbols::java_lang_Integer() || coleenp@4719: _class_name == vmSymbols::java_lang_Long())) { coleenp@4719: allocation_style = 0; // Allocate oops first coleenp@4719: compact_fields = false; // Don't compact fields coleenp@4719: } coleenp@4719: shade@5146: // Rearrange fields for a given allocation style coleenp@4719: if( allocation_style == 0 ) { coleenp@4719: // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields coleenp@4719: next_nonstatic_oop_offset = next_nonstatic_field_offset; coleenp@4719: next_nonstatic_double_offset = next_nonstatic_oop_offset + coleenp@4719: (nonstatic_oop_count * heapOopSize); coleenp@4719: } else if( allocation_style == 1 ) { coleenp@4719: // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields coleenp@4719: next_nonstatic_double_offset = next_nonstatic_field_offset; coleenp@4719: } else if( allocation_style == 2 ) { coleenp@4719: // Fields allocation: oops fields in super and sub classes are together. coleenp@4719: if( nonstatic_field_size > 0 && _super_klass() != NULL && coleenp@4719: _super_klass->nonstatic_oop_map_size() > 0 ) { coleenp@4719: unsigned int map_count = _super_klass->nonstatic_oop_map_count(); coleenp@4719: OopMapBlock* first_map = _super_klass->start_of_nonstatic_oop_maps(); coleenp@4719: OopMapBlock* last_map = first_map + map_count - 1; coleenp@4719: int next_offset = last_map->offset() + (last_map->count() * heapOopSize); coleenp@4719: if (next_offset == next_nonstatic_field_offset) { coleenp@4719: allocation_style = 0; // allocate oops first coleenp@4719: next_nonstatic_oop_offset = next_nonstatic_field_offset; coleenp@4719: next_nonstatic_double_offset = next_nonstatic_oop_offset + coleenp@4719: (nonstatic_oop_count * heapOopSize); coleenp@4719: } coleenp@4719: } coleenp@4719: if( allocation_style == 2 ) { coleenp@4719: allocation_style = 1; // allocate oops last coleenp@4719: next_nonstatic_double_offset = next_nonstatic_field_offset; coleenp@4719: } coleenp@4719: } else { coleenp@4719: ShouldNotReachHere(); coleenp@4719: } coleenp@4719: coleenp@4719: int nonstatic_oop_space_count = 0; coleenp@4719: int nonstatic_word_space_count = 0; coleenp@4719: int nonstatic_short_space_count = 0; coleenp@4719: int nonstatic_byte_space_count = 0; coleenp@4719: int nonstatic_oop_space_offset; coleenp@4719: int nonstatic_word_space_offset; coleenp@4719: int nonstatic_short_space_offset; coleenp@4719: int nonstatic_byte_space_offset; coleenp@4719: shade@5146: // Try to squeeze some of the fields into the gaps due to shade@5146: // long/double alignment. coleenp@4719: if( nonstatic_double_count > 0 ) { coleenp@4719: int offset = next_nonstatic_double_offset; coleenp@4719: next_nonstatic_double_offset = align_size_up(offset, BytesPerLong); coleenp@4719: if( compact_fields && offset != next_nonstatic_double_offset ) { coleenp@4719: // Allocate available fields into the gap before double field. coleenp@4719: int length = next_nonstatic_double_offset - offset; coleenp@4719: assert(length == BytesPerInt, ""); coleenp@4719: nonstatic_word_space_offset = offset; coleenp@4719: if( nonstatic_word_count > 0 ) { coleenp@4719: nonstatic_word_count -= 1; coleenp@4719: nonstatic_word_space_count = 1; // Only one will fit coleenp@4719: length -= BytesPerInt; coleenp@4719: offset += BytesPerInt; coleenp@4719: } coleenp@4719: nonstatic_short_space_offset = offset; coleenp@4719: while( length >= BytesPerShort && nonstatic_short_count > 0 ) { coleenp@4719: nonstatic_short_count -= 1; coleenp@4719: nonstatic_short_space_count += 1; coleenp@4719: length -= BytesPerShort; coleenp@4719: offset += BytesPerShort; coleenp@4719: } coleenp@4719: nonstatic_byte_space_offset = offset; coleenp@4719: while( length > 0 && nonstatic_byte_count > 0 ) { coleenp@4719: nonstatic_byte_count -= 1; coleenp@4719: nonstatic_byte_space_count += 1; coleenp@4719: length -= 1; coleenp@4719: } coleenp@4719: // Allocate oop field in the gap if there are no other fields for that. coleenp@4719: nonstatic_oop_space_offset = offset; coleenp@4719: if( length >= heapOopSize && nonstatic_oop_count > 0 && coleenp@4719: allocation_style != 0 ) { // when oop fields not first coleenp@4719: nonstatic_oop_count -= 1; coleenp@4719: nonstatic_oop_space_count = 1; // Only one will fit coleenp@4719: length -= heapOopSize; coleenp@4719: offset += heapOopSize; coleenp@4719: } coleenp@4719: } coleenp@4719: } coleenp@4719: coleenp@4719: next_nonstatic_word_offset = next_nonstatic_double_offset + coleenp@4719: (nonstatic_double_count * BytesPerLong); coleenp@4719: next_nonstatic_short_offset = next_nonstatic_word_offset + coleenp@4719: (nonstatic_word_count * BytesPerInt); coleenp@4719: next_nonstatic_byte_offset = next_nonstatic_short_offset + coleenp@4719: (nonstatic_short_count * BytesPerShort); coleenp@4719: next_nonstatic_padded_offset = next_nonstatic_byte_offset + coleenp@4719: nonstatic_byte_count; coleenp@4719: coleenp@4719: // let oops jump before padding with this allocation style coleenp@4719: if( allocation_style == 1 ) { coleenp@4719: next_nonstatic_oop_offset = next_nonstatic_padded_offset; coleenp@4719: if( nonstatic_oop_count > 0 ) { coleenp@4719: next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize); coleenp@4719: } coleenp@4719: next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize); coleenp@4719: } coleenp@4719: coleenp@4719: // Iterate over fields again and compute correct offsets. coleenp@4719: // The field allocation type was temporarily stored in the offset slot. coleenp@4719: // oop fields are located before non-oop fields (static and non-static). coleenp@4719: for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) { coleenp@4719: coleenp@4719: // skip already laid out fields coleenp@4719: if (fs.is_offset_set()) continue; coleenp@4719: coleenp@4719: // contended instance fields are handled below coleenp@4719: if (fs.is_contended() && !fs.access_flags().is_static()) continue; coleenp@4719: coleenp@4719: int real_offset; coleenp@4719: FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); coleenp@4719: coleenp@4719: // pack the rest of the fields coleenp@4719: switch (atype) { coleenp@4719: case STATIC_OOP: coleenp@4719: real_offset = next_static_oop_offset; coleenp@4719: next_static_oop_offset += heapOopSize; coleenp@4719: break; coleenp@4719: case STATIC_BYTE: coleenp@4719: real_offset = next_static_byte_offset; coleenp@4719: next_static_byte_offset += 1; coleenp@4719: break; coleenp@4719: case STATIC_SHORT: coleenp@4719: real_offset = next_static_short_offset; coleenp@4719: next_static_short_offset += BytesPerShort; coleenp@4719: break; coleenp@4719: case STATIC_WORD: coleenp@4719: real_offset = next_static_word_offset; coleenp@4719: next_static_word_offset += BytesPerInt; coleenp@4719: break; coleenp@4719: case STATIC_DOUBLE: coleenp@4719: real_offset = next_static_double_offset; coleenp@4719: next_static_double_offset += BytesPerLong; coleenp@4719: break; coleenp@4719: case NONSTATIC_OOP: coleenp@4719: if( nonstatic_oop_space_count > 0 ) { coleenp@4719: real_offset = nonstatic_oop_space_offset; coleenp@4719: nonstatic_oop_space_offset += heapOopSize; coleenp@4719: nonstatic_oop_space_count -= 1; coleenp@4719: } else { coleenp@4719: real_offset = next_nonstatic_oop_offset; coleenp@4719: next_nonstatic_oop_offset += heapOopSize; coleenp@4719: } coleenp@4719: // Update oop maps coleenp@4719: if( nonstatic_oop_map_count > 0 && coleenp@4719: nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == coleenp@4719: real_offset - coleenp@4719: int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * coleenp@4719: heapOopSize ) { coleenp@4719: // Extend current oop map shade@5181: assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check"); coleenp@4719: nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; coleenp@4719: } else { coleenp@4719: // Create new oop map shade@5181: assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); coleenp@4719: nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; coleenp@4719: nonstatic_oop_counts [nonstatic_oop_map_count] = 1; coleenp@4719: nonstatic_oop_map_count += 1; coleenp@4719: if( first_nonstatic_oop_offset == 0 ) { // Undefined coleenp@4719: first_nonstatic_oop_offset = real_offset; coleenp@4719: } coleenp@4719: } coleenp@4719: break; coleenp@4719: case NONSTATIC_BYTE: coleenp@4719: if( nonstatic_byte_space_count > 0 ) { coleenp@4719: real_offset = nonstatic_byte_space_offset; coleenp@4719: nonstatic_byte_space_offset += 1; coleenp@4719: nonstatic_byte_space_count -= 1; coleenp@4719: } else { coleenp@4719: real_offset = next_nonstatic_byte_offset; coleenp@4719: next_nonstatic_byte_offset += 1; coleenp@4719: } coleenp@4719: break; coleenp@4719: case NONSTATIC_SHORT: coleenp@4719: if( nonstatic_short_space_count > 0 ) { coleenp@4719: real_offset = nonstatic_short_space_offset; coleenp@4719: nonstatic_short_space_offset += BytesPerShort; coleenp@4719: nonstatic_short_space_count -= 1; coleenp@4719: } else { coleenp@4719: real_offset = next_nonstatic_short_offset; coleenp@4719: next_nonstatic_short_offset += BytesPerShort; coleenp@4719: } coleenp@4719: break; coleenp@4719: case NONSTATIC_WORD: coleenp@4719: if( nonstatic_word_space_count > 0 ) { coleenp@4719: real_offset = nonstatic_word_space_offset; coleenp@4719: nonstatic_word_space_offset += BytesPerInt; coleenp@4719: nonstatic_word_space_count -= 1; coleenp@4719: } else { coleenp@4719: real_offset = next_nonstatic_word_offset; coleenp@4719: next_nonstatic_word_offset += BytesPerInt; coleenp@4719: } coleenp@4719: break; coleenp@4719: case NONSTATIC_DOUBLE: coleenp@4719: real_offset = next_nonstatic_double_offset; coleenp@4719: next_nonstatic_double_offset += BytesPerLong; coleenp@4719: break; coleenp@4719: default: coleenp@4719: ShouldNotReachHere(); coleenp@4719: } coleenp@4719: fs.set_offset(real_offset); coleenp@4719: } coleenp@4719: coleenp@4719: coleenp@4719: // Handle the contended cases. coleenp@4719: // coleenp@4719: // Each contended field should not intersect the cache line with another contended field. coleenp@4719: // In the absence of alignment information, we end up with pessimistically separating coleenp@4719: // the fields with full-width padding. coleenp@4719: // coleenp@4719: // Additionally, this should not break alignment for the fields, so we round the alignment up coleenp@4719: // for each field. shade@5181: if (nonstatic_contended_count > 0) { coleenp@4719: coleenp@4719: // if there is at least one contended field, we need to have pre-padding for them shade@5181: next_nonstatic_padded_offset += ContendedPaddingWidth; coleenp@4719: coleenp@4719: // collect all contended groups coleenp@4719: BitMap bm(_cp->size()); coleenp@4719: for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) { coleenp@4719: // skip already laid out fields coleenp@4719: if (fs.is_offset_set()) continue; coleenp@4719: coleenp@4719: if (fs.is_contended()) { coleenp@4719: bm.set_bit(fs.contended_group()); coleenp@4719: } coleenp@4719: } coleenp@4719: coleenp@4719: int current_group = -1; coleenp@4719: while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) { coleenp@4719: coleenp@4719: for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) { coleenp@4719: coleenp@4719: // skip already laid out fields coleenp@4719: if (fs.is_offset_set()) continue; coleenp@4719: coleenp@4719: // skip non-contended fields and fields from different group coleenp@4719: if (!fs.is_contended() || (fs.contended_group() != current_group)) continue; coleenp@4719: coleenp@4719: // handle statics below coleenp@4719: if (fs.access_flags().is_static()) continue; coleenp@4719: coleenp@4719: int real_offset; coleenp@4719: FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); coleenp@4719: coleenp@4719: switch (atype) { coleenp@4719: case NONSTATIC_BYTE: coleenp@4719: next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, 1); coleenp@4719: real_offset = next_nonstatic_padded_offset; coleenp@4719: next_nonstatic_padded_offset += 1; coleenp@4719: break; coleenp@4719: coleenp@4719: case NONSTATIC_SHORT: coleenp@4719: next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerShort); coleenp@4719: real_offset = next_nonstatic_padded_offset; coleenp@4719: next_nonstatic_padded_offset += BytesPerShort; coleenp@4719: break; coleenp@4719: coleenp@4719: case NONSTATIC_WORD: coleenp@4719: next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerInt); coleenp@4719: real_offset = next_nonstatic_padded_offset; coleenp@4719: next_nonstatic_padded_offset += BytesPerInt; coleenp@4719: break; coleenp@4719: coleenp@4719: case NONSTATIC_DOUBLE: coleenp@4719: next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerLong); coleenp@4719: real_offset = next_nonstatic_padded_offset; coleenp@4719: next_nonstatic_padded_offset += BytesPerLong; coleenp@4719: break; coleenp@4719: coleenp@4719: case NONSTATIC_OOP: coleenp@4719: next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, heapOopSize); coleenp@4719: real_offset = next_nonstatic_padded_offset; coleenp@4719: next_nonstatic_padded_offset += heapOopSize; coleenp@4719: coleenp@4719: // Create new oop map shade@5181: assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); coleenp@4719: nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; coleenp@4719: nonstatic_oop_counts [nonstatic_oop_map_count] = 1; coleenp@4719: nonstatic_oop_map_count += 1; coleenp@4719: if( first_nonstatic_oop_offset == 0 ) { // Undefined coleenp@4719: first_nonstatic_oop_offset = real_offset; coleenp@4719: } coleenp@4719: break; coleenp@4719: coleenp@4719: default: coleenp@4719: ShouldNotReachHere(); coleenp@4719: } coleenp@4719: coleenp@4719: if (fs.contended_group() == 0) { coleenp@4719: // Contended group defines the equivalence class over the fields: coleenp@4719: // the fields within the same contended group are not inter-padded. coleenp@4719: // The only exception is default group, which does not incur the coleenp@4719: // equivalence, and so requires intra-padding. shade@5146: next_nonstatic_padded_offset += ContendedPaddingWidth; coleenp@4719: } coleenp@4719: coleenp@4719: fs.set_offset(real_offset); coleenp@4719: } // for coleenp@4719: coleenp@4719: // Start laying out the next group. coleenp@4719: // Note that this will effectively pad the last group in the back; coleenp@4719: // this is expected to alleviate memory contention effects for coleenp@4719: // subclass fields and/or adjacent object. coleenp@4719: // If this was the default group, the padding is already in place. coleenp@4719: if (current_group != 0) { shade@5146: next_nonstatic_padded_offset += ContendedPaddingWidth; coleenp@4719: } coleenp@4719: } coleenp@4719: coleenp@4719: // handle static fields coleenp@4719: } coleenp@4719: coleenp@4719: // Entire class is contended, pad in the back. coleenp@4719: // This helps to alleviate memory contention effects for subclass fields coleenp@4719: // and/or adjacent object. shade@5181: if (is_contended_class) { shade@5181: next_nonstatic_padded_offset += ContendedPaddingWidth; coleenp@4719: } coleenp@4719: shade@5181: int notaligned_nonstatic_fields_end = next_nonstatic_padded_offset; shade@5181: shade@5181: int nonstatic_fields_end = align_size_up(notaligned_nonstatic_fields_end, heapOopSize); shade@5181: int instance_end = align_size_up(notaligned_nonstatic_fields_end, wordSize); shade@5146: int static_fields_end = align_size_up(next_static_byte_offset, wordSize); shade@5146: shade@5146: int static_field_size = (static_fields_end - shade@5146: InstanceMirrorKlass::offset_of_static_fields()) / wordSize; shade@5146: nonstatic_field_size = nonstatic_field_size + shade@5146: (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize; shade@5146: shade@5146: int instance_size = align_object_size(instance_end / wordSize); coleenp@4719: coleenp@4719: assert(instance_size == align_object_size(align_size_up( shade@5135: (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), coleenp@4719: wordSize) / wordSize), "consistent layout helper value"); coleenp@4719: shade@5181: // Invariant: nonstatic_field end/start should only change if there are shade@5181: // nonstatic fields in the class, or if the class is contended. We compare shade@5181: // against the non-aligned value, so that end alignment will not fail the shade@5181: // assert without actually having the fields. shade@5181: assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) || shade@5181: is_contended_class || shade@5181: (nonstatic_fields_count > 0), "double-check nonstatic start/end"); shade@5181: coleenp@4719: // Number of non-static oop map blocks allocated at end of klass. coleenp@4719: const unsigned int total_oop_map_count = coleenp@4719: compute_oop_map_count(_super_klass, nonstatic_oop_map_count, coleenp@4719: first_nonstatic_oop_offset); coleenp@4719: coleenp@4719: #ifndef PRODUCT coleenp@4719: if (PrintFieldLayout) { coleenp@4719: print_field_layout(_class_name, coleenp@4719: _fields, coleenp@4719: _cp, coleenp@4719: instance_size, shade@5146: nonstatic_fields_start, shade@5146: nonstatic_fields_end, shade@5146: static_fields_end); coleenp@4719: } coleenp@4719: coleenp@4719: #endif coleenp@4719: // Pass back information needed for InstanceKlass creation coleenp@4719: info->nonstatic_oop_offsets = nonstatic_oop_offsets; coleenp@4719: info->nonstatic_oop_counts = nonstatic_oop_counts; coleenp@4719: info->nonstatic_oop_map_count = nonstatic_oop_map_count; coleenp@4719: info->total_oop_map_count = total_oop_map_count; coleenp@4719: info->instance_size = instance_size; coleenp@4719: info->static_field_size = static_field_size; coleenp@4719: info->nonstatic_field_size = nonstatic_field_size; coleenp@4719: info->has_nonstatic_fields = has_nonstatic_fields; coleenp@4719: } coleenp@4719: coleenp@4719: coleenp@2497: instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name, coleenp@4304: ClassLoaderData* loader_data, duke@435: Handle protection_domain, jrose@1145: KlassHandle host_klass, jrose@866: GrowableArray* cp_patches, coleenp@2497: TempNewSymbol& parsed_name, acorn@1408: bool verify, duke@435: TRAPS) { coleenp@4398: dcubed@3360: // When a retransformable agent is attached, JVMTI caches the dcubed@3360: // class bytes that existed before the first retransformation. dcubed@3360: // If RedefineClasses() was used before the retransformable dcubed@3360: // agent attached, then the cached class bytes may not be the dcubed@3360: // original class bytes. jiangli@5421: JvmtiCachedClassFileData *cached_class_file = NULL; coleenp@4304: Handle class_loader(THREAD, loader_data->class_loader()); kamg@4245: bool has_default_methods = false; kamg@4245: ResourceMark rm(THREAD); duke@435: duke@435: ClassFileStream* cfs = stream(); duke@435: // Timing mchung@1310: assert(THREAD->is_Java_thread(), "must be a JavaThread"); mchung@1310: JavaThread* jt = (JavaThread*) THREAD; mchung@1310: mchung@1310: PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(), mchung@1310: ClassLoader::perf_class_parse_selftime(), mchung@1310: NULL, mchung@1310: jt->get_thread_stat()->perf_recursion_counts_addr(), mchung@1310: jt->get_thread_stat()->perf_timers_addr(), mchung@1310: PerfClassTraceTime::PARSE_CLASS); duke@435: coleenp@4719: init_parsed_class_attributes(loader_data); duke@435: duke@435: if (JvmtiExport::should_post_class_file_load_hook()) { dcubed@3380: // Get the cached class file bytes (if any) from the class that dcubed@3380: // is being redefined or retransformed. We use jvmti_thread_state() dcubed@3380: // instead of JvmtiThreadState::state_for(jt) so we don't allocate dcubed@3380: // a JvmtiThreadState any earlier than necessary. This will help dcubed@3380: // avoid the bug described by 7126851. dcubed@3380: JvmtiThreadState *state = jt->jvmti_thread_state(); dcubed@3380: if (state != NULL) { dcubed@3380: KlassHandle *h_class_being_redefined = dcubed@3380: state->get_class_being_redefined(); dcubed@3380: if (h_class_being_redefined != NULL) { dcubed@3380: instanceKlassHandle ikh_class_being_redefined = dcubed@3380: instanceKlassHandle(THREAD, (*h_class_being_redefined)()); jiangli@5421: cached_class_file = ikh_class_being_redefined->get_cached_class_file(); dcubed@3380: } dcubed@3360: } dcubed@3360: duke@435: unsigned char* ptr = cfs->buffer(); duke@435: unsigned char* end_ptr = cfs->buffer() + cfs->length(); duke@435: coleenp@4304: JvmtiExport::post_class_file_load_hook(name, class_loader(), protection_domain, jiangli@5421: &ptr, &end_ptr, &cached_class_file); duke@435: duke@435: if (ptr != cfs->buffer()) { duke@435: // JVMTI agent has modified class file data. duke@435: // Set new class file stream using JVMTI agent modified duke@435: // class file data. duke@435: cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source()); duke@435: set_stream(cfs); duke@435: } duke@435: } duke@435: jrose@1145: _host_klass = host_klass; jrose@866: _cp_patches = cp_patches; duke@435: duke@435: instanceKlassHandle nullHandle; duke@435: duke@435: // Figure out whether we can skip format checking (matching classic VM behavior) acorn@1408: _need_verify = Verifier::should_verify_for(class_loader(), verify); duke@435: duke@435: // Set the verify flag in stream duke@435: cfs->set_verify(_need_verify); duke@435: duke@435: // Save the class file name for easier error message printing. coleenp@2497: _class_name = (name != NULL) ? name : vmSymbols::unknown_class_name(); duke@435: duke@435: cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor duke@435: // Magic value duke@435: u4 magic = cfs->get_u4_fast(); duke@435: guarantee_property(magic == JAVA_CLASSFILE_MAGIC, duke@435: "Incompatible magic value %u in class file %s", duke@435: magic, CHECK_(nullHandle)); duke@435: duke@435: // Version numbers duke@435: u2 minor_version = cfs->get_u2_fast(); duke@435: u2 major_version = cfs->get_u2_fast(); duke@435: duke@435: // Check version numbers - we check this even with verifier off duke@435: if (!is_supported_version(major_version, minor_version)) { coleenp@2497: if (name == NULL) { duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_UnsupportedClassVersionError(), duke@435: "Unsupported major.minor version %u.%u", duke@435: major_version, duke@435: minor_version); duke@435: } else { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_UnsupportedClassVersionError(), duke@435: "%s : Unsupported major.minor version %u.%u", duke@435: name->as_C_string(), duke@435: major_version, duke@435: minor_version); duke@435: } duke@435: return nullHandle; duke@435: } duke@435: duke@435: _major_version = major_version; duke@435: _minor_version = minor_version; duke@435: duke@435: duke@435: // Check if verification needs to be relaxed for this class file duke@435: // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) duke@435: _relax_verify = Verifier::relax_verify_for(class_loader()); duke@435: duke@435: // Constant pool coleenp@4719: constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle)); coleenp@2497: duke@435: int cp_size = cp->length(); duke@435: duke@435: cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len duke@435: duke@435: // Access flags duke@435: AccessFlags access_flags; duke@435: jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS; duke@435: duke@435: if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { duke@435: // Set abstract bit for old class files for backward compatibility duke@435: flags |= JVM_ACC_ABSTRACT; duke@435: } duke@435: verify_legal_class_modifiers(flags, CHECK_(nullHandle)); duke@435: access_flags.set_flags(flags); duke@435: duke@435: // This class and superclass duke@435: u2 this_class_index = cfs->get_u2_fast(); duke@435: check_property( duke@435: valid_cp_range(this_class_index, cp_size) && duke@435: cp->tag_at(this_class_index).is_unresolved_klass(), duke@435: "Invalid this class index %u in constant pool in class file %s", duke@435: this_class_index, CHECK_(nullHandle)); duke@435: coleenp@2497: Symbol* class_name = cp->unresolved_klass_at(this_class_index); coleenp@2497: assert(class_name != NULL, "class_name can't be null"); duke@435: duke@435: // It's important to set parsed_name *before* resolving the super class. duke@435: // (it's used for cleanup by the caller if parsing fails) duke@435: parsed_name = class_name; coleenp@2497: // parsed_name is returned and can be used if there's an error, so add to coleenp@2497: // its reference count. Caller will decrement the refcount. coleenp@2497: parsed_name->increment_refcount(); duke@435: duke@435: // Update _class_name which could be null previously to be class_name duke@435: _class_name = class_name; duke@435: duke@435: // Don't need to check whether this class name is legal or not. duke@435: // It has been checked when constant pool is parsed. duke@435: // However, make sure it is not an array type. duke@435: if (_need_verify) { duke@435: guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY, duke@435: "Bad class name in class file %s", duke@435: CHECK_(nullHandle)); duke@435: } duke@435: coleenp@4037: Klass* preserve_this_klass; // for storing result across HandleMark duke@435: duke@435: // release all handles when parsing is done duke@435: { HandleMark hm(THREAD); duke@435: duke@435: // Checks if name in class file matches requested name coleenp@2497: if (name != NULL && class_name != name) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_NoClassDefFoundError(), duke@435: "%s (wrong name: %s)", duke@435: name->as_C_string(), duke@435: class_name->as_C_string() duke@435: ); duke@435: return nullHandle; duke@435: } duke@435: duke@435: if (TraceClassLoadingPreorder) { bharadwaj@4960: tty->print("[Loading %s", (name != NULL) ? name->as_klass_external_name() : "NoName"); duke@435: if (cfs->source() != NULL) tty->print(" from %s", cfs->source()); duke@435: tty->print_cr("]"); duke@435: } duke@435: duke@435: u2 super_class_index = cfs->get_u2_fast(); coleenp@4719: instanceKlassHandle super_klass = parse_super_class(super_class_index, coleenp@4719: CHECK_NULL); duke@435: duke@435: // Interfaces duke@435: u2 itfs_len = cfs->get_u2_fast(); coleenp@4719: Array* local_interfaces = coleenp@4719: parse_interfaces(itfs_len, protection_domain, _class_name, coleenp@4719: &has_default_methods, CHECK_(nullHandle)); duke@435: jiangli@3373: u2 java_fields_count = 0; duke@435: // Fields (offsets are filled in later) never@3137: FieldAllocationCount fac; coleenp@4719: Array* fields = parse_fields(class_name, coleenp@4719: access_flags.is_interface(), coleenp@4719: &fac, &java_fields_count, coleenp@4719: CHECK_(nullHandle)); duke@435: // Methods duke@435: bool has_final_method = false; duke@435: AccessFlags promoted_flags; duke@435: promoted_flags.set_flags(0); coleenp@4719: Array* methods = parse_methods(access_flags.is_interface(), coleenp@4142: &promoted_flags, coleenp@4142: &has_final_method, kamg@4245: &has_default_methods, coleenp@4142: CHECK_(nullHandle)); duke@435: jrose@3926: // Additional attributes jrose@3926: ClassAnnotationCollector parsed_annotations; coleenp@4719: parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle)); jrose@3926: jrose@3926: // Make sure this is the end of class file stream jrose@3926: guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle)); jrose@3926: duke@435: // We check super class after class file is parsed and format is checked jrose@866: if (super_class_index > 0 && super_klass.is_null()) { coleenp@2497: Symbol* sk = cp->klass_name_at(super_class_index); duke@435: if (access_flags.is_interface()) { duke@435: // Before attempting to resolve the superclass, check for class format duke@435: // errors not checked yet. coleenp@2497: guarantee_property(sk == vmSymbols::java_lang_Object(), duke@435: "Interfaces must have java.lang.Object as superclass in class file %s", duke@435: CHECK_(nullHandle)); duke@435: } coleenp@4142: Klass* k = SystemDictionary::resolve_super_or_fail(class_name, sk, coleenp@4142: class_loader, coleenp@4142: protection_domain, coleenp@4142: true, coleenp@4142: CHECK_(nullHandle)); mchung@1310: duke@435: KlassHandle kh (THREAD, k); duke@435: super_klass = instanceKlassHandle(THREAD, kh()); jrose@866: } jrose@866: if (super_klass.not_null()) { kamg@4245: kamg@4245: if (super_klass->has_default_methods()) { kamg@4245: has_default_methods = true; kamg@4245: } kamg@4245: duke@435: if (super_klass->is_interface()) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_IncompatibleClassChangeError(), duke@435: "class %s has interface %s as super class", duke@435: class_name->as_klass_external_name(), duke@435: super_klass->external_name() duke@435: ); duke@435: return nullHandle; duke@435: } duke@435: // Make sure super class is not final duke@435: if (super_klass->is_final()) { duke@435: THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle); duke@435: } duke@435: } duke@435: coleenp@4719: // save super klass for error handling. coleenp@4719: _super_klass = super_klass; coleenp@4719: duke@435: // Compute the transitive list of all unique interfaces implemented by this class coleenp@4719: _transitive_interfaces = coleenp@4719: compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle)); duke@435: duke@435: // sort methods coleenp@4719: intArray* method_ordering = sort_methods(methods); duke@435: duke@435: // promote flags from parse_methods() to the klass' flags duke@435: access_flags.add_promoted_flags(promoted_flags.as_int()); duke@435: duke@435: // Size of Java vtable (in words) duke@435: int vtable_size = 0; duke@435: int itable_size = 0; duke@435: int num_miranda_methods = 0; duke@435: kamg@4245: GrowableArray all_mirandas(20); kamg@4245: kamg@4245: klassVtable::compute_vtable_size_and_num_mirandas( kamg@4245: &vtable_size, &num_miranda_methods, &all_mirandas, super_klass(), methods, kamg@4245: access_flags, class_loader, class_name, local_interfaces, acorn@1087: CHECK_(nullHandle)); duke@435: duke@435: // Size of Java itable (in words) coleenp@4719: itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(_transitive_interfaces); coleenp@4719: coleenp@4719: FieldLayoutInfo info; coleenp@4719: layout_fields(class_loader, &fac, &parsed_annotations, &info, CHECK_NULL); coleenp@4719: coleenp@4719: int total_oop_map_size2 = coleenp@4719: InstanceKlass::nonstatic_oop_map_size(info.total_oop_map_count); coleenp@4719: duke@435: // Compute reference type duke@435: ReferenceType rt; duke@435: if (super_klass() == NULL) { duke@435: rt = REF_NONE; duke@435: } else { duke@435: rt = super_klass->reference_type(); duke@435: } duke@435: coleenp@4037: // We can now create the basic Klass* for this klass coleenp@4719: _klass = InstanceKlass::allocate_instance_klass(loader_data, coleenp@4719: vtable_size, coleenp@4719: itable_size, coleenp@4719: info.static_field_size, coleenp@4719: total_oop_map_size2, coleenp@4719: rt, coleenp@4719: access_flags, coleenp@4719: name, coleenp@4719: super_klass(), coleenp@4719: !host_klass.is_null(), coleenp@4719: CHECK_(nullHandle)); coleenp@4719: instanceKlassHandle this_klass (THREAD, _klass); coleenp@4719: coleenp@4719: assert(this_klass->static_field_size() == info.static_field_size, "sanity"); coleenp@4719: assert(this_klass->nonstatic_oop_map_count() == info.total_oop_map_count, jcoomes@1373: "sanity"); duke@435: duke@435: // Fill in information already parsed acorn@1410: this_klass->set_should_verify_class(verify); coleenp@4719: jint lh = Klass::instance_layout_helper(info.instance_size, false); duke@435: this_klass->set_layout_helper(lh); duke@435: assert(this_klass->oop_is_instance(), "layout is correct"); coleenp@4719: assert(this_klass->size_helper() == info.instance_size, "correct size_helper"); duke@435: // Not yet: supers are done below to support the new subtype-checking fields duke@435: //this_klass->set_super(super_klass()); coleenp@4037: this_klass->set_class_loader_data(loader_data); coleenp@4719: this_klass->set_nonstatic_field_size(info.nonstatic_field_size); coleenp@4719: this_klass->set_has_nonstatic_fields(info.has_nonstatic_fields); never@3137: this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]); coleenp@4719: coleenp@4719: apply_parsed_class_metadata(this_klass, java_fields_count, CHECK_NULL); coleenp@4719: duke@435: if (has_final_method) { duke@435: this_klass->set_has_final_method(); duke@435: } coleenp@4719: this_klass->copy_method_ordering(method_ordering, CHECK_NULL); coleenp@4037: // The InstanceKlass::_methods_jmethod_ids cache and the coleenp@4037: // InstanceKlass::_methods_cached_itable_indices cache are dcubed@1412: // both managed on the assumption that the initial cache dcubed@1412: // size is equal to the number of methods in the class. If coleenp@4037: // that changes, then InstanceKlass::idnum_can_increment() dcubed@1412: // has to be changed accordingly. duke@435: this_klass->set_initial_method_idnum(methods->length()); duke@435: this_klass->set_name(cp->klass_name_at(this_class_index)); twisti@4163: if (is_anonymous()) // I am well known to myself jrose@909: cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve coleenp@4037: duke@435: this_klass->set_minor_version(minor_version); duke@435: this_klass->set_major_version(major_version); kamg@4245: this_klass->set_has_default_methods(has_default_methods); duke@435: coleenp@4037: // Set up Method*::intrinsic_id as soon as we know the names of methods. jrose@1291: // (We used to do this lazily, but now we query it in Rewriter, jrose@1291: // which is eagerly done for every method, so we might as well do it now, jrose@1291: // when everything is fresh in memory.) coleenp@4037: if (Method::klass_id_for_intrinsics(this_klass()) != vmSymbols::NO_SID) { jrose@1291: for (int j = 0; j < methods->length(); j++) { coleenp@4037: methods->at(j)->init_intrinsic_id(); jrose@1291: } jrose@1291: } jrose@1291: jiangli@5421: if (cached_class_file != NULL) { coleenp@4037: // JVMTI: we have an InstanceKlass now, tell it about the cached bytes jiangli@5421: this_klass->set_cached_class_file(cached_class_file); duke@435: } duke@435: kamg@4245: // Fill in field values obtained by parse_classfile_attributes kamg@4245: if (parsed_annotations.has_any_annotations()) kamg@4245: parsed_annotations.apply_to(this_klass); kamg@4245: apply_parsed_class_attributes(this_klass); kamg@4245: duke@435: // Miranda methods duke@435: if ((num_miranda_methods > 0) || duke@435: // if this class introduced new miranda methods or duke@435: (super_klass.not_null() && (super_klass->has_miranda_methods())) duke@435: // super class exists and this class inherited miranda methods duke@435: ) { duke@435: this_klass->set_has_miranda_methods(); // then set a flag duke@435: } duke@435: duke@435: // Fill in information needed to compute superclasses. duke@435: this_klass->initialize_supers(super_klass(), CHECK_(nullHandle)); duke@435: duke@435: // Initialize itable offset tables duke@435: klassItable::setup_itable_offset_table(this_klass); duke@435: kamg@4245: // Compute transitive closure of interfaces this class implements duke@435: // Do final class setup coleenp@4719: fill_oop_maps(this_klass, info.nonstatic_oop_map_count, info.nonstatic_oop_offsets, info.nonstatic_oop_counts); duke@435: jrose@3926: // Fill in has_finalizer, has_vanilla_constructor, and layout_helper duke@435: set_precomputed_flags(this_klass); duke@435: duke@435: // reinitialize modifiers, using the InnerClasses attribute duke@435: int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle)); duke@435: this_klass->set_modifier_flags(computed_modifiers); duke@435: duke@435: // check if this class can access its super class duke@435: check_super_class_access(this_klass, CHECK_(nullHandle)); duke@435: duke@435: // check if this class can access its superinterfaces duke@435: check_super_interface_access(this_klass, CHECK_(nullHandle)); duke@435: duke@435: // check if this class overrides any final method duke@435: check_final_method_override(this_klass, CHECK_(nullHandle)); duke@435: duke@435: // check that if this class is an interface then it doesn't have static methods duke@435: if (this_klass->is_interface()) { acorn@4422: /* An interface in a JAVA 8 classfile can be static */ acorn@4422: if (_major_version < JAVA_8_VERSION) { acorn@4422: check_illegal_static_method(this_klass, CHECK_(nullHandle)); acorn@4422: } duke@435: } duke@435: coleenp@5176: // Allocate mirror and initialize static fields coleenp@5176: java_lang_Class::create_mirror(this_klass, protection_domain, CHECK_(nullHandle)); coleenp@5176: kamg@4245: kamg@4245: #ifdef ASSERT kamg@4245: if (ParseAllGenericSignatures) { kamg@4245: parseAndPrintGenericSignatures(this_klass, CHECK_(nullHandle)); kamg@4245: } kamg@4245: #endif kamg@4245: kamg@4245: // Generate any default methods - default methods are interface methods kamg@4245: // that have a default implementation. This is new with Lambda project. kamg@4245: if (has_default_methods && !access_flags.is_interface() && kamg@4245: local_interfaces->length() > 0) { kamg@4245: DefaultMethods::generate_default_methods( kamg@4245: this_klass(), &all_mirandas, CHECK_(nullHandle)); kamg@4245: } kamg@4245: coleenp@4037: // Update the loader_data graph. coleenp@4037: record_defined_class_dependencies(this_klass, CHECK_NULL); coleenp@4037: coleenp@4037: ClassLoadingService::notify_class_loaded(InstanceKlass::cast(this_klass()), duke@435: false /* not shared class */); duke@435: duke@435: if (TraceClassLoading) { kamg@4245: ResourceMark rm; duke@435: // print in a single call to reduce interleaving of output duke@435: if (cfs->source() != NULL) { duke@435: tty->print("[Loaded %s from %s]\n", this_klass->external_name(), duke@435: cfs->source()); duke@435: } else if (class_loader.is_null()) { duke@435: if (THREAD->is_Java_thread()) { coleenp@4037: Klass* caller = ((JavaThread*)THREAD)->security_get_caller_class(1); duke@435: tty->print("[Loaded %s by instance of %s]\n", duke@435: this_klass->external_name(), coleenp@4037: InstanceKlass::cast(caller)->external_name()); duke@435: } else { duke@435: tty->print("[Loaded %s]\n", this_klass->external_name()); duke@435: } duke@435: } else { duke@435: tty->print("[Loaded %s from %s]\n", this_klass->external_name(), coleenp@4037: InstanceKlass::cast(class_loader->klass())->external_name()); duke@435: } duke@435: } duke@435: duke@435: if (TraceClassResolution) { kamg@4245: ResourceMark rm; duke@435: // print out the superclass. hseigel@4278: const char * from = this_klass()->external_name(); duke@435: if (this_klass->java_super() != NULL) { coleenp@4037: tty->print("RESOLVE %s %s (super)\n", from, InstanceKlass::cast(this_klass->java_super())->external_name()); duke@435: } duke@435: // print out each of the interface classes referred to by this class. coleenp@4037: Array* local_interfaces = this_klass->local_interfaces(); coleenp@4037: if (local_interfaces != NULL) { duke@435: int length = local_interfaces->length(); duke@435: for (int i = 0; i < length; i++) { coleenp@4037: Klass* k = local_interfaces->at(i); coleenp@4037: InstanceKlass* to_class = InstanceKlass::cast(k); duke@435: const char * to = to_class->external_name(); acorn@1092: tty->print("RESOLVE %s %s (interface)\n", from, to); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // preserve result across HandleMark duke@435: preserve_this_klass = this_klass(); duke@435: } duke@435: coleenp@4037: // Create new handle outside HandleMark (might be needed for coleenp@4037: // Extended Class Redefinition) duke@435: instanceKlassHandle this_klass (THREAD, preserve_this_klass); coleenp@4037: debug_only(this_klass->verify();) duke@435: coleenp@4719: // Clear class if no error has occurred so destructor doesn't deallocate it coleenp@4719: _klass = NULL; duke@435: return this_klass; duke@435: } duke@435: coleenp@4719: // Destructor to clean up if there's an error coleenp@4719: ClassFileParser::~ClassFileParser() { coleenp@4719: MetadataFactory::free_metadata(_loader_data, _cp); coleenp@4719: MetadataFactory::free_array(_loader_data, _fields); coleenp@4719: coleenp@4719: // Free methods coleenp@4719: InstanceKlass::deallocate_methods(_loader_data, _methods); coleenp@4719: coleenp@4719: // beware of the Universe::empty_blah_array!! coleenp@4719: if (_inner_classes != Universe::the_empty_short_array()) { coleenp@4719: MetadataFactory::free_array(_loader_data, _inner_classes); coleenp@4719: } coleenp@4719: coleenp@4719: // Free interfaces coleenp@4719: InstanceKlass::deallocate_interfaces(_loader_data, _super_klass(), coleenp@4719: _local_interfaces, _transitive_interfaces); coleenp@4719: coleenp@4719: MetadataFactory::free_array(_loader_data, _annotations); coleenp@4719: MetadataFactory::free_array(_loader_data, _type_annotations); coleenp@4719: Annotations::free_contents(_loader_data, _fields_annotations); coleenp@4719: Annotations::free_contents(_loader_data, _fields_type_annotations); coleenp@4719: coleenp@4719: clear_class_metadata(); coleenp@4719: coleenp@4719: // deallocate the klass if already created. coleenp@4719: MetadataFactory::free_metadata(_loader_data, _klass); coleenp@4719: _klass = NULL; coleenp@4719: } coleenp@4719: jwilhelm@4430: void ClassFileParser::print_field_layout(Symbol* name, jwilhelm@4430: Array* fields, jwilhelm@4430: constantPoolHandle cp, jwilhelm@4430: int instance_size, jwilhelm@4430: int instance_fields_start, jwilhelm@4430: int instance_fields_end, jwilhelm@4430: int static_fields_end) { jwilhelm@4430: tty->print("%s: field layout\n", name->as_klass_external_name()); jwilhelm@4430: tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---"); jwilhelm@4430: for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { jwilhelm@4430: if (!fs.access_flags().is_static()) { jwilhelm@4430: tty->print(" @%3d \"%s\" %s\n", jwilhelm@4430: fs.offset(), jwilhelm@4430: fs.name()->as_klass_external_name(), jwilhelm@4430: fs.signature()->as_klass_external_name()); jwilhelm@4430: } jwilhelm@4430: } jwilhelm@4430: tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---"); jwilhelm@4430: tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---"); jwilhelm@4430: tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---"); jwilhelm@4430: for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { jwilhelm@4430: if (fs.access_flags().is_static()) { jwilhelm@4430: tty->print(" @%3d \"%s\" %s\n", jwilhelm@4430: fs.offset(), jwilhelm@4430: fs.name()->as_klass_external_name(), jwilhelm@4430: fs.signature()->as_klass_external_name()); jwilhelm@4430: } jwilhelm@4430: } jwilhelm@4430: tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---"); jwilhelm@4430: tty->print("\n"); jwilhelm@4430: } jwilhelm@4430: jcoomes@1374: unsigned int jcoomes@1374: ClassFileParser::compute_oop_map_count(instanceKlassHandle super, jcoomes@1374: unsigned int nonstatic_oop_map_count, jcoomes@1374: int first_nonstatic_oop_offset) { jcoomes@1374: unsigned int map_count = jcoomes@1374: super.is_null() ? 0 : super->nonstatic_oop_map_count(); duke@435: if (nonstatic_oop_map_count > 0) { duke@435: // We have oops to add to map jcoomes@1373: if (map_count == 0) { jcoomes@1373: map_count = nonstatic_oop_map_count; duke@435: } else { jcoomes@1373: // Check whether we should add a new map block or whether the last one can jcoomes@1373: // be extended jcoomes@1373: OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps(); jcoomes@1373: OopMapBlock* const last_map = first_map + map_count - 1; jcoomes@1373: jcoomes@1373: int next_offset = last_map->offset() + last_map->count() * heapOopSize; duke@435: if (next_offset == first_nonstatic_oop_offset) { duke@435: // There is no gap bettwen superklass's last oop field and first duke@435: // local oop field, merge maps. duke@435: nonstatic_oop_map_count -= 1; duke@435: } else { duke@435: // Superklass didn't end with a oop field, add extra maps jcoomes@1373: assert(next_offset < first_nonstatic_oop_offset, "just checking"); duke@435: } jcoomes@1373: map_count += nonstatic_oop_map_count; duke@435: } duke@435: } jcoomes@1373: return map_count; duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::fill_oop_maps(instanceKlassHandle k, jcoomes@1374: unsigned int nonstatic_oop_map_count, jcoomes@1374: int* nonstatic_oop_offsets, jcoomes@1374: unsigned int* nonstatic_oop_counts) { duke@435: OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps(); coleenp@4037: const InstanceKlass* const super = k->superklass(); jcoomes@1374: const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0; jcoomes@1373: if (super_count > 0) { jcoomes@1373: // Copy maps from superklass duke@435: OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps(); jcoomes@1374: for (unsigned int i = 0; i < super_count; ++i) { duke@435: *this_oop_map++ = *super_oop_map++; duke@435: } duke@435: } jcoomes@1373: duke@435: if (nonstatic_oop_map_count > 0) { jcoomes@1373: if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) { jcoomes@1373: // The counts differ because there is no gap between superklass's last oop jcoomes@1373: // field and the first local oop field. Extend the last oop map copied duke@435: // from the superklass instead of creating new one. duke@435: nonstatic_oop_map_count--; duke@435: nonstatic_oop_offsets++; duke@435: this_oop_map--; jcoomes@1373: this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++); duke@435: this_oop_map++; duke@435: } jcoomes@1373: duke@435: // Add new map blocks, fill them duke@435: while (nonstatic_oop_map_count-- > 0) { duke@435: this_oop_map->set_offset(*nonstatic_oop_offsets++); jcoomes@1373: this_oop_map->set_count(*nonstatic_oop_counts++); duke@435: this_oop_map++; duke@435: } jcoomes@1373: assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() == jcoomes@1373: this_oop_map, "sanity"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::set_precomputed_flags(instanceKlassHandle k) { coleenp@4037: Klass* super = k->super(); duke@435: duke@435: // Check if this klass has an empty finalize method (i.e. one with return bytecode only), duke@435: // in which case we don't have to register objects as finalizable duke@435: if (!_has_empty_finalizer) { duke@435: if (_has_finalizer || coleenp@4037: (super != NULL && super->has_finalizer())) { duke@435: k->set_has_finalizer(); duke@435: } duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: bool f = false; coleenp@4037: Method* m = k->lookup_method(vmSymbols::finalize_method_name(), duke@435: vmSymbols::void_method_signature()); duke@435: if (m != NULL && !m->is_empty_method()) { duke@435: f = true; duke@435: } duke@435: assert(f == k->has_finalizer(), "inconsistent has_finalizer"); duke@435: #endif duke@435: duke@435: // Check if this klass supports the java.lang.Cloneable interface never@1577: if (SystemDictionary::Cloneable_klass_loaded()) { never@1577: if (k->is_subtype_of(SystemDictionary::Cloneable_klass())) { duke@435: k->set_is_cloneable(); duke@435: } duke@435: } duke@435: duke@435: // Check if this klass has a vanilla default constructor duke@435: if (super == NULL) { duke@435: // java.lang.Object has empty default constructor duke@435: k->set_has_vanilla_constructor(); duke@435: } else { hseigel@4278: if (super->has_vanilla_constructor() && duke@435: _has_vanilla_constructor) { duke@435: k->set_has_vanilla_constructor(); duke@435: } duke@435: #ifdef ASSERT duke@435: bool v = false; hseigel@4278: if (super->has_vanilla_constructor()) { coleenp@4037: Method* constructor = k->find_method(vmSymbols::object_initializer_name( duke@435: ), vmSymbols::void_method_signature()); duke@435: if (constructor != NULL && constructor->is_vanilla_constructor()) { duke@435: v = true; duke@435: } duke@435: } duke@435: assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor"); duke@435: #endif duke@435: } duke@435: duke@435: // If it cannot be fast-path allocated, set a bit in the layout helper. coleenp@4037: // See documentation of InstanceKlass::can_be_fastpath_allocated(). duke@435: assert(k->size_helper() > 0, "layout_helper is initialized"); duke@435: if ((!RegisterFinalizersAtInit && k->has_finalizer()) duke@435: || k->is_abstract() || k->is_interface() coleenp@4304: || (k->name() == vmSymbols::java_lang_Class() && k->class_loader() == NULL) duke@435: || k->size_helper() >= FastAllocateSizeLimit) { duke@435: // Forbid fast-path allocation. duke@435: jint lh = Klass::instance_layout_helper(k->size_helper(), true); duke@435: k->set_layout_helper(lh); duke@435: } duke@435: } duke@435: coleenp@4037: // Attach super classes and interface classes to class loader data coleenp@4037: void ClassFileParser::record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS) { coleenp@4037: ClassLoaderData * defining_loader_data = defined_klass->class_loader_data(); coleenp@4037: if (defining_loader_data->is_the_null_class_loader_data()) { coleenp@4037: // Dependencies to null class loader data are implicit. coleenp@4037: return; coleenp@4037: } else { coleenp@4037: // add super class dependency coleenp@4037: Klass* super = defined_klass->super(); coleenp@4037: if (super != NULL) { coleenp@4037: defining_loader_data->record_dependency(super, CHECK); coleenp@4037: } coleenp@4037: coleenp@4037: // add super interface dependencies coleenp@4037: Array* local_interfaces = defined_klass->local_interfaces(); coleenp@4037: if (local_interfaces != NULL) { coleenp@4037: int length = local_interfaces->length(); coleenp@4037: for (int i = 0; i < length; i++) { coleenp@4037: defining_loader_data->record_dependency(local_interfaces->at(i), CHECK); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: coleenp@4719: // utility methods for appending an array with check for duplicates coleenp@4037: coleenp@4037: void append_interfaces(GrowableArray* result, Array* ifs) { coleenp@4037: // iterate over new interfaces coleenp@4037: for (int i = 0; i < ifs->length(); i++) { coleenp@4037: Klass* e = ifs->at(i); coleenp@4037: assert(e->is_klass() && InstanceKlass::cast(e)->is_interface(), "just checking"); coleenp@4037: // add new interface coleenp@4037: result->append_if_missing(e); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4719: Array* ClassFileParser::compute_transitive_interfaces( coleenp@4719: instanceKlassHandle super, coleenp@4719: Array* local_ifs, TRAPS) { duke@435: // Compute maximum size for transitive interfaces duke@435: int max_transitive_size = 0; duke@435: int super_size = 0; duke@435: // Add superclass transitive interfaces size duke@435: if (super.not_null()) { duke@435: super_size = super->transitive_interfaces()->length(); duke@435: max_transitive_size += super_size; duke@435: } duke@435: // Add local interfaces' super interfaces duke@435: int local_size = local_ifs->length(); duke@435: for (int i = 0; i < local_size; i++) { coleenp@4037: Klass* l = local_ifs->at(i); coleenp@4037: max_transitive_size += InstanceKlass::cast(l)->transitive_interfaces()->length(); duke@435: } duke@435: // Finally add local interfaces duke@435: max_transitive_size += local_size; duke@435: // Construct array duke@435: if (max_transitive_size == 0) { duke@435: // no interfaces, use canonicalized array coleenp@4037: return Universe::the_empty_klass_array(); duke@435: } else if (max_transitive_size == super_size) { duke@435: // no new local interfaces added, share superklass' transitive interface array coleenp@4037: return super->transitive_interfaces(); duke@435: } else if (max_transitive_size == local_size) { duke@435: // only local interfaces added, share local interface array coleenp@4037: return local_ifs; duke@435: } else { coleenp@4037: ResourceMark rm; coleenp@4037: GrowableArray* result = new GrowableArray(max_transitive_size); coleenp@4037: duke@435: // Copy down from superclass duke@435: if (super.not_null()) { coleenp@4037: append_interfaces(result, super->transitive_interfaces()); duke@435: } coleenp@4037: duke@435: // Copy down from local interfaces' superinterfaces duke@435: for (int i = 0; i < local_ifs->length(); i++) { coleenp@4037: Klass* l = local_ifs->at(i); coleenp@4037: append_interfaces(result, InstanceKlass::cast(l)->transitive_interfaces()); duke@435: } duke@435: // Finally add local interfaces coleenp@4037: append_interfaces(result, local_ifs); coleenp@4037: coleenp@4037: // length will be less than the max_transitive_size if duplicates were removed coleenp@4037: int length = result->length(); coleenp@4037: assert(length <= max_transitive_size, "just checking"); coleenp@4719: Array* new_result = MetadataFactory::new_array(_loader_data, length, CHECK_NULL); coleenp@4037: for (int i = 0; i < length; i++) { coleenp@4037: Klass* e = result->at(i); duke@435: assert(e != NULL, "just checking"); coleenp@4037: new_result->at_put(i, e); duke@435: } coleenp@4037: return new_result; duke@435: } duke@435: } duke@435: duke@435: void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) { coleenp@4037: Klass* super = this_klass->super(); duke@435: if ((super != NULL) && coleenp@4037: (!Reflection::verify_class_access(this_klass(), super, false))) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_IllegalAccessError(), duke@435: "class %s cannot access its superclass %s", duke@435: this_klass->external_name(), coleenp@4037: InstanceKlass::cast(super)->external_name() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::check_super_interface_access(instanceKlassHandle this_klass, TRAPS) { coleenp@4037: Array* local_interfaces = this_klass->local_interfaces(); duke@435: int lng = local_interfaces->length(); duke@435: for (int i = lng - 1; i >= 0; i--) { coleenp@4037: Klass* k = local_interfaces->at(i); hseigel@4278: assert (k != NULL && k->is_interface(), "invalid interface"); coleenp@4037: if (!Reflection::verify_class_access(this_klass(), k, false)) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_IllegalAccessError(), duke@435: "class %s cannot access its superinterface %s", duke@435: this_klass->external_name(), coleenp@4037: InstanceKlass::cast(k)->external_name() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) { coleenp@4037: Array* methods = this_klass->methods(); duke@435: int num_methods = methods->length(); duke@435: duke@435: // go thru each method and check if it overrides a final method duke@435: for (int index = 0; index < num_methods; index++) { coleenp@4037: Method* m = methods->at(index); duke@435: duke@435: // skip private, static and methods duke@435: if ((!m->is_private()) && duke@435: (!m->is_static()) && duke@435: (m->name() != vmSymbols::object_initializer_name())) { duke@435: coleenp@2497: Symbol* name = m->name(); coleenp@2497: Symbol* signature = m->signature(); coleenp@4037: Klass* k = this_klass->super(); coleenp@4037: Method* super_m = NULL; duke@435: while (k != NULL) { duke@435: // skip supers that don't have final methods. coleenp@4037: if (k->has_final_method()) { duke@435: // lookup a matching method in the super class hierarchy coleenp@4037: super_m = InstanceKlass::cast(k)->lookup_method(name, signature); duke@435: if (super_m == NULL) { duke@435: break; // didn't find any match; get out duke@435: } duke@435: duke@435: if (super_m->is_final() && duke@435: // matching method in super is final coleenp@4037: (Reflection::verify_field_access(this_klass(), duke@435: super_m->method_holder(), duke@435: super_m->method_holder(), duke@435: super_m->access_flags(), false)) duke@435: // this class can access super final method and therefore override duke@435: ) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_VerifyError(), duke@435: "class %s overrides final method %s.%s", duke@435: this_klass->external_name(), duke@435: name->as_C_string(), duke@435: signature->as_C_string() duke@435: ); duke@435: return; duke@435: } duke@435: duke@435: // continue to look from super_m's holder's super. coleenp@4251: k = super_m->method_holder()->super(); duke@435: continue; duke@435: } duke@435: coleenp@4037: k = k->super(); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // assumes that this_klass is an interface duke@435: void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) { duke@435: assert(this_klass->is_interface(), "not an interface"); coleenp@4037: Array* methods = this_klass->methods(); duke@435: int num_methods = methods->length(); duke@435: duke@435: for (int index = 0; index < num_methods; index++) { coleenp@4037: Method* m = methods->at(index); duke@435: // if m is static and not the init method, throw a verify error duke@435: if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_VerifyError(), duke@435: "Illegal static method %s in interface %s", duke@435: m->name()->as_C_string(), duke@435: this_klass->external_name() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // utility methods for format checking duke@435: duke@435: void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) { duke@435: if (!_need_verify) { return; } duke@435: duke@435: const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0; duke@435: const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; duke@435: const bool is_final = (flags & JVM_ACC_FINAL) != 0; duke@435: const bool is_super = (flags & JVM_ACC_SUPER) != 0; duke@435: const bool is_enum = (flags & JVM_ACC_ENUM) != 0; duke@435: const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0; duke@435: const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; duke@435: duke@435: if ((is_abstract && is_final) || duke@435: (is_interface && !is_abstract) || duke@435: (is_interface && major_gte_15 && (is_super || is_enum)) || duke@435: (!is_interface && major_gte_15 && is_annotation)) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Illegal class modifiers in class %s: 0x%X", duke@435: _class_name->as_C_string(), flags duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: bool ClassFileParser::has_illegal_visibility(jint flags) { duke@435: const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; duke@435: const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; duke@435: const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; duke@435: duke@435: return ((is_public && is_protected) || duke@435: (is_public && is_private) || duke@435: (is_protected && is_private)); duke@435: } duke@435: duke@435: bool ClassFileParser::is_supported_version(u2 major, u2 minor) { xlu@1559: u2 max_version = xlu@1559: JDK_Version::is_gte_jdk17x_version() ? JAVA_MAX_SUPPORTED_VERSION : xlu@1559: (JDK_Version::is_gte_jdk16x_version() ? JAVA_6_VERSION : JAVA_1_5_VERSION); duke@435: return (major >= JAVA_MIN_SUPPORTED_VERSION) && kamg@611: (major <= max_version) && kamg@611: ((major != max_version) || duke@435: (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION)); duke@435: } duke@435: duke@435: void ClassFileParser::verify_legal_field_modifiers( duke@435: jint flags, bool is_interface, TRAPS) { duke@435: if (!_need_verify) { return; } duke@435: duke@435: const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; duke@435: const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; duke@435: const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; duke@435: const bool is_static = (flags & JVM_ACC_STATIC) != 0; duke@435: const bool is_final = (flags & JVM_ACC_FINAL) != 0; duke@435: const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0; duke@435: const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0; duke@435: const bool is_enum = (flags & JVM_ACC_ENUM) != 0; duke@435: const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; duke@435: duke@435: bool is_illegal = false; duke@435: duke@435: if (is_interface) { duke@435: if (!is_public || !is_static || !is_final || is_private || duke@435: is_protected || is_volatile || is_transient || duke@435: (major_gte_15 && is_enum)) { duke@435: is_illegal = true; duke@435: } duke@435: } else { // not interface duke@435: if (has_illegal_visibility(flags) || (is_final && is_volatile)) { duke@435: is_illegal = true; duke@435: } duke@435: } duke@435: duke@435: if (is_illegal) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Illegal field modifiers in class %s: 0x%X", duke@435: _class_name->as_C_string(), flags); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: void ClassFileParser::verify_legal_method_modifiers( coleenp@2497: jint flags, bool is_interface, Symbol* name, TRAPS) { duke@435: if (!_need_verify) { return; } duke@435: duke@435: const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; duke@435: const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; duke@435: const bool is_static = (flags & JVM_ACC_STATIC) != 0; duke@435: const bool is_final = (flags & JVM_ACC_FINAL) != 0; duke@435: const bool is_native = (flags & JVM_ACC_NATIVE) != 0; duke@435: const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; duke@435: const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0; duke@435: const bool is_strict = (flags & JVM_ACC_STRICT) != 0; duke@435: const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0; acorn@4422: const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; duke@435: const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; kamg@4245: const bool major_gte_8 = _major_version >= JAVA_8_VERSION; duke@435: const bool is_initializer = (name == vmSymbols::object_initializer_name()); duke@435: duke@435: bool is_illegal = false; duke@435: duke@435: if (is_interface) { acorn@4422: if (major_gte_8) { acorn@4422: // Class file version is JAVA_8_VERSION or later Methods of acorn@4422: // interfaces may set any of the flags except ACC_PROTECTED, acorn@4422: // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must acorn@4422: // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. acorn@4422: if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */ acorn@4422: (is_native || is_protected || is_final || is_synchronized) || acorn@4422: // If a specific method of a class or interface has its acorn@4422: // ACC_ABSTRACT flag set, it must not have any of its acorn@4422: // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, acorn@4422: // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to acorn@4422: // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as acorn@4422: // those flags are illegal irrespective of ACC_ABSTRACT being set or not. acorn@4422: (is_abstract && (is_private || is_static || is_strict))) { acorn@4422: is_illegal = true; acorn@4422: } acorn@4422: } else if (major_gte_15) { acorn@4422: // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION) acorn@4422: if (!is_public || is_static || is_final || is_synchronized || acorn@4422: is_native || !is_abstract || is_strict) { acorn@4422: is_illegal = true; acorn@4422: } acorn@4422: } else { acorn@4422: // Class file version is pre-JAVA_1_5_VERSION acorn@4422: if (!is_public || is_static || is_final || is_native || !is_abstract) { acorn@4422: is_illegal = true; acorn@4422: } duke@435: } duke@435: } else { // not interface duke@435: if (is_initializer) { duke@435: if (is_static || is_final || is_synchronized || is_native || duke@435: is_abstract || (major_gte_15 && is_bridge)) { duke@435: is_illegal = true; duke@435: } duke@435: } else { // not initializer duke@435: if (is_abstract) { duke@435: if ((is_final || is_native || is_private || is_static || duke@435: (major_gte_15 && (is_synchronized || is_strict)))) { duke@435: is_illegal = true; duke@435: } duke@435: } duke@435: if (has_illegal_visibility(flags)) { duke@435: is_illegal = true; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (is_illegal) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Method %s in class %s has illegal modifiers: 0x%X", duke@435: name->as_C_string(), _class_name->as_C_string(), flags); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) { duke@435: assert(_need_verify, "only called when _need_verify is true"); duke@435: int i = 0; duke@435: int count = length >> 2; duke@435: for (int k=0; k= 128 (highest bit 1) for v == 0 or v >= 128. duke@435: unsigned char res = b0 | b0 - 1 | duke@435: b1 | b1 - 1 | duke@435: b2 | b2 - 1 | duke@435: b3 | b3 - 1; duke@435: if (res >= 128) break; duke@435: i += 4; duke@435: } duke@435: for(; i < length; i++) { duke@435: unsigned short c; duke@435: // no embedded zeros duke@435: guarantee_property((buffer[i] != 0), "Illegal UTF8 string in constant pool in class file %s", CHECK); duke@435: if(buffer[i] < 128) { duke@435: continue; duke@435: } duke@435: if ((i + 5) < length) { // see if it's legal supplementary character duke@435: if (UTF8::is_supplementary_character(&buffer[i])) { duke@435: c = UTF8::get_supplementary_character(&buffer[i]); duke@435: i += 5; duke@435: continue; duke@435: } duke@435: } duke@435: switch (buffer[i] >> 4) { duke@435: default: break; duke@435: case 0x8: case 0x9: case 0xA: case 0xB: case 0xF: duke@435: classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); duke@435: case 0xC: case 0xD: // 110xxxxx 10xxxxxx duke@435: c = (buffer[i] & 0x1F) << 6; duke@435: i++; duke@435: if ((i < length) && ((buffer[i] & 0xC0) == 0x80)) { duke@435: c += buffer[i] & 0x3F; duke@435: if (_major_version <= 47 || c == 0 || c >= 0x80) { duke@435: // for classes with major > 47, c must a null or a character in its shortest form duke@435: break; duke@435: } duke@435: } duke@435: classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); duke@435: case 0xE: // 1110xxxx 10xxxxxx 10xxxxxx duke@435: c = (buffer[i] & 0xF) << 12; duke@435: i += 2; duke@435: if ((i < length) && ((buffer[i-1] & 0xC0) == 0x80) && ((buffer[i] & 0xC0) == 0x80)) { duke@435: c += ((buffer[i-1] & 0x3F) << 6) + (buffer[i] & 0x3F); duke@435: if (_major_version <= 47 || c >= 0x800) { duke@435: // for classes with major > 47, c must be in its shortest form duke@435: break; duke@435: } duke@435: } duke@435: classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); duke@435: } // end of switch duke@435: } // end of for duke@435: } duke@435: duke@435: // Checks if name is a legal class name. coleenp@2497: void ClassFileParser::verify_legal_class_name(Symbol* name, TRAPS) { duke@435: if (!_need_verify || _relax_verify) { return; } duke@435: duke@435: char buf[fixed_buffer_size]; duke@435: char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); duke@435: unsigned int length = name->utf8_length(); duke@435: bool legal = false; duke@435: duke@435: if (length > 0) { duke@435: char* p; duke@435: if (bytes[0] == JVM_SIGNATURE_ARRAY) { duke@435: p = skip_over_field_signature(bytes, false, length, CHECK); duke@435: legal = (p != NULL) && ((p - bytes) == (int)length); duke@435: } else if (_major_version < JAVA_1_5_VERSION) { duke@435: if (bytes[0] != '<') { duke@435: p = skip_over_field_name(bytes, true, length); duke@435: legal = (p != NULL) && ((p - bytes) == (int)length); duke@435: } duke@435: } else { duke@435: // 4900761: relax the constraints based on JSR202 spec duke@435: // Class names may be drawn from the entire Unicode character set. duke@435: // Identifiers between '/' must be unqualified names. duke@435: // The utf8 string has been verified when parsing cpool entries. duke@435: legal = verify_unqualified_name(bytes, length, LegalClass); duke@435: } duke@435: } duke@435: if (!legal) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Illegal class name \"%s\" in class file %s", bytes, duke@435: _class_name->as_C_string() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: // Checks if name is a legal field name. coleenp@2497: void ClassFileParser::verify_legal_field_name(Symbol* name, TRAPS) { duke@435: if (!_need_verify || _relax_verify) { return; } duke@435: duke@435: char buf[fixed_buffer_size]; duke@435: char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); duke@435: unsigned int length = name->utf8_length(); duke@435: bool legal = false; duke@435: duke@435: if (length > 0) { duke@435: if (_major_version < JAVA_1_5_VERSION) { duke@435: if (bytes[0] != '<') { duke@435: char* p = skip_over_field_name(bytes, false, length); duke@435: legal = (p != NULL) && ((p - bytes) == (int)length); duke@435: } duke@435: } else { duke@435: // 4881221: relax the constraints based on JSR202 spec duke@435: legal = verify_unqualified_name(bytes, length, LegalField); duke@435: } duke@435: } duke@435: duke@435: if (!legal) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Illegal field name \"%s\" in class %s", bytes, duke@435: _class_name->as_C_string() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: // Checks if name is a legal method name. coleenp@2497: void ClassFileParser::verify_legal_method_name(Symbol* name, TRAPS) { duke@435: if (!_need_verify || _relax_verify) { return; } duke@435: coleenp@2497: assert(name != NULL, "method name is null"); duke@435: char buf[fixed_buffer_size]; duke@435: char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); duke@435: unsigned int length = name->utf8_length(); duke@435: bool legal = false; duke@435: duke@435: if (length > 0) { duke@435: if (bytes[0] == '<') { duke@435: if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) { duke@435: legal = true; duke@435: } duke@435: } else if (_major_version < JAVA_1_5_VERSION) { duke@435: char* p; duke@435: p = skip_over_field_name(bytes, false, length); duke@435: legal = (p != NULL) && ((p - bytes) == (int)length); duke@435: } else { duke@435: // 4881221: relax the constraints based on JSR202 spec duke@435: legal = verify_unqualified_name(bytes, length, LegalMethod); duke@435: } duke@435: } duke@435: duke@435: if (!legal) { duke@435: ResourceMark rm(THREAD); duke@435: Exceptions::fthrow( duke@435: THREAD_AND_LOCATION, coleenp@2497: vmSymbols::java_lang_ClassFormatError(), duke@435: "Illegal method name \"%s\" in class %s", bytes, duke@435: _class_name->as_C_string() duke@435: ); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: duke@435: // Checks if signature is a legal field signature. coleenp@2497: void ClassFileParser::verify_legal_field_signature(Symbol* name, Symbol* signature, TRAPS) { duke@435: if (!_need_verify) { return; } duke@435: duke@435: char buf[fixed_buffer_size]; duke@435: char* bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); duke@435: unsigned int length = signature->utf8_length(); duke@435: char* p = skip_over_field_signature(bytes, false, length, CHECK); duke@435: duke@435: if (p == NULL || (p - bytes) != (int)length) { kamg@1941: throwIllegalSignature("Field", name, signature, CHECK); duke@435: } duke@435: } duke@435: duke@435: // Checks if signature is a legal method signature. duke@435: // Returns number of parameters coleenp@2497: int ClassFileParser::verify_legal_method_signature(Symbol* name, Symbol* signature, TRAPS) { duke@435: if (!_need_verify) { duke@435: // make sure caller's args_size will be less than 0 even for non-static duke@435: // method so it will be recomputed in compute_size_of_parameters(). duke@435: return -2; duke@435: } duke@435: duke@435: unsigned int args_size = 0; duke@435: char buf[fixed_buffer_size]; duke@435: char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size); duke@435: unsigned int length = signature->utf8_length(); duke@435: char* nextp; duke@435: duke@435: // The first character must be a '(' duke@435: if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) { duke@435: length--; duke@435: // Skip over legal field signatures duke@435: nextp = skip_over_field_signature(p, false, length, CHECK_0); duke@435: while ((length > 0) && (nextp != NULL)) { duke@435: args_size++; duke@435: if (p[0] == 'J' || p[0] == 'D') { duke@435: args_size++; duke@435: } duke@435: length -= nextp - p; duke@435: p = nextp; duke@435: nextp = skip_over_field_signature(p, false, length, CHECK_0); duke@435: } duke@435: // The first non-signature thing better be a ')' duke@435: if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) { duke@435: length--; duke@435: if (name->utf8_length() > 0 && name->byte_at(0) == '<') { duke@435: // All internal methods must return void duke@435: if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) { duke@435: return args_size; duke@435: } duke@435: } else { duke@435: // Now we better just have a return value duke@435: nextp = skip_over_field_signature(p, true, length, CHECK_0); duke@435: if (nextp && ((int)length == (nextp - p))) { duke@435: return args_size; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: // Report error kamg@1941: throwIllegalSignature("Method", name, signature, CHECK_0); duke@435: return 0; duke@435: } duke@435: duke@435: kamg@2236: // Unqualified names may not contain the characters '.', ';', '[', or '/'. kamg@2236: // Method names also may not contain the characters '<' or '>', unless kamg@2236: // or . Note that method names may not be or in this kamg@2236: // method. Because these names have been checked as special cases before kamg@2236: // calling this method in verify_legal_method_name. kamg@2236: bool ClassFileParser::verify_unqualified_name( kamg@2236: char* name, unsigned int length, int type) { duke@435: jchar ch; duke@435: duke@435: for (char* p = name; p != name + length; ) { duke@435: ch = *p; duke@435: if (ch < 128) { duke@435: p++; kamg@2236: if (ch == '.' || ch == ';' || ch == '[' ) { kamg@2236: return false; // do not permit '.', ';', or '[' duke@435: } duke@435: if (type != LegalClass && ch == '/') { duke@435: return false; // do not permit '/' unless it's class name duke@435: } duke@435: if (type == LegalMethod && (ch == '<' || ch == '>')) { duke@435: return false; // do not permit '<' or '>' in method names duke@435: } duke@435: } else { duke@435: char* tmp_p = UTF8::next(p, &ch); duke@435: p = tmp_p; duke@435: } duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: // Take pointer to a string. Skip over the longest part of the string that could duke@435: // be taken as a fieldname. Allow '/' if slash_ok is true. duke@435: // Return a pointer to just past the fieldname. duke@435: // Return NULL if no fieldname at all was found, or in the case of slash_ok duke@435: // being true, we saw consecutive slashes (meaning we were looking for a duke@435: // qualified path but found something that was badly-formed). duke@435: char* ClassFileParser::skip_over_field_name(char* name, bool slash_ok, unsigned int length) { duke@435: char* p; duke@435: jchar ch; duke@435: jboolean last_is_slash = false; duke@435: jboolean not_first_ch = false; duke@435: duke@435: for (p = name; p != name + length; not_first_ch = true) { duke@435: char* old_p = p; duke@435: ch = *p; duke@435: if (ch < 128) { duke@435: p++; duke@435: // quick check for ascii duke@435: if ((ch >= 'a' && ch <= 'z') || duke@435: (ch >= 'A' && ch <= 'Z') || duke@435: (ch == '_' || ch == '$') || duke@435: (not_first_ch && ch >= '0' && ch <= '9')) { duke@435: last_is_slash = false; duke@435: continue; duke@435: } duke@435: if (slash_ok && ch == '/') { duke@435: if (last_is_slash) { duke@435: return NULL; // Don't permit consecutive slashes duke@435: } duke@435: last_is_slash = true; duke@435: continue; duke@435: } duke@435: } else { duke@435: jint unicode_ch; duke@435: char* tmp_p = UTF8::next_character(p, &unicode_ch); duke@435: p = tmp_p; duke@435: last_is_slash = false; duke@435: // Check if ch is Java identifier start or is Java identifier part duke@435: // 4672820: call java.lang.Character methods directly without generating separate tables. duke@435: EXCEPTION_MARK; never@1577: instanceKlassHandle klass (THREAD, SystemDictionary::Character_klass()); duke@435: duke@435: // return value duke@435: JavaValue result(T_BOOLEAN); duke@435: // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart duke@435: JavaCallArguments args; duke@435: args.push_int(unicode_ch); duke@435: duke@435: // public static boolean isJavaIdentifierStart(char ch); duke@435: JavaCalls::call_static(&result, duke@435: klass, coleenp@2497: vmSymbols::isJavaIdentifierStart_name(), coleenp@2497: vmSymbols::int_bool_signature(), duke@435: &args, duke@435: THREAD); duke@435: duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: return 0; duke@435: } duke@435: if (result.get_jboolean()) { duke@435: continue; duke@435: } duke@435: duke@435: if (not_first_ch) { duke@435: // public static boolean isJavaIdentifierPart(char ch); duke@435: JavaCalls::call_static(&result, duke@435: klass, coleenp@2497: vmSymbols::isJavaIdentifierPart_name(), coleenp@2497: vmSymbols::int_bool_signature(), duke@435: &args, duke@435: THREAD); duke@435: duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: return 0; duke@435: } duke@435: duke@435: if (result.get_jboolean()) { duke@435: continue; duke@435: } duke@435: } duke@435: } duke@435: return (not_first_ch) ? old_p : NULL; duke@435: } duke@435: return (not_first_ch) ? p : NULL; duke@435: } duke@435: duke@435: duke@435: // Take pointer to a string. Skip over the longest part of the string that could duke@435: // be taken as a field signature. Allow "void" if void_ok. duke@435: // Return a pointer to just past the signature. duke@435: // Return NULL if no legal signature is found. duke@435: char* ClassFileParser::skip_over_field_signature(char* signature, duke@435: bool void_ok, duke@435: unsigned int length, duke@435: TRAPS) { duke@435: unsigned int array_dim = 0; duke@435: while (length > 0) { duke@435: switch (signature[0]) { duke@435: case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; } duke@435: case JVM_SIGNATURE_BOOLEAN: duke@435: case JVM_SIGNATURE_BYTE: duke@435: case JVM_SIGNATURE_CHAR: duke@435: case JVM_SIGNATURE_SHORT: duke@435: case JVM_SIGNATURE_INT: duke@435: case JVM_SIGNATURE_FLOAT: duke@435: case JVM_SIGNATURE_LONG: duke@435: case JVM_SIGNATURE_DOUBLE: duke@435: return signature + 1; duke@435: case JVM_SIGNATURE_CLASS: { duke@435: if (_major_version < JAVA_1_5_VERSION) { duke@435: // Skip over the class name if one is there duke@435: char* p = skip_over_field_name(signature + 1, true, --length); duke@435: duke@435: // The next character better be a semicolon duke@435: if (p && (p - signature) > 1 && p[0] == ';') { duke@435: return p + 1; duke@435: } duke@435: } else { duke@435: // 4900761: For class version > 48, any unicode is allowed in class name. duke@435: length--; duke@435: signature++; duke@435: while (length > 0 && signature[0] != ';') { duke@435: if (signature[0] == '.') { duke@435: classfile_parse_error("Class name contains illegal character '.' in descriptor in class file %s", CHECK_0); duke@435: } duke@435: length--; duke@435: signature++; duke@435: } duke@435: if (signature[0] == ';') { return signature + 1; } duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: case JVM_SIGNATURE_ARRAY: duke@435: array_dim++; duke@435: if (array_dim > 255) { duke@435: // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions. duke@435: classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0); duke@435: } duke@435: // The rest of what's there better be a legal signature duke@435: signature++; duke@435: length--; duke@435: void_ok = false; duke@435: break; duke@435: duke@435: default: duke@435: return NULL; duke@435: } duke@435: } duke@435: return NULL; duke@435: }