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
繁體地址,請注明出處:http://hk.pswp.cn/diannao/83675.shtml
英文地址,請注明出處:http://en.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…