jvm安全点(一)openjdk17 c++源码垃圾回收安全点信号函数处理线程阻塞

1. 信号处理入口​

  • JVM_HANDLE_XXX_SIGNAL​ 是 JVM 处理信号的统一入口,负责处理 SIGSEGV、SIGBUS 等信号。
  • javaSignalHandler​ 是实际注册到操作系统的信号处理函数,直接调用 JVM_HANDLE_XXX_SIGNAL

​2. 安全点轮询页的识别​

在 PosixSignals::pd_hotspot_signal_handler 中,以下代码检测是否因访问安全点轮询页触发了信号:

 

c

复制

 

if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) { stub = SharedRuntime::get_poll_stub(pc); }

  • SafepointMechanism::is_poll_address​ 检查触发异常的地址是否是安全点轮询页。
  • 如果是,获取一个 ​​桩代码地址(Stub)​​ stub,该桩代码用于跳转到安全点处理逻辑。

​3. 桩代码与上下文切换​

  • SharedRuntime::get_poll_stub(pc)​ 返回一段平台相关的桩代码地址(如 polling_page_rerun_stub)。
  • os::Posix::ucontext_set_pc(uc, stub)​ 修改信号上下文中的程序计数器(PC),使线程在信号处理返回后跳转到桩代码执行。

​4. 桩代码触发安全点处理​

桩代码(如 polling_page_rerun_stub)的伪代码如下:

 

asm

复制

 

mov %rsp, %rax ; 保存寄存器状态 call handle_poll ; 调用安全点处理函数 ret

  • handle_poll​ 是 JVM 内部函数,最终调用 SafepointSynchronize::handle_polling_page_exception
  • 线程通过桩代码进入安全点处理流程,最终阻塞在安全点屏障。

​5. 核心调用链​

信号处理流程最终通过以下路径调用 handle_polling_page_exception

信号处理函数 (javaSignalHandler)→ pd_hotspot_signal_handler (识别安全点轮询页)→ 设置 PC 到桩代码 (polling_page_safepoint_handler_blob)→ 桩代码调用 polling_page_safepoint_handler_blob→ ThreadSafepointState::handle_polling_page_exception→ SafepointMechanism::process_if_requested→ SafepointSynchronize::block

​关键设计思想​

  1. ​信号驱动​​:通过内存页保护机制(轮询页不可访问)触发 SIGSEGV,将线程控制权交给 JVM。
  2. ​上下文篡改​​:在信号处理中修改线程的 PC,使其跳转到桩代码。
  3. ​桩代码桥接​​:桩代码将信号处理上下文与 JVM 内部安全点逻辑连接,最终调用 handle_polling_page_exception

​总结​

  • 这段代码是 ​​安全点轮询页的信号处理入口​​,通过识别 SIGSEGV 信号和轮询地址,篡改线程执行路径,间接调用 handle_polling_page_exception
  • 最终目的是让所有 Java 线程在安全点处阻塞,等待垃圾回收完成。

##源码

