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 "formmgradapterthree_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "form_mgr_adapter.h"
24 #include "form_util.h"
25 #undef private
26 #undef protected
27 #include "securec.h"
28
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 constexpr size_t FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37 // convert fuzz input data to an integer
38 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
39 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
41 {
42 FormMgrAdapter formMgrAdapter;
43 int64_t matchedFormId = static_cast<int64_t>(GetU32Data(data));
44 FormRecord formRecord;
45 std::map<std::string, std::vector<int64_t>> eventMaps;
46 std::string bundleName(data, size);
47 std::vector<int64_t> matchedFormIds;
48 matchedFormIds.emplace_back(matchedFormId);
49 eventMaps.emplace(bundleName, matchedFormIds);
50 formMgrAdapter.CreateHandleEventMap(matchedFormId, formRecord, eventMaps);
51 sptr<IRemoteObject> callerToken = nullptr;
52 int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
53 int32_t userId = FormUtil::GetCurrentAccountId();
54 formMgrAdapter.UpdateProviderInfoToHost(matchedFormId, userId, callerToken, formVisibleType, formRecord);
55 std::vector<int64_t> formIds;
56 formIds.emplace_back(matchedFormId);
57 int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
58 formMgrAdapter.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
59 std::string abilityName(data, size);
60 Want want;
61 std::string provider(data, size);
62 formMgrAdapter.AcquireFormStateCheck(bundleName, abilityName, want, provider);
63 FormStateInfo stateInfo;
64 formMgrAdapter.AcquireFormState(want, callerToken, stateInfo);
65 bool isVisible = *data % ENABLE;
66 formMgrAdapter.NotifyFormsVisible(formIds, isVisible, callerToken);
67 bool isEnableUpdate = *data % ENABLE;
68 formMgrAdapter.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
69 FormInfo formInfo;
70 std::vector<FormInfo> formInfos;
71 formInfos.emplace_back(formInfo);
72 formMgrAdapter.GetFormsInfoByApp(bundleName, formInfos);
73 std::string moduleName(data, size);
74 formMgrAdapter.GetFormsInfoByModule(bundleName, moduleName, formInfos);
75 return true;
76 }
77 }
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82 /* Run your code on data */
83 if (data == nullptr) {
84 return 0;
85 }
86
87 if (size < OHOS::U32_AT_SIZE) {
88 return 0;
89 }
90
91 /* Validate the length of size */
92 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
93 return 0;
94 }
95
96 char* ch = static_cast<char*>(malloc(size + 1));
97 if (ch == nullptr) {
98 return 0;
99 }
100
101 (void)memset_s(ch, size + 1, 0x00, size + 1);
102 if (memcpy_s(ch, size + 1, data, size) != EOK) {
103 free(ch);
104 ch = nullptr;
105 return 0;
106 }
107
108 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
109 free(ch);
110 ch = nullptr;
111 return 0;
112 }
113
114