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<InstanceKlass*>* _local_interfaces;
 102   Array<InstanceKlass*>* _transitive_interfaces;
 103   Annotations* _combined_annotations;
 104   AnnotationArray* _annotations;
 105   AnnotationArray* _type_annotations;
 106   Array<AnnotationArray*>* _fields_annotations;
 107   Array<AnnotationArray*>* _fields_type_annotations;
 108   InstanceKlass* _klass;  // InstanceKlass* once created.
 109   InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
 110 
 111   ClassAnnotationCollector* _parsed_annotations;
 112   FieldAllocationCount* _fac;
 113   FieldLayoutInfo* _field_info;
 114   const intArray* _method_ordering;
 115   GrowableArray<Method*>* _all_mirandas;
 116 
 117   enum { fixed_buffer_size = 128 };
 118   u_char _linenumbertable_buffer[fixed_buffer_size];
 119 
 120   // Size of Java vtable (in words)
 121   int _vtable_size;
 122   int _itable_size;
 123 
 124   int _num_miranda_methods;
 125 
 126   int _method_sig_count;
 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 
 165   // precomputed flags
 166   bool _has_finalizer;
 167   bool _has_empty_finalizer;
 168   bool _has_vanilla_constructor;
 169   int _max_bootstrap_specifier_index;  // detects BSS values
 170 
 171   void parse_stream(const ClassFileStream* const stream, TRAPS);
 172 
 173   void post_process_parsed_stream(const ClassFileStream* const stream,
 174                                   ConstantPool* cp,
 175                                   TRAPS);
 176 
 177   void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS);
 178   void fix_unsafe_anonymous_class_name(TRAPS);
 179 
 180   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS);
 181   void set_klass(InstanceKlass* instance);
 182 
 183   void set_class_bad_constant_seen(short bad_constant);
 184   short class_bad_constant_seen() { return  _bad_constant_seen; }
 185   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 186   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 187   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 188   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 189 
 190   void create_combined_annotations(TRAPS);
 191   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 192   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 193   void clear_class_metadata();
 194 
 195   // Constant pool parsing
 196   void parse_constant_pool_entries(const ClassFileStream* const stream,
 197                                    ConstantPool* cp,
 198                                    const int length,
 199                                    TRAPS);
 200 
 201   void parse_constant_pool(const ClassFileStream* const cfs,
 202                            ConstantPool* const cp,
 203                            const int length,
 204                            TRAPS);
 205 
 206   // Interface parsing
 207   void parse_interfaces(const ClassFileStream* const stream,
 208                         const int itfs_len,
 209                         ConstantPool* const cp,
 210                         bool* has_nonstatic_concrete_methods,
 211                         TRAPS);
 212 
 213   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 214                                          const int super_class_index,
 215                                          const bool need_verify,
 216                                          TRAPS);
 217 
 218   // Field parsing
 219   void parse_field_attributes(const ClassFileStream* const cfs,
 220                               u2 attributes_count,
 221                               bool is_static,
 222                               u2 signature_index,
 223                               u2* const constantvalue_index_addr,
 224                               bool* const is_synthetic_addr,
 225                               u2* const generic_signature_index_addr,
 226                               FieldAnnotationCollector* parsed_annotations,
 227                               TRAPS);
 228 
 229   void parse_fields(const ClassFileStream* const cfs,
 230                     bool is_interface,
 231                     FieldAllocationCount* const fac,
 232                     ConstantPool* cp,
 233                     const int cp_size,
 234                     u2* const java_fields_count_ptr,
 235                     TRAPS);
 236 
 237   // Method parsing
 238   Method* parse_method(const ClassFileStream* const cfs,
 239                        bool is_interface,
 240                        const ConstantPool* cp,
 241                        AccessFlags* const promoted_flags,
 242                        TRAPS);
 243 
 244   void parse_methods(const ClassFileStream* const cfs,
 245                      bool is_interface,
 246                      AccessFlags* const promoted_flags,
 247                      bool* const has_final_method,
 248                      bool* const declares_nonstatic_concrete_methods,
 249                      TRAPS);
 250 
 251   const unsafe_u2* parse_exception_table(const ClassFileStream* const stream,
 252                                          u4 code_length,
 253                                          u4 exception_table_length,
 254                                          TRAPS);
 255 
 256   void parse_linenumber_table(u4 code_attribute_length,
 257                               u4 code_length,
 258                               CompressedLineNumberWriteStream**const write_stream,
 259                               TRAPS);
 260 
 261   const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs,
 262                                              u4 code_length,
 263                                              u2 max_locals,
 264                                              u4 code_attribute_length,
 265                                              u2* const localvariable_table_length,
 266                                              bool isLVTT,
 267                                              TRAPS);
 268 
 269   const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 270                                             u2* const checked_exceptions_length,
 271                                             u4 method_attribute_length,
 272                                             TRAPS);
 273 
 274   // Classfile attribute parsing
 275   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 276   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 277   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 278                                                         int length,
 279                                                         TRAPS);
 280 
 281   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 282                                                const u1* const inner_classes_attribute_start,
 283                                                bool parsed_enclosingmethod_attribute,
 284                                                u2 enclosing_method_class_index,
 285                                                u2 enclosing_method_method_index,
 286                                                TRAPS);
 287 
 288   u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
 289                                             const u1* const nest_members_attribute_start,
 290                                             TRAPS);
 291 
 292   void parse_classfile_attributes(const ClassFileStream* const cfs,
 293                                   ConstantPool* cp,
 294                                   ClassAnnotationCollector* parsed_annotations,
 295                                   TRAPS);
 296 
 297   void parse_classfile_synthetic_attribute(TRAPS);
 298   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 299   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 300                                                    ConstantPool* cp,
 301                                                    u4 attribute_length,
 302                                                    TRAPS);
 303 
 304   // Annotations handling
 305   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 306                                         int runtime_visible_annotations_length,
 307                                         const u1* const runtime_invisible_annotations,
 308                                         int runtime_invisible_annotations_length,
 309                                         TRAPS);
 310 
 311   void set_precomputed_flags(InstanceKlass* k);
 312 
 313   // Format checker methods
 314   void classfile_parse_error(const char* msg, TRAPS) const;
 315   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 316   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 317   void classfile_parse_error(const char* msg,
 318                              int index,
 319                              const char *name,
 320                              TRAPS) const;
 321   void classfile_parse_error(const char* msg,
 322                              const char* name,
 323                              const char* signature,
 324                              TRAPS) const;
 325 
 326   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 327     if (!b) { classfile_parse_error(msg, CHECK); }
 328   }
 329 
 330   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 331   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 332 
 333   inline void assert_property(bool b, const char* msg, TRAPS) const {
 334 #ifdef ASSERT
 335     if (!b) {
 336       report_assert_property_failure(msg, THREAD);
 337     }
 338 #endif
 339   }
 340 
 341   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 342 #ifdef ASSERT
 343     if (!b) {
 344       report_assert_property_failure(msg, index, THREAD);
 345     }
 346 #endif
 347   }
 348 
 349   inline void check_property(bool property,
 350                              const char* msg,
 351                              int index,
 352                              TRAPS) const {
 353     if (_need_verify) {
 354       guarantee_property(property, msg, index, CHECK);
 355     } else {
 356       assert_property(property, msg, index, CHECK);
 357     }
 358   }
 359 
 360   inline void check_property(bool property, const char* msg, TRAPS) const {
 361     if (_need_verify) {
 362       guarantee_property(property, msg, CHECK);
 363     } else {
 364       assert_property(property, msg, CHECK);
 365     }
 366   }
 367 
 368   inline void guarantee_property(bool b,
 369                                  const char* msg,
 370                                  int index,
 371                                  TRAPS) const {
 372     if (!b) { classfile_parse_error(msg, index, CHECK); }
 373   }
 374 
 375   inline void guarantee_property(bool b,
 376                                  const char* msg,
 377                                  const char *name,
 378                                  TRAPS) const {
 379     if (!b) { classfile_parse_error(msg, name, CHECK); }
 380   }
 381 
 382   inline void guarantee_property(bool b,
 383                                  const char* msg,
 384                                  int index,
 385                                  const char *name,
 386                                  TRAPS) const {
 387     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 388   }
 389 
 390   void throwIllegalSignature(const char* type,
 391                              const Symbol* name,
 392                              const Symbol* sig,
 393                              TRAPS) const;
 394 
 395   void verify_constantvalue(const ConstantPool* const cp,
 396                             int constantvalue_index,
 397                             int signature_index,
 398                             TRAPS) const;
 399 
 400   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 401   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 402   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 403   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 404 
 405   void verify_legal_field_signature(const Symbol* fieldname,
 406                                     const Symbol* signature,
 407                                     TRAPS) const;
 408   int  verify_legal_method_signature(const Symbol* methodname,
 409                                      const Symbol* signature,
 410                                      TRAPS) const;
 411 
 412   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 413   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 414   void verify_legal_method_modifiers(jint flags,
 415                                      bool is_interface,
 416                                      const Symbol* name,
 417                                      TRAPS) const;
 418 
 419   const char* skip_over_field_signature(const char* signature,
 420                                         bool void_ok,
 421                                         unsigned int length,
 422                                         TRAPS) const;
 423 
 424   bool has_cp_patch_at(int index) const {
 425     assert(index >= 0, "oob");
 426     return (_cp_patches != NULL
 427             && index < _cp_patches->length()
 428             && _cp_patches->adr_at(index)->not_null());
 429   }
 430 
 431   Handle cp_patch_at(int index) const {
 432     assert(has_cp_patch_at(index), "oob");
 433     return _cp_patches->at(index);
 434   }
 435 
 436   Handle clear_cp_patch_at(int index);
 437 
 438   void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name);
 439   void patch_constant_pool(ConstantPool* cp,
 440                            int index,
 441                            Handle patch,
 442                            TRAPS);
 443 
 444   // Wrapper for constantTag.is_klass_[or_]reference.
 445   // In older versions of the VM, Klass*s cannot sneak into early phases of
 446   // constant pool construction, but in later versions they can.
 447   // %%% Let's phase out the old is_klass_reference.
 448   bool valid_klass_reference_at(int index) const {
 449     return _cp->is_within_bounds(index) &&
 450              _cp->tag_at(index).is_klass_or_reference();
 451   }
 452 
 453   // Checks that the cpool index is in range and is a utf8
 454   bool valid_symbol_at(int cpool_index) const {
 455     return _cp->is_within_bounds(cpool_index) &&
 456              _cp->tag_at(cpool_index).is_utf8();
 457   }
 458 
 459   void copy_localvariable_table(const ConstMethod* cm,
 460                                 int lvt_cnt,
 461                                 u2* const localvariable_table_length,
 462                                 const unsafe_u2** const localvariable_table_start,
 463                                 int lvtt_cnt,
 464                                 u2* const localvariable_type_table_length,
 465                                 const unsafe_u2** const localvariable_type_table_start,
 466                                 TRAPS);
 467 
 468   void copy_method_annotations(ConstMethod* cm,
 469                                const u1* runtime_visible_annotations,
 470                                int runtime_visible_annotations_length,
 471                                const u1* runtime_invisible_annotations,
 472                                int runtime_invisible_annotations_length,
 473                                const u1* runtime_visible_parameter_annotations,
 474                                int runtime_visible_parameter_annotations_length,
 475                                const u1* runtime_invisible_parameter_annotations,
 476                                int runtime_invisible_parameter_annotations_length,
 477                                const u1* runtime_visible_type_annotations,
 478                                int runtime_visible_type_annotations_length,
 479                                const u1* runtime_invisible_type_annotations,
 480                                int runtime_invisible_type_annotations_length,
 481                                const u1* annotation_default,
 482                                int annotation_default_length,
 483                                TRAPS);
 484 
 485   // lays out fields in class and returns the total oopmap count
 486   void layout_fields(ConstantPool* cp,
 487                      const FieldAllocationCount* fac,
 488                      const ClassAnnotationCollector* parsed_annotations,
 489                      FieldLayoutInfo* info,
 490                      TRAPS);
 491 
 492    void update_class_name(Symbol* new_name);
 493 
 494  public:
 495   ClassFileParser(ClassFileStream* stream,
 496                   Symbol* name,
 497                   ClassLoaderData* loader_data,
 498                   Handle protection_domain,
 499                   const InstanceKlass* unsafe_anonymous_host,
 500                   GrowableArray<Handle>* cp_patches,
 501                   Publicity pub_level,
 502                   TRAPS);
 503 
 504   ~ClassFileParser();
 505 
 506   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS);
 507 
 508   const ClassFileStream* clone_stream() const;
 509 
 510   void set_klass_to_deallocate(InstanceKlass* klass);
 511 
 512   int static_field_size() const;
 513   int total_oop_map_count() const;
 514   jint layout_size() const;
 515 
 516   int vtable_size() const { return _vtable_size; }
 517   int itable_size() const { return _itable_size; }
 518 
 519   u2 this_class_index() const { return _this_class_index; }
 520 
 521   bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; }
 522   bool is_interface() const { return _access_flags.is_interface(); }
 523 
 524   const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; }
 525   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 526   ClassLoaderData* loader_data() const { return _loader_data; }
 527   const Symbol* class_name() const { return _class_name; }
 528   const InstanceKlass* super_klass() const { return _super_klass; }
 529 
 530   ReferenceType reference_type() const { return _rt; }
 531   AccessFlags access_flags() const { return _access_flags; }
 532 
 533   bool is_internal() const { return INTERNAL == _pub_level; }
 534 
 535   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 536 
 537 #ifdef ASSERT
 538   static bool is_internal_format(Symbol* class_name);
 539 #endif
 540 
 541 };
 542 
 543 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP