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 "formsupplycallback_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "form_info_storage.h"
22 #define private public
23 #define protected public
24 #include "form_dump_mgr.h"
25 #include "form_supply_callback.h"
26 #undef private
27 #undef protected
28 #include "securec.h"
29
30 using namespace OHOS::AppExecFwk;
31
32 namespace OHOS {
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37 // convert fuzz input data to an integer
38 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
39 }
40
DoSomethingInterestingWithMyAPI(const char * data,size_t size)41 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
42 {
43 FormSupplyCallback formSupplyCallback;
44 formSupplyCallback.GetInstance();
45 FormProviderInfo formProviderInfo;
46 Want want;
47 formSupplyCallback.OnAcquire(formProviderInfo, want);
48 formSupplyCallback.OnEventHandle(want);
49 FormState state = FormState::UNKNOWN;
50 std::string provider(data, size);
51 Want wantArg;
52 formSupplyCallback.OnAcquireStateResult(state, provider, wantArg, want);
53 sptr<FormAbilityConnection> connection = nullptr;
54 formSupplyCallback.AddConnection(connection);
55 int32_t connectId = static_cast<int32_t>(GetU32Data(data));
56 formSupplyCallback.RemoveConnection(connectId);
57 formSupplyCallback.CanDisconnect(connection);
58 int64_t formId = static_cast<int64_t>(GetU32Data(data));
59 std::string remoteDeviceId(data, size);
60 AAFwk::WantParams wantParams;
61 int64_t requestCode = static_cast<int64_t>(GetU32Data(data));
62 bool result = *data % ENABLE;
63 formSupplyCallback.OnShareAcquire(formId, remoteDeviceId, wantParams, requestCode, result);
64 sptr<IRemoteObject> hostToken = nullptr;
65 formSupplyCallback.RemoveConnection(formId, hostToken);
66 formSupplyCallback.HandleHostDied(hostToken);
67 int32_t userId = static_cast<int32_t>(GetU32Data(data));
68 FormInfo info;
69 std::vector<FormInfo> formInfos;
70 formInfos.emplace_back(info);
71 AAFwk::FormInfoStorage formInfoStorage(userId, formInfos);
72 formInfoStorage.GetAllFormsInfo(userId, formInfos);
73 std::string moduleName(data, size);
74 formInfoStorage.GetFormsInfoByModule(userId, moduleName, formInfos);
75 FormDumpMgr formDumpMgr;
76 FormDBInfo formDBInfo;
77 std::vector<FormDBInfo> storageInfos;
78 storageInfos.emplace_back(formDBInfo);
79 std::string formInfoes(data, size);
80 formDumpMgr.DumpStorageFormInfos(storageInfos, formInfoes);
81 FormRecord formRecord;
82 std::vector<FormRecord> formRecordInfos;
83 formRecordInfos.emplace_back(formRecord);
84 formDumpMgr.DumpFormInfos(formRecordInfos, formInfoes);
85 FormHostRecord formHostRecord;
86 std::string formInfo(data, size);
87 formDumpMgr.DumpFormHostInfo(formHostRecord, formInfo);
88 formDumpMgr.DumpFormInfo(formRecord, formInfo);
89 formDumpMgr.DumpTemporaryFormInfos(formRecordInfos, formInfo);
90 std::vector<FormInfo> bundleFormInfos;
91 formDumpMgr.DumpStaticBundleFormInfos(bundleFormInfos, formInfo);
92 int32_t tokenId = static_cast<int32_t>(GetU32Data(data));
93 std::string bundleName(data, size);
94 int32_t instIndex = static_cast<int32_t>(GetU32Data(data));
95 formDumpMgr.DumpHasFormVisible(tokenId, bundleName, userId, instIndex, formInfo);
96 std::vector<std::string> subscribedKeys;
97 int64_t count = static_cast<int64_t>(GetU32Data(data));
98 formDumpMgr.DumpFormSubscribeInfo(subscribedKeys, count, formInfo);
99 return formSupplyCallback.IsRemoveConnection(formId, hostToken);
100 }
101 }
102
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106 /* Run your code on data */
107 if (data == nullptr) {
108 return 0;
109 }
110
111 if (size < OHOS::U32_AT_SIZE) {
112 return 0;
113 }
114
115 char* ch = static_cast<char*>(malloc(size + 1));
116 if (ch == nullptr) {
117 return 0;
118 }
119
120 (void)memset_s(ch, size + 1, 0x00, size + 1);
121 if (memcpy_s(ch, size + 1, data, size) != EOK) {
122 free(ch);
123 ch = nullptr;
124 return 0;
125 }
126
127 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
128 free(ch);
129 ch = nullptr;
130 return 0;
131 }
132
133