0N/A/*
3650N/A * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
1879N/A#define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
1879N/A
1879N/A#include "classfile/classFileStream.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "oops/typeArrayOop.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "utilities/accessFlags.hpp"
3888N/A#include "classfile/symbolTable.hpp"
1879N/A
2771N/Aclass FieldAllocationCount;
2771N/A
2771N/A
0N/A// Parser for for .class files
0N/A//
0N/A// The bytes describing the class file structure is read from a Stream object
0N/A
0N/Aclass ClassFileParser VALUE_OBJ_CLASS_SPEC {
0N/A private:
0N/A bool _need_verify;
0N/A bool _relax_verify;
0N/A u2 _major_version;
0N/A u2 _minor_version;
2062N/A Symbol* _class_name;
710N/A KlassHandle _host_klass;
431N/A GrowableArray<Handle>* _cp_patches; // overrides for CP entries
0N/A
3888N/A // precomputed flags
0N/A bool _has_finalizer;
0N/A bool _has_empty_finalizer;
0N/A bool _has_vanilla_constructor;
3888N/A int _max_bootstrap_specifier_index; // detects BSS values
0N/A
3888N/A // class attributes parsed before the instance klass is created:
3888N/A bool _synthetic_flag;
3888N/A Symbol* _sourcefile;
3888N/A Symbol* _generic_signature;
3892N/A char* _sde_buffer;
3892N/A int _sde_length;
3888N/A typeArrayHandle _inner_classes;
3888N/A typeArrayHandle _annotations;
3888N/A
3888N/A void set_class_synthetic_flag(bool x) { _synthetic_flag = x; }
3888N/A void set_class_sourcefile(Symbol* x) { _sourcefile = x; }
3888N/A void set_class_generic_signature(Symbol* x) { _generic_signature = x; }
3892N/A void set_class_sde_buffer(char* x, int len) { _sde_buffer = x; _sde_length = len; }
3888N/A void set_class_inner_classes(typeArrayHandle x) { _inner_classes = x; }
3888N/A void set_class_annotations(typeArrayHandle x) { _annotations = x; }
3888N/A void init_parsed_class_attributes() {
3888N/A _synthetic_flag = false;
3888N/A _sourcefile = NULL;
3888N/A _generic_signature = NULL;
3892N/A _sde_buffer = NULL;
3892N/A _sde_length = 0;
3888N/A // initialize the other flags too:
3888N/A _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
3888N/A _max_bootstrap_specifier_index = -1;
3888N/A }
3888N/A void apply_parsed_class_attributes(instanceKlassHandle k); // update k
3888N/A
3888N/A class AnnotationCollector {
3888N/A public:
3888N/A enum Location { _in_field, _in_method, _in_class };
3888N/A enum ID {
3888N/A _unknown = 0,
3888N/A _method_ForceInline,
3932N/A _method_DontInline,
3932N/A _method_LambdaForm_Compiled,
3932N/A _method_LambdaForm_Hidden,
3888N/A _annotation_LIMIT
3888N/A };
3888N/A const Location _location;
3888N/A int _annotations_present;
3888N/A AnnotationCollector(Location location)
3888N/A : _location(location), _annotations_present(0)
3888N/A {
3888N/A assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
3888N/A }
3888N/A // If this annotation name has an ID, report it (or _none).
3888N/A ID annotation_index(Symbol* name);
3888N/A // Set the annotation name:
3888N/A void set_annotation(ID id) {
3888N/A assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
3888N/A _annotations_present |= nth_bit((int)id);
3888N/A }
3888N/A // Report if the annotation is present.
3888N/A bool has_any_annotations() { return _annotations_present != 0; }
3888N/A bool has_annotation(ID id) { return (nth_bit((int)id) & _annotations_present) != 0; }
3888N/A };
3888N/A class FieldAnnotationCollector: public AnnotationCollector {
3888N/A public:
3888N/A FieldAnnotationCollector() : AnnotationCollector(_in_field) { }
3888N/A void apply_to(FieldInfo* f);
3888N/A };
3888N/A class MethodAnnotationCollector: public AnnotationCollector {
3888N/A public:
3888N/A MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
3888N/A void apply_to(methodHandle m);
3888N/A };
3888N/A class ClassAnnotationCollector: public AnnotationCollector {
3888N/A public:
3888N/A ClassAnnotationCollector() : AnnotationCollector(_in_class) { }
3888N/A void apply_to(instanceKlassHandle k);
3888N/A };
1918N/A
0N/A enum { fixed_buffer_size = 128 };
0N/A u_char linenumbertable_buffer[fixed_buffer_size];
0N/A
0N/A ClassFileStream* _stream; // Actual input stream
0N/A
0N/A enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
0N/A
0N/A // Accessors
0N/A ClassFileStream* stream() { return _stream; }
0N/A void set_stream(ClassFileStream* st) { _stream = st; }
0N/A
0N/A // Constant pool parsing
3650N/A void parse_constant_pool_entries(Handle class_loader,
3650N/A constantPoolHandle cp, int length, TRAPS);
0N/A
3650N/A constantPoolHandle parse_constant_pool(Handle class_loader, TRAPS);
0N/A
0N/A // Interface parsing
0N/A objArrayHandle parse_interfaces(constantPoolHandle cp,
0N/A int length,
0N/A Handle class_loader,
0N/A Handle protection_domain,
2062N/A Symbol* class_name,
0N/A TRAPS);
0N/A
0N/A // Field parsing
0N/A void parse_field_attributes(constantPoolHandle cp, u2 attributes_count,
0N/A bool is_static, u2 signature_index,
0N/A u2* constantvalue_index_addr,
0N/A bool* is_synthetic_addr,
0N/A u2* generic_signature_index_addr,
3888N/A typeArrayHandle* field_annotations,
3888N/A FieldAnnotationCollector* parsed_annotations,
3888N/A TRAPS);
2771N/A typeArrayHandle parse_fields(Symbol* class_name,
2771N/A constantPoolHandle cp, bool is_interface,
2771N/A FieldAllocationCount *fac,
2771N/A objArrayHandle* fields_annotations,
3024N/A u2* java_fields_count_ptr, TRAPS);
0N/A
0N/A // Method parsing
0N/A methodHandle parse_method(constantPoolHandle cp, bool is_interface,
0N/A AccessFlags* promoted_flags,
0N/A typeArrayHandle* method_annotations,
0N/A typeArrayHandle* method_parameter_annotations,
0N/A typeArrayHandle* method_default_annotations,
0N/A TRAPS);
0N/A objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface,
0N/A AccessFlags* promoted_flags,
0N/A bool* has_final_method,
0N/A objArrayOop* methods_annotations_oop,
0N/A objArrayOop* methods_parameter_annotations_oop,
0N/A objArrayOop* methods_default_annotations_oop,
0N/A TRAPS);
0N/A typeArrayHandle sort_methods (objArrayHandle methods,
0N/A objArrayHandle methods_annotations,
0N/A objArrayHandle methods_parameter_annotations,
0N/A objArrayHandle methods_default_annotations,
0N/A TRAPS);
3879N/A u2* parse_exception_table(u4 code_length, u4 exception_table_length,
3879N/A constantPoolHandle cp, TRAPS);
0N/A void parse_linenumber_table(
0N/A u4 code_attribute_length, u4 code_length,
0N/A CompressedLineNumberWriteStream** write_stream, TRAPS);
0N/A u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length,
0N/A constantPoolHandle cp, u2* localvariable_table_length,
0N/A bool isLVTT, TRAPS);
0N/A u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length,
0N/A constantPoolHandle cp, TRAPS);
0N/A void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
0N/A u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS);
0N/A typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS);
0N/A
0N/A // Classfile attribute parsing
3888N/A void parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS);
3888N/A void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, int length, TRAPS);
3638N/A u2 parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start,
3638N/A bool parsed_enclosingmethod_attribute,
3638N/A u2 enclosing_method_class_index,
3638N/A u2 enclosing_method_method_index,
3638N/A constantPoolHandle cp,
3888N/A TRAPS);
3888N/A void parse_classfile_attributes(constantPoolHandle cp,
3888N/A ClassAnnotationCollector* parsed_annotations,
3888N/A TRAPS);
3888N/A void parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS);
3888N/A void parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS);
3888N/A void parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, u4 attribute_length, TRAPS);
0N/A
0N/A // Annotations handling
0N/A typeArrayHandle assemble_annotations(u1* runtime_visible_annotations,
0N/A int runtime_visible_annotations_length,
0N/A u1* runtime_invisible_annotations,
0N/A int runtime_invisible_annotations_length, TRAPS);
3888N/A int skip_annotation(u1* buffer, int limit, int index);
3888N/A int skip_annotation_value(u1* buffer, int limit, int index);
3888N/A void parse_annotations(u1* buffer, int limit, constantPoolHandle cp,
3888N/A /* Results (currently, only one result is supported): */
3888N/A AnnotationCollector* result,
3888N/A TRAPS);
0N/A
0N/A // Final setup
939N/A unsigned int compute_oop_map_count(instanceKlassHandle super,
939N/A unsigned int nonstatic_oop_count,
939N/A int first_nonstatic_oop_offset);
939N/A void fill_oop_maps(instanceKlassHandle k,
939N/A unsigned int nonstatic_oop_map_count,
939N/A int* nonstatic_oop_offsets,
939N/A unsigned int* nonstatic_oop_counts);
0N/A void set_precomputed_flags(instanceKlassHandle k);
0N/A objArrayHandle compute_transitive_interfaces(instanceKlassHandle super,
0N/A objArrayHandle local_ifs, TRAPS);
0N/A
0N/A // Format checker methods
0N/A void classfile_parse_error(const char* msg, TRAPS);
0N/A void classfile_parse_error(const char* msg, int index, TRAPS);
0N/A void classfile_parse_error(const char* msg, const char *name, TRAPS);
0N/A void classfile_parse_error(const char* msg, int index, const char *name, TRAPS);
0N/A inline void guarantee_property(bool b, const char* msg, TRAPS) {
0N/A if (!b) { classfile_parse_error(msg, CHECK); }
0N/A }
0N/A
0N/A inline void assert_property(bool b, const char* msg, TRAPS) {
0N/A#ifdef ASSERT
0N/A if (!b) { fatal(msg); }
0N/A#endif
0N/A }
0N/A
0N/A inline void check_property(bool property, const char* msg, int index, TRAPS) {
0N/A if (_need_verify) {
0N/A guarantee_property(property, msg, index, CHECK);
0N/A } else {
0N/A assert_property(property, msg, CHECK);
0N/A }
0N/A }
0N/A
0N/A inline void check_property(bool property, const char* msg, TRAPS) {
0N/A if (_need_verify) {
0N/A guarantee_property(property, msg, CHECK);
0N/A } else {
0N/A assert_property(property, msg, CHECK);
0N/A }
0N/A }
0N/A
0N/A inline void guarantee_property(bool b, const char* msg, int index, TRAPS) {
0N/A if (!b) { classfile_parse_error(msg, index, CHECK); }
0N/A }
0N/A inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) {
0N/A if (!b) { classfile_parse_error(msg, name, CHECK); }
0N/A }
0N/A inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) {
0N/A if (!b) { classfile_parse_error(msg, index, name, CHECK); }
0N/A }
0N/A
1506N/A void throwIllegalSignature(
2062N/A const char* type, Symbol* name, Symbol* sig, TRAPS);
1506N/A
0N/A bool is_supported_version(u2 major, u2 minor);
0N/A bool has_illegal_visibility(jint flags);
0N/A
0N/A void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS);
0N/A void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS);
2062N/A void verify_legal_class_name(Symbol* name, TRAPS);
2062N/A void verify_legal_field_name(Symbol* name, TRAPS);
2062N/A void verify_legal_method_name(Symbol* name, TRAPS);
2062N/A void verify_legal_field_signature(Symbol* fieldname, Symbol* signature, TRAPS);
2062N/A int verify_legal_method_signature(Symbol* methodname, Symbol* signature, TRAPS);
0N/A void verify_legal_class_modifiers(jint flags, TRAPS);
0N/A void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS);
2062N/A void verify_legal_method_modifiers(jint flags, bool is_interface, Symbol* name, TRAPS);
0N/A bool verify_unqualified_name(char* name, unsigned int length, int type);
0N/A char* skip_over_field_name(char* name, bool slash_ok, unsigned int length);
0N/A char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS);
0N/A
710N/A bool is_anonymous() {
2263N/A assert(EnableInvokeDynamic || _host_klass.is_null(), "");
710N/A return _host_klass.not_null();
710N/A }
431N/A bool has_cp_patch_at(int index) {
2263N/A assert(EnableInvokeDynamic, "");
431N/A assert(index >= 0, "oob");
431N/A return (_cp_patches != NULL
431N/A && index < _cp_patches->length()
431N/A && _cp_patches->adr_at(index)->not_null());
431N/A }
431N/A Handle cp_patch_at(int index) {
431N/A assert(has_cp_patch_at(index), "oob");
431N/A return _cp_patches->at(index);
431N/A }
431N/A Handle clear_cp_patch_at(int index) {
431N/A Handle patch = cp_patch_at(index);
431N/A _cp_patches->at_put(index, Handle());
431N/A assert(!has_cp_patch_at(index), "");
431N/A return patch;
431N/A }
431N/A void patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS);
431N/A
431N/A // Wrapper for constantTag.is_klass_[or_]reference.
431N/A // In older versions of the VM, klassOops cannot sneak into early phases of
431N/A // constant pool construction, but in later versions they can.
431N/A // %%% Let's phase out the old is_klass_reference.
431N/A bool is_klass_reference(constantPoolHandle cp, int index) {
4034N/A return (EnableInvokeDynamic
431N/A ? cp->tag_at(index).is_klass_or_reference()
431N/A : cp->tag_at(index).is_klass_reference());
431N/A }
431N/A
0N/A public:
0N/A // Constructor
0N/A ClassFileParser(ClassFileStream* st) { set_stream(st); }
0N/A
0N/A // Parse .class file and return new klassOop. The klassOop is not hooked up
0N/A // to the system dictionary or any other structures, so a .class file can
0N/A // be loaded several times if desired.
0N/A // The system dictionary hookup is done by the caller.
0N/A //
0N/A // "parsed_name" is updated by this method, and is the name found
0N/A // while parsing the stream.
2062N/A instanceKlassHandle parseClassFile(Symbol* name,
0N/A Handle class_loader,
0N/A Handle protection_domain,
2062N/A TempNewSymbol& parsed_name,
973N/A bool verify,
431N/A TRAPS) {
710N/A KlassHandle no_host_klass;
973N/A return parseClassFile(name, class_loader, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
431N/A }
2062N/A instanceKlassHandle parseClassFile(Symbol* name,
431N/A Handle class_loader,
431N/A Handle protection_domain,
710N/A KlassHandle host_klass,
431N/A GrowableArray<Handle>* cp_patches,
2062N/A TempNewSymbol& parsed_name,
973N/A bool verify,
0N/A TRAPS);
0N/A
0N/A // Verifier checks
0N/A static void check_super_class_access(instanceKlassHandle this_klass, TRAPS);
0N/A static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS);
0N/A static void check_final_method_override(instanceKlassHandle this_klass, TRAPS);
0N/A static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS);
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP