annotate src/share/vm/classfile/classFileParser.hpp @ 4731:1f114331df92

8023730: new hotspot build - hs24-b57 Reviewed-by: jcoomes
author amurillo
date Mon, 26 Aug 2013 12:06:09 -0700
parents 1d7922586cf6
children
rev   line source
duke@0 1 /*
coleenp@3247 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1489 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1489 20 * or visit www.oracle.com if you need additional information or have any
trims@1489 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1869 25 #ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
stefank@1869 26 #define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
stefank@1869 27
stefank@1869 28 #include "classfile/classFileStream.hpp"
stefank@1869 29 #include "memory/resourceArea.hpp"
stefank@1869 30 #include "oops/oop.inline.hpp"
stefank@1869 31 #include "oops/typeArrayOop.hpp"
stefank@1869 32 #include "runtime/handles.inline.hpp"
stefank@1869 33 #include "utilities/accessFlags.hpp"
jrose@3490 34 #include "classfile/symbolTable.hpp"
stefank@1869 35
never@2699 36 class FieldAllocationCount;
never@2699 37
never@2699 38
duke@0 39 // Parser for for .class files
duke@0 40 //
duke@0 41 // The bytes describing the class file structure is read from a Stream object
duke@0 42
duke@0 43 class ClassFileParser VALUE_OBJ_CLASS_SPEC {
duke@0 44 private:
duke@0 45 bool _need_verify;
duke@0 46 bool _relax_verify;
duke@0 47 u2 _major_version;
duke@0 48 u2 _minor_version;
coleenp@2059 49 Symbol* _class_name;
jrose@689 50 KlassHandle _host_klass;
jrose@425 51 GrowableArray<Handle>* _cp_patches; // overrides for CP entries
duke@0 52
jrose@3490 53 // precomputed flags
duke@0 54 bool _has_finalizer;
duke@0 55 bool _has_empty_finalizer;
duke@0 56 bool _has_vanilla_constructor;
jrose@3490 57 int _max_bootstrap_specifier_index; // detects BSS values
duke@0 58
jrose@3490 59 // class attributes parsed before the instance klass is created:
jrose@3490 60 bool _synthetic_flag;
jrose@3490 61 Symbol* _sourcefile;
jrose@3490 62 Symbol* _generic_signature;
kvn@3494 63 char* _sde_buffer;
kvn@3494 64 int _sde_length;
jrose@3490 65 typeArrayHandle _inner_classes;
jrose@3490 66 typeArrayHandle _annotations;
jrose@3490 67
jrose@3490 68 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; }
jrose@3490 69 void set_class_sourcefile(Symbol* x) { _sourcefile = x; }
jrose@3490 70 void set_class_generic_signature(Symbol* x) { _generic_signature = x; }
kvn@3494 71 void set_class_sde_buffer(char* x, int len) { _sde_buffer = x; _sde_length = len; }
jrose@3490 72 void set_class_inner_classes(typeArrayHandle x) { _inner_classes = x; }
jrose@3490 73 void set_class_annotations(typeArrayHandle x) { _annotations = x; }
jrose@3490 74 void init_parsed_class_attributes() {
jrose@3490 75 _synthetic_flag = false;
jrose@3490 76 _sourcefile = NULL;
jrose@3490 77 _generic_signature = NULL;
kvn@3494 78 _sde_buffer = NULL;
kvn@3494 79 _sde_length = 0;
jrose@3490 80 // initialize the other flags too:
jrose@3490 81 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
jrose@3490 82 _max_bootstrap_specifier_index = -1;
jrose@3490 83 }
jrose@3490 84 void apply_parsed_class_attributes(instanceKlassHandle k); // update k
jrose@3490 85
jrose@3490 86 class AnnotationCollector {
jrose@3490 87 public:
jrose@3490 88 enum Location { _in_field, _in_method, _in_class };
jrose@3490 89 enum ID {
jrose@3490 90 _unknown = 0,
jrose@3490 91 _method_ForceInline,
twisti@3534 92 _method_DontInline,
twisti@3534 93 _method_LambdaForm_Compiled,
twisti@3534 94 _method_LambdaForm_Hidden,
jrose@3490 95 _annotation_LIMIT
jrose@3490 96 };
jrose@3490 97 const Location _location;
jrose@3490 98 int _annotations_present;
jrose@3490 99 AnnotationCollector(Location location)
jrose@3490 100 : _location(location), _annotations_present(0)
jrose@3490 101 {
jrose@3490 102 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
jrose@3490 103 }
jrose@3490 104 // If this annotation name has an ID, report it (or _none).
jrose@3490 105 ID annotation_index(Symbol* name);
jrose@3490 106 // Set the annotation name:
jrose@3490 107 void set_annotation(ID id) {
jrose@3490 108 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
jrose@3490 109 _annotations_present |= nth_bit((int)id);
jrose@3490 110 }
jrose@3490 111 // Report if the annotation is present.
jrose@3490 112 bool has_any_annotations() { return _annotations_present != 0; }
jrose@3490 113 bool has_annotation(ID id) { return (nth_bit((int)id) & _annotations_present) != 0; }
jrose@3490 114 };
jrose@3490 115 class FieldAnnotationCollector: public AnnotationCollector {
jrose@3490 116 public:
jrose@3490 117 FieldAnnotationCollector() : AnnotationCollector(_in_field) { }
jrose@3490 118 void apply_to(FieldInfo* f);
jrose@3490 119 };
jrose@3490 120 class MethodAnnotationCollector: public AnnotationCollector {
jrose@3490 121 public:
jrose@3490 122 MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
jrose@3490 123 void apply_to(methodHandle m);
jrose@3490 124 };
jrose@3490 125 class ClassAnnotationCollector: public AnnotationCollector {
jrose@3490 126 public:
jrose@3490 127 ClassAnnotationCollector() : AnnotationCollector(_in_class) { }
jrose@3490 128 void apply_to(instanceKlassHandle k);
jrose@3490 129 };
jrose@1880 130
duke@0 131 enum { fixed_buffer_size = 128 };
duke@0 132 u_char linenumbertable_buffer[fixed_buffer_size];
duke@0 133
duke@0 134 ClassFileStream* _stream; // Actual input stream
duke@0 135
duke@0 136 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
duke@0 137
duke@0 138 // Accessors
duke@0 139 ClassFileStream* stream() { return _stream; }
duke@0 140 void set_stream(ClassFileStream* st) { _stream = st; }
duke@0 141
duke@0 142 // Constant pool parsing
coleenp@3247 143 void parse_constant_pool_entries(Handle class_loader,
coleenp@3247 144 constantPoolHandle cp, int length, TRAPS);
duke@0 145
coleenp@3247 146 constantPoolHandle parse_constant_pool(Handle class_loader, TRAPS);
duke@0 147
duke@0 148 // Interface parsing
duke@0 149 objArrayHandle parse_interfaces(constantPoolHandle cp,
duke@0 150 int length,
duke@0 151 Handle class_loader,
duke@0 152 Handle protection_domain,
coleenp@2059 153 Symbol* class_name,
duke@0 154 TRAPS);
duke@0 155
duke@0 156 // Field parsing
duke@0 157 void parse_field_attributes(constantPoolHandle cp, u2 attributes_count,
duke@0 158 bool is_static, u2 signature_index,
duke@0 159 u2* constantvalue_index_addr,
duke@0 160 bool* is_synthetic_addr,
duke@0 161 u2* generic_signature_index_addr,
jrose@3490 162 typeArrayHandle* field_annotations,
jrose@3490 163 FieldAnnotationCollector* parsed_annotations,
jrose@3490 164 TRAPS);
never@2699 165 typeArrayHandle parse_fields(Symbol* class_name,
never@2699 166 constantPoolHandle cp, bool is_interface,
never@2699 167 FieldAllocationCount *fac,
never@2699 168 objArrayHandle* fields_annotations,
jiangli@2935 169 u2* java_fields_count_ptr, TRAPS);
duke@0 170
duke@0 171 // Method parsing
duke@0 172 methodHandle parse_method(constantPoolHandle cp, bool is_interface,
duke@0 173 AccessFlags* promoted_flags,
duke@0 174 typeArrayHandle* method_annotations,
duke@0 175 typeArrayHandle* method_parameter_annotations,
duke@0 176 typeArrayHandle* method_default_annotations,
duke@0 177 TRAPS);
duke@0 178 objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface,
duke@0 179 AccessFlags* promoted_flags,
duke@0 180 bool* has_final_method,
duke@0 181 objArrayOop* methods_annotations_oop,
duke@0 182 objArrayOop* methods_parameter_annotations_oop,
duke@0 183 objArrayOop* methods_default_annotations_oop,
duke@0 184 TRAPS);
duke@0 185 typeArrayHandle sort_methods (objArrayHandle methods,
duke@0 186 objArrayHandle methods_annotations,
duke@0 187 objArrayHandle methods_parameter_annotations,
duke@0 188 objArrayHandle methods_default_annotations,
duke@0 189 TRAPS);
jiangli@3481 190 u2* parse_exception_table(u4 code_length, u4 exception_table_length,
jiangli@3481 191 constantPoolHandle cp, TRAPS);
duke@0 192 void parse_linenumber_table(
duke@0 193 u4 code_attribute_length, u4 code_length,
duke@0 194 CompressedLineNumberWriteStream** write_stream, TRAPS);
duke@0 195 u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length,
duke@0 196 constantPoolHandle cp, u2* localvariable_table_length,
duke@0 197 bool isLVTT, TRAPS);
duke@0 198 u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
duke@0 199 constantPoolHandle cp, TRAPS);
duke@0 200 void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
duke@0 201 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS);
duke@0 202 typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS);
duke@0 203
duke@0 204 // Classfile attribute parsing
jrose@3490 205 void parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS);
jrose@3490 206 void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, int length, TRAPS);
jiangli@3235 207 u2 parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start,
jiangli@3235 208 bool parsed_enclosingmethod_attribute,
jiangli@3235 209 u2 enclosing_method_class_index,
jiangli@3235 210 u2 enclosing_method_method_index,
jiangli@3235 211 constantPoolHandle cp,
jrose@3490 212 TRAPS);
jrose@3490 213 void parse_classfile_attributes(constantPoolHandle cp,
jrose@3490 214 ClassAnnotationCollector* parsed_annotations,
jrose@3490 215 TRAPS);
jrose@3490 216 void parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS);
jrose@3490 217 void parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS);
jrose@3490 218 void parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, u4 attribute_length, TRAPS);
duke@0 219
duke@0 220 // Annotations handling
duke@0 221 typeArrayHandle assemble_annotations(u1* runtime_visible_annotations,
duke@0 222 int runtime_visible_annotations_length,
duke@0 223 u1* runtime_invisible_annotations,
duke@0 224 int runtime_invisible_annotations_length, TRAPS);
jrose@3490 225 int skip_annotation(u1* buffer, int limit, int index);
jrose@3490 226 int skip_annotation_value(u1* buffer, int limit, int index);
jrose@3490 227 void parse_annotations(u1* buffer, int limit, constantPoolHandle cp,
jrose@3490 228 /* Results (currently, only one result is supported): */
jrose@3490 229 AnnotationCollector* result,
jrose@3490 230 TRAPS);
duke@0 231
duke@0 232 // Final setup
jcoomes@956 233 unsigned int compute_oop_map_count(instanceKlassHandle super,
jcoomes@956 234 unsigned int nonstatic_oop_count,
jcoomes@956 235 int first_nonstatic_oop_offset);
jcoomes@956 236 void fill_oop_maps(instanceKlassHandle k,
jcoomes@956 237 unsigned int nonstatic_oop_map_count,
jcoomes@956 238 int* nonstatic_oop_offsets,
jcoomes@956 239 unsigned int* nonstatic_oop_counts);
duke@0 240 void set_precomputed_flags(instanceKlassHandle k);
duke@0 241 objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
duke@0 242 objArrayHandle local_ifs, TRAPS);
duke@0 243
duke@0 244 // Format checker methods
duke@0 245 void classfile_parse_error(const char* msg, TRAPS);
duke@0 246 void classfile_parse_error(const char* msg, int index, TRAPS);
duke@0 247 void classfile_parse_error(const char* msg, const char *name, TRAPS);
duke@0 248 void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
duke@0 249 inline void guarantee_property(bool b, const char* msg, TRAPS) {
duke@0 250 if (!b) { classfile_parse_error(msg, CHECK); }
duke@0 251 }
duke@0 252
duke@0 253 inline void assert_property(bool b, const char* msg, TRAPS) {
duke@0 254 #ifdef ASSERT
duke@0 255 if (!b) { fatal(msg); }
duke@0 256 #endif
duke@0 257 }
duke@0 258
duke@0 259 inline void check_property(bool property, const char* msg, int index, TRAPS) {
duke@0 260 if (_need_verify) {
duke@0 261 guarantee_property(property, msg, index, CHECK);
duke@0 262 } else {
duke@0 263 assert_property(property, msg, CHECK);
duke@0 264 }
duke@0 265 }
duke@0 266
duke@0 267 inline void check_property(bool property, const char* msg, TRAPS) {
duke@0 268 if (_need_verify) {
duke@0 269 guarantee_property(property, msg, CHECK);
duke@0 270 } else {
duke@0 271 assert_property(property, msg, CHECK);
duke@0 272 }
duke@0 273 }
duke@0 274
duke@0 275 inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
duke@0 276 if (!b) { classfile_parse_error(msg, index, CHECK); }
duke@0 277 }
duke@0 278 inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
duke@0 279 if (!b) { classfile_parse_error(msg, name, CHECK); }
duke@0 280 }
duke@0 281 inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
duke@0 282 if (!b) { classfile_parse_error(msg, index, name, CHECK); }
duke@0 283 }
duke@0 284
kamg@1503 285 void throwIllegalSignature(
coleenp@2059 286 const char* type, Symbol* name, Symbol* sig, TRAPS);
kamg@1503 287
duke@0 288 bool is_supported_version(u2 major, u2 minor);
duke@0 289 bool has_illegal_visibility(jint flags);
duke@0 290
duke@0 291 void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
duke@0 292 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
coleenp@2059 293 void verify_legal_class_name(Symbol* name, TRAPS);
coleenp@2059 294 void verify_legal_field_name(Symbol* name, TRAPS);
coleenp@2059 295 void verify_legal_method_name(Symbol* name, TRAPS);
coleenp@2059 296 void verify_legal_field_signature(Symbol* fieldname, Symbol* signature, TRAPS);
coleenp@2059 297 int verify_legal_method_signature(Symbol* methodname, Symbol* signature, TRAPS);
duke@0 298 void verify_legal_class_modifiers(jint flags, TRAPS);
duke@0 299 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
coleenp@2059 300 void verify_legal_method_modifiers(jint flags, bool is_interface, Symbol* name, TRAPS);
duke@0 301 bool verify_unqualified_name(char* name, unsigned int length, int type);
duke@0 302 char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
duke@0 303 char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);
duke@0 304
jrose@689 305 bool is_anonymous() {
twisti@2255 306 assert(EnableInvokeDynamic || _host_klass.is_null(), "");
jrose@689 307 return _host_klass.not_null();
jrose@689 308 }
jrose@425 309 bool has_cp_patch_at(int index) {
twisti@2255 310 assert(EnableInvokeDynamic, "");
jrose@425 311 assert(index >= 0, "oob");
jrose@425 312 return (_cp_patches != NULL
jrose@425 313 && index < _cp_patches->length()
jrose@425 314 && _cp_patches->adr_at(index)->not_null());
jrose@425 315 }
jrose@425 316 Handle cp_patch_at(int index) {
jrose@425 317 assert(has_cp_patch_at(index), "oob");
jrose@425 318 return _cp_patches->at(index);
jrose@425 319 }
jrose@425 320 Handle clear_cp_patch_at(int index) {
jrose@425 321 Handle patch = cp_patch_at(index);
jrose@425 322 _cp_patches->at_put(index, Handle());
jrose@425 323 assert(!has_cp_patch_at(index), "");
jrose@425 324 return patch;
jrose@425 325 }
jrose@425 326 void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);
jrose@425 327
jrose@425 328 // Wrapper for constantTag.is_klass_[or_]reference.
jrose@425 329 // In older versions of the VM, klassOops cannot sneak into early phases of
jrose@425 330 // constant pool construction, but in later versions they can.
jrose@425 331 // %%% Let's phase out the old is_klass_reference.
jrose@425 332 bool is_klass_reference(constantPoolHandle cp, int index) {
twisti@4000 333 return (EnableInvokeDynamic
jrose@425 334 ? cp->tag_at(index).is_klass_or_reference()
jrose@425 335 : cp->tag_at(index).is_klass_reference());
jrose@425 336 }
jrose@425 337
duke@0 338 public:
duke@0 339 // Constructor
duke@0 340 ClassFileParser(ClassFileStream* st) { set_stream(st); }
duke@0 341
duke@0 342 // Parse .class file and return new klassOop. The klassOop is not hooked up
duke@0 343 // to the system dictionary or any other structures, so a .class file can
duke@0 344 // be loaded several times if desired.
duke@0 345 // The system dictionary hookup is done by the caller.
duke@0 346 //
duke@0 347 // "parsed_name" is updated by this method, and is the name found
duke@0 348 // while parsing the stream.
coleenp@2059 349 instanceKlassHandle parseClassFile(Symbol* name,
duke@0 350 Handle class_loader,
duke@0 351 Handle protection_domain,
coleenp@2059 352 TempNewSymbol& parsed_name,
acorn@977 353 bool verify,
jrose@425 354 TRAPS) {
jrose@689 355 KlassHandle no_host_klass;
acorn@977 356 return parseClassFile(name, class_loader, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
jrose@425 357 }
coleenp@2059 358 instanceKlassHandle parseClassFile(Symbol* name,
jrose@425 359 Handle class_loader,
jrose@425 360 Handle protection_domain,
jrose@689 361 KlassHandle host_klass,
jrose@425 362 GrowableArray<Handle>* cp_patches,
coleenp@2059 363 TempNewSymbol& parsed_name,
acorn@977 364 bool verify,
duke@0 365 TRAPS);
duke@0 366
duke@0 367 // Verifier checks
duke@0 368 static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
duke@0 369 static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
duke@0 370 static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
duke@0 371 static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
duke@0 372 };
stefank@1869 373
stefank@1869 374 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP