当前位置:网站首页>Meituan side: why does thread crash not cause JVM crash
Meituan side: why does thread crash not cause JVM crash
2022-07-03 16:46:00 【InfoQ】

- Thread crash , Is the process bound to crash
- How the process crashed - Introduction to signaling mechanism
- Why is it JVM Thread crash in does not cause JVM Process breakdown
- openJDK The source code parsing
Thread crash , Is the process bound to crash ?

- Write data to read-only memory
- #include <stdio.h> #include <stdlib.h> int main() { char *s = "hello world";// Write data to read-only memory , collapse s[1] = 'H'; }
- Accessed the address space that the process does not have permission to access ( Such as kernel space )
- #include <stdio.h> #include <stdlib.h> int main() { int *p = (int *)0xC0000fff; // Write data to the kernel space of the process , collapse *p = 10; }
- stay 32 Bit virtual address space ,p It points to kernel space , Obviously do not have write permission , Therefore, the above assignment operation will cause a crash
- Accessed nonexistent memory , such as
- #include <stdio.h> #include <stdlib.h> int main() { int *a = NULL; *a = 1; }
How the process crashed - Introduction to signaling mechanism

- CPU Execute normal process instructions
- call kill System calls send signals to processes
- The process receives a signal from the operating system ,CPU Pause the current program , And transfer control to the operating system
- call kill System calls send signals to processes ( Assuming that 11, namely SIGSEGV, This error is usually reported for illegal access to memory )
- The operating system executes the corresponding signal processing program according to the situation ( function ), Generally, the process will exit after the signal handler logic is executed
// Examples of custom signal processing functions
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
// Custom signal processing functions , Call... After processing the custom logic exit sign out
void sigHandler(int sig) {
printf("Signal %d catched!\n", sig);
exit(sig);
}
int main(void) {
signal(SIGSEGV, sigHandler);
int *p = (int *)0xC0000fff;
*p = 10; // Write data to kernel space that does not belong to a process , collapse
}
// The above results output : Signal 11 catched!#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
int main(void) {
// Ignore the signal
signal(SIGSEGV, SIG_IGN);
// Produce a SIGSEGV The signal
raise(SIGSEGV);
printf(" Normal end ");
}Why thread crashes don't cause JVM Process breakdown


openJDK The source code parsing

JVM_handle_linux_signal(int sig,
siginfo_t* info,
void* ucVoid,
int abort_if_unrecognized) {
// Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
// This code calls siglongjmp, Mainly used for thread recovery
os::ThreadCrashProtection::check_crash_protection(sig, t);
if (info != NULL && uc != NULL && thread != NULL) {
pc = (address) os::Linux::ucontext_get_pc(uc);
// Handle ALL stack overflow variations here
if (sig == SIGSEGV) {
// Si_addr may not be valid due to a bug in the linux-ppc64 kernel (see
// comment below). Use get_stack_bang_address instead of si_addr.
address addr = ((NativeInstruction*)pc)->get_stack_bang_address(uc);
// Determine whether the stack overflows
if (addr < thread->stack_base() &&
addr >= thread->stack_base() - thread->stack_size()) {
if (thread->thread_state() == _thread_in_Java) {
// For stack overflow JVM Internal treatment of
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
}
}
}
}
if (sig == SIGSEGV &&
!MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
// Null pointer check will be performed here
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
}
// If it is a stack overflow or a null pointer, it will eventually return true, Will not take the last report_and_die, therefore JVM Will not quit
if (stub != NULL) {
// save all thread context in case we need to restore it
if (thread != NULL) thread->set_saved_exception_pc(pc);
uc->uc_mcontext.gregs[REG_PC] = (greg_t)stub;
// return true representative JVM The process does not exit
return true;
}
VMError err(t, sig, pc, info, ucVoid);
// Generate hs_err_pid_xxx.log File and exit
err.report_and_die();
ShouldNotReachHere();
return true; // Mute compiler
}- happen stackoverflow And null pointer errors , They were all sent SIGSEGV, Only the virtual machine does not choose to exit , It's an extra internal process , In fact, it is the process that restores the thread , And throw StackoverflowError and NPE, That's why JVM Will not crash and we can catch these two errors / The cause of the abnormality
- If it is aimed at SIGSEGV Equal signal , In the above function JVM No extra processing , Then we will finally come to report_and_die This method , The main thing this method does is generate hs_err_pid_xxx.log crash file ( Some stack information or errors are logged ), And then quit
summary
边栏推荐
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- The way of wisdom (unity of knowledge and action)
- Unity project optimization case 1
- 斑馬識別成狗,AI犯錯的原因被斯坦福找到了
- PHP production website active push (website)
- [solved] access denied for user 'root' @ 'localhost' (using password: yes)
- [combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
- PHP converts a one-dimensional array into a two-dimensional array
- What is the material of sa302grc? American standard container plate sa302grc chemical composition
- What is the maximum number of concurrent TCP connections for a server? 65535?
猜你喜欢

Daily code 300 lines learning notes day 10

What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material

(补)双指针专题

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

Visual SLAM algorithms: a survey from 2010 to 2016

消息队列消息丢失和消息重复发送的处理策略

What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties

Mysql database DDL and DML

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

A survey of state of the art on visual slam
随机推荐
Daily code 300 lines learning notes day 10
爱可可AI前沿推介(7.3)
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
MySQL user management
CC2530 common registers for serial communication
Mongodb installation and basic operation
Shentong express expects an annual loss of nearly 1billion
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
数据分析必备的能力
线程池执行定时任务
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
Golang 装饰器模式以及在NSQ中的使用
One article takes you to understand machine learning
网络安全web渗透技术
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
A survey of state of the art on visual slam
PHP production website active push (website)
ThreeJS 第二篇:顶点概念、几何体结构
MySQL converts comma separated attribute field data from column to row
美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