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 "audiomanager_fuzzer.h"
16 #include "v5_0/iaudio_manager.h"
17 #include "v5_0/audio_types.h"
18 #include "hdi_service_common.h"
19
20 using namespace std;
21 namespace OHOS {
22 namespace Audio {
23 constexpr size_t THRESHOLD = 200;
24 constexpr int32_t OFFSET = 4;
25 enum ManagerCmdId {
26 AUDIO_MANAGER_LOAD_ADAPTER,
27 AUDIO_MANAGER_UNLOAD_ADAPTER,
28 };
Convert2Uint32(const uint8_t * ptr)29 static uint32_t Convert2Uint32(const uint8_t *ptr)
30 {
31 if (ptr == nullptr) {
32 return 0;
33 }
34 /*
35 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
36 * and the third digit no left
37 */
38 return (ptr[BUFFER_INDEX_ZERO] << PCM_24_BIT) | (ptr[BUFFER_INDEX_ONE] << PCM_16_BIT) |
39 (ptr[BUFFER_INDEX_TWO] << PCM_8_BIT) | (ptr[BUFFER_INDEX_THREE]);
40 }
41
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)42 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
43 {
44 if (rawData == nullptr) {
45 return false;
46 }
47 uint32_t cmd = Convert2Uint32(rawData);
48 rawData = rawData + OFFSET;
49 struct IAudioManager *manager = IAudioManagerGet(false);
50 if (manager == nullptr) {
51 return false;
52 }
53 uint8_t *data = const_cast<uint8_t *>(rawData);
54 switch (cmd) {
55 case AUDIO_MANAGER_LOAD_ADAPTER: {
56 struct AudioPort port = {
57 .dir = *(reinterpret_cast<AudioPortDirection *>(data)),
58 .portId = *(reinterpret_cast<uint32_t *>(data)),
59 .portName = reinterpret_cast<char *>(data),
60 };
61 struct AudioAdapterDescriptor desc = {
62 .adapterName = reinterpret_cast<char *>(data),
63 .ports = &port,
64 };
65 struct IAudioAdapter *adapter = nullptr;
66 manager->LoadAdapter(manager, &desc, &adapter);
67 break;
68 }
69 case AUDIO_MANAGER_UNLOAD_ADAPTER:
70 manager->UnloadAdapter(manager, reinterpret_cast<char *>(data));
71 break;
72 default:
73 break;
74 }
75 IAudioManagerRelease(manager, false);
76 return true;
77 }
78 }
79 }
80
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83 if (size < OHOS::Audio::THRESHOLD) {
84 return 0;
85 }
86 OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size);
87 return 0;
88 }