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_stub.h"
17#include "ecmascript/log_wrapper.h"
18#include "hitrace_meter.h"
19
20namespace OHOS::ArkCompiler {
21int32_t AotCompilerInterfaceStub::OnRemoteRequest(
22    uint32_t code,
23    MessageParcel& data,
24    MessageParcel& reply,
25    MessageOption& option)
26{
27    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
28    std::u16string localDescriptor = GetDescriptor();
29    std::u16string remoteDescriptor = data.ReadInterfaceToken();
30    if (localDescriptor != remoteDescriptor) {
31        return ERR_TRANSACTION_FAILED;
32    }
33    switch (code) {
34        case COMMAND_AOT_COMPILER: {
35            return CommandAOTCompiler(data, reply);
36        }
37        case COMMAND_STOP_AOT_COMPILER: {
38            return CommandStopAOTCompiler(reply);
39        }
40        case COMMAND_GET_AOT_VERSION: {
41            return CommandGetAOTVersion(reply);
42        }
43        case COMMAND_NEED_RE_COMPILE: {
44            return CommandNeedReCompile(data, reply);
45        }
46        default:
47            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48    }
49    return ERR_TRANSACTION_FAILED;
50}
51
52int32_t AotCompilerInterfaceStub::CommandAOTCompiler(MessageParcel &data,
53                                                     MessageParcel &reply)
54{
55    std::unordered_map<std::string, std::string> argsMap;
56    int32_t argsMapSize = data.ReadInt32();
57    if (static_cast<unsigned long>(argsMapSize) > mapMaxSize) {
58        LOG_SA(ERROR) << "The map size exceeds ths security limit!";
59        return ERR_INVALID_DATA;
60    }
61    for (int32_t i = 0; i < argsMapSize; ++i) {
62        std::string key = Str16ToStr8(data.ReadString16());
63        std::string value = Str16ToStr8(data.ReadString16());
64        argsMap[key] = value;
65    }
66    std::vector<int16_t> sigData;
67    ErrCode errCode = AotCompiler(argsMap, sigData);
68    if (!reply.WriteInt32(errCode)) {
69        LOG_SA(ERROR) << "Write Int32 failed!";
70        return ERR_INVALID_VALUE;
71    }
72    if (SUCCEEDED(errCode)) {
73        if (sigData.size() > vectorMaxSize) {
74            LOG_SA(ERROR) << "The vector size exceeds the security limit!";
75            return ERR_INVALID_DATA;
76        }
77        reply.WriteInt32(sigData.size());
78        for (auto it = sigData.begin(); it != sigData.end(); ++it) {
79            if (!reply.WriteInt32(*it)) {
80                LOG_SA(ERROR) << "Write sigData array failed!";
81                return ERR_INVALID_DATA;
82            }
83        }
84    }
85    return ERR_NONE;
86}
87
88int32_t AotCompilerInterfaceStub::CommandStopAOTCompiler(MessageParcel& reply)
89{
90    ErrCode errCode = StopAotCompiler();
91    if (!reply.WriteInt32(errCode)) {
92        LOG_SA(ERROR) << "Write Int32 failed!";
93        return ERR_INVALID_VALUE;
94    }
95    return ERR_NONE;
96}
97
98int32_t AotCompilerInterfaceStub::CommandGetAOTVersion(MessageParcel& reply)
99{
100    std::string sigData;
101    ErrCode errCode = GetAOTVersion(sigData);
102    if (!reply.WriteInt32(errCode)) {
103        LOG_SA(ERROR) << "Write Int32 failed!";
104        return ERR_INVALID_VALUE;
105    }
106    if (SUCCEEDED(errCode)) {
107        reply.WriteString16(Str8ToStr16(sigData));
108    }
109
110    return ERR_NONE;
111}
112
113int32_t AotCompilerInterfaceStub::CommandNeedReCompile(MessageParcel& data,
114                                                       MessageParcel& reply)
115{
116    std::string regs = Str16ToStr8(data.ReadString16());
117    bool sigData;
118    ErrCode errCode = NeedReCompile(regs, sigData);
119    if (!reply.WriteInt32(errCode)) {
120        LOG_SA(ERROR) << "Write Int32 failed!";
121        return ERR_INVALID_VALUE;
122    }
123    if (SUCCEEDED(errCode)) {
124        reply.WriteBool(sigData);
125    }
126    return ERR_NONE;
127}
128
129} // namespace OHOS::ArkCompiler
130