Wed, 12 Nov 2008 22:33:26 -0800
6653858: dynamic languages need to be able to load anonymous classes
Summary: low-level privileged sun.misc.Unsafe.defineAnonymousClass
Reviewed-by: kvn
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 GrowableArray<Handle>* _cp_patches; // overrides for CP entries
38 bool _has_finalizer;
39 bool _has_empty_finalizer;
40 bool _has_vanilla_constructor;
42 enum { fixed_buffer_size = 128 };
43 u_char linenumbertable_buffer[fixed_buffer_size];
45 ClassFileStream* _stream; // Actual input stream
47 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
49 // Accessors
50 ClassFileStream* stream() { return _stream; }
51 void set_stream(ClassFileStream* st) { _stream = st; }
53 // Constant pool parsing
54 void parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS);
56 constantPoolHandle parse_constant_pool(TRAPS);
58 // Interface parsing
59 objArrayHandle parse_interfaces(constantPoolHandle cp,
60 int length,
61 Handle class_loader,
62 Handle protection_domain,
63 PerfTraceTime* vmtimer,
64 symbolHandle class_name,
65 TRAPS);
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);
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);
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);
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);
127 // Final setup
128 int compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_count,
129 int first_nonstatic_oop_offset);
130 void fill_oop_maps(instanceKlassHandle k, int nonstatic_oop_map_count,
131 u2* nonstatic_oop_offsets, u2* nonstatic_oop_length);
132 void set_precomputed_flags(instanceKlassHandle k);
133 objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
134 objArrayHandle local_ifs, TRAPS);
136 // Special handling for certain classes.
137 // Add the "discovered" field to java.lang.ref.Reference if
138 // it does not exist.
139 void java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
140 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS);
141 // Adjust the field allocation counts for java.lang.Class to add
142 // fake fields.
143 void java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
144 FieldAllocationCount *fac_ptr, TRAPS);
145 // Adjust the next_nonstatic_oop_offset to place the fake fields
146 // before any Java fields.
147 void java_lang_Class_fix_post(int* next_nonstatic_oop_offset);
149 // Format checker methods
150 void classfile_parse_error(const char* msg, TRAPS);
151 void classfile_parse_error(const char* msg, int index, TRAPS);
152 void classfile_parse_error(const char* msg, const char *name, TRAPS);
153 void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
154 inline void guarantee_property(bool b, const char* msg, TRAPS) {
155 if (!b) { classfile_parse_error(msg, CHECK); }
156 }
158 inline void assert_property(bool b, const char* msg, TRAPS) {
159 #ifdef ASSERT
160 if (!b) { fatal(msg); }
161 #endif
162 }
164 inline void check_property(bool property, const char* msg, int index, TRAPS) {
165 if (_need_verify) {
166 guarantee_property(property, msg, index, CHECK);
167 } else {
168 assert_property(property, msg, CHECK);
169 }
170 }
172 inline void check_property(bool property, const char* msg, TRAPS) {
173 if (_need_verify) {
174 guarantee_property(property, msg, CHECK);
175 } else {
176 assert_property(property, msg, CHECK);
177 }
178 }
180 inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
181 if (!b) { classfile_parse_error(msg, index, CHECK); }
182 }
183 inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
184 if (!b) { classfile_parse_error(msg, name, CHECK); }
185 }
186 inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
187 if (!b) { classfile_parse_error(msg, index, name, CHECK); }
188 }
190 bool is_supported_version(u2 major, u2 minor);
191 bool has_illegal_visibility(jint flags);
193 void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
194 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
195 void verify_legal_class_name(symbolHandle name, TRAPS);
196 void verify_legal_field_name(symbolHandle name, TRAPS);
197 void verify_legal_method_name(symbolHandle name, TRAPS);
198 void verify_legal_field_signature(symbolHandle fieldname, symbolHandle signature, TRAPS);
199 int verify_legal_method_signature(symbolHandle methodname, symbolHandle signature, TRAPS);
200 void verify_legal_class_modifiers(jint flags, TRAPS);
201 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
202 void verify_legal_method_modifiers(jint flags, bool is_interface, symbolHandle name, TRAPS);
203 bool verify_unqualified_name(char* name, unsigned int length, int type);
204 char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
205 char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);
207 bool has_cp_patch_at(int index) {
208 assert(AnonymousClasses, "");
209 assert(index >= 0, "oob");
210 return (_cp_patches != NULL
211 && index < _cp_patches->length()
212 && _cp_patches->adr_at(index)->not_null());
213 }
214 Handle cp_patch_at(int index) {
215 assert(has_cp_patch_at(index), "oob");
216 return _cp_patches->at(index);
217 }
218 Handle clear_cp_patch_at(int index) {
219 Handle patch = cp_patch_at(index);
220 _cp_patches->at_put(index, Handle());
221 assert(!has_cp_patch_at(index), "");
222 return patch;
223 }
224 void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);
226 // Wrapper for constantTag.is_klass_[or_]reference.
227 // In older versions of the VM, klassOops cannot sneak into early phases of
228 // constant pool construction, but in later versions they can.
229 // %%% Let's phase out the old is_klass_reference.
230 bool is_klass_reference(constantPoolHandle cp, int index) {
231 return ((LinkWellKnownClasses || AnonymousClasses)
232 ? cp->tag_at(index).is_klass_or_reference()
233 : cp->tag_at(index).is_klass_reference());
234 }
236 public:
237 // Constructor
238 ClassFileParser(ClassFileStream* st) { set_stream(st); }
240 // Parse .class file and return new klassOop. The klassOop is not hooked up
241 // to the system dictionary or any other structures, so a .class file can
242 // be loaded several times if desired.
243 // The system dictionary hookup is done by the caller.
244 //
245 // "parsed_name" is updated by this method, and is the name found
246 // while parsing the stream.
247 instanceKlassHandle parseClassFile(symbolHandle name,
248 Handle class_loader,
249 Handle protection_domain,
250 symbolHandle& parsed_name,
251 TRAPS) {
252 return parseClassFile(name, class_loader, protection_domain, NULL, parsed_name, THREAD);
253 }
254 instanceKlassHandle parseClassFile(symbolHandle name,
255 Handle class_loader,
256 Handle protection_domain,
257 GrowableArray<Handle>* cp_patches,
258 symbolHandle& parsed_name,
259 TRAPS);
261 // Verifier checks
262 static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
263 static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
264 static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
265 static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
266 };