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