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