1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  27 
  28 #include "memory/referenceType.hpp"
  29 #include "runtime/handles.inline.hpp"
  30 #include "oops/constantPool.hpp"
  31 #include "oops/typeArrayOop.hpp"
  32 #include "utilities/accessFlags.hpp"
  33 
  34 class Annotations;
  35 template <typename T>
  36 class Array;
  37 class ClassFileStream;
  38 class ClassLoaderData;
  39 class CompressedLineNumberWriteStream;
  40 class ConstMethod;
  41 class FieldInfo;
  42 template <typename T>
  43 class GrowableArray;
  44 class InstanceKlass;
  45 class Symbol;
  46 class TempNewSymbol;
  47 
  48 // Parser for for .class files
  49 //
  50 // The bytes describing the class file structure is read from a Stream object
  51 
  52 class ClassFileParser VALUE_OBJ_CLASS_SPEC {
  53 
  54  class ClassAnnotationCollector;
  55  class FieldAllocationCount;
  56  class FieldAnnotationCollector;
  57  class FieldLayoutInfo;
  58 
  59  public:
  60   // The ClassFileParser has an associated "publicity" level
  61   // It is used to control which subsystems (if any)
  62   // will observe the parsing (logging, events, tracing).
  63   // Default level is "BROADCAST", which is equivalent to
  64   // a "public" parsing attempt.
  65   //
  66   // "INTERNAL" level should be entirely private to the
  67   // caller - this allows for internal reuse of ClassFileParser
  68   //
  69   enum Publicity {
  70     INTERNAL,
  71     BROADCAST,
  72     NOF_PUBLICITY_LEVELS
  73   };
  74 
  75  private:
  76   const ClassFileStream* _stream; // Actual input stream
  77   const Symbol* _requested_name;
  78   Symbol* _class_name;
  79   mutable ClassLoaderData* _loader_data;
  80   const Klass* _host_klass;
  81   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  82   TempNewSymbol* _parsed_name;
  83 
  84   // Metadata created before the instance klass is created.  Must be deallocated
  85   // if not transferred to the InstanceKlass upon successful class loading
  86   // in which case these pointers have been set to NULL.
  87   const InstanceKlass* _super_klass;
  88   ConstantPool* _cp;
  89   Array<u2>* _fields;
  90   Array<Method*>* _methods;
  91   Array<u2>* _inner_classes;
  92   Array<Klass*>* _local_interfaces;
  93   Array<Klass*>* _transitive_interfaces;
  94   Annotations* _combined_annotations;
  95   AnnotationArray* _annotations;
  96   AnnotationArray* _type_annotations;
  97   Array<AnnotationArray*>* _fields_annotations;
  98   Array<AnnotationArray*>* _fields_type_annotations;
  99   InstanceKlass* _klass;  // InstanceKlass* once created.
 100   InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
 101 
 102   ClassAnnotationCollector* _parsed_annotations;
 103   FieldAllocationCount* _fac;
 104   FieldLayoutInfo* _field_info;
 105   const intArray* _method_ordering;
 106   GrowableArray<Method*>* _all_mirandas;
 107 
 108   enum { fixed_buffer_size = 128 };
 109   u_char _linenumbertable_buffer[fixed_buffer_size];
 110 
 111   // Size of Java vtable (in words)
 112   int _vtable_size;
 113   int _itable_size;
 114 
 115   int _num_miranda_methods;
 116 
 117   ReferenceType _rt;
 118   Handle _protection_domain;
 119   AccessFlags _access_flags;
 120 
 121   // for tracing and notifications
 122   Publicity _pub_level;
 123 
 124   // class attributes parsed before the instance klass is created:
 125   bool _synthetic_flag;
 126   int _sde_length;
 127   const char* _sde_buffer;
 128   u2 _sourcefile_index;
 129   u2 _generic_signature_index;
 130 
 131   u2 _major_version;
 132   u2 _minor_version;
 133   u2 _this_class_index;
 134   u2 _super_class_index;
 135   u2 _itfs_len;
 136   u2 _java_fields_count;
 137 
 138   bool _need_verify;
 139   bool _relax_verify;
 140 
 141   bool _has_default_methods;
 142   bool _declares_default_methods;
 143   bool _has_final_method;
 144 
 145   // precomputed flags
 146   bool _has_finalizer;
 147   bool _has_empty_finalizer;
 148   bool _has_vanilla_constructor;
 149   int _max_bootstrap_specifier_index;  // detects BSS values
 150 
 151   void parse_stream(const ClassFileStream* const stream, TRAPS);
 152 
 153   void post_process_parsed_stream(const ClassFileStream* const stream,
 154                                   ConstantPool* cp,
 155                                   TRAPS);
 156 
 157   void fill_instance_klass(InstanceKlass* ik, TRAPS);
 158   void set_klass(InstanceKlass* instance);
 159 
 160   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 161   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 162   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 163   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 164 
 165   void create_combined_annotations(TRAPS);
 166   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 167   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 168   void clear_class_metadata();
 169 
 170   // Constant pool parsing
 171   void parse_constant_pool_entries(const ClassFileStream* const stream,
 172                                    ConstantPool* cp,
 173                                    const int length,
 174                                    TRAPS);
 175 
 176   void parse_constant_pool(const ClassFileStream* const cfs,
 177                            ConstantPool* const cp,
 178                            const int length,
 179                            TRAPS);
 180 
 181   // Interface parsing
 182   void parse_interfaces(const ClassFileStream* const stream,
 183                         const int itfs_len,
 184                         ConstantPool* const cp,
 185                         bool* has_default_methods,
 186                         TRAPS);
 187 
 188   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 189                                          const int super_class_index,
 190                                          const bool need_verify,
 191                                          TRAPS);
 192 
 193   // Field parsing
 194   void parse_field_attributes(const ClassFileStream* const cfs,
 195                               u2 attributes_count,
 196                               bool is_static,
 197                               u2 signature_index,
 198                               u2* const constantvalue_index_addr,
 199                               bool* const is_synthetic_addr,
 200                               u2* const generic_signature_index_addr,
 201                               FieldAnnotationCollector* parsed_annotations,
 202                               TRAPS);
 203 
 204   void parse_fields(const ClassFileStream* const cfs,
 205                     bool is_interface,
 206                     FieldAllocationCount* const fac,
 207                     ConstantPool* cp,
 208                     const int cp_size,
 209                     u2* const java_fields_count_ptr,
 210                     TRAPS);
 211 
 212   // Method parsing
 213   Method* parse_method(const ClassFileStream* const cfs,
 214                        bool is_interface,
 215                        const ConstantPool* cp,
 216                        AccessFlags* const promoted_flags,
 217                        TRAPS);
 218 
 219   void parse_methods(const ClassFileStream* const cfs,
 220                      bool is_interface,
 221                      AccessFlags* const promoted_flags,
 222                      bool* const has_final_method,
 223                      bool* const declares_default_methods,
 224                      TRAPS);
 225 
 226   const u2* parse_exception_table(const ClassFileStream* const stream,
 227                                   u4 code_length,
 228                                   u4 exception_table_length,
 229                                   TRAPS);
 230 
 231   void parse_linenumber_table(u4 code_attribute_length,
 232                               u4 code_length,
 233                               CompressedLineNumberWriteStream**const write_stream,
 234                               TRAPS);
 235 
 236   const u2* parse_localvariable_table(const ClassFileStream* const cfs,
 237                                       u4 code_length,
 238                                       u2 max_locals,
 239                                       u4 code_attribute_length,
 240                                       u2* const localvariable_table_length,
 241                                       bool isLVTT,
 242                                       TRAPS);
 243 
 244   const u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 245                                      u2* const checked_exceptions_length,
 246                                      u4 method_attribute_length,
 247                                      TRAPS);
 248 
 249   void parse_type_array(u2 array_length,
 250                         u4 code_length,
 251                         u4* const u1_index,
 252                         u4* const u2_index,
 253                         u1* const u1_array,
 254                         u2* const u2_array,
 255                         TRAPS);
 256 
 257   // Classfile attribute parsing
 258   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 259   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 260   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 261                                                         int length,
 262                                                         TRAPS);
 263 
 264   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 265                                                const u1* const inner_classes_attribute_start,
 266                                                bool parsed_enclosingmethod_attribute,
 267                                                u2 enclosing_method_class_index,
 268                                                u2 enclosing_method_method_index,
 269                                                TRAPS);
 270 
 271   void parse_classfile_attributes(const ClassFileStream* const cfs,
 272                                   ConstantPool* cp,
 273                                   ClassAnnotationCollector* parsed_annotations,
 274                                   TRAPS);
 275 
 276   void parse_classfile_synthetic_attribute(TRAPS);
 277   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 278   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 279                                                    ConstantPool* cp,
 280                                                    u4 attribute_length,
 281                                                    TRAPS);
 282 
 283   // Annotations handling
 284   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 285                                         int runtime_visible_annotations_length,
 286                                         const u1* const runtime_invisible_annotations,
 287                                         int runtime_invisible_annotations_length,
 288                                         TRAPS);
 289 
 290   void set_precomputed_flags(InstanceKlass* k);
 291 
 292   // Format checker methods
 293   void classfile_parse_error(const char* msg, TRAPS) const;
 294   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 295   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 296   void classfile_parse_error(const char* msg,
 297                              int index,
 298                              const char *name,
 299                              TRAPS) const;
 300 
 301   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 302     if (!b) { classfile_parse_error(msg, CHECK); }
 303   }
 304 
 305   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 306   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 307 
 308   inline void assert_property(bool b, const char* msg, TRAPS) const {
 309 #ifdef ASSERT
 310     if (!b) {
 311       report_assert_property_failure(msg, THREAD);
 312     }
 313 #endif
 314   }
 315 
 316   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 317 #ifdef ASSERT
 318     if (!b) {
 319       report_assert_property_failure(msg, index, THREAD);
 320     }
 321 #endif
 322   }
 323 
 324   inline void check_property(bool property,
 325                              const char* msg,
 326                              int index,
 327                              TRAPS) const {
 328     if (_need_verify) {
 329       guarantee_property(property, msg, index, CHECK);
 330     } else {
 331       assert_property(property, msg, index, CHECK);
 332     }
 333   }
 334 
 335   inline void check_property(bool property, const char* msg, TRAPS) const {
 336     if (_need_verify) {
 337       guarantee_property(property, msg, CHECK);
 338     } else {
 339       assert_property(property, msg, CHECK);
 340     }
 341   }
 342 
 343   inline void guarantee_property(bool b,
 344                                  const char* msg,
 345                                  int index,
 346                                  TRAPS) const {
 347     if (!b) { classfile_parse_error(msg, index, CHECK); }
 348   }
 349 
 350   inline void guarantee_property(bool b,
 351                                  const char* msg,
 352                                  const char *name,
 353                                  TRAPS) const {
 354     if (!b) { classfile_parse_error(msg, name, CHECK); }
 355   }
 356 
 357   inline void guarantee_property(bool b,
 358                                  const char* msg,
 359                                  int index,
 360                                  const char *name,
 361                                  TRAPS) const {
 362     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 363   }
 364 
 365   void throwIllegalSignature(const char* type,
 366                              const Symbol* name,
 367                              const Symbol* sig,
 368                              TRAPS) const;
 369 
 370   void verify_constantvalue(const ConstantPool* const cp,
 371                             int constantvalue_index,
 372                             int signature_index,
 373                             TRAPS) const;
 374 
 375   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 376   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 377   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 378   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 379 
 380   void verify_legal_field_signature(const Symbol* fieldname,
 381                                     const Symbol* signature,
 382                                     TRAPS) const;
 383   int  verify_legal_method_signature(const Symbol* methodname,
 384                                      const Symbol* signature,
 385                                      TRAPS) const;
 386 
 387   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 388   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 389   void verify_legal_method_modifiers(jint flags,
 390                                      bool is_interface,
 391                                      const Symbol* name,
 392                                      TRAPS) const;
 393 
 394   const char* skip_over_field_signature(const char* signature,
 395                                         bool void_ok,
 396                                         unsigned int length,
 397                                         TRAPS) const;
 398 
 399   bool has_cp_patch_at(int index) const {
 400     assert(index >= 0, "oob");
 401     return (_cp_patches != NULL
 402             && index < _cp_patches->length()
 403             && _cp_patches->adr_at(index)->not_null());
 404   }
 405 
 406   Handle cp_patch_at(int index) const {
 407     assert(has_cp_patch_at(index), "oob");
 408     return _cp_patches->at(index);
 409   }
 410 
 411   Handle clear_cp_patch_at(int index) {
 412     Handle patch = cp_patch_at(index);
 413     _cp_patches->at_put(index, Handle());
 414     assert(!has_cp_patch_at(index), "");
 415     return patch;
 416   }
 417 
 418   void patch_constant_pool(ConstantPool* cp,
 419                            int index,
 420                            Handle patch,
 421                            TRAPS);
 422 
 423   // Wrapper for constantTag.is_klass_[or_]reference.
 424   // In older versions of the VM, Klass*s cannot sneak into early phases of
 425   // constant pool construction, but in later versions they can.
 426   // %%% Let's phase out the old is_klass_reference.
 427   bool valid_klass_reference_at(int index) const {
 428     return _cp->is_within_bounds(index) &&
 429              _cp->tag_at(index).is_klass_or_reference();
 430   }
 431 
 432   // Checks that the cpool index is in range and is a utf8
 433   bool valid_symbol_at(int cpool_index) const {
 434     return _cp->is_within_bounds(cpool_index) &&
 435              _cp->tag_at(cpool_index).is_utf8();
 436   }
 437 
 438   void copy_localvariable_table(const ConstMethod* cm,
 439                                 int lvt_cnt,
 440                                 u2* const localvariable_table_length,
 441                                 const u2**const localvariable_table_start,
 442                                 int lvtt_cnt,
 443                                 u2* const localvariable_type_table_length,
 444                                 const u2** const localvariable_type_table_start,
 445                                 TRAPS);
 446 
 447   void copy_method_annotations(ConstMethod* cm,
 448                                const u1* runtime_visible_annotations,
 449                                int runtime_visible_annotations_length,
 450                                const u1* runtime_invisible_annotations,
 451                                int runtime_invisible_annotations_length,
 452                                const u1* runtime_visible_parameter_annotations,
 453                                int runtime_visible_parameter_annotations_length,
 454                                const u1* runtime_invisible_parameter_annotations,
 455                                int runtime_invisible_parameter_annotations_length,
 456                                const u1* runtime_visible_type_annotations,
 457                                int runtime_visible_type_annotations_length,
 458                                const u1* runtime_invisible_type_annotations,
 459                                int runtime_invisible_type_annotations_length,
 460                                const u1* annotation_default,
 461                                int annotation_default_length,
 462                                TRAPS);
 463 
 464   // lays out fields in class and returns the total oopmap count
 465   void layout_fields(ConstantPool* cp,
 466                      const FieldAllocationCount* fac,
 467                      const ClassAnnotationCollector* parsed_annotations,
 468                      FieldLayoutInfo* info,
 469                      TRAPS);
 470 
 471  public:
 472   ClassFileParser(ClassFileStream* stream,
 473                   Symbol* name,
 474                   ClassLoaderData* loader_data,
 475                   Handle protection_domain,
 476                   TempNewSymbol* parsed_name,
 477                   const Klass* host_klass,
 478                   GrowableArray<Handle>* cp_patches,
 479                   Publicity pub_level,
 480                   TRAPS);
 481 
 482   ~ClassFileParser();
 483 
 484   InstanceKlass* create_instance_klass(TRAPS);
 485 
 486   const ClassFileStream* clone_stream() const;
 487 
 488   void set_klass_to_deallocate(InstanceKlass* klass);
 489 
 490   int static_field_size() const;
 491   int total_oop_map_count() const;
 492   jint layout_size() const;
 493 
 494   int vtable_size() const { return _vtable_size; }
 495   int itable_size() const { return _itable_size; }
 496 
 497   u2 this_class_index() const { return _this_class_index; }
 498   u2 super_class_index() const { return _super_class_index; }
 499 
 500   bool is_anonymous() const { return _host_klass != NULL; }
 501   bool is_interface() const { return _access_flags.is_interface(); }
 502 
 503   const Klass* host_klass() const { return _host_klass; }
 504   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 505   ClassLoaderData* loader_data() const { return _loader_data; }
 506   const Symbol* class_name() const { return _class_name; }
 507   const Klass* super_klass() const { return _super_klass; }
 508 
 509   ReferenceType reference_type() const { return _rt; }
 510   AccessFlags access_flags() const { return _access_flags; }
 511 
 512   bool is_internal() const { return INTERNAL == _pub_level; }
 513 
 514 };
 515 
 516 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP