1 /*
2 * Copyright (c) 1997, 2025, 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/fieldInfo.hpp"
32 #include "oops/instanceKlass.hpp"
33 #include "oops/typeArrayOop.hpp"
34 #include "utilities/accessFlags.hpp"
35
36 class Annotations;
37 template <typename T>
38 class Array;
39 class ClassFileStream;
40 class ClassLoaderData;
41 class ClassLoadInfo;
42 class ClassInstanceInfo;
43 class CompressedLineNumberWriteStream;
44 class ConstMethod;
45 class FieldInfo;
46 template <typename T>
47 class GrowableArray;
48 class InstanceKlass;
49 class RecordComponent;
50 class Symbol;
51 class FieldLayoutBuilder;
52
53 // Utility to collect and compact oop maps during layout
54 class OopMapBlocksBuilder : public ResourceObj {
55 public:
56 OopMapBlock* _nonstatic_oop_maps;
57 unsigned int _nonstatic_oop_map_count;
58 unsigned int _max_nonstatic_oop_maps;
59
60 OopMapBlocksBuilder(unsigned int max_blocks);
61 OopMapBlock* last_oop_map() const;
62 void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks);
63 void add(int offset, int count);
64 void copy(OopMapBlock* dst);
65 void compact();
66 void print_on(outputStream* st) const;
67 void print_value_on(outputStream* st) const;
68 };
69
70 // Values needed for oopmap and InstanceKlass creation
71 class FieldLayoutInfo : public ResourceObj {
72 public:
73 OopMapBlocksBuilder* oop_map_blocks;
74 int _instance_size;
75 int _nonstatic_field_size;
76 int _static_field_size;
77 bool _has_nonstatic_fields;
78 };
79
80 // Parser for for .class files
81 //
82 // The bytes describing the class file structure is read from a Stream object
83
84 class ClassFileParser {
85 friend class FieldLayoutBuilder;
86 friend class FieldLayout;
87
88 class ClassAnnotationCollector;
89 class FieldAnnotationCollector;
90
91 public:
92 // The ClassFileParser has an associated "publicity" level
93 // It is used to control which subsystems (if any)
94 // will observe the parsing (logging, events, tracing).
95 // Default level is "BROADCAST", which is equivalent to
96 // a "public" parsing attempt.
97 //
98 // "INTERNAL" level should be entirely private to the
99 // caller - this allows for internal reuse of ClassFileParser
100 //
101 enum Publicity {
102 INTERNAL,
103 BROADCAST
104 };
105
106 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
107
108 private:
109 // Potentially unaligned pointer to various 16-bit entries in the class file
110 typedef void unsafe_u2;
111
112 const ClassFileStream* _stream; // Actual input stream
113 Symbol* _class_name;
114 mutable ClassLoaderData* _loader_data;
115 const bool _is_hidden;
116 const bool _can_access_vm_annotations;
117 int _orig_cp_size;
118 unsigned int _static_oop_count;
119
120 // Metadata created before the instance klass is created. Must be deallocated
121 // if not transferred to the InstanceKlass upon successful class loading
122 // in which case these pointers have been set to null.
123 const InstanceKlass* _super_klass;
124 ConstantPool* _cp;
125 Array<u1>* _fieldinfo_stream;
126 Array<u1>* _fieldinfo_search_table;
127 Array<FieldStatus>* _fields_status;
128 Array<Method*>* _methods;
129 Array<u2>* _inner_classes;
130 Array<u2>* _nest_members;
131 u2 _nest_host;
132 Array<u2>* _permitted_subclasses;
133 Array<RecordComponent*>* _record_components;
134 Array<InstanceKlass*>* _local_interfaces;
135 Array<InstanceKlass*>* _transitive_interfaces;
136 Annotations* _combined_annotations;
137 AnnotationArray* _class_annotations;
138 AnnotationArray* _class_type_annotations;
139 Array<AnnotationArray*>* _fields_annotations;
140 Array<AnnotationArray*>* _fields_type_annotations;
141 InstanceKlass* _klass; // InstanceKlass* once created.
142 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
143
144 ClassAnnotationCollector* _parsed_annotations;
145 FieldLayoutInfo* _field_info;
146 GrowableArray<FieldInfo>* _temp_field_info;
147 const intArray* _method_ordering;
148 GrowableArray<Method*>* _all_mirandas;
149
150 enum { fixed_buffer_size = 128 };
151 u_char _linenumbertable_buffer[fixed_buffer_size];
152
153 // Size of Java vtable (in words)
154 int _vtable_size;
155 int _itable_size;
156
157 int _num_miranda_methods;
158
159 Handle _protection_domain;
160 AccessFlags _access_flags;
161
162 // for tracing and notifications
163 Publicity _pub_level;
164
165 // Used to keep track of whether a constant pool item 19 or 20 is found. These
166 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed
167 // in regular class files. For class file version >= 53, a CFE cannot be thrown
168 // immediately when these are seen because a NCDFE must be thrown if the class's
169 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked
170 // at yet. So, the bad constant pool item is cached here. A value of zero
171 // means that no constant pool item 19 or 20 was found.
172 short _bad_constant_seen;
173
174 // class attributes parsed before the instance klass is created:
175 bool _synthetic_flag;
176 int _sde_length;
177 const char* _sde_buffer;
178 u2 _sourcefile_index;
179 u2 _generic_signature_index;
180
181 u2 _major_version;
182 u2 _minor_version;
183 u2 _this_class_index;
184 u2 _super_class_index;
185 u2 _itfs_len;
186 u2 _java_fields_count;
187
188 bool _need_verify;
189
190 bool _has_nonstatic_concrete_methods;
191 bool _declares_nonstatic_concrete_methods;
192 bool _has_localvariable_table;
193 bool _has_final_method;
194 bool _has_contended_fields;
195
196 // precomputed flags
197 bool _has_finalizer;
198 bool _has_empty_finalizer;
199 int _max_bootstrap_specifier_index; // detects BSS values
200
201 void parse_stream(const ClassFileStream* const stream, TRAPS);
202
203 void mangle_hidden_class_name(InstanceKlass* const ik);
204
205 void post_process_parsed_stream(const ClassFileStream* const stream,
206 ConstantPool* cp,
207 TRAPS);
208
209 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH,
210 const ClassInstanceInfo& cl_inst_info, TRAPS);
211
212 void set_klass(InstanceKlass* instance);
213
214 void set_class_bad_constant_seen(short bad_constant);
215 short class_bad_constant_seen() { return _bad_constant_seen; }
216 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; }
217 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; }
218 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
219 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; }
220
221 void create_combined_annotations(TRAPS);
222 void apply_parsed_class_attributes(InstanceKlass* k); // update k
223 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count);
224 void clear_class_metadata();
225
226 // Constant pool parsing
227 void parse_constant_pool_entries(const ClassFileStream* const stream,
228 ConstantPool* cp,
229 const int length,
230 TRAPS);
231
232 void parse_constant_pool(const ClassFileStream* const cfs,
233 ConstantPool* const cp,
234 const int length,
235 TRAPS);
236
237 // Interface parsing
238 void parse_interfaces(const ClassFileStream* const stream,
239 const int itfs_len,
240 ConstantPool* const cp,
241 bool* has_nonstatic_concrete_methods,
242 TRAPS);
243
244 const InstanceKlass* parse_super_class(ConstantPool* const cp,
245 const int super_class_index,
246 const bool need_verify,
247 TRAPS);
248
249 // Field parsing
250 void parse_field_attributes(const ClassFileStream* const cfs,
251 u2 attributes_count,
252 bool is_static,
253 u2 signature_index,
254 u2* const constantvalue_index_addr,
255 bool* const is_synthetic_addr,
256 u2* const generic_signature_index_addr,
257 FieldAnnotationCollector* parsed_annotations,
258 TRAPS);
259
260 void parse_fields(const ClassFileStream* const cfs,
261 bool is_interface,
262 ConstantPool* cp,
263 const int cp_size,
264 u2* const java_fields_count_ptr,
265 TRAPS);
266
267 // Method parsing
268 Method* parse_method(const ClassFileStream* const cfs,
269 bool is_interface,
270 const ConstantPool* cp,
271 bool* const has_localvariable_table,
272 TRAPS);
273
274 void parse_methods(const ClassFileStream* const cfs,
275 bool is_interface,
276 bool* const has_localvariable_table,
277 bool* const has_final_method,
278 bool* const declares_nonstatic_concrete_methods,
279 TRAPS);
280
281 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream,
282 u4 code_length,
283 u4 exception_table_length,
284 TRAPS);
285
286 void parse_linenumber_table(u4 code_attribute_length,
287 u4 code_length,
288 CompressedLineNumberWriteStream**const write_stream,
289 TRAPS);
290
291 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs,
292 u4 code_length,
293 u2 max_locals,
294 u4 code_attribute_length,
295 u2* const localvariable_table_length,
296 bool isLVTT,
297 TRAPS);
298
299 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs,
300 u2* const checked_exceptions_length,
301 u4 method_attribute_length,
302 TRAPS);
303
304 // Classfile attribute parsing
305 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
306 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
307 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
308 int length,
309 TRAPS);
310
311 // Check for circularity in InnerClasses attribute.
312 bool check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS);
313
314 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
315 const ConstantPool* cp,
316 const u1* const inner_classes_attribute_start,
317 bool parsed_enclosingmethod_attribute,
318 u2 enclosing_method_class_index,
319 u2 enclosing_method_method_index,
320 TRAPS);
321
322 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
323 const u1* const nest_members_attribute_start,
324 TRAPS);
325
326 u2 parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs,
327 const u1* const permitted_subclasses_attribute_start,
328 TRAPS);
329
330 u4 parse_classfile_record_attribute(const ClassFileStream* const cfs,
331 const ConstantPool* cp,
332 const u1* const record_attribute_start,
333 TRAPS);
334
335 void parse_classfile_attributes(const ClassFileStream* const cfs,
336 ConstantPool* cp,
337 ClassAnnotationCollector* parsed_annotations,
338 TRAPS);
339
340 void parse_classfile_synthetic_attribute();
341 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
342 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
343 ConstantPool* cp,
344 u4 attribute_length,
345 TRAPS);
346
347 // Annotations handling
348 AnnotationArray* allocate_annotations(const u1* const anno,
349 int anno_length,
350 TRAPS);
351
352 void set_precomputed_flags(InstanceKlass* k);
353
354 // Format checker methods
355 void classfile_parse_error(const char* msg, TRAPS) const;
356 void classfile_parse_error(const char* msg, int index, TRAPS) const;
357 void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
358 void classfile_parse_error(const char* msg,
359 int index,
360 const char *name,
361 TRAPS) const;
362 void classfile_parse_error(const char* msg,
363 const char* name,
364 const char* signature,
365 TRAPS) const;
366
367 void classfile_icce_error(const char* msg,
368 const Klass* k,
369 TRAPS) const;
370
371 // Uses msg directly in the ICCE, with no additional content
372 void classfile_icce_error(const char* msg,
373 TRAPS) const;
374
375 void classfile_ucve_error(const char* msg,
376 const Symbol* class_name,
377 u2 major,
378 u2 minor,
379 TRAPS) const;
380
381 inline void guarantee_property(bool b, const char* msg, TRAPS) const {
382 if (!b) { classfile_parse_error(msg, THREAD); return; }
383 }
384
385 inline void guarantee_property(bool b,
386 const char* msg,
387 int index,
388 TRAPS) const {
389 if (!b) { classfile_parse_error(msg, index, THREAD); return; }
390 }
391
392 inline void guarantee_property(bool b,
393 const char* msg,
394 const char *name,
395 TRAPS) const {
396 if (!b) { classfile_parse_error(msg, name, THREAD); return; }
397 }
398
399 inline void guarantee_property(bool b,
400 const char* msg,
401 int index,
402 const char *name,
403 TRAPS) const {
404 if (!b) { classfile_parse_error(msg, index, name, THREAD); return; }
405 }
406
407 void throwIllegalSignature(const char* type,
408 const Symbol* name,
409 const Symbol* sig,
410 TRAPS) const;
411
412 void verify_constantvalue(const ConstantPool* const cp,
413 int constantvalue_index,
414 int signature_index,
415 TRAPS) const;
416
417 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
418 void verify_legal_class_name(const Symbol* name, TRAPS) const;
419 void verify_legal_field_name(const Symbol* name, TRAPS) const;
420 void verify_legal_method_name(const Symbol* name, TRAPS) const;
421
422 void verify_legal_field_signature(const Symbol* fieldname,
423 const Symbol* signature,
424 TRAPS) const;
425 int verify_legal_method_signature(const Symbol* methodname,
426 const Symbol* signature,
427 TRAPS) const;
428 void verify_legal_name_with_signature(const Symbol* name,
429 const Symbol* signature,
430 TRAPS) const;
431
432 void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS);
433
434 void verify_legal_class_modifiers(jint flags, TRAPS) const;
435 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
436 void verify_legal_method_modifiers(jint flags,
437 bool is_interface,
438 const Symbol* name,
439 TRAPS) const;
440
441 void check_super_class_access(const InstanceKlass* this_klass,
442 TRAPS);
443
444 void check_super_interface_access(const InstanceKlass* this_klass,
445 TRAPS);
446
447 const char* skip_over_field_signature(const char* signature,
448 bool void_ok,
449 unsigned int length,
450 TRAPS) const;
451
452 // Wrapper for constantTag.is_klass_[or_]reference.
453 // In older versions of the VM, Klass*s cannot sneak into early phases of
454 // constant pool construction, but in later versions they can.
455 // %%% Let's phase out the old is_klass_reference.
456 bool valid_klass_reference_at(int index) const {
457 return _cp->is_within_bounds(index) &&
458 _cp->tag_at(index).is_klass_or_reference();
459 }
460
461 // Checks that the cpool index is in range and is a utf8
462 bool valid_symbol_at(int cpool_index) const {
463 return _cp->is_within_bounds(cpool_index) &&
464 _cp->tag_at(cpool_index).is_utf8();
465 }
466
467 void copy_localvariable_table(const ConstMethod* cm,
468 int lvt_cnt,
469 u2* const localvariable_table_length,
470 const unsafe_u2** const localvariable_table_start,
471 int lvtt_cnt,
472 u2* const localvariable_type_table_length,
473 const unsafe_u2** const localvariable_type_table_start,
474 TRAPS);
475
476 void copy_method_annotations(ConstMethod* cm,
477 const u1* runtime_visible_annotations,
478 int runtime_visible_annotations_length,
479 const u1* runtime_visible_parameter_annotations,
480 int runtime_visible_parameter_annotations_length,
481 const u1* runtime_visible_type_annotations,
482 int runtime_visible_type_annotations_length,
483 const u1* annotation_default,
484 int annotation_default_length,
485 TRAPS);
486
487 void update_class_name(Symbol* new_name);
488
489 public:
490 ClassFileParser(ClassFileStream* stream,
491 Symbol* name,
492 ClassLoaderData* loader_data,
493 const ClassLoadInfo* cl_info,
494 Publicity pub_level,
495 TRAPS);
496
497 ~ClassFileParser();
498
499 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS);
500
501 const ClassFileStream* clone_stream() const;
502
503 void set_klass_to_deallocate(InstanceKlass* klass);
504
505 int static_field_size() const;
506 int total_oop_map_count() const;
507 jint layout_size() const;
508
509 int vtable_size() const { return _vtable_size; }
510 int itable_size() const { return _itable_size; }
511
512 u2 this_class_index() const { return _this_class_index; }
513
514 bool is_hidden() const { return _is_hidden; }
515 bool is_interface() const { return _access_flags.is_interface(); }
516 bool is_abstract() const { return _access_flags.is_abstract(); }
517
518 // Returns true if the Klass to be generated will need to be addressable
519 // with a narrow Klass ID.
520 bool klass_needs_narrow_id() const;
521
522 ClassLoaderData* loader_data() const { return _loader_data; }
523 const Symbol* class_name() const { return _class_name; }
524 const InstanceKlass* super_klass() const { return _super_klass; }
525
526 ReferenceType super_reference_type() const;
527 bool is_instance_ref_klass() const;
528 bool is_java_lang_ref_Reference_subclass() const;
529
530 AccessFlags access_flags() const { return _access_flags; }
531
532 bool is_internal() const { return INTERNAL == _pub_level; }
533
534 static bool verify_unqualified_name(const char* name, unsigned int length, int type);
535
536 #ifdef ASSERT
537 static bool is_internal_format(Symbol* class_name);
538 #endif
539
540 };
541
542 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP