• 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 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     formMgrProxy.SendTransactCmd(IFormMgr::Message::FORM_MGR_ADD_FORM, datas, datas);
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     /* Validate the length of size */
108     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
109         return 0;
110     }
111 
112     char* ch = (char *)malloc(size + 1);
113     if (ch == nullptr) {
114         return 0;
115     }
116 
117     (void)memset_s(ch, size + 1, 0x00, size + 1);
118     if (memcpy_s(ch, size, data, size) != EOK) {
119         free(ch);
120         ch = nullptr;
121         return 0;
122     }
123 
124     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
125     free(ch);
126     ch = nullptr;
127     return 0;
128 }
129 
130