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/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 {
  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   };
  73 
  74   enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
  75 
  76  private:
  77   // Potentially unaligned pointer to various 16-bit entries in the class file
  78   typedef void unsafe_u2;
  79 
  80   const ClassFileStream* _stream; // Actual input stream
  81   const Symbol* _requested_name;
  82   Symbol* _class_name;
  83   mutable ClassLoaderData* _loader_data;
  84   const InstanceKlass* _unsafe_anonymous_host;
  85   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  86   int _num_patched_klasses;
  87   int _max_num_patched_klasses;
  88   int _orig_cp_size;
  89   int _first_patched_klass_resolved_index;
  90 
  91   // Metadata created before the instance klass is created.  Must be deallocated
  92   // if not transferred to the InstanceKlass upon successful class loading
  93   // in which case these pointers have been set to NULL.
  94   const InstanceKlass* _super_klass;
  95   ConstantPool* _cp;
  96   Array<u2>* _fields;
  97   Array<Method*>* _methods;
  98   Array<u2>* _inner_classes;
  99   Array<u2>* _nest_members;
 100   u2 _nest_host;
 101   Array<u2>* _record_params;
 102   Array<u2>* _permitted_subtypes;
 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   int _record_params_count;
 158 
 159   bool _need_verify;
 160   bool _relax_verify;
 161 
 162   bool _has_nonstatic_concrete_methods;
 163   bool _declares_nonstatic_concrete_methods;
 164   bool _has_final_method;
 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, int record_params_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                     FieldAllocationCount* const fac,
 233                     ConstantPool* cp,
 234                     const int cp_size,
 235                     u2* const java_fields_count_ptr,
 236                     TRAPS);
 237 
 238   // Method parsing
 239   Method* parse_method(const ClassFileStream* const cfs,
 240                        bool is_interface,
 241                        const ConstantPool* cp,
 242                        AccessFlags* const promoted_flags,
 243                        TRAPS);
 244 
 245   void parse_methods(const ClassFileStream* const cfs,
 246                      bool is_interface,
 247                      AccessFlags* const promoted_flags,
 248                      bool* const has_final_method,
 249                      bool* const declares_nonstatic_concrete_methods,
 250                      TRAPS);
 251 
 252   const unsafe_u2* parse_exception_table(const ClassFileStream* const stream,
 253                                          u4 code_length,
 254                                          u4 exception_table_length,
 255                                          TRAPS);
 256 
 257   void parse_linenumber_table(u4 code_attribute_length,
 258                               u4 code_length,
 259                               CompressedLineNumberWriteStream**const write_stream,
 260                               TRAPS);
 261 
 262   const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs,
 263                                              u4 code_length,
 264                                              u2 max_locals,
 265                                              u4 code_attribute_length,
 266                                              u2* const localvariable_table_length,
 267                                              bool isLVTT,
 268                                              TRAPS);
 269 
 270   const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 271                                             u2* const checked_exceptions_length,
 272                                             u4 method_attribute_length,
 273                                             TRAPS);
 274 
 275   // Classfile attribute parsing
 276   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 277   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 278   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 279                                                         int length,
 280                                                         TRAPS);
 281 
 282   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 283                                                const u1* const inner_classes_attribute_start,
 284                                                bool parsed_enclosingmethod_attribute,
 285                                                u2 enclosing_method_class_index,
 286                                                u2 enclosing_method_method_index,
 287                                                TRAPS);
 288 
 289   u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
 290                                             const u1* const nest_members_attribute_start,
 291                                             TRAPS);
 292 
 293   u2 parse_classfile_permitted_subtypes_attribute(const ClassFileStream* const cfs,
 294                                             const u1* const permitted_subtypes_attribute_start,
 295                                             TRAPS);
 296 
 297   void parse_classfile_record_attribute(const ClassFileStream* const cfs,
 298                                         const u1* const record_attribute_start,
 299                                         ConstantPool* cp,
 300                                         int* const record_params_count_ptr,
 301                                         TRAPS);
 302 
 303   void parse_classfile_attributes(const ClassFileStream* const cfs,
 304                                   ConstantPool* cp,
 305                                   ClassAnnotationCollector* parsed_annotations,
 306                                   TRAPS);
 307 
 308   void parse_classfile_synthetic_attribute(TRAPS);
 309   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 310   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 311                                                    ConstantPool* cp,
 312                                                    u4 attribute_length,
 313                                                    TRAPS);
 314 
 315   // Annotations handling
 316   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 317                                         int runtime_visible_annotations_length,
 318                                         const u1* const runtime_invisible_annotations,
 319                                         int runtime_invisible_annotations_length,
 320                                         TRAPS);
 321 
 322   void set_precomputed_flags(InstanceKlass* k);
 323 
 324   // Format checker methods
 325   void classfile_parse_error(const char* msg, TRAPS) const;
 326   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 327   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 328   void classfile_parse_error(const char* msg,
 329                              int index,
 330                              const char *name,
 331                              TRAPS) const;
 332   void classfile_parse_error(const char* msg,
 333                              const char* name,
 334                              const char* signature,
 335                              TRAPS) const;
 336 
 337   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 338     if (!b) { classfile_parse_error(msg, CHECK); }
 339   }
 340 
 341   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 342   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 343 
 344   inline void assert_property(bool b, const char* msg, TRAPS) const {
 345 #ifdef ASSERT
 346     if (!b) {
 347       report_assert_property_failure(msg, THREAD);
 348     }
 349 #endif
 350   }
 351 
 352   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 353 #ifdef ASSERT
 354     if (!b) {
 355       report_assert_property_failure(msg, index, THREAD);
 356     }
 357 #endif
 358   }
 359 
 360   inline void check_property(bool property,
 361                              const char* msg,
 362                              int index,
 363                              TRAPS) const {
 364     if (_need_verify) {
 365       guarantee_property(property, msg, index, CHECK);
 366     } else {
 367       assert_property(property, msg, index, CHECK);
 368     }
 369   }
 370 
 371   inline void check_property(bool property, const char* msg, TRAPS) const {
 372     if (_need_verify) {
 373       guarantee_property(property, msg, CHECK);
 374     } else {
 375       assert_property(property, msg, CHECK);
 376     }
 377   }
 378 
 379   inline void guarantee_property(bool b,
 380                                  const char* msg,
 381                                  int index,
 382                                  TRAPS) const {
 383     if (!b) { classfile_parse_error(msg, index, CHECK); }
 384   }
 385 
 386   inline void guarantee_property(bool b,
 387                                  const char* msg,
 388                                  const char *name,
 389                                  TRAPS) const {
 390     if (!b) { classfile_parse_error(msg, name, CHECK); }
 391   }
 392 
 393   inline void guarantee_property(bool b,
 394                                  const char* msg,
 395                                  int index,
 396                                  const char *name,
 397                                  TRAPS) const {
 398     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 399   }
 400 
 401   void throwIllegalSignature(const char* type,
 402                              const Symbol* name,
 403                              const Symbol* sig,
 404                              TRAPS) const;
 405 
 406   void verify_constantvalue(const ConstantPool* const cp,
 407                             int constantvalue_index,
 408                             int signature_index,
 409                             TRAPS) const;
 410 
 411   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 412   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 413   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 414   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 415 
 416   void verify_legal_field_signature(const Symbol* fieldname,
 417                                     const Symbol* signature,
 418                                     TRAPS) const;
 419   int  verify_legal_method_signature(const Symbol* methodname,
 420                                      const Symbol* signature,
 421                                      TRAPS) const;
 422 
 423   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 424   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 425   void verify_legal_method_modifiers(jint flags,
 426                                      bool is_interface,
 427                                      const Symbol* name,
 428                                      TRAPS) const;
 429 
 430   const char* skip_over_field_signature(const char* signature,
 431                                         bool void_ok,
 432                                         unsigned int length,
 433                                         TRAPS) const;
 434 
 435   bool has_cp_patch_at(int index) const {
 436     assert(index >= 0, "oob");
 437     return (_cp_patches != NULL
 438             && index < _cp_patches->length()
 439             && _cp_patches->adr_at(index)->not_null());
 440   }
 441 
 442   Handle cp_patch_at(int index) const {
 443     assert(has_cp_patch_at(index), "oob");
 444     return _cp_patches->at(index);
 445   }
 446 
 447   Handle clear_cp_patch_at(int index);
 448 
 449   void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name);
 450   void patch_constant_pool(ConstantPool* cp,
 451                            int index,
 452                            Handle patch,
 453                            TRAPS);
 454 
 455   // Wrapper for constantTag.is_klass_[or_]reference.
 456   // In older versions of the VM, Klass*s cannot sneak into early phases of
 457   // constant pool construction, but in later versions they can.
 458   // %%% Let's phase out the old is_klass_reference.
 459   bool valid_klass_reference_at(int index) const {
 460     return _cp->is_within_bounds(index) &&
 461              _cp->tag_at(index).is_klass_or_reference();
 462   }
 463 
 464   // Checks that the cpool index is in range and is a utf8
 465   bool valid_symbol_at(int cpool_index) const {
 466     return _cp->is_within_bounds(cpool_index) &&
 467              _cp->tag_at(cpool_index).is_utf8();
 468   }
 469 
 470   void copy_localvariable_table(const ConstMethod* cm,
 471                                 int lvt_cnt,
 472                                 u2* const localvariable_table_length,
 473                                 const unsafe_u2** const localvariable_table_start,
 474                                 int lvtt_cnt,
 475                                 u2* const localvariable_type_table_length,
 476                                 const unsafe_u2** const localvariable_type_table_start,
 477                                 TRAPS);
 478 
 479   void copy_method_annotations(ConstMethod* cm,
 480                                const u1* runtime_visible_annotations,
 481                                int runtime_visible_annotations_length,
 482                                const u1* runtime_invisible_annotations,
 483                                int runtime_invisible_annotations_length,
 484                                const u1* runtime_visible_parameter_annotations,
 485                                int runtime_visible_parameter_annotations_length,
 486                                const u1* runtime_invisible_parameter_annotations,
 487                                int runtime_invisible_parameter_annotations_length,
 488                                const u1* runtime_visible_type_annotations,
 489                                int runtime_visible_type_annotations_length,
 490                                const u1* runtime_invisible_type_annotations,
 491                                int runtime_invisible_type_annotations_length,
 492                                const u1* annotation_default,
 493                                int annotation_default_length,
 494                                TRAPS);
 495 
 496   // lays out fields in class and returns the total oopmap count
 497   void layout_fields(ConstantPool* cp,
 498                      const FieldAllocationCount* fac,
 499                      const ClassAnnotationCollector* parsed_annotations,
 500                      FieldLayoutInfo* info,
 501                      TRAPS);
 502 
 503    // check that the current class is not extending a final class or interface
 504    void check_subtyping(TRAPS);
 505 
 506    void update_class_name(Symbol* new_name);
 507 
 508  public:
 509   ClassFileParser(ClassFileStream* stream,
 510                   Symbol* name,
 511                   ClassLoaderData* loader_data,
 512                   Handle protection_domain,
 513                   const InstanceKlass* unsafe_anonymous_host,
 514                   GrowableArray<Handle>* cp_patches,
 515                   Publicity pub_level,
 516                   TRAPS);
 517 
 518   ~ClassFileParser();
 519 
 520   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS);
 521 
 522   const ClassFileStream* clone_stream() const;
 523 
 524   void set_klass_to_deallocate(InstanceKlass* klass);
 525 
 526   int static_field_size() const;
 527   int total_oop_map_count() const;
 528   jint layout_size() const;
 529 
 530   int vtable_size() const { return _vtable_size; }
 531   int itable_size() const { return _itable_size; }
 532 
 533   u2 this_class_index() const { return _this_class_index; }
 534 
 535   bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; }
 536   bool is_interface() const { return _access_flags.is_interface(); }
 537 
 538   const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; }
 539   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 540   ClassLoaderData* loader_data() const { return _loader_data; }
 541   const Symbol* class_name() const { return _class_name; }
 542   const InstanceKlass* super_klass() const { return _super_klass; }
 543 
 544   ReferenceType reference_type() const { return _rt; }
 545   AccessFlags access_flags() const { return _access_flags; }
 546 
 547   bool is_internal() const { return INTERNAL == _pub_level; }
 548 
 549   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 550 
 551 #ifdef ASSERT
 552   static bool is_internal_format(Symbol* class_name);
 553 #endif
 554 
 555 };
 556 
 557 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP