• 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 
20 #include "accesstoken_kit.h"
21 #include "camera_log.h"
22 #include "ipc_skeleton.h"
23 #include "message_parcel.h"
24 #include "nativetoken_kit.h"
25 #include "os_account_manager.h"
26 #include "profession_session_fuzzer.h"
27 #include "securec.h"
28 #include "test_token.h"
29 #include "token_setproc.h"
30 
31 namespace OHOS {
32 namespace CameraStandard {
33 static constexpr int32_t MAX_CODE_LEN = 512;
34 static constexpr int32_t MIN_SIZE_NUM = 4;
35 static const uint8_t* RAW_DATA = nullptr;
36 const size_t THRESHOLD = 10;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39 
40 const int32_t NUM_FOUR = 4;
41 const int32_t NUM_THREE = 3;
42 sptr<ProfessionSession> ProfessionSessionFuzzer::fuzz_{nullptr};
43 sptr<CameraManager> manager_;
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 
CreatePreviewOutput(Profile & profile)75 sptr<CaptureOutput> CreatePreviewOutput(Profile& profile)
76 {
77     sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
78     Size previewSize = profile.GetSize();
79     previewSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(profile.GetCameraFormat()));
80     previewSurface->SetDefaultWidthAndHeight(previewSize.width, previewSize.height);
81 
82     sptr<IBufferProducer> bp = previewSurface->GetProducer();
83     sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
84 
85     sptr<CaptureOutput> previewOutput = nullptr;
86     previewOutput = manager_->CreatePreviewOutput(profile, pSurface);
87     return previewOutput;
88 }
89 
CreateVideoOutput(VideoProfile & videoProfile)90 sptr<CaptureOutput> CreateVideoOutput(VideoProfile& videoProfile)
91 {
92     sptr<IConsumerSurface> surface = IConsumerSurface::Create();
93     sptr<IBufferProducer> videoProducer = surface->GetProducer();
94     sptr<Surface> videoSurface = Surface::CreateSurfaceAsProducer(videoProducer);
95     sptr<CaptureOutput> videoOutput = nullptr;
96     videoOutput = manager_->CreateVideoOutput(videoProfile, videoSurface);
97     return videoOutput;
98 }
99 
ProfessionSessionFuzzTest1()100 void ProfessionSessionFuzzer::ProfessionSessionFuzzTest1()
101 {
102     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
103         return;
104     }
105     MeteringMode meteringModes = static_cast<MeteringMode>(GetData<int32_t>() % NUM_FOUR);
106     std::vector<MeteringMode> supportedMeteringModes = {};
107     supportedMeteringModes.push_back(meteringModes);
108     fuzz_->GetSupportedMeteringModes(supportedMeteringModes);
109     bool isSupported = GetData<bool>();
110     fuzz_->IsMeteringModeSupported(meteringModes, isSupported);
111     fuzz_->SetMeteringMode(meteringModes);
112     fuzz_->GetMeteringMode(meteringModes);
113 }
114 
ProfessionSessionFuzzTest2()115 void ProfessionSessionFuzzer::ProfessionSessionFuzzTest2()
116 {
117     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
118         return;
119     }
120     bool isSupported = GetData<bool>();
121     fuzz_->HasFlash(isSupported);
122     ColorEffect colorEffects = static_cast<ColorEffect>(GetData<int32_t>() % NUM_FOUR);
123     vector<ColorEffect> supportedColorEffects;
124     fuzz_->GetSupportedColorEffects(supportedColorEffects);
125     fuzz_->GetColorEffect(colorEffects);
126     fuzz_->SetColorEffect(colorEffects);
127     vector<int32_t> isoRange;
128     fuzz_->GetIsoRange(isoRange);
129     int32_t range = GetData<int32_t>();
130     fuzz_->SetISO(range);
131     fuzz_->GetISO(range);
132     fuzz_->IsManualIsoSupported();
133     FocusMode focusMode = static_cast<FocusMode>(GetData<int32_t>() % NUM_FOUR);
134     vector<FocusMode> supportedFocusModes;
135     supportedFocusModes.push_back(focusMode);
136     fuzz_->GetSupportedFocusModes(supportedFocusModes);
137     fuzz_->IsFocusModeSupported(focusMode, isSupported);
138     fuzz_->SetFocusMode(focusMode);
139     fuzz_->GetFocusMode(focusMode);
140     ExposureHintMode exposureHintModes = static_cast<ExposureHintMode>(GetData<int32_t>() % NUM_THREE);
141     vector<ExposureHintMode> supportedExposureHintModes;
142     fuzz_->GetSupportedExposureHintModes(supportedExposureHintModes);
143     fuzz_->SetExposureHintMode(exposureHintModes);
144     fuzz_->GetExposureHintMode(exposureHintModes);
145     FocusAssistFlashMode focusAssistFlashModes = static_cast<FocusAssistFlashMode>(GetData<int32_t>() % NUM_FOUR);
146     vector<FocusAssistFlashMode> supportedFocusAssistFlashModes;
147     fuzz_->GetSupportedFocusAssistFlashModes(supportedFocusAssistFlashModes);
148     fuzz_->IsFocusAssistFlashModeSupported(focusAssistFlashModes, isSupported);
149     fuzz_->SetFocusAssistFlashMode(focusAssistFlashModes);
150     fuzz_->GetFocusAssistFlashMode(focusAssistFlashModes);
151 }
152 
Test()153 void Test()
154 {
155     auto professionSession = std::make_unique<ProfessionSessionFuzzer>();
156     if (professionSession == nullptr) {
157         MEDIA_INFO_LOG("professionSession is null");
158         return;
159     }
160     CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
161     manager_ = CameraManager::GetInstance();
162     sptr<CaptureSessionForSys> captureSessionForSys =
163         CameraManagerForSys::GetInstance()->CreateCaptureSessionForSys(SceneMode::PROFESSIONAL_VIDEO);
164     ProfessionSessionFuzzer::fuzz_ = static_cast<ProfessionSession*>(captureSessionForSys.GetRefPtr());
165     CHECK_RETURN_ELOG(!ProfessionSessionFuzzer::fuzz_, "Create fuzz_ Error");
166     SceneMode sceneMode = SceneMode::PROFESSIONAL_VIDEO;
167     std::vector<sptr<CameraDevice>> cameras;
168     cameras = manager_->GetSupportedCameras();
169     CHECK_RETURN_ELOG(cameras.empty(), "GetCameraDeviceListFromServer Error");
170     sptr<CaptureInput> input = manager_->CreateCameraInput(cameras[0]);
171     CHECK_RETURN_ELOG(!input, "CreateCameraInput Error");
172     sptr<CameraOutputCapability> modeAbility =
173         manager_->GetSupportedOutputCapability(cameras[0], sceneMode);
174     captureSessionForSys->BeginConfig();
175     captureSessionForSys->AddInput(input);
176     input->Open();
177     Profile previewProfile;
178     VideoProfile videoProfile;
179     auto previewProfiles = modeAbility->GetPreviewProfiles();
180     auto videoProfiles = modeAbility->GetVideoProfiles();
181     for (const auto &vProfile : videoProfiles) {
182         for (const auto &pProfile : previewProfiles) {
183             if (vProfile.size_.width == pProfile.size_.width) {
184                 previewProfile = pProfile;
185                 videoProfile = vProfile;
186                 break;
187             }
188         }
189     }
190     sptr<CaptureOutput> previewOutput = CreatePreviewOutput(previewProfile);
191     captureSessionForSys->AddOutput(previewOutput);
192     sptr<CaptureOutput> videoOutput = CreateVideoOutput(videoProfile);
193     captureSessionForSys->AddOutput(videoOutput);
194     captureSessionForSys->CommitConfig();
195     professionSession->ProfessionSessionFuzzTest1();
196     professionSession->ProfessionSessionFuzzTest2();
197 }
198 
199 typedef void (*TestFuncs[1])();
200 TestFuncs g_testFuncs = {
201     Test,
202 };
203 
FuzzTest(const uint8_t * rawData,size_t size)204 bool FuzzTest(const uint8_t* rawData, size_t size)
205 {
206     // initialize data
207     RAW_DATA = rawData;
208     g_dataSize = size;
209     g_pos = 0;
210 
211     uint32_t code = GetData<uint32_t>();
212     uint32_t len = GetArrLength(g_testFuncs);
213     if (len > 0) {
214         g_testFuncs[code % len]();
215     } else {
216         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
217     }
218 
219     return true;
220 }
221 
222 } // namespace CameraStandard
223 } // namespace OHOS
224 
225 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)226 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
227 {
228     if (size < OHOS::CameraStandard::THRESHOLD) {
229         return 0;
230     }
231 
232     OHOS::CameraStandard::FuzzTest(data, size);
233     return 0;
234 }