openjdk17 hotspot JVM解析class文件

embedded/2024/10/30 19:51:19/

##openjdk17 hotspot JVM解析class文件

ClassFileParser::parse_stream 解析字节码

void ClassFileParser::parse_stream(const ClassFileStream* const stream,TRAPS) {assert(stream != NULL, "invariant");assert(_class_name != NULL, "invariant");// BEGIN STREAM PARSINGstream->guarantee_more(8, CHECK);  // magic, major, minor// Magic valueconst u4 magic = stream->get_u4_fast();guarantee_property(magic == JAVA_CLASSFILE_MAGIC,"Incompatible magic value %u in class file %s",magic, CHECK);// Version numbers_minor_version = stream->get_u2_fast();_major_version = stream->get_u2_fast();// Check version numbers - we check this even with verifier offverify_class_version(_major_version, _minor_version, _class_name, CHECK);stream->guarantee_more(3, CHECK); // length, first cp tagu2 cp_size = stream->get_u2_fast();guarantee_property(cp_size >= 1, "Illegal constant pool size %u in class file %s",cp_size, CHECK);_orig_cp_size = cp_size;if (is_hidden()) { // Add a slot for hidden class name.cp_size++;}_cp = ConstantPool::allocate(_loader_data,cp_size,CHECK);ConstantPool* const cp = _cp;parse_constant_pool(stream, cp, _orig_cp_size, CHECK);assert(cp_size == (const u2)cp->length(), "invariant");// ACCESS FLAGSstream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len// Access flagsjint flags;// JVM_ACC_MODULE is defined in JDK-9 and later.if (_major_version >= JAVA_9_VERSION) {flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);} else {flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;}if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {// Set abstract bit for old class files for backward compatibilityflags |= JVM_ACC_ABSTRACT;}verify_legal_class_modifiers(flags, CHECK);short bad_constant = class_bad_constant_seen();if (bad_constant != 0) {// Do not throw CFE until after the access_flags are checked because if// ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);return;}_access_flags.set_flags(flags);// This class and superclass_this_class_index = stream->get_u2_fast();check_property(valid_cp_range(_this_class_index, cp_size) &&cp->tag_at(_this_class_index).is_unresolved_klass(),"Invalid this class index %u in constant pool in class file %s",_this_class_index, CHECK);Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);assert(class_name_in_cp != NULL, "class_name can't be null");// Don't need to check whether this class name is legal or not.// It has been checked when constant pool is parsed.// However, make sure it is not an array type.if (_need_verify) {guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,"Bad class name in class file %s",CHECK);}#ifdef ASSERT// Basic sanity checksif (_is_hidden) {assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");}
#endif// Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.if (_is_hidden) {assert(_class_name != NULL, "Unexpected null _class_name");
#ifdef ASSERTif (_need_verify) {verify_legal_class_name(_class_name, CHECK);}
#endif} else {// Check if name in class file matches given nameif (_class_name != class_name_in_cp) {if (_class_name != vmSymbols::unknown_class_name()) {ResourceMark rm(THREAD);Exceptions::fthrow(THREAD_AND_LOCATION,vmSymbols::java_lang_NoClassDefFoundError(),"%s (wrong name: %s)",class_name_in_cp->as_C_string(),_class_name->as_C_string());return;} else {// The class name was not known by the caller so we set it from// the value in the CP.update_class_name(class_name_in_cp);}// else nothing to do: the expected class name matches what is in the CP}}// Verification prevents us from creating names with dots in them, this// asserts that that's the case.assert(is_internal_format(_class_name), "external class name format used internally");if (!is_internal()) {LogTarget(Debug, class, preorder) lt;if (lt.is_enabled()){ResourceMark rm(THREAD);LogStream ls(lt);ls.print("%s", _class_name->as_klass_external_name());if (stream->source() != NULL) {ls.print(" source: %s", stream->source());}ls.cr();}}// SUPERKLASS_super_class_index = stream->get_u2_fast();_super_klass = parse_super_class(cp,_super_class_index,_need_verify,CHECK);// Interfaces_itfs_len = stream->get_u2_fast();parse_interfaces(stream,_itfs_len,cp,&_has_nonstatic_concrete_methods,CHECK);assert(_local_interfaces != NULL, "invariant");// Fields (offsets are filled in later)_fac = new FieldAllocationCount();parse_fields(stream,_access_flags.is_interface(),_fac,cp,cp_size,&_java_fields_count,CHECK);assert(_fields != NULL, "invariant");// MethodsAccessFlags promoted_flags;parse_methods(stream,_access_flags.is_interface(),&promoted_flags,&_has_final_method,&_declares_nonstatic_concrete_methods,CHECK);assert(_methods != NULL, "invariant");// promote flags from parse_methods() to the klass' flags_access_flags.add_promoted_flags(promoted_flags.as_int());if (_declares_nonstatic_concrete_methods) {_has_nonstatic_concrete_methods = true;}// Additional attributes/annotations_parsed_annotations = new ClassAnnotationCollector();parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);assert(_inner_classes != NULL, "invariant");// Finalize the Annotations metadata object,// now that all annotation arrays have been created.create_combined_annotations(CHECK);// Make sure this is the end of class file streamguarantee_property(stream->at_eos(),"Extra bytes at the end of class file %s",CHECK);// all bytes in stream read and parsed
}

