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 "stream_repeat_proxy_fuzzer.h"
17 #include "camera_log.h"
18 #include "iconsumer_surface.h"
19 #include "securec.h"
20 #include "system_ability_definition.h"
21 #include "iservice_registry.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 const size_t THRESHOLD = 10;
26 std::shared_ptr<StreamRepeatProxy> StreamRepeatProxyFuzz::fuzz_{nullptr};
27
StreamRepeatProxyFuzzTest(FuzzedDataProvider & fdp)28 void StreamRepeatProxyFuzz::StreamRepeatProxyFuzzTest(FuzzedDataProvider &fdp)
29 {
30 auto mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
31 if (mgr == nullptr) {
32 return;
33 }
34 auto object = mgr->GetSystemAbility(CAMERA_SERVICE_ID);
35 if (object == nullptr) {
36 return;
37 }
38 fuzz_ = std::make_shared<StreamRepeatProxy>(object);
39 CHECK_RETURN_ELOG(!fuzz_, "fuzz_ nullptr");
40 int32_t minFrameRate = fdp.ConsumeIntegral<int32_t>();
41 int32_t maxFrameRate = fdp.ConsumeIntegral<int32_t>();
42 fuzz_->SetFrameRate(minFrameRate, maxFrameRate);
43 bool isEnable = fdp.ConsumeIntegral<int32_t>() ? 1 : 0;
44 fuzz_->EnableSecure(isEnable);
45 fuzz_->SetMirror(isEnable);
46 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
47 CHECK_RETURN_ELOG(photoSurface, "PhotoOutputFuzzer: create photoSurface Error");
48 sptr<IBufferProducer> bufferProducer = photoSurface->GetProducer();
49 CHECK_RETURN_ELOG(bufferProducer, "GetProducer Error");
50 int32_t videoMetaType = fdp.ConsumeIntegral<int32_t>();
51 fuzz_->AttachMetaSurface(bufferProducer, videoMetaType);
52 int32_t rotation = fdp.ConsumeIntegral<int32_t>();
53 fuzz_->SetCameraRotation(isEnable, rotation);
54 fuzz_->GetMirror(isEnable);
55 fuzz_->UnSetCallback();
56 }
57
FuzzTest(const uint8_t * rawData,size_t size)58 void FuzzTest(const uint8_t *rawData, size_t size)
59 {
60 FuzzedDataProvider fdp(rawData, size);
61 auto streamRepeatProxy = std::make_unique<StreamRepeatProxyFuzz>();
62 if (streamRepeatProxy == nullptr) {
63 MEDIA_INFO_LOG("streamRepeatProxy is null");
64 return;
65 }
66 streamRepeatProxy->StreamRepeatProxyFuzzTest(fdp);
67 }
68 } // namespace CameraStandard
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
73 {
74 if (size < OHOS::CameraStandard::THRESHOLD) {
75 return 0;
76 }
77
78 OHOS::CameraStandard::FuzzTest(data, size);
79 return 0;
80 }