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_args.h"
17
18 #include <algorithm>
19
20 #include "parcel_macro.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr uint32_t MAX_HSP_VECTOR_SIZE = 10000;
27 }
ReadFromParcel(Parcel & parcel)28 bool HspInfo::ReadFromParcel(Parcel &parcel)
29 {
30 std::u16string bundleNameVal;
31 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, bundleNameVal);
32 bundleName = Str16ToStr8(bundleNameVal);
33 std::u16string moduleNameVal;
34 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, moduleNameVal);
35 moduleName = Str16ToStr8(moduleNameVal);
36 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
37 std::u16string hapPathVal;
38 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, hapPathVal);
39 hapPath = Str16ToStr8(hapPathVal);
40 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, offset);
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, length);
42 return true;
43 }
44
Marshalling(Parcel & parcel) const45 bool HspInfo::Marshalling(Parcel &parcel) const
46 {
47 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
48 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
49 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
50 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapPath));
51 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, offset);
52 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, length);
53 return true;
54 }
55
Unmarshalling(Parcel & parcel)56 HspInfo *HspInfo::Unmarshalling(Parcel &parcel)
57 {
58 HspInfo *hspInfo = new (std::nothrow) HspInfo();
59 if (hspInfo && !hspInfo->ReadFromParcel(parcel)) {
60 APP_LOGE("read HspInfo from parcel failed");
61 delete hspInfo;
62 hspInfo = nullptr;
63 }
64 return hspInfo;
65 }
66
ToString() const67 std::string HspInfo::ToString() const
68 {
69 return "[ bundleName = " + bundleName
70 + ", moduleName = " + moduleName
71 + ", versionCode = " + std::to_string(versionCode)
72 + ", hapPath = " + hapPath
73 + ", offset = " + std::to_string(offset)
74 + ", length = " + std::to_string(length) + "]";
75 }
76
ReadFromParcel(Parcel & parcel)77 bool AOTArgs::ReadFromParcel(Parcel &parcel)
78 {
79 std::u16string bundleNameVal;
80 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, bundleNameVal);
81 bundleName = Str16ToStr8(bundleNameVal);
82 std::u16string moduleNameVal;
83 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, moduleNameVal);
84 moduleName = Str16ToStr8(moduleNameVal);
85 std::u16string compileModeVal;
86 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, compileModeVal);
87 compileMode = Str16ToStr8(compileModeVal);
88 std::u16string hapPathVal;
89 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, hapPathVal);
90 hapPath = Str16ToStr8(hapPathVal);
91 std::u16string coreLibPathVal;
92 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, coreLibPathVal);
93 coreLibPath = Str16ToStr8(coreLibPathVal);
94 std::u16string outputPathVal;
95 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, outputPathVal);
96 outputPath = Str16ToStr8(outputPathVal);
97 std::u16string arkProfilePathVal;
98 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, arkProfilePathVal);
99 arkProfilePath = Str16ToStr8(arkProfilePathVal);
100 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, offset);
101 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, length);
102 uint32_t hspVectorSize = 0;
103 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, hspVectorSize);
104 hspVectorSize = std::min(hspVectorSize, MAX_HSP_VECTOR_SIZE);
105 for (uint32_t i = 0; i < hspVectorSize; ++i) {
106 std::unique_ptr<HspInfo> hspInfoPtr(parcel.ReadParcelable<HspInfo>());
107 if (!hspInfoPtr) {
108 APP_LOGE("read HspInfo failed");
109 return false;
110 }
111 hspVector.emplace_back(*hspInfoPtr);
112 }
113 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, bundleUid);
114 std::u16string appIdentifilerVal;
115 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, appIdentifilerVal);
116 appIdentifier = Str16ToStr8(appIdentifilerVal);
117 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, isEncryptedBundle);
118 return true;
119 }
120
Marshalling(Parcel & parcel) const121 bool AOTArgs::Marshalling(Parcel &parcel) const
122 {
123 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
124 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
125 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(compileMode));
126 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapPath));
127 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(coreLibPath));
128 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(outputPath));
129 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(arkProfilePath));
130 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, offset);
131 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, length);
132 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, hspVector.size());
133 for (const auto &hspInfo : hspVector) {
134 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &hspInfo);
135 }
136 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, bundleUid);
137 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appIdentifier));
138 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, isEncryptedBundle);
139 return true;
140 }
141
Unmarshalling(Parcel & parcel)142 AOTArgs *AOTArgs::Unmarshalling(Parcel &parcel)
143 {
144 AOTArgs *aotArgs = new (std::nothrow) AOTArgs();
145 if (aotArgs && !aotArgs->ReadFromParcel(parcel)) {
146 APP_LOGE("read AOTArgs from parcel failed");
147 delete aotArgs;
148 aotArgs = nullptr;
149 }
150 return aotArgs;
151 }
152
ToString() const153 std::string AOTArgs::ToString() const
154 {
155 std::string ret = "[ bundleName = " + bundleName
156 + ", moduleName = " + moduleName
157 + ", compileMode = " + compileMode
158 + ", hapPath = " + hapPath
159 + ", coreLibPath = " + coreLibPath
160 + ", outputPath = " + outputPath
161 + ", arkProfilePath = " + arkProfilePath
162 + ", offset = " + std::to_string(offset)
163 + ", length = " + std::to_string(length)
164 + ", bundleUid = " + std::to_string(bundleUid)
165 + ", appIdentifier = " + appIdentifier
166 + ", isEncryptedBundle = " + std::to_string(isEncryptedBundle) + "]";
167 ret.append(" hspVector = ");
168 for (const auto &hspInfo : hspVector) {
169 ret.append(hspInfo.ToString());
170 }
171 return ret;
172 }
173 } // namespace AppExecFwk
174 } // namespace OHOS
175