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 "formdatamgrone_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_data_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 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     FormDataMgr formDataMgr;
42     int formUserUid = static_cast<int>(GetU32Data(data));
43     std::vector<int> formUserUids;
44     formUserUids.emplace_back(formUserUid);
45     formDataMgr.IsCallingUidValid(formUserUids);
46     int64_t formId = static_cast<int64_t>(GetU32Data(data));
47     bool formTempFlag = *data % ENABLE;
48     formDataMgr.ModifyFormTempFlag(formId, formTempFlag);
49     formDataMgr.AddFormUserUid(formId, formUserUid);
50     int uid = static_cast<int>(GetU32Data(data));
51     formDataMgr.DeleteFormUserUid(formId, uid);
52     FormRecord formRecord;
53     formDataMgr.UpdateFormRecord(formId, formRecord);
54     std::string bundleName(data, size);
55     std::vector<FormRecord> formInfos;
56     formInfos.emplace_back(formRecord);
57     formDataMgr.GetFormRecord(bundleName, formInfos);
58     formDataMgr.ExistFormRecord(formId);
59     formDataMgr.HasFormUserUids(formId);
60     FormHostRecord formHostRecord;
61     std::vector<FormHostRecord> formHostRecords;
62     formHostRecords.emplace_back(formHostRecord);
63     formDataMgr.GetFormHostRecord(formId, formHostRecords);
64     sptr<IRemoteObject> callerToken = nullptr;
65     formDataMgr.DeleteHostRecord(callerToken, formId);
66     std::vector<int64_t> removedFormIds;
67     removedFormIds.emplace_back(formId);
68     formDataMgr.CleanHostRemovedForms(removedFormIds);
69     sptr<IRemoteObject> remoteHost = nullptr;
70     formDataMgr.HandleHostDied(remoteHost);
71     return true;
72 }
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     if (data == nullptr) {
80         return 0;
81     }
82 
83     if (size < OHOS::U32_AT_SIZE) {
84         return 0;
85     }
86 
87     /* Validate the length of size */
88     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
89         return 0;
90     }
91 
92     char* ch = static_cast<char*>(malloc(size + 1));
93     if (ch == nullptr) {
94         return 0;
95     }
96 
97     (void)memset_s(ch, size + 1, 0x00, size + 1);
98     if (memcpy_s(ch, size + 1, data, size) != EOK) {
99         free(ch);
100         ch = nullptr;
101         return 0;
102     }
103 
104     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
105     free(ch);
106     ch = nullptr;
107     return 0;
108 }
109 
110