1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef ECMASCRIPT_PLATFORM_MAP_H 17#define ECMASCRIPT_PLATFORM_MAP_H 18 19#include <cstddef> 20 21#include "ecmascript/common.h" 22#include "ecmascript/mem/c_string.h" 23 24namespace panda::ecmascript { 25class MemMap { 26public: 27 MemMap() : originAddr_(nullptr), mem_(nullptr), size_(0) {} 28 MemMap(void *mem, size_t size) : originAddr_(mem), mem_(mem), size_(size) {}; 29 MemMap(void *originAddr, void *mem, size_t size) : originAddr_(originAddr), mem_(mem), size_(size) {}; 30 ~MemMap() = default; 31 32 void Reset() 33 { 34 originAddr_ = nullptr; 35 mem_ = nullptr; 36 size_ = 0; 37 } 38 39 inline void *GetMem() const 40 { 41 return mem_; 42 } 43 44 inline size_t GetSize() const 45 { 46 return size_; 47 } 48 49 inline void *GetOriginAddr() const 50 { 51 return originAddr_; 52 } 53private: 54 void *originAddr_ {nullptr}; 55 void *mem_ {nullptr}; 56 size_t size_ {0}; 57}; 58 59enum class PageTagType : uint8_t { 60 HEAP, 61 MACHINE_CODE, 62 MEMPOOL_CACHE, 63}; 64 65#ifdef PANDA_TARGET_WINDOWS 66#define PAGE_PROT_NONE 0x01 67#define PAGE_PROT_READ 0x02 68#define PAGE_PROT_READWRITE 0x04 69#define PAGE_PROT_EXEC_READ 0x20 70// For safety reason, Disallow prot have both write & exec capability except in JIT. 71#define PAGE_PROT_EXEC_READWRITE 0x40 72#else 73#define PAGE_PROT_NONE 0 74#define PAGE_PROT_READ 1 75#define PAGE_PROT_READWRITE 3 76#define PAGE_PROT_EXEC_READ 5 77// For safety reason, Disallow prot have both write & exec capability except in JIT. 78#define PAGE_PROT_EXEC_READWRITE 7 79#endif 80 81// Jit Fort space protection control 82inline int PageProtectProt([[maybe_unused]] bool disable_codesign) 83{ 84#if defined(JIT_ENABLE_CODE_SIGN) && !defined(JIT_FORT_DISABLE) 85 if (!disable_codesign) { 86 return PAGE_PROT_EXEC_READ; 87 } 88#endif 89 return PAGE_PROT_EXEC_READWRITE; 90} 91 92static constexpr char HEAP_TAG[] = "ArkTS Heap"; 93static constexpr char CODE_TAG[] = "ArkTS Code"; 94MemMap PUBLIC_API PageMap(size_t size, int prot = PAGE_PROT_NONE, size_t alignment = 0, void *addr = nullptr, 95 int flags = 0); 96void PUBLIC_API PageUnmap(MemMap it); 97MemMap PUBLIC_API MachineCodePageMap(size_t size, int prot = PAGE_PROT_NONE, size_t alignment = 0); 98void PUBLIC_API MachineCodePageUnmap(MemMap it); 99void PageRelease(void *mem, size_t size); 100void PUBLIC_API PagePreRead(void *mem, size_t size); 101void PageTag(void *mem, size_t size, PageTagType type, const std::string &spaceName = "", 102 const uint32_t threadId = 0); 103void PageClearTag(void *mem, size_t size); 104const std::string GetPageTagString(PageTagType type, const std::string &spaceName, const uint32_t threadId = 0); 105const char *GetPageTagString(PageTagType type); 106bool PageProtect(void *mem, size_t size, int prot); 107size_t PUBLIC_API PageSize(); 108} // namespace panda::ecmascript 109#endif // ECMASCRIPT_PLATFORM_MAP_H 110