动静混合抛js异常报cppCrash

issue:https://gitcode.com/openharmony/ability_ability_runtime/issues/14994?ref=&did=3857921#tid-3857921

Co-Authored-By: Agent

Signed-off-by: rentangyu <rentangyu@h-partners.com>
This commit is contained in:
rentangyu
2026-04-17 17:03:58 +08:00
parent afb2c454c2
commit 2000be0fff
7 changed files with 36 additions and 19 deletions
+11 -3
View File
@@ -2178,7 +2178,15 @@ void MainThread::InitUncatchableTask(JsEnv::UncatchableTask &uncatchableTask, co
ProcessExit(info);
ErrorObject appExecErrorObj = { errorObject.name, errorObject.message, errorObject.stack};
auto mainEnv = (static_cast<AbilityRuntime::JsRuntime&>(*appThread->application_->GetRuntime())).GetNapiEnv();
napi_env mainEnv = nullptr;
auto &runtime = appThread->application_->GetRuntime();
if (runtime->GetLanguage() == AbilityRuntime::Runtime::Language::ETS) {
auto& etsRuntime = static_cast<AbilityRuntime::ETSRuntime&>(*runtime);
auto& jsRuntime = static_cast<AbilityRuntime::JsRuntime&>(*etsRuntime.GetJsRuntime());
mainEnv = jsRuntime.GetNapiEnv();
} else {
mainEnv = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNapiEnv();
}
ApplicationDataManager::ExceptionParams params = {env, mainEnv, exception, summary, isUncatchable};
if (ApplicationDataManager::NotifyUncaughtException(params, appExecErrorObj)) {
return;
@@ -4460,11 +4468,11 @@ void MainThread::RegisterHybridException(const std::unique_ptr<AbilityRuntime::R
InitUncatchableTask(uncaughtExceptionInfo.uncaughtTask, uncatchableTaskInfo);
(static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).RegisterUncaughtExceptionHandler(
uncaughtExceptionInfo);
uncaughtExceptionInfo, true);
JsEnv::UncatchableTask uncatchableTask;
InitUncatchableTask(uncatchableTask, uncatchableTaskInfo, true);
(static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).RegisterUncatchableExceptionHandler(
uncatchableTask);
uncatchableTask, true);
}
}
}
+5 -4
View File
@@ -1585,18 +1585,19 @@ void JsRuntime::UpdateModuleNameAndAssetPath(const std::string& moduleName)
panda::JSNApi::SetModuleName(vm, moduleName_);
}
void JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo)
void JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo,
bool isStatic)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
CHECK_POINTER(jsEnv_);
jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo, isStatic);
}
void JsRuntime::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask)
void JsRuntime::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
CHECK_POINTER(jsEnv_);
jsEnv_->RegisterUncatchableExceptionHandler(uncatchableTask);
jsEnv_->RegisterUncatchableExceptionHandler(uncatchableTask, isStatic);
}
void JsRuntime::RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath)
@@ -127,8 +127,9 @@ public:
bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
bool UnLoadRepairPatch(const std::string& hqfFile) override;
bool NotifyHotReloadPage() override;
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask);
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo,
bool isStatic = false);
void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic = false);
bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle,
const std::string& srcEntrance = "");
@@ -153,7 +153,8 @@ void JsEnvironment::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator
engine_->RegisterSourceMapTranslateCallback(translateUrlBySourceMapFunc);
}
void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo)
void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo,
bool isStatic)
{
if (engine_ == nullptr) {
TAG_LOGE(AAFwkTag::JSENV, "Invalid Native Engine");
@@ -161,10 +162,10 @@ void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExcept
}
engine_->RegisterNapiUncaughtExceptionHandler(NapiUncaughtExceptionCallback(uncaughtExceptionInfo.uncaughtTask,
sourceMapOperator_, reinterpret_cast<napi_env>(engine_)));
sourceMapOperator_, reinterpret_cast<napi_env>(engine_), isStatic));
}
void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask)
void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic)
{
if (uncatchableTask == nullptr) {
return;
@@ -172,12 +173,12 @@ void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::Uncatchable
std::weak_ptr<JsEnvironment> weakThis = shared_from_this();
panda::JSNApi::RegisterUncatchableErrorHandler(const_cast<EcmaVM *>(engine_->GetEcmaVm()),
[weakThis, uncatchableTask] (auto& trycatch) {
[weakThis, uncatchableTask, isStatic] (auto& trycatch) {
auto sharedThis = weakThis.lock();
if (sharedThis) {
void* env = trycatch.GetEnv();
NapiUncaughtExceptionCallback napiUncaughtExceptionCallback(uncatchableTask,
sharedThis->sourceMapOperator_, reinterpret_cast<napi_env>(env));
sharedThis->sourceMapOperator_, reinterpret_cast<napi_env>(env), isStatic);
napiUncaughtExceptionCallback(trycatch);
} else {
TAG_LOGE(AAFwkTag::JSENV, "JsEnvironment has been destructed.");
@@ -85,7 +85,10 @@ void NapiUncaughtExceptionCallback::operator()(panda::TryCatch& trycatch)
void NapiUncaughtExceptionCallback::CallbackTask(napi_value& obj)
{
HandleAndLogIfNotJsError(obj);
// Static objects cannot be dynamically serialized.
if (!isStatic_) {
HandleAndLogIfNotJsError(obj);
}
std::string errorMsg = GetNativeStrFromJsTaggedObj(obj, "message");
AppendExtraInfo(errorMsg);
std::string errorName = GetNativeStrFromJsTaggedObj(obj, "name");
@@ -74,9 +74,11 @@ public:
void RemoveTask(const std::string& name);
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo,
bool isStatic = false);
void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask);
void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask,
bool isStatic = false);
bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
@@ -42,8 +42,8 @@ public:
NapiUncaughtExceptionCallback(
std::function<void(const std::string summary, const JsEnv::ErrorObject errorObj, napi_env env,
napi_value exception)> uncaughtTask,
std::shared_ptr<SourceMapOperator> sourceMapOperator, napi_env env)
: uncaughtTask_(uncaughtTask), sourceMapOperator_(sourceMapOperator), env_(env)
std::shared_ptr<SourceMapOperator> sourceMapOperator, napi_env env, bool isStatic = false)
: uncaughtTask_(uncaughtTask), sourceMapOperator_(sourceMapOperator), env_(env), isStatic_(isStatic)
{}
~NapiUncaughtExceptionCallback() = default;
@@ -71,6 +71,7 @@ private:
napi_value exception)> uncaughtTask_;
std::shared_ptr<SourceMapOperator> sourceMapOperator_ = nullptr;
napi_env env_ = nullptr;
bool isStatic_;
void AppendStackTrace(const std::string& errorStack, std::string& summary);