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 "hstream_operator_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "camera_log.h"
21 #include "message_parcel.h"
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "accesstoken_kit.h"
25 #include "system_ability_definition.h"
26 #include "iservice_registry.h"
27 #include "ipc_skeleton.h"
28 #include "buffer_extra_data_impl.h"
29 #include "picture.h"
30 #include "camera_server_photo_proxy.h"
31 #include "camera_photo_proxy.h"
32
33 namespace OHOS {
34 namespace CameraStandard {
35 static constexpr int32_t MAX_CODE_LEN = 512;
36 static constexpr int32_t MIN_SIZE_NUM = 36;
37 static constexpr int32_t NUM_1 = 1;
38 static const uint8_t* RAW_DATA = nullptr;
39 const size_t THRESHOLD = 10;
40 static size_t g_dataSize = 0;
41 static size_t g_pos;
42
43 sptr<HStreamOperator> HStreamOperatorFuzzer::fuzz_{nullptr};
44
45 /*
46 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
47 * tips: only support basic type
48 */
49 template<class T>
GetData()50 T GetData()
51 {
52 T object {};
53 size_t objectSize = sizeof(object);
54 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
55 return object;
56 }
57 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
58 if (ret != EOK) {
59 return {};
60 }
61 g_pos += objectSize;
62 return object;
63 }
64
65 template<class T>
GetArrLength(T & arr)66 uint32_t GetArrLength(T& arr)
67 {
68 if (arr == nullptr) {
69 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
70 return 0;
71 }
72 return sizeof(arr) / sizeof(arr[0]);
73 }
74
HStreamOperatorFuzzTest()75 void HStreamOperatorFuzzer::HStreamOperatorFuzzTest()
76 {
77 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
78 return;
79 }
80 fuzz_ = HStreamOperator::NewInstance(0, 0);
81 CHECK_ERROR_RETURN_LOG(!fuzz_, "NewInstance Error");
82 int32_t streamId = GetData<int32_t>();
83 fuzz_->GetStreamByStreamID(streamId);
84 int32_t rotation = GetData<int32_t>();
85 int32_t format = GetData<int32_t>();
86 int32_t captureId = GetData<int32_t>();
87 int64_t timestamp = GetData<int64_t>();
88 fuzz_->StartMovingPhotoEncode(rotation, timestamp, format, captureId);
89 fuzz_->StartRecord(timestamp, rotation, captureId);
90 fuzz_->GetHdiStreamByStreamID(streamId);
91 fuzz_->EnableMovingPhotoMirror(GetData<bool>(), GetData<bool>());
92 ColorSpace getColorSpace;
93 fuzz_->GetActiveColorSpace(getColorSpace);
94 constexpr int32_t executionModeCount = static_cast<int32_t>(ColorSpace::P3_PQ_LIMIT) + NUM_1;
95 ColorSpace colorSpace = static_cast<ColorSpace>(GetData<uint8_t>() % executionModeCount);
96 fuzz_->SetColorSpace(colorSpace, GetData<bool>());
97 fuzz_->GetStreamOperator();
98 std::vector<int32_t> results = {GetData<uint32_t>()};
99 fuzz_->ReleaseStreams();
100 std::vector<HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos;
101 fuzz_->CreateStreams(streamInfos);
102 std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
103 const int32_t NUM_10 = 10;
104 const int32_t NUM_100 = 100;
105 settings = std::make_shared<OHOS::Camera::CameraMetadata>(NUM_10, NUM_100);
106 int32_t operationMode = GetData<int32_t>();
107 fuzz_->CommitStreams(settings, operationMode);
108 fuzz_->UpdateStreams(streamInfos);
109 fuzz_->CreateAndCommitStreams(streamInfos, settings, operationMode);
110 const std::vector<int32_t> streamIds;
111 fuzz_->OnCaptureStarted(captureId, streamIds);
112 const std::vector<OHOS::HDI::Camera::V1_2::CaptureStartedInfo> infos;
113 fuzz_->OnCaptureStarted_V1_2(captureId, infos);
114 const std::vector<CaptureEndedInfo> infos_OnCaptureEnded;
115 fuzz_->OnCaptureEnded(captureId, infos_OnCaptureEnded);
116 const std::vector<OHOS::HDI::Camera::V1_3::CaptureEndedInfoExt> infos_OnCaptureEndedExt;
117 fuzz_->OnCaptureEndedExt(captureId, infos_OnCaptureEndedExt);
118 const std::vector<CaptureErrorInfo> infos_OnCaptureError;
119 fuzz_->OnCaptureError(captureId, infos_OnCaptureError);
120 fuzz_->OnFrameShutter(captureId, results, timestamp);
121 fuzz_->OnFrameShutterEnd(captureId, results, timestamp);
122 fuzz_->OnCaptureReady(captureId, results, timestamp);
123 }
124
Test()125 void Test()
126 {
127 auto HStreamOperator = std::make_unique<HStreamOperatorFuzzer>();
128 if (HStreamOperator == nullptr) {
129 MEDIA_INFO_LOG("HStreamOperator is null");
130 return;
131 }
132 HStreamOperator->HStreamOperatorFuzzTest();
133 }
134
135 typedef void (*TestFuncs[1])();
136
137 TestFuncs g_testFuncs = {
138 Test,
139 };
140
FuzzTest(const uint8_t * rawData,size_t size)141 bool FuzzTest(const uint8_t* rawData, size_t size)
142 {
143 // initialize data
144 RAW_DATA = rawData;
145 g_dataSize = size;
146 g_pos = 0;
147
148 uint32_t code = GetData<uint32_t>();
149 uint32_t len = GetArrLength(g_testFuncs);
150 if (len > 0) {
151 g_testFuncs[code % len]();
152 } else {
153 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
154 }
155
156 return true;
157 }
158 } // namespace CameraStandard
159 } // namespace OHOS
160
161 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)162 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
163 {
164 if (size < OHOS::CameraStandard::THRESHOLD) {
165 return 0;
166 }
167
168 OHOS::CameraStandard::FuzzTest(data, size);
169 return 0;
170 }
171