duke@435: /* coleenp@3682: * Copyright (c) 1997, 2012, 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" 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" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "memory/universe.inline.hpp" stefank@2314: #include "oops/constantPoolOop.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/klassOop.hpp" stefank@2314: #include "oops/klassVtable.hpp" stefank@2314: #include "oops/methodOop.hpp" coleenp@2497: #include "oops/symbol.hpp" 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" 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@571: #define JAVA_MAX_SUPPORTED_VERSION 51 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: duke@435: coleenp@3682: void ClassFileParser::parse_constant_pool_entries(Handle class_loader, constantPoolHandle cp, 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 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(); duke@435: 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(); duke@435: 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(); duke@435: 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(); duke@435: 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(); duke@435: 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(); jrose@1957: 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(); jrose@1957: 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 jrose@2353: 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(); duke@435: 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(); duke@435: 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(); duke@435: 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(); duke@435: 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(); duke@435: 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@3682: SymbolTable::new_symbols(class_loader, cp, names_count, names, lengths, indices, hashValues, CHECK); duke@435: names_count = 0; duke@435: } duke@435: } else { duke@435: 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@3682: SymbolTable::new_symbols(class_loader, 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: coleenp@2497: // This class unreferences constant pool symbols if an error has occurred coleenp@2497: // while parsing the class before it is assigned into the class. coleenp@2497: // If it gets an error after that it is unloaded and the constant pool will coleenp@2497: // be cleaned up then. coleenp@2497: class ConstantPoolCleaner : public StackObj { coleenp@2497: constantPoolHandle _cphandle; coleenp@2497: bool _in_error; coleenp@2497: public: coleenp@2497: ConstantPoolCleaner(constantPoolHandle cp) : _cphandle(cp), _in_error(true) {} coleenp@2497: ~ConstantPoolCleaner() { coleenp@2497: if (_in_error && _cphandle.not_null()) { coleenp@2497: _cphandle->unreference_symbols(); coleenp@2497: } coleenp@2497: } coleenp@2497: void set_in_error(bool clean) { _in_error = clean; } coleenp@2497: }; coleenp@2497: duke@435: bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); } duke@435: coleenp@3682: constantPoolHandle ClassFileParser::parse_constant_pool(Handle class_loader, 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)); duke@435: constantPoolOop constant_pool = jmasa@953: oopFactory::new_constantPool(length, ysr@2533: oopDesc::IsSafeConc, jmasa@953: CHECK_(nullHandle)); duke@435: constantPoolHandle cp (THREAD, constant_pool); duke@435: duke@435: cp->set_partially_loaded(); // Enables heap verify to work on partial constantPoolOops coleenp@2497: ConstantPoolCleaner cp_in_error(cp); // set constant pool to be cleaned up. duke@435: duke@435: // parsing constant pool entries coleenp@3682: parse_constant_pool_entries(class_loader, cp, 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); duke@435: check_property(valid_cp_range(klass_ref_index, length) && jrose@866: is_klass_reference(cp, 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); duke@435: check_property( duke@435: valid_cp_range(name_ref_index, length) && duke@435: cp->tag_at(name_ref_index).is_utf8(), duke@435: "Invalid constant pool index %u in class file %s", duke@435: name_ref_index, CHECK_(nullHandle)); duke@435: check_property( duke@435: valid_cp_range(signature_ref_index, length) && duke@435: cp->tag_at(signature_ref_index).is_utf8(), duke@435: "Invalid constant pool index %u in class file %s", duke@435: 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); duke@435: check_property( duke@435: valid_cp_range(class_index, length) && duke@435: cp->tag_at(class_index).is_utf8(), duke@435: "Invalid constant pool index %u in class file %s", duke@435: 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_UnresolvedString : duke@435: ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present duke@435: break; duke@435: case JVM_CONSTANT_StringIndex : duke@435: { duke@435: int string_index = cp->string_index_at(index); duke@435: check_property( duke@435: valid_cp_range(string_index, length) && duke@435: cp->tag_at(string_index).is_utf8(), duke@435: "Invalid constant pool index %u in class file %s", duke@435: 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_invokeStatic: jrose@1957: case JVM_REF_invokeSpecial: 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; 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); jrose@1957: check_property( jrose@1957: valid_cp_range(ref_index, length) && jrose@1957: cp->tag_at(ref_index).is_utf8() && twisti@2698: EnableInvokeDynamic, jrose@1957: "Invalid constant pool index %u in class file %s", jrose@1957: 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: // Ensure that all the patches have been used. jrose@866: for (index = 0; index < _cp_patches->length(); index++) { jrose@866: guarantee_property(!has_cp_patch_at(index), jrose@866: "Unused constant pool patch at %d in class file %s", jrose@866: index, CHECK_(nullHandle)); jrose@866: } jrose@866: } jrose@866: duke@435: if (!_need_verify) { coleenp@2497: cp_in_error.set_in_error(false); 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: coleenp@2497: cp_in_error.set_in_error(false); 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; 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); jrose@866: cp->klass_at_put(index, java_lang_Class::as_klassOop(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: jrose@866: case JVM_CONSTANT_UnresolvedString : jrose@866: // Patching a string means pre-resolving it. jrose@866: // The spelling in the constant pool is ignored. jrose@866: // The constant reference may be any object whatever. jrose@866: // If it is not a real interned string, the constant is referred jrose@866: // to as a "pseudo-string", and must be presented to the CP jrose@866: // explicitly, because it may require scavenging. jrose@866: cp->pseudo_string_at_put(index, patch()); jrose@866: break; 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: duke@435: objArrayHandle ClassFileParser::parse_interfaces(constantPoolHandle cp, duke@435: int length, duke@435: Handle class_loader, duke@435: Handle protection_domain, coleenp@2497: Symbol* class_name, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: assert(length > 0, "only called for length>0"); duke@435: objArrayHandle nullHandle; duke@435: objArrayOop interface_oop = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: objArrayHandle interfaces (THREAD, interface_oop); duke@435: duke@435: int index; duke@435: for (index = 0; index < length; index++) { duke@435: u2 interface_index = cfs->get_u2(CHECK_(nullHandle)); jrose@866: KlassHandle interf; duke@435: check_property( duke@435: valid_cp_range(interface_index, cp->length()) && jrose@866: is_klass_reference(cp, interface_index), duke@435: "Interface name has bad constant pool index %u in class file %s", duke@435: interface_index, CHECK_(nullHandle)); jrose@866: if (cp->tag_at(interface_index).is_klass()) { jrose@866: interf = KlassHandle(THREAD, cp->resolved_klass_at(interface_index)); jrose@866: } else { coleenp@2497: Symbol* unresolved_klass = cp->klass_name_at(interface_index); jrose@866: jrose@866: // Don't need to check legal name because it's checked when parsing constant pool. jrose@866: // But need to make sure it's not an array type. jrose@866: guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY, jrose@866: "Bad interface name in class file %s", CHECK_(nullHandle)); jrose@866: jrose@866: // Call resolve_super so classcircularity is checked jrose@866: klassOop k = SystemDictionary::resolve_super_or_fail(class_name, jrose@866: unresolved_klass, class_loader, protection_domain, jrose@866: false, CHECK_(nullHandle)); jrose@866: interf = KlassHandle(THREAD, k); jrose@866: jrose@909: if (LinkWellKnownClasses) // my super type is well known to me jrose@909: cp->klass_at_put(interface_index, interf()); // eagerly resolve jrose@866: } duke@435: duke@435: if (!Klass::cast(interf())->is_interface()) { duke@435: THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", nullHandle); duke@435: } duke@435: interfaces->obj_at_put(index, interf()); duke@435: } duke@435: duke@435: if (!_need_verify || length <= 1) { duke@435: return interfaces; duke@435: } duke@435: duke@435: // Check if there's any duplicates in interfaces duke@435: ResourceMark rm(THREAD); duke@435: NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, NameSigHash*, HASH_ROW_SIZE); duke@435: initialize_hashtable(interface_names); duke@435: bool dup = false; duke@435: { duke@435: debug_only(No_Safepoint_Verifier nsv;) duke@435: for (index = 0; index < length; index++) { duke@435: klassOop k = (klassOop)interfaces->obj_at(index); coleenp@2497: Symbol* name = instanceKlass::cast(k)->name(); duke@435: // If no duplicates, add (name, NULL) in hashtable interface_names. duke@435: if (!put_after_lookup(name, NULL, interface_names)) { duke@435: dup = true; duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: if (dup) { duke@435: classfile_parse_error("Duplicate interface name in class file %s", duke@435: CHECK_(nullHandle)); duke@435: } duke@435: duke@435: return interfaces; duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, 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 && duke@435: constantvalue_index < cp->length()), duke@435: "Bad initial value index %u in ConstantValue attribute in class file %s", duke@435: constantvalue_index, CHECK); duke@435: constantTag value_type = cp->tag_at(constantvalue_index); duke@435: 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: twisti@1573: guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") duke@435: && (value_type.is_string() || value_type.is_unresolved_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. duke@435: void ClassFileParser::parse_field_attributes(constantPoolHandle cp, duke@435: 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, duke@435: typeArrayHandle* field_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; 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(valid_cp_range(attribute_name_index, cp->length()) && duke@435: cp->tag_at(attribute_name_index).is_utf8(), duke@435: "Invalid field attribute index %u in class file %s", duke@435: attribute_name_index, duke@435: CHECK); coleenp@2497: 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) { duke@435: verify_constantvalue(constantvalue_index, signature_index, cp, 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"); 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); 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; duke@435: *field_annotations = 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); 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 never@3137: NONSTATIC_BYTE , // T_BOOLEAN = 4, never@3137: NONSTATIC_SHORT, // T_CHAR = 5, never@3137: NONSTATIC_WORD, // T_FLOAT = 6, never@3137: NONSTATIC_DOUBLE, // T_DOUBLE = 7, never@3137: NONSTATIC_BYTE, // T_BYTE = 8, never@3137: NONSTATIC_SHORT, // T_SHORT = 9, never@3137: NONSTATIC_WORD, // T_INT = 10, never@3137: NONSTATIC_DOUBLE, // T_LONG = 11, never@3137: NONSTATIC_OOP, // T_OBJECT = 12, never@3137: NONSTATIC_OOP, // T_ARRAY = 13, never@3137: BAD_ALLOCATION_TYPE, // T_VOID = 14, never@3137: BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, never@3137: BAD_ALLOCATION_TYPE, // T_NARROWOOP= 16, never@3137: BAD_ALLOCATION_TYPE, // T_CONFLICT = 17, never@3137: BAD_ALLOCATION_TYPE, // 0 never@3137: BAD_ALLOCATION_TYPE, // 1 never@3137: BAD_ALLOCATION_TYPE, // 2 never@3137: BAD_ALLOCATION_TYPE, // 3 never@3137: STATIC_BYTE , // T_BOOLEAN = 4, never@3137: STATIC_SHORT, // T_CHAR = 5, never@3137: STATIC_WORD, // T_FLOAT = 6, never@3137: STATIC_DOUBLE, // T_DOUBLE = 7, never@3137: STATIC_BYTE, // T_BYTE = 8, never@3137: STATIC_SHORT, // T_SHORT = 9, never@3137: STATIC_WORD, // T_INT = 10, never@3137: STATIC_DOUBLE, // T_LONG = 11, never@3137: STATIC_OOP, // T_OBJECT = 12, never@3137: STATIC_OOP, // T_ARRAY = 13, never@3137: BAD_ALLOCATION_TYPE, // T_VOID = 14, never@3137: BAD_ALLOCATION_TYPE, // T_ADDRESS = 15, never@3137: BAD_ALLOCATION_TYPE, // T_NARROWOOP= 16, never@3137: BAD_ALLOCATION_TYPE, // T_CONFLICT = 17, 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: never@3137: never@3137: typeArrayHandle ClassFileParser::parse_fields(Symbol* class_name, never@3137: constantPoolHandle cp, bool is_interface, never@3137: FieldAllocationCount *fac, never@3137: objArrayHandle* fields_annotations, jiangli@3373: u2* java_fields_count_ptr, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: typeArrayHandle nullHandle; duke@435: cfs->guarantee_more(2, CHECK_(nullHandle)); // 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); never@3137: duke@435: // Tuples of shorts [access, name index, sig index, initial value index, byte offset, generic signature index] never@3137: typeArrayOop new_fields = oopFactory::new_permanent_shortArray((length + num_injected) * FieldInfo::field_slots, CHECK_(nullHandle)); duke@435: typeArrayHandle fields(THREAD, new_fields); duke@435: duke@435: typeArrayHandle field_annotations; duke@435: for (int n = 0; n < length; n++) { duke@435: cfs->guarantee_more(8, CHECK_(nullHandle)); // 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; duke@435: verify_legal_field_modifiers(flags, is_interface, CHECK_(nullHandle)); duke@435: access_flags.set_flags(flags); duke@435: duke@435: u2 name_index = cfs->get_u2_fast(); duke@435: int cp_size = cp->length(); duke@435: check_property( duke@435: valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(), duke@435: "Invalid constant pool index %u for field name in class file %s", duke@435: name_index, CHECK_(nullHandle)); coleenp@2497: Symbol* name = cp->symbol_at(name_index); duke@435: verify_legal_field_name(name, CHECK_(nullHandle)); duke@435: duke@435: u2 signature_index = cfs->get_u2_fast(); duke@435: check_property( duke@435: valid_cp_range(signature_index, cp_size) && duke@435: cp->tag_at(signature_index).is_utf8(), duke@435: "Invalid constant pool index %u for field signature in class file %s", duke@435: signature_index, CHECK_(nullHandle)); coleenp@2497: Symbol* sig = cp->symbol_at(signature_index); duke@435: verify_legal_field_signature(name, sig, CHECK_(nullHandle)); 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(); duke@435: duke@435: u2 attributes_count = cfs->get_u2_fast(); duke@435: if (attributes_count > 0) { duke@435: parse_field_attributes(cp, attributes_count, is_static, signature_index, duke@435: &constantvalue_index, &is_synthetic, duke@435: &generic_signature_index, &field_annotations, duke@435: CHECK_(nullHandle)); duke@435: if (field_annotations.not_null()) { duke@435: if (fields_annotations->is_null()) { duke@435: objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: *fields_annotations = objArrayHandle(THREAD, md); duke@435: } duke@435: (*fields_annotations)->obj_at_put(n, field_annotations()); duke@435: } duke@435: if (is_synthetic) { duke@435: access_flags.set_is_synthetic(); duke@435: } duke@435: } duke@435: never@3137: FieldInfo* field = FieldInfo::from_field_array(fields(), n); never@3137: field->initialize(access_flags.as_short(), never@3137: name_index, never@3137: signature_index, never@3137: constantvalue_index, never@3137: generic_signature_index, never@3137: 0); never@3137: never@3137: BasicType type = cp->basic_type_for_signature_at(signature_index); duke@435: duke@435: // Remember how many oops we encountered and compute allocation type never@3137: FieldAllocationType atype = fac->update(is_static, type); duke@435: duke@435: // The correct offset is computed later (all oop fields will be located together) duke@435: // We temporarily store the allocation type in the offset field never@3137: field->set_offset(atype); never@3137: } never@3137: never@3137: if (num_injected != 0) { never@3137: int index = length; 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++) { never@3137: FieldInfo* f = FieldInfo::from_field_array(fields(), i); never@3137: if (name == cp->symbol_at(f->name_index()) && never@3137: 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 never@3137: FieldInfo* field = FieldInfo::from_field_array(fields(), 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: 0, 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); never@3137: never@3137: // The correct offset is computed later (all oop fields will be located together) never@3137: // We temporarily store the allocation type in the offset field never@3137: field->set_offset(atype); never@3137: index++; never@3137: } never@3137: never@3137: if (index < length + num_injected) { never@3137: // sometimes injected fields already exist in the Java source so never@3137: // the fields array could be too long. In that case trim the never@3137: // fields array. never@3137: new_fields = oopFactory::new_permanent_shortArray(index * FieldInfo::field_slots, CHECK_(nullHandle)); never@3137: for (int i = 0; i < index * FieldInfo::field_slots; i++) { never@3137: new_fields->short_at_put(i, fields->short_at(i)); never@3137: } never@3137: fields = new_fields; never@3137: } 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;) never@3137: 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", duke@435: CHECK_(nullHandle)); 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: duke@435: typeArrayHandle ClassFileParser::parse_exception_table(u4 code_length, duke@435: u4 exception_table_length, duke@435: constantPoolHandle cp, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: typeArrayHandle nullHandle; duke@435: duke@435: // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index] duke@435: typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle)); duke@435: typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh); duke@435: duke@435: int index = 0; duke@435: cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index duke@435: for (unsigned int i = 0; i < exception_table_length; i++) { duke@435: u2 start_pc = cfs->get_u2_fast(); duke@435: u2 end_pc = cfs->get_u2_fast(); duke@435: u2 handler_pc = cfs->get_u2_fast(); duke@435: u2 catch_type_index = cfs->get_u2_fast(); duke@435: // Will check legal target after parsing code array in verifier. duke@435: if (_need_verify) { duke@435: guarantee_property((start_pc < end_pc) && (end_pc <= code_length), duke@435: "Illegal exception table range in class file %s", CHECK_(nullHandle)); duke@435: guarantee_property(handler_pc < code_length, duke@435: "Illegal exception table handler in class file %s", CHECK_(nullHandle)); duke@435: if (catch_type_index != 0) { duke@435: guarantee_property(valid_cp_range(catch_type_index, cp->length()) && jrose@866: is_klass_reference(cp, catch_type_index), duke@435: "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: exception_handlers->int_at_put(index++, start_pc); duke@435: exception_handlers->int_at_put(index++, end_pc); duke@435: exception_handlers->int_at_put(index++, handler_pc); duke@435: exception_handlers->int_at_put(index++, catch_type_index); duke@435: } duke@435: return exception_handlers; 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: duke@435: 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: constantPoolHandle cp, 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: } duke@435: int cp_size = cp->length(); duke@435: guarantee_property( duke@435: valid_cp_range(name_index, cp_size) && duke@435: cp->tag_at(name_index).is_utf8(), duke@435: "Name index %u in %s has bad constant type in class file %s", duke@435: name_index, tbl_name, CHECK_NULL); duke@435: guarantee_property( duke@435: valid_cp_range(descriptor_index, cp_size) && duke@435: cp->tag_at(descriptor_index).is_utf8(), 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@2497: Symbol* name = cp->symbol_at(name_index); coleenp@2497: 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, duke@435: u1* u1_array, u2* u2_array, constantPoolHandle cp, 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); duke@435: guarantee_property(valid_cp_range(class_index, cp->length()) && jrose@866: is_klass_reference(cp, 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: duke@435: typeArrayOop 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: } duke@435: duke@435: typeArrayOop stackmap_data = duke@435: oopFactory::new_permanent_byteArray(code_attribute_length, CHECK_NULL); duke@435: duke@435: stackmap_data->set_length(code_attribute_length); duke@435: memcpy((void*)stackmap_data->byte_at_addr(0), duke@435: (void*)stackmap_table_start, code_attribute_length); duke@435: return stackmap_data; duke@435: } duke@435: duke@435: u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length, duke@435: u4 method_attribute_length, duke@435: constantPoolHandle cp, 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( duke@435: valid_cp_range(checked_exception, cp->length()) && jrose@866: is_klass_reference(cp, 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: duke@435: #define MAX_ARGS_SIZE 255 duke@435: #define MAX_CODE_SIZE 65535 duke@435: #define INITIAL_MAX_LVT_NUMBER 256 duke@435: duke@435: // Note: the parse_method below is big and clunky because all parsing of the code and exceptions duke@435: // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the duke@435: // methodOop to save footprint, so we only know the size of the resulting methodOop 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: duke@435: methodHandle ClassFileParser::parse_method(constantPoolHandle cp, bool is_interface, duke@435: AccessFlags *promoted_flags, duke@435: typeArrayHandle* method_annotations, duke@435: typeArrayHandle* method_parameter_annotations, duke@435: typeArrayHandle* method_default_annotations, 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(); duke@435: int cp_size = cp->length(); duke@435: check_property( duke@435: valid_cp_range(name_index, cp_size) && duke@435: cp->tag_at(name_index).is_utf8(), duke@435: "Illegal constant pool index %u for method name in class file %s", duke@435: name_index, CHECK_(nullHandle)); coleenp@2497: 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( duke@435: valid_cp_range(signature_index, cp_size) && duke@435: cp->tag_at(signature_index).is_utf8(), duke@435: "Illegal constant pool index %u for method signature in class file %s", duke@435: signature_index, CHECK_(nullHandle)); coleenp@2497: 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; duke@435: typeArrayHandle exception_handlers(THREAD, 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; 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 duke@435: typeArrayHandle stackmap_data; duke@435: u2 generic_signature_index = 0; 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; 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( duke@435: valid_cp_range(method_attribute_name_index, cp_size) && duke@435: cp->tag_at(method_attribute_name_index).is_utf8(), duke@435: "Invalid method attribute name index %u in class file %s", duke@435: method_attribute_name_index, CHECK_(nullHandle)); duke@435: coleenp@2497: 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) { duke@435: guarantee_property(!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) { duke@435: exception_handlers = duke@435: parse_exception_table(code_length, exception_table_length, cp, 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); duke@435: check_property(valid_cp_range(code_attribute_name_index, cp_size) && duke@435: cp->tag_at(code_attribute_name_index).is_utf8(), 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 && duke@435: 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 && duke@435: 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: cp, 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 && duke@435: 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: cp, duke@435: &localvariable_type_table_length[lvtt_cnt], duke@435: true, // is LVTT duke@435: CHECK_(nullHandle)); duke@435: lvtt_cnt++; duke@435: } else if (UseSplitVerifier && duke@435: _major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && duke@435: 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: } duke@435: typeArrayOop sm = duke@435: parse_stackmap_table(code_attribute_length, CHECK_(nullHandle)); duke@435: stackmap_data = typeArrayHandle(THREAD, sm); 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, duke@435: cp, CHECK_(nullHandle)); 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"); 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)); 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: duke@435: // All sizing information for a methodOop is finally available, now create it ysr@2533: methodOop m_oop = oopFactory::new_method(code_length, access_flags, linenumber_table_length, ysr@2533: total_lvt_length, checked_exceptions_length, ysr@2533: oopDesc::IsSafeConc, CHECK_(nullHandle)); duke@435: methodHandle m (THREAD, m_oop); duke@435: duke@435: ClassLoadingService::add_class_method_size(m_oop->size()*HeapWordSize); duke@435: duke@435: // Fill in information from fixed part (access_flags already set) duke@435: m->set_constants(cp()); duke@435: m->set_name_index(name_index); duke@435: m->set_signature_index(signature_index); duke@435: m->set_generic_signature_index(generic_signature_index); duke@435: #ifdef CC_INTERP duke@435: // hmm is there a gc issue here?? duke@435: 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); duke@435: m->constMethod()->set_stackmap_data(stackmap_data()); duke@435: duke@435: /** duke@435: * The exception_table field is the flag used to indicate duke@435: * that the methodOop and it's associated constMethodOop are partially duke@435: * initialized and thus are exempt from pre/post GC verification. Once duke@435: * the field is set, the oops are considered fully initialized so make duke@435: * sure that the oops can pass verification when this field is set. duke@435: */ duke@435: m->set_exception_table(exception_handlers()); 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: 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: duke@435: /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. duke@435: * duke@435: * Rules for LVT's and LVTT's are: duke@435: * - There can be any number of LVT's and LVTT's. duke@435: * - If there are n LVT's, it is the same as if there was just duke@435: * one LVT containing all the entries from the n LVT's. duke@435: * - There may be no more than one LVT entry per local variable. duke@435: * Two LVT entries are 'equal' if these fields are the same: duke@435: * start_pc, length, name, slot duke@435: * - There may be no more than one LVTT entry per each LVT entry. duke@435: * Each LVTT entry has to match some LVT entry. duke@435: * - HotSpot internal LVT keeps natural ordering of class file LVT entries. duke@435: */ duke@435: if (total_lvt_length > 0) { duke@435: int tbl_no, idx; duke@435: duke@435: promoted_flags->set_has_localvariable_table(); duke@435: duke@435: LVT_Hash** lvt_Hash = NEW_RESOURCE_ARRAY(LVT_Hash*, HASH_ROW_SIZE); duke@435: initialize_hashtable(lvt_Hash); duke@435: duke@435: // To fill LocalVariableTable in duke@435: Classfile_LVT_Element* cf_lvt; duke@435: LocalVariableTableElement* lvt = m->localvariable_table_start(); duke@435: duke@435: for (tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { duke@435: cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; duke@435: for (idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { duke@435: copy_lvt_element(&cf_lvt[idx], lvt); duke@435: // If no duplicates, add LVT elem in hashtable lvt_Hash. duke@435: if (LVT_put_after_lookup(lvt, lvt_Hash) == false duke@435: && _need_verify duke@435: && _major_version >= JAVA_1_5_VERSION ) { duke@435: clear_hashtable(lvt_Hash); duke@435: classfile_parse_error("Duplicated LocalVariableTable attribute " duke@435: "entry for '%s' in class file %s", duke@435: cp->symbol_at(lvt->name_cp_index)->as_utf8(), duke@435: CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // To merge LocalVariableTable and LocalVariableTypeTable duke@435: Classfile_LVT_Element* cf_lvtt; duke@435: LocalVariableTableElement lvtt_elem; duke@435: duke@435: for (tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { duke@435: cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; duke@435: for (idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { duke@435: copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); duke@435: int index = hash(&lvtt_elem); duke@435: LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash); duke@435: if (entry == NULL) { duke@435: if (_need_verify) { duke@435: clear_hashtable(lvt_Hash); duke@435: classfile_parse_error("LVTT entry for '%s' in class file %s " duke@435: "does not match any LVT entry", duke@435: cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), duke@435: CHECK_(nullHandle)); duke@435: } duke@435: } else if (entry->_elem->signature_cp_index != 0 && _need_verify) { duke@435: clear_hashtable(lvt_Hash); duke@435: classfile_parse_error("Duplicated LocalVariableTypeTable attribute " duke@435: "entry for '%s' in class file %s", duke@435: cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), duke@435: CHECK_(nullHandle)); duke@435: } else { duke@435: // to add generic signatures into LocalVariableTable duke@435: entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index; duke@435: } duke@435: } duke@435: } duke@435: clear_hashtable(lvt_Hash); duke@435: } duke@435: duke@435: *method_annotations = 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_(nullHandle)); duke@435: *method_parameter_annotations = assemble_annotations(runtime_visible_parameter_annotations, duke@435: runtime_visible_parameter_annotations_length, duke@435: runtime_invisible_parameter_annotations, duke@435: runtime_invisible_parameter_annotations_length, duke@435: CHECK_(nullHandle)); duke@435: *method_default_annotations = assemble_annotations(annotation_default, duke@435: annotation_default_length, duke@435: NULL, duke@435: 0, duke@435: CHECK_(nullHandle)); 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: twisti@2698: if (EnableInvokeDynamic && (m->is_method_handle_invoke() || jrose@1862: m->is_method_handle_adapter())) { jrose@1145: THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(), jrose@1145: "Method handle invokers must be defined internally to the VM", nullHandle); jrose@1145: } jrose@1145: 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: duke@435: objArrayHandle ClassFileParser::parse_methods(constantPoolHandle cp, bool is_interface, duke@435: AccessFlags* promoted_flags, duke@435: bool* has_final_method, duke@435: objArrayOop* methods_annotations_oop, duke@435: objArrayOop* methods_parameter_annotations_oop, duke@435: objArrayOop* methods_default_annotations_oop, duke@435: TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: objArrayHandle nullHandle; duke@435: typeArrayHandle method_annotations; duke@435: typeArrayHandle method_parameter_annotations; duke@435: typeArrayHandle method_default_annotations; duke@435: cfs->guarantee_more(2, CHECK_(nullHandle)); // length duke@435: u2 length = cfs->get_u2_fast(); duke@435: if (length == 0) { duke@435: return objArrayHandle(THREAD, Universe::the_empty_system_obj_array()); duke@435: } else { duke@435: objArrayOop m = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: objArrayHandle methods(THREAD, m); duke@435: HandleMark hm(THREAD); duke@435: objArrayHandle methods_annotations; duke@435: objArrayHandle methods_parameter_annotations; duke@435: objArrayHandle methods_default_annotations; duke@435: for (int index = 0; index < length; index++) { duke@435: methodHandle method = parse_method(cp, is_interface, duke@435: promoted_flags, duke@435: &method_annotations, duke@435: &method_parameter_annotations, duke@435: &method_default_annotations, duke@435: CHECK_(nullHandle)); duke@435: if (method->is_final()) { duke@435: *has_final_method = true; duke@435: } duke@435: methods->obj_at_put(index, method()); duke@435: if (method_annotations.not_null()) { duke@435: if (methods_annotations.is_null()) { duke@435: objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: methods_annotations = objArrayHandle(THREAD, md); duke@435: } duke@435: methods_annotations->obj_at_put(index, method_annotations()); duke@435: } duke@435: if (method_parameter_annotations.not_null()) { duke@435: if (methods_parameter_annotations.is_null()) { duke@435: objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: methods_parameter_annotations = objArrayHandle(THREAD, md); duke@435: } duke@435: methods_parameter_annotations->obj_at_put(index, method_parameter_annotations()); duke@435: } duke@435: if (method_default_annotations.not_null()) { duke@435: if (methods_default_annotations.is_null()) { duke@435: objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); duke@435: methods_default_annotations = objArrayHandle(THREAD, md); duke@435: } duke@435: methods_default_annotations->obj_at_put(index, method_default_annotations()); duke@435: } duke@435: } 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++) { duke@435: methodOop m = (methodOop)methods->obj_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", duke@435: CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: duke@435: *methods_annotations_oop = methods_annotations(); duke@435: *methods_parameter_annotations_oop = methods_parameter_annotations(); duke@435: *methods_default_annotations_oop = methods_default_annotations(); duke@435: duke@435: return methods; duke@435: } duke@435: } duke@435: duke@435: duke@435: typeArrayHandle ClassFileParser::sort_methods(objArrayHandle methods, duke@435: objArrayHandle methods_annotations, duke@435: objArrayHandle methods_parameter_annotations, duke@435: objArrayHandle methods_default_annotations, duke@435: TRAPS) { duke@435: typeArrayHandle nullHandle; duke@435: 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. duke@435: // We temporarily use the vtable_index field in the methodOop 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++) { duke@435: methodOop m = methodOop(methods->obj_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 duke@435: methodOopDesc::sort_methods(methods(), duke@435: methods_annotations(), duke@435: methods_parameter_annotations(), duke@435: methods_default_annotations()); duke@435: 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) { duke@435: typeArrayOop new_ordering = oopFactory::new_permanent_intArray(length, CHECK_(nullHandle)); duke@435: typeArrayHandle method_ordering(THREAD, new_ordering); duke@435: for (int index = 0; index < length; index++) { duke@435: methodOop m = methodOop(methods->obj_at(index)); duke@435: int old_index = m->vtable_index(); duke@435: assert(old_index >= 0 && old_index < length, "invalid method index"); duke@435: method_ordering->int_at_put(index, old_index); duke@435: m->set_vtable_index(methodOopDesc::invalid_vtable_index); duke@435: } duke@435: return method_ordering; duke@435: } else { duke@435: return typeArrayHandle(THREAD, Universe::the_empty_int_array()); duke@435: } duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, 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( duke@435: valid_cp_range(sourcefile_index, cp->length()) && duke@435: cp->tag_at(sourcefile_index).is_utf8(), duke@435: "Invalid SourceFile attribute at constant pool index %u in class file %s", duke@435: sourcefile_index, CHECK); duke@435: k->set_source_file_name(cp->symbol_at(sourcefile_index)); duke@435: } duke@435: duke@435: duke@435: duke@435: void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, duke@435: instanceKlassHandle k, duke@435: 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()) { duke@435: // Optimistically assume that only 1 byte UTF format is used duke@435: // (common case) coleenp@2497: TempNewSymbol sde_symbol = SymbolTable::new_symbol((const char*)sde_buffer, length, CHECK); duke@435: k->set_source_debug_extension(sde_symbol); coleenp@2497: // Note that set_source_debug_extension() increments the reference count coleenp@2497: // for its copy of the Symbol*, so use a TempNewSymbol here. 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 duke@435: u2 ClassFileParser::parse_classfile_inner_classes_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: cfs->guarantee_more(2, CHECK_0); // length duke@435: u2 length = cfs->get_u2_fast(); duke@435: duke@435: // 4-tuples of shorts [inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags] duke@435: typeArrayOop ic = oopFactory::new_permanent_shortArray(length*4, CHECK_0); duke@435: typeArrayHandle inner_classes(THREAD, ic); duke@435: int index = 0; duke@435: 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 || duke@435: (valid_cp_range(inner_class_info_index, cp_size) && jrose@866: is_klass_reference(cp, 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 || duke@435: (valid_cp_range(outer_class_info_index, cp_size) && jrose@866: is_klass_reference(cp, 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( duke@435: inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) && duke@435: cp->tag_at(inner_name_index).is_utf8()), 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: duke@435: inner_classes->short_at_put(index++, inner_class_info_index); duke@435: inner_classes->short_at_put(index++, outer_class_info_index); duke@435: inner_classes->short_at_put(index++, inner_name_index); duke@435: inner_classes->short_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) { duke@435: for(int i = 0; i < inner_classes->length(); i += 4) { duke@435: for(int j = i + 4; j < inner_classes->length(); j += 4) { duke@435: guarantee_property((inner_classes->ushort_at(i) != inner_classes->ushort_at(j) || duke@435: inner_classes->ushort_at(i+1) != inner_classes->ushort_at(j+1) || duke@435: inner_classes->ushort_at(i+2) != inner_classes->ushort_at(j+2) || duke@435: inner_classes->ushort_at(i+3) != inner_classes->ushort_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: duke@435: // Update instanceKlass with inner class info. duke@435: k->set_inner_classes(inner_classes()); duke@435: return length; duke@435: } duke@435: duke@435: void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { duke@435: k->set_is_synthetic(); duke@435: } duke@435: duke@435: void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: u2 signature_index = cfs->get_u2(CHECK); duke@435: check_property( duke@435: valid_cp_range(signature_index, cp->length()) && duke@435: cp->tag_at(signature_index).is_utf8(), duke@435: "Invalid constant pool index %u in Signature attribute in class file %s", duke@435: signature_index, CHECK); duke@435: k->set_generic_signature(cp->symbol_at(signature_index)); duke@435: } duke@435: jrose@2353: void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, instanceKlassHandle k, jrose@2353: 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: jrose@2353: typeArrayOop operands_oop = oopFactory::new_permanent_intArray(index_size + operand_count, CHECK); jrose@2353: typeArrayHandle operands(THREAD, operands_oop); jrose@2353: operands_oop = NULL; // tidy jrose@2353: jrose@2353: int operand_fill_index = index_size; jrose@2353: 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. jrose@2353: assert(constantPoolOopDesc::operand_offset_at(operands(), n) == 0, ""); jrose@2353: constantPoolOopDesc::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) && jrose@2353: 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); jrose@2353: operands->short_at_put(operand_fill_index++, bootstrap_method_index); jrose@2353: operands->short_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) && twisti@2408: 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); twisti@2408: operands->short_at_put(operand_fill_index++, argument_index); jrose@2353: } jrose@2353: } jrose@2353: jrose@2353: assert(operand_fill_index == operands()->length(), "exact fill"); jrose@2353: assert(constantPoolOopDesc::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: cp->set_operands(operands()); jrose@2353: } jrose@2353: jrose@2353: duke@435: void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { duke@435: ClassFileStream* cfs = stream(); duke@435: // Set inner classes attribute to default sentinel duke@435: k->set_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; 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( duke@435: valid_cp_range(attribute_name_index, cp->length()) && duke@435: cp->tag_at(attribute_name_index).is_utf8(), duke@435: "Attribute name has bad constant pool index %u in class file %s", duke@435: attribute_name_index, CHECK); coleenp@2497: 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: } duke@435: parse_classfile_sourcefile_attribute(cp, k, CHECK); duke@435: } else if (tag == vmSymbols::tag_source_debug_extension()) { duke@435: // Check for SourceDebugExtension tag duke@435: parse_classfile_source_debug_extension_attribute(cp, k, (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: } duke@435: u2 num_of_classes = parse_classfile_inner_classes_attribute(cp, k, CHECK); duke@435: if (_need_verify && _major_version >= JAVA_1_5_VERSION) { duke@435: guarantee_property(attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, duke@435: "Wrong InnerClasses attribute length in class file %s", CHECK); duke@435: } 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: } duke@435: parse_classfile_synthetic_attribute(cp, k, 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: } duke@435: parse_classfile_signature_attribute(cp, k, 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"); 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 duke@435: u2 class_index = cfs->get_u2_fast(); duke@435: u2 method_index = cfs->get_u2_fast(); duke@435: if (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 duke@435: if (!cp->is_within_bounds(class_index) || jrose@866: !is_klass_reference(cp, class_index)) { duke@435: classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK); duke@435: } duke@435: if (method_index != 0 && duke@435: (!cp->is_within_bounds(method_index) || duke@435: !cp->tag_at(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: } duke@435: k->set_enclosing_method_indices(class_index, method_index); 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; jrose@2353: parse_classfile_bootstrap_methods_attribute(cp, k, attribute_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: } duke@435: typeArrayHandle annotations = 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); duke@435: k->set_class_annotations(annotations()); jrose@2353: 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: duke@435: duke@435: typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, duke@435: int runtime_visible_annotations_length, duke@435: u1* runtime_invisible_annotations, duke@435: int runtime_invisible_annotations_length, TRAPS) { duke@435: typeArrayHandle annotations; duke@435: if (runtime_visible_annotations != NULL || duke@435: runtime_invisible_annotations != NULL) { duke@435: typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length + duke@435: runtime_invisible_annotations_length, CHECK_(annotations)); duke@435: annotations = typeArrayHandle(THREAD, anno); duke@435: if (runtime_visible_annotations != NULL) { duke@435: memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length); duke@435: } duke@435: if (runtime_invisible_annotations != NULL) { duke@435: memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length); duke@435: } duke@435: } duke@435: return annotations; duke@435: } duke@435: duke@435: coleenp@2497: instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name, duke@435: Handle class_loader, 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) { 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. duke@435: unsigned char *cached_class_file_bytes = NULL; duke@435: jint cached_class_file_length; 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: duke@435: _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false; jrose@2353: _max_bootstrap_specifier_index = -1; 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)()); dcubed@3380: cached_class_file_bytes = dcubed@3380: ikh_class_being_redefined->get_cached_class_file_bytes(); dcubed@3380: cached_class_file_length = dcubed@3380: ikh_class_being_redefined->get_cached_class_file_len(); 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: duke@435: JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain, duke@435: &ptr, &end_ptr, duke@435: &cached_class_file_bytes, duke@435: &cached_class_file_length); 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@3682: constantPoolHandle cp = parse_constant_pool(class_loader, CHECK_(nullHandle)); coleenp@2497: ConstantPoolCleaner error_handler(cp); // set constant pool to be cleaned up. 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: instanceKlassHandle super_klass; 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: duke@435: klassOop 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) { coleenp@2497: tty->print("[Loading %s", name->as_klass_external_name()); 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(); duke@435: if (super_class_index == 0) { coleenp@2497: check_property(class_name == vmSymbols::java_lang_Object(), duke@435: "Invalid superclass index %u in class file %s", duke@435: super_class_index, duke@435: CHECK_(nullHandle)); duke@435: } else { duke@435: check_property(valid_cp_range(super_class_index, cp_size) && jrose@866: is_klass_reference(cp, super_class_index), duke@435: "Invalid superclass index %u in class file %s", duke@435: super_class_index, duke@435: CHECK_(nullHandle)); duke@435: // The class name should be legal because it is checked when parsing constant pool. duke@435: // However, make sure it is not an array type. jrose@866: bool is_array = false; jrose@866: if (cp->tag_at(super_class_index).is_klass()) { jrose@866: super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index)); jrose@866: if (_need_verify) jrose@866: is_array = super_klass->oop_is_array(); jrose@866: } else if (_need_verify) { jrose@866: is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY); jrose@866: } duke@435: if (_need_verify) { jrose@866: guarantee_property(!is_array, duke@435: "Bad superclass name in class file %s", CHECK_(nullHandle)); duke@435: } duke@435: } duke@435: duke@435: // Interfaces duke@435: u2 itfs_len = cfs->get_u2_fast(); duke@435: objArrayHandle local_interfaces; duke@435: if (itfs_len == 0) { duke@435: local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array()); duke@435: } else { mchung@1310: local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle)); duke@435: } duke@435: jiangli@3373: u2 java_fields_count = 0; duke@435: // Fields (offsets are filled in later) never@3137: FieldAllocationCount fac; duke@435: objArrayHandle fields_annotations; never@3137: typeArrayHandle fields = parse_fields(class_name, cp, access_flags.is_interface(), &fac, &fields_annotations, never@3137: &java_fields_count, never@3137: CHECK_(nullHandle)); duke@435: // Methods duke@435: bool has_final_method = false; duke@435: AccessFlags promoted_flags; duke@435: promoted_flags.set_flags(0); duke@435: // These need to be oop pointers because they are allocated lazily duke@435: // inside parse_methods inside a nested HandleMark duke@435: objArrayOop methods_annotations_oop = NULL; duke@435: objArrayOop methods_parameter_annotations_oop = NULL; duke@435: objArrayOop methods_default_annotations_oop = NULL; duke@435: objArrayHandle methods = parse_methods(cp, access_flags.is_interface(), duke@435: &promoted_flags, duke@435: &has_final_method, duke@435: &methods_annotations_oop, duke@435: &methods_parameter_annotations_oop, duke@435: &methods_default_annotations_oop, duke@435: CHECK_(nullHandle)); duke@435: duke@435: objArrayHandle methods_annotations(THREAD, methods_annotations_oop); duke@435: objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop); duke@435: objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop); duke@435: 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: } duke@435: klassOop k = SystemDictionary::resolve_super_or_fail(class_name, duke@435: sk, duke@435: class_loader, duke@435: protection_domain, duke@435: true, duke@435: CHECK_(nullHandle)); mchung@1310: duke@435: KlassHandle kh (THREAD, k); duke@435: super_klass = instanceKlassHandle(THREAD, kh()); jrose@909: if (LinkWellKnownClasses) // my super class is well known to me jrose@909: cp->klass_at_put(super_class_index, super_klass()); // eagerly resolve jrose@866: } jrose@866: if (super_klass.not_null()) { 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: duke@435: // Compute the transitive list of all unique interfaces implemented by this class duke@435: objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle)); duke@435: duke@435: // sort methods duke@435: typeArrayHandle method_ordering = sort_methods(methods, duke@435: methods_annotations, duke@435: methods_parameter_annotations, duke@435: methods_default_annotations, duke@435: CHECK_(nullHandle)); 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: duke@435: klassVtable::compute_vtable_size_and_num_mirandas(vtable_size, duke@435: num_miranda_methods, duke@435: super_klass(), duke@435: methods(), duke@435: access_flags, acorn@1087: class_loader, acorn@1087: class_name, acorn@1087: local_interfaces(), acorn@1087: CHECK_(nullHandle)); duke@435: duke@435: // Size of Java itable (in words) duke@435: itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(transitive_interfaces); duke@435: duke@435: // Field size and offset computation duke@435: int nonstatic_field_size = super_klass() == NULL ? 0 : super_klass->nonstatic_field_size(); duke@435: #ifndef PRODUCT duke@435: int orig_nonstatic_field_size = 0; duke@435: #endif duke@435: int static_field_size = 0; duke@435: int next_static_oop_offset; duke@435: int next_static_double_offset; duke@435: int next_static_word_offset; duke@435: int next_static_short_offset; duke@435: int next_static_byte_offset; duke@435: int next_static_type_offset; duke@435: int next_nonstatic_oop_offset; duke@435: int next_nonstatic_double_offset; duke@435: int next_nonstatic_word_offset; duke@435: int next_nonstatic_short_offset; duke@435: int next_nonstatic_byte_offset; duke@435: int next_nonstatic_type_offset; duke@435: int first_nonstatic_oop_offset; duke@435: int first_nonstatic_field_offset; duke@435: int next_nonstatic_field_offset; duke@435: duke@435: // Calculate the starting byte offsets never@2658: next_static_oop_offset = instanceMirrorKlass::offset_of_static_fields(); duke@435: next_static_double_offset = next_static_oop_offset + never@3137: (fac.count[STATIC_OOP] * heapOopSize); never@3137: if ( fac.count[STATIC_DOUBLE] && duke@435: (Universe::field_type_should_be_aligned(T_DOUBLE) || duke@435: Universe::field_type_should_be_aligned(T_LONG)) ) { duke@435: next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong); duke@435: } duke@435: duke@435: next_static_word_offset = next_static_double_offset + never@3137: (fac.count[STATIC_DOUBLE] * BytesPerLong); duke@435: next_static_short_offset = next_static_word_offset + never@3137: (fac.count[STATIC_WORD] * BytesPerInt); duke@435: next_static_byte_offset = next_static_short_offset + never@3137: (fac.count[STATIC_SHORT] * BytesPerShort); duke@435: next_static_type_offset = align_size_up((next_static_byte_offset + never@3137: fac.count[STATIC_BYTE] ), wordSize ); duke@435: static_field_size = (next_static_type_offset - duke@435: next_static_oop_offset) / wordSize; never@2658: kvn@600: first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() + kvn@600: nonstatic_field_size * heapOopSize; duke@435: next_nonstatic_field_offset = first_nonstatic_field_offset; duke@435: never@3137: unsigned int nonstatic_double_count = fac.count[NONSTATIC_DOUBLE]; never@3137: unsigned int nonstatic_word_count = fac.count[NONSTATIC_WORD]; never@3137: unsigned int nonstatic_short_count = fac.count[NONSTATIC_SHORT]; never@3137: unsigned int nonstatic_byte_count = fac.count[NONSTATIC_BYTE]; never@3137: unsigned int nonstatic_oop_count = fac.count[NONSTATIC_OOP]; duke@435: coleenp@548: bool super_has_nonstatic_fields = coleenp@548: (super_klass() != NULL && super_klass->has_nonstatic_fields()); coleenp@548: bool has_nonstatic_fields = super_has_nonstatic_fields || coleenp@548: ((nonstatic_double_count + nonstatic_word_count + coleenp@548: nonstatic_short_count + nonstatic_byte_count + coleenp@548: nonstatic_oop_count) != 0); coleenp@548: coleenp@548: jcoomes@1374: // Prepare list of oops for oop map generation. jcoomes@1374: int* nonstatic_oop_offsets; jcoomes@1374: unsigned int* nonstatic_oop_counts; jcoomes@1374: unsigned int nonstatic_oop_map_count = 0; duke@435: duke@435: nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD( jcoomes@1374: THREAD, int, nonstatic_oop_count + 1); jcoomes@1373: nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD( jcoomes@1374: THREAD, unsigned int, nonstatic_oop_count + 1); duke@435: never@3137: first_nonstatic_oop_offset = 0; // will be set for first oop field duke@435: duke@435: #ifndef PRODUCT duke@435: if( PrintCompactFieldsSavings ) { duke@435: next_nonstatic_double_offset = next_nonstatic_field_offset + coleenp@548: (nonstatic_oop_count * heapOopSize); duke@435: if ( nonstatic_double_count > 0 ) { duke@435: next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong); duke@435: } duke@435: next_nonstatic_word_offset = next_nonstatic_double_offset + duke@435: (nonstatic_double_count * BytesPerLong); duke@435: next_nonstatic_short_offset = next_nonstatic_word_offset + duke@435: (nonstatic_word_count * BytesPerInt); duke@435: next_nonstatic_byte_offset = next_nonstatic_short_offset + duke@435: (nonstatic_short_count * BytesPerShort); duke@435: next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset + kvn@600: nonstatic_byte_count ), heapOopSize ); duke@435: orig_nonstatic_field_size = nonstatic_field_size + kvn@600: ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize); duke@435: } duke@435: #endif duke@435: bool compact_fields = CompactFields; duke@435: int allocation_style = FieldsAllocationStyle; kvn@1801: if( allocation_style < 0 || allocation_style > 2 ) { // Out of range? kvn@1801: assert(false, "0 <= FieldsAllocationStyle <= 2"); duke@435: allocation_style = 1; // Optimistic duke@435: } duke@435: duke@435: // The next classes have predefined hard-coded fields offsets duke@435: // (see in JavaClasses::compute_hard_coded_offsets()). duke@435: // Use default fields allocation order for them. duke@435: if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() && coleenp@2497: (class_name == vmSymbols::java_lang_AssertionStatusDirectives() || coleenp@2497: class_name == vmSymbols::java_lang_Class() || coleenp@2497: class_name == vmSymbols::java_lang_ClassLoader() || coleenp@2497: class_name == vmSymbols::java_lang_ref_Reference() || coleenp@2497: class_name == vmSymbols::java_lang_ref_SoftReference() || coleenp@2497: class_name == vmSymbols::java_lang_StackTraceElement() || coleenp@2497: class_name == vmSymbols::java_lang_String() || coleenp@2497: class_name == vmSymbols::java_lang_Throwable() || coleenp@2497: class_name == vmSymbols::java_lang_Boolean() || coleenp@2497: class_name == vmSymbols::java_lang_Character() || coleenp@2497: class_name == vmSymbols::java_lang_Float() || coleenp@2497: class_name == vmSymbols::java_lang_Double() || coleenp@2497: class_name == vmSymbols::java_lang_Byte() || coleenp@2497: class_name == vmSymbols::java_lang_Short() || coleenp@2497: class_name == vmSymbols::java_lang_Integer() || coleenp@2497: class_name == vmSymbols::java_lang_Long())) { duke@435: allocation_style = 0; // Allocate oops first duke@435: compact_fields = false; // Don't compact fields duke@435: } duke@435: duke@435: if( allocation_style == 0 ) { duke@435: // Fields order: oops, longs/doubles, ints, shorts/chars, bytes duke@435: next_nonstatic_oop_offset = next_nonstatic_field_offset; duke@435: next_nonstatic_double_offset = next_nonstatic_oop_offset + coleenp@548: (nonstatic_oop_count * heapOopSize); duke@435: } else if( allocation_style == 1 ) { duke@435: // Fields order: longs/doubles, ints, shorts/chars, bytes, oops duke@435: next_nonstatic_double_offset = next_nonstatic_field_offset; kvn@1801: } else if( allocation_style == 2 ) { kvn@1801: // Fields allocation: oops fields in super and sub classes are together. kvn@1801: if( nonstatic_field_size > 0 && super_klass() != NULL && kvn@1801: super_klass->nonstatic_oop_map_size() > 0 ) { kvn@2983: int map_count = super_klass->nonstatic_oop_map_count(); kvn@1801: OopMapBlock* first_map = super_klass->start_of_nonstatic_oop_maps(); kvn@2983: OopMapBlock* last_map = first_map + map_count - 1; kvn@1801: int next_offset = last_map->offset() + (last_map->count() * heapOopSize); kvn@1801: if (next_offset == next_nonstatic_field_offset) { kvn@1801: allocation_style = 0; // allocate oops first kvn@1801: next_nonstatic_oop_offset = next_nonstatic_field_offset; kvn@1801: next_nonstatic_double_offset = next_nonstatic_oop_offset + kvn@1801: (nonstatic_oop_count * heapOopSize); kvn@1801: } kvn@1801: } kvn@1801: if( allocation_style == 2 ) { kvn@1801: allocation_style = 1; // allocate oops last kvn@1801: next_nonstatic_double_offset = next_nonstatic_field_offset; kvn@1801: } duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: int nonstatic_oop_space_count = 0; duke@435: int nonstatic_word_space_count = 0; duke@435: int nonstatic_short_space_count = 0; duke@435: int nonstatic_byte_space_count = 0; duke@435: int nonstatic_oop_space_offset; duke@435: int nonstatic_word_space_offset; duke@435: int nonstatic_short_space_offset; duke@435: int nonstatic_byte_space_offset; duke@435: kvn@600: if( nonstatic_double_count > 0 ) { kvn@600: int offset = next_nonstatic_double_offset; duke@435: next_nonstatic_double_offset = align_size_up(offset, BytesPerLong); duke@435: if( compact_fields && offset != next_nonstatic_double_offset ) { duke@435: // Allocate available fields into the gap before double field. duke@435: int length = next_nonstatic_double_offset - offset; duke@435: assert(length == BytesPerInt, ""); duke@435: nonstatic_word_space_offset = offset; duke@435: if( nonstatic_word_count > 0 ) { duke@435: nonstatic_word_count -= 1; duke@435: nonstatic_word_space_count = 1; // Only one will fit duke@435: length -= BytesPerInt; duke@435: offset += BytesPerInt; duke@435: } duke@435: nonstatic_short_space_offset = offset; duke@435: while( length >= BytesPerShort && nonstatic_short_count > 0 ) { duke@435: nonstatic_short_count -= 1; duke@435: nonstatic_short_space_count += 1; duke@435: length -= BytesPerShort; duke@435: offset += BytesPerShort; duke@435: } duke@435: nonstatic_byte_space_offset = offset; duke@435: while( length > 0 && nonstatic_byte_count > 0 ) { duke@435: nonstatic_byte_count -= 1; duke@435: nonstatic_byte_space_count += 1; duke@435: length -= 1; duke@435: } duke@435: // Allocate oop field in the gap if there are no other fields for that. duke@435: nonstatic_oop_space_offset = offset; kvn@600: if( length >= heapOopSize && nonstatic_oop_count > 0 && duke@435: allocation_style != 0 ) { // when oop fields not first duke@435: nonstatic_oop_count -= 1; duke@435: nonstatic_oop_space_count = 1; // Only one will fit coleenp@548: length -= heapOopSize; coleenp@548: offset += heapOopSize; duke@435: } duke@435: } duke@435: } duke@435: duke@435: next_nonstatic_word_offset = next_nonstatic_double_offset + duke@435: (nonstatic_double_count * BytesPerLong); duke@435: next_nonstatic_short_offset = next_nonstatic_word_offset + duke@435: (nonstatic_word_count * BytesPerInt); duke@435: next_nonstatic_byte_offset = next_nonstatic_short_offset + duke@435: (nonstatic_short_count * BytesPerShort); duke@435: duke@435: int notaligned_offset; duke@435: if( allocation_style == 0 ) { duke@435: notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count; duke@435: } else { // allocation_style == 1 duke@435: next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count; duke@435: if( nonstatic_oop_count > 0 ) { coleenp@548: next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize); duke@435: } coleenp@548: notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize); duke@435: } kvn@600: next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize ); duke@435: nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset kvn@600: - first_nonstatic_field_offset)/heapOopSize); duke@435: duke@435: // Iterate over fields again and compute correct offsets. duke@435: // The field allocation type was temporarily stored in the offset slot. duke@435: // oop fields are located before non-oop fields (static and non-static). never@3137: for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { duke@435: int real_offset; never@3137: FieldAllocationType atype = (FieldAllocationType) fs.offset(); duke@435: switch (atype) { duke@435: case STATIC_OOP: duke@435: real_offset = next_static_oop_offset; coleenp@548: next_static_oop_offset += heapOopSize; duke@435: break; duke@435: case STATIC_BYTE: duke@435: real_offset = next_static_byte_offset; duke@435: next_static_byte_offset += 1; duke@435: break; duke@435: case STATIC_SHORT: duke@435: real_offset = next_static_short_offset; duke@435: next_static_short_offset += BytesPerShort; duke@435: break; duke@435: case STATIC_WORD: duke@435: real_offset = next_static_word_offset; duke@435: next_static_word_offset += BytesPerInt; duke@435: break; duke@435: case STATIC_DOUBLE: duke@435: real_offset = next_static_double_offset; duke@435: next_static_double_offset += BytesPerLong; duke@435: break; duke@435: case NONSTATIC_OOP: duke@435: if( nonstatic_oop_space_count > 0 ) { duke@435: real_offset = nonstatic_oop_space_offset; coleenp@548: nonstatic_oop_space_offset += heapOopSize; duke@435: nonstatic_oop_space_count -= 1; duke@435: } else { duke@435: real_offset = next_nonstatic_oop_offset; coleenp@548: next_nonstatic_oop_offset += heapOopSize; duke@435: } duke@435: // Update oop maps duke@435: if( nonstatic_oop_map_count > 0 && duke@435: nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == jcoomes@1374: real_offset - jcoomes@1374: int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * jcoomes@1374: heapOopSize ) { duke@435: // Extend current oop map jcoomes@1373: nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; duke@435: } else { duke@435: // Create new oop map jcoomes@1374: nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; jcoomes@1373: nonstatic_oop_counts [nonstatic_oop_map_count] = 1; duke@435: nonstatic_oop_map_count += 1; duke@435: if( first_nonstatic_oop_offset == 0 ) { // Undefined duke@435: first_nonstatic_oop_offset = real_offset; duke@435: } duke@435: } duke@435: break; duke@435: case NONSTATIC_BYTE: duke@435: if( nonstatic_byte_space_count > 0 ) { duke@435: real_offset = nonstatic_byte_space_offset; duke@435: nonstatic_byte_space_offset += 1; duke@435: nonstatic_byte_space_count -= 1; duke@435: } else { duke@435: real_offset = next_nonstatic_byte_offset; duke@435: next_nonstatic_byte_offset += 1; duke@435: } duke@435: break; duke@435: case NONSTATIC_SHORT: duke@435: if( nonstatic_short_space_count > 0 ) { duke@435: real_offset = nonstatic_short_space_offset; duke@435: nonstatic_short_space_offset += BytesPerShort; duke@435: nonstatic_short_space_count -= 1; duke@435: } else { duke@435: real_offset = next_nonstatic_short_offset; duke@435: next_nonstatic_short_offset += BytesPerShort; duke@435: } duke@435: break; duke@435: case NONSTATIC_WORD: duke@435: if( nonstatic_word_space_count > 0 ) { duke@435: real_offset = nonstatic_word_space_offset; duke@435: nonstatic_word_space_offset += BytesPerInt; duke@435: nonstatic_word_space_count -= 1; duke@435: } else { duke@435: real_offset = next_nonstatic_word_offset; duke@435: next_nonstatic_word_offset += BytesPerInt; duke@435: } duke@435: break; duke@435: case NONSTATIC_DOUBLE: duke@435: real_offset = next_nonstatic_double_offset; duke@435: next_nonstatic_double_offset += BytesPerLong; duke@435: break; duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } never@3137: fs.set_offset(real_offset); duke@435: } duke@435: duke@435: // Size of instances duke@435: int instance_size; duke@435: kvn@600: next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize ); duke@435: instance_size = align_object_size(next_nonstatic_type_offset / wordSize); duke@435: kvn@600: assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value"); duke@435: jcoomes@1373: // Number of non-static oop map blocks allocated at end of klass. jcoomes@1374: const unsigned int total_oop_map_count = jcoomes@1374: compute_oop_map_count(super_klass, nonstatic_oop_map_count, jcoomes@1374: first_nonstatic_oop_offset); duke@435: 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: duke@435: // We can now create the basic klassOop for this klass never@2658: klassOop ik = oopFactory::new_instanceKlass(name, vtable_size, itable_size, jcoomes@1374: static_field_size, jcoomes@1374: total_oop_map_count, jcoomes@1374: rt, CHECK_(nullHandle)); duke@435: instanceKlassHandle this_klass (THREAD, ik); duke@435: jcoomes@1373: assert(this_klass->static_field_size() == static_field_size, "sanity"); jcoomes@1373: assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count, jcoomes@1373: "sanity"); duke@435: duke@435: // Fill in information already parsed duke@435: this_klass->set_access_flags(access_flags); acorn@1410: this_klass->set_should_verify_class(verify); duke@435: jint lh = Klass::instance_layout_helper(instance_size, false); duke@435: this_klass->set_layout_helper(lh); duke@435: assert(this_klass->oop_is_instance(), "layout is correct"); duke@435: assert(this_klass->size_helper() == 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()); duke@435: this_klass->set_class_loader(class_loader()); duke@435: this_klass->set_nonstatic_field_size(nonstatic_field_size); acorn@1410: this_klass->set_has_nonstatic_fields(has_nonstatic_fields); never@3137: this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]); duke@435: cp->set_pool_holder(this_klass()); coleenp@2497: error_handler.set_in_error(false); // turn off error handler for cp duke@435: this_klass->set_constants(cp()); duke@435: this_klass->set_local_interfaces(local_interfaces()); never@3137: this_klass->set_fields(fields(), java_fields_count); duke@435: this_klass->set_methods(methods()); duke@435: if (has_final_method) { duke@435: this_klass->set_has_final_method(); duke@435: } duke@435: this_klass->set_method_ordering(method_ordering()); dcubed@1412: // The instanceKlass::_methods_jmethod_ids cache and the dcubed@1412: // 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 dcubed@1412: // 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)); jrose@1145: if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself jrose@909: cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve duke@435: this_klass->set_protection_domain(protection_domain()); duke@435: this_klass->set_fields_annotations(fields_annotations()); duke@435: this_klass->set_methods_annotations(methods_annotations()); duke@435: this_klass->set_methods_parameter_annotations(methods_parameter_annotations()); duke@435: this_klass->set_methods_default_annotations(methods_default_annotations()); duke@435: duke@435: this_klass->set_minor_version(minor_version); duke@435: this_klass->set_major_version(major_version); duke@435: jrose@1291: // Set up methodOop::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.) jrose@1291: if (methodOopDesc::klass_id_for_intrinsics(this_klass->as_klassOop()) != vmSymbols::NO_SID) { jrose@1291: for (int j = 0; j < methods->length(); j++) { jrose@1291: ((methodOop)methods->obj_at(j))->init_intrinsic_id(); jrose@1291: } jrose@1291: } jrose@1291: duke@435: if (cached_class_file_bytes != NULL) { duke@435: // JVMTI: we have an instanceKlass now, tell it about the cached bytes duke@435: this_klass->set_cached_class_file(cached_class_file_bytes, duke@435: cached_class_file_length); duke@435: } duke@435: 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: // Additional attributes duke@435: parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle)); duke@435: duke@435: // Make sure this is the end of class file stream duke@435: guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle)); duke@435: duke@435: // VerifyOops believes that once this has been set, the object is completely loaded. duke@435: // Compute transitive closure of interfaces this class implements duke@435: this_klass->set_transitive_interfaces(transitive_interfaces()); 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: duke@435: // Do final class setup jcoomes@1373: fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts); duke@435: 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()) { duke@435: check_illegal_static_method(this_klass, CHECK_(nullHandle)); duke@435: } duke@435: never@2658: // Allocate mirror and initialize static fields never@2658: java_lang_Class::create_mirror(this_klass, CHECK_(nullHandle)); never@2658: duke@435: ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()), duke@435: false /* not shared class */); duke@435: duke@435: if (TraceClassLoading) { 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()) { duke@435: klassOop 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(), duke@435: 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: ResourceMark rm; duke@435: tty->print("[Loaded %s from %s]\n", this_klass->external_name(), duke@435: instanceKlass::cast(class_loader->klass())->external_name()); duke@435: } duke@435: } duke@435: duke@435: if (TraceClassResolution) { duke@435: // print out the superclass. duke@435: const char * from = Klass::cast(this_klass())->external_name(); duke@435: if (this_klass->java_super() != NULL) { acorn@1092: 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. duke@435: objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces()); duke@435: if (!local_interfaces.is_null()) { duke@435: int length = local_interfaces->length(); duke@435: for (int i = 0; i < length; i++) { duke@435: klassOop k = klassOop(local_interfaces->obj_at(i)); duke@435: 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: #ifndef PRODUCT duke@435: if( PrintCompactFieldsSavings ) { duke@435: if( nonstatic_field_size < orig_nonstatic_field_size ) { kvn@600: tty->print("[Saved %d of %d bytes in %s]\n", kvn@600: (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize, kvn@600: orig_nonstatic_field_size*heapOopSize, kvn@600: this_klass->external_name()); duke@435: } else if( nonstatic_field_size > orig_nonstatic_field_size ) { kvn@600: tty->print("[Wasted %d over %d bytes in %s]\n", kvn@600: (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize, kvn@600: orig_nonstatic_field_size*heapOopSize, kvn@600: this_klass->external_name()); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: // preserve result across HandleMark duke@435: preserve_this_klass = this_klass(); duke@435: } duke@435: duke@435: // Create new handle outside HandleMark duke@435: instanceKlassHandle this_klass (THREAD, preserve_this_klass); duke@435: debug_only(this_klass->as_klassOop()->verify();) duke@435: duke@435: return this_klass; duke@435: } duke@435: duke@435: 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(); jcoomes@1373: 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) { duke@435: klassOop 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 || duke@435: (super != NULL && super->klass_part()->has_finalizer())) { duke@435: k->set_has_finalizer(); duke@435: } duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: bool f = false; duke@435: methodOop 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 { duke@435: if (Klass::cast(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; duke@435: if (Klass::cast(super)->has_vanilla_constructor()) { duke@435: methodOop 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. duke@435: // 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() duke@435: || (k->name() == vmSymbols::java_lang_Class() duke@435: && 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: duke@435: duke@435: // utility method for appending and array with check for duplicates duke@435: duke@435: void append_interfaces(objArrayHandle result, int& index, objArrayOop ifs) { duke@435: // iterate over new interfaces duke@435: for (int i = 0; i < ifs->length(); i++) { duke@435: oop e = ifs->obj_at(i); duke@435: assert(e->is_klass() && instanceKlass::cast(klassOop(e))->is_interface(), "just checking"); duke@435: // check for duplicates duke@435: bool duplicate = false; duke@435: for (int j = 0; j < index; j++) { duke@435: if (result->obj_at(j) == e) { duke@435: duplicate = true; duke@435: break; duke@435: } duke@435: } duke@435: // add new interface duke@435: if (!duplicate) { duke@435: result->obj_at_put(index++, e); duke@435: } duke@435: } duke@435: } duke@435: duke@435: objArrayHandle ClassFileParser::compute_transitive_interfaces(instanceKlassHandle super, objArrayHandle 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++) { duke@435: klassOop l = klassOop(local_ifs->obj_at(i)); duke@435: 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: objArrayHandle result; duke@435: if (max_transitive_size == 0) { duke@435: // no interfaces, use canonicalized array duke@435: result = objArrayHandle(THREAD, Universe::the_empty_system_obj_array()); duke@435: } else if (max_transitive_size == super_size) { duke@435: // no new local interfaces added, share superklass' transitive interface array duke@435: result = objArrayHandle(THREAD, super->transitive_interfaces()); duke@435: } else if (max_transitive_size == local_size) { duke@435: // only local interfaces added, share local interface array duke@435: result = local_ifs; duke@435: } else { duke@435: objArrayHandle nullHandle; duke@435: objArrayOop new_objarray = oopFactory::new_system_objArray(max_transitive_size, CHECK_(nullHandle)); duke@435: result = objArrayHandle(THREAD, new_objarray); duke@435: int index = 0; duke@435: // Copy down from superclass duke@435: if (super.not_null()) { duke@435: append_interfaces(result, index, super->transitive_interfaces()); duke@435: } duke@435: // Copy down from local interfaces' superinterfaces duke@435: for (int i = 0; i < local_ifs->length(); i++) { duke@435: klassOop l = klassOop(local_ifs->obj_at(i)); duke@435: append_interfaces(result, index, instanceKlass::cast(l)->transitive_interfaces()); duke@435: } duke@435: // Finally add local interfaces duke@435: append_interfaces(result, index, local_ifs()); duke@435: duke@435: // Check if duplicates were removed duke@435: if (index != max_transitive_size) { duke@435: assert(index < max_transitive_size, "just checking"); duke@435: objArrayOop new_result = oopFactory::new_system_objArray(index, CHECK_(nullHandle)); duke@435: for (int i = 0; i < index; i++) { duke@435: oop e = result->obj_at(i); duke@435: assert(e != NULL, "just checking"); duke@435: new_result->obj_at_put(i, e); duke@435: } duke@435: result = objArrayHandle(THREAD, new_result); duke@435: } duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: duke@435: void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) { duke@435: klassOop super = this_klass->super(); duke@435: if ((super != NULL) && duke@435: (!Reflection::verify_class_access(this_klass->as_klassOop(), 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(), duke@435: 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) { duke@435: objArrayHandle local_interfaces (THREAD, this_klass->local_interfaces()); duke@435: int lng = local_interfaces->length(); duke@435: for (int i = lng - 1; i >= 0; i--) { duke@435: klassOop k = klassOop(local_interfaces->obj_at(i)); duke@435: assert (k != NULL && Klass::cast(k)->is_interface(), "invalid interface"); duke@435: if (!Reflection::verify_class_access(this_klass->as_klassOop(), 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(), duke@435: 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) { duke@435: objArrayHandle methods (THREAD, 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++) { duke@435: methodOop m = (methodOop)methods->obj_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(); duke@435: klassOop k = this_klass->super(); duke@435: methodOop super_m = NULL; duke@435: while (k != NULL) { duke@435: // skip supers that don't have final methods. duke@435: if (k->klass_part()->has_final_method()) { duke@435: // lookup a matching method in the super class hierarchy duke@435: 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 duke@435: (Reflection::verify_field_access(this_klass->as_klassOop(), 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. duke@435: k = instanceKlass::cast(super_m->method_holder())->super(); duke@435: continue; duke@435: } duke@435: duke@435: k = k->klass_part()->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"); duke@435: objArrayHandle methods (THREAD, this_klass->methods()); duke@435: int num_methods = methods->length(); duke@435: duke@435: for (int index = 0; index < num_methods; index++) { duke@435: methodOop m = (methodOop)methods->obj_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; duke@435: const bool major_gte_15 = _major_version >= JAVA_1_5_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) { duke@435: if (!is_abstract || !is_public || is_static || is_final || duke@435: is_native || (major_gte_15 && (is_synchronized || is_strict))) { duke@435: is_illegal = true; 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: }