• 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 "formmgr_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_mgr.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     FormMgr formMgr;
41     int errorCode = static_cast<int>(GetU32Data(data));
42     formMgr.GetErrorMsg(errorCode);
43     int64_t formId = static_cast<int64_t>(GetU32Data(data));
44     Want want;
45     sptr<IRemoteObject> callerToken = nullptr;
46     FormJsInfo formInfo;
47     formMgr.AddForm(formId, want, callerToken, formInfo);
48     formMgr.DeleteForm(formId, callerToken);
49     bool delCache = *data % ENABLE;
50     formMgr.ReleaseForm(formId, callerToken, delCache);
51     FormProviderData formBindingData;
52     formMgr.UpdateForm(formId, formBindingData);
53     formMgr.RequestForm(formId, callerToken, want);
54     std::vector<int64_t> formIds;
55     formIds.emplace_back(formId);
56     int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
57     formMgr.NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
58     formMgr.CastTempForm(formId, callerToken);
59     std::string stringData(data);
60     formMgr.DumpStorageFormInfos(stringData);
61     std::string bundleName(data);
62     std::string formInfos(data);
63     formMgr.DumpFormInfoByBundleName(bundleName, formInfos);
64     formMgr.DumpFormInfoByFormId(formId, stringData);
65     formMgr.DumpFormTimerByFormId(formId, stringData);
66     formMgr.MessageEvent(formId, want, callerToken);
67     formMgr.RouterEvent(formId, want, callerToken);
68     int64_t nextTime = static_cast<int64_t>(GetU32Data(data));
69     formMgr.SetNextRefreshTime(formId, nextTime);
70     std::unique_ptr<FormProviderData> formBindingDatas = nullptr;
71     formMgr.RequestPublishForm(want, delCache, formBindingDatas, formId);
72     formMgr.LifecycleUpdate(formIds, callerToken, delCache);
73     formMgr.GetRecoverStatus();
74     formMgr.SetRecoverStatus(errorCode);
75     formMgr.GetErrorMessage(errorCode);
76     std::shared_ptr<FormCallbackInterface> formDeathCallback = nullptr;
77     formMgr.RegisterDeathCallback(formDeathCallback);
78     formMgr.UnRegisterDeathCallback(formDeathCallback);
79     formMgr.GetDeathRecipient();
80     formMgr.CheckIsDeathCallbackRegistered(formDeathCallback);
81     return formMgr.Reconnect();
82 }
83 }
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88     /* Run your code on data */
89     if (data == nullptr) {
90         return 0;
91     }
92 
93     if (size < OHOS::U32_AT_SIZE) {
94         return 0;
95     }
96 
97     char* ch = static_cast<char*>(malloc(size + 1));
98     if (ch == nullptr) {
99         return 0;
100     }
101 
102     (void)memset_s(ch, size + 1, 0x00, size + 1);
103     if (memcpy_s(ch, size + 1, data, size) != EOK) {
104         free(ch);
105         ch = nullptr;
106         return 0;
107     }
108 
109     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
110     free(ch);
111     ch = nullptr;
112     return 0;
113 }
114 
115