当前位置:网站首页>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
边栏推荐
- Informatics Olympiad all in one YBT 1175: divide by 13 | openjudge noi 1.13 27: divide by 13
- 手机注册股票开户安全吗 开户需要钱吗
- 香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
- Arduino esp32: overall framework of lvgl project (I)
- Unreal_ Datatable implements ID self increment and sets rowname
- NSQ source code installation and operation process
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- Top k questions of interview
- Golang decorator mode and its use in NSQ
- Necessary ability of data analysis
猜你喜欢
What is the maximum number of concurrent TCP connections for a server? 65535?
Web crawler knowledge day03
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
MySQL converts comma separated attribute field data from column to row
2022爱分析· 国央企数字化厂商全景报告
Recommendation of good books on learning QT programming
斑马识别成狗,AI犯错的原因被斯坦福找到了
【LeetCode】94. Middle order traversal of binary tree
NSQ source code installation and operation process
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
随机推荐
Characteristic polynomial and constant coefficient homogeneous linear recurrence
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
The way of wisdom (unity of knowledge and action)
QT串口ui设计和解决显示中文乱码
CC2530 common registers for watchdog
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
Golang 匿名函数使用
Mongodb installation and basic operation
CC2530 common registers for port interrupts
Record windows10 installation tensorflow-gpu2.4.0
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
14 topics for performance interviews between superiors and subordinates (4)
网络安全web渗透技术
Netease UI automation test exploration: airtest+poco
【剑指 Offer 】57 - II. 和为s的连续正数序列
利用MySQL中的乐观锁和悲观锁实现分布式锁
JSON 与 BSON 区别
Cocos Creator 2.x 自动打包(构建 + 编译)
(补)双指针专题
Unity project optimization case 1