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   enum { LegalClass, LegalField, LegalMethod, LegalModule }; // used to verify unqualified names
  76 
  77  private:
  78   const ClassFileStream* _stream; // Actual input stream
  79   const Symbol* _requested_name;
  80   Symbol* _class_name;
  81   mutable ClassLoaderData* _loader_data;
  82   const Klass* _host_klass;
  83   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  84 
  85   // Metadata created before the instance klass is created.  Must be deallocated
  86   // if not transferred to the InstanceKlass upon successful class loading
  87   // in which case these pointers have been set to NULL.
  88   const InstanceKlass* _super_klass;
  89   ConstantPool* _cp;
  90   Array<u2>* _fields;
  91   Array<Method*>* _methods;
  92   Array<u2>* _inner_classes;
  93   Array<Klass*>* _local_interfaces;
  94   Array<Klass*>* _transitive_interfaces;
  95   Annotations* _combined_annotations;
  96   AnnotationArray* _annotations;
  97   AnnotationArray* _type_annotations;
  98   Array<AnnotationArray*>* _fields_annotations;
  99   Array<AnnotationArray*>* _fields_type_annotations;
 100   InstanceKlass* _klass;  // InstanceKlass* once created.
 101   InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
 102 
 103   ClassAnnotationCollector* _parsed_annotations;
 104   FieldAllocationCount* _fac;
 105   FieldLayoutInfo* _field_info;
 106   const intArray* _method_ordering;
 107   GrowableArray<Method*>* _all_mirandas;
 108 
 109   enum { fixed_buffer_size = 128 };
 110   u_char _linenumbertable_buffer[fixed_buffer_size];
 111 
 112   // Size of Java vtable (in words)
 113   int _vtable_size;
 114   int _itable_size;
 115 
 116   int _num_miranda_methods;
 117 
 118   ReferenceType _rt;
 119   Handle _protection_domain;
 120   AccessFlags _access_flags;
 121 
 122   // for tracing and notifications
 123   Publicity _pub_level;
 124 
 125   // class attributes parsed before the instance klass is created:
 126   bool _synthetic_flag;
 127   int _sde_length;
 128   const char* _sde_buffer;
 129   u2 _sourcefile_index;
 130   u2 _generic_signature_index;
 131 
 132   u2 _major_version;
 133   u2 _minor_version;
 134   u2 _this_class_index;
 135   u2 _super_class_index;
 136   u2 _itfs_len;
 137   u2 _java_fields_count;
 138 
 139   bool _need_verify;
 140   bool _relax_verify;
 141 
 142   bool _has_default_methods;
 143   bool _declares_default_methods;
 144   bool _has_final_method;
 145 
 146   // precomputed flags
 147   bool _has_finalizer;
 148   bool _has_empty_finalizer;
 149   bool _has_vanilla_constructor;
 150   int _max_bootstrap_specifier_index;  // detects BSS values
 151 
 152   void parse_stream(const ClassFileStream* const stream, TRAPS);
 153 
 154   void post_process_parsed_stream(const ClassFileStream* const stream,
 155                                   ConstantPool* cp,
 156                                   TRAPS);
 157 
 158   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS);
 159   void set_klass(InstanceKlass* instance);
 160 
 161   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 162   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 163   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 164   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 165 
 166   void create_combined_annotations(TRAPS);
 167   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 168   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 169   void clear_class_metadata();
 170 
 171   // Constant pool parsing
 172   void parse_constant_pool_entries(const ClassFileStream* const stream,
 173                                    ConstantPool* cp,
 174                                    const int length,
 175                                    TRAPS);
 176 
 177   void parse_constant_pool(const ClassFileStream* const cfs,
 178                            ConstantPool* const cp,
 179                            const int length,
 180                            TRAPS);
 181 
 182   // Interface parsing
 183   void parse_interfaces(const ClassFileStream* const stream,
 184                         const int itfs_len,
 185                         ConstantPool* const cp,
 186                         bool* has_default_methods,
 187                         TRAPS);
 188 
 189   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 190                                          const int super_class_index,
 191                                          const bool need_verify,
 192                                          TRAPS);
 193 
 194   // Field parsing
 195   void parse_field_attributes(const ClassFileStream* const cfs,
 196                               u2 attributes_count,
 197                               bool is_static,
 198                               u2 signature_index,
 199                               u2* const constantvalue_index_addr,
 200                               bool* const is_synthetic_addr,
 201                               u2* const generic_signature_index_addr,
 202                               FieldAnnotationCollector* parsed_annotations,
 203                               TRAPS);
 204 
 205   void parse_fields(const ClassFileStream* const cfs,
 206                     bool is_interface,
 207                     FieldAllocationCount* const fac,
 208                     ConstantPool* cp,
 209                     const int cp_size,
 210                     u2* const java_fields_count_ptr,
 211                     TRAPS);
 212 
 213   // Method parsing
 214   Method* parse_method(const ClassFileStream* const cfs,
 215                        bool is_interface,
 216                        const ConstantPool* cp,
 217                        AccessFlags* const promoted_flags,
 218                        TRAPS);
 219 
 220   void parse_methods(const ClassFileStream* const cfs,
 221                      bool is_interface,
 222                      AccessFlags* const promoted_flags,
 223                      bool* const has_final_method,
 224                      bool* const declares_default_methods,
 225                      TRAPS);
 226 
 227   const u2* parse_exception_table(const ClassFileStream* const stream,
 228                                   u4 code_length,
 229                                   u4 exception_table_length,
 230                                   TRAPS);
 231 
 232   void parse_linenumber_table(u4 code_attribute_length,
 233                               u4 code_length,
 234                               CompressedLineNumberWriteStream**const write_stream,
 235                               TRAPS);
 236 
 237   const u2* parse_localvariable_table(const ClassFileStream* const cfs,
 238                                       u4 code_length,
 239                                       u2 max_locals,
 240                                       u4 code_attribute_length,
 241                                       u2* const localvariable_table_length,
 242                                       bool isLVTT,
 243                                       TRAPS);
 244 
 245   const u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 246                                      u2* const checked_exceptions_length,
 247                                      u4 method_attribute_length,
 248                                      TRAPS);
 249 
 250   void parse_type_array(u2 array_length,
 251                         u4 code_length,
 252                         u4* const u1_index,
 253                         u4* const u2_index,
 254                         u1* const u1_array,
 255                         u2* const u2_array,
 256                         TRAPS);
 257 
 258   // Classfile attribute parsing
 259   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 260   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 261   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 262                                                         int length,
 263                                                         TRAPS);
 264 
 265   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 266                                                const u1* const inner_classes_attribute_start,
 267                                                bool parsed_enclosingmethod_attribute,
 268                                                u2 enclosing_method_class_index,
 269                                                u2 enclosing_method_method_index,
 270                                                TRAPS);
 271 
 272   void parse_classfile_attributes(const ClassFileStream* const cfs,
 273                                   ConstantPool* cp,
 274                                   ClassAnnotationCollector* parsed_annotations,
 275                                   TRAPS);
 276 
 277   void parse_classfile_synthetic_attribute(TRAPS);
 278   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 279   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 280                                                    ConstantPool* cp,
 281                                                    u4 attribute_length,
 282                                                    TRAPS);
 283 
 284   // Annotations handling
 285   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 286                                         int runtime_visible_annotations_length,
 287                                         const u1* const runtime_invisible_annotations,
 288                                         int runtime_invisible_annotations_length,
 289                                         TRAPS);
 290 
 291   void set_precomputed_flags(InstanceKlass* k);
 292 
 293   // Format checker methods
 294   void classfile_parse_error(const char* msg, TRAPS) const;
 295   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 296   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 297   void classfile_parse_error(const char* msg,
 298                              int index,
 299                              const char *name,
 300                              TRAPS) const;
 301 
 302   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 303     if (!b) { classfile_parse_error(msg, CHECK); }
 304   }
 305 
 306   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 307   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 308 
 309   inline void assert_property(bool b, const char* msg, TRAPS) const {
 310 #ifdef ASSERT
 311     if (!b) {
 312       report_assert_property_failure(msg, THREAD);
 313     }
 314 #endif
 315   }
 316 
 317   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 318 #ifdef ASSERT
 319     if (!b) {
 320       report_assert_property_failure(msg, index, THREAD);
 321     }
 322 #endif
 323   }
 324 
 325   inline void check_property(bool property,
 326                              const char* msg,
 327                              int index,
 328                              TRAPS) const {
 329     if (_need_verify) {
 330       guarantee_property(property, msg, index, CHECK);
 331     } else {
 332       assert_property(property, msg, index, CHECK);
 333     }
 334   }
 335 
 336   inline void check_property(bool property, const char* msg, TRAPS) const {
 337     if (_need_verify) {
 338       guarantee_property(property, msg, CHECK);
 339     } else {
 340       assert_property(property, msg, CHECK);
 341     }
 342   }
 343 
 344   inline void guarantee_property(bool b,
 345                                  const char* msg,
 346                                  int index,
 347                                  TRAPS) const {
 348     if (!b) { classfile_parse_error(msg, index, CHECK); }
 349   }
 350 
 351   inline void guarantee_property(bool b,
 352                                  const char* msg,
 353                                  const char *name,
 354                                  TRAPS) const {
 355     if (!b) { classfile_parse_error(msg, name, CHECK); }
 356   }
 357 
 358   inline void guarantee_property(bool b,
 359                                  const char* msg,
 360                                  int index,
 361                                  const char *name,
 362                                  TRAPS) const {
 363     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 364   }
 365 
 366   void throwIllegalSignature(const char* type,
 367                              const Symbol* name,
 368                              const Symbol* sig,
 369                              TRAPS) const;
 370 
 371   void verify_constantvalue(const ConstantPool* const cp,
 372                             int constantvalue_index,
 373                             int signature_index,
 374                             TRAPS) const;
 375 
 376   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 377   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 378   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 379   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 380 
 381   void verify_legal_field_signature(const Symbol* fieldname,
 382                                     const Symbol* signature,
 383                                     TRAPS) const;
 384   int  verify_legal_method_signature(const Symbol* methodname,
 385                                      const Symbol* signature,
 386                                      TRAPS) const;
 387 
 388   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 389   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 390   void verify_legal_method_modifiers(jint flags,
 391                                      bool is_interface,
 392                                      const Symbol* name,
 393                                      TRAPS) const;
 394 
 395   const char* skip_over_field_signature(const char* signature,
 396                                         bool void_ok,
 397                                         unsigned int length,
 398                                         TRAPS) const;
 399 
 400   bool has_cp_patch_at(int index) const {
 401     assert(index >= 0, "oob");
 402     return (_cp_patches != NULL
 403             && index < _cp_patches->length()
 404             && _cp_patches->adr_at(index)->not_null());
 405   }
 406 
 407   Handle cp_patch_at(int index) const {
 408     assert(has_cp_patch_at(index), "oob");
 409     return _cp_patches->at(index);
 410   }
 411 
 412   Handle clear_cp_patch_at(int index) {
 413     Handle patch = cp_patch_at(index);
 414     _cp_patches->at_put(index, Handle());
 415     assert(!has_cp_patch_at(index), "");
 416     return patch;
 417   }
 418 
 419   void patch_constant_pool(ConstantPool* cp,
 420                            int index,
 421                            Handle patch,
 422                            TRAPS);
 423 
 424   // Wrapper for constantTag.is_klass_[or_]reference.
 425   // In older versions of the VM, Klass*s cannot sneak into early phases of
 426   // constant pool construction, but in later versions they can.
 427   // %%% Let's phase out the old is_klass_reference.
 428   bool valid_klass_reference_at(int index) const {
 429     return _cp->is_within_bounds(index) &&
 430              _cp->tag_at(index).is_klass_or_reference();
 431   }
 432 
 433   // Checks that the cpool index is in range and is a utf8
 434   bool valid_symbol_at(int cpool_index) const {
 435     return _cp->is_within_bounds(cpool_index) &&
 436              _cp->tag_at(cpool_index).is_utf8();
 437   }
 438 
 439   void copy_localvariable_table(const ConstMethod* cm,
 440                                 int lvt_cnt,
 441                                 u2* const localvariable_table_length,
 442                                 const u2**const localvariable_table_start,
 443                                 int lvtt_cnt,
 444                                 u2* const localvariable_type_table_length,
 445                                 const u2** const localvariable_type_table_start,
 446                                 TRAPS);
 447 
 448   void copy_method_annotations(ConstMethod* cm,
 449                                const u1* runtime_visible_annotations,
 450                                int runtime_visible_annotations_length,
 451                                const u1* runtime_invisible_annotations,
 452                                int runtime_invisible_annotations_length,
 453                                const u1* runtime_visible_parameter_annotations,
 454                                int runtime_visible_parameter_annotations_length,
 455                                const u1* runtime_invisible_parameter_annotations,
 456                                int runtime_invisible_parameter_annotations_length,
 457                                const u1* runtime_visible_type_annotations,
 458                                int runtime_visible_type_annotations_length,
 459                                const u1* runtime_invisible_type_annotations,
 460                                int runtime_invisible_type_annotations_length,
 461                                const u1* annotation_default,
 462                                int annotation_default_length,
 463                                TRAPS);
 464 
 465   // lays out fields in class and returns the total oopmap count
 466   void layout_fields(ConstantPool* cp,
 467                      const FieldAllocationCount* fac,
 468                      const ClassAnnotationCollector* parsed_annotations,
 469                      FieldLayoutInfo* info,
 470                      TRAPS);
 471 
 472  public:
 473   ClassFileParser(ClassFileStream* stream,
 474                   Symbol* name,
 475                   ClassLoaderData* loader_data,
 476                   Handle protection_domain,
 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(bool cf_changed_in_CFLH, 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   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 515 
 516 #ifdef ASSERT
 517   static bool is_internal_format(Symbol* class_name);
 518 #endif
 519 
 520 };
 521 
 522 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP