1 /*
2 * Copyright (c) 2024 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 "capture_output_fuzzer.h"
17 #include "camera_input.h"
18 #include "camera_log.h"
19 #include "camera_photo_proxy.h"
20 #include "capture_input.h"
21 #include "capture_output.h"
22 #include "capture_scene_const.h"
23 #include "input/camera_manager.h"
24 #include "message_parcel.h"
25 #include "refbase.h"
26 #include <cstddef>
27 #include <cstdint>
28 #include <memory>
29 #include "token_setproc.h"
30 #include "nativetoken_kit.h"
31 #include "accesstoken_kit.h"
32 #include "securec.h"
33
34 namespace OHOS {
35 namespace CameraStandard {
36 static constexpr int32_t MIN_SIZE_NUM = 8;
37
38 std::shared_ptr<CaptureOutput> CaptureOutputFuzzer::fuzz_{nullptr};
39
40 /*
41 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
42 * tips: only support basic type
43 */
CaptureOutputFuzzTest(FuzzedDataProvider & fdp)44 void CaptureOutputFuzzer::CaptureOutputFuzzTest(FuzzedDataProvider& fdp)
45 {
46 int32_t randomInt = fdp.ConsumeIntegral<int32_t>();
47 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
48 CHECK_RETURN_ELOG(photoSurface, "PhotoOutputFuzzer: create photoSurface Error");
49 sptr<IBufferProducer> bufferProducer = photoSurface->GetProducer();
50 CHECK_RETURN_ELOG(bufferProducer, "GetProducer Error");
51 sptr<IStreamCommon> stream;
52 CaptureOutputType randomOutputType = static_cast<CaptureOutputType>(
53 (randomInt % static_cast<int32_t>(CAPTURE_OUTPUT_TYPE_MAX)));
54 fuzz_ = std::make_shared<CaptureOutputTest>(randomOutputType, StreamType::CAPTURE, bufferProducer, stream);
55 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
56 fuzz_->GetBufferProducer();
57 int32_t pid = fdp.ConsumeIntegral<int32_t>();
58 fuzz_->OnCameraServerDied(pid);
59 fuzz_->IsStreamCreated();
60 DepthProfile depthProfile;
61 fuzz_->SetDepthProfile(depthProfile);
62 fuzz_->GetDepthProfile();
63 fuzz_->ClearProfiles();
64 fuzz_->AddTag(CaptureOutput::Tag::DYNAMIC_PROFILE);
65 fuzz_->RemoveTag(CaptureOutput::Tag::DYNAMIC_PROFILE);
66 }
67
Test(uint8_t * data,size_t size)68 void Test(uint8_t* data, size_t size)
69 {
70 auto captureOutput = std::make_unique<CaptureOutputFuzzer>();
71 if (captureOutput == nullptr) {
72 MEDIA_INFO_LOG("captureOutput is null");
73 return;
74 }
75 FuzzedDataProvider fdp(data, size);
76 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
77 return;
78 }
79 captureOutput->CaptureOutputFuzzTest(fdp);
80 }
81 } // namespace CameraStandard
82 } // namespace OHOS
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
86 {
87 OHOS::CameraStandard::Test(data, size);
88 return 0;
89 }