diff --git a/frameworks/native/runtime/js_worker.cpp b/frameworks/native/runtime/js_worker.cpp index 1b0a2b6f71..fbd4a43230 100644 --- a/frameworks/native/runtime/js_worker.cpp +++ b/frameworks/native/runtime/js_worker.cpp @@ -142,6 +142,16 @@ using FileMapper = AbilityBase::FileMapper; using FileMapperType = AbilityBase::FileMapperType; using IBundleMgr = AppExecFwk::IBundleMgr; +void ReleaseWorkerSafeMemFunc(void* mapper) +{ + TAG_LOGI(AAFwkTag::JSRUNTIME, "called"); + if (mapper) { + FileMapper* myMapper = static_cast(mapper); + myMapper->SetAutoReleaseMem(true); + delete myMapper; + } +} + std::string AssetHelper::NormalizedFileName(const std::string& fileName) const { std::string normalizedFilePath; @@ -169,7 +179,7 @@ AssetHelper::~AssetHelper() } void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::vector& content, - std::string& ami, bool& useSecureMem, bool isRestricted) + std::string& ami, bool& useSecureMem, void** mapper, bool isRestricted) { if (uri.empty() || buff == nullptr || buffSize == nullptr || workerInfo_ == nullptr) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Input params invalid"); @@ -215,10 +225,10 @@ void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buf TAG_LOGD(AAFwkTag::JSRUNTIME, "Get asset, ami: %{private}s", ami.c_str()); if (ami.find(CACHE_DIRECTORY) != std::string::npos) { - if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted)) { + if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted, mapper)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by ami failed"); } - } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted)) { + } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted, mapper)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by filepath failed"); } } else { @@ -258,16 +268,16 @@ void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buf ami = (workerInfo_->codePath).GetOriginString() + filePath; TAG_LOGD(AAFwkTag::JSRUNTIME, "Get asset, ami: %{private}s", ami.c_str()); if (ami.find(CACHE_DIRECTORY) != std::string::npos) { - if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted)) { + if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted, mapper)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by ami failed"); } - } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted)) { + } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted, mapper)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by filepath failed"); } } } -bool AssetHelper::GetSafeData(const std::string& ami, uint8_t** buff, size_t* buffSize) +bool AssetHelper::GetSafeData(const std::string& ami, uint8_t** buff, size_t* buffSize, void** mapper) { TAG_LOGD(AAFwkTag::JSRUNTIME, "called"); std::string resolvedPath; @@ -308,12 +318,13 @@ bool AssetHelper::GetSafeData(const std::string& ami, uint8_t** buff, size_t* bu *buff = fileMapper->GetDataPtr(); *buffSize = fileMapper->GetDataLen(); + *mapper = fileMapper.release(); fd_ = fd; return true; } bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, std::vector& content, - bool& useSecureMem, bool isRestricted) + bool& useSecureMem, bool isRestricted, void** mapper) { // Current function is a private, validity of workerInfo_ has been checked by caller. int32_t apiTargetVersion = static_cast(workerInfo_->apiTargetVersion.GetOriginPointer()); @@ -321,8 +332,8 @@ bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* bu if (GetIsStageModel() && !isRestricted && apiSatisfy) { if (apiTargetVersion >= API12) { useSecureMem = true; - return GetSafeData(ami, buff, buffSize); - } else if (GetSafeData(ami, buff, buffSize)) { + return GetSafeData(ami, buff, buffSize, mapper); + } else if (GetSafeData(ami, buff, buffSize, mapper)) { useSecureMem = true; return true; } else { @@ -361,7 +372,7 @@ bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* bu } bool AssetHelper::ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, - std::vector& content, bool& useSecureMem, bool isRestricted) + std::vector& content, bool& useSecureMem, bool isRestricted, void** mapper) { auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { @@ -434,11 +445,13 @@ bool AssetHelper::ReadFilePathData(const std::string& filePath, uint8_t** buff, } *buff = safeData->GetDataPtr(); *buffSize = safeData->GetDataLen(); + *mapper = safeData.release(); return true; } else if (safeData != nullptr) { useSecureMem = true; *buff = safeData->GetDataPtr(); *buffSize = safeData->GetDataLen(); + *mapper = safeData.release(); return true; } else { // If api version less than 12 and get secure mem failed, try get normal mem. diff --git a/frameworks/native/runtime/js_worker.h b/frameworks/native/runtime/js_worker.h index a9f17439d6..cf4a2113bb 100644 --- a/frameworks/native/runtime/js_worker.h +++ b/frameworks/native/runtime/js_worker.h @@ -26,6 +26,7 @@ namespace AbilityRuntime { void StartDebuggerInWorkerModule(bool isDebugApp, bool isNativeStart); void InitWorkerFunc(NativeEngine* nativeEngine); void OffWorkerFunc(NativeEngine* nativeEngine); +void ReleaseWorkerSafeMemFunc(void* mapper); int32_t GetContainerId(); void UpdateContainerScope(int32_t id); void RestoreContainerScope(int32_t id); @@ -44,20 +45,20 @@ public: virtual ~AssetHelper(); void operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::vector& content, - std::string& ami, bool& useSecureMem, bool isRestricted = false); + std::string& ami, bool& useSecureMem, void** mapper, bool isRestricted = false); private: std::string NormalizedFileName(const std::string& fileName) const; bool ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, std::vector& content, - bool& useSecureMem, bool isRestricted); + bool& useSecureMem, bool isRestricted, void** mapper); bool ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, std::vector& content, - bool& useSecureMem, bool isRestricted); + bool& useSecureMem, bool isRestricted, void** mapper); void GetAmi(std::string& ami, const std::string& filePath); - bool GetSafeData(const std::string& ami, uint8_t** buff, size_t* buffSize); + bool GetSafeData(const std::string& ami, uint8_t** buff, size_t* buffSize, void** mapper); bool GetIsStageModel(); diff --git a/frameworks/native/runtime/ohos_js_environment_impl.cpp b/frameworks/native/runtime/ohos_js_environment_impl.cpp index 027e359527..83469839d2 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.cpp +++ b/frameworks/native/runtime/ohos_js_environment_impl.cpp @@ -173,6 +173,7 @@ void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine* engine, std::shared_p CHECK_POINTER(workerInfo); engine->SetInitWorkerFunc(InitWorkerFunc); engine->SetOffWorkerFunc(OffWorkerFunc); + engine->SetReleaseWorkerSafeMemFunc(ReleaseWorkerSafeMemFunc); engine->SetGetAssetFunc(AssetHelper(workerInfo)); engine->SetApiVersion(static_cast(workerInfo->apiTargetVersion.GetOriginPointer())); diff --git a/test/fuzztest/abilityframeworksnativejsworker_fuzzer/abilityframeworksnativejsworker_fuzzer.cpp b/test/fuzztest/abilityframeworksnativejsworker_fuzzer/abilityframeworksnativejsworker_fuzzer.cpp index 5c6bc409fc..142d9d9144 100644 --- a/test/fuzztest/abilityframeworksnativejsworker_fuzzer/abilityframeworksnativejsworker_fuzzer.cpp +++ b/test/fuzztest/abilityframeworksnativejsworker_fuzzer/abilityframeworksnativejsworker_fuzzer.cpp @@ -18,6 +18,8 @@ #include #include +#include "extractor.h" +#include "file_mapper.h" #define private public #include "js_worker.h" #undef private @@ -73,13 +75,15 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) std::string jsonStr(data, size); uint8_t *buff = nullptr; size_t buffSize; - helper.GetSafeData(jsonStr, &buff, &buffSize); + std::unique_ptr fileMapper = std::make_unique(); + void* mapper = static_cast(fileMapper.get()); + helper.GetSafeData(jsonStr, &buff, &buffSize, &mapper); helper.NormalizedFileName(jsonStr); bool useSecureMem = *data % ENABLE; bool isRestricted = *data % ENABLE; std::vector content; - helper.ReadAmiData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted); - helper.ReadFilePathData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted); + helper.ReadAmiData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted, &mapper); + helper.ReadFilePathData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted, &mapper); helper.GetAmi(jsonStr, jsonStr); AbilityRuntime::GetContainerId(); bool isDebugApp = *data % ENABLE; diff --git a/test/unittest/runtime_test/js_worker_test.cpp b/test/unittest/runtime_test/js_worker_test.cpp index 787951acec..6a4e04642d 100644 --- a/test/unittest/runtime_test/js_worker_test.cpp +++ b/test/unittest/runtime_test/js_worker_test.cpp @@ -17,6 +17,8 @@ #include #include +#include "extractor.h" +#include "file_mapper.h" #include "js_environment_impl.h" #define private public #define protected public @@ -88,7 +90,9 @@ HWTEST_F(JsWorkerTest, AssetHelper_0100, TestSize.Level1) bool useSecureMem; bool isRestricted = false; auto func = TestGetGetAssetFunc(); - func("/data", &buff, &buffSize, content, ami, useSecureMem, isRestricted); + std::unique_ptr fileMapper = std::make_unique(); + void* mapper = static_cast(fileMapper.get()); + func("/data", &buff, &buffSize, content, ami, useSecureMem, &mapper, isRestricted); EXPECT_EQ(useSecureMem, false); } @@ -114,7 +118,9 @@ HWTEST_F(JsWorkerTest, AssetHelper_0200, TestSize.Level1) uint8_t *buff = nullptr; size_t buffSize; - auto ret = helper.GetSafeData("test.txt", &buff, &buffSize); + std::unique_ptr fileMapper = std::make_unique(); + void* mapper = static_cast(fileMapper.get()); + auto ret = helper.GetSafeData("test.txt", &buff, &buffSize, &mapper); EXPECT_EQ(ret, false); } } // namespace AbilityRuntime