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 "imageloader_fuzzer.h"
17
18 #include "core/image/image_cache.h"
19 #include "core/image/image_compressor.h"
20 #include "core/image/image_loader.h"
21 #include "core/image/image_object.h"
22 #include "core/image/image_provider.h"
23
24 namespace OHOS::Ace {
25 constexpr uint32_t u16m = 65535;
26
CreateAnimatedImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const sk_sp<SkData> & data)27 RefPtr<ImageObject> CreateAnimatedImageObject(
28 ImageSourceInfo source, const Size& imageSize, int32_t frameCount, const sk_sp<SkData>& data)
29 {
30 return Referenced::MakeRefPtr<StaticImageObject>(source, imageSize, frameCount, data);
31 }
32
GetImageSvgDomObj(ImageSourceInfo source,const std::unique_ptr<SkMemoryStream> & svgStream,const RefPtr<PipelineBase> & context,std::optional<Color> & color)33 RefPtr<ImageObject> GetImageSvgDomObj(ImageSourceInfo source, const std::unique_ptr<SkMemoryStream > &svgStream,
34 const RefPtr<PipelineBase>& context, std::optional<Color>& color)
35 {
36 return nullptr;
37 }
38
CreatImageSourceInfo(const uint8_t * data,size_t size)39 const Ace::ImageSourceInfo CreatImageSourceInfo(const uint8_t* data, size_t size)
40 {
41 std::string randomString(reinterpret_cast<const char*>(data), size);
42 Ace::InternalResource::ResourceId resourceId = Ace::InternalResource::ResourceId::NO_ID;
43 Ace::RefPtr<Ace::PixelMap> pixmap = nullptr;
44 Ace::Dimension dimension(static_cast<double>(size % u16m));
45 Ace::ImageSourceInfo info(randomString, dimension, dimension, resourceId, pixmap);
46 return info;
47 }
48
FileImageLoaderTest(const uint8_t * data,size_t size)49 bool FileImageLoaderTest(const uint8_t* data, size_t size)
50 {
51 auto info = CreatImageSourceInfo(data, size);
52 Ace::WeakPtr<Ace::PipelineBase> context = nullptr;
53 Ace::DataProviderImageLoader dataProviderImageLoader;
54 return dataProviderImageLoader.LoadImageData(info, context) != nullptr;
55 }
56
AssetImageLoad(const uint8_t * data,size_t size)57 bool AssetImageLoad(const uint8_t* data, size_t size)
58 {
59 auto info = CreatImageSourceInfo(data, size);
60 WeakPtr<Ace::PipelineBase> context = nullptr;
61 AssetImageLoader assetImageLoader;
62 return assetImageLoader.LoadImageData(info, context) != nullptr;
63 }
64
NetworkImageLoad(const uint8_t * data,size_t size)65 bool NetworkImageLoad(const uint8_t* data, size_t size)
66 {
67 auto info = CreatImageSourceInfo(data, size);
68 WeakPtr<Ace::PipelineBase> context = nullptr;
69 NetworkImageLoader networkImageLoader;
70 return networkImageLoader.LoadImageData(info, context) != nullptr;
71 }
72
InternalImageLoad(const uint8_t * data,size_t size)73 bool InternalImageLoad(const uint8_t* data, size_t size)
74 {
75 auto info = CreatImageSourceInfo(data, size);
76 Ace::WeakPtr<Ace::PipelineBase> context = nullptr;
77 Ace::InternalImageLoader internalImageLoader;
78 return internalImageLoader.LoadImageData(info, context) != nullptr;
79 }
80 } // namespace OHOS::Ace
81
82 using namespace OHOS;
83 using namespace OHOS::Ace;
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 InternalImageLoad(data, size);
88 NetworkImageLoad(data, size);
89 AssetImageLoad(data, size);
90 FileImageLoaderTest(data, size);
91 return 0;
92 }
93