jvm安全点(二)openjdk17 c++源码垃圾回收安全点信号函数处理线程阻塞
1. 信号处理与桩代码(Stub)
当线程访问安全点轮询页(Polling Page)时:
- 触发 SIGSEGV 信号:访问只读的轮询页会引发
SIGSEGV
异常。 - 信号处理函数:
pd_hotspot_signal_handler
检测到SafepointMechanism::is_poll_address
为真,调用SharedRuntime::get_poll_stub
获取桩代码入口地址(如polling_page_safepoint_handler_blob
)。 - 篡改 PC:
os::Posix::ucontext_set_pc(uc, stub)
将线程的 程序计数器(PC) 设置为桩代码地址。
2. 桩代码的职责
桩代码(如 polling_page_safepoint_handler_blob
)是平台相关的汇编代码,其核心逻辑为:
asm
复制
// 伪代码示例 call SafepointSynchronize::handle_polling_page_exception ; 调用安全点处理函数 ret
- 直接调用:桩代码通过
call
指令直接调用SafepointSynchronize::handle_polling_page_exception
。 - 触发阻塞:
handle_polling_page_exception
最终通过SafepointSynchronize::block
让线程阻塞在安全点。
3. 关键调用链
信号处理与安全点处理的完整路径:
信号处理函数 (javaSignalHandler)→ PosixSignals::pd_hotspot_signal_handler→ 检测到安全点轮询页(SafepointMechanism::is_poll_address)→ SharedRuntime::get_poll_stub(pc) 获取桩代码地址→ 篡改 PC 到桩代码(如 polling_page_safepoint_handler_blob)→ 桩代码执行→ SafepointSynchronize::handle_polling_page_exception→ SafepointMechanism::process→ SafepointSynchronize::block→ 线程阻塞等待安全点
4. 核心设计思想
- 信号驱动:通过操作系统的内存保护机制(轮询页不可访问)触发信号,将控制权交给 JVM。
- 间接跳转:信号处理函数不直接调用安全点逻辑,而是通过修改线程执行路径(PC),跳转到桩代码。
- 桩代码桥接:桩代码作为 桥梁,将信号处理上下文与 JVM 内部安全点处理逻辑连接。
5. 普通线程阻塞的触发
- 所有 Java 线程:无论是用户线程、JIT 编译代码线程,还是解释器执行的线程,访问轮询页时都会触发此流程。
- 统一入口:无论线程原本在执行什么,最终都会通过桩代码调用
handle_polling_page_exception
,确保所有线程在安全点处阻塞。
总结
- 信号处理函数不直接调用:
handle_polling_page_exception
由 桩代码 直接调用,而非信号处理函数本身。 - 间接触发阻塞:通过篡改 PC 到桩代码,再由桩代码触发安全点处理逻辑,最终实现线程阻塞。
- 统一安全点处理:所有 Java 线程通过此机制在安全点同步,确保 GC 等操作的安全执行。
##源码
address SharedRuntime::get_poll_stub(address pc) {address stub;// Look up the code blobCodeBlob *cb = CodeCache::find_blob(pc);// Should be an nmethodguarantee(cb != NULL && cb->is_compiled(), "safepoint polling: pc must refer to an nmethod");// Look up the relocation informationassert(((CompiledMethod*)cb)->is_at_poll_or_poll_return(pc),"safepoint polling: type must be poll");#ifdef ASSERTif (!((NativeInstruction*)pc)->is_safepoint_poll()) {tty->print_cr("bad pc: " PTR_FORMAT, p2i(pc));Disassembler::decode(cb);fatal("Only polling locations are used for safepoint");}
#endifbool at_poll_return = ((CompiledMethod*)cb)->is_at_poll_return(pc);bool has_wide_vectors = ((CompiledMethod*)cb)->has_wide_vectors();if (at_poll_return) {assert(SharedRuntime::polling_page_return_handler_blob() != NULL,"polling page return stub not created yet");stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();} else if (has_wide_vectors) {assert(SharedRuntime::polling_page_vectors_safepoint_handler_blob() != NULL,"polling page vectors safepoint stub not created yet");stub = SharedRuntime::polling_page_vectors_safepoint_handler_blob()->entry_point();} else {assert(SharedRuntime::polling_page_safepoint_handler_blob() != NULL,"polling page safepoint stub not created yet");stub = SharedRuntime::polling_page_safepoint_handler_blob()->entry_point();}log_debug(safepoint)("... found polling page %s exception at pc = "INTPTR_FORMAT ", stub =" INTPTR_FORMAT,at_poll_return ? "return" : "loop",(intptr_t)pc, (intptr_t)stub);return stub;
}bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,ucontext_t* uc, JavaThread* thread) {if (sig == SIGILL &&((info->si_addr == (caddr_t)check_simd_fault_instr)|| info->si_addr == (caddr_t)check_vfp_fault_instr|| info->si_addr == (caddr_t)check_vfp3_32_fault_instr|| info->si_addr == (caddr_t)check_mp_ext_fault_instr)) {// skip faulty instruction + instruction that sets return value to// success and set return value to failure.os::Posix::ucontext_set_pc(uc, (address)info->si_addr + 8);uc->uc_mcontext.arm_r0 = 0;return true;}address stub = NULL;address pc = NULL;bool unsafe_access = false;if (info != NULL && uc != NULL && thread != NULL) {pc = (address) os::Posix::ucontext_get_pc(uc);// Handle ALL stack overflow variations hereif (sig == SIGSEGV) {address addr = (address) info->si_addr;// check if fault address is within thread stackif (thread->is_in_full_stack(addr)) {// stack overflowStackOverflow* overflow_state = thread->stack_overflow_state();if (overflow_state->in_stack_yellow_reserved_zone(addr)) {overflow_state->disable_stack_yellow_reserved_zone();if (thread->thread_state() == _thread_in_Java) {// Throw a stack overflow exception. Guard pages will be reenabled// while unwinding the stack.stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);} else {// Thread was in the vm or native code. Return and try to finish.return true;}} else if (overflow_state->in_stack_red_zone(addr)) {// Fatal red zone violation. Disable the guard pages and fall through// to handle_unexpected_exception way down below.overflow_state->disable_stack_red_zone();tty->print_raw_cr("An irrecoverable stack overflow has occurred.");} else {// Accessing stack address below sp may cause SEGV if current// thread has MAP_GROWSDOWN stack. This should only happen when// current thread was created by user code with MAP_GROWSDOWN flag// and then attached to VM. See notes in os_linux.cpp.if (thread->osthread()->expanding_stack() == 0) {thread->osthread()->set_expanding_stack();if (os::Linux::manually_expand_stack(thread, addr)) {thread->osthread()->clear_expanding_stack();return true;}thread->osthread()->clear_expanding_stack();} else {fatal("recursive segv. expanding stack.");}}}}if (thread->thread_state() == _thread_in_Java) {// Java thread running in Java code => find exception handler if any// a fault inside compiled code, the interpreter, or a stubif (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {stub = SharedRuntime::get_poll_stub(pc);} else if (sig == SIGBUS) {// BugId 4454115: A read from a MappedByteBuffer can fault// here if the underlying file has been truncated.// Do not crash the VM in such a case.CodeBlob* cb = CodeCache::find_blob_unsafe(pc);CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;if ((nm != NULL && nm->has_unsafe_access()) || (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc))) {unsafe_access = true;}} else if (sig == SIGSEGV &&MacroAssembler::uses_implicit_null_check(info->si_addr)) {// Determination of interpreter/vtable stub/compiled code null exceptionCodeBlob* cb = CodeCache::find_blob_unsafe(pc);if (cb != NULL) {stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);}} else if (sig == SIGILL && *(int *)pc == NativeInstruction::zombie_illegal_instruction) {// Zombiestub = SharedRuntime::get_handle_wrong_method_stub();}} else if ((thread->thread_state() == _thread_in_vm ||thread->thread_state() == _thread_in_native) &&sig == SIGBUS && thread->doing_unsafe_access()) {unsafe_access = true;}// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in// and the heap gets shrunk before the field access.if (sig == SIGSEGV || sig == SIGBUS) {address addr = JNI_FastGetField::find_slowcase_pc(pc);if (addr != (address)-1) {stub = addr;}}}if (unsafe_access && stub == NULL) {// it can be an unsafe access and we haven't found// any other suitable exception reason,// so assume it is an unsafe access.address next_pc = pc + Assembler::InstructionSize;if (UnsafeCopyMemory::contains_pc(pc)) {next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);}
#ifdef __thumb__if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {next_pc = (address)((intptr_t)next_pc | 0x1);}
#endifstub = SharedRuntime::handle_unsafe_access(thread, next_pc);}if (stub != NULL) {
#ifdef __thumb__if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {intptr_t p = (intptr_t)pc | 0x1;pc = (address)p;// Clear Thumb mode bit if we're redirected into the ARM ISA based codeif (((intptr_t)stub & 0x1) == 0) {uc->uc_mcontext.arm_cpsr &= ~PSR_T_BIT;}} else {// No Thumb2 compiled stubs are triggered from ARM ISA compiled JIT'd code today.// The support needs to be added if that changesassert((((intptr_t)stub & 0x1) == 0), "can't return to Thumb code");}
#endif// save all thread context in case we need to restore itif (thread != NULL) thread->set_saved_exception_pc(pc);os::Posix::ucontext_set_pc(uc, stub);return true;}return false;
}
相关文章:
jvm安全点(二)openjdk17 c++源码垃圾回收安全点信号函数处理线程阻塞
1. 信号处理与桩代码(Stub) 当线程访问安全点轮询页(Polling Page)时: 触发 SIGSEGV 信号:访问只读的轮询页会引发 SIGSEGV 异常。信号处理函数:pd_hotspot_signal_handl…...

YOLOv7训练时4个类别只出2个类别
正常是4个类别: 但是YOLOv7训练完后预测总是只有两个类别: 而且都是LFM和SFM 我一开始检查了下特征图大小,如果输入是640*640的话,三个尺度特征图是80*80,40*40,20*20;如果输入是416*416的话,三个尺度特征…...

【论文阅读】针对BEV感知的攻击
Understanding the Robustness of 3D Object Detection with Bird’s-Eye-View Representations in Autonomous Driving 这篇文章是发表在CVPR上的一篇文章,针对基于BEV的目标检测算法进行了两类可靠性分析,即恶劣自然条件以及敌对攻击。同时也提出了一…...
18.中介者模式:思考与解读
原文地址:中介者模式:思考与解读 更多内容请关注:深入思考与解读设计模式 引言 在软件开发中,尤其是处理多个对象交互时,你是否遇到过一个问题:当多个对象需要互相通信时,系统变得复杂,难以管…...

flutter 配置 安卓、Ios启动图
android 配置启动图 launch_background.xml <?xml version"1.0" encoding"utf-8"?> <!-- Modify this file to customize your launch splash screen --> <layer-list xmlns:android"http://schemas.android.com/apk/res/android&…...

基于朴素贝叶斯与 LSTM 的假新闻检测模型对比分析
一、引言 在信息爆炸的时代,假新闻的传播对社会产生了诸多负面影响。如何快速、准确地识别假新闻成为了重要的研究课题。本文将对比传统机器学习算法(朴素贝叶斯)与深度学习模型(LSTM)在假新闻检测任务中的性能表现&am…...

【LeetCode 热题 100】搜索插入位置 / 搜索旋转排序数组 / 寻找旋转排序数组中的最小值
⭐️个人主页:小羊 ⭐️所属专栏:LeetCode 热题 100 很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~ 目录 搜索插入位置搜索二维矩阵在排序数组中查找元素的第一个和最后一个位置搜索旋转排序数组寻找旋转排序数组中的最小值…...

副业小程序YUERGS,从开发到变现
文章目录 我为什么写这个小程序网站转小程序有什么坑有什么推广渠道个人开发者如何变现简单介绍YUERGS小程序给独立开发者一点小建议 我为什么写这个小程序 关注我的粉丝应该知道,我在硕士阶段就已经掌握了小程序开发技能,并写了一个名为“约球online”…...
计算机视觉与深度学习 | Python实现EMD-VMD-LSTM时间序列预测(完整源码和数据)
EMD-VMD-LSTM 一、完整代码实现二、代码结构解析三、关键参数说明四、性能优化建议五、工业部署方案以下是用Python实现EMD-VMD-LSTM时间序列预测的完整代码,结合经验模态分解(EMD)、变分模态分解(VMD)与LSTM深度学习模型,适用于复杂非平稳信号的预测任务。代码包含数据生…...

基于LLM合成高质量情感数据,提升情感分类能力!!
摘要:大多数用于情感分析的数据集缺乏意见表达的上下文,而上下文对于理解情绪往往至关重要,并且这些数据集主要局限于几种情绪类别。像 GPT-4 这样的基础大型语言模型(Foundation Large Language Models,LLMsÿ…...

网络检测工具InternetTest v8.9.1.2504 单文件版,支持一键查询IP/DNS、WIFI密码信息
—————【下 载 地 址】——————— 【本章下载一】:https://drive.uc.cn/s/295e068b79314 【本章下载二】:https://pan.xunlei.com/s/VOQDXguH0DYPxrql5y2zlkhTA1?pwdg2nx# 【百款黑科技】:https://ucnygalh6wle.feishu.cn/wiki/…...
SpringBoot中使用Flux实现流式返回的技术总结
背景 近期在使用deepseek/openai等网页和APP时,发现大模型在思考和回复时,内容是一点点的显示出来的,于是好奇他们的实现方式。经调研和使用开发者工具抓取请求,每次聊天会向后台发送一个http请求,而这个接口跟普通接…...

【网络编程】十、详解 UDP 协议
文章目录 Ⅰ. 传输层概述1、进程之间的通信2、再谈端口号端口号的引出五元组标识一个通信端口号范围划分常见的知名端口号查看知名端口号协议号 VS 端口号 3、两个问题一个端口号是否可以被多个进程绑定?一个进程是否可以绑定多个端口号? 4、部分常见指令…...
从零开始理解Jetty:轻量级Java服务器的入门指南
目录 一、Jetty是什么?先看一个生活比喻 二、5分钟快速入门:搭建你的第一个Jetty服务 步骤1:Maven依赖配置 步骤2:编写简易Servlet(厨房厨师) 步骤3:组装服务器(餐厅开业准备&am…...
python05——循环结构
1、while循环 n0 #初始条件 while n<5: #判断print(hello python) #要重复执行的代码print(n) #注意同级代码缩进相同n1 #计数器结果: hello python 0 hello python 1 hello python 2 hello python 3 hello python 4 hello python 5 #求阶乘和 sum0 n1 whil…...
windows触摸板快捷指南
以下是结构化整理后的触控手势说明,采用清晰的层级划分和标准化表述: **触控手势操作规范****1. 单指操作****2. 双指操作****3. 三指操作****4. 四指操作** **优化说明:** 触控手势操作规范 1. 单指操作 手势功能描述等效操作单击滑动选择…...
STM32 ADC 模数转换器详解:原理、配置与应用
STM32 ADC 模数转换器详解:原理、配置与应用 在嵌入式系统中,模数转换(ADC)是实现传感器信号采集、信号处理等任务的关键环节。STM32 微控制器作为一款功能强大的 32 位微控制器,其内置的 ADC 模块为开发者提供了高效…...

[目标检测] YOLO系列算法讲解
前言 目标检测就是做到给模型输入一张图片或者视频,模型可以迅速判断出视频和图片里面感兴趣的目标所有的位置和它 的类别,而当前最热门的目标检测的模型也就是YOLO系列了。 YOLO系列的模型的提出,是为了解决当时目标检测的模型帧率太低而提…...
React 中,闭包陷阱
文章目录 前言1. 经典闭包陷阱示例过期状态问题 2. 解决方案2.1 正确声明依赖数组2.2 使用 useRef 捕获最新值**2.3 使用函数式更新(针对状态更新)****2.4 使用 useCallback 冻结闭包** **3. 异步操作中的闭包陷阱****事件监听示例** **4. 自定义 Hooks …...

.NET NativeAOT 指南
目录 1. 引言 2. 什么是 .NET NativeAOT? 2.1 NativeAOT 的定义 2.2 NativeAOT 与传统 JIT 的对比 2.3 NativeAOT 的适用场景 3. NativeAOT 的核心优势 3.1 性能提升 3.2 简化部署 3.3 更小的应用体积 3.4 知识产权保护 4. NativeAOT 的基本用法 4.1 环境…...

uniapp-商城-57-后台 新增商品(弹窗属性数据添加父级)
后台增加商品,需要添加相关的数据信息,这里还要添加属性,前面已经对相关的界面布局继续了编写。这里还要对页面添加的数据,置入到云数据库,继续永久保存,便于后期的使用。这里主要是讲属性数据 父级信息的添…...

摩方 12 代 N200 迷你主机(Ubuntu 系统)WiFi 抓包环境配置教程
摩方12代N200迷你主机标配 Intel AX201无线网卡,支持 WiFi 6 协议(802.11ax)及蓝牙5.2。此网卡兼容主流抓包工具,但需注意: 驱动兼容性:Ubuntu 20.04及以上内核版本(5.4)默认支持AX2…...
matlab多智能体网络一致性研究
一个基于连续时间多智能体系统(Multi-Agent Systems, MAS)的一阶一致性协议的MATLAB仿真代码,包含网络拓扑建模、一致性协议设计和收敛性分析。代码支持固定拓扑和时变拓扑,适用于学术研究。 1. 基础模型与代码框架 (1) 网络拓扑…...

Unity(URP渲染管线)的后处理、动画制作、虚拟相机(Virtual Camera)
一、URP渲染管线 渲染管线是一系列渲染操作的集合,Unity提供了内置渲染管线(Built-In)和可编程渲染管线(SRP)两类渲染管线。内置渲染管线是Unity的默认渲染管线,其自定义选项有限。而可编程渲染管线可以通…...
C语言:在 Win 10 上,gcc 如何编译 gtk 应用程序
在 Windows 10 上使用 g(或 gcc)编译基于 GTK 的 C 语言程序是完全可行的,且相比 Tcc 更为推荐,因为 g(GNU 编译器套件)对 GTK 的支持更加完善,配置也更简单。以下是详细步骤和注意事项…...
阿里云CMH镜像迁移与SMC整机迁移对比及功能详解(同地域跨主体账号场景)
文章目录 一、核心功能对比二、CMH镜像迁移操作流程1.资源调研2.镜像共享3.迁移验证4.限制: 三、SMC整机迁移操作流程1.迁移源导入2.任务配置3.增量同步4.应用验证…...
用vue和go实现登录加密
前端使用CryptoJS默认加密方法: var pass CryptoJS.AES.encrypt(formData.password, key.value).toString()使用 CryptoJS.AES.encrypt() 时不指定加密模式和参数时,CryptoJS 默认会执行以下操作 var encrypted CryptoJS.AES.encrypt("明文&quo…...
政府数据开放试点企业如何抢占特许经营协议黄金席位
首席数据官高鹏律师团队 《中共中央办公厅 国务院办公厅关于 加快公共数据资源开发利用的意见》的落地,标志着数据从“封闭管理的行政资源”正式转变为“可流通的市场要素”。但机遇与风险从来是一枚硬币的两面——特许经营协议的黄金席位背后,隐藏着…...
CSS 锚点滑动效果的技术
CSS 锚点滑动效果的技术 引言 介绍锚点滑动效果的概念及其在网页设计中的重要性。简要说明 基本锚点链接 如何使用HTML中的<a>标签创建基本的锚点链接。示例代码: <a href"#section1">跳转到第一部分</a> <div id"section…...

mac-M系列芯片安装软件报错:***已损坏,无法打开。推出磁盘问题
因为你安装的软件在Intel 或arm芯片的mac上没有签名导致。 首先打开任何来源操作 在系统设置中配置,如下图: 2. 然后打开终端,输入: sudo spctl --master-disable然后输入电脑锁屏密码 打开了任何来源,还遇到已损坏…...