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 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MarkConsumedFuzzTest" };
28 } // namespace
29
30 class InputEventConsumerTest : public IInputEventConsumer {
31 public:
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const32 void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override{};
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const33 void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override
34 {
35 MMI_HILOGD("Report pointer event success");
36 };
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const37 void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override{};
38 };
39
GetObject(const uint8_t * data,size_t size,T & object)40 template <class T> size_t GetObject(const uint8_t *data, size_t size, T &object)
41 {
42 size_t objectSize = sizeof(object);
43 if (objectSize > size) {
44 return 0;
45 }
46 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
47 if (ret != EOK) {
48 return 0;
49 }
50 return objectSize;
51 }
52
MarkConsumedFuzzTest(const uint8_t * data,size_t size)53 void MarkConsumedFuzzTest(const uint8_t *data, size_t size)
54 {
55 CALL_DEBUG_ENTER;
56 int32_t eventId;
57 GetObject<int32_t>(data, size, eventId);
58 auto consumer = std::make_shared<InputEventConsumerTest>();
59 int32_t monitorId = InputManager::GetInstance()->AddMonitor(consumer);
60 InputManager::GetInstance()->MarkConsumed(monitorId, eventId);
61 InputManager::GetInstance()->RemoveMonitor(monitorId);
62 }
63 } // namespace MMI
64 } // namespace 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 OHOS::MMI::MarkConsumedFuzzTest(data, size);
71 return 0;
72 }
73