• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "aot/aot_executor.h"
17 
18 #include <cerrno>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <cstring>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28 
29 #include "app_log_wrapper.h"
30 #include "bundle_constants.h"
31 #include "bundle_extractor.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 namespace {
36 const std::string ABC_RELATIVE_PATH = "ets/modules.abc";
37 const std::string HEX_PREFIX = "0x";
38 }
39 
GetInstance()40 AOTExecutor& AOTExecutor::GetInstance()
41 {
42     static AOTExecutor executor;
43     return executor;
44 }
45 
DecToHex(uint32_t decimal) const46 std::string AOTExecutor::DecToHex(uint32_t decimal) const
47 {
48     APP_LOGD("DecToHex begin, decimal : %{public}u", decimal);
49     std::stringstream ss;
50     ss << std::hex << decimal;
51     std::string hexString = HEX_PREFIX + ss.str();
52     APP_LOGD("hex : %{public}s", hexString.c_str());
53     return hexString;
54 }
55 
CheckArgs(const AOTArgs & aotArgs) const56 bool AOTExecutor::CheckArgs(const AOTArgs &aotArgs) const
57 {
58     if (aotArgs.compileMode.empty() || aotArgs.hapPath.empty() || aotArgs.outputPath.empty()) {
59         APP_LOGE("aotArgs check failed");
60         return false;
61     }
62     if (aotArgs.compileMode == Constants::COMPILE_PARTIAL && aotArgs.arkProfilePath.empty()) {
63         APP_LOGE("partial mode, arkProfilePath can't be empty");
64         return false;
65     }
66     return true;
67 }
68 
GetAbcFileInfo(const std::string & hapPath,uint32_t & offset,uint32_t & length) const69 bool AOTExecutor::GetAbcFileInfo(const std::string &hapPath, uint32_t &offset, uint32_t &length) const
70 {
71     BundleExtractor extractor(hapPath);
72     if (!extractor.Init()) {
73         APP_LOGE("init BundleExtractor failed");
74         return false;
75     }
76     if (!extractor.GetFileInfo(ABC_RELATIVE_PATH, offset, length)) {
77         APP_LOGE("GetFileInfo failed");
78         return false;
79     }
80     APP_LOGD("GetFileInfo success, offset : %{public}u, length : %{public}u", offset, length);
81     return true;
82 }
83 
PrepareArgs(const AOTArgs & aotArgs,AOTArgs & completeArgs) const84 ErrCode AOTExecutor::PrepareArgs(const AOTArgs &aotArgs, AOTArgs &completeArgs) const
85 {
86     APP_LOGD("PrepareArgs begin");
87     if (!CheckArgs(aotArgs)) {
88         APP_LOGE("param check failed");
89         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
90     }
91     uint32_t offset = 0;
92     uint32_t length = 0;
93     if (!GetAbcFileInfo(aotArgs.hapPath, offset, length)) {
94         APP_LOGE("GetAbcFileInfo failed");
95         return ERR_APPEXECFWK_INSTALLD_AOT_ABC_NOT_EXIST;
96     }
97     completeArgs = aotArgs;
98     completeArgs.offset = offset;
99     completeArgs.length = length;
100     APP_LOGD("PrepareArgs success");
101     return ERR_OK;
102 }
103 
ExecuteInChildProcess(const AOTArgs & aotArgs) const104 void AOTExecutor::ExecuteInChildProcess(const AOTArgs &aotArgs) const
105 {
106     APP_LOGD("ExecuteInChildProcess, args : %{public}s", aotArgs.ToString().c_str());
107     std::vector<std::string> tmpVector = {
108         "/system/bin/ark_aot_compiler",
109         "--target-compiler-mode=" + aotArgs.compileMode,
110         "--hap-path=" + aotArgs.hapPath,
111         "--aot-file=" + aotArgs.outputPath + Constants::PATH_SEPARATOR + aotArgs.moduleName,
112         "--hap-abc-offset=" + DecToHex(aotArgs.offset),
113         "--hap-abc-size=" + DecToHex(aotArgs.length),
114     };
115     if (aotArgs.compileMode == Constants::COMPILE_PARTIAL) {
116         tmpVector.emplace_back("--compiler-pgo-profiler-path=" + aotArgs.arkProfilePath);
117     }
118     tmpVector.emplace_back(aotArgs.hapPath + Constants::PATH_SEPARATOR + ABC_RELATIVE_PATH);
119 
120     std::vector<const char*> argv;
121     argv.reserve(tmpVector.size() + 1);
122     for (const auto &arg : tmpVector) {
123         argv.emplace_back(arg.c_str());
124     }
125     argv.emplace_back(nullptr);
126     APP_LOGD("argv size : %{public}zu", argv.size());
127     for (const auto &arg : argv) {
128         APP_LOGD("%{public}s", arg);
129     }
130     execv(argv[0], const_cast<char* const*>(argv.data()));
131     APP_LOGE("execv failed : %{public}s", strerror(errno));
132     exit(-1);
133 }
134 
ExecuteInParentProcess(pid_t childPid,ErrCode & ret) const135 void AOTExecutor::ExecuteInParentProcess(pid_t childPid, ErrCode &ret) const
136 {
137     int status;
138     waitpid(childPid, &status, 0);
139     if (WIFEXITED(status)) {
140         int exit_status = WEXITSTATUS(status);
141         APP_LOGI("child process exited with status: %{public}d", exit_status);
142         ret = exit_status == 0 ? ERR_OK : ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
143     } else if (WIFSIGNALED(status)) {
144         int signal_number = WTERMSIG(status);
145         APP_LOGW("child process terminated by signal: %{public}d", signal_number);
146         ret = ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
147     }
148 }
149 
ExecuteAOT(const AOTArgs & aotArgs,ErrCode & ret) const150 void AOTExecutor::ExecuteAOT(const AOTArgs &aotArgs, ErrCode &ret) const
151 {
152     AOTArgs completeArgs;
153     ret = PrepareArgs(aotArgs, completeArgs);
154     if (ret != ERR_OK) {
155         APP_LOGE("PrepareArgs failed");
156         return;
157     }
158     std::lock_guard<std::mutex> lock(mutex_);
159     APP_LOGD("begin to fork");
160     pid_t pid = fork();
161     if (pid == -1) {
162         APP_LOGE("fork process failed : %{public}s", strerror(errno));
163         ret = ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
164     } else if (pid == 0) {
165         ExecuteInChildProcess(completeArgs);
166     } else {
167         ExecuteInParentProcess(pid, ret);
168     }
169 }
170 }  // namespace AppExecFwk
171 }  // namespace OHOS
172