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
24 namespace panda::ecmascript {
25 class MemMap {
26 public:
MemMap()27 MemMap() : originAddr_(nullptr), mem_(nullptr), size_(0) {}
MemMap(void * mem,size_t size)28 MemMap(void *mem, size_t size) : originAddr_(mem), mem_(mem), size_(size) {};
MemMap(void * originAddr,void * mem,size_t size)29 MemMap(void *originAddr, void *mem, size_t size) : originAddr_(originAddr), mem_(mem), size_(size) {};
30 ~MemMap() = default;
31
Reset()32 void Reset()
33 {
34 originAddr_ = nullptr;
35 mem_ = nullptr;
36 size_ = 0;
37 }
38
GetMem()39 inline void *GetMem() const
40 {
41 return mem_;
42 }
43
GetSize()44 inline size_t GetSize() const
45 {
46 return size_;
47 }
48
GetOriginAddr()49 inline void *GetOriginAddr() const
50 {
51 return originAddr_;
52 }
53 private:
54 void *originAddr_ {nullptr};
55 void *mem_ {nullptr};
56 size_t size_ {0};
57 };
58
59 enum class PageTagType : uint8_t {
60 HEAP,
61 MACHINE_CODE,
62 MEMPOOL_CACHE,
63 METHOD_LITERAL,
64 };
65
66 #ifdef PANDA_TARGET_WINDOWS
67 #define PAGE_PROT_NONE 0x01
68 #define PAGE_PROT_READ 0x02
69 #define PAGE_PROT_READWRITE 0x04
70 #define PAGE_PROT_EXEC_READ 0x20
71 // For safety reason, Disallow prot have both write & exec capability except in JIT.
72 #define PAGE_PROT_EXEC_READWRITE 0x40
73 #define PAGE_FLAG_MAP_FIXED 0x10
74 #else
75 #define PAGE_PROT_NONE 0
76 #define PAGE_PROT_READ 1
77 #define PAGE_PROT_READWRITE 3
78 #define PAGE_PROT_EXEC_READ 5
79 // For safety reason, Disallow prot have both write & exec capability except in JIT.
80 #define PAGE_PROT_EXEC_READWRITE 7
81 #define PAGE_FLAG_MAP_FIXED 0x10
82 #endif
83
84 // Jit Fort space protection control
PageProtectProt(bool disable_codesign)85 inline int PageProtectProt([[maybe_unused]] bool disable_codesign)
86 {
87 #if defined(JIT_ENABLE_CODE_SIGN) && !defined(JIT_FORT_DISABLE)
88 if (!disable_codesign) {
89 return PAGE_PROT_EXEC_READ;
90 }
91 #endif
92 return PAGE_PROT_EXEC_READWRITE;
93 }
94
95 static constexpr char HEAP_TAG[] = "ArkTS Heap";
96 static constexpr char CODE_TAG[] = "ArkTS Code";
97 MemMap PUBLIC_API PageMap(size_t size, int prot = PAGE_PROT_NONE, size_t alignment = 0, void *addr = nullptr,
98 int flags = 0);
99 void PUBLIC_API PageUnmap(MemMap it);
100 MemMap PUBLIC_API MachineCodePageMap(size_t size, int prot = PAGE_PROT_NONE, size_t alignment = 0);
101 void PUBLIC_API MachineCodePageUnmap(MemMap it);
102 void PageRelease(void *mem, size_t size);
103 void PUBLIC_API PagePreRead(void *mem, size_t size);
104 void PageTag(void *mem, size_t size, PageTagType type, const std::string &spaceName = "",
105 const uint32_t threadId = 0);
106 void PageClearTag(void *mem, size_t size);
107 const std::string GetPageTagString(PageTagType type, const std::string &spaceName, const uint32_t threadId = 0);
108 const char *GetPageTagString(PageTagType type);
109 bool PageProtect(void *mem, size_t size, int prot);
110 size_t PUBLIC_API PageSize();
111 } // namespace panda::ecmascript
112 #endif // ECMASCRIPT_PLATFORM_MAP_H
113