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 #include "unRegistered_type.h"
42
43 using namespace OHOS::MiscServices;
44 namespace OHOS {
45 constexpr size_t THRESHOLD = 10;
46 constexpr int32_t MAIN_USER_ID = 100;
47
ConvertToUint32(const uint8_t * ptr)48 uint32_t ConvertToUint32(const uint8_t *ptr)
49 {
50 if (ptr == nullptr) {
51 return 0;
52 }
53 uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
54 return bigVar;
55 }
56
InitializeClientInfo(InputClientInfo & clientInfo)57 bool InitializeClientInfo(InputClientInfo &clientInfo)
58 {
59 auto clientStub = new (std::nothrow) InputClientStub();
60 if (clientStub == nullptr) {
61 IMSA_HILOGE("failed to create client");
62 return false;
63 }
64 auto deathRecipient = new (std::nothrow) InputDeathRecipient();
65 if (deathRecipient == nullptr) {
66 IMSA_HILOGE("failed to new deathRecipient");
67 delete clientStub;
68 return ErrorCode::ERROR_EX_NULL_POINTER;
69 }
70 clientInfo = { .userID = MAIN_USER_ID, .client = clientStub, .deathRecipient = deathRecipient };
71 return true;
72 }
73
FuzzPerUserSession(const uint8_t * rawData,size_t size)74 bool FuzzPerUserSession(const uint8_t *rawData, size_t size)
75 {
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();
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
95 userSessions->OnRegisterProxyIme(core, agent);
96 int32_t type = 4;
97 userSessions->OnUnRegisteredProxyIme(static_cast<UnRegisteredType>(type), core);
98 userSessions->IsProxyImeEnable();
99
100 userSessions->OnPrepareInput(clientInfo);
101 userSessions->OnSetCoreAndAgent(core, agent);
102 userSessions->OnShowCurrentInput();
103 sptr<IRemoteObject> agentObject = nullptr;
104 clientInfo.isShowKeyboard = false;
105 userSessions->OnStartInput(clientInfo, agentObject);
106 clientInfo.isShowKeyboard = true;
107 userSessions->OnStartInput(clientInfo, agentObject);
108 userSessions->NotifyImeChangeToClients(property, subProperty);
109 userSessions->OnHideCurrentInput();
110 userSessions->OnHideInput(client);
111 userSessions->OnReleaseInput(client);
112 userSessions->DeactivateIme("", "");
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 }