1 /*
2  * Copyright (c) 2023-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 FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_EXECUTOR
17 #define FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_EXECUTOR
18 
19 #include <mutex>
20 #include <nlohmann/json.hpp>
21 #include <unordered_map>
22 
23 #include "aot/aot_args.h"
24 #include "appexecfwk_errors.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 struct AOTState {
30     bool running = false;
31     std::string outputPath;
32 };
33 
34 class AOTExecutor final {
35 public:
36     static AOTExecutor& GetInstance();
37     void ExecuteAOT(const AOTArgs &aotArgs, ErrCode &ret, std::vector<uint8_t> &pendSignData);
38     ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData) const;
39     ErrCode StopAOT();
40 private:
41     AOTExecutor() = default;
42     ~AOTExecutor() = default;
43     DISALLOW_COPY_AND_MOVE(AOTExecutor);
44 
45     std::string DecToHex(uint32_t decimal) const;
46     bool CheckArgs(const AOTArgs &aotArgs) const;
47     bool GetAbcFileInfo(const std::string &hapPath, uint32_t &offset, uint32_t &length) const;
48     ErrCode PrepareArgs(const AOTArgs &aotArgs, AOTArgs &completeArgs) const;
49     nlohmann::json GetSubjectInfo(const AOTArgs &aotArgs) const;
50     void MapArgs(const AOTArgs &aotArgs, std::unordered_map<std::string, std::string> &argsMap);
51     ErrCode EnforceCodeSign(const std::string &anFileName, const std::vector<uint8_t> &signData) const;
52     ErrCode StartAOTCompiler(const AOTArgs &aotArgs, std::vector<uint8_t> &signData);
53     void InitState(const AOTArgs &aotArgs);
54     void ResetState();
55 private:
56     mutable std::mutex stateMutex_;
57     AOTState state_;
58 };
59 }  // namespace AppExecFwk
60 }  // namespace OHOS
61 #endif  // FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_EXECUTOR
62