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 "mpeg_manager_fuzzer.h"
17 #include "message_parcel.h"
18 #include "ipc_file_descriptor.h"
19 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace DeferredProcessing;
25 static constexpr int32_t MIN_SIZE_NUM = 64;
26 const size_t MAX_LENGTH_STRING = 64;
27 std::shared_ptr<MpegManager> MpegManagerFuzzer::fuzz_{nullptr};
28
MpegManagerFuzzTest(FuzzedDataProvider & fdp)29 void MpegManagerFuzzer::MpegManagerFuzzTest(FuzzedDataProvider& fdp)
30 {
31 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
32 return;
33 }
34 fuzz_ = std::make_shared<MpegManager>();
35 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
36 MediaResult result1 = MediaResult::FAIL;
37 MediaResult result2 = MediaResult::PAUSE;
38 fuzz_->UnInit(result1);
39 fuzz_->UnInit(result2);
40 fuzz_->GetSurface();
41 fuzz_->GetMakerSurface();
42 fuzz_->GetProcessTimeStamp();
43 fuzz_->GetResultFd();
44 fuzz_->InitVideoCodec();
45 fuzz_->UnInitVideoCodec();
46 fuzz_->InitVideoMakerSurface();
47 fuzz_->UnInitVideoMaker();
48 std::vector<uint8_t> memoryFlags = {
49 static_cast<uint8_t>(MemoryFlag::MEMORY_READ_ONLY),
50 static_cast<uint8_t>(MemoryFlag::MEMORY_WRITE_ONLY),
51 static_cast<uint8_t>(MemoryFlag::MEMORY_READ_WRITE)
52 };
53 uint8_t randomIndex = fdp.ConsumeIntegral<uint8_t>() % memoryFlags.size();
54 MemoryFlag selectedFlag = static_cast<MemoryFlag>(memoryFlags[randomIndex]);
55 std::shared_ptr<AVAllocator> avAllocator = AVAllocatorFactory::CreateSharedAllocator(selectedFlag);
56 fuzz_->OnMakerBufferAvailable();
57 int64_t timestamp = fdp.ConsumeIntegral<int64_t>();
58 fuzz_->AcquireMakerBuffer(timestamp);
59 int flags = 1;
60 std::string requestId(fdp.ConsumeRandomLengthString(MAX_LENGTH_STRING));
61 fuzz_->GetFileFd(requestId, flags, "_vid_temp");
62 fuzz_->GetFileFd(requestId, flags, "_vid");
63 }
64
Test(uint8_t * data,size_t size)65 void Test(uint8_t* data, size_t size)
66 {
67 FuzzedDataProvider fdp(data, size);
68 auto mpegManager = std::make_unique<MpegManagerFuzzer>();
69 if (mpegManager == nullptr) {
70 MEDIA_INFO_LOG("mpegManager is null");
71 return;
72 }
73 mpegManager->MpegManagerFuzzTest(fdp);
74 }
75
76 } // namespace CameraStandard
77 } // namespace OHOS
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
81 {
82 OHOS::CameraStandard::Test(data, size);
83 return 0;
84 }