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 <filesystem>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include "app_log_wrapper.h"
31 #include "bundle_constants.h"
32 #include "bundle_extractor.h"
33 #include <nlohmann/json.hpp>
34
35 namespace OHOS {
36 namespace AppExecFwk {
37 namespace {
38 constexpr const char* ABC_RELATIVE_PATH = "ets/modules.abc";
39 constexpr const char* HEX_PREFIX = "0x";
40 constexpr const char* BUNDLE_NAME = "bundleName";
41 constexpr const char* MODULE_NAME = "moduleName";
42 constexpr const char* PKG_PATH = "pkgPath";
43 constexpr const char* ABC_NAME = "abcName";
44 constexpr const char* ABC_OFFSET = "abcOffset";
45 constexpr const char* ABC_SIZE = "abcSize";
46 constexpr const char* PROCESS_UID = "processUid";
47 constexpr const char* BUNDLE_UID = "bundleUid";
48 constexpr const char* APP_IDENTIFIER = "appIdentifier";
49 constexpr const char* IS_ENCRYPTED_BUNDLE = "isEncryptedBundle";
50 constexpr const char* PGO_DIR = "pgoDir";
51 }
52
GetInstance()53 AOTExecutor& AOTExecutor::GetInstance()
54 {
55 static AOTExecutor executor;
56 return executor;
57 }
58
DecToHex(uint32_t decimal) const59 std::string AOTExecutor::DecToHex(uint32_t decimal) const
60 {
61 APP_LOGD("DecToHex begin, decimal : %{public}u", decimal);
62 std::stringstream ss;
63 ss << std::hex << decimal;
64 std::string hexString = HEX_PREFIX + ss.str();
65 APP_LOGD("hex : %{public}s", hexString.c_str());
66 return hexString;
67 }
68
CheckArgs(const AOTArgs & aotArgs) const69 bool AOTExecutor::CheckArgs(const AOTArgs &aotArgs) const
70 {
71 if (aotArgs.compileMode.empty() || aotArgs.hapPath.empty() || aotArgs.outputPath.empty()) {
72 APP_LOGE("aotArgs check failed");
73 return false;
74 }
75 if (aotArgs.compileMode == Constants::COMPILE_PARTIAL && aotArgs.arkProfilePath.empty()) {
76 APP_LOGE("partial mode, arkProfilePath can't be empty");
77 return false;
78 }
79 return true;
80 }
81
GetAbcFileInfo(const std::string & hapPath,uint32_t & offset,uint32_t & length) const82 bool AOTExecutor::GetAbcFileInfo(const std::string &hapPath, uint32_t &offset, uint32_t &length) const
83 {
84 BundleExtractor extractor(hapPath);
85 if (!extractor.Init()) {
86 APP_LOGE("init BundleExtractor failed");
87 return false;
88 }
89 if (!extractor.GetFileInfo(ABC_RELATIVE_PATH, offset, length)) {
90 APP_LOGE("GetFileInfo failed");
91 return false;
92 }
93 APP_LOGD("GetFileInfo success, offset : %{public}u, length : %{public}u", offset, length);
94 return true;
95 }
96
PrepareArgs(const AOTArgs & aotArgs,AOTArgs & completeArgs) const97 ErrCode AOTExecutor::PrepareArgs(const AOTArgs &aotArgs, AOTArgs &completeArgs) const
98 {
99 APP_LOGD("PrepareArgs begin");
100 if (!CheckArgs(aotArgs)) {
101 APP_LOGE("param check failed");
102 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
103 }
104 completeArgs = aotArgs;
105 if (!GetAbcFileInfo(completeArgs.hapPath, completeArgs.offset, completeArgs.length)) {
106 APP_LOGE("GetAbcFileInfo failed");
107 return ERR_APPEXECFWK_INSTALLD_AOT_ABC_NOT_EXIST;
108 }
109 // handle hsp
110 for (auto &hspInfo : completeArgs.hspVector) {
111 (void)GetAbcFileInfo(hspInfo.hapPath, hspInfo.offset, hspInfo.length);
112 }
113 APP_LOGD("PrepareArgs success");
114 return ERR_OK;
115 }
116
ExecuteInChildProcess(const AOTArgs & aotArgs) const117 void AOTExecutor::ExecuteInChildProcess(const AOTArgs &aotArgs) const
118 {
119 APP_LOGD("ExecuteInChildProcess, args : %{public}s", aotArgs.ToString().c_str());
120
121 /* obtain the uid of current process */
122 int32_t currentProcessUid = static_cast<int32_t>(getuid());
123
124 std::filesystem::path filePath(aotArgs.arkProfilePath);
125 nlohmann::json subject;
126 subject[BUNDLE_NAME] = aotArgs.bundleName;
127 subject[MODULE_NAME] = aotArgs.moduleName;
128 subject[PKG_PATH] = aotArgs.hapPath;
129 subject[ABC_NAME] = ABC_RELATIVE_PATH;
130 subject[ABC_OFFSET] = DecToHex(aotArgs.offset);
131 subject[ABC_SIZE] = DecToHex(aotArgs.length);
132 subject[PROCESS_UID] = DecToHex(currentProcessUid);
133 subject[BUNDLE_UID] = DecToHex(aotArgs.bundleUid);
134 subject[APP_IDENTIFIER] = aotArgs.appIdentifier;
135 subject[IS_ENCRYPTED_BUNDLE] = DecToHex(aotArgs.isEncryptedBundle);
136 subject[PGO_DIR] = filePath.parent_path().string();
137
138 nlohmann::json objectArray = nlohmann::json::array();
139 for (const auto &hspInfo : aotArgs.hspVector) {
140 nlohmann::json object;
141 object[BUNDLE_NAME] = hspInfo.bundleName;
142 object[MODULE_NAME] = hspInfo.moduleName;
143 object[PKG_PATH] = hspInfo.hapPath;
144 object[ABC_NAME] = ABC_RELATIVE_PATH;
145 object[ABC_OFFSET] = DecToHex(hspInfo.offset);
146 object[ABC_SIZE] = DecToHex(hspInfo.length);
147 objectArray.push_back(object);
148 }
149
150 std::vector<std::string> tmpVector = {
151 "/system/bin/ark_aot_compiler",
152 "--target-compiler-mode=" + aotArgs.compileMode,
153 "--aot-file=" + aotArgs.outputPath + Constants::PATH_SEPARATOR + aotArgs.moduleName,
154 "--compiler-pkg-info=" + subject.dump(),
155 "--compiler-external-pkg-info=" + objectArray.dump(),
156 };
157 tmpVector.emplace_back(aotArgs.hapPath + Constants::PATH_SEPARATOR + ABC_RELATIVE_PATH);
158
159 std::vector<const char*> argv;
160 argv.reserve(tmpVector.size() + 1);
161 for (const auto &arg : tmpVector) {
162 argv.emplace_back(arg.c_str());
163 }
164 argv.emplace_back(nullptr);
165 APP_LOGD("argv size : %{public}zu", argv.size());
166 for (const auto &arg : argv) {
167 APP_LOGD("%{public}s", arg);
168 }
169 execv(argv[0], const_cast<char* const*>(argv.data()));
170 APP_LOGE("execv failed : %{public}s", strerror(errno));
171 exit(-1);
172 }
173
ExecuteInParentProcess(pid_t childPid,ErrCode & ret) const174 void AOTExecutor::ExecuteInParentProcess(pid_t childPid, ErrCode &ret) const
175 {
176 int status;
177 waitpid(childPid, &status, 0);
178 if (WIFEXITED(status)) {
179 int exit_status = WEXITSTATUS(status);
180 APP_LOGI("child process exited with status: %{public}d", exit_status);
181 ret = exit_status == 0 ? ERR_OK : ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
182 } else if (WIFSIGNALED(status)) {
183 int signal_number = WTERMSIG(status);
184 APP_LOGW("child process terminated by signal: %{public}d", signal_number);
185 ret = ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
186 }
187 }
188
ExecuteAOT(const AOTArgs & aotArgs,ErrCode & ret) const189 void AOTExecutor::ExecuteAOT(const AOTArgs &aotArgs, ErrCode &ret) const
190 {
191 AOTArgs completeArgs;
192 ret = PrepareArgs(aotArgs, completeArgs);
193 if (ret != ERR_OK) {
194 APP_LOGE("PrepareArgs failed");
195 return;
196 }
197 std::lock_guard<std::mutex> lock(mutex_);
198 APP_LOGD("begin to fork");
199 pid_t pid = fork();
200 if (pid == -1) {
201 APP_LOGE("fork process failed : %{public}s", strerror(errno));
202 ret = ERR_APPEXECFWK_INSTALLD_AOT_EXECUTE_FAILED;
203 } else if (pid == 0) {
204 ExecuteInChildProcess(completeArgs);
205 } else {
206 ExecuteInParentProcess(pid, ret);
207 }
208 }
209 } // namespace AppExecFwk
210 } // namespace OHOS
211