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 "formmgrproxy_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_mgr_proxy.h"
24 #undef private
25 #undef protected
26 #include "securec.h"
27 
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 constexpr size_t FOO_MAX_LEN = 1024;
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     sptr<IRemoteObject> impl = nullptr;
42     FormMgrProxy formMgrProxy(impl);
43     int64_t formId = static_cast<int64_t>(GetU32Data(data));
44     Want want;
45     sptr<IRemoteObject> callerToken = nullptr;
46     FormJsInfo formInfo;
47     formMgrProxy.AddForm(formId, want, callerToken, formInfo);
48     formMgrProxy.DeleteForm(formId, callerToken);
49     bool delCache = *data % ENABLE;
50     formMgrProxy.ReleaseForm(formId, callerToken, delCache);
51     formMgrProxy.RequestForm(formId, callerToken, want);
52     std::vector<int64_t> formIds;
53     formIds.emplace_back(formId);
54     int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
55     formMgrProxy.NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
56     formMgrProxy.CastTempForm(formId, callerToken);
57     std::string stringData(data);
58     formMgrProxy.DumpStorageFormInfos(stringData);
59     std::string bundleName(data);
60     formMgrProxy.DumpFormInfoByBundleName(bundleName, stringData);
61     formMgrProxy.DumpFormInfoByFormId(formId, stringData);
62     std::string isTimingService(data);
63     formMgrProxy.DumpFormTimerByFormId(formId, isTimingService);
64     formMgrProxy.MessageEvent(formId, want, callerToken);
65     MessageParcel datas;
66     formMgrProxy.WriteInterfaceToken(datas);
67     std::string stringInfo(data);
68     formMgrProxy.GetStringInfo(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, stringInfo);
69     FormInfo formInfoes;
70     std::vector<FormInfo> formInfos;
71     formInfos.emplace_back(formInfoes);
72     formMgrProxy.GetFormsInfo(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, formInfos);
73     MessageOption option(MessageOption::TF_SYNC);
74     formMgrProxy.SendTransactCmd(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, datas, option);
75     int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
76     formMgrProxy.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
77     FormStateInfo stateInfo;
78     formMgrProxy.AcquireFormState(want, callerToken, stateInfo);
79     bool isVisible = *data % ENABLE;
80     formMgrProxy.NotifyFormsVisible(formIds, isVisible, callerToken);
81     bool isEnableUpdate = *data % ENABLE;
82     formMgrProxy.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
83     formMgrProxy.GetAllFormsInfo(formInfos);
84     formMgrProxy.GetFormsInfoByApp(bundleName, formInfos);
85     std::string moduleName(data);
86     formMgrProxy.GetFormsInfoByModule(bundleName, moduleName, formInfos);
87     FormInfoFilter filter;
88     formMgrProxy.GetFormsInfo(filter, formInfos);
89     formMgrProxy.StartAbility(want, callerToken);
90     std::string deviceId(data);
91     int64_t requestCode = static_cast<int64_t>(GetU32Data(data));
92     return formMgrProxy.ShareForm(formId, deviceId, callerToken, requestCode);
93 }
94 }
95 
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99     /* Run your code on data */
100     if (data == nullptr) {
101         return 0;
102     }
103 
104     if (size < OHOS::U32_AT_SIZE) {
105         return 0;
106     }
107 
108     /* Validate the length of size */
109     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
110         return 0;
111     }
112 
113     char* ch = static_cast<char*>(malloc(size + 1));
114     if (ch == nullptr) {
115         return 0;
116     }
117 
118     (void)memset_s(ch, size + 1, 0x00, size + 1);
119     if (memcpy_s(ch, size + 1, data, size) != EOK) {
120         free(ch);
121         ch = nullptr;
122         return 0;
123     }
124 
125     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
126     free(ch);
127     ch = nullptr;
128     return 0;
129 }
130 
131