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 27namespace panda::ecmascript { 28size_t MallocUsableSize(void *p) 29{ 30 return malloc_size(p); 31} 32 33uint32_t NumberOfCpuCore() 34{ 35 return static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN)); 36} 37 38size_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 52int PrctlSetVMA([[maybe_unused]] const void *p, [[maybe_unused]] const size_t size, [[maybe_unused]] const char *tag) 53{ 54 return -1; 55} 56 57long PtracePeektext([[maybe_unused]] int pid, [[maybe_unused]] uintptr_t addr) 58{ 59 return static_cast<long>(-1); 60} 61 62void BindSmallCpuCore() 63{ 64 LOG_ECMA(INFO) << "Bind Small Core in macos not support"; 65} 66 67void BindMidCpuCore() 68{ 69 LOG_ECMA(INFO) << "Bind Mid Core in macos not support"; 70} 71 72void *PageMapExecFortSpace(void *addr, [[maybe_unused]] size_t size, [[maybe_unused]] int prot) 73{ 74 // basically no op 75 return addr; 76} 77 78void 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} // namespace panda::ecmascript 92