• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "securec.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include <iostream>
20 #include <memory>
21 #include "audio_adapter.h"
22 #include "audio_system_manager.h"
23 #include "avsession_log.h"
24 #include "avsession_errors.h"
25 #include "audioadapterinterface_fuzzer.h"
26 
27 using namespace std;
28 using namespace OHOS;
29 using namespace OHOS::AVSession;
30 using OHOS::AudioStandard::AudioRendererChangeInfo;
31 
32 static const int32_t TEST_CLIENT_UID = 1;
33 static const int32_t TEST_CLIENT_PID = 1;
34 static const int32_t TEST_SESSION_ID = 2;
35 
36 static const int32_t MIN_SIZE_NUM = 10;
37 static const uint8_t *RAW_DATA = nullptr;
38 static size_t g_totalSize = 0;
39 static size_t g_sizePos;
40 
41 namespace {
42     /*
43     * describe: get data from FUZZ untrusted data(RAW_DATA) which size is according to sizeof(T)
44     * tips: only support basic type
45     */
46     template<class T>
GetData()47     T GetData()
48     {
49         T object {};
50         size_t objectSize = sizeof(object);
51         if (RAW_DATA == nullptr || objectSize > g_totalSize - g_sizePos) {
52             return object;
53         }
54         errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_sizePos, objectSize);
55         if (ret != EOK) {
56             return {};
57         }
58         g_sizePos += objectSize;
59         return object;
60     }
61 
62     template<class T>
GetArrLength(T & arr)63     uint32_t GetArrLength(T& arr)
64     {
65         if (arr == nullptr) {
66             SLOGE("%{public}s: The array length is equal to 0", __func__);
67             return 0;
68         }
69         return sizeof(arr) / sizeof(arr[0]);
70     }
71 
72     typedef void (*TestFuncs[1])();
73 
74     TestFuncs g_allFuncs = {
75         AudioAdapterTest
76     };
77 
FuzzTest(const uint8_t * rawData,size_t size)78     bool FuzzTest(const uint8_t* rawData, size_t size)
79     {
80         if (rawData == nullptr) {
81             return false;
82         }
83 
84         // initialize data
85         RAW_DATA = rawData;
86         g_totalSize = size;
87         g_sizePos = 0;
88 
89         uint32_t code = GetData<uint32_t>();
90         uint32_t len = GetArrLength(g_allFuncs);
91         if (len > 0) {
92             g_allFuncs[code % len]();
93         } else {
94             SLOGE("%{public}s: The len length is equal to 0", __func__);
95         }
96 
97         return true;
98     }
99 }
100 
AudioAdapterTest()101 void OHOS::AVSession::AudioAdapterTest()
102 {
103     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
104     info->clientUID = TEST_CLIENT_UID;
105     info->clientPid = TEST_CLIENT_PID;
106     info->sessionId = TEST_SESSION_ID;
107     info->rendererState = AudioStandard::RENDERER_RELEASED;
108     AudioRendererChangeInfos infosExpected;
109     infosExpected.push_back(std::move(info));
110     DeviceChangeAction deviceChangeAction;
111     int32_t uid = GetData<int32_t>();
112     int32_t pid = GetData<int32_t>();
113     OHOS::AudioStandard::StreamUsage streamUsage {};
114 
115     AudioAdapter audioAdapter;
116     audioAdapter.MuteAudioStream(uid, pid);
117     audioAdapter.MuteAudioStream(TEST_CLIENT_UID, TEST_CLIENT_PID);
118     audioAdapter.UnMuteAudioStream(uid);
119     audioAdapter.UnMuteAudioStream(TEST_CLIENT_UID);
120     audioAdapter.GetRendererRunning(uid);
121     audioAdapter.PauseAudioStream(uid, streamUsage);
122     audioAdapter.OnRendererStateChange(infosExpected);
123 }
124 
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127 {
128     if (size < MIN_SIZE_NUM) {
129         return 0;
130     }
131     /* Run your code on data */
132     FuzzTest(data, size);
133     return 0;
134 }