• 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 "portrait_session_fuzzer.h"
17 #include "access_token.h"
18 #include "camera_log.h"
19 #include "camera_output_capability.h"
20 #include "input/camera_manager.h"
21 #include "input/camera_manager_for_sys.h"
22 #include "message_parcel.h"
23 #include "token_setproc.h"
24 #include "nativetoken_kit.h"
25 #include "accesstoken_kit.h"
26 #include "securec.h"
27 #include "ipc_skeleton.h"
28 #include <cstdint>
29 #include <vector>
30 #include "camera_util.h"
31 #include "hap_token_info.h"
32 #include "metadata_utils.h"
33 #include "surface.h"
34 #include "os_account_manager.h"
35 #include <fuzzer/FuzzedDataProvider.h>
36 #include "test_token.h"
37 #include "test_token.h"
38 
39 using namespace OHOS;
40 using namespace OHOS::CameraStandard;
41 static constexpr int32_t MIN_SIZE_NUM = 4;
42 sptr<CameraManager> cameraManager_ = nullptr;
43 sptr<CameraManagerForSys> cameraManagerForSys_ = nullptr;
44 std::vector<Profile> previewProfile_ = {};
45 std::vector<Profile> photoProfile_ = {};
46 bool g_preIsSupportedPortraitmode = false;
47 bool g_phoIsSupportedPortraitmode = false;
48 
CreatePreviewOutput()49 sptr<CaptureOutput> CreatePreviewOutput()
50 {
51     previewProfile_ = {};
52     std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
53     if (!cameraManager_ || cameras.empty()) {
54         return nullptr;
55     }
56     g_preIsSupportedPortraitmode = false;
57     for (sptr<CameraDevice> camDevice : cameras) {
58         std::vector<SceneMode> modes = cameraManager_->GetSupportedModes(camDevice);
59         if (find(modes.begin(), modes.end(), SceneMode::PORTRAIT) != modes.end()) {
60             g_preIsSupportedPortraitmode = true;
61         }
62 
63         if (!g_preIsSupportedPortraitmode) {
64             continue;
65         }
66 
67         auto outputCapability = cameraManager_->GetSupportedOutputCapability(camDevice, static_cast<int32_t>(PORTRAIT));
68         if (!outputCapability) {
69             return nullptr;
70         }
71 
72         previewProfile_ = outputCapability->GetPreviewProfiles();
73         if (previewProfile_.empty()) {
74             return nullptr;
75         }
76 
77         sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
78         if (surface == nullptr) {
79             return nullptr;
80         }
81         return cameraManager_->CreatePreviewOutput(previewProfile_[0], surface);
82     }
83     return nullptr;
84 }
85 
CreatePhotoOutput()86 sptr<CaptureOutput> CreatePhotoOutput()
87 {
88     photoProfile_ = {};
89     std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
90     if (!cameraManager_ || cameras.empty()) {
91         return nullptr;
92     }
93     g_phoIsSupportedPortraitmode = false;
94     for (sptr<CameraDevice> camDevice : cameras) {
95         std::vector<SceneMode> modes = cameraManager_->GetSupportedModes(camDevice);
96         if (find(modes.begin(), modes.end(), SceneMode::PORTRAIT) != modes.end()) {
97             g_phoIsSupportedPortraitmode = true;
98         }
99 
100         if (!g_phoIsSupportedPortraitmode) {
101             continue;
102         }
103 
104         auto outputCapability = cameraManager_->GetSupportedOutputCapability(camDevice, static_cast<int32_t>(PORTRAIT));
105         if (!outputCapability) {
106             return nullptr;
107         }
108 
109         photoProfile_ = outputCapability->GetPhotoProfiles();
110         if (photoProfile_.empty()) {
111             return nullptr;
112         }
113 
114         sptr<IConsumerSurface> surface = IConsumerSurface::Create();
115         if (surface == nullptr) {
116             return nullptr;
117         }
118         sptr<IBufferProducer> surfaceProducer = surface->GetProducer();
119         return cameraManager_->CreatePhotoOutput(photoProfile_[0], surfaceProducer);
120     }
121     return nullptr;
122 }
123 
PortraitSessionFuzzTest(FuzzedDataProvider & fdp)124 void PortraitSessionFuzzTest(FuzzedDataProvider& fdp)
125 {
126     cameraManager_ = CameraManager::GetInstance();
127     cameraManagerForSys_ = CameraManagerForSys::GetInstance();
128     std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
129     CHECK_RETURN_ELOG(cameras.empty(), "PortraitSessionFuzzer: GetCameraDeviceListFromServer Error");
130     sptr<CaptureInput> input = cameraManager_->CreateCameraInput(cameras[0]);
131     CHECK_RETURN_ELOG(!input, "CreateCameraInput Error");
132     sptr<CameraInput> camInput = (sptr<CameraInput> &)input;
133     std::string cameraSettings = camInput->GetCameraSettings();
134     camInput->SetCameraSettings(cameraSettings);
135     camInput->GetCameraDevice()->Open();
136     sptr<CaptureOutput> preview = CreatePreviewOutput();
137     sptr<CaptureOutput> photo = CreatePhotoOutput();
138     sptr<CaptureSession> captureSessionForSys = cameraManagerForSys_->CreateCaptureSessionForSys(SceneMode::PORTRAIT);
139     sptr<PortraitSession> portraitSession = nullptr;
140     portraitSession = static_cast<PortraitSession *> (captureSessionForSys.GetRefPtr());
141     portraitSession->BeginConfig();
142     portraitSession->AddInput(input);
143     sptr<CameraDevice> info = captureSessionForSys->innerInputDevice_->GetCameraDeviceInfo();
144     info->modePreviewProfiles_.emplace(static_cast<int32_t>(PORTRAIT), previewProfile_);
145     info->modePhotoProfiles_.emplace(static_cast<int32_t>(PORTRAIT), photoProfile_);
146     portraitSession->AddOutput(preview);
147     portraitSession->AddOutput(photo);
148     portraitSession->CommitConfig();
149     portraitSession->LockForControl();
150     auto portraitEffect = portraitSession->GetSupportedPortraitEffects();
151     portraitSession->GetPortraitEffect();
152     if (!portraitEffect.empty()) {
153         uint8_t portraitEnum = fdp.ConsumeIntegralInRange(0, 5);
154         portraitSession->SetPortraitEffect(portraitEffect[portraitEnum]);
155     }
156     portraitSession->CanAddOutput(photo);
157     portraitSession->UnlockForControl();
158 }
159 
Test(uint8_t * data,size_t size)160 void Test(uint8_t* data, size_t size)
161 {
162     FuzzedDataProvider fdp(data, size);
163     if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
164         return;
165     }
166     CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
167     PortraitSessionFuzzTest(fdp);
168 }
169 
170 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)171 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
172 {
173     Test(data, size);
174     return 0;
175 }