From 95b6b1ac863b33f5681352437fd7e82d7dcae710 Mon Sep 17 00:00:00 2001 From: wangyue Date: Mon, 22 Apr 2024 12:32:40 +0400 Subject: [PATCH] enable cppcrash by sigaction Signed-off-by: wangyue Change-Id: Ie7c44a378096fc059787c6e210f049bd2a0cc2d5 --- ecmascript/dfx/stackinfo/js_stackinfo.cpp | 3 -- ecmascript/ecma_vm.cpp | 46 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ecmascript/dfx/stackinfo/js_stackinfo.cpp b/ecmascript/dfx/stackinfo/js_stackinfo.cpp index 8322e7b5c6..1ac53a1a84 100644 --- a/ecmascript/dfx/stackinfo/js_stackinfo.cpp +++ b/ecmascript/dfx/stackinfo/js_stackinfo.cpp @@ -285,15 +285,12 @@ void CrashCallback(char *buf __attribute__((unused)), size_t len __attribute__(( // 3. do not do much things inside callback, stack size is limited // 4. do not use normal log if (JsStackInfo::loader == nullptr) { - JsStackInfo::BuildCrashInfo(false); return; } if (!JsStackInfo::loader->InsideStub(pc) && !JsStackInfo::loader->InsideAOT(pc)) { - JsStackInfo::BuildCrashInfo(false); return; } LOG_ECMA(ERROR) << std::hex << "CrashCallback pc:" << pc << " fp:" << fp; - JsStackInfo::BuildCrashInfo(false, pc); FrameIterator frame(reinterpret_cast(fp)); bool isBuiltinStub = (frame.GetFrameType() == FrameType::OPTIMIZED_FRAME); Method *method = frame.CheckAndGetMethod(); diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index 30ab9d2825..dd9c8df3e2 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -108,6 +108,42 @@ using RandomGenerator = base::RandomGenerator; using PGOProfilerManager = pgo::PGOProfilerManager; AOTFileManager *JsStackInfo::loader = nullptr; JSRuntimeOptions *JsStackInfo::options = nullptr; +#ifdef JIT_ESCAPE_ENABLE +static struct sigaction s_oldSa[SIGSYS + 1]; // SIGSYS = 31 + +void GetSignalHandler(int signal, siginfo_t *info, void *context) +{ + [[maybe_unused]] ucontext_t *ucontext = reinterpret_cast(context); + [[maybe_unused]] mcontext_t &mcontext = ucontext->uc_mcontext; + uintptr_t pc = 0; +#if defined(PANDA_TARGET_AMD64) + pc = static_cast(mcontext.gregs[REG_RIP]); +#elif defined(PANDA_TARGET_ARM64) + pc = static_cast(mcontext.pc); +#endif + if (JsStackInfo::loader == nullptr) { + ecmascript::JsStackInfo::BuildCrashInfo(false); + } else if (!JsStackInfo::loader->InsideStub(pc) && !JsStackInfo::loader->InsideAOT(pc)) { + ecmascript::JsStackInfo::BuildCrashInfo(false); + } else { + ecmascript::JsStackInfo::BuildCrashInfo(false, pc); + } + sigaction(signal, &s_oldSa[signal], nullptr); + int rc = syscall(SYS_rt_tgsigqueueinfo, getpid(), syscall(SYS_gettid), info->si_signo, info); + if (rc != 0) { + LOG_ECMA(ERROR) << "GetSignalHandler() failed to resend signal during crash"; + } +} + +void SignalReg(int signo) +{ + sigaction(signo, nullptr, &s_oldSa[signo]); + struct sigaction newAction; + newAction.sa_flags = SA_RESTART | SA_SIGINFO; + newAction.sa_sigaction = GetSignalHandler; + sigaction(signo, &newAction, nullptr); +} +#endif EcmaVM *EcmaVM::Create(const JSRuntimeOptions &options) { @@ -171,6 +207,16 @@ void EcmaVM::PostFork() heap_->SetHeapMode(HeapMode::SHARE); GetAssociatedJSThread()->PostFork(); Taskpool::GetCurrentTaskpool()->Initialize(); +#ifdef JIT_ESCAPE_ENABLE + SignalReg(SIGABRT); + SignalReg(SIGBUS); + SignalReg(SIGSEGV); + SignalReg(SIGILL); + SignalReg(SIGKILL); + SignalReg(SIGSTKFLT); + SignalReg(SIGFPE); + SignalReg(SIGTRAP); +#endif SharedHeap::GetInstance()->EnableParallelGC(GetJSOptions()); heap_->EnableParallelGC(); std::string bundleName = PGOProfilerManager::GetInstance()->GetBundleName();