classFileParser.hpp 16.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33
#ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
#define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP

#include "classfile/classFileStream.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.hpp"
#include "runtime/handles.inline.hpp"
#include "utilities/accessFlags.hpp"
34
#include "classfile/symbolTable.hpp"
35

36 37 38
class FieldAllocationCount;


D
duke 已提交
39 40 41 42 43 44 45 46 47 48
// Parser for for .class files
//
// The bytes describing the class file structure is read from a Stream object

class ClassFileParser VALUE_OBJ_CLASS_SPEC {
 private:
  bool _need_verify;
  bool _relax_verify;
  u2   _major_version;
  u2   _minor_version;
49
  Symbol* _class_name;
50
  KlassHandle _host_klass;
51
  GrowableArray<Handle>* _cp_patches; // overrides for CP entries
D
duke 已提交
52

53
  // precomputed flags
D
duke 已提交
54 55 56
  bool _has_finalizer;
  bool _has_empty_finalizer;
  bool _has_vanilla_constructor;
57 58 59 60 61 62
  int _max_bootstrap_specifier_index;  // detects BSS values

  // class attributes parsed before the instance klass is created:
  bool       _synthetic_flag;
  Symbol*    _sourcefile;
  Symbol*    _generic_signature;
K
Merge  
kvn 已提交
63 64
  char*      _sde_buffer;
  int        _sde_length;
65 66 67 68 69 70
  typeArrayHandle _inner_classes;
  typeArrayHandle _annotations;

  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; }
K
Merge  
kvn 已提交
71
  void set_class_sde_buffer(char* x, int len)     { _sde_buffer = x; _sde_length = len; }
72 73 74 75 76 77
  void set_class_inner_classes(typeArrayHandle x) { _inner_classes = x; }
  void set_class_annotations(typeArrayHandle x)   { _annotations = x; }
  void init_parsed_class_attributes() {
    _synthetic_flag = false;
    _sourcefile = NULL;
    _generic_signature = NULL;
K
Merge  
kvn 已提交
78 79
    _sde_buffer = NULL;
    _sde_length = 0;
80 81 82 83 84 85 86 87 88 89 90 91
    // initialize the other flags too:
    _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
    _max_bootstrap_specifier_index = -1;
  }
  void apply_parsed_class_attributes(instanceKlassHandle k);  // update k

  class AnnotationCollector {
  public:
    enum Location { _in_field, _in_method, _in_class };
    enum ID {
      _unknown = 0,
      _method_ForceInline,
92 93 94
      _method_DontInline,
      _method_LambdaForm_Compiled,
      _method_LambdaForm_Hidden,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      _annotation_LIMIT
    };
    const Location _location;
    int _annotations_present;
    AnnotationCollector(Location location)
    : _location(location), _annotations_present(0)
    {
      assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
    }
    // If this annotation name has an ID, report it (or _none).
    ID annotation_index(Symbol* name);
    // Set the annotation name:
    void set_annotation(ID id) {
      assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
      _annotations_present |= nth_bit((int)id);
    }
    // Report if the annotation is present.
    bool has_any_annotations() { return _annotations_present != 0; }
    bool has_annotation(ID id) { return (nth_bit((int)id) & _annotations_present) != 0; }
  };
  class FieldAnnotationCollector: public AnnotationCollector {
  public:
    FieldAnnotationCollector() : AnnotationCollector(_in_field) { }
    void apply_to(FieldInfo* f);
  };
  class MethodAnnotationCollector: public AnnotationCollector {
  public:
    MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
    void apply_to(methodHandle m);
  };
  class ClassAnnotationCollector: public AnnotationCollector {
  public:
    ClassAnnotationCollector() : AnnotationCollector(_in_class) { }
    void apply_to(instanceKlassHandle k);
  };
130

D
duke 已提交
131 132 133 134 135 136 137 138 139 140 141 142
  enum { fixed_buffer_size = 128 };
  u_char linenumbertable_buffer[fixed_buffer_size];

  ClassFileStream* _stream;              // Actual input stream

  enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names

  // Accessors
  ClassFileStream* stream()                        { return _stream; }
  void set_stream(ClassFileStream* st)             { _stream = st; }

  // Constant pool parsing
143 144
  void parse_constant_pool_entries(Handle class_loader,
                                   constantPoolHandle cp, int length, TRAPS);
