classFileParser.hpp 19.8 KB
Newer Older
D
duke 已提交
1
/*
C
coleenp 已提交
2
 * Copyright (c) 1997, 2013, 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
class FieldAllocationCount;
37
class FieldLayoutInfo;
38 39


D
duke 已提交
40 41 42 43 44 45 46 47 48 49
// 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;
50
  Symbol* _class_name;
51
  ClassLoaderData* _loader_data;
52
  KlassHandle _host_klass;
53
  GrowableArray<Handle>* _cp_patches; // overrides for CP entries
D
duke 已提交
54

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

  // class attributes parsed before the instance klass is created:
  bool       _synthetic_flag;
63 64
  int        _sde_length;
  char*      _sde_buffer;
65 66
  u2         _sourcefile_index;
  u2         _generic_signature_index;
67 68 69 70 71 72 73 74 75 76 77

  // 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;
78
  AnnotationArray* _annotations;
79
  AnnotationArray* _type_annotations;
80 81 82
  Array<AnnotationArray*>* _fields_annotations;
  Array<AnnotationArray*>* _fields_type_annotations;
  InstanceKlass*   _klass;  // InstanceKlass once created.
83

84 85 86 87
  void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
  void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
  void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
  void set_class_sde_buffer(char* x, int len)  { _sde_buffer = x; _sde_length = len; }
88 89 90

  void init_parsed_class_attributes(ClassLoaderData* loader_data) {
    _loader_data = loader_data;
91
    _synthetic_flag = false;
92 93
    _sourcefile_index = 0;
    _generic_signature_index = 0;
K
Merge  
kvn 已提交
94 95
    _sde_buffer = NULL;
    _sde_length = 0;
96 97 98
    // initialize the other flags too:
    _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
    _max_bootstrap_specifier_index = -1;
99 100
    clear_class_metadata();
    _klass = NULL;
101 102
  }
  void apply_parsed_class_attributes(instanceKlassHandle k);  // update k
103 104 105 106 107 108 109 110 111 112 113 114 115
  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;
  }
116 117 118 119 120 121

  class AnnotationCollector {
  public:
    enum Location { _in_field, _in_method, _in_class };
    enum ID {
      _unknown = 0,
122
      _method_CallerSensitive,
123
      _method_ForceInline,
124 125 126
      _method_DontInline,
      _method_LambdaForm_Compiled,
      _method_LambdaForm_Hidden,
127
      _sun_misc_Contended,
128 129 130 131
      _annotation_LIMIT
    };
    const Location _location;
    int _annotations_present;
132 133
    u2 _contended_group;

134 135 136 137 138 139
    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).
140
    ID annotation_index(ClassLoaderData* loader_data, Symbol* name);
141 142 143 144 145 146 147 148
    // 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; }
149 150 151 152 153

    void set_contended_group(u2 group) { _contended_group = group; }
    u2 contended_group() { return _contended_group; }

    bool is_contended() { return has_annotation(_sun_misc_Contended); }
154
  };
155 156

  // This class also doubles as a holder for metadata cleanup.
157
  class FieldAnnotationCollector: public AnnotationCollector {
158 159 160
    ClassLoaderData* _loader_data;
    AnnotationArray* _field_annotations;
    AnnotationArray* _field_type_annotations;
161
  public:
162 163 164 165 166
    FieldAnnotationCollector(ClassLoaderData* loader_data) :
                                 AnnotationCollector(_in_field),
                                 _loader_data(loader_data),
                                 _field_annotations(NULL),
                                 _field_type_annotations(NULL) {}
167
    void apply_to(FieldInfo* f);
168 169 170 171 172 173
    ~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; }
174
  };
175

176 177 178 179 180 181 182 183 184 185
  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);
  };
186

D
duke 已提交
187 188 189 190 191 192 193 194 195 196 197 198
  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
199
  void parse_constant_pool_entries(int length, TRAPS);
D
duke 已提交
200

201
  constantPoolHandle parse_constant_pool(TRAPS);
D
duke 已提交
202 203

  // Interface parsing
204
  Array<Klass*>* parse_interfaces(int length,
D
duke 已提交
205
                                  Handle protection_domain,
206
                                  Symbol* class_name,
207
                                  bool* has_default_methods,
D
duke 已提交
208
                                  TRAPS);
209
  void record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS);
D
duke 已提交
210

211
  instanceKlassHandle parse_super_class(int super_class_index, TRAPS);
D
duke 已提交
212
  // Field parsing
213
  void parse_field_attributes(u2 attributes_count,
D
duke 已提交
214 215 216 217
                              bool is_static, u2 signature_index,
                              u2* constantvalue_index_addr,
                              bool* is_synthetic_addr,
                              u2* generic_signature_index_addr,
218 219
                              FieldAnnotationCollector* parsed_annotations,
                              TRAPS);
220 221
  Array<u2>* parse_fields(Symbol* class_name,
                          bool is_interface,
222 223
                          FieldAllocationCount *fac,
                          u2* java_fields_count_ptr, TRAPS);
D
duke 已提交
224

225 226 227 228 229 230 231 232
  void print_field_layout(Symbol* name,
                          Array<u2>* fields,
                          constantPoolHandle cp,
                          int instance_size,
                          int instance_fields_start,
                          int instance_fields_end,
                          int static_fields_end);

D
duke 已提交
233
  // Method parsing
234
  methodHandle parse_method(bool is_interface,
D
duke 已提交
235 236
                            AccessFlags* promoted_flags,
                            TRAPS);
237
  Array<Method*>* parse_methods(bool is_interface,
D
duke 已提交
238 239
                                AccessFlags* promoted_flags,
                                bool* has_final_method,
240
                                bool* has_default_method,
D
duke 已提交
241
                                TRAPS);
242 243 244 245
  intArray* sort_methods(Array<Method*>* methods);

  u2* parse_exception_table(u4 code_length, u4 exception_table_length,
                            TRAPS);
D
duke 已提交
246 247 248 249
  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,
250
                                u2* localvariable_table_length,
D
duke 已提交
251 252
                                bool isLVTT, TRAPS);
  u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
253
                               TRAPS);
D
duke 已提交
254
  void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
255 256
                        u1* u1_array, u2* u2_array, TRAPS);
  u1* parse_stackmap_table(u4 code_attribute_length, TRAPS);
D
duke 已提交
257 258

  // Classfile attribute parsing
259 260 261
  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,
262 263 264
                                               bool parsed_enclosingmethod_attribute,
                                               u2 enclosing_method_class_index,
                                               u2 enclosing_method_method_index,
265
                                               TRAPS);
266
  void parse_classfile_attributes(ClassAnnotationCollector* parsed_annotations,
267
                                  TRAPS);
268 269 270
  void parse_classfile_synthetic_attribute(TRAPS);
  void parse_classfile_signature_attribute(TRAPS);
  void parse_classfile_bootstrap_methods_attribute(u4 attribute_length, TRAPS);
D
duke 已提交
271 272

  // Annotations handling
273
  AnnotationArray* assemble_annotations(u1* runtime_visible_annotations,
274 275 276
                                        int runtime_visible_annotations_length,
                                        u1* runtime_invisible_annotations,
                                        int runtime_invisible_annotations_length, TRAPS);
277 278
  int skip_annotation(u1* buffer, int limit, int index);
  int skip_annotation_value(u1* buffer, int limit, int index);
279
  void parse_annotations(u1* buffer, int limit,
280 281 282
                         /* Results (currently, only one result is supported): */
                         AnnotationCollector* result,
                         TRAPS);
D
duke 已提交
283 284

  // Final setup
285 286 287 288 289 290 291
  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 已提交
292
  void set_precomputed_flags(instanceKlassHandle k);
293
  Array<Klass*>* compute_transitive_interfaces(instanceKlassHandle super,
294
                                               Array<Klass*>* local_ifs, TRAPS);
D
duke 已提交
295 296 297 298 299 300 301 302 303 304 305 306

  // 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
307 308 309 310 311 312 313 314 315 316 317 318 319
    if (!b) {
      ResourceMark rm(THREAD);
      fatal(err_msg(msg, _class_name->as_C_string()));
    }
#endif
  }

  inline void assert_property(bool b, const char* msg, int index, TRAPS) {
#ifdef ASSERT
    if (!b) {
      ResourceMark rm(THREAD);
      fatal(err_msg(msg, index, _class_name->as_C_string()));
    }
D
duke 已提交
320 321 322 323 324 325 326
#endif
  }

  inline void check_property(bool property, const char* msg, int index, TRAPS) {
    if (_need_verify) {
      guarantee_property(property, msg, index, CHECK);
    } else {
327
      assert_property(property, msg, index, CHECK);
D
duke 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    }
  }

  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); }
  }

349
  void throwIllegalSignature(
350
      const char* type, Symbol* name, Symbol* sig, TRAPS);
