提交 cdc18c13 编写于 作者: C coleenp

8003419: NPG: Clean up metadata created during class loading if failure

Summary: Store metadata on ClassFileParser instance to be cleaned up by destructor.  This enabled some refactoring of the enormous parseClassFile function.
Reviewed-by: jmasa, acorn
上级 ea32fa70
......@@ -34,6 +34,7 @@
#include "classfile/symbolTable.hpp"
class FieldAllocationCount;
class FieldLayoutInfo;
// Parser for for .class files
......@@ -47,6 +48,7 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
u2 _major_version;
u2 _minor_version;
Symbol* _class_name;
ClassLoaderData* _loader_data;
KlassHandle _host_klass;
GrowableArray<Handle>* _cp_patches; // overrides for CP entries
......@@ -58,33 +60,59 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
// class attributes parsed before the instance klass is created:
bool _synthetic_flag;
int _sde_length;
char* _sde_buffer;
Symbol* _sourcefile;
Symbol* _generic_signature;
char* _sde_buffer;
int _sde_length;
Array<u2>* _inner_classes;
// Metadata created before the instance klass is created. Must be deallocated
// if not transferred to the InstanceKlass upon successful class loading
// in which case these pointers have been set to NULL.
instanceKlassHandle _super_klass;
ConstantPool* _cp;
Array<u2>* _fields;
Array<Method*>* _methods;
Array<u2>* _inner_classes;
Array<Klass*>* _local_interfaces;
Array<Klass*>* _transitive_interfaces;
AnnotationArray* _annotations;
AnnotationArray* _type_annotations;
Array<AnnotationArray*>* _fields_annotations;
Array<AnnotationArray*>* _fields_type_annotations;
InstanceKlass* _klass; // InstanceKlass once created.
void set_class_synthetic_flag(bool x) { _synthetic_flag = x; }
void set_class_sourcefile(Symbol* x) { _sourcefile = x; }
void set_class_generic_signature(Symbol* x) { _generic_signature = x; }
void set_class_sde_buffer(char* x, int len) { _sde_buffer = x; _sde_length = len; }
void set_class_inner_classes(Array<u2>* x) { _inner_classes = x; }
void set_class_annotations(AnnotationArray* x) { _annotations = x; }
void set_class_type_annotations(AnnotationArray* x) { _type_annotations = x; }
void init_parsed_class_attributes() {
void init_parsed_class_attributes(ClassLoaderData* loader_data) {
_loader_data = loader_data;
_synthetic_flag = false;
_sourcefile = NULL;
_generic_signature = NULL;
_sde_buffer = NULL;
_sde_length = 0;
_annotations = _type_annotations = NULL;
// initialize the other flags too:
_has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
_max_bootstrap_specifier_index = -1;
clear_class_metadata();
_klass = NULL;
}
void apply_parsed_class_attributes(instanceKlassHandle k); // update k
void apply_parsed_class_metadata(instanceKlassHandle k, int fields_count, TRAPS);
void clear_class_metadata() {
// metadata created before the instance klass is created. Must be
// deallocated if classfile parsing returns an error.
_cp = NULL;
_fields = NULL;
_methods = NULL;
_inner_classes = NULL;
_local_interfaces = NULL;
_transitive_interfaces = NULL;
_annotations = _type_annotations = NULL;
_fields_annotations = _fields_type_annotations = NULL;
}
class AnnotationCollector {
public:
......@@ -124,11 +152,27 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
void set_contended(bool contended) { set_annotation(_sun_misc_Contended); }
bool is_contended() { return has_annotation(_sun_misc_Contended); }
};
// This class also doubles as a holder for metadata cleanup.
class FieldAnnotationCollector: public AnnotationCollector {
ClassLoaderData* _loader_data;
AnnotationArray* _field_annotations;
AnnotationArray* _field_type_annotations;
public:
FieldAnnotationCollector() : AnnotationCollector(_in_field) { }
FieldAnnotationCollector(ClassLoaderData* loader_data) :
AnnotationCollector(_in_field),
_loader_data(loader_data),
_field_annotations(NULL),
_field_type_annotations(NULL) {}
void apply_to(FieldInfo* f);
~FieldAnnotationCollector();
AnnotationArray* field_annotations() { return _field_annotations; }
AnnotationArray* field_type_annotations() { return _field_type_annotations; }
void set_field_annotations(AnnotationArray* a) { _field_annotations = a; }
void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }
};
class MethodAnnotationCollector: public AnnotationCollector {
public:
MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
......@@ -152,38 +196,30 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
void set_stream(ClassFileStream* st) { _stream = st; }
// Constant pool parsing
void parse_constant_pool_entries(ClassLoaderData* loader_data,
constantPoolHandle cp, int length, TRAPS);
void parse_constant_pool_entries(int length, TRAPS);
constantPoolHandle parse_constant_pool(ClassLoaderData* loader_data, TRAPS);
constantPoolHandle parse_constant_pool(TRAPS);
// Interface parsing
Array<Klass*>* parse_interfaces(constantPoolHandle cp,
int length,
ClassLoaderData* loader_data,
Array<Klass*>* parse_interfaces(int length,
Handle protection_domain,
Symbol* class_name,
bool* has_default_methods,
TRAPS);
void record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS);
instanceKlassHandle parse_super_class(int super_class_index, TRAPS);
// Field parsing
void parse_field_attributes(ClassLoaderData* loader_data,
constantPoolHandle cp, u2 attributes_count,
void parse_field_attributes(u2 attributes_count,
bool is_static, u2 signature_index,
u2* constantvalue_index_addr,
bool* is_synthetic_addr,
u2* generic_signature_index_addr,
AnnotationArray** field_annotations,
AnnotationArray** field_type_annotations,
FieldAnnotationCollector* parsed_annotations,
TRAPS);
Array<u2>* parse_fields(ClassLoaderData* loader_data,
Symbol* class_name,
constantPoolHandle cp, bool is_interface,
Array<u2>* parse_fields(Symbol* class_name,
bool is_interface,
FieldAllocationCount *fac,
Array<AnnotationArray*>** fields_annotations,
Array<AnnotationArray*>** fields_type_annotations,
u2* java_fields_count_ptr, TRAPS);
void print_field_layout(Symbol* name,
......@@ -195,65 +231,52 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
int static_fields_end);
// Method parsing
methodHandle parse_method(ClassLoaderData* loader_data,
constantPoolHandle cp,
bool is_interface,
methodHandle parse_method(bool is_interface,
AccessFlags* promoted_flags,
TRAPS);
Array<Method*>* parse_methods(ClassLoaderData* loader_data,
constantPoolHandle cp,
bool is_interface,
Array<Method*>* parse_methods(bool is_interface,
AccessFlags* promoted_flags,
bool* has_final_method,
bool* has_default_method,
TRAPS);
Array<int>* sort_methods(ClassLoaderData* loader_data,
Array<Method*>* methods,
TRAPS);
u2* parse_exception_table(ClassLoaderData* loader_data,
u4 code_length, u4 exception_table_length,
constantPoolHandle cp, TRAPS);
intArray* sort_methods(Array<Method*>* methods);
u2* parse_exception_table(u4 code_length, u4 exception_table_length,
TRAPS);
void parse_linenumber_table(
u4 code_attribute_length, u4 code_length,
CompressedLineNumberWriteStream** write_stream, TRAPS);
u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length,
constantPoolHandle cp, u2* localvariable_table_length,
u2* localvariable_table_length,
bool isLVTT, TRAPS);
u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
constantPoolHandle cp, TRAPS);
TRAPS);
void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS);
Array<u1>* parse_stackmap_table(ClassLoaderData* loader_data, u4 code_attribute_length, TRAPS);
u1* u1_array, u2* u2_array, TRAPS);
u1* parse_stackmap_table(u4 code_attribute_length, TRAPS);
// Classfile attribute parsing
void parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS);
void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
int length, TRAPS);
u2 parse_classfile_inner_classes_attribute(ClassLoaderData* loader_data,
u1* inner_classes_attribute_start,
void parse_classfile_sourcefile_attribute(TRAPS);
void parse_classfile_source_debug_extension_attribute(int length, TRAPS);
u2 parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start,
bool parsed_enclosingmethod_attribute,
u2 enclosing_method_class_index,
u2 enclosing_method_method_index,
constantPoolHandle cp,
TRAPS);
void parse_classfile_attributes(ClassLoaderData* loader_data,
constantPoolHandle cp,
ClassAnnotationCollector* parsed_annotations,
void parse_classfile_attributes(ClassAnnotationCollector* parsed_annotations,
TRAPS);
void parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS);
void parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS);
void parse_classfile_bootstrap_methods_attribute(ClassLoaderData* loader_data, constantPoolHandle cp, u4 attribute_length, TRAPS);
void parse_classfile_synthetic_attribute(TRAPS);
void parse_classfile_signature_attribute(TRAPS);
void parse_classfile_bootstrap_methods_attribute(u4 attribute_length, TRAPS);
// Annotations handling
AnnotationArray* assemble_annotations(ClassLoaderData* loader_data,
u1* runtime_visible_annotations,
AnnotationArray* assemble_annotations(u1* runtime_visible_annotations,
int runtime_visible_annotations_length,
u1* runtime_invisible_annotations,
int runtime_invisible_annotations_length, TRAPS);
int skip_annotation(u1* buffer, int limit, int index);
int skip_annotation_value(u1* buffer, int limit, int index);
void parse_annotations(ClassLoaderData* loader_data,
u1* buffer, int limit, constantPoolHandle cp,
void parse_annotations(u1* buffer, int limit,
/* Results (currently, only one result is supported): */
AnnotationCollector* result,
TRAPS);
......@@ -267,8 +290,7 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
int* nonstatic_oop_offsets,
unsigned int* nonstatic_oop_counts);
void set_precomputed_flags(instanceKlassHandle k);
Array<Klass*>* compute_transitive_interfaces(ClassLoaderData* loader_data,
instanceKlassHandle super,
Array<Klass*>* compute_transitive_interfaces(instanceKlassHandle super,
Array<Klass*>* local_ifs, TRAPS);
// Format checker methods
......@@ -318,7 +340,7 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
bool is_supported_version(u2 major, u2 minor);
bool has_illegal_visibility(jint flags);
void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
void verify_constantvalue(int constantvalue_index, int signature_index, TRAPS);
void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
void verify_legal_class_name(Symbol* name, TRAPS);
void verify_legal_field_name(Symbol* name, TRAPS);
......@@ -359,10 +381,17 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
// In older versions of the VM, Klass*s cannot sneak into early phases of
// constant pool construction, but in later versions they can.
// %%% Let's phase out the old is_klass_reference.
bool is_klass_reference(constantPoolHandle cp, int index) {
return (EnableInvokeDynamic
? cp->tag_at(index).is_klass_or_reference()
: cp->tag_at(index).is_klass_reference());
bool valid_klass_reference_at(int index) {
return _cp->is_within_bounds(index) &&
(EnableInvokeDynamic
? _cp->tag_at(index).is_klass_or_reference()
: _cp->tag_at(index).is_klass_reference());
}
// Checks that the cpool index is in range and is a utf8
bool valid_symbol_at(int cpool_index) {
return (_cp->is_within_bounds(cpool_index) &&
_cp->tag_at(cpool_index).is_utf8());
}
void copy_localvariable_table(ConstMethod* cm, int lvt_cnt,
......@@ -373,8 +402,7 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
u2** localvariable_type_table_start,
TRAPS);
void copy_method_annotations(ClassLoaderData* loader_data,
ConstMethod* cm,
void copy_method_annotations(ConstMethod* cm,
u1* runtime_visible_annotations,
int runtime_visible_annotations_length,
u1* runtime_invisible_annotations,
......@@ -391,9 +419,15 @@ class ClassFileParser VALUE_OBJ_CLASS_SPEC {
int annotation_default_length,
TRAPS);
// lays out fields in class and returns the total oopmap count
void layout_fields(Handle class_loader, FieldAllocationCount* fac,
ClassAnnotationCollector* parsed_annotations,
FieldLayoutInfo* info, TRAPS);
public:
// Constructor
ClassFileParser(ClassFileStream* st) { set_stream(st); }
~ClassFileParser();
// Parse .class file and return new Klass*. The Klass* is not hooked up
// to the system dictionary or any other structures, so a .class file can
......
......@@ -67,6 +67,12 @@ ConstMethod::ConstMethod(int byte_code_size,
set_size_of_parameters(0);
}
// Accessor that copies to metadata.
void ConstMethod::copy_stackmap_data(ClassLoaderData* loader_data,
u1* sd, int length, TRAPS) {
_stackmap_data = MetadataFactory::new_array<u1>(loader_data, length, CHECK);
memcpy((void*)_stackmap_data->adr_at(0), (void*)sd, length);
}
// Deallocate metadata fields associated with ConstMethod*
void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) {
......
......@@ -280,6 +280,7 @@ public:
// stackmap table data
Array<u1>* stackmap_data() const { return _stackmap_data; }
void set_stackmap_data(Array<u1>* sd) { _stackmap_data = sd; }
void copy_stackmap_data(ClassLoaderData* loader_data, u1* sd, int length, TRAPS);
bool has_stackmap_table() const { return _stackmap_data != NULL; }
void init_fingerprint() {
......
......@@ -165,7 +165,8 @@ HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__end,
volatile int InstanceKlass::_total_instanceKlass_count = 0;
Klass* InstanceKlass::allocate_instance_klass(ClassLoaderData* loader_data,
InstanceKlass* InstanceKlass::allocate_instance_klass(
ClassLoaderData* loader_data,
int vtable_len,
int itable_len,
int static_field_size,
......@@ -207,10 +208,35 @@ Klass* InstanceKlass::allocate_instance_klass(ClassLoaderData* loader_data,
access_flags, is_anonymous);
}
// Check for pending exception before adding to the loader data and incrementing
// class count. Can get OOM here.
if (HAS_PENDING_EXCEPTION) {
return NULL;
}
// Add all classes to our internal class loader list here,
// including classes in the bootstrap (NULL) class loader.
loader_data->add_class(ik);
Atomic::inc(&_total_instanceKlass_count);
return ik;
}
// copy method ordering from resource area to Metaspace
void InstanceKlass::copy_method_ordering(intArray* m, TRAPS) {
if (m != NULL) {
// allocate a new array and copy contents (memcpy?)
_method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
for (int i = 0; i < m->length(); i++) {
_method_ordering->at_put(i, m->at(i));
}
} else {
_method_ordering = Universe::the_empty_int_array();
}
}
InstanceKlass::InstanceKlass(int vtable_len,
int itable_len,
int static_field_size,
......@@ -223,9 +249,6 @@ InstanceKlass::InstanceKlass(int vtable_len,
int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
access_flags.is_interface(), is_anonymous);
// The sizes of these these three variables are used for determining the
// size of the instanceKlassOop. It is critical that these are set to the right
// sizes before the first GC, i.e., when we allocate the mirror.
set_vtable_length(vtable_len);
set_itable_length(itable_len);
set_static_field_size(static_field_size);
......@@ -288,12 +311,51 @@ InstanceKlass::InstanceKlass(int vtable_len,
}
void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
Array<Method*>* methods) {
if (methods != NULL && methods != Universe::the_empty_method_array()) {
for (int i = 0; i < methods->length(); i++) {
Method* method = methods->at(i);
if (method == NULL) continue; // maybe null if error processing
// Only want to delete methods that are not executing for RedefineClasses.
// The previous version will point to them so they're not totally dangling
assert (!method->on_stack(), "shouldn't be called with methods on stack");
MetadataFactory::free_metadata(loader_data, method);
}
MetadataFactory::free_array<Method*>(loader_data, methods);
}
}
void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
Klass* super_klass,
Array<Klass*>* local_interfaces,
Array<Klass*>* transitive_interfaces) {
// Only deallocate transitive interfaces if not empty, same as super class
// or same as local interfaces. See code in parseClassFile.
Array<Klass*>* ti = transitive_interfaces;
if (ti != Universe::the_empty_klass_array() && ti != local_interfaces) {
// check that the interfaces don't come from super class
Array<Klass*>* sti = (super_klass == NULL) ? NULL :
InstanceKlass::cast(super_klass)->transitive_interfaces();
if (ti != sti) {
MetadataFactory::free_array<Klass*>(loader_data, ti);
}
}
// local interfaces can be empty
if (local_interfaces != Universe::the_empty_klass_array()) {
MetadataFactory::free_array<Klass*>(loader_data, local_interfaces);
}
}
// This function deallocates the metadata and C heap pointers that the
// InstanceKlass points to.
void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
// Orphan the mirror first, CMS thinks it's still live.
java_lang_Class::set_klass(java_mirror(), NULL);
if (java_mirror() != NULL) {
java_lang_Class::set_klass(java_mirror(), NULL);
}
// Need to take this class off the class loader data list.
loader_data->remove_class(this);
......@@ -308,17 +370,7 @@ void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
// reference counting symbol names.
release_C_heap_structures();
Array<Method*>* ms = methods();
if (ms != Universe::the_empty_method_array()) {
for (int i = 0; i <= methods()->length() -1 ; i++) {
Method* method = methods()->at(i);
// Only want to delete methods that are not executing for RedefineClasses.
// The previous version will point to them so they're not totally dangling
assert (!method->on_stack(), "shouldn't be called with methods on stack");
MetadataFactory::free_metadata(loader_data, method);
}
MetadataFactory::free_array<Method*>(loader_data, methods());
}
deallocate_methods(loader_data, methods());
set_methods(NULL);
if (method_ordering() != Universe::the_empty_int_array()) {
......@@ -335,24 +387,8 @@ void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
}
set_secondary_supers(NULL);
// Only deallocate transitive interfaces if not empty, same as super class
// or same as local interfaces. See code in parseClassFile.
Array<Klass*>* ti = transitive_interfaces();
if (ti != Universe::the_empty_klass_array() && ti != local_interfaces()) {
// check that the interfaces don't come from super class
Array<Klass*>* sti = (super() == NULL) ? NULL :
InstanceKlass::cast(super())->transitive_interfaces();
if (ti != sti) {
MetadataFactory::free_array<Klass*>(loader_data, ti);
}
}
deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
set_transitive_interfaces(NULL);
// local interfaces can be empty
Array<Klass*>* li = local_interfaces();
if (li != Universe::the_empty_klass_array()) {
MetadataFactory::free_array<Klass*>(loader_data, li);
}
set_local_interfaces(NULL);
MetadataFactory::free_array<jushort>(loader_data, fields());
......@@ -360,9 +396,11 @@ void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
// If a method from a redefined class is using this constant pool, don't
// delete it, yet. The new class's previous version will point to this.
assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
MetadataFactory::free_metadata(loader_data, constants());
set_constants(NULL);
if (constants() != NULL) {
assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
MetadataFactory::free_metadata(loader_data, constants());
set_constants(NULL);
}
if (inner_classes() != Universe::the_empty_short_array()) {
MetadataFactory::free_array<jushort>(loader_data, inner_classes());
......
......@@ -147,7 +147,8 @@ class InstanceKlass: public Klass {
AccessFlags access_flags,
bool is_anonymous);
public:
static Klass* allocate_instance_klass(ClassLoaderData* loader_data,
static InstanceKlass* allocate_instance_klass(
ClassLoaderData* loader_data,
int vtable_len,
int itable_len,
int static_field_size,
......@@ -266,7 +267,6 @@ class InstanceKlass: public Klass {
u1 _init_state; // state of class
u1 _reference_type; // reference type
JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map; // JVMTI: used during heap iteration
NOT_PRODUCT(int _verify_count;) // to avoid redundant verifies
......@@ -358,16 +358,19 @@ class InstanceKlass: public Klass {
// method ordering
Array<int>* method_ordering() const { return _method_ordering; }
void set_method_ordering(Array<int>* m) { _method_ordering = m; }
void copy_method_ordering(intArray* m, TRAPS);
// interfaces
Array<Klass*>* local_interfaces() const { return _local_interfaces; }
void set_local_interfaces(Array<Klass*>* a) {
guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
_local_interfaces = a; }
Array<Klass*>* transitive_interfaces() const { return _transitive_interfaces; }
void set_transitive_interfaces(Array<Klass*>* a) {
guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
_transitive_interfaces = a; }
_transitive_interfaces = a;
}
private:
friend class fieldDescriptor;
......@@ -383,10 +386,9 @@ class InstanceKlass: public Klass {
int java_fields_count() const { return (int)_java_fields_count; }
Array<u2>* fields() const { return _fields; }
void set_fields(Array<u2>* f, u2 java_fields_count) {
guarantee(_fields == NULL || f == NULL, "Just checking");
_fields = f;
_fields = f;
_java_fields_count = java_fields_count;
}
......@@ -916,8 +918,15 @@ class InstanceKlass: public Klass {
void clean_method_data(BoolObjectClosure* is_alive);
// Explicit metaspace deallocation of fields
// For RedefineClasses, we need to deallocate instanceKlasses
// For RedefineClasses and class file parsing errors, we need to deallocate
// instanceKlasses and the metadata they point to.
void deallocate_contents(ClassLoaderData* loader_data);
static void deallocate_methods(ClassLoaderData* loader_data,
Array<Method*>* methods);
void static deallocate_interfaces(ClassLoaderData* loader_data,
Klass* super_klass,
Array<Klass*>* local_interfaces,
Array<Klass*>* transitive_interfaces);
// The constant pool is on stack if any of the methods are executing or
// referenced by handles.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册