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_method_ability.h"
34 #include "input_method_agent_proxy.h"
35 #include "input_method_agent_stub.h"
36 #include "input_method_core_proxy.h"
37 #include "input_method_info.h"
38 #include "input_method_property.h"
39 #include "iremote_broker.h"
40 #include "message_parcel.h"
41
42 using namespace OHOS::MiscServices;
43 namespace OHOS {
44 constexpr size_t THRESHOLD = 10;
45 constexpr int32_t MAIN_USER_ID = 100;
46
ConvertToUint32(const uint8_t * ptr)47 uint32_t ConvertToUint32(const uint8_t *ptr)
48 {
49 if (ptr == nullptr) {
50 return 0;
51 }
52 uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
53 return bigVar;
54 }
55
InitializeClientInfo(InputClientInfo & clientInfo)56 bool InitializeClientInfo(InputClientInfo &clientInfo)
57 {
58 auto clientStub = new (std::nothrow) InputClientStub();
59 if (clientStub == nullptr) {
60 IMSA_HILOGE("failed to create client");
61 return false;
62 }
63 auto deathRecipient = new (std::nothrow) InputDeathRecipient();
64 if (deathRecipient == nullptr) {
65 IMSA_HILOGE("failed to new deathRecipient");
66 delete clientStub;
67 return ErrorCode::ERROR_EX_NULL_POINTER;
68 }
69 clientInfo = { .userID = MAIN_USER_ID, .client = clientStub, .deathRecipient = deathRecipient };
70 return true;
71 }
72
FuzzPerUserSession(const uint8_t * rawData,size_t size)73 bool FuzzPerUserSession(const uint8_t *rawData, size_t size)
74 {
75 std::string str(rawData, rawData + size);
76 Property property;
77 SubProperty subProperty;
78 InputClientInfo clientInfo;
79 if (!InitializeClientInfo(clientInfo)) {
80 return false;
81 }
82 auto client = iface_cast<IInputClient>(clientInfo.client->AsObject());
83 sptr<InputMethodCoreStub> coreStub = new (std::nothrow) InputMethodCoreStub(MAIN_USER_ID);
84 if (coreStub == nullptr) {
85 return false;
86 }
87 auto core = iface_cast<IInputMethodCore>(coreStub->AsObject());
88 sptr<InputMethodAgentStub> agentStub = new (std::nothrow) InputMethodAgentStub();
89 if (agentStub == nullptr) {
90 return false;
91 }
92 auto agent = iface_cast<IInputMethodAgent>(agentStub);
93 static std::shared_ptr<PerUserSession> userSessions = std::make_shared<PerUserSession>(MAIN_USER_ID);
94 userSessions->SetImsCore(0, core);
95 userSessions->SetAgent(agent);
96
97 userSessions->OnPrepareInput(clientInfo);
98 userSessions->OnSetCoreAndAgent(core, agent);
99 userSessions->OnShowKeyboardSelf();
100 userSessions->OnStartInput(client, false, false);
101 userSessions->OnStartInput(client, true, false);
102 userSessions->OnSwitchIme(property, subProperty, false);
103 userSessions->OnSwitchIme(property, subProperty, true);
104 userSessions->OnHideKeyboardSelf();
105 userSessions->OnStopInput(client);
106 userSessions->OnReleaseInput(client);
107 userSessions->StopInputService(str);
108 return true;
109 }
110 } // namespace OHOS
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
113 {
114 if (size < OHOS::THRESHOLD) {
115 return 0;
116 }
117 /* Run your code on data */
118 OHOS::FuzzPerUserSession(data, size);
119 return 0;
120 }