• 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 #include "audioadapter_fuzzer.h"
16 #include "hdi_service_common.h"
17 
18 namespace OHOS {
19 namespace Audio {
20 
21 constexpr size_t THRESHOLD = 10;
22 constexpr int32_t OFFSET = 4;
23 enum AdapterCmdId {
24     AUDIO_ADAPTER_CREAT_RENDER,
25     AUDIO_ADAPTER_DESTORY_RENDER,
26     AUDIO_ADAPTER_CREAT_CAPTURE,
27     AUDIO_ADAPTER_DESTORY_CAPTURE,
28     AUDIO_ADAPTER_GET_PORT_CAPABILITY,
29     AUDIO_ADAPTER_SET_PASSTHROUGH_MODE,
30     AUDIO_ADAPTER_GET_PASSTHROUGH_MODE,
31 };
Convert2Uint32(const uint8_t * ptr)32 static uint32_t Convert2Uint32(const uint8_t *ptr)
33 {
34     if (ptr == nullptr) {
35         return 0;
36     }
37     /*
38      * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
39      * and the third digit no left
40      */
41     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
42 }
43 
AdapterFucSwitch(struct IAudioAdapter * & adapter,uint32_t cmd,const uint8_t * & rawData)44 static void AdapterFucSwitch(struct IAudioAdapter *&adapter, uint32_t cmd, const uint8_t *&rawData)
45 {
46     uint8_t *data = const_cast<uint8_t *>(rawData);
47     switch (cmd) {
48         case AUDIO_ADAPTER_CREAT_RENDER: {
49             struct IAudioRender *render = nullptr;
50             adapter->CreateRender(adapter, reinterpret_cast<const struct AudioDeviceDescriptor *>(rawData),
51                                   reinterpret_cast<const struct AudioSampleAttributes *>(rawData), &render);
52             break;
53         }
54         case AUDIO_ADAPTER_DESTORY_RENDER:
55             adapter->DestroyRender(adapter, reinterpret_cast<const struct AudioDeviceDescriptor *>(rawData));
56             break;
57         case AUDIO_ADAPTER_CREAT_CAPTURE: {
58             struct IAudioCapture *capture = nullptr;
59             adapter->CreateCapture(adapter, reinterpret_cast<const struct AudioDeviceDescriptor *>(rawData),
60                                    reinterpret_cast<const struct AudioSampleAttributes *>(rawData), &capture);
61             break;
62         }
63         case AUDIO_ADAPTER_DESTORY_CAPTURE:
64             adapter->DestroyCapture(adapter, reinterpret_cast<const struct AudioDeviceDescriptor *>(rawData));
65             break;
66         case AUDIO_ADAPTER_GET_PORT_CAPABILITY: {
67             struct AudioPortCapability capability = {};
68             adapter->GetPortCapability(adapter, reinterpret_cast<const struct AudioPort *>(rawData), &capability);
69             break;
70         }
71         case AUDIO_ADAPTER_SET_PASSTHROUGH_MODE: {
72             struct AudioPort port = {
73                 .dir = *(reinterpret_cast<AudioPortDirection *>(data)),
74                 .portId = *(reinterpret_cast<uint32_t *>(data)),
75                 .portName = reinterpret_cast<char *>(data),
76             };
77             adapter->SetPassthroughMode(adapter, &port, *(reinterpret_cast<const AudioPortPassthroughMode *>(rawData)));
78             break;
79         }
80         case AUDIO_ADAPTER_GET_PASSTHROUGH_MODE: {
81             AudioPortPassthroughMode mode = PORT_PASSTHROUGH_LPCM;
82             struct AudioPort port = {
83                 .dir = *(reinterpret_cast<AudioPortDirection *>(data)),
84                 .portId = *(reinterpret_cast<uint32_t *>(data)),
85                 .portName = reinterpret_cast<char *>(data),
86             };
87             adapter->GetPassthroughMode(adapter, &port, &mode);
88             break;
89         }
90         default:
91             return;
92     }
93 }
94 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)95 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
96 {
97     if (rawData == nullptr) {
98         return false;
99     }
100     uint32_t cmd = Convert2Uint32(rawData);
101     rawData = rawData + OFFSET;
102     size = size - OFFSET;
103     struct IAudioManager *manager = IAudioManagerGet(true);
104     if (manager == nullptr) {
105         return false;
106     }
107     struct AudioPort audioPort = {};
108     struct IAudioAdapter *adapter = nullptr;
109     int32_t ret = GetLoadAdapter(manager, PORT_OUT, ADAPTER_NAME, &adapter, audioPort);
110     if (ret != HDF_SUCCESS) {
111         return false;
112     }
113     AdapterFucSwitch(adapter, cmd, rawData);
114     manager->UnloadAdapter(manager, ADAPTER_NAME.c_str());
115     IAudioManagerRelease(manager, true);
116     return true;
117 }
118 
119 }
120 }
121 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)122 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
123 {
124     if (size < OHOS::Audio::THRESHOLD) {
125         return 0;
126     }
127     OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size);
128     return 0;
129 }