1 /*
2  * Copyright (c) 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 "markconsumed_fuzzer.h"
17 
18 #include "securec.h"
19 
20 #include "define_multimodal.h"
21 #include "input_manager.h"
22 #include "mmi_log.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "MarkConsumedFuzzTest"
26 
27 namespace OHOS {
28 namespace MMI {
29 class InputEventConsumerTest : public IInputEventConsumer {
30 public:
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const31     void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override{};
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const32     void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override
33     {
34         MMI_HILOGD("Report pointer event success");
35     };
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const36     void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override{};
37 };
38 
GetObject(const uint8_t * data,size_t size,T & object)39 template <class T> size_t GetObject(const uint8_t *data, size_t size, T &object)
40 {
41     size_t objectSize = sizeof(object);
42     if (objectSize > size) {
43         return 0;
44     }
45     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
46     if (ret != EOK) {
47         return 0;
48     }
49     return objectSize;
50 }
51 
MarkConsumedFuzzTest(const uint8_t * data,size_t size)52 void MarkConsumedFuzzTest(const uint8_t *data, size_t size)
53 {
54     CALL_DEBUG_ENTER;
55     int32_t eventId;
56     GetObject<int32_t>(data, size, eventId);
57     auto consumer = std::make_shared<InputEventConsumerTest>();
58     int32_t monitorId = InputManager::GetInstance()->AddMonitor(consumer);
59     InputManager::GetInstance()->MarkConsumed(monitorId, eventId);
60     InputManager::GetInstance()->RemoveMonitor(monitorId);
61 }
62 } // namespace MMI
63 } // namespace OHOS
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68     /* Run your code on data */
69     OHOS::MMI::MarkConsumedFuzzTest(data, size);
70     return 0;
71 }
72