extern "C" JNIEXPORT
int JVM_HANDLE_XXX_SIGNAL(int sig, siginfo_t* info,void* ucVoid, int abort_if_unrecognized)
{assert(info != NULL && ucVoid != NULL, "sanity");// Note: it's not uncommon that JNI code uses signal/sigset to install,// then restore certain signal handler (e.g. to temporarily block SIGPIPE,// or have a SIGILL handler when detecting CPU type). When that happens,// this handler might be invoked with junk info/ucVoid. To avoid unnecessary// crash when libjsig is not preloaded, try handle signals that do not require// siginfo/ucontext first.// Preserve errno value over signal handler.//  (note: RAII ok here, even with JFR thread crash protection, see below).ErrnoPreserver ep;// Unblock all synchronous error signals (see JDK-8252533)PosixSignals::unblock_error_signals();ucontext_t* const uc = (ucontext_t*) ucVoid;Thread* const t = Thread::current_or_null_safe();// Handle JFR thread crash protection.//  Note: this may cause us to longjmp away. Do not use any code before this//  point which really needs any form of epilogue code running, eg RAII objects.os::ThreadCrashProtection::check_crash_protection(sig, t);bool signal_was_handled = false;// Handle assertion poison page accesses.
#ifdef CAN_SHOW_REGISTERS_ON_ASSERTif (!signal_was_handled &&((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison)) {signal_was_handled = handle_assert_poison_fault(ucVoid, info->si_addr);}
#endifif (!signal_was_handled) {// Handle SafeFetch access.
#ifndef ZEROif (uc != NULL) {address pc = os::Posix::ucontext_get_pc(uc);if (StubRoutines::is_safefetch_fault(pc)) {os::Posix::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));signal_was_handled = true;}}
#else// See JDK-8076185if (sig == SIGSEGV || sig == SIGBUS) {sigjmp_buf* const pjb = get_jmp_buf_for_continuation();if (pjb) {siglongjmp(*pjb, 1);}}
#endif // ZERO}// Ignore SIGPIPE and SIGXFSZ (4229104, 6499219).if (!signal_was_handled &&(sig == SIGPIPE || sig == SIGXFSZ)) {PosixSignals::chained_handler(sig, info, ucVoid);signal_was_handled = true; // unconditionally.}// Call platform dependent signal handler.if (!signal_was_handled) {JavaThread* const jt = (t != NULL && t->is_Java_thread()) ? (JavaThread*) t : NULL;signal_was_handled = PosixSignals::pd_hotspot_signal_handler(sig, info, uc, jt);}// From here on, if the signal had not been handled, it is a fatal error.// Give the chained signal handler - should it exist - a shot.if (!signal_was_handled) {signal_was_handled = PosixSignals::chained_handler(sig, info, ucVoid);}// Invoke fatal error handling.if (!signal_was_handled && abort_if_unrecognized) {// Extract pc from context for the error handler to display.address pc = NULL;if (uc != NULL) {// prepare fault pc address for error reporting.if (S390_ONLY(sig == SIGILL || sig == SIGFPE) NOT_S390(false)) {pc = (address)info->si_addr;} else if (ZERO_ONLY(true) NOT_ZERO(false)) {// Non-arch-specific Zero code does not really know the pc.// This can be alleviated by making arch-specific os::Posix::ucontext_get_pc// available for Zero for known architectures. But for generic Zero// code, it would still remain unknown.pc = NULL;} else {pc = os::Posix::ucontext_get_pc(uc);}}// For Zero, we ignore the crash context, because://  a) The crash would be in C++ interpreter code, so context is not really relevant;//  b) Generic Zero code would not be able to parse it, so when generic error//     reporting code asks e.g. about frames on stack, Zero would experience//     a secondary ShouldNotCallThis() crash.VMError::report_and_die(t, sig, pc, info, NOT_ZERO(ucVoid) ZERO_ONLY(NULL));// VMError should not return.ShouldNotReachHere();}return signal_was_handled;
}// Entry point for the hotspot signal handler.
static void javaSignalHandler(int sig, siginfo_t* info, void* ucVoid) {// Do not add any code here!// Only add code to either JVM_HANDLE_XXX_SIGNAL or PosixSignals::pd_hotspot_signal_handler.(void)JVM_HANDLE_XXX_SIGNAL(sig, info, ucVoid, true);
}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;
}

##gdb调试堆栈

(gdb) bt
#0  PosixSignals::pd_hotspot_signal_handler (sig=11, info=0x7ffff7bfdb70, uc=0x7ffff7bfda40, thread=0x7ffff002ab00)at /home/yym/openjdk17/jdk17-master/src/hotspot/os_cpu/linux_x86/os_linux_x86.cpp:213
#1  0x00007ffff6a616e1 in JVM_handle_linux_signal (sig=11, info=0x7ffff7bfdb70, ucVoid=0x7ffff7bfda40, abort_if_unrecognized=1)at /home/yym/openjdk17/jdk17-master/src/hotspot/os/posix/signals_posix.cpp:628
#2  0x00007ffff6a617c1 in javaSignalHandler (sig=11, info=0x7ffff7bfdb70, ucVoid=0x7ffff7bfda40) at /home/yym/openjdk17/jdk17-master/src/hotspot/os/posix/signals_posix.cpp:672
#3  <signal handler called>
#4  0x00007fffe100064d in ?? ()
#5  0x0000000000000246 in ?? ()
#6  0x00007fffe1000744 in ?? ()
#7  0x00007ffff002ab00 in ?? ()
#8  0x00007ffff7bfe360 in ?? ()
#9  0x00007ffff6c3a3f4 in VM_Version::get_processor_features () at /home/yym/openjdk17/jdk17-master/src/hotspot/cpu/x86/vm_version_x86.cpp:630

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/diannao/83675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序:封装表格组件并引用

