From 7506c0d9890503cbda7291d377885576a52fee9e Mon Sep 17 00:00:00 2001 From: anyueling Date: Tue, 11 Apr 2023 16:21:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E5=8E=8B?= =?UTF-8?q?=E6=B5=8Bcrash=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- frameworks/js/napi/BUILD.gn | 164 +++++++----------- .../js/napi/include/download_base_notify.h | 11 +- frameworks/js/napi/include/uv_queue.h | 10 +- .../js/napi/src/download_base_notify.cpp | 101 +++-------- frameworks/js/napi/src/uv_queue.cpp | 25 ++- .../service/src/download_service_manager.cpp | 2 +- .../downloadbasenotify_fuzzer.cpp | 1 - 7 files changed, 124 insertions(+), 190 deletions(-) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 265e1ca8..806e29fc 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -26,62 +26,67 @@ config("upload_public_config") { ] } -ohos_shared_library("request") { - include_dirs = [ - "${arkui_path}/ace_engine/frameworks/base/utils", - "${ability_runtime_napi_path}/featureAbility", - "${ability_runtime_napi_path}/inner/napi_common", - "${request_path}/frameworks/native/download/include", - "${request_path}/frameworks/native/upload/include", - "//third_party/libuv/include", - ] +request_src = [ + "src/download/async_call.cpp", + "src/download_base_notify.cpp", + "src/download_event.cpp", + "src/download_pause.cpp", + "src/download_query.cpp", + "src/download_query_mimetype.cpp", + "src/download_remove.cpp", + "src/download_resume.cpp", + "src/download_task_napi.cpp", + "src/download_task_napi_V9.cpp", + "src/header_receive_callback.cpp", + "src/js_util.cpp", + "src/legacy/download_manager.cpp", + "src/legacy/download_task.cpp", + "src/napi_utils.cpp", + "src/notify_callback.cpp", + "src/progress_callback.cpp", + "src/request_module.cpp", + "src/upload/async_call.cpp", + "src/upload_task_napi.cpp", + "src/upload_task_napiV5.cpp", + "src/upload_task_napiV9.cpp", + "src/uv_queue.cpp", +] +request_include = [ + "${arkui_path}/ace_engine/frameworks/base/utils", + "${ability_runtime_napi_path}/featureAbility", + "${ability_runtime_napi_path}/inner/napi_common", + "${request_path}/frameworks/native/download/include", + "${request_path}/frameworks/native/upload/include", + "//third_party/libuv/include", +] + +request_deps = [ + "${request_path}/common:request_common_static", + "${request_path}/frameworks/native/download:downloadsingle", + "//third_party/libuv:uv", +] + +request_external_deps = [ + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:data_ability_helper", + "ability_runtime:napi_base_context", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "napi:ace_napi", +] + +ohos_shared_library("request") { + include_dirs = request_include configs = [ "//build/config/gcc:symbol_visibility_hidden" ] public_configs = [ ":upload_public_config" ] + sources = request_src - sources = [ - "src/download/async_call.cpp", - "src/download_base_notify.cpp", - "src/download_event.cpp", - "src/download_pause.cpp", - "src/download_query.cpp", - "src/download_query_mimetype.cpp", - "src/download_remove.cpp", - "src/download_resume.cpp", - "src/download_task_napi.cpp", - "src/download_task_napi_V9.cpp", - "src/header_receive_callback.cpp", - "src/js_util.cpp", - "src/legacy/download_manager.cpp", - "src/legacy/download_task.cpp", - "src/napi_utils.cpp", - "src/notify_callback.cpp", - "src/progress_callback.cpp", - "src/request_module.cpp", - "src/upload/async_call.cpp", - "src/upload_task_napi.cpp", - "src/upload_task_napiV5.cpp", - "src/upload_task_napiV9.cpp", - "src/uv_queue.cpp", - ] - - deps = [ - "${request_path}/common:request_common_static", - "${request_path}/frameworks/native/download:downloadsingle", - "${request_path}/frameworks/native/upload:upload_lib", - "//third_party/libuv:uv", - ] - - external_deps = [ - "ability_base:zuri", - "ability_runtime:abilitykit_native", - "ability_runtime:data_ability_helper", - "ability_runtime:napi_base_context", - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "napi:ace_napi", - ] + deps = request_deps + deps += [ "${request_path}/frameworks/native/upload:upload_lib" ] + external_deps = request_external_deps relative_install_dir = "module" subsystem_name = "request" @@ -89,60 +94,13 @@ ohos_shared_library("request") { } ohos_static_library("request_static") { - include_dirs = [ - "${arkui_path}/ace_engine/frameworks/base/utils", - "${ability_runtime_napi_path}/featureAbility", - "${ability_runtime_napi_path}/inner/napi_common", - "${request_path}/frameworks/native/download/include", - "${request_path}/frameworks/native/upload/include", - "//third_party/libuv/include", - "./inlcude", - ] - + include_dirs = request_include configs = [ "//build/config/gcc:symbol_visibility_hidden" ] public_configs = [ ":upload_public_config" ] + sources = request_src - sources = [ - "src/download/async_call.cpp", - "src/download_base_notify.cpp", - "src/download_event.cpp", - "src/download_pause.cpp", - "src/download_query.cpp", - "src/download_query_mimetype.cpp", - "src/download_remove.cpp", - "src/download_resume.cpp", - "src/download_task_napi.cpp", - "src/download_task_napi_V9.cpp", - "src/header_receive_callback.cpp", - "src/js_util.cpp", - "src/legacy/download_manager.cpp", - "src/legacy/download_task.cpp", - "src/napi_utils.cpp", - "src/notify_callback.cpp", - "src/progress_callback.cpp", - "src/request_module.cpp", - "src/upload/async_call.cpp", - "src/upload_task_napi.cpp", - "src/upload_task_napiV9.cpp", - ] - - deps = [ - "${request_path}/common:request_common_static", - "${request_path}/frameworks/native/download:downloadsingle", - "${request_path}/frameworks/native/upload:upload_lib", - "//third_party/libuv:uv", - ] - - external_deps = [ - "ability_base:zuri", - "ability_runtime:abilitykit_native", - "ability_runtime:data_ability_helper", - "ability_runtime:napi_base_context", - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "napi:ace_napi", - ] + deps = request_deps + external_deps = request_external_deps subsystem_name = "request" part_name = "request" diff --git a/frameworks/js/napi/include/download_base_notify.h b/frameworks/js/napi/include/download_base_notify.h index 5693bf96..593d200f 100644 --- a/frameworks/js/napi/include/download_base_notify.h +++ b/frameworks/js/napi/include/download_base_notify.h @@ -19,6 +19,8 @@ #include #include #include "visibility.h" +#include +#include "uv_queue.h" #include "download/async_call.h" #include "download_notify_stub.h" #include "download_task.h" @@ -33,11 +35,10 @@ struct NotifyData { uint32_t paramNumber; std::mutex mutex; std::vector params; -}; - -struct CallbackData { - napi_env env_; - napi_ref ref_; + ~NotifyData() + { + UvQueue::DeleteRef(env, ref); + } }; struct NotifyDataPtr { diff --git a/frameworks/js/napi/include/uv_queue.h b/frameworks/js/napi/include/uv_queue.h index 8fac7201..3757e5b2 100644 --- a/frameworks/js/napi/include/uv_queue.h +++ b/frameworks/js/napi/include/uv_queue.h @@ -19,10 +19,16 @@ #include "napi/native_api.h" #include "uv.h" -namespace OHOS::Request::UploadNapi { +namespace OHOS::Request { +struct CallbackData { + napi_env env; + napi_ref ref; +}; class UvQueue { public: static bool Call(napi_env env, void *data, uv_after_work_cb afterCallback); + static void DeleteRef(napi_env env, napi_ref ref); + static void UvDelete(uv_work_t *work, int status); }; -} // namespace OHOS::Request::UploadNapi +} // namespace OHOS::Request #endif // UV_QUEUE_H diff --git a/frameworks/js/napi/src/download_base_notify.cpp b/frameworks/js/napi/src/download_base_notify.cpp index bd2693d4..7450c747 100644 --- a/frameworks/js/napi/src/download_base_notify.cpp +++ b/frameworks/js/napi/src/download_base_notify.cpp @@ -14,10 +14,8 @@ */ #include "download_base_notify.h" - -#include - #include "log.h" +#include "uv_queue.h" #include "napi_utils.h" namespace OHOS::Request::Download { @@ -31,92 +29,43 @@ DownloadBaseNotify::DownloadBaseNotify(napi_env env, uint32_t paramNumber, napi_ DownloadBaseNotify::~DownloadBaseNotify() { - DOWNLOAD_HILOGI("~DownloadBaseNotify()"); - if (notifyData_ != nullptr) { - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(notifyData_->env, &loop); - if (loop == nullptr) { - DOWNLOAD_HILOGE("Failed to get uv event loop"); - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - DOWNLOAD_HILOGE("Failed to create uv work"); - return; - } - CallbackData *callbackData = new (std::nothrow) CallbackData; - if (callbackData == nullptr) { - delete work; - return; - } - callbackData->env_ = notifyData_->env; - callbackData->ref_ = notifyData_->ref; - work->data = reinterpret_cast(callbackData); - int ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int statusInt) { - CallbackData *callbackDataPtr = reinterpret_cast(work->data); - if (callbackDataPtr != nullptr) { - napi_delete_reference(callbackDataPtr->env_, callbackDataPtr->ref_); - delete callbackDataPtr; - delete work; - } - }); - if (ret != 0) { - delete callbackData; - delete work; - } - } } void DownloadBaseNotify::CallBack(const std::vector ¶ms) { DOWNLOAD_HILOGD("Pause callback in"); - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(notifyData_->env, &loop); - if (loop == nullptr) { - DOWNLOAD_HILOGE("Failed to get uv event loop"); - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - DOWNLOAD_HILOGE("Failed to create uv work"); - return; - } NotifyDataPtr *notifyDataPtr = GetNotifyDataPtr(); { std::lock_guard lock(notifyData_->mutex); notifyData_->params = params; } notifyDataPtr->notifyData = notifyData_; - work->data = notifyDataPtr; - uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int statusInt) { - NotifyDataPtr *notifyDataPtr = static_cast(work->data); - if (notifyDataPtr != nullptr) { - napi_handle_scope scope = nullptr; - napi_open_handle_scope(notifyDataPtr->notifyData->env, &scope); - napi_value undefined = 0; - napi_get_undefined(notifyDataPtr->notifyData->env, &undefined); - napi_value callbackFunc = nullptr; - napi_get_reference_value(notifyDataPtr->notifyData->env, notifyDataPtr->notifyData->ref, &callbackFunc); - napi_value callbackResult = nullptr; - napi_value callbackValues[Download::TWO_PARAMETER] = { 0 }; - { - std::lock_guard lock(notifyDataPtr->notifyData->mutex); - for (uint32_t i = 0; i < notifyDataPtr->notifyData->paramNumber; i++) { - napi_create_int64(notifyDataPtr->notifyData->env, notifyDataPtr->notifyData->params[i], - &callbackValues[i]); - } + uv_after_work_cb afterCallback = [](uv_work_t *work, int status) { + NotifyDataPtr *notifyDataPtr = static_cast(work->data); + if (notifyDataPtr != nullptr && notifyDataPtr->notifyData != nullptr) { + napi_handle_scope scope = nullptr; + napi_open_handle_scope(notifyDataPtr->notifyData->env, &scope); + napi_value undefined = 0; + napi_get_undefined(notifyDataPtr->notifyData->env, &undefined); + napi_value callbackFunc = nullptr; + napi_get_reference_value(notifyDataPtr->notifyData->env, notifyDataPtr->notifyData->ref, &callbackFunc); + napi_value callbackResult = nullptr; + napi_value callbackValues[Download::TWO_PARAMETER] = { 0 }; + { + std::lock_guard lock(notifyDataPtr->notifyData->mutex); + for (uint32_t i = 0; i < notifyDataPtr->notifyData->paramNumber; i++) { + napi_create_int64(notifyDataPtr->notifyData->env, notifyDataPtr->notifyData->params[i], + &callbackValues[i]); } - napi_call_function(notifyDataPtr->notifyData->env, nullptr, callbackFunc, - notifyDataPtr->notifyData->paramNumber, callbackValues, &callbackResult); - napi_close_handle_scope(notifyDataPtr->notifyData->env, scope); - delete notifyDataPtr; - delete work; } - }); + napi_call_function(notifyDataPtr->notifyData->env, nullptr, callbackFunc, + notifyDataPtr->notifyData->paramNumber, callbackValues, &callbackResult); + napi_close_handle_scope(notifyDataPtr->notifyData->env, scope); + delete notifyDataPtr; + delete work; + } + }; + UvQueue::Call(notifyData_->env, reinterpret_cast(notifyDataPtr), afterCallback); } NotifyDataPtr *DownloadBaseNotify::GetNotifyDataPtr() diff --git a/frameworks/js/napi/src/uv_queue.cpp b/frameworks/js/napi/src/uv_queue.cpp index a0f5f9c6..c54d0a7f 100644 --- a/frameworks/js/napi/src/uv_queue.cpp +++ b/frameworks/js/napi/src/uv_queue.cpp @@ -14,7 +14,7 @@ */ #include "uv_queue.h" -namespace OHOS::Request::UploadNapi { +namespace OHOS::Request { bool UvQueue::Call(napi_env env, void *data, uv_after_work_cb afterCallback) { uv_loop_s *loop = nullptr; @@ -31,4 +31,25 @@ bool UvQueue::Call(napi_env env, void *data, uv_after_work_cb afterCallback) loop, work, [](uv_work_t *work) {}, afterCallback); return true; } -} // namespace OHOS::Request::UploadNapi \ No newline at end of file + +void UvQueue::DeleteRef(napi_env env, napi_ref ref) +{ + CallbackData *callbackData = new (std::nothrow) CallbackData(); + if (callbackData == nullptr) { + return; + } + callbackData->env = env; + callbackData->ref = ref; + UvQueue::Call(env, reinterpret_cast(callbackData), UvDelete); +} + +void UvQueue::UvDelete(uv_work_t *work, int status) +{ + CallbackData *callbackData = reinterpret_cast(work->data); + if (callbackData != nullptr) { + napi_delete_reference(callbackData->env, callbackData->ref); + delete callbackData; + delete work; + } +} +} // namespace OHOS::Request \ No newline at end of file diff --git a/services/service/src/download_service_manager.cpp b/services/service/src/download_service_manager.cpp index 72bd796e..113af03b 100644 --- a/services/service/src/download_service_manager.cpp +++ b/services/service/src/download_service_manager.cpp @@ -612,6 +612,6 @@ bool DownloadServiceManager::IsSameBundleName(const std::string &sName, const st bool DownloadServiceManager::IsSameUid(int32_t sUid, int32_t dUid) { - return sUid = dUid; + return sUid == dUid; } } // namespace OHOS::Request::Download \ No newline at end of file diff --git a/test/fuzztest/downloadbasenotify_fuzzer/downloadbasenotify_fuzzer.cpp b/test/fuzztest/downloadbasenotify_fuzzer/downloadbasenotify_fuzzer.cpp index bb871277..48f42011 100644 --- a/test/fuzztest/downloadbasenotify_fuzzer/downloadbasenotify_fuzzer.cpp +++ b/test/fuzztest/downloadbasenotify_fuzzer/downloadbasenotify_fuzzer.cpp @@ -17,7 +17,6 @@ #include #include - #include "download_base_notify.h" #include "message_parcel.h"