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 int32_t GetAOTVersion(std::string& sigData); 41 int32_t NeedReCompile(const std::string& args, bool& sigData); 42 void HandlePowerDisconnected(); 43 void HandleScreenOn(); 44 void HandleThermalLevelChanged(const int32_t level); 45 #ifdef ENABLE_COMPILER_SERVICE_GET_PARAMETER 46 void SetAnFileMaxSizeBySysParam(); 47 void SetEnableCodeCommentBySysParam(); 48 #endif 49 50 protected: 51 int32_t FindArgsIdxToInteger(const std::unordered_map<std::string, std::string> &argsMap, 52 const std::string &keyName, int32_t &bundleID); 53 int32_t FindArgsIdxToString(const std::unordered_map<std::string, std::string> &argsMap, 54 const std::string &keyName, std::string &bundleArg); 55 int32_t PrepareArgs(const std::unordered_map<std::string, std::string> &argsMap); 56 void GetBundleId(int32_t &bundleUid, int32_t &bundleGid) const; 57 void DropCapabilities() const; 58 void GetAotArgsVector(std::vector<const char*> &argv) const; 59 void ExecuteInChildProcess() const; 60 int32_t PrintAOTCompilerResult(const int compilerStatus) const; 61 void ExecuteInParentProcess(pid_t childPid, int32_t &ret); 62 void GetCodeSignArgs(std::string &appSignature, std::string &fileName) const; 63 int32_t AOTLocalCodeSign(std::vector<int16_t> &sigData) const; 64 int32_t RemoveAotFiles() const; 65 void InitState(const pid_t childPid); 66 void AddExpandArgs(std::vector<std::string> &argVector); 67 void ResetState(); 68 void PauseAotCompiler(); 69 void AllowAotCompiler(); 70 bool VerifyCompilerModeAndPkgInfo(const std::unordered_map<std::string, std::string> &argsMap); 71 72 AotCompilerImpl() = default; 73 ~AotCompilerImpl() = default; 74 AotCompilerImpl(const AotCompilerImpl&) = delete; 75 AotCompilerImpl(AotCompilerImpl&&) = delete; 76 AotCompilerImpl& operator=(const AotCompilerImpl&) = delete; 77 AotCompilerImpl& operator=(AotCompilerImpl&&) = delete; 78 protected: 79 std::atomic<bool> allowAotCompiler_ {true}; 80 mutable std::mutex hapArgsMutex_; 81 mutable std::mutex stateMutex_; 82 struct HapArgs { 83 std::vector<std::string> argVector; 84 std::string fileName; 85 std::string signature; 86 int32_t bundleUid {0}; 87 int32_t bundleGid {0}; 88 } hapArgs_; 89 struct AOTState { 90 bool running {false}; 91 pid_t childPid {-1}; 92 } state_; 93 int32_t thermalLevel_ {0}; 94 }; 95 } // namespace OHOS::ArkCompiler 96 #endif // OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H