一、效果 封装表格组件,在父页面中展示表格组件并显示数据 二、表格组件 1、创建页面 创建一个components文件夹,专门用于存储组件的文件夹 创建Table表格组件 2、视图层 (1)表头数据 这里会从父组件中传递表头数据,这里为columns,后续会讲解数据由来 循环表头数组,…

【FMC216】基于 VITA57.1 的 2 路 TLK2711 发送、2 路 TLK2711 接收 FMC 子卡模块

产品概述 FMC216 是一款基于 VITA57.1 标准规范的 2 路 TLK2711 接收、2 路 TLK2711 发送 FMC 子卡模块。该板卡支持 2 路 TLK2711 数据的收发&#xff0c;支持线速率 1.6Gbps&#xff0c;经过 TLK2711 高速串行收发器&#xff0c;可以将 1.6Gbps 的高速串行数据解串为 16 位并…

K8S Gateway API 快速开始、胎教级教程

假设有如下三个节点的 K8S 集群&#xff1a; ​​ k8s31master 是控制节点 k8s31node1、k8s31node2 是工作节点 容器运行时是 containerd 一、Gateway 是什么 背景和目的 入口&#xff08;Ingress&#xff09;目前已停止更新。新的功能正在集成至网关 API 中。在 Kubernetes …

时序数据库IoTDB分布式架构解析与运维指南

一、IoTDB分布式架构概述 分布式系统由一组独立的计算机组成&#xff0c;通过网络通信&#xff0c;对外表现为一个统一的整体。IoTDB的原生分布式架构将服务分为两个核心部分&#xff1a; ‌ConfigNode&#xff08;CN&#xff09;‌&#xff1a;管理节点&#xff0c;负责管理…

Ubuntu 20.04 LTS 中部署 网页 + Node.js 应用 + Nginx 跨域配置 的详细步骤

Ubuntu 20.04 LTS 中部署 网页 Node.js 应用 Nginx 跨域配置 的详细步骤 一、准备工作1、连接服务器2、更新系统 二、安装 Node.js 环境1、安装 Node.js 官方 PPA&#xff08;用于获取最新稳定版&#xff09;&#xff1a;2、安装 Node.js 和 npm&#xff08;LTS 长期支持版本…

3DVR制作的工具或平台

3DVR&#xff08;三维虚拟现实&#xff09;是利用三维图像技术和虚拟现实技术&#xff0c;将真实场景进行三维扫描并转换成计算机可识别的三维模型&#xff0c;使用户能够在虚拟空间中自由漫游&#xff0c;体验身临其境的感觉。3DVR技术结合了全景拍摄和虚拟现实&#xff0c;提…

垂直智能体:企业AI落地的正确打开方式

在当前AI浪潮中&#xff0c;许多企业急于跟进&#xff0c;推出自己的AI智能体解决方案。然而&#xff0c;市场上大量出现的"万能型"智能体却鲜有真正解决实际问题的产品。本文将探讨为何企业应该专注于开发垂直领域智能体&#xff0c;而非追求表面上的全能&#xff0…

软件工程各种图总结

目录 1.数据流图 2.N-S盒图 3.程序流程图 4.UML图 UML用例图 UML状态图 UML时序图 5.E-R图 首先要先了解整个软件生命周期&#xff1a; 通常包含以下五个阶段&#xff1a;需求分析-》设计-》编码 -》测试-》运行和维护。 软件工程中应用到的图全部有&#xff1a;系统…

王者荣耀游戏测试场景题

如何测试一个新英雄&#xff1a;方法论与实践维度 测试一个新英雄不仅仅是“打打打”&#xff0c;而是一套完整的测试流程&#xff0c;包括设计文档验证、功能验证、数值验证、性能验证、交互验证等。可以从以下多个角度展开&#xff1a; &#x1f50d; 1. 方法论维度 ✅ 测试…

第四天的尝试

目录 一、每日一言 二、练习题 三、效果展示 四、下次题目 五、总结 一、每日一言 很抱歉的说一下&#xff0c;我昨天看白色巨塔电视剧&#xff0c;看的入迷了&#xff0c;同时也看出一些道理&#xff0c;学到东西&#xff1b; 但是把昨天的写事情给忘记了&#xff0c;今天…

