• 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 "audio_deferred_process_fuzzer.h"
17 
18 #include "camera_log.h"
19 #include "camera_util.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 
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 
31 /*
32 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
33 * tips: only support basic type
34 */
35 template<class T>
GetData()36 T GetData()
37 {
38     T object {};
39     size_t objectSize = sizeof(object);
40     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
41         return object;
42     }
43     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
44     if (ret != EOK) {
45         return {};
46     }
47     g_pos += objectSize;
48     return object;
49 }
50 
51 template<class T>
GetArrLength(T & arr)52 uint32_t GetArrLength(T& arr)
53 {
54     if (arr == nullptr) {
55         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
56         return 0;
57     }
58     return sizeof(arr) / sizeof(arr[0]);
59 }
60 
AudioDeferredProcessFuzzerTest()61 void AudioDeferredProcessFuzzer::AudioDeferredProcessFuzzerTest()
62 {
63     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
64         return;
65     }
66     audioDeferredProcess_ = std::make_shared<AudioDeferredProcess>();
67     CHECK_ERROR_RETURN_LOG(!audioDeferredProcess_, "Create audioDeferredProcess_ Error");
68 
69     AudioStreamInfo inputOptions;
70     AudioStreamInfo outputOptions;
71     audioDeferredProcess_->GetOfflineEffectChain();
72     audioDeferredProcess_->StoreOptions(inputOptions, outputOptions);
73     audioDeferredProcess_->GetOneUnprocessedSize();
74     vector<sptr<AudioRecord>> audioRecords{};
75     vector<sptr<AudioRecord>> processedRecords{};
76     audioDeferredProcess_->Process(audioRecords, processedRecords);
77     audioDeferredProcess_->Release();
78 }
79 
Test()80 void Test()
81 {
82     auto audioDeferredProcessFuzzer = std::make_shared<AudioDeferredProcessFuzzer>();
83     if (audioDeferredProcessFuzzer == nullptr) {
84         MEDIA_INFO_LOG("audioCapturerSessionFuzzer is null");
85         return;
86     }
87     audioDeferredProcessFuzzer->AudioDeferredProcessFuzzerTest();
88 }
89 
90 typedef void (*TestFuncs[1])();
91 
92 TestFuncs g_testFuncs = {
93     Test,
94 };
95 
FuzzTest(const uint8_t * rawData,size_t size)96 bool FuzzTest(const uint8_t* rawData, size_t size)
97 {
98     // initialize data
99     RAW_DATA = rawData;
100     g_dataSize = size;
101     g_pos = 0;
102 
103     uint32_t code = GetData<uint32_t>();
104     uint32_t len = GetArrLength(g_testFuncs);
105     if (len > 0) {
106         g_testFuncs[code % len]();
107     } else {
108         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
109     }
110 
111     return true;
112 }
113 } // namespace CameraStandard
114 } // namespace OHOS
115 
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
118 {
119     if (size < OHOS::CameraStandard::THRESHOLD) {
120         return 0;
121     }
122 
123     OHOS::CameraStandard::FuzzTest(data, size);
124     return 0;
125 }