D
duke 已提交
145

146
  constantPoolHandle parse_constant_pool(Handle class_loader, TRAPS);
D
duke 已提交
147 148 149 150 151 152

  // Interface parsing
  objArrayHandle parse_interfaces(constantPoolHandle cp,
                                  int length,
                                  Handle class_loader,
                                  Handle protection_domain,
153
                                  Symbol* class_name,
D
duke 已提交
154 155 156 157 158 159 160 161
                                  TRAPS);

  // Field parsing
  void parse_field_attributes(constantPoolHandle cp, u2 attributes_count,
                              bool is_static, u2 signature_index,
                              u2* constantvalue_index_addr,
                              bool* is_synthetic_addr,
                              u2* generic_signature_index_addr,
162 163 164
                              typeArrayHandle* field_annotations,
                              FieldAnnotationCollector* parsed_annotations,
                              TRAPS);
165 166 167 168
  typeArrayHandle parse_fields(Symbol* class_name,
                               constantPoolHandle cp, bool is_interface,
                               FieldAllocationCount *fac,
                               objArrayHandle* fields_annotations,
169
                               u2* java_fields_count_ptr, TRAPS);
D
duke 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  // Method parsing
  methodHandle parse_method(constantPoolHandle cp, bool is_interface,
                            AccessFlags* promoted_flags,
                            typeArrayHandle* method_annotations,
                            typeArrayHandle* method_parameter_annotations,
                            typeArrayHandle* method_default_annotations,
                            TRAPS);
  objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface,
                                AccessFlags* promoted_flags,
                                bool* has_final_method,
                                objArrayOop* methods_annotations_oop,
                                objArrayOop* methods_parameter_annotations_oop,
                                objArrayOop* methods_default_annotations_oop,
                                TRAPS);
  typeArrayHandle sort_methods (objArrayHandle methods,
                                objArrayHandle methods_annotations,
                                objArrayHandle methods_parameter_annotations,
                                objArrayHandle methods_default_annotations,
                                TRAPS);
190 191
  u2* parse_exception_table(u4 code_length, u4 exception_table_length,
                            constantPoolHandle cp, TRAPS);
D
duke 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
  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,
                                bool isLVTT, TRAPS);
  u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
                               constantPoolHandle cp, 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);
  typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS);

  // Classfile attribute parsing
205 206
  void parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS);
  void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, int length, TRAPS);
207 208 209 210 211
  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,
212 213 214 215 216 217 218
                                               TRAPS);
  void parse_classfile_attributes(constantPoolHandle cp,
                                  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(constantPoolHandle cp, u4 attribute_length, TRAPS);
D
duke 已提交
219 220 221 222 223 224

  // Annotations handling
  typeArrayHandle assemble_annotations(u1* runtime_visible_annotations,
                                       int runtime_visible_annotations_length,
                                       u1* runtime_invisible_annotations,
                                       int runtime_invisible_annotations_length, TRAPS);
225 226 227 228 229 230
  int skip_annotation(u1* buffer, int limit, int index);
  int skip_annotation_value(u1* buffer, int limit, int index);
  void parse_annotations(u1* buffer, int limit, constantPoolHandle cp,
                         /* Results (currently, only one result is supported): */
                         AnnotationCollector* result,
                         TRAPS);
D
duke 已提交
231 232

  // Final setup
233 234 235 236 237 238 239
  unsigned int compute_oop_map_count(instanceKlassHandle super,
                                     unsigned int nonstatic_oop_count,
                                     int first_nonstatic_oop_offset);
  void fill_oop_maps(instanceKlassHandle k,
                     unsigned int nonstatic_oop_map_count,
                     int* nonstatic_oop_offsets,
                     unsigned int* nonstatic_oop_counts);
D
duke 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  void set_precomputed_flags(instanceKlassHandle k);
  objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
                                               objArrayHandle local_ifs, TRAPS);

  // Format checker methods
  void classfile_parse_error(const char* msg, TRAPS);
  void classfile_parse_error(const char* msg, int index, TRAPS);
  void classfile_parse_error(const char* msg, const char *name, TRAPS);
  void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
  inline void guarantee_property(bool b, const char* msg, TRAPS) {
    if (!b) { classfile_parse_error(msg, CHECK); }
  }

  inline void assert_property(bool b, const char* msg, TRAPS) {
#ifdef ASSERT
    if (!b) { fatal(msg); }
#endif
  }

  inline void check_property(bool property, const char* msg, int index, TRAPS) {
    if (_need_verify) {
      guarantee_property(property, msg, index, CHECK);
    } else {
      assert_property(property, msg, CHECK);
    }
  }

  inline void check_property(bool property, const char* msg, TRAPS) {
    if (_need_verify) {
      guarantee_property(property, msg, CHECK);
    } else {
      assert_property(property, msg, CHECK);
    }
  }

  inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
    if (!b) { classfile_parse_error(msg, index, CHECK); }
  }
  inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
    if (!b) { classfile_parse_error(msg, name, CHECK); }
  }
  inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
    if (!b) { classfile_parse_error(msg, index, name, CHECK); }
  }