多模态大语言模型arxiv论文略读(七十八)

AID: Adapting Image2Video Diffusion Models for Instruction-guided Video Prediction ➡️ 论文标题&#xff1a;AID: Adapting Image2Video Diffusion Models for Instruction-guided Video Prediction ➡️ 论文作者&#xff1a;Zhen Xing, Qi Dai, Zejia Weng, Zuxuan W…

优化 Spring Boot 应用启动性能的实践指南

1. 引言 Spring Boot 以其“开箱即用”的特性深受开发者喜爱,但随着项目复杂度的增加,应用的启动时间也可能会变得较长。对于云原生、Serverless 等场景而言,快速启动是一个非常关键的指标。 2. 分析启动过程 2.1 启动阶段概述 Spring Boot 的启动流程主要包括以下几个阶…

Ubuntu下配置VScode出现#include错误请更新includePath的解决方法

首先Ubuntu新手小白一定要先安装g&#xff0c;安装方法是&#xff1a; 在桌面右键打开终端&#xff0c;输入&#xff1a;sudo apt-get install g 安装好g之后&#xff0c;在vscode终端输入&#xff1a;g -v -E -x c - 输出这些路径&#xff0c;复制 如果还存在显示cout不存在的…

【背包dp】小结

背包问题总结 一、什么是背包问题&#xff1f; 定义&#xff1a;给定一个容量为 W 的背包和 n 件物品&#xff0c;每件物品有一个重量 w[i] 和价值 v[i]&#xff0c;要求选择若干物品放入背包&#xff0c;在不超过容量的前提下&#xff0c;使总价值最大。 背包问题本质是&am…

济南国网数字化培训班学习笔记-第三组-1-电力通信传输网认知

电力通信传输网认知 电力通信基本情况 传输介质 传输介质类型&#xff08;导引与非导引&#xff09; 导引传输介质&#xff0c;如电缆、光纤&#xff1b; 非导引传输介质&#xff0c;如无线电波&#xff1b; 传输介质的选择影响信号传输质量 信号传输模式&#xff08;单工…

代码随想录算法训练营第六十四天| 图论9—卡码网47. 参加科学大会,94. 城市间货物运输 I

每日被新算法方式轰炸的一天&#xff0c;今天是dijkstra&#xff08;堆优化版&#xff09;以及Bellman_ford &#xff0c;尝试理解中&#xff0c;属于是只能照着代码大概说一下在干嘛。 47. 参加科学大会 https://kamacoder.com/problempage.php?pid1047 dijkstra&#xff08…

upload-labs通关笔记-第8关 文件上传之点绕过

目录 一、点绕过原理 二、deldot()函数 三、源码分析 四、渗透实战 1、构建脚本test8.php 2、打开靶场 3、bp开启拦截 4、点击上传 5、bp拦截 6、后缀名增加点 7、发包并获取脚本地址 8、访问脚本 本文通过《upload-labs靶场通关笔记系列》来进行upload-labs靶场的渗…

Spring Web MVC————入门(3)

今天我们来一个大练习&#xff0c;我们要实现一个登录界面&#xff0c;登录进去了先获取到登录人信息&#xff0c;可以选择计算器和留言板两个功能&#xff0c;另外我们是学后端的&#xff0c;对于前端我们会些基础的就行了&#xff0c;知道ajax怎么用&#xff0c;知道怎么关联…

PhpStudy | PhpStudy 工具安装 —— Windows 系统安装 PhpStudy

&#x1f31f;想了解这个工具的其它相关笔记&#xff1f;看看这个&#xff1a;[网安工具] 服务器环境配置工具 —— PhpStudy 使用手册 笔者备注&#xff1a;Windows 中安装 PhpStudy 属于傻瓜式安装&#xff0c;本文只是为了体系完善而发。 在前面的章节中&#xff0c;笔者简…

K230 ISP:一种新的白平衡标定方法

第一次遇见需要利用光谱响应曲线进行白平衡标定的方法。很好奇是如何利用光谱响应曲线进行白平衡标定的。 参考资料参考&#xff1a;K230 ISP图像调优指南 K230 介绍 嘉楠科技 Kendryte 系列 AIoT 芯片中的最新一代 AIoT SoC K230 芯片采用全新的多核异构单元加速计算架构&a…