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 "ohosimagedecoderadapterimpl_fuzzer.h"
17
18 #include <securec.h>
19 #include <sys/mman.h>
20
21 #include "image_source.h"
22 #include "image_type.h"
23 #include "media_errors.h"
24 #include "ohos_adapter_helper.h"
25
26 using namespace OHOS::NWeb;
27 const std::string DEFAULT_MOUSE_DRAG_IMAGE { "/system/etc/device_status/drag_icon/Copy_Drag.svg" };
28
29 namespace OHOS {
30
ValidateInput(const uint8_t * data,size_t size)31 bool ValidateInput(const uint8_t* data, size_t size)
32 {
33 return (data != nullptr) && (size >= sizeof(int32_t));
34 }
35
CreateDecoderAdapter()36 std::shared_ptr<OhosImageDecoderAdapter> CreateDecoderAdapter()
37 {
38 return OhosAdapterHelper::GetInstance().CreateOhosImageDecoderAdapter();
39 }
40
HandleAbnormalCase(const std::shared_ptr<OhosImageDecoderAdapter> & adapter)41 void HandleAbnormalCase(const std::shared_ptr<OhosImageDecoderAdapter>& adapter)
42 {
43 size_t len = 0;
44 std::unique_ptr<uint8_t[]> rawData;
45
46 adapter->ParseImageInfo(rawData.get(), len);
47 adapter->DecodeToPixelMap(rawData.get(), len);
48 }
49
ProcessImageSource(std::shared_ptr<OhosImageDecoderAdapter> & adapter)50 bool ProcessImageSource(std::shared_ptr<OhosImageDecoderAdapter>& adapter)
51 {
52 uint32_t errorCode = 0;
53 OHOS::Media::SourceOptions opts;
54 auto imageSource = OHOS::Media::ImageSource::CreateImageSource(DEFAULT_MOUSE_DRAG_IMAGE, opts, errorCode);
55 if (!imageSource || errorCode != Media::SUCCESS) {
56 return false;
57 }
58
59 OHOS::Media::DecodeOptions decodeOpts;
60 auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
61 if (!pixelMap || errorCode != OHOS::Media::SUCCESS) {
62 return false;
63 }
64
65 const uint8_t* imageData = pixelMap->GetPixels();
66 if (!imageData) {
67 return false;
68 }
69
70 size_t len = pixelMap->GetPixelBytes();
71 adapter->ParseImageInfo(imageData, len);
72 adapter->DecodeToPixelMap(imageData, len);
73 return true;
74 }
75
ApplyOhosImageDecoderAdapterFuzzTest(const uint8_t * data,size_t size)76 bool ApplyOhosImageDecoderAdapterFuzzTest(const uint8_t* data, size_t size)
77 {
78 if (!ValidateInput(data, size)) {
79 return false;
80 }
81
82 auto adapter = CreateDecoderAdapter();
83 if (!adapter) {
84 return false;
85 }
86
87 HandleAbnormalCase(adapter);
88 return ProcessImageSource(adapter);
89 }
90
91 } // namespace OHOS
92
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96 OHOS::ApplyOhosImageDecoderAdapterFuzzTest(data, size);
97 return 0;
98 }
99