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

1. 信号处理与桩代码(Stub)​

当线程访问安全点轮询页(Polling Page)时:

  1. ​触发 SIGSEGV 信号​​:访问只读的轮询页会引发 SIGSEGV 异常。
  2. ​信号处理函数​​:pd_hotspot_signal_handler 检测到 SafepointMechanism::is_poll_address 为真,调用 SharedRuntime::get_poll_stub 获取桩代码入口地址(如 polling_page_safepoint_handler_blob)。
  3. ​篡改 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;
}

##源码

(gdb) bt
#0  SafepointSynchronize::block (thread=0x7ffff02c8200) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/safepoint.cpp:692
#1  0x00007ffff6966332 in SafepointMechanism::process (thread=0x7ffff02c8200, allow_suspend=false)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/safepointMechanism.cpp:125
#2  0x00007ffff5daa6e5 in SafepointMechanism::process_if_requested (thread=0x7ffff02c8200, allow_suspend=false)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/safepointMechanism.inline.hpp:99
#3  0x00007ffff5daaf6d in ThreadBlockInVMPreprocess<InFlightMutexRelease>::~ThreadBlockInVMPreprocess (this=0x7fffd0dfecc8, __in_chrg=<optimized out>)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/interfaceSupport.inline.hpp:264
#4  0x00007ffff5daad70 in ThreadBlockInVM::~ThreadBlockInVM (this=0x7fffd0dfecc0, __in_chrg=<optimized out>)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/interfaceSupport.inline.hpp:289
#5  0x00007ffff696f9ed in ServiceThread::service_thread_entry (jt=0x7ffff02c8200, __the_thread__=0x7ffff02c8200)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/serviceThread.cpp:191
#6  0x00007ffff6b5d26c in JavaThread::thread_main_inner (this=0x7ffff02c8200) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/thread.cpp:1305
#7  0x00007ffff6b5d102 in JavaThread::run (this=0x7ffff02c8200) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/thread.cpp:1288
#8  0x00007ffff6b5a805 in Thread::call_run (this=0x7ffff02c8200) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/runtime/thread.cpp:394
#9  0x00007ffff6874aeb in thread_native_entry (thread=0x7ffff02c8200) at /home/yym/openjdk17/jdk17-master/src/hotspot/os/linux/os_linux.cpp:720
#10 0x00007ffff7c94ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#11 0x00007ffff7d26850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

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

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

相关文章

如何用数据可视化提升你的决策力?

在数字化浪潮席卷全球的当下&#xff0c;数据已然成为企业和组织发展的核心资产。然而&#xff0c;单纯的数据堆积犹如未经雕琢的璞玉&#xff0c;难以直接为决策提供清晰有力的支持。数据可视化作为一种强大的工具&#xff0c;能够将海量、复杂的数据转化为直观、易懂的图形、…

VoiceFixer语音修复介绍与使用

一.简介 VoiceFixer 是一款基于深度学习的通用语音修复工具&#xff0c;主要用于恢复严重退化的语音信号&#xff0c;支持降噪、消除回声、提升音质等功能。 二.核心功能 1.语音修复与增强 VoiceFixer 采用端到端的神经网络模型&#xff0c;能够处理多种语音退化问题&#x…

Vue百日学习计划Day19-20天详细计划-Gemini版

重要提示&#xff1a; 番茄时钟&#xff1a; 每个番茄钟为25分钟学习&#xff0c;之后休息5分钟。每完成4个番茄钟&#xff0c;进行一次15-30分钟的长休息。动手实践&#xff1a; DevTools 的使用和 Git 命令的掌握都需要大量的实际操作。请务必边学边练。环境准备&#xff1a…

Qt初识.

认识 QLabel 类&#xff0c;能够在界面上显示字符串. 通过 setText 来设置的。参数 QString (Qt 中把 C 里的很多容器类&#xff0c;进行了重新封装。历史原因) 内存泄露 / 文件资源泄露对象树. Qt 中通过对象树&#xff0c;来统一的释放界面的控件对象. Qt 还是推荐使用 new 的…

WebGPU 图形计算

以下是关于 WebGPU 图形计算的基本知识点总结: 一、WebGPU 核心定位与优势 1. 与传统技术对比 维度WebGLWebGPU架构设计OpenGL ES 封装现代图形API抽象(Vulkan/Metal/D3D12)多线程支持单线程渲染多线程并行计算计算能力有限通用计算完整计算管线支持资源控制隐式状态管理显…

视觉基础模型

2.1 视觉的“大模型”时代&#xff1a;ViT的诞生与革新 在计算机视觉领域&#xff0c;卷积神经网络&#xff08;CNN&#xff09;曾是当之无愧的霸主。从LeNet到ResNet&#xff0c;CNN在图像分类、目标检测等任务上取得了巨大成功。然而&#xff0c;随着Transformer模型在自然语…

【React Native】快速入门

对于移动端应用来说&#xff0c;开发 Android 应用使用的语言有 java 和 kotlin&#xff0c;开发 ios 应用使用的语言有 obj-c 和 Swift 。因此&#xff0c;我们使用 react-native 编写一套代码进行跨端开发。 构建项目&#xff1a; npx create-expo-applatest安装 nativewin…

AR 开启昆虫学习新视界,解锁奇妙微观宇宙

在传统昆虫学习中&#xff0c;课堂教学是主要方式&#xff0c;老师通过板书、PPT 传授知识&#xff0c;但学生被动接受&#xff0c;书本静态图片无法展现昆虫真实比例、立体形态&#xff0c;学生难以直观感受复杂身体结构。博物馆的昆虫标本也是学习途径&#xff0c;不过标本放…

BI 大屏是什么意思?具体应用在哪些方面?

目录 一、BI 大屏的定义与内涵 1. 基本概念 2. 核心要素 3. 特点优势 二、如何搭建高效的 BI 大屏 1. 明确需求与目标 2. 选择合适的 BI大屏工具 3. 数据整合与清洗 4. 设计可视化界面 5. 持续优化与更新 三、BI 大屏在企业运营管理中的应用 1. 销售与营销领域 2.…

Kafka Go客户端--Sarama

Kafka Go客户端 在Go中里面有三个比较有名气的Go客户端。 Sarama:用户数量最多&#xff0c;早期这个项目是在Shopify下面&#xff0c;现在挪到了IBM下。segmentio/kafka-go:没啥大的缺点。confluent-kafka-go&#xff1a;需要启用cgo,跨平台问题比较多&#xff0c;交叉编译也…

Axure全链路交互设计:快速提升实现能力(基础交互+高级交互)

想让你的设计稿像真实App一样丝滑&#xff1f;本专栏带你玩转Axure交互&#xff0c;从选中高亮到动态面板骚操作&#xff0c;再到中继器表单花式交互&#xff0c;全程动图教学&#xff0c;一看就会&#xff01; 本专栏系统讲解多个核心交互效果&#xff0c;是你的Axure交互急救…

自动化测试脚本点击运行后,打开Chrome很久??

亲爱的小伙伴们大家好。 小编最近刚换了电脑&#xff0c;这几天做自动化测试发现打开Chrome浏览器需要等待好长时间&#xff0c;起初还以为代码有问题&#xff0c;或者Chromedriver与Chrome不匹配造成的&#xff0c;但排查后发现并不是&#xff01;&#xff01; 在driver.py中…

现代人工智能系统的实用设计模式

关键要点 AI设计模式是为现代AI驱动的软件中常见问题提供的可复用解决方案&#xff0c;帮助团队避免重复造轮子。我们将其分为五类&#xff1a;提示与上下文&#xff08;Prompting & Context&#xff09;、负责任的AI&#xff08;Responsible AI&#xff09;、用户体验&…

经典面试题:TCP 三次握手、四次挥手详解

在网络通信的复杂架构里&#xff0c;“三次握手”与“四次挥手”仿若一座无形的桥梁&#xff0c;它们是连接客户端与服务器的关键纽带。这座“桥梁”不仅确保了连接的稳固建立&#xff0c;还保障了连接的有序结束&#xff0c;使得网络世界中的信息能够顺畅、准确地流动。 在面…

食品饮料行业AI转型趋势分析与智能化解决方案探索​

一、行业洞察&#xff1a;AI驱动食品饮料行业价值重构​ 当前&#xff0c;食品饮料行业正面临消费分级显性化、需求多元化与技术范式革新的三重挑战。根据《2024食品饮料行业全营销白皮书》&#xff0c;高收入群体倾向于高端化、个性化产品&#xff0c;而下沉市场更关注性价比…

Electron使用WebAssembly实现CRC-8 ITU校验

Electron使用WebAssembly实现CRC-8 ITU校验 将C/C语言代码&#xff0c;经由WebAssembly编译为库函数&#xff0c;可以在JS语言环境进行调用。这里介绍在Electron工具环境使用WebAssembly调用CRC-8 ITU格式校验的方式。 CRC-8 ITU校验函数WebAssembly源文件 C语言实现CRC-8 I…

python如何遍历postgresql所有的用户表

要遍历PostgreSQL数据库中的所有用户表&#xff0c;可以按照以下步骤操作&#xff1a; 安装必要依赖库 pip install psycopg2-binary使用标准SQL查询方案&#xff08;推荐&#xff09; import psycopg2def list_user_tables():try:conn psycopg2.connect(host"your_ho…

面试相关的知识点

1 vllm 1.1常用概念 1 vllm&#xff1a;是一种大模型推理的框架&#xff0c;使用了张量并行原理&#xff0c;把大型矩阵分割成低秩矩阵&#xff0c;分散到不同的GPU上运行。 2 模型推理与训练&#xff1a;模型训练是指利用pytorch进行对大模型进行预训练。 模型推理是指用训…

node.js如何实现双 Token + Cookie 存储 + 无感刷新机制

node.js如何实现双 Token Cookie 存储 无感刷新机制 为什么要实施双token机制&#xff1f; 优点描述安全性Access Token 短期有效&#xff0c;降低泄露风险&#xff1b;Refresh Token 权限受限&#xff0c;仅用于获取新 Token用户体验用户无需频繁重新登录&#xff0c;Toke…

MySQL——6、内置函数

内置函数 1、日期函数2、字符串函数3、数学函数4、其他函数 1、日期函数 1.1、获取当前日期&#xff1a; 1.2、获取当前时间&#xff1a; 1.3、获取当前时间戳&#xff1a; 1.4、获取当前日期时间&#xff1a; 1.5、提取出日期&#xff1a; 1.6、给日期添加天数或时间…