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 "stubgetpointersnapshot_fuzzer.h"
17
18 #include "securec.h"
19 #include "singleton.h"
20
21 #include "mmi_service.h"
22 #include "multimodal_input_connect_stub.h"
23
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "StubGetPointerSnapshotFuzzTest"
26
27 namespace OHOS {
28 namespace MMI {
29 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)30 size_t GetObject(T &object, const uint8_t *data, size_t size)
31 {
32 size_t objectSize = sizeof(object);
33 if (objectSize > size) {
34 return 0;
35 }
36 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
37 if (ret != EOK) {
38 return 0;
39 }
40 return objectSize;
41 }
42 const std::u16string FORMMGR_INTERFACE_TOKEN { u"ohos.multimodalinput.IConnectManager" };
43
StubGetPointerSnapshotFuzzTest(const uint8_t * data,size_t size)44 bool StubGetPointerSnapshotFuzzTest(const uint8_t *data, size_t size)
45 {
46 size_t startPos = 0;
47 int32_t rowsBefore;
48 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
49 MessageParcel datas;
50 if (!datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN) ||
51 !datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
52 return false;
53 }
54 MessageParcel reply;
55 MessageOption option;
56 MMIService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
57 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
58 MMIService::GetInstance()->OnRemoteRequest(
59 static_cast<uint32_t>(MultimodalinputConnectInterfaceCode::GET_POINTER_SNAPSHOT), datas, reply, option);
60 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
61 return true;
62 }
63 } // MMI
64 } // OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69 /* Run your code on data */
70 if (data == nullptr) {
71 return 0;
72 }
73
74 OHOS::MMI::StubGetPointerSnapshotFuzzTest(data, size);
75 return 0;
76 }