• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
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     return formSupplyCallback.IsRemoveConnection(formId, hostToken);
90 }
91 }
92 
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96     /* Run your code on data */
97     if (data == nullptr) {
98         return 0;
99     }
100 
101     if (size < OHOS::U32_AT_SIZE) {
102         return 0;
103     }
104 
105     /* Validate the length of size */
106     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
107         return 0;
108     }
109 
110     char* ch = (char *)malloc(size + 1);
111     if (ch == nullptr) {
112         return 0;
113     }
114 
115     (void)memset_s(ch, size + 1, 0x00, size + 1);
116     if (memcpy_s(ch, size, data, size) != EOK) {
117         free(ch);
118         ch = nullptr;
119         return 0;
120     }
121 
122     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
123     free(ch);
124     ch = nullptr;
125     return 0;
126 }
127 
128