1 /*
2 * Copyright (c) 2023 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 "event_queue.h"
17 #include "eventqueueannex_fuzzer.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace {
22 constexpr size_t U32_AT_SIZE = 4;
23 }
24
25 class MyFileDescriptorListener : public AppExecFwk::FileDescriptorListener {
26 public:
MyFileDescriptorListener()27 MyFileDescriptorListener()
28 {}
~MyFileDescriptorListener()29 ~MyFileDescriptorListener()
30 {}
31
32 /* @param int32_t fileDescriptor */
OnReadable(int32_t)33 void OnReadable(int32_t)
34 {}
35
36 /* @param int32_t fileDescriptor */
OnWritable(int32_t)37 void OnWritable(int32_t)
38 {}
39
40 /* @param int32_t fileDescriptor */
OnException(int32_t)41 void OnException(int32_t)
42 {}
43
44 MyFileDescriptorListener(const MyFileDescriptorListener &) = delete;
45 MyFileDescriptorListener &operator=(const MyFileDescriptorListener &) = delete;
46 MyFileDescriptorListener(MyFileDescriptorListener &&) = delete;
47 MyFileDescriptorListener &operator=(MyFileDescriptorListener &&) = delete;
48 };
49
DoSomethingInterestingWithMyAPI(const char * data,size_t size)50 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
51 {
52 std::string stringData(data);
53 std::shared_ptr<AppExecFwk::IoWaiter> ioWaiter = nullptr;
54 AppExecFwk::EventQueue eventQueue(ioWaiter);
55 std::shared_ptr<AppExecFwk::EventHandler> myHandler;
56 uint32_t innerEventId = *data;
57 int64_t param = U32_AT(reinterpret_cast<const uint8_t*>(data));
58 eventQueue.HasInnerEvent(myHandler, innerEventId);
59 eventQueue.Remove(myHandler);
60 eventQueue.Remove(myHandler, innerEventId);
61 eventQueue.Remove(myHandler, innerEventId, param);
62 eventQueue.Remove(myHandler, stringData);
63 int32_t fileDescriptor = U32_AT(reinterpret_cast<const uint8_t*>(data));
64 auto fileDescriptorListener = std::make_shared<MyFileDescriptorListener>();
65 eventQueue.AddFileDescriptorListener(fileDescriptor, innerEventId, fileDescriptorListener);
66 eventQueue.RemoveFileDescriptorListener(myHandler);
67 eventQueue.RemoveFileDescriptorListener(fileDescriptor);
68 eventQueue.RemoveOrphan();
69 eventQueue.GetEvent();
70 eventQueue.IsIdle();
71 eventQueue.Prepare();
72 eventQueue.Finish();
73 return true;
74 }
75 }
76
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80 /* Run your code on data */
81 if (data == nullptr) {
82 return 0;
83 }
84
85 if (size < OHOS::U32_AT_SIZE) {
86 return 0;
87 }
88
89 char* ch = (char *)malloc(size + 1);
90 if (ch == nullptr) {
91 return 0;
92 }
93
94 (void)memset_s(ch, size + 1, 0x00, size + 1);
95 if (memcpy_s(ch, size, data, size) != EOK) {
96 free(ch);
97 ch = nullptr;
98 return 0;
99 }
100
101 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
102 free(ch);
103 ch = nullptr;
104 return 0;
105 }
106