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 "common_event_manager_service.h"
17 #include "common_event_data.h"
18 #include "commoneventmanagerservice_fuzzer.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace {
23 constexpr size_t U32_AT_SIZE = 4;
24 constexpr uint8_t ENABLE = 2;
25 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
27 {
28 std::string stringData(data);
29 int32_t code = U32_AT(reinterpret_cast<const uint8_t*>(data));
30 bool enabled = *data % ENABLE;
31 MessageParcel dataParcel;
32 MessageParcel reply;
33 MessageOption option;
34 EventFwk::CommonEventManagerService commonEventManagerService;
35 AAFwk::Want want;
36 EventFwk::CommonEventData commonEventData;
37 commonEventData.SetWant(want);
38 commonEventData.SetCode(code);
39 commonEventData.SetData(stringData);
40 commonEventData.GetWant();
41 commonEventData.GetCode();
42 commonEventData.GetData();
43 Parcel p;
44 commonEventData.Marshalling(p);
45 commonEventData.Unmarshalling(p);
46 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
47 std::vector<std::string> permissions;
48 permissions.emplace_back(stringData);
49 commonEventPublishInfo.SetSubscriberPermissions(permissions);
50 sptr<IRemoteObject> commonEventListener = nullptr;
51 commonEventManagerService.PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code);
52 commonEventManagerService.PublishCommonEvent(
53 commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
54 EventFwk::MatchingSkills matchingSkills;
55 matchingSkills.AddEvent(stringData);
56 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
57 subscribeInfo.SetPriority(code);
58 commonEventManagerService.SubscribeCommonEvent(subscribeInfo, commonEventListener);
59 commonEventManagerService.UnsubscribeCommonEvent(commonEventListener);
60 commonEventManagerService.GetStickyCommonEvent(stringData, commonEventData);
61 uint8_t dumpType = *data;
62 std::vector<std::string> state;
63 state.emplace_back(stringData);
64 commonEventManagerService.DumpState(dumpType, stringData, code, state);
65 commonEventManagerService.FinishReceiver(commonEventListener, code, stringData, enabled);
66 commonEventManagerService.Freeze(code);
67 commonEventManagerService.Unfreeze(code);
68 return commonEventManagerService.UnfreezeAll();
69 }
70 }
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75 /* Run your code on data */
76 if (data == nullptr) {
77 return 0;
78 }
79
80 if (size < OHOS::U32_AT_SIZE) {
81 return 0;
82 }
83
84 char* ch = (char *)malloc(size + 1);
85 if (ch == nullptr) {
86 return 0;
87 }
88
89 (void)memset_s(ch, size + 1, 0x00, size + 1);
90 if (memcpy_s(ch, size, data, size) != EOK) {
91 free(ch);
92 ch = nullptr;
93 return 0;
94 }
95
96 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
97 free(ch);
98 ch = nullptr;
99 return 0;
100 }
101