285
  void throwIllegalSignature(
286
      const char* type, Symbol* name, Symbol* sig, TRAPS);
287

D
duke 已提交
288 289 290 291 292
  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_legal_utf8(const unsigned char* buffer, int length, TRAPS);
293 294 295 296 297
  void verify_legal_class_name(Symbol* name, TRAPS);
  void verify_legal_field_name(Symbol* name, TRAPS);
  void verify_legal_method_name(Symbol* name, TRAPS);
  void verify_legal_field_signature(Symbol* fieldname, Symbol* signature, TRAPS);
  int  verify_legal_method_signature(Symbol* methodname, Symbol* signature, TRAPS);
D
duke 已提交
298 299
  void verify_legal_class_modifiers(jint flags, TRAPS);
  void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
300
  void verify_legal_method_modifiers(jint flags, bool is_interface, Symbol* name, TRAPS);
D
duke 已提交
301 302 303 304
  bool verify_unqualified_name(char* name, unsigned int length, int type);
  char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
  char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);

305
  bool is_anonymous() {
306
    assert(EnableInvokeDynamic || _host_klass.is_null(), "");
307 308
    return _host_klass.not_null();
  }
309
  bool has_cp_patch_at(int index) {
310
    assert(EnableInvokeDynamic, "");
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    assert(index >= 0, "oob");
    return (_cp_patches != NULL
            && index < _cp_patches->length()
            && _cp_patches->adr_at(index)->not_null());
  }
  Handle cp_patch_at(int index) {
    assert(has_cp_patch_at(index), "oob");
    return _cp_patches->at(index);
  }
  Handle clear_cp_patch_at(int index) {
    Handle patch = cp_patch_at(index);
    _cp_patches->at_put(index, Handle());
    assert(!has_cp_patch_at(index), "");
    return patch;
  }
  void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);

  // Wrapper for constantTag.is_klass_[or_]reference.
  // In older versions of the VM, klassOops 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) {
333
    return ((LinkWellKnownClasses || EnableInvokeDynamic)
334 335 336 337
            ? cp->tag_at(index).is_klass_or_reference()
            : cp->tag_at(index).is_klass_reference());
  }

D
duke 已提交
338 339 340 341 342 343 344 345 346 347 348
 public:
  // Constructor
  ClassFileParser(ClassFileStream* st) { set_stream(st); }

  // Parse .class file and return new klassOop. The klassOop is not hooked up
  // to the system dictionary or any other structures, so a .class file can
  // be loaded several times if desired.
  // The system dictionary hookup is done by the caller.
  //
  // "parsed_name" is updated by this method, and is the name found
  // while parsing the stream.
349
  instanceKlassHandle parseClassFile(Symbol* name,
D
duke 已提交
350 351
                                     Handle class_loader,
                                     Handle protection_domain,
352
                                     TempNewSymbol& parsed_name,
353
                                     bool verify,
354
                                     TRAPS) {
355
    KlassHandle no_host_klass;
356
    return parseClassFile(name, class_loader, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
357
  }
358
  instanceKlassHandle parseClassFile(Symbol* name,
359 360
                                     Handle class_loader,
                                     Handle protection_domain,
361
                                     KlassHandle host_klass,
362
                                     GrowableArray<Handle>* cp_patches,
363
                                     TempNewSymbol& parsed_name,
364
                                     bool verify,
D
duke 已提交
365 366 367 368 369 370 371 372
                                     TRAPS);

  // Verifier checks
  static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
  static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
  static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
  static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
};
373 374

#endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP