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.pswp.cn/news/906525.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/906525.shtml
英文地址,請注明出處:http://en.pswp.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、給日期添加天數或時間…