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