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