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 "sketch_wrapper_fuzzer.h"
17 #include "camera_device.h"
18 #include "camera_log.h"
19 #include "camera_output_capability.h"
20 #include "capture_scene_const.h"
21 #include <cstdint>
22 #include "input/camera_manager.h"
23 #include "message_parcel.h"
24 #include <cstdint>
25 #include <memory>
26 #include "token_setproc.h"
27 #include "nativetoken_kit.h"
28 #include "accesstoken_kit.h"
29 #include "securec.h"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace CameraStandard {
35 const uint32_t METADATA_ITEM_SIZE = 20;
36 const uint32_t METADATA_DATA_SIZE = 200;
37 static constexpr int32_t MAX_CODE_LEN = 512;
38 static constexpr int32_t MIN_SIZE_NUM = 4;
39 static const uint8_t* RAW_DATA = nullptr;
40 const size_t THRESHOLD = 10;
41 static size_t g_dataSize = 0;
42 static size_t g_pos;
43 const uint32_t PREVIEW_WIDTH_1 = 1440;
44 const uint32_t PREVIEW_HEIGHT_1 = 1080;
45 const uint32_t PREVIEW_WIDTH_2 = 640;
46 const uint32_t PREVIEW_HEIGHT_2 = 480;
47
48 std::shared_ptr<SketchWrapper> SketchWrapperFuzzer::fuzz_{nullptr};
49
50 /*
51 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
52 * tips: only support basic type
53 */
54 template<class T>
GetData()55 T GetData()
56 {
57 T object {};
58 size_t objectSize = sizeof(object);
59 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
60 return object;
61 }
62 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
63 if (ret != EOK) {
64 return {};
65 }
66 g_pos += objectSize;
67 return object;
68 }
69
70 template<class T>
GetArrLength(T & arr)71 uint32_t GetArrLength(T& arr)
72 {
73 if (arr == nullptr) {
74 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
75 return 0;
76 }
77 return sizeof(arr) / sizeof(arr[0]);
78 }
79
SketchWrapperFuzzTest1()80 void SketchWrapperFuzzer::SketchWrapperFuzzTest1()
81 {
82 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
83 return;
84 }
85 auto cameraManager_ = CameraManager::GetInstance();
86 CameraFormat previewFormat = CAMERA_FORMAT_YUV_420_SP;
87 Size previewSize;
88 previewSize.width = PREVIEW_WIDTH_1;
89 previewSize.height = PREVIEW_HEIGHT_1;
90 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
91 Profile previewProfile = Profile(previewFormat, previewSize);
92 sptr<CaptureOutput> previewOutput = cameraManager_->CreatePreviewOutput(previewProfile, surface);
93 Size sketchSize;
94 sketchSize.width = PREVIEW_WIDTH_2;
95 sketchSize.height = PREVIEW_HEIGHT_2;
96 fuzz_ = std::make_shared<SketchWrapper>(previewOutput->GetStream(), sketchSize);
97 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
98 std::shared_ptr<OHOS::Camera::CameraMetadata> deviceMetadata =
99 std::make_shared<OHOS::Camera::CameraMetadata>(METADATA_ITEM_SIZE, METADATA_DATA_SIZE);
100 sptr<CaptureSession> session = CameraManager::GetInstance()->CreateCaptureSession();
101 auto modeName = session->GetFeaturesMode();
102 fuzz_->Init(deviceMetadata, modeName);
103 float floatNum = GetData<float>();
104 fuzz_->UpdateSketchRatio(floatNum);
105 fuzz_->AttachSketchSurface(nullptr);
106 fuzz_->StartSketchStream();
107 fuzz_->StopSketchStream();
108 SketchStatus sketchStatus = SketchStatus::STARTING;
109 std::shared_ptr<SceneFeaturesMode> sceneFeaturesMode = std::make_shared<SceneFeaturesMode>();
110 fuzz_->OnSketchStatusChanged(sketchStatus, *sceneFeaturesMode);
111 fuzz_->OnSketchStatusChanged(*sceneFeaturesMode);
112 fuzz_->UpdateSketchStaticInfo(deviceMetadata);
113 fuzz_->GetSceneFeaturesModeFromModeData(floatNum);
114 fuzz_->UpdateSketchEnableRatio(deviceMetadata);
115 SketchReferenceFovRange sketchReferenceFovRange = { .zoomMin = -1.0f, .zoomMax = -1.0f, .referenceValue = -1.0f };
116 fuzz_->InsertSketchReferenceFovRatioMapValue(*sceneFeaturesMode, sketchReferenceFovRange);
117 fuzz_->InsertSketchEnableRatioMapValue(*sceneFeaturesMode, floatNum);
118 fuzz_->UpdateSketchReferenceFovRatio(deviceMetadata);
119 }
120
SketchWrapperFuzzTest2()121 void SketchWrapperFuzzer::SketchWrapperFuzzTest2()
122 {
123 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
124 return;
125 }
126 auto cameraManager_ = CameraManager::GetInstance();
127 CameraFormat previewFormat = CAMERA_FORMAT_YUV_420_SP;
128 Size previewSize;
129 previewSize.width = PREVIEW_WIDTH_1;
130 previewSize.height = PREVIEW_HEIGHT_1;
131 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
132 Profile previewProfile = Profile(previewFormat, previewSize);
133 sptr<CaptureOutput> previewOutput = cameraManager_->CreatePreviewOutput(previewProfile, surface);
134 Size sketchSize;
135 sketchSize.width = PREVIEW_WIDTH_2;
136 sketchSize.height = PREVIEW_HEIGHT_2;
137 fuzz_ = std::make_shared<SketchWrapper>(previewOutput->GetStream(), sketchSize);
138 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
139 std::shared_ptr<OHOS::Camera::CameraMetadata> deviceMetadata =
140 std::make_shared<OHOS::Camera::CameraMetadata>(METADATA_ITEM_SIZE, METADATA_DATA_SIZE);
141 sptr<CaptureSession> session = CameraManager::GetInstance()->CreateCaptureSession();
142 auto modeName = session->GetFeaturesMode();
143 fuzz_->Init(deviceMetadata, modeName);
144 float floatNum = GetData<float>();
145 std::shared_ptr<SceneFeaturesMode> sceneFeaturesMode = std::make_shared<SceneFeaturesMode>();
146 camera_metadata_item_t item;
147 OHOS::Camera::FindCameraMetadataItem(deviceMetadata->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
148 fuzz_->UpdateSketchReferenceFovRatio(item);
149 fuzz_->GetMoonCaptureBoostAbilityItem(deviceMetadata, item);
150 fuzz_->UpdateSketchConfigFromMoonCaptureBoostConfig(deviceMetadata);
151 fuzz_->GetSketchReferenceFovRatio(*sceneFeaturesMode, floatNum);
152 fuzz_->GetSketchEnableRatio(*sceneFeaturesMode);
153 fuzz_->UpdateZoomRatio(floatNum);
154 fuzz_->AutoStream();
155 camera_device_metadata_tag_t tag = OHOS_CONTROL_ZOOM_RATIO;
156
157 fuzz_->OnMetadataChangedMacro(*sceneFeaturesMode, tag, item);
158 fuzz_->Destroy();
159 fuzz_->sketchStream_ = nullptr;
160 fuzz_->StartSketchStream();
161 fuzz_->StopSketchStream();
162 }
163
Test()164 void Test()
165 {
166 auto sketchWrapper = std::make_unique<SketchWrapperFuzzer>();
167 if (sketchWrapper == nullptr) {
168 MEDIA_INFO_LOG("sketchWrapper is null");
169 return;
170 }
171 sketchWrapper->SketchWrapperFuzzTest1();
172 sketchWrapper->SketchWrapperFuzzTest2();
173 }
174
175 typedef void (*TestFuncs[1])();
176
177 TestFuncs g_testFuncs = {
178 Test,
179 };
180
FuzzTest(const uint8_t * rawData,size_t size)181 bool FuzzTest(const uint8_t* rawData, size_t size)
182 {
183 // initialize data
184 RAW_DATA = rawData;
185 g_dataSize = size;
186 g_pos = 0;
187
188 uint32_t code = GetData<uint32_t>();
189 uint32_t len = GetArrLength(g_testFuncs);
190 if (len > 0) {
191 g_testFuncs[code % len]();
192 } else {
193 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
194 }
195
196 return true;
197 }
198 } // namespace CameraStandard
199 } // namespace OHOS
200
201 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)202 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
203 {
204 if (size < OHOS::CameraStandard::THRESHOLD) {
205 return 0;
206 }
207
208 OHOS::CameraStandard::FuzzTest(data, size);
209 return 0;
210 }