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_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 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 FormMgrService formMgrService;
42 formMgrService.CheckFMSReady();
43 int64_t formId = static_cast<int64_t>(GetU32Data(data));
44 Want want;
45 sptr<IRemoteObject> callerToken = nullptr;
46 FormJsInfo formInfo;
47 formMgrService.AddForm(formId, want, callerToken, formInfo);
48 formMgrService.DeleteForm(formId, callerToken);
49 bool delCache = *data % ENABLE;
50 formMgrService.ReleaseForm(formId, callerToken, delCache);
51 formMgrService.RequestForm(formId, callerToken, want);
52 int64_t nextTime = static_cast<int64_t>(GetU32Data(data));
53 formMgrService.SetNextRefreshTime(formId, nextTime);
54 std::vector<int64_t> formIds;
55 formIds.emplace_back(formId);
56 int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
57 formMgrService.NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
58 formMgrService.CastTempForm(formId, callerToken);
59 bool updateType = *data % ENABLE;
60 formMgrService.LifecycleUpdate(formIds, callerToken, updateType);
61 std::string bundleName(data, size);
62 std::string formInfos(data, size);
63 formMgrService.DumpFormInfoByBundleName(bundleName, formInfos);
64 formMgrService.DumpFormInfoByFormId(formId, formInfos);
65 std::string isTimingService(data, size);
66 formMgrService.DumpFormTimerByFormId(formId, isTimingService);
67 formMgrService.OnStop();
68 formMgrService.CheckFormPermission();
69 int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
70 formMgrService.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
71 FormStateInfo stateInfo;
72 formMgrService.AcquireFormState(want, callerToken, stateInfo);
73 bool isVisible = *data % ENABLE;
74 formMgrService.NotifyFormsVisible(formIds, isVisible, callerToken);
75 bool isEnableUpdate = *data % ENABLE;
76 formMgrService.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
77 FormInfo aa;
78 std::vector<FormInfo> formInfoes;
79 formInfoes.emplace_back(aa);
80 formMgrService.GetAllFormsInfo(formInfoes);
81 formMgrService.StartAbility(want, callerToken);
82 formMgrService.InitFormShareMgrSerialQueue();
83 FormShareInfo info;
84 formMgrService.RecvFormShareInfoFromRemote(info);
85 int fd = static_cast<int>(GetU32Data(data));
86 std::vector<std::u16string> args = {u"-h"};
87 formMgrService.Dump(fd, args);
88 std::string result(data, size);
89 formMgrService.Dump(args, result);
90 std::string value(data, size);
91 formMgrService.HiDumpFormInfoByBundleName(value, result);
92 formMgrService.HiDumpFormInfoByFormId(value, result);
93 return true;
94 }
95 }
96
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99 {
100 /* Run your code on data */
101 if (data == nullptr) {
102 return 0;
103 }
104
105 if (size < OHOS::U32_AT_SIZE) {
106 return 0;
107 }
108
109 /* Validate the length of size */
110 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
111 return 0;
112 }
113
114 char* ch = static_cast<char*>(malloc(size + 1));
115 if (ch == nullptr) {
116 return 0;
117 }
118
119 (void)memset_s(ch, size + 1, 0x00, size + 1);
120 if (memcpy_s(ch, size + 1, data, size) != EOK) {
121 free(ch);
122 ch = nullptr;
123 return 0;
124 }
125
126 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
127 free(ch);
128 ch = nullptr;
129 return 0;
130 }
131
132