• 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/os.h"
17 
18 #include <malloc.h>
19 #include <sys/mman.h>
20 #include <sys/prctl.h>
21 #include <sys/ptrace.h>
22 #include <sys/statfs.h>
23 #include <sys/statvfs.h>
24 #include <sys/xattr.h>
25 #include <unistd.h>
26 
27 #include "ecmascript/log_wrapper.h"
28 
29 #ifndef PR_SET_VMA
30 #define PR_SET_VMA 0x53564d41
31 #endif
32 
33 #ifndef PR_SET_VMA_ANON_NAME
34 #define PR_SET_VMA_ANON_NAME 0
35 #endif
36 
37 #define JITFORT_QUERY_ENCAPS 'E'
38 #define HM_PR_SET_JITFORT 0x6a6974
39 
40 namespace panda::ecmascript {
MallocUsableSize(void * p)41 size_t MallocUsableSize(void *p)
42 {
43     return malloc_usable_size(p);
44 }
45 
PrctlSetVMA(const void * p,const size_t size,const char * tag)46 int PrctlSetVMA(const void *p, const size_t size, const char *tag)
47 {
48     return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, size, tag);
49 }
50 
PtracePeektext(int pid,uintptr_t addr)51 long PtracePeektext(int pid, uintptr_t addr)
52 {
53     return ptrace(PTRACE_PEEKTEXT, pid, addr, NULL);
54 }
55 
BindSmallCpuCore()56 void BindSmallCpuCore()
57 {
58     cpu_set_t cpuset;
59 
60     CPU_ZERO(&cpuset);
61     CPU_SET(0, &cpuset); // 0: bind this thread to cpu0
62     CPU_SET(1, &cpuset); // 1: bind this thread to cpu1
63     CPU_SET(2, &cpuset); // 2: bind this thread to cpu2
64     CPU_SET(3, &cpuset); // 3: bind this thread to cpu3
65 
66     if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
67         LOG_ECMA(ERROR) << "Set CPU affinity failed";
68     }
69 }
70 
BindMidCpuCore()71 void BindMidCpuCore()
72 {
73     cpu_set_t cpuset;
74 
75     CPU_ZERO(&cpuset);
76     for (size_t i = 0; i < 7; i++) { // 7: 0-3 little core, 4-6 mid core
77         CPU_SET(i, &cpuset);
78     }
79 
80     if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
81         LOG_ECMA(ERROR) << "Set CPU affinity failed";
82     }
83 }
84 
BindAllCpuCore()85 void BindAllCpuCore()
86 {
87     cpu_set_t cpuset;
88 
89     CPU_ZERO(&cpuset);
90     for (size_t i = 0; i < common::NumberOfCpuCore(); i++) {
91         CPU_SET(i, &cpuset);
92     }
93 
94     if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
95         LOG_ECMA(ERROR) << "Set CPU affinity failed";
96     }
97 }
98 
PageMapExecFortSpace(void * addr,size_t size,int prot)99 void *PageMapExecFortSpace(void *addr, size_t size, int prot)
100 {
101     void *res = mmap(addr, size, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_EXECUTABLE, -1, 0);
102     if (res == MAP_FAILED) {
103         LOG_ECMA(ERROR) << "PageMapExecFortSpace mmap failed, addr = " << addr << ", size = " << size <<
104             ", prot = " << prot << ", error code is " << errno;
105     }
106     return res;
107 }
108 
HasJitFortACL()109 bool HasJitFortACL()
110 {
111     return (prctl(HM_PR_SET_JITFORT, JITFORT_QUERY_ENCAPS, 0) == 0);
112 }
113 
SetSecurityLabel(const std::string & path)114 void SetSecurityLabel(const std::string& path)
115 {
116     auto xattrValueSize = getxattr(path.c_str(), XATTR_KEY, nullptr, 0);
117     if (xattrValueSize == static_cast<ssize_t>(DEFAULT_DATA_LENGTH)) {
118         char xattrValue[DEFAULT_DATA_LENGTH + 1];
119         xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue, xattrValueSize);
120         xattrValue[DEFAULT_DATA_LENGTH] = '\0';
121     }
122 
123     if (setxattr(path.c_str(), XATTR_KEY, DEFAULT_DATA_LEVEL.data(), DEFAULT_DATA_LEVEL.size(), 0) < 0) {
124         LOG_ECMA(WARN) << "set label failed! level: " << DEFAULT_DATA_LEVEL << ", file: " << path;
125     }
126 }
127 
CheckDiskSpace(const std::string & path,size_t requiredBytes)128 bool CheckDiskSpace(const std::string& path, size_t requiredBytes)
129 {
130     struct statvfs stat;
131     if (statvfs(path.c_str(), &stat) != 0) {
132         return false;
133     }
134     size_t availableBytes = static_cast<size_t>(stat.f_bavail) * stat.f_frsize;
135     return availableBytes >= requiredBytes;
136 }
137 
GetDeviceValidSize(const std::string & path)138 uint64_t GetDeviceValidSize(const std::string &path)
139 {
140     struct statfs stat;
141     if (statfs(path.c_str(), &stat) != 0) {
142         LOG_ECMA(ERROR) << "execute fail because it is not a partition " << path.c_str();
143         return 0;
144     }
145     constexpr uint64_t units = 1024;
146     uint64_t bfree = static_cast<uint64_t>(stat.f_bfree);
147     uint64_t bsize = static_cast<uint64_t>(stat.f_bsize);
148     if (bsize == 0 || bfree > UINT64_MAX / bsize) {
149         LOG_ECMA(ERROR) << "overflow detected: bfree * bsize";
150         return 0;
151     }
152     return (bfree * bsize) / (units * units);
153 }
154 }  // namespace panda::ecmascript
155