• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/platform/map.h"
17 
18 #include <io.h>
19 #include <windows.h>
20 
21 #ifdef ERROR
22 #undef ERROR
23 #endif
24 
25 #include "ecmascript/log_wrapper.h"
26 #include "ecmascript/mem/mem.h"
27 
28 namespace panda::ecmascript {
29 static constexpr int INSUFFICIENT_CONTINUOUS_MEM = 1455;
30 
PageMap(size_t size,int prot,size_t alignment)31 MemMap PageMap(size_t size, int prot, size_t alignment)
32 {
33     ASSERT(size == AlignUp(size, PageSize()));
34     ASSERT(alignment == AlignUp(alignment, PageSize()));
35     size_t allocSize = size + alignment;
36     void *result = VirtualAlloc(nullptr, allocSize, MEM_COMMIT, prot);
37     if (result == nullptr) {
38         int errCode = GetLastError();
39         if (errCode == INSUFFICIENT_CONTINUOUS_MEM) {
40             LOG_NO_TAG(ERROR) << "[ArkRuntime Log]Failed to request a continuous segment of " << size
41                               << " virtual memory. Please clean up other heavy processes or restart the computer.";
42         }
43         LOG_ECMA(FATAL) << "PageMap "<< size << ", prot:" << prot << " fail, the error code is: " << errCode;
44     }
45     if (alignment != 0) {
46         auto alignResult = AlignUp(reinterpret_cast<uintptr_t>(result), alignment);
47         return MemMap(result, reinterpret_cast<void *>(alignResult), size);
48     }
49     return MemMap(result, result, size);
50 }
51 
PageUnmap(MemMap it)52 void PageUnmap(MemMap it)
53 {
54     if (!VirtualFree(it.GetOriginAddr(), 0, MEM_RELEASE)) {
55         int errCode = GetLastError();
56         LOG_ECMA(ERROR) << "PageUnmap failed, error code is " << errCode;
57     }
58 }
59 
MachineCodePageMap(size_t size,int prot,size_t alignment)60 MemMap MachineCodePageMap(size_t size, int prot, size_t alignment)
61 {
62     MemMap memMap = PageMap(size, prot, alignment);
63     PageTag(memMap.GetMem(), memMap.GetSize());
64     return memMap;
65 }
66 
MachineCodePageUnmap(MemMap it)67 void MachineCodePageUnmap(MemMap it)
68 {
69     PageTag(it.GetMem(), it.GetSize(), true);
70     PageUnmap(it);
71 }
72 
PageRelease(void * mem,size_t size)73 void PageRelease([[maybe_unused]] void *mem, [[maybe_unused]] size_t size)
74 {
75 }
76 
PageTag(void * mem,size_t size,bool remove)77 void PageTag([[maybe_unused]] void *mem, [[maybe_unused]] size_t size, [[maybe_unused]] bool remove)
78 {
79 }
80 
PageProtect(void * mem,size_t size,int prot)81 void PageProtect(void *mem, size_t size, int prot)
82 {
83     [[maybe_unused]] DWORD oldProtect;
84     if (!VirtualProtect(mem, size, prot, &oldProtect)) {
85         int errCode = GetLastError();
86         LOG_ECMA(ERROR) << "PageProtect change to " << prot << " failed, error code is " << errCode;
87     }
88 }
89 
PageSize()90 size_t PageSize()
91 {
92     SYSTEM_INFO info;
93     GetSystemInfo(&info);
94     return info.dwPageSize;
95 }
96 }  // namespace panda::ecmascript
97