1 /*
2 * Copyright (c) 2024 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 "moon_capture_boost_feature_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "camera_log.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 static constexpr int32_t MAX_CODE_LEN = 512;
25 static constexpr int32_t MIN_SIZE_NUM = 4;
26 static const uint8_t* RAW_DATA = nullptr;
27 const size_t THRESHOLD = 10;
28 static size_t g_dataSize = 0;
29 static size_t g_pos;
30
31 std::shared_ptr<MoonCaptureBoostFeature> MoonCaptureBoostFeatureFuzzer::fuzz_{nullptr};
32
33 /*
34 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
35 * tips: only support basic type
36 */
37 template<class T>
GetData()38 T GetData()
39 {
40 T object {};
41 size_t objectSize = sizeof(object);
42 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
43 return object;
44 }
45 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
46 if (ret != EOK) {
47 return {};
48 }
49 g_pos += objectSize;
50 return object;
51 }
52
53 template<class T>
GetArrLength(T & arr)54 uint32_t GetArrLength(T& arr)
55 {
56 if (arr == nullptr) {
57 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
58 return 0;
59 }
60 return sizeof(arr) / sizeof(arr[0]);
61 }
62
MoonCaptureBoostFeatureFuzzTest()63 void MoonCaptureBoostFeatureFuzzer::MoonCaptureBoostFeatureFuzzTest()
64 {
65 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
66 return;
67 }
68 SceneMode relatedMode = SceneMode::CAPTURE;
69 auto deviceAbility = std::make_shared<OHOS::Camera::CameraMetadata>(1, 1);
70 fuzz_ = std::make_shared<MoonCaptureBoostFeature>(relatedMode, deviceAbility);
71 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
72 fuzz_->GetSketchEnableRatio();
73 auto inputZoomRatio = GetData<float>();
74 fuzz_->GetSketchReferenceFovRatio(inputZoomRatio);
75 }
76
Test()77 void Test()
78 {
79 auto moonCaptureBoostFeature = std::make_unique<MoonCaptureBoostFeatureFuzzer>();
80 if (moonCaptureBoostFeature == nullptr) {
81 MEDIA_INFO_LOG("moonCaptureBoostFeature is null");
82 return;
83 }
84 moonCaptureBoostFeature->MoonCaptureBoostFeatureFuzzTest();
85 }
86
87 typedef void (*TestFuncs[1])();
88
89 TestFuncs g_testFuncs = {
90 Test,
91 };
92
FuzzTest(const uint8_t * rawData,size_t size)93 bool FuzzTest(const uint8_t* rawData, size_t size)
94 {
95 // initialize data
96 RAW_DATA = rawData;
97 g_dataSize = size;
98 g_pos = 0;
99
100 uint32_t code = GetData<uint32_t>();
101 uint32_t len = GetArrLength(g_testFuncs);
102 if (len > 0) {
103 g_testFuncs[code % len]();
104 } else {
105 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
106 }
107
108 return true;
109 }
110 } // namespace CameraStandard
111 } // namespace OHOS
112
113 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)114 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
115 {
116 if (size < OHOS::CameraStandard::THRESHOLD) {
117 return 0;
118 }
119
120 OHOS::CameraStandard::FuzzTest(data, size);
121 return 0;
122 }