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 "formprovidermgr_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "form_item_info.h"
22 #define private public
23 #define protected public
24 #include "form_provider_mgr.h"
25 #undef private
26 #undef protected
27 #include "securec.h"
28
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 constexpr size_t FOO_MAX_LEN = 1024;
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 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
41 {
42 FormItemInfo formItemInfo;
43 bool isFormVisibleNotify = *data % ENABLE;
44 formItemInfo.SetFormVisibleNotify(isFormVisibleNotify);
45 std::string formSrc(data, size);
46 formItemInfo.SetFormSrc(formSrc);
47 formItemInfo.GetFormSrc();
48 FormWindow formWindow;
49 formItemInfo.SetFormWindow(formWindow);
50 formItemInfo.GetFormWindow();
51 uint32_t versionCode = static_cast<uint32_t>(GetU32Data(data));
52 formItemInfo.SetVersionCode(versionCode);
53 formItemInfo.GetVersionCode();
54 std::string versionName(data, size);
55 formItemInfo.SetVersionName(versionName);
56 formItemInfo.GetVersionName();
57 uint32_t compatibleVersion = static_cast<uint32_t>(GetU32Data(data));
58 formItemInfo.SetCompatibleVersion(compatibleVersion);
59 formItemInfo.GetCompatibleVersion();
60 formItemInfo.GetIcon();
61 std::string deviceId(data, size);
62 formItemInfo.SetDeviceId(deviceId);
63 formItemInfo.GetDeviceId();
64 FormType type = FormType::JS;
65 formItemInfo.SetType(type);
66 formItemInfo.GetType();
67 FormProviderMgr formProviderMgr;
68 int64_t formId = static_cast<int64_t>(GetU32Data(data));
69 FormProviderInfo formProviderInfo;
70 formProviderMgr.AcquireForm(formId, formProviderInfo);
71 Want want;
72 bool isVisibleToFresh = *data % ENABLE;
73 formProviderMgr.RefreshForm(formId, want, isVisibleToFresh);
74 FormRecord record;
75 bool isTimerRefresh = *data % ENABLE;
76 formProviderMgr.ConnectAmsForRefresh(formId, record, want, isTimerRefresh);
77 formProviderMgr.NotifyProviderFormDelete(formId, record);
78 std::string bundleName(data, size);
79 std::string abilityName(data, size);
80 std::set<int64_t> formIds;
81 formIds.insert(formId);
82 formProviderMgr.NotifyProviderFormsBatchDelete(bundleName, abilityName, formIds);
83 formProviderMgr.UpdateForm(formId, formProviderInfo);
84 FormProviderData formProviderData;
85 formProviderMgr.UpdateForm(formId, record, formProviderData);
86 formProviderMgr.MessageEvent(formId, record, want);
87 formProviderMgr.IncreaseTimerRefreshCount(formId);
88 AppExecFwk::FormState state = AppExecFwk::FormState::UNKNOWN;
89 std::string provider(data, size);
90 Want wantArg;
91 formProviderMgr.AcquireFormStateBack(state, provider, wantArg);
92 formProviderMgr.IsNeedToFresh(record, formId, isVisibleToFresh);
93 formProviderMgr.GetFormAbilityInfo(record);
94 formProviderMgr.IsFormCached(record);
95 sptr<AAFwk::IAbilityConnection> formRefreshConnection = nullptr;
96 return formProviderMgr.RebindByFreeInstall(record, want, formRefreshConnection);
97 }
98 }
99
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
102 {
103 /* Run your code on data */
104 if (data == nullptr) {
105 return 0;
106 }
107
108 if (size < OHOS::U32_AT_SIZE) {
109 return 0;
110 }
111
112 /* Validate the length of size */
113 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
114 return 0;
115 }
116
117 char* ch = (char *)malloc(size + 1);
118 if (ch == nullptr) {
119 return 0;
120 }
121
122 (void)memset_s(ch, size + 1, 0x00, size + 1);
123 if (memcpy_s(ch, size, data, size) != EOK) {
124 free(ch);
125 ch = nullptr;
126 return 0;
127 }
128
129 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
130 free(ch);
131 ch = nullptr;
132 return 0;
133 }
134
135