14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "aot_compiler_impl.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include <cerrno> 194514f5e3Sopenharmony_ci#include <cstdint> 204514f5e3Sopenharmony_ci#include <cstdlib> 214514f5e3Sopenharmony_ci#include <cstring> 224514f5e3Sopenharmony_ci#include <linux/capability.h> 234514f5e3Sopenharmony_ci#include <securec.h> 244514f5e3Sopenharmony_ci#include <sys/capability.h> 254514f5e3Sopenharmony_ci#include <sys/types.h> 264514f5e3Sopenharmony_ci#include <sys/wait.h> 274514f5e3Sopenharmony_ci#include <unistd.h> 284514f5e3Sopenharmony_ci#include <thread> 294514f5e3Sopenharmony_ci 304514f5e3Sopenharmony_ci#include "aot_compiler_constants.h" 314514f5e3Sopenharmony_ci#include "aot_compiler_error_utils.h" 324514f5e3Sopenharmony_ci#include "aot_compiler_service.h" 334514f5e3Sopenharmony_ci#include "ecmascript/log_wrapper.h" 344514f5e3Sopenharmony_ci#include "hitrace_meter.h" 354514f5e3Sopenharmony_ci#ifdef CODE_SIGN_ENABLE 364514f5e3Sopenharmony_ci#include "local_code_sign_kit.h" 374514f5e3Sopenharmony_ci#endif 384514f5e3Sopenharmony_ci#include "system_ability_definition.h" 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_cinamespace OHOS::ArkCompiler { 414514f5e3Sopenharmony_ciAotCompilerImpl& AotCompilerImpl::GetInstance() 424514f5e3Sopenharmony_ci{ 434514f5e3Sopenharmony_ci static AotCompilerImpl aotCompiler; 444514f5e3Sopenharmony_ci return aotCompiler; 454514f5e3Sopenharmony_ci} 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_ciint32_t AotCompilerImpl::FindArgsIdxToInteger(const std::unordered_map<std::string, std::string> &argsMap, 484514f5e3Sopenharmony_ci const std::string &keyName, int32_t &bundleID) 494514f5e3Sopenharmony_ci{ 504514f5e3Sopenharmony_ci if (argsMap.find(keyName) == argsMap.end()) { 514514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 524514f5e3Sopenharmony_ci } 534514f5e3Sopenharmony_ci if (argsMap.at(keyName).empty() || !isdigit(argsMap.at(keyName).at(0))) { 544514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci size_t sz; 574514f5e3Sopenharmony_ci bundleID = static_cast<int32_t>(std::stoi(argsMap.at(keyName), &sz)); 584514f5e3Sopenharmony_ci if (sz < static_cast<size_t>(argsMap.at(keyName).size())) { 594514f5e3Sopenharmony_ci LOG_SA(ERROR) << "trigger exception as converting string to integer"; 604514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 614514f5e3Sopenharmony_ci } 624514f5e3Sopenharmony_ci return ERR_OK; 634514f5e3Sopenharmony_ci} 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ciint32_t AotCompilerImpl::FindArgsIdxToString(const std::unordered_map<std::string, std::string> &argsMap, 664514f5e3Sopenharmony_ci const std::string &keyName, std::string &bundleArg) 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci if (argsMap.find(keyName) == argsMap.end()) { 694514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci bundleArg = argsMap.at(keyName); 724514f5e3Sopenharmony_ci return ERR_OK; 734514f5e3Sopenharmony_ci} 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ciint32_t AotCompilerImpl::PrepareArgs(const std::unordered_map<std::string, std::string> &argsMap) 764514f5e3Sopenharmony_ci{ 774514f5e3Sopenharmony_ci for (const auto &arg : argsMap) { 784514f5e3Sopenharmony_ci LOG_SA(DEBUG) << arg.first << ": " << arg.second; 794514f5e3Sopenharmony_ci } 804514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(hapArgsMutex_); 814514f5e3Sopenharmony_ci std::string abcPath; 824514f5e3Sopenharmony_ci if ((FindArgsIdxToInteger(argsMap, ArgsIdx::BUNDLE_UID, hapArgs_.bundleUid) != ERR_OK) || 834514f5e3Sopenharmony_ci (FindArgsIdxToInteger(argsMap, ArgsIdx::BUNDLE_GID, hapArgs_.bundleGid) != ERR_OK) || 844514f5e3Sopenharmony_ci (FindArgsIdxToString(argsMap, ArgsIdx::AN_FILE_NAME, hapArgs_.fileName) != ERR_OK) || 854514f5e3Sopenharmony_ci (FindArgsIdxToString(argsMap, ArgsIdx::APP_SIGNATURE, hapArgs_.signature) != ERR_OK) || 864514f5e3Sopenharmony_ci (FindArgsIdxToString(argsMap, ArgsIdx::ABC_PATH, abcPath) != ERR_OK)) { 874514f5e3Sopenharmony_ci LOG_SA(ERROR) << "aot compiler Args parsing error"; 884514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci hapArgs_.argVector.clear(); 914514f5e3Sopenharmony_ci hapArgs_.argVector.emplace_back(Cmds::ARK_AOT_COMPILER); 924514f5e3Sopenharmony_ci // service process add aot compile args here 934514f5e3Sopenharmony_ci AddExpandArgs(hapArgs_.argVector); 944514f5e3Sopenharmony_ci for (auto &argPair : argsMap) { 954514f5e3Sopenharmony_ci if (AotArgsSet.find(argPair.first) != AotArgsSet.end()) { 964514f5e3Sopenharmony_ci hapArgs_.argVector.emplace_back(Symbols::PREFIX + argPair.first + Symbols::EQ + argPair.second); 974514f5e3Sopenharmony_ci } 984514f5e3Sopenharmony_ci } 994514f5e3Sopenharmony_ci hapArgs_.argVector.emplace_back(abcPath); 1004514f5e3Sopenharmony_ci return ERR_OK; 1014514f5e3Sopenharmony_ci} 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_civoid AotCompilerImpl::GetBundleId(int32_t &bundleUid, int32_t &bundleGid) const 1044514f5e3Sopenharmony_ci{ 1054514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(hapArgsMutex_); 1064514f5e3Sopenharmony_ci bundleUid = hapArgs_.bundleUid; 1074514f5e3Sopenharmony_ci bundleGid = hapArgs_.bundleGid; 1084514f5e3Sopenharmony_ci} 1094514f5e3Sopenharmony_ci 1104514f5e3Sopenharmony_civoid AotCompilerImpl::DropCapabilities() const 1114514f5e3Sopenharmony_ci{ 1124514f5e3Sopenharmony_ci LOG_SA(INFO) << "begin to drop capabilities"; 1134514f5e3Sopenharmony_ci int32_t bundleUid = 0; 1144514f5e3Sopenharmony_ci int32_t bundleGid = 0; 1154514f5e3Sopenharmony_ci GetBundleId(bundleUid, bundleGid); 1164514f5e3Sopenharmony_ci if (setgid(bundleGid)) { 1174514f5e3Sopenharmony_ci LOG_SA(ERROR) << "dropCapabilities setgid failed : " << strerror(errno); 1184514f5e3Sopenharmony_ci exit(-1); 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci if (setuid(bundleUid)) { 1214514f5e3Sopenharmony_ci LOG_SA(ERROR) << "dropCapabilities setuid failed : " << strerror(errno); 1224514f5e3Sopenharmony_ci exit(-1); 1234514f5e3Sopenharmony_ci } 1244514f5e3Sopenharmony_ci struct __user_cap_header_struct capHeader; 1254514f5e3Sopenharmony_ci if (memset_s(&capHeader, sizeof(capHeader), 0, sizeof(capHeader)) != EOK) { 1264514f5e3Sopenharmony_ci LOG_SA(ERROR) << "memset_s capHeader failed : " << strerror(errno); 1274514f5e3Sopenharmony_ci exit(-1); 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci capHeader.version = _LINUX_CAPABILITY_VERSION_3; 1304514f5e3Sopenharmony_ci capHeader.pid = 0; 1314514f5e3Sopenharmony_ci struct __user_cap_data_struct capData[2]; 1324514f5e3Sopenharmony_ci if (memset_s(&capData, sizeof(capData), 0, sizeof(capData)) != EOK) { 1334514f5e3Sopenharmony_ci LOG_SA(ERROR) << "memset_s capData failed : " << strerror(errno); 1344514f5e3Sopenharmony_ci exit(-1); 1354514f5e3Sopenharmony_ci } 1364514f5e3Sopenharmony_ci if (capset(&capHeader, capData) != 0) { 1374514f5e3Sopenharmony_ci LOG_SA(ERROR) << "capset failed : " << strerror(errno); 1384514f5e3Sopenharmony_ci exit(-1); 1394514f5e3Sopenharmony_ci } 1404514f5e3Sopenharmony_ci LOG_SA(INFO) << "drop capabilities success"; 1414514f5e3Sopenharmony_ci} 1424514f5e3Sopenharmony_ci 1434514f5e3Sopenharmony_civoid AotCompilerImpl::GetAotArgsVector(std::vector<const char*> &argv) const 1444514f5e3Sopenharmony_ci{ 1454514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(hapArgsMutex_); 1464514f5e3Sopenharmony_ci const std::vector<std::string> &aotVector = hapArgs_.argVector; 1474514f5e3Sopenharmony_ci argv.reserve(aotVector.size() + 1); 1484514f5e3Sopenharmony_ci for (auto &arg : aotVector) { 1494514f5e3Sopenharmony_ci argv.emplace_back(arg.c_str()); 1504514f5e3Sopenharmony_ci } 1514514f5e3Sopenharmony_ci} 1524514f5e3Sopenharmony_ci 1534514f5e3Sopenharmony_civoid AotCompilerImpl::ExecuteInChildProcess() const 1544514f5e3Sopenharmony_ci{ 1554514f5e3Sopenharmony_ci std::vector<const char*> argv; 1564514f5e3Sopenharmony_ci GetAotArgsVector(argv); 1574514f5e3Sopenharmony_ci LOG_SA(INFO) << "ark_aot_compiler argv size : " << argv.size(); 1584514f5e3Sopenharmony_ci for (const auto &arg : argv) { 1594514f5e3Sopenharmony_ci LOG_SA(INFO) << arg; 1604514f5e3Sopenharmony_ci } 1614514f5e3Sopenharmony_ci argv.emplace_back(nullptr); 1624514f5e3Sopenharmony_ci LOG_SA(INFO) << "begin to execute ark_aot_compiler"; 1634514f5e3Sopenharmony_ci execv(argv[0], const_cast<char* const*>(argv.data())); 1644514f5e3Sopenharmony_ci LOG_SA(ERROR) << "execv failed : " << strerror(errno); 1654514f5e3Sopenharmony_ci exit(-1); 1664514f5e3Sopenharmony_ci} 1674514f5e3Sopenharmony_ci 1684514f5e3Sopenharmony_civoid AotCompilerImpl::AddExpandArgs(std::vector<std::string> &argVector) 1694514f5e3Sopenharmony_ci{ 1704514f5e3Sopenharmony_ci std::string thermalLevelArg = "--compiler-thermal-level=" + std::to_string(thermalLevel_); 1714514f5e3Sopenharmony_ci argVector.emplace_back(thermalLevelArg); 1724514f5e3Sopenharmony_ci} 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ciint32_t AotCompilerImpl::PrintAOTCompilerResult(const int compilerStatus) const 1754514f5e3Sopenharmony_ci{ 1764514f5e3Sopenharmony_ci if (RetInfoOfCompiler.find(compilerStatus) == RetInfoOfCompiler.end()) { 1774514f5e3Sopenharmony_ci LOG_SA(ERROR) << OtherInfoOfCompiler.mesg; 1784514f5e3Sopenharmony_ci return OtherInfoOfCompiler.retCode; 1794514f5e3Sopenharmony_ci } 1804514f5e3Sopenharmony_ci if (RetInfoOfCompiler.at(compilerStatus).retCode == ERR_AOT_COMPILER_CALL_FAILED) { 1814514f5e3Sopenharmony_ci LOG_SA(ERROR) << RetInfoOfCompiler.at(compilerStatus).mesg; 1824514f5e3Sopenharmony_ci } else { 1834514f5e3Sopenharmony_ci LOG_SA(INFO) << RetInfoOfCompiler.at(compilerStatus).mesg; 1844514f5e3Sopenharmony_ci } 1854514f5e3Sopenharmony_ci return RetInfoOfCompiler.at(compilerStatus).retCode; 1864514f5e3Sopenharmony_ci} 1874514f5e3Sopenharmony_ci 1884514f5e3Sopenharmony_civoid AotCompilerImpl::ExecuteInParentProcess(const pid_t childPid, int32_t &ret) 1894514f5e3Sopenharmony_ci{ 1904514f5e3Sopenharmony_ci { 1914514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(stateMutex_); 1924514f5e3Sopenharmony_ci InitState(childPid); 1934514f5e3Sopenharmony_ci } 1944514f5e3Sopenharmony_ci int status; 1954514f5e3Sopenharmony_ci int waitRet = waitpid(childPid, &status, 0); 1964514f5e3Sopenharmony_ci if (waitRet == -1) { 1974514f5e3Sopenharmony_ci LOG_SA(ERROR) << "waitpid failed"; 1984514f5e3Sopenharmony_ci ret = ERR_AOT_COMPILER_CALL_FAILED; 1994514f5e3Sopenharmony_ci } else if (WIFEXITED(status)) { 2004514f5e3Sopenharmony_ci int exitStatus = WEXITSTATUS(status); 2014514f5e3Sopenharmony_ci LOG_SA(INFO) << "child process exited with status: " << exitStatus; 2024514f5e3Sopenharmony_ci ret = PrintAOTCompilerResult(exitStatus); 2034514f5e3Sopenharmony_ci } else if (WIFSIGNALED(status)) { 2044514f5e3Sopenharmony_ci int signalNumber = WTERMSIG(status); 2054514f5e3Sopenharmony_ci LOG_SA(WARN) << "child process terminated by signal: " << signalNumber; 2064514f5e3Sopenharmony_ci ret = signalNumber == SIGKILL ? ERR_AOT_COMPILER_CALL_CANCELLED : ERR_AOT_COMPILER_CALL_CRASH; 2074514f5e3Sopenharmony_ci } else if (WIFSTOPPED(status)) { 2084514f5e3Sopenharmony_ci int signalNumber = WSTOPSIG(status); 2094514f5e3Sopenharmony_ci LOG_SA(WARN) << "child process was stopped by signal: " << signalNumber; 2104514f5e3Sopenharmony_ci ret = ERR_AOT_COMPILER_CALL_FAILED; 2114514f5e3Sopenharmony_ci } else if (WIFCONTINUED(status)) { 2124514f5e3Sopenharmony_ci LOG_SA(WARN) << "child process was resumed"; 2134514f5e3Sopenharmony_ci ret = ERR_AOT_COMPILER_CALL_FAILED; 2144514f5e3Sopenharmony_ci } else { 2154514f5e3Sopenharmony_ci LOG_SA(WARN) << "unknown"; 2164514f5e3Sopenharmony_ci ret = ERR_AOT_COMPILER_CALL_FAILED; 2174514f5e3Sopenharmony_ci } 2184514f5e3Sopenharmony_ci { 2194514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(stateMutex_); 2204514f5e3Sopenharmony_ci ResetState(); 2214514f5e3Sopenharmony_ci } 2224514f5e3Sopenharmony_ci} 2234514f5e3Sopenharmony_ci 2244514f5e3Sopenharmony_ciint32_t AotCompilerImpl::EcmascriptAotCompiler(const std::unordered_map<std::string, std::string> &argsMap, 2254514f5e3Sopenharmony_ci std::vector<int16_t> &sigData) 2264514f5e3Sopenharmony_ci{ 2274514f5e3Sopenharmony_ci#ifdef CODE_SIGN_ENABLE 2284514f5e3Sopenharmony_ci if (!allowAotCompiler_) { 2294514f5e3Sopenharmony_ci LOG_SA(ERROR) << "aot compiler is not allowed now"; 2304514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_CONNECT_FAILED; 2314514f5e3Sopenharmony_ci } 2324514f5e3Sopenharmony_ci if (argsMap.empty() || (PrepareArgs(argsMap) != ERR_OK)) { 2334514f5e3Sopenharmony_ci LOG_SA(ERROR) << "aot compiler arguments error"; 2344514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_PARAM_FAILED; 2354514f5e3Sopenharmony_ci } 2364514f5e3Sopenharmony_ci int32_t ret = ERR_OK; 2374514f5e3Sopenharmony_ci LOG_SA(INFO) << "begin to fork"; 2384514f5e3Sopenharmony_ci pid_t pid = fork(); 2394514f5e3Sopenharmony_ci if (pid == -1) { 2404514f5e3Sopenharmony_ci LOG_SA(ERROR) << "fork process failed : " << strerror(errno); 2414514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_CALL_FAILED; 2424514f5e3Sopenharmony_ci } else if (pid == 0) { 2434514f5e3Sopenharmony_ci DropCapabilities(); 2444514f5e3Sopenharmony_ci ExecuteInChildProcess(); 2454514f5e3Sopenharmony_ci } else { 2464514f5e3Sopenharmony_ci ExecuteInParentProcess(pid, ret); 2474514f5e3Sopenharmony_ci } 2484514f5e3Sopenharmony_ci if (ret == ERR_OK_NO_AOT_FILE) { 2494514f5e3Sopenharmony_ci return ERR_OK; 2504514f5e3Sopenharmony_ci } 2514514f5e3Sopenharmony_ci return ret != ERR_OK ? ret : AOTLocalCodeSign(sigData); 2524514f5e3Sopenharmony_ci#else 2534514f5e3Sopenharmony_ci LOG_SA(ERROR) << "no need to AOT compile when code signature disable"; 2544514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_SIGNATURE_DISABLE; 2554514f5e3Sopenharmony_ci#endif 2564514f5e3Sopenharmony_ci} 2574514f5e3Sopenharmony_ci 2584514f5e3Sopenharmony_ciint32_t AotCompilerImpl::GetAOTVersion(std::string& sigData) 2594514f5e3Sopenharmony_ci{ 2604514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::GetAOTVersion"; 2614514f5e3Sopenharmony_ci sigData = panda::ecmascript::AOTFileVersion::GetAOTVersion(); 2624514f5e3Sopenharmony_ci 2634514f5e3Sopenharmony_ci return ERR_OK; 2644514f5e3Sopenharmony_ci} 2654514f5e3Sopenharmony_ci 2664514f5e3Sopenharmony_ciint32_t AotCompilerImpl::NeedReCompile(const std::string& args, bool& sigData) 2674514f5e3Sopenharmony_ci{ 2684514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::NeedReCompile"; 2694514f5e3Sopenharmony_ci sigData = panda::ecmascript::AOTFileVersion::CheckAOTVersion(args); 2704514f5e3Sopenharmony_ci return ERR_OK; 2714514f5e3Sopenharmony_ci} 2724514f5e3Sopenharmony_ci 2734514f5e3Sopenharmony_civoid AotCompilerImpl::GetCodeSignArgs(std::string &appSignature, std::string &fileName) const 2744514f5e3Sopenharmony_ci{ 2754514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(hapArgsMutex_); 2764514f5e3Sopenharmony_ci appSignature = hapArgs_.signature; 2774514f5e3Sopenharmony_ci fileName = hapArgs_.fileName; 2784514f5e3Sopenharmony_ci} 2794514f5e3Sopenharmony_ci 2804514f5e3Sopenharmony_ciint32_t AotCompilerImpl::AOTLocalCodeSign(std::vector<int16_t> &sigData) const 2814514f5e3Sopenharmony_ci{ 2824514f5e3Sopenharmony_ci#ifdef CODE_SIGN_ENABLE 2834514f5e3Sopenharmony_ci std::string appSignature; 2844514f5e3Sopenharmony_ci std::string fileName; 2854514f5e3Sopenharmony_ci GetCodeSignArgs(appSignature, fileName); 2864514f5e3Sopenharmony_ci Security::CodeSign::ByteBuffer sig; 2874514f5e3Sopenharmony_ci if (Security::CodeSign::LocalCodeSignKit::SignLocalCode(appSignature, fileName, sig) 2884514f5e3Sopenharmony_ci != CommonErrCode::CS_SUCCESS) { 2894514f5e3Sopenharmony_ci LOG_SA(ERROR) << "failed to sign the aot file"; 2904514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_SIGNATURE_FAILED; 2914514f5e3Sopenharmony_ci } 2924514f5e3Sopenharmony_ci LOG_SA(DEBUG) << "aot file local sign success"; 2934514f5e3Sopenharmony_ci uint8_t *dataPtr = sig.GetBuffer(); 2944514f5e3Sopenharmony_ci for (uint32_t i = 0; i < sig.GetSize(); ++i) { 2954514f5e3Sopenharmony_ci sigData.emplace_back(static_cast<int16_t>(dataPtr[i])); 2964514f5e3Sopenharmony_ci } 2974514f5e3Sopenharmony_ci return ERR_OK; 2984514f5e3Sopenharmony_ci#else 2994514f5e3Sopenharmony_ci LOG_SA(ERROR) << "no need to AOT local code sign when code signature disable"; 3004514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_SIGNATURE_DISABLE; 3014514f5e3Sopenharmony_ci#endif 3024514f5e3Sopenharmony_ci} 3034514f5e3Sopenharmony_ci 3044514f5e3Sopenharmony_ciint32_t AotCompilerImpl::StopAotCompiler() 3054514f5e3Sopenharmony_ci{ 3064514f5e3Sopenharmony_ci LOG_SA(INFO) << "begin to stop AOT"; 3074514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(stateMutex_); 3084514f5e3Sopenharmony_ci if (!state_.running) { 3094514f5e3Sopenharmony_ci LOG_SA(INFO) << "AOT not running, return directly"; 3104514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_STOP_FAILED; 3114514f5e3Sopenharmony_ci } 3124514f5e3Sopenharmony_ci if (state_.childPid <= 0) { 3134514f5e3Sopenharmony_ci LOG_SA(ERROR) << "invalid child pid"; 3144514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_STOP_FAILED; 3154514f5e3Sopenharmony_ci } 3164514f5e3Sopenharmony_ci LOG_SA(INFO) << "begin to kill child process : " << state_.childPid; 3174514f5e3Sopenharmony_ci auto result = kill(state_.childPid, SIGKILL); 3184514f5e3Sopenharmony_ci int32_t ret = RemoveAotFiles(); 3194514f5e3Sopenharmony_ci if (result != 0) { 3204514f5e3Sopenharmony_ci LOG_SA(INFO) << "kill child process failed: " << result; 3214514f5e3Sopenharmony_ci ret = ERR_AOT_COMPILER_STOP_FAILED; 3224514f5e3Sopenharmony_ci } else { 3234514f5e3Sopenharmony_ci LOG_SA(INFO) << "kill child process success"; 3244514f5e3Sopenharmony_ci } 3254514f5e3Sopenharmony_ci ResetState(); 3264514f5e3Sopenharmony_ci return ret; 3274514f5e3Sopenharmony_ci} 3284514f5e3Sopenharmony_ci 3294514f5e3Sopenharmony_ciint32_t AotCompilerImpl::RemoveAotFiles() const 3304514f5e3Sopenharmony_ci{ 3314514f5e3Sopenharmony_ci std::lock_guard<std::mutex> lock(hapArgsMutex_); 3324514f5e3Sopenharmony_ci if (access(hapArgs_.fileName.c_str(), ERR_OK) != ERR_FAIL) { 3334514f5e3Sopenharmony_ci auto delRes = std::remove(hapArgs_.fileName.c_str()); 3344514f5e3Sopenharmony_ci if (delRes != ERR_OK) { 3354514f5e3Sopenharmony_ci LOG_SA(INFO) << "delete invalid aot file failed: " << delRes; 3364514f5e3Sopenharmony_ci return ERR_AOT_COMPILER_STOP_FAILED; 3374514f5e3Sopenharmony_ci } else { 3384514f5e3Sopenharmony_ci LOG_SA(INFO) << "delete invalid aot file success"; 3394514f5e3Sopenharmony_ci } 3404514f5e3Sopenharmony_ci } 3414514f5e3Sopenharmony_ci return ERR_OK; 3424514f5e3Sopenharmony_ci} 3434514f5e3Sopenharmony_ci 3444514f5e3Sopenharmony_civoid AotCompilerImpl::HandlePowerDisconnected() 3454514f5e3Sopenharmony_ci{ 3464514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::HandlePowerDisconnected"; 3474514f5e3Sopenharmony_ci PauseAotCompiler(); 3484514f5e3Sopenharmony_ci std::thread([]() { 3494514f5e3Sopenharmony_ci (void)AotCompilerImpl::GetInstance().StopAotCompiler(); 3504514f5e3Sopenharmony_ci sleep(30); // wait for 30 seconds 3514514f5e3Sopenharmony_ci AotCompilerImpl::GetInstance().AllowAotCompiler(); 3524514f5e3Sopenharmony_ci }).detach(); 3534514f5e3Sopenharmony_ci} 3544514f5e3Sopenharmony_ci 3554514f5e3Sopenharmony_civoid AotCompilerImpl::HandleScreenOn() 3564514f5e3Sopenharmony_ci{ 3574514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::HandleScreenOn"; 3584514f5e3Sopenharmony_ci PauseAotCompiler(); 3594514f5e3Sopenharmony_ci std::thread([]() { 3604514f5e3Sopenharmony_ci (void)AotCompilerImpl::GetInstance().StopAotCompiler(); 3614514f5e3Sopenharmony_ci sleep(40); // wait for 40 seconds 3624514f5e3Sopenharmony_ci AotCompilerImpl::GetInstance().AllowAotCompiler(); 3634514f5e3Sopenharmony_ci }).detach(); 3644514f5e3Sopenharmony_ci} 3654514f5e3Sopenharmony_ci 3664514f5e3Sopenharmony_civoid AotCompilerImpl::HandleThermalLevelChanged(const int32_t level) 3674514f5e3Sopenharmony_ci{ 3684514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::HandleThermalLevelChanged"; 3694514f5e3Sopenharmony_ci thermalLevel_ = level; 3704514f5e3Sopenharmony_ci // thermal level >= 2, stop aot compile 3714514f5e3Sopenharmony_ci if (thermalLevel_ >= AOT_COMPILE_STOP_LEVEL) { 3724514f5e3Sopenharmony_ci PauseAotCompiler(); 3734514f5e3Sopenharmony_ci } else { 3744514f5e3Sopenharmony_ci AllowAotCompiler(); 3754514f5e3Sopenharmony_ci } 3764514f5e3Sopenharmony_ci} 3774514f5e3Sopenharmony_ci 3784514f5e3Sopenharmony_civoid AotCompilerImpl::PauseAotCompiler() 3794514f5e3Sopenharmony_ci{ 3804514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::PauseAotCompiler"; 3814514f5e3Sopenharmony_ci allowAotCompiler_ = false; 3824514f5e3Sopenharmony_ci} 3834514f5e3Sopenharmony_ci 3844514f5e3Sopenharmony_civoid AotCompilerImpl::AllowAotCompiler() 3854514f5e3Sopenharmony_ci{ 3864514f5e3Sopenharmony_ci LOG_SA(INFO) << "AotCompilerImpl::AllowAotCompiler"; 3874514f5e3Sopenharmony_ci allowAotCompiler_ = true; 3884514f5e3Sopenharmony_ci} 3894514f5e3Sopenharmony_ci 3904514f5e3Sopenharmony_civoid AotCompilerImpl::InitState(const pid_t childPid) 3914514f5e3Sopenharmony_ci{ 3924514f5e3Sopenharmony_ci state_.running = true; 3934514f5e3Sopenharmony_ci state_.childPid = childPid; 3944514f5e3Sopenharmony_ci} 3954514f5e3Sopenharmony_ci 3964514f5e3Sopenharmony_civoid AotCompilerImpl::ResetState() 3974514f5e3Sopenharmony_ci{ 3984514f5e3Sopenharmony_ci state_.running = false; 3994514f5e3Sopenharmony_ci state_.childPid = -1; 4004514f5e3Sopenharmony_ci} 4014514f5e3Sopenharmony_ci} // namespace OHOS::ArkCompiler