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