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