• 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 "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 U32_AT_SIZE = 4;
32 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)33 uint32_t GetU32Data(const char* ptr)
34 {
35     // convert fuzz input data to an integer
36     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
37 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
39 {
40     sptr<IRemoteObject> impl = nullptr;
41     FormMgrProxy formMgrProxy(impl);
42     int64_t formId = static_cast<int64_t>(GetU32Data(data));
43     Want want;
44     sptr<IRemoteObject> callerToken = nullptr;
45     FormJsInfo formInfo;
46     formMgrProxy.AddForm(formId, want, callerToken, formInfo);
47     formMgrProxy.DeleteForm(formId, callerToken);
48     bool delCache = *data % ENABLE;
49     formMgrProxy.ReleaseForm(formId, callerToken, delCache);
50     formMgrProxy.RequestForm(formId, callerToken, want);
51     std::vector<int64_t> formIds;
52     formIds.emplace_back(formId);
53     int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
54     formMgrProxy.NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
55     formMgrProxy.CastTempForm(formId, callerToken);
56     std::string stringData(data);
57     formMgrProxy.DumpStorageFormInfos(stringData);
58     std::string bundleName(data);
59     formMgrProxy.DumpFormInfoByBundleName(bundleName, stringData);
60     formMgrProxy.DumpFormInfoByFormId(formId, stringData);
61     std::string isTimingService(data);
62     formMgrProxy.DumpFormTimerByFormId(formId, isTimingService);
63     formMgrProxy.MessageEvent(formId, want, callerToken);
64     MessageParcel datas;
65     formMgrProxy.WriteInterfaceToken(datas);
66     std::string stringInfo(data);
67     formMgrProxy.GetStringInfo(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, stringInfo);
68     FormInfo formInfoes;
69     std::vector<FormInfo> formInfos;
70     formInfos.emplace_back(formInfoes);
71     formMgrProxy.GetFormsInfo(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, formInfos);
72     MessageOption option(MessageOption::TF_SYNC);
73     formMgrProxy.SendTransactCmd(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, datas, option);
74     int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
75     formMgrProxy.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
76     FormStateInfo stateInfo;
77     formMgrProxy.AcquireFormState(want, callerToken, stateInfo);
78     bool isVisible = *data % ENABLE;
79     formMgrProxy.NotifyFormsVisible(formIds, isVisible, callerToken);
80     bool isEnableUpdate = *data % ENABLE;
81     formMgrProxy.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
82     formMgrProxy.GetAllFormsInfo(formInfos);
83     formMgrProxy.GetFormsInfoByApp(bundleName, formInfos);
84     std::string moduleName(data);
85     formMgrProxy.GetFormsInfoByModule(bundleName, moduleName, formInfos);
86     FormInfoFilter filter;
87     formMgrProxy.GetFormsInfo(filter, formInfos);
88     formMgrProxy.StartAbility(want, callerToken);
89     std::string deviceId(data);
90     int64_t requestCode = static_cast<int64_t>(GetU32Data(data));
91     return formMgrProxy.ShareForm(formId, deviceId, callerToken, requestCode);
92 }
93 }
94 
95 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)96 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
97 {
98     /* Run your code on data */
99     if (data == nullptr) {
100         return 0;
101     }
102 
103     if (size < OHOS::U32_AT_SIZE) {
104         return 0;
105     }
106 
107     char* ch = static_cast<char*>(malloc(size + 1));
108     if (ch == nullptr) {
109         return 0;
110     }
111 
112     (void)memset_s(ch, size + 1, 0x00, size + 1);
113     if (memcpy_s(ch, size + 1, data, size) != EOK) {
114         free(ch);
115         ch = nullptr;
116         return 0;
117     }
118 
119     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
120     free(ch);
121     ch = nullptr;
122     return 0;
123 }
124 
125