• 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 "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 U32_AT_SIZE = 4;
33 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)34 uint32_t GetU32Data(const char* ptr)
35 {
36     // convert fuzz input data to an integer
37     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40 {
41     FormItemInfo formItemInfo;
42     bool isFormVisibleNotify = *data % ENABLE;
43     formItemInfo.SetFormVisibleNotify(isFormVisibleNotify);
44     std::string formSrc(data, size);
45     formItemInfo.SetFormSrc(formSrc);
46     formItemInfo.GetFormSrc();
47     FormWindow formWindow;
48     formItemInfo.SetFormWindow(formWindow);
49     formItemInfo.GetFormWindow();
50     uint32_t versionCode = static_cast<uint32_t>(GetU32Data(data));
51     formItemInfo.SetVersionCode(versionCode);
52     formItemInfo.GetVersionCode();
53     std::string versionName(data, size);
54     formItemInfo.SetVersionName(versionName);
55     formItemInfo.GetVersionName();
56     uint32_t compatibleVersion = static_cast<uint32_t>(GetU32Data(data));
57     formItemInfo.SetCompatibleVersion(compatibleVersion);
58     formItemInfo.GetCompatibleVersion();
59     formItemInfo.GetIcon();
60     std::string deviceId(data, size);
61     formItemInfo.SetDeviceId(deviceId);
62     formItemInfo.GetDeviceId();
63     FormType type = FormType::JS;
64     formItemInfo.SetType(type);
65     formItemInfo.GetType();
66     FormProviderMgr formProviderMgr;
67     int64_t formId = static_cast<int64_t>(GetU32Data(data));
68     FormProviderInfo formProviderInfo;
69     formProviderMgr.AcquireForm(formId, formProviderInfo);
70     Want want;
71     bool isVisibleToFresh = *data % ENABLE;
72     formProviderMgr.RefreshForm(formId, want, isVisibleToFresh);
73     FormRecord record;
74     bool isTimerRefresh = *data % ENABLE;
75     formProviderMgr.ConnectAmsForRefresh(formId, record, want, isTimerRefresh);
76     formProviderMgr.NotifyProviderFormDelete(formId, record);
77     std::string bundleName(data, size);
78     std::string abilityName(data, size);
79     std::set<int64_t> formIds;
80     formIds.insert(formId);
81     formProviderMgr.NotifyProviderFormsBatchDelete(bundleName, abilityName, formIds);
82     formProviderMgr.UpdateForm(formId, formProviderInfo);
83     FormProviderData formProviderData;
84     formProviderMgr.UpdateForm(formId, record, formProviderData);
85     formProviderMgr.MessageEvent(formId, record, want);
86     formProviderMgr.IncreaseTimerRefreshCount(formId);
87     AppExecFwk::FormState state = AppExecFwk::FormState::UNKNOWN;
88     std::string provider(data, size);
89     Want wantArg;
90     formProviderMgr.AcquireFormStateBack(state, provider, wantArg);
91     formProviderMgr.IsNeedToFresh(record, formId, isVisibleToFresh);
92     formProviderMgr.GetFormAbilityInfo(record);
93     formProviderMgr.IsFormCached(record);
94     sptr<AAFwk::IAbilityConnection> formRefreshConnection = nullptr;
95     return formProviderMgr.RebindByFreeInstall(record, want, formRefreshConnection);
96 }
97 }
98 
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102     /* Run your code on data */
103     if (data == nullptr) {
104         return 0;
105     }
106 
107     if (size < OHOS::U32_AT_SIZE) {
108         return 0;
109     }
110 
111     char* ch = static_cast<char*>(malloc(size + 1));
112     if (ch == nullptr) {
113         return 0;
114     }
115 
116     (void)memset_s(ch, size + 1, 0x00, size + 1);
117     if (memcpy_s(ch, size + 1, data, size) != EOK) {
118         free(ch);
119         ch = nullptr;
120         return 0;
121     }
122 
123     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
124     free(ch);
125     ch = nullptr;
126     return 0;
127 }
128 
129