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 "formproviderdata_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "form_provider_data.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;
GetU32Data(const char * ptr)32 uint32_t GetU32Data(const char* ptr)
33 {
34 // convert fuzz input data to an integer
35 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
36 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)37 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
38 {
39 FormProviderData formProviderData;
40 std::string picName(data, size);
41 const std::shared_ptr<char> datas = nullptr;
42 int32_t sizes = static_cast<int32_t>(GetU32Data(data));
43 formProviderData.AddImageData(picName, datas, sizes);
44 int fd = static_cast<int>(GetU32Data(data));
45 formProviderData.AddImageData(picName, fd);
46 formProviderData.ParseImagesData();
47 formProviderData.RemoveImageData(picName);
48 std::string jsonDataString(data, size);
49 formProviderData.SetDataString(jsonDataString);
50 nlohmann::json addJsonData;
51 formProviderData.MergeData(addJsonData);
52 formProviderData.GetImageDataMap();
53 int32_t imageDataState = static_cast<int32_t>(GetU32Data(data));
54 formProviderData.SetImageDataState(imageDataState);
55 Parcel parcel;
56 formProviderData.ReadFromParcel(parcel);
57 formProviderData.Marshalling(parcel);
58 formProviderData.Unmarshalling(parcel);
59 formProviderData.ClearData();
60 formProviderData.NeedCache();
61 formProviderData.WriteImageDataToParcel(parcel, picName, datas, sizes);
62 formProviderData.ConvertRawImageData();
63 return formProviderData.GetDataString() == "aa";
64 }
65 }
66
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70 /* Run your code on data */
71 if (data == nullptr) {
72 return 0;
73 }
74
75 if (size < OHOS::U32_AT_SIZE) {
76 return 0;
77 }
78
79 char* ch = static_cast<char*>(malloc(size + 1));
80 if (ch == nullptr) {
81 return 0;
82 }
83
84 (void)memset_s(ch, size + 1, 0x00, size + 1);
85 if (memcpy_s(ch, size + 1, data, size) != EOK) {
86 free(ch);
87 ch = nullptr;
88 return 0;
89 }
90
91 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
92 free(ch);
93 ch = nullptr;
94 return 0;
95 }
96
97