• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 #include <string_ex.h>
22 
23 #include "global.h"
24 #include "i_input_method_agent.h"
25 #include "i_input_method_core.h"
26 #include "input_client_proxy.h"
27 #include "input_method_ability.h"
28 #include "input_method_agent_proxy.h"
29 #include "input_method_agent_stub.h"
30 #include "input_method_core_proxy.h"
31 #include "input_method_info.h"
32 #include "input_method_property.h"
33 #include "iremote_broker.h"
34 #include "message_parcel.h"
35 #include "peruser_session.h"
36 
37 using namespace OHOS::MiscServices;
38 namespace OHOS {
39     constexpr size_t THRESHOLD = 10;
40 
ConvertToUint32(const uint8_t * ptr)41     uint32_t ConvertToUint32(const uint8_t *ptr)
42     {
43         if (ptr == nullptr) {
44             return 0;
45         }
46         uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
47         return bigVar;
48     }
49 
FuzzPerUserSession(const uint8_t * rawData,size_t size)50     bool FuzzPerUserSession(const uint8_t *rawData, size_t size)
51     {
52         Property property;
53         SubProperty subProperty;
54 
55         int flags = static_cast<int32_t>(*rawData);
56         std::string str(rawData, rawData + size);
57         std::u16string packageName = Str8ToStr16(str);
58         bool isShowKeyboard = true;
59         constexpr int32_t MAIN_USER_ID = 100;
60         sptr<IInputClient> client = new (std::nothrow) InputClientStub();
61         sptr<IRemoteObject> object = client->AsObject();
62         std::shared_ptr<PerUserSession> userSessions = std::make_shared<PerUserSession>(MAIN_USER_ID);
63         sptr<IInputMethodCore> core = new InputMethodCoreProxy(object);
64         sptr<IInputMethodAgent> agent = new InputMethodAgentProxy(object);
65         InputMethodInfo *ime = new InputMethodInfo();
66 
67         userSessions->OnShowKeyboardSelf();
68         userSessions->OnInputMethodSwitched(property, subProperty);
69         userSessions->GetCurrentSubProperty();
70         userSessions->SetCurrentSubProperty(subProperty);
71         userSessions->StopInputService(str);
72         userSessions->JoinWorkThread();
73         userSessions->OnHideKeyboardSelf(flags);
74         userSessions->OnStartInput(client, isShowKeyboard);
75         userSessions->OnStopInput(client);
76         userSessions->OnReleaseInput(client);
77         userSessions->OnSetCoreAndAgent(core, agent);
78 
79         delete ime;
80         ime = nullptr;
81         return true;
82     }
83 }
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87     if (size < OHOS::THRESHOLD) {
88         return 0;
89     }
90     /* Run your code on data */
91     OHOS::FuzzPerUserSession(data, size);
92     return 0;
93 }