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 "formmgrservice_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "form_mgr/form_mgr_service.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 FormMgrService formMgrService;
41 formMgrService.CheckFMSReady();
42 int64_t formId = static_cast<int64_t>(GetU32Data(data));
43 Want want;
44 sptr<IRemoteObject> callerToken = nullptr;
45 FormJsInfo formInfo;
46 formMgrService.AddForm(formId, want, callerToken, formInfo);
47 formMgrService.DeleteForm(formId, callerToken);
48 bool delCache = *data % ENABLE;
49 formMgrService.ReleaseForm(formId, callerToken, delCache);
50 formMgrService.RequestForm(formId, callerToken, want);
51 int64_t nextTime = static_cast<int64_t>(GetU32Data(data));
52 formMgrService.SetNextRefreshTime(formId, nextTime);
53 std::vector<int64_t> formIds;
54 formIds.emplace_back(formId);
55 int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
56 formMgrService.NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
57 formMgrService.CastTempForm(formId, callerToken);
58 bool updateType = *data % ENABLE;
59 formMgrService.LifecycleUpdate(formIds, callerToken, updateType);
60 std::string bundleName(data, size);
61 std::string formInfos(data, size);
62 formMgrService.DumpFormInfoByBundleName(bundleName, formInfos);
63 formMgrService.DumpFormInfoByFormId(formId, formInfos);
64 std::string isTimingService(data, size);
65 formMgrService.DumpFormTimerByFormId(formId, isTimingService);
66 formMgrService.OnStop();
67 formMgrService.CheckFormPermission();
68 int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
69 formMgrService.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
70 FormStateInfo stateInfo;
71 formMgrService.AcquireFormState(want, callerToken, stateInfo);
72 bool isVisible = *data % ENABLE;
73 formMgrService.NotifyFormsVisible(formIds, isVisible, callerToken);
74 bool isEnableUpdate = *data % ENABLE;
75 formMgrService.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
76 FormInfo aa;
77 std::vector<FormInfo> formInfoes;
78 formInfoes.emplace_back(aa);
79 formMgrService.GetAllFormsInfo(formInfoes);
80 formMgrService.StartAbility(want, callerToken);
81 formMgrService.InitFormShareMgrSerialQueue();
82 FormShareInfo info;
83 formMgrService.RecvFormShareInfoFromRemote(info);
84 int fd = static_cast<int>(GetU32Data(data));
85 std::vector<std::u16string> args = {u"-h"};
86 formMgrService.Dump(fd, args);
87 std::string result(data, size);
88 formMgrService.Dump(args, result);
89 std::string value(data, size);
90 formMgrService.HiDumpFormInfoByBundleName(value, result);
91 formMgrService.HiDumpFormInfoByFormId(value, result);
92 return true;
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 char* ch = static_cast<char*>(malloc(size + 1));
109 if (ch == nullptr) {
110 return 0;
111 }
112
113 (void)memset_s(ch, size + 1, 0x00, size + 1);
114 if (memcpy_s(ch, size + 1, data, size) != EOK) {
115 free(ch);
116 ch = nullptr;
117 return 0;
118 }
119
120 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
121 free(ch);
122 ch = nullptr;
123 return 0;
124 }
125
126