##解析方法

parse_methods(stream,
                _access_flags.is_interface(),
                &promoted_flags,
                &_has_final_method,
                &_declares_nonstatic_concrete_methods,
                CHECK);

void ClassFileParser::parse_methods(const ClassFileStream* const cfs,bool is_interface,AccessFlags* promoted_flags,bool* has_final_method,bool* declares_nonstatic_concrete_methods,TRAPS) {assert(cfs != NULL, "invariant");assert(promoted_flags != NULL, "invariant");assert(has_final_method != NULL, "invariant");assert(declares_nonstatic_concrete_methods != NULL, "invariant");assert(NULL == _methods, "invariant");cfs->guarantee_more(2, CHECK);  // lengthconst u2 length = cfs->get_u2_fast();if (length == 0) {_methods = Universe::the_empty_method_array();} else {_methods = MetadataFactory::new_array<Method*>(_loader_data,length,NULL,CHECK);for (int index = 0; index < length; index++) {Method* method = parse_method(cfs,is_interface,_cp,promoted_flags,CHECK);if (method->is_final()) {*has_final_method = true;}// declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags// used for interface initialization, and default method inheritance analysisif (is_interface && !(*declares_nonstatic_concrete_methods)&& !method->is_abstract() && !method->is_static()) {*declares_nonstatic_concrete_methods = true;}_methods->at_put(index, method);}if (_need_verify && length > 1) {// Check duplicated methodsResourceMark rm(THREAD);NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, NameSigHash*, HASH_ROW_SIZE);initialize_hashtable(names_and_sigs);bool dup = false;const Symbol* name = NULL;const Symbol* sig = NULL;{debug_only(NoSafepointVerifier nsv;)for (int i = 0; i < length; i++) {const Method* const m = _methods->at(i);name = m->name();sig = m->signature();// If no duplicates, add name/signature in hashtable names_and_sigs.if (!put_after_lookup(name, sig, names_and_sigs)) {dup = true;break;}}}if (dup) {classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s",name->as_C_string(), sig->as_klass_external_name(), THREAD);}}}
}


http://www.ppmy.cn/embedded/133687.html

相关文章

WebGL进阶(四)-视点和视线

理论基础&#xff1a; 顶点着色器 Vertex Shader 主要是负责处理顶点位置、顶点颜色、顶点向量等顶点的数据&#xff1b;处理一些顶点的变换&#xff1a;例如在进行视图变换和投影变换时MVP矩阵会改变顶点的位置信息。 输入&#xff1a; 顶点着色器输入部分主要是声明&…

Spark SQL DSL

1、 Spark sql -- 代替hive的&#xff08;并非完全代替&#xff09; (1) Spark sql 和 hive 区别 : 两者都是写sql的&#xff0c;区别是计算引擎不一样 hive -- 计算引擎是MapReduce &#xff0c;是通过MR做计算的 Spark sql -- 计算引擎是Saprk Core&#xff…

云腾五洲的智联引擎是什么?

智联引擎是成都云腾五洲科技有限公司旗下的数智化转型服务平台&#xff0c;它提供云边协同的分布式物联网平台引擎服务。这一平台以其强大的功能和灵活性&#xff0c;为全行业提供数智化转型的新动力&#xff0c;帮助企业在数智化升级中实现持续增长。 核心能力 智联引擎的核心…

在面试了些外包以后,我有了些自己的思考

大家好&#xff0c;我是洋子&#xff0c;最近公司在降本增效&#xff0c;需要把外包从北京迁移到陕西的某新一线城市&#xff0c;其实就是变相裁员&#xff0c;减少外包的成本&#xff0c;裁掉现有的员工&#xff0c;重新招聘新人 在整个测试行业&#xff0c;外包测试的比重是…

STM32F103C8T6 IO 操作

1.开启相关时钟 在 STM32 微控制器中&#xff0c;开启 GPIO 端口的时钟是确保 IO 口可以正常工作的第一步。 查找 RCC 寄存器使能时钟 在 STM32 中&#xff0c;时钟控制的寄存器通常位于 RCC (Reset and Clock Control) 模块中。不同的 STM32 系列&#xff08;如 STM32F1、STM…

【Windows电脑通过cmd命令查看电脑电池健康度】

Cmd输入 powercfg /batteryreport打印电池使用报告&#xff0c;打开即可 此处查看FULL CHARGE CAPACITY&#xff08;充满电容量&#xff09;

研二了,该想想做啥呢?

写于22年10月24日&#xff0c;之前删了&#xff0c;今天回看&#xff0c;自己当时还挺有意思哈哈哈 一、自我介绍 行秋&#xff0c;男&#xff0c;24岁&#xff0c;电子信息专业&#xff0c;硕士在读。 二、新学期目标 1、好好学习 ① 学好一门语言&#xff0c;学好英语&am…

提升RAG系统的回答质量:PDF解析代码详解-PdfParser核心流程

在上一篇文章中&#xff0c;我们探讨了如何通过计算机视觉大模型提升RAG系统解析PDF文档的能力&#xff0c;并展示了该技术在行业文档识别中的实际应用效果。文章发布后&#xff0c;受到了广泛关注。读者主要关心两个问题&#xff1a;其一&#xff0c;如何在PDF文档识别过程中编…