351

D
duke 已提交
352 353 354
  bool is_supported_version(u2 major, u2 minor);
  bool has_illegal_visibility(jint flags);

355
  void verify_constantvalue(int constantvalue_index, int signature_index, TRAPS);
D
duke 已提交
356
  void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
357 358 359 360 361
  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 已提交
362 363
  void verify_legal_class_modifiers(jint flags, TRAPS);
  void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
364
  void verify_legal_method_modifiers(jint flags, bool is_interface, Symbol* name, TRAPS);
D
duke 已提交
365 366 367 368
  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);

369
  bool is_anonymous() {
370
    assert(EnableInvokeDynamic || _host_klass.is_null(), "");
371 372
    return _host_klass.not_null();
  }
373
  bool has_cp_patch_at(int index) {
374
    assert(EnableInvokeDynamic, "");
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    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.
393
  // In older versions of the VM, Klass*s cannot sneak into early phases of
394 395
  // constant pool construction, but in later versions they can.
  // %%% Let's phase out the old is_klass_reference.
396 397 398 399 400 401 402 403 404 405 406
  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());
407 408
  }

C
coleenp 已提交
409 410 411 412 413 414 415 416
  void copy_localvariable_table(ConstMethod* cm, int lvt_cnt,
                                u2* localvariable_table_length,
                                u2** localvariable_table_start,
                                int lvtt_cnt,
                                u2* localvariable_type_table_length,
                                u2** localvariable_type_table_start,
                                TRAPS);

417
  void copy_method_annotations(ConstMethod* cm,
C
coleenp 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                               u1* runtime_visible_annotations,
                               int runtime_visible_annotations_length,
                               u1* runtime_invisible_annotations,
                               int runtime_invisible_annotations_length,
                               u1* runtime_visible_parameter_annotations,
                               int runtime_visible_parameter_annotations_length,
                               u1* runtime_invisible_parameter_annotations,
                               int runtime_invisible_parameter_annotations_length,
                               u1* runtime_visible_type_annotations,
                               int runtime_visible_type_annotations_length,
                               u1* runtime_invisible_type_annotations,
                               int runtime_invisible_type_annotations_length,
                               u1* annotation_default,
                               int annotation_default_length,
                               TRAPS);

434 435 436 437 438
  // 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);

D
duke 已提交
439 440 441
 public:
  // Constructor
  ClassFileParser(ClassFileStream* st) { set_stream(st); }
442
  ~ClassFileParser();
D
duke 已提交
443

444
  // Parse .class file and return new Klass*. The Klass* is not hooked up
D
duke 已提交
445 446 447 448 449 450
  // 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.
451
  instanceKlassHandle parseClassFile(Symbol* name,
452
                                     ClassLoaderData* loader_data,
D
duke 已提交
453
                                     Handle protection_domain,
454
                                     TempNewSymbol& parsed_name,
455
                                     bool verify,
456
                                     TRAPS) {
457
    KlassHandle no_host_klass;
458
    return parseClassFile(name, loader_data, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
459
  }
460
  instanceKlassHandle parseClassFile(Symbol* name,
461
                                     ClassLoaderData* loader_data,
462
                                     Handle protection_domain,
463
                                     KlassHandle host_klass,
464
                                     GrowableArray<Handle>* cp_patches,
465
                                     TempNewSymbol& parsed_name,
466
                                     bool verify,
D
duke 已提交
467 468 469 470 471 472 473 474
                                     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);
};
475 476

#endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP