js worker

Signed-off-by: zhuhan <zhuhan10@huawei.com>
Change-Id: If499ee563fade33888901ff75970aa3c0f86d674
This commit is contained in:
zhuhan
2025-03-14 11:36:05 +08:00
parent fb2ebcb5b5
commit d2ac17fe1f
5 changed files with 44 additions and 19 deletions
+23 -10
View File
@@ -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<FileMapper*>(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<uint8_t>& 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<uint8_t>& 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<int32_t>(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<uint8_t>& content, bool& useSecureMem, bool isRestricted)
std::vector<uint8_t>& content, bool& useSecureMem, bool isRestricted, void** mapper)
{
auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::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.
+5 -4
View File
@@ -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<uint8_t>& 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<uint8_t>& 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<uint8_t>& 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();
@@ -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<int32_t>(workerInfo->apiTargetVersion.GetOriginPointer()));
@@ -18,6 +18,8 @@
#include <cstddef>
#include <cstdint>
#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<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
void* mapper = static_cast<void*>(fileMapper.get());
helper.GetSafeData(jsonStr, &buff, &buffSize, &mapper);
helper.NormalizedFileName(jsonStr);
bool useSecureMem = *data % ENABLE;
bool isRestricted = *data % ENABLE;
std::vector<uint8_t> 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;
@@ -17,6 +17,8 @@
#include <cstdarg>
#include <string>
#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<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
void* mapper = static_cast<void*>(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<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
void* mapper = static_cast<void*>(fileMapper.get());
auto ret = helper.GetSafeData("test.txt", &buff, &buffSize, &mapper);
EXPECT_EQ(ret, false);
}
} // namespace AbilityRuntime