src/share/vm/classfile/classFileParser.hpp

Wed, 08 Apr 2009 10:56:49 -0700

author
jrose
date
Wed, 08 Apr 2009 10:56:49 -0700
changeset 1145
e5b0439ef4ae
parent 905
ad8c8ca4ab0f
child 1310
6a93908f268f
permissions
-rw-r--r--

6655638: dynamic languages need method handles
Summary: initial implementation, with known omissions (x86/64, sparc, compiler optim., c-oops, C++ interp.)
Reviewed-by: kvn, twisti, never

     1 /*
     2  * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // Parser for for .class files
    26 //
    27 // The bytes describing the class file structure is read from a Stream object
    29 class ClassFileParser VALUE_OBJ_CLASS_SPEC {
    30  private:
    31   bool _need_verify;
    32   bool _relax_verify;
    33   u2   _major_version;
    34   u2   _minor_version;
    35   symbolHandle _class_name;
    36   KlassHandle _host_klass;
    37   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
    39   bool _has_finalizer;
    40   bool _has_empty_finalizer;
    41   bool _has_vanilla_constructor;
    43   enum { fixed_buffer_size = 128 };
    44   u_char linenumbertable_buffer[fixed_buffer_size];
    46   ClassFileStream* _stream;              // Actual input stream
    48   enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
    50   // Accessors
    51   ClassFileStream* stream()                        { return _stream; }
    52   void set_stream(ClassFileStream* st)             { _stream = st; }
    54   // Constant pool parsing
    55   void parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS);
    57   constantPoolHandle parse_constant_pool(TRAPS);
    59   // Interface parsing
    60   objArrayHandle parse_interfaces(constantPoolHandle cp,
    61                                   int length,
    62                                   Handle class_loader,
    63                                   Handle protection_domain,
    64                                   PerfTraceTime* vmtimer,
    65                                   symbolHandle class_name,
    66                                   TRAPS);
    68   // Field parsing
    69   void parse_field_attributes(constantPoolHandle cp, u2 attributes_count,
    70                               bool is_static, u2 signature_index,
    71                               u2* constantvalue_index_addr,
    72                               bool* is_synthetic_addr,
    73                               u2* generic_signature_index_addr,
    74                               typeArrayHandle* field_annotations, TRAPS);
    75   typeArrayHandle parse_fields(constantPoolHandle cp, bool is_interface,
    76                                struct FieldAllocationCount *fac,
    77                                objArrayHandle* fields_annotations, TRAPS);
    79   // Method parsing
    80   methodHandle parse_method(constantPoolHandle cp, bool is_interface,
    81                             AccessFlags* promoted_flags,
    82                             typeArrayHandle* method_annotations,
    83                             typeArrayHandle* method_parameter_annotations,
    84                             typeArrayHandle* method_default_annotations,
    85                             TRAPS);
    86   objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface,
    87                                 AccessFlags* promoted_flags,
    88                                 bool* has_final_method,
    89                                 objArrayOop* methods_annotations_oop,
    90                                 objArrayOop* methods_parameter_annotations_oop,
    91                                 objArrayOop* methods_default_annotations_oop,
    92                                 TRAPS);
    93   typeArrayHandle sort_methods (objArrayHandle methods,
    94                                 objArrayHandle methods_annotations,
    95                                 objArrayHandle methods_parameter_annotations,
    96                                 objArrayHandle methods_default_annotations,
    97                                 TRAPS);
    98   typeArrayHandle parse_exception_table(u4 code_length, u4 exception_table_length,
    99                                         constantPoolHandle cp, TRAPS);
   100   void parse_linenumber_table(
   101       u4 code_attribute_length, u4 code_length,
   102       CompressedLineNumberWriteStream** write_stream, TRAPS);
   103   u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length,
   104                                 constantPoolHandle cp, u2* localvariable_table_length,
   105                                 bool isLVTT, TRAPS);
   106   u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
   107                                constantPoolHandle cp, TRAPS);
   108   void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
   109                         u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS);
   110   typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS);
   112   // Classfile attribute parsing
   113   void parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
   114   void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
   115                                                 instanceKlassHandle k, int length, TRAPS);
   116   u2   parse_classfile_inner_classes_attribute(constantPoolHandle cp,
   117                                                instanceKlassHandle k, TRAPS);
   118   void parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
   119   void parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
   120   void parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
   122   // Annotations handling
   123   typeArrayHandle assemble_annotations(u1* runtime_visible_annotations,
   124                                        int runtime_visible_annotations_length,
   125                                        u1* runtime_invisible_annotations,
   126                                        int runtime_invisible_annotations_length, TRAPS);
   128   // Final setup
   129   int  compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_count,
   130                             int first_nonstatic_oop_offset);
   131   void fill_oop_maps(instanceKlassHandle k, int nonstatic_oop_map_count,
   132                      u2* nonstatic_oop_offsets, u2* nonstatic_oop_length);
   133   void set_precomputed_flags(instanceKlassHandle k);
   134   objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
   135                                                objArrayHandle local_ifs, TRAPS);
   137   // Special handling for certain classes.
   138   // Add the "discovered" field to java.lang.ref.Reference if
   139   // it does not exist.
   140   void java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
   141     constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS);
   142   // Adjust the field allocation counts for java.lang.Class to add
   143   // fake fields.
   144   void java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
   145     FieldAllocationCount *fac_ptr, TRAPS);
   146   // Adjust the next_nonstatic_oop_offset to place the fake fields
   147   // before any Java fields.
   148   void java_lang_Class_fix_post(int* next_nonstatic_oop_offset);
   149   // Adjust the field allocation counts for java.dyn.MethodHandle to add
   150   // a fake address (void*) field.
   151   void java_dyn_MethodHandle_fix_pre(constantPoolHandle cp,
   152                                      typeArrayHandle* fields_ptr,
   153                                      FieldAllocationCount *fac_ptr, TRAPS);
   155   // Format checker methods
   156   void classfile_parse_error(const char* msg, TRAPS);
   157   void classfile_parse_error(const char* msg, int index, TRAPS);
   158   void classfile_parse_error(const char* msg, const char *name, TRAPS);
   159   void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
   160   inline void guarantee_property(bool b, const char* msg, TRAPS) {
   161     if (!b) { classfile_parse_error(msg, CHECK); }
   162   }
   164   inline void assert_property(bool b, const char* msg, TRAPS) {
   165 #ifdef ASSERT
   166     if (!b) { fatal(msg); }
   167 #endif
   168   }
   170   inline void check_property(bool property, const char* msg, int index, TRAPS) {
   171     if (_need_verify) {
   172       guarantee_property(property, msg, index, CHECK);
   173     } else {
   174       assert_property(property, msg, CHECK);
   175     }
   176   }
   178   inline void check_property(bool property, const char* msg, TRAPS) {
   179     if (_need_verify) {
   180       guarantee_property(property, msg, CHECK);
   181     } else {
   182       assert_property(property, msg, CHECK);
   183     }
   184   }
   186   inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
   187     if (!b) { classfile_parse_error(msg, index, CHECK); }
   188   }
   189   inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
   190     if (!b) { classfile_parse_error(msg, name, CHECK); }
   191   }
   192   inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
   193     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
   194   }
   196   bool is_supported_version(u2 major, u2 minor);
   197   bool has_illegal_visibility(jint flags);
   199   void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
   200   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
   201   void verify_legal_class_name(symbolHandle name, TRAPS);
   202   void verify_legal_field_name(symbolHandle name, TRAPS);
   203   void verify_legal_method_name(symbolHandle name, TRAPS);
   204   void verify_legal_field_signature(symbolHandle fieldname, symbolHandle signature, TRAPS);
   205   int  verify_legal_method_signature(symbolHandle methodname, symbolHandle signature, TRAPS);
   206   void verify_legal_class_modifiers(jint flags, TRAPS);
   207   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
   208   void verify_legal_method_modifiers(jint flags, bool is_interface, symbolHandle name, TRAPS);
   209   bool verify_unqualified_name(char* name, unsigned int length, int type);
   210   char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
   211   char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);
   213   bool is_anonymous() {
   214     assert(AnonymousClasses || _host_klass.is_null(), "");
   215     return _host_klass.not_null();
   216   }
   217   bool has_cp_patch_at(int index) {
   218     assert(AnonymousClasses, "");
   219     assert(index >= 0, "oob");
   220     return (_cp_patches != NULL
   221             && index < _cp_patches->length()
   222             && _cp_patches->adr_at(index)->not_null());
   223   }
   224   Handle cp_patch_at(int index) {
   225     assert(has_cp_patch_at(index), "oob");
   226     return _cp_patches->at(index);
   227   }
   228   Handle clear_cp_patch_at(int index) {
   229     Handle patch = cp_patch_at(index);
   230     _cp_patches->at_put(index, Handle());
   231     assert(!has_cp_patch_at(index), "");
   232     return patch;
   233   }
   234   void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);
   236   // Wrapper for constantTag.is_klass_[or_]reference.
   237   // In older versions of the VM, klassOops cannot sneak into early phases of
   238   // constant pool construction, but in later versions they can.
   239   // %%% Let's phase out the old is_klass_reference.
   240   bool is_klass_reference(constantPoolHandle cp, int index) {
   241     return ((LinkWellKnownClasses || AnonymousClasses)
   242             ? cp->tag_at(index).is_klass_or_reference()
   243             : cp->tag_at(index).is_klass_reference());
   244   }
   246  public:
   247   // Constructor
   248   ClassFileParser(ClassFileStream* st) { set_stream(st); }
   250   // Parse .class file and return new klassOop. The klassOop is not hooked up
   251   // to the system dictionary or any other structures, so a .class file can
   252   // be loaded several times if desired.
   253   // The system dictionary hookup is done by the caller.
   254   //
   255   // "parsed_name" is updated by this method, and is the name found
   256   // while parsing the stream.
   257   instanceKlassHandle parseClassFile(symbolHandle name,
   258                                      Handle class_loader,
   259                                      Handle protection_domain,
   260                                      symbolHandle& parsed_name,
   261                                      TRAPS) {
   262     KlassHandle no_host_klass;
   263     return parseClassFile(name, class_loader, protection_domain, no_host_klass, NULL, parsed_name, THREAD);
   264   }
   265   instanceKlassHandle parseClassFile(symbolHandle name,
   266                                      Handle class_loader,
   267                                      Handle protection_domain,
   268                                      KlassHandle host_klass,
   269                                      GrowableArray<Handle>* cp_patches,
   270                                      symbolHandle& parsed_name,
   271                                      TRAPS);
   273   // Verifier checks
   274   static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
   275   static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
   276   static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
   277   static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
   278 };

mercurial