From f9180800dd5d3c636b43317452aac1ea73367e15 Mon Sep 17 00:00:00 2001 From: zhangyiwei Date: Mon, 12 Aug 2024 19:41:48 +0800 Subject: [PATCH] fix jit PageProtect error Issue: IAJIJN Signed-off-by: zhangyiwei Change-Id: I78fda141ea63b59930474fa36c7b83405d44753f --- ecmascript/jit/jit.cpp | 2 +- ecmascript/jit/jit_task.cpp | 7 ++++--- ecmascript/mem/heap-inl.h | 2 +- ecmascript/mem/machine_code.cpp | 12 ++++++------ ecmascript/mem/machine_code.h | 1 + ecmascript/mem/space.cpp | 10 +++++++--- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/ecmascript/jit/jit.cpp b/ecmascript/jit/jit.cpp index 5b88c91b5a..02769c2367 100644 --- a/ecmascript/jit/jit.cpp +++ b/ecmascript/jit/jit.cpp @@ -397,7 +397,7 @@ void Jit::Compile(EcmaVM *vm, JSHandle &jsFunction, CompilerTier tie if (vm->GetJSOptions().IsEnableJitFastCompile()) { maxSize = 15; // 15 is method codesize threshold during fast compiling } - if (codeSize > maxSize) { + if (codeSize > maxSize && !(vm->GetJSOptions().IsEnableForceJitCompileMain() && mode == SYNC)) { if (tier == CompilerTier::BASELINE) { LOG_BASELINEJIT(DEBUG) << "skip jit task, as too large:" << methodInfo; } else { diff --git a/ecmascript/jit/jit_task.cpp b/ecmascript/jit/jit_task.cpp index 5eb1c19c19..a2bf4e2743 100644 --- a/ecmascript/jit/jit_task.cpp +++ b/ecmascript/jit/jit_task.cpp @@ -168,7 +168,7 @@ static size_t ComputePayLoadSize(MachineCodeDesc &codeDesc) size_t allocSize = AlignUp(payLoadSize + MachineCode::SIZE, static_cast(MemAlignment::MEM_ALIGN_OBJECT)); codeDesc.instructionsSize = codeDesc.codeSizeAlign; - LOG_JIT(INFO) << "InstallCode:: MachineCode Object size to allocate: " + LOG_JIT(DEBUG) << "InstallCode:: MachineCode Object size to allocate: " << allocSize << " (instruction size): " << codeDesc.codeSizeAlign; if (allocSize > MAX_REGULAR_HEAP_OBJECT_SIZE) { return payLoadSize; @@ -190,7 +190,7 @@ static size_t ComputePayLoadSize(MachineCodeDesc &codeDesc) size_t payLoadSize = codeDesc.funcEntryDesSizeAlign + instructionsSize + codeDesc.stackMapSizeAlign; size_t allocSize = AlignUp(payLoadSize + MachineCode::SIZE, static_cast(MemAlignment::MEM_ALIGN_OBJECT)); - LOG_JIT(INFO) << "InstallCode:: MachineCode Object size to allocate: " + LOG_JIT(DEBUG) << "InstallCode:: MachineCode Object size to allocate: " << allocSize << " (instruction size): " << instructionsSize; codeDesc.instructionsSize = instructionsSize; @@ -262,12 +262,13 @@ void JitTask::InstallCode() size_t size = ComputePayLoadSize(codeDesc_); + codeDesc_.isAsyncCompileMode = IsAsyncTask(); JSHandle machineCodeObj; if (Jit::GetInstance()->IsEnableJitFort()) { // skip install if JitFort out of memory TaggedObject *machineCode = hostThread_->GetEcmaVM()->GetFactory()->NewMachineCodeObject(size, codeDesc_); if (machineCode == nullptr) { - LOG_JIT(INFO) << "InstallCode skipped. NewMachineCode NULL for size " << size; + LOG_JIT(DEBUG) << "InstallCode skipped. NewMachineCode NULL for size " << size; if (hostThread_->HasPendingException()) { hostThread_->SetMachineCodeLowMemory(true); hostThread_->ClearException(); diff --git a/ecmascript/mem/heap-inl.h b/ecmascript/mem/heap-inl.h index 1117faa23f..7957d2c400 100644 --- a/ecmascript/mem/heap-inl.h +++ b/ecmascript/mem/heap-inl.h @@ -477,7 +477,7 @@ TaggedObject *Heap::AllocateMachineCodeObject(JSHClass *hclass, size_t size, Mac // Jit Fort enabled ASSERT(GetEcmaVM()->GetJSOptions().GetEnableJitFort()); - if (!GetEcmaVM()->GetJSOptions().GetEnableAsyncCopyToFort()) { + if (!GetEcmaVM()->GetJSOptions().GetEnableAsyncCopyToFort() || !desc->isAsyncCompileMode) { desc->instructionsAddr = 0; if (size <= MAX_REGULAR_HEAP_OBJECT_SIZE) { // for non huge code cache obj, allocate fort space before allocating the code object diff --git a/ecmascript/mem/machine_code.cpp b/ecmascript/mem/machine_code.cpp index 1fb6165019..4ed2aea405 100644 --- a/ecmascript/mem/machine_code.cpp +++ b/ecmascript/mem/machine_code.cpp @@ -32,11 +32,10 @@ using namespace OHOS::Security::CodeSign; static bool SetPageProtect(uint8_t *textStart, size_t dataSize) { if (!Jit::GetInstance()->IsEnableJitFort()) { - size_t pageSize = 4096; - uintptr_t startPage = reinterpret_cast(textStart) & ~(pageSize - 1); - uintptr_t endPage = (reinterpret_cast(textStart) + dataSize) & ~(pageSize - 1); - size_t protSize = (endPage == startPage) ? ((dataSize + pageSize - 1U) & (~(pageSize - 1))) : - (pageSize + ((dataSize + pageSize - 1U) & (~(pageSize - 1)))); + constexpr size_t pageSize = 4096; + uintptr_t startPage = AlignDown(reinterpret_cast(textStart), pageSize); + uintptr_t endPage = AlignUp(reinterpret_cast(textStart) + dataSize, pageSize); + size_t protSize = endPage - startPage; return PageProtect(reinterpret_cast(startPage), protSize, PAGE_PROT_EXEC_READWRITE); } return true; @@ -54,7 +53,8 @@ bool MachineCode::SetText(const MachineCodeDesc &desc) } pText += desc.rodataSizeBeforeTextAlign; } - if (!Jit::GetInstance()->IsEnableJitFort() || !Jit::GetInstance()->IsEnableAsyncCopyToFort()) { + if (!Jit::GetInstance()->IsEnableJitFort() || !Jit::GetInstance()->IsEnableAsyncCopyToFort() || + !desc.isAsyncCompileMode) { #ifdef CODE_SIGN_ENABLE if ((uintptr_t)desc.codeSigner == 0) { if (memcpy_s(pText, desc.codeSizeAlign, reinterpret_cast(desc.codeAddr), desc.codeSize) != EOK) { diff --git a/ecmascript/mem/machine_code.h b/ecmascript/mem/machine_code.h index 382a7cd7f4..e182403090 100644 --- a/ecmascript/mem/machine_code.h +++ b/ecmascript/mem/machine_code.h @@ -59,6 +59,7 @@ struct MachineCodeDesc { size_t funcEntryDesSizeAlign {0}; size_t stackMapSizeAlign {0}; MemDesc *memDesc {nullptr}; + bool isAsyncCompileMode {false}; }; class MachineCode; diff --git a/ecmascript/mem/space.cpp b/ecmascript/mem/space.cpp index 1f69e7d3dd..450176e70f 100644 --- a/ecmascript/mem/space.cpp +++ b/ecmascript/mem/space.cpp @@ -178,9 +178,13 @@ uintptr_t HugeMachineCodeSpace::Allocate(size_t objectSize, JSThread *thread, vo if (allocType == AllocateEventType::NORMAL) { thread->CheckSafepointIfSuspended(); } - Region *region = reinterpret_cast(heap_)->GetEcmaVM()->GetJSOptions().GetEnableAsyncCopyToFort() ? - reinterpret_cast(reinterpret_cast(pDesc)->hugeObjRegion) : - AllocateFort(objectSize, thread, pDesc); + Region *region; + if (reinterpret_cast(heap_)->GetEcmaVM()->GetJSOptions().GetEnableAsyncCopyToFort() && + reinterpret_cast(pDesc)->isAsyncCompileMode) { + region = reinterpret_cast(reinterpret_cast(pDesc)->hugeObjRegion); + } else { + region = AllocateFort(objectSize, thread, pDesc); + } AddRegion(region); // It need to mark unpoison when huge object being allocated. ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast(region->GetBegin()), objectSize); -- Gitee