• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "image_source_proxy_fuzzer.h"
17 #include "camera_log.h"
18 
19 namespace OHOS {
20 namespace CameraStandard {
21 static constexpr int32_t MIN_SIZE_NUM = 64;
22 
23 std::shared_ptr<ImageSourceProxy> ImageSourceProxyFuzzer::imageSourceProxyFuzz_{nullptr};
24 
ImageSourceProxyFuzzerTest(FuzzedDataProvider & fdp)25 void ImageSourceProxyFuzzer::ImageSourceProxyFuzzerTest(FuzzedDataProvider& fdp)
26 {
27     if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
28         return;
29     }
30     CHECK_EXECUTE(imageSourceProxyFuzz_ == nullptr,
31         imageSourceProxyFuzz_ = ImageSourceProxy::CreateImageSourceProxy());
32     CHECK_RETURN_ELOG(!imageSourceProxyFuzz_, "CreateImageSourceProxy Error");
33     size_t width = 256;
34     size_t height = 256;
35     size_t blockNum = ((width + 4 - 1) / 4) * ((height + 4 - 1) / 4);
36     size_t dataSize = blockNum * 16 + 16;
37     uint8_t* data = (uint8_t*)malloc(dataSize);
38     uint32_t size = fdp.ConsumeIntegral<uint32_t>();
39     if (size > dataSize) {
40         size = static_cast<uint32_t>(dataSize);
41     }
42     Media::SourceOptions opts;
43     uint32_t errorCode = fdp.ConsumeIntegral<uint32_t>();
44     imageSourceProxyFuzz_->CreateImageSource(data, size, opts, errorCode);
45 
46     Media::DecodeOptions decodeOpts;
47     imageSourceProxyFuzz_->CreatePixelMap(decodeOpts, errorCode);
48 }
49 
Test(uint8_t * data,size_t size)50 void Test(uint8_t* data, size_t size)
51 {
52     FuzzedDataProvider fdp(data, size);
53     auto imageSourceProxyFuzzer = std::make_unique<ImageSourceProxyFuzzer>();
54     if (imageSourceProxyFuzzer == nullptr) {
55         MEDIA_INFO_LOG("imageSourceProxyFuzzer is null");
56         return;
57     }
58     imageSourceProxyFuzzer->ImageSourceProxyFuzzerTest(fdp);
59 }
60 
61 } // namespace CameraStandard
62 } // namespace OHOS
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
66 {
67     OHOS::CameraStandard::Test(data, size);
68     return 0;
69 }