1 /* 2 * Copyright (c) 2024 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 #ifndef OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H 17 #define OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 #include "ecmascript/compiler/aot_file/aot_version.h" 25 26 namespace OHOS::ArkCompiler { 27 class AotCompilerImpl { 28 public: 29 static constexpr int32_t AOT_COMPILE_STOP_LEVEL = 2; 30 static AotCompilerImpl &GetInstance(); 31 /** 32 * @brief ecmascript aot_compiler 33 * @param argsMap input command strings 34 * @param sigData contain signature string data for EnforceCodeSignForFile() 35 * @return err code 36 */ 37 int32_t EcmascriptAotCompiler(const std::unordered_map<std::string, std::string> &argsMap, 38 std::vector<int16_t> &sigData); 39 int32_t StopAotCompiler(); 40 41 int32_t GetAOTVersion(std::string& sigData); 42 43 int32_t NeedReCompile(const std::string& args, bool& sigData); 44 45 void HandlePowerDisconnected(); 46 47 void HandleScreenOn(); 48 49 void HandleThermalLevelChanged(const int32_t level); 50 51 private: 52 inline int32_t FindArgsIdxToInteger(const std::unordered_map<std::string, std::string> &argsMap, 53 const std::string &keyName, int32_t &bundleID); 54 inline int32_t FindArgsIdxToString(const std::unordered_map<std::string, std::string> &argsMap, 55 const std::string &keyName, std::string &bundleArg); 56 int32_t PrepareArgs(const std::unordered_map<std::string, std::string> &argsMap); 57 void DropCapabilities(const int32_t &bundleUid, const int32_t &bundleGid) const; 58 void ExecuteInChildProcess(const std::vector<std::string> &aotVector) const; 59 int32_t PrintAOTCompilerResult(const int compilerStatus); 60 void ExecuteInParentProcess(pid_t childPid, int32_t &ret); 61 int32_t AOTLocalCodeSign(const std::string &fileName, const std::string &appSignature, 62 std::vector<int16_t> &sigData); 63 void InitState(const pid_t childPid); 64 void AddExpandArgs(std::vector<std::string> &argVector); 65 void ResetState(); 66 void PauseAotCompiler(); 67 void AllowAotCompiler(); 68 bool VerifyCompilerModeAndPkgInfo(const std::unordered_map<std::string, std::string> &argsMap); 69 70 AotCompilerImpl() = default; 71 ~AotCompilerImpl() = default; 72 73 AotCompilerImpl(const AotCompilerImpl&) = delete; 74 AotCompilerImpl(AotCompilerImpl&&) = delete; 75 AotCompilerImpl& operator=(const AotCompilerImpl&) = delete; 76 AotCompilerImpl& operator=(AotCompilerImpl&&) = delete; 77 private: 78 mutable std::mutex mutex_; 79 mutable std::mutex stateMutex_; 80 struct HapArgs { 81 std::vector<std::string> argVector; 82 std::string fileName; 83 std::string signature; 84 int32_t bundleUid; 85 int32_t bundleGid; 86 } hapArgs; 87 struct AOTState { 88 bool running = false; 89 pid_t childPid = -1; 90 } state_; 91 std::atomic<bool> allowAotCompiler_ {true}; 92 int32_t thermalLevel_ {0}; 93 }; 94 } // namespace OHOS::ArkCompiler 95 #endif // OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H