From c001c70b57a7c062d86b35a756c3c397d4b37554 Mon Sep 17 00:00:00 2001 From: dingding <dingding5@huawei.com> Date: Thu, 27 Mar 2025 22:11:55 +0800 Subject: [PATCH] Add FileName for OpenPandaFileFromSecureMemory Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IBWV36 Signed-off-by: dingding <dingding5@huawei.com> --- static_core/libpandafile/file.cpp | 11 +++++++---- static_core/libpandafile/file.h | 2 +- .../ets/runtime/intrinsics/std_core_AbcFile.cpp | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/static_core/libpandafile/file.cpp b/static_core/libpandafile/file.cpp index bec50ed435..2bcf3ac7cc 100644 --- a/static_core/libpandafile/file.cpp +++ b/static_core/libpandafile/file.cpp @@ -306,7 +306,7 @@ std::unique_ptr<const File> OpenPandaFileFromMemory(const void *buffer, size_t s return panda_file::File::OpenFromMemory(std::move(ptr), std::to_string(hash(mem))); } -std::unique_ptr<const File> OpenPandaFileFromSecureMemory(uint8_t *buffer, size_t size) +std::unique_ptr<const File> OpenPandaFileFromSecureMemory(uint8_t *buffer, size_t size, std::string filename) { if (buffer == nullptr) { PLOG(ERROR, PANDAFILE) << "OpenPandaFileFromSecureMemory buffer is nullptr'"; @@ -314,14 +314,17 @@ std::unique_ptr<const File> OpenPandaFileFromSecureMemory(uint8_t *buffer, size_ } auto *mem = reinterpret_cast<std::byte *>(buffer); - os::mem::ConstBytePtr ptr(mem, size, nullptr); + os::mem::ConstBytePtr ptr(mem, size, [](std::byte *, size_t) noexcept {}); if (ptr.Get() == nullptr) { PLOG(ERROR, PANDAFILE) << "Failed to open panda file from secure memory'"; return nullptr; } std::hash<std::byte *> hash; - return panda_file::File::OpenFromMemory(std::move(ptr), std::to_string(hash(mem))); + if (filename.empty()) { // filename is sandbox path in application + filename = std::to_string(hash(mem)); + } + return panda_file::File::OpenFromMemory(std::move(ptr), filename); } class ClassIdxIterator { @@ -426,7 +429,7 @@ File::File(std::string filename, os::mem::ConstBytePtr &&base) : base_(std::forward<os::mem::ConstBytePtr>(base)), filename_(std::move(filename)), filenameHash_(CalcFilenameHash(filename_)), - fullFilename_(os::GetAbsolutePath(filename_)), + fullFilename_(os::GetAbsolutePath(filename_).empty() ? filename_ : os::GetAbsolutePath(filename_)), pandaCache_(std::make_unique<PandaCache>()), uniqId_(MergeHashes(filenameHash_, GetHash32(reinterpret_cast<const uint8_t *>(GetHeader()), sizeof(Header)))) { diff --git a/static_core/libpandafile/file.h b/static_core/libpandafile/file.h index fc7c65d9d4..fd1d4371ea 100644 --- a/static_core/libpandafile/file.h +++ b/static_core/libpandafile/file.h @@ -436,7 +436,7 @@ std::unique_ptr<const File> OpenPandaFileFromMemory(const void *buffer, size_t s /* * OpenPandaFileFromMemory from secure buffer. */ -std::unique_ptr<const File> OpenPandaFileFromSecureMemory(uint8_t *buffer, size_t size); +std::unique_ptr<const File> OpenPandaFileFromSecureMemory(uint8_t *buffer, size_t size, std::string filename = ""); /* * OpenZipPandaFile from the location zip. diff --git a/static_core/plugins/ets/runtime/intrinsics/std_core_AbcFile.cpp b/static_core/plugins/ets/runtime/intrinsics/std_core_AbcFile.cpp index 036d3dedb7..4b2038aec7 100644 --- a/static_core/plugins/ets/runtime/intrinsics/std_core_AbcFile.cpp +++ b/static_core/plugins/ets/runtime/intrinsics/std_core_AbcFile.cpp @@ -78,7 +78,7 @@ EtsAbcFile *EtsAbcFileLoadAbcFile(EtsRuntimeLinker *runtimeLinker, EtsString *fi return nullptr; } auto safeData = extractor->GetSafeData(pathStr); - pf = panda_file::OpenPandaFileFromSecureMemory(safeData->GetDataPtr(), safeData->GetDataLen()); + pf = panda_file::OpenPandaFileFromSecureMemory(safeData->GetDataPtr(), safeData->GetDataLen(), pathStr); } if (pf == nullptr) { -- Gitee