From a93fd5516f76cd666731742eed419f51bef0aad9 Mon Sep 17 00:00:00 2001 From: "chengjian.scj" Date: Thu, 6 Jul 2023 15:40:59 +0800 Subject: [PATCH] Fix bugs in signal processing functions --- patrons/src/main/cpp/patrons_core.c | 9 ++++---- patrons/src/main/cpp/patrons_core.h | 33 +++++++++++++++++++--------- patrons/src/main/cpp/xhook/xh_core.c | 26 +++++++++++++++++----- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/patrons/src/main/cpp/patrons_core.c b/patrons/src/main/cpp/patrons_core.c index 0d8708d..37338a2 100644 --- a/patrons/src/main/cpp/patrons_core.c +++ b/patrons/src/main/cpp/patrons_core.c @@ -250,7 +250,7 @@ Java_com_alibaba_android_patronus__1Patrons__1_1init(__unused JNIEnv *env, __unu LOGE("signal handler reg failed."); } else { has_exception_handle_ = true; - LOGI("signal handler reg success, old handler = %p", &sig_act_old[SIGSEGV]); + LOGI("signal handler reg success, old handler = %p", &sig_act_old); } int initCode; @@ -325,11 +325,12 @@ Java_com_alibaba_android_patronus__1Patrons_shrinkRegionSpace(__unused JNIEnv *e return false; } + bool ret = false; if (ClampGrowthLimit && region_space_) { if (has_exception_handle_ && !debuggable) { i_want_handle_signal_flag = 1; if (0 == sigsetjmp(time_machine, 1)) { - ResizeRegionSpace(new_size * MB); + ret = ResizeRegionSpace(new_size * MB); } else { LOGE("resize failed, found exception signal."); return false; @@ -337,7 +338,7 @@ Java_com_alibaba_android_patronus__1Patrons_shrinkRegionSpace(__unused JNIEnv *e i_want_handle_signal_flag = 0; } else { - return ResizeRegionSpace(new_size * MB); + ret = ResizeRegionSpace(new_size * MB); } } else { LOGE("resize failed, key param is NULL, instance = %p, method = %p.", @@ -347,7 +348,7 @@ Java_com_alibaba_android_patronus__1Patrons_shrinkRegionSpace(__unused JNIEnv *e return false; } - return true; + return ret; } JNIEXPORT jlong JNICALL diff --git a/patrons/src/main/cpp/patrons_core.h b/patrons/src/main/cpp/patrons_core.h index e3a3c71..e1f5751 100644 --- a/patrons/src/main/cpp/patrons_core.h +++ b/patrons/src/main/cpp/patrons_core.h @@ -164,7 +164,7 @@ bool has_exception_handle_ = false; /** * 异常处理 */ -static struct sigaction sig_act_old[16] = {0}; +static struct sigaction sig_act_old = {0}; static volatile int i_want_handle_signal_flag = 0; static sigjmp_buf time_machine; @@ -364,9 +364,9 @@ void InitEnv() { /** * 自定义异常处理函数 */ -static void CustomSignalHandler(int sig) { +static void CustomSignalHandler(int signum, siginfo_t* siginfo, void* context) { if (i_want_handle_signal_flag) { - __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "found exception signal %d", sig); + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "found exception signal %d", signum); // reset flag. i_want_handle_signal_flag = 0; @@ -374,8 +374,22 @@ static void CustomSignalHandler(int sig) { siglongjmp(time_machine, 1); } else { // use raw log method, LOGE not thread safe. - __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "found exception signal %d, but not my business.", sig); - sigaction(sig, &sig_act_old[sig], NULL); + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "found exception signal %d, but not my business.", signum); + + // sigaction(sig, &sig_act_old, NULL); + if (sig_act_old.sa_flags & SA_SIGINFO) { + sig_act_old.sa_sigaction(signum, siginfo, context); + } else { + if (SIG_DFL == sig_act_old.sa_handler) { + // If the previous handler was the default handler, cause a core dump. + signal(signum, SIG_DFL); + raise(signum); + } else if (SIG_IGN == sig_act_old.sa_handler) { + return; + } else { + sig_act_old.sa_handler(signum); + } + } } } @@ -383,18 +397,17 @@ static void CustomSignalHandler(int sig) { * 覆盖特定信号的处理函数 */ static int HandleSignal(int sig) { - struct sigaction act, handler; + struct sigaction act = {0}; if (0 != sigemptyset(&act.sa_mask)) return (0 == errno ? SIGNAL_HANDLER_REG_ERROR : errno); - act.sa_handler = CustomSignalHandler; + act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART; + act.sa_sigaction = CustomSignalHandler; - if (0 != sigaction(sig, &act, &handler)) + if (0 != sigaction(sig, &act, &sig_act_old)) return (0 == errno ? SIGNAL_HANDLER_REG_ERROR : errno); - sig_act_old[sig] = handler; - return 0; } diff --git a/patrons/src/main/cpp/xhook/xh_core.c b/patrons/src/main/cpp/xhook/xh_core.c index 30bc0bb..fc1b98b 100644 --- a/patrons/src/main/cpp/xhook/xh_core.c +++ b/patrons/src/main/cpp/xhook/xh_core.c @@ -89,23 +89,37 @@ static int xh_core_sigsegv_enable = 1; //enable by default static struct sigaction xh_core_sigsegv_act_old; static volatile int xh_core_sigsegv_flag = 0; static sigjmp_buf xh_core_sigsegv_env; -static void xh_core_sigsegv_handler(int sig) +static void xh_core_sigsegv_handler(int signum, siginfo_t* siginfo, void* context) { - (void)sig; - if(xh_core_sigsegv_flag) siglongjmp(xh_core_sigsegv_env, 1); else - sigaction(SIGSEGV, &xh_core_sigsegv_act_old, NULL); + { + // sigaction(SIGSEGV, &xh_core_sigsegv_act_old, NULL); + if (xh_core_sigsegv_act_old.sa_flags & SA_SIGINFO) { + xh_core_sigsegv_act_old.sa_sigaction(signum, siginfo, context); + } else { + if (SIG_DFL == xh_core_sigsegv_act_old.sa_handler) { + // If the previous handler was the default handler, cause a core dump. + signal(signum, SIG_DFL); + raise(signum); + } else if (SIG_IGN == xh_core_sigsegv_act_old.sa_handler) { + return; + } else { + xh_core_sigsegv_act_old.sa_handler(signum); + } + } + } } static int xh_core_add_sigsegv_handler() { - struct sigaction act; + struct sigaction act = {0}; if(!xh_core_sigsegv_enable) return 0; if(0 != sigemptyset(&act.sa_mask)) return (0 == errno ? XH_ERRNO_UNKNOWN : errno); - act.sa_handler = xh_core_sigsegv_handler; + act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART; + act.sa_sigaction = xh_core_sigsegv_handler; if(0 != sigaction(SIGSEGV, &act, &xh_core_sigsegv_act_old)) return (0 == errno ? XH_ERRNO_UNKNOWN : errno);