• 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 "formmgradapterthree_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_mgr/form_mgr_adapter.h"
24 #include "common/util/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 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     FormMgrAdapter formMgrAdapter;
42     int64_t matchedFormId = static_cast<int64_t>(GetU32Data(data));
43     FormRecord formRecord;
44     std::map<std::string, std::vector<int64_t>> eventMaps;
45     std::string bundleName(data, size);
46     std::vector<int64_t> matchedFormIds;
47     matchedFormIds.emplace_back(matchedFormId);
48     eventMaps.emplace(bundleName, matchedFormIds);
49     formMgrAdapter.CreateHandleEventMap(matchedFormId, formRecord, eventMaps);
50     sptr<IRemoteObject> callerToken = nullptr;
51     int32_t formVisibleType = static_cast<int32_t>(GetU32Data(data));
52     int32_t userId = FormUtil::GetCurrentAccountId();
53     formMgrAdapter.UpdateProviderInfoToHost(matchedFormId, userId, callerToken, formVisibleType, formRecord);
54     std::vector<int64_t> formIds;
55     formIds.emplace_back(matchedFormId);
56     int32_t numFormsDeleted = static_cast<int32_t>(GetU32Data(data));
57     formMgrAdapter.DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
58     std::string abilityName(data, size);
59     Want want;
60     std::string provider(data, size);
61     formMgrAdapter.AcquireFormStateCheck(bundleName, abilityName, want, provider);
62     FormStateInfo stateInfo;
63     formMgrAdapter.AcquireFormState(want, callerToken, stateInfo);
64     bool isVisible = *data % ENABLE;
65     formMgrAdapter.NotifyFormsVisible(formIds, isVisible, callerToken);
66     bool isEnableUpdate = *data % ENABLE;
67     formMgrAdapter.NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
68     FormInfo formInfo;
69     std::vector<FormInfo> formInfos;
70     formInfos.emplace_back(formInfo);
71     formMgrAdapter.GetFormsInfoByApp(bundleName, formInfos);
72     std::string moduleName(data, size);
73     formMgrAdapter.GetFormsInfoByModule(bundleName, moduleName, formInfos);
74     return true;
75 }
76 }
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81     /* Run your code on data */
82     if (data == nullptr) {
83         return 0;
84     }
85 
86     if (size < OHOS::U32_AT_SIZE) {
87         return 0;
88     }
89 
90     char* ch = static_cast<char*>(malloc(size + 1));
91     if (ch == nullptr) {
92         return 0;
93     }
94 
95     (void)memset_s(ch, size + 1, 0x00, size + 1);
96     if (memcpy_s(ch, size + 1, data, size) != EOK) {
97         free(ch);
98         ch = nullptr;
99         return 0;
100     }
101 
102     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
103     free(ch);
104     ch = nullptr;
105     return 0;
106 }
107 
108