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 #include "aot_compiler_interface_proxy.h"
17 #include "ecmascript/log_wrapper.h"
18 #include "hitrace_meter.h"
19
20 namespace OHOS::ArkCompiler {
AotCompiler(const std::unordered_map<std::string,std::string> & argsMap,std::vector<int16_t> & sigData)21 ErrCode AotCompilerInterfaceProxy::AotCompiler(
22 const std::unordered_map<std::string, std::string>& argsMap,
23 std::vector<int16_t>& sigData)
24 {
25 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
26 MessageParcel data;
27 MessageParcel reply;
28 MessageOption option(MessageOption::TF_SYNC);
29
30 if (!data.WriteInterfaceToken(GetDescriptor())) {
31 LOG_SA(ERROR) << "Write interface token failed!";
32 return ERR_INVALID_VALUE;
33 }
34
35 if (argsMap.size() > mapMaxSize) {
36 LOG_SA(ERROR) << "The map size exceeds the security limit!";
37 return ERR_INVALID_DATA;
38 }
39
40 data.WriteInt32(argsMap.size());
41 for (auto it = argsMap.begin(); it != argsMap.end(); ++it) {
42 if (!data.WriteString16(Str8ToStr16((it->first)))) {
43 LOG_SA(ERROR) << "Write [(it->first)] failed!";
44 return ERR_INVALID_DATA;
45 }
46 if (!data.WriteString16(Str8ToStr16((it->second)))) {
47 LOG_SA(ERROR) << "Write [(it->second)] failed!";
48 return ERR_INVALID_DATA;
49 }
50 }
51
52 sptr<IRemoteObject> remote = Remote();
53 if (remote == nullptr) {
54 LOG_SA(ERROR) << "Remote is nullptr!";
55 return ERR_INVALID_DATA;
56 }
57 int32_t result = remote->SendRequest(COMMAND_AOT_COMPILER, data, reply, option);
58 if (FAILED(result)) {
59 LOG_SA(ERROR) << "Send request failed!";
60 return result;
61 }
62
63 ErrCode errCode = reply.ReadInt32();
64 if (FAILED(errCode)) {
65 LOG_SA(ERROR) << "Read Int32 failed!";
66 return errCode;
67 }
68
69 int32_t sigDataSize = reply.ReadInt32();
70 if (static_cast<unsigned long>(sigDataSize) > vectorMaxSize) {
71 LOG_SA(ERROR) << "The vector/array size exceeds the security limit!";
72 return ERR_INVALID_DATA;
73 }
74 for (int32_t i = 0; i < sigDataSize; ++i) {
75 int16_t value = reply.ReadInt16();
76 sigData.push_back(value);
77 }
78 return ERR_OK;
79 }
80
StopAotCompiler()81 ErrCode AotCompilerInterfaceProxy::StopAotCompiler()
82 {
83 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
84 MessageParcel data;
85 MessageParcel reply;
86 MessageOption option(MessageOption::TF_SYNC);
87
88 if (!data.WriteInterfaceToken(GetDescriptor())) {
89 LOG_SA(ERROR) << "Write interface token failed!";
90 return ERR_INVALID_VALUE;
91 }
92
93 sptr<IRemoteObject> remote = Remote();
94 if (remote == nullptr) {
95 LOG_SA(ERROR) << "Remote is nullptr!";
96 return ERR_INVALID_DATA;
97 }
98 int32_t result = remote->SendRequest(COMMAND_STOP_AOT_COMPILER, data, reply, option);
99 if (FAILED(result)) {
100 LOG_SA(ERROR) << "Send request failed!";
101 return result;
102 }
103
104 ErrCode errCode = reply.ReadInt32();
105 if (FAILED(errCode)) {
106 LOG_SA(ERROR) << "Read Int32 failed!";
107 return errCode;
108 }
109
110 return ERR_OK;
111 }
112
GetAOTVersion(std::string & sigData)113 ErrCode AotCompilerInterfaceProxy::GetAOTVersion(std::string& sigData)
114 {
115 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
116 MessageParcel data;
117 MessageParcel reply;
118 MessageOption option(MessageOption::TF_SYNC);
119
120 if (!data.WriteInterfaceToken(GetDescriptor())) {
121 LOG_SA(ERROR) << "Write interface token failed!";
122 return ERR_INVALID_VALUE;
123 }
124
125 sptr<IRemoteObject> remote = Remote();
126 if (remote == nullptr) {
127 LOG_SA(ERROR) << "Remote is nullptr!";
128 return ERR_INVALID_DATA;
129 }
130 int32_t result = remote->SendRequest(COMMAND_GET_AOT_VERSION, data, reply, option);
131 if (FAILED(result)) {
132 LOG_SA(ERROR) << "Send request failed!";
133 return result;
134 }
135
136 ErrCode errCode = reply.ReadInt32();
137 if (FAILED(errCode)) {
138 LOG_SA(ERROR) << "Read Int32 failed!";
139 return errCode;
140 }
141
142 sigData = Str16ToStr8(reply.ReadString16());
143
144 return ERR_OK;
145 }
146
NeedReCompile(const std::string & args,bool & sigData)147 ErrCode AotCompilerInterfaceProxy::NeedReCompile(const std::string& args, bool& sigData)
148 {
149 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
150 MessageParcel data;
151 MessageParcel reply;
152 MessageOption option(MessageOption::TF_SYNC);
153
154 if (!data.WriteInterfaceToken(GetDescriptor())) {
155 LOG_SA(ERROR) << "Write interface token failed!";
156 return ERR_INVALID_VALUE;
157 }
158
159 data.WriteString16(Str8ToStr16(args));
160
161 sptr<IRemoteObject> remote = Remote();
162 if (remote == nullptr) {
163 LOG_SA(ERROR) << "Remote is nullptr!";
164 return ERR_INVALID_DATA;
165 }
166 int32_t result = remote->SendRequest(COMMAND_NEED_RE_COMPILE, data, reply, option);
167 if (FAILED(result)) {
168 LOG_SA(ERROR) << "Send request failed!";
169 return result;
170 }
171
172 ErrCode errCode = reply.ReadInt32();
173 if (FAILED(errCode)) {
174 LOG_SA(ERROR) << "Read Int32 failed!";
175 return errCode;
176 }
177
178 sigData = reply.ReadBool();
179
180 return ERR_OK;
181 }
182 } // namespace OHOS::ArkCompiler
183