• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstdint>
17 #include <memory>
18 
19 #include "accesstoken_kit.h"
20 #include "camera_device.h"
21 #include "camera_log.h"
22 #include "camera_output_capability.h"
23 #include "capture_scene_const.h"
24 #include "input/camera_manager.h"
25 #include "message_parcel.h"
26 #include "nativetoken_kit.h"
27 #include "photo_output_fuzzer.h"
28 #include "token_setproc.h"
29 #include "test_token.h"
30 
31 namespace OHOS {
32 namespace CameraStandard {
33 namespace PhotoOutputFuzzer {
34 const int32_t LIMITSIZE = 128;
35 const int32_t CONST_2 = 2;
36 
Test(uint8_t * rawData,size_t size)37 void Test(uint8_t *rawData, size_t size)
38 {
39     if (rawData == nullptr || size < LIMITSIZE) {
40         return;
41     }
42     CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
43     auto manager = CameraManager::GetInstance();
44     CHECK_RETURN_ELOG(!manager, "PhotoOutputFuzzer: Get CameraManager instance Error");
45     auto cameras = manager->GetSupportedCameras();
46     CHECK_RETURN_ELOG(cameras.size() < CONST_2, "PhotoOutputFuzzer: GetSupportedCameras Error");
47     MessageParcel data;
48     data.WriteRawData(rawData, size);
49     auto camera = cameras[data.ReadUint32() % cameras.size()];
50     CHECK_RETURN_ELOG(!camera, "PhotoOutputFuzzer: camera is null");
51     int32_t mode = data.ReadInt32() % (SceneMode::APERTURE_VIDEO + CONST_2);
52     auto capability = manager->GetSupportedOutputCapability(camera, mode);
53     CHECK_RETURN_ELOG(!capability, "PhotoOutputFuzzer: GetSupportedOutputCapability Error");
54     auto profiles = capability->GetPhotoProfiles();
55     CHECK_RETURN_ELOG(profiles.empty(), "PhotoOutputFuzzer: GetPhotoProfiles empty");
56     Profile profile = profiles[data.ReadUint32()  % profiles.size()];
57     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
58     CHECK_RETURN_ELOG(!photoSurface, "PhotoOutputFuzzer: create photoSurface Error");
59     sptr<IBufferProducer> producer = photoSurface->GetProducer();
60     CHECK_RETURN_ELOG(!producer, "PhotoOutputFuzzer: GetProducer Error");
61     auto output = manager->CreatePhotoOutput(profile, producer);
62     CHECK_RETURN_ELOG(!output, "PhotoOutputFuzzer: CreatePhotoOutput Error");
63     std::shared_ptr<PhotoCaptureSetting> setting = std::make_shared<PhotoCaptureSetting>();
64     sptr<HStreamCaptureCallbackImpl> callback = new (std::nothrow) HStreamCaptureCallbackImpl(output);
65     TestOutput1(output, rawData, size);
66     TestOutput2(output, rawData, size);
67     CaptureSetting(setting, rawData, size);
68     CaptureCallback(callback, rawData, size);
69 }
70 
TestOutput1(sptr<PhotoOutput> output,uint8_t * rawData,size_t size)71 void TestOutput1(sptr<PhotoOutput> output, uint8_t *rawData, size_t size)
72 {
73     MEDIA_INFO_LOG("PhotoOutputFuzzer: ENTER");
74     MessageParcel data;
75     data.WriteRawData(rawData, size);
76     output->SetCallback(make_shared<PhotoStateCallbackMock>());
77     data.RewindRead(0);
78     output->SetThumbnail(data.ReadBool());
79     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
80     CHECK_RETURN_ELOG(!photoSurface, "PhotoOutputFuzzer: Create photoSurface Error");
81     sptr<IBufferProducer> producer = photoSurface->GetProducer();
82     CHECK_RETURN_ELOG(!producer, "PhotoOutputFuzzer: GetProducer Error");
83     output->Capture(make_shared<PhotoCaptureSetting>());
84     output->Capture();
85     output->CancelCapture();
86     output->ConfirmCapture();
87     output->CreateStream();
88     output->GetApplicationCallback();
89     output->IsMirrorSupported();
90     output->IsQuickThumbnailSupported();
91     int32_t type = data.ReadInt32() % (DeferredDeliveryImageType::DELIVERY_VIDEO + CONST_2);
92     output->DeferImageDeliveryFor(static_cast<DeferredDeliveryImageType>(type));
93     output->IsDeferredImageDeliverySupported(static_cast<DeferredDeliveryImageType>(type));
94     output->IsDeferredImageDeliveryEnabled(static_cast<DeferredDeliveryImageType>(type));
95     data.RewindRead(0);
96     output->SetCallbackFlag(data.ReadUint8());
97     data.RewindRead(0);
98     output->SetNativeSurface(data.ReadBool());
99     data.RewindRead(0);
100     output->ProcessSnapshotDurationUpdates(data.ReadInt32());
101     int32_t isAutoHighQualityPhotoSupported;
102     output->IsAutoHighQualityPhotoSupported(isAutoHighQualityPhotoSupported);
103     data.RewindRead(0);
104     output->EnableAutoHighQualityPhoto(data.ReadBool());
105     output->IsEnableDeferred();
106     output->GetDefaultCaptureSetting();
107     bool isAutoAigcPhotoSupported;
108     output->IsAutoAigcPhotoSupported(isAutoAigcPhotoSupported);
109     data.RewindRead(0);
110     output->EnableAutoHighQualityPhoto(data.ReadBool());
111     output->Release();
112 }
113 
TestOutput2(sptr<PhotoOutput> output,uint8_t * rawData,size_t size)114 void TestOutput2(sptr<PhotoOutput> output, uint8_t *rawData, size_t size) __attribute__((no_sanitize("cfi")))
115 {
116     MEDIA_INFO_LOG("PhotoOutputFuzzer: ENTER");
117     MessageParcel data;
118     data.WriteRawData(rawData, size);
119     output->SetCallback(make_shared<PhotoStateCallbackMock>());
120     data.RewindRead(0);
121     output->IsYuvOrHeifPhoto();
122     output->SetAuxiliaryPhotoHandle(data.ReadInt32());
123     output->GetAuxiliaryPhotoHandle();
124     data.RewindRead(0);
125     output->SetMovingPhotoVideoCodecType(data.ReadInt32());
126     output->IsDepthDataDeliverySupported();
127     data.RewindRead(0);
128     output->EnableMovingPhoto(data.ReadBool());
129     data.RewindRead(0);
130     output->EnableDepthDataDelivery(data.ReadBool());
131     output->CreateMultiChannel();
132     data.RewindRead(0);
133     data.RewindRead(0);
134     output->EnableRawDelivery(data.ReadBool());
135     data.RewindRead(0);
136     output->EnableMirror(data.ReadBool());
137     bool isEnable = static_cast<bool>(rawData);
138     output->IsRawDeliverySupported(isEnable);
139     bool isAutoCloudImageEnhancementSupported = static_cast<bool>(rawData);
140     output->IsAutoCloudImageEnhancementSupported(isAutoCloudImageEnhancementSupported);
141     bool isAutoAigcPhotoSupported = static_cast<bool>(rawData);
142     output->IsAutoAigcPhotoSupported(isAutoAigcPhotoSupported);
143     data.RewindRead(0);
144     output->EnableAutoCloudImageEnhancement(data.ReadBool());
145     data.RewindRead(0);
146     output->GetPhotoRotation(data.ReadInt32());
147     data.RewindRead(0);
148     output->EnableAutoAigcPhoto(data.ReadBool());
149     pid_t pid = *reinterpret_cast<const pid_t*>(rawData);
150     output->CameraServerDied(pid);
151     output->Release();
152 }
153 
CaptureSetting(std::shared_ptr<PhotoCaptureSetting> setting,uint8_t * rawData,size_t size)154 void CaptureSetting(std::shared_ptr<PhotoCaptureSetting> setting, uint8_t *rawData, size_t size)
155 {
156     MEDIA_INFO_LOG("PhotoOutputFuzzer: ENTER");
157     MessageParcel data;
158     data.WriteRawData(rawData, size);
159     PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QUALITY_LEVEL_HIGH;
160     setting->SetQuality(quality);
161     setting->GetQuality();
162     data.RewindRead(0);
163     setting->SetBurstCaptureState(data.ReadInt8());
164     PhotoCaptureSetting::RotationConfig rotationValue = PhotoCaptureSetting::RotationConfig::Rotation_0;
165     setting->SetRotation(rotationValue);
166     setting->GetRotation();
167     data.RewindRead(0);
168     setting->SetGpsLocation(data.ReadDouble(), data.ReadDouble());
169     data.RewindRead(0);
170     setting->SetMirror(data.ReadBool());
171     setting->GetMirror();
172 }
173 
CaptureCallback(sptr<HStreamCaptureCallbackImpl> callback,uint8_t * rawData,size_t size)174 void CaptureCallback(sptr<HStreamCaptureCallbackImpl> callback, uint8_t *rawData, size_t size)
175 {
176     MEDIA_INFO_LOG("PhotoOutputFuzzer: ENTER");
177     MessageParcel data;
178     data.WriteRawData(rawData, size);
179     data.RewindRead(0);
180     callback->OnCaptureStarted(data.ReadInt32());
181     data.RewindRead(0);
182     callback->OnCaptureStarted(data.ReadInt32(), data.ReadInt32());
183     data.RewindRead(0);
184     callback->OnCaptureEnded(data.ReadInt32(), data.ReadInt32());
185     data.RewindRead(0);
186     callback->OnCaptureError(data.ReadInt32(), data.ReadInt32());
187     data.RewindRead(0);
188     callback->OnFrameShutter(data.ReadInt32(), data.ReadInt32());
189     data.RewindRead(0);
190     callback->OnFrameShutterEnd(data.ReadInt32(), data.ReadInt32());
191     data.RewindRead(0);
192     callback->OnCaptureReady(data.ReadInt32(), data.ReadInt32());
193 }
194 
195 } // namespace PhotoOutputFuzzer
196 } // namespace CameraStandard
197 } // namespace OHOS
198 
199 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)200 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
201     /* Run your code on data */
202     OHOS::CameraStandard::PhotoOutputFuzzer::Test(data, size);
203     return 0;
204 }