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 #define private public
17 #define protected public
18 #include "event_handler.h"
19 #undef private
20 #undef protected
21
22 #include "eventhandler_fuzzer.h"
23 #include "securec.h"
24
25 namespace OHOS {
26 namespace {
27 constexpr size_t U32_AT_SIZE = 4;
28 }
29
30 class DumperTest : public AppExecFwk::Dumper {
31 public:
32 DumperTest() = default;
~DumperTest()33 virtual ~DumperTest()
34 {};
Dump(const std::string & message)35 void Dump(const std::string &message) override
36 {}
GetTag()37 std::string GetTag() override
38 {
39 return {};
40 }
41 };
42
DoSomethingInterestingWithMyAPI(const char * data,size_t size)43 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
44 {
45 std::shared_ptr<AppExecFwk::EventRunner> runner = nullptr;
46 AppExecFwk::EventHandler eventHandler(runner);
47 uint32_t innerEventId = *data;
48 std::list<AppExecFwk::InnerEvent::Pointer> events;
49 AppExecFwk::InnerEvent::Pointer event = std::move(events.front());
50 int64_t taskTime = U32_AT(reinterpret_cast<const uint8_t*>(data));
51 AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::LOW;
52 int32_t fileDescriptor = U32_AT(reinterpret_cast<const uint8_t*>(data));
53 DumperTest dumper;
54 eventHandler.Dump(dumper);
55 eventHandler.GetEventName(event);
56 eventHandler.ProcessEvent(event);
57 eventHandler.RemoveAllFileDescriptorListeners();
58 eventHandler.SendTimingEvent(event, taskTime, priority);
59 eventHandler.RemoveFileDescriptorListener(fileDescriptor);
60 return eventHandler.HasInnerEvent(innerEventId);
61 }
62 }
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67 /* Run your code on data */
68 if (data == nullptr) {
69 return 0;
70 }
71
72 if (size < OHOS::U32_AT_SIZE) {
73 return 0;
74 }
75
76 char* ch = static_cast<char *>(malloc(size + 1));
77 if (ch == nullptr) {
78 return 0;
79 }
80
81 (void)memset_s(ch, size + 1, 0x00, size + 1);
82 if (memcpy_s(ch, size, data, size) != EOK) {
83 free(ch);
84 ch = nullptr;
85 return 0;
86 }
87
88 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
89 free(ch);
90 ch = nullptr;
91 return 0;
92 }
93