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 <ctime>
19 #include <malloc/malloc.h>
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
22 #include <sys/xattr.h>
23 #include <unistd.h>
24
25 #include "ecmascript/log_wrapper.h"
26
27 namespace panda::ecmascript {
MallocUsableSize(void * p)28 size_t MallocUsableSize(void *p)
29 {
30 return malloc_size(p);
31 }
32
NumberOfCpuCore()33 uint32_t NumberOfCpuCore()
34 {
35 return static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN));
36 }
37
PhysicalSize()38 size_t PhysicalSize()
39 {
40 static constexpr int MIB_LENGTH = 2;
41 int mib[2];
42 mib[0] = CTL_HW;
43 mib[1] = HW_MEMSIZE;
44 int64_t size = 0;
45 size_t bufferLength = sizeof(size);
46 if (sysctl(mib, MIB_LENGTH, &size, &bufferLength, nullptr, 0) != 0) {
47 LOG_ECMA(FATAL) << "sysctl error";
48 }
49 return static_cast<size_t>(size);
50 }
51
PrctlSetVMA(const void * p,const size_t size,const char * tag)52 int PrctlSetVMA([[maybe_unused]] const void *p, [[maybe_unused]] const size_t size, [[maybe_unused]] const char *tag)
53 {
54 return -1;
55 }
56
PtracePeektext(int pid,uintptr_t addr)57 long PtracePeektext([[maybe_unused]] int pid, [[maybe_unused]] uintptr_t addr)
58 {
59 return static_cast<long>(-1);
60 }
61
BindSmallCpuCore()62 void BindSmallCpuCore()
63 {
64 LOG_ECMA(INFO) << "Bind Small Core in macos not support";
65 }
66
BindMidCpuCore()67 void BindMidCpuCore()
68 {
69 LOG_ECMA(INFO) << "Bind Mid Core in macos not support";
70 }
71
PageMapExecFortSpace(void * addr,size_t size,int prot)72 void *PageMapExecFortSpace(void *addr, [[maybe_unused]] size_t size, [[maybe_unused]] int prot)
73 {
74 // basically no op
75 return addr;
76 }
77
SetSecurityLabel(const std::string & path)78 void SetSecurityLabel(const std::string& path)
79 {
80 auto xattrValueSize = getxattr(path.c_str(), XATTR_KEY, nullptr, 0, 0, 0);
81 if (xattrValueSize == static_cast<ssize_t>(DEFAULT_DATA_LENGTH)) {
82 char xattrValue[DEFAULT_DATA_LENGTH + 1];
83 xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue, xattrValueSize, 0, 0);
84 xattrValue[DEFAULT_DATA_LENGTH] = '\0';
85 }
86
87 if (setxattr(path.c_str(), XATTR_KEY, DEFAULT_DATA_LEVEL.data(), DEFAULT_DATA_LEVEL.size(), 0, 0) < 0) {
88 LOG_ECMA(WARN) << "set label failed! level: " << DEFAULT_DATA_LEVEL << ", file: " << path;
89 }
90 }
91
InitializeMallocConfig()92 void InitializeMallocConfig()
93 {
94 LOG_ECMA(INFO) << "Initialize Malloc Config in mac not support";
95 }
96 } // namespace panda::ecmascript
97