1 /*
2  * Copyright (c) 2022-2022 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 "perusersession_fuzzer.h"
17 
18 #define private public
19 #define protected public
20 #include "peruser_session.h"
21 #undef private
22 
23 #include <string_ex.h>
24 
25 #include <cstddef>
26 #include <cstdint>
27 #include <memory>
28 
29 #include "global.h"
30 #include "i_input_method_agent.h"
31 #include "i_input_method_core.h"
32 #include "input_client_proxy.h"
33 #include "input_client_stub.h"
34 #include "input_method_ability.h"
35 #include "input_method_agent_proxy.h"
36 #include "input_method_agent_stub.h"
37 #include "input_method_core_proxy.h"
38 #include "input_method_core_stub.h"
39 #include "input_method_info.h"
40 #include "input_method_property.h"
41 #include "iremote_broker.h"
42 #include "message_parcel.h"
43 #include "input_method_types.h"
44 
45 using namespace OHOS::MiscServices;
46 namespace OHOS {
47 constexpr size_t THRESHOLD = 10;
48 constexpr int32_t MAIN_USER_ID = 100;
49 
ConvertToUint32(const uint8_t * ptr)50 uint32_t ConvertToUint32(const uint8_t *ptr)
51 {
52     if (ptr == nullptr) {
53         return 0;
54     }
55     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
56     return bigVar;
57 }
58 
InitializeClientInfo(InputClientInfo & clientInfo)59 bool InitializeClientInfo(InputClientInfo &clientInfo)
60 {
61     sptr<IInputClient> clientStub = new (std::nothrow) InputClientStub();
62     if (clientStub == nullptr) {
63         IMSA_HILOGE("failed to create client");
64         return false;
65     }
66     sptr<InputDeathRecipient> deathRecipient = new (std::nothrow) InputDeathRecipient();
67     if (deathRecipient == nullptr) {
68         IMSA_HILOGE("failed to new deathRecipient");
69         return false;
70     }
71     clientInfo = { .userID = MAIN_USER_ID, .client = clientStub, .deathRecipient = deathRecipient };
72     return true;
73 }
74 
FuzzPerUserSession(const uint8_t * rawData,size_t size)75 bool FuzzPerUserSession(const uint8_t *rawData, size_t size)
76 {
77     Property property;
78     SubProperty subProperty;
79     InputClientInfo clientInfo;
80     if (!InitializeClientInfo(clientInfo)) {
81         return false;
82     }
83     auto client = iface_cast<IInputClient>(clientInfo.client->AsObject());
84     sptr<InputMethodCoreStub> coreStub = new (std::nothrow) InputMethodCoreStub();
85     if (coreStub == nullptr) {
86         return false;
87     }
88     auto core = iface_cast<IInputMethodCore>(coreStub->AsObject());
89     sptr<InputMethodAgentStub> agentStub = new (std::nothrow) InputMethodAgentStub();
90     if (agentStub == nullptr) {
91         return false;
92     }
93     auto agent = iface_cast<IInputMethodAgent>(agentStub);
94     static std::shared_ptr<PerUserSession> userSessions = std::make_shared<PerUserSession>(MAIN_USER_ID);
95 
96     userSessions->OnRegisterProxyIme(core, agent->AsObject());
97     int32_t type = 4;
98     userSessions->OnUnRegisteredProxyIme(static_cast<UnRegisteredType>(type), core);
99     userSessions->IsProxyImeEnable();
100 
101     userSessions->OnPrepareInput(clientInfo);
102     userSessions->OnSetCoreAndAgent(core, agent->AsObject());
103     userSessions->OnShowCurrentInput();
104     sptr<IRemoteObject> agentObject = nullptr;
105     clientInfo.isShowKeyboard = false;
106     userSessions->OnStartInput(clientInfo, agentObject);
107     clientInfo.isShowKeyboard = true;
108     userSessions->OnStartInput(clientInfo, agentObject);
109     userSessions->NotifyImeChangeToClients(property, subProperty);
110     userSessions->OnHideCurrentInput();
111     userSessions->OnHideInput(client);
112     userSessions->OnReleaseInput(client);
113     return true;
114 }
115 } // namespace OHOS
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
118 {
119     if (size < OHOS::THRESHOLD) {
120         return 0;
121     }
122     /* Run your code on data */
123     OHOS::FuzzPerUserSession(data, size);
124     return 0;
125 }