• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "avcodev_task_manager_fuzzer.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include "camera_log.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 using namespace DeferredProcessing;
24 static constexpr int32_t MAX_CODE_LEN  = 512;
25 static constexpr int32_t MIN_SIZE_NUM = 4;
26 static const uint8_t* RAW_DATA = nullptr;
27 const size_t THRESHOLD = 10;
28 static size_t g_dataSize = 0;
29 static size_t g_pos;
30 std::shared_ptr<AvcodecTaskManager> AvcodecTaskManagerFuzzer::fuzz_{nullptr};
31 
32 /*
33 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
34 * tips: only support basic type
35 */
36 template<class T>
GetData()37 T GetData()
38 {
39     T object {};
40     size_t objectSize = sizeof(object);
41     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
42         return object;
43     }
44     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
45     if (ret != EOK) {
46         return {};
47     }
48     g_pos += objectSize;
49     return object;
50 }
51 
52 template<class T>
GetArrLength(T & arr)53 uint32_t GetArrLength(T& arr)
54 {
55     if (arr == nullptr) {
56         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
57         return 0;
58     }
59     return sizeof(arr) / sizeof(arr[0]);
60 }
61 
AvcodecTaskManagerFuzzTest()62 void AvcodecTaskManagerFuzzer::AvcodecTaskManagerFuzzTest()
63 {
64     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
65         return;
66     }
67     sptr<AudioCapturerSession> session = new AudioCapturerSession();
68     VideoCodecType type = VideoCodecType::VIDEO_ENCODE_TYPE_AVC;
69     ColorSpace colorSpace = ColorSpace::DISPLAY_P3;
70     fuzz_ = std::make_shared<AvcodecTaskManager>(session, type, colorSpace);
71     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
72     fuzz_->GetTaskManager();
73     fuzz_->GetEncoderManager();
74     int64_t timestamp = GetData<int64_t>();
75     GraphicTransformType type_ = GRAPHIC_ROTATE_90;
76     sptr<SurfaceBuffer> videoBuffer = SurfaceBuffer::Create();
77     sptr<FrameRecord> frameRecord =
78         new(std::nothrow) FrameRecord(videoBuffer, timestamp, type_);
79     function<void()> task = []() {};
80     fuzz_->SubmitTask(task);
81     std::shared_ptr<PhotoAssetIntf> photoAssetProxy = nullptr;
82     int32_t captureId = GetData<int32_t>();
83     int32_t captureRotation = GetData<int32_t>();
84     uint64_t taskName = GetData<uint64_t>();
85     fuzz_->SetVideoFd(timestamp, photoAssetProxy, captureId);
86     vector<sptr<FrameRecord>> frameRecords;
87     fuzz_->DoMuxerVideo(frameRecords, taskName, captureRotation, captureId);
88     vector<sptr<FrameRecord>> choosedBuffer;
89     int64_t shutterTime = GetData<int64_t>();
90     fuzz_->ChooseVideoBuffer(frameRecords, choosedBuffer, shutterTime, captureId);
91     vector<sptr<AudioRecord>> audioRecordVec;
92     sptr<AudioVideoMuxer> muxer;
93     fuzz_->CollectAudioBuffer(audioRecordVec, muxer);
94     fuzz_->videoEncoder_ = nullptr;
95     fuzz_->audioEncoder_ = make_unique<AudioEncoder>();
96     fuzz_->Stop();
97 }
98 
Test()99 void Test()
100 {
101     auto avcodecTaskManager = std::make_unique<AvcodecTaskManagerFuzzer>();
102     if (avcodecTaskManager == nullptr) {
103         MEDIA_INFO_LOG("avcodecTaskManager is null");
104         return;
105     }
106     avcodecTaskManager->AvcodecTaskManagerFuzzTest();
107 }
108 
109 typedef void (*TestFuncs[1])();
110 
111 TestFuncs g_testFuncs = {
112     Test,
113 };
114 
FuzzTest(const uint8_t * rawData,size_t size)115 bool FuzzTest(const uint8_t* rawData, size_t size)
116 {
117     // initialize data
118     RAW_DATA = rawData;
119     g_dataSize = size;
120     g_pos = 0;
121 
122     uint32_t code = GetData<uint32_t>();
123     uint32_t len = GetArrLength(g_testFuncs);
124     if (len > 0) {
125         g_testFuncs[code % len]();
126     } else {
127         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
128     }
129 
130     return true;
131 }
132 } // namespace CameraStandard
133 } // namespace OHOS
134 
135 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)136 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
137 {
138     if (size < OHOS::CameraStandard::THRESHOLD) {
139         return 0;
140     }
141 
142     OHOS::CameraStandard::FuzzTest(data, size);
143     return 0;
144 }