classFileParser.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1997, 2009, 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// Parser for for .class files
26//
27// The bytes describing the class file structure is read from a Stream object
28
29class ClassFileParser VALUE_OBJ_CLASS_SPEC {
30 private:
31  bool _need_verify;
32  bool _relax_verify;
33  u2   _major_version;
34  u2   _minor_version;
35  symbolHandle _class_name;
36  KlassHandle _host_klass;
37  GrowableArray<Handle>* _cp_patches; // overrides for CP entries
38
39  bool _has_finalizer;
40  bool _has_empty_finalizer;
41  bool _has_vanilla_constructor;
42
43  enum { fixed_buffer_size = 128 };
44  u_char linenumbertable_buffer[fixed_buffer_size];
45
46  ClassFileStream* _stream;              // Actual input stream
47
48  enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
49
50  // Accessors
51  ClassFileStream* stream()                        { return _stream; }
52  void set_stream(ClassFileStream* st)             { _stream = st; }
53
54  // Constant pool parsing
55  void parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS);
56
57  constantPoolHandle parse_constant_pool(TRAPS);
58
59  // Interface parsing
60  objArrayHandle parse_interfaces(constantPoolHandle cp,
61                                  int length,
62                                  Handle class_loader,
63                                  Handle protection_domain,
64                                  symbolHandle class_name,
65                                  TRAPS);
66
67  // Field parsing
68  void parse_field_attributes(constantPoolHandle cp, u2 attributes_count,
69                              bool is_static, u2 signature_index,
70                              u2* constantvalue_index_addr,
71                              bool* is_synthetic_addr,
72                              u2* generic_signature_index_addr,
73                              typeArrayHandle* field_annotations, TRAPS);
74  typeArrayHandle parse_fields(constantPoolHandle cp, bool is_interface,
75                               struct FieldAllocationCount *fac,
76                               objArrayHandle* fields_annotations, TRAPS);
77
78  // Method parsing
79  methodHandle parse_method(constantPoolHandle cp, bool is_interface,
80                            AccessFlags* promoted_flags,
81                            typeArrayHandle* method_annotations,
82                            typeArrayHandle* method_parameter_annotations,
83                            typeArrayHandle* method_default_annotations,
84                            TRAPS);
85  objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface,
86                                AccessFlags* promoted_flags,
87                                bool* has_final_method,
88                                objArrayOop* methods_annotations_oop,
89                                objArrayOop* methods_parameter_annotations_oop,
90                                objArrayOop* methods_default_annotations_oop,
91                                TRAPS);
92  typeArrayHandle sort_methods (objArrayHandle methods,
93                                objArrayHandle methods_annotations,
94                                objArrayHandle methods_parameter_annotations,
95                                objArrayHandle methods_default_annotations,
96                                TRAPS);
97  typeArrayHandle parse_exception_table(u4 code_length, u4 exception_table_length,
98                                        constantPoolHandle cp, TRAPS);
99  void parse_linenumber_table(
100      u4 code_attribute_length, u4 code_length,
101      CompressedLineNumberWriteStream** write_stream, TRAPS);
102  u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length,
103                                constantPoolHandle cp, u2* localvariable_table_length,
104                                bool isLVTT, TRAPS);
105  u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
106                               constantPoolHandle cp, TRAPS);
107  void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
108                        u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS);
109  typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS);
110
111  // Classfile attribute parsing
112  void parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
113  void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
114                                                instanceKlassHandle k, int length, TRAPS);
115  u2   parse_classfile_inner_classes_attribute(constantPoolHandle cp,
116                                               instanceKlassHandle k, TRAPS);
117  void parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
118  void parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
119  void parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS);
120
121  // Annotations handling
122  typeArrayHandle assemble_annotations(u1* runtime_visible_annotations,
123                                       int runtime_visible_annotations_length,
124                                       u1* runtime_invisible_annotations,
125                                       int runtime_invisible_annotations_length, TRAPS);
126
127  // Final setup
128  unsigned int compute_oop_map_count(instanceKlassHandle super,
129                                     unsigned int nonstatic_oop_count,
130                                     int first_nonstatic_oop_offset);
131  void fill_oop_maps(instanceKlassHandle k,
132                     unsigned int nonstatic_oop_map_count,
133                     int* nonstatic_oop_offsets,
134                     unsigned int* nonstatic_oop_counts);
135  void set_precomputed_flags(instanceKlassHandle k);
136  objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
137                                               objArrayHandle local_ifs, TRAPS);
138
139  // Special handling for certain classes.
140  // Add the "discovered" field to java.lang.ref.Reference if
141  // it does not exist.
142  void java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
143    constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS);
144  // Adjust the field allocation counts for java.lang.Class to add
145  // fake fields.
146  void java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
147    FieldAllocationCount *fac_ptr, TRAPS);
148  // Adjust the next_nonstatic_oop_offset to place the fake fields
149  // before any Java fields.
150  void java_lang_Class_fix_post(int* next_nonstatic_oop_offset);
151  // Adjust the field allocation counts for java.dyn.MethodHandle to add
152  // a fake address (void*) field.
153  void java_dyn_MethodHandle_fix_pre(constantPoolHandle cp,
154                                     typeArrayHandle* fields_ptr,
155                                     FieldAllocationCount *fac_ptr, TRAPS);
156
157  // Format checker methods
158  void classfile_parse_error(const char* msg, TRAPS);
159  void classfile_parse_error(const char* msg, int index, TRAPS);
160  void classfile_parse_error(const char* msg, const char *name, TRAPS);
161  void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
162  inline void guarantee_property(bool b, const char* msg, TRAPS) {
163    if (!b) { classfile_parse_error(msg, CHECK); }
164  }
165
166  inline void assert_property(bool b, const char* msg, TRAPS) {
167#ifdef ASSERT
168    if (!b) { fatal(msg); }
169#endif
170  }
171
172  inline void check_property(bool property, const char* msg, int index, TRAPS) {
173    if (_need_verify) {
174      guarantee_property(property, msg, index, CHECK);
175    } else {
176      assert_property(property, msg, CHECK);
177    }
178  }
179
180  inline void check_property(bool property, const char* msg, TRAPS) {
181    if (_need_verify) {
182      guarantee_property(property, msg, CHECK);
183    } else {
184      assert_property(property, msg, CHECK);
185    }
186  }
187
188  inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
189    if (!b) { classfile_parse_error(msg, index, CHECK); }
190  }
191  inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
192    if (!b) { classfile_parse_error(msg, name, CHECK); }
193  }
194  inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
195    if (!b) { classfile_parse_error(msg, index, name, CHECK); }
196  }
197
198  bool is_supported_version(u2 major, u2 minor);
199  bool has_illegal_visibility(jint flags);
200
201  void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
202  void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
203  void verify_legal_class_name(symbolHandle name, TRAPS);
204  void verify_legal_field_name(symbolHandle name, TRAPS);
205  void verify_legal_method_name(symbolHandle name, TRAPS);
206  void verify_legal_field_signature(symbolHandle fieldname, symbolHandle signature, TRAPS);
207  int  verify_legal_method_signature(symbolHandle methodname, symbolHandle signature, TRAPS);
208  void verify_legal_class_modifiers(jint flags, TRAPS);
209  void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
210  void verify_legal_method_modifiers(jint flags, bool is_interface, symbolHandle name, TRAPS);
211  bool verify_unqualified_name(char* name, unsigned int length, int type);
212  char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
213  char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);
214
215  bool is_anonymous() {
216    assert(AnonymousClasses || _host_klass.is_null(), "");
217    return _host_klass.not_null();
218  }
219  bool has_cp_patch_at(int index) {
220    assert(AnonymousClasses, "");
221    assert(index >= 0, "oob");
222    return (_cp_patches != NULL
223            && index < _cp_patches->length()
224            && _cp_patches->adr_at(index)->not_null());
225  }
226  Handle cp_patch_at(int index) {
227    assert(has_cp_patch_at(index), "oob");
228    return _cp_patches->at(index);
229  }
230  Handle clear_cp_patch_at(int index) {
231    Handle patch = cp_patch_at(index);
232    _cp_patches->at_put(index, Handle());
233    assert(!has_cp_patch_at(index), "");
234    return patch;
235  }
236  void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);
237
238  // Wrapper for constantTag.is_klass_[or_]reference.
239  // In older versions of the VM, klassOops cannot sneak into early phases of
240  // constant pool construction, but in later versions they can.
241  // %%% Let's phase out the old is_klass_reference.
242  bool is_klass_reference(constantPoolHandle cp, int index) {
243    return ((LinkWellKnownClasses || AnonymousClasses)
244            ? cp->tag_at(index).is_klass_or_reference()
245            : cp->tag_at(index).is_klass_reference());
246  }
247
248 public:
249  // Constructor
250  ClassFileParser(ClassFileStream* st) { set_stream(st); }
251
252  // Parse .class file and return new klassOop. The klassOop is not hooked up
253  // to the system dictionary or any other structures, so a .class file can
254  // be loaded several times if desired.
255  // The system dictionary hookup is done by the caller.
256  //
257  // "parsed_name" is updated by this method, and is the name found
258  // while parsing the stream.
259  instanceKlassHandle parseClassFile(symbolHandle name,
260                                     Handle class_loader,
261                                     Handle protection_domain,
262                                     symbolHandle& parsed_name,
263                                     bool verify,
264                                     TRAPS) {
265    KlassHandle no_host_klass;
266    return parseClassFile(name, class_loader, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
267  }
268  instanceKlassHandle parseClassFile(symbolHandle name,
269                                     Handle class_loader,
270                                     Handle protection_domain,
271                                     KlassHandle host_klass,
272                                     GrowableArray<Handle>* cp_patches,
273                                     symbolHandle& parsed_name,
274                                     bool verify,
275                                     TRAPS);
276
277  // Verifier checks
278  static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
279  static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
280  static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
281  static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
282};
283