1 /*
2 * Copyright (c) 2025 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 "releasepixelmap_fuzzer.h"
17
18 #include <securec.h>
19 #include <sys/mman.h>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "image_source.h"
23 #include "image_type.h"
24 #include "media_errors.h"
25 #include "ohos_adapter_helper.h"
26
27 using namespace OHOS::NWeb;
28 const std::string DEFAULT_MOUSE_DRAG_IMAGE { "/system/etc/device_status/drag_icon/Copy_Drag.svg" };
29
30 namespace OHOS {
31 constexpr int MAX_SET_NUMBER = 1000;
CreateDecoderAdapter()32 std::shared_ptr<OhosImageDecoderAdapter> CreateDecoderAdapter()
33 {
34 return OhosAdapterHelper::GetInstance().CreateOhosImageDecoderAdapter();
35 }
36
ReleasePixelMapTest(const std::shared_ptr<OhosImageDecoderAdapter> & adapter,const uint8_t * data,size_t size)37 void ReleasePixelMapTest(const std::shared_ptr<OhosImageDecoderAdapter>& adapter, const uint8_t* data, size_t size)
38 {
39 FuzzedDataProvider dataProvider(data, size);
40 uint32_t len = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
41 std::unique_ptr<uint8_t[]> rawData;
42
43 adapter->ParseImageInfo(rawData.get(), len);
44 adapter->DecodeToPixelMap(rawData.get(), len);
45 adapter->ReleasePixelMap();
46 }
47
ApplyOhosImageDecoderAdapterFuzzTest(const uint8_t * data,size_t size)48 bool ApplyOhosImageDecoderAdapterFuzzTest(const uint8_t* data, size_t size)
49 {
50 if ((data == nullptr) || (size == 0)) {
51 return true;
52 }
53 size_t callCount = data[0] % 10;
54 for (size_t i = 0; i < callCount; ++i) {
55 auto adapter = CreateDecoderAdapter();
56 if (!adapter) {
57 return false;
58 }
59 ReleasePixelMapTest(adapter, data, size);
60 }
61 return true;
62 }
63
64 } // namespace OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69 OHOS::ApplyOhosImageDecoderAdapterFuzzTest(data, size);
70 return 0;
71 }
72