1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/classfile/classFileParser.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,490 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP 1.29 +#define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP 1.30 + 1.31 +#include "classfile/classFileStream.hpp" 1.32 +#include "memory/resourceArea.hpp" 1.33 +#include "oops/oop.inline.hpp" 1.34 +#include "oops/typeArrayOop.hpp" 1.35 +#include "runtime/handles.inline.hpp" 1.36 +#include "utilities/accessFlags.hpp" 1.37 +#include "classfile/symbolTable.hpp" 1.38 + 1.39 +class FieldAllocationCount; 1.40 +class FieldLayoutInfo; 1.41 + 1.42 + 1.43 +// Parser for for .class files 1.44 +// 1.45 +// The bytes describing the class file structure is read from a Stream object 1.46 + 1.47 +class ClassFileParser VALUE_OBJ_CLASS_SPEC { 1.48 + private: 1.49 + bool _need_verify; 1.50 + bool _relax_verify; 1.51 + u2 _major_version; 1.52 + u2 _minor_version; 1.53 + Symbol* _class_name; 1.54 + ClassLoaderData* _loader_data; 1.55 + KlassHandle _host_klass; 1.56 + GrowableArray<Handle>* _cp_patches; // overrides for CP entries 1.57 + 1.58 + // precomputed flags 1.59 + bool _has_finalizer; 1.60 + bool _has_empty_finalizer; 1.61 + bool _has_vanilla_constructor; 1.62 + int _max_bootstrap_specifier_index; // detects BSS values 1.63 + 1.64 + // class attributes parsed before the instance klass is created: 1.65 + bool _synthetic_flag; 1.66 + int _sde_length; 1.67 + char* _sde_buffer; 1.68 + u2 _sourcefile_index; 1.69 + u2 _generic_signature_index; 1.70 + 1.71 + // Metadata created before the instance klass is created. Must be deallocated 1.72 + // if not transferred to the InstanceKlass upon successful class loading 1.73 + // in which case these pointers have been set to NULL. 1.74 + instanceKlassHandle _super_klass; 1.75 + ConstantPool* _cp; 1.76 + Array<u2>* _fields; 1.77 + Array<Method*>* _methods; 1.78 + Array<u2>* _inner_classes; 1.79 + Array<Klass*>* _local_interfaces; 1.80 + Array<Klass*>* _transitive_interfaces; 1.81 + AnnotationArray* _annotations; 1.82 + AnnotationArray* _type_annotations; 1.83 + Array<AnnotationArray*>* _fields_annotations; 1.84 + Array<AnnotationArray*>* _fields_type_annotations; 1.85 + InstanceKlass* _klass; // InstanceKlass once created. 1.86 + 1.87 + void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 1.88 + void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 1.89 + void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 1.90 + void set_class_sde_buffer(char* x, int len) { _sde_buffer = x; _sde_length = len; } 1.91 + 1.92 + void init_parsed_class_attributes(ClassLoaderData* loader_data) { 1.93 + _loader_data = loader_data; 1.94 + _synthetic_flag = false; 1.95 + _sourcefile_index = 0; 1.96 + _generic_signature_index = 0; 1.97 + _sde_buffer = NULL; 1.98 + _sde_length = 0; 1.99 + // initialize the other flags too: 1.100 + _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false; 1.101 + _max_bootstrap_specifier_index = -1; 1.102 + clear_class_metadata(); 1.103 + _klass = NULL; 1.104 + } 1.105 + void apply_parsed_class_attributes(instanceKlassHandle k); // update k 1.106 + void apply_parsed_class_metadata(instanceKlassHandle k, int fields_count, TRAPS); 1.107 + void clear_class_metadata() { 1.108 + // metadata created before the instance klass is created. Must be 1.109 + // deallocated if classfile parsing returns an error. 1.110 + _cp = NULL; 1.111 + _fields = NULL; 1.112 + _methods = NULL; 1.113 + _inner_classes = NULL; 1.114 + _local_interfaces = NULL; 1.115 + _transitive_interfaces = NULL; 1.116 + _annotations = _type_annotations = NULL; 1.117 + _fields_annotations = _fields_type_annotations = NULL; 1.118 + } 1.119 + 1.120 + class AnnotationCollector { 1.121 + public: 1.122 + enum Location { _in_field, _in_method, _in_class }; 1.123 + enum ID { 1.124 + _unknown = 0, 1.125 + _method_CallerSensitive, 1.126 + _method_ForceInline, 1.127 + _method_DontInline, 1.128 + _method_LambdaForm_Compiled, 1.129 + _method_LambdaForm_Hidden, 1.130 + _sun_misc_Contended, 1.131 + _field_Stable, 1.132 + _annotation_LIMIT 1.133 + }; 1.134 + const Location _location; 1.135 + int _annotations_present; 1.136 + u2 _contended_group; 1.137 + 1.138 + AnnotationCollector(Location location) 1.139 + : _location(location), _annotations_present(0) 1.140 + { 1.141 + assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, ""); 1.142 + } 1.143 + // If this annotation name has an ID, report it (or _none). 1.144 + ID annotation_index(ClassLoaderData* loader_data, Symbol* name); 1.145 + // Set the annotation name: 1.146 + void set_annotation(ID id) { 1.147 + assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 1.148 + _annotations_present |= nth_bit((int)id); 1.149 + } 1.150 + 1.151 + void remove_annotation(ID id) { 1.152 + assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 1.153 + _annotations_present &= ~nth_bit((int)id); 1.154 + } 1.155 + 1.156 + // Report if the annotation is present. 1.157 + bool has_any_annotations() const { return _annotations_present != 0; } 1.158 + bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; } 1.159 + 1.160 + void set_contended_group(u2 group) { _contended_group = group; } 1.161 + u2 contended_group() const { return _contended_group; } 1.162 + 1.163 + bool is_contended() const { return has_annotation(_sun_misc_Contended); } 1.164 + 1.165 + void set_stable(bool stable) { set_annotation(_field_Stable); } 1.166 + bool is_stable() const { return has_annotation(_field_Stable); } 1.167 + }; 1.168 + 1.169 + // This class also doubles as a holder for metadata cleanup. 1.170 + class FieldAnnotationCollector: public AnnotationCollector { 1.171 + ClassLoaderData* _loader_data; 1.172 + AnnotationArray* _field_annotations; 1.173 + AnnotationArray* _field_type_annotations; 1.174 + public: 1.175 + FieldAnnotationCollector(ClassLoaderData* loader_data) : 1.176 + AnnotationCollector(_in_field), 1.177 + _loader_data(loader_data), 1.178 + _field_annotations(NULL), 1.179 + _field_type_annotations(NULL) {} 1.180 + void apply_to(FieldInfo* f); 1.181 + ~FieldAnnotationCollector(); 1.182 + AnnotationArray* field_annotations() { return _field_annotations; } 1.183 + AnnotationArray* field_type_annotations() { return _field_type_annotations; } 1.184 + 1.185 + void set_field_annotations(AnnotationArray* a) { _field_annotations = a; } 1.186 + void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; } 1.187 + }; 1.188 + 1.189 + class MethodAnnotationCollector: public AnnotationCollector { 1.190 + public: 1.191 + MethodAnnotationCollector() : AnnotationCollector(_in_method) { } 1.192 + void apply_to(methodHandle m); 1.193 + }; 1.194 + class ClassAnnotationCollector: public AnnotationCollector { 1.195 + public: 1.196 + ClassAnnotationCollector() : AnnotationCollector(_in_class) { } 1.197 + void apply_to(instanceKlassHandle k); 1.198 + }; 1.199 + 1.200 + enum { fixed_buffer_size = 128 }; 1.201 + u_char linenumbertable_buffer[fixed_buffer_size]; 1.202 + 1.203 + ClassFileStream* _stream; // Actual input stream 1.204 + 1.205 + enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names 1.206 + 1.207 + // Accessors 1.208 + ClassFileStream* stream() { return _stream; } 1.209 + void set_stream(ClassFileStream* st) { _stream = st; } 1.210 + 1.211 + // Constant pool parsing 1.212 + void parse_constant_pool_entries(int length, TRAPS); 1.213 + 1.214 + constantPoolHandle parse_constant_pool(TRAPS); 1.215 + 1.216 + // Interface parsing 1.217 + Array<Klass*>* parse_interfaces(int length, 1.218 + Handle protection_domain, 1.219 + Symbol* class_name, 1.220 + bool* has_default_methods, 1.221 + TRAPS); 1.222 + void record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS); 1.223 + 1.224 + instanceKlassHandle parse_super_class(int super_class_index, TRAPS); 1.225 + // Field parsing 1.226 + void parse_field_attributes(u2 attributes_count, 1.227 + bool is_static, u2 signature_index, 1.228 + u2* constantvalue_index_addr, 1.229 + bool* is_synthetic_addr, 1.230 + u2* generic_signature_index_addr, 1.231 + FieldAnnotationCollector* parsed_annotations, 1.232 + TRAPS); 1.233 + Array<u2>* parse_fields(Symbol* class_name, 1.234 + bool is_interface, 1.235 + FieldAllocationCount *fac, 1.236 + u2* java_fields_count_ptr, TRAPS); 1.237 + 1.238 + void print_field_layout(Symbol* name, 1.239 + Array<u2>* fields, 1.240 + constantPoolHandle cp, 1.241 + int instance_size, 1.242 + int instance_fields_start, 1.243 + int instance_fields_end, 1.244 + int static_fields_end); 1.245 + 1.246 + // Method parsing 1.247 + methodHandle parse_method(bool is_interface, 1.248 + AccessFlags* promoted_flags, 1.249 + TRAPS); 1.250 + Array<Method*>* parse_methods(bool is_interface, 1.251 + AccessFlags* promoted_flags, 1.252 + bool* has_final_method, 1.253 + bool* has_default_method, 1.254 + TRAPS); 1.255 + intArray* sort_methods(Array<Method*>* methods); 1.256 + 1.257 + u2* parse_exception_table(u4 code_length, u4 exception_table_length, 1.258 + TRAPS); 1.259 + void parse_linenumber_table( 1.260 + u4 code_attribute_length, u4 code_length, 1.261 + CompressedLineNumberWriteStream** write_stream, TRAPS); 1.262 + u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length, 1.263 + u2* localvariable_table_length, 1.264 + bool isLVTT, TRAPS); 1.265 + u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length, 1.266 + TRAPS); 1.267 + void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index, 1.268 + u1* u1_array, u2* u2_array, TRAPS); 1.269 + u1* parse_stackmap_table(u4 code_attribute_length, TRAPS); 1.270 + 1.271 + // Classfile attribute parsing 1.272 + u2 parse_generic_signature_attribute(TRAPS); 1.273 + void parse_classfile_sourcefile_attribute(TRAPS); 1.274 + void parse_classfile_source_debug_extension_attribute(int length, TRAPS); 1.275 + u2 parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start, 1.276 + bool parsed_enclosingmethod_attribute, 1.277 + u2 enclosing_method_class_index, 1.278 + u2 enclosing_method_method_index, 1.279 + TRAPS); 1.280 + void parse_classfile_attributes(ClassAnnotationCollector* parsed_annotations, 1.281 + TRAPS); 1.282 + void parse_classfile_synthetic_attribute(TRAPS); 1.283 + void parse_classfile_signature_attribute(TRAPS); 1.284 + void parse_classfile_bootstrap_methods_attribute(u4 attribute_length, TRAPS); 1.285 + 1.286 + // Annotations handling 1.287 + AnnotationArray* assemble_annotations(u1* runtime_visible_annotations, 1.288 + int runtime_visible_annotations_length, 1.289 + u1* runtime_invisible_annotations, 1.290 + int runtime_invisible_annotations_length, TRAPS); 1.291 + int skip_annotation(u1* buffer, int limit, int index); 1.292 + int skip_annotation_value(u1* buffer, int limit, int index); 1.293 + void parse_annotations(u1* buffer, int limit, 1.294 + /* Results (currently, only one result is supported): */ 1.295 + AnnotationCollector* result, 1.296 + TRAPS); 1.297 + 1.298 + // Final setup 1.299 + unsigned int compute_oop_map_count(instanceKlassHandle super, 1.300 + unsigned int nonstatic_oop_count, 1.301 + int first_nonstatic_oop_offset); 1.302 + void fill_oop_maps(instanceKlassHandle k, 1.303 + unsigned int nonstatic_oop_map_count, 1.304 + int* nonstatic_oop_offsets, 1.305 + unsigned int* nonstatic_oop_counts); 1.306 + void set_precomputed_flags(instanceKlassHandle k); 1.307 + Array<Klass*>* compute_transitive_interfaces(instanceKlassHandle super, 1.308 + Array<Klass*>* local_ifs, TRAPS); 1.309 + 1.310 + // Format checker methods 1.311 + void classfile_parse_error(const char* msg, TRAPS); 1.312 + void classfile_parse_error(const char* msg, int index, TRAPS); 1.313 + void classfile_parse_error(const char* msg, const char *name, TRAPS); 1.314 + void classfile_parse_error(const char* msg, int index, const char *name, TRAPS); 1.315 + inline void guarantee_property(bool b, const char* msg, TRAPS) { 1.316 + if (!b) { classfile_parse_error(msg, CHECK); } 1.317 + } 1.318 + 1.319 +PRAGMA_DIAG_PUSH 1.320 +PRAGMA_FORMAT_NONLITERAL_IGNORED 1.321 +inline void assert_property(bool b, const char* msg, TRAPS) { 1.322 +#ifdef ASSERT 1.323 + if (!b) { 1.324 + ResourceMark rm(THREAD); 1.325 + fatal(err_msg(msg, _class_name->as_C_string())); 1.326 + } 1.327 +#endif 1.328 + } 1.329 + 1.330 + inline void assert_property(bool b, const char* msg, int index, TRAPS) { 1.331 +#ifdef ASSERT 1.332 + if (!b) { 1.333 + ResourceMark rm(THREAD); 1.334 + fatal(err_msg(msg, index, _class_name->as_C_string())); 1.335 + } 1.336 +#endif 1.337 + } 1.338 +PRAGMA_DIAG_POP 1.339 + 1.340 + inline void check_property(bool property, const char* msg, int index, TRAPS) { 1.341 + if (_need_verify) { 1.342 + guarantee_property(property, msg, index, CHECK); 1.343 + } else { 1.344 + assert_property(property, msg, index, CHECK); 1.345 + } 1.346 + } 1.347 + 1.348 + inline void check_property(bool property, const char* msg, TRAPS) { 1.349 + if (_need_verify) { 1.350 + guarantee_property(property, msg, CHECK); 1.351 + } else { 1.352 + assert_property(property, msg, CHECK); 1.353 + } 1.354 + } 1.355 + 1.356 + inline void guarantee_property(bool b, const char* msg, int index, TRAPS) { 1.357 + if (!b) { classfile_parse_error(msg, index, CHECK); } 1.358 + } 1.359 + inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) { 1.360 + if (!b) { classfile_parse_error(msg, name, CHECK); } 1.361 + } 1.362 + inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) { 1.363 + if (!b) { classfile_parse_error(msg, index, name, CHECK); } 1.364 + } 1.365 + 1.366 + void throwIllegalSignature( 1.367 + const char* type, Symbol* name, Symbol* sig, TRAPS); 1.368 + 1.369 + bool is_supported_version(u2 major, u2 minor); 1.370 + bool has_illegal_visibility(jint flags); 1.371 + 1.372 + void verify_constantvalue(int constantvalue_index, int signature_index, TRAPS); 1.373 + void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS); 1.374 + void verify_legal_class_name(Symbol* name, TRAPS); 1.375 + void verify_legal_field_name(Symbol* name, TRAPS); 1.376 + void verify_legal_method_name(Symbol* name, TRAPS); 1.377 + void verify_legal_field_signature(Symbol* fieldname, Symbol* signature, TRAPS); 1.378 + int verify_legal_method_signature(Symbol* methodname, Symbol* signature, TRAPS); 1.379 + void verify_legal_class_modifiers(jint flags, TRAPS); 1.380 + void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS); 1.381 + void verify_legal_method_modifiers(jint flags, bool is_interface, Symbol* name, TRAPS); 1.382 + bool verify_unqualified_name(char* name, unsigned int length, int type); 1.383 + char* skip_over_field_name(char* name, bool slash_ok, unsigned int length); 1.384 + char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS); 1.385 + 1.386 + bool is_anonymous() { 1.387 + assert(EnableInvokeDynamic || _host_klass.is_null(), ""); 1.388 + return _host_klass.not_null(); 1.389 + } 1.390 + bool has_cp_patch_at(int index) { 1.391 + assert(EnableInvokeDynamic, ""); 1.392 + assert(index >= 0, "oob"); 1.393 + return (_cp_patches != NULL 1.394 + && index < _cp_patches->length() 1.395 + && _cp_patches->adr_at(index)->not_null()); 1.396 + } 1.397 + Handle cp_patch_at(int index) { 1.398 + assert(has_cp_patch_at(index), "oob"); 1.399 + return _cp_patches->at(index); 1.400 + } 1.401 + Handle clear_cp_patch_at(int index) { 1.402 + Handle patch = cp_patch_at(index); 1.403 + _cp_patches->at_put(index, Handle()); 1.404 + assert(!has_cp_patch_at(index), ""); 1.405 + return patch; 1.406 + } 1.407 + void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS); 1.408 + 1.409 + // Wrapper for constantTag.is_klass_[or_]reference. 1.410 + // In older versions of the VM, Klass*s cannot sneak into early phases of 1.411 + // constant pool construction, but in later versions they can. 1.412 + // %%% Let's phase out the old is_klass_reference. 1.413 + bool valid_klass_reference_at(int index) { 1.414 + return _cp->is_within_bounds(index) && 1.415 + (EnableInvokeDynamic 1.416 + ? _cp->tag_at(index).is_klass_or_reference() 1.417 + : _cp->tag_at(index).is_klass_reference()); 1.418 + } 1.419 + 1.420 + // Checks that the cpool index is in range and is a utf8 1.421 + bool valid_symbol_at(int cpool_index) { 1.422 + return (_cp->is_within_bounds(cpool_index) && 1.423 + _cp->tag_at(cpool_index).is_utf8()); 1.424 + } 1.425 + 1.426 + void copy_localvariable_table(ConstMethod* cm, int lvt_cnt, 1.427 + u2* localvariable_table_length, 1.428 + u2** localvariable_table_start, 1.429 + int lvtt_cnt, 1.430 + u2* localvariable_type_table_length, 1.431 + u2** localvariable_type_table_start, 1.432 + TRAPS); 1.433 + 1.434 + void copy_method_annotations(ConstMethod* cm, 1.435 + u1* runtime_visible_annotations, 1.436 + int runtime_visible_annotations_length, 1.437 + u1* runtime_invisible_annotations, 1.438 + int runtime_invisible_annotations_length, 1.439 + u1* runtime_visible_parameter_annotations, 1.440 + int runtime_visible_parameter_annotations_length, 1.441 + u1* runtime_invisible_parameter_annotations, 1.442 + int runtime_invisible_parameter_annotations_length, 1.443 + u1* runtime_visible_type_annotations, 1.444 + int runtime_visible_type_annotations_length, 1.445 + u1* runtime_invisible_type_annotations, 1.446 + int runtime_invisible_type_annotations_length, 1.447 + u1* annotation_default, 1.448 + int annotation_default_length, 1.449 + TRAPS); 1.450 + 1.451 + // lays out fields in class and returns the total oopmap count 1.452 + void layout_fields(Handle class_loader, FieldAllocationCount* fac, 1.453 + ClassAnnotationCollector* parsed_annotations, 1.454 + FieldLayoutInfo* info, TRAPS); 1.455 + 1.456 + public: 1.457 + // Constructor 1.458 + ClassFileParser(ClassFileStream* st) { set_stream(st); } 1.459 + ~ClassFileParser(); 1.460 + 1.461 + // Parse .class file and return new Klass*. The Klass* is not hooked up 1.462 + // to the system dictionary or any other structures, so a .class file can 1.463 + // be loaded several times if desired. 1.464 + // The system dictionary hookup is done by the caller. 1.465 + // 1.466 + // "parsed_name" is updated by this method, and is the name found 1.467 + // while parsing the stream. 1.468 + instanceKlassHandle parseClassFile(Symbol* name, 1.469 + ClassLoaderData* loader_data, 1.470 + Handle protection_domain, 1.471 + TempNewSymbol& parsed_name, 1.472 + bool verify, 1.473 + TRAPS) { 1.474 + KlassHandle no_host_klass; 1.475 + return parseClassFile(name, loader_data, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD); 1.476 + } 1.477 + instanceKlassHandle parseClassFile(Symbol* name, 1.478 + ClassLoaderData* loader_data, 1.479 + Handle protection_domain, 1.480 + KlassHandle host_klass, 1.481 + GrowableArray<Handle>* cp_patches, 1.482 + TempNewSymbol& parsed_name, 1.483 + bool verify, 1.484 + TRAPS); 1.485 + 1.486 + // Verifier checks 1.487 + static void check_super_class_access(instanceKlassHandle this_klass, TRAPS); 1.488 + static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS); 1.489 + static void check_final_method_override(instanceKlassHandle this_klass, TRAPS); 1.490 + static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS); 1.491 +}; 1.492 + 1.493 +#endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP