• 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/xattr.h>
23 #include <unistd.h>
24 
25 #include "ecmascript/log_wrapper.h"
26 
27 #ifndef PR_SET_VMA
28 #define PR_SET_VMA 0x53564d41
29 #endif
30 
31 #ifndef PR_SET_VMA_ANON_NAME
32 #define PR_SET_VMA_ANON_NAME 0
33 #endif
34 
35 namespace panda::ecmascript {
MallocUsableSize(void * p)36 size_t MallocUsableSize(void *p)
37 {
38     return malloc_usable_size(p);
39 }
40 
NumberOfCpuCore()41 uint32_t NumberOfCpuCore()
42 {
43     return static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN));
44 }
45 
PhysicalSize()46 size_t PhysicalSize()
47 {
48     auto pages = sysconf(_SC_PHYS_PAGES);
49     auto pageSize = sysconf(_SC_PAGE_SIZE);
50     return pages * pageSize;
51 }
52 
PrctlSetVMA(const void * p,const size_t size,const char * tag)53 int PrctlSetVMA(const void *p, const size_t size, const char *tag)
54 {
55     return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, size, tag);
56 }
57 
PtracePeektext(int pid,uintptr_t addr)58 long PtracePeektext(int pid, uintptr_t addr)
59 {
60     return ptrace(PTRACE_PEEKTEXT, pid, addr, NULL);
61 }
62 
BindSmallCpuCore()63 void BindSmallCpuCore()
64 {
65     cpu_set_t cpuset;
66 
67     CPU_ZERO(&cpuset);
68     CPU_SET(0, &cpuset); // 0: bind this process to cpu0
69     CPU_SET(1, &cpuset); // 1: bind this process to cpu1
70     CPU_SET(2, &cpuset); // 2: bind this process to cpu2
71     CPU_SET(3, &cpuset); // 3: bind this process to cpu3
72 
73     if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
74         LOG_ECMA(ERROR) << "Set CPU affinity failed";
75     }
76 }
77 
BindMidCpuCore()78 void BindMidCpuCore()
79 {
80     cpu_set_t cpuset;
81 
82     CPU_ZERO(&cpuset);
83     for (size_t i = 0; i < 7; i++) { // 7: 0-3 little core, 4-6 mid core
84         CPU_SET(i, &cpuset);
85     }
86 
87     if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
88         LOG_ECMA(ERROR) << "Set CPU affinity failed";
89     }
90 }
91 
PageMapExecFortSpace(void * addr,size_t size,int prot)92 void *PageMapExecFortSpace(void *addr, size_t size, int prot)
93 {
94     void *res = mmap(addr, size, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_EXECUTABLE, -1, 0);
95     if (res == MAP_FAILED) {
96         LOG_ECMA(ERROR) << "PageMapExecFortSpace mmap failed, addr = " << addr << ", size = " << size <<
97             ", prot = " << prot << ", error code is " << errno;
98     }
99     return res;
100 }
101 
SetSecurityLabel(const std::string & path)102 void SetSecurityLabel(const std::string& path)
103 {
104     auto xattrValueSize = getxattr(path.c_str(), XATTR_KEY, nullptr, 0);
105     if (xattrValueSize == static_cast<ssize_t>(DEFAULT_DATA_LENGTH)) {
106         char xattrValue[DEFAULT_DATA_LENGTH + 1];
107         xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue, xattrValueSize);
108         xattrValue[DEFAULT_DATA_LENGTH] = '\0';
109     }
110 
111     if (setxattr(path.c_str(), XATTR_KEY, DEFAULT_DATA_LEVEL.data(), DEFAULT_DATA_LEVEL.size(), 0) < 0) {
112         LOG_ECMA(WARN) << "set label failed! level: " << DEFAULT_DATA_LEVEL << ", file: " << path;
113     }
114 }
115 }  // namespace panda::ecmascript
116